1 #ifndef JEMALLOC_INTERNAL_PROF_INLINES_B_H
2 #define JEMALLOC_INTERNAL_PROF_INLINES_B_H
4 #include "jemalloc/internal/sz.h"
6 JEMALLOC_ALWAYS_INLINE bool
7 prof_gdump_get_unlocked(void) {
9 * No locking is used when reading prof_gdump_val in the fast path, so
10 * there are no guarantees regarding how long it will take for all
11 * threads to notice state changes.
13 return prof_gdump_val;
16 JEMALLOC_ALWAYS_INLINE prof_tdata_t *
17 prof_tdata_get(tsd_t *tsd, bool create) {
22 tdata = tsd_prof_tdata_get(tsd);
24 if (unlikely(tdata == NULL)) {
25 if (tsd_nominal(tsd)) {
26 tdata = prof_tdata_init(tsd);
27 tsd_prof_tdata_set(tsd, tdata);
29 } else if (unlikely(tdata->expired)) {
30 tdata = prof_tdata_reinit(tsd, tdata);
31 tsd_prof_tdata_set(tsd, tdata);
33 assert(tdata == NULL || tdata->attached);
39 JEMALLOC_ALWAYS_INLINE prof_tctx_t *
40 prof_tctx_get(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx) {
44 return arena_prof_tctx_get(tsdn, ptr, alloc_ctx);
47 JEMALLOC_ALWAYS_INLINE void
48 prof_tctx_set(tsdn_t *tsdn, const void *ptr, size_t usize,
49 alloc_ctx_t *alloc_ctx, prof_tctx_t *tctx) {
53 arena_prof_tctx_set(tsdn, ptr, usize, alloc_ctx, tctx);
56 JEMALLOC_ALWAYS_INLINE void
57 prof_tctx_reset(tsdn_t *tsdn, const void *ptr, prof_tctx_t *tctx) {
61 arena_prof_tctx_reset(tsdn, ptr, tctx);
64 JEMALLOC_ALWAYS_INLINE bool
65 prof_sample_accum_update(tsd_t *tsd, size_t usize, bool update,
66 prof_tdata_t **tdata_out) {
71 tdata = prof_tdata_get(tsd, true);
72 if (unlikely((uintptr_t)tdata <= (uintptr_t)PROF_TDATA_STATE_MAX)) {
76 if (tdata_out != NULL) {
80 if (unlikely(tdata == NULL)) {
84 if (likely(tdata->bytes_until_sample >= usize)) {
86 tdata->bytes_until_sample -= usize;
90 if (tsd_reentrancy_level_get(tsd) > 0) {
93 /* Compute new sample threshold. */
95 prof_sample_threshold_update(tdata);
97 return !tdata->active;
101 JEMALLOC_ALWAYS_INLINE prof_tctx_t *
102 prof_alloc_prep(tsd_t *tsd, size_t usize, bool prof_active, bool update) {
107 assert(usize == sz_s2u(usize));
109 if (!prof_active || likely(prof_sample_accum_update(tsd, usize, update,
111 ret = (prof_tctx_t *)(uintptr_t)1U;
113 bt_init(&bt, tdata->vec);
115 ret = prof_lookup(tsd, &bt);
121 JEMALLOC_ALWAYS_INLINE void
122 prof_malloc(tsdn_t *tsdn, const void *ptr, size_t usize, alloc_ctx_t *alloc_ctx,
124 cassert(config_prof);
126 assert(usize == isalloc(tsdn, ptr));
128 if (unlikely((uintptr_t)tctx > (uintptr_t)1U)) {
129 prof_malloc_sample_object(tsdn, ptr, usize, tctx);
131 prof_tctx_set(tsdn, ptr, usize, alloc_ctx,
132 (prof_tctx_t *)(uintptr_t)1U);
136 JEMALLOC_ALWAYS_INLINE void
137 prof_realloc(tsd_t *tsd, const void *ptr, size_t usize, prof_tctx_t *tctx,
138 bool prof_active, bool updated, const void *old_ptr, size_t old_usize,
139 prof_tctx_t *old_tctx) {
140 bool sampled, old_sampled, moved;
142 cassert(config_prof);
143 assert(ptr != NULL || (uintptr_t)tctx <= (uintptr_t)1U);
145 if (prof_active && !updated && ptr != NULL) {
146 assert(usize == isalloc(tsd_tsdn(tsd), ptr));
147 if (prof_sample_accum_update(tsd, usize, true, NULL)) {
149 * Don't sample. The usize passed to prof_alloc_prep()
150 * was larger than what actually got allocated, so a
151 * backtrace was captured for this allocation, even
152 * though its actual usize was insufficient to cross the
155 prof_alloc_rollback(tsd, tctx, true);
156 tctx = (prof_tctx_t *)(uintptr_t)1U;
160 sampled = ((uintptr_t)tctx > (uintptr_t)1U);
161 old_sampled = ((uintptr_t)old_tctx > (uintptr_t)1U);
162 moved = (ptr != old_ptr);
164 if (unlikely(sampled)) {
165 prof_malloc_sample_object(tsd_tsdn(tsd), ptr, usize, tctx);
167 prof_tctx_set(tsd_tsdn(tsd), ptr, usize, NULL,
168 (prof_tctx_t *)(uintptr_t)1U);
169 } else if (unlikely(old_sampled)) {
171 * prof_tctx_set() would work for the !moved case as well, but
172 * prof_tctx_reset() is slightly cheaper, and the proper thing
173 * to do here in the presence of explicit knowledge re: moved
176 prof_tctx_reset(tsd_tsdn(tsd), ptr, tctx);
178 assert((uintptr_t)prof_tctx_get(tsd_tsdn(tsd), ptr, NULL) ==
183 * The prof_free_sampled_object() call must come after the
184 * prof_malloc_sample_object() call, because tctx and old_tctx may be
185 * the same, in which case reversing the call order could cause the tctx
186 * to be prematurely destroyed as a side effect of momentarily zeroed
189 if (unlikely(old_sampled)) {
190 prof_free_sampled_object(tsd, old_usize, old_tctx);
194 JEMALLOC_ALWAYS_INLINE void
195 prof_free(tsd_t *tsd, const void *ptr, size_t usize, alloc_ctx_t *alloc_ctx) {
196 prof_tctx_t *tctx = prof_tctx_get(tsd_tsdn(tsd), ptr, alloc_ctx);
198 cassert(config_prof);
199 assert(usize == isalloc(tsd_tsdn(tsd), ptr));
201 if (unlikely((uintptr_t)tctx > (uintptr_t)1U)) {
202 prof_free_sampled_object(tsd, usize, tctx);
206 #endif /* JEMALLOC_INTERNAL_PROF_INLINES_B_H */