Move te_prof_sample_event_lookahead into src/jemalloc.c and rename it to prof_sample_lookahead

This commit is contained in:
Slobodan Predolac 2026-06-02 11:51:23 -07:00
parent de9ad1450b
commit c88d5cf67f
2 changed files with 21 additions and 30 deletions

View file

@ -9,32 +9,6 @@
#include "jemalloc/internal/sz.h"
#include "jemalloc/internal/thread_event.h"
/*
* The lookahead functionality facilitates events to be able to lookahead, i.e.
* without touching the event counters, to determine whether an event would be
* triggered. The event counters are not advanced until the end of the
* allocation / deallocation calls, so the lookahead can be useful if some
* preparation work for some event must be done early in the allocation /
* deallocation calls.
*
* Currently only the profiling sampling event needs the lookahead
* functionality, so we don't yet define general purpose lookahead functions.
*
* Defined here rather than prof.h because the inline body depends on tsd
* accessors that aren't visible until tsd inlines are loaded.
*/
JEMALLOC_ALWAYS_INLINE bool
te_prof_sample_event_lookahead(tsd_t *tsd, size_t usize) {
if (unlikely(!tsd_nominal(tsd) || tsd_reentrancy_level_get(tsd) > 0)) {
return false;
}
/* The subtraction is intentionally susceptible to underflow. */
uint64_t accumbytes = tsd_thread_allocated_get(tsd) + usize
- tsd_thread_allocated_last_event_get(tsd);
return accumbytes >= tsd_prof_sample_event_wait_get(tsd);
}
JEMALLOC_ALWAYS_INLINE void
prof_active_assert(void) {
cassert(config_prof);

View file

@ -141,6 +141,23 @@ const char *const zero_realloc_mode_names[] = {
"abort",
};
/*
* Check whether the next allocation would trip the prof sampler without
* advancing the event counter -- the counter only advances at the end
* of the alloc/dalloc call. Lets the allocation path pre-compute the
* prof context before committing.
*/
JEMALLOC_ALWAYS_INLINE bool
prof_sample_lookahead(tsd_t *tsd, size_t usize) {
if (unlikely(!tsd_nominal(tsd) || tsd_reentrancy_level_get(tsd) > 0)) {
return false;
}
/* The subtraction is intentionally susceptible to underflow. */
uint64_t accumbytes = tsd_thread_allocated_get(tsd) + usize
- tsd_thread_allocated_last_event_get(tsd);
return accumbytes >= tsd_prof_sample_event_wait_get(tsd);
}
/*
* These are the documented values for junk fill debugging facilities -- see the
* man page.
@ -602,7 +619,7 @@ imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {
/* If profiling is on, get our profiling context. */
if (config_prof && opt_prof) {
bool prof_active = prof_active_get_unlocked();
bool sample_event = te_prof_sample_event_lookahead(tsd, usize);
bool sample_event = prof_sample_lookahead(tsd, usize);
prof_tctx_t *tctx = prof_alloc_prep(
tsd, prof_active, sample_event);
@ -1404,7 +1421,7 @@ irallocx_prof(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t size,
prof_info_t old_prof_info;
prof_info_get_and_reset_recent(tsd, old_ptr, alloc_ctx, &old_prof_info);
bool prof_active = prof_active_get_unlocked();
bool sample_event = te_prof_sample_event_lookahead(tsd, usize);
bool sample_event = prof_sample_lookahead(tsd, usize);
prof_tctx_t *tctx = prof_alloc_prep(tsd, prof_active, sample_event);
void *p;
if (unlikely(tctx != PROF_TCTX_SENTINEL)) {
@ -1642,7 +1659,7 @@ ixallocx_prof(tsd_t *tsd, void *ptr, size_t old_usize, size_t size,
usize_max = SC_LARGE_MAXCLASS;
}
bool prof_active = prof_active_get_unlocked();
bool sample_event = te_prof_sample_event_lookahead(tsd, usize_max);
bool sample_event = prof_sample_lookahead(tsd, usize_max);
prof_tctx_t *tctx = prof_alloc_prep(tsd, prof_active, sample_event);
size_t usize;
@ -1677,7 +1694,7 @@ ixallocx_prof(tsd_t *tsd, void *ptr, size_t old_usize, size_t size,
prof_info_get_and_reset_recent(
tsd, ptr, &new_alloc_ctx, &prof_info);
assert(usize <= usize_max);
sample_event = te_prof_sample_event_lookahead(tsd, usize);
sample_event = prof_sample_lookahead(tsd, usize);
prof_realloc(tsd, ptr, size, usize, tctx, prof_active, ptr,
old_usize, &prof_info, sample_event);
}