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