1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <sys/queue.h>
14 #include <rte_common.h>
16 #include <rte_debug.h>
17 #include <rte_memory.h>
18 #include <rte_launch.h>
19 #include <rte_cycles.h>
21 #include <rte_per_lcore.h>
22 #include <rte_lcore.h>
23 #include <rte_atomic.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_mempool.h>
26 #include <rte_spinlock.h>
27 #include <rte_malloc.h>
28 #include <rte_mbuf_pool_ops.h>
36 * Each core get *n_keep* objects per bulk of *n_get_bulk*. Then,
37 * objects are put back in the pool per bulk of *n_put_bulk*.
39 * This sequence is done during TIME_S seconds.
41 * This test is done on the following configurations:
43 * - Cores configuration (*cores*)
45 * - One core with cache
46 * - Two cores with cache
47 * - Max. cores with cache
48 * - One core without cache
49 * - Two cores without cache
50 * - Max. cores without cache
51 * - One core with user-owned cache
52 * - Two cores with user-owned cache
53 * - Max. cores with user-owned cache
55 * - Bulk size (*n_get_bulk*, *n_put_bulk*)
57 * - Bulk get from 1 to 32
58 * - Bulk put from 1 to 32
60 * - Number of kept objects (*n_keep*)
68 #define MEMPOOL_ELT_SIZE 2048
70 #define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
72 #define LOG_ERR() printf("test failed at %s():%d\n", __func__, __LINE__)
73 #define RET_ERR() do { \
77 #define GOTO_ERR(var, label) do { \
83 static int use_external_cache;
84 static unsigned external_cache_size = RTE_MEMPOOL_CACHE_MAX_SIZE;
86 static rte_atomic32_t synchro;
88 /* number of objects in one bulk operation (get or put) */
89 static unsigned n_get_bulk;
90 static unsigned n_put_bulk;
92 /* number of objects retrived from mempool before putting them back */
93 static unsigned n_keep;
95 /* number of enqueues / dequeues */
96 struct mempool_test_stats {
98 } __rte_cache_aligned;
100 static struct mempool_test_stats stats[RTE_MAX_LCORE];
103 * save the object number in the first 4 bytes of object data. All
104 * other bytes are set to 0.
107 my_obj_init(struct rte_mempool *mp, __rte_unused void *arg,
108 void *obj, unsigned i)
110 uint32_t *objnum = obj;
111 memset(obj, 0, mp->elt_size);
116 per_lcore_mempool_test(void *arg)
118 void *obj_table[MAX_KEEP];
120 struct rte_mempool *mp = arg;
121 unsigned lcore_id = rte_lcore_id();
123 uint64_t start_cycles, end_cycles;
124 uint64_t time_diff = 0, hz = rte_get_timer_hz();
125 struct rte_mempool_cache *cache;
127 if (use_external_cache) {
128 /* Create a user-owned mempool cache. */
129 cache = rte_mempool_cache_create(external_cache_size,
134 /* May be NULL if cache is disabled. */
135 cache = rte_mempool_default_cache(mp, lcore_id);
138 /* n_get_bulk and n_put_bulk must be divisors of n_keep */
139 if (((n_keep / n_get_bulk) * n_get_bulk) != n_keep)
141 if (((n_keep / n_put_bulk) * n_put_bulk) != n_keep)
144 stats[lcore_id].enq_count = 0;
146 /* wait synchro for slaves */
147 if (lcore_id != rte_get_master_lcore())
148 while (rte_atomic32_read(&synchro) == 0);
150 start_cycles = rte_get_timer_cycles();
152 while (time_diff/hz < TIME_S) {
153 for (i = 0; likely(i < (N/n_keep)); i++) {
154 /* get n_keep objects by bulk of n_bulk */
156 while (idx < n_keep) {
157 ret = rte_mempool_generic_get(mp,
161 if (unlikely(ret < 0)) {
162 rte_mempool_dump(stdout, mp);
163 /* in this case, objects are lost... */
169 /* put the objects back */
171 while (idx < n_keep) {
172 rte_mempool_generic_put(mp, &obj_table[idx],
178 end_cycles = rte_get_timer_cycles();
179 time_diff = end_cycles - start_cycles;
180 stats[lcore_id].enq_count += N;
184 if (use_external_cache) {
185 rte_mempool_cache_flush(cache, mp);
186 rte_mempool_cache_free(cache);
192 /* launch all the per-lcore test, and display the result */
194 launch_cores(struct rte_mempool *mp, unsigned int cores)
199 unsigned cores_save = cores;
201 rte_atomic32_set(&synchro, 0);
204 memset(stats, 0, sizeof(stats));
206 printf("mempool_autotest cache=%u cores=%u n_get_bulk=%u "
207 "n_put_bulk=%u n_keep=%u ",
209 external_cache_size : (unsigned) mp->cache_size,
210 cores, n_get_bulk, n_put_bulk, n_keep);
212 if (rte_mempool_avail_count(mp) != MEMPOOL_SIZE) {
213 printf("mempool is not full\n");
217 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
221 rte_eal_remote_launch(per_lcore_mempool_test,
225 /* start synchro and launch test on master */
226 rte_atomic32_set(&synchro, 1);
228 ret = per_lcore_mempool_test(mp);
231 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
235 if (rte_eal_wait_lcore(lcore_id) < 0)
240 printf("per-lcore test returned -1\n");
245 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
246 rate += (stats[lcore_id].enq_count / TIME_S);
248 printf("rate_persec=%" PRIu64 "\n", rate);
253 /* for a given number of core, launch all test cases */
255 do_one_mempool_test(struct rte_mempool *mp, unsigned int cores)
257 unsigned bulk_tab_get[] = { 1, 4, 32, 0 };
258 unsigned bulk_tab_put[] = { 1, 4, 32, 0 };
259 unsigned keep_tab[] = { 32, 128, 0 };
260 unsigned *get_bulk_ptr;
261 unsigned *put_bulk_ptr;
265 for (get_bulk_ptr = bulk_tab_get; *get_bulk_ptr; get_bulk_ptr++) {
266 for (put_bulk_ptr = bulk_tab_put; *put_bulk_ptr; put_bulk_ptr++) {
267 for (keep_ptr = keep_tab; *keep_ptr; keep_ptr++) {
269 n_get_bulk = *get_bulk_ptr;
270 n_put_bulk = *put_bulk_ptr;
272 ret = launch_cores(mp, cores);
283 test_mempool_perf(void)
285 struct rte_mempool *mp_cache = NULL;
286 struct rte_mempool *mp_nocache = NULL;
287 struct rte_mempool *default_pool = NULL;
288 const char *default_pool_ops;
291 rte_atomic32_init(&synchro);
293 /* create a mempool (without cache) */
294 mp_nocache = rte_mempool_create("perf_test_nocache", MEMPOOL_SIZE,
295 MEMPOOL_ELT_SIZE, 0, 0,
299 if (mp_nocache == NULL)
302 /* create a mempool (with cache) */
303 mp_cache = rte_mempool_create("perf_test_cache", MEMPOOL_SIZE,
305 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
309 if (mp_cache == NULL)
312 default_pool_ops = rte_mbuf_best_mempool_ops();
313 /* Create a mempool based on Default handler */
314 default_pool = rte_mempool_create_empty("default_pool",
320 if (default_pool == NULL) {
321 printf("cannot allocate %s mempool\n", default_pool_ops);
325 if (rte_mempool_set_ops_byname(default_pool, default_pool_ops, NULL)
327 printf("cannot set %s handler\n", default_pool_ops);
331 if (rte_mempool_populate_default(default_pool) < 0) {
332 printf("cannot populate %s mempool\n", default_pool_ops);
336 rte_mempool_obj_iter(default_pool, my_obj_init, NULL);
338 /* performance test with 1, 2 and max cores */
339 printf("start performance test (without cache)\n");
341 if (do_one_mempool_test(mp_nocache, 1) < 0)
344 if (do_one_mempool_test(mp_nocache, 2) < 0)
347 if (do_one_mempool_test(mp_nocache, rte_lcore_count()) < 0)
350 /* performance test with 1, 2 and max cores */
351 printf("start performance test for %s (without cache)\n",
354 if (do_one_mempool_test(default_pool, 1) < 0)
357 if (do_one_mempool_test(default_pool, 2) < 0)
360 if (do_one_mempool_test(default_pool, rte_lcore_count()) < 0)
363 /* performance test with 1, 2 and max cores */
364 printf("start performance test (with cache)\n");
366 if (do_one_mempool_test(mp_cache, 1) < 0)
369 if (do_one_mempool_test(mp_cache, 2) < 0)
372 if (do_one_mempool_test(mp_cache, rte_lcore_count()) < 0)
375 /* performance test with 1, 2 and max cores */
376 printf("start performance test (with user-owned cache)\n");
377 use_external_cache = 1;
379 if (do_one_mempool_test(mp_nocache, 1) < 0)
382 if (do_one_mempool_test(mp_nocache, 2) < 0)
385 if (do_one_mempool_test(mp_nocache, rte_lcore_count()) < 0)
388 rte_mempool_list_dump(stdout);
393 rte_mempool_free(mp_cache);
394 rte_mempool_free(mp_nocache);
395 rte_mempool_free(default_pool);
399 REGISTER_TEST_COMMAND(mempool_perf_autotest, test_mempool_perf);