blob: a188164a7ba08be447cc557524d66fcadf8f0834 [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>
22
Dave Barach6a5adc32018-07-04 10:56:23 -040023typedef struct
24{
25 /* Address of callers: outer first, inner last. */
26 uword callers[12];
27
28 /* Count of allocations with this traceback. */
Dave Barach6a5adc32018-07-04 10:56:23 -040029 u32 n_allocations;
Dave Barach6a5adc32018-07-04 10:56:23 -040030
31 /* Count of bytes allocated with this traceback. */
32 u32 n_bytes;
33
34 /* Offset of this item */
35 uword offset;
36} mheap_trace_t;
37
38typedef struct
39{
40 clib_spinlock_t lock;
Dave Barach6a5adc32018-07-04 10:56:23 -040041
42 mheap_trace_t *traces;
43
44 /* Indices of free traces. */
45 u32 *trace_free_list;
46
47 /* Hash table mapping callers to trace index. */
48 uword *trace_by_callers;
49
50 /* Hash table mapping mheap offset to trace index. */
51 uword *trace_index_by_offset;
Dave Barachd67a4282019-06-15 12:46:13 -040052
53 /* So we can easily shut off current segment trace, if any */
Benoît Ganne22460d62022-11-16 19:36:15 +010054 const clib_mem_heap_t *current_traced_mheap;
Dave Barachd67a4282019-06-15 12:46:13 -040055
Dave Barach6a5adc32018-07-04 10:56:23 -040056} mheap_trace_main_t;
57
58mheap_trace_main_t mheap_trace_main;
59
Benoît Ganne22460d62022-11-16 19:36:15 +010060static __thread int mheap_trace_thread_disable;
61
62static void
63mheap_get_trace_internal (const clib_mem_heap_t *heap, uword offset,
64 uword size)
Dave Barach6a5adc32018-07-04 10:56:23 -040065{
66 mheap_trace_main_t *tm = &mheap_trace_main;
67 mheap_trace_t *t;
68 uword i, n_callers, trace_index, *p;
69 mheap_trace_t trace;
Dave Barach6a5adc32018-07-04 10:56:23 -040070
Benoît Ganne22460d62022-11-16 19:36:15 +010071 if (heap != tm->current_traced_mheap || mheap_trace_thread_disable)
Dave Barach6a5adc32018-07-04 10:56:23 -040072 return;
73
74 /* Spurious Coverity warnings be gone. */
Dave Barachb7b92992018-10-17 10:38:51 -040075 clib_memset (&trace, 0, sizeof (trace));
Dave Barach6a5adc32018-07-04 10:56:23 -040076
Dave Barach6a5adc32018-07-04 10:56:23 -040077 clib_spinlock_lock (&tm->lock);
78
Benoît Ganne22460d62022-11-16 19:36:15 +010079 /* heap could have changed while we were waiting on the lock */
80 if (heap != tm->current_traced_mheap)
81 goto out;
82
83 /* Turn off tracing for this thread to avoid embarrassment... */
84 mheap_trace_thread_disable = 1;
Dave Barach6a5adc32018-07-04 10:56:23 -040085
Benoît Ganneec4749a2020-09-18 10:05:37 +020086 /* Skip our frame and mspace_get_aligned's frame */
87 n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2);
88 if (n_callers == 0)
89 goto out;
90
Dave Barach6a5adc32018-07-04 10:56:23 -040091 if (!tm->trace_by_callers)
92 tm->trace_by_callers =
93 hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
94
95 p = hash_get_mem (tm->trace_by_callers, &trace.callers);
96 if (p)
97 {
98 trace_index = p[0];
99 t = tm->traces + trace_index;
100 }
101 else
102 {
103 i = vec_len (tm->trace_free_list);
104 if (i > 0)
105 {
106 trace_index = tm->trace_free_list[i - 1];
Damjan Marion8bea5892022-04-04 22:40:45 +0200107 vec_set_len (tm->trace_free_list, i - 1);
Dave Barach6a5adc32018-07-04 10:56:23 -0400108 }
109 else
110 {
111 mheap_trace_t *old_start = tm->traces;
112 mheap_trace_t *old_end = vec_end (tm->traces);
113
114 vec_add2 (tm->traces, t, 1);
115
116 if (tm->traces != old_start)
117 {
118 hash_pair_t *p;
119 mheap_trace_t *q;
Dave Barach6a5adc32018-07-04 10:56:23 -0400120 hash_foreach_pair (p, tm->trace_by_callers,
121 ({
122 q = uword_to_pointer (p->key, mheap_trace_t *);
123 ASSERT (q >= old_start && q < old_end);
124 p->key = pointer_to_uword (tm->traces + (q - old_start));
125 }));
Dave Barach6a5adc32018-07-04 10:56:23 -0400126 }
127 trace_index = t - tm->traces;
128 }
129
130 t = tm->traces + trace_index;
131 t[0] = trace;
132 t->n_allocations = 0;
133 t->n_bytes = 0;
134 hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
135 }
136
137 t->n_allocations += 1;
138 t->n_bytes += size;
139 t->offset = offset; /* keep a sample to autopsy */
140 hash_set (tm->trace_index_by_offset, offset, t - tm->traces);
Benoît Ganneec4749a2020-09-18 10:05:37 +0200141
142out:
Benoît Ganne22460d62022-11-16 19:36:15 +0100143 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400144 clib_spinlock_unlock (&tm->lock);
145}
146
Benoît Ganne22460d62022-11-16 19:36:15 +0100147static void
148mheap_put_trace_internal (const clib_mem_heap_t *heap, uword offset,
149 uword size)
Dave Barach6a5adc32018-07-04 10:56:23 -0400150{
151 mheap_trace_t *t;
152 uword trace_index, *p;
153 mheap_trace_main_t *tm = &mheap_trace_main;
Dave Barach6a5adc32018-07-04 10:56:23 -0400154
Benoît Ganne22460d62022-11-16 19:36:15 +0100155 if (heap != tm->current_traced_mheap || mheap_trace_thread_disable)
Dave Barach6a5adc32018-07-04 10:56:23 -0400156 return;
157
158 clib_spinlock_lock (&tm->lock);
159
Benoît Ganne22460d62022-11-16 19:36:15 +0100160 /* heap could have changed while we were waiting on the lock */
161 if (heap != tm->current_traced_mheap)
162 goto out;
163
164 /* Turn off tracing for this thread for a moment */
165 mheap_trace_thread_disable = 1;
Dave Barach6a5adc32018-07-04 10:56:23 -0400166
167 p = hash_get (tm->trace_index_by_offset, offset);
168 if (!p)
Benoît Ganne22460d62022-11-16 19:36:15 +0100169 goto out;
Dave Barach6a5adc32018-07-04 10:56:23 -0400170
171 trace_index = p[0];
172 hash_unset (tm->trace_index_by_offset, offset);
173 ASSERT (trace_index < vec_len (tm->traces));
174
175 t = tm->traces + trace_index;
176 ASSERT (t->n_allocations > 0);
177 ASSERT (t->n_bytes >= size);
178 t->n_allocations -= 1;
179 t->n_bytes -= size;
180 if (t->n_allocations == 0)
181 {
182 hash_unset_mem (tm->trace_by_callers, t->callers);
183 vec_add1 (tm->trace_free_list, trace_index);
Dave Barachb7b92992018-10-17 10:38:51 -0400184 clib_memset (t, 0, sizeof (t[0]));
Dave Barach6a5adc32018-07-04 10:56:23 -0400185 }
Benoît Ganne22460d62022-11-16 19:36:15 +0100186
187out:
188 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400189 clib_spinlock_unlock (&tm->lock);
190}
191
Benoît Ganne22460d62022-11-16 19:36:15 +0100192void
193mheap_get_trace (uword offset, uword size)
194{
195 mheap_get_trace_internal (clib_mem_get_heap (), offset, size);
196}
197
198void
199mheap_put_trace (uword offset, uword size)
200{
201 mheap_put_trace_internal (clib_mem_get_heap (), offset, size);
202}
203
Dave Barach6a5adc32018-07-04 10:56:23 -0400204always_inline void
205mheap_trace_main_free (mheap_trace_main_t * tm)
206{
Benoît Ganne22460d62022-11-16 19:36:15 +0100207 CLIB_SPINLOCK_ASSERT_LOCKED (&tm->lock);
208 tm->current_traced_mheap = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400209 vec_free (tm->traces);
210 vec_free (tm->trace_free_list);
211 hash_free (tm->trace_by_callers);
212 hash_free (tm->trace_index_by_offset);
Benoît Ganne22460d62022-11-16 19:36:15 +0100213 mheap_trace_thread_disable = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400214}
215
Damjan Marionbfa75d62020-10-06 17:46:06 +0200216static clib_mem_heap_t *
217clib_mem_create_heap_internal (void *base, uword size,
218 clib_mem_page_sz_t log2_page_sz, int is_locked,
219 char *name)
220{
221 clib_mem_heap_t *h;
222 u8 flags = 0;
223 int sz = sizeof (clib_mem_heap_t);
224
225 if (base == 0)
226 {
227 log2_page_sz = clib_mem_log2_page_size_validate (log2_page_sz);
228 size = round_pow2 (size, clib_mem_page_bytes (log2_page_sz));
229 base = clib_mem_vm_map_internal (0, log2_page_sz, size, -1, 0,
230 "main heap");
231
232 if (base == CLIB_MEM_VM_MAP_FAILED)
233 return 0;
234
235 flags = CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY;
236 }
237 else
238 log2_page_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
239
240 if (is_locked)
241 flags |= CLIB_MEM_HEAP_F_LOCKED;
242
243 h = base;
244 h->base = base;
245 h->size = size;
246 h->log2_page_sz = log2_page_sz;
247 h->flags = flags;
248 sz = strlen (name);
249 strcpy (h->name, name);
250 sz = round_pow2 (sz + sizeof (clib_mem_heap_t), 16);
251 h->mspace = create_mspace_with_base (base + sz, size - sz, is_locked);
252
253 mspace_disable_expand (h->mspace);
254
Damjan Marion79934e82022-04-05 12:40:31 +0200255 clib_mem_poison (mspace_least_addr (h->mspace),
Damjan Marionbfa75d62020-10-06 17:46:06 +0200256 mspace_footprint (h->mspace));
257
258 return h;
259}
260
Dave Barach6a5adc32018-07-04 10:56:23 -0400261/* Initialize CLIB heap based on memory/size given by user.
262 Set memory to 0 and CLIB will try to allocate its own heap. */
Dave Baracha690fdb2020-01-21 12:34:55 -0500263static void *
Damjan Marionbfa75d62020-10-06 17:46:06 +0200264clib_mem_init_internal (void *base, uword size,
265 clib_mem_page_sz_t log2_page_sz)
Dave Barach6a5adc32018-07-04 10:56:23 -0400266{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200267 clib_mem_heap_t *h;
Dave Barach6a5adc32018-07-04 10:56:23 -0400268
Damjan Marionc63e2a42020-09-16 21:36:00 +0200269 clib_mem_main_init ();
270
Damjan Marionbfa75d62020-10-06 17:46:06 +0200271 h = clib_mem_create_heap_internal (base, size, log2_page_sz,
272 1 /*is_locked */ , "main heap");
Damjan Marion6bfd0762020-09-11 22:16:53 +0200273
Damjan Marionbfa75d62020-10-06 17:46:06 +0200274 clib_mem_set_heap (h);
Dave Barach6a5adc32018-07-04 10:56:23 -0400275
276 if (mheap_trace_main.lock == 0)
Benoît Ganne22460d62022-11-16 19:36:15 +0100277 {
278 /* clib_spinlock_init() dynamically allocates the spinlock in the current
279 * per-cpu heap, but it is used for all traces accross all heaps and
280 * hence we can't really allocate it in the current per-cpu heap as it
281 * could be destroyed later */
282 static struct clib_spinlock_s mheap_trace_main_lock = {};
283 mheap_trace_main.lock = &mheap_trace_main_lock;
284 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400285
Damjan Marionbfa75d62020-10-06 17:46:06 +0200286 return h;
Dave Barach6a5adc32018-07-04 10:56:23 -0400287}
288
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200289__clib_export void *
Dave Baracha690fdb2020-01-21 12:34:55 -0500290clib_mem_init (void *memory, uword memory_size)
291{
292 return clib_mem_init_internal (memory, memory_size,
Damjan Marionbfa75d62020-10-06 17:46:06 +0200293 CLIB_MEM_PAGE_SZ_DEFAULT);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200294}
295
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200296__clib_export void *
Damjan Marion6bfd0762020-09-11 22:16:53 +0200297clib_mem_init_with_page_size (uword memory_size,
298 clib_mem_page_sz_t log2_page_sz)
299{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200300 return clib_mem_init_internal (0, memory_size, log2_page_sz);
Dave Baracha690fdb2020-01-21 12:34:55 -0500301}
302
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200303__clib_export void *
Dave Barach6a5adc32018-07-04 10:56:23 -0400304clib_mem_init_thread_safe (void *memory, uword memory_size)
305{
Dave Baracha690fdb2020-01-21 12:34:55 -0500306 return clib_mem_init_internal (memory, memory_size,
Damjan Marionbfa75d62020-10-06 17:46:06 +0200307 CLIB_MEM_PAGE_SZ_DEFAULT);
Dave Barach2b793412020-08-28 10:39:00 -0400308}
309
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200310__clib_export void
Dave Barach2b793412020-08-28 10:39:00 -0400311clib_mem_destroy (void)
312{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200313 mheap_trace_main_t *tm = &mheap_trace_main;
314 clib_mem_heap_t *heap = clib_mem_get_heap ();
Damjan Marionbfa75d62020-10-06 17:46:06 +0200315
Benoît Ganne22460d62022-11-16 19:36:15 +0100316 if (heap->mspace == tm->current_traced_mheap)
317 mheap_trace (heap, 0);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200318
319 destroy_mspace (heap->mspace);
Damjan Marion16ca1a92022-04-28 17:46:13 +0200320 clib_mem_vm_unmap (heap);
Dave Barach2b793412020-08-28 10:39:00 -0400321}
322
Damjan Marion4a251d02021-05-06 17:28:12 +0200323__clib_export u8 *
324format_clib_mem_usage (u8 *s, va_list *va)
Dave Barach6a5adc32018-07-04 10:56:23 -0400325{
326 int verbose = va_arg (*va, int);
327 return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
328 verbose);
329}
330
331/*
332 * Magic decoder ring for mallinfo stats (ala dlmalloc):
333 *
334 * size_t arena; / * Non-mmapped space allocated (bytes) * /
335 * size_t ordblks; / * Number of free chunks * /
336 * size_t smblks; / * Number of free fastbin blocks * /
337 * size_t hblks; / * Number of mmapped regions * /
338 * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * /
339 * size_t usmblks; / * Maximum total allocated space (bytes) * /
340 * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * /
341 * size_t uordblks; / * Total allocated space (bytes) * /
342 * size_t fordblks; / * Total free space (bytes) * /
343 * size_t keepcost; / * Top-most, releasable space (bytes) * /
344 *
345 */
346
347u8 *
348format_msize (u8 * s, va_list * va)
349{
350 uword a = va_arg (*va, uword);
351
352 if (a >= 1ULL << 30)
353 s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30))));
354 else if (a >= 1ULL << 20)
355 s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20))));
356 else if (a >= 1ULL << 10)
357 s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10))));
358 else
359 s = format (s, "%lld", a);
360 return s;
361}
362
363static int
364mheap_trace_sort (const void *_t1, const void *_t2)
365{
366 const mheap_trace_t *t1 = _t1;
367 const mheap_trace_t *t2 = _t2;
368 word cmp;
369
370 cmp = (word) t2->n_bytes - (word) t1->n_bytes;
371 if (!cmp)
372 cmp = (word) t2->n_allocations - (word) t1->n_allocations;
373 return cmp;
374}
375
376u8 *
377format_mheap_trace (u8 * s, va_list * va)
378{
379 mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
380 int verbose = va_arg (*va, int);
381 int have_traces = 0;
382 int i;
Benoît Ganneaf62f932023-02-08 18:54:30 +0100383 int n = 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400384
385 clib_spinlock_lock (&tm->lock);
Dave Barachd67a4282019-06-15 12:46:13 -0400386 if (vec_len (tm->traces) > 0 &&
387 clib_mem_get_heap () == tm->current_traced_mheap)
Dave Barach6a5adc32018-07-04 10:56:23 -0400388 {
389 have_traces = 1;
390
391 /* Make a copy of traces since we'll be sorting them. */
392 mheap_trace_t *t, *traces_copy;
393 u32 indent, total_objects_traced;
394
395 traces_copy = vec_dup (tm->traces);
396
397 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
398 mheap_trace_sort);
399
400 total_objects_traced = 0;
401 s = format (s, "\n");
402 vec_foreach (t, traces_copy)
403 {
404 /* Skip over free elements. */
405 if (t->n_allocations == 0)
406 continue;
407
408 total_objects_traced += t->n_allocations;
409
Benoît Ganneaf62f932023-02-08 18:54:30 +0100410 /* When not verbose only report the 50 biggest allocations */
411 if (!verbose && n >= 50)
Dave Barach6a5adc32018-07-04 10:56:23 -0400412 continue;
Benoît Ganneaf62f932023-02-08 18:54:30 +0100413 n++;
Dave Barach6a5adc32018-07-04 10:56:23 -0400414
415 if (t == traces_copy)
416 s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
417 "Sample");
418 s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset);
419 indent = format_get_indent (s);
420 for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++)
421 {
422 if (i > 0)
423 s = format (s, "%U", format_white_space, indent);
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200424#if defined(CLIB_UNIX) && !defined(__APPLE__)
Dave Barach6a5adc32018-07-04 10:56:23 -0400425 /* $$$$ does this actually work? */
426 s =
427 format (s, " %U\n", format_clib_elf_symbol_with_address,
428 t->callers[i]);
429#else
430 s = format (s, " %p\n", t->callers[i]);
431#endif
432 }
433 }
434
435 s = format (s, "%d total traced objects\n", total_objects_traced);
436
437 vec_free (traces_copy);
438 }
439 clib_spinlock_unlock (&tm->lock);
440 if (have_traces == 0)
441 s = format (s, "no traced allocations\n");
442
443 return s;
444}
445
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200446__clib_export u8 *
Damjan Marion4537c302020-09-28 19:03:37 +0200447format_clib_mem_heap (u8 * s, va_list * va)
Dave Barach6a5adc32018-07-04 10:56:23 -0400448{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200449 clib_mem_heap_t *heap = va_arg (*va, clib_mem_heap_t *);
Dave Barach6a5adc32018-07-04 10:56:23 -0400450 int verbose = va_arg (*va, int);
Dave Barachaf7dd5b2018-08-23 17:08:44 -0400451 struct dlmallinfo mi;
Dave Barach6a5adc32018-07-04 10:56:23 -0400452 mheap_trace_main_t *tm = &mheap_trace_main;
Damjan Marionbfa75d62020-10-06 17:46:06 +0200453 u32 indent = format_get_indent (s) + 2;
Dave Barach6a5adc32018-07-04 10:56:23 -0400454
Damjan Marion4537c302020-09-28 19:03:37 +0200455 if (heap == 0)
456 heap = clib_mem_get_heap ();
457
Damjan Marionbfa75d62020-10-06 17:46:06 +0200458 mi = mspace_mallinfo (heap->mspace);
Dave Barach6a5adc32018-07-04 10:56:23 -0400459
Damjan Marionbfa75d62020-10-06 17:46:06 +0200460 s = format (s, "base %p, size %U",
461 heap->base, format_memory_size, heap->size);
462
463#define _(i,v,str) \
464 if (heap->flags & CLIB_MEM_HEAP_F_##v) s = format (s, ", %s", str);
465 foreach_clib_mem_heap_flag;
466#undef _
467
468 s = format (s, ", name '%s'", heap->name);
469
470 if (heap->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
471 {
472 clib_mem_page_stats_t stats;
473 clib_mem_get_page_stats (heap->base, heap->log2_page_sz,
474 heap->size >> heap->log2_page_sz, &stats);
475 s = format (s, "\n%U%U", format_white_space, indent,
476 format_clib_mem_page_stats, &stats);
477 }
478
479 s = format (s, "\n%Utotal: %U, used: %U, free: %U, trimmable: %U",
480 format_white_space, indent,
Dave Barach6a5adc32018-07-04 10:56:23 -0400481 format_msize, mi.arena,
482 format_msize, mi.uordblks,
483 format_msize, mi.fordblks, format_msize, mi.keepcost);
484 if (verbose > 0)
485 {
Damjan Marionbfa75d62020-10-06 17:46:06 +0200486 s = format (s, "\n%Ufree chunks %llu free fastbin blks %llu",
487 format_white_space, indent + 2, mi.ordblks, mi.smblks);
488 s = format (s, "\n%Umax total allocated %U",
489 format_white_space, indent + 2, format_msize, mi.usmblks);
Dave Barach6a5adc32018-07-04 10:56:23 -0400490 }
491
Damjan Marion299571a2022-03-19 00:07:52 +0100492 if (heap->flags & CLIB_MEM_HEAP_F_TRACED)
Dave Barachd67a4282019-06-15 12:46:13 -0400493 s = format (s, "\n%U", format_mheap_trace, tm, verbose);
Dave Barach6a5adc32018-07-04 10:56:23 -0400494 return s;
495}
496
Damjan Marion24738582022-03-31 15:12:20 +0200497__clib_export __clib_flatten void
498clib_mem_get_heap_usage (clib_mem_heap_t *heap, clib_mem_usage_t *usage)
Dave Barach6a5adc32018-07-04 10:56:23 -0400499{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200500 struct dlmallinfo mi = mspace_mallinfo (heap->mspace);
Ole Troan92e30822019-06-16 12:33:51 +0200501
Ole Troana606d922021-05-05 09:23:17 +0200502 usage->bytes_total = mi.arena; /* non-mmapped space allocated from system */
503 usage->bytes_used = mi.uordblks; /* total allocated space */
504 usage->bytes_free = mi.fordblks; /* total free space */
505 usage->bytes_used_mmap = mi.hblkhd; /* space in mmapped regions */
506 usage->bytes_max = mi.usmblks; /* maximum total allocated space */
507 usage->bytes_free_reclaimed = mi.ordblks; /* number of free chunks */
508 usage->bytes_overhead = mi.keepcost; /* releasable (via malloc_trim) space */
509
510 /* Not supported */
511 usage->bytes_used_sbrk = 0;
Ole Troan92e30822019-06-16 12:33:51 +0200512 usage->object_count = 0;
Ole Troan92e30822019-06-16 12:33:51 +0200513}
514
Dave Barach6a5adc32018-07-04 10:56:23 -0400515/* Call serial number for debugger breakpoints. */
516uword clib_mem_validate_serial = 0;
517
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200518__clib_export void
Damjan Marionbfa75d62020-10-06 17:46:06 +0200519mheap_trace (clib_mem_heap_t * h, int enable)
Dave Barach6a5adc32018-07-04 10:56:23 -0400520{
Benoît Ganne22460d62022-11-16 19:36:15 +0100521 mheap_trace_main_t *tm = &mheap_trace_main;
Dave Barach6a5adc32018-07-04 10:56:23 -0400522
Benoît Ganne22460d62022-11-16 19:36:15 +0100523 clib_spinlock_lock (&tm->lock);
524
525 if (tm->current_traced_mheap != 0 && tm->current_traced_mheap != h)
526 {
527 clib_warning ("tracing already enabled for another heap, ignoring");
528 goto out;
529 }
530
531 if (enable)
532 {
533 h->flags |= CLIB_MEM_HEAP_F_TRACED;
534 tm->current_traced_mheap = h;
535 }
536 else
537 {
538 h->flags &= ~CLIB_MEM_HEAP_F_TRACED;
539 mheap_trace_main_free (&mheap_trace_main);
540 }
541
542out:
543 clib_spinlock_unlock (&tm->lock);
Dave Barach6a5adc32018-07-04 10:56:23 -0400544}
545
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200546__clib_export void
Dave Barach6a5adc32018-07-04 10:56:23 -0400547clib_mem_trace (int enable)
548{
Dave Barachd67a4282019-06-15 12:46:13 -0400549 void *current_heap = clib_mem_get_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400550 mheap_trace (current_heap, enable);
Dave Barachd67a4282019-06-15 12:46:13 -0400551}
552
553int
554clib_mem_is_traced (void)
555{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200556 clib_mem_heap_t *h = clib_mem_get_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100557 return (h->flags &= CLIB_MEM_HEAP_F_TRACED) != 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400558}
559
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200560__clib_export uword
Dave Barach6a5adc32018-07-04 10:56:23 -0400561clib_mem_trace_enable_disable (uword enable)
562{
Benoît Ganne22460d62022-11-16 19:36:15 +0100563 uword rv = !mheap_trace_thread_disable;
564 mheap_trace_thread_disable = !enable;
Dave Barach6a5adc32018-07-04 10:56:23 -0400565 return rv;
566}
567
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200568__clib_export clib_mem_heap_t *
Damjan Marion4537c302020-09-28 19:03:37 +0200569clib_mem_create_heap (void *base, uword size, int is_locked, char *fmt, ...)
Dave Barach6a5adc32018-07-04 10:56:23 -0400570{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200571 clib_mem_page_sz_t log2_page_sz = clib_mem_get_log2_page_size ();
572 clib_mem_heap_t *h;
573 char *name;
574 u8 *s = 0;
Damjan Marion4537c302020-09-28 19:03:37 +0200575
Damjan Marionbfa75d62020-10-06 17:46:06 +0200576 if (fmt == 0)
577 {
578 name = "";
579 }
580 else if (strchr (fmt, '%'))
581 {
582 va_list va;
583 va_start (va, fmt);
584 s = va_format (0, fmt, &va);
585 vec_add1 (s, 0);
586 va_end (va);
587 name = (char *) s;
588 }
589 else
590 name = fmt;
591
592 h = clib_mem_create_heap_internal (base, size, log2_page_sz, is_locked,
593 name);
594 vec_free (s);
595 return h;
Damjan Marion4537c302020-09-28 19:03:37 +0200596}
597
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200598__clib_export void
Damjan Marionbfa75d62020-10-06 17:46:06 +0200599clib_mem_destroy_heap (clib_mem_heap_t * h)
Damjan Marion4537c302020-09-28 19:03:37 +0200600{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200601 mheap_trace_main_t *tm = &mheap_trace_main;
602
Benoît Ganne22460d62022-11-16 19:36:15 +0100603 if (h->mspace == tm->current_traced_mheap)
604 mheap_trace (h, 0);
Damjan Marionbfa75d62020-10-06 17:46:06 +0200605
606 destroy_mspace (h->mspace);
607 if (h->flags & CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY)
608 clib_mem_vm_unmap (h->base);
Damjan Marion4537c302020-09-28 19:03:37 +0200609}
610
Damjan Marion24738582022-03-31 15:12:20 +0200611__clib_export __clib_flatten uword
612clib_mem_get_heap_free_space (clib_mem_heap_t *h)
Damjan Marion4537c302020-09-28 19:03:37 +0200613{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200614 struct dlmallinfo dlminfo = mspace_mallinfo (h->mspace);
Damjan Marion4537c302020-09-28 19:03:37 +0200615 return dlminfo.fordblks;
Dave Barach6a5adc32018-07-04 10:56:23 -0400616}
617
Damjan Marion24738582022-03-31 15:12:20 +0200618__clib_export __clib_flatten void *
619clib_mem_get_heap_base (clib_mem_heap_t *h)
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200620{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200621 return h->base;
Damjan Marion4537c302020-09-28 19:03:37 +0200622}
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200623
Damjan Marion24738582022-03-31 15:12:20 +0200624__clib_export __clib_flatten uword
625clib_mem_get_heap_size (clib_mem_heap_t *heap)
Damjan Marion4537c302020-09-28 19:03:37 +0200626{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200627 return heap->size;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200628}
629
Damjan Marion299571a2022-03-19 00:07:52 +0100630/* Memory allocator which may call os_out_of_memory() if it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200631static inline void *
632clib_mem_heap_alloc_inline (void *heap, uword size, uword align,
633 int os_out_of_memory_on_failure)
Damjan Marion299571a2022-03-19 00:07:52 +0100634{
Damjan Marion24738582022-03-31 15:12:20 +0200635 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100636 void *p;
637
638 align = clib_max (CLIB_MEM_MIN_ALIGN, align);
639
640 p = mspace_memalign (h->mspace, align, size);
641
642 if (PREDICT_FALSE (0 == p))
643 {
644 if (os_out_of_memory_on_failure)
645 os_out_of_memory ();
646 return 0;
647 }
648
649 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
Benoît Ganne22460d62022-11-16 19:36:15 +0100650 mheap_get_trace_internal (h, pointer_to_uword (p), clib_mem_size (p));
Damjan Marion299571a2022-03-19 00:07:52 +0100651
Damjan Marion79934e82022-04-05 12:40:31 +0200652 clib_mem_unpoison (p, size);
Damjan Marion299571a2022-03-19 00:07:52 +0100653 return p;
654}
655
656/* Memory allocator which calls os_out_of_memory() when it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200657__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100658clib_mem_alloc (uword size)
659{
Damjan Marion24738582022-03-31 15:12:20 +0200660 return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN,
661 /* os_out_of_memory */ 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100662}
663
Damjan Marion24738582022-03-31 15:12:20 +0200664__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100665clib_mem_alloc_aligned (uword size, uword align)
666{
Damjan Marion24738582022-03-31 15:12:20 +0200667 return clib_mem_heap_alloc_inline (0, size, align,
668 /* os_out_of_memory */ 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100669}
670
671/* Memory allocator which calls os_out_of_memory() when it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200672__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100673clib_mem_alloc_or_null (uword size)
674{
Damjan Marion24738582022-03-31 15:12:20 +0200675 return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN,
676 /* os_out_of_memory */ 0);
Damjan Marion299571a2022-03-19 00:07:52 +0100677}
678
Damjan Marion24738582022-03-31 15:12:20 +0200679__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100680clib_mem_alloc_aligned_or_null (uword size, uword align)
681{
Damjan Marion24738582022-03-31 15:12:20 +0200682 return clib_mem_heap_alloc_inline (0, size, align,
683 /* os_out_of_memory */ 0);
Damjan Marion299571a2022-03-19 00:07:52 +0100684}
685
Damjan Marion24738582022-03-31 15:12:20 +0200686__clib_export __clib_flatten void *
687clib_mem_heap_alloc (void *heap, uword size)
688{
689 return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN,
690 /* os_out_of_memory */ 1);
691}
692
693__clib_export __clib_flatten void *
694clib_mem_heap_alloc_aligned (void *heap, uword size, uword align)
695{
696 return clib_mem_heap_alloc_inline (heap, size, align,
697 /* os_out_of_memory */ 1);
698}
699
700__clib_export __clib_flatten void *
701clib_mem_heap_alloc_or_null (void *heap, uword size)
702{
703 return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN,
704 /* os_out_of_memory */ 0);
705}
706
707__clib_export __clib_flatten void *
708clib_mem_heap_alloc_aligned_or_null (void *heap, uword size, uword align)
709{
710 return clib_mem_heap_alloc_inline (heap, size, align,
711 /* os_out_of_memory */ 0);
712}
713
714__clib_export __clib_flatten void *
715clib_mem_heap_realloc_aligned (void *heap, void *p, uword new_size,
716 uword align)
Damjan Marion299571a2022-03-19 00:07:52 +0100717{
718 uword old_alloc_size;
Damjan Marion24738582022-03-31 15:12:20 +0200719 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100720 void *new;
721
722 ASSERT (count_set_bits (align) == 1);
723
724 old_alloc_size = p ? mspace_usable_size (p) : 0;
725
726 if (new_size == old_alloc_size)
727 return p;
728
729 if (p && pointer_is_aligned (p, align) &&
730 mspace_realloc_in_place (h->mspace, p, new_size))
731 {
Damjan Marion79934e82022-04-05 12:40:31 +0200732 clib_mem_unpoison (p, new_size);
Leung Lai Yung69be0892022-05-22 13:25:53 +0000733 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
734 {
Benoît Ganne22460d62022-11-16 19:36:15 +0100735 mheap_put_trace_internal (h, pointer_to_uword (p), old_alloc_size);
736 mheap_get_trace_internal (h, pointer_to_uword (p),
737 clib_mem_size (p));
Leung Lai Yung69be0892022-05-22 13:25:53 +0000738 }
Damjan Marion299571a2022-03-19 00:07:52 +0100739 }
740 else
741 {
Damjan Marion24738582022-03-31 15:12:20 +0200742 new = clib_mem_heap_alloc_inline (h, new_size, align, 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100743
Damjan Marion79934e82022-04-05 12:40:31 +0200744 clib_mem_unpoison (new, new_size);
Damjan Marion299571a2022-03-19 00:07:52 +0100745 if (old_alloc_size)
746 {
Damjan Marion79934e82022-04-05 12:40:31 +0200747 clib_mem_unpoison (p, old_alloc_size);
Damjan Marion299571a2022-03-19 00:07:52 +0100748 clib_memcpy_fast (new, p, clib_min (new_size, old_alloc_size));
Damjan Marion24738582022-03-31 15:12:20 +0200749 clib_mem_heap_free (h, p);
Damjan Marion299571a2022-03-19 00:07:52 +0100750 }
751 p = new;
752 }
753
754 return p;
755}
756
Damjan Marion24738582022-03-31 15:12:20 +0200757__clib_export __clib_flatten void *
758clib_mem_heap_realloc (void *heap, void *p, uword new_size)
Damjan Marion299571a2022-03-19 00:07:52 +0100759{
Damjan Marion24738582022-03-31 15:12:20 +0200760 return clib_mem_heap_realloc_aligned (heap, p, new_size, CLIB_MEM_MIN_ALIGN);
Damjan Marion299571a2022-03-19 00:07:52 +0100761}
762
Damjan Marion24738582022-03-31 15:12:20 +0200763__clib_export __clib_flatten void *
764clib_mem_realloc_aligned (void *p, uword new_size, uword align)
Damjan Marion299571a2022-03-19 00:07:52 +0100765{
Damjan Marion24738582022-03-31 15:12:20 +0200766 return clib_mem_heap_realloc_aligned (0, p, new_size, align);
767}
768
769__clib_export __clib_flatten void *
770clib_mem_realloc (void *p, uword new_size)
771{
772 return clib_mem_heap_realloc_aligned (0, p, new_size, CLIB_MEM_MIN_ALIGN);
773}
774
775__clib_export __clib_flatten uword
776clib_mem_heap_is_heap_object (void *heap, void *p)
777{
778 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100779 return mspace_is_heap_object (h->mspace, p);
780}
781
Damjan Marion24738582022-03-31 15:12:20 +0200782__clib_export __clib_flatten uword
783clib_mem_is_heap_object (void *p)
Damjan Marion299571a2022-03-19 00:07:52 +0100784{
Damjan Marion24738582022-03-31 15:12:20 +0200785 return clib_mem_heap_is_heap_object (0, p);
786}
787
788__clib_export __clib_flatten void
789clib_mem_heap_free (void *heap, void *p)
790{
791 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100792 uword size = clib_mem_size (p);
793
794 /* Make sure object is in the correct heap. */
Damjan Marion24738582022-03-31 15:12:20 +0200795 ASSERT (clib_mem_heap_is_heap_object (h, p));
Damjan Marion299571a2022-03-19 00:07:52 +0100796
797 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
Benoît Ganne22460d62022-11-16 19:36:15 +0100798 mheap_put_trace_internal (h, pointer_to_uword (p), size);
Damjan Marion79934e82022-04-05 12:40:31 +0200799 clib_mem_poison (p, clib_mem_size (p));
Damjan Marion299571a2022-03-19 00:07:52 +0100800
801 mspace_free (h->mspace, p);
802}
803
Damjan Marion24738582022-03-31 15:12:20 +0200804__clib_export __clib_flatten void
805clib_mem_free (void *p)
806{
807 clib_mem_heap_free (0, p);
808}
809
810__clib_export __clib_flatten uword
Damjan Marion299571a2022-03-19 00:07:52 +0100811clib_mem_size (void *p)
812{
813 return mspace_usable_size (p);
814}
815
816__clib_export void
817clib_mem_free_s (void *p)
818{
819 uword size = clib_mem_size (p);
Damjan Marion79934e82022-04-05 12:40:31 +0200820 clib_mem_unpoison (p, size);
Damjan Marion299571a2022-03-19 00:07:52 +0100821 memset_s_inline (p, size, 0, size);
822 clib_mem_free (p);
823}