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