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>
44 #define ITER_POWER_CL 25 /* log 2 of how many iterations for Cache Line test */
45 #define ITER_POWER 21 /* log 2 of how many iterations we do when timing. */
47 #define BIG_BATCH 1024
49 /* static vars - zero initialized by default */
50 static volatile int quit;
51 static volatile unsigned worker_idx;
54 volatile unsigned handled_packets;
55 } __rte_cache_aligned;
56 struct worker_stats worker_stats[RTE_MAX_LCORE];
59 * worker thread used for testing the time to do a round-trip of a cache
60 * line between two cores and back again
63 flip_bit(volatile uint64_t *arg)
66 while (old_val != 2) {
75 * test case to time the number of cycles to round-trip a cache line between
76 * two cores and back again.
79 time_cache_line_switch(void)
81 /* allocate a full cache line for data, we use only first byte of it */
82 uint64_t data[RTE_CACHE_LINE_SIZE*3 / sizeof(uint64_t)];
84 unsigned i, slaveid = rte_get_next_lcore(rte_lcore_id(), 0, 0);
85 volatile uint64_t *pdata = &data[0];
87 rte_eal_remote_launch((lcore_function_t *)flip_bit, &data[0], slaveid);
91 const uint64_t start_time = rte_rdtsc();
92 for (i = 0; i < (1 << ITER_POWER_CL); i++) {
97 const uint64_t end_time = rte_rdtsc();
102 rte_eal_wait_lcore(slaveid);
103 printf("==== Cache line switch test ===\n");
104 printf("Time for %u iterations = %"PRIu64" ticks\n", (1<<ITER_POWER_CL),
105 end_time-start_time);
106 printf("Ticks per iteration = %"PRIu64"\n\n",
107 (end_time-start_time) >> ITER_POWER_CL);
111 * returns the total count of the number of packets handled by the worker
112 * functions given below.
115 total_packet_count(void)
117 unsigned i, count = 0;
118 for (i = 0; i < worker_idx; i++)
119 count += worker_stats[i].handled_packets;
123 /* resets the packet counts for a new test */
125 clear_packet_count(void)
127 memset(&worker_stats, 0, sizeof(worker_stats));
131 * This is the basic worker function for performance tests.
132 * it does nothing but return packets and count them.
135 handle_work(void *arg)
137 struct rte_distributor *d = arg;
138 unsigned int count = 0;
139 unsigned int num = 0;
141 unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
142 struct rte_mbuf *buf[8] __rte_cache_aligned;
144 for (i = 0; i < 8; i++)
147 num = rte_distributor_get_pkt(d, id, buf, buf, num);
149 worker_stats[id].handled_packets += num;
151 num = rte_distributor_get_pkt(d, id, buf, buf, num);
153 worker_stats[id].handled_packets += num;
155 rte_distributor_return_pkt(d, id, buf, num);
160 * This basic performance test just repeatedly sends in 32 packets at a time
161 * to the distributor and verifies at the end that we got them all in the worker
162 * threads and finally how long per packet the processing took.
165 perf_test(struct rte_distributor *d, struct rte_mempool *p)
169 struct rte_mbuf *bufs[BURST];
171 clear_packet_count();
172 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
173 printf("Error getting mbufs from pool\n");
176 /* ensure we have different hash value for each pkt */
177 for (i = 0; i < BURST; i++)
178 bufs[i]->hash.usr = i;
181 for (i = 0; i < (1<<ITER_POWER); i++)
182 rte_distributor_process(d, bufs, BURST);
187 rte_distributor_process(d, NULL, 0);
188 } while (total_packet_count() < (BURST << ITER_POWER));
190 rte_distributor_clear_returns(d);
192 printf("Time per burst: %"PRIu64"\n", (end - start) >> ITER_POWER);
193 printf("Time per packet: %"PRIu64"\n\n",
194 ((end - start) >> ITER_POWER)/BURST);
195 rte_mempool_put_bulk(p, (void *)bufs, BURST);
197 for (i = 0; i < rte_lcore_count() - 1; i++)
198 printf("Worker %u handled %u packets\n", i,
199 worker_stats[i].handled_packets);
200 printf("Total packets: %u (%x)\n", total_packet_count(),
201 total_packet_count());
202 printf("=== Perf test done ===\n\n");
207 /* Useful function which ensures that all worker functions terminate */
209 quit_workers(struct rte_distributor *d, struct rte_mempool *p)
211 const unsigned int num_workers = rte_lcore_count() - 1;
213 struct rte_mbuf *bufs[RTE_MAX_LCORE];
215 rte_mempool_get_bulk(p, (void *)bufs, num_workers);
218 for (i = 0; i < num_workers; i++)
219 bufs[i]->hash.usr = i << 1;
220 rte_distributor_process(d, bufs, num_workers);
222 rte_mempool_put_bulk(p, (void *)bufs, num_workers);
224 rte_distributor_process(d, NULL, 0);
225 rte_eal_mp_wait_lcore();
231 test_distributor_perf(void)
233 static struct rte_distributor *ds;
234 static struct rte_distributor *db;
235 static struct rte_mempool *p;
237 if (rte_lcore_count() < 2) {
238 printf("ERROR: not enough cores to test distributor\n");
242 /* first time how long it takes to round-trip a cache line */
243 time_cache_line_switch();
246 ds = rte_distributor_create("Test_perf", rte_socket_id(),
247 rte_lcore_count() - 1,
248 RTE_DIST_ALG_SINGLE);
250 printf("Error creating distributor\n");
254 rte_distributor_clear_returns(ds);
258 db = rte_distributor_create("Test_burst", rte_socket_id(),
259 rte_lcore_count() - 1,
262 printf("Error creating burst distributor\n");
266 rte_distributor_clear_returns(db);
269 const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
270 (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
272 p = rte_pktmbuf_pool_create("DPT_MBUF_POOL", nb_bufs, BURST,
273 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
275 printf("Error creating mempool\n");
280 printf("=== Performance test of distributor (single mode) ===\n");
281 rte_eal_mp_remote_launch(handle_work, ds, SKIP_MASTER);
282 if (perf_test(ds, p) < 0)
286 printf("=== Performance test of distributor (burst mode) ===\n");
287 rte_eal_mp_remote_launch(handle_work, db, SKIP_MASTER);
288 if (perf_test(db, p) < 0)
295 REGISTER_TEST_COMMAND(distributor_perf_autotest, test_distributor_perf);