1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
9 #include <rte_ethdev.h>
10 #include <rte_latencystats.h>
11 #include "rte_lcore.h"
12 #include "rte_metrics.h"
14 #include "sample_packet_forward.h"
18 #define LATENCY_NUM_PACKETS 10
21 static uint16_t portid;
22 static struct rte_ring *ring;
24 static struct rte_metric_name lat_stats_strings[] = {
31 /* Test case for latency init with metrics init */
32 static int test_latency_init(void)
36 /* Metrics Initialization */
37 rte_metrics_init(rte_socket_id());
39 ret = rte_latencystats_init(1, NULL);
40 TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_init failed");
45 /* Test case to update the latency stats */
46 static int test_latency_update(void)
50 ret = rte_latencystats_update();
51 TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_update failed");
56 /* Test case to uninit latency stats */
57 static int test_latency_uninit(void)
61 ret = rte_latencystats_uninit();
62 TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_uninit failed");
64 ret = rte_metrics_deinit();
65 TEST_ASSERT(ret >= 0, "Test Failed: rte_metrics_deinit failed");
70 /* Test case to get names of latency stats */
71 static int test_latencystats_get_names(void)
75 struct rte_metric_name names[NUM_STATS];
77 size_t m_size = sizeof(struct rte_metric_name);
78 for (i = 0; i < NUM_STATS; i++)
79 memset(&names[i], 0, m_size);
81 /* Success Test: Valid names and size */
83 ret = rte_latencystats_get_names(names, size);
84 for (i = 0; i < NUM_STATS; i++) {
85 if (strcmp(lat_stats_strings[i].name, names[i].name) == 0)
86 printf(" %s\n", names[i].name);
88 printf("Failed: Names are not matched\n");
90 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get metrics names");
92 /* Failure Test: Invalid names and valid size */
93 ret = rte_latencystats_get_names(NULL, size);
94 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the metrics count,"
95 "Actual: %d Expected: %d", ret, NUM_STATS);
97 /* Failure Test: Valid names and invalid size */
99 ret = rte_latencystats_get_names(names, size);
100 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the metrics count,"
101 "Actual: %d Expected: %d", ret, NUM_STATS);
106 /* Test case to get latency stats values */
107 static int test_latencystats_get(void)
111 struct rte_metric_value values[NUM_STATS];
113 size_t v_size = sizeof(struct rte_metric_value);
114 for (i = 0; i < NUM_STATS; i++)
115 memset(&values[i], 0, v_size);
117 /* Success Test: Valid values and valid size */
119 ret = rte_latencystats_get(values, size);
120 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get latency metrics"
123 /* Failure Test: Invalid values and valid size */
124 ret = rte_latencystats_get(NULL, size);
125 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the stats count,"
126 "Actual: %d Expected: %d", ret, NUM_STATS);
128 /* Failure Test: Valid values and invalid size */
130 ret = rte_latencystats_get(values, size);
131 TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the stats count,"
132 "Actual: %d Expected: %d", ret, NUM_STATS);
137 static int test_latency_ring_setup(void)
139 test_ring_setup(&ring, &portid);
144 static void test_latency_ring_free(void)
146 test_ring_free(ring);
147 test_vdev_uninit("net_ring_net_ringa");
150 static int test_latency_packet_forward(void)
153 struct rte_mbuf *pbuf[LATENCY_NUM_PACKETS] = { };
154 struct rte_mempool *mp;
155 char poolname[] = "mbuf_pool";
157 ret = test_get_mbuf_from_pool(&mp, pbuf, poolname);
159 printf("allocate mbuf pool Failed\n");
162 ret = test_dev_start(portid, mp);
164 printf("test_dev_start(%hu, %p) failed, error code: %d\n",
169 ret = test_packet_forward(pbuf, portid, QUEUE_ID);
171 printf("send pkts Failed\n");
173 rte_eth_dev_stop(portid);
174 test_put_mbuf_to_pool(mp, pbuf);
176 return (ret >= 0) ? TEST_SUCCESS : TEST_FAILED;
180 unit_test_suite latencystats_testsuite = {
181 .suite_name = "Latency Stats Unit Test Suite",
182 .setup = test_latency_ring_setup,
183 .teardown = test_latency_ring_free,
186 /* Test Case 1: To check latency init with
189 TEST_CASE_ST(NULL, NULL, test_latency_init),
191 /* Test Case 2: Do packet forwarding for metrics
192 * calculation and check the latency metrics values
195 TEST_CASE_ST(test_latency_packet_forward, NULL,
196 test_latency_update),
197 /* Test Case 3: To check whether latency stats names
200 TEST_CASE_ST(NULL, NULL, test_latencystats_get_names),
202 /* Test Case 4: To check whether latency stats
203 * values are retrieved
205 TEST_CASE_ST(NULL, NULL, test_latencystats_get),
207 /* Test Case 5: To check uninit of latency test */
208 TEST_CASE_ST(NULL, NULL, test_latency_uninit),
214 static int test_latencystats(void)
216 return unit_test_suite_runner(&latencystats_testsuite);
219 REGISTER_TEST_COMMAND(latencystats_autotest, test_latencystats);