1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
9 #include <rte_cycles.h>
10 #include <rte_launch.h>
11 #include <rte_ethdev.h>
12 #include <rte_eth_ring.h>
16 #define RING_NAME "RING_PERF"
17 #define RING_SIZE 4096
21 * the sizes to enqueue and dequeue in testing
22 * (marked volatile so they won't be seen as compile-time constants)
24 static const volatile unsigned bulk_sizes[] = { 1, 8, 32 };
26 /* The ring structure used for tests */
27 static struct rte_ring *r;
28 static uint16_t ring_ethdev_port;
30 /* Get cycle counts for dequeuing from an empty ring. Should be 2 or 3 cycles */
32 test_empty_dequeue(void)
34 const unsigned iter_shift = 26;
35 const unsigned iterations = 1 << iter_shift;
37 void *burst[MAX_BURST];
39 const uint64_t sc_start = rte_rdtsc();
40 for (i = 0; i < iterations; i++)
41 rte_ring_sc_dequeue_bulk(r, burst, bulk_sizes[0], NULL);
42 const uint64_t sc_end = rte_rdtsc();
44 const uint64_t eth_start = rte_rdtsc();
45 for (i = 0; i < iterations; i++)
46 rte_eth_rx_burst(ring_ethdev_port, 0, (void *)burst,
48 const uint64_t eth_end = rte_rdtsc();
50 printf("Ring empty dequeue : %.1F\n",
51 (double)(sc_end - sc_start) / iterations);
52 printf("Ethdev empty dequeue: %.1F\n",
53 (double)(eth_end - eth_start) / iterations);
57 * Test function that determines how long an enqueue + dequeue of a single item
58 * takes on a single lcore. Result is for comparison with the bulk enq+deq.
61 test_single_enqueue_dequeue(void)
63 const unsigned iter_shift = 24;
64 const unsigned iterations = 1 << iter_shift;
67 struct rte_mbuf *mburst[1] = { NULL };
69 const uint64_t sc_start = rte_rdtsc_precise();
70 rte_compiler_barrier();
71 for (i = 0; i < iterations; i++) {
72 rte_ring_enqueue_bulk(r, &burst, 1, NULL);
73 rte_ring_dequeue_bulk(r, &burst, 1, NULL);
75 const uint64_t sc_end = rte_rdtsc_precise();
76 rte_compiler_barrier();
78 const uint64_t eth_start = rte_rdtsc_precise();
79 rte_compiler_barrier();
80 for (i = 0; i < iterations; i++) {
81 rte_eth_tx_burst(ring_ethdev_port, 0, mburst, 1);
82 rte_eth_rx_burst(ring_ethdev_port, 0, mburst, 1);
84 const uint64_t eth_end = rte_rdtsc_precise();
85 rte_compiler_barrier();
87 printf("Ring single enq/dequeue : %"PRIu64"\n",
88 (sc_end-sc_start) >> iter_shift);
89 printf("Ethdev single enq/dequeue: %"PRIu64"\n",
90 (eth_end-eth_start) >> iter_shift);
93 /* Times enqueue and dequeue on a single lcore */
95 test_bulk_enqueue_dequeue(void)
97 const unsigned iter_shift = 23;
98 const unsigned iterations = 1 << iter_shift;
100 struct rte_mbuf *burst[MAX_BURST] = {0};
102 for (sz = 0; sz < sizeof(bulk_sizes)/sizeof(bulk_sizes[0]); sz++) {
103 const uint64_t sc_start = rte_rdtsc();
104 for (i = 0; i < iterations; i++) {
105 rte_ring_sp_enqueue_bulk(r, (void *)burst,
106 bulk_sizes[sz], NULL);
107 rte_ring_sc_dequeue_bulk(r, (void *)burst,
108 bulk_sizes[sz], NULL);
110 const uint64_t sc_end = rte_rdtsc();
112 const uint64_t eth_start = rte_rdtsc_precise();
113 rte_compiler_barrier();
114 for (i = 0; i < iterations; i++) {
115 rte_eth_tx_burst(ring_ethdev_port, 0, burst, bulk_sizes[sz]);
116 rte_eth_rx_burst(ring_ethdev_port, 0, burst, bulk_sizes[sz]);
118 const uint64_t eth_end = rte_rdtsc_precise();
119 rte_compiler_barrier();
121 double sc_avg = ((double)(sc_end-sc_start) /
122 (iterations * bulk_sizes[sz]));
123 double eth_avg = ((double)(eth_end-eth_start) /
124 (iterations * bulk_sizes[sz]));
126 printf("ring bulk enq/deq (size: %u) : %.1F\n", bulk_sizes[sz],
128 printf("ethdev bulk enq/deq (size:%u): %.1F\n", bulk_sizes[sz],
136 test_ring_pmd_perf(void)
138 r = rte_ring_create(RING_NAME, RING_SIZE, rte_socket_id(),
139 RING_F_SP_ENQ|RING_F_SC_DEQ);
140 if (r == NULL && (r = rte_ring_lookup(RING_NAME)) == NULL)
143 ring_ethdev_port = rte_eth_from_ring(r);
145 printf("\n### Testing const single element enq/deq ###\n");
146 test_single_enqueue_dequeue();
148 printf("\n### Testing empty dequeue ###\n");
149 test_empty_dequeue();
151 printf("\n### Testing using a single lcore ###\n");
152 test_bulk_enqueue_dequeue();
157 REGISTER_TEST_COMMAND(ring_pmd_perf_autotest, test_ring_pmd_perf);