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