xen: core library changes
[dpdk.git] / app / test / test_mempool.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
67  * =======
68  *
69  * Basic tests: done on one core with and without cache:
70  *
71  *    - Get one object, put one object
72  *    - Get two objects, put two objects
73  *    - Get all objects, test that their content is not modified and
74  *      put them back in the pool.
75  */
76
77 #define N 65536
78 #define TIME_S 5
79 #define MEMPOOL_ELT_SIZE 2048
80 #define MAX_KEEP 128
81 #define MEMPOOL_SIZE ((RTE_MAX_LCORE*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
82
83 static struct rte_mempool *mp;
84 static struct rte_mempool *mp_cache, *mp_nocache;
85
86 static rte_atomic32_t synchro;
87
88
89
90 /*
91  * save the object number in the first 4 bytes of object data. All
92  * other bytes are set to 0.
93  */
94 static void
95 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
96             void *obj, unsigned i)
97 {
98         uint32_t *objnum = obj;
99         memset(obj, 0, mp->elt_size);
100         *objnum = i;
101 }
102
103 /* basic tests (done on one core) */
104 static int
105 test_mempool_basic(void)
106 {
107         uint32_t *objnum;
108         void **objtable;
109         void *obj, *obj2;
110         char *obj_data;
111         int ret = 0;
112         unsigned i, j;
113
114         /* dump the mempool status */
115         rte_mempool_dump(mp);
116
117         printf("get an object\n");
118         if (rte_mempool_get(mp, &obj) < 0)
119                 return -1;
120         rte_mempool_dump(mp);
121
122         /* tests that improve coverage */
123         printf("get object count\n");
124         if (rte_mempool_count(mp) != MEMPOOL_SIZE - 1)
125                 return -1;
126
127         printf("get private data\n");
128         if (rte_mempool_get_priv(mp) !=
129                         (char*) mp + MEMPOOL_HEADER_SIZE(mp, mp->pg_num))
130                 return -1;
131
132         printf("get physical address of an object\n");
133         if (MEMPOOL_IS_CONTIG(mp) &&
134                         rte_mempool_virt2phy(mp, obj) !=
135                         (phys_addr_t) (mp->phys_addr +
136                         (phys_addr_t) ((char*) obj - (char*) mp)))
137                 return -1;
138
139         printf("put the object back\n");
140         rte_mempool_put(mp, obj);
141         rte_mempool_dump(mp);
142
143         printf("get 2 objects\n");
144         if (rte_mempool_get(mp, &obj) < 0)
145                 return -1;
146         if (rte_mempool_get(mp, &obj2) < 0) {
147                 rte_mempool_put(mp, obj);
148                 return -1;
149         }
150         rte_mempool_dump(mp);
151
152         printf("put the objects back\n");
153         rte_mempool_put(mp, obj);
154         rte_mempool_put(mp, obj2);
155         rte_mempool_dump(mp);
156
157         /*
158          * get many objects: we cannot get them all because the cache
159          * on other cores may not be empty.
160          */
161         objtable = malloc(MEMPOOL_SIZE * sizeof(void *));
162         if (objtable == NULL) {
163                 return -1;
164         }
165
166         for (i=0; i<MEMPOOL_SIZE; i++) {
167                 if (rte_mempool_get(mp, &objtable[i]) < 0)
168                         break;
169         }
170
171         /*
172          * for each object, check that its content was not modified,
173          * and put objects back in pool
174          */
175         while (i--) {
176                 obj = objtable[i];
177                 obj_data = obj;
178                 objnum = obj;
179                 if (*objnum > MEMPOOL_SIZE) {
180                         printf("bad object number\n");
181                         ret = -1;
182                         break;
183                 }
184                 for (j=sizeof(*objnum); j<mp->elt_size; j++) {
185                         if (obj_data[j] != 0)
186                                 ret = -1;
187                 }
188
189                 rte_mempool_put(mp, objtable[i]);
190         }
191
192         free(objtable);
193         if (ret == -1)
194                 printf("objects were modified!\n");
195
196         return ret;
197 }
198
199 static int test_mempool_creation_with_exceeded_cache_size(void)
200 {
201         struct rte_mempool *mp_cov;
202
203         mp_cov = rte_mempool_create("test_mempool_creation_with_exceeded_cache_size", MEMPOOL_SIZE,
204                                               MEMPOOL_ELT_SIZE,
205                                               RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0,
206                                               NULL, NULL,
207                                               my_obj_init, NULL,
208                                               SOCKET_ID_ANY, 0);
209         if(NULL != mp_cov) {
210                 return -1;
211         }
212
213         return 0;
214 }
215
216 static struct rte_mempool *mp_spsc;
217 static rte_spinlock_t scsp_spinlock;
218 static void *scsp_obj_table[MAX_KEEP];
219
220 /*
221  * single producer function
222  */
223 static int test_mempool_single_producer(void)
224 {
225         unsigned int i;
226         void *obj = NULL;
227         uint64_t start_cycles, end_cycles;
228         uint64_t duration = rte_get_timer_hz() * 8;
229
230         start_cycles = rte_get_timer_cycles();
231         while (1) {
232                 end_cycles = rte_get_timer_cycles();
233                 /* duration uses up, stop producing */
234                 if (start_cycles + duration < end_cycles)
235                         break;
236                 rte_spinlock_lock(&scsp_spinlock);
237                 for (i = 0; i < MAX_KEEP; i ++) {
238                         if (NULL != scsp_obj_table[i]) {
239                                 obj = scsp_obj_table[i];
240                                 break;
241                         }
242                 }
243                 rte_spinlock_unlock(&scsp_spinlock);
244                 if (i >= MAX_KEEP) {
245                         continue;
246                 }
247                 if (rte_mempool_from_obj(obj) != mp_spsc) {
248                         printf("test_mempool_single_producer there is an obj not owned by this mempool\n");
249                         return -1;
250                 }
251                 rte_mempool_sp_put(mp_spsc, obj);
252                 rte_spinlock_lock(&scsp_spinlock);
253                 scsp_obj_table[i] = NULL;
254                 rte_spinlock_unlock(&scsp_spinlock);
255         }
256
257         return 0;
258 }
259
260 /*
261  * single consumer function
262  */
263 static int test_mempool_single_consumer(void)
264 {
265         unsigned int i;
266         void * obj;
267         uint64_t start_cycles, end_cycles;
268         uint64_t duration = rte_get_timer_hz() * 5;
269
270         start_cycles = rte_get_timer_cycles();
271         while (1) {
272                 end_cycles = rte_get_timer_cycles();
273                 /* duration uses up, stop consuming */
274                 if (start_cycles + duration < end_cycles)
275                         break;
276                 rte_spinlock_lock(&scsp_spinlock);
277                 for (i = 0; i < MAX_KEEP; i ++) {
278                         if (NULL == scsp_obj_table[i])
279                                 break;
280                 }
281                 rte_spinlock_unlock(&scsp_spinlock);
282                 if (i >= MAX_KEEP)
283                         continue;
284                 if (rte_mempool_sc_get(mp_spsc, &obj) < 0)
285                         break;
286                 rte_spinlock_lock(&scsp_spinlock);
287                 scsp_obj_table[i] = obj;
288                 rte_spinlock_unlock(&scsp_spinlock);
289         }
290
291         return 0;
292 }
293
294 /*
295  * test function for mempool test based on singple consumer and single producer, can run on one lcore only
296  */
297 static int test_mempool_launch_single_consumer(__attribute__((unused)) void *arg)
298 {
299         return test_mempool_single_consumer();
300 }
301
302 static void my_mp_init(struct rte_mempool * mp, __attribute__((unused)) void * arg)
303 {
304         printf("mempool name is %s\n", mp->name);
305         /* nothing to be implemented here*/
306         return ;
307 }
308
309 /*
310  * it tests the mempool operations based on singple producer and single consumer
311  */
312 static int
313 test_mempool_sp_sc(void)
314 {
315         int ret = 0;
316         unsigned lcore_id = rte_lcore_id();
317         unsigned lcore_next;
318
319         /* create a mempool with single producer/consumer ring */
320         if (NULL == mp_spsc) {
321                 mp_spsc = rte_mempool_create("test_mempool_sp_sc", MEMPOOL_SIZE,
322                                                 MEMPOOL_ELT_SIZE, 0, 0,
323                                                 my_mp_init, NULL,
324                                                 my_obj_init, NULL,
325                                                 SOCKET_ID_ANY, MEMPOOL_F_NO_CACHE_ALIGN | MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET);
326                 if (NULL == mp_spsc) {
327                         return -1;
328                 }
329         }
330         if (rte_mempool_lookup("test_mempool_sp_sc") != mp_spsc) {
331                 printf("Cannot lookup mempool from its name\n");
332                 return -1;
333         }
334         lcore_next = rte_get_next_lcore(lcore_id, 0, 1);
335         if (RTE_MAX_LCORE <= lcore_next)
336                 return -1;
337         if (rte_eal_lcore_role(lcore_next) != ROLE_RTE)
338                 return -1;
339         rte_spinlock_init(&scsp_spinlock);
340         memset(scsp_obj_table, 0, sizeof(scsp_obj_table));
341         rte_eal_remote_launch(test_mempool_launch_single_consumer, NULL, lcore_next);
342         if(test_mempool_single_producer() < 0)
343                 ret = -1;
344
345         if(rte_eal_wait_lcore(lcore_next) < 0)
346                 ret = -1;
347
348         return ret;
349 }
350
351 /*
352  * it tests some more basic of mempool
353  */
354 static int
355 test_mempool_basic_ex(struct rte_mempool * mp)
356 {
357         unsigned i;
358         void **obj;
359         void *err_obj;
360         int ret = -1;
361
362         if (mp == NULL)
363                 return ret;
364
365         obj = (void **)rte_zmalloc("test_mempool_basic_ex", (MEMPOOL_SIZE * sizeof(void *)), 0);
366         if (obj == NULL) {
367                 printf("test_mempool_basic_ex fail to rte_malloc\n");
368                 return ret;
369         }
370         printf("test_mempool_basic_ex now mempool (%s) has %u free entries\n", mp->name, rte_mempool_free_count(mp));
371         if (rte_mempool_full(mp) != 1) {
372                 printf("test_mempool_basic_ex the mempool is not full but it should be\n");
373                 goto fail_mp_basic_ex;
374         }
375
376         for (i = 0; i < MEMPOOL_SIZE; i ++) {
377                 if (rte_mempool_mc_get(mp, &obj[i]) < 0) {
378                         printf("fail_mp_basic_ex fail to get mempool object for [%u]\n", i);
379                         goto fail_mp_basic_ex;
380                 }
381         }
382         if (rte_mempool_mc_get(mp, &err_obj) == 0) {
383                 printf("test_mempool_basic_ex get an impossible obj from mempool\n");
384                 goto fail_mp_basic_ex;
385         }
386         printf("number: %u\n", i);
387         if (rte_mempool_empty(mp) != 1) {
388                 printf("test_mempool_basic_ex the mempool is not empty but it should be\n");
389                 goto fail_mp_basic_ex;
390         }
391
392         for (i = 0; i < MEMPOOL_SIZE; i ++) {
393                 rte_mempool_mp_put(mp, obj[i]);
394         }
395         if (rte_mempool_full(mp) != 1) {
396                 printf("test_mempool_basic_ex the mempool is not full but it should be\n");
397                 goto fail_mp_basic_ex;
398         }
399
400         ret = 0;
401
402 fail_mp_basic_ex:
403         if (obj != NULL)
404                 rte_free((void *)obj);
405
406         return ret;
407 }
408
409 static int
410 test_mempool_same_name_twice_creation(void)
411 {
412         struct rte_mempool *mp_tc;
413
414         mp_tc = rte_mempool_create("test_mempool_same_name_twice_creation", MEMPOOL_SIZE,
415                                                 MEMPOOL_ELT_SIZE, 0, 0,
416                                                 NULL, NULL,
417                                                 NULL, NULL,
418                                                 SOCKET_ID_ANY, 0);
419         if (NULL == mp_tc)
420                 return -1;
421
422         mp_tc = rte_mempool_create("test_mempool_same_name_twice_creation", MEMPOOL_SIZE,
423                                                 MEMPOOL_ELT_SIZE, 0, 0,
424                                                 NULL, NULL,
425                                                 NULL, NULL,
426                                                 SOCKET_ID_ANY, 0);
427         if (NULL != mp_tc)
428                 return -1;
429
430         return 0;
431 }
432
433 /*
434  * BAsic test for mempool_xmem functions.
435  */
436 static int
437 test_mempool_xmem_misc(void)
438 {
439         uint32_t elt_num, total_size;
440         size_t sz;
441         ssize_t usz;
442
443         elt_num = MAX_KEEP;
444         total_size = rte_mempool_calc_obj_size(MEMPOOL_ELT_SIZE, 0, NULL);
445         sz = rte_mempool_xmem_size(elt_num, total_size, MEMPOOL_PG_SHIFT_MAX);
446
447         usz = rte_mempool_xmem_usage(NULL, elt_num, total_size, 0, 1,
448                 MEMPOOL_PG_SHIFT_MAX);
449
450         if(sz != (size_t)usz)  {
451                 printf("failure @ %s: rte_mempool_xmem_usage(%u, %u) "
452                         "returns: %#zx, while expected: %#zx;\n",
453                         __func__, elt_num, total_size, sz, (size_t)usz);
454                 return (-1);
455         }
456
457         return (0);
458 }
459
460 int
461 test_mempool(void)
462 {
463         rte_atomic32_init(&synchro);
464
465         /* create a mempool (without cache) */
466         if (mp_nocache == NULL)
467                 mp_nocache = rte_mempool_create("test_nocache", MEMPOOL_SIZE,
468                                                 MEMPOOL_ELT_SIZE, 0, 0,
469                                                 NULL, NULL,
470                                                 my_obj_init, NULL,
471                                                 SOCKET_ID_ANY, 0);
472         if (mp_nocache == NULL)
473                 return -1;
474
475         /* create a mempool (with cache) */
476         if (mp_cache == NULL)
477                 mp_cache = rte_mempool_create("test_cache", MEMPOOL_SIZE,
478                                               MEMPOOL_ELT_SIZE,
479                                               RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
480                                               NULL, NULL,
481                                               my_obj_init, NULL,
482                                               SOCKET_ID_ANY, 0);
483         if (mp_cache == NULL)
484                 return -1;
485
486
487         /* retrieve the mempool from its name */
488         if (rte_mempool_lookup("test_nocache") != mp_nocache) {
489                 printf("Cannot lookup mempool from its name\n");
490                 return -1;
491         }
492
493         rte_mempool_list_dump();
494
495         /* basic tests without cache */
496         mp = mp_nocache;
497         if (test_mempool_basic() < 0)
498                 return -1;
499
500         /* basic tests with cache */
501         mp = mp_cache;
502         if (test_mempool_basic() < 0)
503                 return -1;
504
505         /* more basic tests without cache */
506         if (test_mempool_basic_ex(mp_nocache) < 0)
507                 return -1;
508
509         /* mempool operation test based on single producer and single comsumer */
510         if (test_mempool_sp_sc() < 0)
511                 return -1;
512
513         if (test_mempool_creation_with_exceeded_cache_size() < 0)
514                 return -1;
515
516         if (test_mempool_same_name_twice_creation() < 0)
517                 return -1;
518
519         if (test_mempool_xmem_misc() < 0)
520                 return -1;
521
522         rte_mempool_list_dump();
523
524         return 0;
525 }