b55419f9b9
Develop new data structure and code logic for holding profiling related information stored in the extent that may be needed after the extent is released, which in particular is the case for the reallocation code path (e.g. in `rallocx()` and `xallocx()`). The data structure is a generalization of `prof_tctx_t`: we previously only copy out the `prof_tctx` before the extent is released, but we may be in need of additional fields. Currently the only additional field is the allocation time field, but there may be more fields in the future. The restructuring also resolved a bug: `prof_realloc()` mistakenly passed the new `ptr` to `prof_free_sampled_object()`, but passing in the `old_ptr` would crash because it's already been released. Now the essential profiling information is collectively copied out early and safely passed to `prof_free_sampled_object()` after the extent is released.
219 lines
5.8 KiB
C
219 lines
5.8 KiB
C
#ifndef JEMALLOC_INTERNAL_PROF_INLINES_B_H
|
|
#define JEMALLOC_INTERNAL_PROF_INLINES_B_H
|
|
|
|
#include "jemalloc/internal/safety_check.h"
|
|
#include "jemalloc/internal/sz.h"
|
|
#include "jemalloc/internal/thread_event.h"
|
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
|
prof_gdump_get_unlocked(void) {
|
|
/*
|
|
* No locking is used when reading prof_gdump_val in the fast path, so
|
|
* there are no guarantees regarding how long it will take for all
|
|
* threads to notice state changes.
|
|
*/
|
|
return prof_gdump_val;
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE prof_tdata_t *
|
|
prof_tdata_get(tsd_t *tsd, bool create) {
|
|
prof_tdata_t *tdata;
|
|
|
|
cassert(config_prof);
|
|
|
|
tdata = tsd_prof_tdata_get(tsd);
|
|
if (create) {
|
|
assert(tsd_reentrancy_level_get(tsd) == 0);
|
|
if (unlikely(tdata == NULL)) {
|
|
if (tsd_nominal(tsd)) {
|
|
tdata = prof_tdata_init(tsd);
|
|
tsd_prof_tdata_set(tsd, tdata);
|
|
}
|
|
} else if (unlikely(tdata->expired)) {
|
|
tdata = prof_tdata_reinit(tsd, tdata);
|
|
tsd_prof_tdata_set(tsd, tdata);
|
|
}
|
|
assert(tdata == NULL || tdata->attached);
|
|
}
|
|
|
|
return tdata;
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
prof_info_get(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx,
|
|
prof_info_t *prof_info) {
|
|
cassert(config_prof);
|
|
assert(ptr != NULL);
|
|
assert(prof_info != NULL);
|
|
|
|
arena_prof_info_get(tsdn, ptr, alloc_ctx, prof_info);
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
prof_tctx_set(tsdn_t *tsdn, const void *ptr, size_t usize,
|
|
alloc_ctx_t *alloc_ctx, prof_tctx_t *tctx) {
|
|
cassert(config_prof);
|
|
assert(ptr != NULL);
|
|
|
|
arena_prof_tctx_set(tsdn, ptr, usize, alloc_ctx, tctx);
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
prof_tctx_reset(tsdn_t *tsdn, const void *ptr, prof_tctx_t *tctx) {
|
|
cassert(config_prof);
|
|
assert(ptr != NULL);
|
|
|
|
arena_prof_tctx_reset(tsdn, ptr, tctx);
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
prof_alloc_time_set(tsdn_t *tsdn, const void *ptr, nstime_t t) {
|
|
cassert(config_prof);
|
|
assert(ptr != NULL);
|
|
|
|
arena_prof_alloc_time_set(tsdn, ptr, t);
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
|
prof_sample_accum_update(tsd_t *tsd, size_t usize, bool update,
|
|
prof_tdata_t **tdata_out) {
|
|
cassert(config_prof);
|
|
|
|
/* Fastpath: no need to load tdata */
|
|
if (likely(prof_sample_event_wait_get(tsd) > 0)) {
|
|
return true;
|
|
}
|
|
|
|
if (tsd_reentrancy_level_get(tsd) > 0) {
|
|
return true;
|
|
}
|
|
|
|
prof_tdata_t *tdata = prof_tdata_get(tsd, true);
|
|
if (unlikely((uintptr_t)tdata <= (uintptr_t)PROF_TDATA_STATE_MAX)) {
|
|
tdata = NULL;
|
|
}
|
|
|
|
if (tdata_out != NULL) {
|
|
*tdata_out = tdata;
|
|
}
|
|
|
|
if (unlikely(tdata == NULL)) {
|
|
return true;
|
|
}
|
|
|
|
/* Compute new sample threshold. */
|
|
if (update) {
|
|
prof_sample_threshold_update(tsd);
|
|
}
|
|
return !tdata->active;
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE prof_tctx_t *
|
|
prof_alloc_prep(tsd_t *tsd, size_t usize, bool prof_active, bool update) {
|
|
prof_tctx_t *ret;
|
|
prof_tdata_t *tdata;
|
|
prof_bt_t bt;
|
|
|
|
assert(usize == sz_s2u(usize));
|
|
|
|
if (!prof_active || likely(prof_sample_accum_update(tsd, usize, update,
|
|
&tdata))) {
|
|
ret = (prof_tctx_t *)(uintptr_t)1U;
|
|
} else {
|
|
bt_init(&bt, tdata->vec);
|
|
prof_backtrace(tsd, &bt);
|
|
ret = prof_lookup(tsd, &bt);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
prof_malloc(tsdn_t *tsdn, const void *ptr, size_t usize, alloc_ctx_t *alloc_ctx,
|
|
prof_tctx_t *tctx) {
|
|
cassert(config_prof);
|
|
assert(ptr != NULL);
|
|
assert(usize == isalloc(tsdn, ptr));
|
|
|
|
if (unlikely((uintptr_t)tctx > (uintptr_t)1U)) {
|
|
prof_malloc_sample_object(tsdn, ptr, usize, tctx);
|
|
} else {
|
|
prof_tctx_set(tsdn, ptr, usize, alloc_ctx,
|
|
(prof_tctx_t *)(uintptr_t)1U);
|
|
}
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
prof_realloc(tsd_t *tsd, const void *ptr, size_t usize, prof_tctx_t *tctx,
|
|
bool prof_active, bool updated, const void *old_ptr, size_t old_usize,
|
|
prof_info_t *old_prof_info) {
|
|
bool sampled, old_sampled, moved;
|
|
|
|
cassert(config_prof);
|
|
assert(ptr != NULL || (uintptr_t)tctx <= (uintptr_t)1U);
|
|
|
|
if (prof_active && !updated && ptr != NULL) {
|
|
assert(usize == isalloc(tsd_tsdn(tsd), ptr));
|
|
if (prof_sample_accum_update(tsd, usize, true, NULL)) {
|
|
/*
|
|
* Don't sample. The usize passed to prof_alloc_prep()
|
|
* was larger than what actually got allocated, so a
|
|
* backtrace was captured for this allocation, even
|
|
* though its actual usize was insufficient to cross the
|
|
* sample threshold.
|
|
*/
|
|
prof_alloc_rollback(tsd, tctx, true);
|
|
tctx = (prof_tctx_t *)(uintptr_t)1U;
|
|
}
|
|
}
|
|
|
|
sampled = ((uintptr_t)tctx > (uintptr_t)1U);
|
|
old_sampled = ((uintptr_t)old_prof_info->prof_tctx > (uintptr_t)1U);
|
|
moved = (ptr != old_ptr);
|
|
|
|
if (unlikely(sampled)) {
|
|
prof_malloc_sample_object(tsd_tsdn(tsd), ptr, usize, tctx);
|
|
} else if (moved) {
|
|
prof_tctx_set(tsd_tsdn(tsd), ptr, usize, NULL,
|
|
(prof_tctx_t *)(uintptr_t)1U);
|
|
} else if (unlikely(old_sampled)) {
|
|
/*
|
|
* prof_tctx_set() would work for the !moved case as well, but
|
|
* prof_tctx_reset() is slightly cheaper, and the proper thing
|
|
* to do here in the presence of explicit knowledge re: moved
|
|
* state.
|
|
*/
|
|
prof_tctx_reset(tsd_tsdn(tsd), ptr, tctx);
|
|
} else {
|
|
prof_info_t prof_info;
|
|
prof_info_get(tsd_tsdn(tsd), ptr, NULL, &prof_info);
|
|
assert((uintptr_t)prof_info.prof_tctx == (uintptr_t)1U);
|
|
}
|
|
|
|
/*
|
|
* The prof_free_sampled_object() call must come after the
|
|
* prof_malloc_sample_object() call, because tctx and old_tctx may be
|
|
* the same, in which case reversing the call order could cause the tctx
|
|
* to be prematurely destroyed as a side effect of momentarily zeroed
|
|
* counters.
|
|
*/
|
|
if (unlikely(old_sampled)) {
|
|
prof_free_sampled_object(tsd, old_usize, old_prof_info);
|
|
}
|
|
}
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
prof_free(tsd_t *tsd, const void *ptr, size_t usize, alloc_ctx_t *alloc_ctx) {
|
|
prof_info_t prof_info;
|
|
prof_info_get(tsd_tsdn(tsd), ptr, alloc_ctx, &prof_info);
|
|
|
|
cassert(config_prof);
|
|
assert(usize == isalloc(tsd_tsdn(tsd), ptr));
|
|
|
|
if (unlikely((uintptr_t)prof_info.prof_tctx > (uintptr_t)1U)) {
|
|
prof_free_sampled_object(tsd, usize, &prof_info);
|
|
}
|
|
}
|
|
|
|
#endif /* JEMALLOC_INTERNAL_PROF_INLINES_B_H */
|