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 */);
139 mempool_add_elem(struct rte_mempool *mp, void *obj)
141 struct rte_mempool_objhdr *hdr;
142 struct rte_mempool_objtlr *tlr __rte_unused;
144 obj = (char *)obj + mp->header_size;
146 /* set mempool ptr in header */
147 hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
149 STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
151 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
152 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
153 tlr = __mempool_get_trailer(obj);
154 tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
157 /* enqueue in ring */
158 rte_ring_sp_enqueue(mp->ring, obj);
161 /* Iterate through objects at the given address
163 * Given the pointer to the memory, and its topology in physical memory
164 * (the physical addresses table), iterate through the "elt_num" objects
165 * of size "elt_sz" aligned at "align". For each object in this memory
166 * chunk, invoke a callback. It returns the effective number of objects
170 rte_mempool_obj_mem_iter(void *vaddr, uint32_t elt_num, size_t total_elt_sz,
171 size_t align, const phys_addr_t paddr[], uint32_t pg_num,
172 uint32_t pg_shift, rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg)
176 uintptr_t end, start, va;
179 pg_sz = (uintptr_t)1 << pg_shift;
180 va = (uintptr_t)vaddr;
185 while (i != elt_num && j != pg_num) {
187 start = RTE_ALIGN_CEIL(va, align);
188 end = start + total_elt_sz;
190 /* index of the first page for the next element. */
191 pgf = (end >> pg_shift) - (start >> pg_shift);
193 /* index of the last page for the current element. */
194 pgn = ((end - 1) >> pg_shift) - (start >> pg_shift);
197 /* do we have enough space left for the element. */
203 paddr[k] + pg_sz == paddr[k + 1];
208 * if next pgn chunks of memory physically continuous,
209 * use it to create next element.
210 * otherwise, just skip that chunk unused.
213 if (obj_iter != NULL)
214 obj_iter(obj_iter_arg, (void *)start,
220 va = RTE_ALIGN_CEIL((va + 1), pg_sz);
228 /* call obj_cb() for each mempool element */
230 rte_mempool_obj_iter(struct rte_mempool *mp,
231 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
233 struct rte_mempool_objhdr *hdr;
237 STAILQ_FOREACH(hdr, &mp->elt_list, next) {
238 obj = (char *)hdr + sizeof(*hdr);
239 obj_cb(mp, obj_cb_arg, obj, n);
247 * Populate mempool with the objects.
251 mempool_obj_populate(void *arg, void *start, void *end,
252 __rte_unused uint32_t idx)
254 struct rte_mempool *mp = arg;
256 mempool_add_elem(mp, start);
257 mp->elt_va_end = (uintptr_t)end;
261 mempool_populate(struct rte_mempool *mp, size_t num, size_t align)
265 elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
267 mp->size = rte_mempool_obj_mem_iter((void *)mp->elt_va_start,
269 mp->elt_pa, mp->pg_num, mp->pg_shift,
270 mempool_obj_populate, mp);
273 /* get the header, trailer and total size of a mempool element. */
275 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
276 struct rte_mempool_objsz *sz)
278 struct rte_mempool_objsz lsz;
280 sz = (sz != NULL) ? sz : &lsz;
282 sz->header_size = sizeof(struct rte_mempool_objhdr);
283 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
284 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
287 sz->trailer_size = sizeof(struct rte_mempool_objtlr);
289 /* element size is 8 bytes-aligned at least */
290 sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
292 /* expand trailer to next cache line */
293 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
294 sz->total_size = sz->header_size + sz->elt_size +
296 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
297 (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
298 RTE_MEMPOOL_ALIGN_MASK);
302 * increase trailer to add padding between objects in order to
303 * spread them across memory channels/ranks
305 if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
307 new_size = optimize_object_size(sz->header_size + sz->elt_size +
309 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
312 if (! rte_eal_has_hugepages()) {
314 * compute trailer size so that pool elements fit exactly in
317 int page_size = getpagesize();
318 int new_size = page_size - sz->header_size - sz->elt_size;
319 if (new_size < 0 || (unsigned int)new_size < sz->trailer_size) {
320 printf("When hugepages are disabled, pool objects "
321 "can't exceed PAGE_SIZE: %d + %d + %d > %d\n",
322 sz->header_size, sz->elt_size, sz->trailer_size,
326 sz->trailer_size = new_size;
329 /* this is the size of an object, including header and trailer */
330 sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
332 return sz->total_size;
337 * Calculate maximum amount of memory required to store given number of objects.
340 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift)
342 size_t n, pg_num, pg_sz, sz;
344 pg_sz = (size_t)1 << pg_shift;
346 if ((n = pg_sz / total_elt_sz) > 0) {
347 pg_num = (elt_num + n - 1) / n;
348 sz = pg_num << pg_shift;
350 sz = RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
356 /* Callback used by rte_mempool_xmem_usage(): it sets the opaque
357 * argument to the end of the object.
360 mempool_lelem_iter(void *arg, __rte_unused void *start, void *end,
361 __rte_unused uint32_t idx)
363 *(uintptr_t *)arg = (uintptr_t)end;
367 * Calculate how much memory would be actually required with the
368 * given memory footprint to store required number of elements.
371 rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t total_elt_sz,
372 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
378 pg_sz = (size_t)1 << pg_shift;
379 va = (uintptr_t)vaddr;
382 if ((n = rte_mempool_obj_mem_iter(vaddr, elt_num, total_elt_sz, 1,
383 paddr, pg_num, pg_shift, mempool_lelem_iter,
388 uv = RTE_ALIGN_CEIL(uv, pg_sz);
393 #ifndef RTE_LIBRTE_XEN_DOM0
394 /* stub if DOM0 support not configured */
396 rte_dom0_mempool_create(const char *name __rte_unused,
397 unsigned n __rte_unused,
398 unsigned elt_size __rte_unused,
399 unsigned cache_size __rte_unused,
400 unsigned private_data_size __rte_unused,
401 rte_mempool_ctor_t *mp_init __rte_unused,
402 void *mp_init_arg __rte_unused,
403 rte_mempool_obj_ctor_t *obj_init __rte_unused,
404 void *obj_init_arg __rte_unused,
405 int socket_id __rte_unused,
406 unsigned flags __rte_unused)
413 /* create the mempool */
415 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
416 unsigned cache_size, unsigned private_data_size,
417 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
418 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
419 int socket_id, unsigned flags)
421 if (rte_xen_dom0_supported())
422 return rte_dom0_mempool_create(name, n, elt_size,
423 cache_size, private_data_size,
424 mp_init, mp_init_arg,
425 obj_init, obj_init_arg,
428 return rte_mempool_xmem_create(name, n, elt_size,
429 cache_size, private_data_size,
430 mp_init, mp_init_arg,
431 obj_init, obj_init_arg,
433 NULL, NULL, MEMPOOL_PG_NUM_DEFAULT,
434 MEMPOOL_PG_SHIFT_MAX);
437 /* create the internal ring */
439 rte_mempool_ring_create(struct rte_mempool *mp)
442 char rg_name[RTE_RING_NAMESIZE];
445 snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, mp->name);
448 if (mp->flags & MEMPOOL_F_SP_PUT)
449 rg_flags |= RING_F_SP_ENQ;
450 if (mp->flags & MEMPOOL_F_SC_GET)
451 rg_flags |= RING_F_SC_DEQ;
453 /* Allocate the ring that will be used to store objects.
454 * Ring functions will return appropriate errors if we are
455 * running as a secondary process etc., so no checks made
456 * in this function for that condition.
458 r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
459 mp->socket_id, rg_flags);
468 * Create the mempool over already allocated chunk of memory.
469 * That external memory buffer can consists of physically disjoint pages.
470 * Setting vaddr to NULL, makes mempool to fallback to original behaviour
471 * and allocate space for mempool and it's elements as one big chunk of
472 * physically continuos memory.
475 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
476 unsigned cache_size, unsigned private_data_size,
477 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
478 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
479 int socket_id, unsigned flags, void *vaddr,
480 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
482 char mz_name[RTE_MEMZONE_NAMESIZE];
483 struct rte_mempool_list *mempool_list;
484 struct rte_mempool *mp = NULL;
485 struct rte_tailq_entry *te = NULL;
486 const struct rte_memzone *mz;
488 int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
490 struct rte_mempool_objsz objsz;
492 int page_size = getpagesize();
494 /* compilation-time checks */
495 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
496 RTE_CACHE_LINE_MASK) != 0);
497 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
498 RTE_CACHE_LINE_MASK) != 0);
499 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
500 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
501 RTE_CACHE_LINE_MASK) != 0);
502 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
503 RTE_CACHE_LINE_MASK) != 0);
506 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
508 /* asked cache too big */
509 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
510 CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
515 /* check that we have both VA and PA */
516 if (vaddr != NULL && paddr == NULL) {
521 /* Check that pg_num and pg_shift parameters are valid. */
522 if (pg_num < RTE_DIM(mp->elt_pa) || pg_shift > MEMPOOL_PG_SHIFT_MAX) {
527 /* "no cache align" imply "no spread" */
528 if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
529 flags |= MEMPOOL_F_NO_SPREAD;
531 /* calculate mempool object sizes. */
532 if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
537 rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
540 * reserve a memory zone for this mempool: private data is
543 private_data_size = (private_data_size +
544 RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
546 if (! rte_eal_has_hugepages()) {
548 * expand private data size to a whole page, so that the
549 * first pool element will start on a new standard page
551 int head = sizeof(struct rte_mempool);
552 int new_size = (private_data_size + head) % page_size;
554 private_data_size += page_size - new_size;
557 /* try to allocate tailq entry */
558 te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
560 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
565 * If user provided an external memory buffer, then use it to
566 * store mempool objects. Otherwise reserve a memzone that is large
567 * enough to hold mempool header and metadata plus mempool objects.
569 mempool_size = MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size);
570 mempool_size += private_data_size;
571 mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
573 mempool_size += (size_t)objsz.total_size * n;
575 if (! rte_eal_has_hugepages()) {
577 * we want the memory pool to start on a page boundary,
578 * because pool elements crossing page boundaries would
579 * result in discontiguous physical addresses
581 mempool_size += page_size;
584 snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
586 mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
590 if (rte_eal_has_hugepages()) {
591 startaddr = (void*)mz->addr;
593 /* align memory pool start address on a page boundary */
594 unsigned long addr = (unsigned long)mz->addr;
595 if (addr & (page_size - 1)) {
597 addr &= ~(page_size - 1);
599 startaddr = (void*)addr;
602 /* init the mempool structure */
604 memset(mp, 0, sizeof(*mp));
605 snprintf(mp->name, sizeof(mp->name), "%s", name);
606 mp->phys_addr = mz->phys_addr;
607 mp->socket_id = socket_id;
610 mp->elt_size = objsz.elt_size;
611 mp->header_size = objsz.header_size;
612 mp->trailer_size = objsz.trailer_size;
613 mp->cache_size = cache_size;
614 mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
615 mp->private_data_size = private_data_size;
616 STAILQ_INIT(&mp->elt_list);
618 if (rte_mempool_ring_create(mp) < 0)
622 * local_cache pointer is set even if cache_size is zero.
623 * The local_cache points to just past the elt_pa[] array.
625 mp->local_cache = (struct rte_mempool_cache *)
626 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, pg_num, 0));
628 /* calculate address of the first element for continuous mempool. */
629 obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size) +
631 obj = RTE_PTR_ALIGN_CEIL(obj, RTE_MEMPOOL_ALIGN);
633 /* populate address translation fields. */
635 mp->pg_shift = pg_shift;
636 mp->pg_mask = RTE_LEN2MASK(mp->pg_shift, typeof(mp->pg_mask));
638 /* mempool elements allocated together with mempool */
640 mp->elt_va_start = (uintptr_t)obj;
641 mp->elt_pa[0] = mp->phys_addr +
642 (mp->elt_va_start - (uintptr_t)mp);
644 /* mempool elements in a separate chunk of memory. */
645 mp->elt_va_start = (uintptr_t)vaddr;
646 memcpy(mp->elt_pa, paddr, sizeof (mp->elt_pa[0]) * pg_num);
649 mp->elt_va_end = mp->elt_va_start;
651 /* call the initializer */
653 mp_init(mp, mp_init_arg);
655 mempool_populate(mp, n, 1);
657 /* call the initializer */
659 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
661 te->data = (void *) mp;
663 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
664 TAILQ_INSERT_TAIL(mempool_list, te, next);
665 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
666 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
671 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
673 rte_ring_free(mp->ring);
679 /* Return the number of entries in the mempool */
681 rte_mempool_count(const struct rte_mempool *mp)
686 count = rte_ring_count(mp->ring);
688 if (mp->cache_size == 0)
691 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
692 count += mp->local_cache[lcore_id].len;
695 * due to race condition (access to len is not locked), the
696 * total can be greater than size... so fix the result
698 if (count > mp->size)
703 /* dump the cache status */
705 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
709 unsigned cache_count;
711 fprintf(f, " cache infos:\n");
712 fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
714 if (mp->cache_size == 0)
717 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
718 cache_count = mp->local_cache[lcore_id].len;
719 fprintf(f, " cache_count[%u]=%u\n", lcore_id, cache_count);
720 count += cache_count;
722 fprintf(f, " total_cache_count=%u\n", count);
726 #ifndef __INTEL_COMPILER
727 #pragma GCC diagnostic ignored "-Wcast-qual"
730 /* check and update cookies or panic (internal) */
731 void rte_mempool_check_cookies(const struct rte_mempool *mp,
732 void * const *obj_table_const, unsigned n, int free)
734 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
735 struct rte_mempool_objhdr *hdr;
736 struct rte_mempool_objtlr *tlr;
742 /* Force to drop the "const" attribute. This is done only when
743 * DEBUG is enabled */
744 tmp = (void *) obj_table_const;
745 obj_table = (void **) tmp;
750 if (rte_mempool_from_obj(obj) != mp)
751 rte_panic("MEMPOOL: object is owned by another "
754 hdr = __mempool_get_header(obj);
755 cookie = hdr->cookie;
758 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
759 rte_log_set_history(0);
760 RTE_LOG(CRIT, MEMPOOL,
761 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
762 obj, (const void *) mp, cookie);
763 rte_panic("MEMPOOL: bad header cookie (put)\n");
765 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
766 } else if (free == 1) {
767 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
768 rte_log_set_history(0);
769 RTE_LOG(CRIT, MEMPOOL,
770 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
771 obj, (const void *) mp, cookie);
772 rte_panic("MEMPOOL: bad header cookie (get)\n");
774 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
775 } else if (free == 2) {
776 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
777 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
778 rte_log_set_history(0);
779 RTE_LOG(CRIT, MEMPOOL,
780 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
781 obj, (const void *) mp, cookie);
782 rte_panic("MEMPOOL: bad header cookie (audit)\n");
785 tlr = __mempool_get_trailer(obj);
786 cookie = tlr->cookie;
787 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
788 rte_log_set_history(0);
789 RTE_LOG(CRIT, MEMPOOL,
790 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
791 obj, (const void *) mp, cookie);
792 rte_panic("MEMPOOL: bad trailer cookie\n");
797 RTE_SET_USED(obj_table_const);
803 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
805 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
806 void *obj, __rte_unused unsigned idx)
808 __mempool_check_cookies(mp, &obj, 1, 2);
812 mempool_audit_cookies(struct rte_mempool *mp)
816 num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
817 if (num != mp->size) {
818 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
819 "iterated only over %u elements\n",
824 #define mempool_audit_cookies(mp) do {} while(0)
827 #ifndef __INTEL_COMPILER
828 #pragma GCC diagnostic error "-Wcast-qual"
831 /* check cookies before and after objects */
833 mempool_audit_cache(const struct rte_mempool *mp)
835 /* check cache size consistency */
838 if (mp->cache_size == 0)
841 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
842 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
843 RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
845 rte_panic("MEMPOOL: invalid cache len\n");
850 /* check the consistency of mempool (size, cookies, ...) */
852 rte_mempool_audit(struct rte_mempool *mp)
854 mempool_audit_cache(mp);
855 mempool_audit_cookies(mp);
857 /* For case where mempool DEBUG is not set, and cache size is 0 */
861 /* dump the status of the mempool on the console */
863 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
865 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
866 struct rte_mempool_debug_stats sum;
869 unsigned common_count;
870 unsigned cache_count;
872 RTE_ASSERT(f != NULL);
873 RTE_ASSERT(mp != NULL);
875 fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
876 fprintf(f, " flags=%x\n", mp->flags);
877 fprintf(f, " ring=<%s>@%p\n", mp->ring->name, mp->ring);
878 fprintf(f, " phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
879 fprintf(f, " size=%"PRIu32"\n", mp->size);
880 fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
881 fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
882 fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
883 fprintf(f, " total_obj_size=%"PRIu32"\n",
884 mp->header_size + mp->elt_size + mp->trailer_size);
886 fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
887 fprintf(f, " pg_num=%"PRIu32"\n", mp->pg_num);
888 fprintf(f, " pg_shift=%"PRIu32"\n", mp->pg_shift);
889 fprintf(f, " pg_mask=%#tx\n", mp->pg_mask);
890 fprintf(f, " elt_va_start=%#tx\n", mp->elt_va_start);
891 fprintf(f, " elt_va_end=%#tx\n", mp->elt_va_end);
892 fprintf(f, " elt_pa[0]=0x%" PRIx64 "\n", mp->elt_pa[0]);
895 fprintf(f, " avg bytes/object=%#Lf\n",
896 (long double)(mp->elt_va_end - mp->elt_va_start) /
899 cache_count = rte_mempool_dump_cache(f, mp);
900 common_count = rte_ring_count(mp->ring);
901 if ((cache_count + common_count) > mp->size)
902 common_count = mp->size - cache_count;
903 fprintf(f, " common_pool_count=%u\n", common_count);
905 /* sum and dump statistics */
906 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
907 memset(&sum, 0, sizeof(sum));
908 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
909 sum.put_bulk += mp->stats[lcore_id].put_bulk;
910 sum.put_objs += mp->stats[lcore_id].put_objs;
911 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
912 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
913 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
914 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
916 fprintf(f, " stats:\n");
917 fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
918 fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
919 fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
920 fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
921 fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
922 fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
924 fprintf(f, " no statistics available\n");
927 rte_mempool_audit(mp);
930 /* dump the status of all mempools on the console */
932 rte_mempool_list_dump(FILE *f)
934 struct rte_mempool *mp = NULL;
935 struct rte_tailq_entry *te;
936 struct rte_mempool_list *mempool_list;
938 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
940 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
942 TAILQ_FOREACH(te, mempool_list, next) {
943 mp = (struct rte_mempool *) te->data;
944 rte_mempool_dump(f, mp);
947 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
950 /* search a mempool from its name */
952 rte_mempool_lookup(const char *name)
954 struct rte_mempool *mp = NULL;
955 struct rte_tailq_entry *te;
956 struct rte_mempool_list *mempool_list;
958 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
960 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
962 TAILQ_FOREACH(te, mempool_list, next) {
963 mp = (struct rte_mempool *) te->data;
964 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
968 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
978 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
981 struct rte_tailq_entry *te = NULL;
982 struct rte_mempool_list *mempool_list;
984 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
986 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
988 TAILQ_FOREACH(te, mempool_list, next) {
989 (*func)((struct rte_mempool *) te->data, arg);
992 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);