xref: /dpdk/app/test/test_latencystats.c (revision 089e5ed727a15da2729cfee9b63533dd120bd04c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8 
9 #include <rte_latencystats.h>
10 #include "rte_lcore.h"
11 #include "rte_metrics.h"
12 
13 #include "sample_packet_forward.h"
14 #include "test.h"
15 
16 #define NUM_STATS 4
17 #define LATENCY_NUM_PACKETS 10
18 #define QUEUE_ID 0
19 
20 uint16_t portid;
21 struct rte_ring *ring;
22 
23 struct rte_metric_name lat_stats_strings[] = {
24 	{"min_latency_ns"},
25 	{"avg_latency_ns"},
26 	{"max_latency_ns"},
27 	{"jitter_ns"},
28 };
29 
30 /* Test case for latency init with metrics init */
31 static int test_latency_init(void)
32 {
33 	int ret = 0;
34 
35 	/* Metrics Initialization */
36 	rte_metrics_init(rte_socket_id());
37 
38 	ret = rte_latencystats_init(1, NULL);
39 	TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_init failed");
40 
41 	return TEST_SUCCESS;
42 }
43 
44 /* Test case to update the latency stats */
45 static int test_latency_update(void)
46 {
47 	int ret = 0;
48 
49 	ret = rte_latencystats_update();
50 	TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_update failed");
51 
52 	return TEST_SUCCESS;
53 }
54 
55 /* Test case to uninit latency stats */
56 static int test_latency_uninit(void)
57 {
58 	int ret = 0;
59 
60 	ret = rte_latencystats_uninit();
61 	TEST_ASSERT(ret >= 0, "Test Failed: rte_latencystats_uninit failed");
62 
63 	return TEST_SUCCESS;
64 }
65 
66 /* Test case to get names of latency stats */
67 static int test_latencystats_get_names(void)
68 {
69 	int ret = 0, i = 0;
70 	int size = 0;
71 	struct rte_metric_name names[NUM_STATS];
72 
73 	size_t m_size = sizeof(struct rte_metric_name);
74 	for (i = 0; i < NUM_STATS; i++)
75 		memset(&names[i], 0, m_size);
76 
77 	/* Success Test: Valid names and size */
78 	size = NUM_STATS;
79 	ret = rte_latencystats_get_names(names, size);
80 	for (i = 0; i <= NUM_STATS; i++) {
81 		if (strcmp(lat_stats_strings[i].name, names[i].name) == 0)
82 			printf(" %s\n", names[i].name);
83 		else
84 			printf("Failed: Names are not matched\n");
85 	}
86 	TEST_ASSERT((ret == NUM_STATS), "Test Failed to get metrics names");
87 
88 	/* Failure Test: Invalid names and valid size */
89 	ret = rte_latencystats_get_names(NULL, size);
90 	TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the metrics count,"
91 		    "Actual: %d Expected: %d", ret, NUM_STATS);
92 
93 	/* Failure Test: Valid names and invalid size */
94 	size = 0;
95 	ret = rte_latencystats_get_names(names, size);
96 	TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the metrics count,"
97 		    "Actual: %d Expected: %d", ret, NUM_STATS);
98 
99 	return TEST_SUCCESS;
100 }
101 
102 /* Test case to get latency stats values */
103 static int test_latencystats_get(void)
104 {
105 	int ret = 0, i = 0;
106 	int size = 0;
107 	struct rte_metric_value values[NUM_STATS];
108 
109 	size_t v_size = sizeof(struct rte_metric_value);
110 	for (i = 0; i < NUM_STATS; i++)
111 		memset(&values[i], 0, v_size);
112 
113 	/* Success Test: Valid values and valid size */
114 	size = NUM_STATS;
115 	ret = rte_latencystats_get(values, size);
116 	TEST_ASSERT((ret == NUM_STATS), "Test Failed to get latency metrics"
117 			" values");
118 
119 	/* Failure Test: Invalid values and valid size */
120 	ret = rte_latencystats_get(NULL, size);
121 	TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the stats count,"
122 		    "Actual: %d Expected: %d", ret, NUM_STATS);
123 
124 	/* Failure Test: Valid values and invalid size */
125 	size = 0;
126 	ret = rte_latencystats_get(values, size);
127 	TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the stats count,"
128 		    "Actual: %d Expected: %d", ret, NUM_STATS);
129 
130 	return TEST_SUCCESS;
131 }
132 
133 static int test_latency_ring_setup(void)
134 {
135 	test_ring_setup(&ring, &portid);
136 
137 	return TEST_SUCCESS;
138 }
139 
140 static void test_latency_ring_free(void)
141 {
142 	test_ring_free(ring);
143 	test_vdev_uninit("net_ring_net_ringa");
144 }
145 
146 static int test_latency_packet_forward(void)
147 {
148 	int ret;
149 	struct rte_mbuf *pbuf[LATENCY_NUM_PACKETS] = { };
150 	struct rte_mempool *mp;
151 	char poolname[] = "mbuf_pool";
152 
153 	ret = test_get_mbuf_from_pool(&mp, pbuf, poolname);
154 	if (ret < 0) {
155 		printf("allocate mbuf pool Failed\n");
156 		return TEST_FAILED;
157 	}
158 	ret = test_packet_forward(pbuf, portid, QUEUE_ID);
159 	if (ret < 0)
160 		printf("send pkts Failed\n");
161 	test_put_mbuf_to_pool(mp, pbuf);
162 
163 	return TEST_SUCCESS;
164 }
165 
166 static struct
167 unit_test_suite latencystats_testsuite = {
168 	.suite_name = "Latency Stats Unit Test Suite",
169 	.setup = test_latency_ring_setup,
170 	.teardown = test_latency_ring_free,
171 	.unit_test_cases = {
172 
173 		/* Test Case 1: To check latency init with
174 		 * metrics init
175 		 */
176 		TEST_CASE_ST(NULL, NULL, test_latency_init),
177 
178 		/* Test Case 2: Do packet forwarding for metrics
179 		 * calculation and check the latency metrics values
180 		 * are updated
181 		 */
182 		TEST_CASE_ST(test_latency_packet_forward, NULL,
183 				test_latency_update),
184 		/* Test Case 3: To check whether latency stats names
185 		 * are retrieved
186 		 */
187 		TEST_CASE_ST(NULL, NULL, test_latencystats_get_names),
188 
189 		/* Test Case 4: To check whether latency stats
190 		 * values are retrieved
191 		 */
192 		TEST_CASE_ST(NULL, NULL, test_latencystats_get),
193 
194 		/* Test Case 5: To check uninit of latency test */
195 		TEST_CASE_ST(NULL, NULL, test_latency_uninit),
196 
197 		TEST_CASES_END()
198 	}
199 };
200 
201 static int test_latencystats(void)
202 {
203 	return unit_test_suite_runner(&latencystats_testsuite);
204 }
205 
206 REGISTER_TEST_COMMAND(latencystats_autotest, test_latencystats);
207