blob: 21540ff46e7554653748893260060d8dadbd8ce6 [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 Evans3fa9a2f2010-03-07 15:34:14 -08007bool opt_tcache = true;
Jason Evanse7339702010-10-23 18:37:06 -07008ssize_t opt_lg_tcache_max = LG_TCACHE_MAXCLASS_DEFAULT;
Jason Evanse476f8a2010-01-16 09:53:50 -08009
Jason Evans84c8eef2011-03-16 10:30:13 -070010tcache_bin_info_t *tcache_bin_info;
11static unsigned stack_nelms; /* Total stack elms per tcache. */
12
Jason Evans603b3bd2016-02-24 11:02:14 -080013unsigned nhbins;
Jason Evanscd9a1342012-03-21 18:33:03 -070014size_t tcache_maxclass;
Jason Evanse476f8a2010-01-16 09:53:50 -080015
Jason Evans1cb181e2015-01-29 15:30:47 -080016tcaches_t *tcaches;
17
18/* Index of first element within tcaches that has never been used. */
19static unsigned tcaches_past;
20
21/* Head of singly linked list tracking available tcaches elements. */
22static tcaches_t *tcaches_avail;
23
Jason Evanse476f8a2010-01-16 09:53:50 -080024/******************************************************************************/
25
Jason Evansf6bd2e52016-03-23 15:32:07 -070026size_t
Jason Evansc1e00ef2016-05-10 22:21:10 -070027tcache_salloc(tsdn_t *tsdn, const void *ptr)
Jason Evansf7088e62012-04-19 18:28:03 -070028{
29
Jason Evansc1e00ef2016-05-10 22:21:10 -070030 return (arena_salloc(tsdn, ptr, false));
Jason Evansf7088e62012-04-19 18:28:03 -070031}
32
Jason Evans203484e2012-05-02 00:30:36 -070033void
Jason Evans1cb181e2015-01-29 15:30:47 -080034tcache_event_hard(tsd_t *tsd, tcache_t *tcache)
Jason Evans203484e2012-05-02 00:30:36 -070035{
Jason Evansd01fd192015-08-19 15:21:32 -070036 szind_t binind = tcache->next_gc_bin;
Jason Evans203484e2012-05-02 00:30:36 -070037 tcache_bin_t *tbin = &tcache->tbins[binind];
38 tcache_bin_info_t *tbin_info = &tcache_bin_info[binind];
39
40 if (tbin->low_water > 0) {
41 /*
42 * Flush (ceiling) 3/4 of the objects below the low water mark.
43 */
44 if (binind < NBINS) {
Jason Evans41cfe032015-02-13 15:28:56 -080045 tcache_bin_flush_small(tsd, tcache, tbin, binind,
46 tbin->ncached - tbin->low_water + (tbin->low_water
47 >> 2));
Jason Evans203484e2012-05-02 00:30:36 -070048 } else {
Jason Evans1cb181e2015-01-29 15:30:47 -080049 tcache_bin_flush_large(tsd, tbin, binind, tbin->ncached
50 - tbin->low_water + (tbin->low_water >> 2), tcache);
Jason Evans203484e2012-05-02 00:30:36 -070051 }
52 /*
53 * Reduce fill count by 2X. Limit lg_fill_div such that the
54 * fill count is always at least 1.
55 */
56 if ((tbin_info->ncached_max >> (tbin->lg_fill_div+1)) >= 1)
57 tbin->lg_fill_div++;
58 } else if (tbin->low_water < 0) {
59 /*
60 * Increase fill count by 2X. Make sure lg_fill_div stays
61 * greater than 0.
62 */
63 if (tbin->lg_fill_div > 1)
64 tbin->lg_fill_div--;
65 }
66 tbin->low_water = tbin->ncached;
67
68 tcache->next_gc_bin++;
69 if (tcache->next_gc_bin == nhbins)
70 tcache->next_gc_bin = 0;
Jason Evans203484e2012-05-02 00:30:36 -070071}
72
Jason Evanse476f8a2010-01-16 09:53:50 -080073void *
Jason Evansc1e00ef2016-05-10 22:21:10 -070074tcache_alloc_small_hard(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,
Qi Wangf4a0f322015-10-27 15:12:10 -070075 tcache_bin_t *tbin, szind_t binind, bool *tcache_success)
Jason Evanse476f8a2010-01-16 09:53:50 -080076{
77 void *ret;
78
Jason Evansc1e00ef2016-05-10 22:21:10 -070079 arena_tcache_fill_small(tsdn, arena, tbin, binind, config_prof ?
Jason Evans41cfe032015-02-13 15:28:56 -080080 tcache->prof_accumbytes : 0);
Jason Evans7372b152012-02-10 20:22:09 -080081 if (config_prof)
82 tcache->prof_accumbytes = 0;
Qi Wangf4a0f322015-10-27 15:12:10 -070083 ret = tcache_alloc_easy(tbin, tcache_success);
Jason Evanse476f8a2010-01-16 09:53:50 -080084
85 return (ret);
86}
87
Jason Evanse476f8a2010-01-16 09:53:50 -080088void
Jason Evans41cfe032015-02-13 15:28:56 -080089tcache_bin_flush_small(tsd_t *tsd, tcache_t *tcache, tcache_bin_t *tbin,
Jason Evansd01fd192015-08-19 15:21:32 -070090 szind_t binind, unsigned rem)
Jason Evanse476f8a2010-01-16 09:53:50 -080091{
Jason Evans1cb181e2015-01-29 15:30:47 -080092 arena_t *arena;
Jason Evans84c8eef2011-03-16 10:30:13 -070093 void *ptr;
Jason Evans3fa9a2f2010-03-07 15:34:14 -080094 unsigned i, nflush, ndeferred;
Jason Evansa8118232011-03-14 12:56:51 -070095 bool merged_stats = false;
Jason Evanse476f8a2010-01-16 09:53:50 -080096
Jason Evansb1726102012-02-28 16:50:47 -080097 assert(binind < NBINS);
Jason Evans86815df2010-03-13 20:32:56 -080098 assert(rem <= tbin->ncached);
99
Jason Evans90827a32016-05-03 15:00:42 -0700100 arena = arena_choose(tsd, NULL);
Jason Evans1cb181e2015-01-29 15:30:47 -0800101 assert(arena != NULL);
Jason Evans84c8eef2011-03-16 10:30:13 -0700102 for (nflush = tbin->ncached - rem; nflush > 0; nflush = ndeferred) {
Jason Evans86815df2010-03-13 20:32:56 -0800103 /* Lock the arena bin associated with the first object. */
Jason Evans84c8eef2011-03-16 10:30:13 -0700104 arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(
Qi Wangf4a0f322015-10-27 15:12:10 -0700105 *(tbin->avail - 1));
Jason Evansee41ad42015-02-15 18:04:46 -0800106 arena_t *bin_arena = extent_node_arena_get(&chunk->node);
Jason Evans064dbfb2015-02-12 00:09:37 -0800107 arena_bin_t *bin = &bin_arena->bins[binind];
Jason Evans86815df2010-03-13 20:32:56 -0800108
Jason Evans1cb181e2015-01-29 15:30:47 -0800109 if (config_prof && bin_arena == arena) {
Jason Evansc1e00ef2016-05-10 22:21:10 -0700110 if (arena_prof_accum(tsd_tsdn(tsd), arena,
Jason Evansb2c0d632016-04-13 23:36:15 -0700111 tcache->prof_accumbytes))
Jason Evansc1e00ef2016-05-10 22:21:10 -0700112 prof_idump(tsd_tsdn(tsd));
Jason Evansd34f9e72010-02-11 13:19:21 -0800113 tcache->prof_accumbytes = 0;
Jason Evanse69bee02010-03-15 22:25:23 -0700114 }
Jason Evanse69bee02010-03-15 22:25:23 -0700115
Jason Evansc1e00ef2016-05-10 22:21:10 -0700116 malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
Jason Evans1cb181e2015-01-29 15:30:47 -0800117 if (config_stats && bin_arena == arena) {
Jason Evans551ebc42014-10-03 10:16:09 -0700118 assert(!merged_stats);
Jason Evansa8118232011-03-14 12:56:51 -0700119 merged_stats = true;
Jason Evans86815df2010-03-13 20:32:56 -0800120 bin->stats.nflushes++;
121 bin->stats.nrequests += tbin->tstats.nrequests;
122 tbin->tstats.nrequests = 0;
Jason Evansd34f9e72010-02-11 13:19:21 -0800123 }
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800124 ndeferred = 0;
125 for (i = 0; i < nflush; i++) {
Qi Wangf4a0f322015-10-27 15:12:10 -0700126 ptr = *(tbin->avail - 1 - i);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800127 assert(ptr != NULL);
Jason Evanse476f8a2010-01-16 09:53:50 -0800128 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
Jason Evansee41ad42015-02-15 18:04:46 -0800129 if (extent_node_arena_get(&chunk->node) == bin_arena) {
Jason Evans7393f442010-10-01 17:35:43 -0700130 size_t pageind = ((uintptr_t)ptr -
Jason Evansae4c7b42012-04-02 07:04:34 -0700131 (uintptr_t)chunk) >> LG_PAGE;
Qinfan Wuff6a31d2014-08-29 13:34:40 -0700132 arena_chunk_map_bits_t *bitselm =
Jason Evans61a6dfc2016-03-23 16:04:38 -0700133 arena_bitselm_get_mutable(chunk, pageind);
Jason Evansc1e00ef2016-05-10 22:21:10 -0700134 arena_dalloc_bin_junked_locked(tsd_tsdn(tsd),
135 bin_arena, chunk, ptr, bitselm);
Jason Evanse476f8a2010-01-16 09:53:50 -0800136 } else {
137 /*
138 * This object was allocated via a different
Jason Evans86815df2010-03-13 20:32:56 -0800139 * arena bin than the one that is currently
140 * locked. Stash the object, so that it can be
141 * handled in a future pass.
Jason Evanse476f8a2010-01-16 09:53:50 -0800142 */
Qi Wangf4a0f322015-10-27 15:12:10 -0700143 *(tbin->avail - 1 - ndeferred) = ptr;
Jason Evanse476f8a2010-01-16 09:53:50 -0800144 ndeferred++;
145 }
146 }
Jason Evansc1e00ef2016-05-10 22:21:10 -0700147 malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
148 arena_decay_ticks(tsd_tsdn(tsd), bin_arena, nflush - ndeferred);
Jason Evanse476f8a2010-01-16 09:53:50 -0800149 }
Jason Evans551ebc42014-10-03 10:16:09 -0700150 if (config_stats && !merged_stats) {
Jason Evansa8118232011-03-14 12:56:51 -0700151 /*
152 * The flush loop didn't happen to flush to this thread's
153 * arena, so the stats didn't get merged. Manually do so now.
154 */
Jason Evans1cb181e2015-01-29 15:30:47 -0800155 arena_bin_t *bin = &arena->bins[binind];
Jason Evansc1e00ef2016-05-10 22:21:10 -0700156 malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
Jason Evansa8118232011-03-14 12:56:51 -0700157 bin->stats.nflushes++;
158 bin->stats.nrequests += tbin->tstats.nrequests;
159 tbin->tstats.nrequests = 0;
Jason Evansc1e00ef2016-05-10 22:21:10 -0700160 malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
Jason Evansa8118232011-03-14 12:56:51 -0700161 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800162
Qi Wangf4a0f322015-10-27 15:12:10 -0700163 memmove(tbin->avail - rem, tbin->avail - tbin->ncached, rem *
164 sizeof(void *));
Jason Evanse476f8a2010-01-16 09:53:50 -0800165 tbin->ncached = rem;
Jason Evans1dcb4f82011-03-21 00:18:17 -0700166 if ((int)tbin->ncached < tbin->low_water)
Jason Evans86815df2010-03-13 20:32:56 -0800167 tbin->low_water = tbin->ncached;
Jason Evanse476f8a2010-01-16 09:53:50 -0800168}
169
Jason Evansdafde142010-03-17 16:27:39 -0700170void
Jason Evansd01fd192015-08-19 15:21:32 -0700171tcache_bin_flush_large(tsd_t *tsd, tcache_bin_t *tbin, szind_t binind,
Jason Evans1cb181e2015-01-29 15:30:47 -0800172 unsigned rem, tcache_t *tcache)
Jason Evansdafde142010-03-17 16:27:39 -0700173{
Jason Evans1cb181e2015-01-29 15:30:47 -0800174 arena_t *arena;
Jason Evans84c8eef2011-03-16 10:30:13 -0700175 void *ptr;
Jason Evansdafde142010-03-17 16:27:39 -0700176 unsigned i, nflush, ndeferred;
Jason Evans84c8eef2011-03-16 10:30:13 -0700177 bool merged_stats = false;
Jason Evansdafde142010-03-17 16:27:39 -0700178
179 assert(binind < nhbins);
180 assert(rem <= tbin->ncached);
181
Jason Evans90827a32016-05-03 15:00:42 -0700182 arena = arena_choose(tsd, NULL);
Jason Evans1cb181e2015-01-29 15:30:47 -0800183 assert(arena != NULL);
Jason Evans84c8eef2011-03-16 10:30:13 -0700184 for (nflush = tbin->ncached - rem; nflush > 0; nflush = ndeferred) {
Jason Evansdafde142010-03-17 16:27:39 -0700185 /* Lock the arena associated with the first object. */
Jason Evans84c8eef2011-03-16 10:30:13 -0700186 arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(
Qi Wangf4a0f322015-10-27 15:12:10 -0700187 *(tbin->avail - 1));
Jason Evansee41ad42015-02-15 18:04:46 -0800188 arena_t *locked_arena = extent_node_arena_get(&chunk->node);
Jason Evans88c222c2013-02-06 11:59:30 -0800189 UNUSED bool idump;
Jason Evansdafde142010-03-17 16:27:39 -0700190
Jason Evans88c222c2013-02-06 11:59:30 -0800191 if (config_prof)
192 idump = false;
Jason Evansc1e00ef2016-05-10 22:21:10 -0700193 malloc_mutex_lock(tsd_tsdn(tsd), &locked_arena->lock);
Jason Evans1cb181e2015-01-29 15:30:47 -0800194 if ((config_prof || config_stats) && locked_arena == arena) {
Jason Evans7372b152012-02-10 20:22:09 -0800195 if (config_prof) {
Jason Evans88c222c2013-02-06 11:59:30 -0800196 idump = arena_prof_accum_locked(arena,
Jason Evans7372b152012-02-10 20:22:09 -0800197 tcache->prof_accumbytes);
198 tcache->prof_accumbytes = 0;
199 }
200 if (config_stats) {
201 merged_stats = true;
202 arena->stats.nrequests_large +=
203 tbin->tstats.nrequests;
Jason Evansb1726102012-02-28 16:50:47 -0800204 arena->stats.lstats[binind - NBINS].nrequests +=
Jason Evans7372b152012-02-10 20:22:09 -0800205 tbin->tstats.nrequests;
206 tbin->tstats.nrequests = 0;
207 }
Jason Evansdafde142010-03-17 16:27:39 -0700208 }
Jason Evansdafde142010-03-17 16:27:39 -0700209 ndeferred = 0;
210 for (i = 0; i < nflush; i++) {
Qi Wangf4a0f322015-10-27 15:12:10 -0700211 ptr = *(tbin->avail - 1 - i);
Jason Evansdafde142010-03-17 16:27:39 -0700212 assert(ptr != NULL);
Jason Evansdafde142010-03-17 16:27:39 -0700213 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
Jason Evansee41ad42015-02-15 18:04:46 -0800214 if (extent_node_arena_get(&chunk->node) ==
215 locked_arena) {
Jason Evansc1e00ef2016-05-10 22:21:10 -0700216 arena_dalloc_large_junked_locked(tsd_tsdn(tsd),
Jason Evansb2c0d632016-04-13 23:36:15 -0700217 locked_arena, chunk, ptr);
Jason Evansfc0b3b72014-10-09 17:54:06 -0700218 } else {
Jason Evansdafde142010-03-17 16:27:39 -0700219 /*
220 * This object was allocated via a different
221 * arena than the one that is currently locked.
222 * Stash the object, so that it can be handled
223 * in a future pass.
224 */
Qi Wangf4a0f322015-10-27 15:12:10 -0700225 *(tbin->avail - 1 - ndeferred) = ptr;
Jason Evansdafde142010-03-17 16:27:39 -0700226 ndeferred++;
227 }
228 }
Jason Evansc1e00ef2016-05-10 22:21:10 -0700229 malloc_mutex_unlock(tsd_tsdn(tsd), &locked_arena->lock);
Jason Evans88c222c2013-02-06 11:59:30 -0800230 if (config_prof && idump)
Jason Evansc1e00ef2016-05-10 22:21:10 -0700231 prof_idump(tsd_tsdn(tsd));
232 arena_decay_ticks(tsd_tsdn(tsd), locked_arena, nflush -
233 ndeferred);
Jason Evansdafde142010-03-17 16:27:39 -0700234 }
Jason Evans551ebc42014-10-03 10:16:09 -0700235 if (config_stats && !merged_stats) {
Jason Evans84c8eef2011-03-16 10:30:13 -0700236 /*
237 * The flush loop didn't happen to flush to this thread's
238 * arena, so the stats didn't get merged. Manually do so now.
239 */
Jason Evansc1e00ef2016-05-10 22:21:10 -0700240 malloc_mutex_lock(tsd_tsdn(tsd), &arena->lock);
Jason Evans84c8eef2011-03-16 10:30:13 -0700241 arena->stats.nrequests_large += tbin->tstats.nrequests;
Jason Evansb1726102012-02-28 16:50:47 -0800242 arena->stats.lstats[binind - NBINS].nrequests +=
Jason Evans84c8eef2011-03-16 10:30:13 -0700243 tbin->tstats.nrequests;
244 tbin->tstats.nrequests = 0;
Jason Evansc1e00ef2016-05-10 22:21:10 -0700245 malloc_mutex_unlock(tsd_tsdn(tsd), &arena->lock);
Jason Evans84c8eef2011-03-16 10:30:13 -0700246 }
Jason Evansdafde142010-03-17 16:27:39 -0700247
Qi Wangf4a0f322015-10-27 15:12:10 -0700248 memmove(tbin->avail - rem, tbin->avail - tbin->ncached, rem *
249 sizeof(void *));
Jason Evansdafde142010-03-17 16:27:39 -0700250 tbin->ncached = rem;
Jason Evans1dcb4f82011-03-21 00:18:17 -0700251 if ((int)tbin->ncached < tbin->low_water)
Jason Evansdafde142010-03-17 16:27:39 -0700252 tbin->low_water = tbin->ncached;
253}
254
Jason Evansc1e00ef2016-05-10 22:21:10 -0700255static void
256tcache_arena_associate(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena)
Jason Evanscd9a1342012-03-21 18:33:03 -0700257{
258
259 if (config_stats) {
260 /* Link into list of extant tcaches. */
Jason Evansc1e00ef2016-05-10 22:21:10 -0700261 malloc_mutex_lock(tsdn, &arena->lock);
Jason Evanscd9a1342012-03-21 18:33:03 -0700262 ql_elm_new(tcache, link);
263 ql_tail_insert(&arena->tcache_ql, tcache, link);
Jason Evansc1e00ef2016-05-10 22:21:10 -0700264 malloc_mutex_unlock(tsdn, &arena->lock);
Jason Evanscd9a1342012-03-21 18:33:03 -0700265 }
Jason Evanscd9a1342012-03-21 18:33:03 -0700266}
267
Jason Evansc1e00ef2016-05-10 22:21:10 -0700268static void
269tcache_arena_dissociate(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena)
Jason Evanscd9a1342012-03-21 18:33:03 -0700270{
271
272 if (config_stats) {
273 /* Unlink from list of extant tcaches. */
Jason Evansc1e00ef2016-05-10 22:21:10 -0700274 malloc_mutex_lock(tsdn, &arena->lock);
Jason Evans1cb181e2015-01-29 15:30:47 -0800275 if (config_debug) {
276 bool in_ql = false;
277 tcache_t *iter;
278 ql_foreach(iter, &arena->tcache_ql, link) {
279 if (iter == tcache) {
280 in_ql = true;
281 break;
282 }
283 }
284 assert(in_ql);
285 }
286 ql_remove(&arena->tcache_ql, tcache, link);
Jason Evansc1e00ef2016-05-10 22:21:10 -0700287 tcache_stats_merge(tsdn, tcache, arena);
288 malloc_mutex_unlock(tsdn, &arena->lock);
Jason Evanscd9a1342012-03-21 18:33:03 -0700289 }
290}
291
Jason Evansc1e00ef2016-05-10 22:21:10 -0700292void
293tcache_arena_reassociate(tsdn_t *tsdn, tcache_t *tcache, arena_t *oldarena,
294 arena_t *newarena)
295{
296
297 tcache_arena_dissociate(tsdn, tcache, oldarena);
298 tcache_arena_associate(tsdn, tcache, newarena);
299}
300
Jason Evanse476f8a2010-01-16 09:53:50 -0800301tcache_t *
Jason Evans5460aa62014-09-22 21:09:23 -0700302tcache_get_hard(tsd_t *tsd)
Ben Maurera7619b72014-04-15 13:28:37 -0700303{
Jason Evans8bb31982014-10-07 23:14:57 -0700304 arena_t *arena;
Ben Maurera7619b72014-04-15 13:28:37 -0700305
Jason Evans551ebc42014-10-03 10:16:09 -0700306 if (!tcache_enabled_get()) {
Jason Evans029d44c2014-10-04 11:12:53 -0700307 if (tsd_nominal(tsd))
308 tcache_enabled_set(false); /* Memoize. */
Ben Maurera7619b72014-04-15 13:28:37 -0700309 return (NULL);
310 }
Jason Evans90827a32016-05-03 15:00:42 -0700311 arena = arena_choose(tsd, NULL);
Jason Evans8bb31982014-10-07 23:14:57 -0700312 if (unlikely(arena == NULL))
313 return (NULL);
Jason Evansc1e00ef2016-05-10 22:21:10 -0700314 return (tcache_create(tsd_tsdn(tsd), arena));
Ben Maurera7619b72014-04-15 13:28:37 -0700315}
316
317tcache_t *
Jason Evansc1e00ef2016-05-10 22:21:10 -0700318tcache_create(tsdn_t *tsdn, arena_t *arena)
Jason Evanse476f8a2010-01-16 09:53:50 -0800319{
320 tcache_t *tcache;
Jason Evans84c8eef2011-03-16 10:30:13 -0700321 size_t size, stack_offset;
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800322 unsigned i;
Jason Evanse476f8a2010-01-16 09:53:50 -0800323
Jason Evansc2fc8c82010-10-01 18:02:43 -0700324 size = offsetof(tcache_t, tbins) + (sizeof(tcache_bin_t) * nhbins);
Jason Evans84c8eef2011-03-16 10:30:13 -0700325 /* Naturally align the pointer stacks. */
326 size = PTR_CEILING(size);
327 stack_offset = size;
328 size += stack_nelms * sizeof(void *);
Jason Evansfc0b3b72014-10-09 17:54:06 -0700329 /* Avoid false cacheline sharing. */
330 size = sa2u(size, CACHELINE);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800331
Jason Evansc1e00ef2016-05-10 22:21:10 -0700332 tcache = ipallocztm(tsdn, size, CACHELINE, true, NULL, true,
333 arena_get(TSDN_NULL, 0, true));
Jason Evanse476f8a2010-01-16 09:53:50 -0800334 if (tcache == NULL)
335 return (NULL);
336
Jason Evansc1e00ef2016-05-10 22:21:10 -0700337 tcache_arena_associate(tsdn, tcache, arena);
Jason Evanse476f8a2010-01-16 09:53:50 -0800338
Jason Evansc87ab252016-02-02 20:37:24 -0800339 ticker_init(&tcache->gc_ticker, TCACHE_GC_INCR);
340
Jason Evansdafde142010-03-17 16:27:39 -0700341 assert((TCACHE_NSLOTS_SMALL_MAX & 1U) == 0);
Jason Evans84c8eef2011-03-16 10:30:13 -0700342 for (i = 0; i < nhbins; i++) {
Jason Evans1dcb4f82011-03-21 00:18:17 -0700343 tcache->tbins[i].lg_fill_div = 1;
Qi Wangf4a0f322015-10-27 15:12:10 -0700344 stack_offset += tcache_bin_info[i].ncached_max * sizeof(void *);
345 /*
346 * avail points past the available space. Allocations will
347 * access the slots toward higher addresses (for the benefit of
348 * prefetch).
349 */
Jason Evans84c8eef2011-03-16 10:30:13 -0700350 tcache->tbins[i].avail = (void **)((uintptr_t)tcache +
351 (uintptr_t)stack_offset);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800352 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800353
Jason Evanse476f8a2010-01-16 09:53:50 -0800354 return (tcache);
355}
356
Jason Evans5460aa62014-09-22 21:09:23 -0700357static void
358tcache_destroy(tsd_t *tsd, tcache_t *tcache)
Jason Evanse476f8a2010-01-16 09:53:50 -0800359{
Jason Evans1cb181e2015-01-29 15:30:47 -0800360 arena_t *arena;
Jason Evanse476f8a2010-01-16 09:53:50 -0800361 unsigned i;
362
Jason Evans90827a32016-05-03 15:00:42 -0700363 arena = arena_choose(tsd, NULL);
Jason Evansc1e00ef2016-05-10 22:21:10 -0700364 tcache_arena_dissociate(tsd_tsdn(tsd), tcache, arena);
Jason Evanse476f8a2010-01-16 09:53:50 -0800365
Jason Evansb1726102012-02-28 16:50:47 -0800366 for (i = 0; i < NBINS; i++) {
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800367 tcache_bin_t *tbin = &tcache->tbins[i];
Jason Evans41cfe032015-02-13 15:28:56 -0800368 tcache_bin_flush_small(tsd, tcache, tbin, i, 0);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800369
Jason Evans7372b152012-02-10 20:22:09 -0800370 if (config_stats && tbin->tstats.nrequests != 0) {
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800371 arena_bin_t *bin = &arena->bins[i];
Jason Evansc1e00ef2016-05-10 22:21:10 -0700372 malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800373 bin->stats.nrequests += tbin->tstats.nrequests;
Jason Evansc1e00ef2016-05-10 22:21:10 -0700374 malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
Jason Evanse476f8a2010-01-16 09:53:50 -0800375 }
376 }
377
Jason Evansdafde142010-03-17 16:27:39 -0700378 for (; i < nhbins; i++) {
379 tcache_bin_t *tbin = &tcache->tbins[i];
Jason Evans1cb181e2015-01-29 15:30:47 -0800380 tcache_bin_flush_large(tsd, tbin, i, 0, tcache);
Jason Evansdafde142010-03-17 16:27:39 -0700381
Jason Evans7372b152012-02-10 20:22:09 -0800382 if (config_stats && tbin->tstats.nrequests != 0) {
Jason Evansc1e00ef2016-05-10 22:21:10 -0700383 malloc_mutex_lock(tsd_tsdn(tsd), &arena->lock);
Jason Evansdafde142010-03-17 16:27:39 -0700384 arena->stats.nrequests_large += tbin->tstats.nrequests;
Jason Evansb1726102012-02-28 16:50:47 -0800385 arena->stats.lstats[i - NBINS].nrequests +=
Jason Evansdafde142010-03-17 16:27:39 -0700386 tbin->tstats.nrequests;
Jason Evansc1e00ef2016-05-10 22:21:10 -0700387 malloc_mutex_unlock(tsd_tsdn(tsd), &arena->lock);
Jason Evansdafde142010-03-17 16:27:39 -0700388 }
Jason Evansdafde142010-03-17 16:27:39 -0700389 }
390
Jason Evans88c222c2013-02-06 11:59:30 -0800391 if (config_prof && tcache->prof_accumbytes > 0 &&
Jason Evansc1e00ef2016-05-10 22:21:10 -0700392 arena_prof_accum(tsd_tsdn(tsd), arena, tcache->prof_accumbytes))
393 prof_idump(tsd_tsdn(tsd));
Jason Evansd34f9e72010-02-11 13:19:21 -0800394
Jason Evansc1e00ef2016-05-10 22:21:10 -0700395 idalloctm(tsd_tsdn(tsd), tcache, NULL, true, true);
Jason Evanse476f8a2010-01-16 09:53:50 -0800396}
397
Jason Evanscd9a1342012-03-21 18:33:03 -0700398void
Jason Evans5460aa62014-09-22 21:09:23 -0700399tcache_cleanup(tsd_t *tsd)
Jason Evanse476f8a2010-01-16 09:53:50 -0800400{
Jason Evans5460aa62014-09-22 21:09:23 -0700401 tcache_t *tcache;
Jason Evanse476f8a2010-01-16 09:53:50 -0800402
Jason Evans5460aa62014-09-22 21:09:23 -0700403 if (!config_tcache)
404 return;
405
406 if ((tcache = tsd_tcache_get(tsd)) != NULL) {
407 tcache_destroy(tsd, tcache);
408 tsd_tcache_set(tsd, NULL);
Jason Evanse476f8a2010-01-16 09:53:50 -0800409 }
410}
411
Jason Evans5460aa62014-09-22 21:09:23 -0700412void
413tcache_enabled_cleanup(tsd_t *tsd)
414{
415
416 /* Do nothing. */
417}
418
Jason Evanse476f8a2010-01-16 09:53:50 -0800419void
Jason Evansc1e00ef2016-05-10 22:21:10 -0700420tcache_stats_merge(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena)
Jason Evanse476f8a2010-01-16 09:53:50 -0800421{
422 unsigned i;
423
Jason Evans30e7cb12013-10-21 15:00:06 -0700424 cassert(config_stats);
425
Jason Evansc1e00ef2016-05-10 22:21:10 -0700426 malloc_mutex_assert_owner(tsdn, &arena->lock);
Jason Evansb2c0d632016-04-13 23:36:15 -0700427
Jason Evanse476f8a2010-01-16 09:53:50 -0800428 /* Merge and reset tcache stats. */
Jason Evansb1726102012-02-28 16:50:47 -0800429 for (i = 0; i < NBINS; i++) {
Jason Evanse476f8a2010-01-16 09:53:50 -0800430 arena_bin_t *bin = &arena->bins[i];
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800431 tcache_bin_t *tbin = &tcache->tbins[i];
Jason Evansc1e00ef2016-05-10 22:21:10 -0700432 malloc_mutex_lock(tsdn, &bin->lock);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800433 bin->stats.nrequests += tbin->tstats.nrequests;
Jason Evansc1e00ef2016-05-10 22:21:10 -0700434 malloc_mutex_unlock(tsdn, &bin->lock);
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800435 tbin->tstats.nrequests = 0;
Jason Evanse476f8a2010-01-16 09:53:50 -0800436 }
Jason Evansdafde142010-03-17 16:27:39 -0700437
438 for (; i < nhbins; i++) {
Jason Evansb1726102012-02-28 16:50:47 -0800439 malloc_large_stats_t *lstats = &arena->stats.lstats[i - NBINS];
Jason Evansdafde142010-03-17 16:27:39 -0700440 tcache_bin_t *tbin = &tcache->tbins[i];
441 arena->stats.nrequests_large += tbin->tstats.nrequests;
442 lstats->nrequests += tbin->tstats.nrequests;
443 tbin->tstats.nrequests = 0;
444 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800445}
Jason Evanse476f8a2010-01-16 09:53:50 -0800446
Jason Evans84c8eef2011-03-16 10:30:13 -0700447bool
Jason Evans962a2972016-10-20 23:59:12 -0700448tcaches_create(tsd_t *tsd, unsigned *r_ind)
Jason Evans1cb181e2015-01-29 15:30:47 -0800449{
Jason Evans66cd9532016-04-22 14:34:14 -0700450 arena_t *arena;
Jason Evans1cb181e2015-01-29 15:30:47 -0800451 tcache_t *tcache;
452 tcaches_t *elm;
453
454 if (tcaches == NULL) {
Jason Evans962a2972016-10-20 23:59:12 -0700455 tcaches = base_alloc(tsd_tsdn(tsd), sizeof(tcache_t *) *
Jason Evans1cb181e2015-01-29 15:30:47 -0800456 (MALLOCX_TCACHE_MAX+1));
457 if (tcaches == NULL)
458 return (true);
459 }
460
461 if (tcaches_avail == NULL && tcaches_past > MALLOCX_TCACHE_MAX)
462 return (true);
Jason Evans962a2972016-10-20 23:59:12 -0700463 arena = arena_ichoose(tsd, NULL);
Jason Evans66cd9532016-04-22 14:34:14 -0700464 if (unlikely(arena == NULL))
465 return (true);
Jason Evans962a2972016-10-20 23:59:12 -0700466 tcache = tcache_create(tsd_tsdn(tsd), arena);
Jason Evans1cb181e2015-01-29 15:30:47 -0800467 if (tcache == NULL)
468 return (true);
469
470 if (tcaches_avail != NULL) {
471 elm = tcaches_avail;
472 tcaches_avail = tcaches_avail->next;
473 elm->tcache = tcache;
Jason Evans9e1810c2016-02-24 12:42:23 -0800474 *r_ind = (unsigned)(elm - tcaches);
Jason Evans1cb181e2015-01-29 15:30:47 -0800475 } else {
476 elm = &tcaches[tcaches_past];
477 elm->tcache = tcache;
478 *r_ind = tcaches_past;
479 tcaches_past++;
480 }
481
482 return (false);
483}
484
485static void
486tcaches_elm_flush(tsd_t *tsd, tcaches_t *elm)
487{
488
489 if (elm->tcache == NULL)
490 return;
491 tcache_destroy(tsd, elm->tcache);
492 elm->tcache = NULL;
493}
494
495void
496tcaches_flush(tsd_t *tsd, unsigned ind)
497{
498
499 tcaches_elm_flush(tsd, &tcaches[ind]);
500}
501
502void
503tcaches_destroy(tsd_t *tsd, unsigned ind)
504{
505 tcaches_t *elm = &tcaches[ind];
506 tcaches_elm_flush(tsd, elm);
507 elm->next = tcaches_avail;
508 tcaches_avail = elm;
509}
510
511bool
Jason Evansc1e00ef2016-05-10 22:21:10 -0700512tcache_boot(tsdn_t *tsdn)
Jason Evanse476f8a2010-01-16 09:53:50 -0800513{
Jason Evans37013672012-04-06 12:41:55 -0700514 unsigned i;
Jason Evanse476f8a2010-01-16 09:53:50 -0800515
Jason Evans37013672012-04-06 12:41:55 -0700516 /*
Jason Evans676df882015-09-11 20:50:20 -0700517 * If necessary, clamp opt_lg_tcache_max, now that large_maxclass is
Jason Evans37013672012-04-06 12:41:55 -0700518 * known.
519 */
Jason Evans8f61fde2016-10-27 21:31:25 -0700520 if (opt_lg_tcache_max < 0 || (ZU(1) << opt_lg_tcache_max) < SMALL_MAXCLASS)
Jason Evans37013672012-04-06 12:41:55 -0700521 tcache_maxclass = SMALL_MAXCLASS;
Jason Evans6468dd52016-11-15 21:08:28 -0800522 else if ((ZU(1) << opt_lg_tcache_max) > large_maxclass)
Jason Evans676df882015-09-11 20:50:20 -0700523 tcache_maxclass = large_maxclass;
Jason Evans37013672012-04-06 12:41:55 -0700524 else
Jason Evans8f61fde2016-10-27 21:31:25 -0700525 tcache_maxclass = (ZU(1) << opt_lg_tcache_max);
Jason Evans84c8eef2011-03-16 10:30:13 -0700526
Jason Evans5aa50a22015-05-19 17:40:37 -0700527 nhbins = size2index(tcache_maxclass) + 1;
Jason Evansdafde142010-03-17 16:27:39 -0700528
Jason Evans37013672012-04-06 12:41:55 -0700529 /* Initialize tcache_bin_info. */
Jason Evansc1e00ef2016-05-10 22:21:10 -0700530 tcache_bin_info = (tcache_bin_info_t *)base_alloc(tsdn, nhbins *
Jason Evans37013672012-04-06 12:41:55 -0700531 sizeof(tcache_bin_info_t));
532 if (tcache_bin_info == NULL)
533 return (true);
534 stack_nelms = 0;
535 for (i = 0; i < NBINS; i++) {
Jason Evans836bbe92015-05-19 17:47:16 -0700536 if ((arena_bin_info[i].nregs << 1) <= TCACHE_NSLOTS_SMALL_MIN) {
537 tcache_bin_info[i].ncached_max =
538 TCACHE_NSLOTS_SMALL_MIN;
539 } else if ((arena_bin_info[i].nregs << 1) <=
540 TCACHE_NSLOTS_SMALL_MAX) {
Jason Evans37013672012-04-06 12:41:55 -0700541 tcache_bin_info[i].ncached_max =
542 (arena_bin_info[i].nregs << 1);
543 } else {
544 tcache_bin_info[i].ncached_max =
545 TCACHE_NSLOTS_SMALL_MAX;
Jason Evans84c8eef2011-03-16 10:30:13 -0700546 }
Jason Evans37013672012-04-06 12:41:55 -0700547 stack_nelms += tcache_bin_info[i].ncached_max;
548 }
549 for (; i < nhbins; i++) {
550 tcache_bin_info[i].ncached_max = TCACHE_NSLOTS_LARGE;
551 stack_nelms += tcache_bin_info[i].ncached_max;
Jason Evanscd9a1342012-03-21 18:33:03 -0700552 }
Jason Evans84c8eef2011-03-16 10:30:13 -0700553
Jason Evanscd9a1342012-03-21 18:33:03 -0700554 return (false);
555}