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 #define TEST_RING_VERIFY(exp) \
62 printf("error at %s:%d\tcondition " #exp " failed\n", \
63 __func__, __LINE__); \
64 rte_ring_dump(stdout, r); \
68 #define TEST_RING_FULL_EMTPY_ITER 8
71 * helper routine for test_ring_basic
74 test_ring_basic_full_empty(struct rte_ring
*r
, void * const src
[], void *dst
[])
77 const unsigned rsz
= RING_SIZE
- 1;
79 printf("Basic full/empty test\n");
81 for (i
= 0; TEST_RING_FULL_EMTPY_ITER
!= i
; i
++) {
83 /* random shift in the ring */
84 rand
= RTE_MAX(rte_rand() % RING_SIZE
, 1UL);
85 printf("%s: iteration %u, random shift: %u;\n",
87 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r
, src
, rand
,
89 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r
, dst
, rand
,
93 TEST_RING_VERIFY(rte_ring_enqueue_bulk(r
, src
, rsz
, NULL
) != 0);
94 TEST_RING_VERIFY(0 == rte_ring_free_count(r
));
95 TEST_RING_VERIFY(rsz
== rte_ring_count(r
));
96 TEST_RING_VERIFY(rte_ring_full(r
));
97 TEST_RING_VERIFY(0 == rte_ring_empty(r
));
100 TEST_RING_VERIFY(rte_ring_dequeue_bulk(r
, dst
, rsz
,
102 TEST_RING_VERIFY(rsz
== rte_ring_free_count(r
));
103 TEST_RING_VERIFY(0 == rte_ring_count(r
));
104 TEST_RING_VERIFY(0 == rte_ring_full(r
));
105 TEST_RING_VERIFY(rte_ring_empty(r
));
108 TEST_RING_VERIFY(0 == memcmp(src
, dst
, rsz
));
109 rte_ring_dump(stdout
, r
);
115 test_ring_basic(struct rte_ring
*r
)
117 void **src
= NULL
, **cur_src
= NULL
, **dst
= NULL
, **cur_dst
= NULL
;
119 unsigned i
, num_elems
;
121 /* alloc dummy object pointers */
122 src
= malloc(RING_SIZE
*2*sizeof(void *));
126 for (i
= 0; i
< RING_SIZE
*2 ; i
++) {
127 src
[i
] = (void *)(unsigned long)i
;
131 /* alloc some room for copied objects */
132 dst
= malloc(RING_SIZE
*2*sizeof(void *));
136 memset(dst
, 0, RING_SIZE
*2*sizeof(void *));
139 printf("enqueue 1 obj\n");
140 ret
= rte_ring_sp_enqueue_bulk(r
, cur_src
, 1, NULL
);
145 printf("enqueue 2 objs\n");
146 ret
= rte_ring_sp_enqueue_bulk(r
, cur_src
, 2, NULL
);
151 printf("enqueue MAX_BULK objs\n");
152 ret
= rte_ring_sp_enqueue_bulk(r
, cur_src
, MAX_BULK
, NULL
);
157 printf("dequeue 1 obj\n");
158 ret
= rte_ring_sc_dequeue_bulk(r
, cur_dst
, 1, NULL
);
163 printf("dequeue 2 objs\n");
164 ret
= rte_ring_sc_dequeue_bulk(r
, cur_dst
, 2, NULL
);
169 printf("dequeue MAX_BULK objs\n");
170 ret
= rte_ring_sc_dequeue_bulk(r
, cur_dst
, MAX_BULK
, NULL
);
176 if (memcmp(src
, dst
, cur_dst
- dst
)) {
177 rte_hexdump(stdout
, "src", src
, cur_src
- src
);
178 rte_hexdump(stdout
, "dst", dst
, cur_dst
- dst
);
179 printf("data after dequeue is not the same\n");
185 printf("enqueue 1 obj\n");
186 ret
= rte_ring_mp_enqueue_bulk(r
, cur_src
, 1, NULL
);
191 printf("enqueue 2 objs\n");
192 ret
= rte_ring_mp_enqueue_bulk(r
, cur_src
, 2, NULL
);
197 printf("enqueue MAX_BULK objs\n");
198 ret
= rte_ring_mp_enqueue_bulk(r
, cur_src
, MAX_BULK
, NULL
);
203 printf("dequeue 1 obj\n");
204 ret
= rte_ring_mc_dequeue_bulk(r
, cur_dst
, 1, NULL
);
209 printf("dequeue 2 objs\n");
210 ret
= rte_ring_mc_dequeue_bulk(r
, cur_dst
, 2, NULL
);
215 printf("dequeue MAX_BULK objs\n");
216 ret
= rte_ring_mc_dequeue_bulk(r
, cur_dst
, MAX_BULK
, NULL
);
222 if (memcmp(src
, dst
, cur_dst
- dst
)) {
223 rte_hexdump(stdout
, "src", src
, cur_src
- src
);
224 rte_hexdump(stdout
, "dst", dst
, cur_dst
- dst
);
225 printf("data after dequeue is not the same\n");
231 printf("fill and empty the ring\n");
232 for (i
= 0; i
<RING_SIZE
/MAX_BULK
; i
++) {
233 ret
= rte_ring_mp_enqueue_bulk(r
, cur_src
, MAX_BULK
, NULL
);
237 ret
= rte_ring_mc_dequeue_bulk(r
, cur_dst
, MAX_BULK
, NULL
);
244 if (memcmp(src
, dst
, cur_dst
- dst
)) {
245 rte_hexdump(stdout
, "src", src
, cur_src
- src
);
246 rte_hexdump(stdout
, "dst", dst
, cur_dst
- dst
);
247 printf("data after dequeue is not the same\n");
251 if (test_ring_basic_full_empty(r
, src
, dst
) != 0)
257 printf("test default bulk enqueue / dequeue\n");
263 ret
= rte_ring_enqueue_bulk(r
, cur_src
, num_elems
, NULL
);
264 cur_src
+= num_elems
;
266 printf("Cannot enqueue\n");
269 ret
= rte_ring_enqueue_bulk(r
, cur_src
, num_elems
, NULL
);
270 cur_src
+= num_elems
;
272 printf("Cannot enqueue\n");
275 ret
= rte_ring_dequeue_bulk(r
, cur_dst
, num_elems
, NULL
);
276 cur_dst
+= num_elems
;
278 printf("Cannot dequeue\n");
281 ret
= rte_ring_dequeue_bulk(r
, cur_dst
, num_elems
, NULL
);
282 cur_dst
+= num_elems
;
284 printf("Cannot dequeue2\n");
289 if (memcmp(src
, dst
, cur_dst
- dst
)) {
290 rte_hexdump(stdout
, "src", src
, cur_src
- src
);
291 rte_hexdump(stdout
, "dst", dst
, cur_dst
- dst
);
292 printf("data after dequeue is not the same\n");
299 ret
= rte_ring_mp_enqueue(r
, cur_src
);
303 ret
= rte_ring_mc_dequeue(r
, cur_dst
);
318 test_ring_burst_basic(struct rte_ring
*r
)
320 void **src
= NULL
, **cur_src
= NULL
, **dst
= NULL
, **cur_dst
= NULL
;
324 /* alloc dummy object pointers */
325 src
= malloc(RING_SIZE
*2*sizeof(void *));
329 for (i
= 0; i
< RING_SIZE
*2 ; i
++) {
330 src
[i
] = (void *)(unsigned long)i
;
334 /* alloc some room for copied objects */
335 dst
= malloc(RING_SIZE
*2*sizeof(void *));
339 memset(dst
, 0, RING_SIZE
*2*sizeof(void *));
342 printf("Test SP & SC basic functions \n");
343 printf("enqueue 1 obj\n");
344 ret
= rte_ring_sp_enqueue_burst(r
, cur_src
, 1, NULL
);
349 printf("enqueue 2 objs\n");
350 ret
= rte_ring_sp_enqueue_burst(r
, cur_src
, 2, NULL
);
355 printf("enqueue MAX_BULK objs\n");
356 ret
= rte_ring_sp_enqueue_burst(r
, cur_src
, MAX_BULK
, NULL
);
361 printf("dequeue 1 obj\n");
362 ret
= rte_ring_sc_dequeue_burst(r
, cur_dst
, 1, NULL
);
367 printf("dequeue 2 objs\n");
368 ret
= rte_ring_sc_dequeue_burst(r
, cur_dst
, 2, NULL
);
373 printf("dequeue MAX_BULK objs\n");
374 ret
= rte_ring_sc_dequeue_burst(r
, cur_dst
, MAX_BULK
, NULL
);
380 if (memcmp(src
, dst
, cur_dst
- dst
)) {
381 rte_hexdump(stdout
, "src", src
, cur_src
- src
);
382 rte_hexdump(stdout
, "dst", dst
, cur_dst
- dst
);
383 printf("data after dequeue is not the same\n");
390 printf("Test enqueue without enough memory space \n");
391 for (i
= 0; i
< (RING_SIZE
/MAX_BULK
- 1); i
++) {
392 ret
= rte_ring_sp_enqueue_burst(r
, cur_src
, MAX_BULK
, NULL
);
398 printf("Enqueue 2 objects, free entries = MAX_BULK - 2 \n");
399 ret
= rte_ring_sp_enqueue_burst(r
, cur_src
, 2, NULL
);
404 printf("Enqueue the remaining entries = MAX_BULK - 2 \n");
405 /* Always one free entry left */
406 ret
= rte_ring_sp_enqueue_burst(r
, cur_src
, MAX_BULK
, NULL
);
407 cur_src
+= MAX_BULK
- 3;
408 if (ret
!= MAX_BULK
- 3)
411 printf("Test if ring is full \n");
412 if (rte_ring_full(r
) != 1)
415 printf("Test enqueue for a full entry \n");
416 ret
= rte_ring_sp_enqueue_burst(r
, cur_src
, MAX_BULK
, NULL
);
420 printf("Test dequeue without enough objects \n");
421 for (i
= 0; i
<RING_SIZE
/MAX_BULK
- 1; i
++) {
422 ret
= rte_ring_sc_dequeue_burst(r
, cur_dst
, MAX_BULK
, NULL
);
428 /* Available memory space for the exact MAX_BULK entries */
429 ret
= rte_ring_sc_dequeue_burst(r
, cur_dst
, 2, NULL
);
434 ret
= rte_ring_sc_dequeue_burst(r
, cur_dst
, MAX_BULK
, NULL
);
435 cur_dst
+= MAX_BULK
- 3;
436 if (ret
!= MAX_BULK
- 3)
439 printf("Test if ring is empty \n");
440 /* Check if ring is empty */
441 if (1 != rte_ring_empty(r
))
445 if (memcmp(src
, dst
, cur_dst
- dst
)) {
446 rte_hexdump(stdout
, "src", src
, cur_src
- src
);
447 rte_hexdump(stdout
, "dst", dst
, cur_dst
- dst
);
448 printf("data after dequeue is not the same\n");
455 printf("Test MP & MC basic functions \n");
457 printf("enqueue 1 obj\n");
458 ret
= rte_ring_mp_enqueue_burst(r
, cur_src
, 1, NULL
);
463 printf("enqueue 2 objs\n");
464 ret
= rte_ring_mp_enqueue_burst(r
, cur_src
, 2, NULL
);
469 printf("enqueue MAX_BULK objs\n");
470 ret
= rte_ring_mp_enqueue_burst(r
, cur_src
, MAX_BULK
, NULL
);
475 printf("dequeue 1 obj\n");
476 ret
= rte_ring_mc_dequeue_burst(r
, cur_dst
, 1, NULL
);
481 printf("dequeue 2 objs\n");
482 ret
= rte_ring_mc_dequeue_burst(r
, cur_dst
, 2, NULL
);
487 printf("dequeue MAX_BULK objs\n");
488 ret
= rte_ring_mc_dequeue_burst(r
, cur_dst
, MAX_BULK
, NULL
);
494 if (memcmp(src
, dst
, cur_dst
- dst
)) {
495 rte_hexdump(stdout
, "src", src
, cur_src
- src
);
496 rte_hexdump(stdout
, "dst", dst
, cur_dst
- dst
);
497 printf("data after dequeue is not the same\n");
504 printf("fill and empty the ring\n");
505 for (i
= 0; i
<RING_SIZE
/MAX_BULK
; i
++) {
506 ret
= rte_ring_mp_enqueue_burst(r
, cur_src
, MAX_BULK
, NULL
);
510 ret
= rte_ring_mc_dequeue_burst(r
, cur_dst
, MAX_BULK
, NULL
);
517 if (memcmp(src
, dst
, cur_dst
- dst
)) {
518 rte_hexdump(stdout
, "src", src
, cur_src
- src
);
519 rte_hexdump(stdout
, "dst", dst
, cur_dst
- dst
);
520 printf("data after dequeue is not the same\n");
527 printf("Test enqueue without enough memory space \n");
528 for (i
= 0; i
<RING_SIZE
/MAX_BULK
- 1; i
++) {
529 ret
= rte_ring_mp_enqueue_burst(r
, cur_src
, MAX_BULK
, NULL
);
535 /* Available memory space for the exact MAX_BULK objects */
536 ret
= rte_ring_mp_enqueue_burst(r
, cur_src
, 2, NULL
);
541 ret
= rte_ring_mp_enqueue_burst(r
, cur_src
, MAX_BULK
, NULL
);
542 cur_src
+= MAX_BULK
- 3;
543 if (ret
!= MAX_BULK
- 3)
547 printf("Test dequeue without enough objects \n");
548 for (i
= 0; i
<RING_SIZE
/MAX_BULK
- 1; i
++) {
549 ret
= rte_ring_mc_dequeue_burst(r
, cur_dst
, MAX_BULK
, NULL
);
555 /* Available objects - the exact MAX_BULK */
556 ret
= rte_ring_mc_dequeue_burst(r
, cur_dst
, 2, NULL
);
561 ret
= rte_ring_mc_dequeue_burst(r
, cur_dst
, MAX_BULK
, NULL
);
562 cur_dst
+= MAX_BULK
- 3;
563 if (ret
!= MAX_BULK
- 3)
567 if (memcmp(src
, dst
, cur_dst
- dst
)) {
568 rte_hexdump(stdout
, "src", src
, cur_src
- src
);
569 rte_hexdump(stdout
, "dst", dst
, cur_dst
- dst
);
570 printf("data after dequeue is not the same\n");
577 printf("Covering rte_ring_enqueue_burst functions \n");
579 ret
= rte_ring_enqueue_burst(r
, cur_src
, 2, NULL
);
584 ret
= rte_ring_dequeue_burst(r
, cur_dst
, 2, NULL
);
589 /* Free memory before test completed */
601 * it will always fail to create ring with a wrong ring size number in this function
604 test_ring_creation_with_wrong_size(void)
606 struct rte_ring
* rp
= NULL
;
608 /* Test if ring size is not power of 2 */
609 rp
= rte_ring_create("test_bad_ring_size", RING_SIZE
+ 1, SOCKET_ID_ANY
, 0);
614 /* Test if ring size is exceeding the limit */
615 rp
= rte_ring_create("test_bad_ring_size", (RTE_RING_SZ_MASK
+ 1), SOCKET_ID_ANY
, 0);
623 * it tests if it would always fail to create ring with an used ring name
626 test_ring_creation_with_an_used_name(void)
628 struct rte_ring
* rp
;
630 rp
= rte_ring_create("test", RING_SIZE
, SOCKET_ID_ANY
, 0);
638 * Test to if a non-power of 2 count causes the create
639 * function to fail correctly
642 test_create_count_odd(void)
644 struct rte_ring
*r
= rte_ring_create("test_ring_count",
645 4097, SOCKET_ID_ANY
, 0 );
653 test_lookup_null(void)
655 struct rte_ring
*rlp
= rte_ring_lookup("ring_not_found");
657 if (rte_errno
!= ENOENT
){
658 printf( "test failed to returnn error on null pointer\n");
665 * it tests some more basic ring operations
668 test_ring_basic_ex(void)
672 struct rte_ring
*rp
= NULL
;
675 obj
= rte_calloc("test_ring_basic_ex_malloc", RING_SIZE
, sizeof(void *), 0);
677 printf("test_ring_basic_ex fail to rte_malloc\n");
681 rp
= rte_ring_create("test_ring_basic_ex", RING_SIZE
, SOCKET_ID_ANY
,
682 RING_F_SP_ENQ
| RING_F_SC_DEQ
);
684 printf("test_ring_basic_ex fail to create ring\n");
688 if (rte_ring_lookup("test_ring_basic_ex") != rp
) {
692 if (rte_ring_empty(rp
) != 1) {
693 printf("test_ring_basic_ex ring is not empty but it should be\n");
697 printf("%u ring entries are now free\n", rte_ring_free_count(rp
));
699 for (i
= 0; i
< RING_SIZE
; i
++) {
700 rte_ring_enqueue(rp
, obj
[i
]);
703 if (rte_ring_full(rp
) != 1) {
704 printf("test_ring_basic_ex ring is not full but it should be\n");
708 for (i
= 0; i
< RING_SIZE
; i
++) {
709 rte_ring_dequeue(rp
, &obj
[i
]);
712 if (rte_ring_empty(rp
) != 1) {
713 printf("test_ring_basic_ex ring is not empty but it should be\n");
717 /* Covering the ring burst operation */
718 ret
= rte_ring_enqueue_burst(rp
, obj
, 2, NULL
);
720 printf("test_ring_basic_ex: rte_ring_enqueue_burst fails \n");
724 ret
= rte_ring_dequeue_burst(rp
, obj
, 2, NULL
);
726 printf("test_ring_basic_ex: rte_ring_dequeue_burst fails \n");
740 test_ring_with_exact_size(void)
742 struct rte_ring
*std_ring
= NULL
, *exact_sz_ring
= NULL
;
744 static const unsigned int ring_sz
= RTE_DIM(ptr_array
);
748 std_ring
= rte_ring_create("std", ring_sz
, rte_socket_id(),
749 RING_F_SP_ENQ
| RING_F_SC_DEQ
);
750 if (std_ring
== NULL
) {
751 printf("%s: error, can't create std ring\n", __func__
);
754 exact_sz_ring
= rte_ring_create("exact sz", ring_sz
, rte_socket_id(),
755 RING_F_SP_ENQ
| RING_F_SC_DEQ
| RING_F_EXACT_SZ
);
756 if (exact_sz_ring
== NULL
) {
757 printf("%s: error, can't create exact size ring\n", __func__
);
762 * Check that the exact size ring is bigger than the standard ring
764 if (rte_ring_get_size(std_ring
) >= rte_ring_get_size(exact_sz_ring
)) {
765 printf("%s: error, std ring (size: %u) is not smaller than exact size one (size %u)\n",
767 rte_ring_get_size(std_ring
),
768 rte_ring_get_size(exact_sz_ring
));
772 * check that the exact_sz_ring can hold one more element than the
773 * standard ring. (16 vs 15 elements)
775 for (i
= 0; i
< ring_sz
- 1; i
++) {
776 rte_ring_enqueue(std_ring
, NULL
);
777 rte_ring_enqueue(exact_sz_ring
, NULL
);
779 if (rte_ring_enqueue(std_ring
, NULL
) != -ENOBUFS
) {
780 printf("%s: error, unexpected successful enqueue\n", __func__
);
783 if (rte_ring_enqueue(exact_sz_ring
, NULL
) == -ENOBUFS
) {
784 printf("%s: error, enqueue failed\n", __func__
);
788 /* check that dequeue returns the expected number of elements */
789 if (rte_ring_dequeue_burst(exact_sz_ring
, ptr_array
,
790 RTE_DIM(ptr_array
), NULL
) != ring_sz
) {
791 printf("%s: error, failed to dequeue expected nb of elements\n",
796 /* check that the capacity function returns expected value */
797 if (rte_ring_get_capacity(exact_sz_ring
) != ring_sz
) {
798 printf("%s: error, incorrect ring capacity reported\n",
803 ret
= 0; /* all ok if we get here */
805 rte_ring_free(std_ring
);
806 rte_ring_free(exact_sz_ring
);
813 struct rte_ring
*r
= NULL
;
815 /* some more basic operations */
816 if (test_ring_basic_ex() < 0)
819 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(r
) < 0)
835 /* basic operations */
836 if (test_ring_basic(r
) < 0)
839 /* basic operations */
840 if ( test_create_count_odd() < 0){
841 printf("Test failed to detect odd count\n");
844 printf("Test detected odd count\n");
846 if ( test_lookup_null() < 0){
847 printf("Test failed to detect NULL ring lookup\n");
850 printf("Test detected NULL ring lookup\n");
852 /* test of creating ring with wrong size */
853 if (test_ring_creation_with_wrong_size() < 0)
856 /* test of creation ring with an used name */
857 if (test_ring_creation_with_an_used_name() < 0)
860 if (test_ring_with_exact_size() < 0)
863 /* dump the ring status */
864 rte_ring_list_dump(stdout
);
876 REGISTER_TEST_COMMAND(ring_autotest
, test_ring
);