blob: 663b2f162f73d47e204237cd527048890cf25dd0 [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
24void *clib_per_cpu_mheaps[CLIB_MAX_MHEAPS];
Dave Baracha690fdb2020-01-21 12:34:55 -050025void *clib_per_numa_mheaps[CLIB_MAX_NUMAS];
Dave Barach6a5adc32018-07-04 10:56:23 -040026
27typedef struct
28{
29 /* Address of callers: outer first, inner last. */
30 uword callers[12];
31
32 /* Count of allocations with this traceback. */
Dave Barach6a5adc32018-07-04 10:56:23 -040033 u32 n_allocations;
Dave Barach6a5adc32018-07-04 10:56:23 -040034
35 /* Count of bytes allocated with this traceback. */
36 u32 n_bytes;
37
38 /* Offset of this item */
39 uword offset;
40} mheap_trace_t;
41
42typedef struct
43{
44 clib_spinlock_t lock;
45 uword enabled;
46
47 mheap_trace_t *traces;
48
49 /* Indices of free traces. */
50 u32 *trace_free_list;
51
52 /* Hash table mapping callers to trace index. */
53 uword *trace_by_callers;
54
55 /* Hash table mapping mheap offset to trace index. */
56 uword *trace_index_by_offset;
Dave Barachd67a4282019-06-15 12:46:13 -040057
58 /* So we can easily shut off current segment trace, if any */
59 void *current_traced_mheap;
60
Dave Barach6a5adc32018-07-04 10:56:23 -040061} mheap_trace_main_t;
62
63mheap_trace_main_t mheap_trace_main;
64
65void
66mheap_get_trace (uword offset, uword size)
67{
68 mheap_trace_main_t *tm = &mheap_trace_main;
69 mheap_trace_t *t;
70 uword i, n_callers, trace_index, *p;
71 mheap_trace_t trace;
72 uword save_enabled;
73
Dave Barachd67a4282019-06-15 12:46:13 -040074 if (tm->enabled == 0 || (clib_mem_get_heap () != tm->current_traced_mheap))
Dave Barach6a5adc32018-07-04 10:56:23 -040075 return;
76
77 /* Spurious Coverity warnings be gone. */
Dave Barachb7b92992018-10-17 10:38:51 -040078 clib_memset (&trace, 0, sizeof (trace));
Dave Barach6a5adc32018-07-04 10:56:23 -040079
80 /* Skip our frame and mspace_get_aligned's frame */
81 n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2);
82 if (n_callers == 0)
83 return;
84
Dave Barach6a5adc32018-07-04 10:56:23 -040085 clib_spinlock_lock (&tm->lock);
86
87 /* Turn off tracing to avoid embarrassment... */
88 save_enabled = tm->enabled;
89 tm->enabled = 0;
90
91 if (!tm->trace_by_callers)
92 tm->trace_by_callers =
93 hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
94
95 p = hash_get_mem (tm->trace_by_callers, &trace.callers);
96 if (p)
97 {
98 trace_index = p[0];
99 t = tm->traces + trace_index;
100 }
101 else
102 {
103 i = vec_len (tm->trace_free_list);
104 if (i > 0)
105 {
106 trace_index = tm->trace_free_list[i - 1];
107 _vec_len (tm->trace_free_list) = i - 1;
108 }
109 else
110 {
111 mheap_trace_t *old_start = tm->traces;
112 mheap_trace_t *old_end = vec_end (tm->traces);
113
114 vec_add2 (tm->traces, t, 1);
115
116 if (tm->traces != old_start)
117 {
118 hash_pair_t *p;
119 mheap_trace_t *q;
120 /* *INDENT-OFF* */
121 hash_foreach_pair (p, tm->trace_by_callers,
122 ({
123 q = uword_to_pointer (p->key, mheap_trace_t *);
124 ASSERT (q >= old_start && q < old_end);
125 p->key = pointer_to_uword (tm->traces + (q - old_start));
126 }));
127 /* *INDENT-ON* */
128 }
129 trace_index = t - tm->traces;
130 }
131
132 t = tm->traces + trace_index;
133 t[0] = trace;
134 t->n_allocations = 0;
135 t->n_bytes = 0;
136 hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
137 }
138
139 t->n_allocations += 1;
140 t->n_bytes += size;
141 t->offset = offset; /* keep a sample to autopsy */
142 hash_set (tm->trace_index_by_offset, offset, t - tm->traces);
143 tm->enabled = save_enabled;
144 clib_spinlock_unlock (&tm->lock);
145}
146
147void
148mheap_put_trace (uword offset, uword size)
149{
150 mheap_trace_t *t;
151 uword trace_index, *p;
152 mheap_trace_main_t *tm = &mheap_trace_main;
153 uword save_enabled;
154
155 if (tm->enabled == 0)
156 return;
157
158 clib_spinlock_lock (&tm->lock);
159
160 /* Turn off tracing for a moment */
161 save_enabled = tm->enabled;
162 tm->enabled = 0;
163
164 p = hash_get (tm->trace_index_by_offset, offset);
165 if (!p)
166 {
167 tm->enabled = save_enabled;
168 clib_spinlock_unlock (&tm->lock);
169 return;
170 }
171
172 trace_index = p[0];
173 hash_unset (tm->trace_index_by_offset, offset);
174 ASSERT (trace_index < vec_len (tm->traces));
175
176 t = tm->traces + trace_index;
177 ASSERT (t->n_allocations > 0);
178 ASSERT (t->n_bytes >= size);
179 t->n_allocations -= 1;
180 t->n_bytes -= size;
181 if (t->n_allocations == 0)
182 {
183 hash_unset_mem (tm->trace_by_callers, t->callers);
184 vec_add1 (tm->trace_free_list, trace_index);
Dave Barachb7b92992018-10-17 10:38:51 -0400185 clib_memset (t, 0, sizeof (t[0]));
Dave Barach6a5adc32018-07-04 10:56:23 -0400186 }
187 tm->enabled = save_enabled;
188 clib_spinlock_unlock (&tm->lock);
189}
190
191always_inline void
192mheap_trace_main_free (mheap_trace_main_t * tm)
193{
194 vec_free (tm->traces);
195 vec_free (tm->trace_free_list);
196 hash_free (tm->trace_by_callers);
197 hash_free (tm->trace_index_by_offset);
198}
199
200/* Initialize CLIB heap based on memory/size given by user.
201 Set memory to 0 and CLIB will try to allocate its own heap. */
Dave Baracha690fdb2020-01-21 12:34:55 -0500202static void *
203clib_mem_init_internal (void *memory, uword memory_size, int set_heap)
Dave Barach6a5adc32018-07-04 10:56:23 -0400204{
205 u8 *heap;
206
207 if (memory)
208 {
209 heap = create_mspace_with_base (memory, memory_size, 1 /* locked */ );
210 mspace_disable_expand (heap);
211 }
212 else
213 heap = create_mspace (memory_size, 1 /* locked */ );
214
BenoƮt Ganneb2f09142019-12-16 15:37:28 +0100215 CLIB_MEM_POISON (mspace_least_addr (heap), mspace_footprint (heap));
216
Dave Baracha690fdb2020-01-21 12:34:55 -0500217 if (set_heap)
218 clib_mem_set_heap (heap);
Dave Barach6a5adc32018-07-04 10:56:23 -0400219
220 if (mheap_trace_main.lock == 0)
221 clib_spinlock_init (&mheap_trace_main.lock);
222
223 return heap;
224}
225
226void *
Dave Baracha690fdb2020-01-21 12:34:55 -0500227clib_mem_init (void *memory, uword memory_size)
228{
229 return clib_mem_init_internal (memory, memory_size,
230 1 /* do clib_mem_set_heap */ );
231}
232
233void *
Dave Barach6a5adc32018-07-04 10:56:23 -0400234clib_mem_init_thread_safe (void *memory, uword memory_size)
235{
Dave Baracha690fdb2020-01-21 12:34:55 -0500236 return clib_mem_init_internal (memory, memory_size,
237 1 /* do clib_mem_set_heap */ );
238}
239
240void *
Florin Coras4c959952020-02-09 18:09:31 +0000241clib_mem_init_thread_safe_numa (void *memory, uword memory_size, u8 numa)
Dave Baracha690fdb2020-01-21 12:34:55 -0500242{
Florin Coras4c959952020-02-09 18:09:31 +0000243 clib_mem_vm_alloc_t alloc = { 0 };
244 clib_error_t *err;
Dave Baracha690fdb2020-01-21 12:34:55 -0500245 void *heap;
Dave Baracha690fdb2020-01-21 12:34:55 -0500246
Florin Coras4c959952020-02-09 18:09:31 +0000247 alloc.size = memory_size;
248 alloc.flags = CLIB_MEM_VM_F_NUMA_FORCE;
249 alloc.numa_node = numa;
250 if ((err = clib_mem_vm_ext_alloc (&alloc)))
251 {
252 clib_error_report (err);
253 return 0;
254 }
255
256 heap = clib_mem_init_internal (memory, memory_size,
257 0 /* do NOT clib_mem_set_heap */ );
Dave Baracha690fdb2020-01-21 12:34:55 -0500258
259 ASSERT (heap);
260
Dave Baracha690fdb2020-01-21 12:34:55 -0500261 return heap;
Dave Barach6a5adc32018-07-04 10:56:23 -0400262}
263
Dave Barach6a5adc32018-07-04 10:56:23 -0400264u8 *
265format_clib_mem_usage (u8 * s, va_list * va)
266{
267 int verbose = va_arg (*va, int);
268 return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
269 verbose);
270}
271
272/*
273 * Magic decoder ring for mallinfo stats (ala dlmalloc):
274 *
275 * size_t arena; / * Non-mmapped space allocated (bytes) * /
276 * size_t ordblks; / * Number of free chunks * /
277 * size_t smblks; / * Number of free fastbin blocks * /
278 * size_t hblks; / * Number of mmapped regions * /
279 * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * /
280 * size_t usmblks; / * Maximum total allocated space (bytes) * /
281 * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * /
282 * size_t uordblks; / * Total allocated space (bytes) * /
283 * size_t fordblks; / * Total free space (bytes) * /
284 * size_t keepcost; / * Top-most, releasable space (bytes) * /
285 *
286 */
287
288u8 *
289format_msize (u8 * s, va_list * va)
290{
291 uword a = va_arg (*va, uword);
292
293 if (a >= 1ULL << 30)
294 s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30))));
295 else if (a >= 1ULL << 20)
296 s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20))));
297 else if (a >= 1ULL << 10)
298 s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10))));
299 else
300 s = format (s, "%lld", a);
301 return s;
302}
303
304static int
305mheap_trace_sort (const void *_t1, const void *_t2)
306{
307 const mheap_trace_t *t1 = _t1;
308 const mheap_trace_t *t2 = _t2;
309 word cmp;
310
311 cmp = (word) t2->n_bytes - (word) t1->n_bytes;
312 if (!cmp)
313 cmp = (word) t2->n_allocations - (word) t1->n_allocations;
314 return cmp;
315}
316
317u8 *
318format_mheap_trace (u8 * s, va_list * va)
319{
320 mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
321 int verbose = va_arg (*va, int);
322 int have_traces = 0;
323 int i;
324
325 clib_spinlock_lock (&tm->lock);
Dave Barachd67a4282019-06-15 12:46:13 -0400326 if (vec_len (tm->traces) > 0 &&
327 clib_mem_get_heap () == tm->current_traced_mheap)
Dave Barach6a5adc32018-07-04 10:56:23 -0400328 {
329 have_traces = 1;
330
331 /* Make a copy of traces since we'll be sorting them. */
332 mheap_trace_t *t, *traces_copy;
333 u32 indent, total_objects_traced;
334
335 traces_copy = vec_dup (tm->traces);
336
337 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
338 mheap_trace_sort);
339
340 total_objects_traced = 0;
341 s = format (s, "\n");
342 vec_foreach (t, traces_copy)
343 {
344 /* Skip over free elements. */
345 if (t->n_allocations == 0)
346 continue;
347
348 total_objects_traced += t->n_allocations;
349
350 /* When not verbose only report allocations of more than 1k. */
351 if (!verbose && t->n_bytes < 1024)
352 continue;
353
354 if (t == traces_copy)
355 s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
356 "Sample");
357 s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset);
358 indent = format_get_indent (s);
359 for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++)
360 {
361 if (i > 0)
362 s = format (s, "%U", format_white_space, indent);
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200363#if defined(CLIB_UNIX) && !defined(__APPLE__)
Dave Barach6a5adc32018-07-04 10:56:23 -0400364 /* $$$$ does this actually work? */
365 s =
366 format (s, " %U\n", format_clib_elf_symbol_with_address,
367 t->callers[i]);
368#else
369 s = format (s, " %p\n", t->callers[i]);
370#endif
371 }
372 }
373
374 s = format (s, "%d total traced objects\n", total_objects_traced);
375
376 vec_free (traces_copy);
377 }
378 clib_spinlock_unlock (&tm->lock);
379 if (have_traces == 0)
380 s = format (s, "no traced allocations\n");
381
382 return s;
383}
384
385
386u8 *
387format_mheap (u8 * s, va_list * va)
388{
389 void *heap = va_arg (*va, u8 *);
390 int verbose = va_arg (*va, int);
Dave Barachaf7dd5b2018-08-23 17:08:44 -0400391 struct dlmallinfo mi;
Dave Barach6a5adc32018-07-04 10:56:23 -0400392 mheap_trace_main_t *tm = &mheap_trace_main;
393
394 mi = mspace_mallinfo (heap);
395
396 s = format (s, "total: %U, used: %U, free: %U, trimmable: %U",
397 format_msize, mi.arena,
398 format_msize, mi.uordblks,
399 format_msize, mi.fordblks, format_msize, mi.keepcost);
400 if (verbose > 0)
401 {
402 s = format (s, "\n free chunks %llu free fastbin blks %llu",
403 mi.ordblks, mi.smblks);
404 s =
405 format (s, "\n max total allocated %U", format_msize, mi.usmblks);
406 }
407
Dave Barachd67a4282019-06-15 12:46:13 -0400408 if (mspace_is_traced (heap))
409 s = format (s, "\n%U", format_mheap_trace, tm, verbose);
Dave Barach6a5adc32018-07-04 10:56:23 -0400410 return s;
411}
412
413void
414clib_mem_usage (clib_mem_usage_t * u)
415{
416 clib_warning ("unimp");
417}
418
Ole Troan92e30822019-06-16 12:33:51 +0200419void
420mheap_usage (void *heap, clib_mem_usage_t * usage)
421{
422 struct dlmallinfo mi = mspace_mallinfo (heap);
423
424 /* TODO: Fill in some more values */
425 usage->object_count = 0;
426 usage->bytes_total = mi.arena;
427 usage->bytes_overhead = 0;
428 usage->bytes_max = 0;
429 usage->bytes_used = mi.uordblks;
430 usage->bytes_free = mi.fordblks;
431 usage->bytes_free_reclaimed = 0;
432}
433
Dave Barach6a5adc32018-07-04 10:56:23 -0400434/* Call serial number for debugger breakpoints. */
435uword clib_mem_validate_serial = 0;
436
437void
438clib_mem_validate (void)
439{
440 clib_warning ("unimp");
441}
442
443void
444mheap_trace (void *v, int enable)
445{
446 (void) mspace_enable_disable_trace (v, enable);
447
448 if (enable == 0)
449 mheap_trace_main_free (&mheap_trace_main);
450}
451
452void
453clib_mem_trace (int enable)
454{
455 mheap_trace_main_t *tm = &mheap_trace_main;
Dave Barachd67a4282019-06-15 12:46:13 -0400456 void *current_heap = clib_mem_get_heap ();
Dave Barach6a5adc32018-07-04 10:56:23 -0400457
458 tm->enabled = enable;
Dave Barachd67a4282019-06-15 12:46:13 -0400459 mheap_trace (current_heap, enable);
460
461 if (enable)
462 tm->current_traced_mheap = current_heap;
463 else
464 tm->current_traced_mheap = 0;
465}
466
467int
468clib_mem_is_traced (void)
469{
470 return mspace_is_traced (clib_mem_get_heap ());
Dave Barach6a5adc32018-07-04 10:56:23 -0400471}
472
473uword
474clib_mem_trace_enable_disable (uword enable)
475{
476 uword rv;
477 mheap_trace_main_t *tm = &mheap_trace_main;
478
479 rv = tm->enabled;
480 tm->enabled = enable;
481 return rv;
482}
483
484/*
485 * These API functions seem like layering violations, but
486 * by introducing them we greatly reduce the number
487 * of code changes required to use dlmalloc spaces
488 */
489void *
490mheap_alloc_with_lock (void *memory, uword size, int locked)
491{
492 void *rv;
493 if (memory == 0)
494 return create_mspace (size, locked);
495 else
496 {
497 rv = create_mspace_with_base (memory, size, locked);
498 if (rv)
499 mspace_disable_expand (rv);
500 return rv;
501 }
502}
503
504/*
505 * fd.io coding-style-patch-verification: ON
506 *
507 * Local Variables:
508 * eval: (c-set-style "gnu")
509 * End:
510 */