4 * Copyright(c) 2015 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.
38 #include <rte_cycles.h>
39 #include <rte_launch.h>
40 #include <rte_ethdev.h>
41 #include <rte_eth_ring.h>
45 #define RING_NAME "RING_PERF"
46 #define RING_SIZE 4096
50 * the sizes to enqueue and dequeue in testing
51 * (marked volatile so they won't be seen as compile-time constants)
53 static const volatile unsigned bulk_sizes[] = { 1, 8, 32 };
55 /* The ring structure used for tests */
56 static struct rte_ring *r;
57 static uint8_t ring_ethdev_port;
59 /* Get cycle counts for dequeuing from an empty ring. Should be 2 or 3 cycles */
61 test_empty_dequeue(void)
63 const unsigned iter_shift = 26;
64 const unsigned iterations = 1 << iter_shift;
66 void *burst[MAX_BURST];
68 const uint64_t sc_start = rte_rdtsc();
69 for (i = 0; i < iterations; i++)
70 rte_ring_sc_dequeue_bulk(r, burst, bulk_sizes[0], NULL);
71 const uint64_t sc_end = rte_rdtsc();
73 const uint64_t eth_start = rte_rdtsc();
74 for (i = 0; i < iterations; i++)
75 rte_eth_rx_burst(ring_ethdev_port, 0, (void *)burst,
77 const uint64_t eth_end = rte_rdtsc();
79 printf("Ring empty dequeue : %.1F\n",
80 (double)(sc_end - sc_start) / iterations);
81 printf("Ethdev empty dequeue: %.1F\n",
82 (double)(eth_end - eth_start) / iterations);
86 * Test function that determines how long an enqueue + dequeue of a single item
87 * takes on a single lcore. Result is for comparison with the bulk enq+deq.
90 test_single_enqueue_dequeue(void)
92 const unsigned iter_shift = 24;
93 const unsigned iterations = 1 << iter_shift;
96 struct rte_mbuf *mburst[1] = { NULL };
98 const uint64_t sc_start = rte_rdtsc_precise();
99 rte_compiler_barrier();
100 for (i = 0; i < iterations; i++) {
101 rte_ring_enqueue_bulk(r, &burst, 1, NULL);
102 rte_ring_dequeue_bulk(r, &burst, 1, NULL);
104 const uint64_t sc_end = rte_rdtsc_precise();
105 rte_compiler_barrier();
107 const uint64_t eth_start = rte_rdtsc_precise();
108 rte_compiler_barrier();
109 for (i = 0; i < iterations; i++) {
110 rte_eth_tx_burst(ring_ethdev_port, 0, mburst, 1);
111 rte_eth_rx_burst(ring_ethdev_port, 0, mburst, 1);
113 const uint64_t eth_end = rte_rdtsc_precise();
114 rte_compiler_barrier();
116 printf("Ring single enq/dequeue : %"PRIu64"\n",
117 (sc_end-sc_start) >> iter_shift);
118 printf("Ethdev single enq/dequeue: %"PRIu64"\n",
119 (eth_end-eth_start) >> iter_shift);
122 /* Times enqueue and dequeue on a single lcore */
124 test_bulk_enqueue_dequeue(void)
126 const unsigned iter_shift = 23;
127 const unsigned iterations = 1 << iter_shift;
129 struct rte_mbuf *burst[MAX_BURST] = {0};
131 for (sz = 0; sz < sizeof(bulk_sizes)/sizeof(bulk_sizes[0]); sz++) {
132 const uint64_t sc_start = rte_rdtsc();
133 for (i = 0; i < iterations; i++) {
134 rte_ring_sp_enqueue_bulk(r, (void *)burst,
135 bulk_sizes[sz], NULL);
136 rte_ring_sc_dequeue_bulk(r, (void *)burst,
137 bulk_sizes[sz], NULL);
139 const uint64_t sc_end = rte_rdtsc();
141 const uint64_t eth_start = rte_rdtsc_precise();
142 rte_compiler_barrier();
143 for (i = 0; i < iterations; i++) {
144 rte_eth_tx_burst(ring_ethdev_port, 0, burst, bulk_sizes[sz]);
145 rte_eth_rx_burst(ring_ethdev_port, 0, burst, bulk_sizes[sz]);
147 const uint64_t eth_end = rte_rdtsc_precise();
148 rte_compiler_barrier();
150 double sc_avg = ((double)(sc_end-sc_start) /
151 (iterations * bulk_sizes[sz]));
152 double eth_avg = ((double)(eth_end-eth_start) /
153 (iterations * bulk_sizes[sz]));
155 printf("ring bulk enq/deq (size: %u) : %.1F\n", bulk_sizes[sz],
157 printf("ethdev bulk enq/deq (size:%u): %.1F\n", bulk_sizes[sz],
165 test_ring_pmd_perf(void)
167 r = rte_ring_create(RING_NAME, RING_SIZE, rte_socket_id(),
168 RING_F_SP_ENQ|RING_F_SC_DEQ);
169 if (r == NULL && (r = rte_ring_lookup(RING_NAME)) == NULL)
172 ring_ethdev_port = rte_eth_from_ring(r);
174 printf("\n### Testing const single element enq/deq ###\n");
175 test_single_enqueue_dequeue();
177 printf("\n### Testing empty dequeue ###\n");
178 test_empty_dequeue();
180 printf("\n### Testing using a single lcore ###\n");
181 test_bulk_enqueue_dequeue();
186 REGISTER_TEST_COMMAND(ring_pmd_perf_autotest, test_ring_pmd_perf);