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>
55 #include <rte_mempool.h>
56 #include <rte_spinlock.h>
57 #include <rte_malloc.h>
65 * Each core get *n_keep* objects per bulk of *n_get_bulk*. Then,
66 * objects are put back in the pool per bulk of *n_put_bulk*.
68 * This sequence is done during TIME_S seconds.
70 * This test is done on the following configurations:
72 * - Cores configuration (*cores*)
74 * - One core with cache
75 * - Two cores with cache
76 * - Max. cores with cache
77 * - One core without cache
78 * - Two cores without cache
79 * - Max. cores without cache
80 * - One core with user-owned cache
81 * - Two cores with user-owned cache
82 * - Max. cores with user-owned cache
84 * - Bulk size (*n_get_bulk*, *n_put_bulk*)
86 * - Bulk get from 1 to 32
87 * - Bulk put from 1 to 32
89 * - Number of kept objects (*n_keep*)
97 #define MEMPOOL_ELT_SIZE 2048
99 #define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
101 #define LOG_ERR() printf("test failed at %s():%d\n", __func__, __LINE__)
102 #define RET_ERR() do { \
106 #define GOTO_ERR(var, label) do { \
112 static int use_external_cache;
113 static unsigned external_cache_size = RTE_MEMPOOL_CACHE_MAX_SIZE;
115 static rte_atomic32_t synchro;
117 /* number of objects in one bulk operation (get or put) */
118 static unsigned n_get_bulk;
119 static unsigned n_put_bulk;
121 /* number of objects retrived from mempool before putting them back */
122 static unsigned n_keep;
124 /* number of enqueues / dequeues */
125 struct mempool_test_stats {
127 } __rte_cache_aligned;
129 static struct mempool_test_stats stats[RTE_MAX_LCORE];
132 * save the object number in the first 4 bytes of object data. All
133 * other bytes are set to 0.
136 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
137 void *obj, unsigned i)
139 uint32_t *objnum = obj;
140 memset(obj, 0, mp->elt_size);
145 per_lcore_mempool_test(void *arg)
147 void *obj_table[MAX_KEEP];
149 struct rte_mempool *mp = arg;
150 unsigned lcore_id = rte_lcore_id();
152 uint64_t start_cycles, end_cycles;
153 uint64_t time_diff = 0, hz = rte_get_timer_hz();
154 struct rte_mempool_cache *cache;
156 if (use_external_cache) {
157 /* Create a user-owned mempool cache. */
158 cache = rte_mempool_cache_create(external_cache_size,
163 /* May be NULL if cache is disabled. */
164 cache = rte_mempool_default_cache(mp, lcore_id);
167 /* n_get_bulk and n_put_bulk must be divisors of n_keep */
168 if (((n_keep / n_get_bulk) * n_get_bulk) != n_keep)
170 if (((n_keep / n_put_bulk) * n_put_bulk) != n_keep)
173 stats[lcore_id].enq_count = 0;
175 /* wait synchro for slaves */
176 if (lcore_id != rte_get_master_lcore())
177 while (rte_atomic32_read(&synchro) == 0);
179 start_cycles = rte_get_timer_cycles();
181 while (time_diff/hz < TIME_S) {
182 for (i = 0; likely(i < (N/n_keep)); i++) {
183 /* get n_keep objects by bulk of n_bulk */
185 while (idx < n_keep) {
186 ret = rte_mempool_generic_get(mp,
190 if (unlikely(ret < 0)) {
191 rte_mempool_dump(stdout, mp);
192 /* in this case, objects are lost... */
198 /* put the objects back */
200 while (idx < n_keep) {
201 rte_mempool_generic_put(mp, &obj_table[idx],
207 end_cycles = rte_get_timer_cycles();
208 time_diff = end_cycles - start_cycles;
209 stats[lcore_id].enq_count += N;
213 if (use_external_cache) {
214 rte_mempool_cache_flush(cache, mp);
215 rte_mempool_cache_free(cache);
221 /* launch all the per-lcore test, and display the result */
223 launch_cores(struct rte_mempool *mp, unsigned int cores)
228 unsigned cores_save = cores;
230 rte_atomic32_set(&synchro, 0);
233 memset(stats, 0, sizeof(stats));
235 printf("mempool_autotest cache=%u cores=%u n_get_bulk=%u "
236 "n_put_bulk=%u n_keep=%u ",
238 external_cache_size : (unsigned) mp->cache_size,
239 cores, n_get_bulk, n_put_bulk, n_keep);
241 if (rte_mempool_avail_count(mp) != MEMPOOL_SIZE) {
242 printf("mempool is not full\n");
246 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
250 rte_eal_remote_launch(per_lcore_mempool_test,
254 /* start synchro and launch test on master */
255 rte_atomic32_set(&synchro, 1);
257 ret = per_lcore_mempool_test(mp);
260 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
264 if (rte_eal_wait_lcore(lcore_id) < 0)
269 printf("per-lcore test returned -1\n");
274 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
275 rate += (stats[lcore_id].enq_count / TIME_S);
277 printf("rate_persec=%" PRIu64 "\n", rate);
282 /* for a given number of core, launch all test cases */
284 do_one_mempool_test(struct rte_mempool *mp, unsigned int cores)
286 unsigned bulk_tab_get[] = { 1, 4, 32, 0 };
287 unsigned bulk_tab_put[] = { 1, 4, 32, 0 };
288 unsigned keep_tab[] = { 32, 128, 0 };
289 unsigned *get_bulk_ptr;
290 unsigned *put_bulk_ptr;
294 for (get_bulk_ptr = bulk_tab_get; *get_bulk_ptr; get_bulk_ptr++) {
295 for (put_bulk_ptr = bulk_tab_put; *put_bulk_ptr; put_bulk_ptr++) {
296 for (keep_ptr = keep_tab; *keep_ptr; keep_ptr++) {
298 n_get_bulk = *get_bulk_ptr;
299 n_put_bulk = *put_bulk_ptr;
301 ret = launch_cores(mp, cores);
312 test_mempool_perf(void)
314 struct rte_mempool *mp_cache = NULL;
315 struct rte_mempool *mp_nocache = NULL;
316 struct rte_mempool *default_pool = NULL;
319 rte_atomic32_init(&synchro);
321 /* create a mempool (without cache) */
322 mp_nocache = rte_mempool_create("perf_test_nocache", MEMPOOL_SIZE,
323 MEMPOOL_ELT_SIZE, 0, 0,
327 if (mp_nocache == NULL)
330 /* create a mempool (with cache) */
331 mp_cache = rte_mempool_create("perf_test_cache", MEMPOOL_SIZE,
333 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
337 if (mp_cache == NULL)
340 /* Create a mempool based on Default handler */
341 default_pool = rte_mempool_create_empty("default_pool",
347 if (default_pool == NULL) {
348 printf("cannot allocate %s mempool\n",
349 RTE_MBUF_DEFAULT_MEMPOOL_OPS);
353 if (rte_mempool_set_ops_byname(default_pool,
354 RTE_MBUF_DEFAULT_MEMPOOL_OPS, NULL)
356 printf("cannot set %s handler\n", RTE_MBUF_DEFAULT_MEMPOOL_OPS);
360 if (rte_mempool_populate_default(default_pool) < 0) {
361 printf("cannot populate %s mempool\n",
362 RTE_MBUF_DEFAULT_MEMPOOL_OPS);
366 rte_mempool_obj_iter(default_pool, my_obj_init, NULL);
368 /* performance test with 1, 2 and max cores */
369 printf("start performance test (without cache)\n");
371 if (do_one_mempool_test(mp_nocache, 1) < 0)
374 if (do_one_mempool_test(mp_nocache, 2) < 0)
377 if (do_one_mempool_test(mp_nocache, rte_lcore_count()) < 0)
380 /* performance test with 1, 2 and max cores */
381 printf("start performance test for %s (without cache)\n",
382 RTE_MBUF_DEFAULT_MEMPOOL_OPS);
384 if (do_one_mempool_test(default_pool, 1) < 0)
387 if (do_one_mempool_test(default_pool, 2) < 0)
390 if (do_one_mempool_test(default_pool, rte_lcore_count()) < 0)
393 /* performance test with 1, 2 and max cores */
394 printf("start performance test (with cache)\n");
396 if (do_one_mempool_test(mp_cache, 1) < 0)
399 if (do_one_mempool_test(mp_cache, 2) < 0)
402 if (do_one_mempool_test(mp_cache, rte_lcore_count()) < 0)
405 /* performance test with 1, 2 and max cores */
406 printf("start performance test (with user-owned cache)\n");
407 use_external_cache = 1;
409 if (do_one_mempool_test(mp_nocache, 1) < 0)
412 if (do_one_mempool_test(mp_nocache, 2) < 0)
415 if (do_one_mempool_test(mp_nocache, rte_lcore_count()) < 0)
418 rte_mempool_list_dump(stdout);
423 rte_mempool_free(mp_cache);
424 rte_mempool_free(mp_nocache);
425 rte_mempool_free(default_pool);
429 REGISTER_TEST_COMMAND(mempool_perf_autotest, test_mempool_perf);