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