4 * Copyright(c) 2010-2012 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.
33 * version: DPDK.L.1.2.3-3
43 #include <sys/queue.h>
45 #include <rte_common.h>
47 #include <rte_debug.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_launch.h>
51 #include <rte_cycles.h>
52 #include <rte_tailq.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_atomic.h>
57 #include <rte_branch_prediction.h>
59 #include <rte_mempool.h>
60 #include <rte_spinlock.h>
61 #include <rte_malloc.h>
63 #include <cmdline_parse.h>
71 * #. Basic tests: done on one core with and without cache:
73 * - Get one object, put one object
74 * - Get two objects, put two objects
75 * - Get all objects, test that their content is not modified and
76 * put them back in the pool.
78 * #. Performance tests:
80 * Each core get *n_keep* objects per bulk of *n_get_bulk*. Then,
81 * objects are put back in the pool per bulk of *n_put_bulk*.
83 * This sequence is done during TIME_S seconds.
85 * This test is done on the following configurations:
87 * - Cores configuration (*cores*)
89 * - One core with cache
90 * - Two cores with cache
91 * - Max. cores with cache
92 * - One core without cache
93 * - Two cores without cache
94 * - Max. cores without cache
96 * - Bulk size (*n_get_bulk*, *n_put_bulk*)
98 * - Bulk get from 1 to 32
99 * - Bulk put from 1 to 32
101 * - Number of kept objects (*n_keep*)
109 #define MEMPOOL_ELT_SIZE 2048
111 #define MEMPOOL_SIZE ((RTE_MAX_LCORE*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
113 static struct rte_mempool *mp;
114 static struct rte_mempool *mp_cache, *mp_nocache;
116 static rte_atomic32_t synchro;
118 /* number of objects in one bulk operation (get or put) */
119 static unsigned n_get_bulk;
120 static unsigned n_put_bulk;
122 /* number of objects retrived from mempool before putting them back */
123 static unsigned n_keep;
125 /* number of enqueues / dequeues */
126 struct mempool_test_stats {
128 } __rte_cache_aligned;
130 static struct mempool_test_stats stats[RTE_MAX_LCORE];
133 per_lcore_mempool_test(__attribute__((unused)) void *arg)
135 void *obj_table[MAX_KEEP];
137 unsigned lcore_id = rte_lcore_id();
139 uint64_t start_cycles, end_cycles;
140 uint64_t time_diff = 0, hz = rte_get_hpet_hz();
142 /* n_get_bulk and n_put_bulk must be divisors of n_keep */
143 if (((n_keep / n_get_bulk) * n_get_bulk) != n_keep)
145 if (((n_keep / n_put_bulk) * n_put_bulk) != n_keep)
148 stats[lcore_id].enq_count = 0;
150 /* wait synchro for slaves */
151 if (lcore_id != rte_get_master_lcore())
152 while (rte_atomic32_read(&synchro) == 0);
154 start_cycles = rte_get_hpet_cycles();
156 while (time_diff/hz < TIME_S) {
157 for (i = 0; likely(i < (N/n_keep)); i++) {
158 /* get n_keep objects by bulk of n_bulk */
160 while (idx < n_keep) {
161 ret = rte_mempool_get_bulk(mp, &obj_table[idx],
163 if (unlikely(ret < 0)) {
164 rte_mempool_dump(mp);
165 rte_ring_dump(mp->ring);
166 /* in this case, objects are lost... */
172 /* put the objects back */
174 while (idx < n_keep) {
175 rte_mempool_put_bulk(mp, &obj_table[idx],
180 end_cycles = rte_get_hpet_cycles();
181 time_diff = end_cycles - start_cycles;
182 stats[lcore_id].enq_count += N;
188 /* launch all the per-lcore test, and display the result */
190 launch_cores(unsigned cores)
195 unsigned cores_save = cores;
197 rte_atomic32_set(&synchro, 0);
200 memset(stats, 0, sizeof(stats));
202 printf("mempool_autotest cache=%u cores=%u n_get_bulk=%u "
203 "n_put_bulk=%u n_keep=%u ",
204 (unsigned) mp->cache_size, cores, n_get_bulk, n_put_bulk, n_keep);
206 if (rte_mempool_count(mp) != MEMPOOL_SIZE) {
207 printf("mempool is not full\n");
211 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
215 rte_eal_remote_launch(per_lcore_mempool_test,
219 /* start synchro and launch test on master */
220 rte_atomic32_set(&synchro, 1);
222 ret = per_lcore_mempool_test(NULL);
225 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
229 if (rte_eal_wait_lcore(lcore_id) < 0)
234 printf("per-lcore test returned -1\n");
239 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
240 rate += (stats[lcore_id].enq_count / TIME_S);
242 printf("rate_persec=%u\n", rate);
247 /* for a given number of core, launch all test cases */
249 do_one_mempool_test(unsigned cores)
251 unsigned bulk_tab_get[] = { 1, 4, 32, 0 };
252 unsigned bulk_tab_put[] = { 1, 4, 32, 0 };
253 unsigned keep_tab[] = { 32, 128, 0 };
254 unsigned *get_bulk_ptr;
255 unsigned *put_bulk_ptr;
259 for (get_bulk_ptr = bulk_tab_get; *get_bulk_ptr; get_bulk_ptr++) {
260 for (put_bulk_ptr = bulk_tab_put; *put_bulk_ptr; put_bulk_ptr++) {
261 for (keep_ptr = keep_tab; *keep_ptr; keep_ptr++) {
263 n_get_bulk = *get_bulk_ptr;
264 n_put_bulk = *put_bulk_ptr;
266 ret = launch_cores(cores);
278 * save the object number in the first 4 bytes of object data. All
279 * other bytes are set to 0.
282 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
283 void *obj, unsigned i)
285 uint32_t *objnum = obj;
286 memset(obj, 0, mp->elt_size);
290 /* basic tests (done on one core) */
292 test_mempool_basic(void)
300 unsigned old_bulk_count;
302 /* dump the mempool status */
303 rte_mempool_dump(mp);
304 old_bulk_count = rte_mempool_get_bulk_count(mp);
305 rte_mempool_dump(mp);
306 if (rte_mempool_set_bulk_count(mp, 0) == 0)
308 if (rte_mempool_get_bulk_count(mp) == 0)
310 if (rte_mempool_set_bulk_count(mp, 2) < 0)
312 if (rte_mempool_get_bulk_count(mp) != 2)
314 rte_mempool_dump(mp);
315 if (rte_mempool_set_bulk_count(mp, old_bulk_count) < 0)
317 if (rte_mempool_get_bulk_count(mp) != old_bulk_count)
319 rte_mempool_dump(mp);
321 printf("get an object\n");
322 if (rte_mempool_get(mp, &obj) < 0)
324 rte_mempool_dump(mp);
326 printf("put the object back\n");
327 rte_mempool_put(mp, obj);
328 rte_mempool_dump(mp);
330 printf("get 2 objects\n");
331 if (rte_mempool_get(mp, &obj) < 0)
333 if (rte_mempool_get(mp, &obj2) < 0) {
334 rte_mempool_put(mp, obj);
337 rte_mempool_dump(mp);
339 printf("put the objects back\n");
340 rte_mempool_put(mp, obj);
341 rte_mempool_put(mp, obj2);
342 rte_mempool_dump(mp);
345 * get many objects: we cannot get them all because the cache
346 * on other cores may not be empty.
348 objtable = malloc(MEMPOOL_SIZE * sizeof(void *));
349 if (objtable == NULL) {
353 for (i=0; i<MEMPOOL_SIZE; i++) {
354 if (rte_mempool_get(mp, &objtable[i]) < 0)
359 * for each object, check that its content was not modified,
360 * and put objects back in pool
366 if (*objnum > MEMPOOL_SIZE) {
367 printf("bad object number\n");
371 for (j=sizeof(*objnum); j<mp->elt_size; j++) {
372 if (obj_data[j] != 0)
376 rte_mempool_put(mp, objtable[i]);
381 printf("objects were modified!\n");
386 static int test_mempool_creation_with_exceeded_cache_size(void)
388 struct rte_mempool *mp_cov;
390 mp_cov = rte_mempool_create("test_mempool_creation_with_exceeded_cache_size", MEMPOOL_SIZE,
392 RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0,
403 static struct rte_mempool *mp_spsc;
404 static rte_spinlock_t scsp_spinlock;
405 static void *scsp_obj_table[MAX_KEEP];
408 * single producer function
410 static int test_mempool_single_producer(void)
414 uint64_t start_cycles, end_cycles;
415 uint64_t duration = rte_get_hpet_hz() * 8;
417 start_cycles = rte_get_hpet_cycles();
419 end_cycles = rte_get_hpet_cycles();
420 /* duration uses up, stop producing */
421 if (start_cycles + duration < end_cycles)
423 rte_spinlock_lock(&scsp_spinlock);
424 for (i = 0; i < MAX_KEEP; i ++) {
425 if (NULL != scsp_obj_table[i])
426 obj = scsp_obj_table[i];
429 rte_spinlock_unlock(&scsp_spinlock);
433 if (rte_mempool_from_obj(obj) != mp_spsc) {
434 printf("test_mempool_single_producer there is an obj not owned by this mempool\n");
437 rte_mempool_sp_put(mp_spsc, obj);
438 rte_spinlock_lock(&scsp_spinlock);
439 scsp_obj_table[i] = NULL;
440 rte_spinlock_unlock(&scsp_spinlock);
447 * single consumer function
449 static int test_mempool_single_consumer(void)
453 uint64_t start_cycles, end_cycles;
454 uint64_t duration = rte_get_hpet_hz() * 5;
456 start_cycles = rte_get_hpet_cycles();
458 end_cycles = rte_get_hpet_cycles();
459 /* duration uses up, stop consuming */
460 if (start_cycles + duration < end_cycles)
462 rte_spinlock_lock(&scsp_spinlock);
463 for (i = 0; i < MAX_KEEP; i ++) {
464 if (NULL == scsp_obj_table[i])
467 rte_spinlock_unlock(&scsp_spinlock);
470 if (rte_mempool_sc_get(mp_spsc, &obj) < 0)
472 rte_spinlock_lock(&scsp_spinlock);
473 scsp_obj_table[i] = obj;
474 rte_spinlock_unlock(&scsp_spinlock);
481 * test function for mempool test based on singple consumer and single producer, can run on one lcore only
483 static int test_mempool_launch_single_consumer(__attribute__((unused)) void *arg)
485 return test_mempool_single_consumer();
488 static void my_mp_init(struct rte_mempool * mp, __attribute__((unused)) void * arg)
490 printf("mempool name is %s\n", mp->name);
491 /* nothing to be implemented here*/
496 * it tests the mempool operations based on singple producer and single consumer
499 test_mempool_sp_sc(void)
502 unsigned lcore_id = rte_lcore_id();
505 /* create a mempool with single producer/consumer ring */
506 if (NULL == mp_spsc) {
507 mp_spsc = rte_mempool_create("test_mempool_sp_sc", MEMPOOL_SIZE,
508 MEMPOOL_ELT_SIZE, 0, 0,
511 SOCKET_ID_ANY, MEMPOOL_F_NO_CACHE_ALIGN | MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET);
512 if (NULL == mp_spsc) {
516 if (rte_mempool_lookup("test_mempool_sp_sc") != mp_spsc) {
517 printf("Cannot lookup mempool from its name\n");
520 lcore_next = rte_get_next_lcore(lcore_id, 0, 1);
521 if (RTE_MAX_LCORE <= lcore_next)
523 if (rte_eal_lcore_role(lcore_next) != ROLE_RTE)
525 rte_spinlock_init(&scsp_spinlock);
526 memset(scsp_obj_table, 0, sizeof(scsp_obj_table));
527 rte_eal_remote_launch(test_mempool_launch_single_consumer, NULL, lcore_next);
528 if(test_mempool_single_producer() < 0)
531 if(rte_eal_wait_lcore(lcore_next) < 0)
538 * it tests some more basic of mempool
541 test_mempool_basic_ex(struct rte_mempool * mp)
551 obj = (void **)rte_zmalloc("test_mempool_basic_ex", (MEMPOOL_SIZE * sizeof(void *)), 0);
553 printf("test_mempool_basic_ex fail to rte_malloc\n");
556 printf("test_mempool_basic_ex now mempool (%s) has %u free entries\n", mp->name, rte_mempool_free_count(mp));
557 if (rte_mempool_full(mp) != 1) {
558 printf("test_mempool_basic_ex the mempool is not full but it should be\n");
559 goto fail_mp_basic_ex;
562 for (i = 0; i < MEMPOOL_SIZE; i ++) {
563 if (rte_mempool_mc_get(mp, &obj[i]) < 0) {
564 printf("fail_mp_basic_ex fail to get mempool object for [%u]\n", i);
565 goto fail_mp_basic_ex;
568 if (rte_mempool_mc_get(mp, &err_obj) == 0) {
569 printf("test_mempool_basic_ex get an impossible obj from mempool\n");
570 goto fail_mp_basic_ex;
572 printf("number: %u\n", i);
573 if (rte_mempool_empty(mp) != 1) {
574 printf("test_mempool_basic_ex the mempool is not empty but it should be\n");
575 goto fail_mp_basic_ex;
578 for (i = 0; i < MEMPOOL_SIZE; i ++) {
579 rte_mempool_mp_put(mp, obj[i]);
581 if (rte_mempool_full(mp) != 1) {
582 printf("test_mempool_basic_ex the mempool is not full but it should be\n");
583 goto fail_mp_basic_ex;
590 rte_free((void *)obj);
596 test_mempool_same_name_twice_creation(void)
598 struct rte_mempool *mp_tc;
600 mp_tc = rte_mempool_create("test_mempool_same_name_twice_creation", MEMPOOL_SIZE,
601 MEMPOOL_ELT_SIZE, 0, 0,
608 mp_tc = rte_mempool_create("test_mempool_same_name_twice_creation", MEMPOOL_SIZE,
609 MEMPOOL_ELT_SIZE, 0, 0,
622 rte_atomic32_init(&synchro);
624 /* create a mempool (without cache) */
625 if (mp_nocache == NULL)
626 mp_nocache = rte_mempool_create("test_nocache", MEMPOOL_SIZE,
627 MEMPOOL_ELT_SIZE, 0, 0,
631 if (mp_nocache == NULL)
634 /* create a mempool (with cache) */
635 if (mp_cache == NULL)
636 mp_cache = rte_mempool_create("test_cache", MEMPOOL_SIZE,
638 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
642 if (mp_cache == NULL)
646 /* retrieve the mempool from its name */
647 if (rte_mempool_lookup("test_nocache") != mp_nocache) {
648 printf("Cannot lookup mempool from its name\n");
652 rte_mempool_list_dump();
654 /* basic tests without cache */
656 if (test_mempool_basic() < 0)
659 /* basic tests with cache */
661 if (test_mempool_basic() < 0)
664 /* more basic tests without cache */
665 if (test_mempool_basic_ex(mp_nocache) < 0)
668 /* performance test with 1, 2 and max cores */
669 printf("start performance test (without cache)\n");
672 if (do_one_mempool_test(1) < 0)
675 if (do_one_mempool_test(2) < 0)
678 if (do_one_mempool_test(rte_lcore_count()) < 0)
681 /* performance test with 1, 2 and max cores */
682 printf("start performance test (with cache)\n");
685 if (do_one_mempool_test(1) < 0)
688 if (do_one_mempool_test(2) < 0)
691 if (do_one_mempool_test(rte_lcore_count()) < 0)
694 /* mempool operation test based on single producer and single comsumer */
695 if (test_mempool_sp_sc() < 0)
698 if (test_mempool_creation_with_exceeded_cache_size() < 0)
701 if (test_mempool_same_name_twice_creation() < 0)
704 rte_mempool_list_dump();