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 | |
| 77 | /* Skip our frame and mspace_get_aligned's frame */ |
| 78 | n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2); |
| 79 | if (n_callers == 0) |
| 80 | return; |
| 81 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 82 | clib_spinlock_lock (&tm->lock); |
| 83 | |
| 84 | /* Turn off tracing to avoid embarrassment... */ |
| 85 | save_enabled = tm->enabled; |
| 86 | tm->enabled = 0; |
| 87 | |
| 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); |
| 140 | tm->enabled = save_enabled; |
| 141 | clib_spinlock_unlock (&tm->lock); |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | mheap_put_trace (uword offset, uword size) |
| 146 | { |
| 147 | mheap_trace_t *t; |
| 148 | uword trace_index, *p; |
| 149 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 150 | uword save_enabled; |
| 151 | |
| 152 | if (tm->enabled == 0) |
| 153 | return; |
| 154 | |
| 155 | clib_spinlock_lock (&tm->lock); |
| 156 | |
| 157 | /* Turn off tracing for a moment */ |
| 158 | save_enabled = tm->enabled; |
| 159 | tm->enabled = 0; |
| 160 | |
| 161 | p = hash_get (tm->trace_index_by_offset, offset); |
| 162 | if (!p) |
| 163 | { |
| 164 | tm->enabled = save_enabled; |
| 165 | clib_spinlock_unlock (&tm->lock); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | trace_index = p[0]; |
| 170 | hash_unset (tm->trace_index_by_offset, offset); |
| 171 | ASSERT (trace_index < vec_len (tm->traces)); |
| 172 | |
| 173 | t = tm->traces + trace_index; |
| 174 | ASSERT (t->n_allocations > 0); |
| 175 | ASSERT (t->n_bytes >= size); |
| 176 | t->n_allocations -= 1; |
| 177 | t->n_bytes -= size; |
| 178 | if (t->n_allocations == 0) |
| 179 | { |
| 180 | hash_unset_mem (tm->trace_by_callers, t->callers); |
| 181 | vec_add1 (tm->trace_free_list, trace_index); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 182 | clib_memset (t, 0, sizeof (t[0])); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 183 | } |
| 184 | tm->enabled = save_enabled; |
| 185 | clib_spinlock_unlock (&tm->lock); |
| 186 | } |
| 187 | |
| 188 | always_inline void |
| 189 | mheap_trace_main_free (mheap_trace_main_t * tm) |
| 190 | { |
| 191 | vec_free (tm->traces); |
| 192 | vec_free (tm->trace_free_list); |
| 193 | hash_free (tm->trace_by_callers); |
| 194 | hash_free (tm->trace_index_by_offset); |
| 195 | } |
| 196 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 197 | static clib_mem_heap_t * |
| 198 | clib_mem_create_heap_internal (void *base, uword size, |
| 199 | clib_mem_page_sz_t log2_page_sz, int is_locked, |
| 200 | char *name) |
| 201 | { |
| 202 | clib_mem_heap_t *h; |
| 203 | u8 flags = 0; |
| 204 | int sz = sizeof (clib_mem_heap_t); |
| 205 | |
| 206 | if (base == 0) |
| 207 | { |
| 208 | log2_page_sz = clib_mem_log2_page_size_validate (log2_page_sz); |
| 209 | size = round_pow2 (size, clib_mem_page_bytes (log2_page_sz)); |
| 210 | base = clib_mem_vm_map_internal (0, log2_page_sz, size, -1, 0, |
| 211 | "main heap"); |
| 212 | |
| 213 | if (base == CLIB_MEM_VM_MAP_FAILED) |
| 214 | return 0; |
| 215 | |
| 216 | flags = CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY; |
| 217 | } |
| 218 | else |
| 219 | log2_page_sz = CLIB_MEM_PAGE_SZ_UNKNOWN; |
| 220 | |
| 221 | if (is_locked) |
| 222 | flags |= CLIB_MEM_HEAP_F_LOCKED; |
| 223 | |
| 224 | h = base; |
| 225 | h->base = base; |
| 226 | h->size = size; |
| 227 | h->log2_page_sz = log2_page_sz; |
| 228 | h->flags = flags; |
| 229 | sz = strlen (name); |
| 230 | strcpy (h->name, name); |
| 231 | sz = round_pow2 (sz + sizeof (clib_mem_heap_t), 16); |
| 232 | h->mspace = create_mspace_with_base (base + sz, size - sz, is_locked); |
| 233 | |
| 234 | mspace_disable_expand (h->mspace); |
| 235 | |
| 236 | CLIB_MEM_POISON (mspace_least_addr (h->mspace), |
| 237 | mspace_footprint (h->mspace)); |
| 238 | |
| 239 | return h; |
| 240 | } |
| 241 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 242 | /* Initialize CLIB heap based on memory/size given by user. |
| 243 | 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] | 244 | static void * |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 245 | clib_mem_init_internal (void *base, uword size, |
| 246 | clib_mem_page_sz_t log2_page_sz) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 247 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 248 | clib_mem_heap_t *h; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 249 | |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 250 | clib_mem_main_init (); |
| 251 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 252 | h = clib_mem_create_heap_internal (base, size, log2_page_sz, |
| 253 | 1 /*is_locked */ , "main heap"); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 254 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 255 | clib_mem_set_heap (h); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 256 | |
| 257 | if (mheap_trace_main.lock == 0) |
| 258 | clib_spinlock_init (&mheap_trace_main.lock); |
| 259 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 260 | return h; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 261 | } |
| 262 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 263 | __clib_export void * |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 264 | clib_mem_init (void *memory, uword memory_size) |
| 265 | { |
| 266 | return clib_mem_init_internal (memory, memory_size, |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 267 | CLIB_MEM_PAGE_SZ_DEFAULT); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 270 | __clib_export void * |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 271 | clib_mem_init_with_page_size (uword memory_size, |
| 272 | clib_mem_page_sz_t log2_page_sz) |
| 273 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 274 | return clib_mem_init_internal (0, memory_size, log2_page_sz); |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 275 | } |
| 276 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 277 | __clib_export void * |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 278 | clib_mem_init_thread_safe (void *memory, uword memory_size) |
| 279 | { |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 280 | return clib_mem_init_internal (memory, memory_size, |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 281 | CLIB_MEM_PAGE_SZ_DEFAULT); |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 284 | __clib_export void |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 285 | clib_mem_destroy (void) |
| 286 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 287 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 288 | clib_mem_heap_t *heap = clib_mem_get_heap (); |
| 289 | void *base = mspace_least_addr (heap->mspace); |
| 290 | |
| 291 | if (tm->enabled && heap->mspace == tm->current_traced_mheap) |
| 292 | tm->enabled = 0; |
| 293 | |
| 294 | destroy_mspace (heap->mspace); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 295 | clib_mem_vm_unmap (base); |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 298 | u8 * |
| 299 | format_clib_mem_usage (u8 * s, va_list * va) |
| 300 | { |
| 301 | int verbose = va_arg (*va, int); |
| 302 | return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (), |
| 303 | verbose); |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Magic decoder ring for mallinfo stats (ala dlmalloc): |
| 308 | * |
| 309 | * size_t arena; / * Non-mmapped space allocated (bytes) * / |
| 310 | * size_t ordblks; / * Number of free chunks * / |
| 311 | * size_t smblks; / * Number of free fastbin blocks * / |
| 312 | * size_t hblks; / * Number of mmapped regions * / |
| 313 | * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * / |
| 314 | * size_t usmblks; / * Maximum total allocated space (bytes) * / |
| 315 | * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * / |
| 316 | * size_t uordblks; / * Total allocated space (bytes) * / |
| 317 | * size_t fordblks; / * Total free space (bytes) * / |
| 318 | * size_t keepcost; / * Top-most, releasable space (bytes) * / |
| 319 | * |
| 320 | */ |
| 321 | |
| 322 | u8 * |
| 323 | format_msize (u8 * s, va_list * va) |
| 324 | { |
| 325 | uword a = va_arg (*va, uword); |
| 326 | |
| 327 | if (a >= 1ULL << 30) |
| 328 | s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30)))); |
| 329 | else if (a >= 1ULL << 20) |
| 330 | s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20)))); |
| 331 | else if (a >= 1ULL << 10) |
| 332 | s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10)))); |
| 333 | else |
| 334 | s = format (s, "%lld", a); |
| 335 | return s; |
| 336 | } |
| 337 | |
| 338 | static int |
| 339 | mheap_trace_sort (const void *_t1, const void *_t2) |
| 340 | { |
| 341 | const mheap_trace_t *t1 = _t1; |
| 342 | const mheap_trace_t *t2 = _t2; |
| 343 | word cmp; |
| 344 | |
| 345 | cmp = (word) t2->n_bytes - (word) t1->n_bytes; |
| 346 | if (!cmp) |
| 347 | cmp = (word) t2->n_allocations - (word) t1->n_allocations; |
| 348 | return cmp; |
| 349 | } |
| 350 | |
| 351 | u8 * |
| 352 | format_mheap_trace (u8 * s, va_list * va) |
| 353 | { |
| 354 | mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *); |
| 355 | int verbose = va_arg (*va, int); |
| 356 | int have_traces = 0; |
| 357 | int i; |
| 358 | |
| 359 | clib_spinlock_lock (&tm->lock); |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 360 | if (vec_len (tm->traces) > 0 && |
| 361 | clib_mem_get_heap () == tm->current_traced_mheap) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 362 | { |
| 363 | have_traces = 1; |
| 364 | |
| 365 | /* Make a copy of traces since we'll be sorting them. */ |
| 366 | mheap_trace_t *t, *traces_copy; |
| 367 | u32 indent, total_objects_traced; |
| 368 | |
| 369 | traces_copy = vec_dup (tm->traces); |
| 370 | |
| 371 | qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]), |
| 372 | mheap_trace_sort); |
| 373 | |
| 374 | total_objects_traced = 0; |
| 375 | s = format (s, "\n"); |
| 376 | vec_foreach (t, traces_copy) |
| 377 | { |
| 378 | /* Skip over free elements. */ |
| 379 | if (t->n_allocations == 0) |
| 380 | continue; |
| 381 | |
| 382 | total_objects_traced += t->n_allocations; |
| 383 | |
| 384 | /* When not verbose only report allocations of more than 1k. */ |
| 385 | if (!verbose && t->n_bytes < 1024) |
| 386 | continue; |
| 387 | |
| 388 | if (t == traces_copy) |
| 389 | s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count", |
| 390 | "Sample"); |
| 391 | s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset); |
| 392 | indent = format_get_indent (s); |
| 393 | for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++) |
| 394 | { |
| 395 | if (i > 0) |
| 396 | s = format (s, "%U", format_white_space, indent); |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 397 | #if defined(CLIB_UNIX) && !defined(__APPLE__) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 398 | /* $$$$ does this actually work? */ |
| 399 | s = |
| 400 | format (s, " %U\n", format_clib_elf_symbol_with_address, |
| 401 | t->callers[i]); |
| 402 | #else |
| 403 | s = format (s, " %p\n", t->callers[i]); |
| 404 | #endif |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | s = format (s, "%d total traced objects\n", total_objects_traced); |
| 409 | |
| 410 | vec_free (traces_copy); |
| 411 | } |
| 412 | clib_spinlock_unlock (&tm->lock); |
| 413 | if (have_traces == 0) |
| 414 | s = format (s, "no traced allocations\n"); |
| 415 | |
| 416 | return s; |
| 417 | } |
| 418 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 419 | __clib_export u8 * |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 420 | format_clib_mem_heap (u8 * s, va_list * va) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 421 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 422 | clib_mem_heap_t *heap = va_arg (*va, clib_mem_heap_t *); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 423 | int verbose = va_arg (*va, int); |
Dave Barach | af7dd5b | 2018-08-23 17:08:44 -0400 | [diff] [blame] | 424 | struct dlmallinfo mi; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 425 | mheap_trace_main_t *tm = &mheap_trace_main; |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 426 | u32 indent = format_get_indent (s) + 2; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 427 | |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 428 | if (heap == 0) |
| 429 | heap = clib_mem_get_heap (); |
| 430 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 431 | mi = mspace_mallinfo (heap->mspace); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 432 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 433 | s = format (s, "base %p, size %U", |
| 434 | heap->base, format_memory_size, heap->size); |
| 435 | |
| 436 | #define _(i,v,str) \ |
| 437 | if (heap->flags & CLIB_MEM_HEAP_F_##v) s = format (s, ", %s", str); |
| 438 | foreach_clib_mem_heap_flag; |
| 439 | #undef _ |
| 440 | |
| 441 | s = format (s, ", name '%s'", heap->name); |
| 442 | |
| 443 | if (heap->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN) |
| 444 | { |
| 445 | clib_mem_page_stats_t stats; |
| 446 | clib_mem_get_page_stats (heap->base, heap->log2_page_sz, |
| 447 | heap->size >> heap->log2_page_sz, &stats); |
| 448 | s = format (s, "\n%U%U", format_white_space, indent, |
| 449 | format_clib_mem_page_stats, &stats); |
| 450 | } |
| 451 | |
| 452 | s = format (s, "\n%Utotal: %U, used: %U, free: %U, trimmable: %U", |
| 453 | format_white_space, indent, |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 454 | format_msize, mi.arena, |
| 455 | format_msize, mi.uordblks, |
| 456 | format_msize, mi.fordblks, format_msize, mi.keepcost); |
| 457 | if (verbose > 0) |
| 458 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 459 | s = format (s, "\n%Ufree chunks %llu free fastbin blks %llu", |
| 460 | format_white_space, indent + 2, mi.ordblks, mi.smblks); |
| 461 | s = format (s, "\n%Umax total allocated %U", |
| 462 | format_white_space, indent + 2, format_msize, mi.usmblks); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 463 | } |
| 464 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 465 | if (mspace_is_traced (heap->mspace)) |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 466 | s = format (s, "\n%U", format_mheap_trace, tm, verbose); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 467 | return s; |
| 468 | } |
| 469 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 470 | __clib_export void |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 471 | 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] | 472 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 473 | struct dlmallinfo mi = mspace_mallinfo (heap->mspace); |
Ole Troan | 92e3082 | 2019-06-16 12:33:51 +0200 | [diff] [blame] | 474 | |
| 475 | /* TODO: Fill in some more values */ |
| 476 | usage->object_count = 0; |
| 477 | usage->bytes_total = mi.arena; |
| 478 | usage->bytes_overhead = 0; |
| 479 | usage->bytes_max = 0; |
| 480 | usage->bytes_used = mi.uordblks; |
| 481 | usage->bytes_free = mi.fordblks; |
| 482 | usage->bytes_free_reclaimed = 0; |
| 483 | } |
| 484 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 485 | /* Call serial number for debugger breakpoints. */ |
| 486 | uword clib_mem_validate_serial = 0; |
| 487 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 488 | __clib_export void |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 489 | mheap_trace (clib_mem_heap_t * h, int enable) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 490 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 491 | (void) mspace_enable_disable_trace (h->mspace, enable); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 492 | |
| 493 | if (enable == 0) |
| 494 | mheap_trace_main_free (&mheap_trace_main); |
| 495 | } |
| 496 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 497 | __clib_export void |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 498 | clib_mem_trace (int enable) |
| 499 | { |
| 500 | mheap_trace_main_t *tm = &mheap_trace_main; |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 501 | void *current_heap = clib_mem_get_heap (); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 502 | |
| 503 | tm->enabled = enable; |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 504 | mheap_trace (current_heap, enable); |
| 505 | |
| 506 | if (enable) |
| 507 | tm->current_traced_mheap = current_heap; |
| 508 | else |
| 509 | tm->current_traced_mheap = 0; |
| 510 | } |
| 511 | |
| 512 | int |
| 513 | clib_mem_is_traced (void) |
| 514 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 515 | clib_mem_heap_t *h = clib_mem_get_heap (); |
| 516 | return mspace_is_traced (h->mspace); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 519 | __clib_export uword |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 520 | clib_mem_trace_enable_disable (uword enable) |
| 521 | { |
| 522 | uword rv; |
| 523 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 524 | |
| 525 | rv = tm->enabled; |
| 526 | tm->enabled = enable; |
| 527 | return rv; |
| 528 | } |
| 529 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 530 | __clib_export clib_mem_heap_t * |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 531 | 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] | 532 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 533 | clib_mem_page_sz_t log2_page_sz = clib_mem_get_log2_page_size (); |
| 534 | clib_mem_heap_t *h; |
| 535 | char *name; |
| 536 | u8 *s = 0; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 537 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 538 | if (fmt == 0) |
| 539 | { |
| 540 | name = ""; |
| 541 | } |
| 542 | else if (strchr (fmt, '%')) |
| 543 | { |
| 544 | va_list va; |
| 545 | va_start (va, fmt); |
| 546 | s = va_format (0, fmt, &va); |
| 547 | vec_add1 (s, 0); |
| 548 | va_end (va); |
| 549 | name = (char *) s; |
| 550 | } |
| 551 | else |
| 552 | name = fmt; |
| 553 | |
| 554 | h = clib_mem_create_heap_internal (base, size, log2_page_sz, is_locked, |
| 555 | name); |
| 556 | vec_free (s); |
| 557 | return h; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 558 | } |
| 559 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 560 | __clib_export void |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 561 | clib_mem_destroy_heap (clib_mem_heap_t * h) |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 562 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 563 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 564 | |
| 565 | if (tm->enabled && h->mspace == tm->current_traced_mheap) |
| 566 | tm->enabled = 0; |
| 567 | |
| 568 | destroy_mspace (h->mspace); |
| 569 | if (h->flags & CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY) |
| 570 | clib_mem_vm_unmap (h->base); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 571 | } |
| 572 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 573 | __clib_export uword |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 574 | clib_mem_get_heap_free_space (clib_mem_heap_t * h) |
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 | struct dlmallinfo dlminfo = mspace_mallinfo (h->mspace); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 577 | return dlminfo.fordblks; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 578 | } |
| 579 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 580 | __clib_export void * |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 581 | clib_mem_get_heap_base (clib_mem_heap_t * h) |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 582 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 583 | return h->base; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 584 | } |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 585 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 586 | __clib_export uword |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 587 | clib_mem_get_heap_size (clib_mem_heap_t * heap) |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 588 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 589 | return heap->size; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 592 | /* |
| 593 | * fd.io coding-style-patch-verification: ON |
| 594 | * |
| 595 | * Local Variables: |
| 596 | * eval: (c-set-style "gnu") |
| 597 | * End: |
| 598 | */ |