summaryrefslogtreecommitdiff
path: root/deps/jemalloc.orig/src/chunk_swap.c
blob: cb25ae0dde22c62a44a578a0f2a182169c1bef87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#define	JEMALLOC_CHUNK_SWAP_C_
#include "jemalloc/internal/jemalloc_internal.h"
#ifdef JEMALLOC_SWAP
/******************************************************************************/
/* Data. */

malloc_mutex_t	swap_mtx;
bool		swap_enabled;
bool		swap_prezeroed;
size_t		swap_nfds;
int		*swap_fds;
#ifdef JEMALLOC_STATS
size_t		swap_avail;
#endif

/* Base address of the mmap()ed file(s). */
static void	*swap_base;
/* Current end of the space in use (<= swap_max). */
static void	*swap_end;
/* Absolute upper limit on file-backed addresses. */
static void	*swap_max;

/*
 * Trees of chunks that were previously allocated (trees differ only in node
 * ordering).  These are used when allocating chunks, in an attempt to re-use
 * address space.  Depending on function, different tree orderings are needed,
 * which is why there are two trees with the same contents.
 */
static extent_tree_t	swap_chunks_szad;
static extent_tree_t	swap_chunks_ad;

/******************************************************************************/
/* Function prototypes for non-inline static functions. */

static void	*chunk_recycle_swap(size_t size, bool *zero);
static extent_node_t *chunk_dealloc_swap_record(void *chunk, size_t size);

/******************************************************************************/

static void *
chunk_recycle_swap(size_t size, bool *zero)
{
	extent_node_t *node, key;

	key.addr = NULL;
	key.size = size;
	malloc_mutex_lock(&swap_mtx);
	node = extent_tree_szad_nsearch(&swap_chunks_szad, &key);
	if (node != NULL) {
		void *ret = node->addr;

		/* Remove node from the tree. */
		extent_tree_szad_remove(&swap_chunks_szad, node);
		if (node->size == size) {
			extent_tree_ad_remove(&swap_chunks_ad, node);
			base_node_dealloc(node);
		} else {
			/*
			 * Insert the remainder of node's address range as a
			 * smaller chunk.  Its position within swap_chunks_ad
			 * does not change.
			 */
			assert(node->size > size);
			node->addr = (void *)((uintptr_t)node->addr + size);
			node->size -= size;
			extent_tree_szad_insert(&swap_chunks_szad, node);
		}
#ifdef JEMALLOC_STATS
		swap_avail -= size;
#endif
		malloc_mutex_unlock(&swap_mtx);

		if (*zero)
			memset(ret, 0, size);
		return (ret);
	}
	malloc_mutex_unlock(&swap_mtx);

	return (NULL);
}

void *
chunk_alloc_swap(size_t size, bool *zero)
{
	void *ret;

	assert(swap_enabled);

	ret = chunk_recycle_swap(size, zero);
	if (ret != NULL)
		return (ret);

	malloc_mutex_lock(&swap_mtx);
	if ((uintptr_t)swap_end + size <= (uintptr_t)swap_max) {
		ret = swap_end;
		swap_end = (void *)((uintptr_t)swap_end + size);
#ifdef JEMALLOC_STATS
		swap_avail -= size;
#endif
		malloc_mutex_unlock(&swap_mtx);

		if (swap_prezeroed)
			*zero = true;
		else if (*zero)
			memset(ret, 0, size);
	} else {
		malloc_mutex_unlock(&swap_mtx);
		return (NULL);
	}

	return (ret);
}

static extent_node_t *
chunk_dealloc_swap_record(void *chunk, size_t size)
{
	extent_node_t *xnode, *node, *prev, key;

	xnode = NULL;
	while (true) {
		key.addr = (void *)((uintptr_t)chunk + size);
		node = extent_tree_ad_nsearch(&swap_chunks_ad, &key);
		/* Try to coalesce forward. */
		if (node != NULL && node->addr == key.addr) {
			/*
			 * Coalesce chunk with the following address range.
			 * This does not change the position within
			 * swap_chunks_ad, so only remove/insert from/into
			 * swap_chunks_szad.
			 */
			extent_tree_szad_remove(&swap_chunks_szad, node);
			node->addr = chunk;
			node->size += size;
			extent_tree_szad_insert(&swap_chunks_szad, node);
			break;
		} else if (xnode == NULL) {
			/*
			 * It is possible that base_node_alloc() will cause a
			 * new base chunk to be allocated, so take care not to
			 * deadlock on swap_mtx, and recover if another thread
			 * deallocates an adjacent chunk while this one is busy
			 * allocating xnode.
			 */
			malloc_mutex_unlock(&swap_mtx);
			xnode = base_node_alloc();
			malloc_mutex_lock(&swap_mtx);
			if (xnode == NULL)
				return (NULL);
		} else {
			/* Coalescing forward failed, so insert a new node. */
			node = xnode;
			xnode = NULL;
			node->addr = chunk;
			node->size = size;
			extent_tree_ad_insert(&swap_chunks_ad, node);
			extent_tree_szad_insert(&swap_chunks_szad, node);
			break;
		}
	}
	/* Discard xnode if it ended up unused do to a race. */
	if (xnode != NULL)
		base_node_dealloc(xnode);

	/* Try to coalesce backward. */
	prev = extent_tree_ad_prev(&swap_chunks_ad, node);
	if (prev != NULL && (void *)((uintptr_t)prev->addr + prev->size) ==
	    chunk) {
		/*
		 * Coalesce chunk with the previous address range.  This does
		 * not change the position within swap_chunks_ad, so only
		 * remove/insert node from/into swap_chunks_szad.
		 */
		extent_tree_szad_remove(&swap_chunks_szad, prev);
		extent_tree_ad_remove(&swap_chunks_ad, prev);

		extent_tree_szad_remove(&swap_chunks_szad, node);
		node->addr = prev->addr;
		node->size += prev->size;
		extent_tree_szad_insert(&swap_chunks_szad, node);

		base_node_dealloc(prev);
	}

	return (node);
}

bool
chunk_in_swap(void *chunk)
{
	bool ret;

	assert(swap_enabled);

	malloc_mutex_lock(&swap_mtx);
	if ((uintptr_t)chunk >= (uintptr_t)swap_base
	    && (uintptr_t)chunk < (uintptr_t)swap_max)
		ret = true;
	else
		ret = false;
	malloc_mutex_unlock(&swap_mtx);

	return (ret);
}

bool
chunk_dealloc_swap(void *chunk, size_t size)
{
	bool ret;

	assert(swap_enabled);

	malloc_mutex_lock(&swap_mtx);
	if ((uintptr_t)chunk >= (uintptr_t)swap_base
	    && (uintptr_t)chunk < (uintptr_t)swap_max) {
		extent_node_t *node;

		/* Try to coalesce with other unused chunks. */
		node = chunk_dealloc_swap_record(chunk, size);
		if (node != NULL) {
			chunk = node->addr;
			size = node->size;
		}

		/*
		 * Try to shrink the in-use memory if this chunk is at the end
		 * of the in-use memory.
		 */
		if ((void *)((uintptr_t)chunk + size) == swap_end) {
			swap_end = (void *)((uintptr_t)swap_end - size);

			if (node != NULL) {
				extent_tree_szad_remove(&swap_chunks_szad,
				    node);
				extent_tree_ad_remove(&swap_chunks_ad, node);
				base_node_dealloc(node);
			}
		} else
			madvise(chunk, size, MADV_DONTNEED);

#ifdef JEMALLOC_STATS
		swap_avail += size;
#endif
		ret = false;
		goto RETURN;
	}

	ret = true;
RETURN:
	malloc_mutex_unlock(&swap_mtx);
	return (ret);
}

bool
chunk_swap_enable(const int *fds, unsigned nfds, bool prezeroed)
{
	bool ret;
	unsigned i;
	off_t off;
	void *vaddr;
	size_t cumsize, voff;
	size_t sizes[nfds];

	malloc_mutex_lock(&swap_mtx);

	/* Get file sizes. */
	for (i = 0, cumsize = 0; i < nfds; i++) {
		off = lseek(fds[i], 0, SEEK_END);
		if (off == ((off_t)-1)) {
			ret = true;
			goto RETURN;
		}
		if (PAGE_CEILING(off) != off) {
			/* Truncate to a multiple of the page size. */
			off &= ~PAGE_MASK;
			if (ftruncate(fds[i], off) != 0) {
				ret = true;
				goto RETURN;
			}
		}
		sizes[i] = off;
		if (cumsize + off < cumsize) {
			/*
			 * Cumulative file size is greater than the total
			 * address space.  Bail out while it's still obvious
			 * what the problem is.
			 */
			ret = true;
			goto RETURN;
		}
		cumsize += off;
	}

	/* Round down to a multiple of the chunk size. */
	cumsize &= ~chunksize_mask;
	if (cumsize == 0) {
		ret = true;
		goto RETURN;
	}

	/*
	 * Allocate a chunk-aligned region of anonymous memory, which will
	 * be the final location for the memory-mapped files.
	 */
	vaddr = chunk_alloc_mmap_noreserve(cumsize);
	if (vaddr == NULL) {
		ret = true;
		goto RETURN;
	}

	/* Overlay the files onto the anonymous mapping. */
	for (i = 0, voff = 0; i < nfds; i++) {
		void *addr = mmap((void *)((uintptr_t)vaddr + voff), sizes[i],
		    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fds[i], 0);
		if (addr == MAP_FAILED) {
			char buf[BUFERROR_BUF];


			buferror(errno, buf, sizeof(buf));
			malloc_write(
			    "<jemalloc>: Error in mmap(..., MAP_FIXED, ...): ");
			malloc_write(buf);
			malloc_write("\n");
			if (opt_abort)
				abort();
			if (munmap(vaddr, voff) == -1) {
				buferror(errno, buf, sizeof(buf));
				malloc_write("<jemalloc>: Error in munmap(): ");
				malloc_write(buf);
				malloc_write("\n");
			}
			ret = true;
			goto RETURN;
		}
		assert(addr == (void *)((uintptr_t)vaddr + voff));

		/*
		 * Tell the kernel that the mapping will be accessed randomly,
		 * and that it should not gratuitously sync pages to the
		 * filesystem.
		 */
#ifdef MADV_RANDOM
		madvise(addr, sizes[i], MADV_RANDOM);
#endif
#ifdef MADV_NOSYNC
		madvise(addr, sizes[i], MADV_NOSYNC);
#endif

		voff += sizes[i];
	}

	swap_prezeroed = prezeroed;
	swap_base = vaddr;
	swap_end = swap_base;
	swap_max = (void *)((uintptr_t)vaddr + cumsize);

	/* Copy the fds array for mallctl purposes. */
	swap_fds = (int *)base_alloc(nfds * sizeof(int));
	if (swap_fds == NULL) {
		ret = true;
		goto RETURN;
	}
	memcpy(swap_fds, fds, nfds * sizeof(int));
	swap_nfds = nfds;

#ifdef JEMALLOC_STATS
	swap_avail = cumsize;
#endif

	swap_enabled = true;

	ret = false;
RETURN:
	malloc_mutex_unlock(&swap_mtx);
	return (ret);
}

bool
chunk_swap_boot(void)
{

	if (malloc_mutex_init(&swap_mtx))
		return (true);

	swap_enabled = false;
	swap_prezeroed = false; /* swap.* mallctl's depend on this. */
	swap_nfds = 0;
	swap_fds = NULL;
#ifdef JEMALLOC_STATS
	swap_avail = 0;
#endif
	swap_base = NULL;
	swap_end = NULL;
	swap_max = NULL;

	extent_tree_szad_new(&swap_chunks_szad);
	extent_tree_ad_new(&swap_chunks_ad);

	return (false);
}

/******************************************************************************/
#endif /* JEMALLOC_SWAP */