4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright(c) 2016 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/queue.h>
44 #include <rte_common.h>
46 #include <rte_debug.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_malloc.h>
50 #include <rte_atomic.h>
51 #include <rte_launch.h>
53 #include <rte_eal_memconfig.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_branch_prediction.h>
58 #include <rte_errno.h>
59 #include <rte_string_fns.h>
60 #include <rte_spinlock.h>
62 #include "rte_mempool.h"
64 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
66 static struct rte_tailq_elem rte_mempool_tailq = {
67 .name = "RTE_MEMPOOL",
69 EAL_REGISTER_TAILQ(rte_mempool_tailq)
71 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
72 #define CALC_CACHE_FLUSHTHRESH(c) \
73 ((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
76 * return the greatest common divisor between a and b (fast algorithm)
79 static unsigned get_gcd(unsigned a, unsigned b)
104 * Depending on memory configuration, objects addresses are spread
105 * between channels and ranks in RAM: the pool allocator will add
106 * padding between objects. This function return the new size of the
109 static unsigned optimize_object_size(unsigned obj_size)
111 unsigned nrank, nchan;
112 unsigned new_obj_size;
114 /* get number of channels */
115 nchan = rte_memory_get_nchannel();
119 nrank = rte_memory_get_nrank();
123 /* process new object size */
124 new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN;
125 while (get_gcd(new_obj_size, nrank * nchan) != 1)
127 return new_obj_size * RTE_MEMPOOL_ALIGN;
131 * A mempool object iterator callback function.
133 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
134 void * /*obj_start*/,
136 uint32_t /*obj_index */,
137 phys_addr_t /*physaddr*/);
140 mempool_add_elem(struct rte_mempool *mp, void *obj, phys_addr_t physaddr)
142 struct rte_mempool_objhdr *hdr;
143 struct rte_mempool_objtlr *tlr __rte_unused;
145 obj = (char *)obj + mp->header_size;
146 physaddr += mp->header_size;
148 /* set mempool ptr in header */
149 hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
151 hdr->physaddr = physaddr;
152 STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
154 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
155 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
156 tlr = __mempool_get_trailer(obj);
157 tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
160 /* enqueue in ring */
161 rte_ring_sp_enqueue(mp->ring, obj);
164 /* Iterate through objects at the given address
166 * Given the pointer to the memory, and its topology in physical memory
167 * (the physical addresses table), iterate through the "elt_num" objects
168 * of size "elt_sz" aligned at "align". For each object in this memory
169 * chunk, invoke a callback. It returns the effective number of objects
173 rte_mempool_obj_mem_iter(void *vaddr, uint32_t elt_num, size_t total_elt_sz,
174 size_t align, const phys_addr_t paddr[], uint32_t pg_num,
175 uint32_t pg_shift, rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg)
179 uintptr_t end, start, va;
181 phys_addr_t physaddr;
183 pg_sz = (uintptr_t)1 << pg_shift;
184 va = (uintptr_t)vaddr;
189 while (i != elt_num && j != pg_num) {
191 start = RTE_ALIGN_CEIL(va, align);
192 end = start + total_elt_sz;
194 /* index of the first page for the next element. */
195 pgf = (end >> pg_shift) - (start >> pg_shift);
197 /* index of the last page for the current element. */
198 pgn = ((end - 1) >> pg_shift) - (start >> pg_shift);
201 /* do we have enough space left for the element. */
207 paddr[k] + pg_sz == paddr[k + 1];
212 * if next pgn chunks of memory physically continuous,
213 * use it to create next element.
214 * otherwise, just skip that chunk unused.
217 physaddr = paddr[k] + (start & (pg_sz - 1));
218 if (obj_iter != NULL)
219 obj_iter(obj_iter_arg, (void *)start,
220 (void *)end, i, physaddr);
225 va = RTE_ALIGN_CEIL((va + 1), pg_sz);
233 /* call obj_cb() for each mempool element */
235 rte_mempool_obj_iter(struct rte_mempool *mp,
236 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
238 struct rte_mempool_objhdr *hdr;
242 STAILQ_FOREACH(hdr, &mp->elt_list, next) {
243 obj = (char *)hdr + sizeof(*hdr);
244 obj_cb(mp, obj_cb_arg, obj, n);
252 * Populate mempool with the objects.
256 mempool_obj_populate(void *arg, void *start, void *end,
257 __rte_unused uint32_t idx, phys_addr_t physaddr)
259 struct rte_mempool *mp = arg;
261 mempool_add_elem(mp, start, physaddr);
262 mp->elt_va_end = (uintptr_t)end;
266 mempool_populate(struct rte_mempool *mp, size_t num, size_t align)
270 elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
272 mp->size = rte_mempool_obj_mem_iter((void *)mp->elt_va_start,
274 mp->elt_pa, mp->pg_num, mp->pg_shift,
275 mempool_obj_populate, mp);
278 /* get the header, trailer and total size of a mempool element. */
280 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
281 struct rte_mempool_objsz *sz)
283 struct rte_mempool_objsz lsz;
285 sz = (sz != NULL) ? sz : &lsz;
287 sz->header_size = sizeof(struct rte_mempool_objhdr);
288 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
289 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
292 sz->trailer_size = sizeof(struct rte_mempool_objtlr);
294 /* element size is 8 bytes-aligned at least */
295 sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
297 /* expand trailer to next cache line */
298 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
299 sz->total_size = sz->header_size + sz->elt_size +
301 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
302 (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
303 RTE_MEMPOOL_ALIGN_MASK);
307 * increase trailer to add padding between objects in order to
308 * spread them across memory channels/ranks
310 if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
312 new_size = optimize_object_size(sz->header_size + sz->elt_size +
314 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
317 if (! rte_eal_has_hugepages()) {
319 * compute trailer size so that pool elements fit exactly in
322 int page_size = getpagesize();
323 int new_size = page_size - sz->header_size - sz->elt_size;
324 if (new_size < 0 || (unsigned int)new_size < sz->trailer_size) {
325 printf("When hugepages are disabled, pool objects "
326 "can't exceed PAGE_SIZE: %d + %d + %d > %d\n",
327 sz->header_size, sz->elt_size, sz->trailer_size,
331 sz->trailer_size = new_size;
334 /* this is the size of an object, including header and trailer */
335 sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
337 return sz->total_size;
342 * Calculate maximum amount of memory required to store given number of objects.
345 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift)
347 size_t n, pg_num, pg_sz, sz;
349 pg_sz = (size_t)1 << pg_shift;
351 if ((n = pg_sz / total_elt_sz) > 0) {
352 pg_num = (elt_num + n - 1) / n;
353 sz = pg_num << pg_shift;
355 sz = RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
361 /* Callback used by rte_mempool_xmem_usage(): it sets the opaque
362 * argument to the end of the object.
365 mempool_lelem_iter(void *arg, __rte_unused void *start, void *end,
366 __rte_unused uint32_t idx, __rte_unused phys_addr_t physaddr)
368 *(uintptr_t *)arg = (uintptr_t)end;
372 * Calculate how much memory would be actually required with the
373 * given memory footprint to store required number of elements.
376 rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t total_elt_sz,
377 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
383 pg_sz = (size_t)1 << pg_shift;
384 va = (uintptr_t)vaddr;
387 if ((n = rte_mempool_obj_mem_iter(vaddr, elt_num, total_elt_sz, 1,
388 paddr, pg_num, pg_shift, mempool_lelem_iter,
393 uv = RTE_ALIGN_CEIL(uv, pg_sz);
398 #ifndef RTE_LIBRTE_XEN_DOM0
399 /* stub if DOM0 support not configured */
401 rte_dom0_mempool_create(const char *name __rte_unused,
402 unsigned n __rte_unused,
403 unsigned elt_size __rte_unused,
404 unsigned cache_size __rte_unused,
405 unsigned private_data_size __rte_unused,
406 rte_mempool_ctor_t *mp_init __rte_unused,
407 void *mp_init_arg __rte_unused,
408 rte_mempool_obj_ctor_t *obj_init __rte_unused,
409 void *obj_init_arg __rte_unused,
410 int socket_id __rte_unused,
411 unsigned flags __rte_unused)
418 /* create the mempool */
420 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
421 unsigned cache_size, unsigned private_data_size,
422 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
423 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
424 int socket_id, unsigned flags)
426 if (rte_xen_dom0_supported())
427 return rte_dom0_mempool_create(name, n, elt_size,
428 cache_size, private_data_size,
429 mp_init, mp_init_arg,
430 obj_init, obj_init_arg,
433 return rte_mempool_xmem_create(name, n, elt_size,
434 cache_size, private_data_size,
435 mp_init, mp_init_arg,
436 obj_init, obj_init_arg,
438 NULL, NULL, MEMPOOL_PG_NUM_DEFAULT,
439 MEMPOOL_PG_SHIFT_MAX);
442 /* create the internal ring */
444 rte_mempool_ring_create(struct rte_mempool *mp)
447 char rg_name[RTE_RING_NAMESIZE];
450 snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, mp->name);
453 if (mp->flags & MEMPOOL_F_SP_PUT)
454 rg_flags |= RING_F_SP_ENQ;
455 if (mp->flags & MEMPOOL_F_SC_GET)
456 rg_flags |= RING_F_SC_DEQ;
458 /* Allocate the ring that will be used to store objects.
459 * Ring functions will return appropriate errors if we are
460 * running as a secondary process etc., so no checks made
461 * in this function for that condition.
463 r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
464 mp->socket_id, rg_flags);
473 * Create the mempool over already allocated chunk of memory.
474 * That external memory buffer can consists of physically disjoint pages.
475 * Setting vaddr to NULL, makes mempool to fallback to original behaviour
476 * and allocate space for mempool and it's elements as one big chunk of
477 * physically continuos memory.
480 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
481 unsigned cache_size, unsigned private_data_size,
482 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
483 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
484 int socket_id, unsigned flags, void *vaddr,
485 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
487 char mz_name[RTE_MEMZONE_NAMESIZE];
488 struct rte_mempool_list *mempool_list;
489 struct rte_mempool *mp = NULL;
490 struct rte_tailq_entry *te = NULL;
491 const struct rte_memzone *mz;
493 int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
495 struct rte_mempool_objsz objsz;
497 int page_size = getpagesize();
499 /* compilation-time checks */
500 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
501 RTE_CACHE_LINE_MASK) != 0);
502 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
503 RTE_CACHE_LINE_MASK) != 0);
504 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
505 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
506 RTE_CACHE_LINE_MASK) != 0);
507 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
508 RTE_CACHE_LINE_MASK) != 0);
511 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
513 /* asked cache too big */
514 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
515 CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
520 /* check that we have both VA and PA */
521 if (vaddr != NULL && paddr == NULL) {
526 /* Check that pg_num and pg_shift parameters are valid. */
527 if (pg_num < RTE_DIM(mp->elt_pa) || pg_shift > MEMPOOL_PG_SHIFT_MAX) {
532 /* "no cache align" imply "no spread" */
533 if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
534 flags |= MEMPOOL_F_NO_SPREAD;
536 /* calculate mempool object sizes. */
537 if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
542 rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
545 * reserve a memory zone for this mempool: private data is
548 private_data_size = (private_data_size +
549 RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
551 if (! rte_eal_has_hugepages()) {
553 * expand private data size to a whole page, so that the
554 * first pool element will start on a new standard page
556 int head = sizeof(struct rte_mempool);
557 int new_size = (private_data_size + head) % page_size;
559 private_data_size += page_size - new_size;
562 /* try to allocate tailq entry */
563 te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
565 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
570 * If user provided an external memory buffer, then use it to
571 * store mempool objects. Otherwise reserve a memzone that is large
572 * enough to hold mempool header and metadata plus mempool objects.
574 mempool_size = MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size);
575 mempool_size += private_data_size;
576 mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
578 mempool_size += (size_t)objsz.total_size * n;
580 if (! rte_eal_has_hugepages()) {
582 * we want the memory pool to start on a page boundary,
583 * because pool elements crossing page boundaries would
584 * result in discontiguous physical addresses
586 mempool_size += page_size;
589 snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
591 mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
595 if (rte_eal_has_hugepages()) {
596 startaddr = (void*)mz->addr;
598 /* align memory pool start address on a page boundary */
599 unsigned long addr = (unsigned long)mz->addr;
600 if (addr & (page_size - 1)) {
602 addr &= ~(page_size - 1);
604 startaddr = (void*)addr;
607 /* init the mempool structure */
609 memset(mp, 0, sizeof(*mp));
610 snprintf(mp->name, sizeof(mp->name), "%s", name);
611 mp->phys_addr = mz->phys_addr;
612 mp->socket_id = socket_id;
615 mp->elt_size = objsz.elt_size;
616 mp->header_size = objsz.header_size;
617 mp->trailer_size = objsz.trailer_size;
618 mp->cache_size = cache_size;
619 mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
620 mp->private_data_size = private_data_size;
621 STAILQ_INIT(&mp->elt_list);
623 if (rte_mempool_ring_create(mp) < 0)
627 * local_cache pointer is set even if cache_size is zero.
628 * The local_cache points to just past the elt_pa[] array.
630 mp->local_cache = (struct rte_mempool_cache *)
631 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, pg_num, 0));
633 /* calculate address of the first element for continuous mempool. */
634 obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size) +
636 obj = RTE_PTR_ALIGN_CEIL(obj, RTE_MEMPOOL_ALIGN);
638 /* populate address translation fields. */
640 mp->pg_shift = pg_shift;
641 mp->pg_mask = RTE_LEN2MASK(mp->pg_shift, typeof(mp->pg_mask));
643 /* mempool elements allocated together with mempool */
645 mp->elt_va_start = (uintptr_t)obj;
646 mp->elt_pa[0] = mp->phys_addr +
647 (mp->elt_va_start - (uintptr_t)mp);
649 /* mempool elements in a separate chunk of memory. */
650 mp->elt_va_start = (uintptr_t)vaddr;
651 memcpy(mp->elt_pa, paddr, sizeof (mp->elt_pa[0]) * pg_num);
654 mp->elt_va_end = mp->elt_va_start;
656 /* call the initializer */
658 mp_init(mp, mp_init_arg);
660 mempool_populate(mp, n, 1);
662 /* call the initializer */
664 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
666 te->data = (void *) mp;
668 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
669 TAILQ_INSERT_TAIL(mempool_list, te, next);
670 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
671 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
676 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
678 rte_ring_free(mp->ring);
684 /* Return the number of entries in the mempool */
686 rte_mempool_count(const struct rte_mempool *mp)
691 count = rte_ring_count(mp->ring);
693 if (mp->cache_size == 0)
696 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
697 count += mp->local_cache[lcore_id].len;
700 * due to race condition (access to len is not locked), the
701 * total can be greater than size... so fix the result
703 if (count > mp->size)
708 /* dump the cache status */
710 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
714 unsigned cache_count;
716 fprintf(f, " cache infos:\n");
717 fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
719 if (mp->cache_size == 0)
722 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
723 cache_count = mp->local_cache[lcore_id].len;
724 fprintf(f, " cache_count[%u]=%u\n", lcore_id, cache_count);
725 count += cache_count;
727 fprintf(f, " total_cache_count=%u\n", count);
731 #ifndef __INTEL_COMPILER
732 #pragma GCC diagnostic ignored "-Wcast-qual"
735 /* check and update cookies or panic (internal) */
736 void rte_mempool_check_cookies(const struct rte_mempool *mp,
737 void * const *obj_table_const, unsigned n, int free)
739 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
740 struct rte_mempool_objhdr *hdr;
741 struct rte_mempool_objtlr *tlr;
747 /* Force to drop the "const" attribute. This is done only when
748 * DEBUG is enabled */
749 tmp = (void *) obj_table_const;
750 obj_table = (void **) tmp;
755 if (rte_mempool_from_obj(obj) != mp)
756 rte_panic("MEMPOOL: object is owned by another "
759 hdr = __mempool_get_header(obj);
760 cookie = hdr->cookie;
763 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
764 rte_log_set_history(0);
765 RTE_LOG(CRIT, MEMPOOL,
766 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
767 obj, (const void *) mp, cookie);
768 rte_panic("MEMPOOL: bad header cookie (put)\n");
770 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
771 } else if (free == 1) {
772 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
773 rte_log_set_history(0);
774 RTE_LOG(CRIT, MEMPOOL,
775 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
776 obj, (const void *) mp, cookie);
777 rte_panic("MEMPOOL: bad header cookie (get)\n");
779 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
780 } else if (free == 2) {
781 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
782 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
783 rte_log_set_history(0);
784 RTE_LOG(CRIT, MEMPOOL,
785 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
786 obj, (const void *) mp, cookie);
787 rte_panic("MEMPOOL: bad header cookie (audit)\n");
790 tlr = __mempool_get_trailer(obj);
791 cookie = tlr->cookie;
792 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
793 rte_log_set_history(0);
794 RTE_LOG(CRIT, MEMPOOL,
795 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
796 obj, (const void *) mp, cookie);
797 rte_panic("MEMPOOL: bad trailer cookie\n");
802 RTE_SET_USED(obj_table_const);
808 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
810 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
811 void *obj, __rte_unused unsigned idx)
813 __mempool_check_cookies(mp, &obj, 1, 2);
817 mempool_audit_cookies(struct rte_mempool *mp)
821 num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
822 if (num != mp->size) {
823 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
824 "iterated only over %u elements\n",
829 #define mempool_audit_cookies(mp) do {} while(0)
832 #ifndef __INTEL_COMPILER
833 #pragma GCC diagnostic error "-Wcast-qual"
836 /* check cookies before and after objects */
838 mempool_audit_cache(const struct rte_mempool *mp)
840 /* check cache size consistency */
843 if (mp->cache_size == 0)
846 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
847 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
848 RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
850 rte_panic("MEMPOOL: invalid cache len\n");
855 /* check the consistency of mempool (size, cookies, ...) */
857 rte_mempool_audit(struct rte_mempool *mp)
859 mempool_audit_cache(mp);
860 mempool_audit_cookies(mp);
862 /* For case where mempool DEBUG is not set, and cache size is 0 */
866 /* dump the status of the mempool on the console */
868 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
870 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
871 struct rte_mempool_debug_stats sum;
874 unsigned common_count;
875 unsigned cache_count;
877 RTE_ASSERT(f != NULL);
878 RTE_ASSERT(mp != NULL);
880 fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
881 fprintf(f, " flags=%x\n", mp->flags);
882 fprintf(f, " ring=<%s>@%p\n", mp->ring->name, mp->ring);
883 fprintf(f, " phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
884 fprintf(f, " size=%"PRIu32"\n", mp->size);
885 fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
886 fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
887 fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
888 fprintf(f, " total_obj_size=%"PRIu32"\n",
889 mp->header_size + mp->elt_size + mp->trailer_size);
891 fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
892 fprintf(f, " pg_num=%"PRIu32"\n", mp->pg_num);
893 fprintf(f, " pg_shift=%"PRIu32"\n", mp->pg_shift);
894 fprintf(f, " pg_mask=%#tx\n", mp->pg_mask);
895 fprintf(f, " elt_va_start=%#tx\n", mp->elt_va_start);
896 fprintf(f, " elt_va_end=%#tx\n", mp->elt_va_end);
897 fprintf(f, " elt_pa[0]=0x%" PRIx64 "\n", mp->elt_pa[0]);
900 fprintf(f, " avg bytes/object=%#Lf\n",
901 (long double)(mp->elt_va_end - mp->elt_va_start) /
904 cache_count = rte_mempool_dump_cache(f, mp);
905 common_count = rte_ring_count(mp->ring);
906 if ((cache_count + common_count) > mp->size)
907 common_count = mp->size - cache_count;
908 fprintf(f, " common_pool_count=%u\n", common_count);
910 /* sum and dump statistics */
911 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
912 memset(&sum, 0, sizeof(sum));
913 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
914 sum.put_bulk += mp->stats[lcore_id].put_bulk;
915 sum.put_objs += mp->stats[lcore_id].put_objs;
916 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
917 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
918 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
919 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
921 fprintf(f, " stats:\n");
922 fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
923 fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
924 fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
925 fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
926 fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
927 fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
929 fprintf(f, " no statistics available\n");
932 rte_mempool_audit(mp);
935 /* dump the status of all mempools on the console */
937 rte_mempool_list_dump(FILE *f)
939 struct rte_mempool *mp = NULL;
940 struct rte_tailq_entry *te;
941 struct rte_mempool_list *mempool_list;
943 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
945 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
947 TAILQ_FOREACH(te, mempool_list, next) {
948 mp = (struct rte_mempool *) te->data;
949 rte_mempool_dump(f, mp);
952 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
955 /* search a mempool from its name */
957 rte_mempool_lookup(const char *name)
959 struct rte_mempool *mp = NULL;
960 struct rte_tailq_entry *te;
961 struct rte_mempool_list *mempool_list;
963 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
965 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
967 TAILQ_FOREACH(te, mempool_list, next) {
968 mp = (struct rte_mempool *) te->data;
969 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
973 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
983 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
986 struct rte_tailq_entry *te = NULL;
987 struct rte_mempool_list *mempool_list;
989 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
991 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
993 TAILQ_FOREACH(te, mempool_list, next) {
994 (*func)((struct rte_mempool *) te->data, arg);
997 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);