Home | History | Annotate | Line # | Download | only in internal
      1  1.1  christos #ifndef JEMALLOC_INTERNAL_THREAD_EVENT_H
      2  1.1  christos #define JEMALLOC_INTERNAL_THREAD_EVENT_H
      3  1.1  christos 
      4  1.1  christos #include "jemalloc/internal/tsd.h"
      5  1.1  christos 
      6  1.1  christos /* "te" is short for "thread_event" */
      7  1.1  christos 
      8  1.1  christos /*
      9  1.1  christos  * TE_MIN_START_WAIT should not exceed the minimal allocation usize.
     10  1.1  christos  */
     11  1.1  christos #define TE_MIN_START_WAIT ((uint64_t)1U)
     12  1.1  christos #define TE_MAX_START_WAIT UINT64_MAX
     13  1.1  christos 
     14  1.1  christos /*
     15  1.1  christos  * Maximum threshold on thread_(de)allocated_next_event_fast, so that there is
     16  1.1  christos  * no need to check overflow in malloc fast path. (The allocation size in malloc
     17  1.1  christos  * fast path never exceeds SC_LOOKUP_MAXCLASS.)
     18  1.1  christos  */
     19  1.1  christos #define TE_NEXT_EVENT_FAST_MAX (UINT64_MAX - SC_LOOKUP_MAXCLASS + 1U)
     20  1.1  christos 
     21  1.1  christos /*
     22  1.1  christos  * The max interval helps make sure that malloc stays on the fast path in the
     23  1.1  christos  * common case, i.e. thread_allocated < thread_allocated_next_event_fast.  When
     24  1.1  christos  * thread_allocated is within an event's distance to TE_NEXT_EVENT_FAST_MAX
     25  1.1  christos  * above, thread_allocated_next_event_fast is wrapped around and we fall back to
     26  1.1  christos  * the medium-fast path. The max interval makes sure that we're not staying on
     27  1.1  christos  * the fallback case for too long, even if there's no active event or if all
     28  1.1  christos  * active events have long wait times.
     29  1.1  christos  */
     30  1.1  christos #define TE_MAX_INTERVAL ((uint64_t)(4U << 20))
     31  1.1  christos 
     32  1.1  christos /*
     33  1.1  christos  * Invalid elapsed time, for situations where elapsed time is not needed.  See
     34  1.1  christos  * comments in thread_event.c for more info.
     35  1.1  christos  */
     36  1.1  christos #define TE_INVALID_ELAPSED UINT64_MAX
     37  1.1  christos 
     38  1.1  christos typedef struct te_ctx_s {
     39  1.1  christos 	bool is_alloc;
     40  1.1  christos 	uint64_t *current;
     41  1.1  christos 	uint64_t *last_event;
     42  1.1  christos 	uint64_t *next_event;
     43  1.1  christos 	uint64_t *next_event_fast;
     44  1.1  christos } te_ctx_t;
     45  1.1  christos 
     46  1.1  christos void te_assert_invariants_debug(tsd_t *tsd);
     47  1.1  christos void te_event_trigger(tsd_t *tsd, te_ctx_t *ctx);
     48  1.1  christos void te_recompute_fast_threshold(tsd_t *tsd);
     49  1.1  christos void tsd_te_init(tsd_t *tsd);
     50  1.1  christos 
     51  1.1  christos /*
     52  1.1  christos  * List of all events, in the following format:
     53  1.1  christos  *  E(event,		(condition), is_alloc_event)
     54  1.1  christos  */
     55  1.1  christos #define ITERATE_OVER_ALL_EVENTS						\
     56  1.1  christos     E(tcache_gc,		(opt_tcache_gc_incr_bytes > 0), true)	\
     57  1.1  christos     E(prof_sample,		(config_prof && opt_prof), true)  	\
     58  1.1  christos     E(stats_interval,		(opt_stats_interval >= 0), true)   	\
     59  1.1  christos     E(tcache_gc_dalloc,		(opt_tcache_gc_incr_bytes > 0), false)	\
     60  1.1  christos     E(peak_alloc,		config_stats, true)			\
     61  1.1  christos     E(peak_dalloc,		config_stats, false)
     62  1.1  christos 
     63  1.1  christos #define E(event, condition_unused, is_alloc_event_unused)		\
     64  1.1  christos     C(event##_event_wait)
     65  1.1  christos 
     66  1.1  christos /* List of all thread event counters. */
     67  1.1  christos #define ITERATE_OVER_ALL_COUNTERS					\
     68  1.1  christos     C(thread_allocated)							\
     69  1.1  christos     C(thread_allocated_last_event)					\
     70  1.1  christos     ITERATE_OVER_ALL_EVENTS						\
     71  1.1  christos     C(prof_sample_last_event)						\
     72  1.1  christos     C(stats_interval_last_event)
     73  1.1  christos 
     74  1.1  christos /* Getters directly wrap TSD getters. */
     75  1.1  christos #define C(counter)							\
     76  1.1  christos JEMALLOC_ALWAYS_INLINE uint64_t						\
     77  1.1  christos counter##_get(tsd_t *tsd) {						\
     78  1.1  christos 	return tsd_##counter##_get(tsd);				\
     79  1.1  christos }
     80  1.1  christos 
     81  1.1  christos ITERATE_OVER_ALL_COUNTERS
     82  1.1  christos #undef C
     83  1.1  christos 
     84  1.1  christos /*
     85  1.1  christos  * Setters call the TSD pointer getters rather than the TSD setters, so that
     86  1.1  christos  * the counters can be modified even when TSD state is reincarnated or
     87  1.1  christos  * minimal_initialized: if an event is triggered in such cases, we will
     88  1.1  christos  * temporarily delay the event and let it be immediately triggered at the next
     89  1.1  christos  * allocation call.
     90  1.1  christos  */
     91  1.1  christos #define C(counter)							\
     92  1.1  christos JEMALLOC_ALWAYS_INLINE void						\
     93  1.1  christos counter##_set(tsd_t *tsd, uint64_t v) {					\
     94  1.1  christos 	*tsd_##counter##p_get(tsd) = v;					\
     95  1.1  christos }
     96  1.1  christos 
     97  1.1  christos ITERATE_OVER_ALL_COUNTERS
     98  1.1  christos #undef C
     99  1.1  christos 
    100  1.1  christos /*
    101  1.1  christos  * For generating _event_wait getter / setter functions for each individual
    102  1.1  christos  * event.
    103  1.1  christos  */
    104  1.1  christos #undef E
    105  1.1  christos 
    106  1.1  christos /*
    107  1.1  christos  * The malloc and free fastpath getters -- use the unsafe getters since tsd may
    108  1.1  christos  * be non-nominal, in which case the fast_threshold will be set to 0.  This
    109  1.1  christos  * allows checking for events and tsd non-nominal in a single branch.
    110  1.1  christos  *
    111  1.1  christos  * Note that these can only be used on the fastpath.
    112  1.1  christos  */
    113  1.1  christos JEMALLOC_ALWAYS_INLINE void
    114  1.1  christos te_malloc_fastpath_ctx(tsd_t *tsd, uint64_t *allocated, uint64_t *threshold) {
    115  1.1  christos 	*allocated = *tsd_thread_allocatedp_get_unsafe(tsd);
    116  1.1  christos 	*threshold = *tsd_thread_allocated_next_event_fastp_get_unsafe(tsd);
    117  1.1  christos 	assert(*threshold <= TE_NEXT_EVENT_FAST_MAX);
    118  1.1  christos }
    119  1.1  christos 
    120  1.1  christos JEMALLOC_ALWAYS_INLINE void
    121  1.1  christos te_free_fastpath_ctx(tsd_t *tsd, uint64_t *deallocated, uint64_t *threshold) {
    122  1.1  christos 	/* Unsafe getters since this may happen before tsd_init. */
    123  1.1  christos 	*deallocated = *tsd_thread_deallocatedp_get_unsafe(tsd);
    124  1.1  christos 	*threshold = *tsd_thread_deallocated_next_event_fastp_get_unsafe(tsd);
    125  1.1  christos 	assert(*threshold <= TE_NEXT_EVENT_FAST_MAX);
    126  1.1  christos }
    127  1.1  christos 
    128  1.1  christos JEMALLOC_ALWAYS_INLINE bool
    129  1.1  christos te_ctx_is_alloc(te_ctx_t *ctx) {
    130  1.1  christos 	return ctx->is_alloc;
    131  1.1  christos }
    132  1.1  christos 
    133  1.1  christos JEMALLOC_ALWAYS_INLINE uint64_t
    134  1.1  christos te_ctx_current_bytes_get(te_ctx_t *ctx) {
    135  1.1  christos 	return *ctx->current;
    136  1.1  christos }
    137  1.1  christos 
    138  1.1  christos JEMALLOC_ALWAYS_INLINE void
    139  1.1  christos te_ctx_current_bytes_set(te_ctx_t *ctx, uint64_t v) {
    140  1.1  christos 	*ctx->current = v;
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos JEMALLOC_ALWAYS_INLINE uint64_t
    144  1.1  christos te_ctx_last_event_get(te_ctx_t *ctx) {
    145  1.1  christos 	return *ctx->last_event;
    146  1.1  christos }
    147  1.1  christos 
    148  1.1  christos JEMALLOC_ALWAYS_INLINE void
    149  1.1  christos te_ctx_last_event_set(te_ctx_t *ctx, uint64_t v) {
    150  1.1  christos 	*ctx->last_event = v;
    151  1.1  christos }
    152  1.1  christos 
    153  1.1  christos /* Below 3 for next_event_fast. */
    154  1.1  christos JEMALLOC_ALWAYS_INLINE uint64_t
    155  1.1  christos te_ctx_next_event_fast_get(te_ctx_t *ctx) {
    156  1.1  christos 	uint64_t v = *ctx->next_event_fast;
    157  1.1  christos 	assert(v <= TE_NEXT_EVENT_FAST_MAX);
    158  1.1  christos 	return v;
    159  1.1  christos }
    160  1.1  christos 
    161  1.1  christos JEMALLOC_ALWAYS_INLINE void
    162  1.1  christos te_ctx_next_event_fast_set(te_ctx_t *ctx, uint64_t v) {
    163  1.1  christos 	assert(v <= TE_NEXT_EVENT_FAST_MAX);
    164  1.1  christos 	*ctx->next_event_fast = v;
    165  1.1  christos }
    166  1.1  christos 
    167  1.1  christos JEMALLOC_ALWAYS_INLINE void
    168  1.1  christos te_next_event_fast_set_non_nominal(tsd_t *tsd) {
    169  1.1  christos 	/*
    170  1.1  christos 	 * Set the fast thresholds to zero when tsd is non-nominal.  Use the
    171  1.1  christos 	 * unsafe getter as this may get called during tsd init and clean up.
    172  1.1  christos 	 */
    173  1.1  christos 	*tsd_thread_allocated_next_event_fastp_get_unsafe(tsd) = 0;
    174  1.1  christos 	*tsd_thread_deallocated_next_event_fastp_get_unsafe(tsd) = 0;
    175  1.1  christos }
    176  1.1  christos 
    177  1.1  christos /* For next_event.  Setter also updates the fast threshold. */
    178  1.1  christos JEMALLOC_ALWAYS_INLINE uint64_t
    179  1.1  christos te_ctx_next_event_get(te_ctx_t *ctx) {
    180  1.1  christos 	return *ctx->next_event;
    181  1.1  christos }
    182  1.1  christos 
    183  1.1  christos JEMALLOC_ALWAYS_INLINE void
    184  1.1  christos te_ctx_next_event_set(tsd_t *tsd, te_ctx_t *ctx, uint64_t v) {
    185  1.1  christos 	*ctx->next_event = v;
    186  1.1  christos 	te_recompute_fast_threshold(tsd);
    187  1.1  christos }
    188  1.1  christos 
    189  1.1  christos /*
    190  1.1  christos  * The function checks in debug mode whether the thread event counters are in
    191  1.1  christos  * a consistent state, which forms the invariants before and after each round
    192  1.1  christos  * of thread event handling that we can rely on and need to promise.
    193  1.1  christos  * The invariants are only temporarily violated in the middle of
    194  1.1  christos  * te_event_advance() if an event is triggered (the te_event_trigger() call at
    195  1.1  christos  * the end will restore the invariants).
    196  1.1  christos  */
    197  1.1  christos JEMALLOC_ALWAYS_INLINE void
    198  1.1  christos te_assert_invariants(tsd_t *tsd) {
    199  1.1  christos 	if (config_debug) {
    200  1.1  christos 		te_assert_invariants_debug(tsd);
    201  1.1  christos 	}
    202  1.1  christos }
    203  1.1  christos 
    204  1.1  christos JEMALLOC_ALWAYS_INLINE void
    205  1.1  christos te_ctx_get(tsd_t *tsd, te_ctx_t *ctx, bool is_alloc) {
    206  1.1  christos 	ctx->is_alloc = is_alloc;
    207  1.1  christos 	if (is_alloc) {
    208  1.1  christos 		ctx->current = tsd_thread_allocatedp_get(tsd);
    209  1.1  christos 		ctx->last_event = tsd_thread_allocated_last_eventp_get(tsd);
    210  1.1  christos 		ctx->next_event = tsd_thread_allocated_next_eventp_get(tsd);
    211  1.1  christos 		ctx->next_event_fast =
    212  1.1  christos 		    tsd_thread_allocated_next_event_fastp_get(tsd);
    213  1.1  christos 	} else {
    214  1.1  christos 		ctx->current = tsd_thread_deallocatedp_get(tsd);
    215  1.1  christos 		ctx->last_event = tsd_thread_deallocated_last_eventp_get(tsd);
    216  1.1  christos 		ctx->next_event = tsd_thread_deallocated_next_eventp_get(tsd);
    217  1.1  christos 		ctx->next_event_fast =
    218  1.1  christos 		    tsd_thread_deallocated_next_event_fastp_get(tsd);
    219  1.1  christos 	}
    220  1.1  christos }
    221  1.1  christos 
    222  1.1  christos /*
    223  1.1  christos  * The lookahead functionality facilitates events to be able to lookahead, i.e.
    224  1.1  christos  * without touching the event counters, to determine whether an event would be
    225  1.1  christos  * triggered.  The event counters are not advanced until the end of the
    226  1.1  christos  * allocation / deallocation calls, so the lookahead can be useful if some
    227  1.1  christos  * preparation work for some event must be done early in the allocation /
    228  1.1  christos  * deallocation calls.
    229  1.1  christos  *
    230  1.1  christos  * Currently only the profiling sampling event needs the lookahead
    231  1.1  christos  * functionality, so we don't yet define general purpose lookahead functions.
    232  1.1  christos  *
    233  1.1  christos  * Surplus is a terminology referring to the amount of bytes beyond what's
    234  1.1  christos  * needed for triggering an event, which can be a useful quantity to have in
    235  1.1  christos  * general when lookahead is being called.
    236  1.1  christos  */
    237  1.1  christos 
    238  1.1  christos JEMALLOC_ALWAYS_INLINE bool
    239  1.1  christos te_prof_sample_event_lookahead_surplus(tsd_t *tsd, size_t usize,
    240  1.1  christos     size_t *surplus) {
    241  1.1  christos 	if (surplus != NULL) {
    242  1.1  christos 		/*
    243  1.1  christos 		 * This is a dead store: the surplus will be overwritten before
    244  1.1  christos 		 * any read.  The initialization suppresses compiler warnings.
    245  1.1  christos 		 * Meanwhile, using SIZE_MAX to initialize is good for
    246  1.1  christos 		 * debugging purpose, because a valid surplus value is strictly
    247  1.1  christos 		 * less than usize, which is at most SIZE_MAX.
    248  1.1  christos 		 */
    249  1.1  christos 		*surplus = SIZE_MAX;
    250  1.1  christos 	}
    251  1.1  christos 	if (unlikely(!tsd_nominal(tsd) || tsd_reentrancy_level_get(tsd) > 0)) {
    252  1.1  christos 		return false;
    253  1.1  christos 	}
    254  1.1  christos 	/* The subtraction is intentionally susceptible to underflow. */
    255  1.1  christos 	uint64_t accumbytes = tsd_thread_allocated_get(tsd) + usize -
    256  1.1  christos 	    tsd_thread_allocated_last_event_get(tsd);
    257  1.1  christos 	uint64_t sample_wait = tsd_prof_sample_event_wait_get(tsd);
    258  1.1  christos 	if (accumbytes < sample_wait) {
    259  1.1  christos 		return false;
    260  1.1  christos 	}
    261  1.1  christos 	assert(accumbytes - sample_wait < (uint64_t)usize);
    262  1.1  christos 	if (surplus != NULL) {
    263  1.1  christos 		*surplus = (size_t)(accumbytes - sample_wait);
    264  1.1  christos 	}
    265  1.1  christos 	return true;
    266  1.1  christos }
    267  1.1  christos 
    268  1.1  christos JEMALLOC_ALWAYS_INLINE bool
    269  1.1  christos te_prof_sample_event_lookahead(tsd_t *tsd, size_t usize) {
    270  1.1  christos 	return te_prof_sample_event_lookahead_surplus(tsd, usize, NULL);
    271  1.1  christos }
    272  1.1  christos 
    273  1.1  christos JEMALLOC_ALWAYS_INLINE void
    274  1.1  christos te_event_advance(tsd_t *tsd, size_t usize, bool is_alloc) {
    275  1.1  christos 	te_assert_invariants(tsd);
    276  1.1  christos 
    277  1.1  christos 	te_ctx_t ctx;
    278  1.1  christos 	te_ctx_get(tsd, &ctx, is_alloc);
    279  1.1  christos 
    280  1.1  christos 	uint64_t bytes_before = te_ctx_current_bytes_get(&ctx);
    281  1.1  christos 	te_ctx_current_bytes_set(&ctx, bytes_before + usize);
    282  1.1  christos 
    283  1.1  christos 	/* The subtraction is intentionally susceptible to underflow. */
    284  1.1  christos 	if (likely(usize < te_ctx_next_event_get(&ctx) - bytes_before)) {
    285  1.1  christos 		te_assert_invariants(tsd);
    286  1.1  christos 	} else {
    287  1.1  christos 		te_event_trigger(tsd, &ctx);
    288  1.1  christos 	}
    289  1.1  christos }
    290  1.1  christos 
    291  1.1  christos JEMALLOC_ALWAYS_INLINE void
    292  1.1  christos thread_dalloc_event(tsd_t *tsd, size_t usize) {
    293  1.1  christos 	te_event_advance(tsd, usize, false);
    294  1.1  christos }
    295  1.1  christos 
    296  1.1  christos JEMALLOC_ALWAYS_INLINE void
    297  1.1  christos thread_alloc_event(tsd_t *tsd, size_t usize) {
    298  1.1  christos 	te_event_advance(tsd, usize, true);
    299  1.1  christos }
    300  1.1  christos 
    301  1.1  christos #endif /* JEMALLOC_INTERNAL_THREAD_EVENT_H */
    302