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 #define TEST_RING_VERIFY(exp) \
91 printf("error at %s:%d\tcondition " #exp " failed\n", \
92 __func__, __LINE__); \
93 rte_ring_dump(stdout, r); \
97 #define TEST_RING_FULL_EMTPY_ITER 8
100 * helper routine for test_ring_basic
103 test_ring_basic_full_empty(struct rte_ring *r, void * const src[], void *dst[])
106 const unsigned rsz = RING_SIZE - 1;
108 printf("Basic full/empty test\n");
110 for (i = 0; TEST_RING_FULL_EMTPY_ITER != i; i++) {
112 /* random shift in the ring */
113 rand = RTE_MAX(rte_rand() % RING_SIZE, 1UL);
114 printf("%s: iteration %u, random shift: %u;\n",
116 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rand,
118 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rand,
122 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r, src, rsz, NULL) != 0);
123 TEST_RING_VERIFY(0 == rte_ring_free_count(r));
124 TEST_RING_VERIFY(rsz == rte_ring_count(r));
125 TEST_RING_VERIFY(rte_ring_full(r));
126 TEST_RING_VERIFY(0 == rte_ring_empty(r));
129 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r, dst, rsz,
131 TEST_RING_VERIFY(rsz == rte_ring_free_count(r));
132 TEST_RING_VERIFY(0 == rte_ring_count(r));
133 TEST_RING_VERIFY(0 == rte_ring_full(r));
134 TEST_RING_VERIFY(rte_ring_empty(r));
137 TEST_RING_VERIFY(0 == memcmp(src, dst, rsz));
138 rte_ring_dump(stdout, r);
144 test_ring_basic(struct rte_ring *r)
146 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
148 unsigned i, num_elems;
150 /* alloc dummy object pointers */
151 src = malloc(RING_SIZE*2*sizeof(void *));
155 for (i = 0; i < RING_SIZE*2 ; i++) {
156 src[i] = (void *)(unsigned long)i;
160 /* alloc some room for copied objects */
161 dst = malloc(RING_SIZE*2*sizeof(void *));
165 memset(dst, 0, RING_SIZE*2*sizeof(void *));
168 printf("enqueue 1 obj\n");
169 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 1, NULL);
174 printf("enqueue 2 objs\n");
175 ret = rte_ring_sp_enqueue_bulk(r, cur_src, 2, NULL);
180 printf("enqueue MAX_BULK objs\n");
181 ret = rte_ring_sp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
186 printf("dequeue 1 obj\n");
187 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 1, NULL);
192 printf("dequeue 2 objs\n");
193 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, 2, NULL);
198 printf("dequeue MAX_BULK objs\n");
199 ret = rte_ring_sc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
205 if (memcmp(src, dst, cur_dst - dst)) {
206 rte_hexdump(stdout, "src", src, cur_src - src);
207 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
208 printf("data after dequeue is not the same\n");
214 printf("enqueue 1 obj\n");
215 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 1, NULL);
220 printf("enqueue 2 objs\n");
221 ret = rte_ring_mp_enqueue_bulk(r, cur_src, 2, NULL);
226 printf("enqueue MAX_BULK objs\n");
227 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
232 printf("dequeue 1 obj\n");
233 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 1, NULL);
238 printf("dequeue 2 objs\n");
239 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, 2, NULL);
244 printf("dequeue MAX_BULK objs\n");
245 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
251 if (memcmp(src, dst, cur_dst - dst)) {
252 rte_hexdump(stdout, "src", src, cur_src - src);
253 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
254 printf("data after dequeue is not the same\n");
260 printf("fill and empty the ring\n");
261 for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
262 ret = rte_ring_mp_enqueue_bulk(r, cur_src, MAX_BULK, NULL);
266 ret = rte_ring_mc_dequeue_bulk(r, cur_dst, MAX_BULK, NULL);
273 if (memcmp(src, dst, cur_dst - dst)) {
274 rte_hexdump(stdout, "src", src, cur_src - src);
275 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
276 printf("data after dequeue is not the same\n");
280 if (test_ring_basic_full_empty(r, src, dst) != 0)
286 printf("test default bulk enqueue / dequeue\n");
292 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
293 cur_src += num_elems;
295 printf("Cannot enqueue\n");
298 ret = rte_ring_enqueue_bulk(r, cur_src, num_elems, NULL);
299 cur_src += num_elems;
301 printf("Cannot enqueue\n");
304 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems, NULL);
305 cur_dst += num_elems;
307 printf("Cannot dequeue\n");
310 ret = rte_ring_dequeue_bulk(r, cur_dst, num_elems, NULL);
311 cur_dst += num_elems;
313 printf("Cannot dequeue2\n");
318 if (memcmp(src, dst, cur_dst - dst)) {
319 rte_hexdump(stdout, "src", src, cur_src - src);
320 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
321 printf("data after dequeue is not the same\n");
328 ret = rte_ring_mp_enqueue(r, cur_src);
332 ret = rte_ring_mc_dequeue(r, cur_dst);
347 test_ring_burst_basic(struct rte_ring *r)
349 void **src = NULL, **cur_src = NULL, **dst = NULL, **cur_dst = NULL;
353 /* alloc dummy object pointers */
354 src = malloc(RING_SIZE*2*sizeof(void *));
358 for (i = 0; i < RING_SIZE*2 ; i++) {
359 src[i] = (void *)(unsigned long)i;
363 /* alloc some room for copied objects */
364 dst = malloc(RING_SIZE*2*sizeof(void *));
368 memset(dst, 0, RING_SIZE*2*sizeof(void *));
371 printf("Test SP & SC basic functions \n");
372 printf("enqueue 1 obj\n");
373 ret = rte_ring_sp_enqueue_burst(r, cur_src, 1, NULL);
378 printf("enqueue 2 objs\n");
379 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
384 printf("enqueue MAX_BULK objs\n");
385 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
390 printf("dequeue 1 obj\n");
391 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 1, NULL);
396 printf("dequeue 2 objs\n");
397 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2, NULL);
402 printf("dequeue MAX_BULK objs\n");
403 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
409 if (memcmp(src, dst, cur_dst - dst)) {
410 rte_hexdump(stdout, "src", src, cur_src - src);
411 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
412 printf("data after dequeue is not the same\n");
419 printf("Test enqueue without enough memory space \n");
420 for (i = 0; i< (RING_SIZE/MAX_BULK - 1); i++) {
421 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
427 printf("Enqueue 2 objects, free entries = MAX_BULK - 2 \n");
428 ret = rte_ring_sp_enqueue_burst(r, cur_src, 2, NULL);
433 printf("Enqueue the remaining entries = MAX_BULK - 2 \n");
434 /* Always one free entry left */
435 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
436 cur_src += MAX_BULK - 3;
437 if (ret != MAX_BULK - 3)
440 printf("Test if ring is full \n");
441 if (rte_ring_full(r) != 1)
444 printf("Test enqueue for a full entry \n");
445 ret = rte_ring_sp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
449 printf("Test dequeue without enough objects \n");
450 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
451 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
457 /* Available memory space for the exact MAX_BULK entries */
458 ret = rte_ring_sc_dequeue_burst(r, cur_dst, 2, NULL);
463 ret = rte_ring_sc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
464 cur_dst += MAX_BULK - 3;
465 if (ret != MAX_BULK - 3)
468 printf("Test if ring is empty \n");
469 /* Check if ring is empty */
470 if (1 != rte_ring_empty(r))
474 if (memcmp(src, dst, cur_dst - dst)) {
475 rte_hexdump(stdout, "src", src, cur_src - src);
476 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
477 printf("data after dequeue is not the same\n");
484 printf("Test MP & MC basic functions \n");
486 printf("enqueue 1 obj\n");
487 ret = rte_ring_mp_enqueue_burst(r, cur_src, 1, NULL);
492 printf("enqueue 2 objs\n");
493 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
498 printf("enqueue MAX_BULK objs\n");
499 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
504 printf("dequeue 1 obj\n");
505 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 1, NULL);
510 printf("dequeue 2 objs\n");
511 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2, NULL);
516 printf("dequeue MAX_BULK objs\n");
517 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
523 if (memcmp(src, dst, cur_dst - dst)) {
524 rte_hexdump(stdout, "src", src, cur_src - src);
525 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
526 printf("data after dequeue is not the same\n");
533 printf("fill and empty the ring\n");
534 for (i = 0; i<RING_SIZE/MAX_BULK; i++) {
535 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
539 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
546 if (memcmp(src, dst, cur_dst - dst)) {
547 rte_hexdump(stdout, "src", src, cur_src - src);
548 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
549 printf("data after dequeue is not the same\n");
556 printf("Test enqueue without enough memory space \n");
557 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
558 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
564 /* Available memory space for the exact MAX_BULK objects */
565 ret = rte_ring_mp_enqueue_burst(r, cur_src, 2, NULL);
570 ret = rte_ring_mp_enqueue_burst(r, cur_src, MAX_BULK, NULL);
571 cur_src += MAX_BULK - 3;
572 if (ret != MAX_BULK - 3)
576 printf("Test dequeue without enough objects \n");
577 for (i = 0; i<RING_SIZE/MAX_BULK - 1; i++) {
578 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
584 /* Available objects - the exact MAX_BULK */
585 ret = rte_ring_mc_dequeue_burst(r, cur_dst, 2, NULL);
590 ret = rte_ring_mc_dequeue_burst(r, cur_dst, MAX_BULK, NULL);
591 cur_dst += MAX_BULK - 3;
592 if (ret != MAX_BULK - 3)
596 if (memcmp(src, dst, cur_dst - dst)) {
597 rte_hexdump(stdout, "src", src, cur_src - src);
598 rte_hexdump(stdout, "dst", dst, cur_dst - dst);
599 printf("data after dequeue is not the same\n");
606 printf("Covering rte_ring_enqueue_burst functions \n");
608 ret = rte_ring_enqueue_burst(r, cur_src, 2, NULL);
613 ret = rte_ring_dequeue_burst(r, cur_dst, 2, NULL);
618 /* Free memory before test completed */
630 * it will always fail to create ring with a wrong ring size number in this function
633 test_ring_creation_with_wrong_size(void)
635 struct rte_ring * rp = NULL;
637 /* Test if ring size is not power of 2 */
638 rp = rte_ring_create("test_bad_ring_size", RING_SIZE + 1, SOCKET_ID_ANY, 0);
643 /* Test if ring size is exceeding the limit */
644 rp = rte_ring_create("test_bad_ring_size", (RTE_RING_SZ_MASK + 1), SOCKET_ID_ANY, 0);
652 * it tests if it would always fail to create ring with an used ring name
655 test_ring_creation_with_an_used_name(void)
657 struct rte_ring * rp;
659 rp = rte_ring_create("test", RING_SIZE, SOCKET_ID_ANY, 0);
667 * Test to if a non-power of 2 count causes the create
668 * function to fail correctly
671 test_create_count_odd(void)
673 struct rte_ring *r = rte_ring_create("test_ring_count",
674 4097, SOCKET_ID_ANY, 0 );
682 test_lookup_null(void)
684 struct rte_ring *rlp = rte_ring_lookup("ring_not_found");
686 if (rte_errno != ENOENT){
687 printf( "test failed to returnn error on null pointer\n");
694 * it tests some more basic ring operations
697 test_ring_basic_ex(void)
701 struct rte_ring *rp = NULL;
704 obj = rte_calloc("test_ring_basic_ex_malloc", RING_SIZE, sizeof(void *), 0);
706 printf("test_ring_basic_ex fail to rte_malloc\n");
710 rp = rte_ring_create("test_ring_basic_ex", RING_SIZE, SOCKET_ID_ANY,
711 RING_F_SP_ENQ | RING_F_SC_DEQ);
713 printf("test_ring_basic_ex fail to create ring\n");
717 if (rte_ring_lookup("test_ring_basic_ex") != rp) {
721 if (rte_ring_empty(rp) != 1) {
722 printf("test_ring_basic_ex ring is not empty but it should be\n");
726 printf("%u ring entries are now free\n", rte_ring_free_count(rp));
728 for (i = 0; i < RING_SIZE; i ++) {
729 rte_ring_enqueue(rp, obj[i]);
732 if (rte_ring_full(rp) != 1) {
733 printf("test_ring_basic_ex ring is not full but it should be\n");
737 for (i = 0; i < RING_SIZE; i ++) {
738 rte_ring_dequeue(rp, &obj[i]);
741 if (rte_ring_empty(rp) != 1) {
742 printf("test_ring_basic_ex ring is not empty but it should be\n");
746 /* Covering the ring burst operation */
747 ret = rte_ring_enqueue_burst(rp, obj, 2, NULL);
749 printf("test_ring_basic_ex: rte_ring_enqueue_burst fails \n");
753 ret = rte_ring_dequeue_burst(rp, obj, 2, NULL);
755 printf("test_ring_basic_ex: rte_ring_dequeue_burst fails \n");
769 test_ring_with_exact_size(void)
771 struct rte_ring *std_ring = NULL, *exact_sz_ring = NULL;
773 static const unsigned int ring_sz = RTE_DIM(ptr_array);
777 std_ring = rte_ring_create("std", ring_sz, rte_socket_id(),
778 RING_F_SP_ENQ | RING_F_SC_DEQ);
779 if (std_ring == NULL) {
780 printf("%s: error, can't create std ring\n", __func__);
783 exact_sz_ring = rte_ring_create("exact sz", ring_sz, rte_socket_id(),
784 RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
785 if (exact_sz_ring == NULL) {
786 printf("%s: error, can't create exact size ring\n", __func__);
791 * Check that the exact size ring is bigger than the standard ring
793 if (rte_ring_get_size(std_ring) >= rte_ring_get_size(exact_sz_ring)) {
794 printf("%s: error, std ring (size: %u) is not smaller than exact size one (size %u)\n",
796 rte_ring_get_size(std_ring),
797 rte_ring_get_size(exact_sz_ring));
801 * check that the exact_sz_ring can hold one more element than the
802 * standard ring. (16 vs 15 elements)
804 for (i = 0; i < ring_sz - 1; i++) {
805 rte_ring_enqueue(std_ring, NULL);
806 rte_ring_enqueue(exact_sz_ring, NULL);
808 if (rte_ring_enqueue(std_ring, NULL) != -ENOBUFS) {
809 printf("%s: error, unexpected successful enqueue\n", __func__);
812 if (rte_ring_enqueue(exact_sz_ring, NULL) == -ENOBUFS) {
813 printf("%s: error, enqueue failed\n", __func__);
817 /* check that dequeue returns the expected number of elements */
818 if (rte_ring_dequeue_burst(exact_sz_ring, ptr_array,
819 RTE_DIM(ptr_array), NULL) != ring_sz) {
820 printf("%s: error, failed to dequeue expected nb of elements\n",
825 /* check that the capacity function returns expected value */
826 if (rte_ring_get_capacity(exact_sz_ring) != ring_sz) {
827 printf("%s: error, incorrect ring capacity reported\n",
832 ret = 0; /* all ok if we get here */
834 rte_ring_free(std_ring);
835 rte_ring_free(exact_sz_ring);
842 struct rte_ring *r = NULL;
844 /* some more basic operations */
845 if (test_ring_basic_ex() < 0)
848 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(r) < 0)
864 /* basic operations */
865 if (test_ring_basic(r) < 0)
868 /* basic operations */
869 if ( test_create_count_odd() < 0){
870 printf("Test failed to detect odd count\n");
873 printf("Test detected odd count\n");
875 if ( test_lookup_null() < 0){
876 printf("Test failed to detect NULL ring lookup\n");
879 printf("Test detected NULL ring lookup\n");
881 /* test of creating ring with wrong size */
882 if (test_ring_creation_with_wrong_size() < 0)
885 /* test of creation ring with an used name */
886 if (test_ring_creation_with_an_used_name() < 0)
889 if (test_ring_with_exact_size() < 0)
892 /* dump the ring status */
893 rte_ring_list_dump(stdout);
905 REGISTER_TEST_COMMAND(ring_autotest, test_ring);