tailq: remove unused macros
[dpdk.git] / lib / librte_mempool / rte_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 <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 #include <inttypes.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_malloc.h>
49 #include <rte_atomic.h>
50 #include <rte_launch.h>
51 #include <rte_eal.h>
52 #include <rte_eal_memconfig.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_ring.h>
57 #include <rte_errno.h>
58 #include <rte_string_fns.h>
59 #include <rte_spinlock.h>
60
61 #include "rte_mempool.h"
62
63 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
64
65 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
66
67 /*
68  * return the greatest common divisor between a and b (fast algorithm)
69  *
70  */
71 static unsigned get_gcd(unsigned a, unsigned b)
72 {
73         unsigned c;
74
75         if (0 == a)
76                 return b;
77         if (0 == b)
78                 return a;
79
80         if (a < b) {
81                 c = a;
82                 a = b;
83                 b = c;
84         }
85
86         while (b != 0) {
87                 c = a % b;
88                 a = b;
89                 b = c;
90         }
91
92         return a;
93 }
94
95 /*
96  * Depending on memory configuration, objects addresses are spread
97  * between channels and ranks in RAM: the pool allocator will add
98  * padding between objects. This function return the new size of the
99  * object.
100  */
101 static unsigned optimize_object_size(unsigned obj_size)
102 {
103         unsigned nrank, nchan;
104         unsigned new_obj_size;
105
106         /* get number of channels */
107         nchan = rte_memory_get_nchannel();
108         if (nchan == 0)
109                 nchan = 1;
110
111         nrank = rte_memory_get_nrank();
112         if (nrank == 0)
113                 nrank = 1;
114
115         /* process new object size */
116         new_obj_size = (obj_size + RTE_CACHE_LINE_MASK) / RTE_CACHE_LINE_SIZE;
117         while (get_gcd(new_obj_size, nrank * nchan) != 1)
118                 new_obj_size++;
119         return new_obj_size * RTE_CACHE_LINE_SIZE;
120 }
121
122 static void
123 mempool_add_elem(struct rte_mempool *mp, void *obj, uint32_t obj_idx,
124         rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg)
125 {
126         struct rte_mempool **mpp;
127
128         obj = (char *)obj + mp->header_size;
129
130         /* set mempool ptr in header */
131         mpp = __mempool_from_obj(obj);
132         *mpp = mp;
133
134 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
135         __mempool_write_header_cookie(obj, 1);
136         __mempool_write_trailer_cookie(obj);
137 #endif
138         /* call the initializer */
139         if (obj_init)
140                 obj_init(mp, obj_init_arg, obj, obj_idx);
141
142         /* enqueue in ring */
143         rte_ring_sp_enqueue(mp->ring, obj);
144 }
145
146 uint32_t
147 rte_mempool_obj_iter(void *vaddr, uint32_t elt_num, size_t elt_sz, size_t align,
148         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
149         rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg)
150 {
151         uint32_t i, j, k;
152         uint32_t pgn;
153         uintptr_t end, start, va;
154         uintptr_t pg_sz;
155
156         pg_sz = (uintptr_t)1 << pg_shift;
157         va = (uintptr_t)vaddr;
158
159         i = 0;
160         j = 0;
161
162         while (i != elt_num && j != pg_num) {
163
164                 start = RTE_ALIGN_CEIL(va, align);
165                 end = start + elt_sz;
166
167                 pgn = (end >> pg_shift) - (start >> pg_shift);
168                 pgn += j;
169
170                 /* do we have enough space left for the next element. */
171                 if (pgn >= pg_num)
172                         break;
173
174                 for (k = j;
175                                 k != pgn &&
176                                 paddr[k] + pg_sz == paddr[k + 1];
177                                 k++)
178                         ;
179
180                 /*
181                  * if next pgn chunks of memory physically continuous,
182                  * use it to create next element.
183                  * otherwise, just skip that chunk unused.
184                  */
185                 if (k == pgn) {
186                         if (obj_iter != NULL)
187                                 obj_iter(obj_iter_arg, (void *)start,
188                                         (void *)end, i);
189                         va = end;
190                         j = pgn;
191                         i++;
192                 } else {
193                         va = RTE_ALIGN_CEIL((va + 1), pg_sz);
194                         j++;
195                 }
196         }
197
198         return (i);
199 }
200
201 /*
202  * Populate  mempool with the objects.
203  */
204
205 struct mempool_populate_arg {
206         struct rte_mempool     *mp;
207         rte_mempool_obj_ctor_t *obj_init;
208         void                   *obj_init_arg;
209 };
210
211 static void
212 mempool_obj_populate(void *arg, void *start, void *end, uint32_t idx)
213 {
214         struct mempool_populate_arg *pa = arg;
215
216         mempool_add_elem(pa->mp, start, idx, pa->obj_init, pa->obj_init_arg);
217         pa->mp->elt_va_end = (uintptr_t)end;
218 }
219
220 static void
221 mempool_populate(struct rte_mempool *mp, size_t num, size_t align,
222         rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg)
223 {
224         uint32_t elt_sz;
225         struct mempool_populate_arg arg;
226
227         elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
228         arg.mp = mp;
229         arg.obj_init = obj_init;
230         arg.obj_init_arg = obj_init_arg;
231
232         mp->size = rte_mempool_obj_iter((void *)mp->elt_va_start,
233                 num, elt_sz, align,
234                 mp->elt_pa, mp->pg_num, mp->pg_shift,
235                 mempool_obj_populate, &arg);
236 }
237
238 uint32_t
239 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
240         struct rte_mempool_objsz *sz)
241 {
242         struct rte_mempool_objsz lsz;
243
244         sz = (sz != NULL) ? sz : &lsz;
245
246         /*
247          * In header, we have at least the pointer to the pool, and
248          * optionaly a 64 bits cookie.
249          */
250         sz->header_size = 0;
251         sz->header_size += sizeof(struct rte_mempool *); /* ptr to pool */
252 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
253         sz->header_size += sizeof(uint64_t); /* cookie */
254 #endif
255         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
256                 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
257                         RTE_CACHE_LINE_SIZE);
258
259         /* trailer contains the cookie in debug mode */
260         sz->trailer_size = 0;
261 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
262         sz->trailer_size += sizeof(uint64_t); /* cookie */
263 #endif
264         /* element size is 8 bytes-aligned at least */
265         sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
266
267         /* expand trailer to next cache line */
268         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
269                 sz->total_size = sz->header_size + sz->elt_size +
270                         sz->trailer_size;
271                 sz->trailer_size += ((RTE_CACHE_LINE_SIZE -
272                                   (sz->total_size & RTE_CACHE_LINE_MASK)) &
273                                  RTE_CACHE_LINE_MASK);
274         }
275
276         /*
277          * increase trailer to add padding between objects in order to
278          * spread them across memory channels/ranks
279          */
280         if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
281                 unsigned new_size;
282                 new_size = optimize_object_size(sz->header_size + sz->elt_size +
283                         sz->trailer_size);
284                 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
285         }
286
287         if (! rte_eal_has_hugepages()) {
288                 /*
289                  * compute trailer size so that pool elements fit exactly in
290                  * a standard page
291                  */
292                 int page_size = getpagesize();
293                 int new_size = page_size - sz->header_size - sz->elt_size;
294                 if (new_size < 0 || (unsigned int)new_size < sz->trailer_size) {
295                         printf("When hugepages are disabled, pool objects "
296                                "can't exceed PAGE_SIZE: %d + %d + %d > %d\n",
297                                sz->header_size, sz->elt_size, sz->trailer_size,
298                                page_size);
299                         return 0;
300                 }
301                 sz->trailer_size = new_size;
302         }
303
304         /* this is the size of an object, including header and trailer */
305         sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
306
307         return (sz->total_size);
308 }
309
310
311 /*
312  * Calculate maximum amount of memory required to store given number of objects.
313  */
314 size_t
315 rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz, uint32_t pg_shift)
316 {
317         size_t n, pg_num, pg_sz, sz;
318
319         pg_sz = (size_t)1 << pg_shift;
320
321         if ((n = pg_sz / elt_sz) > 0) {
322                 pg_num = (elt_num + n - 1) / n;
323                 sz = pg_num << pg_shift;
324         } else {
325                 sz = RTE_ALIGN_CEIL(elt_sz, pg_sz) * elt_num;
326         }
327
328         return (sz);
329 }
330
331 /*
332  * Calculate how much memory would be actually required with the
333  * given memory footprint to store required number of elements.
334  */
335 static void
336 mempool_lelem_iter(void *arg, __rte_unused void *start, void *end,
337         __rte_unused uint32_t idx)
338 {
339         *(uintptr_t *)arg = (uintptr_t)end;
340 }
341
342 ssize_t
343 rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
344         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
345 {
346         uint32_t n;
347         uintptr_t va, uv;
348         size_t pg_sz, usz;
349
350         pg_sz = (size_t)1 << pg_shift;
351         va = (uintptr_t)vaddr;
352         uv = va;
353
354         if ((n = rte_mempool_obj_iter(vaddr, elt_num, elt_sz, 1,
355                         paddr, pg_num, pg_shift, mempool_lelem_iter,
356                         &uv)) != elt_num) {
357                 return (-n);
358         }
359
360         uv = RTE_ALIGN_CEIL(uv, pg_sz);
361         usz = uv - va;
362         return (usz);
363 }
364
365 /* create the mempool */
366 struct rte_mempool *
367 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
368                    unsigned cache_size, unsigned private_data_size,
369                    rte_mempool_ctor_t *mp_init, void *mp_init_arg,
370                    rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
371                    int socket_id, unsigned flags)
372 {
373 #ifdef RTE_LIBRTE_XEN_DOM0
374         return (rte_dom0_mempool_create(name, n, elt_size,
375                 cache_size, private_data_size,
376                 mp_init, mp_init_arg,
377                 obj_init, obj_init_arg,
378                 socket_id, flags));
379 #else
380         return (rte_mempool_xmem_create(name, n, elt_size,
381                 cache_size, private_data_size,
382                 mp_init, mp_init_arg,
383                 obj_init, obj_init_arg,
384                 socket_id, flags,
385                 NULL, NULL, MEMPOOL_PG_NUM_DEFAULT, MEMPOOL_PG_SHIFT_MAX));
386 #endif
387 }
388
389 /*
390  * Create the mempool over already allocated chunk of memory.
391  * That external memory buffer can consists of physically disjoint pages.
392  * Setting vaddr to NULL, makes mempool to fallback to original behaviour
393  * and allocate space for mempool and it's elements as one big chunk of
394  * physically continuos memory.
395  * */
396 struct rte_mempool *
397 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
398                 unsigned cache_size, unsigned private_data_size,
399                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
400                 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
401                 int socket_id, unsigned flags, void *vaddr,
402                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
403 {
404         char mz_name[RTE_MEMZONE_NAMESIZE];
405         char rg_name[RTE_RING_NAMESIZE];
406         struct rte_mempool_list *mempool_list;
407         struct rte_mempool *mp = NULL;
408         struct rte_tailq_entry *te;
409         struct rte_ring *r;
410         const struct rte_memzone *mz;
411         size_t mempool_size;
412         int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
413         int rg_flags = 0;
414         void *obj;
415         struct rte_mempool_objsz objsz;
416         void *startaddr;
417         int page_size = getpagesize();
418
419         /* compilation-time checks */
420         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
421                           RTE_CACHE_LINE_MASK) != 0);
422 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
423         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
424                           RTE_CACHE_LINE_MASK) != 0);
425         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, local_cache) &
426                           RTE_CACHE_LINE_MASK) != 0);
427 #endif
428 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
429         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
430                           RTE_CACHE_LINE_MASK) != 0);
431         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
432                           RTE_CACHE_LINE_MASK) != 0);
433 #endif
434
435         /* check that we have an initialised tail queue */
436         mempool_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL,
437                                                rte_mempool_list);
438         if (mempool_list == NULL) {
439                 rte_errno = E_RTE_NO_TAILQ;
440                 return NULL;
441         }
442
443         /* asked cache too big */
444         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
445                 rte_errno = EINVAL;
446                 return NULL;
447         }
448
449         /* check that we have both VA and PA */
450         if (vaddr != NULL && paddr == NULL) {
451                 rte_errno = EINVAL;
452                 return NULL;
453         }
454
455         /* Check that pg_num and pg_shift parameters are valid. */
456         if (pg_num < RTE_DIM(mp->elt_pa) || pg_shift > MEMPOOL_PG_SHIFT_MAX) {
457                 rte_errno = EINVAL;
458                 return NULL;
459         }
460
461         /* "no cache align" imply "no spread" */
462         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
463                 flags |= MEMPOOL_F_NO_SPREAD;
464
465         /* ring flags */
466         if (flags & MEMPOOL_F_SP_PUT)
467                 rg_flags |= RING_F_SP_ENQ;
468         if (flags & MEMPOOL_F_SC_GET)
469                 rg_flags |= RING_F_SC_DEQ;
470
471         /* calculate mempool object sizes. */
472         if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
473                 rte_errno = EINVAL;
474                 return NULL;
475         }
476
477         rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
478
479         /* allocate the ring that will be used to store objects */
480         /* Ring functions will return appropriate errors if we are
481          * running as a secondary process etc., so no checks made
482          * in this function for that condition */
483         snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
484         r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
485         if (r == NULL)
486                 goto exit;
487
488         /*
489          * reserve a memory zone for this mempool: private data is
490          * cache-aligned
491          */
492         private_data_size = (private_data_size +
493                              RTE_CACHE_LINE_MASK) & (~RTE_CACHE_LINE_MASK);
494
495         if (! rte_eal_has_hugepages()) {
496                 /*
497                  * expand private data size to a whole page, so that the
498                  * first pool element will start on a new standard page
499                  */
500                 int head = sizeof(struct rte_mempool);
501                 int new_size = (private_data_size + head) % page_size;
502                 if (new_size) {
503                         private_data_size += page_size - new_size;
504                 }
505         }
506
507         /* try to allocate tailq entry */
508         te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
509         if (te == NULL) {
510                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
511                 goto exit;
512         }
513
514         /*
515          * If user provided an external memory buffer, then use it to
516          * store mempool objects. Otherwise reserve memzone big enough to
517          * hold mempool header and metadata plus mempool objects.
518          */
519         mempool_size = MEMPOOL_HEADER_SIZE(mp, pg_num) + private_data_size;
520         if (vaddr == NULL)
521                 mempool_size += (size_t)objsz.total_size * n;
522
523         if (! rte_eal_has_hugepages()) {
524                 /*
525                  * we want the memory pool to start on a page boundary,
526                  * because pool elements crossing page boundaries would
527                  * result in discontiguous physical addresses
528                  */
529                 mempool_size += page_size;
530         }
531
532         snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
533
534         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
535
536         /*
537          * no more memory: in this case we loose previously reserved
538          * space for the as we cannot free it
539          */
540         if (mz == NULL) {
541                 rte_free(te);
542                 goto exit;
543         }
544
545         if (rte_eal_has_hugepages()) {
546                 startaddr = (void*)mz->addr;
547         } else {
548                 /* align memory pool start address on a page boundary */
549                 unsigned long addr = (unsigned long)mz->addr;
550                 if (addr & (page_size - 1)) {
551                         addr += page_size;
552                         addr &= ~(page_size - 1);
553                 }
554                 startaddr = (void*)addr;
555         }
556
557         /* init the mempool structure */
558         mp = startaddr;
559         memset(mp, 0, sizeof(*mp));
560         snprintf(mp->name, sizeof(mp->name), "%s", name);
561         mp->phys_addr = mz->phys_addr;
562         mp->ring = r;
563         mp->size = n;
564         mp->flags = flags;
565         mp->elt_size = objsz.elt_size;
566         mp->header_size = objsz.header_size;
567         mp->trailer_size = objsz.trailer_size;
568         mp->cache_size = cache_size;
569         mp->cache_flushthresh = (uint32_t)
570                 (cache_size * CACHE_FLUSHTHRESH_MULTIPLIER);
571         mp->private_data_size = private_data_size;
572
573         /* calculate address of the first element for continuous mempool. */
574         obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num) +
575                 private_data_size;
576
577         /* populate address translation fields. */
578         mp->pg_num = pg_num;
579         mp->pg_shift = pg_shift;
580         mp->pg_mask = RTE_LEN2MASK(mp->pg_shift, typeof(mp->pg_mask));
581
582         /* mempool elements allocated together with mempool */
583         if (vaddr == NULL) {
584                 mp->elt_va_start = (uintptr_t)obj;
585                 mp->elt_pa[0] = mp->phys_addr +
586                         (mp->elt_va_start - (uintptr_t)mp);
587
588         /* mempool elements in a separate chunk of memory. */
589         } else {
590                 mp->elt_va_start = (uintptr_t)vaddr;
591                 memcpy(mp->elt_pa, paddr, sizeof (mp->elt_pa[0]) * pg_num);
592         }
593
594         mp->elt_va_end = mp->elt_va_start;
595
596         /* call the initializer */
597         if (mp_init)
598                 mp_init(mp, mp_init_arg);
599
600         mempool_populate(mp, n, 1, obj_init, obj_init_arg);
601
602         te->data = (void *) mp;
603
604         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
605         TAILQ_INSERT_TAIL(mempool_list, te, next);
606         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
607
608 exit:
609         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
610
611         return mp;
612 }
613
614 /* Return the number of entries in the mempool */
615 unsigned
616 rte_mempool_count(const struct rte_mempool *mp)
617 {
618         unsigned count;
619
620         count = rte_ring_count(mp->ring);
621
622 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
623         {
624                 unsigned lcore_id;
625                 if (mp->cache_size == 0)
626                         return count;
627
628                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
629                         count += mp->local_cache[lcore_id].len;
630         }
631 #endif
632
633         /*
634          * due to race condition (access to len is not locked), the
635          * total can be greater than size... so fix the result
636          */
637         if (count > mp->size)
638                 return mp->size;
639         return count;
640 }
641
642 /* dump the cache status */
643 static unsigned
644 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
645 {
646 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
647         unsigned lcore_id;
648         unsigned count = 0;
649         unsigned cache_count;
650
651         fprintf(f, "  cache infos:\n");
652         fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
653         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
654                 cache_count = mp->local_cache[lcore_id].len;
655                 fprintf(f, "    cache_count[%u]=%u\n", lcore_id, cache_count);
656                 count += cache_count;
657         }
658         fprintf(f, "    total_cache_count=%u\n", count);
659         return count;
660 #else
661         RTE_SET_USED(mp);
662         fprintf(f, "  cache disabled\n");
663         return 0;
664 #endif
665 }
666
667 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
668 /* check cookies before and after objects */
669 #ifndef __INTEL_COMPILER
670 #pragma GCC diagnostic ignored "-Wcast-qual"
671 #endif
672
673 struct mempool_audit_arg {
674         const struct rte_mempool *mp;
675         uintptr_t obj_end;
676         uint32_t obj_num;
677 };
678
679 static void
680 mempool_obj_audit(void *arg, void *start, void *end, uint32_t idx)
681 {
682         struct mempool_audit_arg *pa = arg;
683         void *obj;
684
685         obj = (char *)start + pa->mp->header_size;
686         pa->obj_end = (uintptr_t)end;
687         pa->obj_num = idx + 1;
688         __mempool_check_cookies(pa->mp, &obj, 1, 2);
689 }
690
691 static void
692 mempool_audit_cookies(const struct rte_mempool *mp)
693 {
694         uint32_t elt_sz, num;
695         struct mempool_audit_arg arg;
696
697         elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
698
699         arg.mp = mp;
700         arg.obj_end = mp->elt_va_start;
701         arg.obj_num = 0;
702
703         num = rte_mempool_obj_iter((void *)mp->elt_va_start,
704                 mp->size, elt_sz, 1,
705                 mp->elt_pa, mp->pg_num, mp->pg_shift,
706                 mempool_obj_audit, &arg);
707
708         if (num != mp->size) {
709                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
710                         "iterated only over %u elements\n",
711                         mp, mp->size, num);
712         } else if (arg.obj_end != mp->elt_va_end || arg.obj_num != mp->size) {
713                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
714                         "last callback va_end: %#tx (%#tx expeceted), "
715                         "num of objects: %u (%u expected)\n",
716                         mp, mp->size,
717                         arg.obj_end, mp->elt_va_end,
718                         arg.obj_num, mp->size);
719         }
720 }
721
722 #ifndef __INTEL_COMPILER
723 #pragma GCC diagnostic error "-Wcast-qual"
724 #endif
725 #else
726 #define mempool_audit_cookies(mp) do {} while(0)
727 #endif
728
729 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
730 /* check cookies before and after objects */
731 static void
732 mempool_audit_cache(const struct rte_mempool *mp)
733 {
734         /* check cache size consistency */
735         unsigned lcore_id;
736         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
737                 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
738                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
739                                 lcore_id);
740                         rte_panic("MEMPOOL: invalid cache len\n");
741                 }
742         }
743 }
744 #else
745 #define mempool_audit_cache(mp) do {} while(0)
746 #endif
747
748
749 /* check the consistency of mempool (size, cookies, ...) */
750 void
751 rte_mempool_audit(const struct rte_mempool *mp)
752 {
753         mempool_audit_cache(mp);
754         mempool_audit_cookies(mp);
755
756         /* For case where mempool DEBUG is not set, and cache size is 0 */
757         RTE_SET_USED(mp);
758 }
759
760 /* dump the status of the mempool on the console */
761 void
762 rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
763 {
764 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
765         struct rte_mempool_debug_stats sum;
766         unsigned lcore_id;
767 #endif
768         unsigned common_count;
769         unsigned cache_count;
770
771         RTE_VERIFY(f != NULL);
772         RTE_VERIFY(mp != NULL);
773
774         fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
775         fprintf(f, "  flags=%x\n", mp->flags);
776         fprintf(f, "  ring=<%s>@%p\n", mp->ring->name, mp->ring);
777         fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
778         fprintf(f, "  size=%"PRIu32"\n", mp->size);
779         fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
780         fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
781         fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
782         fprintf(f, "  total_obj_size=%"PRIu32"\n",
783                mp->header_size + mp->elt_size + mp->trailer_size);
784
785         fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
786         fprintf(f, "  pg_num=%"PRIu32"\n", mp->pg_num);
787         fprintf(f, "  pg_shift=%"PRIu32"\n", mp->pg_shift);
788         fprintf(f, "  pg_mask=%#tx\n", mp->pg_mask);
789         fprintf(f, "  elt_va_start=%#tx\n", mp->elt_va_start);
790         fprintf(f, "  elt_va_end=%#tx\n", mp->elt_va_end);
791         fprintf(f, "  elt_pa[0]=0x%" PRIx64 "\n", mp->elt_pa[0]);
792
793         if (mp->size != 0)
794                 fprintf(f, "  avg bytes/object=%#Lf\n",
795                         (long double)(mp->elt_va_end - mp->elt_va_start) /
796                         mp->size);
797
798         cache_count = rte_mempool_dump_cache(f, mp);
799         common_count = rte_ring_count(mp->ring);
800         if ((cache_count + common_count) > mp->size)
801                 common_count = mp->size - cache_count;
802         fprintf(f, "  common_pool_count=%u\n", common_count);
803
804         /* sum and dump statistics */
805 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
806         memset(&sum, 0, sizeof(sum));
807         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
808                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
809                 sum.put_objs += mp->stats[lcore_id].put_objs;
810                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
811                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
812                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
813                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
814         }
815         fprintf(f, "  stats:\n");
816         fprintf(f, "    put_bulk=%"PRIu64"\n", sum.put_bulk);
817         fprintf(f, "    put_objs=%"PRIu64"\n", sum.put_objs);
818         fprintf(f, "    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
819         fprintf(f, "    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
820         fprintf(f, "    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
821         fprintf(f, "    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
822 #else
823         fprintf(f, "  no statistics available\n");
824 #endif
825
826         rte_mempool_audit(mp);
827 }
828
829 /* dump the status of all mempools on the console */
830 void
831 rte_mempool_list_dump(FILE *f)
832 {
833         const struct rte_mempool *mp = NULL;
834         struct rte_tailq_entry *te;
835         struct rte_mempool_list *mempool_list;
836
837         if ((mempool_list =
838              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL, rte_mempool_list)) == NULL) {
839                 rte_errno = E_RTE_NO_TAILQ;
840                 return;
841         }
842
843         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
844
845         TAILQ_FOREACH(te, mempool_list, next) {
846                 mp = (struct rte_mempool *) te->data;
847                 rte_mempool_dump(f, mp);
848         }
849
850         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
851 }
852
853 /* search a mempool from its name */
854 struct rte_mempool *
855 rte_mempool_lookup(const char *name)
856 {
857         struct rte_mempool *mp = NULL;
858         struct rte_tailq_entry *te;
859         struct rte_mempool_list *mempool_list;
860
861         if ((mempool_list =
862              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL, rte_mempool_list)) == NULL) {
863                 rte_errno = E_RTE_NO_TAILQ;
864                 return NULL;
865         }
866
867         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
868
869         TAILQ_FOREACH(te, mempool_list, next) {
870                 mp = (struct rte_mempool *) te->data;
871                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
872                         break;
873         }
874
875         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
876
877         if (te == NULL) {
878                 rte_errno = ENOENT;
879                 return NULL;
880         }
881
882         return mp;
883 }
884
885 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *),
886                       void *arg)
887 {
888         struct rte_tailq_entry *te = NULL;
889         struct rte_mempool_list *mempool_list;
890
891         if ((mempool_list =
892              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL, rte_mempool_list)) == NULL) {
893                 rte_errno = E_RTE_NO_TAILQ;
894                 return;
895         }
896
897         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
898
899         TAILQ_FOREACH(te, mempool_list, next) {
900                 (*func)((struct rte_mempool *) te->data, arg);
901         }
902
903         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
904 }