4 * Copyright(c) 2010-2013 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.
39 #include <rte_cycles.h>
40 #include <rte_launch.h>
42 #include <cmdline_parse.h>
50 * Measures performance of various operations using rdtsc
51 * * Empty ring dequeue
52 * * Enqueue/dequeue of bursts in 1 threads
53 * * Enqueue/dequeue of bursts in 2 threads
56 #define RING_NAME "RING_PERF"
57 #define RING_SIZE 4096
61 * the sizes to enqueue and dequeue in testing
62 * (marked volatile so they won't be seen as compile-time constants)
64 static const volatile unsigned bulk_sizes[] = { 8, 32 };
66 /* The ring structure used for tests */
67 static struct rte_ring *r;
73 static volatile unsigned lcore_count = 0;
75 /**** Functions to analyse our core mask to get cores for different tests ***/
78 get_two_hyperthreads(struct lcore_pair *lcp)
81 unsigned c1, c2, s1, s2;
82 RTE_LCORE_FOREACH(id1) {
83 /* inner loop just re-reads all id's. We could skip the first few
84 * elements, but since number of cores is small there is little point
86 RTE_LCORE_FOREACH(id2) {
89 c1 = lcore_config[id1].core_id;
90 c2 = lcore_config[id2].core_id;
91 s1 = lcore_config[id1].socket_id;
92 s2 = lcore_config[id2].socket_id;
93 if ((c1 == c2) && (s1 == s2)){
104 get_two_cores(struct lcore_pair *lcp)
107 unsigned c1, c2, s1, s2;
108 RTE_LCORE_FOREACH(id1) {
109 RTE_LCORE_FOREACH(id2) {
112 c1 = lcore_config[id1].core_id;
113 c2 = lcore_config[id2].core_id;
114 s1 = lcore_config[id1].socket_id;
115 s2 = lcore_config[id2].socket_id;
116 if ((c1 != c2) && (s1 == s2)){
127 get_two_sockets(struct lcore_pair *lcp)
131 RTE_LCORE_FOREACH(id1) {
132 RTE_LCORE_FOREACH(id2) {
135 s1 = lcore_config[id1].socket_id;
136 s2 = lcore_config[id2].socket_id;
147 /* Get cycle counts for dequeuing from an empty ring. Should be 2 or 3 cycles */
149 test_empty_dequeue(void)
151 const unsigned iter_shift = 26;
152 const unsigned iterations = 1<<iter_shift;
154 void *burst[MAX_BURST];
156 const uint64_t sc_start = rte_rdtsc();
157 for (i = 0; i < iterations; i++)
158 rte_ring_sc_dequeue_bulk(r, burst, bulk_sizes[0]);
159 const uint64_t sc_end = rte_rdtsc();
161 const uint64_t mc_start = rte_rdtsc();
162 for (i = 0; i < iterations; i++)
163 rte_ring_mc_dequeue_bulk(r, burst, bulk_sizes[0]);
164 const uint64_t mc_end = rte_rdtsc();
166 printf("SC empty dequeue: %.2F\n",
167 (double)(sc_end-sc_start) / iterations);
168 printf("MC empty dequeue: %.2F\n",
169 (double)(mc_end-mc_start) / iterations);
173 * for the separate enqueue and dequeue threads they take in one param
174 * and return two. Input = burst size, output = cycle average for sp/sc & mp/mc
176 struct thread_params {
177 unsigned size; /* input value, the burst size */
178 double spsc, mpmc; /* output value, the single or multi timings */
182 * Function that uses rdtsc to measure timing for ring enqueue. Needs pair
183 * thread running dequeue_bulk function
186 enqueue_bulk(void *p)
188 const unsigned iter_shift = 23;
189 const unsigned iterations = 1<<iter_shift;
190 struct thread_params *params = p;
191 const unsigned size = params->size;
193 void *burst[MAX_BURST] = {0};
195 if ( __sync_add_and_fetch(&lcore_count, 1) != 2 )
196 while(lcore_count != 2)
199 const uint64_t sp_start = rte_rdtsc();
200 for (i = 0; i < iterations; i++)
201 while (rte_ring_sp_enqueue_bulk(r, burst, size) != 0)
203 const uint64_t sp_end = rte_rdtsc();
205 const uint64_t mp_start = rte_rdtsc();
206 for (i = 0; i < iterations; i++)
207 while (rte_ring_mp_enqueue_bulk(r, burst, size) != 0)
209 const uint64_t mp_end = rte_rdtsc();
211 params->spsc = ((double)(sp_end - sp_start))/(iterations*size);
212 params->mpmc = ((double)(mp_end - mp_start))/(iterations*size);
217 * Function that uses rdtsc to measure timing for ring dequeue. Needs pair
218 * thread running enqueue_bulk function
221 dequeue_bulk(void *p)
223 const unsigned iter_shift = 23;
224 const unsigned iterations = 1<<iter_shift;
225 struct thread_params *params = p;
226 const unsigned size = params->size;
228 void *burst[MAX_BURST] = {0};
230 if ( __sync_add_and_fetch(&lcore_count, 1) != 2 )
231 while(lcore_count != 2)
234 const uint64_t sc_start = rte_rdtsc();
235 for (i = 0; i < iterations; i++)
236 while (rte_ring_sc_dequeue_bulk(r, burst, size) != 0)
238 const uint64_t sc_end = rte_rdtsc();
240 const uint64_t mc_start = rte_rdtsc();
241 for (i = 0; i < iterations; i++)
242 while (rte_ring_mc_dequeue_bulk(r, burst, size) != 0)
244 const uint64_t mc_end = rte_rdtsc();
246 params->spsc = ((double)(sc_end - sc_start))/(iterations*size);
247 params->mpmc = ((double)(mc_end - mc_start))/(iterations*size);
252 * Function that calls the enqueue and dequeue bulk functions on pairs of cores.
253 * used to measure ring perf between hyperthreads, cores and sockets.
256 run_on_core_pair(struct lcore_pair *cores,
257 lcore_function_t f1, lcore_function_t f2)
259 struct thread_params param1 = {.size = 0}, param2 = {.size = 0};
261 for (i = 0; i < sizeof(bulk_sizes)/sizeof(bulk_sizes[0]); i++) {
263 param1.size = param2.size = bulk_sizes[i];
264 if (cores->c1 == rte_get_master_lcore()) {
265 rte_eal_remote_launch(f2, ¶m2, cores->c2);
267 rte_eal_wait_lcore(cores->c2);
269 rte_eal_remote_launch(f1, ¶m1, cores->c1);
270 rte_eal_remote_launch(f2, ¶m2, cores->c2);
271 rte_eal_wait_lcore(cores->c1);
272 rte_eal_wait_lcore(cores->c2);
274 printf("SP/SC bulk enq/dequeue (size: %u): %.2F\n", bulk_sizes[i],
275 param1.spsc + param2.spsc);
276 printf("MP/MC bulk enq/dequeue (size: %u): %.2F\n", bulk_sizes[i],
277 param1.mpmc + param2.mpmc);
282 * Test function that determines how long an enqueue + dequeue of a single item
283 * takes on a single lcore. Result is for comparison with the bulk enq+deq.
286 test_single_enqueue_dequeue(void)
288 const unsigned iter_shift = 24;
289 const unsigned iterations = 1<<iter_shift;
293 const uint64_t sc_start = rte_rdtsc();
294 for (i = 0; i < iterations; i++) {
295 rte_ring_sp_enqueue(r, burst);
296 rte_ring_sc_dequeue(r, &burst);
298 const uint64_t sc_end = rte_rdtsc();
300 const uint64_t mc_start = rte_rdtsc();
301 for (i = 0; i < iterations; i++) {
302 rte_ring_mp_enqueue(r, burst);
303 rte_ring_mc_dequeue(r, &burst);
305 const uint64_t mc_end = rte_rdtsc();
307 printf("SP/SC single enq/dequeue: %"PRIu64"\n",
308 (sc_end-sc_start) >> iter_shift);
309 printf("MP/MC single enq/dequeue: %"PRIu64"\n",
310 (mc_end-mc_start) >> iter_shift);
314 * Test that does both enqueue and dequeue on a core using the burst() API calls
315 * instead of the bulk() calls used in other tests. Results should be the same
316 * as for the bulk function called on a single lcore.
319 test_burst_enqueue_dequeue(void)
321 const unsigned iter_shift = 23;
322 const unsigned iterations = 1<<iter_shift;
324 void *burst[MAX_BURST] = {0};
326 for (sz = 0; sz < sizeof(bulk_sizes)/sizeof(bulk_sizes[0]); sz++) {
327 const uint64_t sc_start = rte_rdtsc();
328 for (i = 0; i < iterations; i++) {
329 rte_ring_sp_enqueue_burst(r, burst, bulk_sizes[sz]);
330 rte_ring_sc_dequeue_burst(r, burst, bulk_sizes[sz]);
332 const uint64_t sc_end = rte_rdtsc();
334 const uint64_t mc_start = rte_rdtsc();
335 for (i = 0; i < iterations; i++) {
336 rte_ring_mp_enqueue_burst(r, burst, bulk_sizes[sz]);
337 rte_ring_mc_dequeue_burst(r, burst, bulk_sizes[sz]);
339 const uint64_t mc_end = rte_rdtsc();
341 uint64_t mc_avg = ((mc_end-mc_start) >> iter_shift) / bulk_sizes[sz];
342 uint64_t sc_avg = ((sc_end-sc_start) >> iter_shift) / bulk_sizes[sz];
344 printf("SP/SC burst enq/dequeue (size: %u): %"PRIu64"\n", bulk_sizes[sz],
346 printf("MP/MC burst enq/dequeue (size: %u): %"PRIu64"\n", bulk_sizes[sz],
351 /* Times enqueue and dequeue on a single lcore */
353 test_bulk_enqueue_dequeue(void)
355 const unsigned iter_shift = 23;
356 const unsigned iterations = 1<<iter_shift;
358 void *burst[MAX_BURST] = {0};
360 for (sz = 0; sz < sizeof(bulk_sizes)/sizeof(bulk_sizes[0]); sz++) {
361 const uint64_t sc_start = rte_rdtsc();
362 for (i = 0; i < iterations; i++) {
363 rte_ring_sp_enqueue_bulk(r, burst, bulk_sizes[sz]);
364 rte_ring_sc_dequeue_bulk(r, burst, bulk_sizes[sz]);
366 const uint64_t sc_end = rte_rdtsc();
368 const uint64_t mc_start = rte_rdtsc();
369 for (i = 0; i < iterations; i++) {
370 rte_ring_mp_enqueue_bulk(r, burst, bulk_sizes[sz]);
371 rte_ring_mc_dequeue_bulk(r, burst, bulk_sizes[sz]);
373 const uint64_t mc_end = rte_rdtsc();
375 double sc_avg = ((double)(sc_end-sc_start) /
376 (iterations * bulk_sizes[sz]));
377 double mc_avg = ((double)(mc_end-mc_start) /
378 (iterations * bulk_sizes[sz]));
380 printf("SP/SC bulk enq/dequeue (size: %u): %.2F\n", bulk_sizes[sz],
382 printf("MP/MC bulk enq/dequeue (size: %u): %.2F\n", bulk_sizes[sz],
390 struct lcore_pair cores;
391 r = rte_ring_create(RING_NAME, RING_SIZE, rte_socket_id(), 0);
392 if (r == NULL && (r = rte_ring_lookup(RING_NAME)) == NULL)
395 printf("### Testing single element and burst enq/deq ###\n");
396 test_single_enqueue_dequeue();
397 test_burst_enqueue_dequeue();
399 printf("\n### Testing empty dequeue ###\n");
400 test_empty_dequeue();
402 printf("\n### Testing using a single lcore ###\n");
403 test_bulk_enqueue_dequeue();
405 if (get_two_hyperthreads(&cores) == 0) {
406 printf("\n### Testing using two hyperthreads ###\n");
407 run_on_core_pair(&cores, enqueue_bulk, dequeue_bulk);
409 if (get_two_cores(&cores) == 0) {
410 printf("\n### Testing using two physical cores ###\n");
411 run_on_core_pair(&cores, enqueue_bulk, dequeue_bulk);
413 if (get_two_sockets(&cores) == 0) {
414 printf("\n### Testing using two NUMA nodes ###\n");
415 run_on_core_pair(&cores, enqueue_bulk, dequeue_bulk);