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>
24 void *clib_per_cpu_mheaps[CLIB_MAX_MHEAPS];
28 /* Address of callers: outer first, inner last. */
31 /* Count of allocations with this traceback. */
38 /* Count of bytes allocated with this traceback. */
41 /* Offset of this item */
50 mheap_trace_t *traces;
52 /* Indices of free traces. */
55 /* Hash table mapping callers to trace index. */
56 uword *trace_by_callers;
58 /* Hash table mapping mheap offset to trace index. */
59 uword *trace_index_by_offset;
61 /* So we can easily shut off current segment trace, if any */
62 void *current_traced_mheap;
66 mheap_trace_main_t mheap_trace_main;
69 mheap_get_trace (uword offset, uword size)
71 mheap_trace_main_t *tm = &mheap_trace_main;
73 uword i, n_callers, trace_index, *p;
77 if (tm->enabled == 0 || (clib_mem_get_heap () != tm->current_traced_mheap))
80 /* Spurious Coverity warnings be gone. */
81 clib_memset (&trace, 0, sizeof (trace));
83 /* Skip our frame and mspace_get_aligned's frame */
84 n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2);
88 clib_spinlock_lock (&tm->lock);
90 /* Turn off tracing to avoid embarrassment... */
91 save_enabled = tm->enabled;
94 if (!tm->trace_by_callers)
95 tm->trace_by_callers =
96 hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
98 p = hash_get_mem (tm->trace_by_callers, &trace.callers);
102 t = tm->traces + trace_index;
106 i = vec_len (tm->trace_free_list);
109 trace_index = tm->trace_free_list[i - 1];
110 _vec_len (tm->trace_free_list) = i - 1;
114 mheap_trace_t *old_start = tm->traces;
115 mheap_trace_t *old_end = vec_end (tm->traces);
117 vec_add2 (tm->traces, t, 1);
119 if (tm->traces != old_start)
124 hash_foreach_pair (p, tm->trace_by_callers,
126 q = uword_to_pointer (p->key, mheap_trace_t *);
127 ASSERT (q >= old_start && q < old_end);
128 p->key = pointer_to_uword (tm->traces + (q - old_start));
132 trace_index = t - tm->traces;
135 t = tm->traces + trace_index;
137 t->n_allocations = 0;
139 hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
142 t->n_allocations += 1;
144 t->offset = offset; /* keep a sample to autopsy */
145 hash_set (tm->trace_index_by_offset, offset, t - tm->traces);
146 tm->enabled = save_enabled;
147 clib_spinlock_unlock (&tm->lock);
151 mheap_put_trace (uword offset, uword size)
154 uword trace_index, *p;
155 mheap_trace_main_t *tm = &mheap_trace_main;
158 if (tm->enabled == 0)
161 clib_spinlock_lock (&tm->lock);
163 /* Turn off tracing for a moment */
164 save_enabled = tm->enabled;
167 p = hash_get (tm->trace_index_by_offset, offset);
170 tm->enabled = save_enabled;
171 clib_spinlock_unlock (&tm->lock);
176 hash_unset (tm->trace_index_by_offset, offset);
177 ASSERT (trace_index < vec_len (tm->traces));
179 t = tm->traces + trace_index;
180 ASSERT (t->n_allocations > 0);
181 ASSERT (t->n_bytes >= size);
182 t->n_allocations -= 1;
184 if (t->n_allocations == 0)
186 hash_unset_mem (tm->trace_by_callers, t->callers);
187 vec_add1 (tm->trace_free_list, trace_index);
188 clib_memset (t, 0, sizeof (t[0]));
190 tm->enabled = save_enabled;
191 clib_spinlock_unlock (&tm->lock);
195 mheap_trace_main_free (mheap_trace_main_t * tm)
197 vec_free (tm->traces);
198 vec_free (tm->trace_free_list);
199 hash_free (tm->trace_by_callers);
200 hash_free (tm->trace_index_by_offset);
203 /* Initialize CLIB heap based on memory/size given by user.
204 Set memory to 0 and CLIB will try to allocate its own heap. */
206 clib_mem_init (void *memory, uword memory_size)
212 heap = create_mspace_with_base (memory, memory_size, 1 /* locked */ );
213 mspace_disable_expand (heap);
216 heap = create_mspace (memory_size, 1 /* locked */ );
218 clib_mem_set_heap (heap);
220 if (mheap_trace_main.lock == 0)
221 clib_spinlock_init (&mheap_trace_main.lock);
223 CLIB_MEM_POISON (mspace_least_addr (heap), mspace_footprint (heap));
228 clib_mem_init_thread_safe (void *memory, uword memory_size)
230 return clib_mem_init (memory, memory_size);
234 format_clib_mem_usage (u8 * s, va_list * va)
236 int verbose = va_arg (*va, int);
237 return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
242 * Magic decoder ring for mallinfo stats (ala dlmalloc):
244 * size_t arena; / * Non-mmapped space allocated (bytes) * /
245 * size_t ordblks; / * Number of free chunks * /
246 * size_t smblks; / * Number of free fastbin blocks * /
247 * size_t hblks; / * Number of mmapped regions * /
248 * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * /
249 * size_t usmblks; / * Maximum total allocated space (bytes) * /
250 * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * /
251 * size_t uordblks; / * Total allocated space (bytes) * /
252 * size_t fordblks; / * Total free space (bytes) * /
253 * size_t keepcost; / * Top-most, releasable space (bytes) * /
258 format_msize (u8 * s, va_list * va)
260 uword a = va_arg (*va, uword);
263 s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30))));
264 else if (a >= 1ULL << 20)
265 s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20))));
266 else if (a >= 1ULL << 10)
267 s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10))));
269 s = format (s, "%lld", a);
274 mheap_trace_sort (const void *_t1, const void *_t2)
276 const mheap_trace_t *t1 = _t1;
277 const mheap_trace_t *t2 = _t2;
280 cmp = (word) t2->n_bytes - (word) t1->n_bytes;
282 cmp = (word) t2->n_allocations - (word) t1->n_allocations;
287 format_mheap_trace (u8 * s, va_list * va)
289 mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
290 int verbose = va_arg (*va, int);
294 clib_spinlock_lock (&tm->lock);
295 if (vec_len (tm->traces) > 0 &&
296 clib_mem_get_heap () == tm->current_traced_mheap)
300 /* Make a copy of traces since we'll be sorting them. */
301 mheap_trace_t *t, *traces_copy;
302 u32 indent, total_objects_traced;
304 traces_copy = vec_dup (tm->traces);
306 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
309 total_objects_traced = 0;
310 s = format (s, "\n");
311 vec_foreach (t, traces_copy)
313 /* Skip over free elements. */
314 if (t->n_allocations == 0)
317 total_objects_traced += t->n_allocations;
319 /* When not verbose only report allocations of more than 1k. */
320 if (!verbose && t->n_bytes < 1024)
323 if (t == traces_copy)
324 s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
326 s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset);
327 indent = format_get_indent (s);
328 for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++)
331 s = format (s, "%U", format_white_space, indent);
332 #if defined(CLIB_UNIX) && !defined(__APPLE__)
333 /* $$$$ does this actually work? */
335 format (s, " %U\n", format_clib_elf_symbol_with_address,
338 s = format (s, " %p\n", t->callers[i]);
343 s = format (s, "%d total traced objects\n", total_objects_traced);
345 vec_free (traces_copy);
347 clib_spinlock_unlock (&tm->lock);
348 if (have_traces == 0)
349 s = format (s, "no traced allocations\n");
356 format_mheap (u8 * s, va_list * va)
358 void *heap = va_arg (*va, u8 *);
359 int verbose = va_arg (*va, int);
360 struct dlmallinfo mi;
361 mheap_trace_main_t *tm = &mheap_trace_main;
363 mi = mspace_mallinfo (heap);
365 s = format (s, "total: %U, used: %U, free: %U, trimmable: %U",
366 format_msize, mi.arena,
367 format_msize, mi.uordblks,
368 format_msize, mi.fordblks, format_msize, mi.keepcost);
371 s = format (s, "\n free chunks %llu free fastbin blks %llu",
372 mi.ordblks, mi.smblks);
374 format (s, "\n max total allocated %U", format_msize, mi.usmblks);
377 if (mspace_is_traced (heap))
378 s = format (s, "\n%U", format_mheap_trace, tm, verbose);
383 clib_mem_usage (clib_mem_usage_t * u)
385 clib_warning ("unimp");
389 mheap_usage (void *heap, clib_mem_usage_t * usage)
391 struct dlmallinfo mi = mspace_mallinfo (heap);
393 /* TODO: Fill in some more values */
394 usage->object_count = 0;
395 usage->bytes_total = mi.arena;
396 usage->bytes_overhead = 0;
397 usage->bytes_max = 0;
398 usage->bytes_used = mi.uordblks;
399 usage->bytes_free = mi.fordblks;
400 usage->bytes_free_reclaimed = 0;
403 /* Call serial number for debugger breakpoints. */
404 uword clib_mem_validate_serial = 0;
407 clib_mem_validate (void)
409 clib_warning ("unimp");
413 mheap_trace (void *v, int enable)
415 (void) mspace_enable_disable_trace (v, enable);
418 mheap_trace_main_free (&mheap_trace_main);
422 clib_mem_trace (int enable)
424 mheap_trace_main_t *tm = &mheap_trace_main;
425 void *current_heap = clib_mem_get_heap ();
427 tm->enabled = enable;
428 mheap_trace (current_heap, enable);
431 tm->current_traced_mheap = current_heap;
433 tm->current_traced_mheap = 0;
437 clib_mem_is_traced (void)
439 return mspace_is_traced (clib_mem_get_heap ());
443 clib_mem_trace_enable_disable (uword enable)
446 mheap_trace_main_t *tm = &mheap_trace_main;
449 tm->enabled = enable;
454 * These API functions seem like layering violations, but
455 * by introducing them we greatly reduce the number
456 * of code changes required to use dlmalloc spaces
459 mheap_alloc_with_lock (void *memory, uword size, int locked)
463 return create_mspace (size, locked);
466 rv = create_mspace_with_base (memory, size, locked);
468 mspace_disable_expand (rv);
474 * fd.io coding-style-patch-verification: ON
477 * eval: (c-set-style "gnu")