xref: /dpdk/app/test/test_event_ring.c (revision e0a8442ccd15bafbb7eb150c35331c8e3b828c53)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4 
5 #include "test.h"
6 
7 #include <string.h>
8 
9 #ifdef RTE_EXEC_ENV_WINDOWS
10 static int
test_event_ring(void)11 test_event_ring(void)
12 {
13 	printf("event_ring not supported on Windows, skipping test\n");
14 	return TEST_SKIPPED;
15 }
16 
17 #else
18 
19 #include <rte_event_ring.h>
20 
21 /*
22  * Event Ring
23  * ===========
24  *
25  * Test some basic ops for the event rings.
26  * Does not fully test everything, since most code is reused from rte_ring
27  * library and tested as part of the normal ring autotests.
28  */
29 
30 #define RING_SIZE 4096
31 #define MAX_BULK 32
32 
33 static struct rte_event_ring *r;
34 
35 /*
36  * ensure failure to create ring with a bad ring size
37  */
38 static int
test_event_ring_creation_with_wrong_size(void)39 test_event_ring_creation_with_wrong_size(void)
40 {
41 	struct rte_event_ring *rp = NULL;
42 
43 	/* Test if ring size is not power of 2 */
44 	rp = rte_event_ring_create("test_bad_ring_size", RING_SIZE + 1,
45 			SOCKET_ID_ANY, 0);
46 	if (rp != NULL)
47 		return -1;
48 
49 	/* Test if ring size is exceeding the limit */
50 	rp = rte_event_ring_create("test_bad_ring_size", (RTE_RING_SZ_MASK + 1),
51 			SOCKET_ID_ANY, 0);
52 	if (rp != NULL)
53 		return -1;
54 	return 0;
55 }
56 
57 /*
58  * Test to check if a non-power-of-2 count causes the create
59  * function to fail correctly
60  */
61 static int
test_create_count_odd(void)62 test_create_count_odd(void)
63 {
64 	struct rte_event_ring *r = rte_event_ring_create("test_event_ring_count",
65 			4097, SOCKET_ID_ANY, 0);
66 	if (r != NULL)
67 		return -1;
68 	return 0;
69 }
70 
71 static int
test_lookup_null(void)72 test_lookup_null(void)
73 {
74 	struct rte_event_ring *rlp = rte_event_ring_lookup("ring_not_found");
75 	if (rlp == NULL && rte_errno != ENOENT) {
76 		printf("test failed to return error on null pointer\n");
77 		return -1;
78 	}
79 	return 0;
80 }
81 
82 static int
test_basic_event_enqueue_dequeue(void)83 test_basic_event_enqueue_dequeue(void)
84 {
85 	struct rte_event_ring *sr = NULL;
86 	struct rte_event evs[16];
87 	uint16_t ret, free_count, used_count;
88 
89 	memset(evs, 0, sizeof(evs));
90 	sr = rte_event_ring_create("spsc_ring", 32, rte_socket_id(),
91 			RING_F_SP_ENQ | RING_F_SC_DEQ);
92 	if (sr == NULL) {
93 		printf("Failed to create sp/sc ring\n");
94 		return -1;
95 	}
96 	if (rte_event_ring_get_capacity(sr) != 31) {
97 		printf("Error, invalid capacity\n");
98 		goto error;
99 	}
100 
101 	/* test sp/sc ring */
102 	if (rte_event_ring_count(sr) != 0) {
103 		printf("Error, ring not empty as expected\n");
104 		goto error;
105 	}
106 	if (rte_event_ring_free_count(sr) != rte_event_ring_get_capacity(sr)) {
107 		printf("Error, ring free count not as expected\n");
108 		goto error;
109 	}
110 
111 	ret = rte_event_ring_enqueue_burst(sr, evs, RTE_DIM(evs), &free_count);
112 	if (ret != RTE_DIM(evs) ||
113 			free_count != rte_event_ring_get_capacity(sr) - ret) {
114 		printf("Error, status after enqueue is unexpected\n");
115 		goto error;
116 	}
117 
118 	ret = rte_event_ring_enqueue_burst(sr, evs, RTE_DIM(evs), &free_count);
119 	if (ret != RTE_DIM(evs) - 1 ||
120 			free_count != 0) {
121 		printf("Error, status after enqueue is unexpected\n");
122 		goto error;
123 	}
124 
125 	ret = rte_event_ring_dequeue_burst(sr, evs, RTE_DIM(evs), &used_count);
126 	if (ret != RTE_DIM(evs) ||
127 			used_count != rte_event_ring_get_capacity(sr) - ret) {
128 		printf("Error, status after enqueue is unexpected\n");
129 		goto error;
130 	}
131 	ret = rte_event_ring_dequeue_burst(sr, evs, RTE_DIM(evs), &used_count);
132 	if (ret != RTE_DIM(evs) - 1 ||
133 			used_count != 0) {
134 		printf("Error, status after enqueue is unexpected\n");
135 		goto error;
136 	}
137 
138 	rte_event_ring_free(sr);
139 	return 0;
140 error:
141 	rte_event_ring_free(sr);
142 	return -1;
143 }
144 
145 static int
test_event_ring_with_exact_size(void)146 test_event_ring_with_exact_size(void)
147 {
148 	struct rte_event_ring *std_ring, *exact_sz_ring;
149 	struct rte_event ev = { .mbuf = NULL };
150 	struct rte_event ev_array[16];
151 	static const unsigned int ring_sz = RTE_DIM(ev_array);
152 	unsigned int i;
153 
154 	std_ring = rte_event_ring_create("std", ring_sz, rte_socket_id(),
155 			RING_F_SP_ENQ | RING_F_SC_DEQ);
156 	if (std_ring == NULL) {
157 		printf("%s: error, can't create std ring\n", __func__);
158 		return -1;
159 	}
160 	exact_sz_ring = rte_event_ring_create("exact sz",
161 			ring_sz, rte_socket_id(),
162 			RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
163 	if (exact_sz_ring == NULL) {
164 		printf("%s: error, can't create exact size ring\n", __func__);
165 		return -1;
166 	}
167 
168 	/*
169 	 * Check that the exact size ring is bigger than the standard ring
170 	 */
171 	if (rte_event_ring_get_size(std_ring) >=
172 			rte_event_ring_get_size(exact_sz_ring)) {
173 		printf("%s: error, std ring (size: %u) is not smaller than exact size one (size %u)\n",
174 				__func__,
175 				rte_event_ring_get_size(std_ring),
176 				rte_event_ring_get_size(exact_sz_ring));
177 		return -1;
178 	}
179 	/*
180 	 * check that the exact_sz_ring can hold one more element than the
181 	 * standard ring. (16 vs 15 elements)
182 	 */
183 	for (i = 0; i < ring_sz - 1; i++) {
184 		rte_event_ring_enqueue_burst(std_ring, &ev, 1, NULL);
185 		rte_event_ring_enqueue_burst(exact_sz_ring, &ev, 1, NULL);
186 	}
187 	if (rte_event_ring_enqueue_burst(std_ring, &ev, 1, NULL) != 0) {
188 		printf("%s: error, unexpected successful enqueue\n", __func__);
189 		return -1;
190 	}
191 	if (rte_event_ring_enqueue_burst(exact_sz_ring, &ev, 1, NULL) != 1) {
192 		printf("%s: error, enqueue failed\n", __func__);
193 		return -1;
194 	}
195 
196 	/* check that dequeue returns the expected number of elements */
197 	if (rte_event_ring_dequeue_burst(exact_sz_ring, ev_array,
198 			RTE_DIM(ev_array), NULL) != ring_sz) {
199 		printf("%s: error, failed to dequeue expected nb of elements\n",
200 				__func__);
201 		return -1;
202 	}
203 
204 	/* check that the capacity function returns expected value */
205 	if (rte_event_ring_get_capacity(exact_sz_ring) != ring_sz) {
206 		printf("%s: error, incorrect ring capacity reported\n",
207 				__func__);
208 		return -1;
209 	}
210 
211 	rte_event_ring_free(std_ring);
212 	rte_event_ring_free(exact_sz_ring);
213 	return 0;
214 }
215 
216 static int
test_event_ring(void)217 test_event_ring(void)
218 {
219 	if (r == NULL)
220 		r = rte_event_ring_create("ev_test", RING_SIZE,
221 				SOCKET_ID_ANY, 0);
222 	if (r == NULL)
223 		return -1;
224 
225 	/* retrieve the ring from its name */
226 	if (rte_event_ring_lookup("ev_test") != r) {
227 		printf("Cannot lookup ring from its name\n");
228 		return -1;
229 	}
230 
231 	/* basic operations */
232 	if (test_create_count_odd() < 0) {
233 		printf("Test failed to detect odd count\n");
234 		return -1;
235 	}
236 	printf("Test detected odd count\n");
237 
238 	if (test_lookup_null() < 0) {
239 		printf("Test failed to detect NULL ring lookup\n");
240 		return -1;
241 	}
242 	printf("Test detected NULL ring lookup\n");
243 
244 	/* test of creating ring with wrong size */
245 	if (test_event_ring_creation_with_wrong_size() < 0)
246 		return -1;
247 
248 	if (test_basic_event_enqueue_dequeue() < 0)
249 		return -1;
250 
251 	if (test_event_ring_with_exact_size() < 0)
252 		return -1;
253 
254 	return 0;
255 }
256 
257 #endif /* !RTE_EXEC_ENV_WINDOWS */
258 
259 REGISTER_FAST_TEST(event_ring_autotest, true, true, test_event_ring);
260