4 * Copyright(c) 2010-2014 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 20 /* log 2 of how many iterations we do when timing. */
46 #define BIG_BATCH 1024
48 /* static vars - zero initialized by default */
49 static volatile int quit;
50 static volatile unsigned worker_idx;
53 volatile unsigned handled_packets;
54 } __rte_cache_aligned;
55 struct worker_stats worker_stats[RTE_MAX_LCORE];
57 /* worker thread used for testing the time to do a round-trip of a cache
58 * line between two cores and back again
61 flip_bit(volatile uint64_t *arg)
64 while (old_val != 2) {
73 /* test case to time the number of cycles to round-trip a cache line between
74 * two cores and back again.
77 time_cache_line_switch(void)
79 /* allocate a full cache line for data, we use only first byte of it */
80 uint64_t data[RTE_CACHE_LINE_SIZE*3 / sizeof(uint64_t)];
82 unsigned i, slaveid = rte_get_next_lcore(rte_lcore_id(), 0, 0);
83 volatile uint64_t *pdata = &data[0];
85 rte_eal_remote_launch((lcore_function_t *)flip_bit, &data[0], slaveid);
89 const uint64_t start_time = rte_rdtsc();
90 for (i = 0; i < (1 << ITER_POWER); i++) {
95 const uint64_t end_time = rte_rdtsc();
100 rte_eal_wait_lcore(slaveid);
101 printf("==== Cache line switch test ===\n");
102 printf("Time for %u iterations = %"PRIu64" ticks\n", (1<<ITER_POWER),
103 end_time-start_time);
104 printf("Ticks per iteration = %"PRIu64"\n\n",
105 (end_time-start_time) >> ITER_POWER);
108 /* returns the total count of the number of packets handled by the worker
109 * functions given below.
112 total_packet_count(void)
114 unsigned i, count = 0;
115 for (i = 0; i < worker_idx; i++)
116 count += worker_stats[i].handled_packets;
120 /* resets the packet counts for a new test */
122 clear_packet_count(void)
124 memset(&worker_stats, 0, sizeof(worker_stats));
127 /* this is the basic worker function for performance tests.
128 * it does nothing but return packets and count them.
131 handle_work(void *arg)
133 struct rte_mbuf *pkt = NULL;
134 struct rte_distributor *d = arg;
136 unsigned id = __sync_fetch_and_add(&worker_idx, 1);
138 pkt = rte_distributor_get_pkt(d, id, NULL);
140 worker_stats[id].handled_packets++, count++;
141 pkt = rte_distributor_get_pkt(d, id, pkt);
143 worker_stats[id].handled_packets++, count++;
144 rte_distributor_return_pkt(d, id, pkt);
148 /* this basic performance test just repeatedly sends in 32 packets at a time
149 * to the distributor and verifies at the end that we got them all in the worker
150 * threads and finally how long per packet the processing took.
153 perf_test(struct rte_distributor *d, struct rte_mempool *p)
157 struct rte_mbuf *bufs[BURST];
159 clear_packet_count();
160 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
161 printf("Error getting mbufs from pool\n");
164 /* ensure we have different hash value for each pkt */
165 for (i = 0; i < BURST; i++)
166 bufs[i]->hash.usr = i;
169 for (i = 0; i < (1<<ITER_POWER); i++)
170 rte_distributor_process(d, bufs, BURST);
175 rte_distributor_process(d, NULL, 0);
176 } while (total_packet_count() < (BURST << ITER_POWER));
178 printf("=== Performance test of distributor ===\n");
179 printf("Time per burst: %"PRIu64"\n", (end - start) >> ITER_POWER);
180 printf("Time per packet: %"PRIu64"\n\n",
181 ((end - start) >> ITER_POWER)/BURST);
182 rte_mempool_put_bulk(p, (void *)bufs, BURST);
184 for (i = 0; i < rte_lcore_count() - 1; i++)
185 printf("Worker %u handled %u packets\n", i,
186 worker_stats[i].handled_packets);
187 printf("Total packets: %u (%x)\n", total_packet_count(),
188 total_packet_count());
189 printf("=== Perf test done ===\n\n");
194 /* Useful function which ensures that all worker functions terminate */
196 quit_workers(struct rte_distributor *d, struct rte_mempool *p)
198 const unsigned num_workers = rte_lcore_count() - 1;
200 struct rte_mbuf *bufs[RTE_MAX_LCORE];
201 rte_mempool_get_bulk(p, (void *)bufs, num_workers);
204 for (i = 0; i < num_workers; i++)
205 bufs[i]->hash.usr = i << 1;
206 rte_distributor_process(d, bufs, num_workers);
208 rte_mempool_put_bulk(p, (void *)bufs, num_workers);
210 rte_distributor_process(d, NULL, 0);
211 rte_eal_mp_wait_lcore();
217 test_distributor_perf(void)
219 static struct rte_distributor *d;
220 static struct rte_mempool *p;
222 if (rte_lcore_count() < 2) {
223 printf("ERROR: not enough cores to test distributor\n");
227 /* first time how long it takes to round-trip a cache line */
228 time_cache_line_switch();
231 d = rte_distributor_create("Test_perf", rte_socket_id(),
232 rte_lcore_count() - 1);
234 printf("Error creating distributor\n");
238 rte_distributor_flush(d);
239 rte_distributor_clear_returns(d);
242 const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
243 (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
245 p = rte_pktmbuf_pool_create("DPT_MBUF_POOL", nb_bufs, BURST,
246 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
248 printf("Error creating mempool\n");
253 rte_eal_mp_remote_launch(handle_work, d, SKIP_MASTER);
254 if (perf_test(d, p) < 0)
261 REGISTER_TEST_COMMAND(distributor_perf_autotest, test_distributor_perf);