1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
10 #include <rte_lcore.h>
11 #include <rte_metrics.h>
15 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
16 #define REG_METRIC_COUNT 6
17 #define METRIC_LESSER_COUNT 3
21 /* Initializes metric module. This function must be called
22 * from a primary process before metrics are used
25 test_metrics_init(void)
27 rte_metrics_init(rte_socket_id());
31 /* Deinitialize metric module. This function must be called
32 * from a primary process after metrics usage is over
35 test_metrics_deinitialize(void)
38 err = rte_metrics_deinit();
39 TEST_ASSERT(err == 0, "%s, %d", __func__, __LINE__);
44 /* Test Case to check failures when memzone init is not done */
46 test_metrics_without_init(void)
49 const uint64_t value[REG_METRIC_COUNT] = {0};
50 const char * const mnames[] = {
51 "mean_bits_in", "mean_bits_out",
52 "peak_bits_in", "peak_bits_out",
55 /* Failure Test: Checking for memzone initialization */
56 err = rte_metrics_reg_name("peak_bits_in");
57 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
59 err = rte_metrics_reg_names(&mnames[0], 1);
60 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
62 err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY, VALUE);
63 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
65 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, KEY, &value[0], 4);
66 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
68 err = rte_metrics_get_names(NULL, 0);
69 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
71 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, NULL, 0);
72 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
77 /* Test Case to validate registering a single metric */
79 test_metrics_reg_name_with_validname(void)
83 /* Test to register the new metric name */
84 err = rte_metrics_reg_name("peak_bits_out");
85 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
87 /* Test to register the same metric name */
88 err = rte_metrics_reg_name("peak_bits_out");
89 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
91 /* Test case to validate registering a invalid metric */
92 err = rte_metrics_reg_name(NULL);
93 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
98 /* Test case to validate registering a list of valid metric names */
100 test_metrics_reg_names(void)
103 const char * const mnames[] = {
104 "mean_bits_in", "mean_bits_out",
105 "peak_bits_in", "peak_bits_out",
108 /* Success Test: valid array and count size */
109 err = rte_metrics_reg_names(&mnames[0], ARRAY_SIZE(mnames));
110 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
115 /* Test case to validate update a metric */
117 test_metrics_update_value(void)
121 /* Successful Test: Valid port_id, key and value */
122 err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY, VALUE);
123 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
125 /* Successful Test: Valid port_id otherthan RTE_METRICS_GLOBAL, key
128 err = rte_metrics_update_value(9, KEY, VALUE);
129 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
131 /* Failed Test: Invalid port_id with lower value */
132 err = rte_metrics_update_value(-2, KEY, VALUE);
133 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
135 /* Failed Test: Invalid port_id with higher value */
136 err = rte_metrics_update_value(39, KEY, VALUE);
137 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
139 /* Failed Test: valid port id, value with invalid key */
140 err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY+12, VALUE);
141 TEST_ASSERT(err < 0, "%s, %d", __func__, __LINE__);
146 /* Test case to validate update a list of metrics */
148 test_metrics_update_values(void)
151 const uint64_t value[REG_METRIC_COUNT] = {1, 2, 3, 4, 5, 6};
153 /* Successful Test: update metrics with first set */
154 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 0,
156 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
158 /* Successful Test: update metrics with second set */
159 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 1,
161 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
163 /* Successful Test: update metrics with third set */
164 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 2,
166 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
168 /* Failed Test: Invalid count size */
169 err = rte_metrics_update_values(RTE_METRICS_GLOBAL,
170 KEY, &value[0], ARRAY_SIZE(value));
171 TEST_ASSERT(err < 0, "%s, %d", __func__, __LINE__);
173 /* Failed Test: Invalid port_id(lower value) and valid data */
174 err = rte_metrics_update_values(-2, KEY, &value[0], ARRAY_SIZE(value));
175 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
177 /* Failed Test: Invalid port_id(higher value) and valid data */
178 err = rte_metrics_update_values(39, 1, &value[0], ARRAY_SIZE(value));
179 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
181 /* Failed Test: Invalid array */
182 err = rte_metrics_update_values(RTE_METRICS_GLOBAL,
183 KEY, NULL, ARRAY_SIZE(value));
184 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
189 /* Test to validate get metric name-key lookup table */
191 test_metrics_get_names(void)
194 struct rte_metric_name metrics[METRIC_LESSER_COUNT];
195 struct rte_metric_name success_metrics[REG_METRIC_COUNT];
197 /* Successful Test: Invalid array list */
198 err = rte_metrics_get_names(NULL, 0);
199 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
201 /* Successful Test: Valid array list, Correct Count Stats same
204 err = rte_metrics_get_names(success_metrics, REG_METRIC_COUNT);
205 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
207 /* Successful Test: Valid array list, Increase Count Stats than
210 err = rte_metrics_get_names(success_metrics, REG_METRIC_COUNT+5);
211 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
213 /* Successful Test, Not update results:
214 * Invalid array list, Lesser Count Stats than allocated stats
216 err = rte_metrics_get_names(metrics, METRIC_LESSER_COUNT);
217 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
222 /* Test to validate get list of metric values */
224 test_metrics_get_values(void)
228 struct rte_metric_value getvalues[REG_METRIC_COUNT];
230 size_t m_size = sizeof(struct rte_metric_value);
231 for (i = 0; i < REG_METRIC_COUNT; i++)
232 memset(&getvalues[i], 0, m_size);
234 /* Successful Test, Not update results: valid arguments
235 * count lessthan the memzone stats
237 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
238 METRIC_LESSER_COUNT);
239 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
241 /* Successful Test, update results: valid arguments */
242 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
244 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
246 /* Successful Test : valid arguments count greaterthan the
249 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
251 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
253 /* Failure Test: Invalid port_id(lower value) with correct values
256 err = rte_metrics_get_values(-2, getvalues, REG_METRIC_COUNT);
257 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
259 /* Failure Test: Invalid port_id(higher value) with correct values
262 err = rte_metrics_get_values(33, getvalues, REG_METRIC_COUNT);
263 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
265 /* Successful Test: valid port_id with incorrect values and valid
268 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, NULL,
270 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
275 static struct unit_test_suite metrics_testsuite = {
276 .suite_name = "Metrics Unit Test Suite",
280 /* Test Case 1: Test to check all metric APIs without
283 TEST_CASE(test_metrics_without_init),
285 /* TEST CASE 2: Test to register valid metrics*/
286 TEST_CASE_ST(test_metrics_init, NULL,
287 test_metrics_reg_name_with_validname),
289 /* TEST CASE 3: Test to register list of metrics with valid
290 * names and valid count size, invalid names and invalid
293 TEST_CASE(test_metrics_reg_names),
295 /* TEST CASE 4: Test to register a update value with valid port
296 * id and invalid port id
298 TEST_CASE(test_metrics_update_value),
300 /* TEST CASE 5: Test to register update list of values with
301 * valid port id, key, value, count size and invalid port id,
302 * key, value, count size
304 TEST_CASE(test_metrics_update_values),
306 /* TEST CASE 6: Test to get metric names-key with valid
307 * array list, count size and invalid array list, count size
309 TEST_CASE(test_metrics_get_names),
311 /* TEST CASE 7: Test to get list of metric values with valid
312 * port id, array list, count size and invalid port id,
313 * arraylist, count size
315 TEST_CASE(test_metrics_get_values),
317 /* TEST CASE 8: Test to unregister metrics*/
318 TEST_CASE(test_metrics_deinitialize),
327 return unit_test_suite_runner(&metrics_testsuite);
330 REGISTER_TEST_COMMAND(metrics_autotest, test_metrics);