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 mempool_add_elem(struct rte_mempool *mp, void *obj, phys_addr_t physaddr)
133 struct rte_mempool_objhdr *hdr;
134 struct rte_mempool_objtlr *tlr __rte_unused;
136 /* set mempool ptr in header */
137 hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
139 hdr->physaddr = physaddr;
140 STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
141 mp->populated_size++;
143 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
144 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
145 tlr = __mempool_get_trailer(obj);
146 tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
149 /* enqueue in ring */
150 rte_ring_sp_enqueue(mp->ring, obj);
153 /* call obj_cb() for each mempool element */
155 rte_mempool_obj_iter(struct rte_mempool *mp,
156 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
158 struct rte_mempool_objhdr *hdr;
162 STAILQ_FOREACH(hdr, &mp->elt_list, next) {
163 obj = (char *)hdr + sizeof(*hdr);
164 obj_cb(mp, obj_cb_arg, obj, n);
171 /* call mem_cb() for each mempool memory chunk */
173 rte_mempool_mem_iter(struct rte_mempool *mp,
174 rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg)
176 struct rte_mempool_memhdr *hdr;
179 STAILQ_FOREACH(hdr, &mp->mem_list, next) {
180 mem_cb(mp, mem_cb_arg, hdr, n);
187 /* get the header, trailer and total size of a mempool element. */
189 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
190 struct rte_mempool_objsz *sz)
192 struct rte_mempool_objsz lsz;
194 sz = (sz != NULL) ? sz : &lsz;
196 sz->header_size = sizeof(struct rte_mempool_objhdr);
197 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
198 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
201 sz->trailer_size = sizeof(struct rte_mempool_objtlr);
203 /* element size is 8 bytes-aligned at least */
204 sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
206 /* expand trailer to next cache line */
207 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
208 sz->total_size = sz->header_size + sz->elt_size +
210 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
211 (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
212 RTE_MEMPOOL_ALIGN_MASK);
216 * increase trailer to add padding between objects in order to
217 * spread them across memory channels/ranks
219 if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
221 new_size = optimize_object_size(sz->header_size + sz->elt_size +
223 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
226 if (! rte_eal_has_hugepages()) {
228 * compute trailer size so that pool elements fit exactly in
231 int page_size = getpagesize();
232 int new_size = page_size - sz->header_size - sz->elt_size;
233 if (new_size < 0 || (unsigned int)new_size < sz->trailer_size) {
234 printf("When hugepages are disabled, pool objects "
235 "can't exceed PAGE_SIZE: %d + %d + %d > %d\n",
236 sz->header_size, sz->elt_size, sz->trailer_size,
240 sz->trailer_size = new_size;
243 /* this is the size of an object, including header and trailer */
244 sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
246 return sz->total_size;
251 * Calculate maximum amount of memory required to store given number of objects.
254 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift)
256 size_t n, pg_num, pg_sz, sz;
258 pg_sz = (size_t)1 << pg_shift;
260 if ((n = pg_sz / total_elt_sz) > 0) {
261 pg_num = (elt_num + n - 1) / n;
262 sz = pg_num << pg_shift;
264 sz = RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
271 * Calculate how much memory would be actually required with the
272 * given memory footprint to store required number of elements.
275 rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num,
276 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
279 uint32_t elt_cnt = 0;
280 phys_addr_t start, end;
282 size_t pg_sz = (size_t)1 << pg_shift;
284 /* if paddr is NULL, assume contiguous memory */
287 end = pg_sz * pg_num;
291 end = paddr[0] + pg_sz;
294 while (elt_cnt < elt_num) {
296 if (end - start >= total_elt_sz) {
297 /* enough contiguous memory, add an object */
298 start += total_elt_sz;
300 } else if (paddr_idx < pg_num) {
301 /* no room to store one obj, add a page */
302 if (end == paddr[paddr_idx]) {
305 start = paddr[paddr_idx];
306 end = paddr[paddr_idx] + pg_sz;
311 /* no more page, return how many elements fit */
312 return -(size_t)elt_cnt;
316 return (size_t)paddr_idx << pg_shift;
319 #ifndef RTE_LIBRTE_XEN_DOM0
320 /* stub if DOM0 support not configured */
322 rte_dom0_mempool_create(const char *name __rte_unused,
323 unsigned n __rte_unused,
324 unsigned elt_size __rte_unused,
325 unsigned cache_size __rte_unused,
326 unsigned private_data_size __rte_unused,
327 rte_mempool_ctor_t *mp_init __rte_unused,
328 void *mp_init_arg __rte_unused,
329 rte_mempool_obj_ctor_t *obj_init __rte_unused,
330 void *obj_init_arg __rte_unused,
331 int socket_id __rte_unused,
332 unsigned flags __rte_unused)
339 /* create the mempool */
341 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
342 unsigned cache_size, unsigned private_data_size,
343 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
344 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
345 int socket_id, unsigned flags)
347 if (rte_xen_dom0_supported())
348 return rte_dom0_mempool_create(name, n, elt_size,
349 cache_size, private_data_size,
350 mp_init, mp_init_arg,
351 obj_init, obj_init_arg,
354 return rte_mempool_xmem_create(name, n, elt_size,
355 cache_size, private_data_size,
356 mp_init, mp_init_arg,
357 obj_init, obj_init_arg,
359 NULL, NULL, MEMPOOL_PG_NUM_DEFAULT,
360 MEMPOOL_PG_SHIFT_MAX);
363 /* create the internal ring */
365 rte_mempool_ring_create(struct rte_mempool *mp)
368 char rg_name[RTE_RING_NAMESIZE];
371 snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, mp->name);
374 if (mp->flags & MEMPOOL_F_SP_PUT)
375 rg_flags |= RING_F_SP_ENQ;
376 if (mp->flags & MEMPOOL_F_SC_GET)
377 rg_flags |= RING_F_SC_DEQ;
379 /* Allocate the ring that will be used to store objects.
380 * Ring functions will return appropriate errors if we are
381 * running as a secondary process etc., so no checks made
382 * in this function for that condition.
384 r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
385 mp->socket_id, rg_flags);
393 /* Free memory chunks used by a mempool. Objects must be in pool */
395 rte_mempool_free_memchunks(struct rte_mempool *mp)
397 struct rte_mempool_memhdr *memhdr;
400 while (!STAILQ_EMPTY(&mp->elt_list)) {
401 rte_ring_sc_dequeue(mp->ring, &elt);
403 STAILQ_REMOVE_HEAD(&mp->elt_list, next);
404 mp->populated_size--;
407 while (!STAILQ_EMPTY(&mp->mem_list)) {
408 memhdr = STAILQ_FIRST(&mp->mem_list);
409 STAILQ_REMOVE_HEAD(&mp->mem_list, next);
415 /* Add objects in the pool, using a physically contiguous memory
416 * zone. Return the number of objects added, or a negative value
420 rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
421 phys_addr_t paddr, size_t len)
423 unsigned total_elt_sz;
426 struct rte_mempool_memhdr *memhdr;
428 /* mempool is already populated */
429 if (mp->populated_size >= mp->size)
432 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
434 memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
439 memhdr->addr = vaddr;
440 memhdr->phys_addr = paddr;
443 if (mp->flags & MEMPOOL_F_NO_CACHE_ALIGN)
444 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
446 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_CACHE_LINE_SIZE) - vaddr;
448 while (off + total_elt_sz <= len && mp->populated_size < mp->size) {
449 off += mp->header_size;
450 mempool_add_elem(mp, (char *)vaddr + off, paddr + off);
451 off += mp->elt_size + mp->trailer_size;
455 /* not enough room to store one object */
459 STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
464 /* Add objects in the pool, using a table of physical pages. Return the
465 * number of objects added, or a negative value on error.
468 rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
469 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
473 size_t pg_sz = (size_t)1 << pg_shift;
475 /* mempool must not be populated */
476 if (mp->nb_mem_chunks != 0)
479 for (i = 0; i < pg_num && mp->populated_size < mp->size; i += n) {
481 /* populate with the largest group of contiguous pages */
482 for (n = 1; (i + n) < pg_num &&
483 paddr[i] + pg_sz == paddr[i+n]; n++)
486 ret = rte_mempool_populate_phys(mp, vaddr + i * pg_sz,
487 paddr[i], n * pg_sz);
489 rte_mempool_free_memchunks(mp);
498 * Create the mempool over already allocated chunk of memory.
499 * That external memory buffer can consists of physically disjoint pages.
500 * Setting vaddr to NULL, makes mempool to fallback to original behaviour
501 * and allocate space for mempool and it's elements as one big chunk of
502 * physically continuos memory.
505 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
506 unsigned cache_size, unsigned private_data_size,
507 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
508 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
509 int socket_id, unsigned flags, void *vaddr,
510 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
512 char mz_name[RTE_MEMZONE_NAMESIZE];
513 struct rte_mempool_list *mempool_list;
514 struct rte_mempool *mp = NULL;
515 struct rte_tailq_entry *te = NULL;
516 const struct rte_memzone *mz;
518 int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
520 struct rte_mempool_objsz objsz;
522 int page_size = getpagesize();
525 /* compilation-time checks */
526 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
527 RTE_CACHE_LINE_MASK) != 0);
528 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
529 RTE_CACHE_LINE_MASK) != 0);
530 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
531 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
532 RTE_CACHE_LINE_MASK) != 0);
533 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
534 RTE_CACHE_LINE_MASK) != 0);
537 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
539 /* asked cache too big */
540 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
541 CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
546 /* check that we have both VA and PA */
547 if (vaddr != NULL && paddr == NULL) {
552 /* Check that pg_num and pg_shift parameters are valid. */
553 if (pg_num == 0 || pg_shift > MEMPOOL_PG_SHIFT_MAX) {
558 /* "no cache align" imply "no spread" */
559 if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
560 flags |= MEMPOOL_F_NO_SPREAD;
562 /* calculate mempool object sizes. */
563 if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
568 rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
571 * reserve a memory zone for this mempool: private data is
574 private_data_size = (private_data_size +
575 RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
577 if (! rte_eal_has_hugepages()) {
579 * expand private data size to a whole page, so that the
580 * first pool element will start on a new standard page
582 int head = sizeof(struct rte_mempool);
583 int new_size = (private_data_size + head) % page_size;
585 private_data_size += page_size - new_size;
588 /* try to allocate tailq entry */
589 te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
591 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
596 * If user provided an external memory buffer, then use it to
597 * store mempool objects. Otherwise reserve a memzone that is large
598 * enough to hold mempool header and metadata plus mempool objects.
600 mempool_size = MEMPOOL_HEADER_SIZE(mp, cache_size);
601 mempool_size += private_data_size;
602 mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
604 mempool_size += (size_t)objsz.total_size * n;
606 if (! rte_eal_has_hugepages()) {
608 * we want the memory pool to start on a page boundary,
609 * because pool elements crossing page boundaries would
610 * result in discontiguous physical addresses
612 mempool_size += page_size;
615 snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
617 mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
621 if (rte_eal_has_hugepages()) {
622 startaddr = (void*)mz->addr;
624 /* align memory pool start address on a page boundary */
625 unsigned long addr = (unsigned long)mz->addr;
626 if (addr & (page_size - 1)) {
628 addr &= ~(page_size - 1);
630 startaddr = (void*)addr;
633 /* init the mempool structure */
635 memset(mp, 0, sizeof(*mp));
636 snprintf(mp->name, sizeof(mp->name), "%s", name);
637 mp->phys_addr = mz->phys_addr;
638 mp->socket_id = socket_id;
641 mp->elt_size = objsz.elt_size;
642 mp->header_size = objsz.header_size;
643 mp->trailer_size = objsz.trailer_size;
644 mp->cache_size = cache_size;
645 mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
646 mp->private_data_size = private_data_size;
647 STAILQ_INIT(&mp->elt_list);
648 STAILQ_INIT(&mp->mem_list);
650 if (rte_mempool_ring_create(mp) < 0)
654 * local_cache pointer is set even if cache_size is zero.
655 * The local_cache points to just past the elt_pa[] array.
657 mp->local_cache = (struct rte_mempool_cache *)
658 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
660 /* call the initializer */
662 mp_init(mp, mp_init_arg);
664 /* mempool elements allocated together with mempool */
666 /* calculate address of the first elt for continuous mempool. */
667 obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, cache_size) +
669 obj = RTE_PTR_ALIGN_CEIL(obj, RTE_MEMPOOL_ALIGN);
671 ret = rte_mempool_populate_phys(mp, obj,
672 mp->phys_addr + ((char *)obj - (char *)mp),
673 objsz.total_size * n);
674 if (ret != (int)mp->size)
677 ret = rte_mempool_populate_phys_tab(mp, vaddr,
678 paddr, pg_num, pg_shift);
679 if (ret != (int)mp->size)
683 /* call the initializer */
685 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
687 te->data = (void *) mp;
689 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
690 TAILQ_INSERT_TAIL(mempool_list, te, next);
691 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
692 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
697 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
699 rte_mempool_free_memchunks(mp);
700 rte_ring_free(mp->ring);
707 /* Return the number of entries in the mempool */
709 rte_mempool_count(const struct rte_mempool *mp)
714 count = rte_ring_count(mp->ring);
716 if (mp->cache_size == 0)
719 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
720 count += mp->local_cache[lcore_id].len;
723 * due to race condition (access to len is not locked), the
724 * total can be greater than size... so fix the result
726 if (count > mp->size)
731 /* dump the cache status */
733 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
737 unsigned cache_count;
739 fprintf(f, " cache infos:\n");
740 fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
742 if (mp->cache_size == 0)
745 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
746 cache_count = mp->local_cache[lcore_id].len;
747 fprintf(f, " cache_count[%u]=%u\n", lcore_id, cache_count);
748 count += cache_count;
750 fprintf(f, " total_cache_count=%u\n", count);
754 #ifndef __INTEL_COMPILER
755 #pragma GCC diagnostic ignored "-Wcast-qual"
758 /* check and update cookies or panic (internal) */
759 void rte_mempool_check_cookies(const struct rte_mempool *mp,
760 void * const *obj_table_const, unsigned n, int free)
762 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
763 struct rte_mempool_objhdr *hdr;
764 struct rte_mempool_objtlr *tlr;
770 /* Force to drop the "const" attribute. This is done only when
771 * DEBUG is enabled */
772 tmp = (void *) obj_table_const;
773 obj_table = (void **) tmp;
778 if (rte_mempool_from_obj(obj) != mp)
779 rte_panic("MEMPOOL: object is owned by another "
782 hdr = __mempool_get_header(obj);
783 cookie = hdr->cookie;
786 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
787 rte_log_set_history(0);
788 RTE_LOG(CRIT, MEMPOOL,
789 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
790 obj, (const void *) mp, cookie);
791 rte_panic("MEMPOOL: bad header cookie (put)\n");
793 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
794 } else if (free == 1) {
795 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
796 rte_log_set_history(0);
797 RTE_LOG(CRIT, MEMPOOL,
798 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
799 obj, (const void *) mp, cookie);
800 rte_panic("MEMPOOL: bad header cookie (get)\n");
802 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
803 } else if (free == 2) {
804 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
805 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
806 rte_log_set_history(0);
807 RTE_LOG(CRIT, MEMPOOL,
808 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
809 obj, (const void *) mp, cookie);
810 rte_panic("MEMPOOL: bad header cookie (audit)\n");
813 tlr = __mempool_get_trailer(obj);
814 cookie = tlr->cookie;
815 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
816 rte_log_set_history(0);
817 RTE_LOG(CRIT, MEMPOOL,
818 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
819 obj, (const void *) mp, cookie);
820 rte_panic("MEMPOOL: bad trailer cookie\n");
825 RTE_SET_USED(obj_table_const);
831 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
833 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
834 void *obj, __rte_unused unsigned idx)
836 __mempool_check_cookies(mp, &obj, 1, 2);
840 mempool_audit_cookies(struct rte_mempool *mp)
844 num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
845 if (num != mp->size) {
846 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
847 "iterated only over %u elements\n",
852 #define mempool_audit_cookies(mp) do {} while(0)
855 #ifndef __INTEL_COMPILER
856 #pragma GCC diagnostic error "-Wcast-qual"
859 /* check cookies before and after objects */
861 mempool_audit_cache(const struct rte_mempool *mp)
863 /* check cache size consistency */
866 if (mp->cache_size == 0)
869 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
870 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
871 RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
873 rte_panic("MEMPOOL: invalid cache len\n");
878 /* check the consistency of mempool (size, cookies, ...) */
880 rte_mempool_audit(struct rte_mempool *mp)
882 mempool_audit_cache(mp);
883 mempool_audit_cookies(mp);
885 /* For case where mempool DEBUG is not set, and cache size is 0 */
889 /* dump the status of the mempool on the console */
891 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
893 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
894 struct rte_mempool_debug_stats sum;
897 struct rte_mempool_memhdr *memhdr;
898 unsigned common_count;
899 unsigned cache_count;
902 RTE_ASSERT(f != NULL);
903 RTE_ASSERT(mp != NULL);
905 fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
906 fprintf(f, " flags=%x\n", mp->flags);
907 fprintf(f, " ring=<%s>@%p\n", mp->ring->name, mp->ring);
908 fprintf(f, " phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
909 fprintf(f, " nb_mem_chunks=%u\n", mp->nb_mem_chunks);
910 fprintf(f, " size=%"PRIu32"\n", mp->size);
911 fprintf(f, " populated_size=%"PRIu32"\n", mp->populated_size);
912 fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
913 fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
914 fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
915 fprintf(f, " total_obj_size=%"PRIu32"\n",
916 mp->header_size + mp->elt_size + mp->trailer_size);
918 fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
920 STAILQ_FOREACH(memhdr, &mp->mem_list, next)
921 mem_len += memhdr->len;
923 fprintf(f, " avg bytes/object=%#Lf\n",
924 (long double)mem_len / mp->size);
927 cache_count = rte_mempool_dump_cache(f, mp);
928 common_count = rte_ring_count(mp->ring);
929 if ((cache_count + common_count) > mp->size)
930 common_count = mp->size - cache_count;
931 fprintf(f, " common_pool_count=%u\n", common_count);
933 /* sum and dump statistics */
934 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
935 memset(&sum, 0, sizeof(sum));
936 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
937 sum.put_bulk += mp->stats[lcore_id].put_bulk;
938 sum.put_objs += mp->stats[lcore_id].put_objs;
939 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
940 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
941 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
942 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
944 fprintf(f, " stats:\n");
945 fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
946 fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
947 fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
948 fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
949 fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
950 fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
952 fprintf(f, " no statistics available\n");
955 rte_mempool_audit(mp);
958 /* dump the status of all mempools on the console */
960 rte_mempool_list_dump(FILE *f)
962 struct rte_mempool *mp = NULL;
963 struct rte_tailq_entry *te;
964 struct rte_mempool_list *mempool_list;
966 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
968 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
970 TAILQ_FOREACH(te, mempool_list, next) {
971 mp = (struct rte_mempool *) te->data;
972 rte_mempool_dump(f, mp);
975 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
978 /* search a mempool from its name */
980 rte_mempool_lookup(const char *name)
982 struct rte_mempool *mp = NULL;
983 struct rte_tailq_entry *te;
984 struct rte_mempool_list *mempool_list;
986 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
988 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
990 TAILQ_FOREACH(te, mempool_list, next) {
991 mp = (struct rte_mempool *) te->data;
992 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
996 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1006 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1009 struct rte_tailq_entry *te = NULL;
1010 struct rte_mempool_list *mempool_list;
1012 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1014 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1016 TAILQ_FOREACH(te, mempool_list, next) {
1017 (*func)((struct rte_mempool *) te->data, arg);
1020 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);