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