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>
45 #include <rte_common.h>
47 #include <rte_debug.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_malloc.h>
51 #include <rte_atomic.h>
52 #include <rte_launch.h>
54 #include <rte_eal_memconfig.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_branch_prediction.h>
59 #include <rte_errno.h>
60 #include <rte_string_fns.h>
61 #include <rte_spinlock.h>
63 #include "rte_mempool.h"
65 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
67 static struct rte_tailq_elem rte_mempool_tailq = {
68 .name = "RTE_MEMPOOL",
70 EAL_REGISTER_TAILQ(rte_mempool_tailq)
72 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
73 #define CALC_CACHE_FLUSHTHRESH(c) \
74 ((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
77 * return the greatest common divisor between a and b (fast algorithm)
80 static unsigned get_gcd(unsigned a, unsigned b)
105 * Depending on memory configuration, objects addresses are spread
106 * between channels and ranks in RAM: the pool allocator will add
107 * padding between objects. This function return the new size of the
110 static unsigned optimize_object_size(unsigned obj_size)
112 unsigned nrank, nchan;
113 unsigned new_obj_size;
115 /* get number of channels */
116 nchan = rte_memory_get_nchannel();
120 nrank = rte_memory_get_nrank();
124 /* process new object size */
125 new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN;
126 while (get_gcd(new_obj_size, nrank * nchan) != 1)
128 return new_obj_size * RTE_MEMPOOL_ALIGN;
132 mempool_add_elem(struct rte_mempool *mp, void *obj, phys_addr_t physaddr)
134 struct rte_mempool_objhdr *hdr;
135 struct rte_mempool_objtlr *tlr __rte_unused;
137 /* set mempool ptr in header */
138 hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
140 hdr->physaddr = physaddr;
141 STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
142 mp->populated_size++;
144 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
145 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
146 tlr = __mempool_get_trailer(obj);
147 tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
150 /* enqueue in ring */
151 rte_mempool_ops_enqueue_bulk(mp, &obj, 1);
154 /* call obj_cb() for each mempool element */
156 rte_mempool_obj_iter(struct rte_mempool *mp,
157 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
159 struct rte_mempool_objhdr *hdr;
163 STAILQ_FOREACH(hdr, &mp->elt_list, next) {
164 obj = (char *)hdr + sizeof(*hdr);
165 obj_cb(mp, obj_cb_arg, obj, n);
172 /* call mem_cb() for each mempool memory chunk */
174 rte_mempool_mem_iter(struct rte_mempool *mp,
175 rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg)
177 struct rte_mempool_memhdr *hdr;
180 STAILQ_FOREACH(hdr, &mp->mem_list, next) {
181 mem_cb(mp, mem_cb_arg, hdr, n);
188 /* get the header, trailer and total size of a mempool element. */
190 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
191 struct rte_mempool_objsz *sz)
193 struct rte_mempool_objsz lsz;
195 sz = (sz != NULL) ? sz : &lsz;
197 sz->header_size = sizeof(struct rte_mempool_objhdr);
198 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
199 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
202 sz->trailer_size = sizeof(struct rte_mempool_objtlr);
204 /* element size is 8 bytes-aligned at least */
205 sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
207 /* expand trailer to next cache line */
208 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
209 sz->total_size = sz->header_size + sz->elt_size +
211 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
212 (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
213 RTE_MEMPOOL_ALIGN_MASK);
217 * increase trailer to add padding between objects in order to
218 * spread them across memory channels/ranks
220 if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
222 new_size = optimize_object_size(sz->header_size + sz->elt_size +
224 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
227 /* this is the size of an object, including header and trailer */
228 sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
230 return sz->total_size;
235 * Calculate maximum amount of memory required to store given number of objects.
238 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift)
240 size_t obj_per_page, pg_num, pg_sz;
242 if (total_elt_sz == 0)
246 return total_elt_sz * elt_num;
248 pg_sz = (size_t)1 << pg_shift;
249 obj_per_page = pg_sz / total_elt_sz;
250 if (obj_per_page == 0)
251 return RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
253 pg_num = (elt_num + obj_per_page - 1) / obj_per_page;
254 return pg_num << pg_shift;
258 * Calculate how much memory would be actually required with the
259 * given memory footprint to store required number of elements.
262 rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num,
263 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
266 uint32_t elt_cnt = 0;
267 phys_addr_t start, end;
269 size_t pg_sz = (size_t)1 << pg_shift;
271 /* if paddr is NULL, assume contiguous memory */
274 end = pg_sz * pg_num;
278 end = paddr[0] + pg_sz;
281 while (elt_cnt < elt_num) {
283 if (end - start >= total_elt_sz) {
284 /* enough contiguous memory, add an object */
285 start += total_elt_sz;
287 } else if (paddr_idx < pg_num) {
288 /* no room to store one obj, add a page */
289 if (end == paddr[paddr_idx]) {
292 start = paddr[paddr_idx];
293 end = paddr[paddr_idx] + pg_sz;
298 /* no more page, return how many elements fit */
299 return -(size_t)elt_cnt;
303 return (size_t)paddr_idx << pg_shift;
306 /* free a memchunk allocated with rte_memzone_reserve() */
308 rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
311 const struct rte_memzone *mz = opaque;
312 rte_memzone_free(mz);
315 /* Free memory chunks used by a mempool. Objects must be in pool */
317 rte_mempool_free_memchunks(struct rte_mempool *mp)
319 struct rte_mempool_memhdr *memhdr;
322 while (!STAILQ_EMPTY(&mp->elt_list)) {
323 rte_mempool_ops_dequeue_bulk(mp, &elt, 1);
325 STAILQ_REMOVE_HEAD(&mp->elt_list, next);
326 mp->populated_size--;
329 while (!STAILQ_EMPTY(&mp->mem_list)) {
330 memhdr = STAILQ_FIRST(&mp->mem_list);
331 STAILQ_REMOVE_HEAD(&mp->mem_list, next);
332 if (memhdr->free_cb != NULL)
333 memhdr->free_cb(memhdr, memhdr->opaque);
339 /* Add objects in the pool, using a physically contiguous memory
340 * zone. Return the number of objects added, or a negative value
344 rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
345 phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
348 unsigned total_elt_sz;
351 struct rte_mempool_memhdr *memhdr;
354 /* create the internal ring if not already done */
355 if ((mp->flags & MEMPOOL_F_POOL_CREATED) == 0) {
356 ret = rte_mempool_ops_alloc(mp);
359 mp->flags |= MEMPOOL_F_POOL_CREATED;
362 /* mempool is already populated */
363 if (mp->populated_size >= mp->size)
366 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
368 memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
373 memhdr->addr = vaddr;
374 memhdr->phys_addr = paddr;
376 memhdr->free_cb = free_cb;
377 memhdr->opaque = opaque;
379 if (mp->flags & MEMPOOL_F_NO_CACHE_ALIGN)
380 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
382 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_CACHE_LINE_SIZE) - vaddr;
384 while (off + total_elt_sz <= len && mp->populated_size < mp->size) {
385 off += mp->header_size;
386 if (paddr == RTE_BAD_PHYS_ADDR)
387 mempool_add_elem(mp, (char *)vaddr + off,
390 mempool_add_elem(mp, (char *)vaddr + off, paddr + off);
391 off += mp->elt_size + mp->trailer_size;
395 /* not enough room to store one object */
399 STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
404 /* Add objects in the pool, using a table of physical pages. Return the
405 * number of objects added, or a negative value on error.
408 rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
409 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
410 rte_mempool_memchunk_free_cb_t *free_cb, void *opaque)
414 size_t pg_sz = (size_t)1 << pg_shift;
416 /* mempool must not be populated */
417 if (mp->nb_mem_chunks != 0)
420 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
421 return rte_mempool_populate_phys(mp, vaddr, RTE_BAD_PHYS_ADDR,
422 pg_num * pg_sz, free_cb, opaque);
424 for (i = 0; i < pg_num && mp->populated_size < mp->size; i += n) {
426 /* populate with the largest group of contiguous pages */
427 for (n = 1; (i + n) < pg_num &&
428 paddr[i] + pg_sz == paddr[i+n]; n++)
431 ret = rte_mempool_populate_phys(mp, vaddr + i * pg_sz,
432 paddr[i], n * pg_sz, free_cb, opaque);
434 rte_mempool_free_memchunks(mp);
437 /* no need to call the free callback for next chunks */
444 /* Populate the mempool with a virtual area. Return the number of
445 * objects added, or a negative value on error.
448 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
449 size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
453 size_t off, phys_len;
456 /* mempool must not be populated */
457 if (mp->nb_mem_chunks != 0)
459 /* address and len must be page-aligned */
460 if (RTE_PTR_ALIGN_CEIL(addr, pg_sz) != addr)
462 if (RTE_ALIGN_CEIL(len, pg_sz) != len)
465 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
466 return rte_mempool_populate_phys(mp, addr, RTE_BAD_PHYS_ADDR,
467 len, free_cb, opaque);
469 for (off = 0; off + pg_sz <= len &&
470 mp->populated_size < mp->size; off += phys_len) {
472 paddr = rte_mem_virt2phy(addr + off);
473 /* required for xen_dom0 to get the machine address */
474 paddr = rte_mem_phy2mch(-1, paddr);
476 if (paddr == RTE_BAD_PHYS_ADDR) {
481 /* populate with the largest group of contiguous pages */
482 for (phys_len = pg_sz; off + phys_len < len; phys_len += pg_sz) {
483 phys_addr_t paddr_tmp;
485 paddr_tmp = rte_mem_virt2phy(addr + off + phys_len);
486 paddr_tmp = rte_mem_phy2mch(-1, paddr_tmp);
488 if (paddr_tmp != paddr + phys_len)
492 ret = rte_mempool_populate_phys(mp, addr + off, paddr,
493 phys_len, free_cb, opaque);
496 /* no need to call the free callback for next chunks */
504 rte_mempool_free_memchunks(mp);
508 /* Default function to populate the mempool: allocate memory in memzones,
509 * and populate them. Return the number of objects added, or a negative
513 rte_mempool_populate_default(struct rte_mempool *mp)
515 int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
516 char mz_name[RTE_MEMZONE_NAMESIZE];
517 const struct rte_memzone *mz;
518 size_t size, total_elt_sz, align, pg_sz, pg_shift;
523 /* mempool must not be populated */
524 if (mp->nb_mem_chunks != 0)
527 if (rte_xen_dom0_supported()) {
528 pg_sz = RTE_PGSIZE_2M;
529 pg_shift = rte_bsf32(pg_sz);
531 } else if (rte_eal_has_hugepages()) {
532 pg_shift = 0; /* not needed, zone is physically contiguous */
534 align = RTE_CACHE_LINE_SIZE;
536 pg_sz = getpagesize();
537 pg_shift = rte_bsf32(pg_sz);
541 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
542 for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
543 size = rte_mempool_xmem_size(n, total_elt_sz, pg_shift);
545 ret = snprintf(mz_name, sizeof(mz_name),
546 RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
547 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
552 mz = rte_memzone_reserve_aligned(mz_name, size,
553 mp->socket_id, mz_flags, align);
554 /* not enough memory, retry with the biggest zone we have */
556 mz = rte_memzone_reserve_aligned(mz_name, 0,
557 mp->socket_id, mz_flags, align);
563 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
564 paddr = RTE_BAD_PHYS_ADDR;
566 paddr = mz->phys_addr;
568 if (rte_eal_has_hugepages() && !rte_xen_dom0_supported())
569 ret = rte_mempool_populate_phys(mp, mz->addr,
571 rte_mempool_memchunk_mz_free,
572 (void *)(uintptr_t)mz);
574 ret = rte_mempool_populate_virt(mp, mz->addr,
576 rte_mempool_memchunk_mz_free,
577 (void *)(uintptr_t)mz);
585 rte_mempool_free_memchunks(mp);
589 /* return the memory size required for mempool objects in anonymous mem */
591 get_anon_size(const struct rte_mempool *mp)
593 size_t size, total_elt_sz, pg_sz, pg_shift;
595 pg_sz = getpagesize();
596 pg_shift = rte_bsf32(pg_sz);
597 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
598 size = rte_mempool_xmem_size(mp->size, total_elt_sz, pg_shift);
603 /* unmap a memory zone mapped by rte_mempool_populate_anon() */
605 rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr,
608 munmap(opaque, get_anon_size(memhdr->mp));
611 /* populate the mempool with an anonymous mapping */
613 rte_mempool_populate_anon(struct rte_mempool *mp)
619 /* mempool is already populated, error */
620 if (!STAILQ_EMPTY(&mp->mem_list)) {
625 /* get chunk of virtually continuous memory */
626 size = get_anon_size(mp);
627 addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
628 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
629 if (addr == MAP_FAILED) {
633 /* can't use MMAP_LOCKED, it does not exist on BSD */
634 if (mlock(addr, size) < 0) {
640 ret = rte_mempool_populate_virt(mp, addr, size, getpagesize(),
641 rte_mempool_memchunk_anon_free, addr);
645 return mp->populated_size;
648 rte_mempool_free_memchunks(mp);
654 rte_mempool_free(struct rte_mempool *mp)
656 struct rte_mempool_list *mempool_list = NULL;
657 struct rte_tailq_entry *te;
662 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
663 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
664 /* find out tailq entry */
665 TAILQ_FOREACH(te, mempool_list, next) {
666 if (te->data == (void *)mp)
671 TAILQ_REMOVE(mempool_list, te, next);
674 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
676 rte_mempool_free_memchunks(mp);
677 rte_mempool_ops_free(mp);
678 rte_memzone_free(mp->mz);
682 mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)
685 cache->flushthresh = CALC_CACHE_FLUSHTHRESH(size);
690 * Create and initialize a cache for objects that are retrieved from and
691 * returned to an underlying mempool. This structure is identical to the
692 * local_cache[lcore_id] pointed to by the mempool structure.
694 struct rte_mempool_cache *
695 rte_mempool_cache_create(uint32_t size, int socket_id)
697 struct rte_mempool_cache *cache;
699 if (size == 0 || size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
704 cache = rte_zmalloc_socket("MEMPOOL_CACHE", sizeof(*cache),
705 RTE_CACHE_LINE_SIZE, socket_id);
707 RTE_LOG(ERR, MEMPOOL, "Cannot allocate mempool cache.\n");
712 mempool_cache_init(cache, size);
718 * Free a cache. It's the responsibility of the user to make sure that any
719 * remaining objects in the cache are flushed to the corresponding
723 rte_mempool_cache_free(struct rte_mempool_cache *cache)
728 /* create an empty mempool */
730 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
731 unsigned cache_size, unsigned private_data_size,
732 int socket_id, unsigned flags)
734 char mz_name[RTE_MEMZONE_NAMESIZE];
735 struct rte_mempool_list *mempool_list;
736 struct rte_mempool *mp = NULL;
737 struct rte_tailq_entry *te = NULL;
738 const struct rte_memzone *mz = NULL;
740 int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
741 struct rte_mempool_objsz objsz;
745 /* compilation-time checks */
746 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
747 RTE_CACHE_LINE_MASK) != 0);
748 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
749 RTE_CACHE_LINE_MASK) != 0);
750 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
751 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
752 RTE_CACHE_LINE_MASK) != 0);
753 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
754 RTE_CACHE_LINE_MASK) != 0);
757 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
759 /* asked cache too big */
760 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
761 CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
766 /* "no cache align" imply "no spread" */
767 if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
768 flags |= MEMPOOL_F_NO_SPREAD;
770 /* calculate mempool object sizes. */
771 if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
776 rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
779 * reserve a memory zone for this mempool: private data is
782 private_data_size = (private_data_size +
783 RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
786 /* try to allocate tailq entry */
787 te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
789 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
793 mempool_size = MEMPOOL_HEADER_SIZE(mp, cache_size);
794 mempool_size += private_data_size;
795 mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
797 ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
798 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
799 rte_errno = ENAMETOOLONG;
803 mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
807 /* init the mempool structure */
809 memset(mp, 0, MEMPOOL_HEADER_SIZE(mp, cache_size));
810 ret = snprintf(mp->name, sizeof(mp->name), "%s", name);
811 if (ret < 0 || ret >= (int)sizeof(mp->name)) {
812 rte_errno = ENAMETOOLONG;
816 mp->socket_id = socket_id;
819 mp->socket_id = socket_id;
820 mp->elt_size = objsz.elt_size;
821 mp->header_size = objsz.header_size;
822 mp->trailer_size = objsz.trailer_size;
823 /* Size of default caches, zero means disabled. */
824 mp->cache_size = cache_size;
825 mp->private_data_size = private_data_size;
826 STAILQ_INIT(&mp->elt_list);
827 STAILQ_INIT(&mp->mem_list);
830 * local_cache pointer is set even if cache_size is zero.
831 * The local_cache points to just past the elt_pa[] array.
833 mp->local_cache = (struct rte_mempool_cache *)
834 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
836 /* Init all default caches. */
837 if (cache_size != 0) {
838 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
839 mempool_cache_init(&mp->local_cache[lcore_id],
845 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
846 TAILQ_INSERT_TAIL(mempool_list, te, next);
847 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
848 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
853 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
855 rte_mempool_free(mp);
859 /* create the mempool */
861 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
862 unsigned cache_size, unsigned private_data_size,
863 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
864 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
865 int socket_id, unsigned flags)
867 struct rte_mempool *mp;
869 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
870 private_data_size, socket_id, flags);
875 * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
876 * set the correct index into the table of ops structs.
878 if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
879 rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL);
880 else if (flags & MEMPOOL_F_SP_PUT)
881 rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL);
882 else if (flags & MEMPOOL_F_SC_GET)
883 rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL);
885 rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
887 /* call the mempool priv initializer */
889 mp_init(mp, mp_init_arg);
891 if (rte_mempool_populate_default(mp) < 0)
894 /* call the object initializers */
896 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
901 rte_mempool_free(mp);
906 * Create the mempool over already allocated chunk of memory.
907 * That external memory buffer can consists of physically disjoint pages.
908 * Setting vaddr to NULL, makes mempool to fallback to original behaviour
909 * and allocate space for mempool and it's elements as one big chunk of
910 * physically continuos memory.
913 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
914 unsigned cache_size, unsigned private_data_size,
915 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
916 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
917 int socket_id, unsigned flags, void *vaddr,
918 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
920 struct rte_mempool *mp = NULL;
923 /* no virtual address supplied, use rte_mempool_create() */
925 return rte_mempool_create(name, n, elt_size, cache_size,
926 private_data_size, mp_init, mp_init_arg,
927 obj_init, obj_init_arg, socket_id, flags);
929 /* check that we have both VA and PA */
935 /* Check that pg_shift parameter is valid. */
936 if (pg_shift > MEMPOOL_PG_SHIFT_MAX) {
941 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
942 private_data_size, socket_id, flags);
946 /* call the mempool priv initializer */
948 mp_init(mp, mp_init_arg);
950 ret = rte_mempool_populate_phys_tab(mp, vaddr, paddr, pg_num, pg_shift,
952 if (ret < 0 || ret != (int)mp->size)
955 /* call the object initializers */
957 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
962 rte_mempool_free(mp);
966 /* Return the number of entries in the mempool */
968 rte_mempool_avail_count(const struct rte_mempool *mp)
973 count = rte_mempool_ops_get_count(mp);
975 if (mp->cache_size == 0)
978 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
979 count += mp->local_cache[lcore_id].len;
982 * due to race condition (access to len is not locked), the
983 * total can be greater than size... so fix the result
985 if (count > mp->size)
990 /* return the number of entries allocated from the mempool */
992 rte_mempool_in_use_count(const struct rte_mempool *mp)
994 return mp->size - rte_mempool_avail_count(mp);
998 rte_mempool_count(const struct rte_mempool *mp)
1000 return rte_mempool_avail_count(mp);
1003 /* dump the cache status */
1005 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
1009 unsigned cache_count;
1011 fprintf(f, " internal cache infos:\n");
1012 fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
1014 if (mp->cache_size == 0)
1017 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1018 cache_count = mp->local_cache[lcore_id].len;
1019 fprintf(f, " cache_count[%u]=%"PRIu32"\n",
1020 lcore_id, cache_count);
1021 count += cache_count;
1023 fprintf(f, " total_cache_count=%u\n", count);
1027 #ifndef __INTEL_COMPILER
1028 #pragma GCC diagnostic ignored "-Wcast-qual"
1031 /* check and update cookies or panic (internal) */
1032 void rte_mempool_check_cookies(const struct rte_mempool *mp,
1033 void * const *obj_table_const, unsigned n, int free)
1035 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1036 struct rte_mempool_objhdr *hdr;
1037 struct rte_mempool_objtlr *tlr;
1043 /* Force to drop the "const" attribute. This is done only when
1044 * DEBUG is enabled */
1045 tmp = (void *) obj_table_const;
1046 obj_table = (void **) tmp;
1051 if (rte_mempool_from_obj(obj) != mp)
1052 rte_panic("MEMPOOL: object is owned by another "
1055 hdr = __mempool_get_header(obj);
1056 cookie = hdr->cookie;
1059 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
1060 RTE_LOG(CRIT, MEMPOOL,
1061 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1062 obj, (const void *) mp, cookie);
1063 rte_panic("MEMPOOL: bad header cookie (put)\n");
1065 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
1066 } else if (free == 1) {
1067 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1068 RTE_LOG(CRIT, MEMPOOL,
1069 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1070 obj, (const void *) mp, cookie);
1071 rte_panic("MEMPOOL: bad header cookie (get)\n");
1073 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
1074 } else if (free == 2) {
1075 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
1076 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1077 RTE_LOG(CRIT, MEMPOOL,
1078 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1079 obj, (const void *) mp, cookie);
1080 rte_panic("MEMPOOL: bad header cookie (audit)\n");
1083 tlr = __mempool_get_trailer(obj);
1084 cookie = tlr->cookie;
1085 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
1086 RTE_LOG(CRIT, MEMPOOL,
1087 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1088 obj, (const void *) mp, cookie);
1089 rte_panic("MEMPOOL: bad trailer cookie\n");
1094 RTE_SET_USED(obj_table_const);
1100 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1102 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
1103 void *obj, __rte_unused unsigned idx)
1105 __mempool_check_cookies(mp, &obj, 1, 2);
1109 mempool_audit_cookies(struct rte_mempool *mp)
1113 num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
1114 if (num != mp->size) {
1115 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
1116 "iterated only over %u elements\n",
1121 #define mempool_audit_cookies(mp) do {} while(0)
1124 #ifndef __INTEL_COMPILER
1125 #pragma GCC diagnostic error "-Wcast-qual"
1128 /* check cookies before and after objects */
1130 mempool_audit_cache(const struct rte_mempool *mp)
1132 /* check cache size consistency */
1135 if (mp->cache_size == 0)
1138 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1139 const struct rte_mempool_cache *cache;
1140 cache = &mp->local_cache[lcore_id];
1141 if (cache->len > cache->flushthresh) {
1142 RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
1144 rte_panic("MEMPOOL: invalid cache len\n");
1149 /* check the consistency of mempool (size, cookies, ...) */
1151 rte_mempool_audit(struct rte_mempool *mp)
1153 mempool_audit_cache(mp);
1154 mempool_audit_cookies(mp);
1156 /* For case where mempool DEBUG is not set, and cache size is 0 */
1160 /* dump the status of the mempool on the console */
1162 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1164 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1165 struct rte_mempool_debug_stats sum;
1168 struct rte_mempool_memhdr *memhdr;
1169 unsigned common_count;
1170 unsigned cache_count;
1173 RTE_ASSERT(f != NULL);
1174 RTE_ASSERT(mp != NULL);
1176 fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1177 fprintf(f, " flags=%x\n", mp->flags);
1178 fprintf(f, " pool=%p\n", mp->pool_data);
1179 fprintf(f, " phys_addr=0x%" PRIx64 "\n", mp->mz->phys_addr);
1180 fprintf(f, " nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1181 fprintf(f, " size=%"PRIu32"\n", mp->size);
1182 fprintf(f, " populated_size=%"PRIu32"\n", mp->populated_size);
1183 fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
1184 fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
1185 fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
1186 fprintf(f, " total_obj_size=%"PRIu32"\n",
1187 mp->header_size + mp->elt_size + mp->trailer_size);
1189 fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
1191 STAILQ_FOREACH(memhdr, &mp->mem_list, next)
1192 mem_len += memhdr->len;
1194 fprintf(f, " avg bytes/object=%#Lf\n",
1195 (long double)mem_len / mp->size);
1198 cache_count = rte_mempool_dump_cache(f, mp);
1199 common_count = rte_mempool_ops_get_count(mp);
1200 if ((cache_count + common_count) > mp->size)
1201 common_count = mp->size - cache_count;
1202 fprintf(f, " common_pool_count=%u\n", common_count);
1204 /* sum and dump statistics */
1205 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1206 memset(&sum, 0, sizeof(sum));
1207 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1208 sum.put_bulk += mp->stats[lcore_id].put_bulk;
1209 sum.put_objs += mp->stats[lcore_id].put_objs;
1210 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1211 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1212 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1213 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1215 fprintf(f, " stats:\n");
1216 fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
1217 fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
1218 fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1219 fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1220 fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1221 fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1223 fprintf(f, " no statistics available\n");
1226 rte_mempool_audit(mp);
1229 /* dump the status of all mempools on the console */
1231 rte_mempool_list_dump(FILE *f)
1233 struct rte_mempool *mp = NULL;
1234 struct rte_tailq_entry *te;
1235 struct rte_mempool_list *mempool_list;
1237 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1239 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1241 TAILQ_FOREACH(te, mempool_list, next) {
1242 mp = (struct rte_mempool *) te->data;
1243 rte_mempool_dump(f, mp);
1246 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1249 /* search a mempool from its name */
1250 struct rte_mempool *
1251 rte_mempool_lookup(const char *name)
1253 struct rte_mempool *mp = NULL;
1254 struct rte_tailq_entry *te;
1255 struct rte_mempool_list *mempool_list;
1257 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1259 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1261 TAILQ_FOREACH(te, mempool_list, next) {
1262 mp = (struct rte_mempool *) te->data;
1263 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1267 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1277 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1280 struct rte_tailq_entry *te = NULL;
1281 struct rte_mempool_list *mempool_list;
1283 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1285 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1287 TAILQ_FOREACH(te, mempool_list, next) {
1288 (*func)((struct rte_mempool *) te->data, arg);
1291 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);