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, uint32_t obj_idx,
140 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg)
142 struct rte_mempool_objhdr *hdr;
143 struct rte_mempool_objtlr *tlr __rte_unused;
145 obj = (char *)obj + mp->header_size;
147 /* set mempool ptr in header */
148 hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
150 STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
152 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
153 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
154 tlr = __mempool_get_trailer(obj);
155 tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
157 /* call the initializer */
159 obj_init(mp, obj_init_arg, obj, obj_idx);
161 /* enqueue in ring */
162 rte_ring_sp_enqueue(mp->ring, obj);
165 /* Iterate through objects at the given address
167 * Given the pointer to the memory, and its topology in physical memory
168 * (the physical addresses table), iterate through the "elt_num" objects
169 * of size "elt_sz" aligned at "align". For each object in this memory
170 * chunk, invoke a callback. It returns the effective number of objects
174 rte_mempool_obj_mem_iter(void *vaddr, uint32_t elt_num, size_t total_elt_sz,
175 size_t align, const phys_addr_t paddr[], uint32_t pg_num,
176 uint32_t pg_shift, rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg)
180 uintptr_t end, start, va;
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 if (obj_iter != NULL)
218 obj_iter(obj_iter_arg, (void *)start,
224 va = RTE_ALIGN_CEIL((va + 1), pg_sz);
232 /* call obj_cb() for each mempool element */
234 rte_mempool_obj_iter(struct rte_mempool *mp,
235 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
237 struct rte_mempool_objhdr *hdr;
241 STAILQ_FOREACH(hdr, &mp->elt_list, next) {
242 obj = (char *)hdr + sizeof(*hdr);
243 obj_cb(mp, obj_cb_arg, obj, n);
251 * Populate mempool with the objects.
254 struct mempool_populate_arg {
255 struct rte_mempool *mp;
256 rte_mempool_obj_cb_t *obj_init;
261 mempool_obj_populate(void *arg, void *start, void *end, uint32_t idx)
263 struct mempool_populate_arg *pa = arg;
265 mempool_add_elem(pa->mp, start, idx, pa->obj_init, pa->obj_init_arg);
266 pa->mp->elt_va_end = (uintptr_t)end;
270 mempool_populate(struct rte_mempool *mp, size_t num, size_t align,
271 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg)
274 struct mempool_populate_arg arg;
276 elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
278 arg.obj_init = obj_init;
279 arg.obj_init_arg = obj_init_arg;
281 mp->size = rte_mempool_obj_mem_iter((void *)mp->elt_va_start,
283 mp->elt_pa, mp->pg_num, mp->pg_shift,
284 mempool_obj_populate, &arg);
287 /* get the header, trailer and total size of a mempool element. */
289 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
290 struct rte_mempool_objsz *sz)
292 struct rte_mempool_objsz lsz;
294 sz = (sz != NULL) ? sz : &lsz;
296 sz->header_size = sizeof(struct rte_mempool_objhdr);
297 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
298 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
301 sz->trailer_size = sizeof(struct rte_mempool_objtlr);
303 /* element size is 8 bytes-aligned at least */
304 sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
306 /* expand trailer to next cache line */
307 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
308 sz->total_size = sz->header_size + sz->elt_size +
310 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
311 (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
312 RTE_MEMPOOL_ALIGN_MASK);
316 * increase trailer to add padding between objects in order to
317 * spread them across memory channels/ranks
319 if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
321 new_size = optimize_object_size(sz->header_size + sz->elt_size +
323 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
326 if (! rte_eal_has_hugepages()) {
328 * compute trailer size so that pool elements fit exactly in
331 int page_size = getpagesize();
332 int new_size = page_size - sz->header_size - sz->elt_size;
333 if (new_size < 0 || (unsigned int)new_size < sz->trailer_size) {
334 printf("When hugepages are disabled, pool objects "
335 "can't exceed PAGE_SIZE: %d + %d + %d > %d\n",
336 sz->header_size, sz->elt_size, sz->trailer_size,
340 sz->trailer_size = new_size;
343 /* this is the size of an object, including header and trailer */
344 sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
346 return sz->total_size;
351 * Calculate maximum amount of memory required to store given number of objects.
354 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift)
356 size_t n, pg_num, pg_sz, sz;
358 pg_sz = (size_t)1 << pg_shift;
360 if ((n = pg_sz / total_elt_sz) > 0) {
361 pg_num = (elt_num + n - 1) / n;
362 sz = pg_num << pg_shift;
364 sz = RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
370 /* Callback used by rte_mempool_xmem_usage(): it sets the opaque
371 * argument to the end of the object.
374 mempool_lelem_iter(void *arg, __rte_unused void *start, void *end,
375 __rte_unused uint32_t idx)
377 *(uintptr_t *)arg = (uintptr_t)end;
381 * Calculate how much memory would be actually required with the
382 * given memory footprint to store required number of elements.
385 rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t total_elt_sz,
386 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
392 pg_sz = (size_t)1 << pg_shift;
393 va = (uintptr_t)vaddr;
396 if ((n = rte_mempool_obj_mem_iter(vaddr, elt_num, total_elt_sz, 1,
397 paddr, pg_num, pg_shift, mempool_lelem_iter,
402 uv = RTE_ALIGN_CEIL(uv, pg_sz);
407 #ifndef RTE_LIBRTE_XEN_DOM0
408 /* stub if DOM0 support not configured */
410 rte_dom0_mempool_create(const char *name __rte_unused,
411 unsigned n __rte_unused,
412 unsigned elt_size __rte_unused,
413 unsigned cache_size __rte_unused,
414 unsigned private_data_size __rte_unused,
415 rte_mempool_ctor_t *mp_init __rte_unused,
416 void *mp_init_arg __rte_unused,
417 rte_mempool_obj_ctor_t *obj_init __rte_unused,
418 void *obj_init_arg __rte_unused,
419 int socket_id __rte_unused,
420 unsigned flags __rte_unused)
427 /* create the mempool */
429 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
430 unsigned cache_size, unsigned private_data_size,
431 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
432 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
433 int socket_id, unsigned flags)
435 if (rte_xen_dom0_supported())
436 return rte_dom0_mempool_create(name, n, elt_size,
437 cache_size, private_data_size,
438 mp_init, mp_init_arg,
439 obj_init, obj_init_arg,
442 return rte_mempool_xmem_create(name, n, elt_size,
443 cache_size, private_data_size,
444 mp_init, mp_init_arg,
445 obj_init, obj_init_arg,
447 NULL, NULL, MEMPOOL_PG_NUM_DEFAULT,
448 MEMPOOL_PG_SHIFT_MAX);
452 * Create the mempool over already allocated chunk of memory.
453 * That external memory buffer can consists of physically disjoint pages.
454 * Setting vaddr to NULL, makes mempool to fallback to original behaviour
455 * and allocate space for mempool and it's elements as one big chunk of
456 * physically continuos memory.
459 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
460 unsigned cache_size, unsigned private_data_size,
461 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
462 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
463 int socket_id, unsigned flags, void *vaddr,
464 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
466 char mz_name[RTE_MEMZONE_NAMESIZE];
467 char rg_name[RTE_RING_NAMESIZE];
468 struct rte_mempool_list *mempool_list;
469 struct rte_mempool *mp = NULL;
470 struct rte_tailq_entry *te = NULL;
471 struct rte_ring *r = NULL;
472 const struct rte_memzone *mz;
474 int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
477 struct rte_mempool_objsz objsz;
479 int page_size = getpagesize();
481 /* compilation-time checks */
482 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
483 RTE_CACHE_LINE_MASK) != 0);
484 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
485 RTE_CACHE_LINE_MASK) != 0);
486 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
487 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
488 RTE_CACHE_LINE_MASK) != 0);
489 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
490 RTE_CACHE_LINE_MASK) != 0);
493 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
495 /* asked cache too big */
496 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
497 CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
502 /* check that we have both VA and PA */
503 if (vaddr != NULL && paddr == NULL) {
508 /* Check that pg_num and pg_shift parameters are valid. */
509 if (pg_num < RTE_DIM(mp->elt_pa) || pg_shift > MEMPOOL_PG_SHIFT_MAX) {
514 /* "no cache align" imply "no spread" */
515 if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
516 flags |= MEMPOOL_F_NO_SPREAD;
519 if (flags & MEMPOOL_F_SP_PUT)
520 rg_flags |= RING_F_SP_ENQ;
521 if (flags & MEMPOOL_F_SC_GET)
522 rg_flags |= RING_F_SC_DEQ;
524 /* calculate mempool object sizes. */
525 if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
530 rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
532 /* allocate the ring that will be used to store objects */
533 /* Ring functions will return appropriate errors if we are
534 * running as a secondary process etc., so no checks made
535 * in this function for that condition */
536 snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
537 r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
542 * reserve a memory zone for this mempool: private data is
545 private_data_size = (private_data_size +
546 RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
548 if (! rte_eal_has_hugepages()) {
550 * expand private data size to a whole page, so that the
551 * first pool element will start on a new standard page
553 int head = sizeof(struct rte_mempool);
554 int new_size = (private_data_size + head) % page_size;
556 private_data_size += page_size - new_size;
559 /* try to allocate tailq entry */
560 te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
562 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
567 * If user provided an external memory buffer, then use it to
568 * store mempool objects. Otherwise reserve a memzone that is large
569 * enough to hold mempool header and metadata plus mempool objects.
571 mempool_size = MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size);
572 mempool_size += private_data_size;
573 mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
575 mempool_size += (size_t)objsz.total_size * n;
577 if (! rte_eal_has_hugepages()) {
579 * we want the memory pool to start on a page boundary,
580 * because pool elements crossing page boundaries would
581 * result in discontiguous physical addresses
583 mempool_size += page_size;
586 snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
588 mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
592 if (rte_eal_has_hugepages()) {
593 startaddr = (void*)mz->addr;
595 /* align memory pool start address on a page boundary */
596 unsigned long addr = (unsigned long)mz->addr;
597 if (addr & (page_size - 1)) {
599 addr &= ~(page_size - 1);
601 startaddr = (void*)addr;
604 /* init the mempool structure */
606 memset(mp, 0, sizeof(*mp));
607 snprintf(mp->name, sizeof(mp->name), "%s", name);
608 mp->phys_addr = mz->phys_addr;
612 mp->elt_size = objsz.elt_size;
613 mp->header_size = objsz.header_size;
614 mp->trailer_size = objsz.trailer_size;
615 mp->cache_size = cache_size;
616 mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
617 mp->private_data_size = private_data_size;
618 STAILQ_INIT(&mp->elt_list);
621 * local_cache pointer is set even if cache_size is zero.
622 * The local_cache points to just past the elt_pa[] array.
624 mp->local_cache = (struct rte_mempool_cache *)
625 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, pg_num, 0));
627 /* calculate address of the first element for continuous mempool. */
628 obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size) +
630 obj = RTE_PTR_ALIGN_CEIL(obj, RTE_MEMPOOL_ALIGN);
632 /* populate address translation fields. */
634 mp->pg_shift = pg_shift;
635 mp->pg_mask = RTE_LEN2MASK(mp->pg_shift, typeof(mp->pg_mask));
637 /* mempool elements allocated together with mempool */
639 mp->elt_va_start = (uintptr_t)obj;
640 mp->elt_pa[0] = mp->phys_addr +
641 (mp->elt_va_start - (uintptr_t)mp);
643 /* mempool elements in a separate chunk of memory. */
644 mp->elt_va_start = (uintptr_t)vaddr;
645 memcpy(mp->elt_pa, paddr, sizeof (mp->elt_pa[0]) * pg_num);
648 mp->elt_va_end = mp->elt_va_start;
650 /* call the initializer */
652 mp_init(mp, mp_init_arg);
654 mempool_populate(mp, n, 1, obj_init, obj_init_arg);
656 te->data = (void *) mp;
658 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
659 TAILQ_INSERT_TAIL(mempool_list, te, next);
660 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
661 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
666 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
673 /* Return the number of entries in the mempool */
675 rte_mempool_count(const struct rte_mempool *mp)
680 count = rte_ring_count(mp->ring);
682 if (mp->cache_size == 0)
685 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
686 count += mp->local_cache[lcore_id].len;
689 * due to race condition (access to len is not locked), the
690 * total can be greater than size... so fix the result
692 if (count > mp->size)
697 /* dump the cache status */
699 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
703 unsigned cache_count;
705 fprintf(f, " cache infos:\n");
706 fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
708 if (mp->cache_size == 0)
711 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
712 cache_count = mp->local_cache[lcore_id].len;
713 fprintf(f, " cache_count[%u]=%u\n", lcore_id, cache_count);
714 count += cache_count;
716 fprintf(f, " total_cache_count=%u\n", count);
720 #ifndef __INTEL_COMPILER
721 #pragma GCC diagnostic ignored "-Wcast-qual"
724 struct mempool_audit_arg {
725 const struct rte_mempool *mp;
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(void *arg, void *start, void *end, uint32_t idx)
807 struct mempool_audit_arg *pa = arg;
810 obj = (char *)start + pa->mp->header_size;
811 pa->obj_end = (uintptr_t)end;
812 pa->obj_num = idx + 1;
813 __mempool_check_cookies(pa->mp, &obj, 1, 2);
817 mempool_audit_cookies(struct rte_mempool *mp)
819 uint32_t elt_sz, num;
820 struct mempool_audit_arg arg;
822 elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
825 arg.obj_end = mp->elt_va_start;
828 num = rte_mempool_obj_mem_iter((void *)mp->elt_va_start,
830 mp->elt_pa, mp->pg_num, mp->pg_shift,
831 mempool_obj_audit, &arg);
833 if (num != mp->size) {
834 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
835 "iterated only over %u elements\n",
837 } else if (arg.obj_end != mp->elt_va_end || arg.obj_num != mp->size) {
838 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
839 "last callback va_end: %#tx (%#tx expeceted), "
840 "num of objects: %u (%u expected)\n",
842 arg.obj_end, mp->elt_va_end,
843 arg.obj_num, mp->size);
847 #define mempool_audit_cookies(mp) do {} while(0)
850 #ifndef __INTEL_COMPILER
851 #pragma GCC diagnostic error "-Wcast-qual"
854 /* check cookies before and after objects */
856 mempool_audit_cache(const struct rte_mempool *mp)
858 /* check cache size consistency */
861 if (mp->cache_size == 0)
864 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
865 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
866 RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
868 rte_panic("MEMPOOL: invalid cache len\n");
873 /* check the consistency of mempool (size, cookies, ...) */
875 rte_mempool_audit(struct rte_mempool *mp)
877 mempool_audit_cache(mp);
878 mempool_audit_cookies(mp);
880 /* For case where mempool DEBUG is not set, and cache size is 0 */
884 /* dump the status of the mempool on the console */
886 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
888 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
889 struct rte_mempool_debug_stats sum;
892 unsigned common_count;
893 unsigned cache_count;
895 RTE_ASSERT(f != NULL);
896 RTE_ASSERT(mp != NULL);
898 fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
899 fprintf(f, " flags=%x\n", mp->flags);
900 fprintf(f, " ring=<%s>@%p\n", mp->ring->name, mp->ring);
901 fprintf(f, " phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
902 fprintf(f, " size=%"PRIu32"\n", mp->size);
903 fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
904 fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
905 fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
906 fprintf(f, " total_obj_size=%"PRIu32"\n",
907 mp->header_size + mp->elt_size + mp->trailer_size);
909 fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
910 fprintf(f, " pg_num=%"PRIu32"\n", mp->pg_num);
911 fprintf(f, " pg_shift=%"PRIu32"\n", mp->pg_shift);
912 fprintf(f, " pg_mask=%#tx\n", mp->pg_mask);
913 fprintf(f, " elt_va_start=%#tx\n", mp->elt_va_start);
914 fprintf(f, " elt_va_end=%#tx\n", mp->elt_va_end);
915 fprintf(f, " elt_pa[0]=0x%" PRIx64 "\n", mp->elt_pa[0]);
918 fprintf(f, " avg bytes/object=%#Lf\n",
919 (long double)(mp->elt_va_end - mp->elt_va_start) /
922 cache_count = rte_mempool_dump_cache(f, mp);
923 common_count = rte_ring_count(mp->ring);
924 if ((cache_count + common_count) > mp->size)
925 common_count = mp->size - cache_count;
926 fprintf(f, " common_pool_count=%u\n", common_count);
928 /* sum and dump statistics */
929 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
930 memset(&sum, 0, sizeof(sum));
931 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
932 sum.put_bulk += mp->stats[lcore_id].put_bulk;
933 sum.put_objs += mp->stats[lcore_id].put_objs;
934 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
935 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
936 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
937 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
939 fprintf(f, " stats:\n");
940 fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
941 fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
942 fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
943 fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
944 fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
945 fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
947 fprintf(f, " no statistics available\n");
950 rte_mempool_audit(mp);
953 /* dump the status of all mempools on the console */
955 rte_mempool_list_dump(FILE *f)
957 struct rte_mempool *mp = NULL;
958 struct rte_tailq_entry *te;
959 struct rte_mempool_list *mempool_list;
961 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
963 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
965 TAILQ_FOREACH(te, mempool_list, next) {
966 mp = (struct rte_mempool *) te->data;
967 rte_mempool_dump(f, mp);
970 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
973 /* search a mempool from its name */
975 rte_mempool_lookup(const char *name)
977 struct rte_mempool *mp = NULL;
978 struct rte_tailq_entry *te;
979 struct rte_mempool_list *mempool_list;
981 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
983 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
985 TAILQ_FOREACH(te, mempool_list, next) {
986 mp = (struct rte_mempool *) te->data;
987 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
991 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1001 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1004 struct rte_tailq_entry *te = NULL;
1005 struct rte_mempool_list *mempool_list;
1007 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1009 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1011 TAILQ_FOREACH(te, mempool_list, next) {
1012 (*func)((struct rte_mempool *) te->data, arg);
1015 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);