tcache.h revision f7088e6c992d079bc3162e0c48ed4dc5def6d263
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 103size_t tcache_salloc(const void *ptr); 104void *tcache_alloc_small_hard(tcache_t *tcache, tcache_bin_t *tbin, 105 size_t binind); 106void tcache_bin_flush_small(tcache_bin_t *tbin, size_t binind, unsigned rem, 107 tcache_t *tcache); 108void tcache_bin_flush_large(tcache_bin_t *tbin, size_t binind, unsigned rem, 109 tcache_t *tcache); 110void tcache_arena_associate(tcache_t *tcache, arena_t *arena); 111void tcache_arena_dissociate(tcache_t *tcache); 112tcache_t *tcache_create(arena_t *arena); 113void tcache_destroy(tcache_t *tcache); 114void tcache_thread_cleanup(void *arg); 115void tcache_stats_merge(tcache_t *tcache, arena_t *arena); 116bool tcache_boot0(void); 117bool tcache_boot1(void); 118 119#endif /* JEMALLOC_H_EXTERNS */ 120/******************************************************************************/ 121#ifdef JEMALLOC_H_INLINES 122 123#ifndef JEMALLOC_ENABLE_INLINE 124malloc_tsd_protos(JEMALLOC_ATTR(unused), tcache, tcache_t *) 125malloc_tsd_protos(JEMALLOC_ATTR(unused), tcache_enabled, tcache_enabled_t) 126 127void tcache_event(tcache_t *tcache); 128void tcache_flush(void); 129bool tcache_enabled_get(void); 130tcache_t *tcache_get(bool create); 131void tcache_enabled_set(bool enabled); 132void *tcache_alloc_easy(tcache_bin_t *tbin); 133void *tcache_alloc_small(tcache_t *tcache, size_t size, bool zero); 134void *tcache_alloc_large(tcache_t *tcache, size_t size, bool zero); 135void tcache_dalloc_small(tcache_t *tcache, void *ptr); 136void tcache_dalloc_large(tcache_t *tcache, void *ptr, size_t size); 137#endif 138 139#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_TCACHE_C_)) 140/* Map of thread-specific caches. */ 141malloc_tsd_externs(tcache, tcache_t *) 142malloc_tsd_funcs(JEMALLOC_INLINE, tcache, tcache_t *, NULL, 143 tcache_thread_cleanup) 144/* Per thread flag that allows thread caches to be disabled. */ 145malloc_tsd_externs(tcache_enabled, tcache_enabled_t) 146malloc_tsd_funcs(JEMALLOC_INLINE, tcache_enabled, tcache_enabled_t, 147 tcache_enabled_default, malloc_tsd_no_cleanup) 148 149JEMALLOC_INLINE void 150tcache_flush(void) 151{ 152 tcache_t *tcache; 153 154 cassert(config_tcache); 155 156 tcache = *tcache_tsd_get(); 157 if ((uintptr_t)tcache <= (uintptr_t)TCACHE_STATE_MAX) 158 return; 159 tcache_destroy(tcache); 160 tcache = NULL; 161 tcache_tsd_set(&tcache); 162} 163 164JEMALLOC_INLINE bool 165tcache_enabled_get(void) 166{ 167 tcache_enabled_t tcache_enabled; 168 169 cassert(config_tcache); 170 171 tcache_enabled = *tcache_enabled_tsd_get(); 172 if (tcache_enabled == tcache_enabled_default) { 173 tcache_enabled = (tcache_enabled_t)opt_tcache; 174 tcache_enabled_tsd_set(&tcache_enabled); 175 } 176 177 return ((bool)tcache_enabled); 178} 179 180JEMALLOC_INLINE void 181tcache_enabled_set(bool enabled) 182{ 183 tcache_enabled_t tcache_enabled; 184 tcache_t *tcache; 185 186 cassert(config_tcache); 187 188 tcache_enabled = (tcache_enabled_t)enabled; 189 tcache_enabled_tsd_set(&tcache_enabled); 190 tcache = *tcache_tsd_get(); 191 if (enabled) { 192 if (tcache == TCACHE_STATE_DISABLED) { 193 tcache = NULL; 194 tcache_tsd_set(&tcache); 195 } 196 } else /* disabled */ { 197 if (tcache > TCACHE_STATE_MAX) { 198 tcache_destroy(tcache); 199 tcache = NULL; 200 } 201 if (tcache == NULL) { 202 tcache = TCACHE_STATE_DISABLED; 203 tcache_tsd_set(&tcache); 204 } 205 } 206} 207 208JEMALLOC_INLINE tcache_t * 209tcache_get(bool create) 210{ 211 tcache_t *tcache; 212 213 if (config_tcache == false) 214 return (NULL); 215 if (config_lazy_lock && isthreaded == false) 216 return (NULL); 217 218 tcache = *tcache_tsd_get(); 219 if ((uintptr_t)tcache <= (uintptr_t)TCACHE_STATE_MAX) { 220 if (tcache == TCACHE_STATE_DISABLED) 221 return (NULL); 222 if (tcache == NULL) { 223 if (create == false) { 224 /* 225 * Creating a tcache here would cause 226 * allocation as a side effect of free(). 227 * Ordinarily that would be okay since 228 * tcache_create() failure is a soft failure 229 * that doesn't propagate. However, if TLS 230 * data are freed via free() as in glibc, 231 * subtle corruption could result from setting 232 * a TLS variable after its backing memory is 233 * freed. 234 */ 235 return (NULL); 236 } 237 if (tcache_enabled_get() == false) { 238 tcache_enabled_set(false); /* Memoize. */ 239 return (NULL); 240 } 241 return (tcache_create(choose_arena(NULL))); 242 } 243 if (tcache == TCACHE_STATE_PURGATORY) { 244 /* 245 * Make a note that an allocator function was called 246 * after tcache_thread_cleanup() was called. 247 */ 248 tcache = TCACHE_STATE_REINCARNATED; 249 tcache_tsd_set(&tcache); 250 return (NULL); 251 } 252 if (tcache == TCACHE_STATE_REINCARNATED) 253 return (NULL); 254 not_reached(); 255 } 256 257 return (tcache); 258} 259 260JEMALLOC_INLINE void 261tcache_event(tcache_t *tcache) 262{ 263 264 if (TCACHE_GC_INCR == 0) 265 return; 266 267 tcache->ev_cnt++; 268 assert(tcache->ev_cnt <= TCACHE_GC_INCR); 269 if (tcache->ev_cnt == TCACHE_GC_INCR) { 270 size_t binind = tcache->next_gc_bin; 271 tcache_bin_t *tbin = &tcache->tbins[binind]; 272 tcache_bin_info_t *tbin_info = &tcache_bin_info[binind]; 273 274 if (tbin->low_water > 0) { 275 /* 276 * Flush (ceiling) 3/4 of the objects below the low 277 * water mark. 278 */ 279 if (binind < NBINS) { 280 tcache_bin_flush_small(tbin, binind, 281 tbin->ncached - tbin->low_water + 282 (tbin->low_water >> 2), tcache); 283 } else { 284 tcache_bin_flush_large(tbin, binind, 285 tbin->ncached - tbin->low_water + 286 (tbin->low_water >> 2), tcache); 287 } 288 /* 289 * Reduce fill count by 2X. Limit lg_fill_div such that 290 * the fill count is always at least 1. 291 */ 292 if ((tbin_info->ncached_max >> (tbin->lg_fill_div+1)) 293 >= 1) 294 tbin->lg_fill_div++; 295 } else if (tbin->low_water < 0) { 296 /* 297 * Increase fill count by 2X. Make sure lg_fill_div 298 * stays greater than 0. 299 */ 300 if (tbin->lg_fill_div > 1) 301 tbin->lg_fill_div--; 302 } 303 tbin->low_water = tbin->ncached; 304 305 tcache->next_gc_bin++; 306 if (tcache->next_gc_bin == nhbins) 307 tcache->next_gc_bin = 0; 308 tcache->ev_cnt = 0; 309 } 310} 311 312JEMALLOC_INLINE void * 313tcache_alloc_easy(tcache_bin_t *tbin) 314{ 315 void *ret; 316 317 if (tbin->ncached == 0) { 318 tbin->low_water = -1; 319 return (NULL); 320 } 321 tbin->ncached--; 322 if ((int)tbin->ncached < tbin->low_water) 323 tbin->low_water = tbin->ncached; 324 ret = tbin->avail[tbin->ncached]; 325 return (ret); 326} 327 328JEMALLOC_INLINE void * 329tcache_alloc_small(tcache_t *tcache, size_t size, bool zero) 330{ 331 void *ret; 332 size_t binind; 333 tcache_bin_t *tbin; 334 335 binind = SMALL_SIZE2BIN(size); 336 assert(binind < NBINS); 337 tbin = &tcache->tbins[binind]; 338 ret = tcache_alloc_easy(tbin); 339 if (ret == NULL) { 340 ret = tcache_alloc_small_hard(tcache, tbin, binind); 341 if (ret == NULL) 342 return (NULL); 343 } 344 assert(tcache_salloc(ret) == arena_bin_info[binind].reg_size); 345 346 if (zero == false) { 347 if (config_fill) { 348 if (opt_junk) { 349 arena_alloc_junk_small(ret, 350 &arena_bin_info[binind], false); 351 } else if (opt_zero) 352 memset(ret, 0, size); 353 } 354 } else { 355 if (config_fill && opt_junk) { 356 arena_alloc_junk_small(ret, &arena_bin_info[binind], 357 true); 358 } 359 VALGRIND_MAKE_MEM_UNDEFINED(ret, size); 360 memset(ret, 0, size); 361 } 362 363 if (config_stats) 364 tbin->tstats.nrequests++; 365 if (config_prof) 366 tcache->prof_accumbytes += arena_bin_info[binind].reg_size; 367 tcache_event(tcache); 368 return (ret); 369} 370 371JEMALLOC_INLINE void * 372tcache_alloc_large(tcache_t *tcache, size_t size, bool zero) 373{ 374 void *ret; 375 size_t binind; 376 tcache_bin_t *tbin; 377 378 size = PAGE_CEILING(size); 379 assert(size <= tcache_maxclass); 380 binind = NBINS + (size >> LG_PAGE) - 1; 381 assert(binind < nhbins); 382 tbin = &tcache->tbins[binind]; 383 ret = tcache_alloc_easy(tbin); 384 if (ret == NULL) { 385 /* 386 * Only allocate one large object at a time, because it's quite 387 * expensive to create one and not use it. 388 */ 389 ret = arena_malloc_large(tcache->arena, size, zero); 390 if (ret == NULL) 391 return (NULL); 392 } else { 393 if (config_prof) { 394 arena_chunk_t *chunk = 395 (arena_chunk_t *)CHUNK_ADDR2BASE(ret); 396 size_t pageind = (((uintptr_t)ret - (uintptr_t)chunk) >> 397 LG_PAGE); 398 chunk->map[pageind-map_bias].bits &= 399 ~CHUNK_MAP_CLASS_MASK; 400 } 401 if (zero == false) { 402 if (config_fill) { 403 if (opt_junk) 404 memset(ret, 0xa5, size); 405 else if (opt_zero) 406 memset(ret, 0, size); 407 } 408 } else { 409 VALGRIND_MAKE_MEM_UNDEFINED(ret, size); 410 memset(ret, 0, size); 411 } 412 413 if (config_stats) 414 tbin->tstats.nrequests++; 415 if (config_prof) 416 tcache->prof_accumbytes += size; 417 } 418 419 tcache_event(tcache); 420 return (ret); 421} 422 423JEMALLOC_INLINE void 424tcache_dalloc_small(tcache_t *tcache, void *ptr) 425{ 426 arena_t *arena; 427 arena_chunk_t *chunk; 428 arena_run_t *run; 429 arena_bin_t *bin; 430 tcache_bin_t *tbin; 431 tcache_bin_info_t *tbin_info; 432 size_t pageind, binind; 433 arena_chunk_map_t *mapelm; 434 435 assert(tcache_salloc(ptr) <= SMALL_MAXCLASS); 436 437 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); 438 arena = chunk->arena; 439 pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; 440 mapelm = &chunk->map[pageind-map_bias]; 441 run = (arena_run_t *)((uintptr_t)chunk + (uintptr_t)((pageind - 442 (mapelm->bits >> LG_PAGE)) << LG_PAGE)); 443 bin = run->bin; 444 binind = ((uintptr_t)bin - (uintptr_t)&arena->bins) / 445 sizeof(arena_bin_t); 446 assert(binind < NBINS); 447 448 if (config_fill && opt_junk) 449 arena_dalloc_junk_small(ptr, &arena_bin_info[binind]); 450 451 tbin = &tcache->tbins[binind]; 452 tbin_info = &tcache_bin_info[binind]; 453 if (tbin->ncached == tbin_info->ncached_max) { 454 tcache_bin_flush_small(tbin, binind, (tbin_info->ncached_max >> 455 1), tcache); 456 } 457 assert(tbin->ncached < tbin_info->ncached_max); 458 tbin->avail[tbin->ncached] = ptr; 459 tbin->ncached++; 460 461 tcache_event(tcache); 462} 463 464JEMALLOC_INLINE void 465tcache_dalloc_large(tcache_t *tcache, void *ptr, size_t size) 466{ 467 size_t binind; 468 tcache_bin_t *tbin; 469 tcache_bin_info_t *tbin_info; 470 471 assert((size & PAGE_MASK) == 0); 472 assert(tcache_salloc(ptr) > SMALL_MAXCLASS); 473 assert(tcache_salloc(ptr) <= tcache_maxclass); 474 475 binind = NBINS + (size >> LG_PAGE) - 1; 476 477 if (config_fill && opt_junk) 478 memset(ptr, 0x5a, size); 479 480 tbin = &tcache->tbins[binind]; 481 tbin_info = &tcache_bin_info[binind]; 482 if (tbin->ncached == tbin_info->ncached_max) { 483 tcache_bin_flush_large(tbin, binind, (tbin_info->ncached_max >> 484 1), tcache); 485 } 486 assert(tbin->ncached < tbin_info->ncached_max); 487 tbin->avail[tbin->ncached] = ptr; 488 tbin->ncached++; 489 490 tcache_event(tcache); 491} 492#endif 493 494#endif /* JEMALLOC_H_INLINES */ 495/******************************************************************************/ 496