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_branch_prediction.h>
24 #include <rte_mempool.h>
25 #include <rte_spinlock.h>
26 #include <rte_malloc.h>
27 #include <rte_mbuf_pool_ops.h>
36 * Basic tests: done on one core with and without cache:
38 * - Get one object, put one object
39 * - Get two objects, put two objects
40 * - Get all objects, test that their content is not modified and
41 * put them back in the pool.
44 #define MEMPOOL_ELT_SIZE 2048
46 #define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
48 #define LOG_ERR() printf("test failed at %s():%d\n", __func__, __LINE__)
49 #define RET_ERR() do { \
53 #define GOTO_ERR(var, label) do { \
60 * save the object number in the first 4 bytes of object data. All
61 * other bytes are set to 0.
64 my_obj_init(struct rte_mempool *mp, __rte_unused void *arg,
65 void *obj, unsigned i)
67 uint32_t *objnum = obj;
69 memset(obj, 0, mp->elt_size);
73 /* basic tests (done on one core) */
75 test_mempool_basic(struct rte_mempool *mp, int use_external_cache)
84 struct rte_mempool_cache *cache;
86 if (use_external_cache) {
87 /* Create a user-owned mempool cache. */
88 cache = rte_mempool_cache_create(RTE_MEMPOOL_CACHE_MAX_SIZE,
93 /* May be NULL if cache is disabled. */
94 cache = rte_mempool_default_cache(mp, rte_lcore_id());
97 /* dump the mempool status */
98 rte_mempool_dump(stdout, mp);
100 printf("get an object\n");
101 if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
103 rte_mempool_dump(stdout, mp);
105 /* tests that improve coverage */
106 printf("get object count\n");
107 /* We have to count the extra caches, one in this case. */
108 offset = use_external_cache ? 1 * cache->len : 0;
109 if (rte_mempool_avail_count(mp) + offset != MEMPOOL_SIZE - 1)
112 printf("get private data\n");
113 if (rte_mempool_get_priv(mp) != (char *)mp +
114 MEMPOOL_HEADER_SIZE(mp, mp->cache_size))
117 #ifndef RTE_EXEC_ENV_FREEBSD /* rte_mem_virt2iova() not supported on bsd */
118 printf("get physical address of an object\n");
119 if (rte_mempool_virt2iova(obj) != rte_mem_virt2iova(obj))
123 printf("put the object back\n");
124 rte_mempool_generic_put(mp, &obj, 1, cache);
125 rte_mempool_dump(stdout, mp);
127 printf("get 2 objects\n");
128 if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
130 if (rte_mempool_generic_get(mp, &obj2, 1, cache) < 0) {
131 rte_mempool_generic_put(mp, &obj, 1, cache);
134 rte_mempool_dump(stdout, mp);
136 printf("put the objects back\n");
137 rte_mempool_generic_put(mp, &obj, 1, cache);
138 rte_mempool_generic_put(mp, &obj2, 1, cache);
139 rte_mempool_dump(stdout, mp);
142 * get many objects: we cannot get them all because the cache
143 * on other cores may not be empty.
145 objtable = malloc(MEMPOOL_SIZE * sizeof(void *));
146 if (objtable == NULL)
149 for (i = 0; i < MEMPOOL_SIZE; i++) {
150 if (rte_mempool_generic_get(mp, &objtable[i], 1, cache) < 0)
155 * for each object, check that its content was not modified,
156 * and put objects back in pool
162 if (*objnum > MEMPOOL_SIZE) {
163 printf("bad object number(%d)\n", *objnum);
167 for (j = sizeof(*objnum); j < mp->elt_size; j++) {
168 if (obj_data[j] != 0)
172 rte_mempool_generic_put(mp, &objtable[i], 1, cache);
177 printf("objects were modified!\n");
180 if (use_external_cache) {
181 rte_mempool_cache_flush(cache, mp);
182 rte_mempool_cache_free(cache);
188 static int test_mempool_creation_with_exceeded_cache_size(void)
190 struct rte_mempool *mp_cov;
192 mp_cov = rte_mempool_create("test_mempool_cache_too_big",
195 RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0,
200 if (mp_cov != NULL) {
201 rte_mempool_free(mp_cov);
208 static struct rte_mempool *mp_spsc;
209 static rte_spinlock_t scsp_spinlock;
210 static void *scsp_obj_table[MAX_KEEP];
213 * single producer function
215 static int test_mempool_single_producer(void)
219 uint64_t start_cycles, end_cycles;
220 uint64_t duration = rte_get_timer_hz() / 4;
222 start_cycles = rte_get_timer_cycles();
224 end_cycles = rte_get_timer_cycles();
225 /* duration uses up, stop producing */
226 if (start_cycles + duration < end_cycles)
228 rte_spinlock_lock(&scsp_spinlock);
229 for (i = 0; i < MAX_KEEP; i ++) {
230 if (NULL != scsp_obj_table[i]) {
231 obj = scsp_obj_table[i];
235 rte_spinlock_unlock(&scsp_spinlock);
239 if (rte_mempool_from_obj(obj) != mp_spsc) {
240 printf("obj not owned by this mempool\n");
243 rte_mempool_put(mp_spsc, obj);
244 rte_spinlock_lock(&scsp_spinlock);
245 scsp_obj_table[i] = NULL;
246 rte_spinlock_unlock(&scsp_spinlock);
253 * single consumer function
255 static int test_mempool_single_consumer(void)
259 uint64_t start_cycles, end_cycles;
260 uint64_t duration = rte_get_timer_hz() / 8;
262 start_cycles = rte_get_timer_cycles();
264 end_cycles = rte_get_timer_cycles();
265 /* duration uses up, stop consuming */
266 if (start_cycles + duration < end_cycles)
268 rte_spinlock_lock(&scsp_spinlock);
269 for (i = 0; i < MAX_KEEP; i ++) {
270 if (NULL == scsp_obj_table[i])
273 rte_spinlock_unlock(&scsp_spinlock);
276 if (rte_mempool_get(mp_spsc, &obj) < 0)
278 rte_spinlock_lock(&scsp_spinlock);
279 scsp_obj_table[i] = obj;
280 rte_spinlock_unlock(&scsp_spinlock);
287 * test function for mempool test based on singple consumer and single producer,
288 * can run on one lcore only
291 test_mempool_launch_single_consumer(__rte_unused void *arg)
293 return test_mempool_single_consumer();
297 my_mp_init(struct rte_mempool *mp, __rte_unused void *arg)
299 printf("mempool name is %s\n", mp->name);
300 /* nothing to be implemented here*/
305 * it tests the mempool operations based on singple producer and single consumer
308 test_mempool_sp_sc(void)
311 unsigned lcore_id = rte_lcore_id();
314 /* create a mempool with single producer/consumer ring */
315 if (mp_spsc == NULL) {
316 mp_spsc = rte_mempool_create("test_mempool_sp_sc", MEMPOOL_SIZE,
317 MEMPOOL_ELT_SIZE, 0, 0,
321 MEMPOOL_F_NO_CACHE_ALIGN | MEMPOOL_F_SP_PUT |
326 if (rte_mempool_lookup("test_mempool_sp_sc") != mp_spsc) {
327 printf("Cannot lookup mempool from its name\n");
331 lcore_next = rte_get_next_lcore(lcore_id, 0, 1);
332 if (lcore_next >= RTE_MAX_LCORE) {
336 if (rte_eal_lcore_role(lcore_next) != ROLE_RTE) {
340 rte_spinlock_init(&scsp_spinlock);
341 memset(scsp_obj_table, 0, sizeof(scsp_obj_table));
342 rte_eal_remote_launch(test_mempool_launch_single_consumer, NULL,
344 if (test_mempool_single_producer() < 0)
347 if (rte_eal_wait_lcore(lcore_next) < 0)
351 rte_mempool_free(mp_spsc);
358 * it tests some more basic of mempool
361 test_mempool_basic_ex(struct rte_mempool *mp)
371 obj = rte_calloc("test_mempool_basic_ex", MEMPOOL_SIZE,
374 printf("test_mempool_basic_ex fail to rte_malloc\n");
377 printf("test_mempool_basic_ex now mempool (%s) has %u free entries\n",
378 mp->name, rte_mempool_in_use_count(mp));
379 if (rte_mempool_full(mp) != 1) {
380 printf("test_mempool_basic_ex the mempool should be full\n");
381 goto fail_mp_basic_ex;
384 for (i = 0; i < MEMPOOL_SIZE; i ++) {
385 if (rte_mempool_get(mp, &obj[i]) < 0) {
386 printf("test_mp_basic_ex fail to get object for [%u]\n",
388 goto fail_mp_basic_ex;
391 if (rte_mempool_get(mp, &err_obj) == 0) {
392 printf("test_mempool_basic_ex get an impossible obj\n");
393 goto fail_mp_basic_ex;
395 printf("number: %u\n", i);
396 if (rte_mempool_empty(mp) != 1) {
397 printf("test_mempool_basic_ex the mempool should be empty\n");
398 goto fail_mp_basic_ex;
401 for (i = 0; i < MEMPOOL_SIZE; i++)
402 rte_mempool_put(mp, obj[i]);
404 if (rte_mempool_full(mp) != 1) {
405 printf("test_mempool_basic_ex the mempool should be full\n");
406 goto fail_mp_basic_ex;
413 rte_free((void *)obj);
419 test_mempool_same_name_twice_creation(void)
421 struct rte_mempool *mp_tc, *mp_tc2;
423 mp_tc = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
424 MEMPOOL_ELT_SIZE, 0, 0,
432 mp_tc2 = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
433 MEMPOOL_ELT_SIZE, 0, 0,
438 if (mp_tc2 != NULL) {
439 rte_mempool_free(mp_tc);
440 rte_mempool_free(mp_tc2);
444 rte_mempool_free(mp_tc);
449 walk_cb(struct rte_mempool *mp, void *userdata __rte_unused)
451 printf("\t%s\n", mp->name);
459 test_mp_mem_init(struct rte_mempool *mp,
460 __rte_unused void *opaque,
461 __rte_unused struct rte_mempool_memhdr *memhdr,
462 __rte_unused unsigned int mem_idx)
464 struct mp_data *data = opaque;
470 /* nothing to be implemented here*/
478 uint32_t nb_objs = 0;
479 uint32_t nb_mem_chunks = 0;
480 struct rte_mempool *mp_cache = NULL;
481 struct rte_mempool *mp_nocache = NULL;
482 struct rte_mempool *mp_stack_anon = NULL;
483 struct rte_mempool *mp_stack_mempool_iter = NULL;
484 struct rte_mempool *mp_stack = NULL;
485 struct rte_mempool *default_pool = NULL;
486 struct mp_data cb_arg = {
489 const char *default_pool_ops = rte_mbuf_best_mempool_ops();
491 /* create a mempool (without cache) */
492 mp_nocache = rte_mempool_create("test_nocache", MEMPOOL_SIZE,
493 MEMPOOL_ELT_SIZE, 0, 0,
498 if (mp_nocache == NULL) {
499 printf("cannot allocate mp_nocache mempool\n");
503 /* create a mempool (with cache) */
504 mp_cache = rte_mempool_create("test_cache", MEMPOOL_SIZE,
506 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
511 if (mp_cache == NULL) {
512 printf("cannot allocate mp_cache mempool\n");
516 /* create an empty mempool */
517 mp_stack_anon = rte_mempool_create_empty("test_stack_anon",
520 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
523 if (mp_stack_anon == NULL)
526 /* populate an empty mempool */
527 ret = rte_mempool_populate_anon(mp_stack_anon);
528 printf("%s ret = %d\n", __func__, ret);
532 /* Try to populate when already populated */
533 ret = rte_mempool_populate_anon(mp_stack_anon);
537 /* create a mempool */
538 mp_stack_mempool_iter = rte_mempool_create("test_iter_obj",
541 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
546 if (mp_stack_mempool_iter == NULL)
549 /* test to initialize mempool objects and memory */
550 nb_objs = rte_mempool_obj_iter(mp_stack_mempool_iter, my_obj_init,
555 nb_mem_chunks = rte_mempool_mem_iter(mp_stack_mempool_iter,
556 test_mp_mem_init, &cb_arg);
557 if (nb_mem_chunks == 0 || cb_arg.ret < 0)
560 /* create a mempool with an external handler */
561 mp_stack = rte_mempool_create_empty("test_stack",
564 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
567 if (mp_stack == NULL) {
568 printf("cannot allocate mp_stack mempool\n");
571 if (rte_mempool_set_ops_byname(mp_stack, "stack", NULL) < 0) {
572 printf("cannot set stack handler\n");
575 if (rte_mempool_populate_default(mp_stack) < 0) {
576 printf("cannot populate mp_stack mempool\n");
579 rte_mempool_obj_iter(mp_stack, my_obj_init, NULL);
581 /* Create a mempool based on Default handler */
582 printf("Testing %s mempool handler\n", default_pool_ops);
583 default_pool = rte_mempool_create_empty("default_pool",
586 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
589 if (default_pool == NULL) {
590 printf("cannot allocate default mempool\n");
593 if (rte_mempool_set_ops_byname(default_pool,
594 default_pool_ops, NULL) < 0) {
595 printf("cannot set %s handler\n", default_pool_ops);
598 if (rte_mempool_populate_default(default_pool) < 0) {
599 printf("cannot populate %s mempool\n", default_pool_ops);
602 rte_mempool_obj_iter(default_pool, my_obj_init, NULL);
604 /* retrieve the mempool from its name */
605 if (rte_mempool_lookup("test_nocache") != mp_nocache) {
606 printf("Cannot lookup mempool from its name\n");
610 printf("Walk into mempools:\n");
611 rte_mempool_walk(walk_cb, NULL);
613 rte_mempool_list_dump(stdout);
615 /* basic tests without cache */
616 if (test_mempool_basic(mp_nocache, 0) < 0)
619 /* basic tests with cache */
620 if (test_mempool_basic(mp_cache, 0) < 0)
623 /* basic tests with user-owned cache */
624 if (test_mempool_basic(mp_nocache, 1) < 0)
627 /* more basic tests without cache */
628 if (test_mempool_basic_ex(mp_nocache) < 0)
631 /* mempool operation test based on single producer and single comsumer */
632 if (test_mempool_sp_sc() < 0)
635 if (test_mempool_creation_with_exceeded_cache_size() < 0)
638 if (test_mempool_same_name_twice_creation() < 0)
641 /* test the stack handler */
642 if (test_mempool_basic(mp_stack, 1) < 0)
645 if (test_mempool_basic(default_pool, 1) < 0)
648 rte_mempool_list_dump(stdout);
653 rte_mempool_free(mp_nocache);
654 rte_mempool_free(mp_cache);
655 rte_mempool_free(mp_stack_anon);
656 rte_mempool_free(mp_stack_mempool_iter);
657 rte_mempool_free(mp_stack);
658 rte_mempool_free(default_pool);
663 REGISTER_TEST_COMMAND(mempool_autotest, test_mempool);