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_ring_sp_enqueue(mp->ring, obj);
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;
243 return total_elt_sz * elt_num;
245 pg_sz = (size_t)1 << pg_shift;
246 obj_per_page = pg_sz / total_elt_sz;
247 if (obj_per_page == 0)
248 return RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
250 pg_num = (elt_num + obj_per_page - 1) / obj_per_page;
251 return pg_num << pg_shift;
255 * Calculate how much memory would be actually required with the
256 * given memory footprint to store required number of elements.
259 rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num,
260 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
263 uint32_t elt_cnt = 0;
264 phys_addr_t start, end;
266 size_t pg_sz = (size_t)1 << pg_shift;
268 /* if paddr is NULL, assume contiguous memory */
271 end = pg_sz * pg_num;
275 end = paddr[0] + pg_sz;
278 while (elt_cnt < elt_num) {
280 if (end - start >= total_elt_sz) {
281 /* enough contiguous memory, add an object */
282 start += total_elt_sz;
284 } else if (paddr_idx < pg_num) {
285 /* no room to store one obj, add a page */
286 if (end == paddr[paddr_idx]) {
289 start = paddr[paddr_idx];
290 end = paddr[paddr_idx] + pg_sz;
295 /* no more page, return how many elements fit */
296 return -(size_t)elt_cnt;
300 return (size_t)paddr_idx << pg_shift;
303 /* create the internal ring */
305 rte_mempool_ring_create(struct rte_mempool *mp)
308 char rg_name[RTE_RING_NAMESIZE];
311 snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, mp->name);
314 if (mp->flags & MEMPOOL_F_SP_PUT)
315 rg_flags |= RING_F_SP_ENQ;
316 if (mp->flags & MEMPOOL_F_SC_GET)
317 rg_flags |= RING_F_SC_DEQ;
319 /* Allocate the ring that will be used to store objects.
320 * Ring functions will return appropriate errors if we are
321 * running as a secondary process etc., so no checks made
322 * in this function for that condition.
324 r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
325 mp->socket_id, rg_flags);
330 mp->flags |= MEMPOOL_F_RING_CREATED;
334 /* free a memchunk allocated with rte_memzone_reserve() */
336 rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
339 const struct rte_memzone *mz = opaque;
340 rte_memzone_free(mz);
343 /* Free memory chunks used by a mempool. Objects must be in pool */
345 rte_mempool_free_memchunks(struct rte_mempool *mp)
347 struct rte_mempool_memhdr *memhdr;
350 while (!STAILQ_EMPTY(&mp->elt_list)) {
351 rte_ring_sc_dequeue(mp->ring, &elt);
353 STAILQ_REMOVE_HEAD(&mp->elt_list, next);
354 mp->populated_size--;
357 while (!STAILQ_EMPTY(&mp->mem_list)) {
358 memhdr = STAILQ_FIRST(&mp->mem_list);
359 STAILQ_REMOVE_HEAD(&mp->mem_list, next);
360 if (memhdr->free_cb != NULL)
361 memhdr->free_cb(memhdr, memhdr->opaque);
367 /* Add objects in the pool, using a physically contiguous memory
368 * zone. Return the number of objects added, or a negative value
372 rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
373 phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
376 unsigned total_elt_sz;
379 struct rte_mempool_memhdr *memhdr;
382 /* create the internal ring if not already done */
383 if ((mp->flags & MEMPOOL_F_RING_CREATED) == 0) {
384 ret = rte_mempool_ring_create(mp);
389 /* mempool is already populated */
390 if (mp->populated_size >= mp->size)
393 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
395 memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
400 memhdr->addr = vaddr;
401 memhdr->phys_addr = paddr;
403 memhdr->free_cb = free_cb;
404 memhdr->opaque = opaque;
406 if (mp->flags & MEMPOOL_F_NO_CACHE_ALIGN)
407 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
409 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_CACHE_LINE_SIZE) - vaddr;
411 while (off + total_elt_sz <= len && mp->populated_size < mp->size) {
412 off += mp->header_size;
413 mempool_add_elem(mp, (char *)vaddr + off, paddr + off);
414 off += mp->elt_size + mp->trailer_size;
418 /* not enough room to store one object */
422 STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
427 /* Add objects in the pool, using a table of physical pages. Return the
428 * number of objects added, or a negative value on error.
431 rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
432 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
433 rte_mempool_memchunk_free_cb_t *free_cb, void *opaque)
437 size_t pg_sz = (size_t)1 << pg_shift;
439 /* mempool must not be populated */
440 if (mp->nb_mem_chunks != 0)
443 for (i = 0; i < pg_num && mp->populated_size < mp->size; i += n) {
445 /* populate with the largest group of contiguous pages */
446 for (n = 1; (i + n) < pg_num &&
447 paddr[i] + pg_sz == paddr[i+n]; n++)
450 ret = rte_mempool_populate_phys(mp, vaddr + i * pg_sz,
451 paddr[i], n * pg_sz, free_cb, opaque);
453 rte_mempool_free_memchunks(mp);
456 /* no need to call the free callback for next chunks */
463 /* Populate the mempool with a virtual area. Return the number of
464 * objects added, or a negative value on error.
467 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
468 size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
472 size_t off, phys_len;
475 /* mempool must not be populated */
476 if (mp->nb_mem_chunks != 0)
478 /* address and len must be page-aligned */
479 if (RTE_PTR_ALIGN_CEIL(addr, pg_sz) != addr)
481 if (RTE_ALIGN_CEIL(len, pg_sz) != len)
484 for (off = 0; off + pg_sz <= len &&
485 mp->populated_size < mp->size; off += phys_len) {
487 paddr = rte_mem_virt2phy(addr + off);
488 /* required for xen_dom0 to get the machine address */
489 paddr = rte_mem_phy2mch(-1, paddr);
491 if (paddr == RTE_BAD_PHYS_ADDR) {
496 /* populate with the largest group of contiguous pages */
497 for (phys_len = pg_sz; off + phys_len < len; phys_len += pg_sz) {
498 phys_addr_t paddr_tmp;
500 paddr_tmp = rte_mem_virt2phy(addr + off + phys_len);
501 paddr_tmp = rte_mem_phy2mch(-1, paddr_tmp);
503 if (paddr_tmp != paddr + phys_len)
507 ret = rte_mempool_populate_phys(mp, addr + off, paddr,
508 phys_len, free_cb, opaque);
511 /* no need to call the free callback for next chunks */
519 rte_mempool_free_memchunks(mp);
523 /* Default function to populate the mempool: allocate memory in memzones,
524 * and populate them. Return the number of objects added, or a negative
528 rte_mempool_populate_default(struct rte_mempool *mp)
530 int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
531 char mz_name[RTE_MEMZONE_NAMESIZE];
532 const struct rte_memzone *mz;
533 size_t size, total_elt_sz, align, pg_sz, pg_shift;
537 /* mempool must not be populated */
538 if (mp->nb_mem_chunks != 0)
541 if (rte_eal_has_hugepages()) {
542 pg_shift = 0; /* not needed, zone is physically contiguous */
544 align = RTE_CACHE_LINE_SIZE;
546 pg_sz = getpagesize();
547 pg_shift = rte_bsf32(pg_sz);
551 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
552 for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
553 size = rte_mempool_xmem_size(n, total_elt_sz, pg_shift);
555 ret = snprintf(mz_name, sizeof(mz_name),
556 RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
557 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
562 mz = rte_memzone_reserve_aligned(mz_name, size,
563 mp->socket_id, mz_flags, align);
564 /* not enough memory, retry with the biggest zone we have */
566 mz = rte_memzone_reserve_aligned(mz_name, 0,
567 mp->socket_id, mz_flags, align);
573 /* use memzone physical address if it is valid */
574 if (rte_eal_has_hugepages() && !rte_xen_dom0_supported())
575 ret = rte_mempool_populate_phys(mp, mz->addr,
576 mz->phys_addr, mz->len,
577 rte_mempool_memchunk_mz_free,
578 (void *)(uintptr_t)mz);
580 ret = rte_mempool_populate_virt(mp, mz->addr,
582 rte_mempool_memchunk_mz_free,
583 (void *)(uintptr_t)mz);
591 rte_mempool_free_memchunks(mp);
595 /* return the memory size required for mempool objects in anonymous mem */
597 get_anon_size(const struct rte_mempool *mp)
599 size_t size, total_elt_sz, pg_sz, pg_shift;
601 pg_sz = getpagesize();
602 pg_shift = rte_bsf32(pg_sz);
603 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
604 size = rte_mempool_xmem_size(mp->size, total_elt_sz, pg_shift);
609 /* unmap a memory zone mapped by rte_mempool_populate_anon() */
611 rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr,
614 munmap(opaque, get_anon_size(memhdr->mp));
617 /* populate the mempool with an anonymous mapping */
618 __rte_unused static int
619 rte_mempool_populate_anon(struct rte_mempool *mp)
625 /* mempool is already populated, error */
626 if (!STAILQ_EMPTY(&mp->mem_list)) {
631 /* get chunk of virtually continuous memory */
632 size = get_anon_size(mp);
633 addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
634 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
635 if (addr == MAP_FAILED) {
639 /* can't use MMAP_LOCKED, it does not exist on BSD */
640 if (mlock(addr, size) < 0) {
646 ret = rte_mempool_populate_virt(mp, addr, size, getpagesize(),
647 rte_mempool_memchunk_anon_free, addr);
651 return mp->populated_size;
654 rte_mempool_free_memchunks(mp);
660 rte_mempool_free(struct rte_mempool *mp)
662 struct rte_mempool_list *mempool_list = NULL;
663 struct rte_tailq_entry *te;
668 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
669 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
670 /* find out tailq entry */
671 TAILQ_FOREACH(te, mempool_list, next) {
672 if (te->data == (void *)mp)
677 TAILQ_REMOVE(mempool_list, te, next);
680 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
682 rte_mempool_free_memchunks(mp);
683 rte_ring_free(mp->ring);
684 rte_memzone_free(mp->mz);
687 /* create an empty mempool */
688 static struct rte_mempool *
689 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
690 unsigned cache_size, unsigned private_data_size,
691 int socket_id, unsigned flags)
693 char mz_name[RTE_MEMZONE_NAMESIZE];
694 struct rte_mempool_list *mempool_list;
695 struct rte_mempool *mp = NULL;
696 struct rte_tailq_entry *te = NULL;
697 const struct rte_memzone *mz = NULL;
699 int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
700 struct rte_mempool_objsz objsz;
702 /* compilation-time checks */
703 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
704 RTE_CACHE_LINE_MASK) != 0);
705 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
706 RTE_CACHE_LINE_MASK) != 0);
707 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
708 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
709 RTE_CACHE_LINE_MASK) != 0);
710 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
711 RTE_CACHE_LINE_MASK) != 0);
714 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
716 /* asked cache too big */
717 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
718 CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
723 /* "no cache align" imply "no spread" */
724 if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
725 flags |= MEMPOOL_F_NO_SPREAD;
727 /* calculate mempool object sizes. */
728 if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
733 rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
736 * reserve a memory zone for this mempool: private data is
739 private_data_size = (private_data_size +
740 RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
743 /* try to allocate tailq entry */
744 te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
746 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
750 mempool_size = MEMPOOL_HEADER_SIZE(mp, cache_size);
751 mempool_size += private_data_size;
752 mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
754 snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
756 mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
760 /* init the mempool structure */
762 memset(mp, 0, sizeof(*mp));
763 snprintf(mp->name, sizeof(mp->name), "%s", name);
765 mp->socket_id = socket_id;
768 mp->socket_id = socket_id;
769 mp->elt_size = objsz.elt_size;
770 mp->header_size = objsz.header_size;
771 mp->trailer_size = objsz.trailer_size;
772 mp->cache_size = cache_size;
773 mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
774 mp->private_data_size = private_data_size;
775 STAILQ_INIT(&mp->elt_list);
776 STAILQ_INIT(&mp->mem_list);
779 * local_cache pointer is set even if cache_size is zero.
780 * The local_cache points to just past the elt_pa[] array.
782 mp->local_cache = (struct rte_mempool_cache *)
783 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
786 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
787 TAILQ_INSERT_TAIL(mempool_list, te, next);
788 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
789 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
794 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
796 rte_mempool_free(mp);
800 /* create the mempool */
802 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
803 unsigned cache_size, unsigned private_data_size,
804 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
805 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
806 int socket_id, unsigned flags)
808 struct rte_mempool *mp;
810 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
811 private_data_size, socket_id, flags);
815 /* call the mempool priv initializer */
817 mp_init(mp, mp_init_arg);
819 if (rte_mempool_populate_default(mp) < 0)
822 /* call the object initializers */
824 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
829 rte_mempool_free(mp);
834 * Create the mempool over already allocated chunk of memory.
835 * That external memory buffer can consists of physically disjoint pages.
836 * Setting vaddr to NULL, makes mempool to fallback to original behaviour
837 * and allocate space for mempool and it's elements as one big chunk of
838 * physically continuos memory.
841 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
842 unsigned cache_size, unsigned private_data_size,
843 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
844 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
845 int socket_id, unsigned flags, void *vaddr,
846 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
848 struct rte_mempool *mp = NULL;
851 /* no virtual address supplied, use rte_mempool_create() */
853 return rte_mempool_create(name, n, elt_size, cache_size,
854 private_data_size, mp_init, mp_init_arg,
855 obj_init, obj_init_arg, socket_id, flags);
857 /* check that we have both VA and PA */
863 /* Check that pg_shift parameter is valid. */
864 if (pg_shift > MEMPOOL_PG_SHIFT_MAX) {
869 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
870 private_data_size, socket_id, flags);
874 /* call the mempool priv initializer */
876 mp_init(mp, mp_init_arg);
878 ret = rte_mempool_populate_phys_tab(mp, vaddr, paddr, pg_num, pg_shift,
880 if (ret < 0 || ret != (int)mp->size)
883 /* call the object initializers */
885 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
890 rte_mempool_free(mp);
894 /* Return the number of entries in the mempool */
896 rte_mempool_count(const struct rte_mempool *mp)
901 count = rte_ring_count(mp->ring);
903 if (mp->cache_size == 0)
906 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
907 count += mp->local_cache[lcore_id].len;
910 * due to race condition (access to len is not locked), the
911 * total can be greater than size... so fix the result
913 if (count > mp->size)
918 /* dump the cache status */
920 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
924 unsigned cache_count;
926 fprintf(f, " cache infos:\n");
927 fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
929 if (mp->cache_size == 0)
932 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
933 cache_count = mp->local_cache[lcore_id].len;
934 fprintf(f, " cache_count[%u]=%u\n", lcore_id, cache_count);
935 count += cache_count;
937 fprintf(f, " total_cache_count=%u\n", count);
941 #ifndef __INTEL_COMPILER
942 #pragma GCC diagnostic ignored "-Wcast-qual"
945 /* check and update cookies or panic (internal) */
946 void rte_mempool_check_cookies(const struct rte_mempool *mp,
947 void * const *obj_table_const, unsigned n, int free)
949 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
950 struct rte_mempool_objhdr *hdr;
951 struct rte_mempool_objtlr *tlr;
957 /* Force to drop the "const" attribute. This is done only when
958 * DEBUG is enabled */
959 tmp = (void *) obj_table_const;
960 obj_table = (void **) tmp;
965 if (rte_mempool_from_obj(obj) != mp)
966 rte_panic("MEMPOOL: object is owned by another "
969 hdr = __mempool_get_header(obj);
970 cookie = hdr->cookie;
973 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
974 rte_log_set_history(0);
975 RTE_LOG(CRIT, MEMPOOL,
976 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
977 obj, (const void *) mp, cookie);
978 rte_panic("MEMPOOL: bad header cookie (put)\n");
980 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
981 } else if (free == 1) {
982 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
983 rte_log_set_history(0);
984 RTE_LOG(CRIT, MEMPOOL,
985 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
986 obj, (const void *) mp, cookie);
987 rte_panic("MEMPOOL: bad header cookie (get)\n");
989 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
990 } else if (free == 2) {
991 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
992 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
993 rte_log_set_history(0);
994 RTE_LOG(CRIT, MEMPOOL,
995 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
996 obj, (const void *) mp, cookie);
997 rte_panic("MEMPOOL: bad header cookie (audit)\n");
1000 tlr = __mempool_get_trailer(obj);
1001 cookie = tlr->cookie;
1002 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
1003 rte_log_set_history(0);
1004 RTE_LOG(CRIT, MEMPOOL,
1005 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1006 obj, (const void *) mp, cookie);
1007 rte_panic("MEMPOOL: bad trailer cookie\n");
1012 RTE_SET_USED(obj_table_const);
1018 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1020 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
1021 void *obj, __rte_unused unsigned idx)
1023 __mempool_check_cookies(mp, &obj, 1, 2);
1027 mempool_audit_cookies(struct rte_mempool *mp)
1031 num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
1032 if (num != mp->size) {
1033 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
1034 "iterated only over %u elements\n",
1039 #define mempool_audit_cookies(mp) do {} while(0)
1042 #ifndef __INTEL_COMPILER
1043 #pragma GCC diagnostic error "-Wcast-qual"
1046 /* check cookies before and after objects */
1048 mempool_audit_cache(const struct rte_mempool *mp)
1050 /* check cache size consistency */
1053 if (mp->cache_size == 0)
1056 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1057 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
1058 RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
1060 rte_panic("MEMPOOL: invalid cache len\n");
1065 /* check the consistency of mempool (size, cookies, ...) */
1067 rte_mempool_audit(struct rte_mempool *mp)
1069 mempool_audit_cache(mp);
1070 mempool_audit_cookies(mp);
1072 /* For case where mempool DEBUG is not set, and cache size is 0 */
1076 /* dump the status of the mempool on the console */
1078 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1080 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1081 struct rte_mempool_debug_stats sum;
1084 struct rte_mempool_memhdr *memhdr;
1085 unsigned common_count;
1086 unsigned cache_count;
1089 RTE_ASSERT(f != NULL);
1090 RTE_ASSERT(mp != NULL);
1092 fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1093 fprintf(f, " flags=%x\n", mp->flags);
1094 fprintf(f, " ring=<%s>@%p\n", mp->ring->name, mp->ring);
1095 fprintf(f, " phys_addr=0x%" PRIx64 "\n", mp->mz->phys_addr);
1096 fprintf(f, " nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1097 fprintf(f, " size=%"PRIu32"\n", mp->size);
1098 fprintf(f, " populated_size=%"PRIu32"\n", mp->populated_size);
1099 fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
1100 fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
1101 fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
1102 fprintf(f, " total_obj_size=%"PRIu32"\n",
1103 mp->header_size + mp->elt_size + mp->trailer_size);
1105 fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
1107 STAILQ_FOREACH(memhdr, &mp->mem_list, next)
1108 mem_len += memhdr->len;
1110 fprintf(f, " avg bytes/object=%#Lf\n",
1111 (long double)mem_len / mp->size);
1114 cache_count = rte_mempool_dump_cache(f, mp);
1115 common_count = rte_ring_count(mp->ring);
1116 if ((cache_count + common_count) > mp->size)
1117 common_count = mp->size - cache_count;
1118 fprintf(f, " common_pool_count=%u\n", common_count);
1120 /* sum and dump statistics */
1121 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1122 memset(&sum, 0, sizeof(sum));
1123 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1124 sum.put_bulk += mp->stats[lcore_id].put_bulk;
1125 sum.put_objs += mp->stats[lcore_id].put_objs;
1126 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1127 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1128 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1129 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1131 fprintf(f, " stats:\n");
1132 fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
1133 fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
1134 fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1135 fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1136 fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1137 fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1139 fprintf(f, " no statistics available\n");
1142 rte_mempool_audit(mp);
1145 /* dump the status of all mempools on the console */
1147 rte_mempool_list_dump(FILE *f)
1149 struct rte_mempool *mp = NULL;
1150 struct rte_tailq_entry *te;
1151 struct rte_mempool_list *mempool_list;
1153 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1155 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1157 TAILQ_FOREACH(te, mempool_list, next) {
1158 mp = (struct rte_mempool *) te->data;
1159 rte_mempool_dump(f, mp);
1162 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1165 /* search a mempool from its name */
1166 struct rte_mempool *
1167 rte_mempool_lookup(const char *name)
1169 struct rte_mempool *mp = NULL;
1170 struct rte_tailq_entry *te;
1171 struct rte_mempool_list *mempool_list;
1173 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1175 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1177 TAILQ_FOREACH(te, mempool_list, next) {
1178 mp = (struct rte_mempool *) te->data;
1179 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1183 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1193 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1196 struct rte_tailq_entry *te = NULL;
1197 struct rte_mempool_list *mempool_list;
1199 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1201 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1203 TAILQ_FOREACH(te, mempool_list, next) {
1204 (*func)((struct rte_mempool *) te->data, arg);
1207 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);