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