buf0buf.h 37.7 KB
Newer Older
osku's avatar
osku committed
1
/*   Innobase relational database engine; Copyright (C) 2001 Innobase Oy
2

osku's avatar
osku committed
3 4 5
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License 2
     as published by the Free Software Foundation in June 1991.
6

osku's avatar
osku committed
7 8 9 10
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
11

osku's avatar
osku committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
     You should have received a copy of the GNU General Public License 2
     along with this program (in file COPYING); if not, write to the Free
     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/******************************************************
The database buffer pool high-level routines

(c) 1995 Innobase Oy

Created 11/5/1995 Heikki Tuuri
*******************************************************/

#ifndef buf0buf_h
#define buf0buf_h

#include "univ.i"
#include "fil0fil.h"
#include "mtr0types.h"
#include "buf0types.h"
#include "sync0rw.h"
#include "hash0hash.h"
#include "ut0byte.h"
#include "os0proc.h"
marko's avatar
marko committed
34
#include "page0types.h"
osku's avatar
osku committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

/* Modes for buf_page_get_gen */
#define BUF_GET			10	/* get always */
#define	BUF_GET_IF_IN_POOL	11	/* get if in pool */
#define	BUF_GET_NOWAIT		12	/* get if can set the latch without
					waiting */
#define BUF_GET_NO_LATCH	14	/* get and bufferfix, but set no latch;
					we have separated this case, because
					it is error-prone programming not to
					set a latch, and it should be used
					with care */
/* Modes for buf_page_get_known_nowait */
#define BUF_MAKE_YOUNG	51
#define BUF_KEEP_OLD	52
/* Magic value to use instead of checksums when they are disabled */
#define BUF_NO_CHECKSUM_MAGIC 0xDEADBEEFUL

52
extern buf_pool_t*	buf_pool;	/* The buffer pool of the database */
osku's avatar
osku committed
53 54 55 56 57 58
#ifdef UNIV_DEBUG
extern ibool		buf_debug_prints;/* If this is set TRUE, the program
					prints info whenever read or flush
					occurs */
#endif /* UNIV_DEBUG */
extern ulint srv_buf_pool_write_requests; /* variable to count write request
59
					  issued */
osku's avatar
osku committed
60

61
/* States of a control block */
62 63 64 65 66 67
enum buf_page_state {
	BUF_BLOCK_ZIP_PAGE = 1,		/* contains a compressed page only;
					must be smaller than
					BUF_BLOCK_NOT_USED;
					cf. buf_block_state_valid() */
	BUF_BLOCK_NOT_USED,		/* is in the free list */
68 69 70 71 72 73 74 75
	BUF_BLOCK_READY_FOR_USE,	/* when buf_get_free_block returns
					a block, it is in this state */
	BUF_BLOCK_FILE_PAGE,		/* contains a buffered file page */
	BUF_BLOCK_MEMORY,		/* contains some main memory object */
	BUF_BLOCK_REMOVE_HASH		/* hash index should be removed
					before putting to the free list */
};

osku's avatar
osku committed
76 77 78 79
/************************************************************************
Creates the buffer pool. */

buf_pool_t*
80 81
buf_pool_init(void);
/*===============*/
osku's avatar
osku committed
82 83
				/* out, own: buf_pool object, NULL if not
				enough memory or error */
84 85 86 87 88 89
/************************************************************************
Resizes the buffer pool. */

void
buf_pool_resize(void);
/*=================*/
osku's avatar
osku committed
90
/*************************************************************************
91
Gets the current size of buffer buf_pool in bytes. */
osku's avatar
osku committed
92 93 94 95 96 97 98
UNIV_INLINE
ulint
buf_pool_get_curr_size(void);
/*========================*/
			/* out: size in bytes */
/************************************************************************
Gets the smallest oldest_modification lsn for any page in the pool. Returns
99
zero if all modified pages have been flushed to disk. */
osku's avatar
osku committed
100
UNIV_INLINE
101
ib_ulonglong
osku's avatar
osku committed
102 103 104
buf_pool_get_oldest_modification(void);
/*==================================*/
				/* out: oldest modification in pool,
105
				zero if none */
106 107 108 109 110 111
/************************************************************************
Allocates a buffer block. */
UNIV_INLINE
buf_block_t*
buf_block_alloc(
/*============*/
112
				/* out, own: the allocated block */
113 114 115 116 117 118 119 120 121
	ulint	zip_size);	/* in: compressed page size in bytes,
				or 0 if uncompressed tablespace */
/************************************************************************
Frees a buffer block which does not contain a file page. */
UNIV_INLINE
void
buf_block_free(
/*===========*/
	buf_block_t*	block);	/* in, own: block to be freed */
osku's avatar
osku committed
122 123 124 125 126 127
/*************************************************************************
Copies contents of a buffer frame to a given buffer. */
UNIV_INLINE
byte*
buf_frame_copy(
/*===========*/
128 129 130
					/* out: buf */
	byte*			buf,	/* in: buffer to copy to */
	const buf_frame_t*	frame);	/* in: buffer frame */
osku's avatar
osku committed
131 132 133 134
/******************************************************************
NOTE! The following macros should be used instead of buf_page_get_gen,
to improve debugging. Only values RW_S_LATCH and RW_X_LATCH are allowed
in LA! */
135
#define buf_page_get(SP, OF, LA, MTR)	 buf_page_get_gen(\
osku's avatar
osku committed
136 137 138 139 140 141 142 143
				SP, OF, LA, NULL,\
				BUF_GET, __FILE__, __LINE__, MTR)
/******************************************************************
Use these macros to bufferfix a page with no latching. Remember not to
read the contents of the page unless you know it is safe. Do not modify
the contents of the page! We have separated this case, because it is
error-prone programming not to set a latch, and it should be used
with care. */
144
#define buf_page_get_with_no_latch(SP, OF, MTR)	   buf_page_get_gen(\
osku's avatar
osku committed
145 146 147 148 149
				SP, OF, RW_NO_LATCH, NULL,\
				BUF_GET_NO_LATCH, __FILE__, __LINE__, MTR)
/******************************************************************
NOTE! The following macros should be used instead of buf_page_get_gen, to
improve debugging. Only values RW_S_LATCH and RW_X_LATCH are allowed as LA! */
150
#define buf_page_get_nowait(SP, OF, LA, MTR)	buf_page_get_gen(\
osku's avatar
osku committed
151 152 153 154 155 156
				SP, OF, LA, NULL,\
				BUF_GET_NOWAIT, __FILE__, __LINE__, MTR)
/******************************************************************
NOTE! The following macros should be used instead of
buf_page_optimistic_get_func, to improve debugging. Only values RW_S_LATCH and
RW_X_LATCH are allowed as LA! */
157 158
#define buf_page_optimistic_get(LA, BL, MC, MTR)			     \
	buf_page_optimistic_get_func(LA, BL, MC, __FILE__, __LINE__, MTR)
osku's avatar
osku committed
159 160 161 162 163 164 165 166 167 168
/************************************************************************
This is the general function used to get optimistic access to a database
page. */

ibool
buf_page_optimistic_get_func(
/*=========================*/
				/* out: TRUE if success */
	ulint		rw_latch,/* in: RW_S_LATCH, RW_X_LATCH */
	buf_block_t*	block,	/* in: guessed block */
169
	ib_ulonglong	modify_clock,/* in: modify clock value if mode is
osku's avatar
osku committed
170 171 172 173 174 175 176 177 178 179
				..._GUESS_ON_CLOCK */
	const char*	file,	/* in: file name */
	ulint		line,	/* in: line where called */
	mtr_t*		mtr);	/* in: mini-transaction */
/************************************************************************
Tries to get the page, but if file io is required, releases all latches
in mtr down to the given savepoint. If io is required, this function
retrieves the page to buffer buf_pool, but does not bufferfix it or latch
it. */
UNIV_INLINE
180
buf_block_t*
osku's avatar
osku committed
181 182
buf_page_get_release_on_io(
/*=======================*/
183
				/* out: pointer to the block, or NULL
osku's avatar
osku committed
184 185 186 187
				if not in buffer buf_pool */
	ulint	space,		/* in: space id */
	ulint	offset,		/* in: offset of the page within space
				in units of a page */
188
	buf_block_t* guess,	/* in: guessed frame or NULL */
osku's avatar
osku committed
189 190 191 192 193 194 195 196 197 198 199 200 201
	ulint	rw_latch,	/* in: RW_X_LATCH, RW_S_LATCH,
				or RW_NO_LATCH */
	ulint	savepoint,	/* in: mtr savepoint */
	mtr_t*	mtr);		/* in: mtr */
/************************************************************************
This is used to get access to a known database page, when no waiting can be
done. */

ibool
buf_page_get_known_nowait(
/*======================*/
				/* out: TRUE if success */
	ulint		rw_latch,/* in: RW_S_LATCH, RW_X_LATCH */
202
	buf_block_t*	block,	/* in: the known page */
osku's avatar
osku committed
203 204 205 206 207 208 209
	ulint		mode,	/* in: BUF_MAKE_YOUNG or BUF_KEEP_OLD */
	const char*	file,	/* in: file name */
	ulint		line,	/* in: line where called */
	mtr_t*		mtr);	/* in: mini-transaction */
/************************************************************************
This is the general function used to get access to a database page. */

210
buf_block_t*
osku's avatar
osku committed
211 212
buf_page_get_gen(
/*=============*/
213
				/* out: pointer to the block or NULL */
osku's avatar
osku committed
214 215 216
	ulint		space,	/* in: space id */
	ulint		offset,	/* in: page number */
	ulint		rw_latch,/* in: RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
217
	buf_block_t*	guess,	/* in: guessed block or NULL */
osku's avatar
osku committed
218 219 220 221 222 223 224 225 226 227 228
	ulint		mode,	/* in: BUF_GET, BUF_GET_IF_IN_POOL,
				BUF_GET_NO_LATCH */
	const char*	file,	/* in: file name */
	ulint		line,	/* in: line where called */
	mtr_t*		mtr);	/* in: mini-transaction */
/************************************************************************
Initializes a page to the buffer buf_pool. The page is usually not read
from a file even if it cannot be found in the buffer buf_pool. This is one
of the functions which perform to a block a state transition NOT_USED =>
FILE_PAGE (the other is buf_page_init_for_read above). */

229
buf_block_t*
osku's avatar
osku committed
230 231
buf_page_create(
/*============*/
232
			/* out: pointer to the block, page bufferfixed */
osku's avatar
osku committed
233 234 235
	ulint	space,	/* in: space id */
	ulint	offset,	/* in: offset of the page within space in units of
			a page */
236
	ulint	zip_size,/* in: compressed page size, or 0 */
osku's avatar
osku committed
237
	mtr_t*	mtr);	/* in: mini-transaction handle */
238
#ifdef UNIV_HOTBACKUP
osku's avatar
osku committed
239 240 241 242 243 244 245 246 247
/************************************************************************
Inits a page to the buffer buf_pool, for use in ibbackup --restore. */

void
buf_page_init_for_backup_restore(
/*=============================*/
	ulint		space,	/* in: space id */
	ulint		offset,	/* in: offset of the page within space
				in units of a page */
248 249
	ulint		zip_size,/* in: compressed page size in bytes
				or 0 for uncompressed pages */
osku's avatar
osku committed
250
	buf_block_t*	block);	/* in: block to init */
251
#endif /* UNIV_HOTBACKUP */
osku's avatar
osku committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/************************************************************************
Decrements the bufferfix count of a buffer control block and releases
a latch, if specified. */
UNIV_INLINE
void
buf_page_release(
/*=============*/
	buf_block_t*	block,		/* in: buffer block */
	ulint		rw_latch,	/* in: RW_S_LATCH, RW_X_LATCH,
					RW_NO_LATCH */
	mtr_t*		mtr);		/* in: mtr */
/************************************************************************
Moves a page to the start of the buffer pool LRU list. This high-level
function can be used to prevent an important page from from slipping out of
the buffer pool. */

void
buf_page_make_young(
270
/*================*/
271
	buf_block_t*	block);	/* in: buffer block of a file page */
osku's avatar
osku committed
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
/************************************************************************
Returns TRUE if the page can be found in the buffer pool hash table. NOTE
that it is possible that the page is not yet read from disk, though. */

ibool
buf_page_peek(
/*==========*/
			/* out: TRUE if found from page hash table,
			NOTE that the page is not necessarily yet read
			from disk! */
	ulint	space,	/* in: space id */
	ulint	offset);/* in: page number */
/************************************************************************
Returns the buffer control block if the page can be found in the buffer
pool. NOTE that it is possible that the page is not yet read
from disk, though. This is a very low-level function: use with care! */

buf_block_t*
buf_page_peek_block(
/*================*/
			/* out: control block if found from page hash table,
			otherwise NULL; NOTE that the page is not necessarily
			yet read from disk! */
	ulint	space,	/* in: space id */
	ulint	offset);/* in: page number */
/************************************************************************
Resets the check_index_page_at_flush field of a page if found in the buffer
pool. */

void
buf_reset_check_index_page_at_flush(
/*================================*/
	ulint	space,	/* in: space id */
	ulint	offset);/* in: page number */
306
#ifdef UNIV_DEBUG_FILE_ACCESSES
osku's avatar
osku committed
307 308 309 310 311 312
/************************************************************************
Sets file_page_was_freed TRUE if the page is found in the buffer pool.
This function should be called when we free a file page and want the
debug version to check that it is not accessed any more unless
reallocated. */

313
buf_page_t*
osku's avatar
osku committed
314 315
buf_page_set_file_page_was_freed(
/*=============================*/
316
			/* out: control block if found in page hash table,
osku's avatar
osku committed
317 318
			otherwise NULL */
	ulint	space,	/* in: space id */
319
	ulint	offset);/* in: page number */
osku's avatar
osku committed
320 321 322 323 324 325
/************************************************************************
Sets file_page_was_freed FALSE if the page is found in the buffer pool.
This function should be called when we free a file page and want the
debug version to check that it is not accessed any more unless
reallocated. */

326
buf_page_t*
osku's avatar
osku committed
327 328
buf_page_reset_file_page_was_freed(
/*===============================*/
329
			/* out: control block if found in page hash table,
osku's avatar
osku committed
330 331 332
			otherwise NULL */
	ulint	space,	/* in: space id */
	ulint	offset);	/* in: page number */
333
#endif /* UNIV_DEBUG_FILE_ACCESSES */
osku's avatar
osku committed
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
/************************************************************************
Recommends a move of a block to the start of the LRU list if there is danger
of dropping from the buffer pool. NOTE: does not reserve the buffer pool
mutex. */
UNIV_INLINE
ibool
buf_block_peek_if_too_old(
/*======================*/
				/* out: TRUE if should be made younger */
	buf_block_t*	block);	/* in: block to make younger */
/************************************************************************
Returns the current state of is_hashed of a page. FALSE if the page is
not in the pool. NOTE that this operation does not fix the page in the
pool if it is found there. */

ibool
buf_page_peek_if_search_hashed(
/*===========================*/
			/* out: TRUE if page hash index is built in search
			system */
	ulint	space,	/* in: space id */
	ulint	offset);/* in: page number */
/************************************************************************
Gets the youngest modification log sequence number for a frame.
Returns zero if not file page or no modification occurred yet. */
UNIV_INLINE
360
ib_ulonglong
361
buf_block_get_newest_modification(
osku's avatar
osku committed
362 363
/*==============================*/
				/* out: newest modification to page */
364
	buf_block_t*	block);	/* in: block containing the page frame */
osku's avatar
osku committed
365 366 367 368 369
/************************************************************************
Increments the modify clock of a frame by 1. The caller must (1) own the
buf_pool mutex and block bufferfix count has to be zero, (2) or own an x-lock
on the block. */
UNIV_INLINE
370
void
osku's avatar
osku committed
371 372 373 374
buf_block_modify_clock_inc(
/*=======================*/
	buf_block_t*	block);	/* in: block */
/************************************************************************
375
Returns the value of the modify clock. The caller must have an s-lock
osku's avatar
osku committed
376 377
or x-lock on the block. */
UNIV_INLINE
378
ib_ulonglong
osku's avatar
osku committed
379 380 381 382
buf_block_get_modify_clock(
/*=======================*/
				/* out: value */
	buf_block_t*	block);	/* in: block */
383
/************************************************************************
osku's avatar
osku committed
384 385 386 387 388 389 390
Calculates a page checksum which is stored to the page when it is written
to a file. Note that we must be careful to calculate the same value
on 32-bit and 64-bit architectures. */

ulint
buf_calc_page_new_checksum(
/*=======================*/
marko's avatar
marko committed
391 392
				/* out: checksum */
	const byte*	page);	/* in: buffer page */
osku's avatar
osku committed
393 394 395
/************************************************************************
In versions < 4.0.14 and < 4.1.1 there was a bug that the checksum only
looked at the first few bytes of the page. This calculates that old
396
checksum.
osku's avatar
osku committed
397 398 399 400 401 402 403
NOTE: we must first store the new formula checksum to
FIL_PAGE_SPACE_OR_CHKSUM before calculating and storing this old checksum
because this takes that field as an input! */

ulint
buf_calc_page_old_checksum(
/*=======================*/
marko's avatar
marko committed
404 405
				/* out: checksum */
	const byte*	 page);	/* in: buffer page */
osku's avatar
osku committed
406 407 408 409 410 411
/************************************************************************
Checks if a page is corrupt. */

ibool
buf_page_is_corrupted(
/*==================*/
marko's avatar
marko committed
412 413 414 415
					/* out: TRUE if corrupted */
	const byte*	read_buf,	/* in: a database page */
	ulint		zip_size);	/* in: size of compressed page;
					0 for uncompressed pages */
osku's avatar
osku committed
416 417 418 419 420 421 422
/**************************************************************************
Gets the space id, page offset, and byte offset within page of a
pointer pointing to a buffer frame containing a file page. */
UNIV_INLINE
void
buf_ptr_get_fsp_addr(
/*=================*/
423
	const void*	ptr,	/* in: pointer to a buffer frame */
osku's avatar
osku committed
424 425 426
	ulint*		space,	/* out: space id */
	fil_addr_t*	addr);	/* out: page offset and byte offset */
/**************************************************************************
427 428
Gets the hash value of a block. This can be used in searches in the
lock hash table. */
osku's avatar
osku committed
429 430
UNIV_INLINE
ulint
431
buf_block_get_lock_hash_val(
osku's avatar
osku committed
432
/*========================*/
433 434 435
					/* out: lock hash value */
	const buf_block_t*	block)	/* in: block */
	__attribute__((const));
436
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
osku's avatar
osku committed
437 438 439 440 441 442
/*************************************************************************
Validates the buffer pool data structure. */

ibool
buf_validate(void);
/*==============*/
443 444
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
#if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
osku's avatar
osku committed
445 446 447 448 449 450
/*************************************************************************
Prints info of the buffer pool data structure. */

void
buf_print(void);
/*============*/
451
#endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */
osku's avatar
osku committed
452 453 454 455 456 457
/************************************************************************
Prints a page to stderr. */

void
buf_page_print(
/*===========*/
marko's avatar
marko committed
458 459 460
	const byte*	read_buf,	/* in: a database page */
	ulint		zip_size);	/* in: compressed page size, or
					0 for uncompressed pages */
osku's avatar
osku committed
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 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
/*************************************************************************
Returns the number of latched pages in the buffer pool. */

ulint
buf_get_latched_pages_number(void);
/*==============================*/
/*************************************************************************
Returns the number of pending buf pool ios. */

ulint
buf_get_n_pending_ios(void);
/*=======================*/
/*************************************************************************
Prints info of the buffer i/o. */

void
buf_print_io(
/*=========*/
	FILE*	file);	/* in: file where to print */
/*************************************************************************
Returns the ratio in percents of modified pages in the buffer pool /
database pages in the buffer pool. */

ulint
buf_get_modified_ratio_pct(void);
/*============================*/
/**************************************************************************
Refreshes the statistics used to print per-second averages. */

void
buf_refresh_io_stats(void);
/*======================*/
/*************************************************************************
Checks that all file pages in the buffer are in a replaceable state. */

ibool
buf_all_freed(void);
/*===============*/
/*************************************************************************
Checks that there currently are no pending i/o-operations for the buffer
pool. */

ibool
buf_pool_check_no_pending_io(void);
/*==============================*/
				/* out: TRUE if there is no pending i/o */
/*************************************************************************
Invalidates the file pages in the buffer pool when an archive recovery is
completed. All the file pages buffered must be in a replaceable state when
this function is called: not latched and not modified. */

void
buf_pool_invalidate(void);
/*=====================*/

/*========================================================================
--------------------------- LOWER LEVEL ROUTINES -------------------------
=========================================================================*/

#ifdef UNIV_SYNC_DEBUG
/*************************************************************************
Adds latch level info for the rw-lock protecting the buffer frame. This
should be called in the debug version after a successful latching of a
page if we know the latching order level of the acquired latch. */
UNIV_INLINE
void
527 528 529 530
buf_block_dbg_add_level(
/*====================*/
	buf_block_t*	block,	/* in: buffer page
				where we have acquired latch */
osku's avatar
osku committed
531 532 533
	ulint		level);	/* in: latching order level */
#endif /* UNIV_SYNC_DEBUG */
/*************************************************************************
534 535
Gets the state of a block. */
UNIV_INLINE
536
enum buf_page_state
537 538 539 540 541 542 543 544
buf_page_get_state(
/*===============*/
					/* out: state */
	const buf_page_t*	bpage);	/* in: pointer to the control block */
/*************************************************************************
Gets the state of a block. */
UNIV_INLINE
enum buf_page_state
545 546 547 548 549 550
buf_block_get_state(
/*================*/
					/* out: state */
	const buf_block_t*	block)	/* in: pointer to the control block */
	__attribute__((pure));
/*************************************************************************
551 552 553 554 555 556
Sets the state of a block. */
UNIV_INLINE
void
buf_block_set_state(
/*================*/
	buf_block_t*		block,	/* in/out: pointer to control block */
557
	enum buf_page_state	state);	/* in: state */
558
/*************************************************************************
559 560 561 562 563 564 565 566 567
Determines if a block is mapped to a tablespace. */
UNIV_INLINE
ibool
buf_page_in_file(
/*=============*/
					/* out: TRUE if mapped */
	const buf_page_t*	bpage)	/* in: pointer to control block */
	__attribute__((pure));
/*************************************************************************
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
Get the flush type of a page. */
UNIV_INLINE
enum buf_flush
buf_page_get_flush_type(
/*====================*/
					/* out: flush type */
	const buf_page_t*	bpage)	/* in: buffer page */
	__attribute__((pure));
/*************************************************************************
Set the flush type of a page. */
UNIV_INLINE
void
buf_page_set_flush_type(
/*====================*/
	buf_page_t*	bpage,		/* in: buffer page */
	enum buf_flush	flush_type);	/* in: flush type */
/*************************************************************************
585 586 587 588 589 590 591 592 593
Map a block to a file page. */
UNIV_INLINE
void
buf_block_set_file_page(
/*====================*/
	buf_block_t*		block,	/* in/out: pointer to control block */
	ulint			space,	/* in: tablespace id */
	ulint			page_no);/* in: page number */
/*************************************************************************
osku's avatar
osku committed
594 595 596 597 598 599
Gets a pointer to the memory frame of a block. */
UNIV_INLINE
buf_frame_t*
buf_block_get_frame(
/*================*/
				/* out: pointer to the frame */
600 601
	buf_block_t*	block)	/* in: pointer to the control block */
	__attribute__((const));
osku's avatar
osku committed
602 603 604 605 606 607
/*************************************************************************
Gets the space id of a block. */
UNIV_INLINE
ulint
buf_block_get_space(
/*================*/
608 609 610
					/* out: space id */
	const buf_block_t*	block)	/* in: pointer to the control block */
	__attribute((const));
osku's avatar
osku committed
611 612 613 614 615 616
/*************************************************************************
Gets the page number of a block. */
UNIV_INLINE
ulint
buf_block_get_page_no(
/*==================*/
617 618 619
					/* out: page number */
	const buf_block_t*	block)	/* in: pointer to the control block */
	__attribute((const));
marko's avatar
marko committed
620
/*************************************************************************
621 622 623 624 625 626 627 628 629
Gets the compressed page size of a block. */
UNIV_INLINE
ulint
buf_block_get_zip_size(
/*===================*/
					/* out: compressed page size, or 0 */
	const buf_block_t*	block)	/* in: pointer to the control block */
	__attribute((const));
/*************************************************************************
630 631
Gets the compressed page descriptor corresponding to an uncompressed page
if applicable. */
marko's avatar
marko committed
632 633
UNIV_INLINE
page_zip_des_t*
634
buf_block_get_page_zip(
marko's avatar
marko committed
635 636
/*===================*/
				/* out: compressed page descriptor, or NULL */
637
	buf_block_t*	block)	/* in: pointer to the control block */
638
	__attribute((const));
639
#if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
osku's avatar
osku committed
640 641 642 643 644 645 646 647
/***********************************************************************
Gets the block to whose frame the pointer is pointing to. */
UNIV_INLINE
buf_block_t*
buf_block_align(
/*============*/
			/* out: pointer to block */
	byte*	ptr);	/* in: pointer to a frame */
648 649 650 651 652 653 654 655 656 657
/*************************************************************************
Gets the compressed page descriptor corresponding to an uncompressed page
if applicable. */
UNIV_INLINE
page_zip_des_t*
buf_frame_get_page_zip(
/*===================*/
			/* out: compressed page descriptor, or NULL */
	byte*	ptr)	/* in: pointer to the page */
	__attribute((const));
658
#endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
osku's avatar
osku committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
/************************************************************************
This function is used to get info if there is an io operation
going on on a buffer page. */
UNIV_INLINE
ibool
buf_page_io_query(
/*==============*/
				/* out: TRUE if io going on */
	buf_block_t*	block);	/* in: pool block, must be bufferfixed */
/************************************************************************
Function which inits a page for read to the buffer buf_pool. If the page is
(1) already in buf_pool, or
(2) if we specify to read only ibuf pages and the page is not an ibuf page, or
(3) if the space is deleted or being deleted,
then this function does nothing.
Sets the io_fix flag to BUF_IO_READ and sets a non-recursive exclusive lock
on the buffer frame. The io-handler must take care that the flag is cleared
and the lock released later. This is one of the functions which perform the
state transition NOT_USED => FILE_PAGE to a block (the other is
678
buf_page_create). */
osku's avatar
osku committed
679 680 681 682 683 684 685 686

buf_block_t*
buf_page_init_for_read(
/*===================*/
				/* out: pointer to the block or NULL */
	ulint*		err,	/* out: DB_SUCCESS or DB_TABLESPACE_DELETED */
	ulint		mode,	/* in: BUF_READ_IBUF_PAGES_ONLY, ... */
	ulint		space,	/* in: space id */
687
	ulint		zip_size,/* in: compressed page size, or 0 */
osku's avatar
osku committed
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	ib_longlong	tablespace_version,/* in: prevents reading from a wrong
				version of the tablespace in case we have done
				DISCARD + IMPORT */
	ulint		offset);/* in: page number */
/************************************************************************
Completes an asynchronous read or write request of a file page to or from
the buffer pool. */

void
buf_page_io_complete(
/*=================*/
	buf_block_t*	block);	/* in: pointer to the block in question */
/************************************************************************
Calculates a folded value of a file page address to use in the page hash
table. */
UNIV_INLINE
ulint
buf_page_address_fold(
/*==================*/
			/* out: the folded value */
	ulint	space,	/* in: space id */
	ulint	offset);/* in: offset of the page within space */
/**********************************************************************
Returns the control block of a file page, NULL if not found. */
UNIV_INLINE
713
buf_page_t*
osku's avatar
osku committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
buf_page_hash_get(
/*==============*/
			/* out: block, NULL if not found */
	ulint	space,	/* in: space id */
	ulint	offset);/* in: offset of the page within space */
/***********************************************************************
Increments the pool clock by one and returns its new value. Remember that
in the 32 bit version the clock wraps around at 4 billion! */
UNIV_INLINE
ulint
buf_pool_clock_tic(void);
/*====================*/
			/* out: new clock value */
/*************************************************************************
Gets the current length of the free list of buffer blocks. */

ulint
buf_get_free_list_len(void);
/*=======================*/


735

736 737 738 739 740 741 742
/* The common buffer control block structure
for compressed and uncompressed frames */

struct buf_page_struct{
	ulint		space:32;	/* tablespace id */
	ulint		offset:32;	/* page number */
	page_zip_des_t	zip;		/* compressed page; zip.state
743 744
					and zip.flush_type are relevant
					for all pages */
745 746
	buf_page_t*	hash;		/* node used in chaining to the page
					hash table */
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762

	/* 2. Page flushing fields; protected by buf_pool->mutex */

	UT_LIST_NODE_T(buf_page_t) flush_list;
					/* node of the modified, not yet
					flushed blocks list */
	ib_ulonglong	newest_modification;
					/* log sequence number of the youngest
					modification to this block, zero if
					not modified */
	ib_ulonglong	oldest_modification;
					/* log sequence number of the START of
					the log entry written of the oldest
					modification to this block which has
					not yet been flushed on disk; zero if
					all modifications are on disk */
763 764 765 766 767
#ifdef UNIV_DEBUG_FILE_ACCESSES
	ibool		file_page_was_freed;
					/* this is set to TRUE when fsp
					frees a page in buffer pool */
#endif /* UNIV_DEBUG_FILE_ACCESSES */
768 769
};

osku's avatar
osku committed
770 771 772 773 774 775
/* The buffer control block structure */

struct buf_block_struct{

	/* 1. General fields */

776 777 778 779
	buf_page_t	page;		/* page information; this must
					be the first field, so that
					buf_pool->page_hash can point
					to buf_page_t or buf_block_t */
osku's avatar
osku committed
780 781 782
	byte*		frame;		/* pointer to buffer frame which
					is of size UNIV_PAGE_SIZE, and
					aligned to an address divisible by
783
					UNIV_PAGE_SIZE */
784 785 786 787 788 789
	mutex_t		mutex;		/* mutex protecting this block:
					state (also protected by the buffer
					pool mutex), io_fix, buf_fix_count,
					and accessed; we introduce this new
					mutex in InnoDB-5.1 to relieve
					contention on the buffer pool mutex */
osku's avatar
osku committed
790 791
	rw_lock_t	lock;		/* read-write lock of the buffer
					frame */
792 793 794
	ulint		lock_hash_val:32;/* hashed value of the page address
					in the record lock hash table */
	ulint		check_index_page_at_flush:1;
osku's avatar
osku committed
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
					/* TRUE if we know that this is
					an index page, and want the database
					to check its consistency before flush;
					note that there may be pages in the
					buffer pool which are index pages,
					but this flag is not set because
					we do not keep track of all pages */

	/* 3. LRU replacement algorithm fields */

	UT_LIST_NODE_T(buf_block_t) free;
					/* node of the free block list */
	ibool		in_free_list;	/* TRUE if in the free list; used in
					debugging */
	UT_LIST_NODE_T(buf_block_t) LRU;
					/* node of the LRU list */
811
#ifdef UNIV_DEBUG
osku's avatar
osku committed
812 813
	ibool		in_LRU_list;	/* TRUE of the page is in the LRU list;
					used in debugging */
814
#endif /* UNIV_DEBUG */
osku's avatar
osku committed
815 816 817 818 819 820 821 822 823
	ulint		LRU_position;	/* value which monotonically
					decreases (or may stay constant if
					the block is in the old blocks) toward
					the end of the LRU list, if the pool
					ulint_clock has not wrapped around:
					NOTE that this value can only be used
					in heuristic algorithms, because of
					the possibility of a wrap-around! */
	ulint		freed_page_clock;/* the value of freed_page_clock
824 825
					of the buffer pool when this block was
					the last time put to the head of the
826 827 828 829 830 831 832
					LRU list; protected by buf_pool->mutex;
					a thread is allowed to read this for
					heuristic purposes without holding any
					mutex or latch */
	ulint		old:1;		/* TRUE if the block is in the old
					blocks in the LRU list; protected
					by buf_pool->mutex */
833
	ulint		accessed:1;	/* TRUE if the page has been accessed
osku's avatar
osku committed
834 835
					while in the buffer pool: read-ahead
					may read in pages which have not been
836 837 838 839
					accessed yet; this is protected by
					block->mutex; a thread is allowed to
					read this for heuristic purposes
					without holding any mutex or latch */
840
	ulint		io_fix:2;	/* if a read is pending to the frame,
osku's avatar
osku committed
841
					io_fix is BUF_IO_READ, in the case
842 843
					of a write BUF_IO_WRITE, otherwise 0;
					this is protected by block->mutex */
844 845 846
	ulint		buf_fix_count:29;/* count of how manyfold this block
					is currently bufferfixed; this is
					protected by block->mutex */
osku's avatar
osku committed
847 848
	/* 4. Optimistic search field */

849
	ib_ulonglong	modify_clock;	/* this clock is incremented every
osku's avatar
osku committed
850 851 852 853 854 855 856 857 858 859 860 861 862
					time a pointer to a record on the
					page may become obsolete; this is
					used in the optimistic cursor
					positioning: if the modify clock has
					not changed, we know that the pointer
					is still valid; this field may be
					changed if the thread (1) owns the
					pool mutex and the page is not
					bufferfixed, or (2) the thread has an
					x-latch on the block */

	/* 5. Hash search fields: NOTE that the first 4 fields are NOT
	protected by any semaphore! */
863

osku's avatar
osku committed
864 865 866 867 868 869
	ulint		n_hash_helps;	/* counter which controls building
					of a new hash index for the page */
	ulint		n_fields;	/* recommended prefix length for hash
					search: number of full fields */
	ulint		n_bytes;	/* recommended prefix: number of bytes
					in an incomplete field */
870
	ibool		left_side;	/* TRUE or FALSE, depending on
osku's avatar
osku committed
871 872 873
					whether the leftmost record of several
					records with the same prefix should be
					indexed in the hash index */
874

875 876 877 878 879 880 881
	/* These 6 fields may only be modified when we have
	an x-latch on btr_search_latch AND
	a) we are holding an s-latch or x-latch on block->lock or
	b) we know that block->buf_fix_count == 0.

	An exception to this is when we init or create a page
	in the buffer pool in buf0buf.c. */
osku's avatar
osku committed
882

883
#ifdef UNIV_DEBUG
osku's avatar
osku committed
884 885 886
	ulint		n_pointers;	/* used in debugging: the number of
					pointers in the adaptive hash index
					pointing to this frame */
887
#endif /* UNIV_DEBUG */
888 889 890 891 892 893 894
	ulint		is_hashed:1;	/* TRUE if hash index has already been
					built on this page; note that it does
					not guarantee that the index is
					complete, though: there may have been
					hash collisions, record deletions,
					etc. */
	ulint		curr_n_fields:10;/* prefix length for hash indexing:
osku's avatar
osku committed
895
					number of full fields */
896 897
	ulint		curr_n_bytes:15;/* number of bytes in hash indexing */
	ibool		curr_left_side:1;/* TRUE or FALSE in hash indexing */
898 899
	dict_index_t*	index;		/* Index for which the adaptive
					hash index has been created. */
osku's avatar
osku committed
900 901 902 903 904 905 906 907 908
	/* 6. Debug fields */
#ifdef UNIV_SYNC_DEBUG
	rw_lock_t	debug_latch;	/* in the debug version, each thread
					which bufferfixes the block acquires
					an s-latch here; so we can use the
					debug utilities in sync0rw */
#endif
};

909 910 911 912
/* Check if a block is in a valid state. */
#define buf_block_state_valid(block)		\
(buf_block_get_state(block) >= BUF_BLOCK_NOT_USED		\
 && (buf_block_get_state(block) <= BUF_BLOCK_REMOVE_HASH))
osku's avatar
osku committed
913 914 915 916 917 918 919 920 921 922 923

/* The buffer pool structure. NOTE! The definition appears here only for
other modules of this directory (buf) to see it. Do not use from outside! */

struct buf_pool_struct{

	/* 1. General fields */

	mutex_t		mutex;		/* mutex protecting the buffer pool
					struct and control blocks, except the
					read-write lock in them */
924 925
	ulint		n_chunks;	/* number of buffer pool chunks */
	buf_chunk_t*	chunks;		/* buffer pool chunks */
926
	ulint		curr_size;	/* current pool size in pages */
osku's avatar
osku committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
	hash_table_t*	page_hash;	/* hash table of the file pages */

	ulint		n_pend_reads;	/* number of pending read operations */

	time_t		last_printout_time; /* when buf_print was last time
					called */
	ulint		n_pages_read;	/* number read operations */
	ulint		n_pages_written;/* number write operations */
	ulint		n_pages_created;/* number of pages created in the pool
					with no read */
	ulint		n_page_gets;	/* number of page gets performed;
					also successful searches through
					the adaptive hash index are
					counted as page gets; this field
					is NOT protected by the buffer
					pool mutex */
	ulint		n_page_gets_old;/* n_page_gets when buf_print was
					last time called: used to calculate
					hit rate */
	ulint		n_pages_read_old;/* n_pages_read when buf_print was
					last time called */
	ulint		n_pages_written_old;/* number write operations */
	ulint		n_pages_created_old;/* number of pages created in
					the pool with no read */
	/* 2. Page flushing algorithm fields */

953
	UT_LIST_BASE_NODE_T(buf_page_t) flush_list;
osku's avatar
osku committed
954 955
					/* base node of the modified block
					list */
956
	ibool		init_flush[BUF_FLUSH_N_TYPES];
osku's avatar
osku committed
957 958
					/* this is TRUE when a flush of the
					given type is being initialized */
959
	ulint		n_flush[BUF_FLUSH_N_TYPES];
osku's avatar
osku committed
960 961
					/* this is the number of pending
					writes in the given flush type */
962
	os_event_t	no_flush[BUF_FLUSH_N_TYPES];
osku's avatar
osku committed
963 964 965 966 967 968 969 970 971 972 973
					/* this is in the set state when there
					is no flush batch of the given type
					running */
	ulint		ulint_clock;	/* a sequence number used to count
					time. NOTE! This counter wraps
					around at 4 billion (if ulint ==
					32 bits)! */
	ulint		freed_page_clock;/* a sequence number used to count the
					number of buffer blocks removed from
					the end of the LRU list; NOTE that
					this counter may wrap around at 4
974 975 976
					billion! A thread is allowed to
					read this for heuristic purposes
					without holding any mutex or latch */
osku's avatar
osku committed
977 978 979 980 981 982 983 984
	ulint		LRU_flush_ended;/* when an LRU flush ends for a page,
					this is incremented by one; this is
					set to zero when a buffer block is
					allocated */

	/* 3. LRU replacement algorithm fields */

	UT_LIST_BASE_NODE_T(buf_block_t) free;
985
					/* base node of the free block list */
osku's avatar
osku committed
986 987
	UT_LIST_BASE_NODE_T(buf_block_t) LRU;
					/* base node of the LRU list */
988
	buf_block_t*	LRU_old;	/* pointer to the about 3/8 oldest
osku's avatar
osku committed
989 990 991 992 993 994 995 996 997 998
					blocks in the LRU list; NULL if LRU
					length less than BUF_LRU_OLD_MIN_LEN */
	ulint		LRU_old_len;	/* length of the LRU list from
					the block to which LRU_old points
					onward, including that block;
					see buf0lru.c for the restrictions
					on this value; not defined if
					LRU_old == NULL */
};

999 1000 1001
/* Io_fix states of a control block; these must be 1..3 */
#define BUF_IO_READ		1
#define BUF_IO_WRITE		2
osku's avatar
osku committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013

/************************************************************************
Let us list the consistency conditions for different control block states.

NOT_USED:	is in free list, not in LRU list, not in flush list, nor
		page hash table
READY_FOR_USE:	is not in free list, LRU list, or flush list, nor page
		hash table
MEMORY:		is not in free list, LRU list, or flush list, nor page
		hash table
FILE_PAGE:	space and offset are defined, is in page hash table
		if io_fix == BUF_IO_WRITE,
1014 1015
			pool: no_flush[flush_type] is in reset state,
			pool: n_flush[flush_type] > 0
1016

osku's avatar
osku committed
1017 1018 1019 1020 1021 1022 1023 1024
		(1) if buf_fix_count == 0, then
			is in LRU list, not in free list
			is in flush list,
				if and only if oldest_modification > 0
			is x-locked,
				if and only if io_fix == BUF_IO_READ
			is s-locked,
				if and only if io_fix == BUF_IO_WRITE
1025

osku's avatar
osku committed
1026 1027 1028 1029
		(2) if buf_fix_count > 0, then
			is not in LRU list, not in free list
			is in flush list,
				if and only if oldest_modification > 0
1030
			if io_fix == BUF_IO_READ,
osku's avatar
osku committed
1031 1032 1033
				is x-locked
			if io_fix == BUF_IO_WRITE,
				is s-locked
1034

osku's avatar
osku committed
1035 1036 1037 1038 1039 1040
State transitions:

NOT_USED => READY_FOR_USE
READY_FOR_USE => MEMORY
READY_FOR_USE => FILE_PAGE
MEMORY => NOT_USED
1041
FILE_PAGE => NOT_USED	NOTE: This transition is allowed if and only if
osku's avatar
osku committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
				(1) buf_fix_count == 0,
				(2) oldest_modification == 0, and
				(3) io_fix == 0.
*/

#ifndef UNIV_NONINL
#include "buf0buf.ic"
#endif

#endif