blob: a94b66ae187fb759fc1195efa5bddd863f38c660 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#ifndef _included_clib_mem_h
39#define _included_clib_mem_h
40
41#include <stdarg.h>
Damjan Marion01914ce2017-09-14 19:04:50 +020042#include <unistd.h>
43#include <sys/mman.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070044
Dave Barachc3799992016-08-15 11:12:27 -040045#include <vppinfra/clib.h> /* uword, etc */
Damjan Marion01914ce2017-09-14 19:04:50 +020046#include <vppinfra/clib_error.h>
Dave Barach6a5adc32018-07-04 10:56:23 -040047
Ed Warnickecb9cada2015-12-08 15:45:58 -070048#include <vppinfra/os.h>
Dave Barachb7b92992018-10-17 10:38:51 -040049#include <vppinfra/string.h> /* memcpy, clib_memset */
Benoît Ganne9fb6d402019-04-15 15:28:21 +020050#include <vppinfra/sanitizer.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
Damjan Marionce8debf2016-02-06 19:16:21 +010052#define CLIB_MAX_MHEAPS 256
Damjan Marion6bfd0762020-09-11 22:16:53 +020053#define CLIB_MAX_NUMAS 16
54#define CLIB_MEM_VM_MAP_FAILED ((void *) ~0)
Damjan Marion561ae5d2020-09-24 13:53:46 +020055#define CLIB_MEM_ERROR (-1)
Dave Baracha690fdb2020-01-21 12:34:55 -050056
Damjan Marionb5095042020-09-11 22:13:46 +020057typedef enum
58{
59 CLIB_MEM_PAGE_SZ_UNKNOWN = 0,
60 CLIB_MEM_PAGE_SZ_DEFAULT = 1,
61 CLIB_MEM_PAGE_SZ_DEFAULT_HUGE = 2,
62 CLIB_MEM_PAGE_SZ_4K = 12,
63 CLIB_MEM_PAGE_SZ_16K = 14,
64 CLIB_MEM_PAGE_SZ_64K = 16,
65 CLIB_MEM_PAGE_SZ_1M = 20,
66 CLIB_MEM_PAGE_SZ_2M = 21,
67 CLIB_MEM_PAGE_SZ_16M = 24,
68 CLIB_MEM_PAGE_SZ_32M = 25,
69 CLIB_MEM_PAGE_SZ_512M = 29,
70 CLIB_MEM_PAGE_SZ_1G = 30,
71 CLIB_MEM_PAGE_SZ_16G = 34,
72} clib_mem_page_sz_t;
73
Damjan Marion6bfd0762020-09-11 22:16:53 +020074typedef struct _clib_mem_vm_map_hdr
75{
76 /* base address */
77 uword base_addr;
78
79 /* number of pages */
80 uword num_pages;
81
82 /* page size (log2) */
83 clib_mem_page_sz_t log2_page_sz;
84
Damjan Marion5ef25162020-09-17 13:29:33 +020085 /* file descriptor, -1 if memory is not shared */
86 int fd;
87
Damjan Marion6bfd0762020-09-11 22:16:53 +020088 /* allocation mame */
89#define CLIB_VM_MAP_HDR_NAME_MAX_LEN 64
90 char name[CLIB_VM_MAP_HDR_NAME_MAX_LEN];
91
92 /* linked list */
93 struct _clib_mem_vm_map_hdr *prev, *next;
94} clib_mem_vm_map_hdr_t;
95
Damjan Marionbfa75d62020-10-06 17:46:06 +020096#define foreach_clib_mem_heap_flag \
97 _(0, LOCKED, "locked") \
98 _(1, UNMAP_ON_DESTROY, "unmap-on-destroy")
99
100typedef enum
101{
102#define _(i, v, s) CLIB_MEM_HEAP_F_##v = (1 << i),
103 foreach_clib_mem_heap_flag
104#undef _
105} clib_mem_heap_flag_t;
106
107typedef struct
108{
109 /* base address */
110 void *base;
111
112 /* dlmalloc mspace */
113 void *mspace;
114
115 /* heap size */
116 uword size;
117
118 /* page size (log2) */
119 clib_mem_page_sz_t log2_page_sz:8;
120
121 /* flags */
122 clib_mem_heap_flag_t flags:8;
123
124 /* name - _MUST_ be last */
125 char name[0];
126} clib_mem_heap_t;
127
Damjan Marion57d1ec02020-09-16 21:15:44 +0200128typedef struct
129{
Damjan Marionc63e2a42020-09-16 21:36:00 +0200130 /* log2 system page size */
131 clib_mem_page_sz_t log2_page_sz;
132
133 /* log2 system default hugepage size */
134 clib_mem_page_sz_t log2_default_hugepage_sz;
135
136 /* bitmap of available numa nodes */
137 u32 numa_node_bitmap;
138
Damjan Marion57d1ec02020-09-16 21:15:44 +0200139 /* per CPU heaps */
140 void *per_cpu_mheaps[CLIB_MAX_MHEAPS];
141
142 /* per NUMA heaps */
143 void *per_numa_mheaps[CLIB_MAX_NUMAS];
Damjan Marion6bfd0762020-09-11 22:16:53 +0200144
145 /* memory maps */
146 clib_mem_vm_map_hdr_t *first_map, *last_map;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200147
148 /* last error */
149 clib_error_t *error;
Damjan Marion57d1ec02020-09-16 21:15:44 +0200150} clib_mem_main_t;
151
152extern clib_mem_main_t clib_mem_main;
153
Dave Baracha690fdb2020-01-21 12:34:55 -0500154/* Unspecified NUMA socket */
155#define VEC_NUMA_UNSPECIFIED (0xFF)
Damjan Marionce8debf2016-02-06 19:16:21 +0100156
Damjan Marionbfa75d62020-10-06 17:46:06 +0200157always_inline clib_mem_heap_t *
Dave Baracha690fdb2020-01-21 12:34:55 -0500158clib_mem_get_per_cpu_heap (void)
159{
160 int cpu = os_get_thread_index ();
Damjan Marion57d1ec02020-09-16 21:15:44 +0200161 return clib_mem_main.per_cpu_mheaps[cpu];
Dave Baracha690fdb2020-01-21 12:34:55 -0500162}
163
164always_inline void *
Damjan Marionbfa75d62020-10-06 17:46:06 +0200165clib_mem_set_per_cpu_heap (void *new_heap)
Dave Baracha690fdb2020-01-21 12:34:55 -0500166{
167 int cpu = os_get_thread_index ();
Damjan Marion57d1ec02020-09-16 21:15:44 +0200168 void *old = clib_mem_main.per_cpu_mheaps[cpu];
169 clib_mem_main.per_cpu_mheaps[cpu] = new_heap;
Dave Baracha690fdb2020-01-21 12:34:55 -0500170 return old;
171}
172
173always_inline void *
174clib_mem_get_per_numa_heap (u32 numa_id)
175{
Damjan Marion57d1ec02020-09-16 21:15:44 +0200176 ASSERT (numa_id < ARRAY_LEN (clib_mem_main.per_numa_mheaps));
177 return clib_mem_main.per_numa_mheaps[numa_id];
Dave Baracha690fdb2020-01-21 12:34:55 -0500178}
179
180always_inline void *
Damjan Marionbfa75d62020-10-06 17:46:06 +0200181clib_mem_set_per_numa_heap (void *new_heap)
Dave Baracha690fdb2020-01-21 12:34:55 -0500182{
183 int numa = os_get_numa_index ();
Damjan Marion57d1ec02020-09-16 21:15:44 +0200184 void *old = clib_mem_main.per_numa_mheaps[numa];
185 clib_mem_main.per_numa_mheaps[numa] = new_heap;
Dave Baracha690fdb2020-01-21 12:34:55 -0500186 return old;
187}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Nathan Skrzypczakd516ca42019-08-01 18:14:06 +0200189always_inline void
190clib_mem_set_thread_index (void)
191{
192 /*
193 * Find an unused slot in the per-cpu-mheaps array,
194 * and grab it for this thread. We need to be able to
195 * push/pop the thread heap without affecting other thread(s).
196 */
197 int i;
198 if (__os_thread_index != 0)
199 return;
Damjan Marion57d1ec02020-09-16 21:15:44 +0200200 for (i = 0; i < ARRAY_LEN (clib_mem_main.per_cpu_mheaps); i++)
201 if (clib_atomic_bool_cmp_and_swap (&clib_mem_main.per_cpu_mheaps[i],
202 0, clib_mem_main.per_cpu_mheaps[0]))
Nathan Skrzypczakd516ca42019-08-01 18:14:06 +0200203 {
204 os_set_thread_index (i);
205 break;
206 }
207 ASSERT (__os_thread_index > 0);
208}
209
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200210always_inline uword
211clib_mem_size_nocheck (void *p)
212{
Damjan Marion4537c302020-09-28 19:03:37 +0200213 size_t mspace_usable_size_with_delta (const void *p);
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200214 return mspace_usable_size_with_delta (p);
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200215}
216
Dave Barach241e5222016-10-13 10:53:26 -0400217/* Memory allocator which may call os_out_of_memory() if it fails */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218always_inline void *
Dave Barach241e5222016-10-13 10:53:26 -0400219clib_mem_alloc_aligned_at_offset (uword size, uword align, uword align_offset,
220 int os_out_of_memory_on_failure)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221{
Damjan Marion4537c302020-09-28 19:03:37 +0200222 void *mspace_get_aligned (void *msp, unsigned long n_user_data_bytes,
223 unsigned long align, unsigned long align_offset);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200224 clib_mem_heap_t *h = clib_mem_get_per_cpu_heap ();
225 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
227 if (align_offset > align)
228 {
229 if (align > 0)
230 align_offset %= align;
231 else
232 align_offset = align;
233 }
234
Damjan Marionbfa75d62020-10-06 17:46:06 +0200235 p = mspace_get_aligned (h->mspace, size, align, align_offset);
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200236
237 if (PREDICT_FALSE (0 == p))
Dave Barach6a5adc32018-07-04 10:56:23 -0400238 {
239 if (os_out_of_memory_on_failure)
240 os_out_of_memory ();
241 return 0;
242 }
243
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200244 CLIB_MEM_UNPOISON (p, size);
Dave Barach6a5adc32018-07-04 10:56:23 -0400245 return p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246}
247
Dave Barach241e5222016-10-13 10:53:26 -0400248/* Memory allocator which calls os_out_of_memory() when it fails */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249always_inline void *
250clib_mem_alloc (uword size)
Dave Barachc3799992016-08-15 11:12:27 -0400251{
252 return clib_mem_alloc_aligned_at_offset (size, /* align */ 1,
Dave Barach241e5222016-10-13 10:53:26 -0400253 /* align_offset */ 0,
254 /* os_out_of_memory */ 1);
Dave Barachc3799992016-08-15 11:12:27 -0400255}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
257always_inline void *
258clib_mem_alloc_aligned (uword size, uword align)
Dave Barachc3799992016-08-15 11:12:27 -0400259{
Dave Barach241e5222016-10-13 10:53:26 -0400260 return clib_mem_alloc_aligned_at_offset (size, align, /* align_offset */ 0,
261 /* os_out_of_memory */ 1);
Dave Barachc3799992016-08-15 11:12:27 -0400262}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263
Dave Barach241e5222016-10-13 10:53:26 -0400264/* Memory allocator which calls os_out_of_memory() when it fails */
265always_inline void *
266clib_mem_alloc_or_null (uword size)
267{
268 return clib_mem_alloc_aligned_at_offset (size, /* align */ 1,
269 /* align_offset */ 0,
270 /* os_out_of_memory */ 0);
271}
272
273always_inline void *
274clib_mem_alloc_aligned_or_null (uword size, uword align)
275{
276 return clib_mem_alloc_aligned_at_offset (size, align, /* align_offset */ 0,
277 /* os_out_of_memory */ 0);
278}
279
280
281
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282/* Memory allocator which panics when it fails.
283 Use macro so that clib_panic macro can expand __FUNCTION__ and __LINE__. */
284#define clib_mem_alloc_aligned_no_fail(size,align) \
285({ \
286 uword _clib_mem_alloc_size = (size); \
287 void * _clib_mem_alloc_p; \
288 _clib_mem_alloc_p = clib_mem_alloc_aligned (_clib_mem_alloc_size, (align)); \
289 if (! _clib_mem_alloc_p) \
290 clib_panic ("failed to allocate %d bytes", _clib_mem_alloc_size); \
291 _clib_mem_alloc_p; \
292})
293
294#define clib_mem_alloc_no_fail(size) clib_mem_alloc_aligned_no_fail(size,1)
295
296/* Alias to stack allocator for naming consistency. */
297#define clib_mem_alloc_stack(bytes) __builtin_alloca(bytes)
298
Dave Barachc3799992016-08-15 11:12:27 -0400299always_inline uword
300clib_mem_is_heap_object (void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301{
Damjan Marion4537c302020-09-28 19:03:37 +0200302 int mspace_is_heap_object (void *msp, void *p);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200303 clib_mem_heap_t *h = clib_mem_get_per_cpu_heap ();
304 return mspace_is_heap_object (h->mspace, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305}
306
Dave Barachc3799992016-08-15 11:12:27 -0400307always_inline void
308clib_mem_free (void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309{
Damjan Marion4537c302020-09-28 19:03:37 +0200310 void mspace_put (void *msp, void *p_arg);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200311 clib_mem_heap_t *h = clib_mem_get_per_cpu_heap ();
312
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313 /* Make sure object is in the correct heap. */
314 ASSERT (clib_mem_is_heap_object (p));
315
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200316 CLIB_MEM_POISON (p, clib_mem_size_nocheck (p));
317
Damjan Marionbfa75d62020-10-06 17:46:06 +0200318 mspace_put (h->mspace, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319}
320
Dave Barachc3799992016-08-15 11:12:27 -0400321always_inline void *
322clib_mem_realloc (void *p, uword new_size, uword old_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323{
324 /* By default use alloc, copy and free to emulate realloc. */
Dave Barachc3799992016-08-15 11:12:27 -0400325 void *q = clib_mem_alloc (new_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326 if (q)
327 {
328 uword copy_size;
329 if (old_size < new_size)
330 copy_size = old_size;
331 else
332 copy_size = new_size;
Dave Barach178cf492018-11-13 16:34:13 -0500333 clib_memcpy_fast (q, p, copy_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 clib_mem_free (p);
335 }
336 return q;
337}
338
Dave Barachc3799992016-08-15 11:12:27 -0400339always_inline uword
340clib_mem_size (void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341{
Dave Barach6a5adc32018-07-04 10:56:23 -0400342 ASSERT (clib_mem_is_heap_object (p));
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200343 return clib_mem_size_nocheck (p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344}
345
Benoît Ganne78af0a82019-04-29 17:27:24 +0200346always_inline void
347clib_mem_free_s (void *p)
348{
349 uword size = clib_mem_size (p);
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200350 CLIB_MEM_UNPOISON (p, size);
Benoît Ganne78af0a82019-04-29 17:27:24 +0200351 memset_s_inline (p, size, 0, size);
352 clib_mem_free (p);
353}
354
Damjan Marionbfa75d62020-10-06 17:46:06 +0200355always_inline clib_mem_heap_t *
Dave Barachc3799992016-08-15 11:12:27 -0400356clib_mem_get_heap (void)
357{
358 return clib_mem_get_per_cpu_heap ();
359}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360
Damjan Marionbfa75d62020-10-06 17:46:06 +0200361always_inline clib_mem_heap_t *
362clib_mem_set_heap (clib_mem_heap_t * heap)
Dave Barachc3799992016-08-15 11:12:27 -0400363{
364 return clib_mem_set_per_cpu_heap (heap);
365}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366
Damjan Marionbfa75d62020-10-06 17:46:06 +0200367void clib_mem_destroy_heap (clib_mem_heap_t * heap);
368clib_mem_heap_t *clib_mem_create_heap (void *base, uword size, int is_locked,
369 char *fmt, ...);
Damjan Marion4537c302020-09-28 19:03:37 +0200370
Damjan Marionc63e2a42020-09-16 21:36:00 +0200371void clib_mem_main_init ();
Damjan Marionbfa75d62020-10-06 17:46:06 +0200372void *clib_mem_init (void *base, uword size);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200373void *clib_mem_init_with_page_size (uword memory_size,
374 clib_mem_page_sz_t log2_page_sz);
Dave Barach6a5adc32018-07-04 10:56:23 -0400375void *clib_mem_init_thread_safe (void *memory, uword memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376
377void clib_mem_exit (void);
378
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379void clib_mem_trace (int enable);
380
Dave Barachd67a4282019-06-15 12:46:13 -0400381int clib_mem_is_traced (void);
382
Dave Barachc3799992016-08-15 11:12:27 -0400383typedef struct
384{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385 /* Total number of objects allocated. */
386 uword object_count;
387
388 /* Total allocated bytes. Bytes used and free.
389 used + free = total */
390 uword bytes_total, bytes_used, bytes_free;
391
392 /* Number of bytes used by mheap data structure overhead
393 (e.g. free lists, mheap header). */
394 uword bytes_overhead;
395
396 /* Amount of free space returned to operating system. */
397 uword bytes_free_reclaimed;
Dave Barachc3799992016-08-15 11:12:27 -0400398
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399 /* For malloc which puts small objects in sbrk region and
400 large objects in mmap'ed regions. */
401 uword bytes_used_sbrk;
402 uword bytes_used_mmap;
403
404 /* Max. number of bytes in this heap. */
405 uword bytes_max;
406} clib_mem_usage_t;
407
Damjan Marionbfa75d62020-10-06 17:46:06 +0200408void clib_mem_get_heap_usage (clib_mem_heap_t * heap,
409 clib_mem_usage_t * usage);
Damjan Marion4537c302020-09-28 19:03:37 +0200410
Damjan Marionbfa75d62020-10-06 17:46:06 +0200411void *clib_mem_get_heap_base (clib_mem_heap_t * heap);
412uword clib_mem_get_heap_size (clib_mem_heap_t * heap);
413uword clib_mem_get_heap_free_space (clib_mem_heap_t * heap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
Dave Barachc3799992016-08-15 11:12:27 -0400415u8 *format_clib_mem_usage (u8 * s, va_list * args);
Damjan Marion4537c302020-09-28 19:03:37 +0200416u8 *format_clib_mem_heap (u8 * s, va_list * va);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200417u8 *format_clib_mem_page_stats (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
Damjan Marion01914ce2017-09-14 19:04:50 +0200419/* Allocate virtual address space. */
420always_inline void *
421clib_mem_vm_alloc (uword size)
422{
423 void *mmap_addr;
424 uword flags = MAP_PRIVATE;
425
426#ifdef MAP_ANONYMOUS
427 flags |= MAP_ANONYMOUS;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428#endif
429
Damjan Marion01914ce2017-09-14 19:04:50 +0200430 mmap_addr = mmap (0, size, PROT_READ | PROT_WRITE, flags, -1, 0);
431 if (mmap_addr == (void *) -1)
432 mmap_addr = 0;
Benoît Ganne1557d9a2020-02-07 11:58:16 +0100433 else
434 CLIB_MEM_UNPOISON (mmap_addr, size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
Damjan Marion01914ce2017-09-14 19:04:50 +0200436 return mmap_addr;
437}
438
439always_inline void
440clib_mem_vm_free (void *addr, uword size)
441{
442 munmap (addr, size);
443}
444
Damjan Marion6bfd0762020-09-11 22:16:53 +0200445void *clib_mem_vm_map_internal (void *base, clib_mem_page_sz_t log2_page_sz,
446 uword size, int fd, uword offset, char *name);
Damjan Marion01914ce2017-09-14 19:04:50 +0200447
Damjan Marion6bfd0762020-09-11 22:16:53 +0200448void *clib_mem_vm_map (void *start, uword size,
449 clib_mem_page_sz_t log2_page_size, char *fmt, ...);
450void *clib_mem_vm_map_stack (uword size, clib_mem_page_sz_t log2_page_size,
451 char *fmt, ...);
452void *clib_mem_vm_map_shared (void *start, uword size, int fd, uword offset,
453 char *fmt, ...);
454int clib_mem_vm_unmap (void *base);
455clib_mem_vm_map_hdr_t *clib_mem_vm_get_next_map_hdr (clib_mem_vm_map_hdr_t *
456 hdr);
Damjan Marion01914ce2017-09-14 19:04:50 +0200457
Damjan Marion6bfd0762020-09-11 22:16:53 +0200458static_always_inline clib_mem_page_sz_t
459clib_mem_get_log2_page_size (void)
460{
461 return clib_mem_main.log2_page_sz;
462}
463
464static_always_inline uword
465clib_mem_get_page_size (void)
466{
467 return 1ULL << clib_mem_main.log2_page_sz;
468}
469
470static_always_inline clib_mem_page_sz_t
471clib_mem_get_log2_default_hugepage_size ()
472{
473 return clib_mem_main.log2_default_hugepage_sz;
474}
475
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200476int clib_mem_vm_create_fd (clib_mem_page_sz_t log2_page_size, char *fmt, ...);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200477uword clib_mem_get_fd_page_size (int fd);
Damjan Marion9787f5f2018-10-24 12:56:32 +0200478uword clib_mem_get_default_hugepage_size (void);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200479clib_mem_page_sz_t clib_mem_get_fd_log2_page_size (int fd);
Damjan Marionb5095042020-09-11 22:13:46 +0200480uword clib_mem_vm_reserve (uword start, uword size,
481 clib_mem_page_sz_t log2_page_sz);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200482u64 *clib_mem_vm_get_paddr (void *mem, clib_mem_page_sz_t log2_page_size,
483 int n_pages);
Dave Barach2b793412020-08-28 10:39:00 -0400484void clib_mem_destroy (void);
Damjan Marion561ae5d2020-09-24 13:53:46 +0200485int clib_mem_set_numa_affinity (u8 numa_node, int force);
486int clib_mem_set_default_numa_affinity ();
Damjan Marionb5095042020-09-11 22:13:46 +0200487void clib_mem_vm_randomize_va (uword * requested_va,
488 clib_mem_page_sz_t log2_page_size);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200489void mheap_trace (clib_mem_heap_t * v, int enable);
Dave Barach8fdde3c2019-05-17 10:46:40 -0400490uword clib_mem_trace_enable_disable (uword enable);
491void clib_mem_trace (int enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492
Damjan Marion6bfd0762020-09-11 22:16:53 +0200493always_inline uword
494clib_mem_round_to_page_size (uword size, clib_mem_page_sz_t log2_page_size)
495{
496 ASSERT (log2_page_size != CLIB_MEM_PAGE_SZ_UNKNOWN);
497
498 if (log2_page_size == CLIB_MEM_PAGE_SZ_DEFAULT)
499 log2_page_size = clib_mem_get_log2_page_size ();
500 else if (log2_page_size == CLIB_MEM_PAGE_SZ_DEFAULT_HUGE)
501 log2_page_size = clib_mem_get_log2_default_hugepage_size ();
502
503 return round_pow2 (size, 1ULL << log2_page_size);
504}
505
506typedef struct
507{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200508 clib_mem_page_sz_t log2_page_sz;
509 uword total;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200510 uword mapped;
511 uword not_mapped;
512 uword per_numa[CLIB_MAX_NUMAS];
513 uword unknown;
514} clib_mem_page_stats_t;
515
516void clib_mem_get_page_stats (void *start, clib_mem_page_sz_t log2_page_size,
517 uword n_pages, clib_mem_page_stats_t * stats);
518
519static_always_inline int
520vlib_mem_get_next_numa_node (int numa)
521{
522 clib_mem_main_t *mm = &clib_mem_main;
523 u32 bitmap = mm->numa_node_bitmap;
524
525 if (numa >= 0)
526 bitmap &= ~pow2_mask (numa + 1);
527 if (bitmap == 0)
528 return -1;
529
530 return count_trailing_zeros (bitmap);
531}
532
533static_always_inline clib_mem_page_sz_t
534clib_mem_log2_page_size_validate (clib_mem_page_sz_t log2_page_size)
535{
536 if (log2_page_size == CLIB_MEM_PAGE_SZ_DEFAULT)
537 return clib_mem_get_log2_page_size ();
538 if (log2_page_size == CLIB_MEM_PAGE_SZ_DEFAULT_HUGE)
539 return clib_mem_get_log2_default_hugepage_size ();
540 return log2_page_size;
541}
542
543static_always_inline uword
544clib_mem_page_bytes (clib_mem_page_sz_t log2_page_size)
545{
546 return 1 << clib_mem_log2_page_size_validate (log2_page_size);
547}
548
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200549static_always_inline clib_error_t *
550clib_mem_get_last_error (void)
551{
552 return clib_mem_main.error;
553}
554
Damjan Marion6bfd0762020-09-11 22:16:53 +0200555
Dave Barachc3799992016-08-15 11:12:27 -0400556#include <vppinfra/error.h> /* clib_panic */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557
558#endif /* _included_clib_mem_h */
Dave Barachc3799992016-08-15 11:12:27 -0400559
560/*
561 * fd.io coding-style-patch-verification: ON
562 *
563 * Local Variables:
564 * eval: (c-set-style "gnu")
565 * End:
566 */