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