1*1208bc7eSAndroid Build Coastguard Worker #define JEMALLOC_TCACHE_C_
2*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/jemalloc_preamble.h"
3*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/jemalloc_internal_includes.h"
4*1208bc7eSAndroid Build Coastguard Worker
5*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/assert.h"
6*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/mutex.h"
7*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/size_classes.h"
8*1208bc7eSAndroid Build Coastguard Worker
9*1208bc7eSAndroid Build Coastguard Worker /******************************************************************************/
10*1208bc7eSAndroid Build Coastguard Worker /* Data. */
11*1208bc7eSAndroid Build Coastguard Worker
12*1208bc7eSAndroid Build Coastguard Worker #if !defined(__BIONIC__) || defined(ANDROID_ENABLE_TCACHE)
13*1208bc7eSAndroid Build Coastguard Worker bool opt_tcache = true;
14*1208bc7eSAndroid Build Coastguard Worker #else
15*1208bc7eSAndroid Build Coastguard Worker bool opt_tcache = false;
16*1208bc7eSAndroid Build Coastguard Worker #endif
17*1208bc7eSAndroid Build Coastguard Worker ssize_t opt_lg_tcache_max = LG_TCACHE_MAXCLASS_DEFAULT;
18*1208bc7eSAndroid Build Coastguard Worker
19*1208bc7eSAndroid Build Coastguard Worker cache_bin_info_t *tcache_bin_info;
20*1208bc7eSAndroid Build Coastguard Worker static unsigned stack_nelms; /* Total stack elms per tcache. */
21*1208bc7eSAndroid Build Coastguard Worker
22*1208bc7eSAndroid Build Coastguard Worker unsigned nhbins;
23*1208bc7eSAndroid Build Coastguard Worker size_t tcache_maxclass;
24*1208bc7eSAndroid Build Coastguard Worker
25*1208bc7eSAndroid Build Coastguard Worker tcaches_t *tcaches;
26*1208bc7eSAndroid Build Coastguard Worker
27*1208bc7eSAndroid Build Coastguard Worker /* Index of first element within tcaches that has never been used. */
28*1208bc7eSAndroid Build Coastguard Worker static unsigned tcaches_past;
29*1208bc7eSAndroid Build Coastguard Worker
30*1208bc7eSAndroid Build Coastguard Worker /* Head of singly linked list tracking available tcaches elements. */
31*1208bc7eSAndroid Build Coastguard Worker static tcaches_t *tcaches_avail;
32*1208bc7eSAndroid Build Coastguard Worker
33*1208bc7eSAndroid Build Coastguard Worker /* Protects tcaches{,_past,_avail}. */
34*1208bc7eSAndroid Build Coastguard Worker static malloc_mutex_t tcaches_mtx;
35*1208bc7eSAndroid Build Coastguard Worker
36*1208bc7eSAndroid Build Coastguard Worker /******************************************************************************/
37*1208bc7eSAndroid Build Coastguard Worker
38*1208bc7eSAndroid Build Coastguard Worker size_t
tcache_salloc(tsdn_t * tsdn,const void * ptr)39*1208bc7eSAndroid Build Coastguard Worker tcache_salloc(tsdn_t *tsdn, const void *ptr) {
40*1208bc7eSAndroid Build Coastguard Worker return arena_salloc(tsdn, ptr);
41*1208bc7eSAndroid Build Coastguard Worker }
42*1208bc7eSAndroid Build Coastguard Worker
43*1208bc7eSAndroid Build Coastguard Worker void
tcache_event_hard(tsd_t * tsd,tcache_t * tcache)44*1208bc7eSAndroid Build Coastguard Worker tcache_event_hard(tsd_t *tsd, tcache_t *tcache) {
45*1208bc7eSAndroid Build Coastguard Worker szind_t binind = tcache->next_gc_bin;
46*1208bc7eSAndroid Build Coastguard Worker
47*1208bc7eSAndroid Build Coastguard Worker cache_bin_t *tbin;
48*1208bc7eSAndroid Build Coastguard Worker if (binind < NBINS) {
49*1208bc7eSAndroid Build Coastguard Worker tbin = tcache_small_bin_get(tcache, binind);
50*1208bc7eSAndroid Build Coastguard Worker } else {
51*1208bc7eSAndroid Build Coastguard Worker tbin = tcache_large_bin_get(tcache, binind);
52*1208bc7eSAndroid Build Coastguard Worker }
53*1208bc7eSAndroid Build Coastguard Worker if (tbin->low_water > 0) {
54*1208bc7eSAndroid Build Coastguard Worker /*
55*1208bc7eSAndroid Build Coastguard Worker * Flush (ceiling) 3/4 of the objects below the low water mark.
56*1208bc7eSAndroid Build Coastguard Worker */
57*1208bc7eSAndroid Build Coastguard Worker if (binind < NBINS) {
58*1208bc7eSAndroid Build Coastguard Worker tcache_bin_flush_small(tsd, tcache, tbin, binind,
59*1208bc7eSAndroid Build Coastguard Worker tbin->ncached - tbin->low_water + (tbin->low_water
60*1208bc7eSAndroid Build Coastguard Worker >> 2));
61*1208bc7eSAndroid Build Coastguard Worker /*
62*1208bc7eSAndroid Build Coastguard Worker * Reduce fill count by 2X. Limit lg_fill_div such that
63*1208bc7eSAndroid Build Coastguard Worker * the fill count is always at least 1.
64*1208bc7eSAndroid Build Coastguard Worker */
65*1208bc7eSAndroid Build Coastguard Worker cache_bin_info_t *tbin_info = &tcache_bin_info[binind];
66*1208bc7eSAndroid Build Coastguard Worker if ((tbin_info->ncached_max >>
67*1208bc7eSAndroid Build Coastguard Worker (tcache->lg_fill_div[binind] + 1)) >= 1) {
68*1208bc7eSAndroid Build Coastguard Worker tcache->lg_fill_div[binind]++;
69*1208bc7eSAndroid Build Coastguard Worker }
70*1208bc7eSAndroid Build Coastguard Worker } else {
71*1208bc7eSAndroid Build Coastguard Worker tcache_bin_flush_large(tsd, tbin, binind, tbin->ncached
72*1208bc7eSAndroid Build Coastguard Worker - tbin->low_water + (tbin->low_water >> 2), tcache);
73*1208bc7eSAndroid Build Coastguard Worker }
74*1208bc7eSAndroid Build Coastguard Worker } else if (tbin->low_water < 0) {
75*1208bc7eSAndroid Build Coastguard Worker /*
76*1208bc7eSAndroid Build Coastguard Worker * Increase fill count by 2X for small bins. Make sure
77*1208bc7eSAndroid Build Coastguard Worker * lg_fill_div stays greater than 0.
78*1208bc7eSAndroid Build Coastguard Worker */
79*1208bc7eSAndroid Build Coastguard Worker if (binind < NBINS && tcache->lg_fill_div[binind] > 1) {
80*1208bc7eSAndroid Build Coastguard Worker tcache->lg_fill_div[binind]--;
81*1208bc7eSAndroid Build Coastguard Worker }
82*1208bc7eSAndroid Build Coastguard Worker }
83*1208bc7eSAndroid Build Coastguard Worker tbin->low_water = tbin->ncached;
84*1208bc7eSAndroid Build Coastguard Worker
85*1208bc7eSAndroid Build Coastguard Worker tcache->next_gc_bin++;
86*1208bc7eSAndroid Build Coastguard Worker if (tcache->next_gc_bin == nhbins) {
87*1208bc7eSAndroid Build Coastguard Worker tcache->next_gc_bin = 0;
88*1208bc7eSAndroid Build Coastguard Worker }
89*1208bc7eSAndroid Build Coastguard Worker }
90*1208bc7eSAndroid Build Coastguard Worker
91*1208bc7eSAndroid Build Coastguard Worker void *
tcache_alloc_small_hard(tsdn_t * tsdn,arena_t * arena,tcache_t * tcache,cache_bin_t * tbin,szind_t binind,bool * tcache_success)92*1208bc7eSAndroid Build Coastguard Worker tcache_alloc_small_hard(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,
93*1208bc7eSAndroid Build Coastguard Worker cache_bin_t *tbin, szind_t binind, bool *tcache_success) {
94*1208bc7eSAndroid Build Coastguard Worker void *ret;
95*1208bc7eSAndroid Build Coastguard Worker
96*1208bc7eSAndroid Build Coastguard Worker assert(tcache->arena != NULL);
97*1208bc7eSAndroid Build Coastguard Worker arena_tcache_fill_small(tsdn, arena, tcache, tbin, binind,
98*1208bc7eSAndroid Build Coastguard Worker config_prof ? tcache->prof_accumbytes : 0);
99*1208bc7eSAndroid Build Coastguard Worker if (config_prof) {
100*1208bc7eSAndroid Build Coastguard Worker tcache->prof_accumbytes = 0;
101*1208bc7eSAndroid Build Coastguard Worker }
102*1208bc7eSAndroid Build Coastguard Worker ret = cache_bin_alloc_easy(tbin, tcache_success);
103*1208bc7eSAndroid Build Coastguard Worker
104*1208bc7eSAndroid Build Coastguard Worker return ret;
105*1208bc7eSAndroid Build Coastguard Worker }
106*1208bc7eSAndroid Build Coastguard Worker
107*1208bc7eSAndroid Build Coastguard Worker void
tcache_bin_flush_small(tsd_t * tsd,tcache_t * tcache,cache_bin_t * tbin,szind_t binind,unsigned rem)108*1208bc7eSAndroid Build Coastguard Worker tcache_bin_flush_small(tsd_t *tsd, tcache_t *tcache, cache_bin_t *tbin,
109*1208bc7eSAndroid Build Coastguard Worker szind_t binind, unsigned rem) {
110*1208bc7eSAndroid Build Coastguard Worker bool merged_stats = false;
111*1208bc7eSAndroid Build Coastguard Worker
112*1208bc7eSAndroid Build Coastguard Worker assert(binind < NBINS);
113*1208bc7eSAndroid Build Coastguard Worker assert((cache_bin_sz_t)rem <= tbin->ncached);
114*1208bc7eSAndroid Build Coastguard Worker
115*1208bc7eSAndroid Build Coastguard Worker arena_t *arena = tcache->arena;
116*1208bc7eSAndroid Build Coastguard Worker assert(arena != NULL);
117*1208bc7eSAndroid Build Coastguard Worker unsigned nflush = tbin->ncached - rem;
118*1208bc7eSAndroid Build Coastguard Worker VARIABLE_ARRAY(extent_t *, item_extent, nflush);
119*1208bc7eSAndroid Build Coastguard Worker /* Look up extent once per item. */
120*1208bc7eSAndroid Build Coastguard Worker for (unsigned i = 0 ; i < nflush; i++) {
121*1208bc7eSAndroid Build Coastguard Worker item_extent[i] = iealloc(tsd_tsdn(tsd), *(tbin->avail - 1 - i));
122*1208bc7eSAndroid Build Coastguard Worker }
123*1208bc7eSAndroid Build Coastguard Worker
124*1208bc7eSAndroid Build Coastguard Worker while (nflush > 0) {
125*1208bc7eSAndroid Build Coastguard Worker /* Lock the arena bin associated with the first object. */
126*1208bc7eSAndroid Build Coastguard Worker extent_t *extent = item_extent[0];
127*1208bc7eSAndroid Build Coastguard Worker arena_t *bin_arena = extent_arena_get(extent);
128*1208bc7eSAndroid Build Coastguard Worker bin_t *bin = &bin_arena->bins[binind];
129*1208bc7eSAndroid Build Coastguard Worker
130*1208bc7eSAndroid Build Coastguard Worker if (config_prof && bin_arena == arena) {
131*1208bc7eSAndroid Build Coastguard Worker if (arena_prof_accum(tsd_tsdn(tsd), arena,
132*1208bc7eSAndroid Build Coastguard Worker tcache->prof_accumbytes)) {
133*1208bc7eSAndroid Build Coastguard Worker prof_idump(tsd_tsdn(tsd));
134*1208bc7eSAndroid Build Coastguard Worker }
135*1208bc7eSAndroid Build Coastguard Worker tcache->prof_accumbytes = 0;
136*1208bc7eSAndroid Build Coastguard Worker }
137*1208bc7eSAndroid Build Coastguard Worker
138*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
139*1208bc7eSAndroid Build Coastguard Worker if (config_stats && bin_arena == arena) {
140*1208bc7eSAndroid Build Coastguard Worker assert(!merged_stats);
141*1208bc7eSAndroid Build Coastguard Worker merged_stats = true;
142*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
143*1208bc7eSAndroid Build Coastguard Worker bin->stats.nflushes++;
144*1208bc7eSAndroid Build Coastguard Worker #endif
145*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE_STATS)
146*1208bc7eSAndroid Build Coastguard Worker bin->stats.nrequests += tbin->tstats.nrequests;
147*1208bc7eSAndroid Build Coastguard Worker tbin->tstats.nrequests = 0;
148*1208bc7eSAndroid Build Coastguard Worker #endif
149*1208bc7eSAndroid Build Coastguard Worker }
150*1208bc7eSAndroid Build Coastguard Worker unsigned ndeferred = 0;
151*1208bc7eSAndroid Build Coastguard Worker for (unsigned i = 0; i < nflush; i++) {
152*1208bc7eSAndroid Build Coastguard Worker void *ptr = *(tbin->avail - 1 - i);
153*1208bc7eSAndroid Build Coastguard Worker extent = item_extent[i];
154*1208bc7eSAndroid Build Coastguard Worker assert(ptr != NULL && extent != NULL);
155*1208bc7eSAndroid Build Coastguard Worker
156*1208bc7eSAndroid Build Coastguard Worker if (extent_arena_get(extent) == bin_arena) {
157*1208bc7eSAndroid Build Coastguard Worker arena_dalloc_bin_junked_locked(tsd_tsdn(tsd),
158*1208bc7eSAndroid Build Coastguard Worker bin_arena, extent, ptr);
159*1208bc7eSAndroid Build Coastguard Worker } else {
160*1208bc7eSAndroid Build Coastguard Worker /*
161*1208bc7eSAndroid Build Coastguard Worker * This object was allocated via a different
162*1208bc7eSAndroid Build Coastguard Worker * arena bin than the one that is currently
163*1208bc7eSAndroid Build Coastguard Worker * locked. Stash the object, so that it can be
164*1208bc7eSAndroid Build Coastguard Worker * handled in a future pass.
165*1208bc7eSAndroid Build Coastguard Worker */
166*1208bc7eSAndroid Build Coastguard Worker *(tbin->avail - 1 - ndeferred) = ptr;
167*1208bc7eSAndroid Build Coastguard Worker item_extent[ndeferred] = extent;
168*1208bc7eSAndroid Build Coastguard Worker ndeferred++;
169*1208bc7eSAndroid Build Coastguard Worker }
170*1208bc7eSAndroid Build Coastguard Worker }
171*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
172*1208bc7eSAndroid Build Coastguard Worker arena_decay_ticks(tsd_tsdn(tsd), bin_arena, nflush - ndeferred);
173*1208bc7eSAndroid Build Coastguard Worker nflush = ndeferred;
174*1208bc7eSAndroid Build Coastguard Worker }
175*1208bc7eSAndroid Build Coastguard Worker if (config_stats && !merged_stats) {
176*1208bc7eSAndroid Build Coastguard Worker /*
177*1208bc7eSAndroid Build Coastguard Worker * The flush loop didn't happen to flush to this thread's
178*1208bc7eSAndroid Build Coastguard Worker * arena, so the stats didn't get merged. Manually do so now.
179*1208bc7eSAndroid Build Coastguard Worker */
180*1208bc7eSAndroid Build Coastguard Worker bin_t *bin = &arena->bins[binind];
181*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
182*1208bc7eSAndroid Build Coastguard Worker #if !defined(ANDROID_MINIMIZE_STRUCTS)
183*1208bc7eSAndroid Build Coastguard Worker bin->stats.nflushes++;
184*1208bc7eSAndroid Build Coastguard Worker #endif
185*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE_STATS)
186*1208bc7eSAndroid Build Coastguard Worker bin->stats.nrequests += tbin->tstats.nrequests;
187*1208bc7eSAndroid Build Coastguard Worker tbin->tstats.nrequests = 0;
188*1208bc7eSAndroid Build Coastguard Worker #endif
189*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
190*1208bc7eSAndroid Build Coastguard Worker }
191*1208bc7eSAndroid Build Coastguard Worker
192*1208bc7eSAndroid Build Coastguard Worker memmove(tbin->avail - rem, tbin->avail - tbin->ncached, rem *
193*1208bc7eSAndroid Build Coastguard Worker sizeof(void *));
194*1208bc7eSAndroid Build Coastguard Worker tbin->ncached = rem;
195*1208bc7eSAndroid Build Coastguard Worker if (tbin->ncached < tbin->low_water) {
196*1208bc7eSAndroid Build Coastguard Worker tbin->low_water = tbin->ncached;
197*1208bc7eSAndroid Build Coastguard Worker }
198*1208bc7eSAndroid Build Coastguard Worker }
199*1208bc7eSAndroid Build Coastguard Worker
200*1208bc7eSAndroid Build Coastguard Worker void
tcache_bin_flush_large(tsd_t * tsd,cache_bin_t * tbin,szind_t binind,unsigned rem,tcache_t * tcache)201*1208bc7eSAndroid Build Coastguard Worker tcache_bin_flush_large(tsd_t *tsd, cache_bin_t *tbin, szind_t binind,
202*1208bc7eSAndroid Build Coastguard Worker unsigned rem, tcache_t *tcache) {
203*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE_STATS)
204*1208bc7eSAndroid Build Coastguard Worker bool merged_stats = false;
205*1208bc7eSAndroid Build Coastguard Worker #endif
206*1208bc7eSAndroid Build Coastguard Worker
207*1208bc7eSAndroid Build Coastguard Worker assert(binind < nhbins);
208*1208bc7eSAndroid Build Coastguard Worker assert((cache_bin_sz_t)rem <= tbin->ncached);
209*1208bc7eSAndroid Build Coastguard Worker
210*1208bc7eSAndroid Build Coastguard Worker arena_t *arena = tcache->arena;
211*1208bc7eSAndroid Build Coastguard Worker assert(arena != NULL);
212*1208bc7eSAndroid Build Coastguard Worker unsigned nflush = tbin->ncached - rem;
213*1208bc7eSAndroid Build Coastguard Worker VARIABLE_ARRAY(extent_t *, item_extent, nflush);
214*1208bc7eSAndroid Build Coastguard Worker /* Look up extent once per item. */
215*1208bc7eSAndroid Build Coastguard Worker for (unsigned i = 0 ; i < nflush; i++) {
216*1208bc7eSAndroid Build Coastguard Worker item_extent[i] = iealloc(tsd_tsdn(tsd), *(tbin->avail - 1 - i));
217*1208bc7eSAndroid Build Coastguard Worker }
218*1208bc7eSAndroid Build Coastguard Worker
219*1208bc7eSAndroid Build Coastguard Worker while (nflush > 0) {
220*1208bc7eSAndroid Build Coastguard Worker /* Lock the arena associated with the first object. */
221*1208bc7eSAndroid Build Coastguard Worker extent_t *extent = item_extent[0];
222*1208bc7eSAndroid Build Coastguard Worker arena_t *locked_arena = extent_arena_get(extent);
223*1208bc7eSAndroid Build Coastguard Worker UNUSED bool idump;
224*1208bc7eSAndroid Build Coastguard Worker
225*1208bc7eSAndroid Build Coastguard Worker if (config_prof) {
226*1208bc7eSAndroid Build Coastguard Worker idump = false;
227*1208bc7eSAndroid Build Coastguard Worker }
228*1208bc7eSAndroid Build Coastguard Worker
229*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &locked_arena->large_mtx);
230*1208bc7eSAndroid Build Coastguard Worker for (unsigned i = 0; i < nflush; i++) {
231*1208bc7eSAndroid Build Coastguard Worker void *ptr = *(tbin->avail - 1 - i);
232*1208bc7eSAndroid Build Coastguard Worker assert(ptr != NULL);
233*1208bc7eSAndroid Build Coastguard Worker extent = item_extent[i];
234*1208bc7eSAndroid Build Coastguard Worker if (extent_arena_get(extent) == locked_arena) {
235*1208bc7eSAndroid Build Coastguard Worker large_dalloc_prep_junked_locked(tsd_tsdn(tsd),
236*1208bc7eSAndroid Build Coastguard Worker extent);
237*1208bc7eSAndroid Build Coastguard Worker }
238*1208bc7eSAndroid Build Coastguard Worker }
239*1208bc7eSAndroid Build Coastguard Worker if ((config_prof || config_stats) && locked_arena == arena) {
240*1208bc7eSAndroid Build Coastguard Worker if (config_prof) {
241*1208bc7eSAndroid Build Coastguard Worker idump = arena_prof_accum(tsd_tsdn(tsd), arena,
242*1208bc7eSAndroid Build Coastguard Worker tcache->prof_accumbytes);
243*1208bc7eSAndroid Build Coastguard Worker tcache->prof_accumbytes = 0;
244*1208bc7eSAndroid Build Coastguard Worker }
245*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE_STATS)
246*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
247*1208bc7eSAndroid Build Coastguard Worker merged_stats = true;
248*1208bc7eSAndroid Build Coastguard Worker arena_stats_large_nrequests_add(tsd_tsdn(tsd),
249*1208bc7eSAndroid Build Coastguard Worker &arena->stats, binind,
250*1208bc7eSAndroid Build Coastguard Worker tbin->tstats.nrequests);
251*1208bc7eSAndroid Build Coastguard Worker tbin->tstats.nrequests = 0;
252*1208bc7eSAndroid Build Coastguard Worker }
253*1208bc7eSAndroid Build Coastguard Worker #endif
254*1208bc7eSAndroid Build Coastguard Worker }
255*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &locked_arena->large_mtx);
256*1208bc7eSAndroid Build Coastguard Worker
257*1208bc7eSAndroid Build Coastguard Worker unsigned ndeferred = 0;
258*1208bc7eSAndroid Build Coastguard Worker for (unsigned i = 0; i < nflush; i++) {
259*1208bc7eSAndroid Build Coastguard Worker void *ptr = *(tbin->avail - 1 - i);
260*1208bc7eSAndroid Build Coastguard Worker extent = item_extent[i];
261*1208bc7eSAndroid Build Coastguard Worker assert(ptr != NULL && extent != NULL);
262*1208bc7eSAndroid Build Coastguard Worker
263*1208bc7eSAndroid Build Coastguard Worker if (extent_arena_get(extent) == locked_arena) {
264*1208bc7eSAndroid Build Coastguard Worker large_dalloc_finish(tsd_tsdn(tsd), extent);
265*1208bc7eSAndroid Build Coastguard Worker } else {
266*1208bc7eSAndroid Build Coastguard Worker /*
267*1208bc7eSAndroid Build Coastguard Worker * This object was allocated via a different
268*1208bc7eSAndroid Build Coastguard Worker * arena than the one that is currently locked.
269*1208bc7eSAndroid Build Coastguard Worker * Stash the object, so that it can be handled
270*1208bc7eSAndroid Build Coastguard Worker * in a future pass.
271*1208bc7eSAndroid Build Coastguard Worker */
272*1208bc7eSAndroid Build Coastguard Worker *(tbin->avail - 1 - ndeferred) = ptr;
273*1208bc7eSAndroid Build Coastguard Worker item_extent[ndeferred] = extent;
274*1208bc7eSAndroid Build Coastguard Worker ndeferred++;
275*1208bc7eSAndroid Build Coastguard Worker }
276*1208bc7eSAndroid Build Coastguard Worker }
277*1208bc7eSAndroid Build Coastguard Worker if (config_prof && idump) {
278*1208bc7eSAndroid Build Coastguard Worker prof_idump(tsd_tsdn(tsd));
279*1208bc7eSAndroid Build Coastguard Worker }
280*1208bc7eSAndroid Build Coastguard Worker arena_decay_ticks(tsd_tsdn(tsd), locked_arena, nflush -
281*1208bc7eSAndroid Build Coastguard Worker ndeferred);
282*1208bc7eSAndroid Build Coastguard Worker nflush = ndeferred;
283*1208bc7eSAndroid Build Coastguard Worker }
284*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE_STATS)
285*1208bc7eSAndroid Build Coastguard Worker if (config_stats && !merged_stats) {
286*1208bc7eSAndroid Build Coastguard Worker /*
287*1208bc7eSAndroid Build Coastguard Worker * The flush loop didn't happen to flush to this thread's
288*1208bc7eSAndroid Build Coastguard Worker * arena, so the stats didn't get merged. Manually do so now.
289*1208bc7eSAndroid Build Coastguard Worker */
290*1208bc7eSAndroid Build Coastguard Worker arena_stats_large_nrequests_add(tsd_tsdn(tsd), &arena->stats,
291*1208bc7eSAndroid Build Coastguard Worker binind, tbin->tstats.nrequests);
292*1208bc7eSAndroid Build Coastguard Worker tbin->tstats.nrequests = 0;
293*1208bc7eSAndroid Build Coastguard Worker }
294*1208bc7eSAndroid Build Coastguard Worker #endif
295*1208bc7eSAndroid Build Coastguard Worker
296*1208bc7eSAndroid Build Coastguard Worker memmove(tbin->avail - rem, tbin->avail - tbin->ncached, rem *
297*1208bc7eSAndroid Build Coastguard Worker sizeof(void *));
298*1208bc7eSAndroid Build Coastguard Worker tbin->ncached = rem;
299*1208bc7eSAndroid Build Coastguard Worker if (tbin->ncached < tbin->low_water) {
300*1208bc7eSAndroid Build Coastguard Worker tbin->low_water = tbin->ncached;
301*1208bc7eSAndroid Build Coastguard Worker }
302*1208bc7eSAndroid Build Coastguard Worker }
303*1208bc7eSAndroid Build Coastguard Worker
304*1208bc7eSAndroid Build Coastguard Worker void
tcache_arena_associate(tsdn_t * tsdn,tcache_t * tcache,arena_t * arena)305*1208bc7eSAndroid Build Coastguard Worker tcache_arena_associate(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena) {
306*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE)
307*1208bc7eSAndroid Build Coastguard Worker assert(tcache->arena == NULL);
308*1208bc7eSAndroid Build Coastguard Worker tcache->arena = arena;
309*1208bc7eSAndroid Build Coastguard Worker
310*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
311*1208bc7eSAndroid Build Coastguard Worker /* Link into list of extant tcaches. */
312*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);
313*1208bc7eSAndroid Build Coastguard Worker
314*1208bc7eSAndroid Build Coastguard Worker ql_elm_new(tcache, link);
315*1208bc7eSAndroid Build Coastguard Worker ql_tail_insert(&arena->tcache_ql, tcache, link);
316*1208bc7eSAndroid Build Coastguard Worker cache_bin_array_descriptor_init(
317*1208bc7eSAndroid Build Coastguard Worker &tcache->cache_bin_array_descriptor, tcache->bins_small,
318*1208bc7eSAndroid Build Coastguard Worker tcache->bins_large);
319*1208bc7eSAndroid Build Coastguard Worker ql_tail_insert(&arena->cache_bin_array_descriptor_ql,
320*1208bc7eSAndroid Build Coastguard Worker &tcache->cache_bin_array_descriptor, link);
321*1208bc7eSAndroid Build Coastguard Worker
322*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);
323*1208bc7eSAndroid Build Coastguard Worker }
324*1208bc7eSAndroid Build Coastguard Worker #endif
325*1208bc7eSAndroid Build Coastguard Worker }
326*1208bc7eSAndroid Build Coastguard Worker
327*1208bc7eSAndroid Build Coastguard Worker static void
tcache_arena_dissociate(tsdn_t * tsdn,tcache_t * tcache)328*1208bc7eSAndroid Build Coastguard Worker tcache_arena_dissociate(tsdn_t *tsdn, tcache_t *tcache) {
329*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE)
330*1208bc7eSAndroid Build Coastguard Worker arena_t *arena = tcache->arena;
331*1208bc7eSAndroid Build Coastguard Worker assert(arena != NULL);
332*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
333*1208bc7eSAndroid Build Coastguard Worker /* Unlink from list of extant tcaches. */
334*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);
335*1208bc7eSAndroid Build Coastguard Worker if (config_debug) {
336*1208bc7eSAndroid Build Coastguard Worker bool in_ql = false;
337*1208bc7eSAndroid Build Coastguard Worker tcache_t *iter;
338*1208bc7eSAndroid Build Coastguard Worker ql_foreach(iter, &arena->tcache_ql, link) {
339*1208bc7eSAndroid Build Coastguard Worker if (iter == tcache) {
340*1208bc7eSAndroid Build Coastguard Worker in_ql = true;
341*1208bc7eSAndroid Build Coastguard Worker break;
342*1208bc7eSAndroid Build Coastguard Worker }
343*1208bc7eSAndroid Build Coastguard Worker }
344*1208bc7eSAndroid Build Coastguard Worker assert(in_ql);
345*1208bc7eSAndroid Build Coastguard Worker }
346*1208bc7eSAndroid Build Coastguard Worker ql_remove(&arena->tcache_ql, tcache, link);
347*1208bc7eSAndroid Build Coastguard Worker ql_remove(&arena->cache_bin_array_descriptor_ql,
348*1208bc7eSAndroid Build Coastguard Worker &tcache->cache_bin_array_descriptor, link);
349*1208bc7eSAndroid Build Coastguard Worker tcache_stats_merge(tsdn, tcache, arena);
350*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);
351*1208bc7eSAndroid Build Coastguard Worker }
352*1208bc7eSAndroid Build Coastguard Worker tcache->arena = NULL;
353*1208bc7eSAndroid Build Coastguard Worker #endif
354*1208bc7eSAndroid Build Coastguard Worker }
355*1208bc7eSAndroid Build Coastguard Worker
356*1208bc7eSAndroid Build Coastguard Worker void
tcache_arena_reassociate(tsdn_t * tsdn,tcache_t * tcache,arena_t * arena)357*1208bc7eSAndroid Build Coastguard Worker tcache_arena_reassociate(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena) {
358*1208bc7eSAndroid Build Coastguard Worker tcache_arena_dissociate(tsdn, tcache);
359*1208bc7eSAndroid Build Coastguard Worker tcache_arena_associate(tsdn, tcache, arena);
360*1208bc7eSAndroid Build Coastguard Worker }
361*1208bc7eSAndroid Build Coastguard Worker
362*1208bc7eSAndroid Build Coastguard Worker bool
tsd_tcache_enabled_data_init(tsd_t * tsd)363*1208bc7eSAndroid Build Coastguard Worker tsd_tcache_enabled_data_init(tsd_t *tsd) {
364*1208bc7eSAndroid Build Coastguard Worker /* Called upon tsd initialization. */
365*1208bc7eSAndroid Build Coastguard Worker tsd_tcache_enabled_set(tsd, opt_tcache);
366*1208bc7eSAndroid Build Coastguard Worker tsd_slow_update(tsd);
367*1208bc7eSAndroid Build Coastguard Worker
368*1208bc7eSAndroid Build Coastguard Worker if (opt_tcache) {
369*1208bc7eSAndroid Build Coastguard Worker /* Trigger tcache init. */
370*1208bc7eSAndroid Build Coastguard Worker tsd_tcache_data_init(tsd);
371*1208bc7eSAndroid Build Coastguard Worker }
372*1208bc7eSAndroid Build Coastguard Worker
373*1208bc7eSAndroid Build Coastguard Worker return false;
374*1208bc7eSAndroid Build Coastguard Worker }
375*1208bc7eSAndroid Build Coastguard Worker
376*1208bc7eSAndroid Build Coastguard Worker /* Initialize auto tcache (embedded in TSD). */
377*1208bc7eSAndroid Build Coastguard Worker static void
tcache_init(tsd_t * tsd,tcache_t * tcache,void * avail_stack)378*1208bc7eSAndroid Build Coastguard Worker tcache_init(tsd_t *tsd, tcache_t *tcache, void *avail_stack) {
379*1208bc7eSAndroid Build Coastguard Worker memset(&tcache->link, 0, sizeof(ql_elm(tcache_t)));
380*1208bc7eSAndroid Build Coastguard Worker tcache->prof_accumbytes = 0;
381*1208bc7eSAndroid Build Coastguard Worker tcache->next_gc_bin = 0;
382*1208bc7eSAndroid Build Coastguard Worker tcache->arena = NULL;
383*1208bc7eSAndroid Build Coastguard Worker
384*1208bc7eSAndroid Build Coastguard Worker ticker_init(&tcache->gc_ticker, TCACHE_GC_INCR);
385*1208bc7eSAndroid Build Coastguard Worker
386*1208bc7eSAndroid Build Coastguard Worker size_t stack_offset = 0;
387*1208bc7eSAndroid Build Coastguard Worker assert((TCACHE_NSLOTS_SMALL_MAX & 1U) == 0);
388*1208bc7eSAndroid Build Coastguard Worker memset(tcache->bins_small, 0, sizeof(cache_bin_t) * NBINS);
389*1208bc7eSAndroid Build Coastguard Worker memset(tcache->bins_large, 0, sizeof(cache_bin_t) * (nhbins - NBINS));
390*1208bc7eSAndroid Build Coastguard Worker unsigned i = 0;
391*1208bc7eSAndroid Build Coastguard Worker for (; i < NBINS; i++) {
392*1208bc7eSAndroid Build Coastguard Worker tcache->lg_fill_div[i] = 1;
393*1208bc7eSAndroid Build Coastguard Worker stack_offset += tcache_bin_info[i].ncached_max * sizeof(void *);
394*1208bc7eSAndroid Build Coastguard Worker /*
395*1208bc7eSAndroid Build Coastguard Worker * avail points past the available space. Allocations will
396*1208bc7eSAndroid Build Coastguard Worker * access the slots toward higher addresses (for the benefit of
397*1208bc7eSAndroid Build Coastguard Worker * prefetch).
398*1208bc7eSAndroid Build Coastguard Worker */
399*1208bc7eSAndroid Build Coastguard Worker tcache_small_bin_get(tcache, i)->avail =
400*1208bc7eSAndroid Build Coastguard Worker (void **)((uintptr_t)avail_stack + (uintptr_t)stack_offset);
401*1208bc7eSAndroid Build Coastguard Worker }
402*1208bc7eSAndroid Build Coastguard Worker for (; i < nhbins; i++) {
403*1208bc7eSAndroid Build Coastguard Worker stack_offset += tcache_bin_info[i].ncached_max * sizeof(void *);
404*1208bc7eSAndroid Build Coastguard Worker tcache_large_bin_get(tcache, i)->avail =
405*1208bc7eSAndroid Build Coastguard Worker (void **)((uintptr_t)avail_stack + (uintptr_t)stack_offset);
406*1208bc7eSAndroid Build Coastguard Worker }
407*1208bc7eSAndroid Build Coastguard Worker assert(stack_offset == stack_nelms * sizeof(void *));
408*1208bc7eSAndroid Build Coastguard Worker }
409*1208bc7eSAndroid Build Coastguard Worker
410*1208bc7eSAndroid Build Coastguard Worker /* Initialize auto tcache (embedded in TSD). */
411*1208bc7eSAndroid Build Coastguard Worker bool
tsd_tcache_data_init(tsd_t * tsd)412*1208bc7eSAndroid Build Coastguard Worker tsd_tcache_data_init(tsd_t *tsd) {
413*1208bc7eSAndroid Build Coastguard Worker tcache_t *tcache = tsd_tcachep_get_unsafe(tsd);
414*1208bc7eSAndroid Build Coastguard Worker assert(tcache_small_bin_get(tcache, 0)->avail == NULL);
415*1208bc7eSAndroid Build Coastguard Worker size_t size = stack_nelms * sizeof(void *);
416*1208bc7eSAndroid Build Coastguard Worker /* Avoid false cacheline sharing. */
417*1208bc7eSAndroid Build Coastguard Worker size = sz_sa2u(size, CACHELINE);
418*1208bc7eSAndroid Build Coastguard Worker
419*1208bc7eSAndroid Build Coastguard Worker void *avail_array = ipallocztm(tsd_tsdn(tsd), size, CACHELINE, true,
420*1208bc7eSAndroid Build Coastguard Worker NULL, true, arena_get(TSDN_NULL, 0, true));
421*1208bc7eSAndroid Build Coastguard Worker if (avail_array == NULL) {
422*1208bc7eSAndroid Build Coastguard Worker return true;
423*1208bc7eSAndroid Build Coastguard Worker }
424*1208bc7eSAndroid Build Coastguard Worker
425*1208bc7eSAndroid Build Coastguard Worker tcache_init(tsd, tcache, avail_array);
426*1208bc7eSAndroid Build Coastguard Worker /*
427*1208bc7eSAndroid Build Coastguard Worker * Initialization is a bit tricky here. After malloc init is done, all
428*1208bc7eSAndroid Build Coastguard Worker * threads can rely on arena_choose and associate tcache accordingly.
429*1208bc7eSAndroid Build Coastguard Worker * However, the thread that does actual malloc bootstrapping relies on
430*1208bc7eSAndroid Build Coastguard Worker * functional tsd, and it can only rely on a0. In that case, we
431*1208bc7eSAndroid Build Coastguard Worker * associate its tcache to a0 temporarily, and later on
432*1208bc7eSAndroid Build Coastguard Worker * arena_choose_hard() will re-associate properly.
433*1208bc7eSAndroid Build Coastguard Worker */
434*1208bc7eSAndroid Build Coastguard Worker tcache->arena = NULL;
435*1208bc7eSAndroid Build Coastguard Worker arena_t *arena;
436*1208bc7eSAndroid Build Coastguard Worker if (!malloc_initialized()) {
437*1208bc7eSAndroid Build Coastguard Worker /* If in initialization, assign to a0. */
438*1208bc7eSAndroid Build Coastguard Worker arena = arena_get(tsd_tsdn(tsd), 0, false);
439*1208bc7eSAndroid Build Coastguard Worker tcache_arena_associate(tsd_tsdn(tsd), tcache, arena);
440*1208bc7eSAndroid Build Coastguard Worker } else {
441*1208bc7eSAndroid Build Coastguard Worker arena = arena_choose(tsd, NULL);
442*1208bc7eSAndroid Build Coastguard Worker /* This may happen if thread.tcache.enabled is used. */
443*1208bc7eSAndroid Build Coastguard Worker if (tcache->arena == NULL) {
444*1208bc7eSAndroid Build Coastguard Worker tcache_arena_associate(tsd_tsdn(tsd), tcache, arena);
445*1208bc7eSAndroid Build Coastguard Worker }
446*1208bc7eSAndroid Build Coastguard Worker }
447*1208bc7eSAndroid Build Coastguard Worker assert(arena == tcache->arena);
448*1208bc7eSAndroid Build Coastguard Worker
449*1208bc7eSAndroid Build Coastguard Worker return false;
450*1208bc7eSAndroid Build Coastguard Worker }
451*1208bc7eSAndroid Build Coastguard Worker
452*1208bc7eSAndroid Build Coastguard Worker /* Created manual tcache for tcache.create mallctl. */
453*1208bc7eSAndroid Build Coastguard Worker tcache_t *
tcache_create_explicit(tsd_t * tsd)454*1208bc7eSAndroid Build Coastguard Worker tcache_create_explicit(tsd_t *tsd) {
455*1208bc7eSAndroid Build Coastguard Worker tcache_t *tcache;
456*1208bc7eSAndroid Build Coastguard Worker size_t size, stack_offset;
457*1208bc7eSAndroid Build Coastguard Worker
458*1208bc7eSAndroid Build Coastguard Worker size = sizeof(tcache_t);
459*1208bc7eSAndroid Build Coastguard Worker /* Naturally align the pointer stacks. */
460*1208bc7eSAndroid Build Coastguard Worker size = PTR_CEILING(size);
461*1208bc7eSAndroid Build Coastguard Worker stack_offset = size;
462*1208bc7eSAndroid Build Coastguard Worker size += stack_nelms * sizeof(void *);
463*1208bc7eSAndroid Build Coastguard Worker /* Avoid false cacheline sharing. */
464*1208bc7eSAndroid Build Coastguard Worker size = sz_sa2u(size, CACHELINE);
465*1208bc7eSAndroid Build Coastguard Worker
466*1208bc7eSAndroid Build Coastguard Worker tcache = ipallocztm(tsd_tsdn(tsd), size, CACHELINE, true, NULL, true,
467*1208bc7eSAndroid Build Coastguard Worker arena_get(TSDN_NULL, 0, true));
468*1208bc7eSAndroid Build Coastguard Worker if (tcache == NULL) {
469*1208bc7eSAndroid Build Coastguard Worker return NULL;
470*1208bc7eSAndroid Build Coastguard Worker }
471*1208bc7eSAndroid Build Coastguard Worker
472*1208bc7eSAndroid Build Coastguard Worker tcache_init(tsd, tcache,
473*1208bc7eSAndroid Build Coastguard Worker (void *)((uintptr_t)tcache + (uintptr_t)stack_offset));
474*1208bc7eSAndroid Build Coastguard Worker tcache_arena_associate(tsd_tsdn(tsd), tcache, arena_ichoose(tsd, NULL));
475*1208bc7eSAndroid Build Coastguard Worker
476*1208bc7eSAndroid Build Coastguard Worker return tcache;
477*1208bc7eSAndroid Build Coastguard Worker }
478*1208bc7eSAndroid Build Coastguard Worker
479*1208bc7eSAndroid Build Coastguard Worker static void
tcache_flush_cache(tsd_t * tsd,tcache_t * tcache)480*1208bc7eSAndroid Build Coastguard Worker tcache_flush_cache(tsd_t *tsd, tcache_t *tcache) {
481*1208bc7eSAndroid Build Coastguard Worker assert(tcache->arena != NULL);
482*1208bc7eSAndroid Build Coastguard Worker
483*1208bc7eSAndroid Build Coastguard Worker for (unsigned i = 0; i < NBINS; i++) {
484*1208bc7eSAndroid Build Coastguard Worker cache_bin_t *tbin = tcache_small_bin_get(tcache, i);
485*1208bc7eSAndroid Build Coastguard Worker tcache_bin_flush_small(tsd, tcache, tbin, i, 0);
486*1208bc7eSAndroid Build Coastguard Worker
487*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE_STATS)
488*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
489*1208bc7eSAndroid Build Coastguard Worker assert(tbin->tstats.nrequests == 0);
490*1208bc7eSAndroid Build Coastguard Worker }
491*1208bc7eSAndroid Build Coastguard Worker #endif
492*1208bc7eSAndroid Build Coastguard Worker }
493*1208bc7eSAndroid Build Coastguard Worker for (unsigned i = NBINS; i < nhbins; i++) {
494*1208bc7eSAndroid Build Coastguard Worker cache_bin_t *tbin = tcache_large_bin_get(tcache, i);
495*1208bc7eSAndroid Build Coastguard Worker tcache_bin_flush_large(tsd, tbin, i, 0, tcache);
496*1208bc7eSAndroid Build Coastguard Worker
497*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE_STATS)
498*1208bc7eSAndroid Build Coastguard Worker if (config_stats) {
499*1208bc7eSAndroid Build Coastguard Worker assert(tbin->tstats.nrequests == 0);
500*1208bc7eSAndroid Build Coastguard Worker }
501*1208bc7eSAndroid Build Coastguard Worker #endif
502*1208bc7eSAndroid Build Coastguard Worker }
503*1208bc7eSAndroid Build Coastguard Worker
504*1208bc7eSAndroid Build Coastguard Worker if (config_prof && tcache->prof_accumbytes > 0 &&
505*1208bc7eSAndroid Build Coastguard Worker arena_prof_accum(tsd_tsdn(tsd), tcache->arena,
506*1208bc7eSAndroid Build Coastguard Worker tcache->prof_accumbytes)) {
507*1208bc7eSAndroid Build Coastguard Worker prof_idump(tsd_tsdn(tsd));
508*1208bc7eSAndroid Build Coastguard Worker }
509*1208bc7eSAndroid Build Coastguard Worker }
510*1208bc7eSAndroid Build Coastguard Worker
511*1208bc7eSAndroid Build Coastguard Worker void
tcache_flush(tsd_t * tsd)512*1208bc7eSAndroid Build Coastguard Worker tcache_flush(tsd_t *tsd) {
513*1208bc7eSAndroid Build Coastguard Worker assert(tcache_available(tsd));
514*1208bc7eSAndroid Build Coastguard Worker tcache_flush_cache(tsd, tsd_tcachep_get(tsd));
515*1208bc7eSAndroid Build Coastguard Worker }
516*1208bc7eSAndroid Build Coastguard Worker
517*1208bc7eSAndroid Build Coastguard Worker static void
tcache_destroy(tsd_t * tsd,tcache_t * tcache,bool tsd_tcache)518*1208bc7eSAndroid Build Coastguard Worker tcache_destroy(tsd_t *tsd, tcache_t *tcache, bool tsd_tcache) {
519*1208bc7eSAndroid Build Coastguard Worker tcache_flush_cache(tsd, tcache);
520*1208bc7eSAndroid Build Coastguard Worker tcache_arena_dissociate(tsd_tsdn(tsd), tcache);
521*1208bc7eSAndroid Build Coastguard Worker
522*1208bc7eSAndroid Build Coastguard Worker if (tsd_tcache) {
523*1208bc7eSAndroid Build Coastguard Worker /* Release the avail array for the TSD embedded auto tcache. */
524*1208bc7eSAndroid Build Coastguard Worker void *avail_array =
525*1208bc7eSAndroid Build Coastguard Worker (void *)((uintptr_t)tcache_small_bin_get(tcache, 0)->avail -
526*1208bc7eSAndroid Build Coastguard Worker (uintptr_t)tcache_bin_info[0].ncached_max * sizeof(void *));
527*1208bc7eSAndroid Build Coastguard Worker idalloctm(tsd_tsdn(tsd), avail_array, NULL, NULL, true, true);
528*1208bc7eSAndroid Build Coastguard Worker } else {
529*1208bc7eSAndroid Build Coastguard Worker /* Release both the tcache struct and avail array. */
530*1208bc7eSAndroid Build Coastguard Worker idalloctm(tsd_tsdn(tsd), tcache, NULL, NULL, true, true);
531*1208bc7eSAndroid Build Coastguard Worker }
532*1208bc7eSAndroid Build Coastguard Worker }
533*1208bc7eSAndroid Build Coastguard Worker
534*1208bc7eSAndroid Build Coastguard Worker /* For auto tcache (embedded in TSD) only. */
535*1208bc7eSAndroid Build Coastguard Worker void
tcache_cleanup(tsd_t * tsd)536*1208bc7eSAndroid Build Coastguard Worker tcache_cleanup(tsd_t *tsd) {
537*1208bc7eSAndroid Build Coastguard Worker tcache_t *tcache = tsd_tcachep_get(tsd);
538*1208bc7eSAndroid Build Coastguard Worker if (!tcache_available(tsd)) {
539*1208bc7eSAndroid Build Coastguard Worker assert(tsd_tcache_enabled_get(tsd) == false);
540*1208bc7eSAndroid Build Coastguard Worker if (config_debug) {
541*1208bc7eSAndroid Build Coastguard Worker assert(tcache_small_bin_get(tcache, 0)->avail == NULL);
542*1208bc7eSAndroid Build Coastguard Worker }
543*1208bc7eSAndroid Build Coastguard Worker return;
544*1208bc7eSAndroid Build Coastguard Worker }
545*1208bc7eSAndroid Build Coastguard Worker assert(tsd_tcache_enabled_get(tsd));
546*1208bc7eSAndroid Build Coastguard Worker assert(tcache_small_bin_get(tcache, 0)->avail != NULL);
547*1208bc7eSAndroid Build Coastguard Worker
548*1208bc7eSAndroid Build Coastguard Worker tcache_destroy(tsd, tcache, true);
549*1208bc7eSAndroid Build Coastguard Worker if (config_debug) {
550*1208bc7eSAndroid Build Coastguard Worker tcache_small_bin_get(tcache, 0)->avail = NULL;
551*1208bc7eSAndroid Build Coastguard Worker }
552*1208bc7eSAndroid Build Coastguard Worker }
553*1208bc7eSAndroid Build Coastguard Worker
554*1208bc7eSAndroid Build Coastguard Worker void
tcache_stats_merge(tsdn_t * tsdn,tcache_t * tcache,arena_t * arena)555*1208bc7eSAndroid Build Coastguard Worker tcache_stats_merge(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena) {
556*1208bc7eSAndroid Build Coastguard Worker #if defined(ANDROID_ENABLE_TCACHE_STATS)
557*1208bc7eSAndroid Build Coastguard Worker unsigned i;
558*1208bc7eSAndroid Build Coastguard Worker
559*1208bc7eSAndroid Build Coastguard Worker cassert(config_stats);
560*1208bc7eSAndroid Build Coastguard Worker
561*1208bc7eSAndroid Build Coastguard Worker /* Merge and reset tcache stats. */
562*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < NBINS; i++) {
563*1208bc7eSAndroid Build Coastguard Worker bin_t *bin = &arena->bins[i];
564*1208bc7eSAndroid Build Coastguard Worker cache_bin_t *tbin = tcache_small_bin_get(tcache, i);
565*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsdn, &bin->lock);
566*1208bc7eSAndroid Build Coastguard Worker bin->stats.nrequests += tbin->tstats.nrequests;
567*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsdn, &bin->lock);
568*1208bc7eSAndroid Build Coastguard Worker tbin->tstats.nrequests = 0;
569*1208bc7eSAndroid Build Coastguard Worker }
570*1208bc7eSAndroid Build Coastguard Worker
571*1208bc7eSAndroid Build Coastguard Worker for (; i < nhbins; i++) {
572*1208bc7eSAndroid Build Coastguard Worker cache_bin_t *tbin = tcache_large_bin_get(tcache, i);
573*1208bc7eSAndroid Build Coastguard Worker arena_stats_large_nrequests_add(tsdn, &arena->stats, i,
574*1208bc7eSAndroid Build Coastguard Worker tbin->tstats.nrequests);
575*1208bc7eSAndroid Build Coastguard Worker tbin->tstats.nrequests = 0;
576*1208bc7eSAndroid Build Coastguard Worker }
577*1208bc7eSAndroid Build Coastguard Worker #endif
578*1208bc7eSAndroid Build Coastguard Worker }
579*1208bc7eSAndroid Build Coastguard Worker
580*1208bc7eSAndroid Build Coastguard Worker static bool
tcaches_create_prep(tsd_t * tsd)581*1208bc7eSAndroid Build Coastguard Worker tcaches_create_prep(tsd_t *tsd) {
582*1208bc7eSAndroid Build Coastguard Worker bool err;
583*1208bc7eSAndroid Build Coastguard Worker
584*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);
585*1208bc7eSAndroid Build Coastguard Worker
586*1208bc7eSAndroid Build Coastguard Worker if (tcaches == NULL) {
587*1208bc7eSAndroid Build Coastguard Worker tcaches = base_alloc(tsd_tsdn(tsd), b0get(), sizeof(tcache_t *)
588*1208bc7eSAndroid Build Coastguard Worker * (MALLOCX_TCACHE_MAX+1), CACHELINE);
589*1208bc7eSAndroid Build Coastguard Worker if (tcaches == NULL) {
590*1208bc7eSAndroid Build Coastguard Worker err = true;
591*1208bc7eSAndroid Build Coastguard Worker goto label_return;
592*1208bc7eSAndroid Build Coastguard Worker }
593*1208bc7eSAndroid Build Coastguard Worker }
594*1208bc7eSAndroid Build Coastguard Worker
595*1208bc7eSAndroid Build Coastguard Worker if (tcaches_avail == NULL && tcaches_past > MALLOCX_TCACHE_MAX) {
596*1208bc7eSAndroid Build Coastguard Worker err = true;
597*1208bc7eSAndroid Build Coastguard Worker goto label_return;
598*1208bc7eSAndroid Build Coastguard Worker }
599*1208bc7eSAndroid Build Coastguard Worker
600*1208bc7eSAndroid Build Coastguard Worker err = false;
601*1208bc7eSAndroid Build Coastguard Worker label_return:
602*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);
603*1208bc7eSAndroid Build Coastguard Worker return err;
604*1208bc7eSAndroid Build Coastguard Worker }
605*1208bc7eSAndroid Build Coastguard Worker
606*1208bc7eSAndroid Build Coastguard Worker bool
tcaches_create(tsd_t * tsd,unsigned * r_ind)607*1208bc7eSAndroid Build Coastguard Worker tcaches_create(tsd_t *tsd, unsigned *r_ind) {
608*1208bc7eSAndroid Build Coastguard Worker witness_assert_depth(tsdn_witness_tsdp_get(tsd_tsdn(tsd)), 0);
609*1208bc7eSAndroid Build Coastguard Worker
610*1208bc7eSAndroid Build Coastguard Worker bool err;
611*1208bc7eSAndroid Build Coastguard Worker
612*1208bc7eSAndroid Build Coastguard Worker if (tcaches_create_prep(tsd)) {
613*1208bc7eSAndroid Build Coastguard Worker err = true;
614*1208bc7eSAndroid Build Coastguard Worker goto label_return;
615*1208bc7eSAndroid Build Coastguard Worker }
616*1208bc7eSAndroid Build Coastguard Worker
617*1208bc7eSAndroid Build Coastguard Worker tcache_t *tcache = tcache_create_explicit(tsd);
618*1208bc7eSAndroid Build Coastguard Worker if (tcache == NULL) {
619*1208bc7eSAndroid Build Coastguard Worker err = true;
620*1208bc7eSAndroid Build Coastguard Worker goto label_return;
621*1208bc7eSAndroid Build Coastguard Worker }
622*1208bc7eSAndroid Build Coastguard Worker
623*1208bc7eSAndroid Build Coastguard Worker tcaches_t *elm;
624*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);
625*1208bc7eSAndroid Build Coastguard Worker if (tcaches_avail != NULL) {
626*1208bc7eSAndroid Build Coastguard Worker elm = tcaches_avail;
627*1208bc7eSAndroid Build Coastguard Worker tcaches_avail = tcaches_avail->next;
628*1208bc7eSAndroid Build Coastguard Worker elm->tcache = tcache;
629*1208bc7eSAndroid Build Coastguard Worker *r_ind = (unsigned)(elm - tcaches);
630*1208bc7eSAndroid Build Coastguard Worker } else {
631*1208bc7eSAndroid Build Coastguard Worker elm = &tcaches[tcaches_past];
632*1208bc7eSAndroid Build Coastguard Worker elm->tcache = tcache;
633*1208bc7eSAndroid Build Coastguard Worker *r_ind = tcaches_past;
634*1208bc7eSAndroid Build Coastguard Worker tcaches_past++;
635*1208bc7eSAndroid Build Coastguard Worker }
636*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);
637*1208bc7eSAndroid Build Coastguard Worker
638*1208bc7eSAndroid Build Coastguard Worker err = false;
639*1208bc7eSAndroid Build Coastguard Worker label_return:
640*1208bc7eSAndroid Build Coastguard Worker witness_assert_depth(tsdn_witness_tsdp_get(tsd_tsdn(tsd)), 0);
641*1208bc7eSAndroid Build Coastguard Worker return err;
642*1208bc7eSAndroid Build Coastguard Worker }
643*1208bc7eSAndroid Build Coastguard Worker
644*1208bc7eSAndroid Build Coastguard Worker static tcache_t *
tcaches_elm_remove(tsd_t * tsd,tcaches_t * elm)645*1208bc7eSAndroid Build Coastguard Worker tcaches_elm_remove(tsd_t *tsd, tcaches_t *elm) {
646*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_assert_owner(tsd_tsdn(tsd), &tcaches_mtx);
647*1208bc7eSAndroid Build Coastguard Worker
648*1208bc7eSAndroid Build Coastguard Worker if (elm->tcache == NULL) {
649*1208bc7eSAndroid Build Coastguard Worker return NULL;
650*1208bc7eSAndroid Build Coastguard Worker }
651*1208bc7eSAndroid Build Coastguard Worker tcache_t *tcache = elm->tcache;
652*1208bc7eSAndroid Build Coastguard Worker elm->tcache = NULL;
653*1208bc7eSAndroid Build Coastguard Worker return tcache;
654*1208bc7eSAndroid Build Coastguard Worker }
655*1208bc7eSAndroid Build Coastguard Worker
656*1208bc7eSAndroid Build Coastguard Worker void
tcaches_flush(tsd_t * tsd,unsigned ind)657*1208bc7eSAndroid Build Coastguard Worker tcaches_flush(tsd_t *tsd, unsigned ind) {
658*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);
659*1208bc7eSAndroid Build Coastguard Worker tcache_t *tcache = tcaches_elm_remove(tsd, &tcaches[ind]);
660*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);
661*1208bc7eSAndroid Build Coastguard Worker if (tcache != NULL) {
662*1208bc7eSAndroid Build Coastguard Worker tcache_destroy(tsd, tcache, false);
663*1208bc7eSAndroid Build Coastguard Worker }
664*1208bc7eSAndroid Build Coastguard Worker }
665*1208bc7eSAndroid Build Coastguard Worker
666*1208bc7eSAndroid Build Coastguard Worker void
tcaches_destroy(tsd_t * tsd,unsigned ind)667*1208bc7eSAndroid Build Coastguard Worker tcaches_destroy(tsd_t *tsd, unsigned ind) {
668*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);
669*1208bc7eSAndroid Build Coastguard Worker tcaches_t *elm = &tcaches[ind];
670*1208bc7eSAndroid Build Coastguard Worker tcache_t *tcache = tcaches_elm_remove(tsd, elm);
671*1208bc7eSAndroid Build Coastguard Worker elm->next = tcaches_avail;
672*1208bc7eSAndroid Build Coastguard Worker tcaches_avail = elm;
673*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);
674*1208bc7eSAndroid Build Coastguard Worker if (tcache != NULL) {
675*1208bc7eSAndroid Build Coastguard Worker tcache_destroy(tsd, tcache, false);
676*1208bc7eSAndroid Build Coastguard Worker }
677*1208bc7eSAndroid Build Coastguard Worker }
678*1208bc7eSAndroid Build Coastguard Worker
679*1208bc7eSAndroid Build Coastguard Worker bool
tcache_boot(tsdn_t * tsdn)680*1208bc7eSAndroid Build Coastguard Worker tcache_boot(tsdn_t *tsdn) {
681*1208bc7eSAndroid Build Coastguard Worker /* If necessary, clamp opt_lg_tcache_max. */
682*1208bc7eSAndroid Build Coastguard Worker if (opt_lg_tcache_max < 0 || (ZU(1) << opt_lg_tcache_max) <
683*1208bc7eSAndroid Build Coastguard Worker SMALL_MAXCLASS) {
684*1208bc7eSAndroid Build Coastguard Worker tcache_maxclass = SMALL_MAXCLASS;
685*1208bc7eSAndroid Build Coastguard Worker } else {
686*1208bc7eSAndroid Build Coastguard Worker tcache_maxclass = (ZU(1) << opt_lg_tcache_max);
687*1208bc7eSAndroid Build Coastguard Worker }
688*1208bc7eSAndroid Build Coastguard Worker
689*1208bc7eSAndroid Build Coastguard Worker if (malloc_mutex_init(&tcaches_mtx, "tcaches", WITNESS_RANK_TCACHES,
690*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_rank_exclusive)) {
691*1208bc7eSAndroid Build Coastguard Worker return true;
692*1208bc7eSAndroid Build Coastguard Worker }
693*1208bc7eSAndroid Build Coastguard Worker
694*1208bc7eSAndroid Build Coastguard Worker nhbins = sz_size2index(tcache_maxclass) + 1;
695*1208bc7eSAndroid Build Coastguard Worker
696*1208bc7eSAndroid Build Coastguard Worker /* Initialize tcache_bin_info. */
697*1208bc7eSAndroid Build Coastguard Worker tcache_bin_info = (cache_bin_info_t *)base_alloc(tsdn, b0get(), nhbins
698*1208bc7eSAndroid Build Coastguard Worker * sizeof(cache_bin_info_t), CACHELINE);
699*1208bc7eSAndroid Build Coastguard Worker if (tcache_bin_info == NULL) {
700*1208bc7eSAndroid Build Coastguard Worker return true;
701*1208bc7eSAndroid Build Coastguard Worker }
702*1208bc7eSAndroid Build Coastguard Worker stack_nelms = 0;
703*1208bc7eSAndroid Build Coastguard Worker unsigned i;
704*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < NBINS; i++) {
705*1208bc7eSAndroid Build Coastguard Worker if ((bin_infos[i].nregs << 1) <= TCACHE_NSLOTS_SMALL_MIN) {
706*1208bc7eSAndroid Build Coastguard Worker tcache_bin_info[i].ncached_max =
707*1208bc7eSAndroid Build Coastguard Worker TCACHE_NSLOTS_SMALL_MIN;
708*1208bc7eSAndroid Build Coastguard Worker } else if ((bin_infos[i].nregs << 1) <=
709*1208bc7eSAndroid Build Coastguard Worker TCACHE_NSLOTS_SMALL_MAX) {
710*1208bc7eSAndroid Build Coastguard Worker tcache_bin_info[i].ncached_max =
711*1208bc7eSAndroid Build Coastguard Worker (bin_infos[i].nregs << 1);
712*1208bc7eSAndroid Build Coastguard Worker } else {
713*1208bc7eSAndroid Build Coastguard Worker tcache_bin_info[i].ncached_max =
714*1208bc7eSAndroid Build Coastguard Worker TCACHE_NSLOTS_SMALL_MAX;
715*1208bc7eSAndroid Build Coastguard Worker }
716*1208bc7eSAndroid Build Coastguard Worker stack_nelms += tcache_bin_info[i].ncached_max;
717*1208bc7eSAndroid Build Coastguard Worker }
718*1208bc7eSAndroid Build Coastguard Worker for (; i < nhbins; i++) {
719*1208bc7eSAndroid Build Coastguard Worker tcache_bin_info[i].ncached_max = TCACHE_NSLOTS_LARGE;
720*1208bc7eSAndroid Build Coastguard Worker stack_nelms += tcache_bin_info[i].ncached_max;
721*1208bc7eSAndroid Build Coastguard Worker }
722*1208bc7eSAndroid Build Coastguard Worker
723*1208bc7eSAndroid Build Coastguard Worker return false;
724*1208bc7eSAndroid Build Coastguard Worker }
725*1208bc7eSAndroid Build Coastguard Worker
726*1208bc7eSAndroid Build Coastguard Worker void
tcache_prefork(tsdn_t * tsdn)727*1208bc7eSAndroid Build Coastguard Worker tcache_prefork(tsdn_t *tsdn) {
728*1208bc7eSAndroid Build Coastguard Worker if (!config_prof && opt_tcache) {
729*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_prefork(tsdn, &tcaches_mtx);
730*1208bc7eSAndroid Build Coastguard Worker }
731*1208bc7eSAndroid Build Coastguard Worker }
732*1208bc7eSAndroid Build Coastguard Worker
733*1208bc7eSAndroid Build Coastguard Worker void
tcache_postfork_parent(tsdn_t * tsdn)734*1208bc7eSAndroid Build Coastguard Worker tcache_postfork_parent(tsdn_t *tsdn) {
735*1208bc7eSAndroid Build Coastguard Worker if (!config_prof && opt_tcache) {
736*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_parent(tsdn, &tcaches_mtx);
737*1208bc7eSAndroid Build Coastguard Worker }
738*1208bc7eSAndroid Build Coastguard Worker }
739*1208bc7eSAndroid Build Coastguard Worker
740*1208bc7eSAndroid Build Coastguard Worker void
tcache_postfork_child(tsdn_t * tsdn)741*1208bc7eSAndroid Build Coastguard Worker tcache_postfork_child(tsdn_t *tsdn) {
742*1208bc7eSAndroid Build Coastguard Worker if (!config_prof && opt_tcache) {
743*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_postfork_child(tsdn, &tcaches_mtx);
744*1208bc7eSAndroid Build Coastguard Worker }
745*1208bc7eSAndroid Build Coastguard Worker }
746