blob: d5ff21e58c0ccfac2ee7ef01ee76d4906992c523 [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>
Damjan Marion78925602024-05-23 13:06:39 +000022#include <vppinfra/stack.h>
Dave Barach6a5adc32018-07-04 10:56:23 -040023
Dave Barach6a5adc32018-07-04 10:56:23 -040024typedef struct
25{
Dave Barach6a5adc32018-07-04 10:56:23 -040026 clib_spinlock_t lock;
Dave Barach6a5adc32018-07-04 10:56:23 -040027
28 mheap_trace_t *traces;
29
30 /* Indices of free traces. */
31 u32 *trace_free_list;
32
33 /* Hash table mapping callers to trace index. */
34 uword *trace_by_callers;
35
36 /* Hash table mapping mheap offset to trace index. */
37 uword *trace_index_by_offset;
Dave Barachd67a4282019-06-15 12:46:13 -040038
39 /* So we can easily shut off current segment trace, if any */
Benoît Ganne22460d62022-11-16 19:36:15 +010040 const clib_mem_heap_t *current_traced_mheap;
Dave Barachd67a4282019-06-15 12:46:13 -040041
Dave Barach6a5adc32018-07-04 10:56:23 -040042} mheap_trace_main_t;
43
44mheap_trace_main_t mheap_trace_main;
45
Benoît Ganne22460d62022-11-16 19:36:15 +010046static __thread int mheap_trace_thread_disable;
47
48static void
49mheap_get_trace_internal (const clib_mem_heap_t *heap, uword offset,
50 uword size)
Dave Barach6a5adc32018-07-04 10:56:23 -040051{
52 mheap_trace_main_t *tm = &mheap_trace_main;
53 mheap_trace_t *t;
Damjan Marion78925602024-05-23 13:06:39 +000054 uword i, trace_index, *p;
55 mheap_trace_t trace = {};
56 int index;
Dave Barach6a5adc32018-07-04 10:56:23 -040057
Benoît Ganne22460d62022-11-16 19:36:15 +010058 if (heap != tm->current_traced_mheap || mheap_trace_thread_disable)
Dave Barach6a5adc32018-07-04 10:56:23 -040059 return;
60
Dave Barach6a5adc32018-07-04 10:56:23 -040061 clib_spinlock_lock (&tm->lock);
62
Benoît Ganne22460d62022-11-16 19:36:15 +010063 /* heap could have changed while we were waiting on the lock */
64 if (heap != tm->current_traced_mheap)
65 goto out;
66
67 /* Turn off tracing for this thread to avoid embarrassment... */
68 mheap_trace_thread_disable = 1;
Dave Barach6a5adc32018-07-04 10:56:23 -040069
Damjan Marion78925602024-05-23 13:06:39 +000070 index = -2; /* skip first 2 stack frames */
71 foreach_clib_stack_frame (sf)
72 {
73 if (index >= 0)
74 {
75 if (index == ARRAY_LEN (trace.callers))
76 break;
77 trace.callers[index] = sf->ip;
78 }
79 index++;
80 }
81
82 if (index < 1)
Benoît Ganneec4749a2020-09-18 10:05:37 +020083 goto out;
84
Dave Barach6a5adc32018-07-04 10:56:23 -040085 if (!tm->trace_by_callers)
86 tm->trace_by_callers =
87 hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
88
89 p = hash_get_mem (tm->trace_by_callers, &trace.callers);
90 if (p)
91 {
92 trace_index = p[0];
93 t = tm->traces + trace_index;
94 }
95 else
96 {
97 i = vec_len (tm->trace_free_list);
98 if (i > 0)
99 {
100 trace_index = tm->trace_free_list[i - 1];
Damjan Marion8bea5892022-04-04 22:40:45 +0200101 vec_set_len (tm->trace_free_list, i - 1);
Dave Barach6a5adc32018-07-04 10:56:23 -0400102 }
103 else
104 {
105 mheap_trace_t *old_start = tm->traces;
106 mheap_trace_t *old_end = vec_end (tm->traces);
107
108 vec_add2 (tm->traces, t, 1);
109
110 if (tm->traces != old_start)
111 {
112 hash_pair_t *p;
113 mheap_trace_t *q;
Dave Barach6a5adc32018-07-04 10:56:23 -0400114 hash_foreach_pair (p, tm->trace_by_callers,
115 ({
116 q = uword_to_pointer (p->key, mheap_trace_t *);
117 ASSERT (q >= old_start && q < old_end);
118 p->key = pointer_to_uword (tm->traces + (q - old_start));
119 }));
Dave Barach6a5adc32018-07-04 10:56:23 -0400120 }
121 trace_index = t - tm->traces;
122 }
123
124 t = tm->traces + trace_index;
125 t[0] = trace;
126 t->n_allocations = 0;
127 t->n_bytes = 0;
128 hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
129 }
130
131 t->n_allocations += 1;
132 t->n_bytes += size;
133 t->offset = offset; /* keep a sample to autopsy */
134 hash_set (tm->trace_index_by_offset, offset, t - tm->traces);
Benoît Ganneec4749a2020-09-18 10:05:37 +0200135
136out:
Benoît Ganne22460d62022-11-16 19:36:15 +0100137 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400138 clib_spinlock_unlock (&tm->lock);
139}
140
Benoît Ganne22460d62022-11-16 19:36:15 +0100141static void
142mheap_put_trace_internal (const clib_mem_heap_t *heap, uword offset,
143 uword size)
Dave Barach6a5adc32018-07-04 10:56:23 -0400144{
145 mheap_trace_t *t;
146 uword trace_index, *p;
147 mheap_trace_main_t *tm = &mheap_trace_main;
Dave Barach6a5adc32018-07-04 10:56:23 -0400148
Benoît Ganne22460d62022-11-16 19:36:15 +0100149 if (heap != tm->current_traced_mheap || mheap_trace_thread_disable)
Dave Barach6a5adc32018-07-04 10:56:23 -0400150 return;
151
152 clib_spinlock_lock (&tm->lock);
153
Benoît Ganne22460d62022-11-16 19:36:15 +0100154 /* heap could have changed while we were waiting on the lock */
155 if (heap != tm->current_traced_mheap)
156 goto out;
157
158 /* Turn off tracing for this thread for a moment */
159 mheap_trace_thread_disable = 1;
Dave Barach6a5adc32018-07-04 10:56:23 -0400160
161 p = hash_get (tm->trace_index_by_offset, offset);
162 if (!p)
Benoît Ganne22460d62022-11-16 19:36:15 +0100163 goto out;
Dave Barach6a5adc32018-07-04 10:56:23 -0400164
165 trace_index = p[0];
166 hash_unset (tm->trace_index_by_offset, offset);
167 ASSERT (trace_index < vec_len (tm->traces));
168
169 t = tm->traces + trace_index;
170 ASSERT (t->n_allocations > 0);
171 ASSERT (t->n_bytes >= size);
172 t->n_allocations -= 1;
173 t->n_bytes -= size;
174 if (t->n_allocations == 0)
175 {
176 hash_unset_mem (tm->trace_by_callers, t->callers);
177 vec_add1 (tm->trace_free_list, trace_index);
Dave Barachb7b92992018-10-17 10:38:51 -0400178 clib_memset (t, 0, sizeof (t[0]));
Dave Barach6a5adc32018-07-04 10:56:23 -0400179 }
Benoît Ganne22460d62022-11-16 19:36:15 +0100180
181out:
182 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400183 clib_spinlock_unlock (&tm->lock);
184}
185
Benoît Ganne22460d62022-11-16 19:36:15 +0100186void
187mheap_get_trace (uword offset, uword size)
188{
189 mheap_get_trace_internal (clib_mem_get_heap (), offset, size);
190}
191
192void
193mheap_put_trace (uword offset, uword size)
194{
195 mheap_put_trace_internal (clib_mem_get_heap (), offset, size);
196}
197
Dave Barach6a5adc32018-07-04 10:56:23 -0400198always_inline void
199mheap_trace_main_free (mheap_trace_main_t * tm)
200{
Benoît Ganne22460d62022-11-16 19:36:15 +0100201 CLIB_SPINLOCK_ASSERT_LOCKED (&tm->lock);
202 tm->current_traced_mheap = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400203 vec_free (tm->traces);
204 vec_free (tm->trace_free_list);
205 hash_free (tm->trace_by_callers);
206 hash_free (tm->trace_index_by_offset);
Benoît Ganne22460d62022-11-16 19:36:15 +0100207 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400208}
209
Damjan Marionbfa75d62020-10-06 17:46:06 +0200210static clib_mem_heap_t *
211clib_mem_create_heap_internal (void *base, uword size,
212 clib_mem_page_sz_t log2_page_sz, int is_locked,
213 char *name)
214{
215 clib_mem_heap_t *h;
216 u8 flags = 0;
217 int sz = sizeof (clib_mem_heap_t);
218
219 if (base == 0)
220 {
221 log2_page_sz = clib_mem_log2_page_size_validate (log2_page_sz);
222 size = round_pow2 (size, clib_mem_page_bytes (log2_page_sz));
223 base = clib_mem_vm_map_internal (0, log2_page_sz, size, -1, 0,
224 "main heap");
225
226 if (base == CLIB_MEM_VM_MAP_FAILED)
227 return 0;
228
229 flags = CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY;
230 }
231 else
232 log2_page_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
233
234 if (is_locked)
235 flags |= CLIB_MEM_HEAP_F_LOCKED;
236
237 h = base;
238 h->base = base;
239 h->size = size;
240 h->log2_page_sz = log2_page_sz;
241 h->flags = flags;
242 sz = strlen (name);
243 strcpy (h->name, name);
244 sz = round_pow2 (sz + sizeof (clib_mem_heap_t), 16);
245 h->mspace = create_mspace_with_base (base + sz, size - sz, is_locked);
246
247 mspace_disable_expand (h->mspace);
248
Damjan Marion79934e82022-04-05 12:40:31 +0200249 clib_mem_poison (mspace_least_addr (h->mspace),
Damjan Marionbfa75d62020-10-06 17:46:06 +0200250 mspace_footprint (h->mspace));
251
252 return h;
253}
254
Dave Barach6a5adc32018-07-04 10:56:23 -0400255/* Initialize CLIB heap based on memory/size given by user.
256 Set memory to 0 and CLIB will try to allocate its own heap. */
Dave Baracha690fdb2020-01-21 12:34:55 -0500257static void *
Damjan Marionbfa75d62020-10-06 17:46:06 +0200258clib_mem_init_internal (void *base, uword size,
259 clib_mem_page_sz_t log2_page_sz)
Dave Barach6a5adc32018-07-04 10:56:23 -0400260{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200261 clib_mem_heap_t *h;
Dave Barach6a5adc32018-07-04 10:56:23 -0400262
Damjan Marionc63e2a42020-09-16 21:36:00 +0200263 clib_mem_main_init ();
264
Damjan Marionbfa75d62020-10-06 17:46:06 +0200265 h = clib_mem_create_heap_internal (base, size, log2_page_sz,
266 1 /*is_locked */ , "main heap");
Damjan Marion6bfd0762020-09-11 22:16:53 +0200267
Damjan Marionbfa75d62020-10-06 17:46:06 +0200268 clib_mem_set_heap (h);
Dave Barach6a5adc32018-07-04 10:56:23 -0400269
270 if (mheap_trace_main.lock == 0)
Benoît Ganne22460d62022-11-16 19:36:15 +0100271 {
272 /* clib_spinlock_init() dynamically allocates the spinlock in the current
273 * per-cpu heap, but it is used for all traces accross all heaps and
274 * hence we can't really allocate it in the current per-cpu heap as it
275 * could be destroyed later */
276 static struct clib_spinlock_s mheap_trace_main_lock = {};
277 mheap_trace_main.lock = &mheap_trace_main_lock;
278 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400279
Damjan Marionbfa75d62020-10-06 17:46:06 +0200280 return h;
Dave Barach6a5adc32018-07-04 10:56:23 -0400281}
282
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200283__clib_export void *
Dave Baracha690fdb2020-01-21 12:34:55 -0500284clib_mem_init (void *memory, uword memory_size)
285{
286 return clib_mem_init_internal (memory, memory_size,
Damjan Marionbfa75d62020-10-06 17:46:06 +0200287 CLIB_MEM_PAGE_SZ_DEFAULT);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200288}
289
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200290__clib_export void *
Damjan Marion6bfd0762020-09-11 22:16:53 +0200291clib_mem_init_with_page_size (uword memory_size,
292 clib_mem_page_sz_t log2_page_sz)
293{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200294 return clib_mem_init_internal (0, memory_size, log2_page_sz);
Dave Baracha690fdb2020-01-21 12:34:55 -0500295}
296
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200297__clib_export void *
Dave Barach6a5adc32018-07-04 10:56:23 -0400298clib_mem_init_thread_safe (void *memory, uword memory_size)
299{
Dave Baracha690fdb2020-01-21 12:34:55 -0500300 return clib_mem_init_internal (memory, memory_size,
Damjan Marionbfa75d62020-10-06 17:46:06 +0200301 CLIB_MEM_PAGE_SZ_DEFAULT);
Dave Barach2b793412020-08-28 10:39:00 -0400302}
303
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200304__clib_export void
Dave Barach2b793412020-08-28 10:39:00 -0400305clib_mem_destroy (void)
306{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200307 mheap_trace_main_t *tm = &mheap_trace_main;
308 clib_mem_heap_t *heap = clib_mem_get_heap ();
Damjan Marionbfa75d62020-10-06 17:46:06 +0200309
Benoît Ganne22460d62022-11-16 19:36:15 +0100310 if (heap->mspace == tm->current_traced_mheap)
311 mheap_trace (heap, 0);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200312
313 destroy_mspace (heap->mspace);
Damjan Marion16ca1a92022-04-28 17:46:13 +0200314 clib_mem_vm_unmap (heap);
Dave Barach2b793412020-08-28 10:39:00 -0400315}
316
Damjan Marion4a251d02021-05-06 17:28:12 +0200317__clib_export u8 *
318format_clib_mem_usage (u8 *s, va_list *va)
Dave Barach6a5adc32018-07-04 10:56:23 -0400319{
320 int verbose = va_arg (*va, int);
321 return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
322 verbose);
323}
324
325/*
326 * Magic decoder ring for mallinfo stats (ala dlmalloc):
327 *
328 * size_t arena; / * Non-mmapped space allocated (bytes) * /
329 * size_t ordblks; / * Number of free chunks * /
330 * size_t smblks; / * Number of free fastbin blocks * /
331 * size_t hblks; / * Number of mmapped regions * /
332 * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * /
333 * size_t usmblks; / * Maximum total allocated space (bytes) * /
334 * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * /
335 * size_t uordblks; / * Total allocated space (bytes) * /
336 * size_t fordblks; / * Total free space (bytes) * /
337 * size_t keepcost; / * Top-most, releasable space (bytes) * /
338 *
339 */
340
341u8 *
342format_msize (u8 * s, va_list * va)
343{
344 uword a = va_arg (*va, uword);
345
346 if (a >= 1ULL << 30)
347 s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30))));
348 else if (a >= 1ULL << 20)
349 s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20))));
350 else if (a >= 1ULL << 10)
351 s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10))));
352 else
353 s = format (s, "%lld", a);
354 return s;
355}
356
357static int
358mheap_trace_sort (const void *_t1, const void *_t2)
359{
360 const mheap_trace_t *t1 = _t1;
361 const mheap_trace_t *t2 = _t2;
362 word cmp;
363
364 cmp = (word) t2->n_bytes - (word) t1->n_bytes;
365 if (!cmp)
366 cmp = (word) t2->n_allocations - (word) t1->n_allocations;
367 return cmp;
368}
369
370u8 *
371format_mheap_trace (u8 * s, va_list * va)
372{
373 mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
374 int verbose = va_arg (*va, int);
375 int have_traces = 0;
376 int i;
Benoît Ganneaf62f932023-02-08 18:54:30 +0100377 int n = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400378
379 clib_spinlock_lock (&tm->lock);
Dave Barachd67a4282019-06-15 12:46:13 -0400380 if (vec_len (tm->traces) > 0 &&
381 clib_mem_get_heap () == tm->current_traced_mheap)
Dave Barach6a5adc32018-07-04 10:56:23 -0400382 {
383 have_traces = 1;
384
385 /* Make a copy of traces since we'll be sorting them. */
386 mheap_trace_t *t, *traces_copy;
387 u32 indent, total_objects_traced;
388
389 traces_copy = vec_dup (tm->traces);
390
391 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
392 mheap_trace_sort);
393
394 total_objects_traced = 0;
395 s = format (s, "\n");
396 vec_foreach (t, traces_copy)
397 {
398 /* Skip over free elements. */
399 if (t->n_allocations == 0)
400 continue;
401
402 total_objects_traced += t->n_allocations;
403
Benoît Ganneaf62f932023-02-08 18:54:30 +0100404 /* When not verbose only report the 50 biggest allocations */
405 if (!verbose && n >= 50)
Dave Barach6a5adc32018-07-04 10:56:23 -0400406 continue;
Benoît Ganneaf62f932023-02-08 18:54:30 +0100407 n++;
Dave Barach6a5adc32018-07-04 10:56:23 -0400408
409 if (t == traces_copy)
410 s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
411 "Sample");
412 s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset);
413 indent = format_get_indent (s);
414 for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++)
415 {
416 if (i > 0)
417 s = format (s, "%U", format_white_space, indent);
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200418#if defined(CLIB_UNIX) && !defined(__APPLE__)
Dave Barach6a5adc32018-07-04 10:56:23 -0400419 /* $$$$ does this actually work? */
420 s =
421 format (s, " %U\n", format_clib_elf_symbol_with_address,
422 t->callers[i]);
423#else
424 s = format (s, " %p\n", t->callers[i]);
425#endif
426 }
427 }
428
429 s = format (s, "%d total traced objects\n", total_objects_traced);
430
431 vec_free (traces_copy);
432 }
433 clib_spinlock_unlock (&tm->lock);
434 if (have_traces == 0)
435 s = format (s, "no traced allocations\n");
436
437 return s;
438}
439
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200440__clib_export u8 *
Damjan Marion4537c302020-09-28 19:03:37 +0200441format_clib_mem_heap (u8 * s, va_list * va)
Dave Barach6a5adc32018-07-04 10:56:23 -0400442{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200443 clib_mem_heap_t *heap = va_arg (*va, clib_mem_heap_t *);
Dave Barach6a5adc32018-07-04 10:56:23 -0400444 int verbose = va_arg (*va, int);
Dave Barachaf7dd5b2018-08-23 17:08:44 -0400445 struct dlmallinfo mi;
Dave Barach6a5adc32018-07-04 10:56:23 -0400446 mheap_trace_main_t *tm = &mheap_trace_main;
Damjan Marionbfa75d62020-10-06 17:46:06 +0200447 u32 indent = format_get_indent (s) + 2;
Dave Barach6a5adc32018-07-04 10:56:23 -0400448
Damjan Marion4537c302020-09-28 19:03:37 +0200449 if (heap == 0)
450 heap = clib_mem_get_heap ();
451
Damjan Marionbfa75d62020-10-06 17:46:06 +0200452 mi = mspace_mallinfo (heap->mspace);
Dave Barach6a5adc32018-07-04 10:56:23 -0400453
Damjan Marionbfa75d62020-10-06 17:46:06 +0200454 s = format (s, "base %p, size %U",
455 heap->base, format_memory_size, heap->size);
456
457#define _(i,v,str) \
458 if (heap->flags & CLIB_MEM_HEAP_F_##v) s = format (s, ", %s", str);
459 foreach_clib_mem_heap_flag;
460#undef _
461
462 s = format (s, ", name '%s'", heap->name);
463
464 if (heap->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
465 {
466 clib_mem_page_stats_t stats;
467 clib_mem_get_page_stats (heap->base, heap->log2_page_sz,
468 heap->size >> heap->log2_page_sz, &stats);
469 s = format (s, "\n%U%U", format_white_space, indent,
470 format_clib_mem_page_stats, &stats);
471 }
472
473 s = format (s, "\n%Utotal: %U, used: %U, free: %U, trimmable: %U",
474 format_white_space, indent,
Dave Barach6a5adc32018-07-04 10:56:23 -0400475 format_msize, mi.arena,
476 format_msize, mi.uordblks,
477 format_msize, mi.fordblks, format_msize, mi.keepcost);
478 if (verbose > 0)
479 {
Damjan Marionbfa75d62020-10-06 17:46:06 +0200480 s = format (s, "\n%Ufree chunks %llu free fastbin blks %llu",
481 format_white_space, indent + 2, mi.ordblks, mi.smblks);
482 s = format (s, "\n%Umax total allocated %U",
483 format_white_space, indent + 2, format_msize, mi.usmblks);
Dave Barach6a5adc32018-07-04 10:56:23 -0400484 }
485
Damjan Marion299571a2022-03-19 00:07:52 +0100486 if (heap->flags & CLIB_MEM_HEAP_F_TRACED)
Dave Barachd67a4282019-06-15 12:46:13 -0400487 s = format (s, "\n%U", format_mheap_trace, tm, verbose);
Dave Barach6a5adc32018-07-04 10:56:23 -0400488 return s;
489}
490
Damjan Marion24738582022-03-31 15:12:20 +0200491__clib_export __clib_flatten void
492clib_mem_get_heap_usage (clib_mem_heap_t *heap, clib_mem_usage_t *usage)
Dave Barach6a5adc32018-07-04 10:56:23 -0400493{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200494 struct dlmallinfo mi = mspace_mallinfo (heap->mspace);
Ole Troan92e30822019-06-16 12:33:51 +0200495
Ole Troana606d922021-05-05 09:23:17 +0200496 usage->bytes_total = mi.arena; /* non-mmapped space allocated from system */
497 usage->bytes_used = mi.uordblks; /* total allocated space */
498 usage->bytes_free = mi.fordblks; /* total free space */
499 usage->bytes_used_mmap = mi.hblkhd; /* space in mmapped regions */
500 usage->bytes_max = mi.usmblks; /* maximum total allocated space */
501 usage->bytes_free_reclaimed = mi.ordblks; /* number of free chunks */
502 usage->bytes_overhead = mi.keepcost; /* releasable (via malloc_trim) space */
503
504 /* Not supported */
505 usage->bytes_used_sbrk = 0;
Ole Troan92e30822019-06-16 12:33:51 +0200506 usage->object_count = 0;
Ole Troan92e30822019-06-16 12:33:51 +0200507}
508
Dave Barach6a5adc32018-07-04 10:56:23 -0400509/* Call serial number for debugger breakpoints. */
510uword clib_mem_validate_serial = 0;
511
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200512__clib_export void
Damjan Marionbfa75d62020-10-06 17:46:06 +0200513mheap_trace (clib_mem_heap_t * h, int enable)
Dave Barach6a5adc32018-07-04 10:56:23 -0400514{
Benoît Ganne22460d62022-11-16 19:36:15 +0100515 mheap_trace_main_t *tm = &mheap_trace_main;
Dave Barach6a5adc32018-07-04 10:56:23 -0400516
Benoît Ganne22460d62022-11-16 19:36:15 +0100517 clib_spinlock_lock (&tm->lock);
518
519 if (tm->current_traced_mheap != 0 && tm->current_traced_mheap != h)
520 {
521 clib_warning ("tracing already enabled for another heap, ignoring");
522 goto out;
523 }
524
525 if (enable)
526 {
527 h->flags |= CLIB_MEM_HEAP_F_TRACED;
528 tm->current_traced_mheap = h;
529 }
530 else
531 {
532 h->flags &= ~CLIB_MEM_HEAP_F_TRACED;
533 mheap_trace_main_free (&mheap_trace_main);
534 }
535
536out:
537 clib_spinlock_unlock (&tm->lock);
Dave Barach6a5adc32018-07-04 10:56:23 -0400538}
539
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200540__clib_export void
Dave Barach6a5adc32018-07-04 10:56:23 -0400541clib_mem_trace (int enable)
542{
Dave Barachd67a4282019-06-15 12:46:13 -0400543 void *current_heap = clib_mem_get_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400544 mheap_trace (current_heap, enable);
Dave Barachd67a4282019-06-15 12:46:13 -0400545}
546
547int
548clib_mem_is_traced (void)
549{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200550 clib_mem_heap_t *h = clib_mem_get_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100551 return (h->flags &= CLIB_MEM_HEAP_F_TRACED) != 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400552}
553
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200554__clib_export uword
Dave Barach6a5adc32018-07-04 10:56:23 -0400555clib_mem_trace_enable_disable (uword enable)
556{
Benoît Ganne22460d62022-11-16 19:36:15 +0100557 uword rv = !mheap_trace_thread_disable;
558 mheap_trace_thread_disable = !enable;
Dave Barach6a5adc32018-07-04 10:56:23 -0400559 return rv;
560}
561
Matus Fabianef827b32024-07-19 15:24:10 +0200562__clib_export mheap_trace_t *
563clib_mem_trace_dup (clib_mem_heap_t *heap)
564{
565 mheap_trace_main_t *tm = &mheap_trace_main;
566 mheap_trace_t *traces_copy = 0;
567
568 clib_spinlock_lock (&tm->lock);
569 if (vec_len (tm->traces) > 0 && heap == tm->current_traced_mheap)
570 {
571 traces_copy = vec_dup (tm->traces);
572 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
573 mheap_trace_sort);
574 }
575 clib_spinlock_unlock (&tm->lock);
576 return traces_copy;
577}
578
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200579__clib_export clib_mem_heap_t *
Damjan Marion4537c302020-09-28 19:03:37 +0200580clib_mem_create_heap (void *base, uword size, int is_locked, char *fmt, ...)
Dave Barach6a5adc32018-07-04 10:56:23 -0400581{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200582 clib_mem_page_sz_t log2_page_sz = clib_mem_get_log2_page_size ();
583 clib_mem_heap_t *h;
584 char *name;
585 u8 *s = 0;
Damjan Marion4537c302020-09-28 19:03:37 +0200586
Damjan Marionbfa75d62020-10-06 17:46:06 +0200587 if (fmt == 0)
588 {
589 name = "";
590 }
591 else if (strchr (fmt, '%'))
592 {
593 va_list va;
594 va_start (va, fmt);
595 s = va_format (0, fmt, &va);
596 vec_add1 (s, 0);
597 va_end (va);
598 name = (char *) s;
599 }
600 else
601 name = fmt;
602
603 h = clib_mem_create_heap_internal (base, size, log2_page_sz, is_locked,
604 name);
605 vec_free (s);
606 return h;
Damjan Marion4537c302020-09-28 19:03:37 +0200607}
608
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200609__clib_export void
Damjan Marionbfa75d62020-10-06 17:46:06 +0200610clib_mem_destroy_heap (clib_mem_heap_t * h)
Damjan Marion4537c302020-09-28 19:03:37 +0200611{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200612 mheap_trace_main_t *tm = &mheap_trace_main;
613
Benoît Ganne22460d62022-11-16 19:36:15 +0100614 if (h->mspace == tm->current_traced_mheap)
615 mheap_trace (h, 0);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200616
617 destroy_mspace (h->mspace);
618 if (h->flags & CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY)
619 clib_mem_vm_unmap (h->base);
Damjan Marion4537c302020-09-28 19:03:37 +0200620}
621
Damjan Marion24738582022-03-31 15:12:20 +0200622__clib_export __clib_flatten uword
623clib_mem_get_heap_free_space (clib_mem_heap_t *h)
Damjan Marion4537c302020-09-28 19:03:37 +0200624{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200625 struct dlmallinfo dlminfo = mspace_mallinfo (h->mspace);
Damjan Marion4537c302020-09-28 19:03:37 +0200626 return dlminfo.fordblks;
Dave Barach6a5adc32018-07-04 10:56:23 -0400627}
628
Damjan Marion24738582022-03-31 15:12:20 +0200629__clib_export __clib_flatten void *
630clib_mem_get_heap_base (clib_mem_heap_t *h)
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200631{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200632 return h->base;
Damjan Marion4537c302020-09-28 19:03:37 +0200633}
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200634
Damjan Marion24738582022-03-31 15:12:20 +0200635__clib_export __clib_flatten uword
636clib_mem_get_heap_size (clib_mem_heap_t *heap)
Damjan Marion4537c302020-09-28 19:03:37 +0200637{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200638 return heap->size;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200639}
640
Damjan Marion299571a2022-03-19 00:07:52 +0100641/* Memory allocator which may call os_out_of_memory() if it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200642static inline void *
643clib_mem_heap_alloc_inline (void *heap, uword size, uword align,
644 int os_out_of_memory_on_failure)
Damjan Marion299571a2022-03-19 00:07:52 +0100645{
Damjan Marion24738582022-03-31 15:12:20 +0200646 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100647 void *p;
648
649 align = clib_max (CLIB_MEM_MIN_ALIGN, align);
650
651 p = mspace_memalign (h->mspace, align, size);
652
653 if (PREDICT_FALSE (0 == p))
654 {
655 if (os_out_of_memory_on_failure)
656 os_out_of_memory ();
657 return 0;
658 }
659
660 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
Benoît Ganne22460d62022-11-16 19:36:15 +0100661 mheap_get_trace_internal (h, pointer_to_uword (p), clib_mem_size (p));
Damjan Marion299571a2022-03-19 00:07:52 +0100662
Damjan Marion79934e82022-04-05 12:40:31 +0200663 clib_mem_unpoison (p, size);
Damjan Marion299571a2022-03-19 00:07:52 +0100664 return p;
665}
666
667/* Memory allocator which calls os_out_of_memory() when it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200668__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100669clib_mem_alloc (uword size)
670{
Damjan Marion24738582022-03-31 15:12:20 +0200671 return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN,
672 /* os_out_of_memory */ 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100673}
674
Damjan Marion24738582022-03-31 15:12:20 +0200675__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100676clib_mem_alloc_aligned (uword size, uword align)
677{
Damjan Marion24738582022-03-31 15:12:20 +0200678 return clib_mem_heap_alloc_inline (0, size, align,
679 /* os_out_of_memory */ 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100680}
681
682/* Memory allocator which calls os_out_of_memory() when it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200683__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100684clib_mem_alloc_or_null (uword size)
685{
Damjan Marion24738582022-03-31 15:12:20 +0200686 return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN,
687 /* os_out_of_memory */ 0);
Damjan Marion299571a2022-03-19 00:07:52 +0100688}
689
Damjan Marion24738582022-03-31 15:12:20 +0200690__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100691clib_mem_alloc_aligned_or_null (uword size, uword align)
692{
Damjan Marion24738582022-03-31 15:12:20 +0200693 return clib_mem_heap_alloc_inline (0, size, align,
694 /* os_out_of_memory */ 0);
Damjan Marion299571a2022-03-19 00:07:52 +0100695}
696
Damjan Marion24738582022-03-31 15:12:20 +0200697__clib_export __clib_flatten void *
698clib_mem_heap_alloc (void *heap, uword size)
699{
700 return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN,
701 /* os_out_of_memory */ 1);
702}
703
704__clib_export __clib_flatten void *
705clib_mem_heap_alloc_aligned (void *heap, uword size, uword align)
706{
707 return clib_mem_heap_alloc_inline (heap, size, align,
708 /* os_out_of_memory */ 1);
709}
710
711__clib_export __clib_flatten void *
712clib_mem_heap_alloc_or_null (void *heap, uword size)
713{
714 return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN,
715 /* os_out_of_memory */ 0);
716}
717
718__clib_export __clib_flatten void *
719clib_mem_heap_alloc_aligned_or_null (void *heap, uword size, uword align)
720{
721 return clib_mem_heap_alloc_inline (heap, size, align,
722 /* os_out_of_memory */ 0);
723}
724
725__clib_export __clib_flatten void *
726clib_mem_heap_realloc_aligned (void *heap, void *p, uword new_size,
727 uword align)
Damjan Marion299571a2022-03-19 00:07:52 +0100728{
729 uword old_alloc_size;
Damjan Marion24738582022-03-31 15:12:20 +0200730 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100731 void *new;
732
733 ASSERT (count_set_bits (align) == 1);
734
735 old_alloc_size = p ? mspace_usable_size (p) : 0;
736
737 if (new_size == old_alloc_size)
738 return p;
739
740 if (p && pointer_is_aligned (p, align) &&
741 mspace_realloc_in_place (h->mspace, p, new_size))
742 {
Damjan Marion79934e82022-04-05 12:40:31 +0200743 clib_mem_unpoison (p, new_size);
Leung Lai Yung69be0892022-05-22 13:25:53 +0000744 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
745 {
Benoît Ganne22460d62022-11-16 19:36:15 +0100746 mheap_put_trace_internal (h, pointer_to_uword (p), old_alloc_size);
747 mheap_get_trace_internal (h, pointer_to_uword (p),
748 clib_mem_size (p));
Leung Lai Yung69be0892022-05-22 13:25:53 +0000749 }
Damjan Marion299571a2022-03-19 00:07:52 +0100750 }
751 else
752 {
Damjan Marion24738582022-03-31 15:12:20 +0200753 new = clib_mem_heap_alloc_inline (h, new_size, align, 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100754
Damjan Marion79934e82022-04-05 12:40:31 +0200755 clib_mem_unpoison (new, new_size);
Damjan Marion299571a2022-03-19 00:07:52 +0100756 if (old_alloc_size)
757 {
Damjan Marion79934e82022-04-05 12:40:31 +0200758 clib_mem_unpoison (p, old_alloc_size);
Damjan Marion299571a2022-03-19 00:07:52 +0100759 clib_memcpy_fast (new, p, clib_min (new_size, old_alloc_size));
Damjan Marion24738582022-03-31 15:12:20 +0200760 clib_mem_heap_free (h, p);
Damjan Marion299571a2022-03-19 00:07:52 +0100761 }
762 p = new;
763 }
764
765 return p;
766}
767
Damjan Marion24738582022-03-31 15:12:20 +0200768__clib_export __clib_flatten void *
769clib_mem_heap_realloc (void *heap, void *p, uword new_size)
Damjan Marion299571a2022-03-19 00:07:52 +0100770{
Damjan Marion24738582022-03-31 15:12:20 +0200771 return clib_mem_heap_realloc_aligned (heap, p, new_size, CLIB_MEM_MIN_ALIGN);
Damjan Marion299571a2022-03-19 00:07:52 +0100772}
773
Damjan Marion24738582022-03-31 15:12:20 +0200774__clib_export __clib_flatten void *
775clib_mem_realloc_aligned (void *p, uword new_size, uword align)
Damjan Marion299571a2022-03-19 00:07:52 +0100776{
Damjan Marion24738582022-03-31 15:12:20 +0200777 return clib_mem_heap_realloc_aligned (0, p, new_size, align);
778}
779
780__clib_export __clib_flatten void *
781clib_mem_realloc (void *p, uword new_size)
782{
783 return clib_mem_heap_realloc_aligned (0, p, new_size, CLIB_MEM_MIN_ALIGN);
784}
785
786__clib_export __clib_flatten uword
787clib_mem_heap_is_heap_object (void *heap, void *p)
788{
789 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100790 return mspace_is_heap_object (h->mspace, p);
791}
792
Damjan Marion24738582022-03-31 15:12:20 +0200793__clib_export __clib_flatten uword
794clib_mem_is_heap_object (void *p)
Damjan Marion299571a2022-03-19 00:07:52 +0100795{
Damjan Marion24738582022-03-31 15:12:20 +0200796 return clib_mem_heap_is_heap_object (0, p);
797}
798
799__clib_export __clib_flatten void
800clib_mem_heap_free (void *heap, void *p)
801{
802 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100803 uword size = clib_mem_size (p);
804
805 /* Make sure object is in the correct heap. */
Damjan Marion24738582022-03-31 15:12:20 +0200806 ASSERT (clib_mem_heap_is_heap_object (h, p));
Damjan Marion299571a2022-03-19 00:07:52 +0100807
808 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
Benoît Ganne22460d62022-11-16 19:36:15 +0100809 mheap_put_trace_internal (h, pointer_to_uword (p), size);
Damjan Marion79934e82022-04-05 12:40:31 +0200810 clib_mem_poison (p, clib_mem_size (p));
Damjan Marion299571a2022-03-19 00:07:52 +0100811
812 mspace_free (h->mspace, p);
813}
814
Damjan Marion24738582022-03-31 15:12:20 +0200815__clib_export __clib_flatten void
816clib_mem_free (void *p)
817{
818 clib_mem_heap_free (0, p);
819}
820
821__clib_export __clib_flatten uword
Damjan Marion299571a2022-03-19 00:07:52 +0100822clib_mem_size (void *p)
823{
824 return mspace_usable_size (p);
825}
826
827__clib_export void
828clib_mem_free_s (void *p)
829{
830 uword size = clib_mem_size (p);
Damjan Marion79934e82022-04-05 12:40:31 +0200831 clib_mem_unpoison (p, size);
Damjan Marion299571a2022-03-19 00:07:52 +0100832 memset_s_inline (p, size, 0, size);
833 clib_mem_free (p);
834}