blob: 7944240390be3ee2a472cb63c234e27631bcabea [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 = {};
Benoît Ganne661fb342024-10-08 16:43:12 +020056 int n_callers;
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
Benoît Ganne661fb342024-10-08 16:43:12 +020070 /* Skip our frame and mspace_get_aligned's frame */
71 n_callers =
72 clib_stack_frame_get_raw (trace.callers, ARRAY_LEN (trace.callers), 2);
73 if (n_callers == 0)
Benoît Ganneec4749a2020-09-18 10:05:37 +020074 goto out;
75
Dave Barach6a5adc32018-07-04 10:56:23 -040076 if (!tm->trace_by_callers)
77 tm->trace_by_callers =
78 hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
79
80 p = hash_get_mem (tm->trace_by_callers, &trace.callers);
81 if (p)
82 {
83 trace_index = p[0];
84 t = tm->traces + trace_index;
85 }
86 else
87 {
88 i = vec_len (tm->trace_free_list);
89 if (i > 0)
90 {
91 trace_index = tm->trace_free_list[i - 1];
Damjan Marion8bea5892022-04-04 22:40:45 +020092 vec_set_len (tm->trace_free_list, i - 1);
Dave Barach6a5adc32018-07-04 10:56:23 -040093 }
94 else
95 {
96 mheap_trace_t *old_start = tm->traces;
97 mheap_trace_t *old_end = vec_end (tm->traces);
98
99 vec_add2 (tm->traces, t, 1);
100
101 if (tm->traces != old_start)
102 {
103 hash_pair_t *p;
104 mheap_trace_t *q;
Dave Barach6a5adc32018-07-04 10:56:23 -0400105 hash_foreach_pair (p, tm->trace_by_callers,
106 ({
107 q = uword_to_pointer (p->key, mheap_trace_t *);
108 ASSERT (q >= old_start && q < old_end);
109 p->key = pointer_to_uword (tm->traces + (q - old_start));
110 }));
Dave Barach6a5adc32018-07-04 10:56:23 -0400111 }
112 trace_index = t - tm->traces;
113 }
114
115 t = tm->traces + trace_index;
116 t[0] = trace;
117 t->n_allocations = 0;
118 t->n_bytes = 0;
119 hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
120 }
121
122 t->n_allocations += 1;
123 t->n_bytes += size;
124 t->offset = offset; /* keep a sample to autopsy */
125 hash_set (tm->trace_index_by_offset, offset, t - tm->traces);
Benoît Ganneec4749a2020-09-18 10:05:37 +0200126
127out:
Benoît Ganne22460d62022-11-16 19:36:15 +0100128 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400129 clib_spinlock_unlock (&tm->lock);
130}
131
Benoît Ganne22460d62022-11-16 19:36:15 +0100132static void
133mheap_put_trace_internal (const clib_mem_heap_t *heap, uword offset,
134 uword size)
Dave Barach6a5adc32018-07-04 10:56:23 -0400135{
136 mheap_trace_t *t;
137 uword trace_index, *p;
138 mheap_trace_main_t *tm = &mheap_trace_main;
Dave Barach6a5adc32018-07-04 10:56:23 -0400139
Benoît Ganne22460d62022-11-16 19:36:15 +0100140 if (heap != tm->current_traced_mheap || mheap_trace_thread_disable)
Dave Barach6a5adc32018-07-04 10:56:23 -0400141 return;
142
143 clib_spinlock_lock (&tm->lock);
144
Benoît Ganne22460d62022-11-16 19:36:15 +0100145 /* heap could have changed while we were waiting on the lock */
146 if (heap != tm->current_traced_mheap)
147 goto out;
148
149 /* Turn off tracing for this thread for a moment */
150 mheap_trace_thread_disable = 1;
Dave Barach6a5adc32018-07-04 10:56:23 -0400151
152 p = hash_get (tm->trace_index_by_offset, offset);
153 if (!p)
Benoît Ganne22460d62022-11-16 19:36:15 +0100154 goto out;
Dave Barach6a5adc32018-07-04 10:56:23 -0400155
156 trace_index = p[0];
157 hash_unset (tm->trace_index_by_offset, offset);
158 ASSERT (trace_index < vec_len (tm->traces));
159
160 t = tm->traces + trace_index;
161 ASSERT (t->n_allocations > 0);
162 ASSERT (t->n_bytes >= size);
163 t->n_allocations -= 1;
164 t->n_bytes -= size;
165 if (t->n_allocations == 0)
166 {
167 hash_unset_mem (tm->trace_by_callers, t->callers);
168 vec_add1 (tm->trace_free_list, trace_index);
Dave Barachb7b92992018-10-17 10:38:51 -0400169 clib_memset (t, 0, sizeof (t[0]));
Dave Barach6a5adc32018-07-04 10:56:23 -0400170 }
Benoît Ganne22460d62022-11-16 19:36:15 +0100171
172out:
173 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400174 clib_spinlock_unlock (&tm->lock);
175}
176
Benoît Ganne22460d62022-11-16 19:36:15 +0100177void
178mheap_get_trace (uword offset, uword size)
179{
180 mheap_get_trace_internal (clib_mem_get_heap (), offset, size);
181}
182
183void
184mheap_put_trace (uword offset, uword size)
185{
186 mheap_put_trace_internal (clib_mem_get_heap (), offset, size);
187}
188
Dave Barach6a5adc32018-07-04 10:56:23 -0400189always_inline void
190mheap_trace_main_free (mheap_trace_main_t * tm)
191{
Benoît Ganne22460d62022-11-16 19:36:15 +0100192 CLIB_SPINLOCK_ASSERT_LOCKED (&tm->lock);
193 tm->current_traced_mheap = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400194 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);
Benoît Ganne22460d62022-11-16 19:36:15 +0100198 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400199}
200
Damjan Marionbfa75d62020-10-06 17:46:06 +0200201static clib_mem_heap_t *
202clib_mem_create_heap_internal (void *base, uword size,
203 clib_mem_page_sz_t log2_page_sz, int is_locked,
204 char *name)
205{
206 clib_mem_heap_t *h;
207 u8 flags = 0;
208 int sz = sizeof (clib_mem_heap_t);
209
210 if (base == 0)
211 {
212 log2_page_sz = clib_mem_log2_page_size_validate (log2_page_sz);
213 size = round_pow2 (size, clib_mem_page_bytes (log2_page_sz));
214 base = clib_mem_vm_map_internal (0, log2_page_sz, size, -1, 0,
215 "main heap");
216
217 if (base == CLIB_MEM_VM_MAP_FAILED)
218 return 0;
219
220 flags = CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY;
221 }
222 else
223 log2_page_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
224
225 if (is_locked)
226 flags |= CLIB_MEM_HEAP_F_LOCKED;
227
228 h = base;
229 h->base = base;
230 h->size = size;
231 h->log2_page_sz = log2_page_sz;
232 h->flags = flags;
233 sz = strlen (name);
234 strcpy (h->name, name);
235 sz = round_pow2 (sz + sizeof (clib_mem_heap_t), 16);
236 h->mspace = create_mspace_with_base (base + sz, size - sz, is_locked);
237
238 mspace_disable_expand (h->mspace);
239
Damjan Marion79934e82022-04-05 12:40:31 +0200240 clib_mem_poison (mspace_least_addr (h->mspace),
Damjan Marionbfa75d62020-10-06 17:46:06 +0200241 mspace_footprint (h->mspace));
242
243 return h;
244}
245
Dave Barach6a5adc32018-07-04 10:56:23 -0400246/* Initialize CLIB heap based on memory/size given by user.
247 Set memory to 0 and CLIB will try to allocate its own heap. */
Dave Baracha690fdb2020-01-21 12:34:55 -0500248static void *
Damjan Marionbfa75d62020-10-06 17:46:06 +0200249clib_mem_init_internal (void *base, uword size,
250 clib_mem_page_sz_t log2_page_sz)
Dave Barach6a5adc32018-07-04 10:56:23 -0400251{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200252 clib_mem_heap_t *h;
Dave Barach6a5adc32018-07-04 10:56:23 -0400253
Damjan Marionc63e2a42020-09-16 21:36:00 +0200254 clib_mem_main_init ();
255
Damjan Marionbfa75d62020-10-06 17:46:06 +0200256 h = clib_mem_create_heap_internal (base, size, log2_page_sz,
257 1 /*is_locked */ , "main heap");
Damjan Marion6bfd0762020-09-11 22:16:53 +0200258
Damjan Marionbfa75d62020-10-06 17:46:06 +0200259 clib_mem_set_heap (h);
Dave Barach6a5adc32018-07-04 10:56:23 -0400260
261 if (mheap_trace_main.lock == 0)
Benoît Ganne22460d62022-11-16 19:36:15 +0100262 {
263 /* clib_spinlock_init() dynamically allocates the spinlock in the current
264 * per-cpu heap, but it is used for all traces accross all heaps and
265 * hence we can't really allocate it in the current per-cpu heap as it
266 * could be destroyed later */
267 static struct clib_spinlock_s mheap_trace_main_lock = {};
268 mheap_trace_main.lock = &mheap_trace_main_lock;
269 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400270
Damjan Marionbfa75d62020-10-06 17:46:06 +0200271 return h;
Dave Barach6a5adc32018-07-04 10:56:23 -0400272}
273
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200274__clib_export void *
Dave Baracha690fdb2020-01-21 12:34:55 -0500275clib_mem_init (void *memory, uword memory_size)
276{
277 return clib_mem_init_internal (memory, memory_size,
Damjan Marionbfa75d62020-10-06 17:46:06 +0200278 CLIB_MEM_PAGE_SZ_DEFAULT);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200279}
280
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200281__clib_export void *
Damjan Marion6bfd0762020-09-11 22:16:53 +0200282clib_mem_init_with_page_size (uword memory_size,
283 clib_mem_page_sz_t log2_page_sz)
284{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200285 return clib_mem_init_internal (0, memory_size, log2_page_sz);
Dave Baracha690fdb2020-01-21 12:34:55 -0500286}
287
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200288__clib_export void *
Dave Barach6a5adc32018-07-04 10:56:23 -0400289clib_mem_init_thread_safe (void *memory, uword memory_size)
290{
Dave Baracha690fdb2020-01-21 12:34:55 -0500291 return clib_mem_init_internal (memory, memory_size,
Damjan Marionbfa75d62020-10-06 17:46:06 +0200292 CLIB_MEM_PAGE_SZ_DEFAULT);
Dave Barach2b793412020-08-28 10:39:00 -0400293}
294
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200295__clib_export void
Dave Barach2b793412020-08-28 10:39:00 -0400296clib_mem_destroy (void)
297{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200298 mheap_trace_main_t *tm = &mheap_trace_main;
299 clib_mem_heap_t *heap = clib_mem_get_heap ();
Damjan Marionbfa75d62020-10-06 17:46:06 +0200300
Benoît Ganne22460d62022-11-16 19:36:15 +0100301 if (heap->mspace == tm->current_traced_mheap)
302 mheap_trace (heap, 0);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200303
304 destroy_mspace (heap->mspace);
Damjan Marion16ca1a92022-04-28 17:46:13 +0200305 clib_mem_vm_unmap (heap);
Dave Barach2b793412020-08-28 10:39:00 -0400306}
307
Damjan Marion4a251d02021-05-06 17:28:12 +0200308__clib_export u8 *
309format_clib_mem_usage (u8 *s, va_list *va)
Dave Barach6a5adc32018-07-04 10:56:23 -0400310{
311 int verbose = va_arg (*va, int);
312 return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
313 verbose);
314}
315
316/*
317 * Magic decoder ring for mallinfo stats (ala dlmalloc):
318 *
319 * size_t arena; / * Non-mmapped space allocated (bytes) * /
320 * size_t ordblks; / * Number of free chunks * /
321 * size_t smblks; / * Number of free fastbin blocks * /
322 * size_t hblks; / * Number of mmapped regions * /
323 * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * /
324 * size_t usmblks; / * Maximum total allocated space (bytes) * /
325 * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * /
326 * size_t uordblks; / * Total allocated space (bytes) * /
327 * size_t fordblks; / * Total free space (bytes) * /
328 * size_t keepcost; / * Top-most, releasable space (bytes) * /
329 *
330 */
331
332u8 *
333format_msize (u8 * s, va_list * va)
334{
335 uword a = va_arg (*va, uword);
336
337 if (a >= 1ULL << 30)
338 s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30))));
339 else if (a >= 1ULL << 20)
340 s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20))));
341 else if (a >= 1ULL << 10)
342 s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10))));
343 else
344 s = format (s, "%lld", a);
345 return s;
346}
347
348static int
349mheap_trace_sort (const void *_t1, const void *_t2)
350{
351 const mheap_trace_t *t1 = _t1;
352 const mheap_trace_t *t2 = _t2;
353 word cmp;
354
355 cmp = (word) t2->n_bytes - (word) t1->n_bytes;
356 if (!cmp)
357 cmp = (word) t2->n_allocations - (word) t1->n_allocations;
358 return cmp;
359}
360
361u8 *
362format_mheap_trace (u8 * s, va_list * va)
363{
364 mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
365 int verbose = va_arg (*va, int);
366 int have_traces = 0;
367 int i;
Benoît Ganneaf62f932023-02-08 18:54:30 +0100368 int n = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400369
370 clib_spinlock_lock (&tm->lock);
Dave Barachd67a4282019-06-15 12:46:13 -0400371 if (vec_len (tm->traces) > 0 &&
372 clib_mem_get_heap () == tm->current_traced_mheap)
Dave Barach6a5adc32018-07-04 10:56:23 -0400373 {
374 have_traces = 1;
375
376 /* Make a copy of traces since we'll be sorting them. */
377 mheap_trace_t *t, *traces_copy;
378 u32 indent, total_objects_traced;
379
380 traces_copy = vec_dup (tm->traces);
381
382 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
383 mheap_trace_sort);
384
385 total_objects_traced = 0;
386 s = format (s, "\n");
387 vec_foreach (t, traces_copy)
388 {
389 /* Skip over free elements. */
390 if (t->n_allocations == 0)
391 continue;
392
393 total_objects_traced += t->n_allocations;
394
Benoît Ganneaf62f932023-02-08 18:54:30 +0100395 /* When not verbose only report the 50 biggest allocations */
396 if (!verbose && n >= 50)
Dave Barach6a5adc32018-07-04 10:56:23 -0400397 continue;
Benoît Ganneaf62f932023-02-08 18:54:30 +0100398 n++;
Dave Barach6a5adc32018-07-04 10:56:23 -0400399
400 if (t == traces_copy)
401 s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
402 "Sample");
403 s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset);
404 indent = format_get_indent (s);
405 for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++)
406 {
407 if (i > 0)
408 s = format (s, "%U", format_white_space, indent);
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200409#if defined(CLIB_UNIX) && !defined(__APPLE__)
Dave Barach6a5adc32018-07-04 10:56:23 -0400410 /* $$$$ does this actually work? */
411 s =
412 format (s, " %U\n", format_clib_elf_symbol_with_address,
413 t->callers[i]);
414#else
415 s = format (s, " %p\n", t->callers[i]);
416#endif
417 }
418 }
419
420 s = format (s, "%d total traced objects\n", total_objects_traced);
421
422 vec_free (traces_copy);
423 }
424 clib_spinlock_unlock (&tm->lock);
425 if (have_traces == 0)
426 s = format (s, "no traced allocations\n");
427
428 return s;
429}
430
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200431__clib_export u8 *
Damjan Marion4537c302020-09-28 19:03:37 +0200432format_clib_mem_heap (u8 * s, va_list * va)
Dave Barach6a5adc32018-07-04 10:56:23 -0400433{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200434 clib_mem_heap_t *heap = va_arg (*va, clib_mem_heap_t *);
Dave Barach6a5adc32018-07-04 10:56:23 -0400435 int verbose = va_arg (*va, int);
Dave Barachaf7dd5b2018-08-23 17:08:44 -0400436 struct dlmallinfo mi;
Dave Barach6a5adc32018-07-04 10:56:23 -0400437 mheap_trace_main_t *tm = &mheap_trace_main;
Damjan Marionbfa75d62020-10-06 17:46:06 +0200438 u32 indent = format_get_indent (s) + 2;
Dave Barach6a5adc32018-07-04 10:56:23 -0400439
Damjan Marion4537c302020-09-28 19:03:37 +0200440 if (heap == 0)
441 heap = clib_mem_get_heap ();
442
Damjan Marionbfa75d62020-10-06 17:46:06 +0200443 mi = mspace_mallinfo (heap->mspace);
Dave Barach6a5adc32018-07-04 10:56:23 -0400444
Damjan Marionbfa75d62020-10-06 17:46:06 +0200445 s = format (s, "base %p, size %U",
446 heap->base, format_memory_size, heap->size);
447
448#define _(i,v,str) \
449 if (heap->flags & CLIB_MEM_HEAP_F_##v) s = format (s, ", %s", str);
450 foreach_clib_mem_heap_flag;
451#undef _
452
453 s = format (s, ", name '%s'", heap->name);
454
455 if (heap->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
456 {
457 clib_mem_page_stats_t stats;
458 clib_mem_get_page_stats (heap->base, heap->log2_page_sz,
459 heap->size >> heap->log2_page_sz, &stats);
460 s = format (s, "\n%U%U", format_white_space, indent,
461 format_clib_mem_page_stats, &stats);
462 }
463
464 s = format (s, "\n%Utotal: %U, used: %U, free: %U, trimmable: %U",
465 format_white_space, indent,
Dave Barach6a5adc32018-07-04 10:56:23 -0400466 format_msize, mi.arena,
467 format_msize, mi.uordblks,
468 format_msize, mi.fordblks, format_msize, mi.keepcost);
469 if (verbose > 0)
470 {
Damjan Marionbfa75d62020-10-06 17:46:06 +0200471 s = format (s, "\n%Ufree chunks %llu free fastbin blks %llu",
472 format_white_space, indent + 2, mi.ordblks, mi.smblks);
473 s = format (s, "\n%Umax total allocated %U",
474 format_white_space, indent + 2, format_msize, mi.usmblks);
Dave Barach6a5adc32018-07-04 10:56:23 -0400475 }
476
Damjan Marion299571a2022-03-19 00:07:52 +0100477 if (heap->flags & CLIB_MEM_HEAP_F_TRACED)
Dave Barachd67a4282019-06-15 12:46:13 -0400478 s = format (s, "\n%U", format_mheap_trace, tm, verbose);
Dave Barach6a5adc32018-07-04 10:56:23 -0400479 return s;
480}
481
Damjan Marion24738582022-03-31 15:12:20 +0200482__clib_export __clib_flatten void
483clib_mem_get_heap_usage (clib_mem_heap_t *heap, clib_mem_usage_t *usage)
Dave Barach6a5adc32018-07-04 10:56:23 -0400484{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200485 struct dlmallinfo mi = mspace_mallinfo (heap->mspace);
Ole Troan92e30822019-06-16 12:33:51 +0200486
Ole Troana606d922021-05-05 09:23:17 +0200487 usage->bytes_total = mi.arena; /* non-mmapped space allocated from system */
488 usage->bytes_used = mi.uordblks; /* total allocated space */
489 usage->bytes_free = mi.fordblks; /* total free space */
490 usage->bytes_used_mmap = mi.hblkhd; /* space in mmapped regions */
491 usage->bytes_max = mi.usmblks; /* maximum total allocated space */
492 usage->bytes_free_reclaimed = mi.ordblks; /* number of free chunks */
493 usage->bytes_overhead = mi.keepcost; /* releasable (via malloc_trim) space */
494
495 /* Not supported */
496 usage->bytes_used_sbrk = 0;
Ole Troan92e30822019-06-16 12:33:51 +0200497 usage->object_count = 0;
Ole Troan92e30822019-06-16 12:33:51 +0200498}
499
Dave Barach6a5adc32018-07-04 10:56:23 -0400500/* Call serial number for debugger breakpoints. */
501uword clib_mem_validate_serial = 0;
502
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200503__clib_export void
Damjan Marionbfa75d62020-10-06 17:46:06 +0200504mheap_trace (clib_mem_heap_t * h, int enable)
Dave Barach6a5adc32018-07-04 10:56:23 -0400505{
Benoît Ganne22460d62022-11-16 19:36:15 +0100506 mheap_trace_main_t *tm = &mheap_trace_main;
Dave Barach6a5adc32018-07-04 10:56:23 -0400507
Benoît Ganne22460d62022-11-16 19:36:15 +0100508 clib_spinlock_lock (&tm->lock);
509
510 if (tm->current_traced_mheap != 0 && tm->current_traced_mheap != h)
511 {
512 clib_warning ("tracing already enabled for another heap, ignoring");
513 goto out;
514 }
515
516 if (enable)
517 {
518 h->flags |= CLIB_MEM_HEAP_F_TRACED;
519 tm->current_traced_mheap = h;
520 }
521 else
522 {
523 h->flags &= ~CLIB_MEM_HEAP_F_TRACED;
524 mheap_trace_main_free (&mheap_trace_main);
525 }
526
527out:
528 clib_spinlock_unlock (&tm->lock);
Dave Barach6a5adc32018-07-04 10:56:23 -0400529}
530
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200531__clib_export void
Dave Barach6a5adc32018-07-04 10:56:23 -0400532clib_mem_trace (int enable)
533{
Dave Barachd67a4282019-06-15 12:46:13 -0400534 void *current_heap = clib_mem_get_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400535 mheap_trace (current_heap, enable);
Dave Barachd67a4282019-06-15 12:46:13 -0400536}
537
538int
539clib_mem_is_traced (void)
540{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200541 clib_mem_heap_t *h = clib_mem_get_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100542 return (h->flags &= CLIB_MEM_HEAP_F_TRACED) != 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400543}
544
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200545__clib_export uword
Dave Barach6a5adc32018-07-04 10:56:23 -0400546clib_mem_trace_enable_disable (uword enable)
547{
Benoît Ganne22460d62022-11-16 19:36:15 +0100548 uword rv = !mheap_trace_thread_disable;
549 mheap_trace_thread_disable = !enable;
Dave Barach6a5adc32018-07-04 10:56:23 -0400550 return rv;
551}
552
Matus Fabianef827b32024-07-19 15:24:10 +0200553__clib_export mheap_trace_t *
554clib_mem_trace_dup (clib_mem_heap_t *heap)
555{
556 mheap_trace_main_t *tm = &mheap_trace_main;
557 mheap_trace_t *traces_copy = 0;
558
559 clib_spinlock_lock (&tm->lock);
560 if (vec_len (tm->traces) > 0 && heap == tm->current_traced_mheap)
561 {
562 traces_copy = vec_dup (tm->traces);
563 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
564 mheap_trace_sort);
565 }
566 clib_spinlock_unlock (&tm->lock);
567 return traces_copy;
568}
569
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200570__clib_export clib_mem_heap_t *
Damjan Marion4537c302020-09-28 19:03:37 +0200571clib_mem_create_heap (void *base, uword size, int is_locked, char *fmt, ...)
Dave Barach6a5adc32018-07-04 10:56:23 -0400572{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200573 clib_mem_page_sz_t log2_page_sz = clib_mem_get_log2_page_size ();
574 clib_mem_heap_t *h;
575 char *name;
576 u8 *s = 0;
Damjan Marion4537c302020-09-28 19:03:37 +0200577
Damjan Marionbfa75d62020-10-06 17:46:06 +0200578 if (fmt == 0)
579 {
580 name = "";
581 }
582 else if (strchr (fmt, '%'))
583 {
584 va_list va;
585 va_start (va, fmt);
586 s = va_format (0, fmt, &va);
587 vec_add1 (s, 0);
588 va_end (va);
589 name = (char *) s;
590 }
591 else
592 name = fmt;
593
594 h = clib_mem_create_heap_internal (base, size, log2_page_sz, is_locked,
595 name);
596 vec_free (s);
597 return h;
Damjan Marion4537c302020-09-28 19:03:37 +0200598}
599
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200600__clib_export void
Damjan Marionbfa75d62020-10-06 17:46:06 +0200601clib_mem_destroy_heap (clib_mem_heap_t * h)
Damjan Marion4537c302020-09-28 19:03:37 +0200602{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200603 mheap_trace_main_t *tm = &mheap_trace_main;
604
Benoît Ganne22460d62022-11-16 19:36:15 +0100605 if (h->mspace == tm->current_traced_mheap)
606 mheap_trace (h, 0);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200607
608 destroy_mspace (h->mspace);
609 if (h->flags & CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY)
610 clib_mem_vm_unmap (h->base);
Damjan Marion4537c302020-09-28 19:03:37 +0200611}
612
Damjan Marion24738582022-03-31 15:12:20 +0200613__clib_export __clib_flatten uword
614clib_mem_get_heap_free_space (clib_mem_heap_t *h)
Damjan Marion4537c302020-09-28 19:03:37 +0200615{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200616 struct dlmallinfo dlminfo = mspace_mallinfo (h->mspace);
Damjan Marion4537c302020-09-28 19:03:37 +0200617 return dlminfo.fordblks;
Dave Barach6a5adc32018-07-04 10:56:23 -0400618}
619
Damjan Marion24738582022-03-31 15:12:20 +0200620__clib_export __clib_flatten void *
621clib_mem_get_heap_base (clib_mem_heap_t *h)
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200622{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200623 return h->base;
Damjan Marion4537c302020-09-28 19:03:37 +0200624}
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200625
Damjan Marion24738582022-03-31 15:12:20 +0200626__clib_export __clib_flatten uword
627clib_mem_get_heap_size (clib_mem_heap_t *heap)
Damjan Marion4537c302020-09-28 19:03:37 +0200628{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200629 return heap->size;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200630}
631
Damjan Marion299571a2022-03-19 00:07:52 +0100632/* Memory allocator which may call os_out_of_memory() if it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200633static inline void *
634clib_mem_heap_alloc_inline (void *heap, uword size, uword align,
635 int os_out_of_memory_on_failure)
Damjan Marion299571a2022-03-19 00:07:52 +0100636{
Damjan Marion24738582022-03-31 15:12:20 +0200637 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100638 void *p;
639
640 align = clib_max (CLIB_MEM_MIN_ALIGN, align);
641
642 p = mspace_memalign (h->mspace, align, size);
643
644 if (PREDICT_FALSE (0 == p))
645 {
646 if (os_out_of_memory_on_failure)
647 os_out_of_memory ();
648 return 0;
649 }
650
651 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
Benoît Ganne22460d62022-11-16 19:36:15 +0100652 mheap_get_trace_internal (h, pointer_to_uword (p), clib_mem_size (p));
Damjan Marion299571a2022-03-19 00:07:52 +0100653
Damjan Marion79934e82022-04-05 12:40:31 +0200654 clib_mem_unpoison (p, size);
Damjan Marion299571a2022-03-19 00:07:52 +0100655 return p;
656}
657
658/* Memory allocator which calls os_out_of_memory() when it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200659__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100660clib_mem_alloc (uword size)
661{
Damjan Marion24738582022-03-31 15:12:20 +0200662 return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN,
663 /* os_out_of_memory */ 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100664}
665
Damjan Marion24738582022-03-31 15:12:20 +0200666__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100667clib_mem_alloc_aligned (uword size, uword align)
668{
Damjan Marion24738582022-03-31 15:12:20 +0200669 return clib_mem_heap_alloc_inline (0, size, align,
670 /* os_out_of_memory */ 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100671}
672
673/* Memory allocator which calls os_out_of_memory() when it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200674__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100675clib_mem_alloc_or_null (uword size)
676{
Damjan Marion24738582022-03-31 15:12:20 +0200677 return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN,
678 /* os_out_of_memory */ 0);
Damjan Marion299571a2022-03-19 00:07:52 +0100679}
680
Damjan Marion24738582022-03-31 15:12:20 +0200681__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100682clib_mem_alloc_aligned_or_null (uword size, uword align)
683{
Damjan Marion24738582022-03-31 15:12:20 +0200684 return clib_mem_heap_alloc_inline (0, size, align,
685 /* os_out_of_memory */ 0);
Damjan Marion299571a2022-03-19 00:07:52 +0100686}
687
Damjan Marion24738582022-03-31 15:12:20 +0200688__clib_export __clib_flatten void *
689clib_mem_heap_alloc (void *heap, uword size)
690{
691 return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN,
692 /* os_out_of_memory */ 1);
693}
694
695__clib_export __clib_flatten void *
696clib_mem_heap_alloc_aligned (void *heap, uword size, uword align)
697{
698 return clib_mem_heap_alloc_inline (heap, size, align,
699 /* os_out_of_memory */ 1);
700}
701
702__clib_export __clib_flatten void *
703clib_mem_heap_alloc_or_null (void *heap, uword size)
704{
705 return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN,
706 /* os_out_of_memory */ 0);
707}
708
709__clib_export __clib_flatten void *
710clib_mem_heap_alloc_aligned_or_null (void *heap, uword size, uword align)
711{
712 return clib_mem_heap_alloc_inline (heap, size, align,
713 /* os_out_of_memory */ 0);
714}
715
716__clib_export __clib_flatten void *
717clib_mem_heap_realloc_aligned (void *heap, void *p, uword new_size,
718 uword align)
Damjan Marion299571a2022-03-19 00:07:52 +0100719{
720 uword old_alloc_size;
Damjan Marion24738582022-03-31 15:12:20 +0200721 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100722 void *new;
723
724 ASSERT (count_set_bits (align) == 1);
725
726 old_alloc_size = p ? mspace_usable_size (p) : 0;
727
728 if (new_size == old_alloc_size)
729 return p;
730
731 if (p && pointer_is_aligned (p, align) &&
732 mspace_realloc_in_place (h->mspace, p, new_size))
733 {
Damjan Marion79934e82022-04-05 12:40:31 +0200734 clib_mem_unpoison (p, new_size);
Leung Lai Yung69be0892022-05-22 13:25:53 +0000735 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
736 {
Benoît Ganne22460d62022-11-16 19:36:15 +0100737 mheap_put_trace_internal (h, pointer_to_uword (p), old_alloc_size);
738 mheap_get_trace_internal (h, pointer_to_uword (p),
739 clib_mem_size (p));
Leung Lai Yung69be0892022-05-22 13:25:53 +0000740 }
Damjan Marion299571a2022-03-19 00:07:52 +0100741 }
742 else
743 {
Damjan Marion24738582022-03-31 15:12:20 +0200744 new = clib_mem_heap_alloc_inline (h, new_size, align, 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100745
Damjan Marion79934e82022-04-05 12:40:31 +0200746 clib_mem_unpoison (new, new_size);
Damjan Marion299571a2022-03-19 00:07:52 +0100747 if (old_alloc_size)
748 {
Damjan Marion79934e82022-04-05 12:40:31 +0200749 clib_mem_unpoison (p, old_alloc_size);
Damjan Marion299571a2022-03-19 00:07:52 +0100750 clib_memcpy_fast (new, p, clib_min (new_size, old_alloc_size));
Damjan Marion24738582022-03-31 15:12:20 +0200751 clib_mem_heap_free (h, p);
Damjan Marion299571a2022-03-19 00:07:52 +0100752 }
753 p = new;
754 }
755
756 return p;
757}
758
Damjan Marion24738582022-03-31 15:12:20 +0200759__clib_export __clib_flatten void *
760clib_mem_heap_realloc (void *heap, void *p, uword new_size)
Damjan Marion299571a2022-03-19 00:07:52 +0100761{
Damjan Marion24738582022-03-31 15:12:20 +0200762 return clib_mem_heap_realloc_aligned (heap, p, new_size, CLIB_MEM_MIN_ALIGN);
Damjan Marion299571a2022-03-19 00:07:52 +0100763}
764
Damjan Marion24738582022-03-31 15:12:20 +0200765__clib_export __clib_flatten void *
766clib_mem_realloc_aligned (void *p, uword new_size, uword align)
Damjan Marion299571a2022-03-19 00:07:52 +0100767{
Damjan Marion24738582022-03-31 15:12:20 +0200768 return clib_mem_heap_realloc_aligned (0, p, new_size, align);
769}
770
771__clib_export __clib_flatten void *
772clib_mem_realloc (void *p, uword new_size)
773{
774 return clib_mem_heap_realloc_aligned (0, p, new_size, CLIB_MEM_MIN_ALIGN);
775}
776
777__clib_export __clib_flatten uword
778clib_mem_heap_is_heap_object (void *heap, void *p)
779{
780 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100781 return mspace_is_heap_object (h->mspace, p);
782}
783
Damjan Marion24738582022-03-31 15:12:20 +0200784__clib_export __clib_flatten uword
785clib_mem_is_heap_object (void *p)
Damjan Marion299571a2022-03-19 00:07:52 +0100786{
Damjan Marion24738582022-03-31 15:12:20 +0200787 return clib_mem_heap_is_heap_object (0, p);
788}
789
790__clib_export __clib_flatten void
791clib_mem_heap_free (void *heap, void *p)
792{
793 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100794 uword size = clib_mem_size (p);
795
796 /* Make sure object is in the correct heap. */
Damjan Marion24738582022-03-31 15:12:20 +0200797 ASSERT (clib_mem_heap_is_heap_object (h, p));
Damjan Marion299571a2022-03-19 00:07:52 +0100798
799 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
Benoît Ganne22460d62022-11-16 19:36:15 +0100800 mheap_put_trace_internal (h, pointer_to_uword (p), size);
Damjan Marion79934e82022-04-05 12:40:31 +0200801 clib_mem_poison (p, clib_mem_size (p));
Damjan Marion299571a2022-03-19 00:07:52 +0100802
803 mspace_free (h->mspace, p);
804}
805
Damjan Marion24738582022-03-31 15:12:20 +0200806__clib_export __clib_flatten void
807clib_mem_free (void *p)
808{
809 clib_mem_heap_free (0, p);
810}
811
812__clib_export __clib_flatten uword
Damjan Marion299571a2022-03-19 00:07:52 +0100813clib_mem_size (void *p)
814{
815 return mspace_usable_size (p);
816}
817
818__clib_export void
819clib_mem_free_s (void *p)
820{
821 uword size = clib_mem_size (p);
Damjan Marion79934e82022-04-05 12:40:31 +0200822 clib_mem_unpoison (p, size);
Damjan Marion299571a2022-03-19 00:07:52 +0100823 memset_s_inline (p, size, 0, size);
824 clib_mem_free (p);
825}