FD.io VPP  v20.05-21-gb1500e9ff
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 #include <numaif.h>
24 
27 
28 typedef struct
29 {
30  /* Address of callers: outer first, inner last. */
31  uword callers[12];
32 
33  /* Count of allocations with this traceback. */
35 
36  /* Count of bytes allocated with this traceback. */
38 
39  /* Offset of this item */
42 
43 typedef struct
44 {
47 
49 
50  /* Indices of free traces. */
52 
53  /* Hash table mapping callers to trace index. */
55 
56  /* Hash table mapping mheap offset to trace index. */
58 
59  /* So we can easily shut off current segment trace, if any */
61 
63 
65 
66 void
68 {
70  mheap_trace_t *t;
71  uword i, n_callers, trace_index, *p;
73  uword save_enabled;
74 
75  if (tm->enabled == 0 || (clib_mem_get_heap () != tm->current_traced_mheap))
76  return;
77 
78  /* Spurious Coverity warnings be gone. */
79  clib_memset (&trace, 0, sizeof (trace));
80 
81  /* Skip our frame and mspace_get_aligned's frame */
82  n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2);
83  if (n_callers == 0)
84  return;
85 
86  clib_spinlock_lock (&tm->lock);
87 
88  /* Turn off tracing to avoid embarrassment... */
89  save_enabled = tm->enabled;
90  tm->enabled = 0;
91 
92  if (!tm->trace_by_callers)
93  tm->trace_by_callers =
94  hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
95 
96  p = hash_get_mem (tm->trace_by_callers, &trace.callers);
97  if (p)
98  {
99  trace_index = p[0];
100  t = tm->traces + trace_index;
101  }
102  else
103  {
104  i = vec_len (tm->trace_free_list);
105  if (i > 0)
106  {
107  trace_index = tm->trace_free_list[i - 1];
108  _vec_len (tm->trace_free_list) = i - 1;
109  }
110  else
111  {
112  mheap_trace_t *old_start = tm->traces;
113  mheap_trace_t *old_end = vec_end (tm->traces);
114 
115  vec_add2 (tm->traces, t, 1);
116 
117  if (tm->traces != old_start)
118  {
119  hash_pair_t *p;
120  mheap_trace_t *q;
121  /* *INDENT-OFF* */
123  ({
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));
127  }));
128  /* *INDENT-ON* */
129  }
130  trace_index = t - tm->traces;
131  }
132 
133  t = tm->traces + trace_index;
134  t[0] = trace;
135  t->n_allocations = 0;
136  t->n_bytes = 0;
137  hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
138  }
139 
140  t->n_allocations += 1;
141  t->n_bytes += size;
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);
146 }
147 
148 void
150 {
151  mheap_trace_t *t;
152  uword trace_index, *p;
154  uword save_enabled;
155 
156  if (tm->enabled == 0)
157  return;
158 
159  clib_spinlock_lock (&tm->lock);
160 
161  /* Turn off tracing for a moment */
162  save_enabled = tm->enabled;
163  tm->enabled = 0;
164 
165  p = hash_get (tm->trace_index_by_offset, offset);
166  if (!p)
167  {
168  tm->enabled = save_enabled;
169  clib_spinlock_unlock (&tm->lock);
170  return;
171  }
172 
173  trace_index = p[0];
174  hash_unset (tm->trace_index_by_offset, offset);
175  ASSERT (trace_index < vec_len (tm->traces));
176 
177  t = tm->traces + trace_index;
178  ASSERT (t->n_allocations > 0);
179  ASSERT (t->n_bytes >= size);
180  t->n_allocations -= 1;
181  t->n_bytes -= size;
182  if (t->n_allocations == 0)
183  {
185  vec_add1 (tm->trace_free_list, trace_index);
186  clib_memset (t, 0, sizeof (t[0]));
187  }
188  tm->enabled = save_enabled;
189  clib_spinlock_unlock (&tm->lock);
190 }
191 
192 always_inline void
194 {
195  vec_free (tm->traces);
199 }
200 
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. */
203 static void *
205 {
206  u8 *heap;
207 
208  if (memory)
209  {
210  heap = create_mspace_with_base (memory, memory_size, 1 /* locked */ );
211  mspace_disable_expand (heap);
212  }
213  else
214  heap = create_mspace (memory_size, 1 /* locked */ );
215 
217 
218  if (set_heap)
219  clib_mem_set_heap (heap);
220 
221  if (mheap_trace_main.lock == 0)
222  clib_spinlock_init (&mheap_trace_main.lock);
223 
224  return heap;
225 }
226 
227 void *
229 {
230  return clib_mem_init_internal (memory, memory_size,
231  1 /* do clib_mem_set_heap */ );
232 }
233 
234 void *
236 {
237  return clib_mem_init_internal (memory, memory_size,
238  1 /* do clib_mem_set_heap */ );
239 }
240 
241 void *
243 {
244  clib_mem_vm_alloc_t alloc = { 0 };
245  clib_error_t *err;
246  void *heap;
247 
248  alloc.size = memory_size;
250  alloc.numa_node = numa;
251  if ((err = clib_mem_vm_ext_alloc (&alloc)))
252  {
253  clib_error_report (err);
254  return 0;
255  }
256 
257  heap = clib_mem_init_internal (memory, memory_size,
258  0 /* do NOT clib_mem_set_heap */ );
259 
260  ASSERT (heap);
261 
262  return heap;
263 }
264 
265 u8 *
266 format_clib_mem_usage (u8 * s, va_list * va)
267 {
268  int verbose = va_arg (*va, int);
269  return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
270  verbose);
271 }
272 
273 /*
274  * Magic decoder ring for mallinfo stats (ala dlmalloc):
275  *
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) * /
286  *
287  */
288 
289 u8 *
290 format_msize (u8 * s, va_list * va)
291 {
292  uword a = va_arg (*va, uword);
293 
294  if (a >= 1ULL << 30)
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))));
300  else
301  s = format (s, "%lld", a);
302  return s;
303 }
304 
305 static int
306 mheap_trace_sort (const void *_t1, const void *_t2)
307 {
308  const mheap_trace_t *t1 = _t1;
309  const mheap_trace_t *t2 = _t2;
310  word cmp;
311 
312  cmp = (word) t2->n_bytes - (word) t1->n_bytes;
313  if (!cmp)
314  cmp = (word) t2->n_allocations - (word) t1->n_allocations;
315  return cmp;
316 }
317 
318 u8 *
319 format_mheap_trace (u8 * s, va_list * va)
320 {
321  mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
322  int verbose = va_arg (*va, int);
323  int have_traces = 0;
324  int i;
325 
326  clib_spinlock_lock (&tm->lock);
327  if (vec_len (tm->traces) > 0 &&
329  {
330  have_traces = 1;
331 
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;
335 
336  traces_copy = vec_dup (tm->traces);
337 
338  qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
340 
341  total_objects_traced = 0;
342  s = format (s, "\n");
343  vec_foreach (t, traces_copy)
344  {
345  /* Skip over free elements. */
346  if (t->n_allocations == 0)
347  continue;
348 
349  total_objects_traced += t->n_allocations;
350 
351  /* When not verbose only report allocations of more than 1k. */
352  if (!verbose && t->n_bytes < 1024)
353  continue;
354 
355  if (t == traces_copy)
356  s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
357  "Sample");
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++)
361  {
362  if (i > 0)
363  s = format (s, "%U", format_white_space, indent);
364 #if defined(CLIB_UNIX) && !defined(__APPLE__)
365  /* $$$$ does this actually work? */
366  s =
368  t->callers[i]);
369 #else
370  s = format (s, " %p\n", t->callers[i]);
371 #endif
372  }
373  }
374 
375  s = format (s, "%d total traced objects\n", total_objects_traced);
376 
377  vec_free (traces_copy);
378  }
379  clib_spinlock_unlock (&tm->lock);
380  if (have_traces == 0)
381  s = format (s, "no traced allocations\n");
382 
383  return s;
384 }
385 
386 
387 u8 *
388 format_mheap (u8 * s, va_list * va)
389 {
390  void *heap = va_arg (*va, u8 *);
391  int verbose = va_arg (*va, int);
392  struct dlmallinfo mi;
394 
395  mi = mspace_mallinfo (heap);
396 
397  s = format (s, "total: %U, used: %U, free: %U, trimmable: %U",
398  format_msize, mi.arena,
401  if (verbose > 0)
402  {
403  s = format (s, "\n free chunks %llu free fastbin blks %llu",
404  mi.ordblks, mi.smblks);
405  s =
406  format (s, "\n max total allocated %U", format_msize, mi.usmblks);
407  }
408 
409  if (mspace_is_traced (heap))
410  s = format (s, "\n%U", format_mheap_trace, tm, verbose);
411  return s;
412 }
413 
414 void
416 {
417  clib_warning ("unimp");
418 }
419 
420 void
422 {
423  struct dlmallinfo mi = mspace_mallinfo (heap);
424 
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;
433 }
434 
435 /* Call serial number for debugger breakpoints. */
437 
438 void
440 {
441  clib_warning ("unimp");
442 }
443 
444 void
445 mheap_trace (void *v, int enable)
446 {
447  (void) mspace_enable_disable_trace (v, enable);
448 
449  if (enable == 0)
450  mheap_trace_main_free (&mheap_trace_main);
451 }
452 
453 void
454 clib_mem_trace (int enable)
455 {
457  void *current_heap = clib_mem_get_heap ();
458 
459  tm->enabled = enable;
460  mheap_trace (current_heap, enable);
461 
462  if (enable)
463  tm->current_traced_mheap = current_heap;
464  else
465  tm->current_traced_mheap = 0;
466 }
467 
468 int
470 {
472 }
473 
474 uword
476 {
477  uword rv;
479 
480  rv = tm->enabled;
481  tm->enabled = enable;
482  return rv;
483 }
484 
485 /*
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
489  */
490 void *
492 {
493  void *rv;
494  if (memory == 0)
495  return create_mspace (size, locked);
496  else
497  {
498  rv = create_mspace_with_base (memory, size, locked);
499  if (rv)
501  return rv;
502  }
503 }
504 
505 /*
506  * fd.io coding-style-patch-verification: ON
507  *
508  * Local Variables:
509  * eval: (c-set-style "gnu")
510  * End:
511  */
void * clib_per_cpu_mheaps[CLIB_MAX_MHEAPS]
Definition: mem_dlmalloc.c:25
uword bytes_overhead
Definition: mem.h:299
uword bytes_total
Definition: mem.h:295
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:899
#define hash_set(h, key, value)
Definition: hash.h:255
uword bytes_free
Definition: mem.h:295
vhost_user_memory_t memory
Definition: vhost_user.h:255
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
void * clib_per_numa_mheaps[CLIB_MAX_NUMAS]
Definition: mem_dlmalloc.c:26
uword callers[12]
Definition: mem_dlmalloc.c:31
uword bytes_free_reclaimed
Definition: mem.h:302
void * current_traced_mheap
Definition: mem_dlmalloc.c:60
void * clib_mem_init(void *memory, uword memory_size)
Definition: mem_dlmalloc.c:228
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
u8 * format_msize(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:290
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:590
void mheap_trace(void *v, int enable)
Definition: mem_dlmalloc.c:445
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:628
int numa_node
numa node preference.
Definition: mem.h:398
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
DLMALLOC_EXPORT struct dlmallinfo mspace_mallinfo(mspace msp)
#define CLIB_MAX_MHEAPS
Definition: mem.h:54
uword bytes_used
Definition: mem.h:295
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
uword object_count
Definition: mem.h:291
DLMALLOC_EXPORT mspace create_mspace_with_base(void *base, size_t capacity, int locked)
MALLINFO_FIELD_TYPE uordblks
Definition: dlmalloc.h:807
clib_error_t * clib_mem_vm_ext_alloc(clib_mem_vm_alloc_t *a)
Definition: mem.c:193
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
uword size
Allocation size, set by caller.
Definition: mem.h:397
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:388
#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:64
MALLINFO_FIELD_TYPE usmblks
Definition: dlmalloc.h:805
void clib_mem_usage(clib_mem_usage_t *u)
Definition: mem_dlmalloc.c:415
DLMALLOC_EXPORT void mspace_disable_expand(mspace msp)
u64 memory_size
Definition: vhost_user.h:151
u64 size
Definition: vhost_user.h:150
#define hash_free(h)
Definition: hash.h:310
mheap_trace_t * traces
Definition: mem_dlmalloc.c:48
#define CLIB_MEM_VM_F_NUMA_FORCE
Definition: mem.h:382
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:427
#define always_inline
Definition: ipsec.h:28
void clib_mem_validate(void)
Definition: mem_dlmalloc.c:439
static int mheap_trace_sort(const void *_t1, const void *_t2)
Definition: mem_dlmalloc.c:306
DLMALLOC_EXPORT int mspace_is_traced(mspace msp)
DLMALLOC_EXPORT mspace create_mspace(size_t capacity, int locked)
uword bytes_max
Definition: mem.h:310
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static void * clib_mem_set_heap(void *heap)
Definition: mem.h:268
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:66
uword * trace_by_callers
Definition: mem_dlmalloc.c:54
u8 * format_mheap_trace(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:319
u32 flags
vm allocation flags: CLIB_MEM_VM_F_SHARED: request shared memory, file descriptor will be provided ...
Definition: mem.h:385
static void * clib_mem_get_heap(void)
Definition: mem.h:262
static void mheap_trace_main_free(mheap_trace_main_t *tm)
Definition: mem_dlmalloc.c:193
int clib_mem_is_traced(void)
Definition: mem_dlmalloc.c:469
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:475
#define CLIB_MAX_NUMAS
Definition: mem.h:55
void mheap_get_trace(uword offset, uword size)
Definition: mem_dlmalloc.c:67
void * mheap_alloc_with_lock(void *memory, uword size, int locked)
Definition: mem_dlmalloc.c:491
uword clib_mem_validate_serial
Definition: mem_dlmalloc.c:436
#define clib_error_report(e)
Definition: error.h:113
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:421
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
static void * clib_mem_init_internal(void *memory, uword memory_size, int set_heap)
Definition: mem_dlmalloc.c:204
void mheap_put_trace(uword offset, uword size)
Definition: mem_dlmalloc.c:149
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:454
void * clib_mem_init_thread_safe(void *memory, uword memory_size)
Definition: mem_dlmalloc.c:235
MALLINFO_FIELD_TYPE fordblks
Definition: dlmalloc.h:808
uword * trace_index_by_offset
Definition: mem_dlmalloc.c:57
clib_spinlock_t lock
Definition: mem_dlmalloc.c:45
u8 * format_clib_mem_usage(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:266
DLMALLOC_EXPORT void * mspace_least_addr(mspace msp)
void * clib_mem_init_thread_safe_numa(void *memory, uword memory_size, u8 numa)
Definition: mem_dlmalloc.c:242
#define CLIB_MEM_POISON(a, s)
Definition: sanitizer.h:46