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_memzone.h>
48 #include <rte_launch.h>
49 #include <rte_cycles.h>
51 #include <rte_per_lcore.h>
52 #include <rte_lcore.h>
53 #include <rte_atomic.h>
54 #include <rte_branch_prediction.h>
56 #include <rte_mempool.h>
57 #include <rte_spinlock.h>
58 #include <rte_malloc.h>
66 * Basic tests: done on one core with and without cache:
68 * - Get one object, put one object
69 * - Get two objects, put two objects
70 * - Get all objects, test that their content is not modified and
71 * put them back in the pool.
74 #define MEMPOOL_ELT_SIZE 2048
76 #define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
78 #define RET_ERR() do { \
79 printf("test failed at %s():%d\n", __func__, __LINE__); \
83 static rte_atomic32_t synchro;
86 * Simple example of custom mempool structure. Holds pointers to all the
87 * elements which are simply malloc'd in this example.
89 struct custom_mempool {
97 * Loop through all the element pointers and allocate a chunk of memory, then
98 * insert that memory into the ring.
101 custom_mempool_alloc(struct rte_mempool *mp)
103 struct custom_mempool *cm;
105 cm = rte_zmalloc("custom_mempool",
106 sizeof(struct custom_mempool) + mp->size * sizeof(void *), 0);
110 rte_spinlock_init(&cm->lock);
118 custom_mempool_free(struct rte_mempool *mp)
120 rte_free((void *)(mp->pool_data));
124 custom_mempool_enqueue(struct rte_mempool *mp, void * const *obj_table,
127 struct custom_mempool *cm = (struct custom_mempool *)(mp->pool_data);
130 rte_spinlock_lock(&cm->lock);
131 if (cm->count + n > cm->size) {
134 memcpy(&cm->elts[cm->count], obj_table, sizeof(void *) * n);
137 rte_spinlock_unlock(&cm->lock);
143 custom_mempool_dequeue(struct rte_mempool *mp, void **obj_table, unsigned n)
145 struct custom_mempool *cm = (struct custom_mempool *)(mp->pool_data);
148 rte_spinlock_lock(&cm->lock);
153 memcpy(obj_table, &cm->elts[cm->count], sizeof(void *) * n);
155 rte_spinlock_unlock(&cm->lock);
160 custom_mempool_get_count(const struct rte_mempool *mp)
162 struct custom_mempool *cm = (struct custom_mempool *)(mp->pool_data);
167 static struct rte_mempool_ops mempool_ops_custom = {
168 .name = "custom_handler",
169 .alloc = custom_mempool_alloc,
170 .free = custom_mempool_free,
171 .enqueue = custom_mempool_enqueue,
172 .dequeue = custom_mempool_dequeue,
173 .get_count = custom_mempool_get_count,
176 MEMPOOL_REGISTER_OPS(mempool_ops_custom);
179 * save the object number in the first 4 bytes of object data. All
180 * other bytes are set to 0.
183 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
184 void *obj, unsigned i)
186 uint32_t *objnum = obj;
188 memset(obj, 0, mp->elt_size);
192 /* basic tests (done on one core) */
194 test_mempool_basic(struct rte_mempool *mp)
203 /* dump the mempool status */
204 rte_mempool_dump(stdout, mp);
206 printf("get an object\n");
207 if (rte_mempool_get(mp, &obj) < 0)
209 rte_mempool_dump(stdout, mp);
211 /* tests that improve coverage */
212 printf("get object count\n");
213 if (rte_mempool_count(mp) != MEMPOOL_SIZE - 1)
216 printf("get private data\n");
217 if (rte_mempool_get_priv(mp) != (char *)mp +
218 MEMPOOL_HEADER_SIZE(mp, mp->cache_size))
221 #ifndef RTE_EXEC_ENV_BSDAPP /* rte_mem_virt2phy() not supported on bsd */
222 printf("get physical address of an object\n");
223 if (rte_mempool_virt2phy(mp, obj) != rte_mem_virt2phy(obj))
227 printf("put the object back\n");
228 rte_mempool_put(mp, obj);
229 rte_mempool_dump(stdout, mp);
231 printf("get 2 objects\n");
232 if (rte_mempool_get(mp, &obj) < 0)
234 if (rte_mempool_get(mp, &obj2) < 0) {
235 rte_mempool_put(mp, obj);
238 rte_mempool_dump(stdout, mp);
240 printf("put the objects back\n");
241 rte_mempool_put(mp, obj);
242 rte_mempool_put(mp, obj2);
243 rte_mempool_dump(stdout, mp);
246 * get many objects: we cannot get them all because the cache
247 * on other cores may not be empty.
249 objtable = malloc(MEMPOOL_SIZE * sizeof(void *));
250 if (objtable == NULL)
253 for (i = 0; i < MEMPOOL_SIZE; i++) {
254 if (rte_mempool_get(mp, &objtable[i]) < 0)
259 * for each object, check that its content was not modified,
260 * and put objects back in pool
266 if (*objnum > MEMPOOL_SIZE) {
267 printf("bad object number(%d)\n", *objnum);
271 for (j = sizeof(*objnum); j < mp->elt_size; j++) {
272 if (obj_data[j] != 0)
276 rte_mempool_put(mp, objtable[i]);
281 printf("objects were modified!\n");
286 static int test_mempool_creation_with_exceeded_cache_size(void)
288 struct rte_mempool *mp_cov;
290 mp_cov = rte_mempool_create("test_mempool_cache_too_big",
293 RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0,
298 if (mp_cov != NULL) {
299 rte_mempool_free(mp_cov);
306 static struct rte_mempool *mp_spsc;
307 static rte_spinlock_t scsp_spinlock;
308 static void *scsp_obj_table[MAX_KEEP];
311 * single producer function
313 static int test_mempool_single_producer(void)
317 uint64_t start_cycles, end_cycles;
318 uint64_t duration = rte_get_timer_hz() / 4;
320 start_cycles = rte_get_timer_cycles();
322 end_cycles = rte_get_timer_cycles();
323 /* duration uses up, stop producing */
324 if (start_cycles + duration < end_cycles)
326 rte_spinlock_lock(&scsp_spinlock);
327 for (i = 0; i < MAX_KEEP; i ++) {
328 if (NULL != scsp_obj_table[i]) {
329 obj = scsp_obj_table[i];
333 rte_spinlock_unlock(&scsp_spinlock);
337 if (rte_mempool_from_obj(obj) != mp_spsc) {
338 printf("obj not owned by this mempool\n");
341 rte_mempool_sp_put(mp_spsc, obj);
342 rte_spinlock_lock(&scsp_spinlock);
343 scsp_obj_table[i] = NULL;
344 rte_spinlock_unlock(&scsp_spinlock);
351 * single consumer function
353 static int test_mempool_single_consumer(void)
357 uint64_t start_cycles, end_cycles;
358 uint64_t duration = rte_get_timer_hz() / 8;
360 start_cycles = rte_get_timer_cycles();
362 end_cycles = rte_get_timer_cycles();
363 /* duration uses up, stop consuming */
364 if (start_cycles + duration < end_cycles)
366 rte_spinlock_lock(&scsp_spinlock);
367 for (i = 0; i < MAX_KEEP; i ++) {
368 if (NULL == scsp_obj_table[i])
371 rte_spinlock_unlock(&scsp_spinlock);
374 if (rte_mempool_sc_get(mp_spsc, &obj) < 0)
376 rte_spinlock_lock(&scsp_spinlock);
377 scsp_obj_table[i] = obj;
378 rte_spinlock_unlock(&scsp_spinlock);
385 * test function for mempool test based on singple consumer and single producer,
386 * can run on one lcore only
389 test_mempool_launch_single_consumer(__attribute__((unused)) void *arg)
391 return test_mempool_single_consumer();
395 my_mp_init(struct rte_mempool *mp, __attribute__((unused)) void *arg)
397 printf("mempool name is %s\n", mp->name);
398 /* nothing to be implemented here*/
403 * it tests the mempool operations based on singple producer and single consumer
406 test_mempool_sp_sc(void)
409 unsigned lcore_id = rte_lcore_id();
412 /* create a mempool with single producer/consumer ring */
413 if (mp_spsc == NULL) {
414 mp_spsc = rte_mempool_create("test_mempool_sp_sc", MEMPOOL_SIZE,
415 MEMPOOL_ELT_SIZE, 0, 0,
419 MEMPOOL_F_NO_CACHE_ALIGN | MEMPOOL_F_SP_PUT |
424 if (rte_mempool_lookup("test_mempool_sp_sc") != mp_spsc) {
425 printf("Cannot lookup mempool from its name\n");
426 rte_mempool_free(mp_spsc);
429 lcore_next = rte_get_next_lcore(lcore_id, 0, 1);
430 if (lcore_next >= RTE_MAX_LCORE) {
431 rte_mempool_free(mp_spsc);
434 if (rte_eal_lcore_role(lcore_next) != ROLE_RTE) {
435 rte_mempool_free(mp_spsc);
438 rte_spinlock_init(&scsp_spinlock);
439 memset(scsp_obj_table, 0, sizeof(scsp_obj_table));
440 rte_eal_remote_launch(test_mempool_launch_single_consumer, NULL,
442 if (test_mempool_single_producer() < 0)
445 if (rte_eal_wait_lcore(lcore_next) < 0)
447 rte_mempool_free(mp_spsc);
453 * it tests some more basic of mempool
456 test_mempool_basic_ex(struct rte_mempool *mp)
466 obj = rte_calloc("test_mempool_basic_ex", MEMPOOL_SIZE,
469 printf("test_mempool_basic_ex fail to rte_malloc\n");
472 printf("test_mempool_basic_ex now mempool (%s) has %u free entries\n",
473 mp->name, rte_mempool_free_count(mp));
474 if (rte_mempool_full(mp) != 1) {
475 printf("test_mempool_basic_ex the mempool should be full\n");
476 goto fail_mp_basic_ex;
479 for (i = 0; i < MEMPOOL_SIZE; i ++) {
480 if (rte_mempool_mc_get(mp, &obj[i]) < 0) {
481 printf("test_mp_basic_ex fail to get object for [%u]\n",
483 goto fail_mp_basic_ex;
486 if (rte_mempool_mc_get(mp, &err_obj) == 0) {
487 printf("test_mempool_basic_ex get an impossible obj\n");
488 goto fail_mp_basic_ex;
490 printf("number: %u\n", i);
491 if (rte_mempool_empty(mp) != 1) {
492 printf("test_mempool_basic_ex the mempool should be empty\n");
493 goto fail_mp_basic_ex;
496 for (i = 0; i < MEMPOOL_SIZE; i++)
497 rte_mempool_mp_put(mp, obj[i]);
499 if (rte_mempool_full(mp) != 1) {
500 printf("test_mempool_basic_ex the mempool should be full\n");
501 goto fail_mp_basic_ex;
508 rte_free((void *)obj);
514 test_mempool_same_name_twice_creation(void)
516 struct rte_mempool *mp_tc, *mp_tc2;
518 mp_tc = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
519 MEMPOOL_ELT_SIZE, 0, 0,
527 mp_tc2 = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
528 MEMPOOL_ELT_SIZE, 0, 0,
533 if (mp_tc2 != NULL) {
534 rte_mempool_free(mp_tc);
535 rte_mempool_free(mp_tc2);
539 rte_mempool_free(mp_tc);
544 * BAsic test for mempool_xmem functions.
547 test_mempool_xmem_misc(void)
549 uint32_t elt_num, total_size;
554 total_size = rte_mempool_calc_obj_size(MEMPOOL_ELT_SIZE, 0, NULL);
555 sz = rte_mempool_xmem_size(elt_num, total_size, MEMPOOL_PG_SHIFT_MAX);
557 usz = rte_mempool_xmem_usage(NULL, elt_num, total_size, 0, 1,
558 MEMPOOL_PG_SHIFT_MAX);
560 if (sz != (size_t)usz) {
561 printf("failure @ %s: rte_mempool_xmem_usage(%u, %u) "
562 "returns: %#zx, while expected: %#zx;\n",
563 __func__, elt_num, total_size, sz, (size_t)usz);
573 struct rte_mempool *mp_cache = NULL;
574 struct rte_mempool *mp_nocache = NULL;
575 struct rte_mempool *mp_ext = NULL;
577 rte_atomic32_init(&synchro);
579 /* create a mempool (without cache) */
580 mp_nocache = rte_mempool_create("test_nocache", MEMPOOL_SIZE,
581 MEMPOOL_ELT_SIZE, 0, 0,
586 if (mp_nocache == NULL) {
587 printf("cannot allocate mp_nocache mempool\n");
591 /* create a mempool (with cache) */
592 mp_cache = rte_mempool_create("test_cache", MEMPOOL_SIZE,
594 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
599 if (mp_cache == NULL) {
600 printf("cannot allocate mp_cache mempool\n");
604 /* create a mempool with an external handler */
605 mp_ext = rte_mempool_create_empty("test_ext",
608 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
611 if (mp_ext == NULL) {
612 printf("cannot allocate mp_ext mempool\n");
615 if (rte_mempool_set_ops_byname(mp_ext, "custom_handler", NULL) < 0) {
616 printf("cannot set custom handler\n");
619 if (rte_mempool_populate_default(mp_ext) < 0) {
620 printf("cannot populate mp_ext mempool\n");
623 rte_mempool_obj_iter(mp_ext, my_obj_init, NULL);
625 /* retrieve the mempool from its name */
626 if (rte_mempool_lookup("test_nocache") != mp_nocache) {
627 printf("Cannot lookup mempool from its name\n");
631 rte_mempool_list_dump(stdout);
633 /* basic tests without cache */
634 if (test_mempool_basic(mp_nocache) < 0)
637 /* basic tests with cache */
638 if (test_mempool_basic(mp_cache) < 0)
641 /* more basic tests without cache */
642 if (test_mempool_basic_ex(mp_nocache) < 0)
645 /* mempool operation test based on single producer and single comsumer */
646 if (test_mempool_sp_sc() < 0)
649 if (test_mempool_creation_with_exceeded_cache_size() < 0)
652 if (test_mempool_same_name_twice_creation() < 0)
655 if (test_mempool_xmem_misc() < 0)
658 rte_mempool_list_dump(stdout);
663 rte_mempool_free(mp_nocache);
664 rte_mempool_free(mp_cache);
665 rte_mempool_free(mp_ext);
669 static struct test_command mempool_cmd = {
670 .command = "mempool_autotest",
671 .callback = test_mempool,
673 REGISTER_TEST_COMMAND(mempool_cmd);