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 /* Test Case to check failures when memzone init is not done */
33 test_metrics_without_init(void)
36 const uint64_t value[REG_METRIC_COUNT] = {0};
37 const char * const mnames[] = {
38 "mean_bits_in", "mean_bits_out",
39 "peak_bits_in", "peak_bits_out",
42 /* Failure Test: Checking for memzone initialization */
43 err = rte_metrics_reg_name("peak_bits_in");
44 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
46 err = rte_metrics_reg_names(&mnames[0], 1);
47 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
49 err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY, VALUE);
50 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
52 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, KEY, &value[0], 4);
53 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
55 err = rte_metrics_get_names(NULL, 0);
56 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
58 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, NULL, 0);
59 TEST_ASSERT(err == -EIO, "%s, %d", __func__, __LINE__);
64 /* Test Case to validate registering a single metric */
66 test_metrics_reg_name_with_validname(void)
70 /* Test to register the new metric name */
71 err = rte_metrics_reg_name("peak_bits_out");
72 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
74 /* Test to register the same metric name */
75 err = rte_metrics_reg_name("peak_bits_out");
76 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
78 /* Test case to validate registering a invalid metric */
79 err = rte_metrics_reg_name(NULL);
80 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
85 /* Test case to validate registering a list of valid metric names */
87 test_metrics_reg_names(void)
90 const char * const mnames[] = {
91 "mean_bits_in", "mean_bits_out",
92 "peak_bits_in", "peak_bits_out",
95 /* Success Test: valid array and count size */
96 err = rte_metrics_reg_names(&mnames[0], ARRAY_SIZE(mnames));
97 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
102 /* Test case to validate update a metric */
104 test_metrics_update_value(void)
108 /* Successful Test: Valid port_id, key and value */
109 err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY, VALUE);
110 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
112 /* Successful Test: Valid port_id otherthan RTE_METRICS_GLOBAL, key
115 err = rte_metrics_update_value(9, KEY, VALUE);
116 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
118 /* Failed Test: Invalid port_id with lower value */
119 err = rte_metrics_update_value(-2, KEY, VALUE);
120 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
122 /* Failed Test: Invalid port_id with higher value */
123 err = rte_metrics_update_value(39, KEY, VALUE);
124 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
126 /* Failed Test: valid port id, value with invalid key */
127 err = rte_metrics_update_value(RTE_METRICS_GLOBAL, KEY+12, VALUE);
128 TEST_ASSERT(err < 0, "%s, %d", __func__, __LINE__);
133 /* Test case to validate update a list of metrics */
135 test_metrics_update_values(void)
138 const uint64_t value[REG_METRIC_COUNT] = {1, 2, 3, 4, 5, 6};
140 /* Successful Test: update metrics with first set */
141 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 0,
143 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
145 /* Successful Test: update metrics with second set */
146 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 1,
148 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
150 /* Successful Test: update metrics with third set */
151 err = rte_metrics_update_values(RTE_METRICS_GLOBAL, 2,
153 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
155 /* Failed Test: Invalid count size */
156 err = rte_metrics_update_values(RTE_METRICS_GLOBAL,
157 KEY, &value[0], ARRAY_SIZE(value));
158 TEST_ASSERT(err < 0, "%s, %d", __func__, __LINE__);
160 /* Failed Test: Invalid port_id(lower value) and valid data */
161 err = rte_metrics_update_values(-2, KEY, &value[0], ARRAY_SIZE(value));
162 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
164 /* Failed Test: Invalid port_id(higher value) and valid data */
165 err = rte_metrics_update_values(39, 1, &value[0], ARRAY_SIZE(value));
166 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
168 /* Failed Test: Invalid array */
169 err = rte_metrics_update_values(RTE_METRICS_GLOBAL,
170 KEY, NULL, ARRAY_SIZE(value));
171 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
176 /* Test to validate get metric name-key lookup table */
178 test_metrics_get_names(void)
181 struct rte_metric_name metrics[METRIC_LESSER_COUNT];
182 struct rte_metric_name success_metrics[REG_METRIC_COUNT];
184 /* Successful Test: Invalid array list */
185 err = rte_metrics_get_names(NULL, 0);
186 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
188 /* Successful Test: Valid array list, Correct Count Stats same
191 err = rte_metrics_get_names(success_metrics, REG_METRIC_COUNT);
192 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
194 /* Successful Test: Valid array list, Increase Count Stats than
197 err = rte_metrics_get_names(success_metrics, REG_METRIC_COUNT+5);
198 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
200 /* Successful Test, Not update results:
201 * Invalid array list, Lesser Count Stats than allocated stats
203 err = rte_metrics_get_names(metrics, METRIC_LESSER_COUNT);
204 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
209 /* Test to validate get list of metric values */
211 test_metrics_get_values(void)
215 struct rte_metric_value getvalues[REG_METRIC_COUNT];
217 size_t m_size = sizeof(struct rte_metric_value);
218 for (i = 0; i < REG_METRIC_COUNT; i++)
219 memset(&getvalues[i], 0, m_size);
221 /* Successful Test, Not update results: valid arguments
222 * count lessthan the memzone stats
224 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
225 METRIC_LESSER_COUNT);
226 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
228 /* Successful Test, update results: valid arguments */
229 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
231 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
233 /* Successful Test : valid arguments count greaterthan the
236 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, getvalues,
238 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
240 /* Failure Test: Invalid port_id(lower value) with correct values
243 err = rte_metrics_get_values(-2, getvalues, REG_METRIC_COUNT);
244 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
246 /* Failure Test: Invalid port_id(higher value) with correct values
249 err = rte_metrics_get_values(33, getvalues, REG_METRIC_COUNT);
250 TEST_ASSERT(err == -EINVAL, "%s, %d", __func__, __LINE__);
252 /* Successful Test: valid port_id with incorrect values and valid
255 err = rte_metrics_get_values(RTE_METRICS_GLOBAL, NULL,
257 TEST_ASSERT(err >= 0, "%s, %d", __func__, __LINE__);
262 static struct unit_test_suite metrics_testsuite = {
263 .suite_name = "Metrics Unit Test Suite",
267 /* Test Case 1: Test to check all metric APIs without
270 TEST_CASE(test_metrics_without_init),
272 /* TEST CASE 2: Test to register valid metrics*/
273 TEST_CASE_ST(test_metrics_init, NULL,
274 test_metrics_reg_name_with_validname),
276 /* TEST CASE 3: Test to register list of metrics with valid
277 * names and valid count size, invalid names and invalid
280 TEST_CASE(test_metrics_reg_names),
282 /* TEST CASE 4: Test to register a update value with valid port
283 * id and invalid port id
285 TEST_CASE(test_metrics_update_value),
287 /* TEST CASE 5: Test to register update list of values with
288 * valid port id, key, value, count size and invalid port id,
289 * key, value, count size
291 TEST_CASE(test_metrics_update_values),
293 /* TEST CASE 6: Test to get metric names-key with valid
294 * array list, count size and invalid array list, count size
296 TEST_CASE(test_metrics_get_names),
298 /* TEST CASE 7: Test to get list of metric values with valid
299 * port id, array list, count size and invalid port id,
300 * arraylist, count size
302 TEST_CASE(test_metrics_get_values),
310 return unit_test_suite_runner(&metrics_testsuite);
313 REGISTER_TEST_COMMAND(metrics_autotest, test_metrics);