18 #include <sys/types.h> 22 #include <linux/mempolicy.h> 23 #include <linux/memfd.h> 33 #if __SIZEOF_POINTER__ >= 8 34 #define DEFAULT_RESERVED_MB 16384 36 #define DEFAULT_RESERVED_MB 256 48 return round_pow2 (size, 1ULL << log2_page_sz) >> log2_page_sz;
57 if (
getcpu (&cpu, numa_node) != 0)
77 if (pt == 0 || pt[0] == 0)
100 u32 n_blocks,
u32 block_align,
u32 numa_node)
106 u32 alloc_chunk_index;
112 u32 i, start = 0, prev = ~0;
138 off = (block_align - (c->
start & (block_align - 1))) & (block_align - 1);
140 if (c->
used || n_blocks + off > c->
size)
144 alloc_chunk_index = c->
next;
151 u32 offset_chunk_index;
155 offset_chunk_index = alloc_chunk_index;
156 alloc_chunk_index = c - pp->
chunks;
162 c->
prev = offset_chunk_index;
164 co->
next = alloc_chunk_index;
168 if (c->
size > n_blocks)
170 u32 tail_chunk_index;
174 tail_chunk_index = ct - pp->
chunks;
178 ct->
prev = alloc_chunk_index;
182 c->
next = tail_chunk_index;
186 else if (c->
next != ~0)
201 uword seek, va, pa, p;
208 p = (
uword) first *elts_per_page;
220 fd = open ((
char *)
"/proc/self/pagemap", O_RDONLY);
221 while (p < (
uword) elts_per_page * count)
226 if (fd != -1 && lseek (fd, seek, SEEK_SET) == seek &&
227 read (fd, &pa,
sizeof (pa)) == (
sizeof (pa)) &&
242 u32 numa_node,
u32 n_pages)
245 int status, rv,
i, mmap_flags;
246 void *va = MAP_FAILED;
248 long unsigned int mask[16] = { 0 };
249 long unsigned int old_mask[16] = { 0 };
269 rv =
get_mempolicy (&old_mpol, old_mask,
sizeof (old_mask) * 8 + 1, 0, 0);
271 if (rv == -1 && numa_node != 0)
277 mask[0] = 1 << numa_node;
279 if (rv == -1 && numa_node != 0)
282 "numa node %u", numa_node);
286 mmap_flags = MAP_FIXED;
290 mmap_flags |= MAP_SHARED;
294 if ((ftruncate (a->
fd, size)) == -1)
300 mmap_flags |= MAP_HUGETLB;
302 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
307 if (mmap (va, size, PROT_READ | PROT_WRITE, mmap_flags, a->
fd, 0) ==
311 "fd %d numa %d flags 0x%x", n_pages,
312 va, a->
fd, numa_node, mmap_flags);
318 mlock (va, size) != 0)
326 rv =
set_mempolicy (old_mpol, old_mask,
sizeof (old_mask) * 8 + 1);
327 if (rv == -1 && numa_node != 0)
336 if ((rv == 0 && status != numa_node) || (rv != 0 && numa_node != 0))
338 pm->
error = rv == -1 ?
340 "%u status %d", numa_node, status) :
342 "%u status %d", numa_node, status);
347 for (i = 0; i < n_pages; i++)
370 return pp - (n_pages - 1);
373 if (va != MAP_FAILED)
377 mmap (va, size, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
395 if (log2_page_sz == 0)
401 1 << (log2_page_sz - 10));
424 memset (a, 0,
sizeof (*a));
437 u32 n_blocks, block_align, *page_index;
455 a->
name =
format (0,
"default-numa-%u%c", numa_node, 0);
515 if (ci1 == ~0 || ci2 == ~0)
538 u32 chunk_index, page_index;
562 if (next->
next != ~0)
564 memset (next, 0,
sizeof (*next));
577 memset (c, 0,
sizeof (*c));
587 int verbose = va_arg (*va,
int);
593 s =
format (s,
"free %u chunks %u free-chunks %d ",
601 s =
format (s,
"\n%U%12s%12s%8s%8s%8s%8s",
603 "chunk offset",
"size",
"used",
"index",
"prev",
"next");
606 s =
format (s,
"\n%U%12u%12u%8s%8d%8d%8d",
610 c->
used ?
"yes" :
"no",
624 int verbose = va_arg (*va,
int);
630 s =
format (s,
"used-pages %u reserved-pages %u default-page-size %U " 649 s =
format (s,
"\n%Uarena '%s' pages %u subpage-size %U numa-node %u",
654 s =
format (s,
" shared fd %d", a->
fd);
674 s =
format (s,
"%16s %13s %8s",
"virtual-addr",
"physical-addr",
"size");
677 uword *lookup_val, pa, va;
682 pa = va - *lookup_val;
__clib_export int clib_mem_vm_create_fd(clib_mem_page_sz_t log2_page_size, char *fmt,...)
#define vec_foreach_index(var, v)
Iterate over vector indices.
#define hash_set(h, key, value)
clib_error_t * clib_sysfs_prealloc_hugepages(int numa_node, int log2_page_size, int nr)
__clib_export u8 * format_pmalloc_map(u8 *s, va_list *va)
#define hash_unset(h, key)
#define pool_foreach(VAR, POOL)
Iterate through pool.
static u8 * format_pmalloc_page(u8 *s, va_list *va)
#define CLIB_PMALLOC_NUMA_LOCAL
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void * clib_pmalloc_alloc_inline(clib_pmalloc_main_t *pm, clib_pmalloc_arena_t *a, uword size, uword align, u32 numa_node)
static void pmalloc_update_lookup_table(clib_pmalloc_main_t *pm, u32 first, u32 count)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
static u32 clib_pmalloc_get_page_index(clib_pmalloc_main_t *pm, void *va)
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
__clib_export u8 * format_pmalloc(u8 *s, va_list *va)
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
void * clib_pmalloc_alloc_aligned(clib_pmalloc_main_t *pm, uword size, uword align)
static int pmalloc_chunks_mergeable(clib_pmalloc_arena_t *a, clib_pmalloc_page_t *pp, u32 ci1, u32 ci2)
static uword min_log2(uword x)
clib_pmalloc_chunk_t * chunks
clib_pmalloc_arena_t * arenas
static int pmalloc_validate_numa_node(u32 *numa_node)
static clib_pmalloc_page_t * pmalloc_map_pages(clib_pmalloc_main_t *pm, clib_pmalloc_arena_t *a, u32 numa_node, u32 n_pages)
static clib_pmalloc_chunk_t * get_chunk(clib_pmalloc_page_t *pp, u32 index)
static long set_mempolicy(int mode, const unsigned long *nodemask, unsigned long maxnode)
static void * alloc_chunk_from_page(clib_pmalloc_main_t *pm, clib_pmalloc_page_t *pp, u32 n_blocks, u32 block_align, u32 numa_node)
static uword pow2_mask(uword x)
description fragment has unexpected format
#define DEFAULT_RESERVED_MB
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
static uword pmalloc_size2pages(uword size, u32 log2_page_sz)
#define clib_error_create(args...)
static heap_elt_t * first(heap_header_t *h)
#define PMALLOC_LOG2_BLOCK_SZ
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
void * clib_pmalloc_alloc_from_arena(clib_pmalloc_main_t *pm, void *arena_va, uword size, uword align)
#define CLIB_PMALLOC_F_NO_PAGEMAP
static int getcpu(unsigned *cpu, unsigned *node)
__clib_export int clib_pmalloc_init(clib_pmalloc_main_t *pm, uword base_addr, uword size)
#define clib_error_return_unix(e, args...)
#define pool_put(P, E)
Free an object E in pool P.
u32 * default_arena_for_numa_node
__clib_export void clib_pmalloc_free(clib_pmalloc_main_t *pm, void *va)
static int get_mempolicy(int *mode, unsigned long *nodemask, unsigned long maxnode, void *addr, unsigned long flags)
clib_pmalloc_page_t * pages
sll srl srl sll sra u16x4 i
#define vec_free(V)
Free vector's memory (no header).
static_always_inline clib_mem_page_sz_t clib_mem_get_log2_page_size(void)
clib_mem_page_sz_t def_log2_page_sz
__clib_export u64 * clib_mem_vm_get_paddr(void *mem, clib_mem_page_sz_t log2_page_size, int n_pages)
__clib_export uword clib_mem_get_default_hugepage_size(void)
static uword round_pow2(uword x, uword pow2)
uword clib_mem_vm_reserve(uword start, uword size, clib_mem_page_sz_t log2_page_sz)
#define uword_to_pointer(u, type)
uword * chunk_index_by_va
__clib_export void * clib_pmalloc_create_shared_arena(clib_pmalloc_main_t *pm, char *name, uword size, u32 log2_page_sz, u32 numa_node)
static uword pointer_to_uword(const void *p)
static uword is_pow2(uword x)
static clib_pmalloc_arena_t * clib_pmalloc_get_arena(clib_pmalloc_main_t *pm, void *va)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define clib_error_free(e)
#define vec_foreach(var, vec)
Vector iterator.
__clib_export void * clib_pmalloc_alloc_aligned_on_numa(clib_pmalloc_main_t *pm, uword size, uword align, u32 numa_node)
static long move_pages(int pid, unsigned long count, void **pages, const int *nodes, int *status, int flags)
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
#define CLIB_CACHE_LINE_BYTES
__clib_export u8 * format_clib_error(u8 *s, va_list *va)
#define CLIB_PMALLOC_ARENA_F_SHARED_MEM
static uword pool_elts(void *v)
Number of active elements in a pool.