9f027587b9d1f984a5d75d92fa42a814e9b803bc
[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 #define RET_ERR() do {                                                  \
81                 printf("test failed at %s():%d\n", __func__, __LINE__); \
82                 return -1;                                              \
83         } while (0)
84
85 static rte_atomic32_t synchro;
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
97         memset(obj, 0, mp->elt_size);
98         *objnum = i;
99 }
100
101 /* basic tests (done on one core) */
102 static int
103 test_mempool_basic(struct rte_mempool *mp)
104 {
105         uint32_t *objnum;
106         void **objtable;
107         void *obj, *obj2;
108         char *obj_data;
109         int ret = 0;
110         unsigned i, j;
111
112         /* dump the mempool status */
113         rte_mempool_dump(stdout, mp);
114
115         printf("get an object\n");
116         if (rte_mempool_get(mp, &obj) < 0)
117                 RET_ERR();
118         rte_mempool_dump(stdout, mp);
119
120         /* tests that improve coverage */
121         printf("get object count\n");
122         if (rte_mempool_count(mp) != MEMPOOL_SIZE - 1)
123                 RET_ERR();
124
125         printf("get private data\n");
126         if (rte_mempool_get_priv(mp) != (char *)mp +
127                         MEMPOOL_HEADER_SIZE(mp, mp->cache_size))
128                 RET_ERR();
129
130 #ifndef RTE_EXEC_ENV_BSDAPP /* rte_mem_virt2phy() not supported on bsd */
131         printf("get physical address of an object\n");
132         if (rte_mempool_virt2phy(mp, obj) != rte_mem_virt2phy(obj))
133                 RET_ERR();
134 #endif
135
136         printf("put the object back\n");
137         rte_mempool_put(mp, obj);
138         rte_mempool_dump(stdout, mp);
139
140         printf("get 2 objects\n");
141         if (rte_mempool_get(mp, &obj) < 0)
142                 RET_ERR();
143         if (rte_mempool_get(mp, &obj2) < 0) {
144                 rte_mempool_put(mp, obj);
145                 RET_ERR();
146         }
147         rte_mempool_dump(stdout, mp);
148
149         printf("put the objects back\n");
150         rte_mempool_put(mp, obj);
151         rte_mempool_put(mp, obj2);
152         rte_mempool_dump(stdout, mp);
153
154         /*
155          * get many objects: we cannot get them all because the cache
156          * on other cores may not be empty.
157          */
158         objtable = malloc(MEMPOOL_SIZE * sizeof(void *));
159         if (objtable == NULL)
160                 RET_ERR();
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(%d)\n", *objnum);
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_cache_too_big",
200                 MEMPOOL_SIZE,
201                 MEMPOOL_ELT_SIZE,
202                 RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0,
203                 NULL, NULL,
204                 my_obj_init, NULL,
205                 SOCKET_ID_ANY, 0);
206
207         if (mp_cov != NULL) {
208                 rte_mempool_free(mp_cov);
209                 RET_ERR();
210         }
211
212         return 0;
213 }
214
215 static struct rte_mempool *mp_spsc;
216 static rte_spinlock_t scsp_spinlock;
217 static void *scsp_obj_table[MAX_KEEP];
218
219 /*
220  * single producer function
221  */
222 static int test_mempool_single_producer(void)
223 {
224         unsigned int i;
225         void *obj = NULL;
226         uint64_t start_cycles, end_cycles;
227         uint64_t duration = rte_get_timer_hz() * 8;
228
229         start_cycles = rte_get_timer_cycles();
230         while (1) {
231                 end_cycles = rte_get_timer_cycles();
232                 /* duration uses up, stop producing */
233                 if (start_cycles + duration < end_cycles)
234                         break;
235                 rte_spinlock_lock(&scsp_spinlock);
236                 for (i = 0; i < MAX_KEEP; i ++) {
237                         if (NULL != scsp_obj_table[i]) {
238                                 obj = scsp_obj_table[i];
239                                 break;
240                         }
241                 }
242                 rte_spinlock_unlock(&scsp_spinlock);
243                 if (i >= MAX_KEEP) {
244                         continue;
245                 }
246                 if (rte_mempool_from_obj(obj) != mp_spsc) {
247                         printf("obj not owned by this mempool\n");
248                         RET_ERR();
249                 }
250                 rte_mempool_sp_put(mp_spsc, obj);
251                 rte_spinlock_lock(&scsp_spinlock);
252                 scsp_obj_table[i] = NULL;
253                 rte_spinlock_unlock(&scsp_spinlock);
254         }
255
256         return 0;
257 }
258
259 /*
260  * single consumer function
261  */
262 static int test_mempool_single_consumer(void)
263 {
264         unsigned int i;
265         void * obj;
266         uint64_t start_cycles, end_cycles;
267         uint64_t duration = rte_get_timer_hz() * 5;
268
269         start_cycles = rte_get_timer_cycles();
270         while (1) {
271                 end_cycles = rte_get_timer_cycles();
272                 /* duration uses up, stop consuming */
273                 if (start_cycles + duration < end_cycles)
274                         break;
275                 rte_spinlock_lock(&scsp_spinlock);
276                 for (i = 0; i < MAX_KEEP; i ++) {
277                         if (NULL == scsp_obj_table[i])
278                                 break;
279                 }
280                 rte_spinlock_unlock(&scsp_spinlock);
281                 if (i >= MAX_KEEP)
282                         continue;
283                 if (rte_mempool_sc_get(mp_spsc, &obj) < 0)
284                         break;
285                 rte_spinlock_lock(&scsp_spinlock);
286                 scsp_obj_table[i] = obj;
287                 rte_spinlock_unlock(&scsp_spinlock);
288         }
289
290         return 0;
291 }
292
293 /*
294  * test function for mempool test based on singple consumer and single producer,
295  * 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 (mp_spsc == NULL) {
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,
326                         MEMPOOL_F_NO_CACHE_ALIGN | MEMPOOL_F_SP_PUT |
327                         MEMPOOL_F_SC_GET);
328                 if (mp_spsc == NULL)
329                         RET_ERR();
330         }
331         if (rte_mempool_lookup("test_mempool_sp_sc") != mp_spsc) {
332                 printf("Cannot lookup mempool from its name\n");
333                 rte_mempool_free(mp_spsc);
334                 RET_ERR();
335         }
336         lcore_next = rte_get_next_lcore(lcore_id, 0, 1);
337         if (lcore_next >= RTE_MAX_LCORE) {
338                 rte_mempool_free(mp_spsc);
339                 RET_ERR();
340         }
341         if (rte_eal_lcore_role(lcore_next) != ROLE_RTE) {
342                 rte_mempool_free(mp_spsc);
343                 RET_ERR();
344         }
345         rte_spinlock_init(&scsp_spinlock);
346         memset(scsp_obj_table, 0, sizeof(scsp_obj_table));
347         rte_eal_remote_launch(test_mempool_launch_single_consumer, NULL,
348                 lcore_next);
349         if (test_mempool_single_producer() < 0)
350                 ret = -1;
351
352         if (rte_eal_wait_lcore(lcore_next) < 0)
353                 ret = -1;
354         rte_mempool_free(mp_spsc);
355
356         return ret;
357 }
358
359 /*
360  * it tests some more basic of mempool
361  */
362 static int
363 test_mempool_basic_ex(struct rte_mempool *mp)
364 {
365         unsigned i;
366         void **obj;
367         void *err_obj;
368         int ret = -1;
369
370         if (mp == NULL)
371                 return ret;
372
373         obj = rte_calloc("test_mempool_basic_ex", MEMPOOL_SIZE,
374                 sizeof(void *), 0);
375         if (obj == NULL) {
376                 printf("test_mempool_basic_ex fail to rte_malloc\n");
377                 return ret;
378         }
379         printf("test_mempool_basic_ex now mempool (%s) has %u free entries\n",
380                 mp->name, rte_mempool_free_count(mp));
381         if (rte_mempool_full(mp) != 1) {
382                 printf("test_mempool_basic_ex the mempool should be full\n");
383                 goto fail_mp_basic_ex;
384         }
385
386         for (i = 0; i < MEMPOOL_SIZE; i ++) {
387                 if (rte_mempool_mc_get(mp, &obj[i]) < 0) {
388                         printf("test_mp_basic_ex fail to get object for [%u]\n",
389                                 i);
390                         goto fail_mp_basic_ex;
391                 }
392         }
393         if (rte_mempool_mc_get(mp, &err_obj) == 0) {
394                 printf("test_mempool_basic_ex get an impossible obj\n");
395                 goto fail_mp_basic_ex;
396         }
397         printf("number: %u\n", i);
398         if (rte_mempool_empty(mp) != 1) {
399                 printf("test_mempool_basic_ex the mempool should be empty\n");
400                 goto fail_mp_basic_ex;
401         }
402
403         for (i = 0; i < MEMPOOL_SIZE; i++)
404                 rte_mempool_mp_put(mp, obj[i]);
405
406         if (rte_mempool_full(mp) != 1) {
407                 printf("test_mempool_basic_ex the mempool should be full\n");
408                 goto fail_mp_basic_ex;
409         }
410
411         ret = 0;
412
413 fail_mp_basic_ex:
414         if (obj != NULL)
415                 rte_free((void *)obj);
416
417         return ret;
418 }
419
420 static int
421 test_mempool_same_name_twice_creation(void)
422 {
423         struct rte_mempool *mp_tc, *mp_tc2;
424
425         mp_tc = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
426                 MEMPOOL_ELT_SIZE, 0, 0,
427                 NULL, NULL,
428                 NULL, NULL,
429                 SOCKET_ID_ANY, 0);
430
431         if (mp_tc == NULL)
432                 RET_ERR();
433
434         mp_tc2 = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
435                 MEMPOOL_ELT_SIZE, 0, 0,
436                 NULL, NULL,
437                 NULL, NULL,
438                 SOCKET_ID_ANY, 0);
439
440         if (mp_tc2 != NULL) {
441                 rte_mempool_free(mp_tc);
442                 rte_mempool_free(mp_tc2);
443                 RET_ERR();
444         }
445
446         rte_mempool_free(mp_tc);
447         return 0;
448 }
449
450 /*
451  * BAsic test for mempool_xmem functions.
452  */
453 static int
454 test_mempool_xmem_misc(void)
455 {
456         uint32_t elt_num, total_size;
457         size_t sz;
458         ssize_t usz;
459
460         elt_num = MAX_KEEP;
461         total_size = rte_mempool_calc_obj_size(MEMPOOL_ELT_SIZE, 0, NULL);
462         sz = rte_mempool_xmem_size(elt_num, total_size, MEMPOOL_PG_SHIFT_MAX);
463
464         usz = rte_mempool_xmem_usage(NULL, elt_num, total_size, 0, 1,
465                 MEMPOOL_PG_SHIFT_MAX);
466
467         if (sz != (size_t)usz)  {
468                 printf("failure @ %s: rte_mempool_xmem_usage(%u, %u) "
469                         "returns: %#zx, while expected: %#zx;\n",
470                         __func__, elt_num, total_size, sz, (size_t)usz);
471                 return -1;
472         }
473
474         return 0;
475 }
476
477 static int
478 test_mempool(void)
479 {
480         struct rte_mempool *mp_cache = NULL;
481         struct rte_mempool *mp_nocache = NULL;
482
483         rte_atomic32_init(&synchro);
484
485         /* create a mempool (without cache) */
486         mp_nocache = rte_mempool_create("test_nocache", MEMPOOL_SIZE,
487                 MEMPOOL_ELT_SIZE, 0, 0,
488                 NULL, NULL,
489                 my_obj_init, NULL,
490                 SOCKET_ID_ANY, 0);
491
492         if (mp_nocache == NULL) {
493                 printf("cannot allocate mp_nocache mempool\n");
494                 goto err;
495         }
496
497         /* create a mempool (with cache) */
498         mp_cache = rte_mempool_create("test_cache", MEMPOOL_SIZE,
499                 MEMPOOL_ELT_SIZE,
500                 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
501                 NULL, NULL,
502                 my_obj_init, NULL,
503                 SOCKET_ID_ANY, 0);
504
505         if (mp_cache == NULL) {
506                 printf("cannot allocate mp_cache mempool\n");
507                 goto err;
508         }
509
510         /* retrieve the mempool from its name */
511         if (rte_mempool_lookup("test_nocache") != mp_nocache) {
512                 printf("Cannot lookup mempool from its name\n");
513                 goto err;
514         }
515
516         rte_mempool_list_dump(stdout);
517
518         /* basic tests without cache */
519         if (test_mempool_basic(mp_nocache) < 0)
520                 goto err;
521
522         /* basic tests with cache */
523         if (test_mempool_basic(mp_cache) < 0)
524                 goto err;
525
526         /* more basic tests without cache */
527         if (test_mempool_basic_ex(mp_nocache) < 0)
528                 goto err;
529
530         /* mempool operation test based on single producer and single comsumer */
531         if (test_mempool_sp_sc() < 0)
532                 goto err;
533
534         if (test_mempool_creation_with_exceeded_cache_size() < 0)
535                 goto err;
536
537         if (test_mempool_same_name_twice_creation() < 0)
538                 goto err;
539
540         if (test_mempool_xmem_misc() < 0)
541                 goto err;
542
543         rte_mempool_list_dump(stdout);
544
545         return 0;
546
547 err:
548         rte_mempool_free(mp_nocache);
549         rte_mempool_free(mp_cache);
550         return -1;
551 }
552
553 static struct test_command mempool_cmd = {
554         .command = "mempool_autotest",
555         .callback = test_mempool,
556 };
557 REGISTER_TEST_COMMAND(mempool_cmd);