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_random.h>
27 #include <rte_errno.h>
28 #include <rte_hexdump.h>
36 * #. Basic tests: done on one core:
38 * - Using single producer/single consumer functions:
40 * - Enqueue one object, two objects, MAX_BULK objects
41 * - Dequeue one object, two objects, MAX_BULK objects
42 * - Check that dequeued pointers are correct
44 * - Using multi producers/multi consumers functions:
46 * - Enqueue one object, two objects, MAX_BULK objects
47 * - Dequeue one object, two objects, MAX_BULK objects
48 * - Check that dequeued pointers are correct
50 * #. Performance tests.
52 * Tests done in test_ring_perf.c
55 #define RING_SIZE 4096
58 static rte_atomic32_t synchro;
60 static struct rte_ring *r;
62 #define TEST_RING_VERIFY(exp) \
64 printf("error at %s:%d\tcondition " #exp " failed\n", \
65 __func__, __LINE__); \
66 rte_ring_dump(stdout, r); \
70 #define TEST_RING_FULL_EMTPY_ITER 8
73 * helper routine for test_ring_basic
76 test_ring_basic_full_empty(void * const src[], void *dst[])
79 const unsigned rsz = RING_SIZE - 1;
81 printf("Basic full/empty test\n");
83 for (i = 0; TEST_RING_FULL_EMTPY_ITER != i; i++) {
85 /* random shift in the ring */
86 rand = RTE_MAX(rte_rand() % RING_SIZE, 1UL);
87 printf("%s: iteration %u, random shift: %u;\n",
89 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rand,
91 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rand,
95 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rsz, NULL) != 0);
96 TEST_RING_VERIFY(0 == rte_ring_free_count(r));
97 TEST_RING_VERIFY(rsz == rte_ring_count(r));
98 TEST_RING_VERIFY(rte_ring_full(r));
99 TEST_RING_VERIFY(0 == rte_ring_empty(r));
102 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rsz,
104 TEST_RING_VERIFY(rsz == rte_ring_free_count(r));
105 TEST_RING_VERIFY(0 == rte_ring_count(r));
106 TEST_RING_VERIFY(0 == rte_ring_full(r));
107 TEST_RING_VERIFY(rte_ring_empty(r));
110 TEST_RING_VERIFY(0 == memcmp(src, dst, rsz));
111 rte_ring_dump(stdout, r);
117 test_ring_basic(void)
119 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
121 unsigned i, num_elems;
123 /* alloc dummy object pointers */
124 src = malloc(RING_SIZE*2*sizeof(void *));
128 for (i = 0; i < RING_SIZE*2 ; i++) {
129 src[i] = (void *)(unsigned long)i;
133 /* alloc some room for copied objects */
134 dst = malloc(RING_SIZE*2*sizeof(void *));
138 memset(dst, 0, RING_SIZE*2*sizeof(void *));
141 printf("enqueue 1 obj\n");
142 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 1, NULL);
147 printf("enqueue 2 objs\n");
148 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 2, NULL);
153 printf("enqueue MAX_BULK objs\n");
154 ret = rte_ring_sp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
159 printf("dequeue 1 obj\n");
160 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 1, NULL);
165 printf("dequeue 2 objs\n");
166 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 2, NULL);
171 printf("dequeue MAX_BULK objs\n");
172 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
178 if (memcmp(src, dst, cur_dst - dst)) {
179 rte_hexdump(stdout, "src", src, cur_src - src);
180 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
181 printf("data after dequeue is not the same\n");
187 printf("enqueue 1 obj\n");
188 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 1, NULL);
193 printf("enqueue 2 objs\n");
194 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 2, NULL);
199 printf("enqueue MAX_BULK objs\n");
200 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
205 printf("dequeue 1 obj\n");
206 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 1, NULL);
211 printf("dequeue 2 objs\n");
212 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 2, NULL);
217 printf("dequeue MAX_BULK objs\n");
218 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
224 if (memcmp(src, dst, cur_dst - dst)) {
225 rte_hexdump(stdout, "src", src, cur_src - src);
226 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
227 printf("data after dequeue is not the same\n");
233 printf("fill and empty the ring\n");
234 for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
235 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
239 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
246 if (memcmp(src, dst, cur_dst - dst)) {
247 rte_hexdump(stdout, "src", src, cur_src - src);
248 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
249 printf("data after dequeue is not the same\n");
253 if (test_ring_basic_full_empty(src, dst) != 0)
259 printf("test default bulk enqueue / dequeue\n");
265 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
266 cur_src += num_elems;
268 printf("Cannot enqueue\n");
271 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
272 cur_src += num_elems;
274 printf("Cannot enqueue\n");
277 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems, NULL);
278 cur_dst += num_elems;
280 printf("Cannot dequeue\n");
283 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems, NULL);
284 cur_dst += num_elems;
286 printf("Cannot dequeue2\n");
291 if (memcmp(src, dst, cur_dst - dst)) {
292 rte_hexdump(stdout, "src", src, cur_src - src);
293 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
294 printf("data after dequeue is not the same\n");
301 ret = rte_ring_mp_enqueue(r, cur_src);
305 ret = rte_ring_mc_dequeue(r, cur_dst);
320 test_ring_burst_basic(void)
322 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
326 /* alloc dummy object pointers */
327 src = malloc(RING_SIZE*2*sizeof(void *));
331 for (i = 0; i < RING_SIZE*2 ; i++) {
332 src[i] = (void *)(unsigned long)i;
336 /* alloc some room for copied objects */
337 dst = malloc(RING_SIZE*2*sizeof(void *));
341 memset(dst, 0, RING_SIZE*2*sizeof(void *));
344 printf("Test SP & SC basic functions \n");
345 printf("enqueue 1 obj\n");
346 ret = rte_ring_sp_enqueue_burst(r, cur_src, 1, NULL);
351 printf("enqueue 2 objs\n");
352 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
357 printf("enqueue MAX_BULK objs\n");
358 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
363 printf("dequeue 1 obj\n");
364 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 1, NULL);
369 printf("dequeue 2 objs\n");
370 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2, NULL);
375 printf("dequeue MAX_BULK objs\n");
376 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
382 if (memcmp(src, dst, cur_dst - dst)) {
383 rte_hexdump(stdout, "src", src, cur_src - src);
384 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
385 printf("data after dequeue is not the same\n");
392 printf("Test enqueue without enough memory space \n");
393 for (i = 0; i< (RING_SIZE/MAX_BULK - 1); i++) {
394 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
400 printf("Enqueue 2 objects, free entries = MAX_BULK - 2 \n");
401 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
406 printf("Enqueue the remaining entries = MAX_BULK - 2 \n");
407 /* Always one free entry left */
408 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
409 cur_src += MAX_BULK - 3;
410 if (ret != MAX_BULK - 3)
413 printf("Test if ring is full \n");
414 if (rte_ring_full(r) != 1)
417 printf("Test enqueue for a full entry \n");
418 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
422 printf("Test dequeue without enough objects \n");
423 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
424 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
430 /* Available memory space for the exact MAX_BULK entries */
431 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2, NULL);
436 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
437 cur_dst += MAX_BULK - 3;
438 if (ret != MAX_BULK - 3)
441 printf("Test if ring is empty \n");
442 /* Check if ring is empty */
443 if (1 != rte_ring_empty(r))
447 if (memcmp(src, dst, cur_dst - dst)) {
448 rte_hexdump(stdout, "src", src, cur_src - src);
449 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
450 printf("data after dequeue is not the same\n");
457 printf("Test MP & MC basic functions \n");
459 printf("enqueue 1 obj\n");
460 ret = rte_ring_mp_enqueue_burst(r, cur_src, 1, NULL);
465 printf("enqueue 2 objs\n");
466 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
471 printf("enqueue MAX_BULK objs\n");
472 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
477 printf("dequeue 1 obj\n");
478 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 1, NULL);
483 printf("dequeue 2 objs\n");
484 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2, NULL);
489 printf("dequeue MAX_BULK objs\n");
490 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
496 if (memcmp(src, dst, cur_dst - dst)) {
497 rte_hexdump(stdout, "src", src, cur_src - src);
498 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
499 printf("data after dequeue is not the same\n");
506 printf("fill and empty the ring\n");
507 for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
508 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
512 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
519 if (memcmp(src, dst, cur_dst - dst)) {
520 rte_hexdump(stdout, "src", src, cur_src - src);
521 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
522 printf("data after dequeue is not the same\n");
529 printf("Test enqueue without enough memory space \n");
530 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
531 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
537 /* Available memory space for the exact MAX_BULK objects */
538 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
543 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
544 cur_src += MAX_BULK - 3;
545 if (ret != MAX_BULK - 3)
549 printf("Test dequeue without enough objects \n");
550 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
551 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
557 /* Available objects - the exact MAX_BULK */
558 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2, NULL);
563 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
564 cur_dst += MAX_BULK - 3;
565 if (ret != MAX_BULK - 3)
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");
579 printf("Covering rte_ring_enqueue_burst functions \n");
581 ret = rte_ring_enqueue_burst(r, cur_src, 2, NULL);
586 ret = rte_ring_dequeue_burst(r, cur_dst, 2, NULL);
591 /* Free memory before test completed */
603 * it will always fail to create ring with a wrong ring size number in this function
606 test_ring_creation_with_wrong_size(void)
608 struct rte_ring * rp = NULL;
610 /* Test if ring size is not power of 2 */
611 rp = rte_ring_create("test_bad_ring_size", RING_SIZE + 1, SOCKET_ID_ANY, 0);
616 /* Test if ring size is exceeding the limit */
617 rp = rte_ring_create("test_bad_ring_size", (RTE_RING_SZ_MASK + 1), SOCKET_ID_ANY, 0);
625 * it tests if it would always fail to create ring with an used ring name
628 test_ring_creation_with_an_used_name(void)
630 struct rte_ring * rp;
632 rp = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
640 * Test to if a non-power of 2 count causes the create
641 * function to fail correctly
644 test_create_count_odd(void)
646 struct rte_ring *r = rte_ring_create("test_ring_count",
647 4097, SOCKET_ID_ANY, 0 );
655 test_lookup_null(void)
657 struct rte_ring *rlp = rte_ring_lookup("ring_not_found");
659 if (rte_errno != ENOENT){
660 printf( "test failed to returnn error on null pointer\n");
667 * it tests some more basic ring operations
670 test_ring_basic_ex(void)
674 struct rte_ring * rp;
677 obj = rte_calloc("test_ring_basic_ex_malloc", RING_SIZE, sizeof(void *), 0);
679 printf("test_ring_basic_ex fail to rte_malloc\n");
683 rp = rte_ring_create("test_ring_basic_ex", RING_SIZE, SOCKET_ID_ANY,
684 RING_F_SP_ENQ | RING_F_SC_DEQ);
686 printf("test_ring_basic_ex fail to create ring\n");
690 if (rte_ring_lookup("test_ring_basic_ex") != rp) {
694 if (rte_ring_empty(rp) != 1) {
695 printf("test_ring_basic_ex ring is not empty but it should be\n");
699 printf("%u ring entries are now free\n", rte_ring_free_count(rp));
701 for (i = 0; i < RING_SIZE; i ++) {
702 rte_ring_enqueue(rp, obj[i]);
705 if (rte_ring_full(rp) != 1) {
706 printf("test_ring_basic_ex ring is not full but it should be\n");
710 for (i = 0; i < RING_SIZE; i ++) {
711 rte_ring_dequeue(rp, &obj[i]);
714 if (rte_ring_empty(rp) != 1) {
715 printf("test_ring_basic_ex ring is not empty but it should be\n");
719 /* Covering the ring burst operation */
720 ret = rte_ring_enqueue_burst(rp, obj, 2, NULL);
722 printf("test_ring_basic_ex: rte_ring_enqueue_burst fails \n");
726 ret = rte_ring_dequeue_burst(rp, obj, 2, NULL);
728 printf("test_ring_basic_ex: rte_ring_dequeue_burst fails \n");
741 test_ring_with_exact_size(void)
743 struct rte_ring *std_ring = NULL, *exact_sz_ring = NULL;
745 static const unsigned int ring_sz = RTE_DIM(ptr_array);
749 std_ring = rte_ring_create("std", ring_sz, rte_socket_id(),
750 RING_F_SP_ENQ | RING_F_SC_DEQ);
751 if (std_ring == NULL) {
752 printf("%s: error, can't create std ring\n", __func__);
755 exact_sz_ring = rte_ring_create("exact sz", ring_sz, rte_socket_id(),
756 RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
757 if (exact_sz_ring == NULL) {
758 printf("%s: error, can't create exact size ring\n", __func__);
763 * Check that the exact size ring is bigger than the standard ring
765 if (rte_ring_get_size(std_ring) >= rte_ring_get_size(exact_sz_ring)) {
766 printf("%s: error, std ring (size: %u) is not smaller than exact size one (size %u)\n",
768 rte_ring_get_size(std_ring),
769 rte_ring_get_size(exact_sz_ring));
773 * check that the exact_sz_ring can hold one more element than the
774 * standard ring. (16 vs 15 elements)
776 for (i = 0; i < ring_sz - 1; i++) {
777 rte_ring_enqueue(std_ring, NULL);
778 rte_ring_enqueue(exact_sz_ring, NULL);
780 if (rte_ring_enqueue(std_ring, NULL) != -ENOBUFS) {
781 printf("%s: error, unexpected successful enqueue\n", __func__);
784 if (rte_ring_enqueue(exact_sz_ring, NULL) == -ENOBUFS) {
785 printf("%s: error, enqueue failed\n", __func__);
789 /* check that dequeue returns the expected number of elements */
790 if (rte_ring_dequeue_burst(exact_sz_ring, ptr_array,
791 RTE_DIM(ptr_array), NULL) != ring_sz) {
792 printf("%s: error, failed to dequeue expected nb of elements\n",
797 /* check that the capacity function returns expected value */
798 if (rte_ring_get_capacity(exact_sz_ring) != ring_sz) {
799 printf("%s: error, incorrect ring capacity reported\n",
804 ret = 0; /* all ok if we get here */
806 rte_ring_free(std_ring);
807 rte_ring_free(exact_sz_ring);
814 /* some more basic operations */
815 if (test_ring_basic_ex() < 0)
818 rte_atomic32_init(&synchro);
821 r = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
825 /* retrieve the ring from its name */
826 if (rte_ring_lookup("test") != r) {
827 printf("Cannot lookup ring from its name\n");
831 /* burst operations */
832 if (test_ring_burst_basic() < 0)
835 /* basic operations */
836 if (test_ring_basic() < 0)
839 /* basic operations */
840 if ( test_create_count_odd() < 0){
841 printf ("Test failed to detect odd count\n");
845 printf ( "Test detected odd count\n");
847 if ( test_lookup_null() < 0){
848 printf ("Test failed to detect NULL ring lookup\n");
852 printf ( "Test detected NULL ring lookup \n");
854 /* test of creating ring with wrong size */
855 if (test_ring_creation_with_wrong_size() < 0)
858 /* test of creation ring with an used name */
859 if (test_ring_creation_with_an_used_name() < 0)
862 if (test_ring_with_exact_size() < 0)
865 /* dump the ring status */
866 rte_ring_list_dump(stdout);
871 REGISTER_TEST_COMMAND(ring_autotest, test_ring);