3749cf8bec36b8a976d2f3f6f436ee09ba204ee1
[dpdk.git] / test / test / test_mempool_perf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <stdarg.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_common.h>
44 #include <rte_log.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>
50 #include <rte_eal.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>
58
59 #include "test.h"
60
61 /*
62  * Mempool performance
63  * =======
64  *
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*.
67  *
68  *    This sequence is done during TIME_S seconds.
69  *
70  *    This test is done on the following configurations:
71  *
72  *    - Cores configuration (*cores*)
73  *
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
83  *
84  *    - Bulk size (*n_get_bulk*, *n_put_bulk*)
85  *
86  *      - Bulk get from 1 to 32
87  *      - Bulk put from 1 to 32
88  *
89  *    - Number of kept objects (*n_keep*)
90  *
91  *      - 32
92  *      - 128
93  */
94
95 #define N 65536
96 #define TIME_S 5
97 #define MEMPOOL_ELT_SIZE 2048
98 #define MAX_KEEP 128
99 #define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
100
101 #define LOG_ERR() printf("test failed at %s():%d\n", __func__, __LINE__)
102 #define RET_ERR() do {                                                  \
103                 LOG_ERR();                                              \
104                 return -1;                                              \
105         } while (0)
106 #define GOTO_ERR(var, label) do {                                       \
107                 LOG_ERR();                                              \
108                 var = -1;                                               \
109                 goto label;                                             \
110         } while (0)
111
112 static int use_external_cache;
113 static unsigned external_cache_size = RTE_MEMPOOL_CACHE_MAX_SIZE;
114
115 static rte_atomic32_t synchro;
116
117 /* number of objects in one bulk operation (get or put) */
118 static unsigned n_get_bulk;
119 static unsigned n_put_bulk;
120
121 /* number of objects retrived from mempool before putting them back */
122 static unsigned n_keep;
123
124 /* number of enqueues / dequeues */
125 struct mempool_test_stats {
126         uint64_t enq_count;
127 } __rte_cache_aligned;
128
129 static struct mempool_test_stats stats[RTE_MAX_LCORE];
130
131 /*
132  * save the object number in the first 4 bytes of object data. All
133  * other bytes are set to 0.
134  */
135 static void
136 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
137             void *obj, unsigned i)
138 {
139         uint32_t *objnum = obj;
140         memset(obj, 0, mp->elt_size);
141         *objnum = i;
142 }
143
144 static int
145 per_lcore_mempool_test(void *arg)
146 {
147         void *obj_table[MAX_KEEP];
148         unsigned i, idx;
149         struct rte_mempool *mp = arg;
150         unsigned lcore_id = rte_lcore_id();
151         int ret = 0;
152         uint64_t start_cycles, end_cycles;
153         uint64_t time_diff = 0, hz = rte_get_timer_hz();
154         struct rte_mempool_cache *cache;
155
156         if (use_external_cache) {
157                 /* Create a user-owned mempool cache. */
158                 cache = rte_mempool_cache_create(external_cache_size,
159                                                  SOCKET_ID_ANY);
160                 if (cache == NULL)
161                         RET_ERR();
162         } else {
163                 /* May be NULL if cache is disabled. */
164                 cache = rte_mempool_default_cache(mp, lcore_id);
165         }
166
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)
169                 GOTO_ERR(ret, out);
170         if (((n_keep / n_put_bulk) * n_put_bulk) != n_keep)
171                 GOTO_ERR(ret, out);
172
173         stats[lcore_id].enq_count = 0;
174
175         /* wait synchro for slaves */
176         if (lcore_id != rte_get_master_lcore())
177                 while (rte_atomic32_read(&synchro) == 0);
178
179         start_cycles = rte_get_timer_cycles();
180
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 */
184                         idx = 0;
185                         while (idx < n_keep) {
186                                 ret = rte_mempool_generic_get(mp,
187                                                               &obj_table[idx],
188                                                               n_get_bulk,
189                                                               cache, 0);
190                                 if (unlikely(ret < 0)) {
191                                         rte_mempool_dump(stdout, mp);
192                                         /* in this case, objects are lost... */
193                                         GOTO_ERR(ret, out);
194                                 }
195                                 idx += n_get_bulk;
196                         }
197
198                         /* put the objects back */
199                         idx = 0;
200                         while (idx < n_keep) {
201                                 rte_mempool_generic_put(mp, &obj_table[idx],
202                                                         n_put_bulk,
203                                                         cache, 0);
204                                 idx += n_put_bulk;
205                         }
206                 }
207                 end_cycles = rte_get_timer_cycles();
208                 time_diff = end_cycles - start_cycles;
209                 stats[lcore_id].enq_count += N;
210         }
211
212 out:
213         if (use_external_cache) {
214                 rte_mempool_cache_flush(cache, mp);
215                 rte_mempool_cache_free(cache);
216         }
217
218         return ret;
219 }
220
221 /* launch all the per-lcore test, and display the result */
222 static int
223 launch_cores(struct rte_mempool *mp, unsigned int cores)
224 {
225         unsigned lcore_id;
226         uint64_t rate;
227         int ret;
228         unsigned cores_save = cores;
229
230         rte_atomic32_set(&synchro, 0);
231
232         /* reset stats */
233         memset(stats, 0, sizeof(stats));
234
235         printf("mempool_autotest cache=%u cores=%u n_get_bulk=%u "
236                "n_put_bulk=%u n_keep=%u ",
237                use_external_cache ?
238                    external_cache_size : (unsigned) mp->cache_size,
239                cores, n_get_bulk, n_put_bulk, n_keep);
240
241         if (rte_mempool_avail_count(mp) != MEMPOOL_SIZE) {
242                 printf("mempool is not full\n");
243                 return -1;
244         }
245
246         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
247                 if (cores == 1)
248                         break;
249                 cores--;
250                 rte_eal_remote_launch(per_lcore_mempool_test,
251                                       mp, lcore_id);
252         }
253
254         /* start synchro and launch test on master */
255         rte_atomic32_set(&synchro, 1);
256
257         ret = per_lcore_mempool_test(mp);
258
259         cores = cores_save;
260         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
261                 if (cores == 1)
262                         break;
263                 cores--;
264                 if (rte_eal_wait_lcore(lcore_id) < 0)
265                         ret = -1;
266         }
267
268         if (ret < 0) {
269                 printf("per-lcore test returned -1\n");
270                 return -1;
271         }
272
273         rate = 0;
274         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
275                 rate += (stats[lcore_id].enq_count / TIME_S);
276
277         printf("rate_persec=%" PRIu64 "\n", rate);
278
279         return 0;
280 }
281
282 /* for a given number of core, launch all test cases */
283 static int
284 do_one_mempool_test(struct rte_mempool *mp, unsigned int cores)
285 {
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;
291         unsigned *keep_ptr;
292         int ret;
293
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++) {
297
298                                 n_get_bulk = *get_bulk_ptr;
299                                 n_put_bulk = *put_bulk_ptr;
300                                 n_keep = *keep_ptr;
301                                 ret = launch_cores(mp, cores);
302
303                                 if (ret < 0)
304                                         return -1;
305                         }
306                 }
307         }
308         return 0;
309 }
310
311 static int
312 test_mempool_perf(void)
313 {
314         struct rte_mempool *mp_cache = NULL;
315         struct rte_mempool *mp_nocache = NULL;
316         int ret = -1;
317
318         rte_atomic32_init(&synchro);
319
320         /* create a mempool (without cache) */
321         mp_nocache = rte_mempool_create("perf_test_nocache", MEMPOOL_SIZE,
322                                         MEMPOOL_ELT_SIZE, 0, 0,
323                                         NULL, NULL,
324                                         my_obj_init, NULL,
325                                         SOCKET_ID_ANY, 0);
326         if (mp_nocache == NULL)
327                 goto err;
328
329         /* create a mempool (with cache) */
330         mp_cache = rte_mempool_create("perf_test_cache", MEMPOOL_SIZE,
331                                       MEMPOOL_ELT_SIZE,
332                                       RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
333                                       NULL, NULL,
334                                       my_obj_init, NULL,
335                                       SOCKET_ID_ANY, 0);
336         if (mp_cache == NULL)
337                 goto err;
338
339         /* performance test with 1, 2 and max cores */
340         printf("start performance test (without cache)\n");
341
342         if (do_one_mempool_test(mp_nocache, 1) < 0)
343                 goto err;
344
345         if (do_one_mempool_test(mp_nocache, 2) < 0)
346                 goto err;
347
348         if (do_one_mempool_test(mp_nocache, rte_lcore_count()) < 0)
349                 goto err;
350
351         /* performance test with 1, 2 and max cores */
352         printf("start performance test (with cache)\n");
353
354         if (do_one_mempool_test(mp_cache, 1) < 0)
355                 goto err;
356
357         if (do_one_mempool_test(mp_cache, 2) < 0)
358                 goto err;
359
360         if (do_one_mempool_test(mp_cache, rte_lcore_count()) < 0)
361                 goto err;
362
363         /* performance test with 1, 2 and max cores */
364         printf("start performance test (with user-owned cache)\n");
365         use_external_cache = 1;
366
367         if (do_one_mempool_test(mp_nocache, 1) < 0)
368                 goto err;
369
370         if (do_one_mempool_test(mp_nocache, 2) < 0)
371                 goto err;
372
373         if (do_one_mempool_test(mp_nocache, rte_lcore_count()) < 0)
374                 goto err;
375
376         rte_mempool_list_dump(stdout);
377
378         ret = 0;
379
380 err:
381         rte_mempool_free(mp_cache);
382         rte_mempool_free(mp_nocache);
383         return ret;
384 }
385
386 REGISTER_TEST_COMMAND(mempool_perf_autotest, test_mempool_perf);