blob: 2cd924a605d5a111bf07f1de9637e22ae7efa5a6 [file] [log] [blame]
Dave Barach6a5adc32018-07-04 10:56:23 -04001/*
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>
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +020022#include <vppinfra/sanitizer.h>
Dave Barach6a5adc32018-07-04 10:56:23 -040023
Dave Barach6a5adc32018-07-04 10:56:23 -040024typedef struct
25{
26 /* Address of callers: outer first, inner last. */
27 uword callers[12];
28
29 /* Count of allocations with this traceback. */
Dave Barach6a5adc32018-07-04 10:56:23 -040030 u32 n_allocations;
Dave Barach6a5adc32018-07-04 10:56:23 -040031
32 /* Count of bytes allocated with this traceback. */
33 u32 n_bytes;
34
35 /* Offset of this item */
36 uword offset;
37} mheap_trace_t;
38
39typedef struct
40{
41 clib_spinlock_t lock;
42 uword enabled;
43
44 mheap_trace_t *traces;
45
46 /* Indices of free traces. */
47 u32 *trace_free_list;
48
49 /* Hash table mapping callers to trace index. */
50 uword *trace_by_callers;
51
52 /* Hash table mapping mheap offset to trace index. */
53 uword *trace_index_by_offset;
Dave Barachd67a4282019-06-15 12:46:13 -040054
55 /* So we can easily shut off current segment trace, if any */
56 void *current_traced_mheap;
57
Dave Barach6a5adc32018-07-04 10:56:23 -040058} mheap_trace_main_t;
59
60mheap_trace_main_t mheap_trace_main;
61
62void
63mheap_get_trace (uword offset, uword size)
64{
65 mheap_trace_main_t *tm = &mheap_trace_main;
66 mheap_trace_t *t;
67 uword i, n_callers, trace_index, *p;
68 mheap_trace_t trace;
69 uword save_enabled;
70
Dave Barachd67a4282019-06-15 12:46:13 -040071 if (tm->enabled == 0 || (clib_mem_get_heap () != tm->current_traced_mheap))
Dave Barach6a5adc32018-07-04 10:56:23 -040072 return;
73
74 /* Spurious Coverity warnings be gone. */
Dave Barachb7b92992018-10-17 10:38:51 -040075 clib_memset (&trace, 0, sizeof (trace));
Dave Barach6a5adc32018-07-04 10:56:23 -040076
77 /* Skip our frame and mspace_get_aligned's frame */
78 n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2);
79 if (n_callers == 0)
80 return;
81
Dave Barach6a5adc32018-07-04 10:56:23 -040082 clib_spinlock_lock (&tm->lock);
83
84 /* Turn off tracing to avoid embarrassment... */
85 save_enabled = tm->enabled;
86 tm->enabled = 0;
87
88 if (!tm->trace_by_callers)
89 tm->trace_by_callers =
90 hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
91
92 p = hash_get_mem (tm->trace_by_callers, &trace.callers);
93 if (p)
94 {
95 trace_index = p[0];
96 t = tm->traces + trace_index;
97 }
98 else
99 {
100 i = vec_len (tm->trace_free_list);
101 if (i > 0)
102 {
103 trace_index = tm->trace_free_list[i - 1];
104 _vec_len (tm->trace_free_list) = i - 1;
105 }
106 else
107 {
108 mheap_trace_t *old_start = tm->traces;
109 mheap_trace_t *old_end = vec_end (tm->traces);
110
111 vec_add2 (tm->traces, t, 1);
112
113 if (tm->traces != old_start)
114 {
115 hash_pair_t *p;
116 mheap_trace_t *q;
117 /* *INDENT-OFF* */
118 hash_foreach_pair (p, tm->trace_by_callers,
119 ({
120 q = uword_to_pointer (p->key, mheap_trace_t *);
121 ASSERT (q >= old_start && q < old_end);
122 p->key = pointer_to_uword (tm->traces + (q - old_start));
123 }));
124 /* *INDENT-ON* */
125 }
126 trace_index = t - tm->traces;
127 }
128
129 t = tm->traces + trace_index;
130 t[0] = trace;
131 t->n_allocations = 0;
132 t->n_bytes = 0;
133 hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
134 }
135
136 t->n_allocations += 1;
137 t->n_bytes += size;
138 t->offset = offset; /* keep a sample to autopsy */
139 hash_set (tm->trace_index_by_offset, offset, t - tm->traces);
140 tm->enabled = save_enabled;
141 clib_spinlock_unlock (&tm->lock);
142}
143
144void
145mheap_put_trace (uword offset, uword size)
146{
147 mheap_trace_t *t;
148 uword trace_index, *p;
149 mheap_trace_main_t *tm = &mheap_trace_main;
150 uword save_enabled;
151
152 if (tm->enabled == 0)
153 return;
154
155 clib_spinlock_lock (&tm->lock);
156
157 /* Turn off tracing for a moment */
158 save_enabled = tm->enabled;
159 tm->enabled = 0;
160
161 p = hash_get (tm->trace_index_by_offset, offset);
162 if (!p)
163 {
164 tm->enabled = save_enabled;
165 clib_spinlock_unlock (&tm->lock);
166 return;
167 }
168
169 trace_index = p[0];
170 hash_unset (tm->trace_index_by_offset, offset);
171 ASSERT (trace_index < vec_len (tm->traces));
172
173 t = tm->traces + trace_index;
174 ASSERT (t->n_allocations > 0);
175 ASSERT (t->n_bytes >= size);
176 t->n_allocations -= 1;
177 t->n_bytes -= size;
178 if (t->n_allocations == 0)
179 {
180 hash_unset_mem (tm->trace_by_callers, t->callers);
181 vec_add1 (tm->trace_free_list, trace_index);
Dave Barachb7b92992018-10-17 10:38:51 -0400182 clib_memset (t, 0, sizeof (t[0]));
Dave Barach6a5adc32018-07-04 10:56:23 -0400183 }
184 tm->enabled = save_enabled;
185 clib_spinlock_unlock (&tm->lock);
186}
187
188always_inline void
189mheap_trace_main_free (mheap_trace_main_t * tm)
190{
191 vec_free (tm->traces);
192 vec_free (tm->trace_free_list);
193 hash_free (tm->trace_by_callers);
194 hash_free (tm->trace_index_by_offset);
195}
196
197/* Initialize CLIB heap based on memory/size given by user.
198 Set memory to 0 and CLIB will try to allocate its own heap. */
Dave Baracha690fdb2020-01-21 12:34:55 -0500199static void *
200clib_mem_init_internal (void *memory, uword memory_size, int set_heap)
Dave Barach6a5adc32018-07-04 10:56:23 -0400201{
202 u8 *heap;
203
204 if (memory)
205 {
206 heap = create_mspace_with_base (memory, memory_size, 1 /* locked */ );
207 mspace_disable_expand (heap);
208 }
209 else
210 heap = create_mspace (memory_size, 1 /* locked */ );
211
BenoƮt Ganneb2f09142019-12-16 15:37:28 +0100212 CLIB_MEM_POISON (mspace_least_addr (heap), mspace_footprint (heap));
213
Dave Baracha690fdb2020-01-21 12:34:55 -0500214 if (set_heap)
215 clib_mem_set_heap (heap);
Dave Barach6a5adc32018-07-04 10:56:23 -0400216
217 if (mheap_trace_main.lock == 0)
218 clib_spinlock_init (&mheap_trace_main.lock);
219
220 return heap;
221}
222
223void *
Dave Baracha690fdb2020-01-21 12:34:55 -0500224clib_mem_init (void *memory, uword memory_size)
225{
226 return clib_mem_init_internal (memory, memory_size,
227 1 /* do clib_mem_set_heap */ );
228}
229
230void *
Dave Barach6a5adc32018-07-04 10:56:23 -0400231clib_mem_init_thread_safe (void *memory, uword memory_size)
232{
Dave Baracha690fdb2020-01-21 12:34:55 -0500233 return clib_mem_init_internal (memory, memory_size,
234 1 /* do clib_mem_set_heap */ );
235}
236
Dave Barach2b793412020-08-28 10:39:00 -0400237void
238clib_mem_destroy_mspace (void *mspace)
239{
240 mheap_trace_main_t *tm = &mheap_trace_main;
241
242 if (tm->enabled && mspace == tm->current_traced_mheap)
243 tm->enabled = 0;
244
245 destroy_mspace (mspace);
246}
247
248void
249clib_mem_destroy (void)
250{
251 clib_mem_destroy_mspace (clib_mem_get_heap ());
252}
253
Dave Baracha690fdb2020-01-21 12:34:55 -0500254void *
Florin Coras4c959952020-02-09 18:09:31 +0000255clib_mem_init_thread_safe_numa (void *memory, uword memory_size, u8 numa)
Dave Baracha690fdb2020-01-21 12:34:55 -0500256{
Florin Coras4c959952020-02-09 18:09:31 +0000257 clib_mem_vm_alloc_t alloc = { 0 };
258 clib_error_t *err;
Dave Baracha690fdb2020-01-21 12:34:55 -0500259 void *heap;
Dave Baracha690fdb2020-01-21 12:34:55 -0500260
Florin Coras4c959952020-02-09 18:09:31 +0000261 alloc.size = memory_size;
262 alloc.flags = CLIB_MEM_VM_F_NUMA_FORCE;
263 alloc.numa_node = numa;
264 if ((err = clib_mem_vm_ext_alloc (&alloc)))
265 {
266 clib_error_report (err);
267 return 0;
268 }
269
270 heap = clib_mem_init_internal (memory, memory_size,
271 0 /* do NOT clib_mem_set_heap */ );
Dave Baracha690fdb2020-01-21 12:34:55 -0500272
273 ASSERT (heap);
274
Dave Baracha690fdb2020-01-21 12:34:55 -0500275 return heap;
Dave Barach6a5adc32018-07-04 10:56:23 -0400276}
277
Dave Barach6a5adc32018-07-04 10:56:23 -0400278u8 *
279format_clib_mem_usage (u8 * s, va_list * va)
280{
281 int verbose = va_arg (*va, int);
282 return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
283 verbose);
284}
285
286/*
287 * Magic decoder ring for mallinfo stats (ala dlmalloc):
288 *
289 * size_t arena; / * Non-mmapped space allocated (bytes) * /
290 * size_t ordblks; / * Number of free chunks * /
291 * size_t smblks; / * Number of free fastbin blocks * /
292 * size_t hblks; / * Number of mmapped regions * /
293 * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * /
294 * size_t usmblks; / * Maximum total allocated space (bytes) * /
295 * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * /
296 * size_t uordblks; / * Total allocated space (bytes) * /
297 * size_t fordblks; / * Total free space (bytes) * /
298 * size_t keepcost; / * Top-most, releasable space (bytes) * /
299 *
300 */
301
302u8 *
303format_msize (u8 * s, va_list * va)
304{
305 uword a = va_arg (*va, uword);
306
307 if (a >= 1ULL << 30)
308 s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30))));
309 else if (a >= 1ULL << 20)
310 s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20))));
311 else if (a >= 1ULL << 10)
312 s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10))));
313 else
314 s = format (s, "%lld", a);
315 return s;
316}
317
318static int
319mheap_trace_sort (const void *_t1, const void *_t2)
320{
321 const mheap_trace_t *t1 = _t1;
322 const mheap_trace_t *t2 = _t2;
323 word cmp;
324
325 cmp = (word) t2->n_bytes - (word) t1->n_bytes;
326 if (!cmp)
327 cmp = (word) t2->n_allocations - (word) t1->n_allocations;
328 return cmp;
329}
330
331u8 *
332format_mheap_trace (u8 * s, va_list * va)
333{
334 mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
335 int verbose = va_arg (*va, int);
336 int have_traces = 0;
337 int i;
338
339 clib_spinlock_lock (&tm->lock);
Dave Barachd67a4282019-06-15 12:46:13 -0400340 if (vec_len (tm->traces) > 0 &&
341 clib_mem_get_heap () == tm->current_traced_mheap)
Dave Barach6a5adc32018-07-04 10:56:23 -0400342 {
343 have_traces = 1;
344
345 /* Make a copy of traces since we'll be sorting them. */
346 mheap_trace_t *t, *traces_copy;
347 u32 indent, total_objects_traced;
348
349 traces_copy = vec_dup (tm->traces);
350
351 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
352 mheap_trace_sort);
353
354 total_objects_traced = 0;
355 s = format (s, "\n");
356 vec_foreach (t, traces_copy)
357 {
358 /* Skip over free elements. */
359 if (t->n_allocations == 0)
360 continue;
361
362 total_objects_traced += t->n_allocations;
363
364 /* When not verbose only report allocations of more than 1k. */
365 if (!verbose && t->n_bytes < 1024)
366 continue;
367
368 if (t == traces_copy)
369 s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
370 "Sample");
371 s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset);
372 indent = format_get_indent (s);
373 for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++)
374 {
375 if (i > 0)
376 s = format (s, "%U", format_white_space, indent);
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200377#if defined(CLIB_UNIX) && !defined(__APPLE__)
Dave Barach6a5adc32018-07-04 10:56:23 -0400378 /* $$$$ does this actually work? */
379 s =
380 format (s, " %U\n", format_clib_elf_symbol_with_address,
381 t->callers[i]);
382#else
383 s = format (s, " %p\n", t->callers[i]);
384#endif
385 }
386 }
387
388 s = format (s, "%d total traced objects\n", total_objects_traced);
389
390 vec_free (traces_copy);
391 }
392 clib_spinlock_unlock (&tm->lock);
393 if (have_traces == 0)
394 s = format (s, "no traced allocations\n");
395
396 return s;
397}
398
399
400u8 *
401format_mheap (u8 * s, va_list * va)
402{
403 void *heap = va_arg (*va, u8 *);
404 int verbose = va_arg (*va, int);
Dave Barachaf7dd5b2018-08-23 17:08:44 -0400405 struct dlmallinfo mi;
Dave Barach6a5adc32018-07-04 10:56:23 -0400406 mheap_trace_main_t *tm = &mheap_trace_main;
407
408 mi = mspace_mallinfo (heap);
409
410 s = format (s, "total: %U, used: %U, free: %U, trimmable: %U",
411 format_msize, mi.arena,
412 format_msize, mi.uordblks,
413 format_msize, mi.fordblks, format_msize, mi.keepcost);
414 if (verbose > 0)
415 {
416 s = format (s, "\n free chunks %llu free fastbin blks %llu",
417 mi.ordblks, mi.smblks);
418 s =
419 format (s, "\n max total allocated %U", format_msize, mi.usmblks);
420 }
421
Dave Barachd67a4282019-06-15 12:46:13 -0400422 if (mspace_is_traced (heap))
423 s = format (s, "\n%U", format_mheap_trace, tm, verbose);
Dave Barach6a5adc32018-07-04 10:56:23 -0400424 return s;
425}
426
427void
428clib_mem_usage (clib_mem_usage_t * u)
429{
430 clib_warning ("unimp");
431}
432
Ole Troan92e30822019-06-16 12:33:51 +0200433void
434mheap_usage (void *heap, clib_mem_usage_t * usage)
435{
436 struct dlmallinfo mi = mspace_mallinfo (heap);
437
438 /* TODO: Fill in some more values */
439 usage->object_count = 0;
440 usage->bytes_total = mi.arena;
441 usage->bytes_overhead = 0;
442 usage->bytes_max = 0;
443 usage->bytes_used = mi.uordblks;
444 usage->bytes_free = mi.fordblks;
445 usage->bytes_free_reclaimed = 0;
446}
447
Dave Barach6a5adc32018-07-04 10:56:23 -0400448/* Call serial number for debugger breakpoints. */
449uword clib_mem_validate_serial = 0;
450
451void
452clib_mem_validate (void)
453{
454 clib_warning ("unimp");
455}
456
457void
458mheap_trace (void *v, int enable)
459{
460 (void) mspace_enable_disable_trace (v, enable);
461
462 if (enable == 0)
463 mheap_trace_main_free (&mheap_trace_main);
464}
465
466void
467clib_mem_trace (int enable)
468{
469 mheap_trace_main_t *tm = &mheap_trace_main;
Dave Barachd67a4282019-06-15 12:46:13 -0400470 void *current_heap = clib_mem_get_heap ();
Dave Barach6a5adc32018-07-04 10:56:23 -0400471
472 tm->enabled = enable;
Dave Barachd67a4282019-06-15 12:46:13 -0400473 mheap_trace (current_heap, enable);
474
475 if (enable)
476 tm->current_traced_mheap = current_heap;
477 else
478 tm->current_traced_mheap = 0;
479}
480
481int
482clib_mem_is_traced (void)
483{
484 return mspace_is_traced (clib_mem_get_heap ());
Dave Barach6a5adc32018-07-04 10:56:23 -0400485}
486
487uword
488clib_mem_trace_enable_disable (uword enable)
489{
490 uword rv;
491 mheap_trace_main_t *tm = &mheap_trace_main;
492
493 rv = tm->enabled;
494 tm->enabled = enable;
495 return rv;
496}
497
498/*
499 * These API functions seem like layering violations, but
500 * by introducing them we greatly reduce the number
501 * of code changes required to use dlmalloc spaces
502 */
503void *
504mheap_alloc_with_lock (void *memory, uword size, int locked)
505{
506 void *rv;
507 if (memory == 0)
508 return create_mspace (size, locked);
509 else
510 {
511 rv = create_mspace_with_base (memory, size, locked);
512 if (rv)
513 mspace_disable_expand (rv);
514 return rv;
515 }
516}
517
518/*
519 * fd.io coding-style-patch-verification: ON
520 *
521 * Local Variables:
522 * eval: (c-set-style "gnu")
523 * End:
524 */