FD.io VPP  v20.01-48-g3e0dafb74
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 #include <vppinfra/sanitizer.h>
23 
25 
26 typedef struct
27 {
28  /* Address of callers: outer first, inner last. */
29  uword callers[12];
30 
31  /* Count of allocations with this traceback. */
32 #if CLIB_VEC64 > 0
33  u64 n_allocations;
34 #else
36 #endif
37 
38  /* Count of bytes allocated with this traceback. */
40 
41  /* Offset of this item */
44 
45 typedef struct
46 {
49 
51 
52  /* Indices of free traces. */
54 
55  /* Hash table mapping callers to trace index. */
57 
58  /* Hash table mapping mheap offset to trace index. */
60 
61  /* So we can easily shut off current segment trace, if any */
63 
65 
67 
68 void
70 {
72  mheap_trace_t *t;
73  uword i, n_callers, trace_index, *p;
75  uword save_enabled;
76 
77  if (tm->enabled == 0 || (clib_mem_get_heap () != tm->current_traced_mheap))
78  return;
79 
80  /* Spurious Coverity warnings be gone. */
81  clib_memset (&trace, 0, sizeof (trace));
82 
83  /* Skip our frame and mspace_get_aligned's frame */
84  n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2);
85  if (n_callers == 0)
86  return;
87 
88  clib_spinlock_lock (&tm->lock);
89 
90  /* Turn off tracing to avoid embarrassment... */
91  save_enabled = tm->enabled;
92  tm->enabled = 0;
93 
94  if (!tm->trace_by_callers)
95  tm->trace_by_callers =
96  hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
97 
98  p = hash_get_mem (tm->trace_by_callers, &trace.callers);
99  if (p)
100  {
101  trace_index = p[0];
102  t = tm->traces + trace_index;
103  }
104  else
105  {
106  i = vec_len (tm->trace_free_list);
107  if (i > 0)
108  {
109  trace_index = tm->trace_free_list[i - 1];
110  _vec_len (tm->trace_free_list) = i - 1;
111  }
112  else
113  {
114  mheap_trace_t *old_start = tm->traces;
115  mheap_trace_t *old_end = vec_end (tm->traces);
116 
117  vec_add2 (tm->traces, t, 1);
118 
119  if (tm->traces != old_start)
120  {
121  hash_pair_t *p;
122  mheap_trace_t *q;
123  /* *INDENT-OFF* */
125  ({
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));
129  }));
130  /* *INDENT-ON* */
131  }
132  trace_index = t - tm->traces;
133  }
134 
135  t = tm->traces + trace_index;
136  t[0] = trace;
137  t->n_allocations = 0;
138  t->n_bytes = 0;
139  hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
140  }
141 
142  t->n_allocations += 1;
143  t->n_bytes += size;
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);
148 }
149 
150 void
152 {
153  mheap_trace_t *t;
154  uword trace_index, *p;
156  uword save_enabled;
157 
158  if (tm->enabled == 0)
159  return;
160 
161  clib_spinlock_lock (&tm->lock);
162 
163  /* Turn off tracing for a moment */
164  save_enabled = tm->enabled;
165  tm->enabled = 0;
166 
167  p = hash_get (tm->trace_index_by_offset, offset);
168  if (!p)
169  {
170  tm->enabled = save_enabled;
171  clib_spinlock_unlock (&tm->lock);
172  return;
173  }
174 
175  trace_index = p[0];
176  hash_unset (tm->trace_index_by_offset, offset);
177  ASSERT (trace_index < vec_len (tm->traces));
178 
179  t = tm->traces + trace_index;
180  ASSERT (t->n_allocations > 0);
181  ASSERT (t->n_bytes >= size);
182  t->n_allocations -= 1;
183  t->n_bytes -= size;
184  if (t->n_allocations == 0)
185  {
187  vec_add1 (tm->trace_free_list, trace_index);
188  clib_memset (t, 0, sizeof (t[0]));
189  }
190  tm->enabled = save_enabled;
191  clib_spinlock_unlock (&tm->lock);
192 }
193 
194 always_inline void
196 {
197  vec_free (tm->traces);
201 }
202 
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. */
205 void *
207 {
208  u8 *heap;
209 
210  if (memory)
211  {
212  heap = create_mspace_with_base (memory, memory_size, 1 /* locked */ );
213  mspace_disable_expand (heap);
214  }
215  else
216  heap = create_mspace (memory_size, 1 /* locked */ );
217 
219 
220  clib_mem_set_heap (heap);
221 
222  if (mheap_trace_main.lock == 0)
223  clib_spinlock_init (&mheap_trace_main.lock);
224 
225  return heap;
226 }
227 
228 void *
230 {
231  return clib_mem_init (memory, memory_size);
232 }
233 
234 u8 *
235 format_clib_mem_usage (u8 * s, va_list * va)
236 {
237  int verbose = va_arg (*va, int);
238  return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
239  verbose);
240 }
241 
242 /*
243  * Magic decoder ring for mallinfo stats (ala dlmalloc):
244  *
245  * size_t arena; / * Non-mmapped space allocated (bytes) * /
246  * size_t ordblks; / * Number of free chunks * /
247  * size_t smblks; / * Number of free fastbin blocks * /
248  * size_t hblks; / * Number of mmapped regions * /
249  * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * /
250  * size_t usmblks; / * Maximum total allocated space (bytes) * /
251  * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * /
252  * size_t uordblks; / * Total allocated space (bytes) * /
253  * size_t fordblks; / * Total free space (bytes) * /
254  * size_t keepcost; / * Top-most, releasable space (bytes) * /
255  *
256  */
257 
258 u8 *
259 format_msize (u8 * s, va_list * va)
260 {
261  uword a = va_arg (*va, uword);
262 
263  if (a >= 1ULL << 30)
264  s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30))));
265  else if (a >= 1ULL << 20)
266  s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20))));
267  else if (a >= 1ULL << 10)
268  s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10))));
269  else
270  s = format (s, "%lld", a);
271  return s;
272 }
273 
274 static int
275 mheap_trace_sort (const void *_t1, const void *_t2)
276 {
277  const mheap_trace_t *t1 = _t1;
278  const mheap_trace_t *t2 = _t2;
279  word cmp;
280 
281  cmp = (word) t2->n_bytes - (word) t1->n_bytes;
282  if (!cmp)
283  cmp = (word) t2->n_allocations - (word) t1->n_allocations;
284  return cmp;
285 }
286 
287 u8 *
288 format_mheap_trace (u8 * s, va_list * va)
289 {
290  mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
291  int verbose = va_arg (*va, int);
292  int have_traces = 0;
293  int i;
294 
295  clib_spinlock_lock (&tm->lock);
296  if (vec_len (tm->traces) > 0 &&
298  {
299  have_traces = 1;
300 
301  /* Make a copy of traces since we'll be sorting them. */
302  mheap_trace_t *t, *traces_copy;
303  u32 indent, total_objects_traced;
304 
305  traces_copy = vec_dup (tm->traces);
306 
307  qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
309 
310  total_objects_traced = 0;
311  s = format (s, "\n");
312  vec_foreach (t, traces_copy)
313  {
314  /* Skip over free elements. */
315  if (t->n_allocations == 0)
316  continue;
317 
318  total_objects_traced += t->n_allocations;
319 
320  /* When not verbose only report allocations of more than 1k. */
321  if (!verbose && t->n_bytes < 1024)
322  continue;
323 
324  if (t == traces_copy)
325  s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
326  "Sample");
327  s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset);
328  indent = format_get_indent (s);
329  for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++)
330  {
331  if (i > 0)
332  s = format (s, "%U", format_white_space, indent);
333 #if defined(CLIB_UNIX) && !defined(__APPLE__)
334  /* $$$$ does this actually work? */
335  s =
337  t->callers[i]);
338 #else
339  s = format (s, " %p\n", t->callers[i]);
340 #endif
341  }
342  }
343 
344  s = format (s, "%d total traced objects\n", total_objects_traced);
345 
346  vec_free (traces_copy);
347  }
348  clib_spinlock_unlock (&tm->lock);
349  if (have_traces == 0)
350  s = format (s, "no traced allocations\n");
351 
352  return s;
353 }
354 
355 
356 u8 *
357 format_mheap (u8 * s, va_list * va)
358 {
359  void *heap = va_arg (*va, u8 *);
360  int verbose = va_arg (*va, int);
361  struct dlmallinfo mi;
363 
364  mi = mspace_mallinfo (heap);
365 
366  s = format (s, "total: %U, used: %U, free: %U, trimmable: %U",
367  format_msize, mi.arena,
370  if (verbose > 0)
371  {
372  s = format (s, "\n free chunks %llu free fastbin blks %llu",
373  mi.ordblks, mi.smblks);
374  s =
375  format (s, "\n max total allocated %U", format_msize, mi.usmblks);
376  }
377 
378  if (mspace_is_traced (heap))
379  s = format (s, "\n%U", format_mheap_trace, tm, verbose);
380  return s;
381 }
382 
383 void
385 {
386  clib_warning ("unimp");
387 }
388 
389 void
391 {
392  struct dlmallinfo mi = mspace_mallinfo (heap);
393 
394  /* TODO: Fill in some more values */
395  usage->object_count = 0;
396  usage->bytes_total = mi.arena;
397  usage->bytes_overhead = 0;
398  usage->bytes_max = 0;
399  usage->bytes_used = mi.uordblks;
400  usage->bytes_free = mi.fordblks;
401  usage->bytes_free_reclaimed = 0;
402 }
403 
404 /* Call serial number for debugger breakpoints. */
406 
407 void
409 {
410  clib_warning ("unimp");
411 }
412 
413 void
414 mheap_trace (void *v, int enable)
415 {
416  (void) mspace_enable_disable_trace (v, enable);
417 
418  if (enable == 0)
419  mheap_trace_main_free (&mheap_trace_main);
420 }
421 
422 void
423 clib_mem_trace (int enable)
424 {
426  void *current_heap = clib_mem_get_heap ();
427 
428  tm->enabled = enable;
429  mheap_trace (current_heap, enable);
430 
431  if (enable)
432  tm->current_traced_mheap = current_heap;
433  else
434  tm->current_traced_mheap = 0;
435 }
436 
437 int
439 {
441 }
442 
443 uword
445 {
446  uword rv;
448 
449  rv = tm->enabled;
450  tm->enabled = enable;
451  return rv;
452 }
453 
454 /*
455  * These API functions seem like layering violations, but
456  * by introducing them we greatly reduce the number
457  * of code changes required to use dlmalloc spaces
458  */
459 void *
461 {
462  void *rv;
463  if (memory == 0)
464  return create_mspace (size, locked);
465  else
466  {
467  rv = create_mspace_with_base (memory, size, locked);
468  if (rv)
470  return rv;
471  }
472 }
473 
474 /*
475  * fd.io coding-style-patch-verification: ON
476  *
477  * Local Variables:
478  * eval: (c-set-style "gnu")
479  * End:
480  */
void * clib_per_cpu_mheaps[CLIB_MAX_MHEAPS]
Definition: mem_dlmalloc.c:24
uword bytes_overhead
Definition: mem.h:312
uword bytes_total
Definition: mem.h:308
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:898
#define hash_set(h, key, value)
Definition: hash.h:255
uword bytes_free
Definition: mem.h:308
vhost_user_memory_t memory
Definition: vhost_user.h:148
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:102
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:80
#define hash_unset(h, key)
Definition: hash.h:261
a
Definition: bitmap.h:538
uword bytes_free_reclaimed
Definition: mem.h:315
void * current_traced_mheap
Definition: mem_dlmalloc.c:62
void * clib_mem_init(void *memory, uword memory_size)
Definition: mem_dlmalloc.c:206
unsigned long u64
Definition: types.h:89
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
uword clib_backtrace(uword *callers, uword max_callers, uword n_frames_to_skip)
Definition: backtrace.c:226
uword callers[12]
Definition: mem_dlmalloc.c:29
u8 * format_msize(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:259
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
void mheap_trace(void *v, int enable)
Definition: mem_dlmalloc.c:414
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:561
int i
static void usage(void)
Definition: health_check.c:14
static u32 format_get_indent(u8 *s)
Definition: format.h:72
#define hash_set_mem(h, key, value)
Definition: hash.h:275
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
mheap_trace_t * traces
Definition: mem_dlmalloc.c:50
DLMALLOC_EXPORT struct dlmallinfo mspace_mallinfo(mspace msp)
#define CLIB_MAX_MHEAPS
Definition: mem.h:58
uword bytes_used
Definition: mem.h:308
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
uword object_count
Definition: mem.h:304
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
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
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:63
u8 * format_mheap(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:357
#define hash_get(h, key)
Definition: hash.h:249
#define hash_unset_mem(h, key)
Definition: hash.h:291
mheap_trace_main_t mheap_trace_main
Definition: mem_dlmalloc.c:66
MALLINFO_FIELD_TYPE usmblks
Definition: dlmalloc.h:805
void clib_mem_usage(clib_mem_usage_t *u)
Definition: mem_dlmalloc.c:384
DLMALLOC_EXPORT void mspace_disable_expand(mspace msp)
u64 memory_size
Definition: vhost_user.h:141
u64 size
Definition: vhost_user.h:140
#define hash_free(h)
Definition: hash.h:310
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:376
#define always_inline
Definition: ipsec.h:28
void clib_mem_validate(void)
Definition: mem_dlmalloc.c:408
static int mheap_trace_sort(const void *_t1, const void *_t2)
Definition: mem_dlmalloc.c:275
uword * trace_index_by_offset
Definition: mem_dlmalloc.c:59
DLMALLOC_EXPORT int mspace_is_traced(mspace msp)
DLMALLOC_EXPORT mspace create_mspace(size_t capacity, int locked)
uword bytes_max
Definition: mem.h:323
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
static void * clib_mem_set_heap(void *heap)
Definition: mem.h:283
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:56
u8 * format_mheap_trace(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:288
static void * clib_mem_get_heap(void)
Definition: mem.h:277
static void mheap_trace_main_free(mheap_trace_main_t *tm)
Definition: mem_dlmalloc.c:195
int clib_mem_is_traced(void)
Definition: mem_dlmalloc.c:438
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:444
void mheap_get_trace(uword offset, uword size)
Definition: mem_dlmalloc.c:69
void * mheap_alloc_with_lock(void *memory, uword size, int locked)
Definition: mem_dlmalloc.c:460
uword clib_mem_validate_serial
Definition: mem_dlmalloc.c:405
template key/value backing page structure
Definition: bihash_doc.h:44
void mheap_usage(void *heap, clib_mem_usage_t *usage)
Definition: mem_dlmalloc.c:390
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:151
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
DLMALLOC_EXPORT size_t mspace_footprint(mspace msp)
#define vec_foreach(var, vec)
Vector iterator.
void clib_mem_trace(int enable)
Definition: mem_dlmalloc.c:423
void * clib_mem_init_thread_safe(void *memory, uword memory_size)
Definition: mem_dlmalloc.c:229
MALLINFO_FIELD_TYPE fordblks
Definition: dlmalloc.h:808
clib_spinlock_t lock
Definition: mem_dlmalloc.c:47
u8 * format_clib_mem_usage(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:235
DLMALLOC_EXPORT void * mspace_least_addr(mspace msp)
#define CLIB_MEM_POISON(a, s)
Definition: sanitizer.h:46