732d86d0edc7b13eb99f42ecaa155cfcf0550f33
[deb_dpdk.git] / test / test / test_distributor_perf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include "test.h"
35
36 #include <unistd.h>
37 #include <string.h>
38 #include <rte_mempool.h>
39 #include <rte_cycles.h>
40 #include <rte_common.h>
41 #include <rte_mbuf.h>
42 #include <rte_distributor.h>
43
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. */
46 #define BURST 64
47 #define BIG_BATCH 1024
48
49 /* static vars - zero initialized by default */
50 static volatile int quit;
51 static volatile unsigned worker_idx;
52
53 struct worker_stats {
54         volatile unsigned handled_packets;
55 } __rte_cache_aligned;
56 struct worker_stats worker_stats[RTE_MAX_LCORE];
57
58 /*
59  * worker thread used for testing the time to do a round-trip of a cache
60  * line between two cores and back again
61  */
62 static void
63 flip_bit(volatile uint64_t *arg)
64 {
65         uint64_t old_val = 0;
66         while (old_val != 2) {
67                 while (!*arg)
68                         rte_pause();
69                 old_val = *arg;
70                 *arg = 0;
71         }
72 }
73
74 /*
75  * test case to time the number of cycles to round-trip a cache line between
76  * two cores and back again.
77  */
78 static void
79 time_cache_line_switch(void)
80 {
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)];
83
84         unsigned i, slaveid = rte_get_next_lcore(rte_lcore_id(), 0, 0);
85         volatile uint64_t *pdata = &data[0];
86         *pdata = 1;
87         rte_eal_remote_launch((lcore_function_t *)flip_bit, &data[0], slaveid);
88         while (*pdata)
89                 rte_pause();
90
91         const uint64_t start_time = rte_rdtsc();
92         for (i = 0; i < (1 << ITER_POWER_CL); i++) {
93                 while (*pdata)
94                         rte_pause();
95                 *pdata = 1;
96         }
97         const uint64_t end_time = rte_rdtsc();
98
99         while (*pdata)
100                 rte_pause();
101         *pdata = 2;
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);
108 }
109
110 /*
111  * returns the total count of the number of packets handled by the worker
112  * functions given below.
113  */
114 static unsigned
115 total_packet_count(void)
116 {
117         unsigned i, count = 0;
118         for (i = 0; i < worker_idx; i++)
119                 count += worker_stats[i].handled_packets;
120         return count;
121 }
122
123 /* resets the packet counts for a new test */
124 static void
125 clear_packet_count(void)
126 {
127         memset(&worker_stats, 0, sizeof(worker_stats));
128 }
129
130 /*
131  * This is the basic worker function for performance tests.
132  * it does nothing but return packets and count them.
133  */
134 static int
135 handle_work(void *arg)
136 {
137         struct rte_distributor *d = arg;
138         unsigned int count = 0;
139         unsigned int num = 0;
140         int i;
141         unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
142         struct rte_mbuf *buf[8] __rte_cache_aligned;
143
144         for (i = 0; i < 8; i++)
145                 buf[i] = NULL;
146
147         num = rte_distributor_get_pkt(d, id, buf, buf, num);
148         while (!quit) {
149                 worker_stats[id].handled_packets += num;
150                 count += num;
151                 num = rte_distributor_get_pkt(d, id, buf, buf, num);
152         }
153         worker_stats[id].handled_packets += num;
154         count += num;
155         rte_distributor_return_pkt(d, id, buf, num);
156         return 0;
157 }
158
159 /*
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.
163  */
164 static inline int
165 perf_test(struct rte_distributor *d, struct rte_mempool *p)
166 {
167         unsigned int i;
168         uint64_t start, end;
169         struct rte_mbuf *bufs[BURST];
170
171         clear_packet_count();
172         if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
173                 printf("Error getting mbufs from pool\n");
174                 return -1;
175         }
176         /* ensure we have different hash value for each pkt */
177         for (i = 0; i < BURST; i++)
178                 bufs[i]->hash.usr = i;
179
180         start = rte_rdtsc();
181         for (i = 0; i < (1<<ITER_POWER); i++)
182                 rte_distributor_process(d, bufs, BURST);
183         end = rte_rdtsc();
184
185         do {
186                 usleep(100);
187                 rte_distributor_process(d, NULL, 0);
188         } while (total_packet_count() < (BURST << ITER_POWER));
189
190         rte_distributor_clear_returns(d);
191
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);
196
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");
203
204         return 0;
205 }
206
207 /* Useful function which ensures that all worker functions terminate */
208 static void
209 quit_workers(struct rte_distributor *d, struct rte_mempool *p)
210 {
211         const unsigned int num_workers = rte_lcore_count() - 1;
212         unsigned int i;
213         struct rte_mbuf *bufs[RTE_MAX_LCORE];
214
215         rte_mempool_get_bulk(p, (void *)bufs, num_workers);
216
217         quit = 1;
218         for (i = 0; i < num_workers; i++)
219                 bufs[i]->hash.usr = i << 1;
220         rte_distributor_process(d, bufs, num_workers);
221
222         rte_mempool_put_bulk(p, (void *)bufs, num_workers);
223
224         rte_distributor_process(d, NULL, 0);
225         rte_eal_mp_wait_lcore();
226         quit = 0;
227         worker_idx = 0;
228 }
229
230 static int
231 test_distributor_perf(void)
232 {
233         static struct rte_distributor *ds;
234         static struct rte_distributor *db;
235         static struct rte_mempool *p;
236
237         if (rte_lcore_count() < 2) {
238                 printf("ERROR: not enough cores to test distributor\n");
239                 return -1;
240         }
241
242         /* first time how long it takes to round-trip a cache line */
243         time_cache_line_switch();
244
245         if (ds == NULL) {
246                 ds = rte_distributor_create("Test_perf", rte_socket_id(),
247                                 rte_lcore_count() - 1,
248                                 RTE_DIST_ALG_SINGLE);
249                 if (ds == NULL) {
250                         printf("Error creating distributor\n");
251                         return -1;
252                 }
253         } else {
254                 rte_distributor_clear_returns(ds);
255         }
256
257         if (db == NULL) {
258                 db = rte_distributor_create("Test_burst", rte_socket_id(),
259                                 rte_lcore_count() - 1,
260                                 RTE_DIST_ALG_BURST);
261                 if (db == NULL) {
262                         printf("Error creating burst distributor\n");
263                         return -1;
264                 }
265         } else {
266                 rte_distributor_clear_returns(db);
267         }
268
269         const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
270                         (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
271         if (p == NULL) {
272                 p = rte_pktmbuf_pool_create("DPT_MBUF_POOL", nb_bufs, BURST,
273                         0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
274                 if (p == NULL) {
275                         printf("Error creating mempool\n");
276                         return -1;
277                 }
278         }
279
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)
283                 return -1;
284         quit_workers(ds, p);
285
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)
289                 return -1;
290         quit_workers(db, p);
291
292         return 0;
293 }
294
295 REGISTER_TEST_COMMAND(distributor_perf_autotest, test_distributor_perf);