blob: 4150fbe8b4b7df78c23e864f2bdf870338c5ad71 [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;
41 uword enabled;
42
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 */
55 void *current_traced_mheap;
56
Dave Barach6a5adc32018-07-04 10:56:23 -040057} mheap_trace_main_t;
58
59mheap_trace_main_t mheap_trace_main;
60
61void
62mheap_get_trace (uword offset, uword size)
63{
64 mheap_trace_main_t *tm = &mheap_trace_main;
65 mheap_trace_t *t;
66 uword i, n_callers, trace_index, *p;
67 mheap_trace_t trace;
68 uword save_enabled;
69
Dave Barachd67a4282019-06-15 12:46:13 -040070 if (tm->enabled == 0 || (clib_mem_get_heap () != tm->current_traced_mheap))
Dave Barach6a5adc32018-07-04 10:56:23 -040071 return;
72
73 /* Spurious Coverity warnings be gone. */
Dave Barachb7b92992018-10-17 10:38:51 -040074 clib_memset (&trace, 0, sizeof (trace));
Dave Barach6a5adc32018-07-04 10:56:23 -040075
Dave Barach6a5adc32018-07-04 10:56:23 -040076 clib_spinlock_lock (&tm->lock);
77
78 /* Turn off tracing to avoid embarrassment... */
79 save_enabled = tm->enabled;
80 tm->enabled = 0;
81
Benoît Ganneec4749a2020-09-18 10:05:37 +020082 /* Skip our frame and mspace_get_aligned's frame */
83 n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2);
84 if (n_callers == 0)
85 goto out;
86
Dave Barach6a5adc32018-07-04 10:56:23 -040087 if (!tm->trace_by_callers)
88 tm->trace_by_callers =
89 hash_create_shmem (0, sizeof (trace.callers), sizeof (uword));
90
91 p = hash_get_mem (tm->trace_by_callers, &trace.callers);
92 if (p)
93 {
94 trace_index = p[0];
95 t = tm->traces + trace_index;
96 }
97 else
98 {
99 i = vec_len (tm->trace_free_list);
100 if (i > 0)
101 {
102 trace_index = tm->trace_free_list[i - 1];
Damjan Marion8bea5892022-04-04 22:40:45 +0200103 vec_set_len (tm->trace_free_list, i - 1);
Dave Barach6a5adc32018-07-04 10:56:23 -0400104 }
105 else
106 {
107 mheap_trace_t *old_start = tm->traces;
108 mheap_trace_t *old_end = vec_end (tm->traces);
109
110 vec_add2 (tm->traces, t, 1);
111
112 if (tm->traces != old_start)
113 {
114 hash_pair_t *p;
115 mheap_trace_t *q;
116 /* *INDENT-OFF* */
117 hash_foreach_pair (p, tm->trace_by_callers,
118 ({
119 q = uword_to_pointer (p->key, mheap_trace_t *);
120 ASSERT (q >= old_start && q < old_end);
121 p->key = pointer_to_uword (tm->traces + (q - old_start));
122 }));
123 /* *INDENT-ON* */
124 }
125 trace_index = t - tm->traces;
126 }
127
128 t = tm->traces + trace_index;
129 t[0] = trace;
130 t->n_allocations = 0;
131 t->n_bytes = 0;
132 hash_set_mem (tm->trace_by_callers, t->callers, trace_index);
133 }
134
135 t->n_allocations += 1;
136 t->n_bytes += size;
137 t->offset = offset; /* keep a sample to autopsy */
138 hash_set (tm->trace_index_by_offset, offset, t - tm->traces);
Benoît Ganneec4749a2020-09-18 10:05:37 +0200139
140out:
Dave Barach6a5adc32018-07-04 10:56:23 -0400141 tm->enabled = save_enabled;
142 clib_spinlock_unlock (&tm->lock);
143}
144
145void
146mheap_put_trace (uword offset, uword size)
147{
148 mheap_trace_t *t;
149 uword trace_index, *p;
150 mheap_trace_main_t *tm = &mheap_trace_main;
151 uword save_enabled;
152
153 if (tm->enabled == 0)
154 return;
155
156 clib_spinlock_lock (&tm->lock);
157
158 /* Turn off tracing for a moment */
159 save_enabled = tm->enabled;
160 tm->enabled = 0;
161
162 p = hash_get (tm->trace_index_by_offset, offset);
163 if (!p)
164 {
165 tm->enabled = save_enabled;
166 clib_spinlock_unlock (&tm->lock);
167 return;
168 }
169
170 trace_index = p[0];
171 hash_unset (tm->trace_index_by_offset, offset);
172 ASSERT (trace_index < vec_len (tm->traces));
173
174 t = tm->traces + trace_index;
175 ASSERT (t->n_allocations > 0);
176 ASSERT (t->n_bytes >= size);
177 t->n_allocations -= 1;
178 t->n_bytes -= size;
179 if (t->n_allocations == 0)
180 {
181 hash_unset_mem (tm->trace_by_callers, t->callers);
182 vec_add1 (tm->trace_free_list, trace_index);
Dave Barachb7b92992018-10-17 10:38:51 -0400183 clib_memset (t, 0, sizeof (t[0]));
Dave Barach6a5adc32018-07-04 10:56:23 -0400184 }
185 tm->enabled = save_enabled;
186 clib_spinlock_unlock (&tm->lock);
187}
188
189always_inline void
190mheap_trace_main_free (mheap_trace_main_t * tm)
191{
192 vec_free (tm->traces);
193 vec_free (tm->trace_free_list);
194 hash_free (tm->trace_by_callers);
195 hash_free (tm->trace_index_by_offset);
196}
197
Damjan Marionbfa75d62020-10-06 17:46:06 +0200198static clib_mem_heap_t *
199clib_mem_create_heap_internal (void *base, uword size,
200 clib_mem_page_sz_t log2_page_sz, int is_locked,
201 char *name)
202{
203 clib_mem_heap_t *h;
204 u8 flags = 0;
205 int sz = sizeof (clib_mem_heap_t);
206
207 if (base == 0)
208 {
209 log2_page_sz = clib_mem_log2_page_size_validate (log2_page_sz);
210 size = round_pow2 (size, clib_mem_page_bytes (log2_page_sz));
211 base = clib_mem_vm_map_internal (0, log2_page_sz, size, -1, 0,
212 "main heap");
213
214 if (base == CLIB_MEM_VM_MAP_FAILED)
215 return 0;
216
217 flags = CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY;
218 }
219 else
220 log2_page_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
221
222 if (is_locked)
223 flags |= CLIB_MEM_HEAP_F_LOCKED;
224
225 h = base;
226 h->base = base;
227 h->size = size;
228 h->log2_page_sz = log2_page_sz;
229 h->flags = flags;
230 sz = strlen (name);
231 strcpy (h->name, name);
232 sz = round_pow2 (sz + sizeof (clib_mem_heap_t), 16);
233 h->mspace = create_mspace_with_base (base + sz, size - sz, is_locked);
234
235 mspace_disable_expand (h->mspace);
236
Damjan Marion79934e82022-04-05 12:40:31 +0200237 clib_mem_poison (mspace_least_addr (h->mspace),
Damjan Marionbfa75d62020-10-06 17:46:06 +0200238 mspace_footprint (h->mspace));
239
240 return h;
241}
242
Dave Barach6a5adc32018-07-04 10:56:23 -0400243/* Initialize CLIB heap based on memory/size given by user.
244 Set memory to 0 and CLIB will try to allocate its own heap. */
Dave Baracha690fdb2020-01-21 12:34:55 -0500245static void *
Damjan Marionbfa75d62020-10-06 17:46:06 +0200246clib_mem_init_internal (void *base, uword size,
247 clib_mem_page_sz_t log2_page_sz)
Dave Barach6a5adc32018-07-04 10:56:23 -0400248{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200249 clib_mem_heap_t *h;
Dave Barach6a5adc32018-07-04 10:56:23 -0400250
Damjan Marionc63e2a42020-09-16 21:36:00 +0200251 clib_mem_main_init ();
252
Damjan Marionbfa75d62020-10-06 17:46:06 +0200253 h = clib_mem_create_heap_internal (base, size, log2_page_sz,
254 1 /*is_locked */ , "main heap");
Damjan Marion6bfd0762020-09-11 22:16:53 +0200255
Damjan Marionbfa75d62020-10-06 17:46:06 +0200256 clib_mem_set_heap (h);
Dave Barach6a5adc32018-07-04 10:56:23 -0400257
258 if (mheap_trace_main.lock == 0)
259 clib_spinlock_init (&mheap_trace_main.lock);
260
Damjan Marionbfa75d62020-10-06 17:46:06 +0200261 return h;
Dave Barach6a5adc32018-07-04 10:56:23 -0400262}
263
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200264__clib_export void *
Dave Baracha690fdb2020-01-21 12:34:55 -0500265clib_mem_init (void *memory, uword memory_size)
266{
267 return clib_mem_init_internal (memory, memory_size,
Damjan Marionbfa75d62020-10-06 17:46:06 +0200268 CLIB_MEM_PAGE_SZ_DEFAULT);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200269}
270
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200271__clib_export void *
Damjan Marion6bfd0762020-09-11 22:16:53 +0200272clib_mem_init_with_page_size (uword memory_size,
273 clib_mem_page_sz_t log2_page_sz)
274{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200275 return clib_mem_init_internal (0, memory_size, log2_page_sz);
Dave Baracha690fdb2020-01-21 12:34:55 -0500276}
277
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200278__clib_export void *
Dave Barach6a5adc32018-07-04 10:56:23 -0400279clib_mem_init_thread_safe (void *memory, uword memory_size)
280{
Dave Baracha690fdb2020-01-21 12:34:55 -0500281 return clib_mem_init_internal (memory, memory_size,
Damjan Marionbfa75d62020-10-06 17:46:06 +0200282 CLIB_MEM_PAGE_SZ_DEFAULT);
Dave Barach2b793412020-08-28 10:39:00 -0400283}
284
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200285__clib_export void
Dave Barach2b793412020-08-28 10:39:00 -0400286clib_mem_destroy (void)
287{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200288 mheap_trace_main_t *tm = &mheap_trace_main;
289 clib_mem_heap_t *heap = clib_mem_get_heap ();
290 void *base = mspace_least_addr (heap->mspace);
291
292 if (tm->enabled && heap->mspace == tm->current_traced_mheap)
293 tm->enabled = 0;
294
295 destroy_mspace (heap->mspace);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200296 clib_mem_vm_unmap (base);
Dave Barach2b793412020-08-28 10:39:00 -0400297}
298
Damjan Marion4a251d02021-05-06 17:28:12 +0200299__clib_export u8 *
300format_clib_mem_usage (u8 *s, va_list *va)
Dave Barach6a5adc32018-07-04 10:56:23 -0400301{
302 int verbose = va_arg (*va, int);
303 return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (),
304 verbose);
305}
306
307/*
308 * Magic decoder ring for mallinfo stats (ala dlmalloc):
309 *
310 * size_t arena; / * Non-mmapped space allocated (bytes) * /
311 * size_t ordblks; / * Number of free chunks * /
312 * size_t smblks; / * Number of free fastbin blocks * /
313 * size_t hblks; / * Number of mmapped regions * /
314 * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * /
315 * size_t usmblks; / * Maximum total allocated space (bytes) * /
316 * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * /
317 * size_t uordblks; / * Total allocated space (bytes) * /
318 * size_t fordblks; / * Total free space (bytes) * /
319 * size_t keepcost; / * Top-most, releasable space (bytes) * /
320 *
321 */
322
323u8 *
324format_msize (u8 * s, va_list * va)
325{
326 uword a = va_arg (*va, uword);
327
328 if (a >= 1ULL << 30)
329 s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30))));
330 else if (a >= 1ULL << 20)
331 s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20))));
332 else if (a >= 1ULL << 10)
333 s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10))));
334 else
335 s = format (s, "%lld", a);
336 return s;
337}
338
339static int
340mheap_trace_sort (const void *_t1, const void *_t2)
341{
342 const mheap_trace_t *t1 = _t1;
343 const mheap_trace_t *t2 = _t2;
344 word cmp;
345
346 cmp = (word) t2->n_bytes - (word) t1->n_bytes;
347 if (!cmp)
348 cmp = (word) t2->n_allocations - (word) t1->n_allocations;
349 return cmp;
350}
351
352u8 *
353format_mheap_trace (u8 * s, va_list * va)
354{
355 mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *);
356 int verbose = va_arg (*va, int);
357 int have_traces = 0;
358 int i;
359
360 clib_spinlock_lock (&tm->lock);
Dave Barachd67a4282019-06-15 12:46:13 -0400361 if (vec_len (tm->traces) > 0 &&
362 clib_mem_get_heap () == tm->current_traced_mheap)
Dave Barach6a5adc32018-07-04 10:56:23 -0400363 {
364 have_traces = 1;
365
366 /* Make a copy of traces since we'll be sorting them. */
367 mheap_trace_t *t, *traces_copy;
368 u32 indent, total_objects_traced;
369
370 traces_copy = vec_dup (tm->traces);
371
372 qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]),
373 mheap_trace_sort);
374
375 total_objects_traced = 0;
376 s = format (s, "\n");
377 vec_foreach (t, traces_copy)
378 {
379 /* Skip over free elements. */
380 if (t->n_allocations == 0)
381 continue;
382
383 total_objects_traced += t->n_allocations;
384
385 /* When not verbose only report allocations of more than 1k. */
386 if (!verbose && t->n_bytes < 1024)
387 continue;
388
389 if (t == traces_copy)
390 s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count",
391 "Sample");
392 s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset);
393 indent = format_get_indent (s);
394 for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++)
395 {
396 if (i > 0)
397 s = format (s, "%U", format_white_space, indent);
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200398#if defined(CLIB_UNIX) && !defined(__APPLE__)
Dave Barach6a5adc32018-07-04 10:56:23 -0400399 /* $$$$ does this actually work? */
400 s =
401 format (s, " %U\n", format_clib_elf_symbol_with_address,
402 t->callers[i]);
403#else
404 s = format (s, " %p\n", t->callers[i]);
405#endif
406 }
407 }
408
409 s = format (s, "%d total traced objects\n", total_objects_traced);
410
411 vec_free (traces_copy);
412 }
413 clib_spinlock_unlock (&tm->lock);
414 if (have_traces == 0)
415 s = format (s, "no traced allocations\n");
416
417 return s;
418}
419
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200420__clib_export u8 *
Damjan Marion4537c302020-09-28 19:03:37 +0200421format_clib_mem_heap (u8 * s, va_list * va)
Dave Barach6a5adc32018-07-04 10:56:23 -0400422{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200423 clib_mem_heap_t *heap = va_arg (*va, clib_mem_heap_t *);
Dave Barach6a5adc32018-07-04 10:56:23 -0400424 int verbose = va_arg (*va, int);
Dave Barachaf7dd5b2018-08-23 17:08:44 -0400425 struct dlmallinfo mi;
Dave Barach6a5adc32018-07-04 10:56:23 -0400426 mheap_trace_main_t *tm = &mheap_trace_main;
Damjan Marionbfa75d62020-10-06 17:46:06 +0200427 u32 indent = format_get_indent (s) + 2;
Dave Barach6a5adc32018-07-04 10:56:23 -0400428
Damjan Marion4537c302020-09-28 19:03:37 +0200429 if (heap == 0)
430 heap = clib_mem_get_heap ();
431
Damjan Marionbfa75d62020-10-06 17:46:06 +0200432 mi = mspace_mallinfo (heap->mspace);
Dave Barach6a5adc32018-07-04 10:56:23 -0400433
Damjan Marionbfa75d62020-10-06 17:46:06 +0200434 s = format (s, "base %p, size %U",
435 heap->base, format_memory_size, heap->size);
436
437#define _(i,v,str) \
438 if (heap->flags & CLIB_MEM_HEAP_F_##v) s = format (s, ", %s", str);
439 foreach_clib_mem_heap_flag;
440#undef _
441
442 s = format (s, ", name '%s'", heap->name);
443
444 if (heap->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
445 {
446 clib_mem_page_stats_t stats;
447 clib_mem_get_page_stats (heap->base, heap->log2_page_sz,
448 heap->size >> heap->log2_page_sz, &stats);
449 s = format (s, "\n%U%U", format_white_space, indent,
450 format_clib_mem_page_stats, &stats);
451 }
452
453 s = format (s, "\n%Utotal: %U, used: %U, free: %U, trimmable: %U",
454 format_white_space, indent,
Dave Barach6a5adc32018-07-04 10:56:23 -0400455 format_msize, mi.arena,
456 format_msize, mi.uordblks,
457 format_msize, mi.fordblks, format_msize, mi.keepcost);
458 if (verbose > 0)
459 {
Damjan Marionbfa75d62020-10-06 17:46:06 +0200460 s = format (s, "\n%Ufree chunks %llu free fastbin blks %llu",
461 format_white_space, indent + 2, mi.ordblks, mi.smblks);
462 s = format (s, "\n%Umax total allocated %U",
463 format_white_space, indent + 2, format_msize, mi.usmblks);
Dave Barach6a5adc32018-07-04 10:56:23 -0400464 }
465
Damjan Marion299571a2022-03-19 00:07:52 +0100466 if (heap->flags & CLIB_MEM_HEAP_F_TRACED)
Dave Barachd67a4282019-06-15 12:46:13 -0400467 s = format (s, "\n%U", format_mheap_trace, tm, verbose);
Dave Barach6a5adc32018-07-04 10:56:23 -0400468 return s;
469}
470
Damjan Marion24738582022-03-31 15:12:20 +0200471__clib_export __clib_flatten void
472clib_mem_get_heap_usage (clib_mem_heap_t *heap, clib_mem_usage_t *usage)
Dave Barach6a5adc32018-07-04 10:56:23 -0400473{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200474 struct dlmallinfo mi = mspace_mallinfo (heap->mspace);
Ole Troan92e30822019-06-16 12:33:51 +0200475
Ole Troana606d922021-05-05 09:23:17 +0200476 usage->bytes_total = mi.arena; /* non-mmapped space allocated from system */
477 usage->bytes_used = mi.uordblks; /* total allocated space */
478 usage->bytes_free = mi.fordblks; /* total free space */
479 usage->bytes_used_mmap = mi.hblkhd; /* space in mmapped regions */
480 usage->bytes_max = mi.usmblks; /* maximum total allocated space */
481 usage->bytes_free_reclaimed = mi.ordblks; /* number of free chunks */
482 usage->bytes_overhead = mi.keepcost; /* releasable (via malloc_trim) space */
483
484 /* Not supported */
485 usage->bytes_used_sbrk = 0;
Ole Troan92e30822019-06-16 12:33:51 +0200486 usage->object_count = 0;
Ole Troan92e30822019-06-16 12:33:51 +0200487}
488
Dave Barach6a5adc32018-07-04 10:56:23 -0400489/* Call serial number for debugger breakpoints. */
490uword clib_mem_validate_serial = 0;
491
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200492__clib_export void
Damjan Marionbfa75d62020-10-06 17:46:06 +0200493mheap_trace (clib_mem_heap_t * h, int enable)
Dave Barach6a5adc32018-07-04 10:56:23 -0400494{
Damjan Marion299571a2022-03-19 00:07:52 +0100495 if (enable)
496 h->flags |= CLIB_MEM_HEAP_F_TRACED;
497 else
498 h->flags &= ~CLIB_MEM_HEAP_F_TRACED;
Dave Barach6a5adc32018-07-04 10:56:23 -0400499
500 if (enable == 0)
501 mheap_trace_main_free (&mheap_trace_main);
502}
503
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200504__clib_export void
Dave Barach6a5adc32018-07-04 10:56:23 -0400505clib_mem_trace (int enable)
506{
507 mheap_trace_main_t *tm = &mheap_trace_main;
Dave Barachd67a4282019-06-15 12:46:13 -0400508 void *current_heap = clib_mem_get_heap ();
Dave Barach6a5adc32018-07-04 10:56:23 -0400509
510 tm->enabled = enable;
Dave Barachd67a4282019-06-15 12:46:13 -0400511 mheap_trace (current_heap, enable);
512
513 if (enable)
514 tm->current_traced_mheap = current_heap;
515 else
516 tm->current_traced_mheap = 0;
517}
518
519int
520clib_mem_is_traced (void)
521{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200522 clib_mem_heap_t *h = clib_mem_get_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100523 return (h->flags &= CLIB_MEM_HEAP_F_TRACED) != 0;
Dave Barach6a5adc32018-07-04 10:56:23 -0400524}
525
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200526__clib_export uword
Dave Barach6a5adc32018-07-04 10:56:23 -0400527clib_mem_trace_enable_disable (uword enable)
528{
529 uword rv;
530 mheap_trace_main_t *tm = &mheap_trace_main;
531
532 rv = tm->enabled;
533 tm->enabled = enable;
534 return rv;
535}
536
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200537__clib_export clib_mem_heap_t *
Damjan Marion4537c302020-09-28 19:03:37 +0200538clib_mem_create_heap (void *base, uword size, int is_locked, char *fmt, ...)
Dave Barach6a5adc32018-07-04 10:56:23 -0400539{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200540 clib_mem_page_sz_t log2_page_sz = clib_mem_get_log2_page_size ();
541 clib_mem_heap_t *h;
542 char *name;
543 u8 *s = 0;
Damjan Marion4537c302020-09-28 19:03:37 +0200544
Damjan Marionbfa75d62020-10-06 17:46:06 +0200545 if (fmt == 0)
546 {
547 name = "";
548 }
549 else if (strchr (fmt, '%'))
550 {
551 va_list va;
552 va_start (va, fmt);
553 s = va_format (0, fmt, &va);
554 vec_add1 (s, 0);
555 va_end (va);
556 name = (char *) s;
557 }
558 else
559 name = fmt;
560
561 h = clib_mem_create_heap_internal (base, size, log2_page_sz, is_locked,
562 name);
563 vec_free (s);
564 return h;
Damjan Marion4537c302020-09-28 19:03:37 +0200565}
566
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200567__clib_export void
Damjan Marionbfa75d62020-10-06 17:46:06 +0200568clib_mem_destroy_heap (clib_mem_heap_t * h)
Damjan Marion4537c302020-09-28 19:03:37 +0200569{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200570 mheap_trace_main_t *tm = &mheap_trace_main;
571
572 if (tm->enabled && h->mspace == tm->current_traced_mheap)
573 tm->enabled = 0;
574
575 destroy_mspace (h->mspace);
576 if (h->flags & CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY)
577 clib_mem_vm_unmap (h->base);
Damjan Marion4537c302020-09-28 19:03:37 +0200578}
579
Damjan Marion24738582022-03-31 15:12:20 +0200580__clib_export __clib_flatten uword
581clib_mem_get_heap_free_space (clib_mem_heap_t *h)
Damjan Marion4537c302020-09-28 19:03:37 +0200582{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200583 struct dlmallinfo dlminfo = mspace_mallinfo (h->mspace);
Damjan Marion4537c302020-09-28 19:03:37 +0200584 return dlminfo.fordblks;
Dave Barach6a5adc32018-07-04 10:56:23 -0400585}
586
Damjan Marion24738582022-03-31 15:12:20 +0200587__clib_export __clib_flatten void *
588clib_mem_get_heap_base (clib_mem_heap_t *h)
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200589{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200590 return h->base;
Damjan Marion4537c302020-09-28 19:03:37 +0200591}
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200592
Damjan Marion24738582022-03-31 15:12:20 +0200593__clib_export __clib_flatten uword
594clib_mem_get_heap_size (clib_mem_heap_t *heap)
Damjan Marion4537c302020-09-28 19:03:37 +0200595{
Damjan Marionbfa75d62020-10-06 17:46:06 +0200596 return heap->size;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200597}
598
Damjan Marion299571a2022-03-19 00:07:52 +0100599/* Memory allocator which may call os_out_of_memory() if it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200600static inline void *
601clib_mem_heap_alloc_inline (void *heap, uword size, uword align,
602 int os_out_of_memory_on_failure)
Damjan Marion299571a2022-03-19 00:07:52 +0100603{
Damjan Marion24738582022-03-31 15:12:20 +0200604 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100605 void *p;
606
607 align = clib_max (CLIB_MEM_MIN_ALIGN, align);
608
609 p = mspace_memalign (h->mspace, align, size);
610
611 if (PREDICT_FALSE (0 == p))
612 {
613 if (os_out_of_memory_on_failure)
614 os_out_of_memory ();
615 return 0;
616 }
617
618 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
619 mheap_get_trace (pointer_to_uword (p), clib_mem_size (p));
620
Damjan Marion79934e82022-04-05 12:40:31 +0200621 clib_mem_unpoison (p, size);
Damjan Marion299571a2022-03-19 00:07:52 +0100622 return p;
623}
624
625/* Memory allocator which calls os_out_of_memory() when it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200626__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100627clib_mem_alloc (uword size)
628{
Damjan Marion24738582022-03-31 15:12:20 +0200629 return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN,
630 /* os_out_of_memory */ 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100631}
632
Damjan Marion24738582022-03-31 15:12:20 +0200633__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100634clib_mem_alloc_aligned (uword size, uword align)
635{
Damjan Marion24738582022-03-31 15:12:20 +0200636 return clib_mem_heap_alloc_inline (0, size, align,
637 /* os_out_of_memory */ 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100638}
639
640/* Memory allocator which calls os_out_of_memory() when it fails */
Damjan Marion24738582022-03-31 15:12:20 +0200641__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100642clib_mem_alloc_or_null (uword size)
643{
Damjan Marion24738582022-03-31 15:12:20 +0200644 return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN,
645 /* os_out_of_memory */ 0);
Damjan Marion299571a2022-03-19 00:07:52 +0100646}
647
Damjan Marion24738582022-03-31 15:12:20 +0200648__clib_export __clib_flatten void *
Damjan Marion299571a2022-03-19 00:07:52 +0100649clib_mem_alloc_aligned_or_null (uword size, uword align)
650{
Damjan Marion24738582022-03-31 15:12:20 +0200651 return clib_mem_heap_alloc_inline (0, size, align,
652 /* os_out_of_memory */ 0);
Damjan Marion299571a2022-03-19 00:07:52 +0100653}
654
Damjan Marion24738582022-03-31 15:12:20 +0200655__clib_export __clib_flatten void *
656clib_mem_heap_alloc (void *heap, uword size)
657{
658 return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN,
659 /* os_out_of_memory */ 1);
660}
661
662__clib_export __clib_flatten void *
663clib_mem_heap_alloc_aligned (void *heap, uword size, uword align)
664{
665 return clib_mem_heap_alloc_inline (heap, size, align,
666 /* os_out_of_memory */ 1);
667}
668
669__clib_export __clib_flatten void *
670clib_mem_heap_alloc_or_null (void *heap, uword size)
671{
672 return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN,
673 /* os_out_of_memory */ 0);
674}
675
676__clib_export __clib_flatten void *
677clib_mem_heap_alloc_aligned_or_null (void *heap, uword size, uword align)
678{
679 return clib_mem_heap_alloc_inline (heap, size, align,
680 /* os_out_of_memory */ 0);
681}
682
683__clib_export __clib_flatten void *
684clib_mem_heap_realloc_aligned (void *heap, void *p, uword new_size,
685 uword align)
Damjan Marion299571a2022-03-19 00:07:52 +0100686{
687 uword old_alloc_size;
Damjan Marion24738582022-03-31 15:12:20 +0200688 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100689 void *new;
690
691 ASSERT (count_set_bits (align) == 1);
692
693 old_alloc_size = p ? mspace_usable_size (p) : 0;
694
695 if (new_size == old_alloc_size)
696 return p;
697
698 if (p && pointer_is_aligned (p, align) &&
699 mspace_realloc_in_place (h->mspace, p, new_size))
700 {
Damjan Marion79934e82022-04-05 12:40:31 +0200701 clib_mem_unpoison (p, new_size);
Damjan Marion299571a2022-03-19 00:07:52 +0100702 }
703 else
704 {
Damjan Marion24738582022-03-31 15:12:20 +0200705 new = clib_mem_heap_alloc_inline (h, new_size, align, 1);
Damjan Marion299571a2022-03-19 00:07:52 +0100706
Damjan Marion79934e82022-04-05 12:40:31 +0200707 clib_mem_unpoison (new, new_size);
Damjan Marion299571a2022-03-19 00:07:52 +0100708 if (old_alloc_size)
709 {
Damjan Marion79934e82022-04-05 12:40:31 +0200710 clib_mem_unpoison (p, old_alloc_size);
Damjan Marion299571a2022-03-19 00:07:52 +0100711 clib_memcpy_fast (new, p, clib_min (new_size, old_alloc_size));
Damjan Marion24738582022-03-31 15:12:20 +0200712 clib_mem_heap_free (h, p);
Damjan Marion299571a2022-03-19 00:07:52 +0100713 }
714 p = new;
715 }
716
717 return p;
718}
719
Damjan Marion24738582022-03-31 15:12:20 +0200720__clib_export __clib_flatten void *
721clib_mem_heap_realloc (void *heap, void *p, uword new_size)
Damjan Marion299571a2022-03-19 00:07:52 +0100722{
Damjan Marion24738582022-03-31 15:12:20 +0200723 return clib_mem_heap_realloc_aligned (heap, p, new_size, CLIB_MEM_MIN_ALIGN);
Damjan Marion299571a2022-03-19 00:07:52 +0100724}
725
Damjan Marion24738582022-03-31 15:12:20 +0200726__clib_export __clib_flatten void *
727clib_mem_realloc_aligned (void *p, uword new_size, uword align)
Damjan Marion299571a2022-03-19 00:07:52 +0100728{
Damjan Marion24738582022-03-31 15:12:20 +0200729 return clib_mem_heap_realloc_aligned (0, p, new_size, align);
730}
731
732__clib_export __clib_flatten void *
733clib_mem_realloc (void *p, uword new_size)
734{
735 return clib_mem_heap_realloc_aligned (0, p, new_size, CLIB_MEM_MIN_ALIGN);
736}
737
738__clib_export __clib_flatten uword
739clib_mem_heap_is_heap_object (void *heap, void *p)
740{
741 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100742 return mspace_is_heap_object (h->mspace, p);
743}
744
Damjan Marion24738582022-03-31 15:12:20 +0200745__clib_export __clib_flatten uword
746clib_mem_is_heap_object (void *p)
Damjan Marion299571a2022-03-19 00:07:52 +0100747{
Damjan Marion24738582022-03-31 15:12:20 +0200748 return clib_mem_heap_is_heap_object (0, p);
749}
750
751__clib_export __clib_flatten void
752clib_mem_heap_free (void *heap, void *p)
753{
754 clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap ();
Damjan Marion299571a2022-03-19 00:07:52 +0100755 uword size = clib_mem_size (p);
756
757 /* Make sure object is in the correct heap. */
Damjan Marion24738582022-03-31 15:12:20 +0200758 ASSERT (clib_mem_heap_is_heap_object (h, p));
Damjan Marion299571a2022-03-19 00:07:52 +0100759
760 if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED))
761 mheap_put_trace (pointer_to_uword (p), size);
Damjan Marion79934e82022-04-05 12:40:31 +0200762 clib_mem_poison (p, clib_mem_size (p));
Damjan Marion299571a2022-03-19 00:07:52 +0100763
764 mspace_free (h->mspace, p);
765}
766
Damjan Marion24738582022-03-31 15:12:20 +0200767__clib_export __clib_flatten void
768clib_mem_free (void *p)
769{
770 clib_mem_heap_free (0, p);
771}
772
773__clib_export __clib_flatten uword
Damjan Marion299571a2022-03-19 00:07:52 +0100774clib_mem_size (void *p)
775{
776 return mspace_usable_size (p);
777}
778
779__clib_export void
780clib_mem_free_s (void *p)
781{
782 uword size = clib_mem_size (p);
Damjan Marion79934e82022-04-05 12:40:31 +0200783 clib_mem_unpoison (p, size);
Damjan Marion299571a2022-03-19 00:07:52 +0100784 memset_s_inline (p, size, 0, size);
785 clib_mem_free (p);
786}