Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 42 | #include <unistd.h> |
| 43 | #include <sys/mman.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 45 | #include <vppinfra/clib.h> /* uword, etc */ |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 46 | #include <vppinfra/clib_error.h> |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 47 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | #include <vppinfra/os.h> |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 49 | #include <vppinfra/string.h> /* memcpy, clib_memset */ |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 50 | #ifdef CLIB_SANITIZE_ADDR |
| 51 | #include <sanitizer/asan_interface.h> |
| 52 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
Damjan Marion | ce8debf | 2016-02-06 19:16:21 +0100 | [diff] [blame] | 54 | #define CLIB_MAX_MHEAPS 256 |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 55 | #define CLIB_MAX_NUMAS 16 |
| 56 | #define CLIB_MEM_VM_MAP_FAILED ((void *) ~0) |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 57 | #define CLIB_MEM_ERROR (-1) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 58 | #define CLIB_MEM_LOG2_MIN_ALIGN (3) |
| 59 | #define CLIB_MEM_MIN_ALIGN (1 << CLIB_MEM_LOG2_MIN_ALIGN) |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 60 | |
Damjan Marion | b509504 | 2020-09-11 22:13:46 +0200 | [diff] [blame] | 61 | typedef enum |
| 62 | { |
| 63 | CLIB_MEM_PAGE_SZ_UNKNOWN = 0, |
| 64 | CLIB_MEM_PAGE_SZ_DEFAULT = 1, |
| 65 | CLIB_MEM_PAGE_SZ_DEFAULT_HUGE = 2, |
| 66 | CLIB_MEM_PAGE_SZ_4K = 12, |
| 67 | CLIB_MEM_PAGE_SZ_16K = 14, |
| 68 | CLIB_MEM_PAGE_SZ_64K = 16, |
| 69 | CLIB_MEM_PAGE_SZ_1M = 20, |
| 70 | CLIB_MEM_PAGE_SZ_2M = 21, |
| 71 | CLIB_MEM_PAGE_SZ_16M = 24, |
| 72 | CLIB_MEM_PAGE_SZ_32M = 25, |
| 73 | CLIB_MEM_PAGE_SZ_512M = 29, |
| 74 | CLIB_MEM_PAGE_SZ_1G = 30, |
| 75 | CLIB_MEM_PAGE_SZ_16G = 34, |
| 76 | } clib_mem_page_sz_t; |
| 77 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 78 | typedef struct _clib_mem_vm_map_hdr |
| 79 | { |
| 80 | /* base address */ |
| 81 | uword base_addr; |
| 82 | |
| 83 | /* number of pages */ |
| 84 | uword num_pages; |
| 85 | |
| 86 | /* page size (log2) */ |
| 87 | clib_mem_page_sz_t log2_page_sz; |
| 88 | |
Damjan Marion | 5ef2516 | 2020-09-17 13:29:33 +0200 | [diff] [blame] | 89 | /* file descriptor, -1 if memory is not shared */ |
| 90 | int fd; |
| 91 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 92 | /* allocation mame */ |
| 93 | #define CLIB_VM_MAP_HDR_NAME_MAX_LEN 64 |
| 94 | char name[CLIB_VM_MAP_HDR_NAME_MAX_LEN]; |
| 95 | |
| 96 | /* linked list */ |
| 97 | struct _clib_mem_vm_map_hdr *prev, *next; |
| 98 | } clib_mem_vm_map_hdr_t; |
| 99 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 100 | #define foreach_clib_mem_heap_flag \ |
| 101 | _ (0, LOCKED, "locked") \ |
| 102 | _ (1, UNMAP_ON_DESTROY, "unmap-on-destroy") \ |
| 103 | _ (2, TRACED, "traced") |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 104 | |
| 105 | typedef enum |
| 106 | { |
| 107 | #define _(i, v, s) CLIB_MEM_HEAP_F_##v = (1 << i), |
| 108 | foreach_clib_mem_heap_flag |
| 109 | #undef _ |
| 110 | } clib_mem_heap_flag_t; |
| 111 | |
| 112 | typedef struct |
| 113 | { |
| 114 | /* base address */ |
| 115 | void *base; |
| 116 | |
| 117 | /* dlmalloc mspace */ |
| 118 | void *mspace; |
| 119 | |
| 120 | /* heap size */ |
| 121 | uword size; |
| 122 | |
| 123 | /* page size (log2) */ |
| 124 | clib_mem_page_sz_t log2_page_sz:8; |
| 125 | |
| 126 | /* flags */ |
| 127 | clib_mem_heap_flag_t flags:8; |
| 128 | |
| 129 | /* name - _MUST_ be last */ |
| 130 | char name[0]; |
| 131 | } clib_mem_heap_t; |
| 132 | |
Damjan Marion | 57d1ec0 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 133 | typedef struct |
| 134 | { |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 135 | /* log2 system page size */ |
| 136 | clib_mem_page_sz_t log2_page_sz; |
| 137 | |
Damjan Marion | ff011b2 | 2021-09-21 11:38:04 +0200 | [diff] [blame] | 138 | /* log2 default hugepage size */ |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 139 | clib_mem_page_sz_t log2_default_hugepage_sz; |
| 140 | |
Damjan Marion | ff011b2 | 2021-09-21 11:38:04 +0200 | [diff] [blame] | 141 | /* log2 system default hugepage size */ |
| 142 | clib_mem_page_sz_t log2_sys_default_hugepage_sz; |
| 143 | |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 144 | /* bitmap of available numa nodes */ |
| 145 | u32 numa_node_bitmap; |
| 146 | |
Damjan Marion | 57d1ec0 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 147 | /* per CPU heaps */ |
| 148 | void *per_cpu_mheaps[CLIB_MAX_MHEAPS]; |
| 149 | |
| 150 | /* per NUMA heaps */ |
| 151 | void *per_numa_mheaps[CLIB_MAX_NUMAS]; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 152 | |
| 153 | /* memory maps */ |
| 154 | clib_mem_vm_map_hdr_t *first_map, *last_map; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 155 | |
Damjan Marion | 70ae0a9 | 2020-10-26 10:39:30 +0100 | [diff] [blame] | 156 | /* map lock */ |
| 157 | u8 map_lock; |
| 158 | |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 159 | /* last error */ |
| 160 | clib_error_t *error; |
Damjan Marion | 57d1ec0 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 161 | } clib_mem_main_t; |
| 162 | |
| 163 | extern clib_mem_main_t clib_mem_main; |
| 164 | |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 165 | /* Unspecified NUMA socket */ |
| 166 | #define VEC_NUMA_UNSPECIFIED (0xFF) |
Damjan Marion | ce8debf | 2016-02-06 19:16:21 +0100 | [diff] [blame] | 167 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 168 | static_always_inline void |
| 169 | clib_mem_poison (const void volatile *p, uword s) |
| 170 | { |
| 171 | #ifdef CLIB_SANITIZE_ADDR |
| 172 | ASAN_POISON_MEMORY_REGION (p, s); |
| 173 | #endif |
| 174 | } |
| 175 | |
| 176 | static_always_inline void |
| 177 | clib_mem_unpoison (const void volatile *p, uword s) |
| 178 | { |
| 179 | #ifdef CLIB_SANITIZE_ADDR |
| 180 | ASAN_UNPOISON_MEMORY_REGION (p, s); |
| 181 | #endif |
| 182 | } |
| 183 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 184 | always_inline clib_mem_heap_t * |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 185 | clib_mem_get_per_cpu_heap (void) |
| 186 | { |
| 187 | int cpu = os_get_thread_index (); |
Damjan Marion | 57d1ec0 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 188 | return clib_mem_main.per_cpu_mheaps[cpu]; |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | always_inline void * |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 192 | clib_mem_set_per_cpu_heap (void *new_heap) |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 193 | { |
| 194 | int cpu = os_get_thread_index (); |
Damjan Marion | 57d1ec0 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 195 | void *old = clib_mem_main.per_cpu_mheaps[cpu]; |
| 196 | clib_mem_main.per_cpu_mheaps[cpu] = new_heap; |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 197 | return old; |
| 198 | } |
| 199 | |
| 200 | always_inline void * |
| 201 | clib_mem_get_per_numa_heap (u32 numa_id) |
| 202 | { |
Damjan Marion | 57d1ec0 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 203 | ASSERT (numa_id < ARRAY_LEN (clib_mem_main.per_numa_mheaps)); |
| 204 | return clib_mem_main.per_numa_mheaps[numa_id]; |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | always_inline void * |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 208 | clib_mem_set_per_numa_heap (void *new_heap) |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 209 | { |
| 210 | int numa = os_get_numa_index (); |
Damjan Marion | 57d1ec0 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 211 | void *old = clib_mem_main.per_numa_mheaps[numa]; |
| 212 | clib_mem_main.per_numa_mheaps[numa] = new_heap; |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 213 | return old; |
| 214 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | |
Nathan Skrzypczak | d516ca4 | 2019-08-01 18:14:06 +0200 | [diff] [blame] | 216 | always_inline void |
| 217 | clib_mem_set_thread_index (void) |
| 218 | { |
| 219 | /* |
| 220 | * Find an unused slot in the per-cpu-mheaps array, |
| 221 | * and grab it for this thread. We need to be able to |
| 222 | * push/pop the thread heap without affecting other thread(s). |
| 223 | */ |
| 224 | int i; |
| 225 | if (__os_thread_index != 0) |
| 226 | return; |
Damjan Marion | 57d1ec0 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 227 | for (i = 0; i < ARRAY_LEN (clib_mem_main.per_cpu_mheaps); i++) |
| 228 | if (clib_atomic_bool_cmp_and_swap (&clib_mem_main.per_cpu_mheaps[i], |
| 229 | 0, clib_mem_main.per_cpu_mheaps[0])) |
Nathan Skrzypczak | d516ca4 | 2019-08-01 18:14:06 +0200 | [diff] [blame] | 230 | { |
| 231 | os_set_thread_index (i); |
| 232 | break; |
| 233 | } |
| 234 | ASSERT (__os_thread_index > 0); |
| 235 | } |
| 236 | |
Dave Barach | 241e522 | 2016-10-13 10:53:26 -0400 | [diff] [blame] | 237 | /* Memory allocator which calls os_out_of_memory() when it fails */ |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 238 | void *clib_mem_alloc (uword size); |
| 239 | void *clib_mem_alloc_aligned (uword size, uword align); |
| 240 | void *clib_mem_alloc_or_null (uword size); |
| 241 | void *clib_mem_alloc_aligned_or_null (uword size, uword align); |
| 242 | void *clib_mem_realloc (void *p, uword new_size); |
| 243 | void *clib_mem_realloc_aligned (void *p, uword new_size, uword align); |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 244 | uword clib_mem_is_heap_object (void *p); |
| 245 | void clib_mem_free (void *p); |
| 246 | |
| 247 | void *clib_mem_heap_alloc (void *heap, uword size); |
| 248 | void *clib_mem_heap_alloc_aligned (void *heap, uword size, uword align); |
| 249 | void *clib_mem_heap_alloc_or_null (void *heap, uword size); |
| 250 | void *clib_mem_heap_alloc_aligned_or_null (void *heap, uword size, |
| 251 | uword align); |
| 252 | void *clib_mem_heap_realloc (void *heap, void *p, uword new_size); |
| 253 | void *clib_mem_heap_realloc_aligned (void *heap, void *p, uword new_size, |
| 254 | uword align); |
| 255 | uword clib_mem_heap_is_heap_object (void *heap, void *p); |
| 256 | void clib_mem_heap_free (void *heap, void *p); |
| 257 | |
| 258 | uword clib_mem_size (void *p); |
| 259 | void clib_mem_free_s (void *p); |
Dave Barach | 241e522 | 2016-10-13 10:53:26 -0400 | [diff] [blame] | 260 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | /* Memory allocator which panics when it fails. |
| 262 | Use macro so that clib_panic macro can expand __FUNCTION__ and __LINE__. */ |
| 263 | #define clib_mem_alloc_aligned_no_fail(size,align) \ |
| 264 | ({ \ |
| 265 | uword _clib_mem_alloc_size = (size); \ |
| 266 | void * _clib_mem_alloc_p; \ |
| 267 | _clib_mem_alloc_p = clib_mem_alloc_aligned (_clib_mem_alloc_size, (align)); \ |
| 268 | if (! _clib_mem_alloc_p) \ |
| 269 | clib_panic ("failed to allocate %d bytes", _clib_mem_alloc_size); \ |
| 270 | _clib_mem_alloc_p; \ |
| 271 | }) |
| 272 | |
| 273 | #define clib_mem_alloc_no_fail(size) clib_mem_alloc_aligned_no_fail(size,1) |
| 274 | |
| 275 | /* Alias to stack allocator for naming consistency. */ |
| 276 | #define clib_mem_alloc_stack(bytes) __builtin_alloca(bytes) |
| 277 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 278 | always_inline clib_mem_heap_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 279 | clib_mem_get_heap (void) |
| 280 | { |
| 281 | return clib_mem_get_per_cpu_heap (); |
| 282 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 284 | always_inline clib_mem_heap_t * |
| 285 | clib_mem_set_heap (clib_mem_heap_t * heap) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 286 | { |
| 287 | return clib_mem_set_per_cpu_heap (heap); |
| 288 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 290 | void clib_mem_destroy_heap (clib_mem_heap_t * heap); |
| 291 | clib_mem_heap_t *clib_mem_create_heap (void *base, uword size, int is_locked, |
| 292 | char *fmt, ...); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 293 | |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 294 | void clib_mem_main_init (); |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 295 | void *clib_mem_init (void *base, uword size); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 296 | void *clib_mem_init_with_page_size (uword memory_size, |
| 297 | clib_mem_page_sz_t log2_page_sz); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 298 | void *clib_mem_init_thread_safe (void *memory, uword memory_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | |
| 300 | void clib_mem_exit (void); |
| 301 | |
Matus Fabian | ef827b3 | 2024-07-19 15:24:10 +0200 | [diff] [blame] | 302 | typedef struct |
| 303 | { |
| 304 | /* Address of callers: outer first, inner last. */ |
| 305 | uword callers[12]; |
| 306 | |
| 307 | /* Count of allocations with this traceback. */ |
| 308 | u32 n_allocations; |
| 309 | |
| 310 | /* Count of bytes allocated with this traceback. */ |
| 311 | u32 n_bytes; |
| 312 | |
| 313 | /* Offset of this item */ |
| 314 | uword offset; |
| 315 | } mheap_trace_t; |
| 316 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | void clib_mem_trace (int enable); |
| 318 | |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 319 | int clib_mem_is_traced (void); |
| 320 | |
Matus Fabian | ef827b3 | 2024-07-19 15:24:10 +0200 | [diff] [blame] | 321 | mheap_trace_t *clib_mem_trace_dup (clib_mem_heap_t *heap); |
| 322 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 323 | typedef struct |
| 324 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 325 | /* Total number of objects allocated. */ |
| 326 | uword object_count; |
| 327 | |
| 328 | /* Total allocated bytes. Bytes used and free. |
| 329 | used + free = total */ |
| 330 | uword bytes_total, bytes_used, bytes_free; |
| 331 | |
| 332 | /* Number of bytes used by mheap data structure overhead |
| 333 | (e.g. free lists, mheap header). */ |
| 334 | uword bytes_overhead; |
| 335 | |
| 336 | /* Amount of free space returned to operating system. */ |
| 337 | uword bytes_free_reclaimed; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 338 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | /* For malloc which puts small objects in sbrk region and |
| 340 | large objects in mmap'ed regions. */ |
| 341 | uword bytes_used_sbrk; |
| 342 | uword bytes_used_mmap; |
| 343 | |
| 344 | /* Max. number of bytes in this heap. */ |
| 345 | uword bytes_max; |
| 346 | } clib_mem_usage_t; |
| 347 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 348 | void clib_mem_get_heap_usage (clib_mem_heap_t * heap, |
| 349 | clib_mem_usage_t * usage); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 350 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 351 | void *clib_mem_get_heap_base (clib_mem_heap_t * heap); |
| 352 | uword clib_mem_get_heap_size (clib_mem_heap_t * heap); |
| 353 | uword clib_mem_get_heap_free_space (clib_mem_heap_t * heap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 355 | u8 *format_clib_mem_usage (u8 * s, va_list * args); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 356 | u8 *format_clib_mem_heap (u8 * s, va_list * va); |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 357 | u8 *format_clib_mem_page_stats (u8 * s, va_list * va); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 359 | /* Allocate virtual address space. */ |
| 360 | always_inline void * |
| 361 | clib_mem_vm_alloc (uword size) |
| 362 | { |
| 363 | void *mmap_addr; |
| 364 | uword flags = MAP_PRIVATE; |
| 365 | |
| 366 | #ifdef MAP_ANONYMOUS |
| 367 | flags |= MAP_ANONYMOUS; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | #endif |
| 369 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 370 | mmap_addr = mmap (0, size, PROT_READ | PROT_WRITE, flags, -1, 0); |
| 371 | if (mmap_addr == (void *) -1) |
| 372 | mmap_addr = 0; |
Benoît Ganne | 1557d9a | 2020-02-07 11:58:16 +0100 | [diff] [blame] | 373 | else |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 374 | clib_mem_unpoison (mmap_addr, size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 376 | return mmap_addr; |
| 377 | } |
| 378 | |
| 379 | always_inline void |
| 380 | clib_mem_vm_free (void *addr, uword size) |
| 381 | { |
| 382 | munmap (addr, size); |
| 383 | } |
| 384 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 385 | void *clib_mem_vm_map_internal (void *base, clib_mem_page_sz_t log2_page_sz, |
| 386 | uword size, int fd, uword offset, char *name); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 387 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 388 | void *clib_mem_vm_map (void *start, uword size, |
| 389 | clib_mem_page_sz_t log2_page_size, char *fmt, ...); |
| 390 | void *clib_mem_vm_map_stack (uword size, clib_mem_page_sz_t log2_page_size, |
| 391 | char *fmt, ...); |
| 392 | void *clib_mem_vm_map_shared (void *start, uword size, int fd, uword offset, |
| 393 | char *fmt, ...); |
| 394 | int clib_mem_vm_unmap (void *base); |
| 395 | clib_mem_vm_map_hdr_t *clib_mem_vm_get_next_map_hdr (clib_mem_vm_map_hdr_t * |
| 396 | hdr); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 397 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 398 | static_always_inline clib_mem_page_sz_t |
| 399 | clib_mem_get_log2_page_size (void) |
| 400 | { |
| 401 | return clib_mem_main.log2_page_sz; |
| 402 | } |
| 403 | |
| 404 | static_always_inline uword |
| 405 | clib_mem_get_page_size (void) |
| 406 | { |
| 407 | return 1ULL << clib_mem_main.log2_page_sz; |
| 408 | } |
| 409 | |
Damjan Marion | ff011b2 | 2021-09-21 11:38:04 +0200 | [diff] [blame] | 410 | static_always_inline void |
| 411 | clib_mem_set_log2_default_hugepage_size (clib_mem_page_sz_t log2_page_sz) |
| 412 | { |
| 413 | clib_mem_main.log2_default_hugepage_sz = log2_page_sz; |
| 414 | } |
| 415 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 416 | static_always_inline clib_mem_page_sz_t |
| 417 | clib_mem_get_log2_default_hugepage_size () |
| 418 | { |
| 419 | return clib_mem_main.log2_default_hugepage_sz; |
| 420 | } |
| 421 | |
Damjan Marion | ff011b2 | 2021-09-21 11:38:04 +0200 | [diff] [blame] | 422 | static_always_inline uword |
| 423 | clib_mem_get_default_hugepage_size (void) |
| 424 | { |
| 425 | return 1ULL << clib_mem_main.log2_default_hugepage_sz; |
| 426 | } |
| 427 | |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 428 | int clib_mem_vm_create_fd (clib_mem_page_sz_t log2_page_size, char *fmt, ...); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 429 | uword clib_mem_get_fd_page_size (int fd); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 430 | clib_mem_page_sz_t clib_mem_get_fd_log2_page_size (int fd); |
Damjan Marion | b509504 | 2020-09-11 22:13:46 +0200 | [diff] [blame] | 431 | uword clib_mem_vm_reserve (uword start, uword size, |
| 432 | clib_mem_page_sz_t log2_page_sz); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 433 | u64 *clib_mem_vm_get_paddr (void *mem, clib_mem_page_sz_t log2_page_size, |
| 434 | int n_pages); |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 435 | void clib_mem_destroy (void); |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 436 | int clib_mem_set_numa_affinity (u8 numa_node, int force); |
| 437 | int clib_mem_set_default_numa_affinity (); |
Damjan Marion | b509504 | 2020-09-11 22:13:46 +0200 | [diff] [blame] | 438 | void clib_mem_vm_randomize_va (uword * requested_va, |
| 439 | clib_mem_page_sz_t log2_page_size); |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 440 | void mheap_trace (clib_mem_heap_t * v, int enable); |
Dave Barach | 8fdde3c | 2019-05-17 10:46:40 -0400 | [diff] [blame] | 441 | uword clib_mem_trace_enable_disable (uword enable); |
| 442 | void clib_mem_trace (int enable); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 444 | always_inline uword |
| 445 | clib_mem_round_to_page_size (uword size, clib_mem_page_sz_t log2_page_size) |
| 446 | { |
| 447 | ASSERT (log2_page_size != CLIB_MEM_PAGE_SZ_UNKNOWN); |
| 448 | |
| 449 | if (log2_page_size == CLIB_MEM_PAGE_SZ_DEFAULT) |
| 450 | log2_page_size = clib_mem_get_log2_page_size (); |
| 451 | else if (log2_page_size == CLIB_MEM_PAGE_SZ_DEFAULT_HUGE) |
| 452 | log2_page_size = clib_mem_get_log2_default_hugepage_size (); |
| 453 | |
| 454 | return round_pow2 (size, 1ULL << log2_page_size); |
| 455 | } |
| 456 | |
| 457 | typedef struct |
| 458 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 459 | clib_mem_page_sz_t log2_page_sz; |
| 460 | uword total; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 461 | uword mapped; |
| 462 | uword not_mapped; |
| 463 | uword per_numa[CLIB_MAX_NUMAS]; |
| 464 | uword unknown; |
| 465 | } clib_mem_page_stats_t; |
| 466 | |
| 467 | void clib_mem_get_page_stats (void *start, clib_mem_page_sz_t log2_page_size, |
| 468 | uword n_pages, clib_mem_page_stats_t * stats); |
| 469 | |
| 470 | static_always_inline int |
| 471 | vlib_mem_get_next_numa_node (int numa) |
| 472 | { |
| 473 | clib_mem_main_t *mm = &clib_mem_main; |
| 474 | u32 bitmap = mm->numa_node_bitmap; |
| 475 | |
| 476 | if (numa >= 0) |
| 477 | bitmap &= ~pow2_mask (numa + 1); |
| 478 | if (bitmap == 0) |
| 479 | return -1; |
| 480 | |
| 481 | return count_trailing_zeros (bitmap); |
| 482 | } |
| 483 | |
| 484 | static_always_inline clib_mem_page_sz_t |
| 485 | clib_mem_log2_page_size_validate (clib_mem_page_sz_t log2_page_size) |
| 486 | { |
| 487 | if (log2_page_size == CLIB_MEM_PAGE_SZ_DEFAULT) |
| 488 | return clib_mem_get_log2_page_size (); |
| 489 | if (log2_page_size == CLIB_MEM_PAGE_SZ_DEFAULT_HUGE) |
| 490 | return clib_mem_get_log2_default_hugepage_size (); |
| 491 | return log2_page_size; |
| 492 | } |
| 493 | |
| 494 | static_always_inline uword |
| 495 | clib_mem_page_bytes (clib_mem_page_sz_t log2_page_size) |
| 496 | { |
Dave Barach | 27c35e3 | 2020-10-07 09:37:36 -0400 | [diff] [blame] | 497 | return 1ULL << clib_mem_log2_page_size_validate (log2_page_size); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 500 | static_always_inline clib_error_t * |
| 501 | clib_mem_get_last_error (void) |
| 502 | { |
| 503 | return clib_mem_main.error; |
| 504 | } |
| 505 | |
Damjan Marion | 0da8168 | 2020-12-22 14:58:56 +0100 | [diff] [blame] | 506 | /* bulk allocator */ |
| 507 | |
| 508 | typedef void *clib_mem_bulk_handle_t; |
| 509 | clib_mem_bulk_handle_t clib_mem_bulk_init (u32 elt_sz, u32 align, |
| 510 | u32 min_elts_per_chunk); |
| 511 | void clib_mem_bulk_destroy (clib_mem_bulk_handle_t h); |
| 512 | void *clib_mem_bulk_alloc (clib_mem_bulk_handle_t h); |
| 513 | void clib_mem_bulk_free (clib_mem_bulk_handle_t h, void *p); |
| 514 | u8 *format_clib_mem_bulk (u8 *s, va_list *args); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 515 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 516 | #include <vppinfra/error.h> /* clib_panic */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | |
| 518 | #endif /* _included_clib_mem_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 519 | |
| 520 | /* |
| 521 | * fd.io coding-style-patch-verification: ON |
| 522 | * |
| 523 | * Local Variables: |
| 524 | * eval: (c-set-style "gnu") |
| 525 | * End: |
| 526 | */ |