1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
8 #include <rte_malloc.h>
9 #include <rte_random.h>
10 #include <rte_stack.h>
14 #define STACK_SIZE 4096
18 test_stack_push_pop(struct rte_stack *s, void **obj_table, unsigned int bulk_sz)
23 popped_objs = rte_calloc(NULL, STACK_SIZE, sizeof(void *), 0);
24 if (popped_objs == NULL) {
25 printf("[%s():%u] failed to calloc %zu bytes\n",
26 __func__, __LINE__, STACK_SIZE * sizeof(void *));
30 for (i = 0; i < STACK_SIZE; i += bulk_sz) {
31 ret = rte_stack_push(s, &obj_table[i], bulk_sz);
34 printf("[%s():%u] push returned: %d (expected %u)\n",
35 __func__, __LINE__, ret, bulk_sz);
36 rte_free(popped_objs);
40 if (rte_stack_count(s) != i + bulk_sz) {
41 printf("[%s():%u] stack count: %u (expected %u)\n",
42 __func__, __LINE__, rte_stack_count(s),
44 rte_free(popped_objs);
48 if (rte_stack_free_count(s) != STACK_SIZE - i - bulk_sz) {
49 printf("[%s():%u] stack free count: %u (expected %u)\n",
50 __func__, __LINE__, rte_stack_count(s),
51 STACK_SIZE - i - bulk_sz);
52 rte_free(popped_objs);
57 for (i = 0; i < STACK_SIZE; i += bulk_sz) {
58 ret = rte_stack_pop(s, &popped_objs[i], bulk_sz);
61 printf("[%s():%u] pop returned: %d (expected %u)\n",
62 __func__, __LINE__, ret, bulk_sz);
63 rte_free(popped_objs);
67 if (rte_stack_count(s) != STACK_SIZE - i - bulk_sz) {
68 printf("[%s():%u] stack count: %u (expected %u)\n",
69 __func__, __LINE__, rte_stack_count(s),
70 STACK_SIZE - i - bulk_sz);
71 rte_free(popped_objs);
75 if (rte_stack_free_count(s) != i + bulk_sz) {
76 printf("[%s():%u] stack free count: %u (expected %u)\n",
77 __func__, __LINE__, rte_stack_count(s),
79 rte_free(popped_objs);
84 for (i = 0; i < STACK_SIZE; i++) {
85 if (obj_table[i] != popped_objs[STACK_SIZE - i - 1]) {
86 printf("[%s():%u] Incorrect value %p at index 0x%x\n",
88 popped_objs[STACK_SIZE - i - 1], i);
89 rte_free(popped_objs);
94 rte_free(popped_objs);
100 test_stack_basic(uint32_t flags)
102 struct rte_stack *s = NULL;
103 void **obj_table = NULL;
106 obj_table = rte_calloc(NULL, STACK_SIZE, sizeof(void *), 0);
107 if (obj_table == NULL) {
108 printf("[%s():%u] failed to calloc %zu bytes\n",
109 __func__, __LINE__, STACK_SIZE * sizeof(void *));
113 for (i = 0; i < STACK_SIZE; i++)
114 obj_table[i] = (void *)(uintptr_t)i;
116 s = rte_stack_create(__func__, STACK_SIZE, rte_socket_id(), flags);
118 printf("[%s():%u] failed to create a stack\n",
123 if (rte_stack_lookup(__func__) != s) {
124 printf("[%s():%u] failed to lookup a stack\n",
129 if (rte_stack_count(s) != 0) {
130 printf("[%s():%u] stack count: %u (expected 0)\n",
131 __func__, __LINE__, rte_stack_count(s));
135 if (rte_stack_free_count(s) != STACK_SIZE) {
136 printf("[%s():%u] stack free count: %u (expected %u)\n",
137 __func__, __LINE__, rte_stack_count(s), STACK_SIZE);
141 ret = test_stack_push_pop(s, obj_table, 1);
143 printf("[%s():%u] Single object push/pop failed\n",
148 ret = test_stack_push_pop(s, obj_table, MAX_BULK);
150 printf("[%s():%u] Bulk object push/pop failed\n",
155 ret = rte_stack_push(s, obj_table, 2 * STACK_SIZE);
157 printf("[%s():%u] Excess objects push succeeded\n",
162 ret = rte_stack_pop(s, obj_table, 1);
164 printf("[%s():%u] Empty stack pop succeeded\n",
180 test_stack_name_reuse(uint32_t flags)
182 struct rte_stack *s[2];
184 s[0] = rte_stack_create("test", STACK_SIZE, rte_socket_id(), flags);
186 printf("[%s():%u] Failed to create a stack\n",
191 s[1] = rte_stack_create("test", STACK_SIZE, rte_socket_id(), flags);
193 printf("[%s():%u] Failed to detect re-used name\n",
198 rte_stack_free(s[0]);
204 test_stack_name_length(uint32_t flags)
206 char name[RTE_STACK_NAMESIZE + 1];
209 memset(name, 's', sizeof(name));
210 name[RTE_STACK_NAMESIZE] = '\0';
212 s = rte_stack_create(name, STACK_SIZE, rte_socket_id(), flags);
214 printf("[%s():%u] Failed to prevent long name\n",
219 if (rte_errno != ENAMETOOLONG) {
220 printf("[%s():%u] rte_stack failed to set correct errno on failed lookup\n",
229 test_lookup_null(void)
231 struct rte_stack *s = rte_stack_lookup("stack_not_found");
234 printf("[%s():%u] rte_stack found a non-existent stack\n",
239 if (rte_errno != ENOENT) {
240 printf("[%s():%u] rte_stack failed to set correct errno on failed lookup\n",
245 s = rte_stack_lookup(NULL);
248 printf("[%s():%u] rte_stack found a non-existent stack\n",
253 if (rte_errno != EINVAL) {
254 printf("[%s():%u] rte_stack failed to set correct errno on failed lookup\n",
265 /* Check whether the library proper handles a NULL pointer */
266 rte_stack_free(NULL);
271 #define NUM_ITERS_PER_THREAD 100000
277 static struct test_args thread_test_args;
280 stack_thread_push_pop(__rte_unused void *args)
282 void *obj_table[MAX_BULK];
285 for (i = 0; i < NUM_ITERS_PER_THREAD; i++) {
288 num = rte_rand() % MAX_BULK;
290 if (rte_stack_push(thread_test_args.s, obj_table, num) != num) {
291 printf("[%s():%u] Failed to push %u pointers\n",
292 __func__, __LINE__, num);
296 if (rte_stack_pop(thread_test_args.s, obj_table, num) != num) {
297 printf("[%s():%u] Failed to pop %u pointers\n",
298 __func__, __LINE__, num);
307 test_stack_multithreaded(uint32_t flags)
309 unsigned int lcore_id;
313 if (rte_lcore_count() < 2) {
314 printf("Not enough cores for test_stack_multithreaded, expecting at least 2\n");
318 printf("[%s():%u] Running with %u lcores\n",
319 __func__, __LINE__, rte_lcore_count());
321 s = rte_stack_create("test", MAX_BULK * rte_lcore_count(), rte_socket_id(), flags);
323 printf("[%s():%u] Failed to create a stack\n",
328 thread_test_args.s = s;
330 if (rte_eal_mp_remote_launch(stack_thread_push_pop, NULL, CALL_MAIN))
331 rte_panic("Failed to launch tests\n");
333 RTE_LCORE_FOREACH(lcore_id) {
334 if (rte_eal_wait_lcore(lcore_id) < 0)
343 __test_stack(uint32_t flags)
345 if (test_stack_basic(flags) < 0)
348 if (test_lookup_null() < 0)
351 if (test_free_null() < 0)
354 if (test_stack_name_reuse(flags) < 0)
357 if (test_stack_name_length(flags) < 0)
360 if (test_stack_multithreaded(flags) < 0)
369 return __test_stack(0);
375 #if defined(RTE_STACK_LF_SUPPORTED)
376 return __test_stack(RTE_STACK_F_LF);
382 REGISTER_TEST_COMMAND(stack_autotest, test_stack);
383 REGISTER_TEST_COMMAND(stack_lf_autotest, test_lf_stack);