blob: e98687fff2ad39628282f7ad0699d86f9a180c4f [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{
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;
Dave Barach6a5adc32018-07-04 10:56:23 -040042
43 mheap_trace_t *traces;
44
45 /* Indices of free traces. */
46 u32 *trace_free_list;
47
48 /* Hash table mapping callers to trace index. */
49 uword *trace_by_callers;
50
51 /* Hash table mapping mheap offset to trace index. */
52 uword *trace_index_by_offset;
Dave Barachd67a4282019-06-15 12:46:13 -040053
54 /* So we can easily shut off current segment trace, if any */
Benoît Ganne22460d62022-11-16 19:36:15 +010055 const clib_mem_heap_t *current_traced_mheap;
Dave Barachd67a4282019-06-15 12:46:13 -040056
Dave Barach6a5adc32018-07-04 10:56:23 -040057} mheap_trace_main_t;
58
59mheap_trace_main_t mheap_trace_main;
60
Benoît Ganne22460d62022-11-16 19:36:15 +010061static __thread int mheap_trace_thread_disable;
62
63static void
64mheap_get_trace_internal (const clib_mem_heap_t *heap, uword offset,
65 uword size)
Dave Barach6a5adc32018-07-04 10:56:23 -040066{
67 mheap_trace_main_t *tm = &mheap_trace_main;
68 mheap_trace_t *t;
Damjan Marion78925602024-05-23 13:06:39 +000069 uword i, trace_index, *p;
70 mheap_trace_t trace = {};
71 int index;
Dave Barach6a5adc32018-07-04 10:56:23 -040072
Benoît Ganne22460d62022-11-16 19:36:15 +010073 if (heap != tm->current_traced_mheap || mheap_trace_thread_disable)
Dave Barach6a5adc32018-07-04 10:56:23 -040074 return;
75
Dave Barach6a5adc32018-07-04 10:56:23 -040076 clib_spinlock_lock (&tm->lock);
77
Benoît Ganne22460d62022-11-16 19:36:15 +010078 /* heap could have changed while we were waiting on the lock */
79 if (heap != tm->current_traced_mheap)
80 goto out;
81
82 /* Turn off tracing for this thread to avoid embarrassment... */
83 mheap_trace_thread_disable = 1;
Dave Barach6a5adc32018-07-04 10:56:23 -040084
Damjan Marion78925602024-05-23 13:06:39 +000085 index = -2; /* skip first 2 stack frames */
86 foreach_clib_stack_frame (sf)
87 {
88 if (index >= 0)
89 {
90 if (index == ARRAY_LEN (trace.callers))
91 break;
92 trace.callers[index] = sf->ip;
93 }
94 index++;
95 }
96
97 if (index < 1)
Benoît Ganneec4749a2020-09-18 10:05:37 +020098 goto out;
99
Dave Barach6a5adc32018-07-04 10:56:23 -0400100 if (!tm->trace_by_callers)
101 tm->trace_by_callers =
102 hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
103
104 p = hash_get_mem (tm->trace_by_callers, &trace.callers);
105 if (p)
106 {
107 trace_index = p[0];
108 t = tm->traces + trace_index;
109 }
110 else
111 {
112 i = vec_len (tm->trace_free_list);
113 if (i > 0)
114 {
115 trace_index = tm->trace_free_list[i - 1];
Damjan Marion8bea5892022-04-04 22:40:45 +0200116 vec_set_len (tm->trace_free_list, i - 1);
Dave Barach6a5adc32018-07-04 10:56:23 -0400117 }
118 else
119 {
120 mheap_trace_t *old_start = tm->traces;
121 mheap_trace_t *old_end = vec_end (tm->traces);
122
123 vec_add2 (tm->traces, t, 1);
124
125 if (tm->traces != old_start)
126 {
127 hash_pair_t *p;
128 mheap_trace_t *q;
Dave Barach6a5adc32018-07-04 10:56:23 -0400129 hash_foreach_pair (p, tm->trace_by_callers,
130 ({
131 q = uword_to_pointer (p->key, mheap_trace_t *);
132 ASSERT (q >= old_start && q < old_end);
133 p->key = pointer_to_uword (tm->traces + (q - old_start));
134 }));
Dave Barach6a5adc32018-07-04 10:56:23 -0400135 }
136 trace_index = t - tm->traces;
137 }
138
139 t = tm->traces + trace_index;
140 t[0] = trace;
141 t->n_allocations = 0;
142 t->n_bytes = 0;
143 hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
144 }
145
146 t->n_allocations += 1;
147 t->n_bytes += size;
148 t->offset = offset; /* keep a sample to autopsy */
149 hash_set (tm->trace_index_by_offset, offset, t - tm->traces);
Benoît Ganneec4749a2020-09-18 10:05:37 +0200150
151out:
Benoît Ganne22460d62022-11-16 19:36:15 +0100152 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400153 clib_spinlock_unlock (&tm->lock);
154}
155
Benoît Ganne22460d62022-11-16 19:36:15 +0100156static void
157mheap_put_trace_internal (const clib_mem_heap_t *heap, uword offset,
158 uword size)
Dave Barach6a5adc32018-07-04 10:56:23 -0400159{
160 mheap_trace_t *t;
161 uword trace_index, *p;
162 mheap_trace_main_t *tm = &mheap_trace_main;
Dave Barach6a5adc32018-07-04 10:56:23 -0400163
Benoît Ganne22460d62022-11-16 19:36:15 +0100164 if (heap != tm->current_traced_mheap || mheap_trace_thread_disable)
Dave Barach6a5adc32018-07-04 10:56:23 -0400165 return;
166
167 clib_spinlock_lock (&tm->lock);
168
Benoît Ganne22460d62022-11-16 19:36:15 +0100169 /* heap could have changed while we were waiting on the lock */
170 if (heap != tm->current_traced_mheap)
171 goto out;
172
173 /* Turn off tracing for this thread for a moment */
174 mheap_trace_thread_disable = 1;
Dave Barach6a5adc32018-07-04 10:56:23 -0400175
176 p = hash_get (tm->trace_index_by_offset, offset);
177 if (!p)
Benoît Ganne22460d62022-11-16 19:36:15 +0100178 goto out;
Dave Barach6a5adc32018-07-04 10:56:23 -0400179
180 trace_index = p[0];
181 hash_unset (tm->trace_index_by_offset, offset);
182 ASSERT (trace_index < vec_len (tm->traces));
183
184 t = tm->traces + trace_index;
185 ASSERT (t->n_allocations > 0);
186 ASSERT (t->n_bytes >= size);
187 t->n_allocations -= 1;
188 t->n_bytes -= size;
189 if (t->n_allocations == 0)
190 {
191 hash_unset_mem (tm->trace_by_callers, t->callers);
192 vec_add1 (tm->trace_free_list, trace_index);
Dave Barachb7b92992018-10-17 10:38:51 -0400193 clib_memset (t, 0, sizeof (t[0]));
Dave Barach6a5adc32018-07-04 10:56:23 -0400194 }
Benoît Ganne22460d62022-11-16 19:36:15 +0100195
196out:
197 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400198 clib_spinlock_unlock (&tm->lock);
199}
200
Benoît Ganne22460d62022-11-16 19:36:15 +0100201void
202mheap_get_trace (uword offset, uword size)
203{
204 mheap_get_trace_internal (clib_mem_get_heap (), offset, size);
205}
206
207void
208mheap_put_trace (uword offset, uword size)
209{
210 mheap_put_trace_internal (clib_mem_get_heap (), offset, size);
211}
212
Dave Barach6a5adc32018-07-04 10:56:23 -0400213always_inline void
214mheap_trace_main_free (mheap_trace_main_t * tm)
215{
Benoît Ganne22460d62022-11-16 19:36:15 +0100216 CLIB_SPINLOCK_ASSERT_LOCKED (&tm->lock);
217 tm->current_traced_mheap = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400218 vec_free (tm->traces);
219 vec_free (tm->trace_free_list);
220 hash_free (tm->trace_by_callers);
221 hash_free (tm->trace_index_by_offset);
Benoît Ganne22460d62022-11-16 19:36:15 +0100222 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400223}
224
Damjan Marionbfa75d62020-10-06 17:46:06 +0200225static clib_mem_heap_t *
226clib_mem_create_heap_internal (void *base, uword size,
227 clib_mem_page_sz_t log2_page_sz, int is_locked,
228 char *name)
229{
230 clib_mem_heap_t *h;
231 u8 flags = 0;
232 int sz = sizeof (clib_mem_heap_t);
233
234 if (base == 0)
235 {
236 log2_page_sz = clib_mem_log2_page_size_validate (log2_page_sz);
237 size = round_pow2 (size, clib_mem_page_bytes (log2_page_sz));
238 base = clib_mem_vm_map_internal (0, log2_page_sz, size, -1, 0,
239 "main heap");
240
241 if (base == CLIB_MEM_VM_MAP_FAILED)
242 return 0;
243
244 flags = CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY;
245 }
246 else
247 log2_page_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
248
249 if (is_locked)
250 flags |= CLIB_MEM_HEAP_F_LOCKED;
251
252 h = base;
253 h->base = base;
254 h->size = size;
255 h->log2_page_sz = log2_page_sz;
256 h->flags = flags;
257 sz = strlen (name);
258 strcpy (h->name, name);
259 sz = round_pow2 (sz + sizeof (clib_mem_heap_t), 16);
260 h->mspace = create_mspace_with_base (base + sz, size - sz, is_locked);
261
262 mspace_disable_expand (h->mspace);
263
Damjan Marion79934e82022-04-05 12:40:31 +0200264 clib_mem_poison (mspace_least_addr (h->mspace),
Damjan Marionbfa75d62020-10-06 17:46:06 +0200265 mspace_footprint (h->mspace));
266
267 return h;
268}
269
Dave Barach6a5adc32018-07-04 10:56:23 -0400270/* Initialize CLIB heap based on memory/size given by user.
271 Set memory to 0 and CLIB will try to allocate its own heap. */
Dave Baracha690fdb2020-01-21 12:34:55 -0500272static void *
Damjan Marionbfa75d62020-10-06 17:46:06 +0200273clib_mem_init_internal (void *base, uword size,
274 clib_mem_page_sz_t log2_page_sz)
Dave Barach6a5adc32018-07-04 10:56:23 -0400275{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200276 clib_mem_heap_t *h;
Dave Barach6a5adc32018-07-04 10:56:23 -0400277
Damjan Marionc63e2a42020-09-16 21:36:00 +0200278 clib_mem_main_init ();
279
Damjan Marionbfa75d62020-10-06 17:46:06 +0200280 h = clib_mem_create_heap_internal (base, size, log2_page_sz,
281 1 /*is_locked */ , "main heap");
Damjan Marion6bfd0762020-09-11 22:16:53 +0200282
Damjan Marionbfa75d62020-10-06 17:46:06 +0200283 clib_mem_set_heap (h);
Dave Barach6a5adc32018-07-04 10:56:23 -0400284
285 if (mheap_trace_main.lock == 0)
Benoît Ganne22460d62022-11-16 19:36:15 +0100286 {
287 /* clib_spinlock_init() dynamically allocates the spinlock in the current
288 * per-cpu heap, but it is used for all traces accross all heaps and
289 * hence we can't really allocate it in the current per-cpu heap as it
290 * could be destroyed later */
291 static struct clib_spinlock_s mheap_trace_main_lock = {};
292 mheap_trace_main.lock = &mheap_trace_main_lock;
293 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400294
Damjan Marionbfa75d62020-10-06 17:46:06 +0200295 return h;
Dave Barach6a5adc32018-07-04 10:56:23 -0400296}
297
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200298__clib_export void *
Dave Baracha690fdb2020-01-21 12:34:55 -0500299clib_mem_init (void *memory, uword memory_size)
300{
301 return clib_mem_init_internal (memory, memory_size,
Damjan Marionbfa75d62020-10-06 17:46:06 +0200302 CLIB_MEM_PAGE_SZ_DEFAULT);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200303}
304
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200305__clib_export void *
Damjan Marion6bfd0762020-09-11 22:16:53 +0200306clib_mem_init_with_page_size (uword memory_size,
307 clib_mem_page_sz_t log2_page_sz)
308{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200309 return clib_mem_init_internal (0, memory_size, log2_page_sz);
Dave Baracha690fdb2020-01-21 12:34:55 -0500310}
311
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200312__clib_export void *
Dave Barach6a5adc32018-07-04 10:56:23 -0400313clib_mem_init_thread_safe (void *memory, uword memory_size)
314{
Dave Baracha690fdb2020-01-21 12:34:55 -0500315 return clib_mem_init_internal (memory, memory_size,
Damjan Marionbfa75d62020-10-06 17:46:06 +0200316 CLIB_MEM_PAGE_SZ_DEFAULT);
Dave Barach2b793412020-08-28 10:39:00 -0400317}
318
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200319__clib_export void
Dave Barach2b793412020-08-28 10:39:00 -0400320clib_mem_destroy (void)
321{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200322 mheap_trace_main_t *tm = &mheap_trace_main;
323 clib_mem_heap_t *heap = clib_mem_get_heap ();
Damjan Marionbfa75d62020-10-06 17:46:06 +0200324
Benoît Ganne22460d62022-11-16 19:36:15 +0100325 if (heap->mspace == tm->current_traced_mheap)
326 mheap_trace (heap, 0);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200327
328 destroy_mspace (heap->mspace);
Damjan Marion16ca1a92022-04-28 17:46:13 +0200329 clib_mem_vm_unmap (heap);
Dave Barach2b793412020-08-28 10:39:00 -0400330}
331
Damjan Marion4a251d02021-05-06 17:28:12 +0200332__clib_export u8 *
333format_clib_mem_usage (u8 *s, va_list *va)
Dave Barach6a5adc32018-07-04 10:56:23 -0400334{
335 int verbose = va_arg (*va, int);
336 return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
337 verbose);
338}
339
340/*
341 * Magic decoder ring for mallinfo stats (ala dlmalloc):
342 *
343 * size_t arena; / * Non-mmapped space allocated (bytes) * /
344 * size_t ordblks; / * Number of free chunks * /
345 * size_t smblks; / * Number of free fastbin blocks * /
346 * size_t hblks; / * Number of mmapped regions * /
347 * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * /
348 * size_t usmblks; / * Maximum total allocated space (bytes) * /
349 * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * /
350 * size_t uordblks; / * Total allocated space (bytes) * /
351 * size_t fordblks; / * Total free space (bytes) * /
352 * size_t keepcost; / * Top-most, releasable space (bytes) * /
353 *
354 */
355
356u8 *
357format_msize (u8 * s, va_list * va)
358{
359 uword a = va_arg (*va, uword);
360
361 if (a >= 1ULL << 30)
362 s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30))));
363 else if (a >= 1ULL << 20)
364 s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20))));
365 else if (a >= 1ULL << 10)
366 s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10))));
367 else
368 s = format (s, "%lld", a);
369 return s;
370}
371
372static int
373mheap_trace_sort (const void *_t1, const void *_t2)
374{
375 const mheap_trace_t *t1 = _t1;
376 const mheap_trace_t *t2 = _t2;
377 word cmp;
378
379 cmp = (word) t2->n_bytes - (word) t1->n_bytes;
380 if (!cmp)
381 cmp = (word) t2->n_allocations - (word) t1->n_allocations;
382 return cmp;
383}
384
385u8 *
386format_mheap_trace (u8 * s, va_list * va)
387{
388 mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
389 int verbose = va_arg (*va, int);
390 int have_traces = 0;
391 int i;
Benoît Ganneaf62f932023-02-08 18:54:30 +0100392 int n = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400393
394 clib_spinlock_lock (&tm->lock);
Dave Barachd67a4282019-06-15 12:46:13 -0400395 if (vec_len (tm->traces) > 0 &&
396 clib_mem_get_heap () == tm->current_traced_mheap)
Dave Barach6a5adc32018-07-04 10:56:23 -0400397 {
398 have_traces = 1;
399
400 /* Make a copy of traces since we'll be sorting them. */
401 mheap_trace_t *t, *traces_copy;
402 u32 indent, total_objects_traced;
403
404 traces_copy = vec_dup (tm->traces);
405
406 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
407 mheap_trace_sort);
408
409 total_objects_traced = 0;
410 s = format (s, "\n");
411 vec_foreach (t, traces_copy)
412 {
413 /* Skip over free elements. */
414 if (t->n_allocations == 0)
415 continue;
416
417 total_objects_traced += t->n_allocations;
418
Benoît Ganneaf62f932023-02-08 18:54:30 +0100419 /* When not verbose only report the 50 biggest allocations */
420 if (!verbose && n >= 50)
Dave Barach6a5adc32018-07-04 10:56:23 -0400421 continue;
Benoît Ganneaf62f932023-02-08 18:54:30 +0100422 n++;
Dave Barach6a5adc32018-07-04 10:56:23 -0400423
424 if (t == traces_copy)
425 s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
426 "Sample");
427 s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset);
428 indent = format_get_indent (s);
429 for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++)
430 {
431 if (i > 0)
432 s = format (s, "%U", format_white_space, indent);
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200433#if defined(CLIB_UNIX) && !defined(__APPLE__)
Dave Barach6a5adc32018-07-04 10:56:23 -0400434 /* $$$$ does this actually work? */
435 s =
436 format (s, " %U\n", format_clib_elf_symbol_with_address,
437 t->callers[i]);
438#else
439 s = format (s, " %p\n", t->callers[i]);
440#endif
441 }
442 }
443
444 s = format (s, "%d total traced objects\n", total_objects_traced);
445
446 vec_free (traces_copy);
447 }
448 clib_spinlock_unlock (&tm->lock);
449 if (have_traces == 0)
450 s = format (s, "no traced allocations\n");
451
452 return s;
453}
454
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200455__clib_export u8 *
Damjan Marion4537c302020-09-28 19:03:37 +0200456format_clib_mem_heap (u8 * s, va_list * va)
Dave Barach6a5adc32018-07-04 10:56:23 -0400457{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200458 clib_mem_heap_t *heap = va_arg (*va, clib_mem_heap_t *);
Dave Barach6a5adc32018-07-04 10:56:23 -0400459 int verbose = va_arg (*va, int);
Dave Barachaf7dd5b2018-08-23 17:08:44 -0400460 struct dlmallinfo mi;
Dave Barach6a5adc32018-07-04 10:56:23 -0400461 mheap_trace_main_t *tm = &mheap_trace_main;
Damjan Marionbfa75d62020-10-06 17:46:06 +0200462 u32 indent = format_get_indent (s) + 2;
Dave Barach6a5adc32018-07-04 10:56:23 -0400463
Damjan Marion4537c302020-09-28 19:03:37 +0200464 if (heap == 0)
465 heap = clib_mem_get_heap ();
466
Damjan Marionbfa75d62020-10-06 17:46:06 +0200467 mi = mspace_mallinfo (heap->mspace);
Dave Barach6a5adc32018-07-04 10:56:23 -0400468
Damjan Marionbfa75d62020-10-06 17:46:06 +0200469 s = format (s, "base %p, size %U",
470 heap->base, format_memory_size, heap->size);
471
472#define _(i,v,str) \
473 if (heap->flags & CLIB_MEM_HEAP_F_##v) s = format (s, ", %s", str);
474 foreach_clib_mem_heap_flag;
475#undef _
476
477 s = format (s, ", name '%s'", heap->name);
478
479 if (heap->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
480 {
481 clib_mem_page_stats_t stats;
482 clib_mem_get_page_stats (heap->base, heap->log2_page_sz,
483 heap->size >> heap->log2_page_sz, &stats);
484 s = format (s, "\n%U%U", format_white_space, indent,
485 format_clib_mem_page_stats, &stats);
486 }
487
488 s = format (s, "\n%Utotal: %U, used: %U, free: %U, trimmable: %U",
489 format_white_space, indent,
Dave Barach6a5adc32018-07-04 10:56:23 -0400490 format_msize, mi.arena,
491 format_msize, mi.uordblks,
492 format_msize, mi.fordblks, format_msize, mi.keepcost);
493 if (verbose > 0)
494 {
Damjan Marionbfa75d62020-10-06 17:46:06 +0200495 s = format (s, "\n%Ufree chunks %llu free fastbin blks %llu",
496 format_white_space, indent + 2, mi.ordblks, mi.smblks);
497 s = format (s, "\n%Umax total allocated %U",
498 format_white_space, indent + 2, format_msize, mi.usmblks);
Dave Barach6a5adc32018-07-04 10:56:23 -0400499 }
500
Damjan Marion299571a2022-03-19 00:07:52 +0100501 if (heap->flags & CLIB_MEM_HEAP_F_TRACED)
Dave Barachd67a4282019-06-15 12:46:13 -0400502 s = format (s, "\n%U", format_mheap_trace, tm, verbose);
Dave Barach6a5adc32018-07-04 10:56:23 -0400503 return s;
504}
505
Damjan Marion24738582022-03-31 15:12:20 +0200506__clib_export __clib_flatten void
507clib_mem_get_heap_usage (clib_mem_heap_t *heap, clib_mem_usage_t *usage)
Dave Barach6a5adc32018-07-04 10:56:23 -0400508{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200509 struct dlmallinfo mi = mspace_mallinfo (heap->mspace);
Ole Troan92e30822019-06-16 12:33:51 +0200510
Ole Troana606d922021-05-05 09:23:17 +0200511 usage->bytes_total = mi.arena; /* non-mmapped space allocated from system */
512 usage->bytes_used = mi.uordblks; /* total allocated space */
513 usage->bytes_free = mi.fordblks; /* total free space */
514 usage->bytes_used_mmap = mi.hblkhd; /* space in mmapped regions */
515 usage->bytes_max = mi.usmblks; /* maximum total allocated space */
516 usage->bytes_free_reclaimed = mi.ordblks; /* number of free chunks */
517 usage->bytes_overhead = mi.keepcost; /* releasable (via malloc_trim) space */
518
519 /* Not supported */
520 usage->bytes_used_sbrk = 0;
Ole Troan92e30822019-06-16 12:33:51 +0200521 usage->object_count = 0;
Ole Troan92e30822019-06-16 12:33:51 +0200522}
523
Dave Barach6a5adc32018-07-04 10:56:23 -0400524/* Call serial number for debugger breakpoints. */
525uword clib_mem_validate_serial = 0;
526
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200527__clib_export void
Damjan Marionbfa75d62020-10-06 17:46:06 +0200528mheap_trace (clib_mem_heap_t * h, int enable)
Dave Barach6a5adc32018-07-04 10:56:23 -0400529{
Benoît Ganne22460d62022-11-16 19:36:15 +0100530 mheap_trace_main_t *tm = &mheap_trace_main;
Dave Barach6a5adc32018-07-04 10:56:23 -0400531
Benoît Ganne22460d62022-11-16 19:36:15 +0100532 clib_spinlock_lock (&tm->lock);
533
534 if (tm->current_traced_mheap != 0 && tm->current_traced_mheap != h)
535 {
536 clib_warning ("tracing already enabled for another heap, ignoring");
537 goto out;
538 }
539
540 if (enable)
541 {
542 h->flags |= CLIB_MEM_HEAP_F_TRACED;
543 tm->current_traced_mheap = h;
544 }
545 else
546 {
547 h->flags &= ~CLIB_MEM_HEAP_F_TRACED;
548 mheap_trace_main_free (&mheap_trace_main);
549 }
550
551out:
552 clib_spinlock_unlock (&tm->lock);
Dave Barach6a5adc32018-07-04 10:56:23 -0400553}
554
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200555__clib_export void
Dave Barach6a5adc32018-07-04 10:56:23 -0400556clib_mem_trace (int enable)
557{
Dave Barachd67a4282019-06-15 12:46:13 -0400558 void *current_heap = clib_mem_get_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400559 mheap_trace (current_heap, enable);
Dave Barachd67a4282019-06-15 12:46:13 -0400560}
561
562int
563clib_mem_is_traced (void)
564{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200565 clib_mem_heap_t *h = clib_mem_get_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100566 return (h->flags &= CLIB_MEM_HEAP_F_TRACED) != 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400567}
568
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200569__clib_export uword
Dave Barach6a5adc32018-07-04 10:56:23 -0400570clib_mem_trace_enable_disable (uword enable)
571{
Benoît Ganne22460d62022-11-16 19:36:15 +0100572 uword rv = !mheap_trace_thread_disable;
573 mheap_trace_thread_disable = !enable;
Dave Barach6a5adc32018-07-04 10:56:23 -0400574 return rv;
575}
576
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200577__clib_export clib_mem_heap_t *
Damjan Marion4537c302020-09-28 19:03:37 +0200578clib_mem_create_heap (void *base, uword size, int is_locked, char *fmt, ...)
Dave Barach6a5adc32018-07-04 10:56:23 -0400579{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200580 clib_mem_page_sz_t log2_page_sz = clib_mem_get_log2_page_size ();
581 clib_mem_heap_t *h;
582 char *name;
583 u8 *s = 0;
Damjan Marion4537c302020-09-28 19:03:37 +0200584
Damjan Marionbfa75d62020-10-06 17:46:06 +0200585 if (fmt == 0)
586 {
587 name = "";
588 }
589 else if (strchr (fmt, '%'))
590 {
591 va_list va;
592 va_start (va, fmt);
593 s = va_format (0, fmt, &va);
594 vec_add1 (s, 0);
595 va_end (va);
596 name = (char *) s;
597 }
598 else
599 name = fmt;
600
601 h = clib_mem_create_heap_internal (base, size, log2_page_sz, is_locked,
602 name);
603 vec_free (s);
604 return h;
Damjan Marion4537c302020-09-28 19:03:37 +0200605}
606
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200607__clib_export void
Damjan Marionbfa75d62020-10-06 17:46:06 +0200608clib_mem_destroy_heap (clib_mem_heap_t * h)
Damjan Marion4537c302020-09-28 19:03:37 +0200609{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200610 mheap_trace_main_t *tm = &mheap_trace_main;
611
Benoît Ganne22460d62022-11-16 19:36:15 +0100612 if (h->mspace == tm->current_traced_mheap)
613 mheap_trace (h, 0);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200614
615 destroy_mspace (h->mspace);
616 if (h->flags & CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY)
617 clib_mem_vm_unmap (h->base);
Damjan Marion4537c302020-09-28 19:03:37 +0200618}
619
Damjan Marion24738582022-03-31 15:12:20 +0200620__clib_export __clib_flatten uword
621clib_mem_get_heap_free_space (clib_mem_heap_t *h)
Damjan Marion4537c302020-09-28 19:03:37 +0200622{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200623 struct dlmallinfo dlminfo = mspace_mallinfo (h->mspace);
Damjan Marion4537c302020-09-28 19:03:37 +0200624 return dlminfo.fordblks;
Dave Barach6a5adc32018-07-04 10:56:23 -0400625}
626
Damjan Marion24738582022-03-31 15:12:20 +0200627__clib_export __clib_flatten void *
628clib_mem_get_heap_base (clib_mem_heap_t *h)
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200629{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200630 return h->base;
Damjan Marion4537c302020-09-28 19:03:37 +0200631}
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200632
Damjan Marion24738582022-03-31 15:12:20 +0200633__clib_export __clib_flatten uword
634clib_mem_get_heap_size (clib_mem_heap_t *heap)
Damjan Marion4537c302020-09-28 19:03:37 +0200635{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200636 return heap->size;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200637}
638
Damjan Marion299571a2022-03-19 00:07:52 +0100639/* Memory allocator which may call os_out_of_memory() if it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200640static inline void *
641clib_mem_heap_alloc_inline (void *heap, uword size, uword align,
642 int os_out_of_memory_on_failure)
Damjan Marion299571a2022-03-19 00:07:52 +0100643{
Damjan Marion24738582022-03-31 15:12:20 +0200644 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100645 void *p;
646
647 align = clib_max (CLIB_MEM_MIN_ALIGN, align);
648
649 p = mspace_memalign (h->mspace, align, size);
650
651 if (PREDICT_FALSE (0 == p))
652 {
653 if (os_out_of_memory_on_failure)
654 os_out_of_memory ();
655 return 0;
656 }
657
658 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
Benoît Ganne22460d62022-11-16 19:36:15 +0100659 mheap_get_trace_internal (h, pointer_to_uword (p), clib_mem_size (p));
Damjan Marion299571a2022-03-19 00:07:52 +0100660
Damjan Marion79934e82022-04-05 12:40:31 +0200661 clib_mem_unpoison (p, size);
Damjan Marion299571a2022-03-19 00:07:52 +0100662 return p;
663}
664
665/* Memory allocator which calls os_out_of_memory() when it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200666__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100667clib_mem_alloc (uword size)
668{
Damjan Marion24738582022-03-31 15:12:20 +0200669 return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN,
670 /* os_out_of_memory */ 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100671}
672
Damjan Marion24738582022-03-31 15:12:20 +0200673__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100674clib_mem_alloc_aligned (uword size, uword align)
675{
Damjan Marion24738582022-03-31 15:12:20 +0200676 return clib_mem_heap_alloc_inline (0, size, align,
677 /* os_out_of_memory */ 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100678}
679
680/* Memory allocator which calls os_out_of_memory() when it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200681__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100682clib_mem_alloc_or_null (uword size)
683{
Damjan Marion24738582022-03-31 15:12:20 +0200684 return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_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 *
Damjan Marion299571a2022-03-19 00:07:52 +0100689clib_mem_alloc_aligned_or_null (uword size, uword align)
690{
Damjan Marion24738582022-03-31 15:12:20 +0200691 return clib_mem_heap_alloc_inline (0, size, align,
692 /* os_out_of_memory */ 0);
Damjan Marion299571a2022-03-19 00:07:52 +0100693}
694
Damjan Marion24738582022-03-31 15:12:20 +0200695__clib_export __clib_flatten void *
696clib_mem_heap_alloc (void *heap, uword size)
697{
698 return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN,
699 /* os_out_of_memory */ 1);
700}
701
702__clib_export __clib_flatten void *
703clib_mem_heap_alloc_aligned (void *heap, uword size, uword align)
704{
705 return clib_mem_heap_alloc_inline (heap, size, align,
706 /* os_out_of_memory */ 1);
707}
708
709__clib_export __clib_flatten void *
710clib_mem_heap_alloc_or_null (void *heap, uword size)
711{
712 return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN,
713 /* os_out_of_memory */ 0);
714}
715
716__clib_export __clib_flatten void *
717clib_mem_heap_alloc_aligned_or_null (void *heap, uword size, uword align)
718{
719 return clib_mem_heap_alloc_inline (heap, size, align,
720 /* os_out_of_memory */ 0);
721}
722
723__clib_export __clib_flatten void *
724clib_mem_heap_realloc_aligned (void *heap, void *p, uword new_size,
725 uword align)
Damjan Marion299571a2022-03-19 00:07:52 +0100726{
727 uword old_alloc_size;
Damjan Marion24738582022-03-31 15:12:20 +0200728 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100729 void *new;
730
731 ASSERT (count_set_bits (align) == 1);
732
733 old_alloc_size = p ? mspace_usable_size (p) : 0;
734
735 if (new_size == old_alloc_size)
736 return p;
737
738 if (p && pointer_is_aligned (p, align) &&
739 mspace_realloc_in_place (h->mspace, p, new_size))
740 {
Damjan Marion79934e82022-04-05 12:40:31 +0200741 clib_mem_unpoison (p, new_size);
Leung Lai Yung69be0892022-05-22 13:25:53 +0000742 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
743 {
Benoît Ganne22460d62022-11-16 19:36:15 +0100744 mheap_put_trace_internal (h, pointer_to_uword (p), old_alloc_size);
745 mheap_get_trace_internal (h, pointer_to_uword (p),
746 clib_mem_size (p));
Leung Lai Yung69be0892022-05-22 13:25:53 +0000747 }
Damjan Marion299571a2022-03-19 00:07:52 +0100748 }
749 else
750 {
Damjan Marion24738582022-03-31 15:12:20 +0200751 new = clib_mem_heap_alloc_inline (h, new_size, align, 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100752
Damjan Marion79934e82022-04-05 12:40:31 +0200753 clib_mem_unpoison (new, new_size);
Damjan Marion299571a2022-03-19 00:07:52 +0100754 if (old_alloc_size)
755 {
Damjan Marion79934e82022-04-05 12:40:31 +0200756 clib_mem_unpoison (p, old_alloc_size);
Damjan Marion299571a2022-03-19 00:07:52 +0100757 clib_memcpy_fast (new, p, clib_min (new_size, old_alloc_size));
Damjan Marion24738582022-03-31 15:12:20 +0200758 clib_mem_heap_free (h, p);
Damjan Marion299571a2022-03-19 00:07:52 +0100759 }
760 p = new;
761 }
762
763 return p;
764}
765
Damjan Marion24738582022-03-31 15:12:20 +0200766__clib_export __clib_flatten void *
767clib_mem_heap_realloc (void *heap, void *p, uword new_size)
Damjan Marion299571a2022-03-19 00:07:52 +0100768{
Damjan Marion24738582022-03-31 15:12:20 +0200769 return clib_mem_heap_realloc_aligned (heap, p, new_size, CLIB_MEM_MIN_ALIGN);
Damjan Marion299571a2022-03-19 00:07:52 +0100770}
771
Damjan Marion24738582022-03-31 15:12:20 +0200772__clib_export __clib_flatten void *
773clib_mem_realloc_aligned (void *p, uword new_size, uword align)
Damjan Marion299571a2022-03-19 00:07:52 +0100774{
Damjan Marion24738582022-03-31 15:12:20 +0200775 return clib_mem_heap_realloc_aligned (0, p, new_size, align);
776}
777
778__clib_export __clib_flatten void *
779clib_mem_realloc (void *p, uword new_size)
780{
781 return clib_mem_heap_realloc_aligned (0, p, new_size, CLIB_MEM_MIN_ALIGN);
782}
783
784__clib_export __clib_flatten uword
785clib_mem_heap_is_heap_object (void *heap, void *p)
786{
787 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100788 return mspace_is_heap_object (h->mspace, p);
789}
790
Damjan Marion24738582022-03-31 15:12:20 +0200791__clib_export __clib_flatten uword
792clib_mem_is_heap_object (void *p)
Damjan Marion299571a2022-03-19 00:07:52 +0100793{
Damjan Marion24738582022-03-31 15:12:20 +0200794 return clib_mem_heap_is_heap_object (0, p);
795}
796
797__clib_export __clib_flatten void
798clib_mem_heap_free (void *heap, void *p)
799{
800 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100801 uword size = clib_mem_size (p);
802
803 /* Make sure object is in the correct heap. */
Damjan Marion24738582022-03-31 15:12:20 +0200804 ASSERT (clib_mem_heap_is_heap_object (h, p));
Damjan Marion299571a2022-03-19 00:07:52 +0100805
806 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
Benoît Ganne22460d62022-11-16 19:36:15 +0100807 mheap_put_trace_internal (h, pointer_to_uword (p), size);
Damjan Marion79934e82022-04-05 12:40:31 +0200808 clib_mem_poison (p, clib_mem_size (p));
Damjan Marion299571a2022-03-19 00:07:52 +0100809
810 mspace_free (h->mspace, p);
811}
812
Damjan Marion24738582022-03-31 15:12:20 +0200813__clib_export __clib_flatten void
814clib_mem_free (void *p)
815{
816 clib_mem_heap_free (0, p);
817}
818
819__clib_export __clib_flatten uword
Damjan Marion299571a2022-03-19 00:07:52 +0100820clib_mem_size (void *p)
821{
822 return mspace_usable_size (p);
823}
824
825__clib_export void
826clib_mem_free_s (void *p)
827{
828 uword size = clib_mem_size (p);
Damjan Marion79934e82022-04-05 12:40:31 +0200829 clib_mem_unpoison (p, size);
Damjan Marion299571a2022-03-19 00:07:52 +0100830 memset_s_inline (p, size, 0, size);
831 clib_mem_free (p);
832}