1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright(c) 2016 6WIND S.A.
13 #include <sys/queue.h>
16 #include <rte_common.h>
18 #include <rte_debug.h>
19 #include <rte_memory.h>
20 #include <rte_memzone.h>
21 #include <rte_malloc.h>
22 #include <rte_atomic.h>
23 #include <rte_launch.h>
25 #include <rte_eal_memconfig.h>
26 #include <rte_per_lcore.h>
27 #include <rte_lcore.h>
28 #include <rte_branch_prediction.h>
29 #include <rte_errno.h>
30 #include <rte_string_fns.h>
31 #include <rte_spinlock.h>
33 #include "rte_mempool.h"
35 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
37 static struct rte_tailq_elem rte_mempool_tailq = {
38 .name = "RTE_MEMPOOL",
40 EAL_REGISTER_TAILQ(rte_mempool_tailq)
42 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
43 #define CALC_CACHE_FLUSHTHRESH(c) \
44 ((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
47 * return the greatest common divisor between a and b (fast algorithm)
50 static unsigned get_gcd(unsigned a, unsigned b)
75 * Depending on memory configuration, objects addresses are spread
76 * between channels and ranks in RAM: the pool allocator will add
77 * padding between objects. This function return the new size of the
80 static unsigned optimize_object_size(unsigned obj_size)
82 unsigned nrank, nchan;
83 unsigned new_obj_size;
85 /* get number of channels */
86 nchan = rte_memory_get_nchannel();
90 nrank = rte_memory_get_nrank();
94 /* process new object size */
95 new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN;
96 while (get_gcd(new_obj_size, nrank * nchan) != 1)
98 return new_obj_size * RTE_MEMPOOL_ALIGN;
102 mempool_add_elem(struct rte_mempool *mp, void *obj, rte_iova_t iova)
104 struct rte_mempool_objhdr *hdr;
105 struct rte_mempool_objtlr *tlr __rte_unused;
107 /* set mempool ptr in header */
108 hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
111 STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
112 mp->populated_size++;
114 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
115 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
116 tlr = __mempool_get_trailer(obj);
117 tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
120 /* enqueue in ring */
121 rte_mempool_ops_enqueue_bulk(mp, &obj, 1);
124 /* call obj_cb() for each mempool element */
126 rte_mempool_obj_iter(struct rte_mempool *mp,
127 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
129 struct rte_mempool_objhdr *hdr;
133 STAILQ_FOREACH(hdr, &mp->elt_list, next) {
134 obj = (char *)hdr + sizeof(*hdr);
135 obj_cb(mp, obj_cb_arg, obj, n);
142 /* call mem_cb() for each mempool memory chunk */
144 rte_mempool_mem_iter(struct rte_mempool *mp,
145 rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg)
147 struct rte_mempool_memhdr *hdr;
150 STAILQ_FOREACH(hdr, &mp->mem_list, next) {
151 mem_cb(mp, mem_cb_arg, hdr, n);
158 /* get the header, trailer and total size of a mempool element. */
160 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
161 struct rte_mempool_objsz *sz)
163 struct rte_mempool_objsz lsz;
165 sz = (sz != NULL) ? sz : &lsz;
167 sz->header_size = sizeof(struct rte_mempool_objhdr);
168 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
169 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
172 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
173 sz->trailer_size = sizeof(struct rte_mempool_objtlr);
175 sz->trailer_size = 0;
178 /* element size is 8 bytes-aligned at least */
179 sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
181 /* expand trailer to next cache line */
182 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
183 sz->total_size = sz->header_size + sz->elt_size +
185 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
186 (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
187 RTE_MEMPOOL_ALIGN_MASK);
191 * increase trailer to add padding between objects in order to
192 * spread them across memory channels/ranks
194 if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
196 new_size = optimize_object_size(sz->header_size + sz->elt_size +
198 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
201 /* this is the size of an object, including header and trailer */
202 sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
204 return sz->total_size;
209 * Calculate maximum amount of memory required to store given number of objects.
212 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift,
215 size_t obj_per_page, pg_num, pg_sz;
218 mask = MEMPOOL_F_CAPA_BLK_ALIGNED_OBJECTS | MEMPOOL_F_CAPA_PHYS_CONTIG;
219 if ((flags & mask) == mask)
220 /* alignment need one additional object */
223 if (total_elt_sz == 0)
227 return total_elt_sz * elt_num;
229 pg_sz = (size_t)1 << pg_shift;
230 obj_per_page = pg_sz / total_elt_sz;
231 if (obj_per_page == 0)
232 return RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
234 pg_num = (elt_num + obj_per_page - 1) / obj_per_page;
235 return pg_num << pg_shift;
239 * Calculate how much memory would be actually required with the
240 * given memory footprint to store required number of elements.
243 rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num,
244 size_t total_elt_sz, const rte_iova_t iova[], uint32_t pg_num,
245 uint32_t pg_shift, unsigned int flags)
247 uint32_t elt_cnt = 0;
248 rte_iova_t start, end;
250 size_t pg_sz = (size_t)1 << pg_shift;
253 mask = MEMPOOL_F_CAPA_BLK_ALIGNED_OBJECTS | MEMPOOL_F_CAPA_PHYS_CONTIG;
254 if ((flags & mask) == mask)
255 /* alignment need one additional object */
258 /* if iova is NULL, assume contiguous memory */
261 end = pg_sz * pg_num;
265 end = iova[0] + pg_sz;
268 while (elt_cnt < elt_num) {
270 if (end - start >= total_elt_sz) {
271 /* enough contiguous memory, add an object */
272 start += total_elt_sz;
274 } else if (iova_idx < pg_num) {
275 /* no room to store one obj, add a page */
276 if (end == iova[iova_idx]) {
279 start = iova[iova_idx];
280 end = iova[iova_idx] + pg_sz;
285 /* no more page, return how many elements fit */
286 return -(size_t)elt_cnt;
290 return (size_t)iova_idx << pg_shift;
293 /* free a memchunk allocated with rte_memzone_reserve() */
295 rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
298 const struct rte_memzone *mz = opaque;
299 rte_memzone_free(mz);
302 /* Free memory chunks used by a mempool. Objects must be in pool */
304 rte_mempool_free_memchunks(struct rte_mempool *mp)
306 struct rte_mempool_memhdr *memhdr;
309 while (!STAILQ_EMPTY(&mp->elt_list)) {
310 rte_mempool_ops_dequeue_bulk(mp, &elt, 1);
312 STAILQ_REMOVE_HEAD(&mp->elt_list, next);
313 mp->populated_size--;
316 while (!STAILQ_EMPTY(&mp->mem_list)) {
317 memhdr = STAILQ_FIRST(&mp->mem_list);
318 STAILQ_REMOVE_HEAD(&mp->mem_list, next);
319 if (memhdr->free_cb != NULL)
320 memhdr->free_cb(memhdr, memhdr->opaque);
326 /* Add objects in the pool, using a physically contiguous memory
327 * zone. Return the number of objects added, or a negative value
331 rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
332 rte_iova_t iova, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
335 unsigned total_elt_sz;
338 struct rte_mempool_memhdr *memhdr;
341 /* create the internal ring if not already done */
342 if ((mp->flags & MEMPOOL_F_POOL_CREATED) == 0) {
343 ret = rte_mempool_ops_alloc(mp);
346 mp->flags |= MEMPOOL_F_POOL_CREATED;
349 /* Notify memory area to mempool */
350 ret = rte_mempool_ops_register_memory_area(mp, vaddr, iova, len);
351 if (ret != -ENOTSUP && ret < 0)
354 /* mempool is already populated */
355 if (mp->populated_size >= mp->size)
358 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
360 /* Detect pool area has sufficient space for elements */
361 if (mp->flags & MEMPOOL_F_CAPA_PHYS_CONTIG) {
362 if (len < total_elt_sz * mp->size) {
363 RTE_LOG(ERR, MEMPOOL,
364 "pool area %" PRIx64 " not enough\n",
370 memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
375 memhdr->addr = vaddr;
378 memhdr->free_cb = free_cb;
379 memhdr->opaque = opaque;
381 if (mp->flags & MEMPOOL_F_CAPA_BLK_ALIGNED_OBJECTS)
382 /* align object start address to a multiple of total_elt_sz */
383 off = total_elt_sz - ((uintptr_t)vaddr % total_elt_sz);
384 else if (mp->flags & MEMPOOL_F_NO_CACHE_ALIGN)
385 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
387 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_CACHE_LINE_SIZE) - vaddr;
389 while (off + total_elt_sz <= len && mp->populated_size < mp->size) {
390 off += mp->header_size;
391 if (iova == RTE_BAD_IOVA)
392 mempool_add_elem(mp, (char *)vaddr + off,
395 mempool_add_elem(mp, (char *)vaddr + off, iova + off);
396 off += mp->elt_size + mp->trailer_size;
400 /* not enough room to store one object */
404 STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
410 rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
411 phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
414 return rte_mempool_populate_iova(mp, vaddr, paddr, len, free_cb, opaque);
417 /* Add objects in the pool, using a table of physical pages. Return the
418 * number of objects added, or a negative value on error.
421 rte_mempool_populate_iova_tab(struct rte_mempool *mp, char *vaddr,
422 const rte_iova_t iova[], uint32_t pg_num, uint32_t pg_shift,
423 rte_mempool_memchunk_free_cb_t *free_cb, void *opaque)
427 size_t pg_sz = (size_t)1 << pg_shift;
429 /* mempool must not be populated */
430 if (mp->nb_mem_chunks != 0)
433 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
434 return rte_mempool_populate_iova(mp, vaddr, RTE_BAD_IOVA,
435 pg_num * pg_sz, free_cb, opaque);
437 for (i = 0; i < pg_num && mp->populated_size < mp->size; i += n) {
439 /* populate with the largest group of contiguous pages */
440 for (n = 1; (i + n) < pg_num &&
441 iova[i + n - 1] + pg_sz == iova[i + n]; n++)
444 ret = rte_mempool_populate_iova(mp, vaddr + i * pg_sz,
445 iova[i], n * pg_sz, free_cb, opaque);
447 rte_mempool_free_memchunks(mp);
450 /* no need to call the free callback for next chunks */
458 rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
459 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
460 rte_mempool_memchunk_free_cb_t *free_cb, void *opaque)
462 return rte_mempool_populate_iova_tab(mp, vaddr, paddr, pg_num, pg_shift,
466 /* Populate the mempool with a virtual area. Return the number of
467 * objects added, or a negative value on error.
470 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
471 size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
475 size_t off, phys_len;
478 /* mempool must not be populated */
479 if (mp->nb_mem_chunks != 0)
481 /* address and len must be page-aligned */
482 if (RTE_PTR_ALIGN_CEIL(addr, pg_sz) != addr)
484 if (RTE_ALIGN_CEIL(len, pg_sz) != len)
487 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
488 return rte_mempool_populate_iova(mp, addr, RTE_BAD_IOVA,
489 len, free_cb, opaque);
491 for (off = 0; off + pg_sz <= len &&
492 mp->populated_size < mp->size; off += phys_len) {
494 iova = rte_mem_virt2iova(addr + off);
496 if (iova == RTE_BAD_IOVA && rte_eal_has_hugepages()) {
501 /* populate with the largest group of contiguous pages */
502 for (phys_len = pg_sz; off + phys_len < len; phys_len += pg_sz) {
505 iova_tmp = rte_mem_virt2iova(addr + off + phys_len);
507 if (iova_tmp != iova + phys_len)
511 ret = rte_mempool_populate_iova(mp, addr + off, iova,
512 phys_len, free_cb, opaque);
515 /* no need to call the free callback for next chunks */
523 rte_mempool_free_memchunks(mp);
527 /* Default function to populate the mempool: allocate memory in memzones,
528 * and populate them. Return the number of objects added, or a negative
532 rte_mempool_populate_default(struct rte_mempool *mp)
534 unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
535 char mz_name[RTE_MEMZONE_NAMESIZE];
536 const struct rte_memzone *mz;
537 size_t size, total_elt_sz, align, pg_sz, pg_shift;
540 unsigned int mp_flags;
543 /* mempool must not be populated */
544 if (mp->nb_mem_chunks != 0)
547 /* Get mempool capabilities */
549 ret = rte_mempool_ops_get_capabilities(mp, &mp_flags);
550 if ((ret < 0) && (ret != -ENOTSUP))
553 /* update mempool capabilities */
554 mp->flags |= mp_flags;
556 if (rte_eal_has_hugepages()) {
557 pg_shift = 0; /* not needed, zone is physically contiguous */
559 align = RTE_CACHE_LINE_SIZE;
561 pg_sz = getpagesize();
562 pg_shift = rte_bsf32(pg_sz);
566 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
567 for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
568 size = rte_mempool_xmem_size(n, total_elt_sz, pg_shift,
571 ret = snprintf(mz_name, sizeof(mz_name),
572 RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
573 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
578 mz = rte_memzone_reserve_aligned(mz_name, size,
579 mp->socket_id, mz_flags, align);
580 /* not enough memory, retry with the biggest zone we have */
582 mz = rte_memzone_reserve_aligned(mz_name, 0,
583 mp->socket_id, mz_flags, align);
589 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
594 if (rte_eal_has_hugepages())
595 ret = rte_mempool_populate_iova(mp, mz->addr,
597 rte_mempool_memchunk_mz_free,
598 (void *)(uintptr_t)mz);
600 ret = rte_mempool_populate_virt(mp, mz->addr,
602 rte_mempool_memchunk_mz_free,
603 (void *)(uintptr_t)mz);
605 rte_memzone_free(mz);
613 rte_mempool_free_memchunks(mp);
617 /* return the memory size required for mempool objects in anonymous mem */
619 get_anon_size(const struct rte_mempool *mp)
621 size_t size, total_elt_sz, pg_sz, pg_shift;
623 pg_sz = getpagesize();
624 pg_shift = rte_bsf32(pg_sz);
625 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
626 size = rte_mempool_xmem_size(mp->size, total_elt_sz, pg_shift,
632 /* unmap a memory zone mapped by rte_mempool_populate_anon() */
634 rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr,
637 munmap(opaque, get_anon_size(memhdr->mp));
640 /* populate the mempool with an anonymous mapping */
642 rte_mempool_populate_anon(struct rte_mempool *mp)
648 /* mempool is already populated, error */
649 if (!STAILQ_EMPTY(&mp->mem_list)) {
654 /* get chunk of virtually continuous memory */
655 size = get_anon_size(mp);
656 addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
657 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
658 if (addr == MAP_FAILED) {
662 /* can't use MMAP_LOCKED, it does not exist on BSD */
663 if (mlock(addr, size) < 0) {
669 ret = rte_mempool_populate_virt(mp, addr, size, getpagesize(),
670 rte_mempool_memchunk_anon_free, addr);
674 return mp->populated_size;
677 rte_mempool_free_memchunks(mp);
683 rte_mempool_free(struct rte_mempool *mp)
685 struct rte_mempool_list *mempool_list = NULL;
686 struct rte_tailq_entry *te;
691 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
692 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
693 /* find out tailq entry */
694 TAILQ_FOREACH(te, mempool_list, next) {
695 if (te->data == (void *)mp)
700 TAILQ_REMOVE(mempool_list, te, next);
703 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
705 rte_mempool_free_memchunks(mp);
706 rte_mempool_ops_free(mp);
707 rte_memzone_free(mp->mz);
711 mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)
714 cache->flushthresh = CALC_CACHE_FLUSHTHRESH(size);
719 * Create and initialize a cache for objects that are retrieved from and
720 * returned to an underlying mempool. This structure is identical to the
721 * local_cache[lcore_id] pointed to by the mempool structure.
723 struct rte_mempool_cache *
724 rte_mempool_cache_create(uint32_t size, int socket_id)
726 struct rte_mempool_cache *cache;
728 if (size == 0 || size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
733 cache = rte_zmalloc_socket("MEMPOOL_CACHE", sizeof(*cache),
734 RTE_CACHE_LINE_SIZE, socket_id);
736 RTE_LOG(ERR, MEMPOOL, "Cannot allocate mempool cache.\n");
741 mempool_cache_init(cache, size);
747 * Free a cache. It's the responsibility of the user to make sure that any
748 * remaining objects in the cache are flushed to the corresponding
752 rte_mempool_cache_free(struct rte_mempool_cache *cache)
757 /* create an empty mempool */
759 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
760 unsigned cache_size, unsigned private_data_size,
761 int socket_id, unsigned flags)
763 char mz_name[RTE_MEMZONE_NAMESIZE];
764 struct rte_mempool_list *mempool_list;
765 struct rte_mempool *mp = NULL;
766 struct rte_tailq_entry *te = NULL;
767 const struct rte_memzone *mz = NULL;
769 unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
770 struct rte_mempool_objsz objsz;
774 /* compilation-time checks */
775 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
776 RTE_CACHE_LINE_MASK) != 0);
777 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
778 RTE_CACHE_LINE_MASK) != 0);
779 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
780 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
781 RTE_CACHE_LINE_MASK) != 0);
782 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
783 RTE_CACHE_LINE_MASK) != 0);
786 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
788 /* asked cache too big */
789 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
790 CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
795 /* "no cache align" imply "no spread" */
796 if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
797 flags |= MEMPOOL_F_NO_SPREAD;
799 /* calculate mempool object sizes. */
800 if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
805 rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
808 * reserve a memory zone for this mempool: private data is
811 private_data_size = (private_data_size +
812 RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
815 /* try to allocate tailq entry */
816 te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
818 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
822 mempool_size = MEMPOOL_HEADER_SIZE(mp, cache_size);
823 mempool_size += private_data_size;
824 mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
826 ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
827 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
828 rte_errno = ENAMETOOLONG;
832 mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
836 /* init the mempool structure */
838 memset(mp, 0, MEMPOOL_HEADER_SIZE(mp, cache_size));
839 ret = snprintf(mp->name, sizeof(mp->name), "%s", name);
840 if (ret < 0 || ret >= (int)sizeof(mp->name)) {
841 rte_errno = ENAMETOOLONG;
847 mp->socket_id = socket_id;
848 mp->elt_size = objsz.elt_size;
849 mp->header_size = objsz.header_size;
850 mp->trailer_size = objsz.trailer_size;
851 /* Size of default caches, zero means disabled. */
852 mp->cache_size = cache_size;
853 mp->private_data_size = private_data_size;
854 STAILQ_INIT(&mp->elt_list);
855 STAILQ_INIT(&mp->mem_list);
858 * local_cache pointer is set even if cache_size is zero.
859 * The local_cache points to just past the elt_pa[] array.
861 mp->local_cache = (struct rte_mempool_cache *)
862 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
864 /* Init all default caches. */
865 if (cache_size != 0) {
866 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
867 mempool_cache_init(&mp->local_cache[lcore_id],
873 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
874 TAILQ_INSERT_TAIL(mempool_list, te, next);
875 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
876 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
881 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
883 rte_mempool_free(mp);
887 /* create the mempool */
889 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
890 unsigned cache_size, unsigned private_data_size,
891 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
892 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
893 int socket_id, unsigned flags)
896 struct rte_mempool *mp;
898 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
899 private_data_size, socket_id, flags);
904 * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
905 * set the correct index into the table of ops structs.
907 if ((flags & MEMPOOL_F_SP_PUT) && (flags & MEMPOOL_F_SC_GET))
908 ret = rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL);
909 else if (flags & MEMPOOL_F_SP_PUT)
910 ret = rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL);
911 else if (flags & MEMPOOL_F_SC_GET)
912 ret = rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL);
914 ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
919 /* call the mempool priv initializer */
921 mp_init(mp, mp_init_arg);
923 if (rte_mempool_populate_default(mp) < 0)
926 /* call the object initializers */
928 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
933 rte_mempool_free(mp);
938 * Create the mempool over already allocated chunk of memory.
939 * That external memory buffer can consists of physically disjoint pages.
940 * Setting vaddr to NULL, makes mempool to fallback to rte_mempool_create()
944 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
945 unsigned cache_size, unsigned private_data_size,
946 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
947 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
948 int socket_id, unsigned flags, void *vaddr,
949 const rte_iova_t iova[], uint32_t pg_num, uint32_t pg_shift)
951 struct rte_mempool *mp = NULL;
954 /* no virtual address supplied, use rte_mempool_create() */
956 return rte_mempool_create(name, n, elt_size, cache_size,
957 private_data_size, mp_init, mp_init_arg,
958 obj_init, obj_init_arg, socket_id, flags);
960 /* check that we have both VA and PA */
966 /* Check that pg_shift parameter is valid. */
967 if (pg_shift > MEMPOOL_PG_SHIFT_MAX) {
972 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
973 private_data_size, socket_id, flags);
977 /* call the mempool priv initializer */
979 mp_init(mp, mp_init_arg);
981 ret = rte_mempool_populate_iova_tab(mp, vaddr, iova, pg_num, pg_shift,
983 if (ret < 0 || ret != (int)mp->size)
986 /* call the object initializers */
988 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
993 rte_mempool_free(mp);
997 /* Return the number of entries in the mempool */
999 rte_mempool_avail_count(const struct rte_mempool *mp)
1004 count = rte_mempool_ops_get_count(mp);
1006 if (mp->cache_size == 0)
1009 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1010 count += mp->local_cache[lcore_id].len;
1013 * due to race condition (access to len is not locked), the
1014 * total can be greater than size... so fix the result
1016 if (count > mp->size)
1021 /* return the number of entries allocated from the mempool */
1023 rte_mempool_in_use_count(const struct rte_mempool *mp)
1025 return mp->size - rte_mempool_avail_count(mp);
1028 /* dump the cache status */
1030 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
1034 unsigned cache_count;
1036 fprintf(f, " internal cache infos:\n");
1037 fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
1039 if (mp->cache_size == 0)
1042 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1043 cache_count = mp->local_cache[lcore_id].len;
1044 fprintf(f, " cache_count[%u]=%"PRIu32"\n",
1045 lcore_id, cache_count);
1046 count += cache_count;
1048 fprintf(f, " total_cache_count=%u\n", count);
1052 #ifndef __INTEL_COMPILER
1053 #pragma GCC diagnostic ignored "-Wcast-qual"
1056 /* check and update cookies or panic (internal) */
1057 void rte_mempool_check_cookies(const struct rte_mempool *mp,
1058 void * const *obj_table_const, unsigned n, int free)
1060 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1061 struct rte_mempool_objhdr *hdr;
1062 struct rte_mempool_objtlr *tlr;
1068 /* Force to drop the "const" attribute. This is done only when
1069 * DEBUG is enabled */
1070 tmp = (void *) obj_table_const;
1076 if (rte_mempool_from_obj(obj) != mp)
1077 rte_panic("MEMPOOL: object is owned by another "
1080 hdr = __mempool_get_header(obj);
1081 cookie = hdr->cookie;
1084 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
1085 RTE_LOG(CRIT, MEMPOOL,
1086 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1087 obj, (const void *) mp, cookie);
1088 rte_panic("MEMPOOL: bad header cookie (put)\n");
1090 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
1091 } else if (free == 1) {
1092 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1093 RTE_LOG(CRIT, MEMPOOL,
1094 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1095 obj, (const void *) mp, cookie);
1096 rte_panic("MEMPOOL: bad header cookie (get)\n");
1098 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
1099 } else if (free == 2) {
1100 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
1101 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1102 RTE_LOG(CRIT, MEMPOOL,
1103 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1104 obj, (const void *) mp, cookie);
1105 rte_panic("MEMPOOL: bad header cookie (audit)\n");
1108 tlr = __mempool_get_trailer(obj);
1109 cookie = tlr->cookie;
1110 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
1111 RTE_LOG(CRIT, MEMPOOL,
1112 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1113 obj, (const void *) mp, cookie);
1114 rte_panic("MEMPOOL: bad trailer cookie\n");
1119 RTE_SET_USED(obj_table_const);
1125 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1127 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
1128 void *obj, __rte_unused unsigned idx)
1130 __mempool_check_cookies(mp, &obj, 1, 2);
1134 mempool_audit_cookies(struct rte_mempool *mp)
1138 num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
1139 if (num != mp->size) {
1140 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
1141 "iterated only over %u elements\n",
1146 #define mempool_audit_cookies(mp) do {} while(0)
1149 #ifndef __INTEL_COMPILER
1150 #pragma GCC diagnostic error "-Wcast-qual"
1153 /* check cookies before and after objects */
1155 mempool_audit_cache(const struct rte_mempool *mp)
1157 /* check cache size consistency */
1160 if (mp->cache_size == 0)
1163 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1164 const struct rte_mempool_cache *cache;
1165 cache = &mp->local_cache[lcore_id];
1166 if (cache->len > cache->flushthresh) {
1167 RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
1169 rte_panic("MEMPOOL: invalid cache len\n");
1174 /* check the consistency of mempool (size, cookies, ...) */
1176 rte_mempool_audit(struct rte_mempool *mp)
1178 mempool_audit_cache(mp);
1179 mempool_audit_cookies(mp);
1181 /* For case where mempool DEBUG is not set, and cache size is 0 */
1185 /* dump the status of the mempool on the console */
1187 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1189 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1190 struct rte_mempool_debug_stats sum;
1193 struct rte_mempool_memhdr *memhdr;
1194 unsigned common_count;
1195 unsigned cache_count;
1198 RTE_ASSERT(f != NULL);
1199 RTE_ASSERT(mp != NULL);
1201 fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1202 fprintf(f, " flags=%x\n", mp->flags);
1203 fprintf(f, " pool=%p\n", mp->pool_data);
1204 fprintf(f, " iova=0x%" PRIx64 "\n", mp->mz->iova);
1205 fprintf(f, " nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1206 fprintf(f, " size=%"PRIu32"\n", mp->size);
1207 fprintf(f, " populated_size=%"PRIu32"\n", mp->populated_size);
1208 fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
1209 fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
1210 fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
1211 fprintf(f, " total_obj_size=%"PRIu32"\n",
1212 mp->header_size + mp->elt_size + mp->trailer_size);
1214 fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
1216 STAILQ_FOREACH(memhdr, &mp->mem_list, next)
1217 mem_len += memhdr->len;
1219 fprintf(f, " avg bytes/object=%#Lf\n",
1220 (long double)mem_len / mp->size);
1223 cache_count = rte_mempool_dump_cache(f, mp);
1224 common_count = rte_mempool_ops_get_count(mp);
1225 if ((cache_count + common_count) > mp->size)
1226 common_count = mp->size - cache_count;
1227 fprintf(f, " common_pool_count=%u\n", common_count);
1229 /* sum and dump statistics */
1230 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1231 memset(&sum, 0, sizeof(sum));
1232 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1233 sum.put_bulk += mp->stats[lcore_id].put_bulk;
1234 sum.put_objs += mp->stats[lcore_id].put_objs;
1235 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1236 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1237 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1238 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1240 fprintf(f, " stats:\n");
1241 fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
1242 fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
1243 fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1244 fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1245 fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1246 fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1248 fprintf(f, " no statistics available\n");
1251 rte_mempool_audit(mp);
1254 /* dump the status of all mempools on the console */
1256 rte_mempool_list_dump(FILE *f)
1258 struct rte_mempool *mp = NULL;
1259 struct rte_tailq_entry *te;
1260 struct rte_mempool_list *mempool_list;
1262 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1264 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1266 TAILQ_FOREACH(te, mempool_list, next) {
1267 mp = (struct rte_mempool *) te->data;
1268 rte_mempool_dump(f, mp);
1271 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1274 /* search a mempool from its name */
1275 struct rte_mempool *
1276 rte_mempool_lookup(const char *name)
1278 struct rte_mempool *mp = NULL;
1279 struct rte_tailq_entry *te;
1280 struct rte_mempool_list *mempool_list;
1282 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1284 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1286 TAILQ_FOREACH(te, mempool_list, next) {
1287 mp = (struct rte_mempool *) te->data;
1288 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1292 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1302 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1305 struct rte_tailq_entry *te = NULL;
1306 struct rte_mempool_list *mempool_list;
1309 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1311 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1313 TAILQ_FOREACH_SAFE(te, mempool_list, next, tmp_te) {
1314 (*func)((struct rte_mempool *) te->data, arg);
1317 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);