FD.io VPP  v19.04.2-12-g66b1689
Vector Packet Processing
mem_dlmalloc.c
Go to the documentation of this file.
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>
22 
24 
25 typedef struct
26 {
27  /* Address of callers: outer first, inner last. */
28  uword callers[12];
29 
30  /* Count of allocations with this traceback. */
31 #if CLIB_VEC64 > 0
32  u64 n_allocations;
33 #else
35 #endif
36 
37  /* Count of bytes allocated with this traceback. */
39 
40  /* Offset of this item */
43 
44 typedef struct
45 {
48 
50 
51  /* Indices of free traces. */
53 
54  /* Hash table mapping callers to trace index. */
56 
57  /* Hash table mapping mheap offset to trace index. */
59 
60  /* So we can easily shut off current segment trace, if any */
62 
64 
66 
67 void
69 {
71  mheap_trace_t *t;
72  uword i, n_callers, trace_index, *p;
74  uword save_enabled;
75 
76  if (tm->enabled == 0 || (clib_mem_get_heap () != tm->current_traced_mheap))
77  return;
78 
79  /* Spurious Coverity warnings be gone. */
80  clib_memset (&trace, 0, sizeof (trace));
81 
82  /* Skip our frame and mspace_get_aligned's frame */
83  n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2);
84  if (n_callers == 0)
85  return;
86 
87  clib_spinlock_lock (&tm->lock);
88 
89  /* Turn off tracing to avoid embarrassment... */
90  save_enabled = tm->enabled;
91  tm->enabled = 0;
92 
93  if (!tm->trace_by_callers)
94  tm->trace_by_callers =
95  hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
96 
97  p = hash_get_mem (tm->trace_by_callers, &trace.callers);
98  if (p)
99  {
100  trace_index = p[0];
101  t = tm->traces + trace_index;
102  }
103  else
104  {
105  i = vec_len (tm->trace_free_list);
106  if (i > 0)
107  {
108  trace_index = tm->trace_free_list[i - 1];
109  _vec_len (tm->trace_free_list) = i - 1;
110  }
111  else
112  {
113  mheap_trace_t *old_start = tm->traces;
114  mheap_trace_t *old_end = vec_end (tm->traces);
115 
116  vec_add2 (tm->traces, t, 1);
117 
118  if (tm->traces != old_start)
119  {
120  hash_pair_t *p;
121  mheap_trace_t *q;
122  /* *INDENT-OFF* */
124  ({
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));
128  }));
129  /* *INDENT-ON* */
130  }
131  trace_index = t - tm->traces;
132  }
133 
134  t = tm->traces + trace_index;
135  t[0] = trace;
136  t->n_allocations = 0;
137  t->n_bytes = 0;
138  hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
139  }
140 
141  t->n_allocations += 1;
142  t->n_bytes += size;
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);
147 }
148 
149 void
151 {
152  mheap_trace_t *t;
153  uword trace_index, *p;
155  uword save_enabled;
156 
157  if (tm->enabled == 0)
158  return;
159 
160  clib_spinlock_lock (&tm->lock);
161 
162  /* Turn off tracing for a moment */
163  save_enabled = tm->enabled;
164  tm->enabled = 0;
165 
166  p = hash_get (tm->trace_index_by_offset, offset);
167  if (!p)
168  {
169  tm->enabled = save_enabled;
170  clib_spinlock_unlock (&tm->lock);
171  return;
172  }
173 
174  trace_index = p[0];
175  hash_unset (tm->trace_index_by_offset, offset);
176  ASSERT (trace_index < vec_len (tm->traces));
177 
178  t = tm->traces + trace_index;
179  ASSERT (t->n_allocations > 0);
180  ASSERT (t->n_bytes >= size);
181  t->n_allocations -= 1;
182  t->n_bytes -= size;
183  if (t->n_allocations == 0)
184  {
186  vec_add1 (tm->trace_free_list, trace_index);
187  clib_memset (t, 0, sizeof (t[0]));
188  }
189  tm->enabled = save_enabled;
190  clib_spinlock_unlock (&tm->lock);
191 }
192 
193 always_inline void
195 {
196  vec_free (tm->traces);
200 }
201 
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. */
204 void *
206 {
207  u8 *heap;
208 
209  if (memory)
210  {
211  heap = create_mspace_with_base (memory, memory_size, 1 /* locked */ );
212  mspace_disable_expand (heap);
213  }
214  else
215  heap = create_mspace (memory_size, 1 /* locked */ );
216 
217  clib_mem_set_heap (heap);
218 
219  if (mheap_trace_main.lock == 0)
220  clib_spinlock_init (&mheap_trace_main.lock);
221 
222  return heap;
223 }
224 
225 void *
227 {
228  return clib_mem_init (memory, memory_size);
229 }
230 
231 u8 *
232 format_clib_mem_usage (u8 * s, va_list * va)
233 {
234  int verbose = va_arg (*va, int);
235  return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
236  verbose);
237 }
238 
239 /*
240  * Magic decoder ring for mallinfo stats (ala dlmalloc):
241  *
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) * /
252  *
253  */
254 
255 u8 *
256 format_msize (u8 * s, va_list * va)
257 {
258  uword a = va_arg (*va, uword);
259 
260  if (a >= 1ULL << 30)
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))));
266  else
267  s = format (s, "%lld", a);
268  return s;
269 }
270 
271 static int
272 mheap_trace_sort (const void *_t1, const void *_t2)
273 {
274  const mheap_trace_t *t1 = _t1;
275  const mheap_trace_t *t2 = _t2;
276  word cmp;
277 
278  cmp = (word) t2->n_bytes - (word) t1->n_bytes;
279  if (!cmp)
280  cmp = (word) t2->n_allocations - (word) t1->n_allocations;
281  return cmp;
282 }
283 
284 u8 *
285 format_mheap_trace (u8 * s, va_list * va)
286 {
287  mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
288  int verbose = va_arg (*va, int);
289  int have_traces = 0;
290  int i;
291 
292  clib_spinlock_lock (&tm->lock);
293  if (vec_len (tm->traces) > 0 &&
295  {
296  have_traces = 1;
297 
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;
301 
302  traces_copy = vec_dup (tm->traces);
303 
304  qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
306 
307  total_objects_traced = 0;
308  s = format (s, "\n");
309  vec_foreach (t, traces_copy)
310  {
311  /* Skip over free elements. */
312  if (t->n_allocations == 0)
313  continue;
314 
315  total_objects_traced += t->n_allocations;
316 
317  /* When not verbose only report allocations of more than 1k. */
318  if (!verbose && t->n_bytes < 1024)
319  continue;
320 
321  if (t == traces_copy)
322  s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
323  "Sample");
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++)
327  {
328  if (i > 0)
329  s = format (s, "%U", format_white_space, indent);
330 #if defined(CLIB_UNIX) && !defined(__APPLE__)
331  /* $$$$ does this actually work? */
332  s =
334  t->callers[i]);
335 #else
336  s = format (s, " %p\n", t->callers[i]);
337 #endif
338  }
339  }
340 
341  s = format (s, "%d total traced objects\n", total_objects_traced);
342 
343  vec_free (traces_copy);
344  }
345  clib_spinlock_unlock (&tm->lock);
346  if (have_traces == 0)
347  s = format (s, "no traced allocations\n");
348 
349  return s;
350 }
351 
352 
353 u8 *
354 format_mheap (u8 * s, va_list * va)
355 {
356  void *heap = va_arg (*va, u8 *);
357  int verbose = va_arg (*va, int);
358  struct dlmallinfo mi;
360 
361  mi = mspace_mallinfo (heap);
362 
363  s = format (s, "total: %U, used: %U, free: %U, trimmable: %U",
364  format_msize, mi.arena,
367  if (verbose > 0)
368  {
369  s = format (s, "\n free chunks %llu free fastbin blks %llu",
370  mi.ordblks, mi.smblks);
371  s =
372  format (s, "\n max total allocated %U", format_msize, mi.usmblks);
373  }
374 
375  if (mspace_is_traced (heap))
376  s = format (s, "\n%U", format_mheap_trace, tm, verbose);
377  return s;
378 }
379 
380 void
382 {
383  clib_warning ("unimp");
384 }
385 
386 /* Call serial number for debugger breakpoints. */
388 
389 void
391 {
392  clib_warning ("unimp");
393 }
394 
395 void
396 mheap_trace (void *v, int enable)
397 {
398  (void) mspace_enable_disable_trace (v, enable);
399 
400  if (enable == 0)
401  mheap_trace_main_free (&mheap_trace_main);
402 }
403 
404 void
405 clib_mem_trace (int enable)
406 {
408  void *current_heap = clib_mem_get_heap ();
409 
410  tm->enabled = enable;
411  mheap_trace (current_heap, enable);
412 
413  if (enable)
414  tm->current_traced_mheap = current_heap;
415  else
416  tm->current_traced_mheap = 0;
417 }
418 
419 int
421 {
423 }
424 
425 uword
427 {
428  uword rv;
430 
431  rv = tm->enabled;
432  tm->enabled = enable;
433  return rv;
434 }
435 
436 /*
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
440  */
441 void *
443 {
444  void *rv;
445  if (memory == 0)
446  return create_mspace (size, locked);
447  else
448  {
449  rv = create_mspace_with_base (memory, size, locked);
450  if (rv)
452  return rv;
453  }
454 }
455 
456 /*
457  * fd.io coding-style-patch-verification: ON
458  *
459  * Local Variables:
460  * eval: (c-set-style "gnu")
461  * End:
462  */
void * clib_per_cpu_mheaps[CLIB_MAX_MHEAPS]
Definition: mem_dlmalloc.c:23
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:862
#define hash_set(h, key, value)
Definition: hash.h:255
vhost_user_memory_t memory
Definition: vhost_user.h:122
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:89
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:74
#define hash_unset(h, key)
Definition: hash.h:261
a
Definition: bitmap.h:538
void * current_traced_mheap
Definition: mem_dlmalloc.c:61
void * clib_mem_init(void *memory, uword memory_size)
Definition: mem_dlmalloc.c:205
unsigned long u64
Definition: types.h:89
uword clib_backtrace(uword *callers, uword max_callers, uword n_frames_to_skip)
Definition: backtrace.c:226
uword callers[12]
Definition: mem_dlmalloc.c:28
u8 * format_msize(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:256
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
void mheap_trace(void *v, int enable)
Definition: mem_dlmalloc.c:396
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:560
int i
static u32 format_get_indent(u8 *s)
Definition: format.h:72
#define hash_set_mem(h, key, value)
Definition: hash.h:275
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
mheap_trace_t * traces
Definition: mem_dlmalloc.c:49
DLMALLOC_EXPORT struct dlmallinfo mspace_mallinfo(mspace msp)
#define CLIB_MAX_MHEAPS
Definition: mem.h:58
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
DLMALLOC_EXPORT mspace create_mspace_with_base(void *base, size_t capacity, int locked)
MALLINFO_FIELD_TYPE uordblks
Definition: dlmalloc.h:807
i64 word
Definition: types.h:111
MALLINFO_FIELD_TYPE arena
Definition: dlmalloc.h:800
#define always_inline
Definition: clib.h:98
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
unsigned int u32
Definition: types.h:88
#define vec_end(v)
End (last data address) of vector.
MALLINFO_FIELD_TYPE ordblks
Definition: dlmalloc.h:801
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:57
u8 * format_mheap(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:354
#define hash_get(h, key)
Definition: hash.h:249
uword size
#define hash_unset_mem(h, key)
Definition: hash.h:291
mheap_trace_main_t mheap_trace_main
Definition: mem_dlmalloc.c:65
MALLINFO_FIELD_TYPE usmblks
Definition: dlmalloc.h:805
void clib_mem_usage(clib_mem_usage_t *u)
Definition: mem_dlmalloc.c:381
DLMALLOC_EXPORT void mspace_disable_expand(mspace msp)
u64 memory_size
Definition: vhost_user.h:115
#define hash_free(h)
Definition: hash.h:310
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:375
void clib_mem_validate(void)
Definition: mem_dlmalloc.c:390
static int mheap_trace_sort(const void *_t1, const void *_t2)
Definition: mem_dlmalloc.c:272
uword * trace_index_by_offset
Definition: mem_dlmalloc.c:58
DLMALLOC_EXPORT int mspace_is_traced(mspace msp)
DLMALLOC_EXPORT mspace create_mspace(size_t capacity, int locked)
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static void * clib_mem_set_heap(void *heap)
Definition: mem.h:261
MALLINFO_FIELD_TYPE keepcost
Definition: dlmalloc.h:809
#define clib_warning(format, args...)
Definition: error.h:59
DLMALLOC_EXPORT int mspace_enable_disable_trace(mspace msp, int enable)
#define ARRAY_LEN(x)
Definition: clib.h:62
uword * trace_by_callers
Definition: mem_dlmalloc.c:55
u8 * format_mheap_trace(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:285
static void * clib_mem_get_heap(void)
Definition: mem.h:255
static void mheap_trace_main_free(mheap_trace_main_t *tm)
Definition: mem_dlmalloc.c:194
int clib_mem_is_traced(void)
Definition: mem_dlmalloc.c:420
MALLINFO_FIELD_TYPE smblks
Definition: dlmalloc.h:802
#define hash_create_shmem(elts, key_bytes, value_bytes)
Definition: hash.h:684
#define ASSERT(truth)
uword clib_mem_trace_enable_disable(uword enable)
Definition: mem_dlmalloc.c:426
void mheap_get_trace(uword offset, uword size)
Definition: mem_dlmalloc.c:68
void * mheap_alloc_with_lock(void *memory, uword size, int locked)
Definition: mem_dlmalloc.c:442
uword clib_mem_validate_serial
Definition: mem_dlmalloc.c:387
template key/value backing page structure
Definition: bihash_doc.h:44
void qsort(void *base, uword n, uword size, int(*compar)(const void *, const void *))
Definition: qsort.c:56
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:373
u64 uword
Definition: types.h:112
void mheap_put_trace(uword offset, uword size)
Definition: mem_dlmalloc.c:150
format_function_t format_clib_elf_symbol_with_address
Definition: elf_clib.h:134
#define hash_get_mem(h, key)
Definition: hash.h:269
struct clib_bihash_value offset
template key/value backing page structure
#define vec_foreach(var, vec)
Vector iterator.
void clib_mem_trace(int enable)
Definition: mem_dlmalloc.c:405
void * clib_mem_init_thread_safe(void *memory, uword memory_size)
Definition: mem_dlmalloc.c:226
MALLINFO_FIELD_TYPE fordblks
Definition: dlmalloc.h:808
clib_spinlock_t lock
Definition: mem_dlmalloc.c:46
u8 * format_clib_mem_usage(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:232