86cba8ae7f7f5e779ce087417eaf72b103d2d70c
[dpdk.git] / app / 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_tailq.h>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_atomic.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_ring.h>
57 #include <rte_mempool.h>
58 #include <rte_spinlock.h>
59 #include <rte_malloc.h>
60
61 #include "test.h"
62
63 /*
64  * Mempool performance
65  * =======
66  *
67  *    Each core get *n_keep* objects per bulk of *n_get_bulk*. Then,
68  *    objects are put back in the pool per bulk of *n_put_bulk*.
69  *
70  *    This sequence is done during TIME_S seconds.
71  *
72  *    This test is done on the following configurations:
73  *
74  *    - Cores configuration (*cores*)
75  *
76  *      - One core with cache
77  *      - Two cores with cache
78  *      - Max. cores with cache
79  *      - One core without cache
80  *      - Two cores without cache
81  *      - Max. cores without cache
82  *
83  *    - Bulk size (*n_get_bulk*, *n_put_bulk*)
84  *
85  *      - Bulk get from 1 to 32
86  *      - Bulk put from 1 to 32
87  *
88  *    - Number of kept objects (*n_keep*)
89  *
90  *      - 32
91  *      - 128
92  */
93
94 #define N 65536
95 #define TIME_S 5
96 #define MEMPOOL_ELT_SIZE 2048
97 #define MAX_KEEP 128
98 #define MEMPOOL_SIZE ((RTE_MAX_LCORE*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
99
100 static struct rte_mempool *mp;
101 static struct rte_mempool *mp_cache, *mp_nocache;
102
103 static rte_atomic32_t synchro;
104
105 /* number of objects in one bulk operation (get or put) */
106 static unsigned n_get_bulk;
107 static unsigned n_put_bulk;
108
109 /* number of objects retrived from mempool before putting them back */
110 static unsigned n_keep;
111
112 /* number of enqueues / dequeues */
113 struct mempool_test_stats {
114         unsigned enq_count;
115 } __rte_cache_aligned;
116
117 static struct mempool_test_stats stats[RTE_MAX_LCORE];
118
119 /*
120  * save the object number in the first 4 bytes of object data. All
121  * other bytes are set to 0.
122  */
123 static void
124 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
125             void *obj, unsigned i)
126 {
127         uint32_t *objnum = obj;
128         memset(obj, 0, mp->elt_size);
129         *objnum = i;
130 }
131
132 static int
133 per_lcore_mempool_test(__attribute__((unused)) void *arg)
134 {
135         void *obj_table[MAX_KEEP];
136         unsigned i, idx;
137         unsigned lcore_id = rte_lcore_id();
138         int ret;
139         uint64_t start_cycles, end_cycles;
140         uint64_t time_diff = 0, hz = rte_get_timer_hz();
141
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)
144                 return -1;
145         if (((n_keep / n_put_bulk) * n_put_bulk) != n_keep)
146                 return -1;
147
148         stats[lcore_id].enq_count = 0;
149
150         /* wait synchro for slaves */
151         if (lcore_id != rte_get_master_lcore())
152                 while (rte_atomic32_read(&synchro) == 0);
153
154         start_cycles = rte_get_timer_cycles();
155
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 */
159                         idx = 0;
160                         while (idx < n_keep) {
161                                 ret = rte_mempool_get_bulk(mp, &obj_table[idx],
162                                                            n_get_bulk);
163                                 if (unlikely(ret < 0)) {
164                                         rte_mempool_dump(mp);
165                                         rte_ring_dump(mp->ring);
166                                         /* in this case, objects are lost... */
167                                         return -1;
168                                 }
169                                 idx += n_get_bulk;
170                         }
171
172                         /* put the objects back */
173                         idx = 0;
174                         while (idx < n_keep) {
175                                 rte_mempool_put_bulk(mp, &obj_table[idx],
176                                                      n_put_bulk);
177                                 idx += n_put_bulk;
178                         }
179                 }
180                 end_cycles = rte_get_timer_cycles();
181                 time_diff = end_cycles - start_cycles;
182                 stats[lcore_id].enq_count += N;
183         }
184
185         return 0;
186 }
187
188 /* launch all the per-lcore test, and display the result */
189 static int
190 launch_cores(unsigned cores)
191 {
192         unsigned lcore_id;
193         unsigned rate;
194         int ret;
195         unsigned cores_save = cores;
196
197         rte_atomic32_set(&synchro, 0);
198
199         /* reset stats */
200         memset(stats, 0, sizeof(stats));
201
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);
205
206         if (rte_mempool_count(mp) != MEMPOOL_SIZE) {
207                 printf("mempool is not full\n");
208                 return -1;
209         }
210
211         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
212                 if (cores == 1)
213                         break;
214                 cores--;
215                 rte_eal_remote_launch(per_lcore_mempool_test,
216                                       NULL, lcore_id);
217         }
218
219         /* start synchro and launch test on master */
220         rte_atomic32_set(&synchro, 1);
221
222         ret = per_lcore_mempool_test(NULL);
223
224         cores = cores_save;
225         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
226                 if (cores == 1)
227                         break;
228                 cores--;
229                 if (rte_eal_wait_lcore(lcore_id) < 0)
230                         ret = -1;
231         }
232
233         if (ret < 0) {
234                 printf("per-lcore test returned -1\n");
235                 return -1;
236         }
237
238         rate = 0;
239         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
240                 rate += (stats[lcore_id].enq_count / TIME_S);
241
242         printf("rate_persec=%u\n", rate);
243
244         return 0;
245 }
246
247 /* for a given number of core, launch all test cases */
248 static int
249 do_one_mempool_test(unsigned cores)
250 {
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;
256         unsigned *keep_ptr;
257         int ret;
258
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++) {
262
263                                 n_get_bulk = *get_bulk_ptr;
264                                 n_put_bulk = *put_bulk_ptr;
265                                 n_keep = *keep_ptr;
266                                 ret = launch_cores(cores);
267
268                                 if (ret < 0)
269                                         return -1;
270                         }
271                 }
272         }
273         return 0;
274 }
275
276 int
277 test_mempool_perf(void)
278 {
279         rte_atomic32_init(&synchro);
280
281         /* create a mempool (without cache) */
282         if (mp_nocache == NULL)
283                 mp_nocache = rte_mempool_create("perf_test_nocache", MEMPOOL_SIZE,
284                                                 MEMPOOL_ELT_SIZE, 0, 0,
285                                                 NULL, NULL,
286                                                 my_obj_init, NULL,
287                                                 SOCKET_ID_ANY, 0);
288         if (mp_nocache == NULL)
289                 return -1;
290
291         /* create a mempool (with cache) */
292         if (mp_cache == NULL)
293                 mp_cache = rte_mempool_create("perf_test_cache", MEMPOOL_SIZE,
294                                               MEMPOOL_ELT_SIZE,
295                                               RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
296                                               NULL, NULL,
297                                               my_obj_init, NULL,
298                                               SOCKET_ID_ANY, 0);
299         if (mp_cache == NULL)
300                 return -1;
301
302         /* performance test with 1, 2 and max cores */
303         printf("start performance test (without cache)\n");
304         mp = mp_nocache;
305
306         if (do_one_mempool_test(1) < 0)
307                 return -1;
308
309         if (do_one_mempool_test(2) < 0)
310                 return -1;
311
312         if (do_one_mempool_test(rte_lcore_count()) < 0)
313                 return -1;
314
315         /* performance test with 1, 2 and max cores */
316         printf("start performance test (with cache)\n");
317         mp = mp_cache;
318
319         if (do_one_mempool_test(1) < 0)
320                 return -1;
321
322         if (do_one_mempool_test(2) < 0)
323                 return -1;
324
325         if (do_one_mempool_test(rte_lcore_count()) < 0)
326                 return -1;
327
328         rte_mempool_list_dump();
329
330         return 0;
331 }