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_launch.h>
47 #include <rte_cycles.h>
49 #include <rte_per_lcore.h>
50 #include <rte_lcore.h>
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_malloc.h>
55 #include <rte_random.h>
56 #include <rte_errno.h>
57 #include <rte_hexdump.h>
65 * #. Basic tests: done on one core:
67 * - Using single producer/single consumer functions:
69 * - Enqueue one object, two objects, MAX_BULK objects
70 * - Dequeue one object, two objects, MAX_BULK objects
71 * - Check that dequeued pointers are correct
73 * - Using multi producers/multi consumers functions:
75 * - Enqueue one object, two objects, MAX_BULK objects
76 * - Dequeue one object, two objects, MAX_BULK objects
77 * - Check that dequeued pointers are correct
79 * #. Performance tests.
81 * Tests done in test_ring_perf.c
84 #define RING_SIZE 4096
87 static rte_atomic32_t synchro;
89 static struct rte_ring *r;
91 #define TEST_RING_VERIFY(exp) \
93 printf("error at %s:%d\tcondition " #exp " failed\n", \
94 __func__, __LINE__); \
95 rte_ring_dump(stdout, r); \
99 #define TEST_RING_FULL_EMTPY_ITER 8
102 * helper routine for test_ring_basic
105 test_ring_basic_full_empty(void * const src[], void *dst[])
108 const unsigned rsz = RING_SIZE - 1;
110 printf("Basic full/empty test\n");
112 for (i = 0; TEST_RING_FULL_EMTPY_ITER != i; i++) {
114 /* random shift in the ring */
115 rand = RTE_MAX(rte_rand() % RING_SIZE, 1UL);
116 printf("%s: iteration %u, random shift: %u;\n",
118 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rand,
120 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rand,
124 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rsz, NULL) != 0);
125 TEST_RING_VERIFY(0 == rte_ring_free_count(r));
126 TEST_RING_VERIFY(rsz == rte_ring_count(r));
127 TEST_RING_VERIFY(rte_ring_full(r));
128 TEST_RING_VERIFY(0 == rte_ring_empty(r));
131 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rsz,
133 TEST_RING_VERIFY(rsz == rte_ring_free_count(r));
134 TEST_RING_VERIFY(0 == rte_ring_count(r));
135 TEST_RING_VERIFY(0 == rte_ring_full(r));
136 TEST_RING_VERIFY(rte_ring_empty(r));
139 TEST_RING_VERIFY(0 == memcmp(src, dst, rsz));
140 rte_ring_dump(stdout, r);
146 test_ring_basic(void)
148 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
150 unsigned i, num_elems;
152 /* alloc dummy object pointers */
153 src = malloc(RING_SIZE*2*sizeof(void *));
157 for (i = 0; i < RING_SIZE*2 ; i++) {
158 src[i] = (void *)(unsigned long)i;
162 /* alloc some room for copied objects */
163 dst = malloc(RING_SIZE*2*sizeof(void *));
167 memset(dst, 0, RING_SIZE*2*sizeof(void *));
170 printf("enqueue 1 obj\n");
171 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 1, NULL);
176 printf("enqueue 2 objs\n");
177 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 2, NULL);
182 printf("enqueue MAX_BULK objs\n");
183 ret = rte_ring_sp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
188 printf("dequeue 1 obj\n");
189 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 1, NULL);
194 printf("dequeue 2 objs\n");
195 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 2, NULL);
200 printf("dequeue MAX_BULK objs\n");
201 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
207 if (memcmp(src, dst, cur_dst - dst)) {
208 rte_hexdump(stdout, "src", src, cur_src - src);
209 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
210 printf("data after dequeue is not the same\n");
216 printf("enqueue 1 obj\n");
217 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 1, NULL);
222 printf("enqueue 2 objs\n");
223 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 2, NULL);
228 printf("enqueue MAX_BULK objs\n");
229 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
234 printf("dequeue 1 obj\n");
235 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 1, NULL);
240 printf("dequeue 2 objs\n");
241 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 2, NULL);
246 printf("dequeue MAX_BULK objs\n");
247 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
253 if (memcmp(src, dst, cur_dst - dst)) {
254 rte_hexdump(stdout, "src", src, cur_src - src);
255 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
256 printf("data after dequeue is not the same\n");
262 printf("fill and empty the ring\n");
263 for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
264 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
268 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
275 if (memcmp(src, dst, cur_dst - dst)) {
276 rte_hexdump(stdout, "src", src, cur_src - src);
277 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
278 printf("data after dequeue is not the same\n");
282 if (test_ring_basic_full_empty(src, dst) != 0)
288 printf("test default bulk enqueue / dequeue\n");
294 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
295 cur_src += num_elems;
297 printf("Cannot enqueue\n");
300 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
301 cur_src += num_elems;
303 printf("Cannot enqueue\n");
306 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems, NULL);
307 cur_dst += num_elems;
309 printf("Cannot dequeue\n");
312 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems, NULL);
313 cur_dst += num_elems;
315 printf("Cannot dequeue2\n");
320 if (memcmp(src, dst, cur_dst - dst)) {
321 rte_hexdump(stdout, "src", src, cur_src - src);
322 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
323 printf("data after dequeue is not the same\n");
330 ret = rte_ring_mp_enqueue(r, cur_src);
334 ret = rte_ring_mc_dequeue(r, cur_dst);
349 test_ring_burst_basic(void)
351 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
355 /* alloc dummy object pointers */
356 src = malloc(RING_SIZE*2*sizeof(void *));
360 for (i = 0; i < RING_SIZE*2 ; i++) {
361 src[i] = (void *)(unsigned long)i;
365 /* alloc some room for copied objects */
366 dst = malloc(RING_SIZE*2*sizeof(void *));
370 memset(dst, 0, RING_SIZE*2*sizeof(void *));
373 printf("Test SP & SC basic functions \n");
374 printf("enqueue 1 obj\n");
375 ret = rte_ring_sp_enqueue_burst(r, cur_src, 1, NULL);
380 printf("enqueue 2 objs\n");
381 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
386 printf("enqueue MAX_BULK objs\n");
387 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
392 printf("dequeue 1 obj\n");
393 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 1, NULL);
398 printf("dequeue 2 objs\n");
399 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2, NULL);
404 printf("dequeue MAX_BULK objs\n");
405 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
411 if (memcmp(src, dst, cur_dst - dst)) {
412 rte_hexdump(stdout, "src", src, cur_src - src);
413 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
414 printf("data after dequeue is not the same\n");
421 printf("Test enqueue without enough memory space \n");
422 for (i = 0; i< (RING_SIZE/MAX_BULK - 1); i++) {
423 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
429 printf("Enqueue 2 objects, free entries = MAX_BULK - 2 \n");
430 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
435 printf("Enqueue the remaining entries = MAX_BULK - 2 \n");
436 /* Always one free entry left */
437 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
438 cur_src += MAX_BULK - 3;
439 if (ret != MAX_BULK - 3)
442 printf("Test if ring is full \n");
443 if (rte_ring_full(r) != 1)
446 printf("Test enqueue for a full entry \n");
447 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
451 printf("Test dequeue without enough objects \n");
452 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
453 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
459 /* Available memory space for the exact MAX_BULK entries */
460 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2, NULL);
465 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
466 cur_dst += MAX_BULK - 3;
467 if (ret != MAX_BULK - 3)
470 printf("Test if ring is empty \n");
471 /* Check if ring is empty */
472 if (1 != rte_ring_empty(r))
476 if (memcmp(src, dst, cur_dst - dst)) {
477 rte_hexdump(stdout, "src", src, cur_src - src);
478 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
479 printf("data after dequeue is not the same\n");
486 printf("Test MP & MC basic functions \n");
488 printf("enqueue 1 obj\n");
489 ret = rte_ring_mp_enqueue_burst(r, cur_src, 1, NULL);
494 printf("enqueue 2 objs\n");
495 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
500 printf("enqueue MAX_BULK objs\n");
501 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
506 printf("dequeue 1 obj\n");
507 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 1, NULL);
512 printf("dequeue 2 objs\n");
513 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2, NULL);
518 printf("dequeue MAX_BULK objs\n");
519 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
525 if (memcmp(src, dst, cur_dst - dst)) {
526 rte_hexdump(stdout, "src", src, cur_src - src);
527 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
528 printf("data after dequeue is not the same\n");
535 printf("fill and empty the ring\n");
536 for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
537 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
541 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
548 if (memcmp(src, dst, cur_dst - dst)) {
549 rte_hexdump(stdout, "src", src, cur_src - src);
550 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
551 printf("data after dequeue is not the same\n");
558 printf("Test enqueue without enough memory space \n");
559 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
560 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
566 /* Available memory space for the exact MAX_BULK objects */
567 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
572 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
573 cur_src += MAX_BULK - 3;
574 if (ret != MAX_BULK - 3)
578 printf("Test dequeue without enough objects \n");
579 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
580 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
586 /* Available objects - the exact MAX_BULK */
587 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2, NULL);
592 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
593 cur_dst += MAX_BULK - 3;
594 if (ret != MAX_BULK - 3)
598 if (memcmp(src, dst, cur_dst - dst)) {
599 rte_hexdump(stdout, "src", src, cur_src - src);
600 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
601 printf("data after dequeue is not the same\n");
608 printf("Covering rte_ring_enqueue_burst functions \n");
610 ret = rte_ring_enqueue_burst(r, cur_src, 2, NULL);
615 ret = rte_ring_dequeue_burst(r, cur_dst, 2, NULL);
620 /* Free memory before test completed */
632 * it will always fail to create ring with a wrong ring size number in this function
635 test_ring_creation_with_wrong_size(void)
637 struct rte_ring * rp = NULL;
639 /* Test if ring size is not power of 2 */
640 rp = rte_ring_create("test_bad_ring_size", RING_SIZE + 1, SOCKET_ID_ANY, 0);
645 /* Test if ring size is exceeding the limit */
646 rp = rte_ring_create("test_bad_ring_size", (RTE_RING_SZ_MASK + 1), SOCKET_ID_ANY, 0);
654 * it tests if it would always fail to create ring with an used ring name
657 test_ring_creation_with_an_used_name(void)
659 struct rte_ring * rp;
661 rp = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
669 * Test to if a non-power of 2 count causes the create
670 * function to fail correctly
673 test_create_count_odd(void)
675 struct rte_ring *r = rte_ring_create("test_ring_count",
676 4097, SOCKET_ID_ANY, 0 );
684 test_lookup_null(void)
686 struct rte_ring *rlp = rte_ring_lookup("ring_not_found");
688 if (rte_errno != ENOENT){
689 printf( "test failed to returnn error on null pointer\n");
696 * it tests some more basic ring operations
699 test_ring_basic_ex(void)
703 struct rte_ring * rp;
706 obj = rte_calloc("test_ring_basic_ex_malloc", RING_SIZE, sizeof(void *), 0);
708 printf("test_ring_basic_ex fail to rte_malloc\n");
712 rp = rte_ring_create("test_ring_basic_ex", RING_SIZE, SOCKET_ID_ANY,
713 RING_F_SP_ENQ | RING_F_SC_DEQ);
715 printf("test_ring_basic_ex fail to create ring\n");
719 if (rte_ring_lookup("test_ring_basic_ex") != rp) {
723 if (rte_ring_empty(rp) != 1) {
724 printf("test_ring_basic_ex ring is not empty but it should be\n");
728 printf("%u ring entries are now free\n", rte_ring_free_count(rp));
730 for (i = 0; i < RING_SIZE; i ++) {
731 rte_ring_enqueue(rp, obj[i]);
734 if (rte_ring_full(rp) != 1) {
735 printf("test_ring_basic_ex ring is not full but it should be\n");
739 for (i = 0; i < RING_SIZE; i ++) {
740 rte_ring_dequeue(rp, &obj[i]);
743 if (rte_ring_empty(rp) != 1) {
744 printf("test_ring_basic_ex ring is not empty but it should be\n");
748 /* Covering the ring burst operation */
749 ret = rte_ring_enqueue_burst(rp, obj, 2, NULL);
751 printf("test_ring_basic_ex: rte_ring_enqueue_burst fails \n");
755 ret = rte_ring_dequeue_burst(rp, obj, 2, NULL);
757 printf("test_ring_basic_ex: rte_ring_dequeue_burst fails \n");
770 test_ring_with_exact_size(void)
772 struct rte_ring *std_ring = NULL, *exact_sz_ring = NULL;
774 static const unsigned int ring_sz = RTE_DIM(ptr_array);
778 std_ring = rte_ring_create("std", ring_sz, rte_socket_id(),
779 RING_F_SP_ENQ | RING_F_SC_DEQ);
780 if (std_ring == NULL) {
781 printf("%s: error, can't create std ring\n", __func__);
784 exact_sz_ring = rte_ring_create("exact sz", ring_sz, rte_socket_id(),
785 RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
786 if (exact_sz_ring == NULL) {
787 printf("%s: error, can't create exact size ring\n", __func__);
792 * Check that the exact size ring is bigger than the standard ring
794 if (rte_ring_get_size(std_ring) >= rte_ring_get_size(exact_sz_ring)) {
795 printf("%s: error, std ring (size: %u) is not smaller than exact size one (size %u)\n",
797 rte_ring_get_size(std_ring),
798 rte_ring_get_size(exact_sz_ring));
802 * check that the exact_sz_ring can hold one more element than the
803 * standard ring. (16 vs 15 elements)
805 for (i = 0; i < ring_sz - 1; i++) {
806 rte_ring_enqueue(std_ring, NULL);
807 rte_ring_enqueue(exact_sz_ring, NULL);
809 if (rte_ring_enqueue(std_ring, NULL) != -ENOBUFS) {
810 printf("%s: error, unexpected successful enqueue\n", __func__);
813 if (rte_ring_enqueue(exact_sz_ring, NULL) == -ENOBUFS) {
814 printf("%s: error, enqueue failed\n", __func__);
818 /* check that dequeue returns the expected number of elements */
819 if (rte_ring_dequeue_burst(exact_sz_ring, ptr_array,
820 RTE_DIM(ptr_array), NULL) != ring_sz) {
821 printf("%s: error, failed to dequeue expected nb of elements\n",
826 /* check that the capacity function returns expected value */
827 if (rte_ring_get_capacity(exact_sz_ring) != ring_sz) {
828 printf("%s: error, incorrect ring capacity reported\n",
833 ret = 0; /* all ok if we get here */
835 rte_ring_free(std_ring);
836 rte_ring_free(exact_sz_ring);
843 /* some more basic operations */
844 if (test_ring_basic_ex() < 0)
847 rte_atomic32_init(&synchro);
850 r = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
854 /* retrieve the ring from its name */
855 if (rte_ring_lookup("test") != r) {
856 printf("Cannot lookup ring from its name\n");
860 /* burst operations */
861 if (test_ring_burst_basic() < 0)
864 /* basic operations */
865 if (test_ring_basic() < 0)
868 /* basic operations */
869 if ( test_create_count_odd() < 0){
870 printf ("Test failed to detect odd count\n");
874 printf ( "Test detected odd count\n");
876 if ( test_lookup_null() < 0){
877 printf ("Test failed to detect NULL ring lookup\n");
881 printf ( "Test detected NULL ring lookup \n");
883 /* test of creating ring with wrong size */
884 if (test_ring_creation_with_wrong_size() < 0)
887 /* test of creation ring with an used name */
888 if (test_ring_creation_with_an_used_name() < 0)
891 if (test_ring_with_exact_size() < 0)
894 /* dump the ring status */
895 rte_ring_list_dump(stdout);
900 REGISTER_TEST_COMMAND(ring_autotest, test_ring);