remove extra parentheses in return statement
[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 static struct rte_mempool *mp;
81 static struct rte_mempool *mp_cache, *mp_nocache;
82
83 static rte_atomic32_t synchro;
84
85
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         memset(obj, 0, mp->elt_size);
97         *objnum = i;
98 }
99
100 /* basic tests (done on one core) */
101 static int
102 test_mempool_basic(void)
103 {
104         uint32_t *objnum;
105         void **objtable;
106         void *obj, *obj2;
107         char *obj_data;
108         int ret = 0;
109         unsigned i, j;
110
111         /* dump the mempool status */
112         rte_mempool_dump(stdout, mp);
113
114         printf("get an object\n");
115         if (rte_mempool_get(mp, &obj) < 0)
116                 return -1;
117         rte_mempool_dump(stdout, mp);
118
119         /* tests that improve coverage */
120         printf("get object count\n");
121         if (rte_mempool_count(mp) != MEMPOOL_SIZE - 1)
122                 return -1;
123
124         printf("get private data\n");
125         if (rte_mempool_get_priv(mp) !=
126                         (char*) mp + MEMPOOL_HEADER_SIZE(mp, mp->pg_num))
127                 return -1;
128
129         printf("get physical address of an object\n");
130         if (MEMPOOL_IS_CONTIG(mp) &&
131                         rte_mempool_virt2phy(mp, obj) !=
132                         (phys_addr_t) (mp->phys_addr +
133                         (phys_addr_t) ((char*) obj - (char*) mp)))
134                 return -1;
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                 return -1;
143         if (rte_mempool_get(mp, &obj2) < 0) {
144                 rte_mempool_put(mp, obj);
145                 return -1;
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                 return -1;
161         }
162
163         for (i=0; i<MEMPOOL_SIZE; i++) {
164                 if (rte_mempool_get(mp, &objtable[i]) < 0)
165                         break;
166         }
167
168         /*
169          * for each object, check that its content was not modified,
170          * and put objects back in pool
171          */
172         while (i--) {
173                 obj = objtable[i];
174                 obj_data = obj;
175                 objnum = obj;
176                 if (*objnum > MEMPOOL_SIZE) {
177                         printf("bad object number\n");
178                         ret = -1;
179                         break;
180                 }
181                 for (j=sizeof(*objnum); j<mp->elt_size; j++) {
182                         if (obj_data[j] != 0)
183                                 ret = -1;
184                 }
185
186                 rte_mempool_put(mp, objtable[i]);
187         }
188
189         free(objtable);
190         if (ret == -1)
191                 printf("objects were modified!\n");
192
193         return ret;
194 }
195
196 static int test_mempool_creation_with_exceeded_cache_size(void)
197 {
198         struct rte_mempool *mp_cov;
199
200         mp_cov = rte_mempool_create("test_mempool_creation_with_exceeded_cache_size", 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         if(NULL != mp_cov) {
207                 return -1;
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() * 8;
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("test_mempool_single_producer there is an obj not owned by this mempool\n");
246                         return -1;
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() * 5;
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, can run on one lcore only
293  */
294 static int test_mempool_launch_single_consumer(__attribute__((unused)) void *arg)
295 {
296         return test_mempool_single_consumer();
297 }
298
299 static void 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 (NULL == mp_spsc) {
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, MEMPOOL_F_NO_CACHE_ALIGN | MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET);
323                 if (NULL == mp_spsc) {
324                         return -1;
325                 }
326         }
327         if (rte_mempool_lookup("test_mempool_sp_sc") != mp_spsc) {
328                 printf("Cannot lookup mempool from its name\n");
329                 return -1;
330         }
331         lcore_next = rte_get_next_lcore(lcore_id, 0, 1);
332         if (RTE_MAX_LCORE <= lcore_next)
333                 return -1;
334         if (rte_eal_lcore_role(lcore_next) != ROLE_RTE)
335                 return -1;
336         rte_spinlock_init(&scsp_spinlock);
337         memset(scsp_obj_table, 0, sizeof(scsp_obj_table));
338         rte_eal_remote_launch(test_mempool_launch_single_consumer, NULL, lcore_next);
339         if(test_mempool_single_producer() < 0)
340                 ret = -1;
341
342         if(rte_eal_wait_lcore(lcore_next) < 0)
343                 ret = -1;
344
345         return ret;
346 }
347
348 /*
349  * it tests some more basic of mempool
350  */
351 static int
352 test_mempool_basic_ex(struct rte_mempool * mp)
353 {
354         unsigned i;
355         void **obj;
356         void *err_obj;
357         int ret = -1;
358
359         if (mp == NULL)
360                 return ret;
361
362         obj = rte_calloc("test_mempool_basic_ex", MEMPOOL_SIZE , sizeof(void *), 0);
363         if (obj == NULL) {
364                 printf("test_mempool_basic_ex fail to rte_malloc\n");
365                 return ret;
366         }
367         printf("test_mempool_basic_ex now mempool (%s) has %u free entries\n", mp->name, rte_mempool_free_count(mp));
368         if (rte_mempool_full(mp) != 1) {
369                 printf("test_mempool_basic_ex the mempool is not full but it should be\n");
370                 goto fail_mp_basic_ex;
371         }
372
373         for (i = 0; i < MEMPOOL_SIZE; i ++) {
374                 if (rte_mempool_mc_get(mp, &obj[i]) < 0) {
375                         printf("fail_mp_basic_ex fail to get mempool object for [%u]\n", i);
376                         goto fail_mp_basic_ex;
377                 }
378         }
379         if (rte_mempool_mc_get(mp, &err_obj) == 0) {
380                 printf("test_mempool_basic_ex get an impossible obj from mempool\n");
381                 goto fail_mp_basic_ex;
382         }
383         printf("number: %u\n", i);
384         if (rte_mempool_empty(mp) != 1) {
385                 printf("test_mempool_basic_ex the mempool is not empty but it should be\n");
386                 goto fail_mp_basic_ex;
387         }
388
389         for (i = 0; i < MEMPOOL_SIZE; i ++) {
390                 rte_mempool_mp_put(mp, obj[i]);
391         }
392         if (rte_mempool_full(mp) != 1) {
393                 printf("test_mempool_basic_ex the mempool is not full but it should be\n");
394                 goto fail_mp_basic_ex;
395         }
396
397         ret = 0;
398
399 fail_mp_basic_ex:
400         if (obj != NULL)
401                 rte_free((void *)obj);
402
403         return ret;
404 }
405
406 static int
407 test_mempool_same_name_twice_creation(void)
408 {
409         struct rte_mempool *mp_tc;
410
411         mp_tc = rte_mempool_create("test_mempool_same_name_twice_creation", MEMPOOL_SIZE,
412                                                 MEMPOOL_ELT_SIZE, 0, 0,
413                                                 NULL, NULL,
414                                                 NULL, NULL,
415                                                 SOCKET_ID_ANY, 0);
416         if (NULL == mp_tc)
417                 return -1;
418
419         mp_tc = rte_mempool_create("test_mempool_same_name_twice_creation", MEMPOOL_SIZE,
420                                                 MEMPOOL_ELT_SIZE, 0, 0,
421                                                 NULL, NULL,
422                                                 NULL, NULL,
423                                                 SOCKET_ID_ANY, 0);
424         if (NULL != mp_tc)
425                 return -1;
426
427         return 0;
428 }
429
430 /*
431  * BAsic test for mempool_xmem functions.
432  */
433 static int
434 test_mempool_xmem_misc(void)
435 {
436         uint32_t elt_num, total_size;
437         size_t sz;
438         ssize_t usz;
439
440         elt_num = MAX_KEEP;
441         total_size = rte_mempool_calc_obj_size(MEMPOOL_ELT_SIZE, 0, NULL);
442         sz = rte_mempool_xmem_size(elt_num, total_size, MEMPOOL_PG_SHIFT_MAX);
443
444         usz = rte_mempool_xmem_usage(NULL, elt_num, total_size, 0, 1,
445                 MEMPOOL_PG_SHIFT_MAX);
446
447         if(sz != (size_t)usz)  {
448                 printf("failure @ %s: rte_mempool_xmem_usage(%u, %u) "
449                         "returns: %#zx, while expected: %#zx;\n",
450                         __func__, elt_num, total_size, sz, (size_t)usz);
451                 return -1;
452         }
453
454         return 0;
455 }
456
457 static int
458 test_mempool(void)
459 {
460         rte_atomic32_init(&synchro);
461
462         /* create a mempool (without cache) */
463         if (mp_nocache == NULL)
464                 mp_nocache = rte_mempool_create("test_nocache", MEMPOOL_SIZE,
465                                                 MEMPOOL_ELT_SIZE, 0, 0,
466                                                 NULL, NULL,
467                                                 my_obj_init, NULL,
468                                                 SOCKET_ID_ANY, 0);
469         if (mp_nocache == NULL)
470                 return -1;
471
472         /* create a mempool (with cache) */
473         if (mp_cache == NULL)
474                 mp_cache = rte_mempool_create("test_cache", MEMPOOL_SIZE,
475                                               MEMPOOL_ELT_SIZE,
476                                               RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
477                                               NULL, NULL,
478                                               my_obj_init, NULL,
479                                               SOCKET_ID_ANY, 0);
480         if (mp_cache == NULL)
481                 return -1;
482
483
484         /* retrieve the mempool from its name */
485         if (rte_mempool_lookup("test_nocache") != mp_nocache) {
486                 printf("Cannot lookup mempool from its name\n");
487                 return -1;
488         }
489
490         rte_mempool_list_dump(stdout);
491
492         /* basic tests without cache */
493         mp = mp_nocache;
494         if (test_mempool_basic() < 0)
495                 return -1;
496
497         /* basic tests with cache */
498         mp = mp_cache;
499         if (test_mempool_basic() < 0)
500                 return -1;
501
502         /* more basic tests without cache */
503         if (test_mempool_basic_ex(mp_nocache) < 0)
504                 return -1;
505
506         /* mempool operation test based on single producer and single comsumer */
507         if (test_mempool_sp_sc() < 0)
508                 return -1;
509
510         if (test_mempool_creation_with_exceeded_cache_size() < 0)
511                 return -1;
512
513         if (test_mempool_same_name_twice_creation() < 0)
514                 return -1;
515
516         if (test_mempool_xmem_misc() < 0)
517                 return -1;
518
519         rte_mempool_list_dump(stdout);
520
521         return 0;
522 }
523
524 static struct test_command mempool_cmd = {
525         .command = "mempool_autotest",
526         .callback = test_mempool,
527 };
528 REGISTER_TEST_COMMAND(mempool_cmd);