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