4 * Copyright(c) 2010-2017 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_mempool.h>
39 #include <rte_cycles.h>
40 #include <rte_common.h>
42 #include <rte_distributor.h>
43 #include <rte_pause.h>
45 #define ITER_POWER_CL 25 /* log 2 of how many iterations for Cache Line test */
46 #define ITER_POWER 21 /* log 2 of how many iterations we do when timing. */
48 #define BIG_BATCH 1024
50 /* static vars - zero initialized by default */
51 static volatile int quit;
52 static volatile unsigned worker_idx;
55 volatile unsigned handled_packets;
56 } __rte_cache_aligned;
57 struct worker_stats worker_stats[RTE_MAX_LCORE];
60 * worker thread used for testing the time to do a round-trip of a cache
61 * line between two cores and back again
64 flip_bit(volatile uint64_t *arg)
67 while (old_val != 2) {
77 * test case to time the number of cycles to round-trip a cache line between
78 * two cores and back again.
81 time_cache_line_switch(void)
83 /* allocate a full cache line for data, we use only first byte of it */
84 uint64_t data[RTE_CACHE_LINE_SIZE*3 / sizeof(uint64_t)];
86 unsigned i, slaveid = rte_get_next_lcore(rte_lcore_id(), 0, 0);
87 volatile uint64_t *pdata = &data[0];
89 rte_eal_remote_launch((lcore_function_t *)flip_bit, &data[0], slaveid);
93 const uint64_t start_time = rte_rdtsc();
94 for (i = 0; i < (1 << ITER_POWER_CL); i++) {
99 const uint64_t end_time = rte_rdtsc();
104 rte_eal_wait_lcore(slaveid);
105 printf("==== Cache line switch test ===\n");
106 printf("Time for %u iterations = %"PRIu64" ticks\n", (1<<ITER_POWER_CL),
107 end_time-start_time);
108 printf("Ticks per iteration = %"PRIu64"\n\n",
109 (end_time-start_time) >> ITER_POWER_CL);
113 * returns the total count of the number of packets handled by the worker
114 * functions given below.
117 total_packet_count(void)
119 unsigned i, count = 0;
120 for (i = 0; i < worker_idx; i++)
121 count += worker_stats[i].handled_packets;
125 /* resets the packet counts for a new test */
127 clear_packet_count(void)
129 memset(&worker_stats, 0, sizeof(worker_stats));
133 * This is the basic worker function for performance tests.
134 * it does nothing but return packets and count them.
137 handle_work(void *arg)
139 struct rte_distributor *d = arg;
140 unsigned int count = 0;
141 unsigned int num = 0;
143 unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
144 struct rte_mbuf *buf[8] __rte_cache_aligned;
146 for (i = 0; i < 8; i++)
149 num = rte_distributor_get_pkt(d, id, buf, buf, num);
151 worker_stats[id].handled_packets += num;
153 num = rte_distributor_get_pkt(d, id, buf, buf, num);
155 worker_stats[id].handled_packets += num;
157 rte_distributor_return_pkt(d, id, buf, num);
162 * This basic performance test just repeatedly sends in 32 packets at a time
163 * to the distributor and verifies at the end that we got them all in the worker
164 * threads and finally how long per packet the processing took.
167 perf_test(struct rte_distributor *d, struct rte_mempool *p)
171 struct rte_mbuf *bufs[BURST];
173 clear_packet_count();
174 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
175 printf("Error getting mbufs from pool\n");
178 /* ensure we have different hash value for each pkt */
179 for (i = 0; i < BURST; i++)
180 bufs[i]->hash.usr = i;
183 for (i = 0; i < (1<<ITER_POWER); i++)
184 rte_distributor_process(d, bufs, BURST);
189 rte_distributor_process(d, NULL, 0);
190 } while (total_packet_count() < (BURST << ITER_POWER));
192 rte_distributor_clear_returns(d);
194 printf("Time per burst: %"PRIu64"\n", (end - start) >> ITER_POWER);
195 printf("Time per packet: %"PRIu64"\n\n",
196 ((end - start) >> ITER_POWER)/BURST);
197 rte_mempool_put_bulk(p, (void *)bufs, BURST);
199 for (i = 0; i < rte_lcore_count() - 1; i++)
200 printf("Worker %u handled %u packets\n", i,
201 worker_stats[i].handled_packets);
202 printf("Total packets: %u (%x)\n", total_packet_count(),
203 total_packet_count());
204 printf("=== Perf test done ===\n\n");
209 /* Useful function which ensures that all worker functions terminate */
211 quit_workers(struct rte_distributor *d, struct rte_mempool *p)
213 const unsigned int num_workers = rte_lcore_count() - 1;
215 struct rte_mbuf *bufs[RTE_MAX_LCORE];
217 rte_mempool_get_bulk(p, (void *)bufs, num_workers);
220 for (i = 0; i < num_workers; i++)
221 bufs[i]->hash.usr = i << 1;
222 rte_distributor_process(d, bufs, num_workers);
224 rte_mempool_put_bulk(p, (void *)bufs, num_workers);
226 rte_distributor_process(d, NULL, 0);
227 rte_eal_mp_wait_lcore();
233 test_distributor_perf(void)
235 static struct rte_distributor *ds;
236 static struct rte_distributor *db;
237 static struct rte_mempool *p;
239 if (rte_lcore_count() < 2) {
240 printf("ERROR: not enough cores to test distributor\n");
244 /* first time how long it takes to round-trip a cache line */
245 time_cache_line_switch();
248 ds = rte_distributor_create("Test_perf", rte_socket_id(),
249 rte_lcore_count() - 1,
250 RTE_DIST_ALG_SINGLE);
252 printf("Error creating distributor\n");
256 rte_distributor_clear_returns(ds);
260 db = rte_distributor_create("Test_burst", rte_socket_id(),
261 rte_lcore_count() - 1,
264 printf("Error creating burst distributor\n");
268 rte_distributor_clear_returns(db);
271 const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
272 (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
274 p = rte_pktmbuf_pool_create("DPT_MBUF_POOL", nb_bufs, BURST,
275 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
277 printf("Error creating mempool\n");
282 printf("=== Performance test of distributor (single mode) ===\n");
283 rte_eal_mp_remote_launch(handle_work, ds, SKIP_MASTER);
284 if (perf_test(ds, p) < 0)
288 printf("=== Performance test of distributor (burst mode) ===\n");
289 rte_eal_mp_remote_launch(handle_work, db, SKIP_MASTER);
290 if (perf_test(db, p) < 0)
297 REGISTER_TEST_COMMAND(distributor_perf_autotest, test_distributor_perf);