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 REG_METRIC_COUNT 6
16 #define METRIC_LESSER_COUNT 3
20 /* Initializes metric module. This function must be called
21 * from a primary process before metrics are used
24 test_metrics_init(void)
26 rte_metrics_init(rte_socket_id());
30 /* Deinitialize metric module. This function must be called
31 * from a primary process after metrics usage is over
34 test_metrics_deinitialize(void)
37 err = rte_metrics_deinit();
38 TEST_ASSERT(err == 0, "%s, %d", __func__, __LINE__);
43 /* Test Case to check failures when memzone init is not done */
45 test_metrics_without_init(void)
48 const uint64_t value[REG_METRIC_COUNT] = {0};
49 const char * const mnames[] = {
50 "mean_bits_in", "mean_bits_out",
51 "peak_bits_in", "peak_bits_out",
54 /* Failure Test: Checking for memzone initialization */
55 err = rte_metrics_reg_name("peak_bits_in");
56 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
58 err = rte_metrics_reg_names(&mnames[0], 1);
59 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
61 err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY, VALUE);
62 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
64 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, KEY, &value[0], 4);
65 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
67 err = rte_metrics_get_names(NULL, 0);
68 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
70 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, NULL, 0);
71 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
76 /* Test Case to validate registering a single metric */
78 test_metrics_reg_name_with_validname(void)
82 /* Test to register the new metric name */
83 err = rte_metrics_reg_name("peak_bits_out");
84 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
86 /* Test to register the same metric name */
87 err = rte_metrics_reg_name("peak_bits_out");
88 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
90 /* Test case to validate registering a invalid metric */
91 err = rte_metrics_reg_name(NULL);
92 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
97 /* Test case to validate registering a list of valid metric names */
99 test_metrics_reg_names(void)
102 const char * const mnames[] = {
103 "mean_bits_in", "mean_bits_out",
104 "peak_bits_in", "peak_bits_out",
107 /* Success Test: valid array and count size */
108 err = rte_metrics_reg_names(&mnames[0], RTE_DIM(mnames));
109 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
114 /* Test case to validate update a metric */
116 test_metrics_update_value(void)
120 /* Successful Test: Valid port_id, key and value */
121 err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY, VALUE);
122 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
124 /* Successful Test: Valid port_id otherthan RTE_METRICS_GLOBAL, key
127 err = rte_metrics_update_value(9, KEY, VALUE);
128 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
130 /* Failed Test: Invalid port_id with lower value */
131 err = rte_metrics_update_value(-2, KEY, VALUE);
132 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
134 /* Failed Test: Invalid port_id with higher value */
135 err = rte_metrics_update_value(39, KEY, VALUE);
136 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
138 /* Failed Test: valid port id, value with invalid key */
139 err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY+12, VALUE);
140 TEST_ASSERT(err < 0, "%s, %d", __func__, __LINE__);
145 /* Test case to validate update a list of metrics */
147 test_metrics_update_values(void)
150 const uint64_t value[REG_METRIC_COUNT] = {1, 2, 3, 4, 5, 6};
152 /* Successful Test: update metrics with first set */
153 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 0,
155 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
157 /* Successful Test: update metrics with second set */
158 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 1,
160 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
162 /* Successful Test: update metrics with third set */
163 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 2,
165 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
167 /* Failed Test: Invalid count size */
168 err = rte_metrics_update_values(RTE_METRICS_GLOBAL,
169 KEY, &value[0], RTE_DIM(value));
170 TEST_ASSERT(err < 0, "%s, %d", __func__, __LINE__);
172 /* Failed Test: Invalid port_id(lower value) and valid data */
173 err = rte_metrics_update_values(-2, KEY, &value[0], RTE_DIM(value));
174 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
176 /* Failed Test: Invalid port_id(higher value) and valid data */
177 err = rte_metrics_update_values(39, 1, &value[0], RTE_DIM(value));
178 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
180 /* Failed Test: Invalid array */
181 err = rte_metrics_update_values(RTE_METRICS_GLOBAL,
182 KEY, NULL, RTE_DIM(value));
183 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
188 /* Test to validate get metric name-key lookup table */
190 test_metrics_get_names(void)
193 struct rte_metric_name metrics[METRIC_LESSER_COUNT];
194 struct rte_metric_name success_metrics[REG_METRIC_COUNT];
196 /* Successful Test: Invalid array list */
197 err = rte_metrics_get_names(NULL, 0);
198 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
200 /* Successful Test: Valid array list, Correct Count Stats same
203 err = rte_metrics_get_names(success_metrics, REG_METRIC_COUNT);
204 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
206 /* Successful Test: Valid array list, Increase Count Stats than
209 err = rte_metrics_get_names(success_metrics, REG_METRIC_COUNT+5);
210 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
212 /* Successful Test, Not update results:
213 * Invalid array list, Lesser Count Stats than allocated stats
215 err = rte_metrics_get_names(metrics, METRIC_LESSER_COUNT);
216 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
221 /* Test to validate get list of metric values */
223 test_metrics_get_values(void)
227 struct rte_metric_value getvalues[REG_METRIC_COUNT];
229 size_t m_size = sizeof(struct rte_metric_value);
230 for (i = 0; i < REG_METRIC_COUNT; i++)
231 memset(&getvalues[i], 0, m_size);
233 /* Successful Test, Not update results: valid arguments
234 * count lessthan the memzone stats
236 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
237 METRIC_LESSER_COUNT);
238 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
240 /* Successful Test, update results: valid arguments */
241 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
243 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
245 /* Successful Test : valid arguments count greaterthan the
248 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
250 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
252 /* Failure Test: Invalid port_id(lower value) with correct values
255 err = rte_metrics_get_values(-2, getvalues, REG_METRIC_COUNT);
256 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
258 /* Failure Test: Invalid port_id(higher value) with correct values
261 err = rte_metrics_get_values(33, getvalues, REG_METRIC_COUNT);
262 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
264 /* Successful Test: valid port_id with incorrect values and valid
267 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, NULL,
269 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
274 static struct unit_test_suite metrics_testsuite = {
275 .suite_name = "Metrics Unit Test Suite",
279 /* Test Case 1: Test to check all metric APIs without
282 TEST_CASE(test_metrics_without_init),
284 /* TEST CASE 2: Test to register valid metrics*/
285 TEST_CASE_ST(test_metrics_init, NULL,
286 test_metrics_reg_name_with_validname),
288 /* TEST CASE 3: Test to register list of metrics with valid
289 * names and valid count size, invalid names and invalid
292 TEST_CASE(test_metrics_reg_names),
294 /* TEST CASE 4: Test to register a update value with valid port
295 * id and invalid port id
297 TEST_CASE(test_metrics_update_value),
299 /* TEST CASE 5: Test to register update list of values with
300 * valid port id, key, value, count size and invalid port id,
301 * key, value, count size
303 TEST_CASE(test_metrics_update_values),
305 /* TEST CASE 6: Test to get metric names-key with valid
306 * array list, count size and invalid array list, count size
308 TEST_CASE(test_metrics_get_names),
310 /* TEST CASE 7: Test to get list of metric values with valid
311 * port id, array list, count size and invalid port id,
312 * arraylist, count size
314 TEST_CASE(test_metrics_get_values),
316 /* TEST CASE 8: Test to unregister metrics*/
317 TEST_CASE(test_metrics_deinitialize),
326 return unit_test_suite_runner(&metrics_testsuite);
329 REGISTER_TEST_COMMAND(metrics_autotest, test_metrics);