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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 #include <vppinfra/sanitizer.h>
25 void *clib_per_cpu_mheaps[CLIB_MAX_MHEAPS];
26 void *clib_per_numa_mheaps[CLIB_MAX_NUMAS];
30 /* Address of callers: outer first, inner last. */
33 /* Count of allocations with this traceback. */
36 /* Count of bytes allocated with this traceback. */
39 /* Offset of this item */
48 mheap_trace_t *traces;
50 /* Indices of free traces. */
53 /* Hash table mapping callers to trace index. */
54 uword *trace_by_callers;
56 /* Hash table mapping mheap offset to trace index. */
57 uword *trace_index_by_offset;
59 /* So we can easily shut off current segment trace, if any */
60 void *current_traced_mheap;
64 mheap_trace_main_t mheap_trace_main;
67 mheap_get_trace (uword offset, uword size)
69 mheap_trace_main_t *tm = &mheap_trace_main;
71 uword i, n_callers, trace_index, *p;
75 if (tm->enabled == 0 || (clib_mem_get_heap () != tm->current_traced_mheap))
78 /* Spurious Coverity warnings be gone. */
79 clib_memset (&trace, 0, sizeof (trace));
81 /* Skip our frame and mspace_get_aligned's frame */
82 n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2);
86 clib_spinlock_lock (&tm->lock);
88 /* Turn off tracing to avoid embarrassment... */
89 save_enabled = tm->enabled;
92 if (!tm->trace_by_callers)
93 tm->trace_by_callers =
94 hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
96 p = hash_get_mem (tm->trace_by_callers, &trace.callers);
100 t = tm->traces + trace_index;
104 i = vec_len (tm->trace_free_list);
107 trace_index = tm->trace_free_list[i - 1];
108 _vec_len (tm->trace_free_list) = i - 1;
112 mheap_trace_t *old_start = tm->traces;
113 mheap_trace_t *old_end = vec_end (tm->traces);
115 vec_add2 (tm->traces, t, 1);
117 if (tm->traces != old_start)
122 hash_foreach_pair (p, tm->trace_by_callers,
124 q = uword_to_pointer (p->key, mheap_trace_t *);
125 ASSERT (q >= old_start && q < old_end);
126 p->key = pointer_to_uword (tm->traces + (q - old_start));
130 trace_index = t - tm->traces;
133 t = tm->traces + trace_index;
135 t->n_allocations = 0;
137 hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
140 t->n_allocations += 1;
142 t->offset = offset; /* keep a sample to autopsy */
143 hash_set (tm->trace_index_by_offset, offset, t - tm->traces);
144 tm->enabled = save_enabled;
145 clib_spinlock_unlock (&tm->lock);
149 mheap_put_trace (uword offset, uword size)
152 uword trace_index, *p;
153 mheap_trace_main_t *tm = &mheap_trace_main;
156 if (tm->enabled == 0)
159 clib_spinlock_lock (&tm->lock);
161 /* Turn off tracing for a moment */
162 save_enabled = tm->enabled;
165 p = hash_get (tm->trace_index_by_offset, offset);
168 tm->enabled = save_enabled;
169 clib_spinlock_unlock (&tm->lock);
174 hash_unset (tm->trace_index_by_offset, offset);
175 ASSERT (trace_index < vec_len (tm->traces));
177 t = tm->traces + trace_index;
178 ASSERT (t->n_allocations > 0);
179 ASSERT (t->n_bytes >= size);
180 t->n_allocations -= 1;
182 if (t->n_allocations == 0)
184 hash_unset_mem (tm->trace_by_callers, t->callers);
185 vec_add1 (tm->trace_free_list, trace_index);
186 clib_memset (t, 0, sizeof (t[0]));
188 tm->enabled = save_enabled;
189 clib_spinlock_unlock (&tm->lock);
193 mheap_trace_main_free (mheap_trace_main_t * tm)
195 vec_free (tm->traces);
196 vec_free (tm->trace_free_list);
197 hash_free (tm->trace_by_callers);
198 hash_free (tm->trace_index_by_offset);
201 /* Initialize CLIB heap based on memory/size given by user.
202 Set memory to 0 and CLIB will try to allocate its own heap. */
204 clib_mem_init_internal (void *memory, uword memory_size, int set_heap)
210 heap = create_mspace_with_base (memory, memory_size, 1 /* locked */ );
211 mspace_disable_expand (heap);
214 heap = create_mspace (memory_size, 1 /* locked */ );
216 CLIB_MEM_POISON (mspace_least_addr (heap), mspace_footprint (heap));
219 clib_mem_set_heap (heap);
221 if (mheap_trace_main.lock == 0)
222 clib_spinlock_init (&mheap_trace_main.lock);
228 clib_mem_init (void *memory, uword memory_size)
230 return clib_mem_init_internal (memory, memory_size,
231 1 /* do clib_mem_set_heap */ );
235 clib_mem_init_thread_safe (void *memory, uword memory_size)
237 return clib_mem_init_internal (memory, memory_size,
238 1 /* do clib_mem_set_heap */ );
242 clib_mem_init_thread_safe_numa (void *memory, uword memory_size, u8 numa)
244 clib_mem_vm_alloc_t alloc = { 0 };
248 alloc.size = memory_size;
249 alloc.flags = CLIB_MEM_VM_F_NUMA_FORCE;
250 alloc.numa_node = numa;
251 if ((err = clib_mem_vm_ext_alloc (&alloc)))
253 clib_error_report (err);
257 heap = clib_mem_init_internal (memory, memory_size,
258 0 /* do NOT clib_mem_set_heap */ );
266 format_clib_mem_usage (u8 * s, va_list * va)
268 int verbose = va_arg (*va, int);
269 return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
274 * Magic decoder ring for mallinfo stats (ala dlmalloc):
276 * size_t arena; / * Non-mmapped space allocated (bytes) * /
277 * size_t ordblks; / * Number of free chunks * /
278 * size_t smblks; / * Number of free fastbin blocks * /
279 * size_t hblks; / * Number of mmapped regions * /
280 * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * /
281 * size_t usmblks; / * Maximum total allocated space (bytes) * /
282 * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * /
283 * size_t uordblks; / * Total allocated space (bytes) * /
284 * size_t fordblks; / * Total free space (bytes) * /
285 * size_t keepcost; / * Top-most, releasable space (bytes) * /
290 format_msize (u8 * s, va_list * va)
292 uword a = va_arg (*va, uword);
295 s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30))));
296 else if (a >= 1ULL << 20)
297 s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20))));
298 else if (a >= 1ULL << 10)
299 s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10))));
301 s = format (s, "%lld", a);
306 mheap_trace_sort (const void *_t1, const void *_t2)
308 const mheap_trace_t *t1 = _t1;
309 const mheap_trace_t *t2 = _t2;
312 cmp = (word) t2->n_bytes - (word) t1->n_bytes;
314 cmp = (word) t2->n_allocations - (word) t1->n_allocations;
319 format_mheap_trace (u8 * s, va_list * va)
321 mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
322 int verbose = va_arg (*va, int);
326 clib_spinlock_lock (&tm->lock);
327 if (vec_len (tm->traces) > 0 &&
328 clib_mem_get_heap () == tm->current_traced_mheap)
332 /* Make a copy of traces since we'll be sorting them. */
333 mheap_trace_t *t, *traces_copy;
334 u32 indent, total_objects_traced;
336 traces_copy = vec_dup (tm->traces);
338 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
341 total_objects_traced = 0;
342 s = format (s, "\n");
343 vec_foreach (t, traces_copy)
345 /* Skip over free elements. */
346 if (t->n_allocations == 0)
349 total_objects_traced += t->n_allocations;
351 /* When not verbose only report allocations of more than 1k. */
352 if (!verbose && t->n_bytes < 1024)
355 if (t == traces_copy)
356 s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
358 s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset);
359 indent = format_get_indent (s);
360 for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++)
363 s = format (s, "%U", format_white_space, indent);
364 #if defined(CLIB_UNIX) && !defined(__APPLE__)
365 /* $$$$ does this actually work? */
367 format (s, " %U\n", format_clib_elf_symbol_with_address,
370 s = format (s, " %p\n", t->callers[i]);
375 s = format (s, "%d total traced objects\n", total_objects_traced);
377 vec_free (traces_copy);
379 clib_spinlock_unlock (&tm->lock);
380 if (have_traces == 0)
381 s = format (s, "no traced allocations\n");
388 format_mheap (u8 * s, va_list * va)
390 void *heap = va_arg (*va, u8 *);
391 int verbose = va_arg (*va, int);
392 struct dlmallinfo mi;
393 mheap_trace_main_t *tm = &mheap_trace_main;
395 mi = mspace_mallinfo (heap);
397 s = format (s, "total: %U, used: %U, free: %U, trimmable: %U",
398 format_msize, mi.arena,
399 format_msize, mi.uordblks,
400 format_msize, mi.fordblks, format_msize, mi.keepcost);
403 s = format (s, "\n free chunks %llu free fastbin blks %llu",
404 mi.ordblks, mi.smblks);
406 format (s, "\n max total allocated %U", format_msize, mi.usmblks);
409 if (mspace_is_traced (heap))
410 s = format (s, "\n%U", format_mheap_trace, tm, verbose);
415 clib_mem_usage (clib_mem_usage_t * u)
417 clib_warning ("unimp");
421 mheap_usage (void *heap, clib_mem_usage_t * usage)
423 struct dlmallinfo mi = mspace_mallinfo (heap);
425 /* TODO: Fill in some more values */
426 usage->object_count = 0;
427 usage->bytes_total = mi.arena;
428 usage->bytes_overhead = 0;
429 usage->bytes_max = 0;
430 usage->bytes_used = mi.uordblks;
431 usage->bytes_free = mi.fordblks;
432 usage->bytes_free_reclaimed = 0;
435 /* Call serial number for debugger breakpoints. */
436 uword clib_mem_validate_serial = 0;
439 clib_mem_validate (void)
441 clib_warning ("unimp");
445 mheap_trace (void *v, int enable)
447 (void) mspace_enable_disable_trace (v, enable);
450 mheap_trace_main_free (&mheap_trace_main);
454 clib_mem_trace (int enable)
456 mheap_trace_main_t *tm = &mheap_trace_main;
457 void *current_heap = clib_mem_get_heap ();
459 tm->enabled = enable;
460 mheap_trace (current_heap, enable);
463 tm->current_traced_mheap = current_heap;
465 tm->current_traced_mheap = 0;
469 clib_mem_is_traced (void)
471 return mspace_is_traced (clib_mem_get_heap ());
475 clib_mem_trace_enable_disable (uword enable)
478 mheap_trace_main_t *tm = &mheap_trace_main;
481 tm->enabled = enable;
486 * These API functions seem like layering violations, but
487 * by introducing them we greatly reduce the number
488 * of code changes required to use dlmalloc spaces
491 mheap_alloc_with_lock (void *memory, uword size, int locked)
495 return create_mspace (size, locked);
498 rv = create_mspace_with_base (memory, size, locked);
500 mspace_disable_expand (rv);
506 * fd.io coding-style-patch-verification: ON
509 * eval: (c-set-style "gnu")