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