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 * Each core get *n_keep* objects per bulk of *n_get_bulk*. Then,
67 * objects are put back in the pool per bulk of *n_put_bulk*.
69 * This sequence is done during TIME_S seconds.
71 * This test is done on the following configurations:
73 * - Cores configuration (*cores*)
75 * - One core with cache
76 * - Two cores with cache
77 * - Max. cores with cache
78 * - One core without cache
79 * - Two cores without cache
80 * - Max. cores without cache
82 * - Bulk size (*n_get_bulk*, *n_put_bulk*)
84 * - Bulk get from 1 to 32
85 * - Bulk put from 1 to 32
87 * - Number of kept objects (*n_keep*)
95 #define MEMPOOL_ELT_SIZE 2048
97 #define MEMPOOL_SIZE ((RTE_MAX_LCORE*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
99 static struct rte_mempool *mp;
100 static struct rte_mempool *mp_cache, *mp_nocache;
102 static rte_atomic32_t synchro;
104 /* number of objects in one bulk operation (get or put) */
105 static unsigned n_get_bulk;
106 static unsigned n_put_bulk;
108 /* number of objects retrived from mempool before putting them back */
109 static unsigned n_keep;
111 /* number of enqueues / dequeues */
112 struct mempool_test_stats {
114 } __rte_cache_aligned;
116 static struct mempool_test_stats stats[RTE_MAX_LCORE];
119 * save the object number in the first 4 bytes of object data. All
120 * other bytes are set to 0.
123 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
124 void *obj, unsigned i)
126 uint32_t *objnum = obj;
127 memset(obj, 0, mp->elt_size);
132 per_lcore_mempool_test(__attribute__((unused)) void *arg)
134 void *obj_table[MAX_KEEP];
136 unsigned lcore_id = rte_lcore_id();
138 uint64_t start_cycles, end_cycles;
139 uint64_t time_diff = 0, hz = rte_get_timer_hz();
141 /* n_get_bulk and n_put_bulk must be divisors of n_keep */
142 if (((n_keep / n_get_bulk) * n_get_bulk) != n_keep)
144 if (((n_keep / n_put_bulk) * n_put_bulk) != n_keep)
147 stats[lcore_id].enq_count = 0;
149 /* wait synchro for slaves */
150 if (lcore_id != rte_get_master_lcore())
151 while (rte_atomic32_read(&synchro) == 0);
153 start_cycles = rte_get_timer_cycles();
155 while (time_diff/hz < TIME_S) {
156 for (i = 0; likely(i < (N/n_keep)); i++) {
157 /* get n_keep objects by bulk of n_bulk */
159 while (idx < n_keep) {
160 ret = rte_mempool_get_bulk(mp, &obj_table[idx],
162 if (unlikely(ret < 0)) {
163 rte_mempool_dump(stdout, mp);
164 rte_ring_dump(stdout, mp->ring);
165 /* in this case, objects are lost... */
171 /* put the objects back */
173 while (idx < n_keep) {
174 rte_mempool_put_bulk(mp, &obj_table[idx],
179 end_cycles = rte_get_timer_cycles();
180 time_diff = end_cycles - start_cycles;
181 stats[lcore_id].enq_count += N;
187 /* launch all the per-lcore test, and display the result */
189 launch_cores(unsigned cores)
194 unsigned cores_save = cores;
196 rte_atomic32_set(&synchro, 0);
199 memset(stats, 0, sizeof(stats));
201 printf("mempool_autotest cache=%u cores=%u n_get_bulk=%u "
202 "n_put_bulk=%u n_keep=%u ",
203 (unsigned) mp->cache_size, cores, n_get_bulk, n_put_bulk, n_keep);
205 if (rte_mempool_count(mp) != MEMPOOL_SIZE) {
206 printf("mempool is not full\n");
210 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
214 rte_eal_remote_launch(per_lcore_mempool_test,
218 /* start synchro and launch test on master */
219 rte_atomic32_set(&synchro, 1);
221 ret = per_lcore_mempool_test(NULL);
224 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
228 if (rte_eal_wait_lcore(lcore_id) < 0)
233 printf("per-lcore test returned -1\n");
238 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
239 rate += (stats[lcore_id].enq_count / TIME_S);
241 printf("rate_persec=%u\n", rate);
246 /* for a given number of core, launch all test cases */
248 do_one_mempool_test(unsigned cores)
250 unsigned bulk_tab_get[] = { 1, 4, 32, 0 };
251 unsigned bulk_tab_put[] = { 1, 4, 32, 0 };
252 unsigned keep_tab[] = { 32, 128, 0 };
253 unsigned *get_bulk_ptr;
254 unsigned *put_bulk_ptr;
258 for (get_bulk_ptr = bulk_tab_get; *get_bulk_ptr; get_bulk_ptr++) {
259 for (put_bulk_ptr = bulk_tab_put; *put_bulk_ptr; put_bulk_ptr++) {
260 for (keep_ptr = keep_tab; *keep_ptr; keep_ptr++) {
262 n_get_bulk = *get_bulk_ptr;
263 n_put_bulk = *put_bulk_ptr;
265 ret = launch_cores(cores);
276 test_mempool_perf(void)
278 rte_atomic32_init(&synchro);
280 /* create a mempool (without cache) */
281 if (mp_nocache == NULL)
282 mp_nocache = rte_mempool_create("perf_test_nocache", MEMPOOL_SIZE,
283 MEMPOOL_ELT_SIZE, 0, 0,
287 if (mp_nocache == NULL)
290 /* create a mempool (with cache) */
291 if (mp_cache == NULL)
292 mp_cache = rte_mempool_create("perf_test_cache", MEMPOOL_SIZE,
294 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
298 if (mp_cache == NULL)
301 /* performance test with 1, 2 and max cores */
302 printf("start performance test (without cache)\n");
305 if (do_one_mempool_test(1) < 0)
308 if (do_one_mempool_test(2) < 0)
311 if (do_one_mempool_test(rte_lcore_count()) < 0)
314 /* performance test with 1, 2 and max cores */
315 printf("start performance test (with cache)\n");
318 if (do_one_mempool_test(1) < 0)
321 if (do_one_mempool_test(2) < 0)
324 if (do_one_mempool_test(rte_lcore_count()) < 0)
327 rte_mempool_list_dump(stdout);
332 static struct test_command mempool_perf_cmd = {
333 .command = "mempool_perf_autotest",
334 .callback = test_mempool_perf,
336 REGISTER_TEST_COMMAND(mempool_perf_cmd);