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