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 /* this is the size of an object, including header and trailer */
227 sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
229 return sz->total_size;
234 * Calculate maximum amount of memory required to store given number of objects.
237 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift)
239 size_t obj_per_page, pg_num, pg_sz;
242 return total_elt_sz * elt_num;
244 pg_sz = (size_t)1 << pg_shift;
245 obj_per_page = pg_sz / total_elt_sz;
246 if (obj_per_page == 0)
247 return RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
249 pg_num = (elt_num + obj_per_page - 1) / obj_per_page;
250 return pg_num << pg_shift;
254 * Calculate how much memory would be actually required with the
255 * given memory footprint to store required number of elements.
258 rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num,
259 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
262 uint32_t elt_cnt = 0;
263 phys_addr_t start, end;
265 size_t pg_sz = (size_t)1 << pg_shift;
267 /* if paddr is NULL, assume contiguous memory */
270 end = pg_sz * pg_num;
274 end = paddr[0] + pg_sz;
277 while (elt_cnt < elt_num) {
279 if (end - start >= total_elt_sz) {
280 /* enough contiguous memory, add an object */
281 start += total_elt_sz;
283 } else if (paddr_idx < pg_num) {
284 /* no room to store one obj, add a page */
285 if (end == paddr[paddr_idx]) {
288 start = paddr[paddr_idx];
289 end = paddr[paddr_idx] + pg_sz;
294 /* no more page, return how many elements fit */
295 return -(size_t)elt_cnt;
299 return (size_t)paddr_idx << pg_shift;
302 /* create the internal ring */
304 rte_mempool_ring_create(struct rte_mempool *mp)
307 char rg_name[RTE_RING_NAMESIZE];
310 snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, mp->name);
313 if (mp->flags & MEMPOOL_F_SP_PUT)
314 rg_flags |= RING_F_SP_ENQ;
315 if (mp->flags & MEMPOOL_F_SC_GET)
316 rg_flags |= RING_F_SC_DEQ;
318 /* Allocate the ring that will be used to store objects.
319 * Ring functions will return appropriate errors if we are
320 * running as a secondary process etc., so no checks made
321 * in this function for that condition.
323 r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
324 mp->socket_id, rg_flags);
329 mp->flags |= MEMPOOL_F_RING_CREATED;
333 /* free a memchunk allocated with rte_memzone_reserve() */
335 rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
338 const struct rte_memzone *mz = opaque;
339 rte_memzone_free(mz);
342 /* Free memory chunks used by a mempool. Objects must be in pool */
344 rte_mempool_free_memchunks(struct rte_mempool *mp)
346 struct rte_mempool_memhdr *memhdr;
349 while (!STAILQ_EMPTY(&mp->elt_list)) {
350 rte_ring_sc_dequeue(mp->ring, &elt);
352 STAILQ_REMOVE_HEAD(&mp->elt_list, next);
353 mp->populated_size--;
356 while (!STAILQ_EMPTY(&mp->mem_list)) {
357 memhdr = STAILQ_FIRST(&mp->mem_list);
358 STAILQ_REMOVE_HEAD(&mp->mem_list, next);
359 if (memhdr->free_cb != NULL)
360 memhdr->free_cb(memhdr, memhdr->opaque);
366 /* Add objects in the pool, using a physically contiguous memory
367 * zone. Return the number of objects added, or a negative value
371 rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
372 phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
375 unsigned total_elt_sz;
378 struct rte_mempool_memhdr *memhdr;
381 /* create the internal ring if not already done */
382 if ((mp->flags & MEMPOOL_F_RING_CREATED) == 0) {
383 ret = rte_mempool_ring_create(mp);
388 /* mempool is already populated */
389 if (mp->populated_size >= mp->size)
392 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
394 memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
399 memhdr->addr = vaddr;
400 memhdr->phys_addr = paddr;
402 memhdr->free_cb = free_cb;
403 memhdr->opaque = opaque;
405 if (mp->flags & MEMPOOL_F_NO_CACHE_ALIGN)
406 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
408 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_CACHE_LINE_SIZE) - vaddr;
410 while (off + total_elt_sz <= len && mp->populated_size < mp->size) {
411 off += mp->header_size;
412 mempool_add_elem(mp, (char *)vaddr + off, paddr + off);
413 off += mp->elt_size + mp->trailer_size;
417 /* not enough room to store one object */
421 STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
426 /* Add objects in the pool, using a table of physical pages. Return the
427 * number of objects added, or a negative value on error.
430 rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
431 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
432 rte_mempool_memchunk_free_cb_t *free_cb, void *opaque)
436 size_t pg_sz = (size_t)1 << pg_shift;
438 /* mempool must not be populated */
439 if (mp->nb_mem_chunks != 0)
442 for (i = 0; i < pg_num && mp->populated_size < mp->size; i += n) {
444 /* populate with the largest group of contiguous pages */
445 for (n = 1; (i + n) < pg_num &&
446 paddr[i] + pg_sz == paddr[i+n]; n++)
449 ret = rte_mempool_populate_phys(mp, vaddr + i * pg_sz,
450 paddr[i], n * pg_sz, free_cb, opaque);
452 rte_mempool_free_memchunks(mp);
455 /* no need to call the free callback for next chunks */
462 /* Populate the mempool with a virtual area. Return the number of
463 * objects added, or a negative value on error.
466 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
467 size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
471 size_t off, phys_len;
474 /* mempool must not be populated */
475 if (mp->nb_mem_chunks != 0)
477 /* address and len must be page-aligned */
478 if (RTE_PTR_ALIGN_CEIL(addr, pg_sz) != addr)
480 if (RTE_ALIGN_CEIL(len, pg_sz) != len)
483 for (off = 0; off + pg_sz <= len &&
484 mp->populated_size < mp->size; off += phys_len) {
486 paddr = rte_mem_virt2phy(addr + off);
487 /* required for xen_dom0 to get the machine address */
488 paddr = rte_mem_phy2mch(-1, paddr);
490 if (paddr == RTE_BAD_PHYS_ADDR) {
495 /* populate with the largest group of contiguous pages */
496 for (phys_len = pg_sz; off + phys_len < len; phys_len += pg_sz) {
497 phys_addr_t paddr_tmp;
499 paddr_tmp = rte_mem_virt2phy(addr + off + phys_len);
500 paddr_tmp = rte_mem_phy2mch(-1, paddr_tmp);
502 if (paddr_tmp != paddr + phys_len)
506 ret = rte_mempool_populate_phys(mp, addr + off, paddr,
507 phys_len, free_cb, opaque);
510 /* no need to call the free callback for next chunks */
518 rte_mempool_free_memchunks(mp);
522 /* Default function to populate the mempool: allocate memory in memzones,
523 * and populate them. Return the number of objects added, or a negative
527 rte_mempool_populate_default(struct rte_mempool *mp)
529 int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
530 char mz_name[RTE_MEMZONE_NAMESIZE];
531 const struct rte_memzone *mz;
532 size_t size, total_elt_sz, align, pg_sz, pg_shift;
536 /* mempool must not be populated */
537 if (mp->nb_mem_chunks != 0)
540 if (rte_eal_has_hugepages()) {
541 pg_shift = 0; /* not needed, zone is physically contiguous */
543 align = RTE_CACHE_LINE_SIZE;
545 pg_sz = getpagesize();
546 pg_shift = rte_bsf32(pg_sz);
550 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
551 for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
552 size = rte_mempool_xmem_size(n, total_elt_sz, pg_shift);
554 ret = snprintf(mz_name, sizeof(mz_name),
555 RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
556 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
561 mz = rte_memzone_reserve_aligned(mz_name, size,
562 mp->socket_id, mz_flags, align);
563 /* not enough memory, retry with the biggest zone we have */
565 mz = rte_memzone_reserve_aligned(mz_name, 0,
566 mp->socket_id, mz_flags, align);
572 /* use memzone physical address if it is valid */
573 if (rte_eal_has_hugepages() && !rte_xen_dom0_supported())
574 ret = rte_mempool_populate_phys(mp, mz->addr,
575 mz->phys_addr, mz->len,
576 rte_mempool_memchunk_mz_free,
577 (void *)(uintptr_t)mz);
579 ret = rte_mempool_populate_virt(mp, mz->addr,
581 rte_mempool_memchunk_mz_free,
582 (void *)(uintptr_t)mz);
590 rte_mempool_free_memchunks(mp);
596 rte_mempool_free(struct rte_mempool *mp)
598 struct rte_mempool_list *mempool_list = NULL;
599 struct rte_tailq_entry *te;
604 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
605 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
606 /* find out tailq entry */
607 TAILQ_FOREACH(te, mempool_list, next) {
608 if (te->data == (void *)mp)
613 TAILQ_REMOVE(mempool_list, te, next);
616 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
618 rte_mempool_free_memchunks(mp);
619 rte_ring_free(mp->ring);
620 rte_memzone_free(mp->mz);
623 /* create an empty mempool */
624 static struct rte_mempool *
625 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
626 unsigned cache_size, unsigned private_data_size,
627 int socket_id, unsigned flags)
629 char mz_name[RTE_MEMZONE_NAMESIZE];
630 struct rte_mempool_list *mempool_list;
631 struct rte_mempool *mp = NULL;
632 struct rte_tailq_entry *te = NULL;
633 const struct rte_memzone *mz = NULL;
635 int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
636 struct rte_mempool_objsz objsz;
638 /* compilation-time checks */
639 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
640 RTE_CACHE_LINE_MASK) != 0);
641 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
642 RTE_CACHE_LINE_MASK) != 0);
643 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
644 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
645 RTE_CACHE_LINE_MASK) != 0);
646 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
647 RTE_CACHE_LINE_MASK) != 0);
650 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
652 /* asked cache too big */
653 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
654 CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
659 /* "no cache align" imply "no spread" */
660 if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
661 flags |= MEMPOOL_F_NO_SPREAD;
663 /* calculate mempool object sizes. */
664 if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
669 rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
672 * reserve a memory zone for this mempool: private data is
675 private_data_size = (private_data_size +
676 RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
679 /* try to allocate tailq entry */
680 te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
682 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
686 mempool_size = MEMPOOL_HEADER_SIZE(mp, cache_size);
687 mempool_size += private_data_size;
688 mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
690 snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
692 mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
696 /* init the mempool structure */
698 memset(mp, 0, sizeof(*mp));
699 snprintf(mp->name, sizeof(mp->name), "%s", name);
701 mp->socket_id = socket_id;
704 mp->socket_id = socket_id;
705 mp->elt_size = objsz.elt_size;
706 mp->header_size = objsz.header_size;
707 mp->trailer_size = objsz.trailer_size;
708 mp->cache_size = cache_size;
709 mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
710 mp->private_data_size = private_data_size;
711 STAILQ_INIT(&mp->elt_list);
712 STAILQ_INIT(&mp->mem_list);
715 * local_cache pointer is set even if cache_size is zero.
716 * The local_cache points to just past the elt_pa[] array.
718 mp->local_cache = (struct rte_mempool_cache *)
719 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
722 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
723 TAILQ_INSERT_TAIL(mempool_list, te, next);
724 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
725 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
730 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
732 rte_mempool_free(mp);
736 /* create the mempool */
738 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
739 unsigned cache_size, unsigned private_data_size,
740 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
741 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
742 int socket_id, unsigned flags)
744 struct rte_mempool *mp;
746 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
747 private_data_size, socket_id, flags);
751 /* call the mempool priv initializer */
753 mp_init(mp, mp_init_arg);
755 if (rte_mempool_populate_default(mp) < 0)
758 /* call the object initializers */
760 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
765 rte_mempool_free(mp);
770 * Create the mempool over already allocated chunk of memory.
771 * That external memory buffer can consists of physically disjoint pages.
772 * Setting vaddr to NULL, makes mempool to fallback to original behaviour
773 * and allocate space for mempool and it's elements as one big chunk of
774 * physically continuos memory.
777 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
778 unsigned cache_size, unsigned private_data_size,
779 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
780 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
781 int socket_id, unsigned flags, void *vaddr,
782 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
784 struct rte_mempool *mp = NULL;
787 /* no virtual address supplied, use rte_mempool_create() */
789 return rte_mempool_create(name, n, elt_size, cache_size,
790 private_data_size, mp_init, mp_init_arg,
791 obj_init, obj_init_arg, socket_id, flags);
793 /* check that we have both VA and PA */
799 /* Check that pg_shift parameter is valid. */
800 if (pg_shift > MEMPOOL_PG_SHIFT_MAX) {
805 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
806 private_data_size, socket_id, flags);
810 /* call the mempool priv initializer */
812 mp_init(mp, mp_init_arg);
814 ret = rte_mempool_populate_phys_tab(mp, vaddr, paddr, pg_num, pg_shift,
816 if (ret < 0 || ret != (int)mp->size)
819 /* call the object initializers */
821 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
826 rte_mempool_free(mp);
830 /* Return the number of entries in the mempool */
832 rte_mempool_count(const struct rte_mempool *mp)
837 count = rte_ring_count(mp->ring);
839 if (mp->cache_size == 0)
842 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
843 count += mp->local_cache[lcore_id].len;
846 * due to race condition (access to len is not locked), the
847 * total can be greater than size... so fix the result
849 if (count > mp->size)
854 /* dump the cache status */
856 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
860 unsigned cache_count;
862 fprintf(f, " cache infos:\n");
863 fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
865 if (mp->cache_size == 0)
868 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
869 cache_count = mp->local_cache[lcore_id].len;
870 fprintf(f, " cache_count[%u]=%u\n", lcore_id, cache_count);
871 count += cache_count;
873 fprintf(f, " total_cache_count=%u\n", count);
877 #ifndef __INTEL_COMPILER
878 #pragma GCC diagnostic ignored "-Wcast-qual"
881 /* check and update cookies or panic (internal) */
882 void rte_mempool_check_cookies(const struct rte_mempool *mp,
883 void * const *obj_table_const, unsigned n, int free)
885 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
886 struct rte_mempool_objhdr *hdr;
887 struct rte_mempool_objtlr *tlr;
893 /* Force to drop the "const" attribute. This is done only when
894 * DEBUG is enabled */
895 tmp = (void *) obj_table_const;
896 obj_table = (void **) tmp;
901 if (rte_mempool_from_obj(obj) != mp)
902 rte_panic("MEMPOOL: object is owned by another "
905 hdr = __mempool_get_header(obj);
906 cookie = hdr->cookie;
909 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
910 rte_log_set_history(0);
911 RTE_LOG(CRIT, MEMPOOL,
912 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
913 obj, (const void *) mp, cookie);
914 rte_panic("MEMPOOL: bad header cookie (put)\n");
916 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
917 } else if (free == 1) {
918 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
919 rte_log_set_history(0);
920 RTE_LOG(CRIT, MEMPOOL,
921 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
922 obj, (const void *) mp, cookie);
923 rte_panic("MEMPOOL: bad header cookie (get)\n");
925 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
926 } else if (free == 2) {
927 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
928 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
929 rte_log_set_history(0);
930 RTE_LOG(CRIT, MEMPOOL,
931 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
932 obj, (const void *) mp, cookie);
933 rte_panic("MEMPOOL: bad header cookie (audit)\n");
936 tlr = __mempool_get_trailer(obj);
937 cookie = tlr->cookie;
938 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
939 rte_log_set_history(0);
940 RTE_LOG(CRIT, MEMPOOL,
941 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
942 obj, (const void *) mp, cookie);
943 rte_panic("MEMPOOL: bad trailer cookie\n");
948 RTE_SET_USED(obj_table_const);
954 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
956 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
957 void *obj, __rte_unused unsigned idx)
959 __mempool_check_cookies(mp, &obj, 1, 2);
963 mempool_audit_cookies(struct rte_mempool *mp)
967 num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
968 if (num != mp->size) {
969 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
970 "iterated only over %u elements\n",
975 #define mempool_audit_cookies(mp) do {} while(0)
978 #ifndef __INTEL_COMPILER
979 #pragma GCC diagnostic error "-Wcast-qual"
982 /* check cookies before and after objects */
984 mempool_audit_cache(const struct rte_mempool *mp)
986 /* check cache size consistency */
989 if (mp->cache_size == 0)
992 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
993 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
994 RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
996 rte_panic("MEMPOOL: invalid cache len\n");
1001 /* check the consistency of mempool (size, cookies, ...) */
1003 rte_mempool_audit(struct rte_mempool *mp)
1005 mempool_audit_cache(mp);
1006 mempool_audit_cookies(mp);
1008 /* For case where mempool DEBUG is not set, and cache size is 0 */
1012 /* dump the status of the mempool on the console */
1014 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1016 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1017 struct rte_mempool_debug_stats sum;
1020 struct rte_mempool_memhdr *memhdr;
1021 unsigned common_count;
1022 unsigned cache_count;
1025 RTE_ASSERT(f != NULL);
1026 RTE_ASSERT(mp != NULL);
1028 fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1029 fprintf(f, " flags=%x\n", mp->flags);
1030 fprintf(f, " ring=<%s>@%p\n", mp->ring->name, mp->ring);
1031 fprintf(f, " phys_addr=0x%" PRIx64 "\n", mp->mz->phys_addr);
1032 fprintf(f, " nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1033 fprintf(f, " size=%"PRIu32"\n", mp->size);
1034 fprintf(f, " populated_size=%"PRIu32"\n", mp->populated_size);
1035 fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
1036 fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
1037 fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
1038 fprintf(f, " total_obj_size=%"PRIu32"\n",
1039 mp->header_size + mp->elt_size + mp->trailer_size);
1041 fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
1043 STAILQ_FOREACH(memhdr, &mp->mem_list, next)
1044 mem_len += memhdr->len;
1046 fprintf(f, " avg bytes/object=%#Lf\n",
1047 (long double)mem_len / mp->size);
1050 cache_count = rte_mempool_dump_cache(f, mp);
1051 common_count = rte_ring_count(mp->ring);
1052 if ((cache_count + common_count) > mp->size)
1053 common_count = mp->size - cache_count;
1054 fprintf(f, " common_pool_count=%u\n", common_count);
1056 /* sum and dump statistics */
1057 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1058 memset(&sum, 0, sizeof(sum));
1059 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1060 sum.put_bulk += mp->stats[lcore_id].put_bulk;
1061 sum.put_objs += mp->stats[lcore_id].put_objs;
1062 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1063 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1064 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1065 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1067 fprintf(f, " stats:\n");
1068 fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
1069 fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
1070 fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1071 fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1072 fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1073 fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1075 fprintf(f, " no statistics available\n");
1078 rte_mempool_audit(mp);
1081 /* dump the status of all mempools on the console */
1083 rte_mempool_list_dump(FILE *f)
1085 struct rte_mempool *mp = NULL;
1086 struct rte_tailq_entry *te;
1087 struct rte_mempool_list *mempool_list;
1089 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1091 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1093 TAILQ_FOREACH(te, mempool_list, next) {
1094 mp = (struct rte_mempool *) te->data;
1095 rte_mempool_dump(f, mp);
1098 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1101 /* search a mempool from its name */
1102 struct rte_mempool *
1103 rte_mempool_lookup(const char *name)
1105 struct rte_mempool *mp = NULL;
1106 struct rte_tailq_entry *te;
1107 struct rte_mempool_list *mempool_list;
1109 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1111 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1113 TAILQ_FOREACH(te, mempool_list, next) {
1114 mp = (struct rte_mempool *) te->data;
1115 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1119 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1129 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1132 struct rte_tailq_entry *te = NULL;
1133 struct rte_mempool_list *mempool_list;
1135 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1137 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1139 TAILQ_FOREACH(te, mempool_list, next) {
1140 (*func)((struct rte_mempool *) te->data, arg);
1143 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);