1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
9 #include <rte_mempool.h>
10 #include <rte_cycles.h>
11 #include <rte_common.h>
14 #ifdef RTE_EXEC_ENV_WINDOWS
16 test_distributor_perf(void)
18 printf("distributor perf not supported on Windows, skipping test\n");
24 #include <rte_distributor.h>
25 #include <rte_pause.h>
27 #define ITER_POWER_CL 25 /* log 2 of how many iterations for Cache Line test */
28 #define ITER_POWER 21 /* log 2 of how many iterations we do when timing. */
30 #define BIG_BATCH 1024
32 /* static vars - zero initialized by default */
33 static volatile int quit;
34 static volatile unsigned worker_idx;
37 volatile unsigned handled_packets;
38 } __rte_cache_aligned;
39 static struct worker_stats worker_stats[RTE_MAX_LCORE];
42 * worker thread used for testing the time to do a round-trip of a cache
43 * line between two cores and back again
46 flip_bit(volatile uint64_t *arg)
49 while (old_val != 2) {
59 * test case to time the number of cycles to round-trip a cache line between
60 * two cores and back again.
63 time_cache_line_switch(void)
65 /* allocate a full cache line for data, we use only first byte of it */
66 uint64_t data[RTE_CACHE_LINE_SIZE*3 / sizeof(uint64_t)];
68 unsigned int i, workerid = rte_get_next_lcore(rte_lcore_id(), 0, 0);
69 volatile uint64_t *pdata = &data[0];
71 rte_eal_remote_launch((lcore_function_t *)flip_bit, &data[0], workerid);
75 const uint64_t start_time = rte_rdtsc();
76 for (i = 0; i < (1 << ITER_POWER_CL); i++) {
81 const uint64_t end_time = rte_rdtsc();
86 rte_eal_wait_lcore(workerid);
87 printf("==== Cache line switch test ===\n");
88 printf("Time for %u iterations = %"PRIu64" ticks\n", (1<<ITER_POWER_CL),
90 printf("Ticks per iteration = %"PRIu64"\n\n",
91 (end_time-start_time) >> ITER_POWER_CL);
95 * returns the total count of the number of packets handled by the worker
96 * functions given below.
99 total_packet_count(void)
101 unsigned i, count = 0;
102 for (i = 0; i < worker_idx; i++)
103 count += worker_stats[i].handled_packets;
107 /* resets the packet counts for a new test */
109 clear_packet_count(void)
111 memset(&worker_stats, 0, sizeof(worker_stats));
115 * This is the basic worker function for performance tests.
116 * it does nothing but return packets and count them.
119 handle_work(void *arg)
121 struct rte_distributor *d = arg;
122 unsigned int num = 0;
124 unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
125 struct rte_mbuf *buf[8] __rte_cache_aligned;
127 for (i = 0; i < 8; i++)
130 num = rte_distributor_get_pkt(d, id, buf, buf, num);
132 worker_stats[id].handled_packets += num;
133 num = rte_distributor_get_pkt(d, id, buf, buf, num);
135 worker_stats[id].handled_packets += num;
136 rte_distributor_return_pkt(d, id, buf, num);
141 * This basic performance test just repeatedly sends in 32 packets at a time
142 * to the distributor and verifies at the end that we got them all in the worker
143 * threads and finally how long per packet the processing took.
146 perf_test(struct rte_distributor *d, struct rte_mempool *p)
150 struct rte_mbuf *bufs[BURST];
152 clear_packet_count();
153 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
154 printf("Error getting mbufs from pool\n");
157 /* ensure we have different hash value for each pkt */
158 for (i = 0; i < BURST; i++)
159 bufs[i]->hash.usr = i;
162 for (i = 0; i < (1<<ITER_POWER); i++)
163 rte_distributor_process(d, bufs, BURST);
168 rte_distributor_process(d, NULL, 0);
169 } while (total_packet_count() < (BURST << ITER_POWER));
171 rte_distributor_clear_returns(d);
173 printf("Time per burst: %"PRIu64"\n", (end - start) >> ITER_POWER);
174 printf("Time per packet: %"PRIu64"\n\n",
175 ((end - start) >> ITER_POWER)/BURST);
176 rte_mempool_put_bulk(p, (void *)bufs, BURST);
178 for (i = 0; i < rte_lcore_count() - 1; i++)
179 printf("Worker %u handled %u packets\n", i,
180 worker_stats[i].handled_packets);
181 printf("Total packets: %u (%x)\n", total_packet_count(),
182 total_packet_count());
183 printf("=== Perf test done ===\n\n");
188 /* Useful function which ensures that all worker functions terminate */
190 quit_workers(struct rte_distributor *d, struct rte_mempool *p)
192 const unsigned int num_workers = rte_lcore_count() - 1;
194 struct rte_mbuf *bufs[RTE_MAX_LCORE];
196 rte_mempool_get_bulk(p, (void *)bufs, num_workers);
199 for (i = 0; i < num_workers; i++) {
200 bufs[i]->hash.usr = i << 1;
201 rte_distributor_process(d, &bufs[i], 1);
204 rte_mempool_put_bulk(p, (void *)bufs, num_workers);
206 rte_distributor_process(d, NULL, 0);
207 rte_distributor_flush(d);
208 rte_eal_mp_wait_lcore();
214 test_distributor_perf(void)
216 static struct rte_distributor *ds;
217 static struct rte_distributor *db;
218 static struct rte_mempool *p;
220 if (rte_lcore_count() < 2) {
221 printf("Not enough cores for distributor_perf_autotest, expecting at least 2\n");
225 /* first time how long it takes to round-trip a cache line */
226 time_cache_line_switch();
229 ds = rte_distributor_create("Test_perf", rte_socket_id(),
230 rte_lcore_count() - 1,
231 RTE_DIST_ALG_SINGLE);
233 printf("Error creating distributor\n");
237 rte_distributor_clear_returns(ds);
241 db = rte_distributor_create("Test_burst", rte_socket_id(),
242 rte_lcore_count() - 1,
245 printf("Error creating burst distributor\n");
249 rte_distributor_clear_returns(db);
252 const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
253 (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
255 p = rte_pktmbuf_pool_create("DPT_MBUF_POOL", nb_bufs, BURST,
256 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
258 printf("Error creating mempool\n");
263 printf("=== Performance test of distributor (single mode) ===\n");
264 rte_eal_mp_remote_launch(handle_work, ds, SKIP_MAIN);
265 if (perf_test(ds, p) < 0)
269 printf("=== Performance test of distributor (burst mode) ===\n");
270 rte_eal_mp_remote_launch(handle_work, db, SKIP_MAIN);
271 if (perf_test(db, p) < 0)
278 #endif /* !RTE_EXEC_ENV_WINDOWS */
280 REGISTER_TEST_COMMAND(distributor_perf_autotest, test_distributor_perf);