Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 1 | /* |
| 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 Marion | 7892560 | 2024-05-23 13:06:39 +0000 | [diff] [blame] | 22 | #include <vppinfra/stack.h> |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 23 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 24 | typedef struct |
| 25 | { |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 26 | clib_spinlock_t lock; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 27 | |
| 28 | mheap_trace_t *traces; |
| 29 | |
| 30 | /* Indices of free traces. */ |
| 31 | u32 *trace_free_list; |
| 32 | |
| 33 | /* Hash table mapping callers to trace index. */ |
| 34 | uword *trace_by_callers; |
| 35 | |
| 36 | /* Hash table mapping mheap offset to trace index. */ |
| 37 | uword *trace_index_by_offset; |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 38 | |
| 39 | /* So we can easily shut off current segment trace, if any */ |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 40 | const clib_mem_heap_t *current_traced_mheap; |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 41 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 42 | } mheap_trace_main_t; |
| 43 | |
| 44 | mheap_trace_main_t mheap_trace_main; |
| 45 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 46 | static __thread int mheap_trace_thread_disable; |
| 47 | |
| 48 | static void |
| 49 | mheap_get_trace_internal (const clib_mem_heap_t *heap, uword offset, |
| 50 | uword size) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 51 | { |
| 52 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 53 | mheap_trace_t *t; |
Damjan Marion | 7892560 | 2024-05-23 13:06:39 +0000 | [diff] [blame] | 54 | uword i, trace_index, *p; |
| 55 | mheap_trace_t trace = {}; |
Benoît Ganne | 661fb34 | 2024-10-08 16:43:12 +0200 | [diff] [blame] | 56 | int n_callers; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 57 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 58 | if (heap != tm->current_traced_mheap || mheap_trace_thread_disable) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 59 | return; |
| 60 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 61 | clib_spinlock_lock (&tm->lock); |
| 62 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 63 | /* heap could have changed while we were waiting on the lock */ |
| 64 | if (heap != tm->current_traced_mheap) |
| 65 | goto out; |
| 66 | |
| 67 | /* Turn off tracing for this thread to avoid embarrassment... */ |
| 68 | mheap_trace_thread_disable = 1; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 69 | |
Benoît Ganne | 661fb34 | 2024-10-08 16:43:12 +0200 | [diff] [blame] | 70 | /* Skip our frame and mspace_get_aligned's frame */ |
| 71 | n_callers = |
| 72 | clib_stack_frame_get_raw (trace.callers, ARRAY_LEN (trace.callers), 2); |
| 73 | if (n_callers == 0) |
Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 74 | goto out; |
| 75 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 76 | if (!tm->trace_by_callers) |
| 77 | tm->trace_by_callers = |
| 78 | hash_create_shmem (0, sizeof (trace.callers), sizeof (uword)); |
| 79 | |
| 80 | p = hash_get_mem (tm->trace_by_callers, &trace.callers); |
| 81 | if (p) |
| 82 | { |
| 83 | trace_index = p[0]; |
| 84 | t = tm->traces + trace_index; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | i = vec_len (tm->trace_free_list); |
| 89 | if (i > 0) |
| 90 | { |
| 91 | trace_index = tm->trace_free_list[i - 1]; |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 92 | vec_set_len (tm->trace_free_list, i - 1); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 93 | } |
| 94 | else |
| 95 | { |
| 96 | mheap_trace_t *old_start = tm->traces; |
| 97 | mheap_trace_t *old_end = vec_end (tm->traces); |
| 98 | |
| 99 | vec_add2 (tm->traces, t, 1); |
| 100 | |
| 101 | if (tm->traces != old_start) |
| 102 | { |
| 103 | hash_pair_t *p; |
| 104 | mheap_trace_t *q; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 105 | hash_foreach_pair (p, tm->trace_by_callers, |
| 106 | ({ |
| 107 | q = uword_to_pointer (p->key, mheap_trace_t *); |
| 108 | ASSERT (q >= old_start && q < old_end); |
| 109 | p->key = pointer_to_uword (tm->traces + (q - old_start)); |
| 110 | })); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 111 | } |
| 112 | trace_index = t - tm->traces; |
| 113 | } |
| 114 | |
| 115 | t = tm->traces + trace_index; |
| 116 | t[0] = trace; |
| 117 | t->n_allocations = 0; |
| 118 | t->n_bytes = 0; |
| 119 | hash_set_mem (tm->trace_by_callers, t->callers, trace_index); |
| 120 | } |
| 121 | |
| 122 | t->n_allocations += 1; |
| 123 | t->n_bytes += size; |
| 124 | t->offset = offset; /* keep a sample to autopsy */ |
| 125 | hash_set (tm->trace_index_by_offset, offset, t - tm->traces); |
Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 126 | |
| 127 | out: |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 128 | mheap_trace_thread_disable = 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 129 | clib_spinlock_unlock (&tm->lock); |
| 130 | } |
| 131 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 132 | static void |
| 133 | mheap_put_trace_internal (const clib_mem_heap_t *heap, uword offset, |
| 134 | uword size) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 135 | { |
| 136 | mheap_trace_t *t; |
| 137 | uword trace_index, *p; |
| 138 | mheap_trace_main_t *tm = &mheap_trace_main; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 139 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 140 | if (heap != tm->current_traced_mheap || mheap_trace_thread_disable) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 141 | return; |
| 142 | |
| 143 | clib_spinlock_lock (&tm->lock); |
| 144 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 145 | /* heap could have changed while we were waiting on the lock */ |
| 146 | if (heap != tm->current_traced_mheap) |
| 147 | goto out; |
| 148 | |
| 149 | /* Turn off tracing for this thread for a moment */ |
| 150 | mheap_trace_thread_disable = 1; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 151 | |
| 152 | p = hash_get (tm->trace_index_by_offset, offset); |
| 153 | if (!p) |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 154 | goto out; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 155 | |
| 156 | trace_index = p[0]; |
| 157 | hash_unset (tm->trace_index_by_offset, offset); |
| 158 | ASSERT (trace_index < vec_len (tm->traces)); |
| 159 | |
| 160 | t = tm->traces + trace_index; |
| 161 | ASSERT (t->n_allocations > 0); |
| 162 | ASSERT (t->n_bytes >= size); |
| 163 | t->n_allocations -= 1; |
| 164 | t->n_bytes -= size; |
| 165 | if (t->n_allocations == 0) |
| 166 | { |
| 167 | hash_unset_mem (tm->trace_by_callers, t->callers); |
| 168 | vec_add1 (tm->trace_free_list, trace_index); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 169 | clib_memset (t, 0, sizeof (t[0])); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 170 | } |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 171 | |
| 172 | out: |
| 173 | mheap_trace_thread_disable = 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 174 | clib_spinlock_unlock (&tm->lock); |
| 175 | } |
| 176 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 177 | void |
| 178 | mheap_get_trace (uword offset, uword size) |
| 179 | { |
| 180 | mheap_get_trace_internal (clib_mem_get_heap (), offset, size); |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | mheap_put_trace (uword offset, uword size) |
| 185 | { |
| 186 | mheap_put_trace_internal (clib_mem_get_heap (), offset, size); |
| 187 | } |
| 188 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 189 | always_inline void |
| 190 | mheap_trace_main_free (mheap_trace_main_t * tm) |
| 191 | { |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 192 | CLIB_SPINLOCK_ASSERT_LOCKED (&tm->lock); |
| 193 | tm->current_traced_mheap = 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 194 | vec_free (tm->traces); |
| 195 | vec_free (tm->trace_free_list); |
| 196 | hash_free (tm->trace_by_callers); |
| 197 | hash_free (tm->trace_index_by_offset); |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 198 | mheap_trace_thread_disable = 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 199 | } |
| 200 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 201 | static clib_mem_heap_t * |
| 202 | clib_mem_create_heap_internal (void *base, uword size, |
| 203 | clib_mem_page_sz_t log2_page_sz, int is_locked, |
| 204 | char *name) |
| 205 | { |
| 206 | clib_mem_heap_t *h; |
| 207 | u8 flags = 0; |
| 208 | int sz = sizeof (clib_mem_heap_t); |
| 209 | |
| 210 | if (base == 0) |
| 211 | { |
| 212 | log2_page_sz = clib_mem_log2_page_size_validate (log2_page_sz); |
| 213 | size = round_pow2 (size, clib_mem_page_bytes (log2_page_sz)); |
| 214 | base = clib_mem_vm_map_internal (0, log2_page_sz, size, -1, 0, |
| 215 | "main heap"); |
| 216 | |
| 217 | if (base == CLIB_MEM_VM_MAP_FAILED) |
| 218 | return 0; |
| 219 | |
| 220 | flags = CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY; |
| 221 | } |
| 222 | else |
| 223 | log2_page_sz = CLIB_MEM_PAGE_SZ_UNKNOWN; |
| 224 | |
| 225 | if (is_locked) |
| 226 | flags |= CLIB_MEM_HEAP_F_LOCKED; |
| 227 | |
| 228 | h = base; |
| 229 | h->base = base; |
| 230 | h->size = size; |
| 231 | h->log2_page_sz = log2_page_sz; |
| 232 | h->flags = flags; |
| 233 | sz = strlen (name); |
| 234 | strcpy (h->name, name); |
| 235 | sz = round_pow2 (sz + sizeof (clib_mem_heap_t), 16); |
| 236 | h->mspace = create_mspace_with_base (base + sz, size - sz, is_locked); |
| 237 | |
| 238 | mspace_disable_expand (h->mspace); |
| 239 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 240 | clib_mem_poison (mspace_least_addr (h->mspace), |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 241 | mspace_footprint (h->mspace)); |
| 242 | |
| 243 | return h; |
| 244 | } |
| 245 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 246 | /* Initialize CLIB heap based on memory/size given by user. |
| 247 | Set memory to 0 and CLIB will try to allocate its own heap. */ |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 248 | static void * |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 249 | clib_mem_init_internal (void *base, uword size, |
| 250 | clib_mem_page_sz_t log2_page_sz) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 251 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 252 | clib_mem_heap_t *h; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 253 | |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 254 | clib_mem_main_init (); |
| 255 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 256 | h = clib_mem_create_heap_internal (base, size, log2_page_sz, |
| 257 | 1 /*is_locked */ , "main heap"); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 258 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 259 | clib_mem_set_heap (h); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 260 | |
| 261 | if (mheap_trace_main.lock == 0) |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 262 | { |
| 263 | /* clib_spinlock_init() dynamically allocates the spinlock in the current |
| 264 | * per-cpu heap, but it is used for all traces accross all heaps and |
| 265 | * hence we can't really allocate it in the current per-cpu heap as it |
| 266 | * could be destroyed later */ |
| 267 | static struct clib_spinlock_s mheap_trace_main_lock = {}; |
| 268 | mheap_trace_main.lock = &mheap_trace_main_lock; |
| 269 | } |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 270 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 271 | return h; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 274 | __clib_export void * |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 275 | clib_mem_init (void *memory, uword memory_size) |
| 276 | { |
| 277 | return clib_mem_init_internal (memory, memory_size, |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 278 | CLIB_MEM_PAGE_SZ_DEFAULT); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 281 | __clib_export void * |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 282 | clib_mem_init_with_page_size (uword memory_size, |
| 283 | clib_mem_page_sz_t log2_page_sz) |
| 284 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 285 | return clib_mem_init_internal (0, memory_size, log2_page_sz); |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 286 | } |
| 287 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 288 | __clib_export void * |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 289 | clib_mem_init_thread_safe (void *memory, uword memory_size) |
| 290 | { |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 291 | return clib_mem_init_internal (memory, memory_size, |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 292 | CLIB_MEM_PAGE_SZ_DEFAULT); |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 293 | } |
| 294 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 295 | __clib_export void |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 296 | clib_mem_destroy (void) |
| 297 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 298 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 299 | clib_mem_heap_t *heap = clib_mem_get_heap (); |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 300 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 301 | if (heap->mspace == tm->current_traced_mheap) |
| 302 | mheap_trace (heap, 0); |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 303 | |
| 304 | destroy_mspace (heap->mspace); |
Damjan Marion | 16ca1a9 | 2022-04-28 17:46:13 +0200 | [diff] [blame] | 305 | clib_mem_vm_unmap (heap); |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 306 | } |
| 307 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 308 | __clib_export u8 * |
| 309 | format_clib_mem_usage (u8 *s, va_list *va) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 310 | { |
| 311 | int verbose = va_arg (*va, int); |
| 312 | return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (), |
| 313 | verbose); |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Magic decoder ring for mallinfo stats (ala dlmalloc): |
| 318 | * |
| 319 | * size_t arena; / * Non-mmapped space allocated (bytes) * / |
| 320 | * size_t ordblks; / * Number of free chunks * / |
| 321 | * size_t smblks; / * Number of free fastbin blocks * / |
| 322 | * size_t hblks; / * Number of mmapped regions * / |
| 323 | * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * / |
| 324 | * size_t usmblks; / * Maximum total allocated space (bytes) * / |
| 325 | * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * / |
| 326 | * size_t uordblks; / * Total allocated space (bytes) * / |
| 327 | * size_t fordblks; / * Total free space (bytes) * / |
| 328 | * size_t keepcost; / * Top-most, releasable space (bytes) * / |
| 329 | * |
| 330 | */ |
| 331 | |
| 332 | u8 * |
| 333 | format_msize (u8 * s, va_list * va) |
| 334 | { |
| 335 | uword a = va_arg (*va, uword); |
| 336 | |
| 337 | if (a >= 1ULL << 30) |
| 338 | s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30)))); |
| 339 | else if (a >= 1ULL << 20) |
| 340 | s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20)))); |
| 341 | else if (a >= 1ULL << 10) |
| 342 | s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10)))); |
| 343 | else |
| 344 | s = format (s, "%lld", a); |
| 345 | return s; |
| 346 | } |
| 347 | |
| 348 | static int |
| 349 | mheap_trace_sort (const void *_t1, const void *_t2) |
| 350 | { |
| 351 | const mheap_trace_t *t1 = _t1; |
| 352 | const mheap_trace_t *t2 = _t2; |
| 353 | word cmp; |
| 354 | |
| 355 | cmp = (word) t2->n_bytes - (word) t1->n_bytes; |
| 356 | if (!cmp) |
| 357 | cmp = (word) t2->n_allocations - (word) t1->n_allocations; |
| 358 | return cmp; |
| 359 | } |
| 360 | |
| 361 | u8 * |
| 362 | format_mheap_trace (u8 * s, va_list * va) |
| 363 | { |
| 364 | mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *); |
| 365 | int verbose = va_arg (*va, int); |
| 366 | int have_traces = 0; |
| 367 | int i; |
Benoît Ganne | af62f93 | 2023-02-08 18:54:30 +0100 | [diff] [blame] | 368 | int n = 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 369 | |
| 370 | clib_spinlock_lock (&tm->lock); |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 371 | if (vec_len (tm->traces) > 0 && |
| 372 | clib_mem_get_heap () == tm->current_traced_mheap) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 373 | { |
| 374 | have_traces = 1; |
| 375 | |
| 376 | /* Make a copy of traces since we'll be sorting them. */ |
| 377 | mheap_trace_t *t, *traces_copy; |
| 378 | u32 indent, total_objects_traced; |
| 379 | |
| 380 | traces_copy = vec_dup (tm->traces); |
| 381 | |
| 382 | qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]), |
| 383 | mheap_trace_sort); |
| 384 | |
| 385 | total_objects_traced = 0; |
| 386 | s = format (s, "\n"); |
| 387 | vec_foreach (t, traces_copy) |
| 388 | { |
| 389 | /* Skip over free elements. */ |
| 390 | if (t->n_allocations == 0) |
| 391 | continue; |
| 392 | |
| 393 | total_objects_traced += t->n_allocations; |
| 394 | |
Benoît Ganne | af62f93 | 2023-02-08 18:54:30 +0100 | [diff] [blame] | 395 | /* When not verbose only report the 50 biggest allocations */ |
| 396 | if (!verbose && n >= 50) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 397 | continue; |
Benoît Ganne | af62f93 | 2023-02-08 18:54:30 +0100 | [diff] [blame] | 398 | n++; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 399 | |
| 400 | if (t == traces_copy) |
| 401 | s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count", |
| 402 | "Sample"); |
| 403 | s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset); |
| 404 | indent = format_get_indent (s); |
| 405 | for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++) |
| 406 | { |
| 407 | if (i > 0) |
| 408 | s = format (s, "%U", format_white_space, indent); |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 409 | #if defined(CLIB_UNIX) && !defined(__APPLE__) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 410 | /* $$$$ does this actually work? */ |
| 411 | s = |
| 412 | format (s, " %U\n", format_clib_elf_symbol_with_address, |
| 413 | t->callers[i]); |
| 414 | #else |
| 415 | s = format (s, " %p\n", t->callers[i]); |
| 416 | #endif |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | s = format (s, "%d total traced objects\n", total_objects_traced); |
| 421 | |
| 422 | vec_free (traces_copy); |
| 423 | } |
| 424 | clib_spinlock_unlock (&tm->lock); |
| 425 | if (have_traces == 0) |
| 426 | s = format (s, "no traced allocations\n"); |
| 427 | |
| 428 | return s; |
| 429 | } |
| 430 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 431 | __clib_export u8 * |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 432 | format_clib_mem_heap (u8 * s, va_list * va) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 433 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 434 | clib_mem_heap_t *heap = va_arg (*va, clib_mem_heap_t *); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 435 | int verbose = va_arg (*va, int); |
Dave Barach | af7dd5b | 2018-08-23 17:08:44 -0400 | [diff] [blame] | 436 | struct dlmallinfo mi; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 437 | mheap_trace_main_t *tm = &mheap_trace_main; |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 438 | u32 indent = format_get_indent (s) + 2; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 439 | |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 440 | if (heap == 0) |
| 441 | heap = clib_mem_get_heap (); |
| 442 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 443 | mi = mspace_mallinfo (heap->mspace); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 444 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 445 | s = format (s, "base %p, size %U", |
| 446 | heap->base, format_memory_size, heap->size); |
| 447 | |
| 448 | #define _(i,v,str) \ |
| 449 | if (heap->flags & CLIB_MEM_HEAP_F_##v) s = format (s, ", %s", str); |
| 450 | foreach_clib_mem_heap_flag; |
| 451 | #undef _ |
| 452 | |
| 453 | s = format (s, ", name '%s'", heap->name); |
| 454 | |
| 455 | if (heap->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN) |
| 456 | { |
| 457 | clib_mem_page_stats_t stats; |
| 458 | clib_mem_get_page_stats (heap->base, heap->log2_page_sz, |
| 459 | heap->size >> heap->log2_page_sz, &stats); |
| 460 | s = format (s, "\n%U%U", format_white_space, indent, |
| 461 | format_clib_mem_page_stats, &stats); |
| 462 | } |
| 463 | |
| 464 | s = format (s, "\n%Utotal: %U, used: %U, free: %U, trimmable: %U", |
| 465 | format_white_space, indent, |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 466 | format_msize, mi.arena, |
| 467 | format_msize, mi.uordblks, |
| 468 | format_msize, mi.fordblks, format_msize, mi.keepcost); |
| 469 | if (verbose > 0) |
| 470 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 471 | s = format (s, "\n%Ufree chunks %llu free fastbin blks %llu", |
| 472 | format_white_space, indent + 2, mi.ordblks, mi.smblks); |
| 473 | s = format (s, "\n%Umax total allocated %U", |
| 474 | format_white_space, indent + 2, format_msize, mi.usmblks); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 475 | } |
| 476 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 477 | if (heap->flags & CLIB_MEM_HEAP_F_TRACED) |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 478 | s = format (s, "\n%U", format_mheap_trace, tm, verbose); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 479 | return s; |
| 480 | } |
| 481 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 482 | __clib_export __clib_flatten void |
| 483 | clib_mem_get_heap_usage (clib_mem_heap_t *heap, clib_mem_usage_t *usage) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 484 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 485 | struct dlmallinfo mi = mspace_mallinfo (heap->mspace); |
Ole Troan | 92e3082 | 2019-06-16 12:33:51 +0200 | [diff] [blame] | 486 | |
Ole Troan | a606d92 | 2021-05-05 09:23:17 +0200 | [diff] [blame] | 487 | usage->bytes_total = mi.arena; /* non-mmapped space allocated from system */ |
| 488 | usage->bytes_used = mi.uordblks; /* total allocated space */ |
| 489 | usage->bytes_free = mi.fordblks; /* total free space */ |
| 490 | usage->bytes_used_mmap = mi.hblkhd; /* space in mmapped regions */ |
| 491 | usage->bytes_max = mi.usmblks; /* maximum total allocated space */ |
| 492 | usage->bytes_free_reclaimed = mi.ordblks; /* number of free chunks */ |
| 493 | usage->bytes_overhead = mi.keepcost; /* releasable (via malloc_trim) space */ |
| 494 | |
| 495 | /* Not supported */ |
| 496 | usage->bytes_used_sbrk = 0; |
Ole Troan | 92e3082 | 2019-06-16 12:33:51 +0200 | [diff] [blame] | 497 | usage->object_count = 0; |
Ole Troan | 92e3082 | 2019-06-16 12:33:51 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 500 | /* Call serial number for debugger breakpoints. */ |
| 501 | uword clib_mem_validate_serial = 0; |
| 502 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 503 | __clib_export void |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 504 | mheap_trace (clib_mem_heap_t * h, int enable) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 505 | { |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 506 | mheap_trace_main_t *tm = &mheap_trace_main; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 507 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 508 | clib_spinlock_lock (&tm->lock); |
| 509 | |
| 510 | if (tm->current_traced_mheap != 0 && tm->current_traced_mheap != h) |
| 511 | { |
| 512 | clib_warning ("tracing already enabled for another heap, ignoring"); |
| 513 | goto out; |
| 514 | } |
| 515 | |
| 516 | if (enable) |
| 517 | { |
| 518 | h->flags |= CLIB_MEM_HEAP_F_TRACED; |
| 519 | tm->current_traced_mheap = h; |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | h->flags &= ~CLIB_MEM_HEAP_F_TRACED; |
| 524 | mheap_trace_main_free (&mheap_trace_main); |
| 525 | } |
| 526 | |
| 527 | out: |
| 528 | clib_spinlock_unlock (&tm->lock); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 529 | } |
| 530 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 531 | __clib_export void |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 532 | clib_mem_trace (int enable) |
| 533 | { |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 534 | void *current_heap = clib_mem_get_heap (); |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 535 | mheap_trace (current_heap, enable); |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | int |
| 539 | clib_mem_is_traced (void) |
| 540 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 541 | clib_mem_heap_t *h = clib_mem_get_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 542 | return (h->flags &= CLIB_MEM_HEAP_F_TRACED) != 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 543 | } |
| 544 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 545 | __clib_export uword |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 546 | clib_mem_trace_enable_disable (uword enable) |
| 547 | { |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 548 | uword rv = !mheap_trace_thread_disable; |
| 549 | mheap_trace_thread_disable = !enable; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 550 | return rv; |
| 551 | } |
| 552 | |
Matus Fabian | ef827b3 | 2024-07-19 15:24:10 +0200 | [diff] [blame] | 553 | __clib_export mheap_trace_t * |
| 554 | clib_mem_trace_dup (clib_mem_heap_t *heap) |
| 555 | { |
| 556 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 557 | mheap_trace_t *traces_copy = 0; |
| 558 | |
| 559 | clib_spinlock_lock (&tm->lock); |
| 560 | if (vec_len (tm->traces) > 0 && heap == tm->current_traced_mheap) |
| 561 | { |
| 562 | traces_copy = vec_dup (tm->traces); |
| 563 | qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]), |
| 564 | mheap_trace_sort); |
| 565 | } |
| 566 | clib_spinlock_unlock (&tm->lock); |
| 567 | return traces_copy; |
| 568 | } |
| 569 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 570 | __clib_export clib_mem_heap_t * |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 571 | clib_mem_create_heap (void *base, uword size, int is_locked, char *fmt, ...) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 572 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 573 | clib_mem_page_sz_t log2_page_sz = clib_mem_get_log2_page_size (); |
| 574 | clib_mem_heap_t *h; |
| 575 | char *name; |
| 576 | u8 *s = 0; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 577 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 578 | if (fmt == 0) |
| 579 | { |
| 580 | name = ""; |
| 581 | } |
| 582 | else if (strchr (fmt, '%')) |
| 583 | { |
| 584 | va_list va; |
| 585 | va_start (va, fmt); |
| 586 | s = va_format (0, fmt, &va); |
| 587 | vec_add1 (s, 0); |
| 588 | va_end (va); |
| 589 | name = (char *) s; |
| 590 | } |
| 591 | else |
| 592 | name = fmt; |
| 593 | |
| 594 | h = clib_mem_create_heap_internal (base, size, log2_page_sz, is_locked, |
| 595 | name); |
| 596 | vec_free (s); |
| 597 | return h; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 600 | __clib_export void |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 601 | clib_mem_destroy_heap (clib_mem_heap_t * h) |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 602 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 603 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 604 | |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 605 | if (h->mspace == tm->current_traced_mheap) |
| 606 | mheap_trace (h, 0); |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 607 | |
| 608 | destroy_mspace (h->mspace); |
| 609 | if (h->flags & CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY) |
| 610 | clib_mem_vm_unmap (h->base); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 613 | __clib_export __clib_flatten uword |
| 614 | clib_mem_get_heap_free_space (clib_mem_heap_t *h) |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 615 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 616 | struct dlmallinfo dlminfo = mspace_mallinfo (h->mspace); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 617 | return dlminfo.fordblks; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 618 | } |
| 619 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 620 | __clib_export __clib_flatten void * |
| 621 | clib_mem_get_heap_base (clib_mem_heap_t *h) |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 622 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 623 | return h->base; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 624 | } |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 625 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 626 | __clib_export __clib_flatten uword |
| 627 | clib_mem_get_heap_size (clib_mem_heap_t *heap) |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 628 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 629 | return heap->size; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 632 | /* Memory allocator which may call os_out_of_memory() if it fails */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 633 | static inline void * |
| 634 | clib_mem_heap_alloc_inline (void *heap, uword size, uword align, |
| 635 | int os_out_of_memory_on_failure) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 636 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 637 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 638 | void *p; |
| 639 | |
| 640 | align = clib_max (CLIB_MEM_MIN_ALIGN, align); |
| 641 | |
| 642 | p = mspace_memalign (h->mspace, align, size); |
| 643 | |
| 644 | if (PREDICT_FALSE (0 == p)) |
| 645 | { |
| 646 | if (os_out_of_memory_on_failure) |
| 647 | os_out_of_memory (); |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED)) |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 652 | mheap_get_trace_internal (h, pointer_to_uword (p), clib_mem_size (p)); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 653 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 654 | clib_mem_unpoison (p, size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 655 | return p; |
| 656 | } |
| 657 | |
| 658 | /* Memory allocator which calls os_out_of_memory() when it fails */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 659 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 660 | clib_mem_alloc (uword size) |
| 661 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 662 | return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN, |
| 663 | /* os_out_of_memory */ 1); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 666 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 667 | clib_mem_alloc_aligned (uword size, uword align) |
| 668 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 669 | return clib_mem_heap_alloc_inline (0, size, align, |
| 670 | /* os_out_of_memory */ 1); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* Memory allocator which calls os_out_of_memory() when it fails */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 674 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 675 | clib_mem_alloc_or_null (uword size) |
| 676 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 677 | return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN, |
| 678 | /* os_out_of_memory */ 0); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 681 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 682 | clib_mem_alloc_aligned_or_null (uword size, uword align) |
| 683 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 684 | return clib_mem_heap_alloc_inline (0, size, align, |
| 685 | /* os_out_of_memory */ 0); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 688 | __clib_export __clib_flatten void * |
| 689 | clib_mem_heap_alloc (void *heap, uword size) |
| 690 | { |
| 691 | return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN, |
| 692 | /* os_out_of_memory */ 1); |
| 693 | } |
| 694 | |
| 695 | __clib_export __clib_flatten void * |
| 696 | clib_mem_heap_alloc_aligned (void *heap, uword size, uword align) |
| 697 | { |
| 698 | return clib_mem_heap_alloc_inline (heap, size, align, |
| 699 | /* os_out_of_memory */ 1); |
| 700 | } |
| 701 | |
| 702 | __clib_export __clib_flatten void * |
| 703 | clib_mem_heap_alloc_or_null (void *heap, uword size) |
| 704 | { |
| 705 | return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN, |
| 706 | /* os_out_of_memory */ 0); |
| 707 | } |
| 708 | |
| 709 | __clib_export __clib_flatten void * |
| 710 | clib_mem_heap_alloc_aligned_or_null (void *heap, uword size, uword align) |
| 711 | { |
| 712 | return clib_mem_heap_alloc_inline (heap, size, align, |
| 713 | /* os_out_of_memory */ 0); |
| 714 | } |
| 715 | |
| 716 | __clib_export __clib_flatten void * |
| 717 | clib_mem_heap_realloc_aligned (void *heap, void *p, uword new_size, |
| 718 | uword align) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 719 | { |
| 720 | uword old_alloc_size; |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 721 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 722 | void *new; |
| 723 | |
| 724 | ASSERT (count_set_bits (align) == 1); |
| 725 | |
| 726 | old_alloc_size = p ? mspace_usable_size (p) : 0; |
| 727 | |
| 728 | if (new_size == old_alloc_size) |
| 729 | return p; |
| 730 | |
| 731 | if (p && pointer_is_aligned (p, align) && |
| 732 | mspace_realloc_in_place (h->mspace, p, new_size)) |
| 733 | { |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 734 | clib_mem_unpoison (p, new_size); |
Leung Lai Yung | 69be089 | 2022-05-22 13:25:53 +0000 | [diff] [blame] | 735 | if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED)) |
| 736 | { |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 737 | mheap_put_trace_internal (h, pointer_to_uword (p), old_alloc_size); |
| 738 | mheap_get_trace_internal (h, pointer_to_uword (p), |
| 739 | clib_mem_size (p)); |
Leung Lai Yung | 69be089 | 2022-05-22 13:25:53 +0000 | [diff] [blame] | 740 | } |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 741 | } |
| 742 | else |
| 743 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 744 | new = clib_mem_heap_alloc_inline (h, new_size, align, 1); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 745 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 746 | clib_mem_unpoison (new, new_size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 747 | if (old_alloc_size) |
| 748 | { |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 749 | clib_mem_unpoison (p, old_alloc_size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 750 | clib_memcpy_fast (new, p, clib_min (new_size, old_alloc_size)); |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 751 | clib_mem_heap_free (h, p); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 752 | } |
| 753 | p = new; |
| 754 | } |
| 755 | |
| 756 | return p; |
| 757 | } |
| 758 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 759 | __clib_export __clib_flatten void * |
| 760 | clib_mem_heap_realloc (void *heap, void *p, uword new_size) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 761 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 762 | return clib_mem_heap_realloc_aligned (heap, p, new_size, CLIB_MEM_MIN_ALIGN); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 763 | } |
| 764 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 765 | __clib_export __clib_flatten void * |
| 766 | clib_mem_realloc_aligned (void *p, uword new_size, uword align) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 767 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 768 | return clib_mem_heap_realloc_aligned (0, p, new_size, align); |
| 769 | } |
| 770 | |
| 771 | __clib_export __clib_flatten void * |
| 772 | clib_mem_realloc (void *p, uword new_size) |
| 773 | { |
| 774 | return clib_mem_heap_realloc_aligned (0, p, new_size, CLIB_MEM_MIN_ALIGN); |
| 775 | } |
| 776 | |
| 777 | __clib_export __clib_flatten uword |
| 778 | clib_mem_heap_is_heap_object (void *heap, void *p) |
| 779 | { |
| 780 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 781 | return mspace_is_heap_object (h->mspace, p); |
| 782 | } |
| 783 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 784 | __clib_export __clib_flatten uword |
| 785 | clib_mem_is_heap_object (void *p) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 786 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 787 | return clib_mem_heap_is_heap_object (0, p); |
| 788 | } |
| 789 | |
| 790 | __clib_export __clib_flatten void |
| 791 | clib_mem_heap_free (void *heap, void *p) |
| 792 | { |
| 793 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 794 | uword size = clib_mem_size (p); |
| 795 | |
| 796 | /* Make sure object is in the correct heap. */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 797 | ASSERT (clib_mem_heap_is_heap_object (h, p)); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 798 | |
| 799 | if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED)) |
Benoît Ganne | 22460d6 | 2022-11-16 19:36:15 +0100 | [diff] [blame] | 800 | mheap_put_trace_internal (h, pointer_to_uword (p), size); |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 801 | clib_mem_poison (p, clib_mem_size (p)); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 802 | |
| 803 | mspace_free (h->mspace, p); |
| 804 | } |
| 805 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 806 | __clib_export __clib_flatten void |
| 807 | clib_mem_free (void *p) |
| 808 | { |
| 809 | clib_mem_heap_free (0, p); |
| 810 | } |
| 811 | |
| 812 | __clib_export __clib_flatten uword |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 813 | clib_mem_size (void *p) |
| 814 | { |
| 815 | return mspace_usable_size (p); |
| 816 | } |
| 817 | |
| 818 | __clib_export void |
| 819 | clib_mem_free_s (void *p) |
| 820 | { |
| 821 | uword size = clib_mem_size (p); |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 822 | clib_mem_unpoison (p, size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 823 | memset_s_inline (p, size, 0, size); |
| 824 | clib_mem_free (p); |
| 825 | } |