1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <sys/queue.h>
14 #include <rte_common.h>
16 #include <rte_memory.h>
17 #include <rte_launch.h>
18 #include <rte_cycles.h>
20 #include <rte_per_lcore.h>
21 #include <rte_lcore.h>
22 #include <rte_atomic.h>
23 #include <rte_branch_prediction.h>
24 #include <rte_malloc.h>
26 #include <rte_ring_elem.h>
27 #include <rte_random.h>
28 #include <rte_errno.h>
29 #include <rte_hexdump.h>
32 #include "test_ring.h"
38 * #. Functional tests. Tests single/bulk/burst, default/SPSC/MPMC,
39 * legacy/custom element size (4B, 8B, 16B, 20B) APIs.
40 * Some tests incorporate unaligned addresses for objects.
41 * The enqueued/dequeued data is validated for correctness.
43 * #. Performance tests are in test_ring_perf.c
46 #define RING_SIZE 4096
49 #define TEST_RING_VERIFY(exp) \
51 printf("error at %s:%d\tcondition " #exp " failed\n", \
52 __func__, __LINE__); \
53 rte_ring_dump(stdout, r); \
57 #define TEST_RING_FULL_EMTPY_ITER 8
59 static const int esize[] = {-1, 4, 8, 16, 20};
62 test_ring_inc_ptr(void **obj, int esize, unsigned int n)
64 /* Legacy queue APIs? */
66 return ((void **)obj) + n;
68 return (void **)(((uint32_t *)obj) +
69 (n * esize / sizeof(uint32_t)));
73 test_ring_mem_init(void *obj, unsigned int count, int esize)
77 /* Legacy queue APIs? */
79 for (i = 0; i < count; i++)
80 ((void **)obj)[i] = (void *)(unsigned long)i;
82 for (i = 0; i < (count * esize / sizeof(uint32_t)); i++)
83 ((uint32_t *)obj)[i] = i;
87 test_ring_print_test_string(const char *istr, unsigned int api_type, int esize)
89 printf("\n%s: ", istr);
92 printf("legacy APIs: ");
94 printf("elem APIs: element size %dB ", esize);
96 if (api_type == TEST_RING_IGNORE_API_TYPE)
99 if (api_type & TEST_RING_THREAD_DEF)
100 printf(": default enqueue/dequeue: ");
101 else if (api_type & TEST_RING_THREAD_SPSC)
103 else if (api_type & TEST_RING_THREAD_MPMC)
106 if (api_type & TEST_RING_ELEM_SINGLE)
108 else if (api_type & TEST_RING_ELEM_BULK)
110 else if (api_type & TEST_RING_ELEM_BURST)
115 * Various negative test cases.
118 test_ring_negative_tests(void)
120 struct rte_ring *rp = NULL;
121 struct rte_ring *rt = NULL;
124 /* Test with esize not a multiple of 4 */
125 rp = test_ring_create("test_bad_element_size", 23,
126 RING_SIZE + 1, SOCKET_ID_ANY, 0);
128 printf("Test failed to detect invalid element size\n");
133 for (i = 0; i < RTE_DIM(esize); i++) {
134 /* Test if ring size is not power of 2 */
135 rp = test_ring_create("test_bad_ring_size", esize[i],
136 RING_SIZE + 1, SOCKET_ID_ANY, 0);
138 printf("Test failed to detect odd count\n");
142 /* Test if ring size is exceeding the limit */
143 rp = test_ring_create("test_bad_ring_size", esize[i],
144 RTE_RING_SZ_MASK + 1, SOCKET_ID_ANY, 0);
146 printf("Test failed to detect limits\n");
150 /* Tests if lookup returns NULL on non-existing ring */
151 rp = rte_ring_lookup("ring_not_found");
152 if (rp != NULL && rte_errno != ENOENT) {
153 printf("Test failed to detect NULL ring lookup\n");
157 /* Test to if a non-power of 2 count causes the create
158 * function to fail correctly
160 rp = test_ring_create("test_ring_count", esize[i], 4097,
165 rp = test_ring_create("test_ring_negative", esize[i], RING_SIZE,
167 RING_F_SP_ENQ | RING_F_SC_DEQ);
169 printf("test_ring_negative fail to create ring\n");
173 if (rte_ring_lookup("test_ring_negative") != rp)
176 if (rte_ring_empty(rp) != 1) {
177 printf("test_ring_nagative ring is not empty but it should be\n");
181 /* Tests if it would always fail to create ring with an used
184 rt = test_ring_create("test_ring_negative", esize[i], RING_SIZE,
202 * Burst and bulk operations with sp/sc, mp/mc and default (during creation)
203 * Random number of elements are enqueued and dequeued.
206 test_ring_burst_bulk_tests1(unsigned int api_type, unsigned int create_flags,
210 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
214 const unsigned int rsz = RING_SIZE - 1;
216 for (i = 0; i < RTE_DIM(esize); i++) {
217 test_ring_print_test_string(tname, api_type, esize[i]);
219 /* Create the ring */
220 r = test_ring_create("test_ring_burst_bulk_tests", esize[i],
221 RING_SIZE, SOCKET_ID_ANY, create_flags);
223 /* alloc dummy object pointers */
224 src = test_ring_calloc(RING_SIZE * 2, esize[i]);
227 test_ring_mem_init(src, RING_SIZE * 2, esize[i]);
230 /* alloc some room for copied objects */
231 dst = test_ring_calloc(RING_SIZE * 2, esize[i]);
236 printf("Random full/empty test\n");
238 for (j = 0; j != TEST_RING_FULL_EMTPY_ITER; j++) {
239 /* random shift in the ring */
240 rand = RTE_MAX(rte_rand() % RING_SIZE, 1UL);
241 printf("%s: iteration %u, random shift: %u;\n",
243 ret = test_ring_enqueue(r, cur_src, esize[i], rand,
245 TEST_RING_VERIFY(ret != 0);
247 ret = test_ring_dequeue(r, cur_dst, esize[i], rand,
249 TEST_RING_VERIFY(ret == rand);
252 ret = test_ring_enqueue(r, cur_src, esize[i], rsz,
254 TEST_RING_VERIFY(ret != 0);
256 TEST_RING_VERIFY(rte_ring_free_count(r) == 0);
257 TEST_RING_VERIFY(rsz == rte_ring_count(r));
258 TEST_RING_VERIFY(rte_ring_full(r));
259 TEST_RING_VERIFY(rte_ring_empty(r) == 0);
262 ret = test_ring_dequeue(r, cur_dst, esize[i], rsz,
264 TEST_RING_VERIFY(ret == (int)rsz);
265 TEST_RING_VERIFY(rsz == rte_ring_free_count(r));
266 TEST_RING_VERIFY(rte_ring_count(r) == 0);
267 TEST_RING_VERIFY(rte_ring_full(r) == 0);
268 TEST_RING_VERIFY(rte_ring_empty(r));
271 TEST_RING_VERIFY(memcmp(src, dst, rsz) == 0);
274 /* Free memory before test completed */
292 * Burst and bulk operations with sp/sc, mp/mc and default (during creation)
293 * Sequence of simple enqueues/dequeues and validate the enqueued and
297 test_ring_burst_bulk_tests2(unsigned int api_type, unsigned int create_flags,
301 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
305 for (i = 0; i < RTE_DIM(esize); i++) {
306 test_ring_print_test_string(tname, api_type, esize[i]);
308 /* Create the ring */
309 r = test_ring_create("test_ring_burst_bulk_tests", esize[i],
310 RING_SIZE, SOCKET_ID_ANY, create_flags);
312 /* alloc dummy object pointers */
313 src = test_ring_calloc(RING_SIZE * 2, esize[i]);
316 test_ring_mem_init(src, RING_SIZE * 2, esize[i]);
319 /* alloc some room for copied objects */
320 dst = test_ring_calloc(RING_SIZE * 2, esize[i]);
325 printf("enqueue 1 obj\n");
326 ret = test_ring_enqueue(r, cur_src, esize[i], 1, api_type);
329 cur_src = test_ring_inc_ptr(cur_src, esize[i], 1);
331 printf("enqueue 2 objs\n");
332 ret = test_ring_enqueue(r, cur_src, esize[i], 2, api_type);
335 cur_src = test_ring_inc_ptr(cur_src, esize[i], 2);
337 printf("enqueue MAX_BULK objs\n");
338 ret = test_ring_enqueue(r, cur_src, esize[i], MAX_BULK,
342 cur_src = test_ring_inc_ptr(cur_src, esize[i], MAX_BULK);
344 printf("dequeue 1 obj\n");
345 ret = test_ring_dequeue(r, cur_dst, esize[i], 1, api_type);
348 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], 1);
350 printf("dequeue 2 objs\n");
351 ret = test_ring_dequeue(r, cur_dst, esize[i], 2, api_type);
354 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], 2);
356 printf("dequeue MAX_BULK objs\n");
357 ret = test_ring_dequeue(r, cur_dst, esize[i], MAX_BULK,
361 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], MAX_BULK);
364 if (memcmp(src, dst, cur_dst - dst)) {
365 rte_hexdump(stdout, "src", src, cur_src - src);
366 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
367 printf("data after dequeue is not the same\n");
371 /* Free memory before test completed */
389 * Burst and bulk operations with sp/sc, mp/mc and default (during creation)
390 * Enqueue and dequeue to cover the entire ring length.
393 test_ring_burst_bulk_tests3(unsigned int api_type, unsigned int create_flags,
397 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
401 for (i = 0; i < RTE_DIM(esize); i++) {
402 test_ring_print_test_string(tname, api_type, esize[i]);
404 /* Create the ring */
405 r = test_ring_create("test_ring_burst_bulk_tests", esize[i],
406 RING_SIZE, SOCKET_ID_ANY, create_flags);
408 /* alloc dummy object pointers */
409 src = test_ring_calloc(RING_SIZE * 2, esize[i]);
412 test_ring_mem_init(src, RING_SIZE * 2, esize[i]);
415 /* alloc some room for copied objects */
416 dst = test_ring_calloc(RING_SIZE * 2, esize[i]);
421 printf("fill and empty the ring\n");
422 for (j = 0; j < RING_SIZE / MAX_BULK; j++) {
423 ret = test_ring_enqueue(r, cur_src, esize[i], MAX_BULK,
427 cur_src = test_ring_inc_ptr(cur_src, esize[i],
430 ret = test_ring_dequeue(r, cur_dst, esize[i], MAX_BULK,
434 cur_dst = test_ring_inc_ptr(cur_dst, esize[i],
439 if (memcmp(src, dst, cur_dst - dst)) {
440 rte_hexdump(stdout, "src", src, cur_src - src);
441 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
442 printf("data after dequeue is not the same\n");
446 /* Free memory before test completed */
464 * Burst and bulk operations with sp/sc, mp/mc and default (during creation)
465 * Enqueue till the ring is full and dequeue till the ring becomes empty.
468 test_ring_burst_bulk_tests4(unsigned int api_type, unsigned int create_flags,
472 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
475 unsigned int num_elems;
477 for (i = 0; i < RTE_DIM(esize); i++) {
478 test_ring_print_test_string(tname, api_type, esize[i]);
480 /* Create the ring */
481 r = test_ring_create("test_ring_burst_bulk_tests", esize[i],
482 RING_SIZE, SOCKET_ID_ANY, create_flags);
484 /* alloc dummy object pointers */
485 src = test_ring_calloc(RING_SIZE * 2, esize[i]);
488 test_ring_mem_init(src, RING_SIZE * 2, esize[i]);
491 /* alloc some room for copied objects */
492 dst = test_ring_calloc(RING_SIZE * 2, esize[i]);
497 printf("Test enqueue without enough memory space\n");
498 for (j = 0; j < (RING_SIZE/MAX_BULK - 1); j++) {
499 ret = test_ring_enqueue(r, cur_src, esize[i], MAX_BULK,
503 cur_src = test_ring_inc_ptr(cur_src, esize[i],
507 printf("Enqueue 2 objects, free entries = MAX_BULK - 2\n");
508 ret = test_ring_enqueue(r, cur_src, esize[i], 2, api_type);
511 cur_src = test_ring_inc_ptr(cur_src, esize[i], 2);
513 printf("Enqueue the remaining entries = MAX_BULK - 3\n");
514 /* Bulk APIs enqueue exact number of elements */
515 if ((api_type & TEST_RING_ELEM_BULK) == TEST_RING_ELEM_BULK)
516 num_elems = MAX_BULK - 3;
518 num_elems = MAX_BULK;
519 /* Always one free entry left */
520 ret = test_ring_enqueue(r, cur_src, esize[i], num_elems,
522 if (ret != MAX_BULK - 3)
524 cur_src = test_ring_inc_ptr(cur_src, esize[i], MAX_BULK - 3);
526 printf("Test if ring is full\n");
527 if (rte_ring_full(r) != 1)
530 printf("Test enqueue for a full entry\n");
531 ret = test_ring_enqueue(r, cur_src, esize[i], MAX_BULK,
536 printf("Test dequeue without enough objects\n");
537 for (j = 0; j < RING_SIZE / MAX_BULK - 1; j++) {
538 ret = test_ring_dequeue(r, cur_dst, esize[i], MAX_BULK,
542 cur_dst = test_ring_inc_ptr(cur_dst, esize[i],
546 /* Available memory space for the exact MAX_BULK entries */
547 ret = test_ring_dequeue(r, cur_dst, esize[i], 2, api_type);
550 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], 2);
552 /* Bulk APIs enqueue exact number of elements */
553 if ((api_type & TEST_RING_ELEM_BULK) == TEST_RING_ELEM_BULK)
554 num_elems = MAX_BULK - 3;
556 num_elems = MAX_BULK;
557 ret = test_ring_dequeue(r, cur_dst, esize[i], num_elems,
559 if (ret != MAX_BULK - 3)
561 cur_dst = test_ring_inc_ptr(cur_dst, esize[i], MAX_BULK - 3);
563 printf("Test if ring is empty\n");
564 /* Check if ring is empty */
565 if (rte_ring_empty(r) != 1)
569 if (memcmp(src, dst, cur_dst - dst)) {
570 rte_hexdump(stdout, "src", src, cur_src - src);
571 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
572 printf("data after dequeue is not the same\n");
576 /* Free memory before test completed */
594 * Test default, single element, bulk and burst APIs
597 test_ring_basic_ex(void)
601 struct rte_ring *rp = NULL;
604 for (i = 0; i < RTE_DIM(esize); i++) {
605 obj = test_ring_calloc(RING_SIZE, esize[i]);
607 printf("%s: failed to alloc memory\n", __func__);
611 rp = test_ring_create("test_ring_basic_ex", esize[i], RING_SIZE,
613 RING_F_SP_ENQ | RING_F_SC_DEQ);
615 printf("%s: failed to create ring\n", __func__);
619 if (rte_ring_lookup("test_ring_basic_ex") != rp) {
620 printf("%s: failed to find ring\n", __func__);
624 if (rte_ring_empty(rp) != 1) {
625 printf("%s: ring is not empty but it should be\n",
630 printf("%u ring entries are now free\n",
631 rte_ring_free_count(rp));
633 for (j = 0; j < RING_SIZE; j++) {
634 test_ring_enqueue(rp, obj, esize[i], 1,
635 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
638 if (rte_ring_full(rp) != 1) {
639 printf("%s: ring is not full but it should be\n",
644 for (j = 0; j < RING_SIZE; j++) {
645 test_ring_dequeue(rp, obj, esize[i], 1,
646 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
649 if (rte_ring_empty(rp) != 1) {
650 printf("%s: ring is not empty but it should be\n",
655 /* Following tests use the configured flags to decide
658 /* Covering the ring burst operation */
659 ret = test_ring_enqueue(rp, obj, esize[i], 2,
660 TEST_RING_THREAD_DEF | TEST_RING_ELEM_BURST);
662 printf("%s: rte_ring_enqueue_burst fails\n", __func__);
666 ret = test_ring_dequeue(rp, obj, esize[i], 2,
667 TEST_RING_THREAD_DEF | TEST_RING_ELEM_BURST);
669 printf("%s: rte_ring_dequeue_burst fails\n", __func__);
673 /* Covering the ring bulk operation */
674 ret = test_ring_enqueue(rp, obj, esize[i], 2,
675 TEST_RING_THREAD_DEF | TEST_RING_ELEM_BULK);
677 printf("%s: rte_ring_enqueue_bulk fails\n", __func__);
681 ret = test_ring_dequeue(rp, obj, esize[i], 2,
682 TEST_RING_THREAD_DEF | TEST_RING_ELEM_BULK);
684 printf("%s: rte_ring_dequeue_bulk fails\n", __func__);
705 * Basic test cases with exact size ring.
708 test_ring_with_exact_size(void)
710 struct rte_ring *std_r = NULL, *exact_sz_r = NULL;
713 const unsigned int ring_sz = 16;
717 for (i = 0; i < RTE_DIM(esize); i++) {
718 test_ring_print_test_string("Test exact size ring",
719 TEST_RING_IGNORE_API_TYPE,
722 /* alloc object pointers. Allocate one extra object
723 * and create an unaligned address.
725 obj_orig = test_ring_calloc(17, esize[i]);
726 if (obj_orig == NULL)
728 obj = ((char *)obj_orig) + 1;
730 std_r = test_ring_create("std", esize[i], ring_sz,
732 RING_F_SP_ENQ | RING_F_SC_DEQ);
734 printf("%s: error, can't create std ring\n", __func__);
737 exact_sz_r = test_ring_create("exact sz", esize[i], ring_sz,
739 RING_F_SP_ENQ | RING_F_SC_DEQ |
741 if (exact_sz_r == NULL) {
742 printf("%s: error, can't create exact size ring\n",
748 * Check that the exact size ring is bigger than the
751 if (rte_ring_get_size(std_r) >= rte_ring_get_size(exact_sz_r)) {
752 printf("%s: error, std ring (size: %u) is not smaller than exact size one (size %u)\n",
754 rte_ring_get_size(std_r),
755 rte_ring_get_size(exact_sz_r));
759 * check that the exact_sz_ring can hold one more element
760 * than the standard ring. (16 vs 15 elements)
762 for (j = 0; j < ring_sz - 1; j++) {
763 test_ring_enqueue(std_r, obj, esize[i], 1,
764 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
765 test_ring_enqueue(exact_sz_r, obj, esize[i], 1,
766 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
768 ret = test_ring_enqueue(std_r, obj, esize[i], 1,
769 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
770 if (ret != -ENOBUFS) {
771 printf("%s: error, unexpected successful enqueue\n",
775 ret = test_ring_enqueue(exact_sz_r, obj, esize[i], 1,
776 TEST_RING_THREAD_DEF | TEST_RING_ELEM_SINGLE);
777 if (ret == -ENOBUFS) {
778 printf("%s: error, enqueue failed\n", __func__);
782 /* check that dequeue returns the expected number of elements */
783 ret = test_ring_dequeue(exact_sz_r, obj, esize[i], ring_sz,
784 TEST_RING_THREAD_DEF | TEST_RING_ELEM_BURST);
785 if (ret != (int)ring_sz) {
786 printf("%s: error, failed to dequeue expected nb of elements\n",
791 /* check that the capacity function returns expected value */
792 if (rte_ring_get_capacity(exact_sz_r) != ring_sz) {
793 printf("%s: error, incorrect ring capacity reported\n",
799 rte_ring_free(std_r);
800 rte_ring_free(exact_sz_r);
810 rte_ring_free(std_r);
811 rte_ring_free(exact_sz_r);
822 static const struct {
823 uint32_t create_flags;
825 } test_sync_modes[] = {
827 RING_F_MP_RTS_ENQ | RING_F_MC_RTS_DEQ,
831 RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ,
836 /* Negative test cases */
837 if (test_ring_negative_tests() < 0)
840 /* Some basic operations */
841 if (test_ring_basic_ex() < 0)
844 if (test_ring_with_exact_size() < 0)
847 /* Burst and bulk operations with sp/sc, mp/mc and default.
848 * The test cases are split into smaller test cases to
849 * help clang compile faster.
851 tname = "Test standard ring";
853 for (j = TEST_RING_ELEM_BULK; j <= TEST_RING_ELEM_BURST; j <<= 1)
854 for (i = TEST_RING_THREAD_DEF;
855 i <= TEST_RING_THREAD_MPMC; i <<= 1)
856 if (test_ring_burst_bulk_tests1(i | j, 0, tname) < 0)
859 for (j = TEST_RING_ELEM_BULK; j <= TEST_RING_ELEM_BURST; j <<= 1)
860 for (i = TEST_RING_THREAD_DEF;
861 i <= TEST_RING_THREAD_MPMC; i <<= 1)
862 if (test_ring_burst_bulk_tests2(i | j, 0, tname) < 0)
865 for (j = TEST_RING_ELEM_BULK; j <= TEST_RING_ELEM_BURST; j <<= 1)
866 for (i = TEST_RING_THREAD_DEF;
867 i <= TEST_RING_THREAD_MPMC; i <<= 1)
868 if (test_ring_burst_bulk_tests3(i | j, 0, tname) < 0)
871 for (j = TEST_RING_ELEM_BULK; j <= TEST_RING_ELEM_BURST; j <<= 1)
872 for (i = TEST_RING_THREAD_DEF;
873 i <= TEST_RING_THREAD_MPMC; i <<= 1)
874 if (test_ring_burst_bulk_tests4(i | j, 0, tname) < 0)
877 /* Burst and bulk operations with MT_RTS and MT_HTS sync modes */
878 for (i = 0; i != RTE_DIM(test_sync_modes); i++) {
879 for (j = TEST_RING_ELEM_BULK; j <= TEST_RING_ELEM_BURST;
882 rc = test_ring_burst_bulk_tests1(
883 TEST_RING_THREAD_DEF | j,
884 test_sync_modes[i].create_flags,
885 test_sync_modes[i].name);
889 rc = test_ring_burst_bulk_tests2(
890 TEST_RING_THREAD_DEF | j,
891 test_sync_modes[i].create_flags,
892 test_sync_modes[i].name);
896 rc = test_ring_burst_bulk_tests3(
897 TEST_RING_THREAD_DEF | j,
898 test_sync_modes[i].create_flags,
899 test_sync_modes[i].name);
903 rc = test_ring_burst_bulk_tests3(
904 TEST_RING_THREAD_DEF | j,
905 test_sync_modes[i].create_flags,
906 test_sync_modes[i].name);
912 /* dump the ring status */
913 rte_ring_list_dump(stdout);
922 REGISTER_TEST_COMMAND(ring_autotest, test_ring);