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