New upstream version 17.11-rc3
[deb_dpdk.git] / test / test / test_mempool_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 <string.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <stdarg.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_common.h>
44 #include <rte_log.h>
45 #include <rte_debug.h>
46 #include <rte_memory.h>
47 #include <rte_launch.h>
48 #include <rte_cycles.h>
49 #include <rte_eal.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_atomic.h>
53 #include <rte_branch_prediction.h>
54 #include <rte_mempool.h>
55 #include <rte_spinlock.h>
56 #include <rte_malloc.h>
57
58 #include "test.h"
59
60 /*
61  * Mempool performance
62  * =======
63  *
64  *    Each core get *n_keep* objects per bulk of *n_get_bulk*. Then,
65  *    objects are put back in the pool per bulk of *n_put_bulk*.
66  *
67  *    This sequence is done during TIME_S seconds.
68  *
69  *    This test is done on the following configurations:
70  *
71  *    - Cores configuration (*cores*)
72  *
73  *      - One core with cache
74  *      - Two cores with cache
75  *      - Max. cores with cache
76  *      - One core without cache
77  *      - Two cores without cache
78  *      - Max. cores without cache
79  *      - One core with user-owned cache
80  *      - Two cores with user-owned cache
81  *      - Max. cores with user-owned cache
82  *
83  *    - Bulk size (*n_get_bulk*, *n_put_bulk*)
84  *
85  *      - Bulk get from 1 to 32
86  *      - Bulk put from 1 to 32
87  *
88  *    - Number of kept objects (*n_keep*)
89  *
90  *      - 32
91  *      - 128
92  */
93
94 #define N 65536
95 #define TIME_S 5
96 #define MEMPOOL_ELT_SIZE 2048
97 #define MAX_KEEP 128
98 #define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
99
100 #define LOG_ERR() printf("test failed at %s():%d\n", __func__, __LINE__)
101 #define RET_ERR() do {                                                  \
102                 LOG_ERR();                                              \
103                 return -1;                                              \
104         } while (0)
105 #define GOTO_ERR(var, label) do {                                       \
106                 LOG_ERR();                                              \
107                 var = -1;                                               \
108                 goto label;                                             \
109         } while (0)
110
111 static int use_external_cache;
112 static unsigned external_cache_size = RTE_MEMPOOL_CACHE_MAX_SIZE;
113
114 static rte_atomic32_t synchro;
115
116 /* number of objects in one bulk operation (get or put) */
117 static unsigned n_get_bulk;
118 static unsigned n_put_bulk;
119
120 /* number of objects retrived from mempool before putting them back */
121 static unsigned n_keep;
122
123 /* number of enqueues / dequeues */
124 struct mempool_test_stats {
125         uint64_t enq_count;
126 } __rte_cache_aligned;
127
128 static struct mempool_test_stats stats[RTE_MAX_LCORE];
129
130 /*
131  * save the object number in the first 4 bytes of object data. All
132  * other bytes are set to 0.
133  */
134 static void
135 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
136             void *obj, unsigned i)
137 {
138         uint32_t *objnum = obj;
139         memset(obj, 0, mp->elt_size);
140         *objnum = i;
141 }
142
143 static int
144 per_lcore_mempool_test(void *arg)
145 {
146         void *obj_table[MAX_KEEP];
147         unsigned i, idx;
148         struct rte_mempool *mp = arg;
149         unsigned lcore_id = rte_lcore_id();
150         int ret = 0;
151         uint64_t start_cycles, end_cycles;
152         uint64_t time_diff = 0, hz = rte_get_timer_hz();
153         struct rte_mempool_cache *cache;
154
155         if (use_external_cache) {
156                 /* Create a user-owned mempool cache. */
157                 cache = rte_mempool_cache_create(external_cache_size,
158                                                  SOCKET_ID_ANY);
159                 if (cache == NULL)
160                         RET_ERR();
161         } else {
162                 /* May be NULL if cache is disabled. */
163                 cache = rte_mempool_default_cache(mp, lcore_id);
164         }
165
166         /* n_get_bulk and n_put_bulk must be divisors of n_keep */
167         if (((n_keep / n_get_bulk) * n_get_bulk) != n_keep)
168                 GOTO_ERR(ret, out);
169         if (((n_keep / n_put_bulk) * n_put_bulk) != n_keep)
170                 GOTO_ERR(ret, out);
171
172         stats[lcore_id].enq_count = 0;
173
174         /* wait synchro for slaves */
175         if (lcore_id != rte_get_master_lcore())
176                 while (rte_atomic32_read(&synchro) == 0);
177
178         start_cycles = rte_get_timer_cycles();
179
180         while (time_diff/hz < TIME_S) {
181                 for (i = 0; likely(i < (N/n_keep)); i++) {
182                         /* get n_keep objects by bulk of n_bulk */
183                         idx = 0;
184                         while (idx < n_keep) {
185                                 ret = rte_mempool_generic_get(mp,
186                                                               &obj_table[idx],
187                                                               n_get_bulk,
188                                                               cache);
189                                 if (unlikely(ret < 0)) {
190                                         rte_mempool_dump(stdout, mp);
191                                         /* in this case, objects are lost... */
192                                         GOTO_ERR(ret, out);
193                                 }
194                                 idx += n_get_bulk;
195                         }
196
197                         /* put the objects back */
198                         idx = 0;
199                         while (idx < n_keep) {
200                                 rte_mempool_generic_put(mp, &obj_table[idx],
201                                                         n_put_bulk,
202                                                         cache);
203                                 idx += n_put_bulk;
204                         }
205                 }
206                 end_cycles = rte_get_timer_cycles();
207                 time_diff = end_cycles - start_cycles;
208                 stats[lcore_id].enq_count += N;
209         }
210
211 out:
212         if (use_external_cache) {
213                 rte_mempool_cache_flush(cache, mp);
214                 rte_mempool_cache_free(cache);
215         }
216
217         return ret;
218 }
219
220 /* launch all the per-lcore test, and display the result */
221 static int
222 launch_cores(struct rte_mempool *mp, unsigned int cores)
223 {
224         unsigned lcore_id;
225         uint64_t rate;
226         int ret;
227         unsigned cores_save = cores;
228
229         rte_atomic32_set(&synchro, 0);
230
231         /* reset stats */
232         memset(stats, 0, sizeof(stats));
233
234         printf("mempool_autotest cache=%u cores=%u n_get_bulk=%u "
235                "n_put_bulk=%u n_keep=%u ",
236                use_external_cache ?
237                    external_cache_size : (unsigned) mp->cache_size,
238                cores, n_get_bulk, n_put_bulk, n_keep);
239
240         if (rte_mempool_avail_count(mp) != MEMPOOL_SIZE) {
241                 printf("mempool is not full\n");
242                 return -1;
243         }
244
245         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
246                 if (cores == 1)
247                         break;
248                 cores--;
249                 rte_eal_remote_launch(per_lcore_mempool_test,
250                                       mp, lcore_id);
251         }
252
253         /* start synchro and launch test on master */
254         rte_atomic32_set(&synchro, 1);
255
256         ret = per_lcore_mempool_test(mp);
257
258         cores = cores_save;
259         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
260                 if (cores == 1)
261                         break;
262                 cores--;
263                 if (rte_eal_wait_lcore(lcore_id) < 0)
264                         ret = -1;
265         }
266
267         if (ret < 0) {
268                 printf("per-lcore test returned -1\n");
269                 return -1;
270         }
271
272         rate = 0;
273         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
274                 rate += (stats[lcore_id].enq_count / TIME_S);
275
276         printf("rate_persec=%" PRIu64 "\n", rate);
277
278         return 0;
279 }
280
281 /* for a given number of core, launch all test cases */
282 static int
283 do_one_mempool_test(struct rte_mempool *mp, unsigned int cores)
284 {
285         unsigned bulk_tab_get[] = { 1, 4, 32, 0 };
286         unsigned bulk_tab_put[] = { 1, 4, 32, 0 };
287         unsigned keep_tab[] = { 32, 128, 0 };
288         unsigned *get_bulk_ptr;
289         unsigned *put_bulk_ptr;
290         unsigned *keep_ptr;
291         int ret;
292
293         for (get_bulk_ptr = bulk_tab_get; *get_bulk_ptr; get_bulk_ptr++) {
294                 for (put_bulk_ptr = bulk_tab_put; *put_bulk_ptr; put_bulk_ptr++) {
295                         for (keep_ptr = keep_tab; *keep_ptr; keep_ptr++) {
296
297                                 n_get_bulk = *get_bulk_ptr;
298                                 n_put_bulk = *put_bulk_ptr;
299                                 n_keep = *keep_ptr;
300                                 ret = launch_cores(mp, cores);
301
302                                 if (ret < 0)
303                                         return -1;
304                         }
305                 }
306         }
307         return 0;
308 }
309
310 static int
311 test_mempool_perf(void)
312 {
313         struct rte_mempool *mp_cache = NULL;
314         struct rte_mempool *mp_nocache = NULL;
315         struct rte_mempool *default_pool = NULL;
316         int ret = -1;
317
318         rte_atomic32_init(&synchro);
319
320         /* create a mempool (without cache) */
321         mp_nocache = rte_mempool_create("perf_test_nocache", MEMPOOL_SIZE,
322                                         MEMPOOL_ELT_SIZE, 0, 0,
323                                         NULL, NULL,
324                                         my_obj_init, NULL,
325                                         SOCKET_ID_ANY, 0);
326         if (mp_nocache == NULL)
327                 goto err;
328
329         /* create a mempool (with cache) */
330         mp_cache = rte_mempool_create("perf_test_cache", MEMPOOL_SIZE,
331                                       MEMPOOL_ELT_SIZE,
332                                       RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
333                                       NULL, NULL,
334                                       my_obj_init, NULL,
335                                       SOCKET_ID_ANY, 0);
336         if (mp_cache == NULL)
337                 goto err;
338
339         /* Create a mempool based on Default handler */
340         default_pool = rte_mempool_create_empty("default_pool",
341                                                 MEMPOOL_SIZE,
342                                                 MEMPOOL_ELT_SIZE,
343                                                 0, 0,
344                                                 SOCKET_ID_ANY, 0);
345
346         if (default_pool == NULL) {
347                 printf("cannot allocate %s mempool\n",
348                        RTE_MBUF_DEFAULT_MEMPOOL_OPS);
349                 goto err;
350         }
351
352         if (rte_mempool_set_ops_byname(default_pool,
353                                        RTE_MBUF_DEFAULT_MEMPOOL_OPS, NULL)
354                                        < 0) {
355                 printf("cannot set %s handler\n", RTE_MBUF_DEFAULT_MEMPOOL_OPS);
356                 goto err;
357         }
358
359         if (rte_mempool_populate_default(default_pool) < 0) {
360                 printf("cannot populate %s mempool\n",
361                        RTE_MBUF_DEFAULT_MEMPOOL_OPS);
362                 goto err;
363         }
364
365         rte_mempool_obj_iter(default_pool, my_obj_init, NULL);
366
367         /* performance test with 1, 2 and max cores */
368         printf("start performance test (without cache)\n");
369
370         if (do_one_mempool_test(mp_nocache, 1) < 0)
371                 goto err;
372
373         if (do_one_mempool_test(mp_nocache, 2) < 0)
374                 goto err;
375
376         if (do_one_mempool_test(mp_nocache, rte_lcore_count()) < 0)
377                 goto err;
378
379         /* performance test with 1, 2 and max cores */
380         printf("start performance test for %s (without cache)\n",
381                RTE_MBUF_DEFAULT_MEMPOOL_OPS);
382
383         if (do_one_mempool_test(default_pool, 1) < 0)
384                 goto err;
385
386         if (do_one_mempool_test(default_pool, 2) < 0)
387                 goto err;
388
389         if (do_one_mempool_test(default_pool, rte_lcore_count()) < 0)
390                 goto err;
391
392         /* performance test with 1, 2 and max cores */
393         printf("start performance test (with cache)\n");
394
395         if (do_one_mempool_test(mp_cache, 1) < 0)
396                 goto err;
397
398         if (do_one_mempool_test(mp_cache, 2) < 0)
399                 goto err;
400
401         if (do_one_mempool_test(mp_cache, rte_lcore_count()) < 0)
402                 goto err;
403
404         /* performance test with 1, 2 and max cores */
405         printf("start performance test (with user-owned cache)\n");
406         use_external_cache = 1;
407
408         if (do_one_mempool_test(mp_nocache, 1) < 0)
409                 goto err;
410
411         if (do_one_mempool_test(mp_nocache, 2) < 0)
412                 goto err;
413
414         if (do_one_mempool_test(mp_nocache, rte_lcore_count()) < 0)
415                 goto err;
416
417         rte_mempool_list_dump(stdout);
418
419         ret = 0;
420
421 err:
422         rte_mempool_free(mp_cache);
423         rte_mempool_free(mp_nocache);
424         rte_mempool_free(default_pool);
425         return ret;
426 }
427
428 REGISTER_TEST_COMMAND(mempool_perf_autotest, test_mempool_perf);