4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/queue.h>
43 #include <rte_common.h>
45 #include <rte_memory.h>
46 #include <rte_memzone.h>
47 #include <rte_launch.h>
48 #include <rte_cycles.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_atomic.h>
53 #include <rte_branch_prediction.h>
54 #include <rte_malloc.h>
56 #include <rte_random.h>
57 #include <rte_errno.h>
58 #include <rte_hexdump.h>
66 * #. Basic tests: done on one core:
68 * - Using single producer/single consumer functions:
70 * - Enqueue one object, two objects, MAX_BULK objects
71 * - Dequeue one object, two objects, MAX_BULK objects
72 * - Check that dequeued pointers are correct
74 * - Using multi producers/multi consumers functions:
76 * - Enqueue one object, two objects, MAX_BULK objects
77 * - Dequeue one object, two objects, MAX_BULK objects
78 * - Check that dequeued pointers are correct
80 * #. Performance tests.
82 * Tests done in test_ring_perf.c
85 #define RING_SIZE 4096
88 static rte_atomic32_t synchro;
90 static struct rte_ring *r;
92 #define TEST_RING_VERIFY(exp) \
94 printf("error at %s:%d\tcondition " #exp " failed\n", \
95 __func__, __LINE__); \
96 rte_ring_dump(stdout, r); \
100 #define TEST_RING_FULL_EMTPY_ITER 8
103 * helper routine for test_ring_basic
106 test_ring_basic_full_empty(void * const src[], void *dst[])
109 const unsigned rsz = RING_SIZE - 1;
111 printf("Basic full/empty test\n");
113 for (i = 0; TEST_RING_FULL_EMTPY_ITER != i; i++) {
115 /* random shift in the ring */
116 rand = RTE_MAX(rte_rand() % RING_SIZE, 1UL);
117 printf("%s: iteration %u, random shift: %u;\n",
119 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rand,
121 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rand,
125 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rsz, NULL) != 0);
126 TEST_RING_VERIFY(0 == rte_ring_free_count(r));
127 TEST_RING_VERIFY(rsz == rte_ring_count(r));
128 TEST_RING_VERIFY(rte_ring_full(r));
129 TEST_RING_VERIFY(0 == rte_ring_empty(r));
132 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rsz,
134 TEST_RING_VERIFY(rsz == rte_ring_free_count(r));
135 TEST_RING_VERIFY(0 == rte_ring_count(r));
136 TEST_RING_VERIFY(0 == rte_ring_full(r));
137 TEST_RING_VERIFY(rte_ring_empty(r));
140 TEST_RING_VERIFY(0 == memcmp(src, dst, rsz));
141 rte_ring_dump(stdout, r);
147 test_ring_basic(void)
149 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
151 unsigned i, num_elems;
153 /* alloc dummy object pointers */
154 src = malloc(RING_SIZE*2*sizeof(void *));
158 for (i = 0; i < RING_SIZE*2 ; i++) {
159 src[i] = (void *)(unsigned long)i;
163 /* alloc some room for copied objects */
164 dst = malloc(RING_SIZE*2*sizeof(void *));
168 memset(dst, 0, RING_SIZE*2*sizeof(void *));
171 printf("enqueue 1 obj\n");
172 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 1, NULL);
177 printf("enqueue 2 objs\n");
178 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 2, NULL);
183 printf("enqueue MAX_BULK objs\n");
184 ret = rte_ring_sp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
189 printf("dequeue 1 obj\n");
190 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 1, NULL);
195 printf("dequeue 2 objs\n");
196 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 2, NULL);
201 printf("dequeue MAX_BULK objs\n");
202 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
208 if (memcmp(src, dst, cur_dst - dst)) {
209 rte_hexdump(stdout, "src", src, cur_src - src);
210 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
211 printf("data after dequeue is not the same\n");
217 printf("enqueue 1 obj\n");
218 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 1, NULL);
223 printf("enqueue 2 objs\n");
224 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 2, NULL);
229 printf("enqueue MAX_BULK objs\n");
230 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
235 printf("dequeue 1 obj\n");
236 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 1, NULL);
241 printf("dequeue 2 objs\n");
242 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 2, NULL);
247 printf("dequeue MAX_BULK objs\n");
248 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
254 if (memcmp(src, dst, cur_dst - dst)) {
255 rte_hexdump(stdout, "src", src, cur_src - src);
256 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
257 printf("data after dequeue is not the same\n");
263 printf("fill and empty the ring\n");
264 for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
265 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
269 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
276 if (memcmp(src, dst, cur_dst - dst)) {
277 rte_hexdump(stdout, "src", src, cur_src - src);
278 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
279 printf("data after dequeue is not the same\n");
283 if (test_ring_basic_full_empty(src, dst) != 0)
289 printf("test default bulk enqueue / dequeue\n");
295 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
296 cur_src += num_elems;
298 printf("Cannot enqueue\n");
301 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
302 cur_src += num_elems;
304 printf("Cannot enqueue\n");
307 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems, NULL);
308 cur_dst += num_elems;
310 printf("Cannot dequeue\n");
313 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems, NULL);
314 cur_dst += num_elems;
316 printf("Cannot dequeue2\n");
321 if (memcmp(src, dst, cur_dst - dst)) {
322 rte_hexdump(stdout, "src", src, cur_src - src);
323 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
324 printf("data after dequeue is not the same\n");
331 ret = rte_ring_mp_enqueue(r, cur_src);
335 ret = rte_ring_mc_dequeue(r, cur_dst);
350 test_ring_burst_basic(void)
352 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
356 /* alloc dummy object pointers */
357 src = malloc(RING_SIZE*2*sizeof(void *));
361 for (i = 0; i < RING_SIZE*2 ; i++) {
362 src[i] = (void *)(unsigned long)i;
366 /* alloc some room for copied objects */
367 dst = malloc(RING_SIZE*2*sizeof(void *));
371 memset(dst, 0, RING_SIZE*2*sizeof(void *));
374 printf("Test SP & SC basic functions \n");
375 printf("enqueue 1 obj\n");
376 ret = rte_ring_sp_enqueue_burst(r, cur_src, 1, NULL);
381 printf("enqueue 2 objs\n");
382 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
387 printf("enqueue MAX_BULK objs\n");
388 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
393 printf("dequeue 1 obj\n");
394 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 1, NULL);
399 printf("dequeue 2 objs\n");
400 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2, NULL);
405 printf("dequeue MAX_BULK objs\n");
406 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
412 if (memcmp(src, dst, cur_dst - dst)) {
413 rte_hexdump(stdout, "src", src, cur_src - src);
414 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
415 printf("data after dequeue is not the same\n");
422 printf("Test enqueue without enough memory space \n");
423 for (i = 0; i< (RING_SIZE/MAX_BULK - 1); i++) {
424 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
430 printf("Enqueue 2 objects, free entries = MAX_BULK - 2 \n");
431 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
436 printf("Enqueue the remaining entries = MAX_BULK - 2 \n");
437 /* Always one free entry left */
438 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
439 cur_src += MAX_BULK - 3;
440 if (ret != MAX_BULK - 3)
443 printf("Test if ring is full \n");
444 if (rte_ring_full(r) != 1)
447 printf("Test enqueue for a full entry \n");
448 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
452 printf("Test dequeue without enough objects \n");
453 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
454 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
460 /* Available memory space for the exact MAX_BULK entries */
461 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2, NULL);
466 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
467 cur_dst += MAX_BULK - 3;
468 if (ret != MAX_BULK - 3)
471 printf("Test if ring is empty \n");
472 /* Check if ring is empty */
473 if (1 != rte_ring_empty(r))
477 if (memcmp(src, dst, cur_dst - dst)) {
478 rte_hexdump(stdout, "src", src, cur_src - src);
479 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
480 printf("data after dequeue is not the same\n");
487 printf("Test MP & MC basic functions \n");
489 printf("enqueue 1 obj\n");
490 ret = rte_ring_mp_enqueue_burst(r, cur_src, 1, NULL);
495 printf("enqueue 2 objs\n");
496 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
501 printf("enqueue MAX_BULK objs\n");
502 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
507 printf("dequeue 1 obj\n");
508 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 1, NULL);
513 printf("dequeue 2 objs\n");
514 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2, NULL);
519 printf("dequeue MAX_BULK objs\n");
520 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
526 if (memcmp(src, dst, cur_dst - dst)) {
527 rte_hexdump(stdout, "src", src, cur_src - src);
528 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
529 printf("data after dequeue is not the same\n");
536 printf("fill and empty the ring\n");
537 for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
538 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
542 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
549 if (memcmp(src, dst, cur_dst - dst)) {
550 rte_hexdump(stdout, "src", src, cur_src - src);
551 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
552 printf("data after dequeue is not the same\n");
559 printf("Test enqueue without enough memory space \n");
560 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
561 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
567 /* Available memory space for the exact MAX_BULK objects */
568 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
573 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
574 cur_src += MAX_BULK - 3;
575 if (ret != MAX_BULK - 3)
579 printf("Test dequeue without enough objects \n");
580 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
581 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
587 /* Available objects - the exact MAX_BULK */
588 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2, NULL);
593 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
594 cur_dst += MAX_BULK - 3;
595 if (ret != MAX_BULK - 3)
599 if (memcmp(src, dst, cur_dst - dst)) {
600 rte_hexdump(stdout, "src", src, cur_src - src);
601 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
602 printf("data after dequeue is not the same\n");
609 printf("Covering rte_ring_enqueue_burst functions \n");
611 ret = rte_ring_enqueue_burst(r, cur_src, 2, NULL);
616 ret = rte_ring_dequeue_burst(r, cur_dst, 2, NULL);
621 /* Free memory before test completed */
633 * it will always fail to create ring with a wrong ring size number in this function
636 test_ring_creation_with_wrong_size(void)
638 struct rte_ring * rp = NULL;
640 /* Test if ring size is not power of 2 */
641 rp = rte_ring_create("test_bad_ring_size", RING_SIZE + 1, SOCKET_ID_ANY, 0);
646 /* Test if ring size is exceeding the limit */
647 rp = rte_ring_create("test_bad_ring_size", (RTE_RING_SZ_MASK + 1), SOCKET_ID_ANY, 0);
655 * it tests if it would always fail to create ring with an used ring name
658 test_ring_creation_with_an_used_name(void)
660 struct rte_ring * rp;
662 rp = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
670 * Test to if a non-power of 2 count causes the create
671 * function to fail correctly
674 test_create_count_odd(void)
676 struct rte_ring *r = rte_ring_create("test_ring_count",
677 4097, SOCKET_ID_ANY, 0 );
685 test_lookup_null(void)
687 struct rte_ring *rlp = rte_ring_lookup("ring_not_found");
689 if (rte_errno != ENOENT){
690 printf( "test failed to returnn error on null pointer\n");
697 * it tests some more basic ring operations
700 test_ring_basic_ex(void)
704 struct rte_ring * rp;
707 obj = rte_calloc("test_ring_basic_ex_malloc", RING_SIZE, sizeof(void *), 0);
709 printf("test_ring_basic_ex fail to rte_malloc\n");
713 rp = rte_ring_create("test_ring_basic_ex", RING_SIZE, SOCKET_ID_ANY,
714 RING_F_SP_ENQ | RING_F_SC_DEQ);
716 printf("test_ring_basic_ex fail to create ring\n");
720 if (rte_ring_lookup("test_ring_basic_ex") != rp) {
724 if (rte_ring_empty(rp) != 1) {
725 printf("test_ring_basic_ex ring is not empty but it should be\n");
729 printf("%u ring entries are now free\n", rte_ring_free_count(rp));
731 for (i = 0; i < RING_SIZE; i ++) {
732 rte_ring_enqueue(rp, obj[i]);
735 if (rte_ring_full(rp) != 1) {
736 printf("test_ring_basic_ex ring is not full but it should be\n");
740 for (i = 0; i < RING_SIZE; i ++) {
741 rte_ring_dequeue(rp, &obj[i]);
744 if (rte_ring_empty(rp) != 1) {
745 printf("test_ring_basic_ex ring is not empty but it should be\n");
749 /* Covering the ring burst operation */
750 ret = rte_ring_enqueue_burst(rp, obj, 2, NULL);
752 printf("test_ring_basic_ex: rte_ring_enqueue_burst fails \n");
756 ret = rte_ring_dequeue_burst(rp, obj, 2, NULL);
758 printf("test_ring_basic_ex: rte_ring_dequeue_burst fails \n");
771 test_ring_with_exact_size(void)
773 struct rte_ring *std_ring = NULL, *exact_sz_ring = NULL;
775 static const unsigned int ring_sz = RTE_DIM(ptr_array);
779 std_ring = rte_ring_create("std", ring_sz, rte_socket_id(),
780 RING_F_SP_ENQ | RING_F_SC_DEQ);
781 if (std_ring == NULL) {
782 printf("%s: error, can't create std ring\n", __func__);
785 exact_sz_ring = rte_ring_create("exact sz", ring_sz, rte_socket_id(),
786 RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
787 if (exact_sz_ring == NULL) {
788 printf("%s: error, can't create exact size ring\n", __func__);
793 * Check that the exact size ring is bigger than the standard ring
795 if (rte_ring_get_size(std_ring) >= rte_ring_get_size(exact_sz_ring)) {
796 printf("%s: error, std ring (size: %u) is not smaller than exact size one (size %u)\n",
798 rte_ring_get_size(std_ring),
799 rte_ring_get_size(exact_sz_ring));
803 * check that the exact_sz_ring can hold one more element than the
804 * standard ring. (16 vs 15 elements)
806 for (i = 0; i < ring_sz - 1; i++) {
807 rte_ring_enqueue(std_ring, NULL);
808 rte_ring_enqueue(exact_sz_ring, NULL);
810 if (rte_ring_enqueue(std_ring, NULL) != -ENOBUFS) {
811 printf("%s: error, unexpected successful enqueue\n", __func__);
814 if (rte_ring_enqueue(exact_sz_ring, NULL) == -ENOBUFS) {
815 printf("%s: error, enqueue failed\n", __func__);
819 /* check that dequeue returns the expected number of elements */
820 if (rte_ring_dequeue_burst(exact_sz_ring, ptr_array,
821 RTE_DIM(ptr_array), NULL) != ring_sz) {
822 printf("%s: error, failed to dequeue expected nb of elements\n",
827 /* check that the capacity function returns expected value */
828 if (rte_ring_get_capacity(exact_sz_ring) != ring_sz) {
829 printf("%s: error, incorrect ring capacity reported\n",
834 ret = 0; /* all ok if we get here */
836 rte_ring_free(std_ring);
837 rte_ring_free(exact_sz_ring);
844 /* some more basic operations */
845 if (test_ring_basic_ex() < 0)
848 rte_atomic32_init(&synchro);
851 r = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
855 /* retrieve the ring from its name */
856 if (rte_ring_lookup("test") != r) {
857 printf("Cannot lookup ring from its name\n");
861 /* burst operations */
862 if (test_ring_burst_basic() < 0)
865 /* basic operations */
866 if (test_ring_basic() < 0)
869 /* basic operations */
870 if ( test_create_count_odd() < 0){
871 printf ("Test failed to detect odd count\n");
875 printf ( "Test detected odd count\n");
877 if ( test_lookup_null() < 0){
878 printf ("Test failed to detect NULL ring lookup\n");
882 printf ( "Test detected NULL ring lookup \n");
884 /* test of creating ring with wrong size */
885 if (test_ring_creation_with_wrong_size() < 0)
888 /* test of creation ring with an used name */
889 if (test_ring_creation_with_an_used_name() < 0)
892 if (test_ring_with_exact_size() < 0)
895 /* dump the ring status */
896 rte_ring_list_dump(stdout);
901 REGISTER_TEST_COMMAND(ring_autotest, test_ring);