Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [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 | #include <vppinfra/format.h> |
| 17 | #include <vppinfra/dlmalloc.h> |
| 18 | #include <vppinfra/os.h> |
| 19 | #include <vppinfra/lock.h> |
| 20 | #include <vppinfra/hash.h> |
| 21 | #include <vppinfra/elf_clib.h> |
Damjan Marion | 7892560 | 2024-05-23 13:06:39 +0000 | [diff] [blame] | 22 | #include <vppinfra/stack.h> |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 23 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 24 | typedef struct |
| 25 | { |
| 26 | /* Address of callers: outer first, inner last. */ |
| 27 | uword callers[12]; |
| 28 | |
| 29 | /* Count of allocations with this traceback. */ |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 30 | u32 n_allocations; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 31 | |
| 32 | /* Count of bytes allocated with this traceback. */ |
| 33 | u32 n_bytes; |
| 34 | |
| 35 | /* Offset of this item */ |
| 36 | uword offset; |
| 37 | } mheap_trace_t; |
| 38 | |
| 39 | typedef struct |
| 40 | { |
| 41 | clib_spinlock_t lock; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 42 | |
| 43 | mheap_trace_t *traces; |
| 44 | |
| 45 | /* Indices of free traces. */ |
| 46 | u32 *trace_free_list; |
| 47 | |
| 48 | /* Hash table mapping callers to trace index. */ |
| 49 | uword *trace_by_callers; |
| 50 | |
| 51 | /* Hash table mapping mheap offset to trace index. */ |
| 52 | uword *trace_index_by_offset; |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 53 | |
| 54 | /* So we can easily shut off current segment trace, if any */ |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 55 | const clib_mem_heap_t *current_traced_mheap; |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 56 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 57 | } mheap_trace_main_t; |
| 58 | |
| 59 | mheap_trace_main_t mheap_trace_main; |
| 60 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 61 | static __thread int mheap_trace_thread_disable; |
| 62 | |
| 63 | static void |
| 64 | mheap_get_trace_internal (const clib_mem_heap_t *heap, uword offset, |
| 65 | uword size) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 66 | { |
| 67 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 68 | mheap_trace_t *t; |
Damjan Marion | 7892560 | 2024-05-23 13:06:39 +0000 | [diff] [blame] | 69 | uword i, trace_index, *p; |
| 70 | mheap_trace_t trace = {}; |
| 71 | int index; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 72 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 73 | if (heap != tm->current_traced_mheap || mheap_trace_thread_disable) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 74 | return; |
| 75 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 76 | clib_spinlock_lock (&tm->lock); |
| 77 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 78 | /* heap could have changed while we were waiting on the lock */ |
| 79 | if (heap != tm->current_traced_mheap) |
| 80 | goto out; |
| 81 | |
| 82 | /* Turn off tracing for this thread to avoid embarrassment... */ |
| 83 | mheap_trace_thread_disable = 1; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 84 | |
Damjan Marion | 7892560 | 2024-05-23 13:06:39 +0000 | [diff] [blame] | 85 | index = -2; /* skip first 2 stack frames */ |
| 86 | foreach_clib_stack_frame (sf) |
| 87 | { |
| 88 | if (index >= 0) |
| 89 | { |
| 90 | if (index == ARRAY_LEN (trace.callers)) |
| 91 | break; |
| 92 | trace.callers[index] = sf->ip; |
| 93 | } |
| 94 | index++; |
| 95 | } |
| 96 | |
| 97 | if (index < 1) |
Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 98 | goto out; |
| 99 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 100 | if (!tm->trace_by_callers) |
| 101 | tm->trace_by_callers = |
| 102 | hash_create_shmem (0, sizeof (trace.callers), sizeof (uword)); |
| 103 | |
| 104 | p = hash_get_mem (tm->trace_by_callers, &trace.callers); |
| 105 | if (p) |
| 106 | { |
| 107 | trace_index = p[0]; |
| 108 | t = tm->traces + trace_index; |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | i = vec_len (tm->trace_free_list); |
| 113 | if (i > 0) |
| 114 | { |
| 115 | trace_index = tm->trace_free_list[i - 1]; |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 116 | vec_set_len (tm->trace_free_list, i - 1); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 117 | } |
| 118 | else |
| 119 | { |
| 120 | mheap_trace_t *old_start = tm->traces; |
| 121 | mheap_trace_t *old_end = vec_end (tm->traces); |
| 122 | |
| 123 | vec_add2 (tm->traces, t, 1); |
| 124 | |
| 125 | if (tm->traces != old_start) |
| 126 | { |
| 127 | hash_pair_t *p; |
| 128 | mheap_trace_t *q; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 129 | hash_foreach_pair (p, tm->trace_by_callers, |
| 130 | ({ |
| 131 | q = uword_to_pointer (p->key, mheap_trace_t *); |
| 132 | ASSERT (q >= old_start && q < old_end); |
| 133 | p->key = pointer_to_uword (tm->traces + (q - old_start)); |
| 134 | })); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 135 | } |
| 136 | trace_index = t - tm->traces; |
| 137 | } |
| 138 | |
| 139 | t = tm->traces + trace_index; |
| 140 | t[0] = trace; |
| 141 | t->n_allocations = 0; |
| 142 | t->n_bytes = 0; |
| 143 | hash_set_mem (tm->trace_by_callers, t->callers, trace_index); |
| 144 | } |
| 145 | |
| 146 | t->n_allocations += 1; |
| 147 | t->n_bytes += size; |
| 148 | t->offset = offset; /* keep a sample to autopsy */ |
| 149 | hash_set (tm->trace_index_by_offset, offset, t - tm->traces); |
Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 150 | |
| 151 | out: |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 152 | mheap_trace_thread_disable = 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 153 | clib_spinlock_unlock (&tm->lock); |
| 154 | } |
| 155 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 156 | static void |
| 157 | mheap_put_trace_internal (const clib_mem_heap_t *heap, uword offset, |
| 158 | uword size) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 159 | { |
| 160 | mheap_trace_t *t; |
| 161 | uword trace_index, *p; |
| 162 | mheap_trace_main_t *tm = &mheap_trace_main; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 163 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 164 | if (heap != tm->current_traced_mheap || mheap_trace_thread_disable) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 165 | return; |
| 166 | |
| 167 | clib_spinlock_lock (&tm->lock); |
| 168 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 169 | /* heap could have changed while we were waiting on the lock */ |
| 170 | if (heap != tm->current_traced_mheap) |
| 171 | goto out; |
| 172 | |
| 173 | /* Turn off tracing for this thread for a moment */ |
| 174 | mheap_trace_thread_disable = 1; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 175 | |
| 176 | p = hash_get (tm->trace_index_by_offset, offset); |
| 177 | if (!p) |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 178 | goto out; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 179 | |
| 180 | trace_index = p[0]; |
| 181 | hash_unset (tm->trace_index_by_offset, offset); |
| 182 | ASSERT (trace_index < vec_len (tm->traces)); |
| 183 | |
| 184 | t = tm->traces + trace_index; |
| 185 | ASSERT (t->n_allocations > 0); |
| 186 | ASSERT (t->n_bytes >= size); |
| 187 | t->n_allocations -= 1; |
| 188 | t->n_bytes -= size; |
| 189 | if (t->n_allocations == 0) |
| 190 | { |
| 191 | hash_unset_mem (tm->trace_by_callers, t->callers); |
| 192 | vec_add1 (tm->trace_free_list, trace_index); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 193 | clib_memset (t, 0, sizeof (t[0])); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 194 | } |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 195 | |
| 196 | out: |
| 197 | mheap_trace_thread_disable = 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 198 | clib_spinlock_unlock (&tm->lock); |
| 199 | } |
| 200 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 201 | void |
| 202 | mheap_get_trace (uword offset, uword size) |
| 203 | { |
| 204 | mheap_get_trace_internal (clib_mem_get_heap (), offset, size); |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | mheap_put_trace (uword offset, uword size) |
| 209 | { |
| 210 | mheap_put_trace_internal (clib_mem_get_heap (), offset, size); |
| 211 | } |
| 212 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 213 | always_inline void |
| 214 | mheap_trace_main_free (mheap_trace_main_t * tm) |
| 215 | { |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 216 | CLIB_SPINLOCK_ASSERT_LOCKED (&tm->lock); |
| 217 | tm->current_traced_mheap = 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 218 | vec_free (tm->traces); |
| 219 | vec_free (tm->trace_free_list); |
| 220 | hash_free (tm->trace_by_callers); |
| 221 | hash_free (tm->trace_index_by_offset); |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 222 | mheap_trace_thread_disable = 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 223 | } |
| 224 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 225 | static clib_mem_heap_t * |
| 226 | clib_mem_create_heap_internal (void *base, uword size, |
| 227 | clib_mem_page_sz_t log2_page_sz, int is_locked, |
| 228 | char *name) |
| 229 | { |
| 230 | clib_mem_heap_t *h; |
| 231 | u8 flags = 0; |
| 232 | int sz = sizeof (clib_mem_heap_t); |
| 233 | |
| 234 | if (base == 0) |
| 235 | { |
| 236 | log2_page_sz = clib_mem_log2_page_size_validate (log2_page_sz); |
| 237 | size = round_pow2 (size, clib_mem_page_bytes (log2_page_sz)); |
| 238 | base = clib_mem_vm_map_internal (0, log2_page_sz, size, -1, 0, |
| 239 | "main heap"); |
| 240 | |
| 241 | if (base == CLIB_MEM_VM_MAP_FAILED) |
| 242 | return 0; |
| 243 | |
| 244 | flags = CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY; |
| 245 | } |
| 246 | else |
| 247 | log2_page_sz = CLIB_MEM_PAGE_SZ_UNKNOWN; |
| 248 | |
| 249 | if (is_locked) |
| 250 | flags |= CLIB_MEM_HEAP_F_LOCKED; |
| 251 | |
| 252 | h = base; |
| 253 | h->base = base; |
| 254 | h->size = size; |
| 255 | h->log2_page_sz = log2_page_sz; |
| 256 | h->flags = flags; |
| 257 | sz = strlen (name); |
| 258 | strcpy (h->name, name); |
| 259 | sz = round_pow2 (sz + sizeof (clib_mem_heap_t), 16); |
| 260 | h->mspace = create_mspace_with_base (base + sz, size - sz, is_locked); |
| 261 | |
| 262 | mspace_disable_expand (h->mspace); |
| 263 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 264 | clib_mem_poison (mspace_least_addr (h->mspace), |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 265 | mspace_footprint (h->mspace)); |
| 266 | |
| 267 | return h; |
| 268 | } |
| 269 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 270 | /* Initialize CLIB heap based on memory/size given by user. |
| 271 | Set memory to 0 and CLIB will try to allocate its own heap. */ |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 272 | static void * |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 273 | clib_mem_init_internal (void *base, uword size, |
| 274 | clib_mem_page_sz_t log2_page_sz) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 275 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 276 | clib_mem_heap_t *h; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 277 | |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 278 | clib_mem_main_init (); |
| 279 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 280 | h = clib_mem_create_heap_internal (base, size, log2_page_sz, |
| 281 | 1 /*is_locked */ , "main heap"); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 282 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 283 | clib_mem_set_heap (h); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 284 | |
| 285 | if (mheap_trace_main.lock == 0) |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 286 | { |
| 287 | /* clib_spinlock_init() dynamically allocates the spinlock in the current |
| 288 | * per-cpu heap, but it is used for all traces accross all heaps and |
| 289 | * hence we can't really allocate it in the current per-cpu heap as it |
| 290 | * could be destroyed later */ |
| 291 | static struct clib_spinlock_s mheap_trace_main_lock = {}; |
| 292 | mheap_trace_main.lock = &mheap_trace_main_lock; |
| 293 | } |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 294 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 295 | return h; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 298 | __clib_export void * |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 299 | clib_mem_init (void *memory, uword memory_size) |
| 300 | { |
| 301 | return clib_mem_init_internal (memory, memory_size, |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 302 | CLIB_MEM_PAGE_SZ_DEFAULT); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 305 | __clib_export void * |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 306 | clib_mem_init_with_page_size (uword memory_size, |
| 307 | clib_mem_page_sz_t log2_page_sz) |
| 308 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 309 | return clib_mem_init_internal (0, memory_size, log2_page_sz); |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 310 | } |
| 311 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 312 | __clib_export void * |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 313 | clib_mem_init_thread_safe (void *memory, uword memory_size) |
| 314 | { |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 315 | return clib_mem_init_internal (memory, memory_size, |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 316 | CLIB_MEM_PAGE_SZ_DEFAULT); |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 317 | } |
| 318 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 319 | __clib_export void |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 320 | clib_mem_destroy (void) |
| 321 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 322 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 323 | clib_mem_heap_t *heap = clib_mem_get_heap (); |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 324 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 325 | if (heap->mspace == tm->current_traced_mheap) |
| 326 | mheap_trace (heap, 0); |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 327 | |
| 328 | destroy_mspace (heap->mspace); |
Damjan Marion | 16ca1a9 | 2022-04-28 17:46:13 +0200 | [diff] [blame] | 329 | clib_mem_vm_unmap (heap); |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 332 | __clib_export u8 * |
| 333 | format_clib_mem_usage (u8 *s, va_list *va) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 334 | { |
| 335 | int verbose = va_arg (*va, int); |
| 336 | return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (), |
| 337 | verbose); |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Magic decoder ring for mallinfo stats (ala dlmalloc): |
| 342 | * |
| 343 | * size_t arena; / * Non-mmapped space allocated (bytes) * / |
| 344 | * size_t ordblks; / * Number of free chunks * / |
| 345 | * size_t smblks; / * Number of free fastbin blocks * / |
| 346 | * size_t hblks; / * Number of mmapped regions * / |
| 347 | * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * / |
| 348 | * size_t usmblks; / * Maximum total allocated space (bytes) * / |
| 349 | * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * / |
| 350 | * size_t uordblks; / * Total allocated space (bytes) * / |
| 351 | * size_t fordblks; / * Total free space (bytes) * / |
| 352 | * size_t keepcost; / * Top-most, releasable space (bytes) * / |
| 353 | * |
| 354 | */ |
| 355 | |
| 356 | u8 * |
| 357 | format_msize (u8 * s, va_list * va) |
| 358 | { |
| 359 | uword a = va_arg (*va, uword); |
| 360 | |
| 361 | if (a >= 1ULL << 30) |
| 362 | s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30)))); |
| 363 | else if (a >= 1ULL << 20) |
| 364 | s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20)))); |
| 365 | else if (a >= 1ULL << 10) |
| 366 | s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10)))); |
| 367 | else |
| 368 | s = format (s, "%lld", a); |
| 369 | return s; |
| 370 | } |
| 371 | |
| 372 | static int |
| 373 | mheap_trace_sort (const void *_t1, const void *_t2) |
| 374 | { |
| 375 | const mheap_trace_t *t1 = _t1; |
| 376 | const mheap_trace_t *t2 = _t2; |
| 377 | word cmp; |
| 378 | |
| 379 | cmp = (word) t2->n_bytes - (word) t1->n_bytes; |
| 380 | if (!cmp) |
| 381 | cmp = (word) t2->n_allocations - (word) t1->n_allocations; |
| 382 | return cmp; |
| 383 | } |
| 384 | |
| 385 | u8 * |
| 386 | format_mheap_trace (u8 * s, va_list * va) |
| 387 | { |
| 388 | mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *); |
| 389 | int verbose = va_arg (*va, int); |
| 390 | int have_traces = 0; |
| 391 | int i; |
Benoît Ganne | af62f93 | 2023-02-08 18:54:30 +0100 | [diff] [blame] | 392 | int n = 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 393 | |
| 394 | clib_spinlock_lock (&tm->lock); |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 395 | if (vec_len (tm->traces) > 0 && |
| 396 | clib_mem_get_heap () == tm->current_traced_mheap) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 397 | { |
| 398 | have_traces = 1; |
| 399 | |
| 400 | /* Make a copy of traces since we'll be sorting them. */ |
| 401 | mheap_trace_t *t, *traces_copy; |
| 402 | u32 indent, total_objects_traced; |
| 403 | |
| 404 | traces_copy = vec_dup (tm->traces); |
| 405 | |
| 406 | qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]), |
| 407 | mheap_trace_sort); |
| 408 | |
| 409 | total_objects_traced = 0; |
| 410 | s = format (s, "\n"); |
| 411 | vec_foreach (t, traces_copy) |
| 412 | { |
| 413 | /* Skip over free elements. */ |
| 414 | if (t->n_allocations == 0) |
| 415 | continue; |
| 416 | |
| 417 | total_objects_traced += t->n_allocations; |
| 418 | |
Benoît Ganne | af62f93 | 2023-02-08 18:54:30 +0100 | [diff] [blame] | 419 | /* When not verbose only report the 50 biggest allocations */ |
| 420 | if (!verbose && n >= 50) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 421 | continue; |
Benoît Ganne | af62f93 | 2023-02-08 18:54:30 +0100 | [diff] [blame] | 422 | n++; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 423 | |
| 424 | if (t == traces_copy) |
| 425 | s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count", |
| 426 | "Sample"); |
| 427 | s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset); |
| 428 | indent = format_get_indent (s); |
| 429 | for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++) |
| 430 | { |
| 431 | if (i > 0) |
| 432 | s = format (s, "%U", format_white_space, indent); |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 433 | #if defined(CLIB_UNIX) && !defined(__APPLE__) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 434 | /* $$$$ does this actually work? */ |
| 435 | s = |
| 436 | format (s, " %U\n", format_clib_elf_symbol_with_address, |
| 437 | t->callers[i]); |
| 438 | #else |
| 439 | s = format (s, " %p\n", t->callers[i]); |
| 440 | #endif |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | s = format (s, "%d total traced objects\n", total_objects_traced); |
| 445 | |
| 446 | vec_free (traces_copy); |
| 447 | } |
| 448 | clib_spinlock_unlock (&tm->lock); |
| 449 | if (have_traces == 0) |
| 450 | s = format (s, "no traced allocations\n"); |
| 451 | |
| 452 | return s; |
| 453 | } |
| 454 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 455 | __clib_export u8 * |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 456 | format_clib_mem_heap (u8 * s, va_list * va) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 457 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 458 | clib_mem_heap_t *heap = va_arg (*va, clib_mem_heap_t *); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 459 | int verbose = va_arg (*va, int); |
Dave Barach | af7dd5b | 2018-08-23 17:08:44 -0400 | [diff] [blame] | 460 | struct dlmallinfo mi; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 461 | mheap_trace_main_t *tm = &mheap_trace_main; |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 462 | u32 indent = format_get_indent (s) + 2; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 463 | |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 464 | if (heap == 0) |
| 465 | heap = clib_mem_get_heap (); |
| 466 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 467 | mi = mspace_mallinfo (heap->mspace); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 468 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 469 | s = format (s, "base %p, size %U", |
| 470 | heap->base, format_memory_size, heap->size); |
| 471 | |
| 472 | #define _(i,v,str) \ |
| 473 | if (heap->flags & CLIB_MEM_HEAP_F_##v) s = format (s, ", %s", str); |
| 474 | foreach_clib_mem_heap_flag; |
| 475 | #undef _ |
| 476 | |
| 477 | s = format (s, ", name '%s'", heap->name); |
| 478 | |
| 479 | if (heap->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN) |
| 480 | { |
| 481 | clib_mem_page_stats_t stats; |
| 482 | clib_mem_get_page_stats (heap->base, heap->log2_page_sz, |
| 483 | heap->size >> heap->log2_page_sz, &stats); |
| 484 | s = format (s, "\n%U%U", format_white_space, indent, |
| 485 | format_clib_mem_page_stats, &stats); |
| 486 | } |
| 487 | |
| 488 | s = format (s, "\n%Utotal: %U, used: %U, free: %U, trimmable: %U", |
| 489 | format_white_space, indent, |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 490 | format_msize, mi.arena, |
| 491 | format_msize, mi.uordblks, |
| 492 | format_msize, mi.fordblks, format_msize, mi.keepcost); |
| 493 | if (verbose > 0) |
| 494 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 495 | s = format (s, "\n%Ufree chunks %llu free fastbin blks %llu", |
| 496 | format_white_space, indent + 2, mi.ordblks, mi.smblks); |
| 497 | s = format (s, "\n%Umax total allocated %U", |
| 498 | format_white_space, indent + 2, format_msize, mi.usmblks); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 499 | } |
| 500 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 501 | if (heap->flags & CLIB_MEM_HEAP_F_TRACED) |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 502 | s = format (s, "\n%U", format_mheap_trace, tm, verbose); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 503 | return s; |
| 504 | } |
| 505 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 506 | __clib_export __clib_flatten void |
| 507 | clib_mem_get_heap_usage (clib_mem_heap_t *heap, clib_mem_usage_t *usage) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 508 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 509 | struct dlmallinfo mi = mspace_mallinfo (heap->mspace); |
Ole Troan | 92e3082 | 2019-06-16 12:33:51 +0200 | [diff] [blame] | 510 | |
Ole Troan | a606d92 | 2021-05-05 09:23:17 +0200 | [diff] [blame] | 511 | usage->bytes_total = mi.arena; /* non-mmapped space allocated from system */ |
| 512 | usage->bytes_used = mi.uordblks; /* total allocated space */ |
| 513 | usage->bytes_free = mi.fordblks; /* total free space */ |
| 514 | usage->bytes_used_mmap = mi.hblkhd; /* space in mmapped regions */ |
| 515 | usage->bytes_max = mi.usmblks; /* maximum total allocated space */ |
| 516 | usage->bytes_free_reclaimed = mi.ordblks; /* number of free chunks */ |
| 517 | usage->bytes_overhead = mi.keepcost; /* releasable (via malloc_trim) space */ |
| 518 | |
| 519 | /* Not supported */ |
| 520 | usage->bytes_used_sbrk = 0; |
Ole Troan | 92e3082 | 2019-06-16 12:33:51 +0200 | [diff] [blame] | 521 | usage->object_count = 0; |
Ole Troan | 92e3082 | 2019-06-16 12:33:51 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 524 | /* Call serial number for debugger breakpoints. */ |
| 525 | uword clib_mem_validate_serial = 0; |
| 526 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 527 | __clib_export void |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 528 | mheap_trace (clib_mem_heap_t * h, int enable) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 529 | { |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 530 | mheap_trace_main_t *tm = &mheap_trace_main; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 531 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 532 | clib_spinlock_lock (&tm->lock); |
| 533 | |
| 534 | if (tm->current_traced_mheap != 0 && tm->current_traced_mheap != h) |
| 535 | { |
| 536 | clib_warning ("tracing already enabled for another heap, ignoring"); |
| 537 | goto out; |
| 538 | } |
| 539 | |
| 540 | if (enable) |
| 541 | { |
| 542 | h->flags |= CLIB_MEM_HEAP_F_TRACED; |
| 543 | tm->current_traced_mheap = h; |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | h->flags &= ~CLIB_MEM_HEAP_F_TRACED; |
| 548 | mheap_trace_main_free (&mheap_trace_main); |
| 549 | } |
| 550 | |
| 551 | out: |
| 552 | clib_spinlock_unlock (&tm->lock); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 553 | } |
| 554 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 555 | __clib_export void |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 556 | clib_mem_trace (int enable) |
| 557 | { |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 558 | void *current_heap = clib_mem_get_heap (); |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 559 | mheap_trace (current_heap, enable); |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | int |
| 563 | clib_mem_is_traced (void) |
| 564 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 565 | clib_mem_heap_t *h = clib_mem_get_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 566 | return (h->flags &= CLIB_MEM_HEAP_F_TRACED) != 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 567 | } |
| 568 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 569 | __clib_export uword |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 570 | clib_mem_trace_enable_disable (uword enable) |
| 571 | { |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 572 | uword rv = !mheap_trace_thread_disable; |
| 573 | mheap_trace_thread_disable = !enable; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 574 | return rv; |
| 575 | } |
| 576 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 577 | __clib_export clib_mem_heap_t * |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 578 | clib_mem_create_heap (void *base, uword size, int is_locked, char *fmt, ...) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 579 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 580 | clib_mem_page_sz_t log2_page_sz = clib_mem_get_log2_page_size (); |
| 581 | clib_mem_heap_t *h; |
| 582 | char *name; |
| 583 | u8 *s = 0; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 584 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 585 | if (fmt == 0) |
| 586 | { |
| 587 | name = ""; |
| 588 | } |
| 589 | else if (strchr (fmt, '%')) |
| 590 | { |
| 591 | va_list va; |
| 592 | va_start (va, fmt); |
| 593 | s = va_format (0, fmt, &va); |
| 594 | vec_add1 (s, 0); |
| 595 | va_end (va); |
| 596 | name = (char *) s; |
| 597 | } |
| 598 | else |
| 599 | name = fmt; |
| 600 | |
| 601 | h = clib_mem_create_heap_internal (base, size, log2_page_sz, is_locked, |
| 602 | name); |
| 603 | vec_free (s); |
| 604 | return h; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 607 | __clib_export void |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 608 | clib_mem_destroy_heap (clib_mem_heap_t * h) |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 609 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 610 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 611 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 612 | if (h->mspace == tm->current_traced_mheap) |
| 613 | mheap_trace (h, 0); |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 614 | |
| 615 | destroy_mspace (h->mspace); |
| 616 | if (h->flags & CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY) |
| 617 | clib_mem_vm_unmap (h->base); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 620 | __clib_export __clib_flatten uword |
| 621 | clib_mem_get_heap_free_space (clib_mem_heap_t *h) |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 622 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 623 | struct dlmallinfo dlminfo = mspace_mallinfo (h->mspace); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 624 | return dlminfo.fordblks; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 625 | } |
| 626 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 627 | __clib_export __clib_flatten void * |
| 628 | clib_mem_get_heap_base (clib_mem_heap_t *h) |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 629 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 630 | return h->base; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 631 | } |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 632 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 633 | __clib_export __clib_flatten uword |
| 634 | clib_mem_get_heap_size (clib_mem_heap_t *heap) |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 635 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 636 | return heap->size; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 639 | /* Memory allocator which may call os_out_of_memory() if it fails */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 640 | static inline void * |
| 641 | clib_mem_heap_alloc_inline (void *heap, uword size, uword align, |
| 642 | int os_out_of_memory_on_failure) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 643 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 644 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 645 | void *p; |
| 646 | |
| 647 | align = clib_max (CLIB_MEM_MIN_ALIGN, align); |
| 648 | |
| 649 | p = mspace_memalign (h->mspace, align, size); |
| 650 | |
| 651 | if (PREDICT_FALSE (0 == p)) |
| 652 | { |
| 653 | if (os_out_of_memory_on_failure) |
| 654 | os_out_of_memory (); |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED)) |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 659 | mheap_get_trace_internal (h, pointer_to_uword (p), clib_mem_size (p)); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 660 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 661 | clib_mem_unpoison (p, size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 662 | return p; |
| 663 | } |
| 664 | |
| 665 | /* Memory allocator which calls os_out_of_memory() when it fails */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 666 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 667 | clib_mem_alloc (uword size) |
| 668 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 669 | return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN, |
| 670 | /* os_out_of_memory */ 1); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 673 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 674 | clib_mem_alloc_aligned (uword size, uword align) |
| 675 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 676 | return clib_mem_heap_alloc_inline (0, size, align, |
| 677 | /* os_out_of_memory */ 1); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* Memory allocator which calls os_out_of_memory() when it fails */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 681 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 682 | clib_mem_alloc_or_null (uword size) |
| 683 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 684 | return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN, |
| 685 | /* os_out_of_memory */ 0); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 688 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 689 | clib_mem_alloc_aligned_or_null (uword size, uword align) |
| 690 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 691 | return clib_mem_heap_alloc_inline (0, size, align, |
| 692 | /* os_out_of_memory */ 0); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 695 | __clib_export __clib_flatten void * |
| 696 | clib_mem_heap_alloc (void *heap, uword size) |
| 697 | { |
| 698 | return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN, |
| 699 | /* os_out_of_memory */ 1); |
| 700 | } |
| 701 | |
| 702 | __clib_export __clib_flatten void * |
| 703 | clib_mem_heap_alloc_aligned (void *heap, uword size, uword align) |
| 704 | { |
| 705 | return clib_mem_heap_alloc_inline (heap, size, align, |
| 706 | /* os_out_of_memory */ 1); |
| 707 | } |
| 708 | |
| 709 | __clib_export __clib_flatten void * |
| 710 | clib_mem_heap_alloc_or_null (void *heap, uword size) |
| 711 | { |
| 712 | return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN, |
| 713 | /* os_out_of_memory */ 0); |
| 714 | } |
| 715 | |
| 716 | __clib_export __clib_flatten void * |
| 717 | clib_mem_heap_alloc_aligned_or_null (void *heap, uword size, uword align) |
| 718 | { |
| 719 | return clib_mem_heap_alloc_inline (heap, size, align, |
| 720 | /* os_out_of_memory */ 0); |
| 721 | } |
| 722 | |
| 723 | __clib_export __clib_flatten void * |
| 724 | clib_mem_heap_realloc_aligned (void *heap, void *p, uword new_size, |
| 725 | uword align) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 726 | { |
| 727 | uword old_alloc_size; |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 728 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 729 | void *new; |
| 730 | |
| 731 | ASSERT (count_set_bits (align) == 1); |
| 732 | |
| 733 | old_alloc_size = p ? mspace_usable_size (p) : 0; |
| 734 | |
| 735 | if (new_size == old_alloc_size) |
| 736 | return p; |
| 737 | |
| 738 | if (p && pointer_is_aligned (p, align) && |
| 739 | mspace_realloc_in_place (h->mspace, p, new_size)) |
| 740 | { |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 741 | clib_mem_unpoison (p, new_size); |
Leung Lai Yung | 69be089 | 2022-05-22 13:25:53 +0000 | [diff] [blame] | 742 | if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED)) |
| 743 | { |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 744 | mheap_put_trace_internal (h, pointer_to_uword (p), old_alloc_size); |
| 745 | mheap_get_trace_internal (h, pointer_to_uword (p), |
| 746 | clib_mem_size (p)); |
Leung Lai Yung | 69be089 | 2022-05-22 13:25:53 +0000 | [diff] [blame] | 747 | } |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 748 | } |
| 749 | else |
| 750 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 751 | new = clib_mem_heap_alloc_inline (h, new_size, align, 1); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 752 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 753 | clib_mem_unpoison (new, new_size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 754 | if (old_alloc_size) |
| 755 | { |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 756 | clib_mem_unpoison (p, old_alloc_size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 757 | clib_memcpy_fast (new, p, clib_min (new_size, old_alloc_size)); |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 758 | clib_mem_heap_free (h, p); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 759 | } |
| 760 | p = new; |
| 761 | } |
| 762 | |
| 763 | return p; |
| 764 | } |
| 765 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 766 | __clib_export __clib_flatten void * |
| 767 | clib_mem_heap_realloc (void *heap, void *p, uword new_size) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 768 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 769 | return clib_mem_heap_realloc_aligned (heap, p, new_size, CLIB_MEM_MIN_ALIGN); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 770 | } |
| 771 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 772 | __clib_export __clib_flatten void * |
| 773 | clib_mem_realloc_aligned (void *p, uword new_size, uword align) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 774 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 775 | return clib_mem_heap_realloc_aligned (0, p, new_size, align); |
| 776 | } |
| 777 | |
| 778 | __clib_export __clib_flatten void * |
| 779 | clib_mem_realloc (void *p, uword new_size) |
| 780 | { |
| 781 | return clib_mem_heap_realloc_aligned (0, p, new_size, CLIB_MEM_MIN_ALIGN); |
| 782 | } |
| 783 | |
| 784 | __clib_export __clib_flatten uword |
| 785 | clib_mem_heap_is_heap_object (void *heap, void *p) |
| 786 | { |
| 787 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 788 | return mspace_is_heap_object (h->mspace, p); |
| 789 | } |
| 790 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 791 | __clib_export __clib_flatten uword |
| 792 | clib_mem_is_heap_object (void *p) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 793 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 794 | return clib_mem_heap_is_heap_object (0, p); |
| 795 | } |
| 796 | |
| 797 | __clib_export __clib_flatten void |
| 798 | clib_mem_heap_free (void *heap, void *p) |
| 799 | { |
| 800 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 801 | uword size = clib_mem_size (p); |
| 802 | |
| 803 | /* Make sure object is in the correct heap. */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 804 | ASSERT (clib_mem_heap_is_heap_object (h, p)); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 805 | |
| 806 | if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED)) |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 807 | mheap_put_trace_internal (h, pointer_to_uword (p), size); |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 808 | clib_mem_poison (p, clib_mem_size (p)); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 809 | |
| 810 | mspace_free (h->mspace, p); |
| 811 | } |
| 812 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 813 | __clib_export __clib_flatten void |
| 814 | clib_mem_free (void *p) |
| 815 | { |
| 816 | clib_mem_heap_free (0, p); |
| 817 | } |
| 818 | |
| 819 | __clib_export __clib_flatten uword |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 820 | clib_mem_size (void *p) |
| 821 | { |
| 822 | return mspace_usable_size (p); |
| 823 | } |
| 824 | |
| 825 | __clib_export void |
| 826 | clib_mem_free_s (void *p) |
| 827 | { |
| 828 | uword size = clib_mem_size (p); |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 829 | clib_mem_unpoison (p, size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 830 | memset_s_inline (p, size, 0, size); |
| 831 | clib_mem_free (p); |
| 832 | } |