xref: /dpdk/app/test/test_latencystats.c (revision 742bde12)
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 	struct rte_metric_name wrongnames[NUM_STATS - 2];
73 
74 	size_t m_size = sizeof(struct rte_metric_name);
75 	for (i = 0; i < NUM_STATS; i++)
76 		memset(&names[i], 0, m_size);
77 	for (i = 0; i < NUM_STATS - 2; i++)
78 		memset(&wrongnames[i], 0, m_size);
79 
80 	/* Success Test: Valid names and size */
81 	size = NUM_STATS;
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);
86 		else
87 			printf("Failed: Names are not matched\n");
88 	}
89 	TEST_ASSERT((ret == NUM_STATS), "Test Failed to get metrics names");
90 
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);
95 
96 	/* Failure Test: Valid names and invalid size */
97 	size = 0;
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);
101 
102 	/* Failure Test: Invalid names (array size lesser than size) */
103 	size = NUM_STATS + 1;
104 	ret = rte_latencystats_get_names(wrongnames, size);
105 	TEST_ASSERT((ret == NUM_STATS), "Test Failed to get metrics names");
106 	return TEST_SUCCESS;
107 }
108 
109 /* Test case to get latency stats values */
110 static int test_latencystats_get(void)
111 {
112 	int ret = 0, i = 0;
113 	int size = 0;
114 	struct rte_metric_value values[NUM_STATS];
115 	struct rte_metric_value wrongvalues[NUM_STATS - 2];
116 
117 	size_t v_size = sizeof(struct rte_metric_value);
118 	for (i = 0; i < NUM_STATS; i++)
119 		memset(&values[i], 0, v_size);
120 	for (i = 0; i < NUM_STATS - 2; i++)
121 		memset(&wrongvalues[i], 0, v_size);
122 
123 	/* Success Test: Valid values and valid size */
124 	size = NUM_STATS;
125 	ret = rte_latencystats_get(values, size);
126 	TEST_ASSERT((ret == NUM_STATS), "Test Failed to get latency metrics"
127 			" values");
128 
129 	/* Failure Test: Invalid values and valid size */
130 	ret = rte_latencystats_get(NULL, size);
131 	TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the stats count,"
132 		    "Actual: %d Expected: %d", ret, NUM_STATS);
133 
134 	/* Failure Test: Valid values and invalid size */
135 	size = 0;
136 	ret = rte_latencystats_get(values, size);
137 	TEST_ASSERT((ret == NUM_STATS), "Test Failed to get the stats count,"
138 		    "Actual: %d Expected: %d", ret, NUM_STATS);
139 
140 	/* Failure Test: Invalid values(array size lesser than size)
141 	 * and invalid size
142 	 */
143 	size = NUM_STATS + 2;
144 	ret = rte_latencystats_get(wrongvalues, size);
145 	TEST_ASSERT(ret == NUM_STATS, "Test Failed to get latency metrics"
146 			" values");
147 
148 	return TEST_SUCCESS;
149 }
150 
151 static int test_latency_ring_setup(void)
152 {
153 	test_ring_setup(&ring, &portid);
154 
155 	return TEST_SUCCESS;
156 }
157 
158 static void test_latency_ring_free(void)
159 {
160 	test_ring_free(ring);
161 	test_vdev_uninit("net_ring_net_ringa");
162 }
163 
164 static int test_latency_packet_forward(void)
165 {
166 	int ret;
167 	struct rte_mbuf *pbuf[LATENCY_NUM_PACKETS] = { };
168 	struct rte_mempool *mp;
169 	char poolname[] = "mbuf_pool";
170 
171 	ret = test_get_mbuf_from_pool(&mp, pbuf, poolname);
172 	if (ret < 0) {
173 		printf("allocate mbuf pool Failed\n");
174 		return TEST_FAILED;
175 	}
176 	ret = test_packet_forward(pbuf, portid, QUEUE_ID);
177 	if (ret < 0)
178 		printf("send pkts Failed\n");
179 	test_put_mbuf_to_pool(mp, pbuf);
180 
181 	return TEST_SUCCESS;
182 }
183 
184 static struct
185 unit_test_suite latencystats_testsuite = {
186 	.suite_name = "Latency Stats Unit Test Suite",
187 	.setup = test_latency_ring_setup,
188 	.teardown = test_latency_ring_free,
189 	.unit_test_cases = {
190 
191 		/* Test Case 1: To check latency init with
192 		 * metrics init
193 		 */
194 		TEST_CASE_ST(NULL, NULL, test_latency_init),
195 
196 		/* Test Case 2: Do packet forwarding for metrics
197 		 * calculation and check the latency metrics values
198 		 * are updated
199 		 */
200 		TEST_CASE_ST(test_latency_packet_forward, NULL,
201 				test_latency_update),
202 		/* Test Case 3: To check whether latency stats names
203 		 * are retrieved
204 		 */
205 		TEST_CASE_ST(NULL, NULL, test_latencystats_get_names),
206 
207 		/* Test Case 4: To check whether latency stats
208 		 * values are retrieved
209 		 */
210 		TEST_CASE_ST(NULL, NULL, test_latencystats_get),
211 
212 		/* Test Case 5: To check uninit of latency test */
213 		TEST_CASE_ST(NULL, NULL, test_latency_uninit),
214 
215 		TEST_CASES_END()
216 	}
217 };
218 
219 static int test_latencystats(void)
220 {
221 	return unit_test_suite_runner(&latencystats_testsuite);
222 }
223 
224 REGISTER_TEST_COMMAND(latencystats_autotest, test_latencystats);
225