4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
41 #include <sys/queue.h>
43 #include <rte_common.h>
45 #include <rte_debug.h>
46 #include <rte_memory.h>
47 #include <rte_launch.h>
48 #include <rte_cycles.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>
64 * Basic tests: done on one core with and without cache:
66 * - Get one object, put one object
67 * - Get two objects, put two objects
68 * - Get all objects, test that their content is not modified and
69 * put them back in the pool.
72 #define MEMPOOL_ELT_SIZE 2048
74 #define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
76 #define LOG_ERR() printf("test failed at %s():%d\n", __func__, __LINE__)
77 #define RET_ERR() do { \
81 #define GOTO_ERR(var, label) do { \
87 static rte_atomic32_t synchro;
90 * save the object number in the first 4 bytes of object data. All
91 * other bytes are set to 0.
94 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
95 void *obj, unsigned i)
97 uint32_t *objnum = obj;
99 memset(obj, 0, mp->elt_size);
103 /* basic tests (done on one core) */
105 test_mempool_basic(struct rte_mempool *mp, int use_external_cache)
114 struct rte_mempool_cache *cache;
116 if (use_external_cache) {
117 /* Create a user-owned mempool cache. */
118 cache = rte_mempool_cache_create(RTE_MEMPOOL_CACHE_MAX_SIZE,
123 /* May be NULL if cache is disabled. */
124 cache = rte_mempool_default_cache(mp, rte_lcore_id());
127 /* dump the mempool status */
128 rte_mempool_dump(stdout, mp);
130 printf("get an object\n");
131 if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
133 rte_mempool_dump(stdout, mp);
135 /* tests that improve coverage */
136 printf("get object count\n");
137 /* We have to count the extra caches, one in this case. */
138 offset = use_external_cache ? 1 * cache->len : 0;
139 if (rte_mempool_avail_count(mp) + offset != MEMPOOL_SIZE - 1)
142 printf("get private data\n");
143 if (rte_mempool_get_priv(mp) != (char *)mp +
144 MEMPOOL_HEADER_SIZE(mp, mp->cache_size))
147 #ifndef RTE_EXEC_ENV_BSDAPP /* rte_mem_virt2iova() not supported on bsd */
148 printf("get physical address of an object\n");
149 if (rte_mempool_virt2iova(obj) != rte_mem_virt2iova(obj))
153 printf("put the object back\n");
154 rte_mempool_generic_put(mp, &obj, 1, cache);
155 rte_mempool_dump(stdout, mp);
157 printf("get 2 objects\n");
158 if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
160 if (rte_mempool_generic_get(mp, &obj2, 1, cache) < 0) {
161 rte_mempool_generic_put(mp, &obj, 1, cache);
164 rte_mempool_dump(stdout, mp);
166 printf("put the objects back\n");
167 rte_mempool_generic_put(mp, &obj, 1, cache);
168 rte_mempool_generic_put(mp, &obj2, 1, cache);
169 rte_mempool_dump(stdout, mp);
172 * get many objects: we cannot get them all because the cache
173 * on other cores may not be empty.
175 objtable = malloc(MEMPOOL_SIZE * sizeof(void *));
176 if (objtable == NULL)
179 for (i = 0; i < MEMPOOL_SIZE; i++) {
180 if (rte_mempool_generic_get(mp, &objtable[i], 1, cache) < 0)
185 * for each object, check that its content was not modified,
186 * and put objects back in pool
192 if (*objnum > MEMPOOL_SIZE) {
193 printf("bad object number(%d)\n", *objnum);
197 for (j = sizeof(*objnum); j < mp->elt_size; j++) {
198 if (obj_data[j] != 0)
202 rte_mempool_generic_put(mp, &objtable[i], 1, cache);
207 printf("objects were modified!\n");
210 if (use_external_cache) {
211 rte_mempool_cache_flush(cache, mp);
212 rte_mempool_cache_free(cache);
218 static int test_mempool_creation_with_exceeded_cache_size(void)
220 struct rte_mempool *mp_cov;
222 mp_cov = rte_mempool_create("test_mempool_cache_too_big",
225 RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0,
230 if (mp_cov != NULL) {
231 rte_mempool_free(mp_cov);
238 static struct rte_mempool *mp_spsc;
239 static rte_spinlock_t scsp_spinlock;
240 static void *scsp_obj_table[MAX_KEEP];
243 * single producer function
245 static int test_mempool_single_producer(void)
249 uint64_t start_cycles, end_cycles;
250 uint64_t duration = rte_get_timer_hz() / 4;
252 start_cycles = rte_get_timer_cycles();
254 end_cycles = rte_get_timer_cycles();
255 /* duration uses up, stop producing */
256 if (start_cycles + duration < end_cycles)
258 rte_spinlock_lock(&scsp_spinlock);
259 for (i = 0; i < MAX_KEEP; i ++) {
260 if (NULL != scsp_obj_table[i]) {
261 obj = scsp_obj_table[i];
265 rte_spinlock_unlock(&scsp_spinlock);
269 if (rte_mempool_from_obj(obj) != mp_spsc) {
270 printf("obj not owned by this mempool\n");
273 rte_mempool_put(mp_spsc, obj);
274 rte_spinlock_lock(&scsp_spinlock);
275 scsp_obj_table[i] = NULL;
276 rte_spinlock_unlock(&scsp_spinlock);
283 * single consumer function
285 static int test_mempool_single_consumer(void)
289 uint64_t start_cycles, end_cycles;
290 uint64_t duration = rte_get_timer_hz() / 8;
292 start_cycles = rte_get_timer_cycles();
294 end_cycles = rte_get_timer_cycles();
295 /* duration uses up, stop consuming */
296 if (start_cycles + duration < end_cycles)
298 rte_spinlock_lock(&scsp_spinlock);
299 for (i = 0; i < MAX_KEEP; i ++) {
300 if (NULL == scsp_obj_table[i])
303 rte_spinlock_unlock(&scsp_spinlock);
306 if (rte_mempool_get(mp_spsc, &obj) < 0)
308 rte_spinlock_lock(&scsp_spinlock);
309 scsp_obj_table[i] = obj;
310 rte_spinlock_unlock(&scsp_spinlock);
317 * test function for mempool test based on singple consumer and single producer,
318 * can run on one lcore only
321 test_mempool_launch_single_consumer(__attribute__((unused)) void *arg)
323 return test_mempool_single_consumer();
327 my_mp_init(struct rte_mempool *mp, __attribute__((unused)) void *arg)
329 printf("mempool name is %s\n", mp->name);
330 /* nothing to be implemented here*/
335 * it tests the mempool operations based on singple producer and single consumer
338 test_mempool_sp_sc(void)
341 unsigned lcore_id = rte_lcore_id();
344 /* create a mempool with single producer/consumer ring */
345 if (mp_spsc == NULL) {
346 mp_spsc = rte_mempool_create("test_mempool_sp_sc", MEMPOOL_SIZE,
347 MEMPOOL_ELT_SIZE, 0, 0,
351 MEMPOOL_F_NO_CACHE_ALIGN | MEMPOOL_F_SP_PUT |
356 if (rte_mempool_lookup("test_mempool_sp_sc") != mp_spsc) {
357 printf("Cannot lookup mempool from its name\n");
358 rte_mempool_free(mp_spsc);
361 lcore_next = rte_get_next_lcore(lcore_id, 0, 1);
362 if (lcore_next >= RTE_MAX_LCORE) {
363 rte_mempool_free(mp_spsc);
366 if (rte_eal_lcore_role(lcore_next) != ROLE_RTE) {
367 rte_mempool_free(mp_spsc);
370 rte_spinlock_init(&scsp_spinlock);
371 memset(scsp_obj_table, 0, sizeof(scsp_obj_table));
372 rte_eal_remote_launch(test_mempool_launch_single_consumer, NULL,
374 if (test_mempool_single_producer() < 0)
377 if (rte_eal_wait_lcore(lcore_next) < 0)
379 rte_mempool_free(mp_spsc);
385 * it tests some more basic of mempool
388 test_mempool_basic_ex(struct rte_mempool *mp)
398 obj = rte_calloc("test_mempool_basic_ex", MEMPOOL_SIZE,
401 printf("test_mempool_basic_ex fail to rte_malloc\n");
404 printf("test_mempool_basic_ex now mempool (%s) has %u free entries\n",
405 mp->name, rte_mempool_in_use_count(mp));
406 if (rte_mempool_full(mp) != 1) {
407 printf("test_mempool_basic_ex the mempool should be full\n");
408 goto fail_mp_basic_ex;
411 for (i = 0; i < MEMPOOL_SIZE; i ++) {
412 if (rte_mempool_get(mp, &obj[i]) < 0) {
413 printf("test_mp_basic_ex fail to get object for [%u]\n",
415 goto fail_mp_basic_ex;
418 if (rte_mempool_get(mp, &err_obj) == 0) {
419 printf("test_mempool_basic_ex get an impossible obj\n");
420 goto fail_mp_basic_ex;
422 printf("number: %u\n", i);
423 if (rte_mempool_empty(mp) != 1) {
424 printf("test_mempool_basic_ex the mempool should be empty\n");
425 goto fail_mp_basic_ex;
428 for (i = 0; i < MEMPOOL_SIZE; i++)
429 rte_mempool_put(mp, obj[i]);
431 if (rte_mempool_full(mp) != 1) {
432 printf("test_mempool_basic_ex the mempool should be full\n");
433 goto fail_mp_basic_ex;
440 rte_free((void *)obj);
446 test_mempool_same_name_twice_creation(void)
448 struct rte_mempool *mp_tc, *mp_tc2;
450 mp_tc = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
451 MEMPOOL_ELT_SIZE, 0, 0,
459 mp_tc2 = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
460 MEMPOOL_ELT_SIZE, 0, 0,
465 if (mp_tc2 != NULL) {
466 rte_mempool_free(mp_tc);
467 rte_mempool_free(mp_tc2);
471 rte_mempool_free(mp_tc);
476 * Basic test for mempool_xmem functions.
479 test_mempool_xmem_misc(void)
481 uint32_t elt_num, total_size;
486 total_size = rte_mempool_calc_obj_size(MEMPOOL_ELT_SIZE, 0, NULL);
487 sz = rte_mempool_xmem_size(elt_num, total_size, MEMPOOL_PG_SHIFT_MAX,
490 usz = rte_mempool_xmem_usage(NULL, elt_num, total_size, 0, 1,
491 MEMPOOL_PG_SHIFT_MAX, 0);
493 if (sz != (size_t)usz) {
494 printf("failure @ %s: rte_mempool_xmem_usage(%u, %u) "
495 "returns: %#zx, while expected: %#zx;\n",
496 __func__, elt_num, total_size, sz, (size_t)usz);
504 walk_cb(struct rte_mempool *mp, void *userdata __rte_unused)
506 printf("\t%s\n", mp->name);
513 struct rte_mempool *mp_cache = NULL;
514 struct rte_mempool *mp_nocache = NULL;
515 struct rte_mempool *mp_stack = NULL;
516 struct rte_mempool *default_pool = NULL;
518 rte_atomic32_init(&synchro);
520 /* create a mempool (without cache) */
521 mp_nocache = rte_mempool_create("test_nocache", MEMPOOL_SIZE,
522 MEMPOOL_ELT_SIZE, 0, 0,
527 if (mp_nocache == NULL) {
528 printf("cannot allocate mp_nocache mempool\n");
532 /* create a mempool (with cache) */
533 mp_cache = rte_mempool_create("test_cache", MEMPOOL_SIZE,
535 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
540 if (mp_cache == NULL) {
541 printf("cannot allocate mp_cache mempool\n");
545 /* create a mempool with an external handler */
546 mp_stack = rte_mempool_create_empty("test_stack",
549 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
552 if (mp_stack == NULL) {
553 printf("cannot allocate mp_stack mempool\n");
556 if (rte_mempool_set_ops_byname(mp_stack, "stack", NULL) < 0) {
557 printf("cannot set stack handler\n");
560 if (rte_mempool_populate_default(mp_stack) < 0) {
561 printf("cannot populate mp_stack mempool\n");
564 rte_mempool_obj_iter(mp_stack, my_obj_init, NULL);
566 /* Create a mempool based on Default handler */
567 printf("Testing %s mempool handler\n",
568 RTE_MBUF_DEFAULT_MEMPOOL_OPS);
569 default_pool = rte_mempool_create_empty("default_pool",
572 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
575 if (default_pool == NULL) {
576 printf("cannot allocate default mempool\n");
579 if (rte_mempool_set_ops_byname(default_pool,
580 RTE_MBUF_DEFAULT_MEMPOOL_OPS, NULL) < 0) {
581 printf("cannot set %s handler\n",
582 RTE_MBUF_DEFAULT_MEMPOOL_OPS);
585 if (rte_mempool_populate_default(default_pool) < 0) {
586 printf("cannot populate %s mempool\n",
587 RTE_MBUF_DEFAULT_MEMPOOL_OPS);
590 rte_mempool_obj_iter(default_pool, my_obj_init, NULL);
592 /* retrieve the mempool from its name */
593 if (rte_mempool_lookup("test_nocache") != mp_nocache) {
594 printf("Cannot lookup mempool from its name\n");
598 printf("Walk into mempools:\n");
599 rte_mempool_walk(walk_cb, NULL);
601 rte_mempool_list_dump(stdout);
603 /* basic tests without cache */
604 if (test_mempool_basic(mp_nocache, 0) < 0)
607 /* basic tests with cache */
608 if (test_mempool_basic(mp_cache, 0) < 0)
611 /* basic tests with user-owned cache */
612 if (test_mempool_basic(mp_nocache, 1) < 0)
615 /* more basic tests without cache */
616 if (test_mempool_basic_ex(mp_nocache) < 0)
619 /* mempool operation test based on single producer and single comsumer */
620 if (test_mempool_sp_sc() < 0)
623 if (test_mempool_creation_with_exceeded_cache_size() < 0)
626 if (test_mempool_same_name_twice_creation() < 0)
629 if (test_mempool_xmem_misc() < 0)
632 /* test the stack handler */
633 if (test_mempool_basic(mp_stack, 1) < 0)
636 if (test_mempool_basic(default_pool, 1) < 0)
639 rte_mempool_list_dump(stdout);
644 rte_mempool_free(mp_nocache);
645 rte_mempool_free(mp_cache);
646 rte_mempool_free(mp_stack);
647 rte_mempool_free(default_pool);
652 REGISTER_TEST_COMMAND(mempool_autotest, test_mempool);