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