New upstream version 17.08
[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 #include <rte_pause.h>
44
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. */
47 #define BURST 64
48 #define BIG_BATCH 1024
49
50 /* static vars - zero initialized by default */
51 static volatile int quit;
52 static volatile unsigned worker_idx;
53
54 struct worker_stats {
55         volatile unsigned handled_packets;
56 } __rte_cache_aligned;
57 struct worker_stats worker_stats[RTE_MAX_LCORE];
58
59 /*
60  * worker thread used for testing the time to do a round-trip of a cache
61  * line between two cores and back again
62  */
63 static void
64 flip_bit(volatile uint64_t *arg)
65 {
66         uint64_t old_val = 0;
67         while (old_val != 2) {
68                 while (!*arg)
69                         rte_pause();
70                 old_val = *arg;
71                 *arg = 0;
72         }
73 }
74
75 /*
76  * test case to time the number of cycles to round-trip a cache line between
77  * two cores and back again.
78  */
79 static void
80 time_cache_line_switch(void)
81 {
82         /* allocate a full cache line for data, we use only first byte of it */
83         uint64_t data[RTE_CACHE_LINE_SIZE*3 / sizeof(uint64_t)];
84
85         unsigned i, slaveid = rte_get_next_lcore(rte_lcore_id(), 0, 0);
86         volatile uint64_t *pdata = &data[0];
87         *pdata = 1;
88         rte_eal_remote_launch((lcore_function_t *)flip_bit, &data[0], slaveid);
89         while (*pdata)
90                 rte_pause();
91
92         const uint64_t start_time = rte_rdtsc();
93         for (i = 0; i < (1 << ITER_POWER_CL); i++) {
94                 while (*pdata)
95                         rte_pause();
96                 *pdata = 1;
97         }
98         const uint64_t end_time = rte_rdtsc();
99
100         while (*pdata)
101                 rte_pause();
102         *pdata = 2;
103         rte_eal_wait_lcore(slaveid);
104         printf("==== Cache line switch test ===\n");
105         printf("Time for %u iterations = %"PRIu64" ticks\n", (1<<ITER_POWER_CL),
106                         end_time-start_time);
107         printf("Ticks per iteration = %"PRIu64"\n\n",
108                         (end_time-start_time) >> ITER_POWER_CL);
109 }
110
111 /*
112  * returns the total count of the number of packets handled by the worker
113  * functions given below.
114  */
115 static unsigned
116 total_packet_count(void)
117 {
118         unsigned i, count = 0;
119         for (i = 0; i < worker_idx; i++)
120                 count += worker_stats[i].handled_packets;
121         return count;
122 }
123
124 /* resets the packet counts for a new test */
125 static void
126 clear_packet_count(void)
127 {
128         memset(&worker_stats, 0, sizeof(worker_stats));
129 }
130
131 /*
132  * This is the basic worker function for performance tests.
133  * it does nothing but return packets and count them.
134  */
135 static int
136 handle_work(void *arg)
137 {
138         struct rte_distributor *d = arg;
139         unsigned int count = 0;
140         unsigned int num = 0;
141         int i;
142         unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
143         struct rte_mbuf *buf[8] __rte_cache_aligned;
144
145         for (i = 0; i < 8; i++)
146                 buf[i] = NULL;
147
148         num = rte_distributor_get_pkt(d, id, buf, buf, num);
149         while (!quit) {
150                 worker_stats[id].handled_packets += num;
151                 count += num;
152                 num = rte_distributor_get_pkt(d, id, buf, buf, num);
153         }
154         worker_stats[id].handled_packets += num;
155         count += num;
156         rte_distributor_return_pkt(d, id, buf, num);
157         return 0;
158 }
159
160 /*
161  * This basic performance test just repeatedly sends in 32 packets at a time
162  * to the distributor and verifies at the end that we got them all in the worker
163  * threads and finally how long per packet the processing took.
164  */
165 static inline int
166 perf_test(struct rte_distributor *d, struct rte_mempool *p)
167 {
168         unsigned int i;
169         uint64_t start, end;
170         struct rte_mbuf *bufs[BURST];
171
172         clear_packet_count();
173         if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
174                 printf("Error getting mbufs from pool\n");
175                 return -1;
176         }
177         /* ensure we have different hash value for each pkt */
178         for (i = 0; i < BURST; i++)
179                 bufs[i]->hash.usr = i;
180
181         start = rte_rdtsc();
182         for (i = 0; i < (1<<ITER_POWER); i++)
183                 rte_distributor_process(d, bufs, BURST);
184         end = rte_rdtsc();
185
186         do {
187                 usleep(100);
188                 rte_distributor_process(d, NULL, 0);
189         } while (total_packet_count() < (BURST << ITER_POWER));
190
191         rte_distributor_clear_returns(d);
192
193         printf("Time per burst:  %"PRIu64"\n", (end - start) >> ITER_POWER);
194         printf("Time per packet: %"PRIu64"\n\n",
195                         ((end - start) >> ITER_POWER)/BURST);
196         rte_mempool_put_bulk(p, (void *)bufs, BURST);
197
198         for (i = 0; i < rte_lcore_count() - 1; i++)
199                 printf("Worker %u handled %u packets\n", i,
200                                 worker_stats[i].handled_packets);
201         printf("Total packets: %u (%x)\n", total_packet_count(),
202                         total_packet_count());
203         printf("=== Perf test done ===\n\n");
204
205         return 0;
206 }
207
208 /* Useful function which ensures that all worker functions terminate */
209 static void
210 quit_workers(struct rte_distributor *d, struct rte_mempool *p)
211 {
212         const unsigned int num_workers = rte_lcore_count() - 1;
213         unsigned int i;
214         struct rte_mbuf *bufs[RTE_MAX_LCORE];
215
216         rte_mempool_get_bulk(p, (void *)bufs, num_workers);
217
218         quit = 1;
219         for (i = 0; i < num_workers; i++)
220                 bufs[i]->hash.usr = i << 1;
221         rte_distributor_process(d, bufs, num_workers);
222
223         rte_mempool_put_bulk(p, (void *)bufs, num_workers);
224
225         rte_distributor_process(d, NULL, 0);
226         rte_eal_mp_wait_lcore();
227         quit = 0;
228         worker_idx = 0;
229 }
230
231 static int
232 test_distributor_perf(void)
233 {
234         static struct rte_distributor *ds;
235         static struct rte_distributor *db;
236         static struct rte_mempool *p;
237
238         if (rte_lcore_count() < 2) {
239                 printf("ERROR: not enough cores to test distributor\n");
240                 return -1;
241         }
242
243         /* first time how long it takes to round-trip a cache line */
244         time_cache_line_switch();
245
246         if (ds == NULL) {
247                 ds = rte_distributor_create("Test_perf", rte_socket_id(),
248                                 rte_lcore_count() - 1,
249                                 RTE_DIST_ALG_SINGLE);
250                 if (ds == NULL) {
251                         printf("Error creating distributor\n");
252                         return -1;
253                 }
254         } else {
255                 rte_distributor_clear_returns(ds);
256         }
257
258         if (db == NULL) {
259                 db = rte_distributor_create("Test_burst", rte_socket_id(),
260                                 rte_lcore_count() - 1,
261                                 RTE_DIST_ALG_BURST);
262                 if (db == NULL) {
263                         printf("Error creating burst distributor\n");
264                         return -1;
265                 }
266         } else {
267                 rte_distributor_clear_returns(db);
268         }
269
270         const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
271                         (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
272         if (p == NULL) {
273                 p = rte_pktmbuf_pool_create("DPT_MBUF_POOL", nb_bufs, BURST,
274                         0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
275                 if (p == NULL) {
276                         printf("Error creating mempool\n");
277                         return -1;
278                 }
279         }
280
281         printf("=== Performance test of distributor (single mode) ===\n");
282         rte_eal_mp_remote_launch(handle_work, ds, SKIP_MASTER);
283         if (perf_test(ds, p) < 0)
284                 return -1;
285         quit_workers(ds, p);
286
287         printf("=== Performance test of distributor (burst mode) ===\n");
288         rte_eal_mp_remote_launch(handle_work, db, SKIP_MASTER);
289         if (perf_test(db, p) < 0)
290                 return -1;
291         quit_workers(db, p);
292
293         return 0;
294 }
295
296 REGISTER_TEST_COMMAND(distributor_perf_autotest, test_distributor_perf);