summaryrefslogtreecommitdiff
path: root/src/block/block_session.c
blob: 268adb530cf56a5607c33c0b01131a3d70132609 (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
/*-
 * Copyright (c) 2014-2016 MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * Per session handle cached block manager information.
 */
typedef struct {
	WT_EXT  *ext_cache;			/* List of WT_EXT handles */
	u_int    ext_cache_cnt;			/* Count */

	WT_SIZE *sz_cache;			/* List of WT_SIZE handles */
	u_int    sz_cache_cnt;			/* Count */
} WT_BLOCK_MGR_SESSION;

/*
 * __block_ext_alloc --
 *	Allocate a new WT_EXT structure.
 */
static int
__block_ext_alloc(WT_SESSION_IMPL *session, WT_EXT **extp)
{
	WT_EXT *ext;

	u_int skipdepth;

	skipdepth = __wt_skip_choose_depth(session);
	WT_RET(__wt_calloc(session, 1,
	    sizeof(WT_EXT) + skipdepth * 2 * sizeof(WT_EXT *), &ext));
	ext->depth = (uint8_t)skipdepth;
	(*extp) = ext;

	return (0);
}

/*
 * __wt_block_ext_alloc --
 *	Return a WT_EXT structure for use.
 */
int
__wt_block_ext_alloc(WT_SESSION_IMPL *session, WT_EXT **extp)
{
	WT_EXT *ext;
	WT_BLOCK_MGR_SESSION *bms;
	u_int i;

	bms = session->block_manager;

	/* Return a WT_EXT structure for use from a cached list. */
	if (bms != NULL && bms->ext_cache != NULL) {
		ext = bms->ext_cache;
		bms->ext_cache = ext->next[0];

		/* Clear any left-over references. */
		for (i = 0; i < ext->depth; ++i)
			ext->next[i] = ext->next[i + ext->depth] = NULL;

		/*
		 * The count is advisory to minimize our exposure to bugs, but
		 * don't let it go negative.
		 */
		if (bms->ext_cache_cnt > 0)
			--bms->ext_cache_cnt;

		*extp = ext;
		return (0);
	}

	return (__block_ext_alloc(session, extp));
}

/*
 * __block_ext_prealloc --
 *	Pre-allocate WT_EXT structures.
 */
static int
__block_ext_prealloc(WT_SESSION_IMPL *session, u_int max)
{
	WT_BLOCK_MGR_SESSION *bms;
	WT_EXT *ext;

	bms = session->block_manager;

	for (; bms->ext_cache_cnt < max; ++bms->ext_cache_cnt) {
		WT_RET(__block_ext_alloc(session, &ext));

		ext->next[0] = bms->ext_cache;
		bms->ext_cache = ext;
	}
	return (0);
}

/*
 * __wt_block_ext_free --
 *	Add a WT_EXT structure to the cached list.
 */
void
__wt_block_ext_free(WT_SESSION_IMPL *session, WT_EXT *ext)
{
	WT_BLOCK_MGR_SESSION *bms;

	if ((bms = session->block_manager) == NULL)
		__wt_free(session, ext);
	else {
		ext->next[0] = bms->ext_cache;
		bms->ext_cache = ext;

		++bms->ext_cache_cnt;
	}
}

/*
 * __block_ext_discard --
 *	Discard some or all of the WT_EXT structure cache.
 */
static int
__block_ext_discard(WT_SESSION_IMPL *session, u_int max)
{
	WT_BLOCK_MGR_SESSION *bms;
	WT_EXT *ext, *next;

	bms = session->block_manager;
	if (max != 0 && bms->ext_cache_cnt <= max)
		return (0);

	for (ext = bms->ext_cache; ext != NULL;) {
		next = ext->next[0];
		__wt_free(session, ext);
		ext = next;

		--bms->ext_cache_cnt;
		if (max != 0 && bms->ext_cache_cnt <= max)
			break;
	}
	bms->ext_cache = ext;

	if (max == 0 && bms->ext_cache_cnt != 0)
		WT_RET_MSG(session, WT_ERROR,
		    "incorrect count in session handle's block manager cache");
	return (0);
}

/*
 * __block_size_alloc --
 *	Allocate a new WT_SIZE structure.
 */
static int
__block_size_alloc(WT_SESSION_IMPL *session, WT_SIZE **szp)
{
	return (__wt_calloc_one(session, szp));
}

/*
 * __wt_block_size_alloc --
 *	Return a WT_SIZE structure for use.
 */
int
__wt_block_size_alloc(WT_SESSION_IMPL *session, WT_SIZE **szp)
{
	WT_BLOCK_MGR_SESSION *bms;

	bms = session->block_manager;

	/* Return a WT_SIZE structure for use from a cached list. */
	if (bms != NULL && bms->sz_cache != NULL) {
		(*szp) = bms->sz_cache;
		bms->sz_cache = bms->sz_cache->next[0];

		/*
		 * The count is advisory to minimize our exposure to bugs, but
		 * don't let it go negative.
		 */
		if (bms->sz_cache_cnt > 0)
			--bms->sz_cache_cnt;
		return (0);
	}

	return (__block_size_alloc(session, szp));
}

/*
 * __block_size_prealloc --
 *	Pre-allocate WT_SIZE structures.
 */
static int
__block_size_prealloc(WT_SESSION_IMPL *session, u_int max)
{
	WT_BLOCK_MGR_SESSION *bms;
	WT_SIZE *sz;

	bms = session->block_manager;

	for (; bms->sz_cache_cnt < max; ++bms->sz_cache_cnt) {
		WT_RET(__block_size_alloc(session, &sz));

		sz->next[0] = bms->sz_cache;
		bms->sz_cache = sz;
	}
	return (0);
}

/*
 * __wt_block_size_free --
 *	Add a WT_SIZE structure to the cached list.
 */
void
__wt_block_size_free(WT_SESSION_IMPL *session, WT_SIZE *sz)
{
	WT_BLOCK_MGR_SESSION *bms;

	if ((bms = session->block_manager) == NULL)
		__wt_free(session, sz);
	else {
		sz->next[0] = bms->sz_cache;
		bms->sz_cache = sz;

		++bms->sz_cache_cnt;
	}
}

/*
 * __block_size_discard --
 *	Discard some or all of the WT_SIZE structure cache.
 */
static int
__block_size_discard(WT_SESSION_IMPL *session, u_int max)
{
	WT_BLOCK_MGR_SESSION *bms;
	WT_SIZE *sz, *nsz;

	bms = session->block_manager;
	if (max != 0 && bms->sz_cache_cnt <= max)
		return (0);

	for (sz = bms->sz_cache; sz != NULL;) {
		nsz = sz->next[0];
		__wt_free(session, sz);
		sz = nsz;

		--bms->sz_cache_cnt;
		if (max != 0 && bms->sz_cache_cnt <= max)
			break;
	}
	bms->sz_cache = sz;

	if (max == 0 && bms->sz_cache_cnt != 0)
		WT_RET_MSG(session, WT_ERROR,
		    "incorrect count in session handle's block manager cache");
	return (0);
}

/*
 * __block_manager_session_cleanup --
 *	Clean up the session handle's block manager information.
 */
static int
__block_manager_session_cleanup(WT_SESSION_IMPL *session)
{
	WT_DECL_RET;

	if (session->block_manager == NULL)
		return (0);

	WT_TRET(__block_ext_discard(session, 0));
	WT_TRET(__block_size_discard(session, 0));

	__wt_free(session, session->block_manager);

	return (ret);
}

/*
 * __wt_block_ext_prealloc --
 *	Pre-allocate WT_EXT and WT_SIZE structures.
 */
int
__wt_block_ext_prealloc(WT_SESSION_IMPL *session, u_int max)
{
	if (session->block_manager == NULL) {
		WT_RET(__wt_calloc(session, 1,
		    sizeof(WT_BLOCK_MGR_SESSION), &session->block_manager));
		session->block_manager_cleanup =
		    __block_manager_session_cleanup;
	}
	WT_RET(__block_ext_prealloc(session, max));
	WT_RET(__block_size_prealloc(session, max));
	return (0);
}

/*
 * __wt_block_ext_discard --
 *	Discard WT_EXT and WT_SIZE structures after checkpoint runs.
 */
int
__wt_block_ext_discard(WT_SESSION_IMPL *session, u_int max)
{
	WT_RET(__block_ext_discard(session, max));
	WT_RET(__block_size_discard(session, max));
	return (0);
}