New upstream version 16.11.7
[deb_dpdk.git] / app / test / test_distributor_perf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 20 /* log 2 of how many iterations we do when timing. */
45 #define BURST 32
46 #define BIG_BATCH 1024
47
48 /* static vars - zero initialized by default */
49 static volatile int quit;
50 static volatile unsigned worker_idx;
51
52 struct worker_stats {
53         volatile unsigned handled_packets;
54 } __rte_cache_aligned;
55 struct worker_stats worker_stats[RTE_MAX_LCORE];
56
57 /* worker thread used for testing the time to do a round-trip of a cache
58  * line between two cores and back again
59  */
60 static int
61 flip_bit(volatile uint64_t *arg)
62 {
63         uint64_t old_val = 0;
64         while (old_val != 2) {
65                 while (!*arg)
66                         rte_pause();
67                 old_val = *arg;
68                 *arg = 0;
69         }
70         return 0;
71 }
72
73 /* test case to time the number of cycles to round-trip a cache line between
74  * two cores and back again.
75  */
76 static void
77 time_cache_line_switch(void)
78 {
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)];
81
82         unsigned i, slaveid = rte_get_next_lcore(rte_lcore_id(), 0, 0);
83         volatile uint64_t *pdata = &data[0];
84         *pdata = 1;
85         rte_eal_remote_launch((lcore_function_t *)flip_bit, &data[0], slaveid);
86         while (*pdata)
87                 rte_pause();
88
89         const uint64_t start_time = rte_rdtsc();
90         for (i = 0; i < (1 << ITER_POWER); i++) {
91                 while (*pdata)
92                         rte_pause();
93                 *pdata = 1;
94         }
95         const uint64_t end_time = rte_rdtsc();
96
97         while (*pdata)
98                 rte_pause();
99         *pdata = 2;
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);
106 }
107
108 /* returns the total count of the number of packets handled by the worker
109  * functions given below.
110  */
111 static unsigned
112 total_packet_count(void)
113 {
114         unsigned i, count = 0;
115         for (i = 0; i < worker_idx; i++)
116                 count += worker_stats[i].handled_packets;
117         return count;
118 }
119
120 /* resets the packet counts for a new test */
121 static void
122 clear_packet_count(void)
123 {
124         memset(&worker_stats, 0, sizeof(worker_stats));
125 }
126
127 /* this is the basic worker function for performance tests.
128  * it does nothing but return packets and count them.
129  */
130 static int
131 handle_work(void *arg)
132 {
133         struct rte_mbuf *pkt = NULL;
134         struct rte_distributor *d = arg;
135         unsigned count = 0;
136         unsigned id = __sync_fetch_and_add(&worker_idx, 1);
137
138         pkt = rte_distributor_get_pkt(d, id, NULL);
139         while (!quit) {
140                 worker_stats[id].handled_packets++, count++;
141                 pkt = rte_distributor_get_pkt(d, id, pkt);
142         }
143         worker_stats[id].handled_packets++, count++;
144         rte_distributor_return_pkt(d, id, pkt);
145         return 0;
146 }
147
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.
151  */
152 static inline int
153 perf_test(struct rte_distributor *d, struct rte_mempool *p)
154 {
155         unsigned i;
156         uint64_t start, end;
157         struct rte_mbuf *bufs[BURST];
158
159         clear_packet_count();
160         if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
161                 printf("Error getting mbufs from pool\n");
162                 return -1;
163         }
164         /* ensure we have different hash value for each pkt */
165         for (i = 0; i < BURST; i++)
166                 bufs[i]->hash.usr = i;
167
168         start = rte_rdtsc();
169         for (i = 0; i < (1<<ITER_POWER); i++)
170                 rte_distributor_process(d, bufs, BURST);
171         end = rte_rdtsc();
172
173         do {
174                 usleep(100);
175                 rte_distributor_process(d, NULL, 0);
176         } while (total_packet_count() < (BURST << ITER_POWER));
177
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);
183
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");
190
191         return 0;
192 }
193
194 /* Useful function which ensures that all worker functions terminate */
195 static void
196 quit_workers(struct rte_distributor *d, struct rte_mempool *p)
197 {
198         const unsigned num_workers = rte_lcore_count() - 1;
199         unsigned i;
200         struct rte_mbuf *bufs[RTE_MAX_LCORE];
201         rte_mempool_get_bulk(p, (void *)bufs, num_workers);
202
203         quit = 1;
204         for (i = 0; i < num_workers; i++)
205                 bufs[i]->hash.usr = i << 1;
206         rte_distributor_process(d, bufs, num_workers);
207
208         rte_mempool_put_bulk(p, (void *)bufs, num_workers);
209
210         rte_distributor_process(d, NULL, 0);
211         rte_eal_mp_wait_lcore();
212         quit = 0;
213         worker_idx = 0;
214 }
215
216 static int
217 test_distributor_perf(void)
218 {
219         static struct rte_distributor *d;
220         static struct rte_mempool *p;
221
222         if (rte_lcore_count() < 2) {
223                 printf("ERROR: not enough cores to test distributor\n");
224                 return -1;
225         }
226
227         /* first time how long it takes to round-trip a cache line */
228         time_cache_line_switch();
229
230         if (d == NULL) {
231                 d = rte_distributor_create("Test_perf", rte_socket_id(),
232                                 rte_lcore_count() - 1);
233                 if (d == NULL) {
234                         printf("Error creating distributor\n");
235                         return -1;
236                 }
237         } else {
238                 rte_distributor_flush(d);
239                 rte_distributor_clear_returns(d);
240         }
241
242         const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
243                         (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
244         if (p == NULL) {
245                 p = rte_pktmbuf_pool_create("DPT_MBUF_POOL", nb_bufs, BURST,
246                         0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
247                 if (p == NULL) {
248                         printf("Error creating mempool\n");
249                         return -1;
250                 }
251         }
252
253         rte_eal_mp_remote_launch(handle_work, d, SKIP_MASTER);
254         if (perf_test(d, p) < 0)
255                 return -1;
256         quit_workers(d, p);
257
258         return 0;
259 }
260
261 REGISTER_TEST_COMMAND(distributor_perf_autotest, test_distributor_perf);