New upstream version 17.11.3
[deb_dpdk.git] / test / 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_launch.h>
48 #include <rte_cycles.h>
49 #include <rte_eal.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_atomic.h>
53 #include <rte_branch_prediction.h>
54 #include <rte_mempool.h>
55 #include <rte_spinlock.h>
56 #include <rte_malloc.h>
57
58 #include "test.h"
59
60 /*
61  * Mempool
62  * =======
63  *
64  * Basic tests: done on one core with and without cache:
65  *
66  *    - Get one object, put one object
67  *    - Get two objects, put two objects
68  *    - Get all objects, test that their content is not modified and
69  *      put them back in the pool.
70  */
71
72 #define MEMPOOL_ELT_SIZE 2048
73 #define MAX_KEEP 16
74 #define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
75
76 #define LOG_ERR() printf("test failed at %s():%d\n", __func__, __LINE__)
77 #define RET_ERR() do {                                                  \
78                 LOG_ERR();                                              \
79                 return -1;                                              \
80         } while (0)
81 #define GOTO_ERR(var, label) do {                                       \
82                 LOG_ERR();                                              \
83                 var = -1;                                               \
84                 goto label;                                             \
85         } while (0)
86
87 static rte_atomic32_t synchro;
88
89 /*
90  * save the object number in the first 4 bytes of object data. All
91  * other bytes are set to 0.
92  */
93 static void
94 my_obj_init(struct rte_mempool *mp, __attribute__((unused)) void *arg,
95             void *obj, unsigned i)
96 {
97         uint32_t *objnum = obj;
98
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(struct rte_mempool *mp, int use_external_cache)
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         int offset;
114         struct rte_mempool_cache *cache;
115
116         if (use_external_cache) {
117                 /* Create a user-owned mempool cache. */
118                 cache = rte_mempool_cache_create(RTE_MEMPOOL_CACHE_MAX_SIZE,
119                                                  SOCKET_ID_ANY);
120                 if (cache == NULL)
121                         RET_ERR();
122         } else {
123                 /* May be NULL if cache is disabled. */
124                 cache = rte_mempool_default_cache(mp, rte_lcore_id());
125         }
126
127         /* dump the mempool status */
128         rte_mempool_dump(stdout, mp);
129
130         printf("get an object\n");
131         if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
132                 GOTO_ERR(ret, out);
133         rte_mempool_dump(stdout, mp);
134
135         /* tests that improve coverage */
136         printf("get object count\n");
137         /* We have to count the extra caches, one in this case. */
138         offset = use_external_cache ? 1 * cache->len : 0;
139         if (rte_mempool_avail_count(mp) + offset != MEMPOOL_SIZE - 1)
140                 GOTO_ERR(ret, out);
141
142         printf("get private data\n");
143         if (rte_mempool_get_priv(mp) != (char *)mp +
144                         MEMPOOL_HEADER_SIZE(mp, mp->cache_size))
145                 GOTO_ERR(ret, out);
146
147 #ifndef RTE_EXEC_ENV_BSDAPP /* rte_mem_virt2iova() not supported on bsd */
148         printf("get physical address of an object\n");
149         if (rte_mempool_virt2iova(obj) != rte_mem_virt2iova(obj))
150                 GOTO_ERR(ret, out);
151 #endif
152
153         printf("put the object back\n");
154         rte_mempool_generic_put(mp, &obj, 1, cache);
155         rte_mempool_dump(stdout, mp);
156
157         printf("get 2 objects\n");
158         if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
159                 GOTO_ERR(ret, out);
160         if (rte_mempool_generic_get(mp, &obj2, 1, cache) < 0) {
161                 rte_mempool_generic_put(mp, &obj, 1, cache);
162                 GOTO_ERR(ret, out);
163         }
164         rte_mempool_dump(stdout, mp);
165
166         printf("put the objects back\n");
167         rte_mempool_generic_put(mp, &obj, 1, cache);
168         rte_mempool_generic_put(mp, &obj2, 1, cache);
169         rte_mempool_dump(stdout, mp);
170
171         /*
172          * get many objects: we cannot get them all because the cache
173          * on other cores may not be empty.
174          */
175         objtable = malloc(MEMPOOL_SIZE * sizeof(void *));
176         if (objtable == NULL)
177                 GOTO_ERR(ret, out);
178
179         for (i = 0; i < MEMPOOL_SIZE; i++) {
180                 if (rte_mempool_generic_get(mp, &objtable[i], 1, cache) < 0)
181                         break;
182         }
183
184         /*
185          * for each object, check that its content was not modified,
186          * and put objects back in pool
187          */
188         while (i--) {
189                 obj = objtable[i];
190                 obj_data = obj;
191                 objnum = obj;
192                 if (*objnum > MEMPOOL_SIZE) {
193                         printf("bad object number(%d)\n", *objnum);
194                         ret = -1;
195                         break;
196                 }
197                 for (j = sizeof(*objnum); j < mp->elt_size; j++) {
198                         if (obj_data[j] != 0)
199                                 ret = -1;
200                 }
201
202                 rte_mempool_generic_put(mp, &objtable[i], 1, cache);
203         }
204
205         free(objtable);
206         if (ret == -1)
207                 printf("objects were modified!\n");
208
209 out:
210         if (use_external_cache) {
211                 rte_mempool_cache_flush(cache, mp);
212                 rte_mempool_cache_free(cache);
213         }
214
215         return ret;
216 }
217
218 static int test_mempool_creation_with_exceeded_cache_size(void)
219 {
220         struct rte_mempool *mp_cov;
221
222         mp_cov = rte_mempool_create("test_mempool_cache_too_big",
223                 MEMPOOL_SIZE,
224                 MEMPOOL_ELT_SIZE,
225                 RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0,
226                 NULL, NULL,
227                 my_obj_init, NULL,
228                 SOCKET_ID_ANY, 0);
229
230         if (mp_cov != NULL) {
231                 rte_mempool_free(mp_cov);
232                 RET_ERR();
233         }
234
235         return 0;
236 }
237
238 static struct rte_mempool *mp_spsc;
239 static rte_spinlock_t scsp_spinlock;
240 static void *scsp_obj_table[MAX_KEEP];
241
242 /*
243  * single producer function
244  */
245 static int test_mempool_single_producer(void)
246 {
247         unsigned int i;
248         void *obj = NULL;
249         uint64_t start_cycles, end_cycles;
250         uint64_t duration = rte_get_timer_hz() / 4;
251
252         start_cycles = rte_get_timer_cycles();
253         while (1) {
254                 end_cycles = rte_get_timer_cycles();
255                 /* duration uses up, stop producing */
256                 if (start_cycles + duration < end_cycles)
257                         break;
258                 rte_spinlock_lock(&scsp_spinlock);
259                 for (i = 0; i < MAX_KEEP; i ++) {
260                         if (NULL != scsp_obj_table[i]) {
261                                 obj = scsp_obj_table[i];
262                                 break;
263                         }
264                 }
265                 rte_spinlock_unlock(&scsp_spinlock);
266                 if (i >= MAX_KEEP) {
267                         continue;
268                 }
269                 if (rte_mempool_from_obj(obj) != mp_spsc) {
270                         printf("obj not owned by this mempool\n");
271                         RET_ERR();
272                 }
273                 rte_mempool_put(mp_spsc, obj);
274                 rte_spinlock_lock(&scsp_spinlock);
275                 scsp_obj_table[i] = NULL;
276                 rte_spinlock_unlock(&scsp_spinlock);
277         }
278
279         return 0;
280 }
281
282 /*
283  * single consumer function
284  */
285 static int test_mempool_single_consumer(void)
286 {
287         unsigned int i;
288         void * obj;
289         uint64_t start_cycles, end_cycles;
290         uint64_t duration = rte_get_timer_hz() / 8;
291
292         start_cycles = rte_get_timer_cycles();
293         while (1) {
294                 end_cycles = rte_get_timer_cycles();
295                 /* duration uses up, stop consuming */
296                 if (start_cycles + duration < end_cycles)
297                         break;
298                 rte_spinlock_lock(&scsp_spinlock);
299                 for (i = 0; i < MAX_KEEP; i ++) {
300                         if (NULL == scsp_obj_table[i])
301                                 break;
302                 }
303                 rte_spinlock_unlock(&scsp_spinlock);
304                 if (i >= MAX_KEEP)
305                         continue;
306                 if (rte_mempool_get(mp_spsc, &obj) < 0)
307                         break;
308                 rte_spinlock_lock(&scsp_spinlock);
309                 scsp_obj_table[i] = obj;
310                 rte_spinlock_unlock(&scsp_spinlock);
311         }
312
313         return 0;
314 }
315
316 /*
317  * test function for mempool test based on singple consumer and single producer,
318  * can run on one lcore only
319  */
320 static int
321 test_mempool_launch_single_consumer(__attribute__((unused)) void *arg)
322 {
323         return test_mempool_single_consumer();
324 }
325
326 static void
327 my_mp_init(struct rte_mempool *mp, __attribute__((unused)) void *arg)
328 {
329         printf("mempool name is %s\n", mp->name);
330         /* nothing to be implemented here*/
331         return ;
332 }
333
334 /*
335  * it tests the mempool operations based on singple producer and single consumer
336  */
337 static int
338 test_mempool_sp_sc(void)
339 {
340         int ret = 0;
341         unsigned lcore_id = rte_lcore_id();
342         unsigned lcore_next;
343
344         /* create a mempool with single producer/consumer ring */
345         if (mp_spsc == NULL) {
346                 mp_spsc = rte_mempool_create("test_mempool_sp_sc", MEMPOOL_SIZE,
347                         MEMPOOL_ELT_SIZE, 0, 0,
348                         my_mp_init, NULL,
349                         my_obj_init, NULL,
350                         SOCKET_ID_ANY,
351                         MEMPOOL_F_NO_CACHE_ALIGN | MEMPOOL_F_SP_PUT |
352                         MEMPOOL_F_SC_GET);
353                 if (mp_spsc == NULL)
354                         RET_ERR();
355         }
356         if (rte_mempool_lookup("test_mempool_sp_sc") != mp_spsc) {
357                 printf("Cannot lookup mempool from its name\n");
358                 ret = -1;
359                 goto err;
360         }
361         lcore_next = rte_get_next_lcore(lcore_id, 0, 1);
362         if (lcore_next >= RTE_MAX_LCORE) {
363                 ret = -1;
364                 goto err;
365         }
366         if (rte_eal_lcore_role(lcore_next) != ROLE_RTE) {
367                 ret = -1;
368                 goto err;
369         }
370         rte_spinlock_init(&scsp_spinlock);
371         memset(scsp_obj_table, 0, sizeof(scsp_obj_table));
372         rte_eal_remote_launch(test_mempool_launch_single_consumer, NULL,
373                 lcore_next);
374         if (test_mempool_single_producer() < 0)
375                 ret = -1;
376
377         if (rte_eal_wait_lcore(lcore_next) < 0)
378                 ret = -1;
379
380 err:
381         rte_mempool_free(mp_spsc);
382         mp_spsc = NULL;
383
384         return ret;
385 }
386
387 /*
388  * it tests some more basic of mempool
389  */
390 static int
391 test_mempool_basic_ex(struct rte_mempool *mp)
392 {
393         unsigned i;
394         void **obj;
395         void *err_obj;
396         int ret = -1;
397
398         if (mp == NULL)
399                 return ret;
400
401         obj = rte_calloc("test_mempool_basic_ex", MEMPOOL_SIZE,
402                 sizeof(void *), 0);
403         if (obj == NULL) {
404                 printf("test_mempool_basic_ex fail to rte_malloc\n");
405                 return ret;
406         }
407         printf("test_mempool_basic_ex now mempool (%s) has %u free entries\n",
408                 mp->name, rte_mempool_in_use_count(mp));
409         if (rte_mempool_full(mp) != 1) {
410                 printf("test_mempool_basic_ex the mempool should be full\n");
411                 goto fail_mp_basic_ex;
412         }
413
414         for (i = 0; i < MEMPOOL_SIZE; i ++) {
415                 if (rte_mempool_get(mp, &obj[i]) < 0) {
416                         printf("test_mp_basic_ex fail to get object for [%u]\n",
417                                 i);
418                         goto fail_mp_basic_ex;
419                 }
420         }
421         if (rte_mempool_get(mp, &err_obj) == 0) {
422                 printf("test_mempool_basic_ex get an impossible obj\n");
423                 goto fail_mp_basic_ex;
424         }
425         printf("number: %u\n", i);
426         if (rte_mempool_empty(mp) != 1) {
427                 printf("test_mempool_basic_ex the mempool should be empty\n");
428                 goto fail_mp_basic_ex;
429         }
430
431         for (i = 0; i < MEMPOOL_SIZE; i++)
432                 rte_mempool_put(mp, obj[i]);
433
434         if (rte_mempool_full(mp) != 1) {
435                 printf("test_mempool_basic_ex the mempool should be full\n");
436                 goto fail_mp_basic_ex;
437         }
438
439         ret = 0;
440
441 fail_mp_basic_ex:
442         if (obj != NULL)
443                 rte_free((void *)obj);
444
445         return ret;
446 }
447
448 static int
449 test_mempool_same_name_twice_creation(void)
450 {
451         struct rte_mempool *mp_tc, *mp_tc2;
452
453         mp_tc = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
454                 MEMPOOL_ELT_SIZE, 0, 0,
455                 NULL, NULL,
456                 NULL, NULL,
457                 SOCKET_ID_ANY, 0);
458
459         if (mp_tc == NULL)
460                 RET_ERR();
461
462         mp_tc2 = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
463                 MEMPOOL_ELT_SIZE, 0, 0,
464                 NULL, NULL,
465                 NULL, NULL,
466                 SOCKET_ID_ANY, 0);
467
468         if (mp_tc2 != NULL) {
469                 rte_mempool_free(mp_tc);
470                 rte_mempool_free(mp_tc2);
471                 RET_ERR();
472         }
473
474         rte_mempool_free(mp_tc);
475         return 0;
476 }
477
478 /*
479  * Basic test for mempool_xmem functions.
480  */
481 static int
482 test_mempool_xmem_misc(void)
483 {
484         uint32_t elt_num, total_size;
485         size_t sz;
486         ssize_t usz;
487
488         elt_num = MAX_KEEP;
489         total_size = rte_mempool_calc_obj_size(MEMPOOL_ELT_SIZE, 0, NULL);
490         sz = rte_mempool_xmem_size(elt_num, total_size, MEMPOOL_PG_SHIFT_MAX,
491                                         0);
492
493         usz = rte_mempool_xmem_usage(NULL, elt_num, total_size, 0, 1,
494                 MEMPOOL_PG_SHIFT_MAX, 0);
495
496         if (sz != (size_t)usz)  {
497                 printf("failure @ %s: rte_mempool_xmem_usage(%u, %u) "
498                         "returns: %#zx, while expected: %#zx;\n",
499                         __func__, elt_num, total_size, sz, (size_t)usz);
500                 return -1;
501         }
502
503         return 0;
504 }
505
506 static void
507 walk_cb(struct rte_mempool *mp, void *userdata __rte_unused)
508 {
509         printf("\t%s\n", mp->name);
510 }
511
512 static int
513 test_mempool(void)
514 {
515         int ret = -1;
516         struct rte_mempool *mp_cache = NULL;
517         struct rte_mempool *mp_nocache = NULL;
518         struct rte_mempool *mp_stack = NULL;
519         struct rte_mempool *default_pool = NULL;
520
521         rte_atomic32_init(&synchro);
522
523         /* create a mempool (without cache) */
524         mp_nocache = rte_mempool_create("test_nocache", MEMPOOL_SIZE,
525                 MEMPOOL_ELT_SIZE, 0, 0,
526                 NULL, NULL,
527                 my_obj_init, NULL,
528                 SOCKET_ID_ANY, 0);
529
530         if (mp_nocache == NULL) {
531                 printf("cannot allocate mp_nocache mempool\n");
532                 goto err;
533         }
534
535         /* create a mempool (with cache) */
536         mp_cache = rte_mempool_create("test_cache", MEMPOOL_SIZE,
537                 MEMPOOL_ELT_SIZE,
538                 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
539                 NULL, NULL,
540                 my_obj_init, NULL,
541                 SOCKET_ID_ANY, 0);
542
543         if (mp_cache == NULL) {
544                 printf("cannot allocate mp_cache mempool\n");
545                 goto err;
546         }
547
548         /* create a mempool with an external handler */
549         mp_stack = rte_mempool_create_empty("test_stack",
550                 MEMPOOL_SIZE,
551                 MEMPOOL_ELT_SIZE,
552                 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
553                 SOCKET_ID_ANY, 0);
554
555         if (mp_stack == NULL) {
556                 printf("cannot allocate mp_stack mempool\n");
557                 goto err;
558         }
559         if (rte_mempool_set_ops_byname(mp_stack, "stack", NULL) < 0) {
560                 printf("cannot set stack handler\n");
561                 goto err;
562         }
563         if (rte_mempool_populate_default(mp_stack) < 0) {
564                 printf("cannot populate mp_stack mempool\n");
565                 goto err;
566         }
567         rte_mempool_obj_iter(mp_stack, my_obj_init, NULL);
568
569         /* Create a mempool based on Default handler */
570         printf("Testing %s mempool handler\n",
571                RTE_MBUF_DEFAULT_MEMPOOL_OPS);
572         default_pool = rte_mempool_create_empty("default_pool",
573                                                 MEMPOOL_SIZE,
574                                                 MEMPOOL_ELT_SIZE,
575                                                 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
576                                                 SOCKET_ID_ANY, 0);
577
578         if (default_pool == NULL) {
579                 printf("cannot allocate default mempool\n");
580                 goto err;
581         }
582         if (rte_mempool_set_ops_byname(default_pool,
583                                 RTE_MBUF_DEFAULT_MEMPOOL_OPS, NULL) < 0) {
584                 printf("cannot set %s handler\n",
585                         RTE_MBUF_DEFAULT_MEMPOOL_OPS);
586                 goto err;
587         }
588         if (rte_mempool_populate_default(default_pool) < 0) {
589                 printf("cannot populate %s mempool\n",
590                         RTE_MBUF_DEFAULT_MEMPOOL_OPS);
591                 goto err;
592         }
593         rte_mempool_obj_iter(default_pool, my_obj_init, NULL);
594
595         /* retrieve the mempool from its name */
596         if (rte_mempool_lookup("test_nocache") != mp_nocache) {
597                 printf("Cannot lookup mempool from its name\n");
598                 goto err;
599         }
600
601         printf("Walk into mempools:\n");
602         rte_mempool_walk(walk_cb, NULL);
603
604         rte_mempool_list_dump(stdout);
605
606         /* basic tests without cache */
607         if (test_mempool_basic(mp_nocache, 0) < 0)
608                 goto err;
609
610         /* basic tests with cache */
611         if (test_mempool_basic(mp_cache, 0) < 0)
612                 goto err;
613
614         /* basic tests with user-owned cache */
615         if (test_mempool_basic(mp_nocache, 1) < 0)
616                 goto err;
617
618         /* more basic tests without cache */
619         if (test_mempool_basic_ex(mp_nocache) < 0)
620                 goto err;
621
622         /* mempool operation test based on single producer and single comsumer */
623         if (test_mempool_sp_sc() < 0)
624                 goto err;
625
626         if (test_mempool_creation_with_exceeded_cache_size() < 0)
627                 goto err;
628
629         if (test_mempool_same_name_twice_creation() < 0)
630                 goto err;
631
632         if (test_mempool_xmem_misc() < 0)
633                 goto err;
634
635         /* test the stack handler */
636         if (test_mempool_basic(mp_stack, 1) < 0)
637                 goto err;
638
639         if (test_mempool_basic(default_pool, 1) < 0)
640                 goto err;
641
642         rte_mempool_list_dump(stdout);
643
644         ret = 0;
645
646 err:
647         rte_mempool_free(mp_nocache);
648         rte_mempool_free(mp_cache);
649         rte_mempool_free(mp_stack);
650         rte_mempool_free(default_pool);
651
652         return ret;
653 }
654
655 REGISTER_TEST_COMMAND(mempool_autotest, test_mempool);