blob: 868f2d777063f33b8ae1860cca61ee836ed2e40c [file] [log] [blame]
Jason Evanse476f8a2010-01-16 09:53:50 -08001#define JEMALLOC_TCACHE_C_
Jason Evans376b1522010-02-11 14:45:59 -08002#include "jemalloc/internal/jemalloc_internal.h"
Jason Evans962463d2012-02-13 12:29:49 -08003
Jason Evanse476f8a2010-01-16 09:53:50 -08004/******************************************************************************/
5/* Data. */
6
Jason Evanscd9a1342012-03-21 18:33:03 -07007malloc_tsd_data(, tcache, tcache_t *, NULL)
Jason Evansd4be8b72012-03-26 18:54:44 -07008malloc_tsd_data(, tcache_enabled, tcache_enabled_t, tcache_enabled_default)
Jason Evanscd9a1342012-03-21 18:33:03 -07009
Jason Evans3fa9a2f2010-03-07 15:34:14 -080010bool opt_tcache = true;
Jason Evanse7339702010-10-23 18:37:06 -070011ssize_t opt_lg_tcache_max = LG_TCACHE_MAXCLASS_DEFAULT;
Jason Evanse476f8a2010-01-16 09:53:50 -080012
Jason Evans84c8eef2011-03-16 10:30:13 -070013tcache_bin_info_t *tcache_bin_info;
14static unsigned stack_nelms; /* Total stack elms per tcache. */
15
Jason Evanscd9a1342012-03-21 18:33:03 -070016size_t nhbins;
17size_t tcache_maxclass;
Jason Evanse476f8a2010-01-16 09:53:50 -080018
19/******************************************************************************/
20
Jason Evansf7088e62012-04-19 18:28:03 -070021size_t tcache_salloc(const void *ptr)
22{
23
24 return (arena_salloc(ptr, false));
25}
26
Jason Evans203484e2012-05-02 00:30:36 -070027void
28tcache_event_hard(tcache_t *tcache)
29{
30 size_t binind = tcache->next_gc_bin;
31 tcache_bin_t *tbin = &tcache->tbins[binind];
32 tcache_bin_info_t *tbin_info = &tcache_bin_info[binind];
33
34 if (tbin->low_water > 0) {
35 /*
36 * Flush (ceiling) 3/4 of the objects below the low water mark.
37 */
38 if (binind < NBINS) {
39 tcache_bin_flush_small(tbin, binind, tbin->ncached -
40 tbin->low_water + (tbin->low_water >> 2), tcache);
41 } else {
42 tcache_bin_flush_large(tbin, binind, tbin->ncached -
43 tbin->low_water + (tbin->low_water >> 2), tcache);
44 }
45 /*
46 * Reduce fill count by 2X. Limit lg_fill_div such that the
47 * fill count is always at least 1.
48 */
49 if ((tbin_info->ncached_max >> (tbin->lg_fill_div+1)) >= 1)
50 tbin->lg_fill_div++;
51 } else if (tbin->low_water < 0) {
52 /*
53 * Increase fill count by 2X. Make sure lg_fill_div stays
54 * greater than 0.
55 */
56 if (tbin->lg_fill_div > 1)
57 tbin->lg_fill_div--;
58 }
59 tbin->low_water = tbin->ncached;
60
61 tcache->next_gc_bin++;
62 if (tcache->next_gc_bin == nhbins)
63 tcache->next_gc_bin = 0;
64 tcache->ev_cnt = 0;
65}
66
Jason Evanse476f8a2010-01-16 09:53:50 -080067void *
Jason Evansdafde142010-03-17 16:27:39 -070068tcache_alloc_small_hard(tcache_t *tcache, tcache_bin_t *tbin, size_t binind)
Jason Evanse476f8a2010-01-16 09:53:50 -080069{
70 void *ret;
71
Jason Evans7372b152012-02-10 20:22:09 -080072 arena_tcache_fill_small(tcache->arena, tbin, binind,
73 config_prof ? tcache->prof_accumbytes : 0);
74 if (config_prof)
75 tcache->prof_accumbytes = 0;
Jason Evansdafde142010-03-17 16:27:39 -070076 ret = tcache_alloc_easy(tbin);
Jason Evanse476f8a2010-01-16 09:53:50 -080077
78 return (ret);
79}
80
Jason Evanse476f8a2010-01-16 09:53:50 -080081void
Jason Evans7372b152012-02-10 20:22:09 -080082tcache_bin_flush_small(tcache_bin_t *tbin, size_t binind, unsigned rem,
83 tcache_t *tcache)
Jason Evanse476f8a2010-01-16 09:53:50 -080084{
Jason Evans84c8eef2011-03-16 10:30:13 -070085 void *ptr;
Jason Evans3fa9a2f2010-03-07 15:34:14 -080086 unsigned i, nflush, ndeferred;
Jason Evansa8118232011-03-14 12:56:51 -070087 bool merged_stats = false;
Jason Evanse476f8a2010-01-16 09:53:50 -080088
Jason Evansb1726102012-02-28 16:50:47 -080089 assert(binind < NBINS);
Jason Evans86815df2010-03-13 20:32:56 -080090 assert(rem <= tbin->ncached);
91
Jason Evans84c8eef2011-03-16 10:30:13 -070092 for (nflush = tbin->ncached - rem; nflush > 0; nflush = ndeferred) {
Jason Evans86815df2010-03-13 20:32:56 -080093 /* Lock the arena bin associated with the first object. */
Jason Evans84c8eef2011-03-16 10:30:13 -070094 arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(
95 tbin->avail[0]);
Jason Evans3fa9a2f2010-03-07 15:34:14 -080096 arena_t *arena = chunk->arena;
Jason Evans86815df2010-03-13 20:32:56 -080097 arena_bin_t *bin = &arena->bins[binind];
98
Jason Evans7372b152012-02-10 20:22:09 -080099 if (config_prof && arena == tcache->arena) {
Jason Evans88c222c2013-02-06 11:59:30 -0800100 if (arena_prof_accum(arena, tcache->prof_accumbytes))
101 prof_idump();
Jason Evansd34f9e72010-02-11 13:19:21 -0800102 tcache->prof_accumbytes = 0;
Jason Evanse69bee02010-03-15 22:25:23 -0700103 }
Jason Evanse69bee02010-03-15 22:25:23 -0700104
105 malloc_mutex_lock(&bin->lock);
Jason Evans7372b152012-02-10 20:22:09 -0800106 if (config_stats && arena == tcache->arena) {
Jason Evansa8118232011-03-14 12:56:51 -0700107 assert(merged_stats == false);
108 merged_stats = true;
Jason Evans86815df2010-03-13 20:32:56 -0800109 bin->stats.nflushes++;
110 bin->stats.nrequests += tbin->tstats.nrequests;
111 tbin->tstats.nrequests = 0;
Jason Evansd34f9e72010-02-11 13:19:21 -0800112 }
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800113 ndeferred = 0;
114 for (i = 0; i < nflush; i++) {
Jason Evans84c8eef2011-03-16 10:30:13 -0700115 ptr = tbin->avail[i];
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800116 assert(ptr != NULL);
Jason Evanse476f8a2010-01-16 09:53:50 -0800117 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
118 if (chunk->arena == arena) {
Jason Evans7393f442010-10-01 17:35:43 -0700119 size_t pageind = ((uintptr_t)ptr -
Jason Evansae4c7b42012-04-02 07:04:34 -0700120 (uintptr_t)chunk) >> LG_PAGE;
Jason Evanse476f8a2010-01-16 09:53:50 -0800121 arena_chunk_map_t *mapelm =
Jason Evans203484e2012-05-02 00:30:36 -0700122 arena_mapp_get(chunk, pageind);
Jason Evans122449b2012-04-06 00:35:09 -0700123 if (config_fill && opt_junk) {
124 arena_alloc_junk_small(ptr,
125 &arena_bin_info[binind], true);
126 }
Jason Evans203484e2012-05-02 00:30:36 -0700127 arena_dalloc_bin_locked(arena, chunk, ptr,
128 mapelm);
Jason Evanse476f8a2010-01-16 09:53:50 -0800129 } else {
130 /*
131 * This object was allocated via a different
Jason Evans86815df2010-03-13 20:32:56 -0800132 * arena bin than the one that is currently
133 * locked. Stash the object, so that it can be
134 * handled in a future pass.
Jason Evanse476f8a2010-01-16 09:53:50 -0800135 */
Jason Evans84c8eef2011-03-16 10:30:13 -0700136 tbin->avail[ndeferred] = ptr;
Jason Evanse476f8a2010-01-16 09:53:50 -0800137 ndeferred++;
138 }
139 }
Jason Evans86815df2010-03-13 20:32:56 -0800140 malloc_mutex_unlock(&bin->lock);
Jason Evanse476f8a2010-01-16 09:53:50 -0800141 }
Jason Evans7372b152012-02-10 20:22:09 -0800142 if (config_stats && merged_stats == false) {
Jason Evansa8118232011-03-14 12:56:51 -0700143 /*
144 * The flush loop didn't happen to flush to this thread's
145 * arena, so the stats didn't get merged. Manually do so now.
146 */
147 arena_bin_t *bin = &tcache->arena->bins[binind];
148 malloc_mutex_lock(&bin->lock);
149 bin->stats.nflushes++;
150 bin->stats.nrequests += tbin->tstats.nrequests;
151 tbin->tstats.nrequests = 0;
152 malloc_mutex_unlock(&bin->lock);
153 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800154
Jason Evans84c8eef2011-03-16 10:30:13 -0700155 memmove(tbin->avail, &tbin->avail[tbin->ncached - rem],
156 rem * sizeof(void *));
Jason Evanse476f8a2010-01-16 09:53:50 -0800157 tbin->ncached = rem;
Jason Evans1dcb4f82011-03-21 00:18:17 -0700158 if ((int)tbin->ncached < tbin->low_water)
Jason Evans86815df2010-03-13 20:32:56 -0800159 tbin->low_water = tbin->ncached;
Jason Evanse476f8a2010-01-16 09:53:50 -0800160}
161
Jason Evansdafde142010-03-17 16:27:39 -0700162void
Jason Evans7372b152012-02-10 20:22:09 -0800163tcache_bin_flush_large(tcache_bin_t *tbin, size_t binind, unsigned rem,
164 tcache_t *tcache)
Jason Evansdafde142010-03-17 16:27:39 -0700165{
Jason Evans84c8eef2011-03-16 10:30:13 -0700166 void *ptr;
Jason Evansdafde142010-03-17 16:27:39 -0700167 unsigned i, nflush, ndeferred;
Jason Evans84c8eef2011-03-16 10:30:13 -0700168 bool merged_stats = false;
Jason Evansdafde142010-03-17 16:27:39 -0700169
170 assert(binind < nhbins);
171 assert(rem <= tbin->ncached);
172
Jason Evans84c8eef2011-03-16 10:30:13 -0700173 for (nflush = tbin->ncached - rem; nflush > 0; nflush = ndeferred) {
Jason Evansdafde142010-03-17 16:27:39 -0700174 /* Lock the arena associated with the first object. */
Jason Evans84c8eef2011-03-16 10:30:13 -0700175 arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(
176 tbin->avail[0]);
Jason Evansdafde142010-03-17 16:27:39 -0700177 arena_t *arena = chunk->arena;
Jason Evans88c222c2013-02-06 11:59:30 -0800178 UNUSED bool idump;
Jason Evansdafde142010-03-17 16:27:39 -0700179
Jason Evans88c222c2013-02-06 11:59:30 -0800180 if (config_prof)
181 idump = false;
Jason Evansdafde142010-03-17 16:27:39 -0700182 malloc_mutex_lock(&arena->lock);
Jason Evans7372b152012-02-10 20:22:09 -0800183 if ((config_prof || config_stats) && arena == tcache->arena) {
184 if (config_prof) {
Jason Evans88c222c2013-02-06 11:59:30 -0800185 idump = arena_prof_accum_locked(arena,
Jason Evans7372b152012-02-10 20:22:09 -0800186 tcache->prof_accumbytes);
187 tcache->prof_accumbytes = 0;
188 }
189 if (config_stats) {
190 merged_stats = true;
191 arena->stats.nrequests_large +=
192 tbin->tstats.nrequests;
Jason Evansb1726102012-02-28 16:50:47 -0800193 arena->stats.lstats[binind - NBINS].nrequests +=
Jason Evans7372b152012-02-10 20:22:09 -0800194 tbin->tstats.nrequests;
195 tbin->tstats.nrequests = 0;
196 }
Jason Evansdafde142010-03-17 16:27:39 -0700197 }
Jason Evansdafde142010-03-17 16:27:39 -0700198 ndeferred = 0;
199 for (i = 0; i < nflush; i++) {
Jason Evans84c8eef2011-03-16 10:30:13 -0700200 ptr = tbin->avail[i];
Jason Evansdafde142010-03-17 16:27:39 -0700201 assert(ptr != NULL);
Jason Evansdafde142010-03-17 16:27:39 -0700202 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
203 if (chunk->arena == arena)
Jason Evans203484e2012-05-02 00:30:36 -0700204 arena_dalloc_large_locked(arena, chunk, ptr);
Jason Evansdafde142010-03-17 16:27:39 -0700205 else {
206 /*
207 * This object was allocated via a different
208 * arena than the one that is currently locked.
209 * Stash the object, so that it can be handled
210 * in a future pass.
211 */
Jason Evans84c8eef2011-03-16 10:30:13 -0700212 tbin->avail[ndeferred] = ptr;
Jason Evansdafde142010-03-17 16:27:39 -0700213 ndeferred++;
214 }
215 }
216 malloc_mutex_unlock(&arena->lock);
Jason Evans88c222c2013-02-06 11:59:30 -0800217 if (config_prof && idump)
218 prof_idump();
Jason Evansdafde142010-03-17 16:27:39 -0700219 }
Jason Evans7372b152012-02-10 20:22:09 -0800220 if (config_stats && merged_stats == false) {
Jason Evans84c8eef2011-03-16 10:30:13 -0700221 /*
222 * The flush loop didn't happen to flush to this thread's
223 * arena, so the stats didn't get merged. Manually do so now.
224 */
225 arena_t *arena = tcache->arena;
226 malloc_mutex_lock(&arena->lock);
227 arena->stats.nrequests_large += tbin->tstats.nrequests;
Jason Evansb1726102012-02-28 16:50:47 -0800228 arena->stats.lstats[binind - NBINS].nrequests +=
Jason Evans84c8eef2011-03-16 10:30:13 -0700229 tbin->tstats.nrequests;
230 tbin->tstats.nrequests = 0;
231 malloc_mutex_unlock(&arena->lock);
232 }
Jason Evansdafde142010-03-17 16:27:39 -0700233
Jason Evans84c8eef2011-03-16 10:30:13 -0700234 memmove(tbin->avail, &tbin->avail[tbin->ncached - rem],
235 rem * sizeof(void *));
Jason Evansdafde142010-03-17 16:27:39 -0700236 tbin->ncached = rem;
Jason Evans1dcb4f82011-03-21 00:18:17 -0700237 if ((int)tbin->ncached < tbin->low_water)
Jason Evansdafde142010-03-17 16:27:39 -0700238 tbin->low_water = tbin->ncached;
239}
240
Jason Evanscd9a1342012-03-21 18:33:03 -0700241void
242tcache_arena_associate(tcache_t *tcache, arena_t *arena)
243{
244
245 if (config_stats) {
246 /* Link into list of extant tcaches. */
247 malloc_mutex_lock(&arena->lock);
248 ql_elm_new(tcache, link);
249 ql_tail_insert(&arena->tcache_ql, tcache, link);
250 malloc_mutex_unlock(&arena->lock);
251 }
252 tcache->arena = arena;
253}
254
255void
256tcache_arena_dissociate(tcache_t *tcache)
257{
258
259 if (config_stats) {
260 /* Unlink from list of extant tcaches. */
261 malloc_mutex_lock(&tcache->arena->lock);
262 ql_remove(&tcache->arena->tcache_ql, tcache, link);
Jason Evanscd9a1342012-03-21 18:33:03 -0700263 tcache_stats_merge(tcache, tcache->arena);
Jason Evans30e7cb12013-10-21 15:00:06 -0700264 malloc_mutex_unlock(&tcache->arena->lock);
Jason Evanscd9a1342012-03-21 18:33:03 -0700265 }
266}
267
Jason Evanse476f8a2010-01-16 09:53:50 -0800268tcache_t *
Ben Maurera7619b72014-04-15 13:28:37 -0700269tcache_get_hard(tcache_t *tcache, bool create)
270{
271
272 if (tcache == NULL) {
273 if (create == false) {
274 /*
275 * Creating a tcache here would cause
276 * allocation as a side effect of free().
277 * Ordinarily that would be okay since
278 * tcache_create() failure is a soft failure
279 * that doesn't propagate. However, if TLS
280 * data are freed via free() as in glibc,
281 * subtle corruption could result from setting
282 * a TLS variable after its backing memory is
283 * freed.
284 */
285 return (NULL);
286 }
287 if (tcache_enabled_get() == false) {
288 tcache_enabled_set(false); /* Memoize. */
289 return (NULL);
290 }
291 return (tcache_create(choose_arena(NULL)));
292 }
293 if (tcache == TCACHE_STATE_PURGATORY) {
294 /*
295 * Make a note that an allocator function was called
296 * after tcache_thread_cleanup() was called.
297 */
298 tcache = TCACHE_STATE_REINCARNATED;
299 tcache_tsd_set(&tcache);
300 return (NULL);
301 }
302 if (tcache == TCACHE_STATE_REINCARNATED)
303 return (NULL);
304 not_reached();
305 return (NULL);
306}
307
308tcache_t *
Jason Evanse476f8a2010-01-16 09:53:50 -0800309tcache_create(arena_t *arena)
310{
311 tcache_t *tcache;
Jason Evans84c8eef2011-03-16 10:30:13 -0700312 size_t size, stack_offset;
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800313 unsigned i;
Jason Evanse476f8a2010-01-16 09:53:50 -0800314
Jason Evansc2fc8c82010-10-01 18:02:43 -0700315 size = offsetof(tcache_t, tbins) + (sizeof(tcache_bin_t) * nhbins);
Jason Evans84c8eef2011-03-16 10:30:13 -0700316 /* Naturally align the pointer stacks. */
317 size = PTR_CEILING(size);
318 stack_offset = size;
319 size += stack_nelms * sizeof(void *);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800320 /*
321 * Round up to the nearest multiple of the cacheline size, in order to
322 * avoid the possibility of false cacheline sharing.
323 *
Jason Evans8e3c3c62010-09-17 15:46:18 -0700324 * That this works relies on the same logic as in ipalloc(), but we
325 * cannot directly call ipalloc() here due to tcache bootstrapping
326 * issues.
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800327 */
328 size = (size + CACHELINE_MASK) & (-CACHELINE);
329
Jason Evansb1726102012-02-28 16:50:47 -0800330 if (size <= SMALL_MAXCLASS)
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800331 tcache = (tcache_t *)arena_malloc_small(arena, size, true);
Jason Evans84c8eef2011-03-16 10:30:13 -0700332 else if (size <= tcache_maxclass)
333 tcache = (tcache_t *)arena_malloc_large(arena, size, true);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800334 else
Jason Evansd82a5e62013-12-12 22:35:52 -0800335 tcache = (tcache_t *)icalloct(size, false, arena);
Jason Evanse476f8a2010-01-16 09:53:50 -0800336
337 if (tcache == NULL)
338 return (NULL);
339
Jason Evanscd9a1342012-03-21 18:33:03 -0700340 tcache_arena_associate(tcache, arena);
Jason Evanse476f8a2010-01-16 09:53:50 -0800341
Jason Evansdafde142010-03-17 16:27:39 -0700342 assert((TCACHE_NSLOTS_SMALL_MAX & 1U) == 0);
Jason Evans84c8eef2011-03-16 10:30:13 -0700343 for (i = 0; i < nhbins; i++) {
Jason Evans1dcb4f82011-03-21 00:18:17 -0700344 tcache->tbins[i].lg_fill_div = 1;
Jason Evans84c8eef2011-03-16 10:30:13 -0700345 tcache->tbins[i].avail = (void **)((uintptr_t)tcache +
346 (uintptr_t)stack_offset);
347 stack_offset += tcache_bin_info[i].ncached_max * sizeof(void *);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800348 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800349
Jason Evanscd9a1342012-03-21 18:33:03 -0700350 tcache_tsd_set(&tcache);
Jason Evanse476f8a2010-01-16 09:53:50 -0800351
352 return (tcache);
353}
354
355void
356tcache_destroy(tcache_t *tcache)
357{
358 unsigned i;
Jason Evans84c8eef2011-03-16 10:30:13 -0700359 size_t tcache_size;
Jason Evanse476f8a2010-01-16 09:53:50 -0800360
Jason Evanscd9a1342012-03-21 18:33:03 -0700361 tcache_arena_dissociate(tcache);
Jason Evanse476f8a2010-01-16 09:53:50 -0800362
Jason Evansb1726102012-02-28 16:50:47 -0800363 for (i = 0; i < NBINS; i++) {
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800364 tcache_bin_t *tbin = &tcache->tbins[i];
Jason Evans7372b152012-02-10 20:22:09 -0800365 tcache_bin_flush_small(tbin, i, 0, tcache);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800366
Jason Evans7372b152012-02-10 20:22:09 -0800367 if (config_stats && tbin->tstats.nrequests != 0) {
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800368 arena_t *arena = tcache->arena;
369 arena_bin_t *bin = &arena->bins[i];
Jason Evans86815df2010-03-13 20:32:56 -0800370 malloc_mutex_lock(&bin->lock);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800371 bin->stats.nrequests += tbin->tstats.nrequests;
Jason Evans86815df2010-03-13 20:32:56 -0800372 malloc_mutex_unlock(&bin->lock);
Jason Evanse476f8a2010-01-16 09:53:50 -0800373 }
374 }
375
Jason Evansdafde142010-03-17 16:27:39 -0700376 for (; i < nhbins; i++) {
377 tcache_bin_t *tbin = &tcache->tbins[i];
Jason Evans7372b152012-02-10 20:22:09 -0800378 tcache_bin_flush_large(tbin, i, 0, tcache);
Jason Evansdafde142010-03-17 16:27:39 -0700379
Jason Evans7372b152012-02-10 20:22:09 -0800380 if (config_stats && tbin->tstats.nrequests != 0) {
Jason Evansdafde142010-03-17 16:27:39 -0700381 arena_t *arena = tcache->arena;
382 malloc_mutex_lock(&arena->lock);
383 arena->stats.nrequests_large += tbin->tstats.nrequests;
Jason Evansb1726102012-02-28 16:50:47 -0800384 arena->stats.lstats[i - NBINS].nrequests +=
Jason Evansdafde142010-03-17 16:27:39 -0700385 tbin->tstats.nrequests;
386 malloc_mutex_unlock(&arena->lock);
387 }
Jason Evansdafde142010-03-17 16:27:39 -0700388 }
389
Jason Evans88c222c2013-02-06 11:59:30 -0800390 if (config_prof && tcache->prof_accumbytes > 0 &&
391 arena_prof_accum(tcache->arena, tcache->prof_accumbytes))
392 prof_idump();
Jason Evansd34f9e72010-02-11 13:19:21 -0800393
Jason Evans122449b2012-04-06 00:35:09 -0700394 tcache_size = arena_salloc(tcache, false);
Jason Evansb1726102012-02-28 16:50:47 -0800395 if (tcache_size <= SMALL_MAXCLASS) {
Jason Evanse476f8a2010-01-16 09:53:50 -0800396 arena_chunk_t *chunk = CHUNK_ADDR2BASE(tcache);
397 arena_t *arena = chunk->arena;
Jason Evans7393f442010-10-01 17:35:43 -0700398 size_t pageind = ((uintptr_t)tcache - (uintptr_t)chunk) >>
Jason Evansae4c7b42012-04-02 07:04:34 -0700399 LG_PAGE;
Jason Evans203484e2012-05-02 00:30:36 -0700400 arena_chunk_map_t *mapelm = arena_mapp_get(chunk, pageind);
Jason Evanse476f8a2010-01-16 09:53:50 -0800401
Jason Evans203484e2012-05-02 00:30:36 -0700402 arena_dalloc_bin(arena, chunk, tcache, pageind, mapelm);
Jason Evans84c8eef2011-03-16 10:30:13 -0700403 } else if (tcache_size <= tcache_maxclass) {
404 arena_chunk_t *chunk = CHUNK_ADDR2BASE(tcache);
405 arena_t *arena = chunk->arena;
406
Jason Evans84c8eef2011-03-16 10:30:13 -0700407 arena_dalloc_large(arena, chunk, tcache);
Jason Evanse476f8a2010-01-16 09:53:50 -0800408 } else
Jason Evansd82a5e62013-12-12 22:35:52 -0800409 idalloct(tcache, false);
Jason Evanse476f8a2010-01-16 09:53:50 -0800410}
411
Jason Evanscd9a1342012-03-21 18:33:03 -0700412void
Jason Evanse476f8a2010-01-16 09:53:50 -0800413tcache_thread_cleanup(void *arg)
414{
Jason Evanscd9a1342012-03-21 18:33:03 -0700415 tcache_t *tcache = *(tcache_t **)arg;
Jason Evanse476f8a2010-01-16 09:53:50 -0800416
Jason Evansd4be8b72012-03-26 18:54:44 -0700417 if (tcache == TCACHE_STATE_DISABLED) {
418 /* Do nothing. */
419 } else if (tcache == TCACHE_STATE_REINCARNATED) {
420 /*
421 * Another destructor called an allocator function after this
Jason Evans37013672012-04-06 12:41:55 -0700422 * destructor was called. Reset tcache to
423 * TCACHE_STATE_PURGATORY in order to receive another callback.
Jason Evansd4be8b72012-03-26 18:54:44 -0700424 */
425 tcache = TCACHE_STATE_PURGATORY;
426 tcache_tsd_set(&tcache);
427 } else if (tcache == TCACHE_STATE_PURGATORY) {
Jason Evans2dbecf12010-09-05 10:35:13 -0700428 /*
429 * The previous time this destructor was called, we set the key
Jason Evans37013672012-04-06 12:41:55 -0700430 * to TCACHE_STATE_PURGATORY so that other destructors wouldn't
431 * cause re-creation of the tcache. This time, do nothing, so
432 * that the destructor will not be called again.
Jason Evans2dbecf12010-09-05 10:35:13 -0700433 */
Jason Evans2dbecf12010-09-05 10:35:13 -0700434 } else if (tcache != NULL) {
Jason Evansd4be8b72012-03-26 18:54:44 -0700435 assert(tcache != TCACHE_STATE_PURGATORY);
Jason Evanse476f8a2010-01-16 09:53:50 -0800436 tcache_destroy(tcache);
Jason Evansd4be8b72012-03-26 18:54:44 -0700437 tcache = TCACHE_STATE_PURGATORY;
Jason Evanscd9a1342012-03-21 18:33:03 -0700438 tcache_tsd_set(&tcache);
Jason Evanse476f8a2010-01-16 09:53:50 -0800439 }
440}
441
Jason Evans30e7cb12013-10-21 15:00:06 -0700442/* Caller must own arena->lock. */
Jason Evanse476f8a2010-01-16 09:53:50 -0800443void
444tcache_stats_merge(tcache_t *tcache, arena_t *arena)
445{
446 unsigned i;
447
Jason Evans30e7cb12013-10-21 15:00:06 -0700448 cassert(config_stats);
449
Jason Evanse476f8a2010-01-16 09:53:50 -0800450 /* Merge and reset tcache stats. */
Jason Evansb1726102012-02-28 16:50:47 -0800451 for (i = 0; i < NBINS; i++) {
Jason Evanse476f8a2010-01-16 09:53:50 -0800452 arena_bin_t *bin = &arena->bins[i];
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800453 tcache_bin_t *tbin = &tcache->tbins[i];
Jason Evans86815df2010-03-13 20:32:56 -0800454 malloc_mutex_lock(&bin->lock);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800455 bin->stats.nrequests += tbin->tstats.nrequests;
Jason Evans86815df2010-03-13 20:32:56 -0800456 malloc_mutex_unlock(&bin->lock);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800457 tbin->tstats.nrequests = 0;
Jason Evanse476f8a2010-01-16 09:53:50 -0800458 }
Jason Evansdafde142010-03-17 16:27:39 -0700459
460 for (; i < nhbins; i++) {
Jason Evansb1726102012-02-28 16:50:47 -0800461 malloc_large_stats_t *lstats = &arena->stats.lstats[i - NBINS];
Jason Evansdafde142010-03-17 16:27:39 -0700462 tcache_bin_t *tbin = &tcache->tbins[i];
463 arena->stats.nrequests_large += tbin->tstats.nrequests;
464 lstats->nrequests += tbin->tstats.nrequests;
465 tbin->tstats.nrequests = 0;
466 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800467}
Jason Evanse476f8a2010-01-16 09:53:50 -0800468
Jason Evans84c8eef2011-03-16 10:30:13 -0700469bool
Jason Evanscd9a1342012-03-21 18:33:03 -0700470tcache_boot0(void)
Jason Evanse476f8a2010-01-16 09:53:50 -0800471{
Jason Evans37013672012-04-06 12:41:55 -0700472 unsigned i;
Jason Evanse476f8a2010-01-16 09:53:50 -0800473
Jason Evans37013672012-04-06 12:41:55 -0700474 /*
475 * If necessary, clamp opt_lg_tcache_max, now that arena_maxclass is
476 * known.
477 */
478 if (opt_lg_tcache_max < 0 || (1U << opt_lg_tcache_max) < SMALL_MAXCLASS)
479 tcache_maxclass = SMALL_MAXCLASS;
480 else if ((1U << opt_lg_tcache_max) > arena_maxclass)
481 tcache_maxclass = arena_maxclass;
482 else
483 tcache_maxclass = (1U << opt_lg_tcache_max);
Jason Evans84c8eef2011-03-16 10:30:13 -0700484
Jason Evans37013672012-04-06 12:41:55 -0700485 nhbins = NBINS + (tcache_maxclass >> LG_PAGE);
Jason Evansdafde142010-03-17 16:27:39 -0700486
Jason Evans37013672012-04-06 12:41:55 -0700487 /* Initialize tcache_bin_info. */
488 tcache_bin_info = (tcache_bin_info_t *)base_alloc(nhbins *
489 sizeof(tcache_bin_info_t));
490 if (tcache_bin_info == NULL)
491 return (true);
492 stack_nelms = 0;
493 for (i = 0; i < NBINS; i++) {
494 if ((arena_bin_info[i].nregs << 1) <= TCACHE_NSLOTS_SMALL_MAX) {
495 tcache_bin_info[i].ncached_max =
496 (arena_bin_info[i].nregs << 1);
497 } else {
498 tcache_bin_info[i].ncached_max =
499 TCACHE_NSLOTS_SMALL_MAX;
Jason Evans84c8eef2011-03-16 10:30:13 -0700500 }
Jason Evans37013672012-04-06 12:41:55 -0700501 stack_nelms += tcache_bin_info[i].ncached_max;
502 }
503 for (; i < nhbins; i++) {
504 tcache_bin_info[i].ncached_max = TCACHE_NSLOTS_LARGE;
505 stack_nelms += tcache_bin_info[i].ncached_max;
Jason Evanscd9a1342012-03-21 18:33:03 -0700506 }
Jason Evans84c8eef2011-03-16 10:30:13 -0700507
Jason Evanscd9a1342012-03-21 18:33:03 -0700508 return (false);
509}
510
511bool
512tcache_boot1(void)
513{
514
Jason Evans37013672012-04-06 12:41:55 -0700515 if (tcache_tsd_boot() || tcache_enabled_tsd_boot())
516 return (true);
Jason Evans84c8eef2011-03-16 10:30:13 -0700517
518 return (false);
Jason Evanse476f8a2010-01-16 09:53:50 -0800519}