~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/*   Innobase relational database engine; Copyright (C) 2001 Innobase Oy
2
3
     This program is free software; you can redistribute it and/or modify
4
     it under the terms of the GNU General Public License 2
5
     as published by the Free Software Foundation in June 1991.
6
7
     This program is distributed in the hope that it will be useful,
8
     but WITHOUT ANY WARRANTY; without even the implied warranty of
9
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
     GNU General Public License for more details.
11
12
     You should have received a copy of the GNU General Public License 2
13
     along with this program (in file COPYING); if not, write to the Free
14
     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
15
/******************************************************
16
The database buffer pool high-level routines
17
18
(c) 1995 Innobase Oy
19
20
Created 11/5/1995 Heikki Tuuri
21
*******************************************************/
22
23
#ifndef buf0buf_h
24
#define buf0buf_h
25
26
#include "univ.i"
27
#include "fil0fil.h"
28
#include "mtr0types.h"
29
#include "buf0types.h"
30
#include "sync0rw.h"
31
#include "hash0hash.h"
32
#include "ut0byte.h"
33
#include "os0proc.h"
34
35
/* Flags for flush types */
36
#define BUF_FLUSH_LRU		1
37
#define BUF_FLUSH_SINGLE_PAGE	2
38
#define BUF_FLUSH_LIST		3	/* An array in the pool struct
39
					has size BUF_FLUSH_LIST + 1: if you
40
					add more flush types, put them in
41
					the middle! */
42
/* Modes for buf_page_get_gen */
43
#define BUF_GET			10	/* get always */
44
#define	BUF_GET_IF_IN_POOL	11	/* get if in pool */
45
#define	BUF_GET_NOWAIT		12	/* get if can set the latch without
46
					waiting */
47
#define BUF_GET_NO_LATCH	14	/* get and bufferfix, but set no latch;
48
					we have separated this case, because
49
					it is error-prone programming not to
50
					set a latch, and it should be used
51
					with care */
52
/* Modes for buf_page_get_known_nowait */
53
#define BUF_MAKE_YOUNG	51
54
#define BUF_KEEP_OLD	52
55
/* Magic value to use instead of checksums when they are disabled */
56
#define BUF_NO_CHECKSUM_MAGIC 0xDEADBEEFUL
57
58
extern buf_pool_t*	buf_pool;	/* The buffer pool of the database */
59
#ifdef UNIV_DEBUG
60
extern ibool		buf_debug_prints;/* If this is set TRUE, the program
61
					prints info whenever read or flush
62
					occurs */
63
#endif /* UNIV_DEBUG */
64
extern ulint srv_buf_pool_write_requests; /* variable to count write request
65
					  issued */
66
67
/************************************************************************
68
Creates the buffer pool. */
69
70
buf_pool_t*
71
buf_pool_init(
72
/*==========*/
73
				/* out, own: buf_pool object, NULL if not
74
				enough memory or error */
75
	ulint	max_size,	/* in: maximum size of the buf_pool in
76
				blocks */
77
	ulint	curr_size,	/* in: current size to use, must be <=
78
				max_size, currently must be equal to
79
				max_size */
80
	ulint	n_frames);	/* in: number of frames; if AWE is used,
81
				this is the size of the address space window
82
				where physical memory pages are mapped; if
83
				AWE is not used then this must be the same
84
				as max_size */
85
/*************************************************************************
86
Gets the current size of buffer buf_pool in bytes. In the case of AWE, the
87
size of AWE window (= the frames). */
88
UNIV_INLINE
89
ulint
90
buf_pool_get_curr_size(void);
91
/*========================*/
92
			/* out: size in bytes */
93
/*************************************************************************
94
Gets the maximum size of buffer pool in bytes. In the case of AWE, the
95
size of AWE window (= the frames). */
96
UNIV_INLINE
97
ulint
98
buf_pool_get_max_size(void);
99
/*=======================*/
100
			/* out: size in bytes */
101
/************************************************************************
102
Gets the smallest oldest_modification lsn for any page in the pool. Returns
103
ut_dulint_zero if all modified pages have been flushed to disk. */
104
UNIV_INLINE
105
dulint
106
buf_pool_get_oldest_modification(void);
107
/*==================================*/
108
				/* out: oldest modification in pool,
109
				ut_dulint_zero if none */
110
/*************************************************************************
111
Allocates a buffer frame. */
112
113
buf_frame_t*
114
buf_frame_alloc(void);
115
/*==================*/
116
				/* out: buffer frame */
117
/*************************************************************************
118
Frees a buffer frame which does not contain a file page. */
119
120
void
121
buf_frame_free(
122
/*===========*/
123
	buf_frame_t*	frame);	/* in: buffer frame */
124
/*************************************************************************
125
Copies contents of a buffer frame to a given buffer. */
126
UNIV_INLINE
127
byte*
128
buf_frame_copy(
129
/*===========*/
130
				/* out: buf */
131
	byte*		buf,	/* in: buffer to copy to */
132
	buf_frame_t*	frame);	/* in: buffer frame */
133
/******************************************************************
134
NOTE! The following macros should be used instead of buf_page_get_gen,
135
to improve debugging. Only values RW_S_LATCH and RW_X_LATCH are allowed
136
in LA! */
137
#define buf_page_get(SP, OF, LA, MTR)	 buf_page_get_gen(\
138
				SP, OF, LA, NULL,\
139
				BUF_GET, __FILE__, __LINE__, MTR)
140
/******************************************************************
141
Use these macros to bufferfix a page with no latching. Remember not to
142
read the contents of the page unless you know it is safe. Do not modify
143
the contents of the page! We have separated this case, because it is
144
error-prone programming not to set a latch, and it should be used
145
with care. */
146
#define buf_page_get_with_no_latch(SP, OF, MTR)	   buf_page_get_gen(\
147
				SP, OF, RW_NO_LATCH, NULL,\
148
				BUF_GET_NO_LATCH, __FILE__, __LINE__, MTR)
149
/******************************************************************
150
NOTE! The following macros should be used instead of buf_page_get_gen, to
151
improve debugging. Only values RW_S_LATCH and RW_X_LATCH are allowed as LA! */
152
#define buf_page_get_nowait(SP, OF, LA, MTR)	buf_page_get_gen(\
153
				SP, OF, LA, NULL,\
154
				BUF_GET_NOWAIT, __FILE__, __LINE__, MTR)
155
/******************************************************************
156
NOTE! The following macros should be used instead of
157
buf_page_optimistic_get_func, to improve debugging. Only values RW_S_LATCH and
158
RW_X_LATCH are allowed as LA! */
159
#define buf_page_optimistic_get(LA, BL, G, MC, MTR)			     \
160
	buf_page_optimistic_get_func(LA, BL, G, MC, __FILE__, __LINE__, MTR)
161
/************************************************************************
162
This is the general function used to get optimistic access to a database
163
page. */
164
165
ibool
166
buf_page_optimistic_get_func(
167
/*=========================*/
168
				/* out: TRUE if success */
169
	ulint		rw_latch,/* in: RW_S_LATCH, RW_X_LATCH */
170
	buf_block_t*	block,	/* in: guessed block */
171
	buf_frame_t*	guess,	/* in: guessed frame; note that AWE may move
172
				frames */
173
	dulint		modify_clock,/* in: modify clock value if mode is
174
				..._GUESS_ON_CLOCK */
175
	const char*	file,	/* in: file name */
176
	ulint		line,	/* in: line where called */
177
	mtr_t*		mtr);	/* in: mini-transaction */
178
/************************************************************************
179
Tries to get the page, but if file io is required, releases all latches
180
in mtr down to the given savepoint. If io is required, this function
181
retrieves the page to buffer buf_pool, but does not bufferfix it or latch
182
it. */
183
UNIV_INLINE
184
buf_frame_t*
185
buf_page_get_release_on_io(
186
/*=======================*/
187
				/* out: pointer to the frame, or NULL
188
				if not in buffer buf_pool */
189
	ulint	space,		/* in: space id */
190
	ulint	offset,		/* in: offset of the page within space
191
				in units of a page */
192
	buf_frame_t* guess,	/* in: guessed frame or NULL */
193
	ulint	rw_latch,	/* in: RW_X_LATCH, RW_S_LATCH,
194
				or RW_NO_LATCH */
195
	ulint	savepoint,	/* in: mtr savepoint */
196
	mtr_t*	mtr);		/* in: mtr */
197
/************************************************************************
198
This is used to get access to a known database page, when no waiting can be
199
done. */
200
201
ibool
202
buf_page_get_known_nowait(
203
/*======================*/
204
				/* out: TRUE if success */
205
	ulint		rw_latch,/* in: RW_S_LATCH, RW_X_LATCH */
206
	buf_frame_t*	guess,	/* in: the known page frame */
207
	ulint		mode,	/* in: BUF_MAKE_YOUNG or BUF_KEEP_OLD */
208
	const char*	file,	/* in: file name */
209
	ulint		line,	/* in: line where called */
210
	mtr_t*		mtr);	/* in: mini-transaction */
211
/************************************************************************
212
This is the general function used to get access to a database page. */
213
214
buf_frame_t*
215
buf_page_get_gen(
216
/*=============*/
217
				/* out: pointer to the frame or NULL */
218
	ulint		space,	/* in: space id */
219
	ulint		offset,	/* in: page number */
220
	ulint		rw_latch,/* in: RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
221
	buf_frame_t*	guess,	/* in: guessed frame or NULL */
222
	ulint		mode,	/* in: BUF_GET, BUF_GET_IF_IN_POOL,
223
				BUF_GET_NO_LATCH */
224
	const char*	file,	/* in: file name */
225
	ulint		line,	/* in: line where called */
226
	mtr_t*		mtr);	/* in: mini-transaction */
227
/************************************************************************
228
Initializes a page to the buffer buf_pool. The page is usually not read
229
from a file even if it cannot be found in the buffer buf_pool. This is one
230
of the functions which perform to a block a state transition NOT_USED =>
231
FILE_PAGE (the other is buf_page_init_for_read above). */
232
233
buf_frame_t*
234
buf_page_create(
235
/*============*/
236
			/* out: pointer to the frame, page bufferfixed */
237
	ulint	space,	/* in: space id */
238
	ulint	offset,	/* in: offset of the page within space in units of
239
			a page */
240
	mtr_t*	mtr);	/* in: mini-transaction handle */
241
/************************************************************************
242
Inits a page to the buffer buf_pool, for use in ibbackup --restore. */
243
244
void
245
buf_page_init_for_backup_restore(
246
/*=============================*/
247
	ulint		space,	/* in: space id */
248
	ulint		offset,	/* in: offset of the page within space
249
				in units of a page */
250
	buf_block_t*	block);	/* in: block to init */
251
/************************************************************************
252
Decrements the bufferfix count of a buffer control block and releases
253
a latch, if specified. */
254
UNIV_INLINE
255
void
256
buf_page_release(
257
/*=============*/
258
	buf_block_t*	block,		/* in: buffer block */
259
	ulint		rw_latch,	/* in: RW_S_LATCH, RW_X_LATCH,
260
					RW_NO_LATCH */
261
	mtr_t*		mtr);		/* in: mtr */
262
/************************************************************************
263
Moves a page to the start of the buffer pool LRU list. This high-level
264
function can be used to prevent an important page from from slipping out of
265
the buffer pool. */
266
267
void
268
buf_page_make_young(
269
/*================*/
270
	buf_frame_t*	frame);	/* in: buffer frame of a file page */
271
/************************************************************************
272
Returns TRUE if the page can be found in the buffer pool hash table. NOTE
273
that it is possible that the page is not yet read from disk, though. */
274
275
ibool
276
buf_page_peek(
277
/*==========*/
278
			/* out: TRUE if found from page hash table,
279
			NOTE that the page is not necessarily yet read
280
			from disk! */
281
	ulint	space,	/* in: space id */
282
	ulint	offset);/* in: page number */
283
/************************************************************************
284
Returns the buffer control block if the page can be found in the buffer
285
pool. NOTE that it is possible that the page is not yet read
286
from disk, though. This is a very low-level function: use with care! */
287
288
buf_block_t*
289
buf_page_peek_block(
290
/*================*/
291
			/* out: control block if found from page hash table,
292
			otherwise NULL; NOTE that the page is not necessarily
293
			yet read from disk! */
294
	ulint	space,	/* in: space id */
295
	ulint	offset);/* in: page number */
296
/************************************************************************
297
Resets the check_index_page_at_flush field of a page if found in the buffer
298
pool. */
299
300
void
301
buf_reset_check_index_page_at_flush(
302
/*================================*/
303
	ulint	space,	/* in: space id */
304
	ulint	offset);/* in: page number */
305
/************************************************************************
306
Sets file_page_was_freed TRUE if the page is found in the buffer pool.
307
This function should be called when we free a file page and want the
308
debug version to check that it is not accessed any more unless
309
reallocated. */
310
311
buf_block_t*
312
buf_page_set_file_page_was_freed(
313
/*=============================*/
314
			/* out: control block if found from page hash table,
315
			otherwise NULL */
316
	ulint	space,	/* in: space id */
317
	ulint	offset);	/* in: page number */
318
/************************************************************************
319
Sets file_page_was_freed FALSE if the page is found in the buffer pool.
320
This function should be called when we free a file page and want the
321
debug version to check that it is not accessed any more unless
322
reallocated. */
323
324
buf_block_t*
325
buf_page_reset_file_page_was_freed(
326
/*===============================*/
327
			/* out: control block if found from page hash table,
328
			otherwise NULL */
329
	ulint	space,	/* in: space id */
330
	ulint	offset);	/* in: page number */
331
/************************************************************************
332
Recommends a move of a block to the start of the LRU list if there is danger
333
of dropping from the buffer pool. NOTE: does not reserve the buffer pool
334
mutex. */
335
UNIV_INLINE
336
ibool
337
buf_block_peek_if_too_old(
338
/*======================*/
339
				/* out: TRUE if should be made younger */
340
	buf_block_t*	block);	/* in: block to make younger */
341
/************************************************************************
342
Returns the current state of is_hashed of a page. FALSE if the page is
343
not in the pool. NOTE that this operation does not fix the page in the
344
pool if it is found there. */
345
346
ibool
347
buf_page_peek_if_search_hashed(
348
/*===========================*/
349
			/* out: TRUE if page hash index is built in search
350
			system */
351
	ulint	space,	/* in: space id */
352
	ulint	offset);/* in: page number */
353
/************************************************************************
354
Gets the youngest modification log sequence number for a frame.
355
Returns zero if not file page or no modification occurred yet. */
356
UNIV_INLINE
357
dulint
358
buf_frame_get_newest_modification(
359
/*==============================*/
360
				/* out: newest modification to page */
361
	buf_frame_t*	frame);	/* in: pointer to a frame */
362
/************************************************************************
363
Increments the modify clock of a frame by 1. The caller must (1) own the
364
pool mutex and block bufferfix count has to be zero, (2) or own an x-lock
365
on the block. */
366
UNIV_INLINE
367
dulint
368
buf_frame_modify_clock_inc(
369
/*=======================*/
370
				/* out: new value */
371
	buf_frame_t*	frame);	/* in: pointer to a frame */
372
/************************************************************************
373
Increments the modify clock of a frame by 1. The caller must (1) own the
374
buf_pool mutex and block bufferfix count has to be zero, (2) or own an x-lock
375
on the block. */
376
UNIV_INLINE
377
dulint
378
buf_block_modify_clock_inc(
379
/*=======================*/
380
				/* out: new value */
381
	buf_block_t*	block);	/* in: block */
382
/************************************************************************
383
Returns the value of the modify clock. The caller must have an s-lock
384
or x-lock on the block. */
385
UNIV_INLINE
386
dulint
387
buf_block_get_modify_clock(
388
/*=======================*/
389
				/* out: value */
390
	buf_block_t*	block);	/* in: block */
391
/************************************************************************
392
Calculates a page checksum which is stored to the page when it is written
393
to a file. Note that we must be careful to calculate the same value
394
on 32-bit and 64-bit architectures. */
395
396
ulint
397
buf_calc_page_new_checksum(
398
/*=======================*/
399
			/* out: checksum */
400
	byte*	page);	/* in: buffer page */
401
/************************************************************************
402
In versions < 4.0.14 and < 4.1.1 there was a bug that the checksum only
403
looked at the first few bytes of the page. This calculates that old
404
checksum.
405
NOTE: we must first store the new formula checksum to
406
FIL_PAGE_SPACE_OR_CHKSUM before calculating and storing this old checksum
407
because this takes that field as an input! */
408
409
ulint
410
buf_calc_page_old_checksum(
411
/*=======================*/
412
			/* out: checksum */
413
	byte*	 page);	/* in: buffer page */
414
/************************************************************************
415
Checks if a page is corrupt. */
416
417
ibool
418
buf_page_is_corrupted(
419
/*==================*/
420
				/* out: TRUE if corrupted */
421
	byte*	read_buf);	/* in: a database page */
422
/**************************************************************************
423
Gets the page number of a pointer pointing within a buffer frame containing
424
a file page. */
425
UNIV_INLINE
426
ulint
427
buf_frame_get_page_no(
428
/*==================*/
429
			/* out: page number */
430
	byte*	ptr);	/* in: pointer to within a buffer frame */
431
/**************************************************************************
432
Gets the space id of a pointer pointing within a buffer frame containing a
433
file page. */
434
UNIV_INLINE
435
ulint
436
buf_frame_get_space_id(
437
/*===================*/
438
			/* out: space id */
439
	byte*	ptr);	/* in: pointer to within a buffer frame */
440
/**************************************************************************
441
Gets the space id, page offset, and byte offset within page of a
442
pointer pointing to a buffer frame containing a file page. */
443
UNIV_INLINE
444
void
445
buf_ptr_get_fsp_addr(
446
/*=================*/
447
	byte*		ptr,	/* in: pointer to a buffer frame */
448
	ulint*		space,	/* out: space id */
449
	fil_addr_t*	addr);	/* out: page offset and byte offset */
450
/**************************************************************************
451
Gets the hash value of the page the pointer is pointing to. This can be used
452
in searches in the lock hash table. */
453
UNIV_INLINE
454
ulint
455
buf_frame_get_lock_hash_val(
456
/*========================*/
457
			/* out: lock hash value */
458
	byte*	ptr);	/* in: pointer to within a buffer frame */
459
/**************************************************************************
460
Gets the mutex number protecting the page record lock hash chain in the lock
461
table. */
462
UNIV_INLINE
463
mutex_t*
464
buf_frame_get_mutex(
465
/*================*/
466
			/* out: mutex */
467
	byte*	ptr);	/* in: pointer to within a buffer frame */
468
/***********************************************************************
469
Gets the frame the pointer is pointing to. */
470
UNIV_INLINE
471
buf_frame_t*
472
buf_frame_align(
473
/*============*/
474
			/* out: pointer to frame */
475
	byte*	ptr);	/* in: pointer to a frame */
476
/***********************************************************************
477
Checks if a pointer points to the block array of the buffer pool (blocks, not
478
the frames). */
479
UNIV_INLINE
480
ibool
481
buf_pool_is_block(
482
/*==============*/
483
			/* out: TRUE if pointer to block */
484
	void*	ptr);	/* in: pointer to memory */
485
#ifdef UNIV_DEBUG
486
/*************************************************************************
487
Validates the buffer pool data structure. */
488
489
ibool
490
buf_validate(void);
491
/*==============*/
492
/*************************************************************************
493
Prints info of the buffer pool data structure. */
494
495
void
496
buf_print(void);
497
/*============*/
498
#endif /* UNIV_DEBUG */
499
/************************************************************************
500
Prints a page to stderr. */
501
502
void
503
buf_page_print(
504
/*===========*/
505
	byte*	read_buf);	/* in: a database page */
506
/*************************************************************************
507
Returns the number of latched pages in the buffer pool. */
508
509
ulint
510
buf_get_latched_pages_number(void);
511
/*==============================*/
512
/*************************************************************************
513
Returns the number of pending buf pool ios. */
514
515
ulint
516
buf_get_n_pending_ios(void);
517
/*=======================*/
518
/*************************************************************************
519
Prints info of the buffer i/o. */
520
521
void
522
buf_print_io(
523
/*=========*/
524
	FILE*	file);	/* in: file where to print */
525
/*************************************************************************
526
Returns the ratio in percents of modified pages in the buffer pool /
527
database pages in the buffer pool. */
528
529
ulint
530
buf_get_modified_ratio_pct(void);
531
/*============================*/
532
/**************************************************************************
533
Refreshes the statistics used to print per-second averages. */
534
535
void
536
buf_refresh_io_stats(void);
537
/*======================*/
538
/*************************************************************************
539
Checks that all file pages in the buffer are in a replaceable state. */
540
541
ibool
542
buf_all_freed(void);
543
/*===============*/
544
/*************************************************************************
545
Checks that there currently are no pending i/o-operations for the buffer
546
pool. */
547
548
ibool
549
buf_pool_check_no_pending_io(void);
550
/*==============================*/
551
				/* out: TRUE if there is no pending i/o */
552
/*************************************************************************
553
Invalidates the file pages in the buffer pool when an archive recovery is
554
completed. All the file pages buffered must be in a replaceable state when
555
this function is called: not latched and not modified. */
556
557
void
558
buf_pool_invalidate(void);
559
/*=====================*/
560
561
/*========================================================================
562
--------------------------- LOWER LEVEL ROUTINES -------------------------
563
=========================================================================*/
564
565
/************************************************************************
566
Maps the page of block to a frame, if not mapped yet. Unmaps some page
567
from the end of the awe_LRU_free_mapped. */
568
569
void
570
buf_awe_map_page_to_frame(
571
/*======================*/
572
	buf_block_t*	block,		/* in: block whose page should be
573
					mapped to a frame */
574
	ibool		add_to_mapped_list);/* in: TRUE if we in the case
575
					we need to map the page should also
576
					add the block to the
577
					awe_LRU_free_mapped list */
578
#ifdef UNIV_SYNC_DEBUG
579
/*************************************************************************
580
Adds latch level info for the rw-lock protecting the buffer frame. This
581
should be called in the debug version after a successful latching of a
582
page if we know the latching order level of the acquired latch. */
583
UNIV_INLINE
584
void
585
buf_page_dbg_add_level(
586
/*===================*/
587
	buf_frame_t*	frame,	/* in: buffer page where we have acquired
588
				a latch */
589
	ulint		level);	/* in: latching order level */
590
#endif /* UNIV_SYNC_DEBUG */
591
/*************************************************************************
592
Gets a pointer to the memory frame of a block. */
593
UNIV_INLINE
594
buf_frame_t*
595
buf_block_get_frame(
596
/*================*/
597
				/* out: pointer to the frame */
598
	buf_block_t*	block);	/* in: pointer to the control block */
599
/*************************************************************************
600
Gets the space id of a block. */
601
UNIV_INLINE
602
ulint
603
buf_block_get_space(
604
/*================*/
605
				/* out: space id */
606
	buf_block_t*	block);	/* in: pointer to the control block */
607
/*************************************************************************
608
Gets the page number of a block. */
609
UNIV_INLINE
610
ulint
611
buf_block_get_page_no(
612
/*==================*/
613
				/* out: page number */
614
	buf_block_t*	block);	/* in: pointer to the control block */
615
/***********************************************************************
616
Gets the block to whose frame the pointer is pointing to. */
617
UNIV_INLINE
618
buf_block_t*
619
buf_block_align(
620
/*============*/
621
			/* out: pointer to block */
622
	byte*	ptr);	/* in: pointer to a frame */
623
/************************************************************************
624
This function is used to get info if there is an io operation
625
going on on a buffer page. */
626
UNIV_INLINE
627
ibool
628
buf_page_io_query(
629
/*==============*/
630
				/* out: TRUE if io going on */
631
	buf_block_t*	block);	/* in: pool block, must be bufferfixed */
632
/***********************************************************************
633
Accessor function for block array. */
634
UNIV_INLINE
635
buf_block_t*
636
buf_pool_get_nth_block(
637
/*===================*/
638
				/* out: pointer to block */
639
	buf_pool_t*	pool,	/* in: pool */
640
	ulint		i);	/* in: index of the block */
641
/************************************************************************
642
Function which inits a page for read to the buffer buf_pool. If the page is
643
(1) already in buf_pool, or
644
(2) if we specify to read only ibuf pages and the page is not an ibuf page, or
645
(3) if the space is deleted or being deleted,
646
then this function does nothing.
647
Sets the io_fix flag to BUF_IO_READ and sets a non-recursive exclusive lock
648
on the buffer frame. The io-handler must take care that the flag is cleared
649
and the lock released later. This is one of the functions which perform the
650
state transition NOT_USED => FILE_PAGE to a block (the other is
651
buf_page_create). */
652
653
buf_block_t*
654
buf_page_init_for_read(
655
/*===================*/
656
				/* out: pointer to the block or NULL */
657
	ulint*		err,	/* out: DB_SUCCESS or DB_TABLESPACE_DELETED */
658
	ulint		mode,	/* in: BUF_READ_IBUF_PAGES_ONLY, ... */
659
	ulint		space,	/* in: space id */
660
	ib_longlong	tablespace_version,/* in: prevents reading from a wrong
661
				version of the tablespace in case we have done
662
				DISCARD + IMPORT */
663
	ulint		offset);/* in: page number */
664
/************************************************************************
665
Completes an asynchronous read or write request of a file page to or from
666
the buffer pool. */
667
668
void
669
buf_page_io_complete(
670
/*=================*/
671
	buf_block_t*	block);	/* in: pointer to the block in question */
672
/************************************************************************
673
Calculates a folded value of a file page address to use in the page hash
674
table. */
675
UNIV_INLINE
676
ulint
677
buf_page_address_fold(
678
/*==================*/
679
			/* out: the folded value */
680
	ulint	space,	/* in: space id */
681
	ulint	offset);/* in: offset of the page within space */
682
/**********************************************************************
683
Returns the control block of a file page, NULL if not found. */
684
UNIV_INLINE
685
buf_block_t*
686
buf_page_hash_get(
687
/*==============*/
688
			/* out: block, NULL if not found */
689
	ulint	space,	/* in: space id */
690
	ulint	offset);/* in: offset of the page within space */
691
/***********************************************************************
692
Increments the pool clock by one and returns its new value. Remember that
693
in the 32 bit version the clock wraps around at 4 billion! */
694
UNIV_INLINE
695
ulint
696
buf_pool_clock_tic(void);
697
/*====================*/
698
			/* out: new clock value */
699
/*************************************************************************
700
Gets the current length of the free list of buffer blocks. */
701
702
ulint
703
buf_get_free_list_len(void);
704
/*=======================*/
705
706
707
708
/* The buffer control block structure */
709
710
struct buf_block_struct{
711
712
	/* 1. General fields */
713
714
	ulint		magic_n;	/* magic number to check */
715
	ulint		state;		/* state of the control block:
716
					BUF_BLOCK_NOT_USED, ...; changing
717
					this is only allowed when a thread
718
					has BOTH the buffer pool mutex AND
719
					block->mutex locked */
720
	byte*		frame;		/* pointer to buffer frame which
721
					is of size UNIV_PAGE_SIZE, and
722
					aligned to an address divisible by
723
					UNIV_PAGE_SIZE; if AWE is used, this
724
					will be NULL for the pages which are
725
					currently not mapped into the virtual
726
					address space window of the buffer
727
					pool */
728
	os_awe_t*	awe_info;	/* if AWE is used, then an array of
729
					awe page infos for
730
					UNIV_PAGE_SIZE / OS_AWE_X86_PAGE_SIZE
731
					(normally = 4) physical memory
732
					pages; otherwise NULL */
733
	ulint		space;		/* space id of the page */
734
	ulint		offset;		/* page number within the space */
735
	ulint		lock_hash_val;	/* hashed value of the page address
736
					in the record lock hash table */
737
	mutex_t		mutex;		/* mutex protecting this block:
738
					state (also protected by the buffer
739
					pool mutex), io_fix, buf_fix_count,
740
					and accessed; we introduce this new
741
					mutex in InnoDB-5.1 to relieve
742
					contention on the buffer pool mutex */
743
	rw_lock_t	lock;		/* read-write lock of the buffer
744
					frame */
745
	buf_block_t*	hash;		/* node used in chaining to the page
746
					hash table */
747
	ibool		check_index_page_at_flush;
748
					/* TRUE if we know that this is
749
					an index page, and want the database
750
					to check its consistency before flush;
751
					note that there may be pages in the
752
					buffer pool which are index pages,
753
					but this flag is not set because
754
					we do not keep track of all pages */
755
	/* 2. Page flushing fields */
756
757
	UT_LIST_NODE_T(buf_block_t) flush_list;
758
					/* node of the modified, not yet
759
					flushed blocks list */
760
	dulint		newest_modification;
761
					/* log sequence number of the youngest
762
					modification to this block, zero if
763
					not modified */
764
	dulint		oldest_modification;
765
					/* log sequence number of the START of
766
					the log entry written of the oldest
767
					modification to this block which has
768
					not yet been flushed on disk; zero if
769
					all modifications are on disk */
770
	ulint		flush_type;	/* if this block is currently being
771
					flushed to disk, this tells the
772
					flush_type: BUF_FLUSH_LRU or
773
					BUF_FLUSH_LIST */
774
775
	/* 3. LRU replacement algorithm fields */
776
777
	UT_LIST_NODE_T(buf_block_t) free;
778
					/* node of the free block list */
779
	ibool		in_free_list;	/* TRUE if in the free list; used in
780
					debugging */
781
	UT_LIST_NODE_T(buf_block_t) LRU;
782
					/* node of the LRU list */
783
	UT_LIST_NODE_T(buf_block_t) awe_LRU_free_mapped;
784
					/* in the AWE version node in the
785
					list of free and LRU blocks which are
786
					mapped to a frame */
787
	ibool		in_LRU_list;	/* TRUE of the page is in the LRU list;
788
					used in debugging */
789
	ulint		LRU_position;	/* value which monotonically
790
					decreases (or may stay constant if
791
					the block is in the old blocks) toward
792
					the end of the LRU list, if the pool
793
					ulint_clock has not wrapped around:
794
					NOTE that this value can only be used
795
					in heuristic algorithms, because of
796
					the possibility of a wrap-around! */
797
	ulint		freed_page_clock;/* the value of freed_page_clock
798
					of the buffer pool when this block was
799
					the last time put to the head of the
800
					LRU list; a thread is allowed to
801
					read this for heuristic purposes
802
					without holding any mutex or latch */
803
	ibool		old;		/* TRUE if the block is in the old
804
					blocks in the LRU list */
805
	ibool		accessed;	/* TRUE if the page has been accessed
806
					while in the buffer pool: read-ahead
807
					may read in pages which have not been
808
					accessed yet; this is protected by
809
					block->mutex; a thread is allowed to
810
					read this for heuristic purposes
811
					without holding any mutex or latch */
812
	ulint		buf_fix_count;	/* count of how manyfold this block
813
					is currently bufferfixed; this is
814
					protected by block->mutex */
815
	ulint		io_fix;		/* if a read is pending to the frame,
816
					io_fix is BUF_IO_READ, in the case
817
					of a write BUF_IO_WRITE, otherwise 0;
818
					this is protected by block->mutex */
819
	/* 4. Optimistic search field */
820
821
	dulint		modify_clock;	/* this clock is incremented every
822
					time a pointer to a record on the
823
					page may become obsolete; this is
824
					used in the optimistic cursor
825
					positioning: if the modify clock has
826
					not changed, we know that the pointer
827
					is still valid; this field may be
828
					changed if the thread (1) owns the
829
					pool mutex and the page is not
830
					bufferfixed, or (2) the thread has an
831
					x-latch on the block */
832
833
	/* 5. Hash search fields: NOTE that the first 4 fields are NOT
834
	protected by any semaphore! */
835
836
	ulint		n_hash_helps;	/* counter which controls building
837
					of a new hash index for the page */
838
	ulint		n_fields;	/* recommended prefix length for hash
839
					search: number of full fields */
840
	ulint		n_bytes;	/* recommended prefix: number of bytes
841
					in an incomplete field */
842
	ibool		left_side;	/* TRUE or FALSE, depending on
843
					whether the leftmost record of several
844
					records with the same prefix should be
845
					indexed in the hash index */
846
847
	/* These 6 fields may only be modified when we have
848
	an x-latch on btr_search_latch AND
849
	a) we are holding an s-latch or x-latch on block->lock or
850
	b) we know that block->buf_fix_count == 0.
851
852
	An exception to this is when we init or create a page
853
	in the buffer pool in buf0buf.c. */
854
855
	ibool		is_hashed;	/* TRUE if hash index has already been
856
					built on this page; note that it does
857
					not guarantee that the index is
858
					complete, though: there may have been
859
					hash collisions, record deletions,
860
					etc. */
861
	ulint		n_pointers;	/* used in debugging: the number of
862
					pointers in the adaptive hash index
863
					pointing to this frame */
864
	ulint		curr_n_fields;	/* prefix length for hash indexing:
865
					number of full fields */
866
	ulint		curr_n_bytes;	/* number of bytes in hash indexing */
867
	ibool		curr_left_side;	/* TRUE or FALSE in hash indexing */
868
	dict_index_t*	index;		/* Index for which the adaptive
869
					hash index has been created. */
870
	/* 6. Debug fields */
871
#ifdef UNIV_SYNC_DEBUG
872
	rw_lock_t	debug_latch;	/* in the debug version, each thread
873
					which bufferfixes the block acquires
874
					an s-latch here; so we can use the
875
					debug utilities in sync0rw */
876
#endif
877
	ibool		file_page_was_freed;
878
					/* this is set to TRUE when fsp
879
					frees a page in buffer pool */
880
};
881
882
#define BUF_BLOCK_MAGIC_N	41526563
883
884
/* The buffer pool structure. NOTE! The definition appears here only for
885
other modules of this directory (buf) to see it. Do not use from outside! */
886
887
struct buf_pool_struct{
888
889
	/* 1. General fields */
890
891
	mutex_t		mutex;		/* mutex protecting the buffer pool
892
					struct and control blocks, except the
893
					read-write lock in them */
894
	byte*		frame_mem;	/* pointer to the memory area which
895
					was allocated for the frames; in AWE
896
					this is the virtual address space
897
					window where we map pages stored
898
					in physical memory */
899
	byte*		frame_zero;	/* pointer to the first buffer frame:
900
					this may differ from frame_mem, because
901
					this is aligned by the frame size */
902
	byte*		high_end;	/* pointer to the end of the buffer
903
					frames */
904
	ulint		n_frames;	/* number of frames */
905
	buf_block_t*	blocks;		/* array of buffer control blocks */
906
	buf_block_t**	blocks_of_frames;/* inverse mapping which can be used
907
					to retrieve the buffer control block
908
					of a frame; this is an array which
909
					lists the blocks of frames in the
910
					order frame_zero,
911
					frame_zero + UNIV_PAGE_SIZE, ...
912
					a control block is always assigned
913
					for each frame, even if the frame does
914
					not contain any data; note that in AWE
915
					there are more control blocks than
916
					buffer frames */
917
	os_awe_t*	awe_info;	/* if AWE is used, AWE info for the
918
					physical 4 kB memory pages associated
919
					with buffer frames */
920
	ulint		max_size;	/* number of control blocks ==
921
					maximum pool size in pages */
922
	ulint		curr_size;	/* current pool size in pages;
923
					currently always the same as
924
					max_size */
925
	hash_table_t*	page_hash;	/* hash table of the file pages */
926
927
	ulint		n_pend_reads;	/* number of pending read operations */
928
929
	time_t		last_printout_time; /* when buf_print was last time
930
					called */
931
	ulint		n_pages_read;	/* number read operations */
932
	ulint		n_pages_written;/* number write operations */
933
	ulint		n_pages_created;/* number of pages created in the pool
934
					with no read */
935
	ulint		n_page_gets;	/* number of page gets performed;
936
					also successful searches through
937
					the adaptive hash index are
938
					counted as page gets; this field
939
					is NOT protected by the buffer
940
					pool mutex */
941
	ulint		n_pages_awe_remapped; /* if AWE is enabled, the
942
					number of remaps of blocks to
943
					buffer frames */
944
	ulint		n_page_gets_old;/* n_page_gets when buf_print was
945
					last time called: used to calculate
946
					hit rate */
947
	ulint		n_pages_read_old;/* n_pages_read when buf_print was
948
					last time called */
949
	ulint		n_pages_written_old;/* number write operations */
950
	ulint		n_pages_created_old;/* number of pages created in
951
					the pool with no read */
952
	ulint		n_pages_awe_remapped_old;
953
	/* 2. Page flushing algorithm fields */
954
955
	UT_LIST_BASE_NODE_T(buf_block_t) flush_list;
956
					/* base node of the modified block
957
					list */
958
	ibool		init_flush[BUF_FLUSH_LIST + 1];
959
					/* this is TRUE when a flush of the
960
					given type is being initialized */
961
	ulint		n_flush[BUF_FLUSH_LIST + 1];
962
					/* this is the number of pending
963
					writes in the given flush type */
964
	os_event_t	no_flush[BUF_FLUSH_LIST + 1];
965
					/* this is in the set state when there
966
					is no flush batch of the given type
967
					running */
968
	ulint		ulint_clock;	/* a sequence number used to count
969
					time. NOTE! This counter wraps
970
					around at 4 billion (if ulint ==
971
					32 bits)! */
972
	ulint		freed_page_clock;/* a sequence number used to count the
973
					number of buffer blocks removed from
974
					the end of the LRU list; NOTE that
975
					this counter may wrap around at 4
976
					billion! A thread is allowed to
977
					read this for heuristic purposes
978
					without holding any mutex or latch */
979
	ulint		LRU_flush_ended;/* when an LRU flush ends for a page,
980
					this is incremented by one; this is
981
					set to zero when a buffer block is
982
					allocated */
983
984
	/* 3. LRU replacement algorithm fields */
985
986
	UT_LIST_BASE_NODE_T(buf_block_t) free;
987
					/* base node of the free block list;
988
					in the case of AWE, at the start are
989
					always free blocks for which the
990
					physical memory is mapped to a frame */
991
	UT_LIST_BASE_NODE_T(buf_block_t) LRU;
992
					/* base node of the LRU list */
993
	buf_block_t*	LRU_old;	/* pointer to the about 3/8 oldest
994
					blocks in the LRU list; NULL if LRU
995
					length less than BUF_LRU_OLD_MIN_LEN */
996
	ulint		LRU_old_len;	/* length of the LRU list from
997
					the block to which LRU_old points
998
					onward, including that block;
999
					see buf0lru.c for the restrictions
1000
					on this value; not defined if
1001
					LRU_old == NULL */
1002
	UT_LIST_BASE_NODE_T(buf_block_t) awe_LRU_free_mapped;
1003
					/* list of those blocks which are
1004
					in the LRU list or the free list, and
1005
					where the page is mapped to a frame;
1006
					thus, frames allocated, e.g., to the
1007
					locki table, are not in this list */
1008
};
1009
1010
/* States of a control block */
1011
#define	BUF_BLOCK_NOT_USED	211	/* is in the free list */
1012
#define BUF_BLOCK_READY_FOR_USE	212	/* when buf_get_free_block returns
1013
					a block, it is in this state */
1014
#define	BUF_BLOCK_FILE_PAGE	213	/* contains a buffered file page */
1015
#define	BUF_BLOCK_MEMORY	214	/* contains some main memory object */
1016
#define BUF_BLOCK_REMOVE_HASH	215	/* hash index should be removed
1017
					before putting to the free list */
1018
1019
/* Io_fix states of a control block; these must be != 0 */
1020
#define BUF_IO_READ		561
1021
#define BUF_IO_WRITE		562
1022
1023
/************************************************************************
1024
Let us list the consistency conditions for different control block states.
1025
1026
NOT_USED:	is in free list, not in LRU list, not in flush list, nor
1027
		page hash table
1028
READY_FOR_USE:	is not in free list, LRU list, or flush list, nor page
1029
		hash table
1030
MEMORY:		is not in free list, LRU list, or flush list, nor page
1031
		hash table
1032
FILE_PAGE:	space and offset are defined, is in page hash table
1033
		if io_fix == BUF_IO_WRITE,
1034
			pool: no_flush[block->flush_type] is in reset state,
1035
			pool: n_flush[block->flush_type] > 0
1036
1037
		(1) if buf_fix_count == 0, then
1038
			is in LRU list, not in free list
1039
			is in flush list,
1040
				if and only if oldest_modification > 0
1041
			is x-locked,
1042
				if and only if io_fix == BUF_IO_READ
1043
			is s-locked,
1044
				if and only if io_fix == BUF_IO_WRITE
1045
1046
		(2) if buf_fix_count > 0, then
1047
			is not in LRU list, not in free list
1048
			is in flush list,
1049
				if and only if oldest_modification > 0
1050
			if io_fix == BUF_IO_READ,
1051
				is x-locked
1052
			if io_fix == BUF_IO_WRITE,
1053
				is s-locked
1054
1055
State transitions:
1056
1057
NOT_USED => READY_FOR_USE
1058
READY_FOR_USE => MEMORY
1059
READY_FOR_USE => FILE_PAGE
1060
MEMORY => NOT_USED
1061
FILE_PAGE => NOT_USED	NOTE: This transition is allowed if and only if
1062
				(1) buf_fix_count == 0,
1063
				(2) oldest_modification == 0, and
1064
				(3) io_fix == 0.
1065
*/
1066
1067
#ifndef UNIV_NONINL
1068
#include "buf0buf.ic"
1069
#endif
1070
1071
#endif