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