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