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>
23 void *clib_per_cpu_mheaps[CLIB_MAX_MHEAPS];
27 /* Address of callers: outer first, inner last. */
30 /* Count of allocations with this traceback. */
37 /* Count of bytes allocated with this traceback. */
40 /* Offset of this item */
49 mheap_trace_t *traces;
51 /* Indices of free traces. */
54 /* Hash table mapping callers to trace index. */
55 uword *trace_by_callers;
57 /* Hash table mapping mheap offset to trace index. */
58 uword *trace_index_by_offset;
60 /* So we can easily shut off current segment trace, if any */
61 void *current_traced_mheap;
65 mheap_trace_main_t mheap_trace_main;
68 mheap_get_trace (uword offset, uword size)
70 mheap_trace_main_t *tm = &mheap_trace_main;
72 uword i, n_callers, trace_index, *p;
76 if (tm->enabled == 0 || (clib_mem_get_heap () != tm->current_traced_mheap))
79 /* Spurious Coverity warnings be gone. */
80 clib_memset (&trace, 0, sizeof (trace));
82 /* Skip our frame and mspace_get_aligned's frame */
83 n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2);
87 clib_spinlock_lock (&tm->lock);
89 /* Turn off tracing to avoid embarrassment... */
90 save_enabled = tm->enabled;
93 if (!tm->trace_by_callers)
94 tm->trace_by_callers =
95 hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
97 p = hash_get_mem (tm->trace_by_callers, &trace.callers);
101 t = tm->traces + trace_index;
105 i = vec_len (tm->trace_free_list);
108 trace_index = tm->trace_free_list[i - 1];
109 _vec_len (tm->trace_free_list) = i - 1;
113 mheap_trace_t *old_start = tm->traces;
114 mheap_trace_t *old_end = vec_end (tm->traces);
116 vec_add2 (tm->traces, t, 1);
118 if (tm->traces != old_start)
123 hash_foreach_pair (p, tm->trace_by_callers,
125 q = uword_to_pointer (p->key, mheap_trace_t *);
126 ASSERT (q >= old_start && q < old_end);
127 p->key = pointer_to_uword (tm->traces + (q - old_start));
131 trace_index = t - tm->traces;
134 t = tm->traces + trace_index;
136 t->n_allocations = 0;
138 hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
141 t->n_allocations += 1;
143 t->offset = offset; /* keep a sample to autopsy */
144 hash_set (tm->trace_index_by_offset, offset, t - tm->traces);
145 tm->enabled = save_enabled;
146 clib_spinlock_unlock (&tm->lock);
150 mheap_put_trace (uword offset, uword size)
153 uword trace_index, *p;
154 mheap_trace_main_t *tm = &mheap_trace_main;
157 if (tm->enabled == 0)
160 clib_spinlock_lock (&tm->lock);
162 /* Turn off tracing for a moment */
163 save_enabled = tm->enabled;
166 p = hash_get (tm->trace_index_by_offset, offset);
169 tm->enabled = save_enabled;
170 clib_spinlock_unlock (&tm->lock);
175 hash_unset (tm->trace_index_by_offset, offset);
176 ASSERT (trace_index < vec_len (tm->traces));
178 t = tm->traces + trace_index;
179 ASSERT (t->n_allocations > 0);
180 ASSERT (t->n_bytes >= size);
181 t->n_allocations -= 1;
183 if (t->n_allocations == 0)
185 hash_unset_mem (tm->trace_by_callers, t->callers);
186 vec_add1 (tm->trace_free_list, trace_index);
187 clib_memset (t, 0, sizeof (t[0]));
189 tm->enabled = save_enabled;
190 clib_spinlock_unlock (&tm->lock);
194 mheap_trace_main_free (mheap_trace_main_t * tm)
196 vec_free (tm->traces);
197 vec_free (tm->trace_free_list);
198 hash_free (tm->trace_by_callers);
199 hash_free (tm->trace_index_by_offset);
202 /* Initialize CLIB heap based on memory/size given by user.
203 Set memory to 0 and CLIB will try to allocate its own heap. */
205 clib_mem_init (void *memory, uword memory_size)
211 heap = create_mspace_with_base (memory, memory_size, 1 /* locked */ );
212 mspace_disable_expand (heap);
215 heap = create_mspace (memory_size, 1 /* locked */ );
217 clib_mem_set_heap (heap);
219 if (mheap_trace_main.lock == 0)
220 clib_spinlock_init (&mheap_trace_main.lock);
226 clib_mem_init_thread_safe (void *memory, uword memory_size)
228 return clib_mem_init (memory, memory_size);
232 format_clib_mem_usage (u8 * s, va_list * va)
234 int verbose = va_arg (*va, int);
235 return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
240 * Magic decoder ring for mallinfo stats (ala dlmalloc):
242 * size_t arena; / * Non-mmapped space allocated (bytes) * /
243 * size_t ordblks; / * Number of free chunks * /
244 * size_t smblks; / * Number of free fastbin blocks * /
245 * size_t hblks; / * Number of mmapped regions * /
246 * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * /
247 * size_t usmblks; / * Maximum total allocated space (bytes) * /
248 * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * /
249 * size_t uordblks; / * Total allocated space (bytes) * /
250 * size_t fordblks; / * Total free space (bytes) * /
251 * size_t keepcost; / * Top-most, releasable space (bytes) * /
256 format_msize (u8 * s, va_list * va)
258 uword a = va_arg (*va, uword);
261 s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30))));
262 else if (a >= 1ULL << 20)
263 s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20))));
264 else if (a >= 1ULL << 10)
265 s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10))));
267 s = format (s, "%lld", a);
272 mheap_trace_sort (const void *_t1, const void *_t2)
274 const mheap_trace_t *t1 = _t1;
275 const mheap_trace_t *t2 = _t2;
278 cmp = (word) t2->n_bytes - (word) t1->n_bytes;
280 cmp = (word) t2->n_allocations - (word) t1->n_allocations;
285 format_mheap_trace (u8 * s, va_list * va)
287 mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
288 int verbose = va_arg (*va, int);
292 clib_spinlock_lock (&tm->lock);
293 if (vec_len (tm->traces) > 0 &&
294 clib_mem_get_heap () == tm->current_traced_mheap)
298 /* Make a copy of traces since we'll be sorting them. */
299 mheap_trace_t *t, *traces_copy;
300 u32 indent, total_objects_traced;
302 traces_copy = vec_dup (tm->traces);
304 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
307 total_objects_traced = 0;
308 s = format (s, "\n");
309 vec_foreach (t, traces_copy)
311 /* Skip over free elements. */
312 if (t->n_allocations == 0)
315 total_objects_traced += t->n_allocations;
317 /* When not verbose only report allocations of more than 1k. */
318 if (!verbose && t->n_bytes < 1024)
321 if (t == traces_copy)
322 s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
324 s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset);
325 indent = format_get_indent (s);
326 for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++)
329 s = format (s, "%U", format_white_space, indent);
330 #if defined(CLIB_UNIX) && !defined(__APPLE__)
331 /* $$$$ does this actually work? */
333 format (s, " %U\n", format_clib_elf_symbol_with_address,
336 s = format (s, " %p\n", t->callers[i]);
341 s = format (s, "%d total traced objects\n", total_objects_traced);
343 vec_free (traces_copy);
345 clib_spinlock_unlock (&tm->lock);
346 if (have_traces == 0)
347 s = format (s, "no traced allocations\n");
354 format_mheap (u8 * s, va_list * va)
356 void *heap = va_arg (*va, u8 *);
357 int verbose = va_arg (*va, int);
358 struct dlmallinfo mi;
359 mheap_trace_main_t *tm = &mheap_trace_main;
361 mi = mspace_mallinfo (heap);
363 s = format (s, "total: %U, used: %U, free: %U, trimmable: %U",
364 format_msize, mi.arena,
365 format_msize, mi.uordblks,
366 format_msize, mi.fordblks, format_msize, mi.keepcost);
369 s = format (s, "\n free chunks %llu free fastbin blks %llu",
370 mi.ordblks, mi.smblks);
372 format (s, "\n max total allocated %U", format_msize, mi.usmblks);
375 if (mspace_is_traced (heap))
376 s = format (s, "\n%U", format_mheap_trace, tm, verbose);
381 clib_mem_usage (clib_mem_usage_t * u)
383 clib_warning ("unimp");
386 /* Call serial number for debugger breakpoints. */
387 uword clib_mem_validate_serial = 0;
390 clib_mem_validate (void)
392 clib_warning ("unimp");
396 mheap_trace (void *v, int enable)
398 (void) mspace_enable_disable_trace (v, enable);
401 mheap_trace_main_free (&mheap_trace_main);
405 clib_mem_trace (int enable)
407 mheap_trace_main_t *tm = &mheap_trace_main;
408 void *current_heap = clib_mem_get_heap ();
410 tm->enabled = enable;
411 mheap_trace (current_heap, enable);
414 tm->current_traced_mheap = current_heap;
416 tm->current_traced_mheap = 0;
420 clib_mem_is_traced (void)
422 return mspace_is_traced (clib_mem_get_heap ());
426 clib_mem_trace_enable_disable (uword enable)
429 mheap_trace_main_t *tm = &mheap_trace_main;
432 tm->enabled = enable;
437 * These API functions seem like layering violations, but
438 * by introducing them we greatly reduce the number
439 * of code changes required to use dlmalloc spaces
442 mheap_alloc_with_lock (void *memory, uword size, int locked)
446 return create_mspace (size, locked);
449 rv = create_mspace_with_base (memory, size, locked);
451 mspace_disable_expand (rv);
457 * fd.io coding-style-patch-verification: ON
460 * eval: (c-set-style "gnu")