1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
9 #include <rte_latencystats.h>
10 #include "rte_lcore.h"
11 #include "rte_metrics.h"
13 #include "sample_packet_forward.h"
17 #define LATENCY_NUM_PACKETS 10
20 static uint16_t portid;
21 static struct rte_ring *ring;
23 static struct rte_metric_name lat_stats_strings[] = {
30 /* Test case for latency init with metrics init */
31 static int test_latency_init(void)
35 /* Metrics Initialization */
36 rte_metrics_init(rte_socket_id());
38 ret = rte_latencystats_init(1, NULL);
39 TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_init failed");
44 /* Test case to update the latency stats */
45 static int test_latency_update(void)
49 ret = rte_latencystats_update();
50 TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_update failed");
55 /* Test case to uninit latency stats */
56 static int test_latency_uninit(void)
60 ret = rte_latencystats_uninit();
61 TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_uninit failed");
63 ret = rte_metrics_deinit();
64 TEST_ASSERT(ret >= 0, "Test Failed: rte_metrics_deinit failed");
69 /* Test case to get names of latency stats */
70 static int test_latencystats_get_names(void)
74 struct rte_metric_name names[NUM_STATS];
76 size_t m_size = sizeof(struct rte_metric_name);
77 for (i = 0; i < NUM_STATS; i++)
78 memset(&names[i], 0, m_size);
80 /* Success Test: Valid names and size */
82 ret = rte_latencystats_get_names(names, size);
83 for (i = 0; i <= NUM_STATS; i++) {
84 if (strcmp(lat_stats_strings[i].name, names[i].name) == 0)
85 printf(" %s\n", names[i].name);
87 printf("Failed: Names are not matched\n");
89 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get metrics names");
91 /* Failure Test: Invalid names and valid size */
92 ret = rte_latencystats_get_names(NULL, size);
93 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the metrics count,"
94 "Actual: %d Expected: %d", ret, NUM_STATS);
96 /* Failure Test: Valid names and invalid size */
98 ret = rte_latencystats_get_names(names, size);
99 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the metrics count,"
100 "Actual: %d Expected: %d", ret, NUM_STATS);
105 /* Test case to get latency stats values */
106 static int test_latencystats_get(void)
110 struct rte_metric_value values[NUM_STATS];
112 size_t v_size = sizeof(struct rte_metric_value);
113 for (i = 0; i < NUM_STATS; i++)
114 memset(&values[i], 0, v_size);
116 /* Success Test: Valid values and valid size */
118 ret = rte_latencystats_get(values, size);
119 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get latency metrics"
122 /* Failure Test: Invalid values and valid size */
123 ret = rte_latencystats_get(NULL, size);
124 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the stats count,"
125 "Actual: %d Expected: %d", ret, NUM_STATS);
127 /* Failure Test: Valid values and invalid size */
129 ret = rte_latencystats_get(values, size);
130 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the stats count,"
131 "Actual: %d Expected: %d", ret, NUM_STATS);
136 static int test_latency_ring_setup(void)
138 test_ring_setup(&ring, &portid);
143 static void test_latency_ring_free(void)
145 test_ring_free(ring);
146 test_vdev_uninit("net_ring_net_ringa");
149 static int test_latency_packet_forward(void)
152 struct rte_mbuf *pbuf[LATENCY_NUM_PACKETS] = { };
153 struct rte_mempool *mp;
154 char poolname[] = "mbuf_pool";
156 ret = test_get_mbuf_from_pool(&mp, pbuf, poolname);
158 printf("allocate mbuf pool Failed\n");
161 ret = test_packet_forward(pbuf, portid, QUEUE_ID);
163 printf("send pkts Failed\n");
164 test_put_mbuf_to_pool(mp, pbuf);
170 unit_test_suite latencystats_testsuite = {
171 .suite_name = "Latency Stats Unit Test Suite",
172 .setup = test_latency_ring_setup,
173 .teardown = test_latency_ring_free,
176 /* Test Case 1: To check latency init with
179 TEST_CASE_ST(NULL, NULL, test_latency_init),
181 /* Test Case 2: Do packet forwarding for metrics
182 * calculation and check the latency metrics values
185 TEST_CASE_ST(test_latency_packet_forward, NULL,
186 test_latency_update),
187 /* Test Case 3: To check whether latency stats names
190 TEST_CASE_ST(NULL, NULL, test_latencystats_get_names),
192 /* Test Case 4: To check whether latency stats
193 * values are retrieved
195 TEST_CASE_ST(NULL, NULL, test_latencystats_get),
197 /* Test Case 5: To check uninit of latency test */
198 TEST_CASE_ST(NULL, NULL, test_latency_uninit),
204 static int test_latencystats(void)
206 return unit_test_suite_runner(&latencystats_testsuite);
209 REGISTER_TEST_COMMAND(latencystats_autotest, test_latencystats);