Home | History | Annotate | Line # | Download | only in internal
      1  1.1  christos #ifdef JEMALLOC_INTERNAL_TSD_INTERNALS_H
      2  1.1  christos #error This file should be included only once, by one of tsd_malloc_thread_cleanup.h, tsd_tls.h, tsd_generic.h, or tsd_win.h
      3  1.1  christos #endif
      4  1.1  christos #define JEMALLOC_INTERNAL_TSD_INTERNALS_H
      5  1.1  christos 
      6  1.1  christos #include "jemalloc/internal/jemalloc_preamble.h"
      7  1.1  christos #include "jemalloc/internal/arena_types.h"
      8  1.1  christos #include "jemalloc/internal/assert.h"
      9  1.1  christos #include "jemalloc/internal/bin_types.h"
     10  1.1  christos #include "jemalloc/internal/jemalloc_internal_externs.h"
     11  1.1  christos #include "jemalloc/internal/peak.h"
     12  1.1  christos #include "jemalloc/internal/prof_types.h"
     13  1.1  christos #include "jemalloc/internal/ql.h"
     14  1.1  christos #include "jemalloc/internal/rtree_tsd.h"
     15  1.1  christos #include "jemalloc/internal/tcache_structs.h"
     16  1.1  christos #include "jemalloc/internal/tcache_types.h"
     17  1.1  christos #include "jemalloc/internal/thread_event_registry.h"
     18  1.1  christos #include "jemalloc/internal/tsd_types.h"
     19  1.1  christos #include "jemalloc/internal/util.h"
     20  1.1  christos #include "jemalloc/internal/witness.h"
     21  1.1  christos 
     22  1.1  christos /*
     23  1.1  christos  * Thread-Specific-Data layout
     24  1.1  christos  *
     25  1.1  christos  * At least some thread-local data gets touched on the fast-path of almost all
     26  1.1  christos  * malloc operations.  But much of it is only necessary down slow-paths, or
     27  1.1  christos  * testing.  We want to colocate the fast-path data so that it can live on the
     28  1.1  christos  * same cacheline if possible.  So we define three tiers of hotness:
     29  1.1  christos  * TSD_DATA_FAST: Touched on the alloc/dalloc fast paths.
     30  1.1  christos  * TSD_DATA_SLOW: Touched down slow paths.  "Slow" here is sort of general;
     31  1.1  christos  *     there are "semi-slow" paths like "not a sized deallocation, but can still
     32  1.1  christos  *     live in the tcache".  We'll want to keep these closer to the fast-path
     33  1.1  christos  *     data.
     34  1.1  christos  * TSD_DATA_SLOWER: Only touched in test or debug modes, or not touched at all.
     35  1.1  christos  *
     36  1.1  christos  * An additional concern is that the larger tcache bins won't be used (we have a
     37  1.1  christos  * bin per size class, but by default only cache relatively small objects).  So
     38  1.1  christos  * the earlier bins are in the TSD_DATA_FAST tier, but the later ones are in the
     39  1.1  christos  * TSD_DATA_SLOWER tier.
     40  1.1  christos  *
     41  1.1  christos  * As a result of all this, we put the slow data first, then the fast data, then
     42  1.1  christos  * the slower data, while keeping the tcache as the last element of the fast
     43  1.1  christos  * data (so that the fast -> slower transition happens midway through the
     44  1.1  christos  * tcache).  While we don't yet play alignment tricks to guarantee it, this
     45  1.1  christos  * increases our odds of getting some cache/page locality on fast paths.
     46  1.1  christos  */
     47  1.1  christos 
     48  1.1  christos #ifdef JEMALLOC_JET
     49  1.1  christos typedef void (*test_callback_t)(int *);
     50  1.1  christos #	define MALLOC_TSD_TEST_DATA_INIT 0x72b65c10
     51  1.1  christos #	define MALLOC_TEST_TSD                                                \
     52  1.1  christos 		O(test_data, int, int)                                         \
     53  1.1  christos 		O(test_callback, test_callback_t, int)
     54  1.1  christos #	define MALLOC_TEST_TSD_INITIALIZER , MALLOC_TSD_TEST_DATA_INIT, NULL
     55  1.1  christos #else
     56  1.1  christos #	define MALLOC_TEST_TSD
     57  1.1  christos #	define MALLOC_TEST_TSD_INITIALIZER
     58  1.1  christos #endif
     59  1.1  christos 
     60  1.1  christos typedef ql_elm(tsd_t) tsd_link_t;
     61  1.1  christos 
     62  1.1  christos /*  O(name,			type,			nullable type) */
     63  1.1  christos #define TSD_DATA_SLOW                                                          \
     64  1.1  christos 	O(tcache_enabled, bool, bool)                                          \
     65  1.1  christos 	O(reentrancy_level, int8_t, int8_t)                                    \
     66  1.1  christos 	O(min_init_state_nfetched, uint8_t, uint8_t)                           \
     67  1.1  christos 	O(thread_allocated_last_event, uint64_t, uint64_t)                     \
     68  1.1  christos 	O(thread_allocated_next_event, uint64_t, uint64_t)                     \
     69  1.1  christos 	O(thread_deallocated_last_event, uint64_t, uint64_t)                   \
     70  1.1  christos 	O(thread_deallocated_next_event, uint64_t, uint64_t)                   \
     71  1.1  christos 	O(te_data, te_data_t, te_data_t)                                       \
     72  1.1  christos 	O(prof_sample_last_event, uint64_t, uint64_t)                          \
     73  1.1  christos 	O(stats_interval_last_event, uint64_t, uint64_t)                       \
     74  1.1  christos 	O(prof_tdata, prof_tdata_t *, prof_tdata_t *)                          \
     75  1.1  christos 	O(prng_state, uint64_t, uint64_t)                                      \
     76  1.1  christos 	O(san_extents_until_guard_small, uint64_t, uint64_t)                   \
     77  1.1  christos 	O(san_extents_until_guard_large, uint64_t, uint64_t)                   \
     78  1.1  christos 	O(iarena, arena_t *, arena_t *)                                        \
     79  1.1  christos 	O(arena, arena_t *, arena_t *)                                         \
     80  1.1  christos 	O(arena_decay_ticker, ticker_geom_t, ticker_geom_t)                    \
     81  1.1  christos 	O(sec_shard, uint8_t, uint8_t)                                         \
     82  1.1  christos 	O(binshards, tsd_binshards_t, tsd_binshards_t)                         \
     83  1.1  christos 	O(tsd_link, tsd_link_t, tsd_link_t)                                    \
     84  1.1  christos 	O(in_hook, bool, bool)                                                 \
     85  1.1  christos 	O(peak, peak_t, peak_t)                                                \
     86  1.1  christos 	O(tcache_slow, tcache_slow_t, tcache_slow_t)                           \
     87  1.1  christos 	O(rtree_ctx, rtree_ctx_t, rtree_ctx_t)
     88  1.1  christos 
     89  1.1  christos #define TSD_DATA_SLOW_INITIALIZER                                              \
     90  1.1  christos 	/* tcache_enabled */ TCACHE_ENABLED_ZERO_INITIALIZER,                  \
     91  1.1  christos 	    /* reentrancy_level */ 0, /* min_init_state_nfetched */ 0,         \
     92  1.1  christos 	    /* thread_allocated_last_event */ 0,                               \
     93  1.1  christos 	    /* thread_allocated_next_event */ 0,                               \
     94  1.1  christos 	    /* thread_deallocated_last_event */ 0,                             \
     95  1.1  christos 	    /* thread_deallocated_next_event */ 0,                             \
     96  1.1  christos 	    /* te_data */ TE_DATA_INITIALIZER, /* prof_sample_last_event */ 0, \
     97  1.1  christos 	    /* stats_interval_last_event */ 0, /* prof_tdata */ NULL,          \
     98  1.1  christos 	    /* prng_state */ 0, /* san_extents_until_guard_small */ 0,         \
     99  1.1  christos 	    /* san_extents_until_guard_large */ 0, /* iarena */ NULL,          \
    100  1.1  christos 	    /* arena */ NULL, /* arena_decay_ticker */                         \
    101  1.1  christos 	    TICKER_GEOM_INIT(ARENA_DECAY_NTICKS_PER_UPDATE),                   \
    102  1.1  christos 	    /* sec_shard */ (uint8_t) - 1,                                     \
    103  1.1  christos 	    /* binshards */ TSD_BINSHARDS_ZERO_INITIALIZER,                    \
    104  1.1  christos 	    /* tsd_link */ {NULL}, /* in_hook */ false,                        \
    105  1.1  christos 	    /* peak */ PEAK_INITIALIZER,                                       \
    106  1.1  christos 	    /* tcache_slow */ TCACHE_SLOW_ZERO_INITIALIZER,                    \
    107  1.1  christos 	    /* rtree_ctx */ RTREE_CTX_INITIALIZER,
    108  1.1  christos 
    109  1.1  christos /*  O(name,			type,			nullable type) */
    110  1.1  christos #define TSD_DATA_FAST                                                          \
    111  1.1  christos 	O(thread_allocated, uint64_t, uint64_t)                                \
    112  1.1  christos 	O(thread_allocated_next_event_fast, uint64_t, uint64_t)                \
    113  1.1  christos 	O(thread_deallocated, uint64_t, uint64_t)                              \
    114  1.1  christos 	O(thread_deallocated_next_event_fast, uint64_t, uint64_t)              \
    115  1.1  christos 	O(tcache, tcache_t, tcache_t)
    116  1.1  christos 
    117  1.1  christos #define TSD_DATA_FAST_INITIALIZER                                              \
    118  1.1  christos 	/* thread_allocated */ 0, /* thread_allocated_next_event_fast */ 0,    \
    119  1.1  christos 	    /* thread_deallocated */ 0,                                        \
    120  1.1  christos 	    /* thread_deallocated_next_event_fast */ 0,                        \
    121  1.1  christos 	    /* tcache */ TCACHE_ZERO_INITIALIZER,
    122  1.1  christos 
    123  1.1  christos /*  O(name,			type,			nullable type) */
    124  1.1  christos #define TSD_DATA_SLOWER                                                        \
    125  1.1  christos 	O(witness_tsd, witness_tsd_t, witness_tsdn_t)                          \
    126  1.1  christos 	MALLOC_TEST_TSD
    127  1.1  christos 
    128  1.1  christos #define TSD_DATA_SLOWER_INITIALIZER                                            \
    129  1.1  christos 	/* witness */ WITNESS_TSD_INITIALIZER                                  \
    130  1.1  christos 	/* test data */ MALLOC_TEST_TSD_INITIALIZER
    131  1.1  christos 
    132  1.1  christos #define TSD_INITIALIZER                                                        \
    133  1.1  christos 	{                                                                      \
    134  1.1  christos 		TSD_DATA_SLOW_INITIALIZER                                      \
    135  1.1  christos 		/* state */ ATOMIC_INIT(tsd_state_uninitialized),              \
    136  1.1  christos 		    TSD_DATA_FAST_INITIALIZER TSD_DATA_SLOWER_INITIALIZER      \
    137  1.1  christos 	}
    138  1.1  christos 
    139  1.1  christos #if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32)
    140  1.1  christos void _malloc_tsd_cleanup_register(bool (*f)(void));
    141  1.1  christos #endif
    142  1.1  christos 
    143  1.1  christos void  *malloc_tsd_malloc(size_t size);
    144  1.1  christos void   malloc_tsd_dalloc(void *wrapper);
    145  1.1  christos tsd_t *malloc_tsd_boot0(void);
    146  1.1  christos void   malloc_tsd_boot1(void);
    147  1.1  christos void   tsd_cleanup(void *arg);
    148  1.1  christos tsd_t *tsd_fetch_slow(tsd_t *tsd, bool minimal);
    149  1.1  christos void   tsd_state_set(tsd_t *tsd, uint8_t new_state);
    150  1.1  christos void   tsd_slow_update(tsd_t *tsd);
    151  1.1  christos void   tsd_prefork(tsd_t *tsd);
    152  1.1  christos void   tsd_postfork_parent(tsd_t *tsd);
    153  1.1  christos void   tsd_postfork_child(tsd_t *tsd);
    154  1.1  christos 
    155  1.1  christos /*
    156  1.1  christos  * Call ..._inc when your module wants to take all threads down the slow paths,
    157  1.1  christos  * and ..._dec when it no longer needs to.
    158  1.1  christos  */
    159  1.1  christos void tsd_global_slow_inc(tsdn_t *tsdn);
    160  1.1  christos void tsd_global_slow_dec(tsdn_t *tsdn);
    161  1.1  christos bool tsd_global_slow(void);
    162  1.1  christos 
    163  1.1  christos #define TSD_MIN_INIT_STATE_MAX_FETCHED (128)
    164  1.1  christos 
    165  1.1  christos enum {
    166  1.1  christos 	/* Common case --> jnz. */
    167  1.1  christos 	tsd_state_nominal = 0,
    168  1.1  christos 	/* Initialized but on slow path. */
    169  1.1  christos 	tsd_state_nominal_slow = 1,
    170  1.1  christos 	/*
    171  1.1  christos 	 * Some thread has changed global state in such a way that all nominal
    172  1.1  christos 	 * threads need to recompute their fast / slow status the next time they
    173  1.1  christos 	 * get a chance.
    174  1.1  christos 	 *
    175  1.1  christos 	 * Any thread can change another thread's status *to* recompute, but
    176  1.1  christos 	 * threads are the only ones who can change their status *from*
    177  1.1  christos 	 * recompute.
    178  1.1  christos 	 */
    179  1.1  christos 	tsd_state_nominal_recompute = 2,
    180  1.1  christos 	/*
    181  1.1  christos 	 * The above nominal states should be lower values.  We use
    182  1.1  christos 	 * tsd_nominal_max to separate nominal states from threads in the
    183  1.1  christos 	 * process of being born / dying.
    184  1.1  christos 	 */
    185  1.1  christos 	tsd_state_nominal_max = 2,
    186  1.1  christos 
    187  1.1  christos 	/*
    188  1.1  christos 	 * A thread might free() during its death as its only allocator action;
    189  1.1  christos 	 * in such scenarios, we need tsd, but set up in such a way that no
    190  1.1  christos 	 * cleanup is necessary.
    191  1.1  christos 	 */
    192  1.1  christos 	tsd_state_minimal_initialized = 3,
    193  1.1  christos 	/* States during which we know we're in thread death. */
    194  1.1  christos 	tsd_state_purgatory = 4,
    195  1.1  christos 	tsd_state_reincarnated = 5,
    196  1.1  christos 	/*
    197  1.1  christos 	 * What it says on the tin; tsd that hasn't been initialized.  Note
    198  1.1  christos 	 * that even when the tsd struct lives in TLS, when need to keep track
    199  1.1  christos 	 * of stuff like whether or not our pthread destructors have been
    200  1.1  christos 	 * scheduled, so this really truly is different than the nominal state.
    201  1.1  christos 	 */
    202  1.1  christos 	tsd_state_uninitialized = 6
    203  1.1  christos };
    204  1.1  christos 
    205  1.1  christos /*
    206  1.1  christos  * Some TSD accesses can only be done in a nominal state.  To enforce this, we
    207  1.1  christos  * wrap TSD member access in a function that asserts on TSD state, and mangle
    208  1.1  christos  * field names to prevent touching them accidentally.
    209  1.1  christos  */
    210  1.1  christos #define TSD_MANGLE(n) cant_access_tsd_items_directly_use_a_getter_or_setter_##n
    211  1.1  christos 
    212  1.1  christos #ifdef JEMALLOC_U8_ATOMICS
    213  1.1  christos #	define tsd_state_t atomic_u8_t
    214  1.1  christos #	define tsd_atomic_load atomic_load_u8
    215  1.1  christos #	define tsd_atomic_store atomic_store_u8
    216  1.1  christos #	define tsd_atomic_exchange atomic_exchange_u8
    217  1.1  christos #else
    218  1.1  christos #	define tsd_state_t atomic_u32_t
    219  1.1  christos #	define tsd_atomic_load atomic_load_u32
    220  1.1  christos #	define tsd_atomic_store atomic_store_u32
    221  1.1  christos #	define tsd_atomic_exchange atomic_exchange_u32
    222  1.1  christos #endif
    223  1.1  christos 
    224  1.1  christos /* The actual tsd. */
    225  1.1  christos struct tsd_s {
    226  1.1  christos 	/*
    227  1.1  christos 	 * The contents should be treated as totally opaque outside the tsd
    228  1.1  christos 	 * module.  Access any thread-local state through the getters and
    229  1.1  christos 	 * setters below.
    230  1.1  christos 	 */
    231  1.1  christos 
    232  1.1  christos #define O(n, t, nt) t TSD_MANGLE(n);
    233  1.1  christos 
    234  1.1  christos 	TSD_DATA_SLOW
    235  1.1  christos 	/*
    236  1.1  christos 	 * We manually limit the state to just a single byte.  Unless the 8-bit
    237  1.1  christos 	 * atomics are unavailable (which is rare).
    238  1.1  christos 	 */
    239  1.1  christos 	tsd_state_t state;
    240  1.1  christos 	TSD_DATA_FAST
    241  1.1  christos 	TSD_DATA_SLOWER
    242  1.1  christos #undef O
    243  1.1  christos };
    244  1.1  christos 
    245  1.1  christos JEMALLOC_ALWAYS_INLINE uint8_t
    246  1.1  christos tsd_state_get(tsd_t *tsd) {
    247  1.1  christos 	/*
    248  1.1  christos 	 * This should be atomic.  Unfortunately, compilers right now can't tell
    249  1.1  christos 	 * that this can be done as a memory comparison, and forces a load into
    250  1.1  christos 	 * a register that hurts fast-path performance.
    251  1.1  christos 	 */
    252  1.1  christos 	/* return atomic_load_u8(&tsd->state, ATOMIC_RELAXED); */
    253  1.1  christos 	return *(uint8_t *)&tsd->state;
    254  1.1  christos }
    255  1.1  christos 
    256  1.1  christos /*
    257  1.1  christos  * Wrapper around tsd_t that makes it possible to avoid implicit conversion
    258  1.1  christos  * between tsd_t and tsdn_t, where tsdn_t is "nullable" and has to be
    259  1.1  christos  * explicitly converted to tsd_t, which is non-nullable.
    260  1.1  christos  */
    261  1.1  christos struct tsdn_s {
    262  1.1  christos 	tsd_t tsd;
    263  1.1  christos };
    264  1.1  christos #define TSDN_NULL ((tsdn_t *)0)
    265  1.1  christos JEMALLOC_ALWAYS_INLINE tsdn_t *
    266  1.1  christos tsd_tsdn(tsd_t *tsd) {
    267  1.1  christos 	return (tsdn_t *)tsd;
    268  1.1  christos }
    269  1.1  christos 
    270  1.1  christos JEMALLOC_ALWAYS_INLINE bool
    271  1.1  christos tsdn_null(const tsdn_t *tsdn) {
    272  1.1  christos 	return tsdn == NULL;
    273  1.1  christos }
    274  1.1  christos 
    275  1.1  christos JEMALLOC_ALWAYS_INLINE tsd_t *
    276  1.1  christos tsdn_tsd(tsdn_t *tsdn) {
    277  1.1  christos 	assert(!tsdn_null(tsdn));
    278  1.1  christos 
    279  1.1  christos 	return &tsdn->tsd;
    280  1.1  christos }
    281