xen: core library changes
[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 <inttypes.h>
39 #include <errno.h>
40 #include <sys/queue.h>
41
42 #include <rte_common.h>
43 #include <rte_log.h>
44 #include <rte_debug.h>
45 #include <rte_memory.h>
46 #include <rte_memzone.h>
47 #include <rte_atomic.h>
48 #include <rte_launch.h>
49 #include <rte_tailq.h>
50 #include <rte_eal.h>
51 #include <rte_eal_memconfig.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_branch_prediction.h>
55 #include <rte_ring.h>
56 #include <rte_errno.h>
57 #include <rte_string_fns.h>
58 #include <rte_spinlock.h>
59
60 #include "rte_mempool.h"
61
62 TAILQ_HEAD(rte_mempool_list, rte_mempool);
63
64 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
65
66 /*
67  * return the greatest common divisor between a and b (fast algorithm)
68  *
69  */
70 static unsigned get_gcd(unsigned a, unsigned b)
71 {
72         unsigned c;
73
74         if (0 == a)
75                 return b;
76         if (0 == b)
77                 return a;
78
79         if (a < b) {
80                 c = a;
81                 a = b;
82                 b = c;
83         }
84
85         while (b != 0) {
86                 c = a % b;
87                 a = b;
88                 b = c;
89         }
90
91         return a;
92 }
93
94 /*
95  * Depending on memory configuration, objects addresses are spreaded
96  * between channels and ranks in RAM: the pool allocator will add
97  * padding between objects. This function return the new size of the
98  * object.
99  */
100 static unsigned optimize_object_size(unsigned obj_size)
101 {
102         unsigned nrank, nchan;
103         unsigned new_obj_size;
104
105         /* get number of channels */
106         nchan = rte_memory_get_nchannel();
107         if (nchan == 0)
108                 nchan = 1;
109
110         nrank = rte_memory_get_nrank();
111         if (nrank == 0)
112                 nrank = 1;
113
114         /* process new object size */
115         new_obj_size = (obj_size + CACHE_LINE_MASK) / CACHE_LINE_SIZE;
116         while (get_gcd(new_obj_size, nrank * nchan) != 1 ||
117                         get_gcd(nchan, new_obj_size) != 1)
118                 new_obj_size++;
119         return new_obj_size * 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                         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 += ((CACHE_LINE_SIZE -
272                                   (sz->total_size & CACHE_LINE_MASK)) &
273                                  CACHE_LINE_MASK);
274         }
275
276         /*
277          * increase trailer to add padding between objects in order to
278          * spread them accross 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         /* this is the size of an object, including header and trailer */
288         sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
289
290         return (sz->total_size);
291 }
292
293
294 /*
295  * Calculate maximum amount of memory required to store given number of objects.
296  */
297 size_t
298 rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz, uint32_t pg_shift)
299 {
300         size_t n, pg_num, pg_sz, sz;
301
302         pg_sz = (size_t)1 << pg_shift;
303
304         if ((n = pg_sz / elt_sz) > 0) {
305                 pg_num = (elt_num + n - 1) / n;
306                 sz = pg_num << pg_shift;
307         } else {
308                 sz = RTE_ALIGN_CEIL(elt_sz, pg_sz) * elt_num;
309         }
310
311         return (sz);
312 }
313
314 /*
315  * Calculate how much memory would be actually required with the
316  * given memory footprint to store required number of elements.
317  */
318 static void
319 mempool_lelem_iter(void *arg, __rte_unused void *start, void *end,
320         __rte_unused uint32_t idx)
321 {
322         *(uintptr_t *)arg = (uintptr_t)end;
323 }
324
325 ssize_t
326 rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
327         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
328 {
329         uint32_t n;
330         uintptr_t va, uv;
331         size_t pg_sz, usz;
332
333         pg_sz = (size_t)1 << pg_shift;
334         va = (uintptr_t)vaddr;
335         uv = va;
336
337         if ((n = rte_mempool_obj_iter(vaddr, elt_num, elt_sz, 1,
338                         paddr, pg_num, pg_shift, mempool_lelem_iter,
339                         &uv)) != elt_num) {
340                 return (-n);
341         }
342
343         uv = RTE_ALIGN_CEIL(uv, pg_sz);
344         usz = uv - va;
345         return (usz);
346 }
347
348 /* create the mempool */
349 struct rte_mempool *
350 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
351                    unsigned cache_size, unsigned private_data_size,
352                    rte_mempool_ctor_t *mp_init, void *mp_init_arg,
353                    rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
354                    int socket_id, unsigned flags)
355 {
356 #ifdef RTE_LIBRTE_XEN_DOM0
357         return (rte_dom0_mempool_create(name, n, elt_size,
358                 cache_size, private_data_size,
359                 mp_init, mp_init_arg,
360                 obj_init, obj_init_arg,
361                 socket_id, flags));
362 #else
363         return (rte_mempool_xmem_create(name, n, elt_size,
364                 cache_size, private_data_size,
365                 mp_init, mp_init_arg,
366                 obj_init, obj_init_arg,
367                 socket_id, flags,
368                 NULL, NULL, MEMPOOL_PG_NUM_DEFAULT, MEMPOOL_PG_SHIFT_MAX));
369 #endif
370 }
371
372 /*
373  * Create the mempool over already allocated chunk of memory.
374  * That external memory buffer can consists of physically disjoint pages.
375  * Setting vaddr to NULL, makes mempool to fallback to original behaviour
376  * and allocate space for mempool and it's elements as one big chunk of
377  * physically continuos memory.
378  * */
379 struct rte_mempool *
380 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
381                 unsigned cache_size, unsigned private_data_size,
382                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
383                 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
384                 int socket_id, unsigned flags, void *vaddr,
385                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
386 {
387         char mz_name[RTE_MEMZONE_NAMESIZE];
388         char rg_name[RTE_RING_NAMESIZE];
389         struct rte_mempool *mp = NULL;
390         struct rte_ring *r;
391         const struct rte_memzone *mz;
392         size_t mempool_size;
393         int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
394         int rg_flags = 0;
395         void *obj; 
396         struct rte_mempool_objsz objsz;
397
398         /* compilation-time checks */
399         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
400                           CACHE_LINE_MASK) != 0);
401 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
402         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
403                           CACHE_LINE_MASK) != 0);
404         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, local_cache) &
405                           CACHE_LINE_MASK) != 0);
406 #endif
407 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
408         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
409                           CACHE_LINE_MASK) != 0);
410         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
411                           CACHE_LINE_MASK) != 0);
412 #endif
413
414         /* check that we have an initialised tail queue */
415         if (RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL,
416                         rte_mempool_list) == NULL) {
417                 rte_errno = E_RTE_NO_TAILQ;
418                 return NULL;    
419         }
420         
421         /* asked cache too big */
422         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
423                 rte_errno = EINVAL;
424                 return NULL;
425         }
426
427         /* check that we have both VA and PA */
428         if (vaddr != NULL && paddr == NULL) {
429                 rte_errno = EINVAL;
430                 return NULL;
431         }
432
433         /* Check that pg_num and pg_shift parameters are valid. */
434         if (pg_num < RTE_DIM(mp->elt_pa) || pg_shift > MEMPOOL_PG_SHIFT_MAX) {
435                 rte_errno = EINVAL;
436                 return NULL;
437         }
438
439         /* "no cache align" imply "no spread" */
440         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
441                 flags |= MEMPOOL_F_NO_SPREAD;
442
443         /* ring flags */
444         if (flags & MEMPOOL_F_SP_PUT)
445                 rg_flags |= RING_F_SP_ENQ;
446         if (flags & MEMPOOL_F_SC_GET)
447                 rg_flags |= RING_F_SC_DEQ;
448
449         /* calculate mempool object sizes. */
450         rte_mempool_calc_obj_size(elt_size, flags, &objsz);
451
452         rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
453
454         /* allocate the ring that will be used to store objects */
455         /* Ring functions will return appropriate errors if we are
456          * running as a secondary process etc., so no checks made
457          * in this function for that condition */
458         rte_snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
459         r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
460         if (r == NULL)
461                 goto exit;
462
463         /*
464          * reserve a memory zone for this mempool: private data is
465          * cache-aligned
466          */
467         private_data_size = (private_data_size +
468                              CACHE_LINE_MASK) & (~CACHE_LINE_MASK);
469
470         /*
471          * If user provided an external memory buffer, then use it to
472          * store mempool objects. Otherwise reserve memzone big enough to
473          * hold mempool header and metadata plus mempool objects.
474          */
475         mempool_size = MEMPOOL_HEADER_SIZE(mp, pg_num) + private_data_size;
476         if (vaddr == NULL)
477                 mempool_size += (size_t)objsz.total_size * n;
478                         
479         rte_snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
480
481         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
482
483         /*
484          * no more memory: in this case we loose previously reserved
485          * space for the as we cannot free it
486          */
487         if (mz == NULL)
488                 goto exit;
489
490         /* init the mempool structure */
491         mp = mz->addr;
492         memset(mp, 0, sizeof(*mp));
493         rte_snprintf(mp->name, sizeof(mp->name), "%s", name);
494         mp->phys_addr = mz->phys_addr;
495         mp->ring = r;
496         mp->size = n;
497         mp->flags = flags;
498         mp->elt_size = objsz.elt_size;
499         mp->header_size = objsz.header_size;
500         mp->trailer_size = objsz.trailer_size;
501         mp->cache_size = cache_size;
502         mp->cache_flushthresh = (uint32_t)
503                 (cache_size * CACHE_FLUSHTHRESH_MULTIPLIER);
504         mp->private_data_size = private_data_size;
505
506         /* calculate address of the first element for continuous mempool. */
507         obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num) +
508                 private_data_size;
509
510         /* populate address translation fields. */
511         mp->pg_num = pg_num;
512         mp->pg_shift = pg_shift;
513         mp->pg_mask = RTE_LEN2MASK(mp->pg_shift, typeof(mp->pg_mask));
514
515         /* mempool elements allocated together with mempool */
516         if (vaddr == NULL) {
517                 mp->elt_va_start = (uintptr_t)obj;
518                 mp->elt_pa[0] = mp->phys_addr +
519                         (mp->elt_va_start - (uintptr_t)mp);
520
521         /* mempool elements in a separate chunk of memory. */
522         } else {
523                 mp->elt_va_start = (uintptr_t)vaddr;
524                 memcpy(mp->elt_pa, paddr, sizeof (mp->elt_pa[0]) * pg_num);
525         }
526
527         mp->elt_va_end = mp->elt_va_start;
528
529         /* call the initializer */
530         if (mp_init)
531                 mp_init(mp, mp_init_arg);
532
533         mempool_populate(mp, n, 1, obj_init, obj_init_arg);
534
535         RTE_EAL_TAILQ_INSERT_TAIL(RTE_TAILQ_MEMPOOL, rte_mempool_list, mp);
536
537 exit:
538         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
539
540         return mp;
541 }
542
543 /* Return the number of entries in the mempool */
544 unsigned
545 rte_mempool_count(const struct rte_mempool *mp)
546 {
547         unsigned count;
548
549         count = rte_ring_count(mp->ring);
550
551 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
552         {
553                 unsigned lcore_id;
554                 if (mp->cache_size == 0)
555                         return count;
556
557                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
558                         count += mp->local_cache[lcore_id].len;
559         }
560 #endif
561
562         /*
563          * due to race condition (access to len is not locked), the
564          * total can be greater than size... so fix the result
565          */
566         if (count > mp->size)
567                 return mp->size;
568         return count;
569 }
570
571 /* dump the cache status */
572 static unsigned
573 rte_mempool_dump_cache(const struct rte_mempool *mp)
574 {
575 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
576         unsigned lcore_id;
577         unsigned count = 0;
578         unsigned cache_count;
579
580         printf("  cache infos:\n");
581         printf("    cache_size=%"PRIu32"\n", mp->cache_size);
582         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
583                 cache_count = mp->local_cache[lcore_id].len;
584                 printf("    cache_count[%u]=%u\n", lcore_id, cache_count);
585                 count += cache_count;
586         }
587         printf("    total_cache_count=%u\n", count);
588         return count;
589 #else
590         RTE_SET_USED(mp);
591         printf("  cache disabled\n");
592         return 0;
593 #endif
594 }
595
596 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
597 /* check cookies before and after objects */
598 #ifndef __INTEL_COMPILER
599 #pragma GCC diagnostic ignored "-Wcast-qual"
600 #endif
601
602 struct mempool_audit_arg {
603         const struct rte_mempool *mp;
604         uintptr_t obj_end;
605         uint32_t obj_num;
606 };
607
608 static void
609 mempool_obj_audit(void *arg, void *start, void *end, uint32_t idx)
610 {
611         struct mempool_audit_arg *pa = arg;
612         void *obj;
613
614         obj = (char *)start + pa->mp->header_size;
615         pa->obj_end = (uintptr_t)end;
616         pa->obj_num = idx + 1;
617         __mempool_check_cookies(pa->mp, &obj, 1, 2);
618 }
619
620 static void
621 mempool_audit_cookies(const struct rte_mempool *mp)
622 {
623         uint32_t elt_sz, num;
624         struct mempool_audit_arg arg;
625
626         elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
627
628         arg.mp = mp;
629         arg.obj_end = mp->elt_va_start;
630         arg.obj_num = 0;
631
632         num = rte_mempool_obj_iter((void *)mp->elt_va_start,
633                 mp->size, elt_sz, 1,
634                 mp->elt_pa, mp->pg_num, mp->pg_shift,
635                 mempool_obj_audit, &arg);
636
637         if (num != mp->size) {
638                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
639                         "iterated only over %u elements\n",
640                         mp, mp->size, num);
641         } else if (arg.obj_end != mp->elt_va_end || arg.obj_num != mp->size) {
642                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
643                         "last callback va_end: %#tx (%#tx expeceted), "
644                         "num of objects: %u (%u expected)\n", 
645                         mp, mp->size,
646                         arg.obj_end, mp->elt_va_end,
647                         arg.obj_num, mp->size);
648         }
649 }
650
651 #ifndef __INTEL_COMPILER
652 #pragma GCC diagnostic error "-Wcast-qual"
653 #endif
654 #else
655 #define mempool_audit_cookies(mp) do {} while(0)
656 #endif
657
658 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
659 /* check cookies before and after objects */
660 static void
661 mempool_audit_cache(const struct rte_mempool *mp)
662 {
663         /* check cache size consistency */
664         unsigned lcore_id;
665         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
666                 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
667                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
668                                 lcore_id);
669                         rte_panic("MEMPOOL: invalid cache len\n");
670                 }
671         }
672 }
673 #else
674 #define mempool_audit_cache(mp) do {} while(0)
675 #endif
676
677
678 /* check the consistency of mempool (size, cookies, ...) */
679 void
680 rte_mempool_audit(const struct rte_mempool *mp)
681 {
682         mempool_audit_cache(mp);
683         mempool_audit_cookies(mp);
684
685         /* For case where mempool DEBUG is not set, and cache size is 0 */
686         RTE_SET_USED(mp);
687 }
688
689 /* dump the status of the mempool on the console */
690 void
691 rte_mempool_dump(const struct rte_mempool *mp)
692 {
693 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
694         struct rte_mempool_debug_stats sum;
695         unsigned lcore_id;
696 #endif
697         unsigned common_count;
698         unsigned cache_count;
699
700         printf("mempool <%s>@%p\n", mp->name, mp);
701         printf("  flags=%x\n", mp->flags);
702         printf("  ring=<%s>@%p\n", mp->ring->name, mp->ring);
703         printf("  phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
704         printf("  size=%"PRIu32"\n", mp->size);
705         printf("  header_size=%"PRIu32"\n", mp->header_size);
706         printf("  elt_size=%"PRIu32"\n", mp->elt_size);
707         printf("  trailer_size=%"PRIu32"\n", mp->trailer_size);
708         printf("  total_obj_size=%"PRIu32"\n",
709                mp->header_size + mp->elt_size + mp->trailer_size);
710
711         printf("  private_data_size=%"PRIu32"\n", mp->private_data_size);
712         printf("  pg_num=%"PRIu32"\n", mp->pg_num);
713         printf("  pg_shift=%"PRIu32"\n", mp->pg_shift);
714         printf("  pg_mask=%#tx\n", mp->pg_mask);
715         printf("  elt_va_start=%#tx\n", mp->elt_va_start);
716         printf("  elt_va_end=%#tx\n", mp->elt_va_end);
717         printf("  elt_pa[0]=0x%" PRIx64 "\n", mp->elt_pa[0]);
718
719         if (mp->size != 0)
720                 printf("  avg bytes/object=%#Lf\n",
721                         (long double)(mp->elt_va_end - mp->elt_va_start) /
722                         mp->size);
723
724         cache_count = rte_mempool_dump_cache(mp);
725         common_count = rte_ring_count(mp->ring);
726         if ((cache_count + common_count) > mp->size)
727                 common_count = mp->size - cache_count;
728         printf("  common_pool_count=%u\n", common_count);
729
730         /* sum and dump statistics */
731 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
732         memset(&sum, 0, sizeof(sum));
733         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
734                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
735                 sum.put_objs += mp->stats[lcore_id].put_objs;
736                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
737                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
738                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
739                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
740         }
741         printf("  stats:\n");
742         printf("    put_bulk=%"PRIu64"\n", sum.put_bulk);
743         printf("    put_objs=%"PRIu64"\n", sum.put_objs);
744         printf("    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
745         printf("    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
746         printf("    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
747         printf("    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
748 #else
749         printf("  no statistics available\n");
750 #endif
751
752         rte_mempool_audit(mp);
753 }
754
755 /* dump the status of all mempools on the console */
756 void
757 rte_mempool_list_dump(void)
758 {
759         const struct rte_mempool *mp = NULL;
760         struct rte_mempool_list *mempool_list;
761
762         if ((mempool_list = 
763              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL, rte_mempool_list)) == NULL) {
764                 rte_errno = E_RTE_NO_TAILQ;
765                 return; 
766         }
767
768         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
769
770         TAILQ_FOREACH(mp, mempool_list, next) {
771                 rte_mempool_dump(mp);
772         }
773
774         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
775 }
776
777 /* search a mempool from its name */
778 struct rte_mempool *
779 rte_mempool_lookup(const char *name)
780 {
781         struct rte_mempool *mp = NULL;
782         struct rte_mempool_list *mempool_list;
783
784         if ((mempool_list = 
785              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL, rte_mempool_list)) == NULL) {
786                 rte_errno = E_RTE_NO_TAILQ;
787                 return NULL;
788         }
789
790         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
791
792         TAILQ_FOREACH(mp, mempool_list, next) {
793                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
794                         break;
795         }
796         
797         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
798         
799         if (mp == NULL)
800                 rte_errno = ENOENT;
801
802         return mp;
803 }