summaryrefslogtreecommitdiff
path: root/btree/c_page.c
blob: 53482c51792e67cf88896a70c52f1e7478eb41df (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2008 WiredTiger Software.
 *	All rights reserved.
 *
 * $Id$
 */

#include "wt_internal.h"

static int __wt_cache_clean(WT_STOC *, u_int32_t, WT_PAGE **);
static int __wt_cache_write(WT_STOC *, WT_PAGE *);

#ifdef HAVE_DIAGNOSTIC
static int __wt_cache_dump(WT_STOC *, char *, FILE *);
#endif

/*
 * __wt_cache_open --
 *	Open an underlying database file in a cache.
 */
int
__wt_cache_open(DB *db)
{
	WT_STOC *stoc;
	ENV *env;
	IDB *idb;
	u_int32_t i;

	env = db->env;
	idb = db->idb;
	stoc = idb->stoc;

	/*
	 * Initialize the cache page queues.  Size for a cache filled with
	 * 16KB pages, and 8 pages per bucket (which works out to 8 buckets
	 * per MB).
	 */
	stoc->hashsize = __wt_prime(env->cachesize * 8);
	WT_RET((__wt_calloc(env,
	    stoc->hashsize, sizeof(stoc->hqh[0]), &stoc->hqh)));
	for (i = 0; i < stoc->hashsize; ++i)
		TAILQ_INIT(&stoc->hqh[i]);
	TAILQ_INIT(&stoc->lqh);

	/* Open the fle. */
	return (__wt_open(env, idb->dbname, idb->mode,
	    F_ISSET(idb, WT_CREATE) ? WT_CREATE : 0, &idb->fh));
}

/*
 * __wt_cache_close --
 *	Close an underlying database file in a cache.
 */
int
__wt_cache_close(DB *db)
{
	ENV *env;
	IDB *idb;
	WT_PAGE *page;
	WT_STOC *stoc;
	int ret;

	env = db->env;
	idb = db->idb;
	stoc = idb->stoc; 
	ret = 0;

	/*
	 * BUG:
	 * Walk the list of STOCs, find all of them associated with this
	 * DB handle and close them.
	 */

	/* Write any modified pages, discard pages. */
	while ((page = TAILQ_FIRST(&stoc->lqh)) != NULL) {
		/* There shouldn't be any pinned pages. */
		WT_ASSERT(env, page->ref == 0);

		if (F_ISSET(page, WT_MODIFIED))
			WT_TRET((__wt_cache_write(stoc, page)));
		WT_TRET((__wt_cache_discard(stoc, page)));
	}

	/* There shouldn't be any allocated bytes. */
	WT_ASSERT(env, stoc->cache_bytes == 0);

	/* Close the underlying file handle. */
	WT_TRET((__wt_close(env, idb->fh)));
	idb->fh = NULL;

	/* Discard buckets. */
	WT_FREE_AND_CLEAR(env, stoc->hqh);
		
	return (ret);
}

/*
 * __wt_cache_db_sync --
 *	Flush an underlying database file to disk.
 */
int
__wt_cache_sync(DB *db)
{
	ENV *env;
	WT_PAGE *page;
	WT_STOC *stoc;

	env = db->env;
	stoc = db->idb->stoc;

	/* Write any modified pages. */
	TAILQ_FOREACH(page, &stoc->lqh, q) {
		/* There shouldn't be any pinned pages. */
		WT_ASSERT(env, page->ref == 0);

		if (F_ISSET(page, WT_MODIFIED))
			WT_RET((__wt_cache_write(stoc, page)));
	}
	return (0);
}

/*
 * __wt_cache_stoc_lru --
 *	Discard cache pages if we're holding too much memory.
 */
int
__wt_cache_stoc_lru(WT_STOC *stoc, ENV *env)
{
	WT_PAGE *page;

	/* Discard pages until we're below our threshold. */
	TAILQ_FOREACH(page, &stoc->lqh, q) {
		if (stoc->cache_bytes <= env->cachesize * WT_MEGABYTE)
			break;
		if (page->ref == 0) {
			if (F_ISSET(page, WT_MODIFIED))
				WT_RET((__wt_cache_write(stoc, page)));
			WT_RET((__wt_cache_discard(stoc, page)));
		}
	}
	return (0);
}

/*
 * WT_PAGE_ALLOC --
 *	Allocate memory for the in-memory page information and for the page
 *	itself.	 They're two separate allocation calls so we (hopefully) get
 *	better alignment from the underlying heap memory allocator.
 *
 *	Clear the memory because code depends on initial values of 0.
 */
#define	WT_PAGE_ALLOC(env, stoc, bytes, page) do {			\
	int __ret;							\
	if ((stoc)->cache_bytes > (env)->cachesize * WT_MEGABYTE)	\
		WT_RET((__wt_cache_stoc_lru(stoc, env)));		\
	WT_RET((__wt_calloc((env), 1, sizeof(WT_PAGE), &(page))));	\
	if (((__ret) = __wt_calloc(					\
	    (env), 1, (size_t)(bytes), &(page)->hdr)) != 0) {		\
		__wt_free((env), (page));				\
		return ((__ret));					\
	}								\
	(stoc)->cache_bytes += (bytes);					\
	WT_STAT_INCR((env)->hstats, CACHE_CLEAN, NULL);			\
} while (0)

/*
 * __wt_cache_alloc --
 *	Allocate bytes from a file.
 */
int
__wt_cache_alloc(WT_STOC *stoc, u_int32_t bytes, WT_PAGE **pagep)
{
	DB *db;
	ENV *env;
	IDB *idb;
	WT_PAGE *page;

	*pagep = NULL;

	idb = stoc->idb;
	db = idb->db;
	env = db->env;

	WT_ASSERT(env, bytes % WT_FRAGMENT == 0);

	WT_PAGE_ALLOC(env, stoc, bytes, page);

	WT_STAT_INCR(env->hstats, CACHE_ALLOC, "pages allocated in the cache");
	WT_STAT_INCR(
	    db->hstats, DB_CACHE_ALLOC, "pages allocated in the cache");

	/* Initialize the page. */
	page->offset = idb->fh->file_size;
	page->addr = WT_OFF_TO_ADDR(db, page->offset);
	page->bytes = bytes;
	page->ref = 1;
	TAILQ_INSERT_TAIL(&stoc->lqh, page, q);
	TAILQ_INSERT_HEAD(&stoc->hqh[WT_HASH(stoc, page->offset)], page, hq);

	idb->fh->file_size += bytes;

	*pagep = page;
	return (0);
}

/*
 * __wt_cache_in --
 *	Pin bytes of a file, reading as necessary.
 */
int
__wt_cache_in(WT_STOC *stoc,
    off_t offset, u_int32_t bytes, u_int32_t flags, WT_PAGE **pagep)
{
	DB *db;
	ENV *env;
	IDB *idb;
	WT_PAGE *page;
	WT_PAGE_HDR *hdr;
	WT_PAGE_HQH *hashq;
	int ret;

	*pagep = NULL;

	idb = stoc->idb;
	db = idb->db;
	env = db->env;

	WT_ASSERT(env, bytes % WT_FRAGMENT == 0);
	WT_ENV_FCHK(env, "__wt_cache_in", flags, WT_APIMASK_WT_CACHE_IN);

	/* Check for the page in the cache. */
	hashq = &stoc->hqh[WT_HASH(stoc, offset)];
	TAILQ_FOREACH(page, hashq, hq)
		if (page->offset == offset)
			break;
	if (page != NULL) {
		++page->ref;

		/* Move to the head of the hash queue */
		TAILQ_REMOVE(hashq, page, hq);
		TAILQ_INSERT_HEAD(hashq, page, hq);

		/* Move to the tail of the LRU queue. */
		TAILQ_REMOVE(&stoc->lqh, page, q);
		TAILQ_INSERT_TAIL(&stoc->lqh, page, q);

		WT_STAT_INCR(env->hstats,
		    CACHE_HIT, "cache hit: reads found in the cache");
		WT_STAT_INCR(db->hstats,
		    DB_CACHE_HIT, "cache hit: reads found in the cache");

		*pagep = page;
		return (0);
	}

	WT_STAT_INCR(env->hstats,
	    CACHE_MISS, "cache miss: reads not found in the cache");
	WT_STAT_INCR(db->hstats,
	    DB_CACHE_MISS, "cache miss: reads not found in the cache");

	WT_PAGE_ALLOC(env, stoc, bytes, page);

	/* Initialize the page. */
	page->offset = offset;
	page->addr = WT_OFF_TO_ADDR(db, offset);
	page->bytes = bytes;
	page->ref = 1;
	TAILQ_INSERT_TAIL(&stoc->lqh, page, q);
	TAILQ_INSERT_HEAD(hashq, page, hq);

	/* Read the page. */
	WT_ERR((__wt_read(env, idb->fh, offset, bytes, page->hdr)));

	/* Verify the checksum. */
	if (!LF_ISSET(WT_UNFORMATTED)) {
		hdr = page->hdr;
		u_int32_t checksum = hdr->checksum;
		hdr->checksum = 0;
		if (checksum != __wt_cksum(hdr, bytes)) {
			__wt_db_errx(db,
			    "file offset %llu with length %lu was read and "
			    "had a checksum error",
			    (u_quad)offset, (u_long)bytes);
			ret = WT_ERROR;
			goto err;
		}
	}

	*pagep = page;
	return (0);

err:	(void)__wt_cache_discard(stoc, page);
	return (ret);
}

/*
 * __wt_cache_out --
 *	Unpin bytes of a file, writing as necessary.
 */
int
__wt_cache_out(WT_STOC *stoc, WT_PAGE *page, u_int32_t flags)
{
	ENV *env;

	env = stoc->idb->db->env;

	WT_ENV_FCHK(env, "__wt_cache_out", flags, WT_APIMASK_WT_CACHE_OUT);

	/* Check and decrement the reference count. */
	WT_ASSERT(env, page->ref > 0);
	--page->ref;

	/* Set modify flag, and update clean/dirty statistics. */
	if (LF_ISSET(WT_MODIFIED) && !F_ISSET(page, WT_MODIFIED)) {
		F_SET(page, WT_MODIFIED);

		WT_STAT_INCR(
		    env->hstats, CACHE_DIRTY, "dirty pages in the cache");
		WT_STAT_DECR(
		    env->hstats, CACHE_CLEAN, "clean pages in the cache");
	}

	/*
	 * If the page isn't to be retained in the cache, write it if it's
	 * dirty, and discard it.
	 */
	if (LF_ISSET(WT_UNFORMATTED)) {
		if (F_ISSET(page, WT_MODIFIED))
			WT_RET((__wt_cache_write(stoc, page)));
		WT_RET((__wt_cache_discard(stoc, page)));
	}

	return (0);
}

/*
 * __wt_cache_clean --
 *	Clear some space out of the cache.
 */
static int
__wt_cache_clean(WT_STOC *stoc, u_int32_t bytes, WT_PAGE **pagep)
{
	ENV *env;
	WT_PAGE *page;
	WT_PAGE_HDR *hdr;
	u_int64_t bytes_free, bytes_need_free;

	*pagep = NULL;

	env = stoc->idb->db->env;
	bytes_free = 0;
	bytes_need_free = bytes * 3;

	do {
		TAILQ_FOREACH(page, &stoc->lqh, q)
			if (page->ref == 0)
				break;
		if (page == NULL)
			break;

		/* Write the page if it's been modified. */
		if (F_ISSET(page, WT_MODIFIED)) {
			WT_STAT_INCR(env->hstats, CACHE_WRITE_EVICT,
			    "dirty pages evicted from the cache");

			WT_RET((__wt_cache_write(stoc, page)));
		} else
			WT_STAT_INCR(env->hstats, CACHE_EVICT,
			    "clean pages evicted from the cache");

		/* Return the page if it's the right size. */
		if (page->bytes == bytes) {
			TAILQ_REMOVE(
			    &stoc->hqh[WT_HASH(stoc, page->offset)], page, hq);
			TAILQ_REMOVE(&stoc->lqh, page, q);

			/* Clear the page. */
			__wt_bt_page_recycle(env, page);
			hdr = page->hdr;
			memset(hdr, 0, (size_t)bytes);
			memset(page, 0, sizeof(WT_PAGE));
			page->hdr = hdr;
			page->bytes = bytes;

			*pagep = page;
			return (0);
		}

		bytes_free += page->bytes;

		/* Discard the page. */
		WT_RET((__wt_cache_discard(stoc, page)));
	} while (bytes_free < bytes_need_free);

	return (0);
}

/*
 * __wt_cache_write --
 *	Write a page to the backing database file.
 */
static int
__wt_cache_write(WT_STOC *stoc, WT_PAGE *page)
{
	ENV *env;
	IDB *idb;
	WT_PAGE_HDR *hdr;

	idb = stoc->idb;
	env = idb->db->env;

	WT_STAT_INCR(env->hstats, CACHE_WRITE, "writes from the cache");

	/* Update the checksum. */
	hdr = page->hdr;
	hdr->checksum = 0;
	hdr->checksum = __wt_cksum(hdr, page->bytes);

	/* Write, and if successful, clear the modified flag. */
	WT_RET((__wt_write(env, idb->fh, page->offset, page->bytes, hdr)));

	F_CLR(page, WT_MODIFIED);

	WT_STAT_DECR(env->hstats, CACHE_DIRTY, NULL);
	WT_STAT_INCR(env->hstats, CACHE_CLEAN, NULL);

	return (0);
}

/*
 * __wt_cache_discard --
 *	Discard a page of a file.
 */
int
__wt_cache_discard(WT_STOC *stoc, WT_PAGE *page)
{
	ENV *env;

	env = stoc->idb->db->env;

	WT_ASSERT(env, page->ref == 0);

	WT_ASSERT(env, stoc->cache_bytes >= page->bytes);
	stoc->cache_bytes -= page->bytes;

	TAILQ_REMOVE(&stoc->hqh[WT_HASH(stoc, page->addr)], page, hq);
	TAILQ_REMOVE(&stoc->lqh, page, q);

	__wt_bt_page_recycle(env, page);

	WT_STAT_DECR(env->hstats, CACHE_CLEAN, NULL);

	__wt_free(env, page->hdr);
	__wt_free(env, page);
	return (0);
}

#ifdef HAVE_DIAGNOSTIC
/*
 * __wt_cache_dump --
 *	Dump a STOC's hash and LRU queues.
 */
static int
__wt_cache_dump(WT_STOC *stoc, char *ofile, FILE *fp)
{
	WT_PAGE *page;
	WT_PAGE_HQH *hashq;
	int bucket_empty, do_close, i;
	char *sep;

	/* Optionally dump to a file, else to a stream, default to stdout. */
	do_close = 0;
	if (ofile != NULL) {
		if ((fp = fopen(ofile, "w")) == NULL)
			return (WT_ERROR);
		do_close = 1;
	} else if (fp == NULL)
		fp = stdout;

	fprintf(fp, "LRU: ");
	sep = "";
	TAILQ_FOREACH(page, &stoc->lqh, q) {
		fprintf(fp, "%s%lu", sep, (u_long)page->addr);
		sep = ", ";
	}
	fprintf(fp, "\n");
	for (i = 0; i < stoc->hashsize; ++i) {
		sep = "";
		bucket_empty = 1;
		hashq = &stoc->hqh[i];
		TAILQ_FOREACH(page, hashq, hq) {
			if (bucket_empty) {
				fprintf(fp, "hash bucket %d: ", i);
				bucket_empty = 0;
			}
			fprintf(fp, "%s%lu", sep, (u_long)page->addr);
			sep = ", ";
		}
		if (!bucket_empty)
			fprintf(fp, "\n");
	}

	if (do_close)
		(void)fclose(fp);

	return (0);
}
#endif