2019-09-04 06:04:48 +08:00
|
|
|
#ifndef JEMALLOC_INTERNAL_THREAD_EVENT_H
|
|
|
|
#define JEMALLOC_INTERNAL_THREAD_EVENT_H
|
|
|
|
|
|
|
|
#include "jemalloc/internal/tsd.h"
|
|
|
|
|
2020-01-31 08:31:45 +08:00
|
|
|
/* "te" is short for "thread_event" */
|
|
|
|
|
2020-04-17 02:50:29 +08:00
|
|
|
/*
|
|
|
|
* TE_MIN_START_WAIT should not exceed the minimal allocation usize.
|
|
|
|
*/
|
|
|
|
#define TE_MIN_START_WAIT ((uint64_t)1U)
|
|
|
|
#define TE_MAX_START_WAIT UINT64_MAX
|
|
|
|
|
2019-09-04 06:04:48 +08:00
|
|
|
/*
|
2020-01-29 13:12:06 +08:00
|
|
|
* Maximum threshold on thread_(de)allocated_next_event_fast, so that there is
|
|
|
|
* no need to check overflow in malloc fast path. (The allocation size in malloc
|
2019-09-04 06:04:48 +08:00
|
|
|
* fast path never exceeds SC_LOOKUP_MAXCLASS.)
|
|
|
|
*/
|
2020-01-31 08:31:45 +08:00
|
|
|
#define TE_NEXT_EVENT_FAST_MAX (UINT64_MAX - SC_LOOKUP_MAXCLASS + 1U)
|
2019-09-04 06:04:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The max interval helps make sure that malloc stays on the fast path in the
|
2020-01-29 13:12:06 +08:00
|
|
|
* common case, i.e. thread_allocated < thread_allocated_next_event_fast. When
|
2020-01-31 08:31:45 +08:00
|
|
|
* thread_allocated is within an event's distance to TE_NEXT_EVENT_FAST_MAX
|
2020-01-29 13:12:06 +08:00
|
|
|
* above, thread_allocated_next_event_fast is wrapped around and we fall back to
|
|
|
|
* the medium-fast path. The max interval makes sure that we're not staying on
|
|
|
|
* the fallback case for too long, even if there's no active event or if all
|
|
|
|
* active events have long wait times.
|
2019-09-04 06:04:48 +08:00
|
|
|
*/
|
2020-01-31 08:31:45 +08:00
|
|
|
#define TE_MAX_INTERVAL ((uint64_t)(4U << 20))
|
2019-09-04 06:04:48 +08:00
|
|
|
|
2020-04-18 01:38:06 +08:00
|
|
|
/*
|
|
|
|
* Invalid elapsed time, for situations where elapsed time is not needed. See
|
|
|
|
* comments in thread_event.c for more info.
|
|
|
|
*/
|
|
|
|
#define TE_INVALID_ELAPSED UINT64_MAX
|
|
|
|
|
2020-01-31 08:31:45 +08:00
|
|
|
typedef struct te_ctx_s {
|
2020-01-29 13:12:06 +08:00
|
|
|
bool is_alloc;
|
|
|
|
uint64_t *current;
|
|
|
|
uint64_t *last_event;
|
|
|
|
uint64_t *next_event;
|
|
|
|
uint64_t *next_event_fast;
|
2020-01-31 08:31:45 +08:00
|
|
|
} te_ctx_t;
|
2020-01-29 13:12:06 +08:00
|
|
|
|
2020-01-31 08:31:45 +08:00
|
|
|
void te_assert_invariants_debug(tsd_t *tsd);
|
2020-03-26 00:33:52 +08:00
|
|
|
void te_event_trigger(tsd_t *tsd, te_ctx_t *ctx);
|
2020-01-31 08:31:45 +08:00
|
|
|
void te_recompute_fast_threshold(tsd_t *tsd);
|
|
|
|
void tsd_te_init(tsd_t *tsd);
|
2019-09-04 06:04:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* List of all events, in the following format:
|
2020-01-29 13:12:06 +08:00
|
|
|
* E(event, (condition), is_alloc_event)
|
2019-09-04 06:04:48 +08:00
|
|
|
*/
|
|
|
|
#define ITERATE_OVER_ALL_EVENTS \
|
2020-05-12 06:53:30 +08:00
|
|
|
E(tcache_gc, (opt_tcache_gc_incr_bytes > 0), true) \
|
2020-01-29 13:12:06 +08:00
|
|
|
E(prof_sample, (config_prof && opt_prof), true) \
|
|
|
|
E(stats_interval, (opt_stats_interval >= 0), true) \
|
2020-05-12 06:53:30 +08:00
|
|
|
E(tcache_gc_dalloc, (opt_tcache_gc_incr_bytes > 0), false)
|
2019-09-04 06:04:48 +08:00
|
|
|
|
2020-01-29 13:12:06 +08:00
|
|
|
#define E(event, condition_unused, is_alloc_event_unused) \
|
2019-09-04 06:04:48 +08:00
|
|
|
C(event##_event_wait)
|
|
|
|
|
|
|
|
/* List of all thread event counters. */
|
|
|
|
#define ITERATE_OVER_ALL_COUNTERS \
|
|
|
|
C(thread_allocated) \
|
|
|
|
C(thread_allocated_last_event) \
|
2019-10-15 00:35:51 +08:00
|
|
|
ITERATE_OVER_ALL_EVENTS \
|
2020-01-14 14:29:17 +08:00
|
|
|
C(prof_sample_last_event) \
|
|
|
|
C(stats_interval_last_event)
|
2019-09-04 06:04:48 +08:00
|
|
|
|
|
|
|
/* Getters directly wrap TSD getters. */
|
|
|
|
#define C(counter) \
|
|
|
|
JEMALLOC_ALWAYS_INLINE uint64_t \
|
|
|
|
counter##_get(tsd_t *tsd) { \
|
|
|
|
return tsd_##counter##_get(tsd); \
|
|
|
|
}
|
|
|
|
|
|
|
|
ITERATE_OVER_ALL_COUNTERS
|
|
|
|
#undef C
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setters call the TSD pointer getters rather than the TSD setters, so that
|
|
|
|
* the counters can be modified even when TSD state is reincarnated or
|
|
|
|
* minimal_initialized: if an event is triggered in such cases, we will
|
|
|
|
* temporarily delay the event and let it be immediately triggered at the next
|
|
|
|
* allocation call.
|
|
|
|
*/
|
|
|
|
#define C(counter) \
|
|
|
|
JEMALLOC_ALWAYS_INLINE void \
|
|
|
|
counter##_set(tsd_t *tsd, uint64_t v) { \
|
|
|
|
*tsd_##counter##p_get(tsd) = v; \
|
|
|
|
}
|
|
|
|
|
|
|
|
ITERATE_OVER_ALL_COUNTERS
|
|
|
|
#undef C
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For generating _event_wait getter / setter functions for each individual
|
|
|
|
* event.
|
|
|
|
*/
|
|
|
|
#undef E
|
|
|
|
|
2019-11-12 08:34:48 +08:00
|
|
|
/*
|
2020-01-29 13:12:06 +08:00
|
|
|
* The malloc and free fastpath getters -- use the unsafe getters since tsd may
|
|
|
|
* be non-nominal, in which case the fast_threshold will be set to 0. This
|
|
|
|
* allows checking for events and tsd non-nominal in a single branch.
|
2019-11-12 08:34:48 +08:00
|
|
|
*
|
|
|
|
* Note that these can only be used on the fastpath.
|
|
|
|
*/
|
2020-01-31 08:31:45 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
|
|
te_malloc_fastpath_ctx(tsd_t *tsd, uint64_t *allocated, uint64_t *threshold) {
|
|
|
|
*allocated = *tsd_thread_allocatedp_get_unsafe(tsd);
|
|
|
|
*threshold = *tsd_thread_allocated_next_event_fastp_get_unsafe(tsd);
|
|
|
|
assert(*threshold <= TE_NEXT_EVENT_FAST_MAX);
|
2019-11-12 08:34:48 +08:00
|
|
|
}
|
|
|
|
|
2020-01-29 13:12:06 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2020-01-31 08:31:45 +08:00
|
|
|
te_free_fastpath_ctx(tsd_t *tsd, uint64_t *deallocated, uint64_t *threshold,
|
|
|
|
bool size_hint) {
|
2020-01-29 13:12:06 +08:00
|
|
|
if (!size_hint) {
|
|
|
|
*deallocated = tsd_thread_deallocated_get(tsd);
|
|
|
|
*threshold = tsd_thread_deallocated_next_event_fast_get(tsd);
|
|
|
|
} else {
|
|
|
|
/* Unsafe getters since this may happen before tsd_init. */
|
|
|
|
*deallocated = *tsd_thread_deallocatedp_get_unsafe(tsd);
|
|
|
|
*threshold =
|
|
|
|
*tsd_thread_deallocated_next_event_fastp_get_unsafe(tsd);
|
|
|
|
}
|
2020-01-31 08:31:45 +08:00
|
|
|
assert(*threshold <= TE_NEXT_EVENT_FAST_MAX);
|
2020-01-29 13:12:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
2020-01-31 08:31:45 +08:00
|
|
|
te_ctx_is_alloc(te_ctx_t *ctx) {
|
2020-01-29 13:12:06 +08:00
|
|
|
return ctx->is_alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE uint64_t
|
2020-01-31 08:31:45 +08:00
|
|
|
te_ctx_current_bytes_get(te_ctx_t *ctx) {
|
2020-01-29 13:12:06 +08:00
|
|
|
return *ctx->current;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2020-01-31 08:31:45 +08:00
|
|
|
te_ctx_current_bytes_set(te_ctx_t *ctx, uint64_t v) {
|
2020-01-29 13:12:06 +08:00
|
|
|
*ctx->current = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE uint64_t
|
2020-01-31 08:31:45 +08:00
|
|
|
te_ctx_last_event_get(te_ctx_t *ctx) {
|
2020-01-29 13:12:06 +08:00
|
|
|
return *ctx->last_event;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2020-01-31 08:31:45 +08:00
|
|
|
te_ctx_last_event_set(te_ctx_t *ctx, uint64_t v) {
|
2020-01-29 13:12:06 +08:00
|
|
|
*ctx->last_event = v;
|
|
|
|
}
|
|
|
|
|
2019-11-12 08:34:48 +08:00
|
|
|
/* Below 3 for next_event_fast. */
|
|
|
|
JEMALLOC_ALWAYS_INLINE uint64_t
|
2020-01-31 08:31:45 +08:00
|
|
|
te_ctx_next_event_fast_get(te_ctx_t *ctx) {
|
2020-01-29 13:12:06 +08:00
|
|
|
uint64_t v = *ctx->next_event_fast;
|
2020-01-31 08:31:45 +08:00
|
|
|
assert(v <= TE_NEXT_EVENT_FAST_MAX);
|
2019-11-12 08:34:48 +08:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2020-01-31 08:31:45 +08:00
|
|
|
te_ctx_next_event_fast_set(te_ctx_t *ctx, uint64_t v) {
|
|
|
|
assert(v <= TE_NEXT_EVENT_FAST_MAX);
|
2020-01-29 13:12:06 +08:00
|
|
|
*ctx->next_event_fast = v;
|
2019-11-12 08:34:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2020-01-31 08:31:45 +08:00
|
|
|
te_next_event_fast_set_non_nominal(tsd_t *tsd) {
|
2019-11-12 08:34:48 +08:00
|
|
|
/*
|
2020-01-29 13:12:06 +08:00
|
|
|
* Set the fast thresholds to zero when tsd is non-nominal. Use the
|
2019-11-12 08:34:48 +08:00
|
|
|
* unsafe getter as this may get called during tsd init and clean up.
|
|
|
|
*/
|
|
|
|
*tsd_thread_allocated_next_event_fastp_get_unsafe(tsd) = 0;
|
2020-01-29 13:12:06 +08:00
|
|
|
*tsd_thread_deallocated_next_event_fastp_get_unsafe(tsd) = 0;
|
2019-11-12 08:34:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For next_event. Setter also updates the fast threshold. */
|
|
|
|
JEMALLOC_ALWAYS_INLINE uint64_t
|
2020-01-31 08:31:45 +08:00
|
|
|
te_ctx_next_event_get(te_ctx_t *ctx) {
|
2020-01-29 13:12:06 +08:00
|
|
|
return *ctx->next_event;
|
2019-11-12 08:34:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2020-01-31 08:31:45 +08:00
|
|
|
te_ctx_next_event_set(tsd_t *tsd, te_ctx_t *ctx, uint64_t v) {
|
2020-01-29 13:12:06 +08:00
|
|
|
*ctx->next_event = v;
|
2020-01-31 08:31:45 +08:00
|
|
|
te_recompute_fast_threshold(tsd);
|
2019-11-12 08:34:48 +08:00
|
|
|
}
|
|
|
|
|
2019-09-04 06:04:48 +08:00
|
|
|
/*
|
|
|
|
* The function checks in debug mode whether the thread event counters are in
|
|
|
|
* a consistent state, which forms the invariants before and after each round
|
|
|
|
* of thread event handling that we can rely on and need to promise.
|
2020-03-26 00:33:52 +08:00
|
|
|
* The invariants are only temporarily violated in the middle of
|
|
|
|
* te_event_advance() if an event is triggered (the te_event_trigger() call at
|
|
|
|
* the end will restore the invariants).
|
2019-09-04 06:04:48 +08:00
|
|
|
*/
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2020-01-31 08:31:45 +08:00
|
|
|
te_assert_invariants(tsd_t *tsd) {
|
2019-09-04 06:04:48 +08:00
|
|
|
if (config_debug) {
|
2020-01-31 08:31:45 +08:00
|
|
|
te_assert_invariants_debug(tsd);
|
2019-09-04 06:04:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2020-01-31 08:31:45 +08:00
|
|
|
te_ctx_get(tsd_t *tsd, te_ctx_t *ctx, bool is_alloc) {
|
2020-01-29 13:12:06 +08:00
|
|
|
ctx->is_alloc = is_alloc;
|
|
|
|
if (is_alloc) {
|
|
|
|
ctx->current = tsd_thread_allocatedp_get(tsd);
|
|
|
|
ctx->last_event = tsd_thread_allocated_last_eventp_get(tsd);
|
|
|
|
ctx->next_event = tsd_thread_allocated_next_eventp_get(tsd);
|
|
|
|
ctx->next_event_fast =
|
|
|
|
tsd_thread_allocated_next_event_fastp_get(tsd);
|
|
|
|
} else {
|
|
|
|
ctx->current = tsd_thread_deallocatedp_get(tsd);
|
|
|
|
ctx->last_event = tsd_thread_deallocated_last_eventp_get(tsd);
|
|
|
|
ctx->next_event = tsd_thread_deallocated_next_eventp_get(tsd);
|
|
|
|
ctx->next_event_fast =
|
|
|
|
tsd_thread_deallocated_next_event_fastp_get(tsd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 06:49:15 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
|
|
|
te_prof_sample_event_lookahead(tsd_t *tsd, size_t usize) {
|
2020-03-11 05:21:05 +08:00
|
|
|
assert(usize == sz_s2u(usize));
|
2020-03-10 06:49:15 +08:00
|
|
|
return tsd_thread_allocated_get(tsd) + usize -
|
|
|
|
tsd_thread_allocated_last_event_get(tsd) >=
|
|
|
|
tsd_prof_sample_event_wait_get(tsd);
|
|
|
|
}
|
|
|
|
|
2020-01-29 13:12:06 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2020-01-31 08:31:45 +08:00
|
|
|
te_event_advance(tsd_t *tsd, size_t usize, bool is_alloc) {
|
|
|
|
te_assert_invariants(tsd);
|
2019-09-04 06:04:48 +08:00
|
|
|
|
2020-01-31 08:31:45 +08:00
|
|
|
te_ctx_t ctx;
|
|
|
|
te_ctx_get(tsd, &ctx, is_alloc);
|
2020-01-29 13:12:06 +08:00
|
|
|
|
2020-01-31 08:31:45 +08:00
|
|
|
uint64_t bytes_before = te_ctx_current_bytes_get(&ctx);
|
|
|
|
te_ctx_current_bytes_set(&ctx, bytes_before + usize);
|
2019-09-04 06:04:48 +08:00
|
|
|
|
|
|
|
/* The subtraction is intentionally susceptible to underflow. */
|
2020-01-31 08:31:45 +08:00
|
|
|
if (likely(usize < te_ctx_next_event_get(&ctx) - bytes_before)) {
|
|
|
|
te_assert_invariants(tsd);
|
2019-09-04 06:04:48 +08:00
|
|
|
} else {
|
2020-03-26 00:33:52 +08:00
|
|
|
te_event_trigger(tsd, &ctx);
|
2019-09-04 06:04:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 13:12:06 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
|
|
thread_dalloc_event(tsd_t *tsd, size_t usize) {
|
2020-01-31 08:31:45 +08:00
|
|
|
te_event_advance(tsd, usize, false);
|
2020-01-29 13:12:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
|
|
thread_alloc_event(tsd_t *tsd, size_t usize) {
|
2020-01-31 08:31:45 +08:00
|
|
|
te_event_advance(tsd, usize, true);
|
2020-01-29 13:12:06 +08:00
|
|
|
}
|
|
|
|
|
2019-09-04 06:04:48 +08:00
|
|
|
#endif /* JEMALLOC_INTERNAL_THREAD_EVENT_H */
|