tcache.h revision d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7
1/******************************************************************************/ 2#ifdef JEMALLOC_H_TYPES 3 4typedef struct tcache_bin_info_s tcache_bin_info_t; 5typedef struct tcache_bin_s tcache_bin_t; 6typedef struct tcache_s tcache_t; 7 8/* 9 * tcache pointers close to NULL are used to encode state information that is 10 * used for two purposes: preventing thread caching on a per thread basis and 11 * cleaning up during thread shutdown. 12 */ 13#define TCACHE_STATE_DISABLED ((tcache_t *)(uintptr_t)1) 14#define TCACHE_STATE_REINCARNATED ((tcache_t *)(uintptr_t)2) 15#define TCACHE_STATE_PURGATORY ((tcache_t *)(uintptr_t)3) 16#define TCACHE_STATE_MAX TCACHE_STATE_PURGATORY 17 18/* 19 * Absolute maximum number of cache slots for each small bin in the thread 20 * cache. This is an additional constraint beyond that imposed as: twice the 21 * number of regions per run for this size class. 22 * 23 * This constant must be an even number. 24 */ 25#define TCACHE_NSLOTS_SMALL_MAX 200 26 27/* Number of cache slots for large size classes. */ 28#define TCACHE_NSLOTS_LARGE 20 29 30/* (1U << opt_lg_tcache_max) is used to compute tcache_maxclass. */ 31#define LG_TCACHE_MAXCLASS_DEFAULT 15 32 33/* 34 * TCACHE_GC_SWEEP is the approximate number of allocation events between 35 * full GC sweeps. Integer rounding may cause the actual number to be 36 * slightly higher, since GC is performed incrementally. 37 */ 38#define TCACHE_GC_SWEEP 8192 39 40/* Number of tcache allocation/deallocation events between incremental GCs. */ 41#define TCACHE_GC_INCR \ 42 ((TCACHE_GC_SWEEP / NBINS) + ((TCACHE_GC_SWEEP / NBINS == 0) ? 0 : 1)) 43 44#endif /* JEMALLOC_H_TYPES */ 45/******************************************************************************/ 46#ifdef JEMALLOC_H_STRUCTS 47 48typedef enum { 49 tcache_enabled_false = 0, /* Enable cast to/from bool. */ 50 tcache_enabled_true = 1, 51 tcache_enabled_default = 2 52} tcache_enabled_t; 53 54/* 55 * Read-only information associated with each element of tcache_t's tbins array 56 * is stored separately, mainly to reduce memory usage. 57 */ 58struct tcache_bin_info_s { 59 unsigned ncached_max; /* Upper limit on ncached. */ 60}; 61 62struct tcache_bin_s { 63 tcache_bin_stats_t tstats; 64 int low_water; /* Min # cached since last GC. */ 65 unsigned lg_fill_div; /* Fill (ncached_max >> lg_fill_div). */ 66 unsigned ncached; /* # of cached objects. */ 67 void **avail; /* Stack of available objects. */ 68}; 69 70struct tcache_s { 71 ql_elm(tcache_t) link; /* Used for aggregating stats. */ 72 uint64_t prof_accumbytes;/* Cleared after arena_prof_accum() */ 73 arena_t *arena; /* This thread's arena. */ 74 unsigned ev_cnt; /* Event count since incremental GC. */ 75 unsigned next_gc_bin; /* Next bin to GC. */ 76 tcache_bin_t tbins[1]; /* Dynamically sized. */ 77 /* 78 * The pointer stacks associated with tbins follow as a contiguous 79 * array. During tcache initialization, the avail pointer in each 80 * element of tbins is initialized to point to the proper offset within 81 * this array. 82 */ 83}; 84 85#endif /* JEMALLOC_H_STRUCTS */ 86/******************************************************************************/ 87#ifdef JEMALLOC_H_EXTERNS 88 89extern bool opt_tcache; 90extern ssize_t opt_lg_tcache_max; 91 92extern tcache_bin_info_t *tcache_bin_info; 93 94/* 95 * Number of tcache bins. There are NBINS small-object bins, plus 0 or more 96 * large-object bins. 97 */ 98extern size_t nhbins; 99 100/* Maximum cached size class. */ 101extern size_t tcache_maxclass; 102 103void tcache_bin_flush_small(tcache_bin_t *tbin, size_t binind, unsigned rem, 104 tcache_t *tcache); 105void tcache_bin_flush_large(tcache_bin_t *tbin, size_t binind, unsigned rem, 106 tcache_t *tcache); 107void tcache_arena_associate(tcache_t *tcache, arena_t *arena); 108void tcache_arena_dissociate(tcache_t *tcache); 109tcache_t *tcache_create(arena_t *arena); 110void *tcache_alloc_small_hard(tcache_t *tcache, tcache_bin_t *tbin, 111 size_t binind); 112void tcache_destroy(tcache_t *tcache); 113void tcache_thread_cleanup(void *arg); 114void tcache_stats_merge(tcache_t *tcache, arena_t *arena); 115bool tcache_boot0(void); 116bool tcache_boot1(void); 117 118#endif /* JEMALLOC_H_EXTERNS */ 119/******************************************************************************/ 120#ifdef JEMALLOC_H_INLINES 121 122#ifndef JEMALLOC_ENABLE_INLINE 123malloc_tsd_protos(JEMALLOC_ATTR(unused), tcache, tcache_t *) 124malloc_tsd_protos(JEMALLOC_ATTR(unused), tcache_enabled, tcache_enabled_t) 125 126void tcache_event(tcache_t *tcache); 127void tcache_flush(void); 128bool tcache_enabled_get(void); 129tcache_t *tcache_get(void); 130void tcache_enabled_set(bool enabled); 131void *tcache_alloc_easy(tcache_bin_t *tbin); 132void *tcache_alloc_small(tcache_t *tcache, size_t size, bool zero); 133void *tcache_alloc_large(tcache_t *tcache, size_t size, bool zero); 134void tcache_dalloc_small(tcache_t *tcache, void *ptr); 135void tcache_dalloc_large(tcache_t *tcache, void *ptr, size_t size); 136#endif 137 138#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_TCACHE_C_)) 139/* Map of thread-specific caches. */ 140malloc_tsd_externs(tcache, tcache_t *) 141malloc_tsd_funcs(JEMALLOC_INLINE, tcache, tcache_t *, NULL, 142 tcache_thread_cleanup) 143/* Per thread flag that allows thread caches to be disabled. */ 144malloc_tsd_externs(tcache_enabled, tcache_enabled_t) 145malloc_tsd_funcs(JEMALLOC_INLINE, tcache_enabled, tcache_enabled_t, 146 tcache_enabled_default, malloc_tsd_no_cleanup) 147 148JEMALLOC_INLINE void 149tcache_flush(void) 150{ 151 tcache_t *tcache; 152 153 cassert(config_tcache); 154 155 tcache = *tcache_tsd_get(); 156 if ((uintptr_t)tcache <= (uintptr_t)TCACHE_STATE_MAX) 157 return; 158 tcache_destroy(tcache); 159 tcache = NULL; 160 tcache_tsd_set(&tcache); 161} 162 163JEMALLOC_INLINE bool 164tcache_enabled_get(void) 165{ 166 tcache_enabled_t tcache_enabled; 167 168 cassert(config_tcache); 169 170 tcache_enabled = *tcache_enabled_tsd_get(); 171 if (tcache_enabled == tcache_enabled_default) { 172 tcache_enabled = (tcache_enabled_t)opt_tcache; 173 tcache_enabled_tsd_set(&tcache_enabled); 174 } 175 176 return ((bool)tcache_enabled); 177} 178 179JEMALLOC_INLINE void 180tcache_enabled_set(bool enabled) 181{ 182 tcache_enabled_t tcache_enabled; 183 tcache_t *tcache; 184 185 cassert(config_tcache); 186 187 tcache_enabled = (tcache_enabled_t)enabled; 188 tcache_enabled_tsd_set(&tcache_enabled); 189 tcache = *tcache_tsd_get(); 190 if (enabled) { 191 if (tcache == TCACHE_STATE_DISABLED) { 192 tcache = NULL; 193 tcache_tsd_set(&tcache); 194 } 195 } else /* disabled */ { 196 if (tcache > TCACHE_STATE_MAX) { 197 tcache_destroy(tcache); 198 tcache = NULL; 199 } 200 if (tcache == NULL) { 201 tcache = TCACHE_STATE_DISABLED; 202 tcache_tsd_set(&tcache); 203 } 204 } 205} 206 207JEMALLOC_INLINE tcache_t * 208tcache_get(void) 209{ 210 tcache_t *tcache; 211 212 if (config_tcache == false) 213 return (NULL); 214 if (config_lazy_lock && isthreaded == false) 215 return (NULL); 216 217 tcache = *tcache_tsd_get(); 218 if ((uintptr_t)tcache <= (uintptr_t)TCACHE_STATE_MAX) { 219 if (tcache == TCACHE_STATE_DISABLED) 220 return (NULL); 221 if (tcache == NULL) { 222 if (tcache_enabled_get() == false) { 223 tcache_enabled_set(false); /* Memoize. */ 224 return (NULL); 225 } 226 return (tcache_create(choose_arena())); 227 } 228 if (tcache == TCACHE_STATE_PURGATORY) { 229 /* 230 * Make a note that an allocator function was called 231 * after tcache_thread_cleanup() was called. 232 */ 233 tcache = TCACHE_STATE_REINCARNATED; 234 tcache_tsd_set(&tcache); 235 return (NULL); 236 } 237 if (tcache == TCACHE_STATE_REINCARNATED) 238 return (NULL); 239 not_reached(); 240 } 241 242 return (tcache); 243} 244 245JEMALLOC_INLINE void 246tcache_event(tcache_t *tcache) 247{ 248 249 if (TCACHE_GC_INCR == 0) 250 return; 251 252 tcache->ev_cnt++; 253 assert(tcache->ev_cnt <= TCACHE_GC_INCR); 254 if (tcache->ev_cnt == TCACHE_GC_INCR) { 255 size_t binind = tcache->next_gc_bin; 256 tcache_bin_t *tbin = &tcache->tbins[binind]; 257 tcache_bin_info_t *tbin_info = &tcache_bin_info[binind]; 258 259 if (tbin->low_water > 0) { 260 /* 261 * Flush (ceiling) 3/4 of the objects below the low 262 * water mark. 263 */ 264 if (binind < NBINS) { 265 tcache_bin_flush_small(tbin, binind, 266 tbin->ncached - tbin->low_water + 267 (tbin->low_water >> 2), tcache); 268 } else { 269 tcache_bin_flush_large(tbin, binind, 270 tbin->ncached - tbin->low_water + 271 (tbin->low_water >> 2), tcache); 272 } 273 /* 274 * Reduce fill count by 2X. Limit lg_fill_div such that 275 * the fill count is always at least 1. 276 */ 277 if ((tbin_info->ncached_max >> (tbin->lg_fill_div+1)) 278 >= 1) 279 tbin->lg_fill_div++; 280 } else if (tbin->low_water < 0) { 281 /* 282 * Increase fill count by 2X. Make sure lg_fill_div 283 * stays greater than 0. 284 */ 285 if (tbin->lg_fill_div > 1) 286 tbin->lg_fill_div--; 287 } 288 tbin->low_water = tbin->ncached; 289 290 tcache->next_gc_bin++; 291 if (tcache->next_gc_bin == nhbins) 292 tcache->next_gc_bin = 0; 293 tcache->ev_cnt = 0; 294 } 295} 296 297JEMALLOC_INLINE void * 298tcache_alloc_easy(tcache_bin_t *tbin) 299{ 300 void *ret; 301 302 if (tbin->ncached == 0) { 303 tbin->low_water = -1; 304 return (NULL); 305 } 306 tbin->ncached--; 307 if ((int)tbin->ncached < tbin->low_water) 308 tbin->low_water = tbin->ncached; 309 ret = tbin->avail[tbin->ncached]; 310 return (ret); 311} 312 313JEMALLOC_INLINE void * 314tcache_alloc_small(tcache_t *tcache, size_t size, bool zero) 315{ 316 void *ret; 317 size_t binind; 318 tcache_bin_t *tbin; 319 320 binind = SMALL_SIZE2BIN(size); 321 assert(binind < NBINS); 322 tbin = &tcache->tbins[binind]; 323 ret = tcache_alloc_easy(tbin); 324 if (ret == NULL) { 325 ret = tcache_alloc_small_hard(tcache, tbin, binind); 326 if (ret == NULL) 327 return (NULL); 328 } 329 assert(arena_salloc(ret) == arena_bin_info[binind].reg_size); 330 331 if (zero == false) { 332 if (config_fill) { 333 if (opt_junk) 334 memset(ret, 0xa5, size); 335 else if (opt_zero) 336 memset(ret, 0, size); 337 } 338 } else 339 memset(ret, 0, size); 340 341 if (config_stats) 342 tbin->tstats.nrequests++; 343 if (config_prof) 344 tcache->prof_accumbytes += arena_bin_info[binind].reg_size; 345 tcache_event(tcache); 346 return (ret); 347} 348 349JEMALLOC_INLINE void * 350tcache_alloc_large(tcache_t *tcache, size_t size, bool zero) 351{ 352 void *ret; 353 size_t binind; 354 tcache_bin_t *tbin; 355 356 size = PAGE_CEILING(size); 357 assert(size <= tcache_maxclass); 358 binind = NBINS + (size >> PAGE_SHIFT) - 1; 359 assert(binind < nhbins); 360 tbin = &tcache->tbins[binind]; 361 ret = tcache_alloc_easy(tbin); 362 if (ret == NULL) { 363 /* 364 * Only allocate one large object at a time, because it's quite 365 * expensive to create one and not use it. 366 */ 367 ret = arena_malloc_large(tcache->arena, size, zero); 368 if (ret == NULL) 369 return (NULL); 370 } else { 371 if (config_prof) { 372 arena_chunk_t *chunk = 373 (arena_chunk_t *)CHUNK_ADDR2BASE(ret); 374 size_t pageind = (((uintptr_t)ret - (uintptr_t)chunk) >> 375 PAGE_SHIFT); 376 chunk->map[pageind-map_bias].bits &= 377 ~CHUNK_MAP_CLASS_MASK; 378 } 379 if (zero == false) { 380 if (config_fill) { 381 if (opt_junk) 382 memset(ret, 0xa5, size); 383 else if (opt_zero) 384 memset(ret, 0, size); 385 } 386 } else 387 memset(ret, 0, size); 388 389 if (config_stats) 390 tbin->tstats.nrequests++; 391 if (config_prof) 392 tcache->prof_accumbytes += size; 393 } 394 395 tcache_event(tcache); 396 return (ret); 397} 398 399JEMALLOC_INLINE void 400tcache_dalloc_small(tcache_t *tcache, void *ptr) 401{ 402 arena_t *arena; 403 arena_chunk_t *chunk; 404 arena_run_t *run; 405 arena_bin_t *bin; 406 tcache_bin_t *tbin; 407 tcache_bin_info_t *tbin_info; 408 size_t pageind, binind; 409 arena_chunk_map_t *mapelm; 410 411 assert(arena_salloc(ptr) <= SMALL_MAXCLASS); 412 413 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); 414 arena = chunk->arena; 415 pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> PAGE_SHIFT; 416 mapelm = &chunk->map[pageind-map_bias]; 417 run = (arena_run_t *)((uintptr_t)chunk + (uintptr_t)((pageind - 418 (mapelm->bits >> PAGE_SHIFT)) << PAGE_SHIFT)); 419 bin = run->bin; 420 binind = ((uintptr_t)bin - (uintptr_t)&arena->bins) / 421 sizeof(arena_bin_t); 422 assert(binind < NBINS); 423 424 if (config_fill && opt_junk) 425 memset(ptr, 0x5a, arena_bin_info[binind].reg_size); 426 427 tbin = &tcache->tbins[binind]; 428 tbin_info = &tcache_bin_info[binind]; 429 if (tbin->ncached == tbin_info->ncached_max) { 430 tcache_bin_flush_small(tbin, binind, (tbin_info->ncached_max >> 431 1), tcache); 432 } 433 assert(tbin->ncached < tbin_info->ncached_max); 434 tbin->avail[tbin->ncached] = ptr; 435 tbin->ncached++; 436 437 tcache_event(tcache); 438} 439 440JEMALLOC_INLINE void 441tcache_dalloc_large(tcache_t *tcache, void *ptr, size_t size) 442{ 443 size_t binind; 444 tcache_bin_t *tbin; 445 tcache_bin_info_t *tbin_info; 446 447 assert((size & PAGE_MASK) == 0); 448 assert(arena_salloc(ptr) > SMALL_MAXCLASS); 449 assert(arena_salloc(ptr) <= tcache_maxclass); 450 451 binind = NBINS + (size >> PAGE_SHIFT) - 1; 452 453 if (config_fill && opt_junk) 454 memset(ptr, 0x5a, size); 455 456 tbin = &tcache->tbins[binind]; 457 tbin_info = &tcache_bin_info[binind]; 458 if (tbin->ncached == tbin_info->ncached_max) { 459 tcache_bin_flush_large(tbin, binind, (tbin_info->ncached_max >> 460 1), tcache); 461 } 462 assert(tbin->ncached < tbin_info->ncached_max); 463 tbin->avail[tbin->ncached] = ptr; 464 tbin->ncached++; 465 466 tcache_event(tcache); 467} 468#endif 469 470#endif /* JEMALLOC_H_INLINES */ 471/******************************************************************************/ 472