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