2 * Copyright (c) 2021 EMnify.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include <vlib/vlib.h>
17 #include <vppinfra/time.h>
18 #include <vppinfra/cache.h>
19 #include <vppinfra/error.h>
21 #include <vlib/counter.h>
22 #include <vlib/stats/stats.h>
36 * Return the stats segment epoch value.
41 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
42 return sm->shared_header->epoch;
45 /* number of times to repeat the counter expand tests */
46 #define EXPAND_TEST_ROUNDS 3
49 * Let a simple counter vector grow and verify that
50 * the stats epoch is increased only when the vector
54 test_simple_counter_expand (vlib_main_t *vm)
56 vlib_simple_counter_main_t counter = {
57 .name = "test-simple-counter-expand",
58 .stat_segment_name = "/vlib/test-simple-counter-expand",
61 uint64_t epoch, new_epoch;
63 // Create one counter to allocate the vector.
64 vlib_validate_simple_counter (&counter, 0);
65 epoch = get_stats_epoch ();
67 for (i = 0; i < EXPAND_TEST_ROUNDS; i++)
69 // Check how many elements fit into the counter vector without expanding
70 // that. The next validate calls should not increase the stats segment
72 int mem_size = vec_max_len (counter.counters[0]);
73 for (index = 1; index <= mem_size - 1; index++)
75 vlib_validate_simple_counter (&counter, index);
76 new_epoch = get_stats_epoch ();
77 if (new_epoch != epoch)
78 return clib_error_return (
79 0, "Stats segment epoch should not increase");
82 // The next counter index does not fit and it will extend the vector.
83 // The stats segment epoch should increase.
84 vlib_validate_simple_counter (&counter, index + 1);
85 new_epoch = get_stats_epoch ();
86 if (new_epoch == epoch)
87 return clib_error_return (0,
88 "Stats segment epoch should have increased");
92 vlib_free_simple_counter (&counter);
93 vlib_validate_simple_counter (&counter, 0);
99 * Let a combined counter vector grow and verify that
100 * the stats epoch is increased only when the vector
103 static clib_error_t *
104 test_combined_counter_expand (vlib_main_t *vm)
106 vlib_combined_counter_main_t counter = {
107 .name = "test-combined-counter-expand",
108 .stat_segment_name = "/vlib/test-combined-counter-expand",
111 uint64_t epoch, new_epoch;
113 // Create one counter to allocate the vector.
114 vlib_validate_combined_counter (&counter, 0);
115 epoch = get_stats_epoch ();
117 for (i = 0; i < EXPAND_TEST_ROUNDS; i++)
119 // Check how many elements fit into the counter vector without expanding
120 // that. The next validate calls should not increase the stats segment
122 int mem_size = vec_max_len (counter.counters[0]);
123 for (index = 1; index <= mem_size - 1; index++)
125 vlib_validate_combined_counter (&counter, index);
126 new_epoch = get_stats_epoch ();
127 if (new_epoch != epoch)
128 return clib_error_return (
129 0, "Stats segment epoch should not increase");
132 // The next counter index does not fit and it will extend the vector.
133 // The stats segment epoch should increase.
134 vlib_validate_combined_counter (&counter, index + 1);
135 new_epoch = get_stats_epoch ();
136 if (new_epoch == epoch)
137 return clib_error_return (0,
138 "Stats segment epoch should have increased");
142 vlib_free_combined_counter (&counter);
143 vlib_validate_combined_counter (&counter, 0);
148 static clib_error_t *
149 test_simple_counter (vlib_main_t *vm, int test_case)
156 error = test_simple_counter_expand (vm);
160 return clib_error_return (0, "no such test");
166 static clib_error_t *
167 test_combined_counter (vlib_main_t *vm, int test_case)
174 error = test_combined_counter_expand (vm);
178 return clib_error_return (0, "no such test");
184 static clib_error_t *
185 test_counter_command_fn (vlib_main_t *vm, unformat_input_t *input,
186 vlib_cli_command_t *cmd)
189 int counter_type = -1;
192 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
194 if (unformat (input, "simple"))
195 counter_type = type_simple;
196 else if (unformat (input, "combined"))
197 counter_type = type_combined;
198 else if (unformat (input, "expand"))
199 test_case = test_expand;
201 return clib_error_return (0, "unknown input '%U'",
202 format_unformat_error, input);
206 return clib_error_return (0, "no such test");
208 switch (counter_type)
211 error = test_simple_counter (vm, test_case);
215 error = test_combined_counter (vm, test_case);
219 return clib_error_return (0, "no such test");
225 VLIB_CLI_COMMAND (test_counter_command, static) = {
226 .path = "test counter",
227 .short_help = "test counter [simple | combined] expand",
228 .function = test_counter_command_fn,
231 static clib_error_t *
232 test_counter_init (vlib_main_t *vm)
237 VLIB_INIT_FUNCTION (test_counter_init);
240 * fd.io coding-style-patch-verification: ON
243 * eval: (c-set-style "gnu")