buf0buf.c 68.8 KB
Newer Older
1
/*   Innobase relational database engine; Copyright (C) 2001 Innobase Oy
2

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

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

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
     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 buf_pool

(c) 1995 Innobase Oy

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

#include "buf0buf.h"

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

#include "mem0mem.h"
#include "btr0btr.h"
#include "fil0fil.h"
#include "lock0lock.h"
#include "btr0sea.h"
#include "ibuf0ibuf.h"
#include "dict0dict.h"
#include "log0recv.h"
37
#include "log0log.h"
38 39
#include "trx0undo.h"
#include "srv0srv.h"
40 41 42 43 44

/*
		IMPLEMENTATION OF THE BUFFER POOL
		=================================

45
Performance improvement:
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
------------------------
Thread scheduling in NT may be so slow that the OS wait mechanism should
not be used even in waiting for disk reads to complete.
Rather, we should put waiting query threads to the queue of
waiting jobs, and let the OS thread do something useful while the i/o
is processed. In this way we could remove most OS thread switches in
an i/o-intensive benchmark like TPC-C.

A possibility is to put a user space thread library between the database
and NT. User space thread libraries might be very fast.

SQL Server 7.0 can be configured to use 'fibers' which are lightweight
threads in NT. These should be studied.

		Buffer frames and blocks
		------------------------
Following the terminology of Gray and Reuter, we call the memory
blocks where file pages are loaded buffer frames. For each buffer
frame there is a control block, or shortly, a block, in the buffer
control array. The control info which does not need to be stored
in the file along with the file page, resides in the control block.

		Buffer pool struct
		------------------
The buffer buf_pool contains a single mutex which protects all the
control data structures of the buf_pool. The content of a buffer frame is
protected by a separate read-write lock in its control block, though.
These locks can be locked and unlocked without owning the buf_pool mutex.
The OS events in the buf_pool struct can be waited for without owning the
buf_pool mutex.

The buf_pool mutex is a hot-spot in main memory, causing a lot of
memory bus traffic on multiprocessor systems when processors
alternately access the mutex. On our Pentium, the mutex is accessed
maybe every 10 microseconds. We gave up the solution to have mutexes
for each control block, for instance, because it seemed to be
complicated.

A solution to reduce mutex contention of the buf_pool mutex is to
create a separate mutex for the page hash table. On Pentium,
accessing the hash table takes 2 microseconds, about half
of the total buf_pool mutex hold time.

		Control blocks
		--------------

The control block contains, for instance, the bufferfix count
which is incremented when a thread wants a file page to be fixed
in a buffer frame. The bufferfix operation does not lock the
contents of the frame, however. For this purpose, the control
block contains a read-write lock.

The buffer frames have to be aligned so that the start memory
address of a frame is divisible by the universal page size, which
is a power of two.

We intend to make the buffer buf_pool size on-line reconfigurable,
that is, the buf_pool size can be changed without closing the database.
Then the database administarator may adjust it to be bigger
at night, for example. The control block array must
contain enough control blocks for the maximum buffer buf_pool size
which is used in the particular database.
If the buf_pool size is cut, we exploit the virtual memory mechanism of
the OS, and just refrain from using frames at high addresses. Then the OS
can swap them to disk.

The control blocks containing file pages are put to a hash table
according to the file address of the page.
We could speed up the access to an individual page by using
"pointer swizzling": we could replace the page references on
non-leaf index pages by direct pointers to the page, if it exists
in the buf_pool. We could make a separate hash table where we could
chain all the page references in non-leaf pages residing in the buf_pool,
using the page reference as the hash key,
and at the time of reading of a page update the pointers accordingly.
Drawbacks of this solution are added complexity and,
possibly, extra space required on non-leaf pages for memory pointers.
A simpler solution is just to speed up the hash table mechanism
in the database, using tables whose size is a power of 2.

		Lists of blocks
		---------------

There are several lists of control blocks. The free list contains
blocks which are currently not used.

The LRU-list contains all the blocks holding a file page
except those for which the bufferfix count is non-zero.
The pages are in the LRU list roughly in the order of the last
access to the page, so that the oldest pages are at the end of the
list. We also keep a pointer to near the end of the LRU list,
which we can use when we want to artificially age a page in the
buf_pool. This is used if we know that some page is not needed
again for some time: we insert the block right after the pointer,
causing it to be replaced sooner than would noramlly be the case.
Currently this aging mechanism is used for read-ahead mechanism
of pages, and it can also be used when there is a scan of a full
table which cannot fit in the memory. Putting the pages near the
of the LRU list, we make sure that most of the buf_pool stays in the
main memory, undisturbed.

The chain of modified blocks contains the blocks
holding file pages that have been modified in the memory
but not written to disk yet. The block with the oldest modification
which has not yet been written to disk is at the end of the chain.

		Loading a file page
		-------------------

First, a victim block for replacement has to be found in the
buf_pool. It is taken from the free list or searched for from the
end of the LRU-list. An exclusive lock is reserved for the frame,
the io_fix field is set in the block fixing the block in buf_pool,
and the io-operation for loading the page is queued. The io-handler thread
releases the X-lock on the frame and resets the io_fix field
when the io operation completes.

unknown's avatar
unknown committed
163 164
A thread may request the above operation using the function
buf_page_get(). It may then continue to request a lock on the frame.
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
The lock is granted when the io-handler releases the x-lock.

		Read-ahead
		----------

The read-ahead mechanism is intended to be intelligent and
isolated from the semantically higher levels of the database
index management. From the higher level we only need the
information if a file page has a natural successor or
predecessor page. On the leaf level of a B-tree index,
these are the next and previous pages in the natural
order of the pages.

Let us first explain the read-ahead mechanism when the leafs
of a B-tree are scanned in an ascending or descending order.
When a read page is the first time referenced in the buf_pool,
the buffer manager checks if it is at the border of a so-called
linear read-ahead area. The tablespace is divided into these
areas of size 64 blocks, for example. So if the page is at the
border of such an area, the read-ahead mechanism checks if
all the other blocks in the area have been accessed in an
ascending or descending order. If this is the case, the system
looks at the natural successor or predecessor of the page,
checks if that is at the border of another area, and in this case
issues read-requests for all the pages in that area. Maybe
we could relax the condition that all the pages in the area
have to be accessed: if data is deleted from a table, there may
appear holes of unused pages in the area.

A different read-ahead mechanism is used when there appears
to be a random access pattern to a file.
If a new page is referenced in the buf_pool, and several pages
of its random access area (for instance, 32 consecutive pages
in a tablespace) have recently been referenced, we may predict
that the whole area may be needed in the near future, and issue
unknown's avatar
unknown committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
the read requests for the whole area.

		AWE implementation
		------------------

By a 'block' we mean the buffer header of type buf_block_t. By a 'page'
we mean the physical 16 kB memory area allocated from RAM for that block.
By a 'frame' we mean a 16 kB area in the virtual address space of the
process, in the frame_mem of buf_pool.

We can map pages to the frames of the buffer pool.

1) A buffer block allocated to use as a non-data page, e.g., to the lock
table, is always mapped to a frame.
2) A bufferfixed or io-fixed data page is always mapped to a frame.
3) When we need to map a block to frame, we look from the list
awe_LRU_free_mapped and try to unmap its last block, but note that
bufferfixed or io-fixed pages cannot be unmapped.
4) For every frame in the buffer pool there is always a block whose page is
mapped to it. When we create the buffer pool, we map the first elements
in the free list to the frames.
5) When we have AWE enabled, we disable adaptive hash indexes.
*/
223

unknown's avatar
unknown committed
224 225 226
/* Value in microseconds */
static const int WAIT_FOR_READ	= 20000;

227 228 229
/* Number of attemtps made to read in a page in the buffer pool */
static const ulint BUF_PAGE_READ_MAX_RETRIES = 100;

230 231
buf_pool_t*	buf_pool = NULL; /* The buffer buf_pool of the database */

232
#ifdef UNIV_DEBUG
233
ulint		buf_dbg_counter	= 0; /* This is used to insert validation
234 235 236 237 238
					operations in excution in the
					debug version */
ibool		buf_debug_prints = FALSE; /* If this is set TRUE,
					the program prints info whenever
					read-ahead or flush occurs */
239
#endif /* UNIV_DEBUG */
240 241
/************************************************************************
Calculates a page checksum which is stored to the page when it is written
unknown's avatar
unknown committed
242 243
to a file. Note that we must be careful to calculate the same value on
32-bit and 64-bit architectures. */
244 245

ulint
unknown's avatar
unknown committed
246 247
buf_calc_page_new_checksum(
/*=======================*/
248 249
			/* out: checksum */
	byte*	 page)	/* in: buffer page */
250
{
251
	ulint checksum;
252

253 254 255 256
	/* Since the field FIL_PAGE_FILE_FLUSH_LSN, and in versions <= 4.1.x
	..._ARCH_LOG_NO, are written outside the buffer pool to the first
	pages of data files, we have to skip them in the page checksum
	calculation.
unknown's avatar
unknown committed
257 258 259
	We must also skip the field FIL_PAGE_SPACE_OR_CHKSUM where the
	checksum is stored, and also the last 8 bytes of page because
	there we store the old formula checksum. */
260 261

	checksum = ut_fold_binary(page + FIL_PAGE_OFFSET,
unknown's avatar
unknown committed
262 263 264 265
				  FIL_PAGE_FILE_FLUSH_LSN - FIL_PAGE_OFFSET)
		+ ut_fold_binary(page + FIL_PAGE_DATA,
				 UNIV_PAGE_SIZE - FIL_PAGE_DATA
				 - FIL_PAGE_END_LSN_OLD_CHKSUM);
266
	checksum = checksum & 0xFFFFFFFFUL;
unknown's avatar
unknown committed
267

268
	return(checksum);
unknown's avatar
unknown committed
269 270 271 272 273
}

/************************************************************************
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
274
checksum.
unknown's avatar
unknown committed
275 276 277 278 279 280 281
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(
/*=======================*/
282 283
			/* out: checksum */
	byte*	 page)	/* in: buffer page */
unknown's avatar
unknown committed
284
{
285 286 287
	ulint checksum;

	checksum = ut_fold_binary(page, FIL_PAGE_FILE_FLUSH_LSN);
unknown's avatar
unknown committed
288

289
	checksum = checksum & 0xFFFFFFFFUL;
290

291
	return(checksum);
292 293 294 295 296 297 298 299 300 301 302 303
}

/************************************************************************
Checks if a page is corrupt. */

ibool
buf_page_is_corrupted(
/*==================*/
				/* out: TRUE if corrupted */
	byte*	read_buf)	/* in: a database page */
{
	ulint	checksum;
unknown's avatar
unknown committed
304 305 306
	ulint	old_checksum;
	ulint	checksum_field;
	ulint	old_checksum_field;
unknown's avatar
unknown committed
307
#ifndef UNIV_HOTBACKUP
308
	dulint	current_lsn;
unknown's avatar
unknown committed
309
#endif
unknown's avatar
unknown committed
310
	if (mach_read_from_4(read_buf + FIL_PAGE_LSN + 4)
unknown's avatar
unknown committed
311 312
	    != mach_read_from_4(read_buf + UNIV_PAGE_SIZE
				- FIL_PAGE_END_LSN_OLD_CHKSUM + 4)) {
313

unknown's avatar
unknown committed
314
		/* Stored log sequence numbers at the start and the end
unknown's avatar
unknown committed
315
		of page do not match */
unknown's avatar
unknown committed
316 317 318 319

		return(TRUE);
	}

320 321 322
#ifndef UNIV_HOTBACKUP
	if (recv_lsn_checks_on && log_peek_lsn(&current_lsn)) {
		if (ut_dulint_cmp(current_lsn,
unknown's avatar
unknown committed
323 324
				  mach_read_from_8(read_buf + FIL_PAGE_LSN))
		    < 0) {
325 326 327
			ut_print_timestamp(stderr);

			fprintf(stderr,
unknown's avatar
unknown committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
				"  InnoDB: Error: page %lu log sequence number"
				" %lu %lu\n"
				"InnoDB: is in the future! Current system "
				"log sequence number %lu %lu.\n"
				"InnoDB: Your database may be corrupt or "
				"you may have copied the InnoDB\n"
				"InnoDB: tablespace but not the InnoDB "
				"log files. See\n"
				"InnoDB: http://dev.mysql.com/doc/refman/"
				"5.1/en/forcing-recovery.html\n"
				"InnoDB: for more information.\n",
				(ulong) mach_read_from_4(read_buf
							 + FIL_PAGE_OFFSET),
				(ulong) ut_dulint_get_high
				(mach_read_from_8(read_buf + FIL_PAGE_LSN)),
				(ulong) ut_dulint_get_low
				(mach_read_from_8(read_buf + FIL_PAGE_LSN)),
345 346
				(ulong) ut_dulint_get_high(current_lsn),
				(ulong) ut_dulint_get_low(current_lsn));
347 348 349
		}
	}
#endif
350 351 352 353 354 355 356 357 358

	/* If we use checksums validation, make additional check before
	returning TRUE to ensure that the checksum is not equal to
	BUF_NO_CHECKSUM_MAGIC which might be stored by InnoDB with checksums
	disabled. Otherwise, skip checksum calculation and return FALSE */

	if (srv_use_checksums) {
		old_checksum = buf_calc_page_old_checksum(read_buf);

359 360 361
		old_checksum_field = mach_read_from_4(
			read_buf + UNIV_PAGE_SIZE
			- FIL_PAGE_END_LSN_OLD_CHKSUM);
362 363 364 365 366 367 368 369 370 371

		/* There are 2 valid formulas for old_checksum_field:

		1. Very old versions of InnoDB only stored 8 byte lsn to the
		start and the end of the page.

		2. Newer InnoDB versions store the old formula checksum
		there. */

		if (old_checksum_field != mach_read_from_4(read_buf
unknown's avatar
unknown committed
372 373 374
							   + FIL_PAGE_LSN)
		    && old_checksum_field != old_checksum
		    && old_checksum_field != BUF_NO_CHECKSUM_MAGIC) {
375 376 377 378 379

			return(TRUE);
		}

		checksum = buf_calc_page_new_checksum(read_buf);
unknown's avatar
unknown committed
380 381
		checksum_field = mach_read_from_4(read_buf
						  + FIL_PAGE_SPACE_OR_CHKSUM);
382 383 384 385 386

		/* InnoDB versions < 4.0.14 and < 4.1.1 stored the space id
		(always equal to 0), to FIL_PAGE_SPACE_SPACE_OR_CHKSUM */

		if (checksum_field != 0 && checksum_field != checksum
unknown's avatar
unknown committed
387
		    && checksum_field != BUF_NO_CHECKSUM_MAGIC) {
388 389 390 391 392

			return(TRUE);
		}
	}

393
	return(FALSE);
394 395
}

396 397 398 399 400 401 402 403 404 405
/************************************************************************
Prints a page to stderr. */

void
buf_page_print(
/*===========*/
	byte*	read_buf)	/* in: a database page */
{
	dict_index_t*	index;
	ulint		checksum;
unknown's avatar
unknown committed
406
	ulint		old_checksum;
407

unknown's avatar
unknown committed
408
	ut_print_timestamp(stderr);
409 410 411 412
	fprintf(stderr, "  InnoDB: Page dump in ascii and hex (%lu bytes):\n",
		(ulint)UNIV_PAGE_SIZE);
	ut_print_buf(stderr, read_buf, UNIV_PAGE_SIZE);
	fputs("InnoDB: End of page dump\n", stderr);
413

unknown's avatar
unknown committed
414 415 416 417
	checksum = srv_use_checksums
		? buf_calc_page_new_checksum(read_buf) : BUF_NO_CHECKSUM_MAGIC;
	old_checksum = srv_use_checksums
		? buf_calc_page_old_checksum(read_buf) : BUF_NO_CHECKSUM_MAGIC;
418

unknown's avatar
unknown committed
419
	ut_print_timestamp(stderr);
420
	fprintf(stderr,
unknown's avatar
unknown committed
421 422 423 424 425 426 427 428 429 430 431 432 433
		"  InnoDB: Page checksum %lu, prior-to-4.0.14-form"
		" checksum %lu\n"
		"InnoDB: stored checksum %lu, prior-to-4.0.14-form"
		" stored checksum %lu\n"
		"InnoDB: Page lsn %lu %lu, low 4 bytes of lsn"
		" at page end %lu\n"
		"InnoDB: Page number (if stored to page already) %lu,\n"
		"InnoDB: space id (if created with >= MySQL-4.1.1"
		" and stored already) %lu\n",
		(ulong) checksum, (ulong) old_checksum,
		(ulong) mach_read_from_4(read_buf + FIL_PAGE_SPACE_OR_CHKSUM),
		(ulong) mach_read_from_4(read_buf + UNIV_PAGE_SIZE
					 - FIL_PAGE_END_LSN_OLD_CHKSUM),
434 435 436
		(ulong) mach_read_from_4(read_buf + FIL_PAGE_LSN),
		(ulong) mach_read_from_4(read_buf + FIL_PAGE_LSN + 4),
		(ulong) mach_read_from_4(read_buf + UNIV_PAGE_SIZE
unknown's avatar
unknown committed
437
					 - FIL_PAGE_END_LSN_OLD_CHKSUM + 4),
438
		(ulong) mach_read_from_4(read_buf + FIL_PAGE_OFFSET),
unknown's avatar
unknown committed
439 440
		(ulong) mach_read_from_4(read_buf
					 + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID));
unknown's avatar
unknown committed
441

442
	if (mach_read_from_2(read_buf + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_TYPE)
unknown's avatar
unknown committed
443
	    == TRX_UNDO_INSERT) {
444
		fprintf(stderr,
445 446
			"InnoDB: Page may be an insert undo log page\n");
	} else if (mach_read_from_2(read_buf + TRX_UNDO_PAGE_HDR
unknown's avatar
unknown committed
447 448
				    + TRX_UNDO_PAGE_TYPE)
		   == TRX_UNDO_UPDATE) {
449
		fprintf(stderr,
450 451 452
			"InnoDB: Page may be an update undo log page\n");
	}

453 454 455
	switch (fil_page_get_type(read_buf)) {
	case FIL_PAGE_INDEX:
		fprintf(stderr,
unknown's avatar
unknown committed
456 457 458 459 460 461
			"InnoDB: Page may be an index page where"
			" index id is %lu %lu\n",
			(ulong) ut_dulint_get_high
			(btr_page_get_index_id(read_buf)),
			(ulong) ut_dulint_get_low
			(btr_page_get_index_id(read_buf)));
462

unknown's avatar
unknown committed
463 464 465 466 467
		/* If the code is in ibbackup, dict_sys may be uninitialized,
		i.e., NULL */

		if (dict_sys != NULL) {

468 469
			index = dict_index_find_on_id_low(
				btr_page_get_index_id(read_buf));
470
			if (index) {
471
				fputs("InnoDB: (", stderr);
472
				dict_index_name_print(stderr, NULL, index);
473
				fputs(")\n", stderr);
unknown's avatar
unknown committed
474
			}
475
		}
476 477
		break;
	case FIL_PAGE_INODE:
478
		fputs("InnoDB: Page may be an 'inode' page\n", stderr);
479 480
		break;
	case FIL_PAGE_IBUF_FREE_LIST:
481
		fputs("InnoDB: Page may be an insert buffer free list page\n",
unknown's avatar
unknown committed
482
		      stderr);
483 484 485
		break;
	case FIL_PAGE_TYPE_ALLOCATED:
		fputs("InnoDB: Page may be a freshly allocated page\n",
unknown's avatar
unknown committed
486
		      stderr);
487 488 489
		break;
	case FIL_PAGE_IBUF_BITMAP:
		fputs("InnoDB: Page may be an insert buffer bitmap page\n",
unknown's avatar
unknown committed
490
		      stderr);
491 492 493
		break;
	case FIL_PAGE_TYPE_SYS:
		fputs("InnoDB: Page may be a system page\n",
unknown's avatar
unknown committed
494
		      stderr);
495 496 497
		break;
	case FIL_PAGE_TYPE_TRX_SYS:
		fputs("InnoDB: Page may be a transaction system page\n",
unknown's avatar
unknown committed
498
		      stderr);
499 500 501
		break;
	case FIL_PAGE_TYPE_FSP_HDR:
		fputs("InnoDB: Page may be a file space header page\n",
unknown's avatar
unknown committed
502
		      stderr);
503 504 505
		break;
	case FIL_PAGE_TYPE_XDES:
		fputs("InnoDB: Page may be an extent descriptor page\n",
unknown's avatar
unknown committed
506
		      stderr);
507 508 509
		break;
	case FIL_PAGE_TYPE_BLOB:
		fputs("InnoDB: Page may be a BLOB page\n",
unknown's avatar
unknown committed
510
		      stderr);
511
		break;
512 513 514
	}
}

515 516 517 518 519 520 521
/************************************************************************
Initializes a buffer control block when the buf_pool is created. */
static
void
buf_block_init(
/*===========*/
	buf_block_t*	block,	/* in: pointer to control block */
unknown's avatar
unknown committed
522 523
	byte*		frame)	/* in: pointer to buffer frame, or NULL if in
				the case of AWE there is no frame */
524
{
525 526
	block->magic_n = 0;

527
	block->state = BUF_BLOCK_NOT_USED;
528

529 530
	block->frame = frame;

unknown's avatar
unknown committed
531 532
	block->awe_info = NULL;

533 534 535
	block->buf_fix_count = 0;
	block->io_fix = 0;

536
	block->modify_clock = ut_dulint_zero;
537

unknown's avatar
unknown committed
538 539
	block->file_page_was_freed = FALSE;

unknown's avatar
unknown committed
540
	block->check_index_page_at_flush = FALSE;
unknown's avatar
unknown committed
541
	block->index = NULL;
unknown's avatar
unknown committed
542

unknown's avatar
unknown committed
543 544 545 546 547
	block->in_free_list = FALSE;
	block->in_LRU_list = FALSE;

	block->n_pointers = 0;

unknown's avatar
unknown committed
548 549
	mutex_create(&block->mutex, SYNC_BUF_BLOCK);

unknown's avatar
unknown committed
550
	rw_lock_create(&block->lock, SYNC_LEVEL_VARYING);
551 552
	ut_ad(rw_lock_validate(&(block->lock)));

unknown's avatar
unknown committed
553
#ifdef UNIV_SYNC_DEBUG
unknown's avatar
unknown committed
554
	rw_lock_create(&block->debug_latch, SYNC_NO_ORDER_CHECK);
555
#endif /* UNIV_SYNC_DEBUG */
556 557 558
}

/************************************************************************
unknown's avatar
unknown committed
559 560
Creates the buffer pool. */

561
buf_pool_t*
unknown's avatar
unknown committed
562 563
buf_pool_init(
/*==========*/
564
				/* out, own: buf_pool object, NULL if not
unknown's avatar
unknown committed
565
				enough memory or error */
566 567
	ulint	max_size,	/* in: maximum size of the buf_pool in
				blocks */
unknown's avatar
unknown committed
568
	ulint	curr_size,	/* in: current size to use, must be <=
569 570
				max_size, currently must be equal to
				max_size */
unknown's avatar
unknown committed
571 572 573 574 575
	ulint	n_frames)	/* in: number of frames; if AWE is used,
				this is the size of the address space window
				where physical memory pages are mapped; if
				AWE is not used then this must be the same
				as max_size */
576 577 578 579
{
	byte*		frame;
	ulint		i;
	buf_block_t*	block;
580

581
	ut_a(max_size == curr_size);
unknown's avatar
unknown committed
582
	ut_a(srv_use_awe || n_frames == max_size);
583

unknown's avatar
unknown committed
584
	if (n_frames > curr_size) {
585
		fprintf(stderr,
unknown's avatar
unknown committed
586 587 588 589 590 591
			"InnoDB: AWE: Error: you must specify in my.cnf"
			" .._awe_mem_mb larger\n"
			"InnoDB: than .._buffer_pool_size. Now the former"
			" is %lu pages,\n"
			"InnoDB: the latter %lu pages.\n",
			(ulong) curr_size, (ulong) n_frames);
unknown's avatar
unknown committed
592 593 594 595

		return(NULL);
	}

596 597 598
	buf_pool = mem_alloc(sizeof(buf_pool_t));

	/* 1. Initialize general fields
unknown's avatar
unknown committed
599
	---------------------------- */
unknown's avatar
unknown committed
600
	mutex_create(&buf_pool->mutex, SYNC_BUF_POOL);
601 602

	mutex_enter(&(buf_pool->mutex));
unknown's avatar
unknown committed
603 604 605 606 607 608

	if (srv_use_awe) {
		/*----------------------------------------*/
		/* Allocate the virtual address space window, i.e., the
		buffer pool frames */

609 610
		buf_pool->frame_mem = os_awe_allocate_virtual_mem_window(
			UNIV_PAGE_SIZE * (n_frames + 1));
611

unknown's avatar
unknown committed
612 613 614 615 616
		/* Allocate the physical memory for AWE and the AWE info array
		for buf_pool */

		if ((curr_size % ((1024 * 1024) / UNIV_PAGE_SIZE)) != 0) {

617
			fprintf(stderr,
unknown's avatar
unknown committed
618 619 620 621 622
				"InnoDB: AWE: Error: physical memory must be"
				" allocated in full megabytes.\n"
				"InnoDB: Trying to allocate %lu"
				" database pages.\n",
				(ulong) curr_size);
unknown's avatar
unknown committed
623

624
			return(NULL);
unknown's avatar
unknown committed
625 626 627
		}

		if (!os_awe_allocate_physical_mem(&(buf_pool->awe_info),
unknown's avatar
unknown committed
628 629 630
						  curr_size
						  / ((1024 * 1024)
						     / UNIV_PAGE_SIZE))) {
unknown's avatar
unknown committed
631 632 633 634 635

			return(NULL);
		}
		/*----------------------------------------*/
	} else {
636 637
		buf_pool->frame_mem = os_mem_alloc_large(
			UNIV_PAGE_SIZE * (n_frames + 1), TRUE, FALSE);
unknown's avatar
unknown committed
638
	}
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654

	if (buf_pool->frame_mem == NULL) {

		return(NULL);
	}

	buf_pool->blocks = ut_malloc(sizeof(buf_block_t) * max_size);

	if (buf_pool->blocks == NULL) {

		return(NULL);
	}

	buf_pool->max_size = max_size;
	buf_pool->curr_size = curr_size;

unknown's avatar
unknown committed
655 656
	buf_pool->n_frames = n_frames;

657 658 659 660
	/* Align pointer to the first frame */

	frame = ut_align(buf_pool->frame_mem, UNIV_PAGE_SIZE);

unknown's avatar
unknown committed
661
	buf_pool->frame_zero = frame;
662
	buf_pool->high_end = frame + UNIV_PAGE_SIZE * n_frames;
663

unknown's avatar
unknown committed
664 665 666 667 668 669
	if (srv_use_awe) {
		/*----------------------------------------*/
		/* Map an initial part of the allocated physical memory to
		the window */

		os_awe_map_physical_mem_to_window(buf_pool->frame_zero,
unknown's avatar
unknown committed
670 671 672 673
						  n_frames
						  * (UNIV_PAGE_SIZE
						     / OS_AWE_X86_PAGE_SIZE),
						  buf_pool->awe_info);
unknown's avatar
unknown committed
674 675 676 677
		/*----------------------------------------*/
	}

	buf_pool->blocks_of_frames = ut_malloc(sizeof(void*) * n_frames);
678

unknown's avatar
unknown committed
679 680 681 682 683 684 685 686 687 688
	if (buf_pool->blocks_of_frames == NULL) {

		return(NULL);
	}

	/* Init block structs and assign frames for them; in the case of
	AWE there are less frames than blocks. Then we assign the frames
	to the first blocks (we already mapped the memory above). We also
	init the awe_info for every block. */

689 690 691
	for (i = 0; i < max_size; i++) {

		block = buf_pool_get_nth_block(buf_pool, i);
unknown's avatar
unknown committed
692 693 694 695 696 697 698

		if (i < n_frames) {
			frame = buf_pool->frame_zero + i * UNIV_PAGE_SIZE;
			*(buf_pool->blocks_of_frames + i) = block;
		} else {
			frame = NULL;
		}
699

700
		buf_block_init(block, frame);
unknown's avatar
unknown committed
701 702 703 704 705 706 707

		if (srv_use_awe) {
			/*----------------------------------------*/
			block->awe_info = buf_pool->awe_info
				+ i * (UNIV_PAGE_SIZE / OS_AWE_X86_PAGE_SIZE);
			/*----------------------------------------*/
		}
708
	}
unknown's avatar
unknown committed
709

unknown's avatar
unknown committed
710
	buf_pool->page_hash = hash_create(2 * max_size);
711 712

	buf_pool->n_pend_reads = 0;
713 714 715

	buf_pool->last_printout_time = time(NULL);

716 717 718
	buf_pool->n_pages_read = 0;
	buf_pool->n_pages_written = 0;
	buf_pool->n_pages_created = 0;
unknown's avatar
unknown committed
719
	buf_pool->n_pages_awe_remapped = 0;
720

721 722 723
	buf_pool->n_page_gets = 0;
	buf_pool->n_page_gets_old = 0;
	buf_pool->n_pages_read_old = 0;
724 725
	buf_pool->n_pages_written_old = 0;
	buf_pool->n_pages_created_old = 0;
unknown's avatar
unknown committed
726
	buf_pool->n_pages_awe_remapped_old = 0;
727

728
	/* 2. Initialize flushing fields
unknown's avatar
unknown committed
729
	---------------------------- */
730 731 732 733
	UT_LIST_INIT(buf_pool->flush_list);

	for (i = BUF_FLUSH_LRU; i <= BUF_FLUSH_LIST; i++) {
		buf_pool->n_flush[i] = 0;
734
		buf_pool->init_flush[i] = FALSE;
735 736 737 738 739 740 741
		buf_pool->no_flush[i] = os_event_create(NULL);
	}

	buf_pool->LRU_flush_ended = 0;

	buf_pool->ulint_clock = 1;
	buf_pool->freed_page_clock = 0;
742

743
	/* 3. Initialize LRU fields
unknown's avatar
unknown committed
744
	---------------------------- */
745 746 747 748
	UT_LIST_INIT(buf_pool->LRU);

	buf_pool->LRU_old = NULL;

unknown's avatar
unknown committed
749 750
	UT_LIST_INIT(buf_pool->awe_LRU_free_mapped);

751 752
	/* Add control blocks to the free list */
	UT_LIST_INIT(buf_pool->free);
unknown's avatar
unknown committed
753

754 755 756
	for (i = 0; i < curr_size; i++) {

		block = buf_pool_get_nth_block(buf_pool, i);
757

unknown's avatar
unknown committed
758 759 760 761
		if (block->frame) {
			/* Wipe contents of frame to eliminate a Purify
			warning */

unknown's avatar
unknown committed
762
#ifdef HAVE_purify
unknown's avatar
unknown committed
763
			memset(block->frame, '\0', UNIV_PAGE_SIZE);
unknown's avatar
unknown committed
764
#endif
unknown's avatar
unknown committed
765 766 767
			if (srv_use_awe) {
				/* Add to the list of blocks mapped to
				frames */
768

unknown's avatar
unknown committed
769
				UT_LIST_ADD_LAST(awe_LRU_free_mapped,
unknown's avatar
unknown committed
770 771
						 buf_pool->awe_LRU_free_mapped,
						 block);
unknown's avatar
unknown committed
772 773
			}
		}
774

unknown's avatar
unknown committed
775
		UT_LIST_ADD_LAST(free, buf_pool->free, block);
unknown's avatar
unknown committed
776
		block->in_free_list = TRUE;
777 778 779 780
	}

	mutex_exit(&(buf_pool->mutex));

unknown's avatar
unknown committed
781
	if (srv_use_adaptive_hash_indexes) {
unknown's avatar
unknown committed
782 783
		btr_search_sys_create(curr_size * UNIV_PAGE_SIZE
				      / sizeof(void*) / 64);
unknown's avatar
unknown committed
784
	} else {
785 786
		/* Create only a small dummy system */
		btr_search_sys_create(1000);
unknown's avatar
unknown committed
787
	}
788 789

	return(buf_pool);
790
}
791 792

/************************************************************************
unknown's avatar
unknown committed
793 794
Maps the page of block to a frame, if not mapped yet. Unmaps some page
from the end of the awe_LRU_free_mapped. */
795 796

void
unknown's avatar
unknown committed
797 798 799 800 801 802 803 804
buf_awe_map_page_to_frame(
/*======================*/
	buf_block_t*	block,		/* in: block whose page should be
					mapped to a frame */
	ibool		add_to_mapped_list) /* in: TRUE if we in the case
					we need to map the page should also
					add the block to the
					awe_LRU_free_mapped list */
805
{
unknown's avatar
unknown committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819
	buf_block_t*	bck;

	ut_ad(mutex_own(&(buf_pool->mutex)));
	ut_ad(block);

	if (block->frame) {

		return;
	}

	/* Scan awe_LRU_free_mapped from the end and try to find a block
	which is not bufferfixed or io-fixed */

	bck = UT_LIST_GET_LAST(buf_pool->awe_LRU_free_mapped);
820

821
	while (bck) {
unknown's avatar
unknown committed
822 823 824 825 826 827 828 829 830
		ibool skip;

		mutex_enter(&bck->mutex);

		skip = (bck->state == BUF_BLOCK_FILE_PAGE
			&& (bck->buf_fix_count != 0 || bck->io_fix != 0));

		if (skip) {
			mutex_exit(&bck->mutex);
unknown's avatar
unknown committed
831 832 833 834 835 836

			/* We have to skip this */
			bck = UT_LIST_GET_PREV(awe_LRU_free_mapped, bck);
		} else {
			/* We can map block to the frame of bck */

837 838 839 840
			os_awe_map_physical_mem_to_window(
				bck->frame,
				UNIV_PAGE_SIZE / OS_AWE_X86_PAGE_SIZE,
				block->awe_info);
unknown's avatar
unknown committed
841 842 843 844

			block->frame = bck->frame;

			*(buf_pool->blocks_of_frames
unknown's avatar
unknown committed
845 846 847
			  + (((ulint)(block->frame
				      - buf_pool->frame_zero))
			     >> UNIV_PAGE_SIZE_SHIFT))
unknown's avatar
unknown committed
848
				= block;
849

unknown's avatar
unknown committed
850 851
			bck->frame = NULL;
			UT_LIST_REMOVE(awe_LRU_free_mapped,
unknown's avatar
unknown committed
852 853
				       buf_pool->awe_LRU_free_mapped,
				       bck);
unknown's avatar
unknown committed
854 855

			if (add_to_mapped_list) {
856 857 858 859
				UT_LIST_ADD_FIRST(
					awe_LRU_free_mapped,
					buf_pool->awe_LRU_free_mapped,
					block);
unknown's avatar
unknown committed
860 861 862
			}

			buf_pool->n_pages_awe_remapped++;
863

unknown's avatar
unknown committed
864 865
			mutex_exit(&bck->mutex);

unknown's avatar
unknown committed
866 867 868 869 870
			return;
		}
	}

	fprintf(stderr,
unknown's avatar
unknown committed
871 872
		"InnoDB: AWE: Fatal error: cannot find a page to unmap\n"
		"InnoDB: awe_LRU_free_mapped list length %lu\n",
873
		(ulong) UT_LIST_GET_LEN(buf_pool->awe_LRU_free_mapped));
874

unknown's avatar
unknown committed
875
	ut_a(0);
876 877 878 879 880 881 882 883
}

/************************************************************************
Allocates a buffer block. */
UNIV_INLINE
buf_block_t*
buf_block_alloc(void)
/*=================*/
unknown's avatar
unknown committed
884 885 886
				/* out, own: the allocated block; also if AWE
				is used it is guaranteed that the page is
				mapped to a frame */
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
{
	buf_block_t*	block;

	block = buf_LRU_get_free_block();

	return(block);
}

/************************************************************************
Moves to the block to the start of the LRU list if there is a danger
that the block would drift out of the buffer pool. */
UNIV_INLINE
void
buf_block_make_young(
/*=================*/
	buf_block_t*	block)	/* in: block to make younger */
{
unknown's avatar
unknown committed
904 905 906 907 908
	ut_ad(!mutex_own(&(buf_pool->mutex)));

	/* Note that we read freed_page_clock's without holding any mutex:
	this is allowed since the result is used only in heuristics */

unknown's avatar
unknown committed
909
	if (buf_block_peek_if_too_old(block)) {
910

unknown's avatar
unknown committed
911
		mutex_enter(&buf_pool->mutex);
912 913 914 915
		/* There has been freeing activity in the LRU list:
		best to move to the head of the LRU list */

		buf_LRU_make_block_young(block);
unknown's avatar
unknown committed
916
		mutex_exit(&buf_pool->mutex);
917 918 919 920 921 922 923 924 925 926
	}
}

/************************************************************************
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(
927
/*================*/
928 929 930
	buf_frame_t*	frame)	/* in: buffer frame of a file page */
{
	buf_block_t*	block;
931

932 933 934 935
	mutex_enter(&(buf_pool->mutex));

	block = buf_block_align(frame);

unknown's avatar
unknown committed
936
	ut_a(block->state == BUF_BLOCK_FILE_PAGE);
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952

	buf_LRU_make_block_young(block);

	mutex_exit(&(buf_pool->mutex));
}

/************************************************************************
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 */
{
	mutex_enter(&(buf_pool->mutex));

unknown's avatar
unknown committed
953 954 955 956
	mutex_enter(&block->mutex);

	ut_a(block->state != BUF_BLOCK_FILE_PAGE);

957 958
	buf_LRU_block_free_non_file_page(block);

unknown's avatar
unknown committed
959 960
	mutex_exit(&block->mutex);

961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
	mutex_exit(&(buf_pool->mutex));
}

/*************************************************************************
Allocates a buffer frame. */

buf_frame_t*
buf_frame_alloc(void)
/*=================*/
				/* out: buffer frame */
{
	return(buf_block_alloc()->frame);
}

/*************************************************************************
Frees a buffer frame which does not contain a file page. */

void
buf_frame_free(
/*===========*/
	buf_frame_t*	frame)	/* in: buffer frame */
{
	buf_block_free(buf_block_align(frame));
}
985

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
/************************************************************************
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 */
{
	buf_block_t*	block;

	mutex_enter_fast(&(buf_pool->mutex));

	block = buf_page_hash_get(space, offset);

	mutex_exit(&(buf_pool->mutex));

	return(block);
}

unknown's avatar
unknown committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
/************************************************************************
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 */
{
	buf_block_t*	block;

	mutex_enter_fast(&(buf_pool->mutex));

	block = buf_page_hash_get(space, offset);

	if (block) {
		block->check_index_page_at_flush = FALSE;
	}
1030

unknown's avatar
unknown committed
1031 1032 1033
	mutex_exit(&(buf_pool->mutex));
}

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
/************************************************************************
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 */
{
	buf_block_t*	block;
	ibool		is_hashed;

	mutex_enter_fast(&(buf_pool->mutex));

	block = buf_page_hash_get(space, offset);

	if (!block) {
		is_hashed = FALSE;
	} else {
		is_hashed = block->is_hashed;
	}

	mutex_exit(&(buf_pool->mutex));

	return(is_hashed);
}

/************************************************************************
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 */
{
	if (buf_page_peek_block(space, offset)) {

		return(TRUE);
	}

	return(FALSE);
}

unknown's avatar
unknown committed
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
/************************************************************************
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. */

buf_block_t*
buf_page_set_file_page_was_freed(
/*=============================*/
			/* out: control block if found from page hash table,
			otherwise NULL */
	ulint	space,	/* in: space id */
	ulint	offset)	/* in: page number */
{
	buf_block_t*	block;

	mutex_enter_fast(&(buf_pool->mutex));

	block = buf_page_hash_get(space, offset);

	if (block) {
		block->file_page_was_freed = TRUE;
	}

	mutex_exit(&(buf_pool->mutex));

	return(block);
}

/************************************************************************
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. */

buf_block_t*
buf_page_reset_file_page_was_freed(
/*===============================*/
			/* out: control block if found from page hash table,
			otherwise NULL */
	ulint	space,	/* in: space id */
	ulint	offset)	/* in: page number */
{
	buf_block_t*	block;

	mutex_enter_fast(&(buf_pool->mutex));

	block = buf_page_hash_get(space, offset);

	if (block) {
		block->file_page_was_freed = FALSE;
	}

	mutex_exit(&(buf_pool->mutex));

	return(block);
}

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
/************************************************************************
This is the general function used to get access to a database page. */

buf_frame_t*
buf_page_get_gen(
/*=============*/
				/* out: pointer to the frame or NULL */
	ulint		space,	/* in: space id */
	ulint		offset,	/* in: page number */
	ulint		rw_latch,/* in: RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
	buf_frame_t*	guess,	/* in: guessed frame or NULL */
	ulint		mode,	/* in: BUF_GET, BUF_GET_IF_IN_POOL,
				BUF_GET_NO_LATCH, BUF_GET_NOWAIT */
1157
	const char*	file,	/* in: file name */
1158 1159 1160 1161 1162 1163 1164 1165
	ulint		line,	/* in: line where called */
	mtr_t*		mtr)	/* in: mini-transaction */
{
	buf_block_t*	block;
	ibool		accessed;
	ulint		fix_type;
	ibool		success;
	ibool		must_read;
1166
	ulint		retries = 0;
1167

1168 1169
	ut_ad(mtr);
	ut_ad((rw_latch == RW_S_LATCH)
unknown's avatar
unknown committed
1170 1171
	      || (rw_latch == RW_X_LATCH)
	      || (rw_latch == RW_NO_LATCH));
1172 1173
	ut_ad((mode != BUF_GET_NO_LATCH) || (rw_latch == RW_NO_LATCH));
	ut_ad((mode == BUF_GET) || (mode == BUF_GET_IF_IN_POOL)
unknown's avatar
unknown committed
1174
	      || (mode == BUF_GET_NO_LATCH) || (mode == BUF_GET_NOWAIT));
1175 1176 1177
#ifndef UNIV_LOG_DEBUG
	ut_ad(!ibuf_inside() || ibuf_page(space, offset));
#endif
1178
	buf_pool->n_page_gets++;
1179 1180
loop:
	block = NULL;
unknown's avatar
unknown committed
1181
	mutex_enter_fast(&(buf_pool->mutex));
1182

1183 1184 1185 1186
	if (guess) {
		block = buf_block_align(guess);

		if ((offset != block->offset) || (space != block->space)
unknown's avatar
unknown committed
1187
		    || (block->state != BUF_BLOCK_FILE_PAGE)) {
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206

			block = NULL;
		}
	}

	if (block == NULL) {
		block = buf_page_hash_get(space, offset);
	}

	if (block == NULL) {
		/* Page not in buf_pool: needs to be read from file */

		mutex_exit(&(buf_pool->mutex));

		if (mode == BUF_GET_IF_IN_POOL) {

			return(NULL);
		}

1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
		if (buf_read_page(space, offset)) {
			retries = 0;
		} else if (retries < BUF_PAGE_READ_MAX_RETRIES) {
			++retries;
		} else {
			fprintf(stderr, "InnoDB: Error: Unable"
				" to read tablespace %lu page no"
				" %lu into the buffer pool after"
				" %lu attempts\n"
				"InnoDB: The most probable cause"
				" of this error may be that the"
				" table has been corrupted.\n"
				"InnoDB: You can try to fix this"
				" problem by using"
				" innodb_force_recovery.\n"
				"InnoDB: Please see reference manual"
				" for more details.\n"
				"InnoDB: Aborting...\n",
				space, offset,
				BUF_PAGE_READ_MAX_RETRIES);

			ut_error;
		}
1230

unknown's avatar
unknown committed
1231
#ifdef UNIV_DEBUG
1232 1233 1234 1235 1236
		buf_dbg_counter++;

		if (buf_dbg_counter % 37 == 0) {
			ut_ad(buf_validate());
		}
unknown's avatar
unknown committed
1237
#endif
1238 1239 1240
		goto loop;
	}

unknown's avatar
unknown committed
1241 1242
	mutex_enter(&block->mutex);

unknown's avatar
unknown committed
1243 1244
	ut_a(block->state == BUF_BLOCK_FILE_PAGE);

1245
	must_read = FALSE;
1246

1247 1248 1249 1250 1251 1252
	if (block->io_fix == BUF_IO_READ) {

		must_read = TRUE;

		if (mode == BUF_GET_IF_IN_POOL) {
			/* The page is only being read to buffer */
unknown's avatar
unknown committed
1253 1254
			mutex_exit(&buf_pool->mutex);
			mutex_exit(&block->mutex);
1255 1256 1257

			return(NULL);
		}
1258
	}
1259

unknown's avatar
unknown committed
1260 1261 1262 1263 1264 1265 1266 1267 1268
	/* If AWE is enabled and the page is not mapped to a frame, then
	map it */

	if (block->frame == NULL) {
		ut_a(srv_use_awe);

		/* We set second parameter TRUE because the block is in the
		LRU list and we must put it to awe_LRU_free_mapped list once
		mapped to a frame */
1269

unknown's avatar
unknown committed
1270 1271
		buf_awe_map_page_to_frame(block, TRUE);
	}
1272

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
#if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG
	if (mode == BUF_GET_IF_IN_POOL && ibuf_debug) {
		/* Try to evict the block from the buffer pool, to use the
		insert buffer as much as possible. */

		if (buf_LRU_free_block(block)) {
			mutex_exit(&buf_pool->mutex);
			mutex_exit(&block->mutex);
			fprintf(stderr,
				"innodb_change_buffering_debug evict %u %u\n",
				(unsigned) space, (unsigned) offset);
			return(NULL);
		} else if (buf_flush_page_try(block)) {
			fprintf(stderr,
				"innodb_change_buffering_debug flush %u %u\n",
				(unsigned) space, (unsigned) offset);
			guess = block->frame;
			goto loop;
		}

		/* Failed to evict the page; change it directly */
	}
#endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */

1297 1298 1299 1300 1301
#ifdef UNIV_SYNC_DEBUG
	buf_block_buf_fix_inc_debug(block, file, line);
#else
	buf_block_buf_fix_inc(block);
#endif
unknown's avatar
unknown committed
1302
	mutex_exit(&buf_pool->mutex);
1303 1304 1305 1306 1307 1308 1309

	/* Check if this is the first access to the page */

	accessed = block->accessed;

	block->accessed = TRUE;

unknown's avatar
unknown committed
1310 1311 1312 1313
	mutex_exit(&block->mutex);

	buf_block_make_young(block);

unknown's avatar
unknown committed
1314 1315
#ifdef UNIV_DEBUG_FILE_ACCESSES
	ut_a(block->file_page_was_freed == FALSE);
1316
#endif
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329

#ifdef UNIV_DEBUG
	buf_dbg_counter++;

	if (buf_dbg_counter % 5771 == 0) {
		ut_ad(buf_validate());
	}
#endif
	ut_ad(block->buf_fix_count > 0);
	ut_ad(block->state == BUF_BLOCK_FILE_PAGE);

	if (mode == BUF_GET_NOWAIT) {
		if (rw_latch == RW_S_LATCH) {
1330
			success = rw_lock_s_lock_func_nowait(&(block->lock),
unknown's avatar
unknown committed
1331
							     file, line);
1332 1333 1334
			fix_type = MTR_MEMO_PAGE_S_FIX;
		} else {
			ut_ad(rw_latch == RW_X_LATCH);
1335
			success = rw_lock_x_lock_func_nowait(&(block->lock),
unknown's avatar
unknown committed
1336
							     file, line);
1337 1338 1339 1340
			fix_type = MTR_MEMO_PAGE_X_FIX;
		}

		if (!success) {
unknown's avatar
unknown committed
1341
			mutex_enter(&block->mutex);
1342 1343

			block->buf_fix_count--;
unknown's avatar
unknown committed
1344 1345

			mutex_exit(&block->mutex);
1346 1347
#ifdef UNIV_SYNC_DEBUG
			rw_lock_s_unlock(&(block->debug_latch));
1348
#endif
1349 1350 1351 1352 1353 1354

			return(NULL);
		}
	} else if (rw_latch == RW_NO_LATCH) {

		if (must_read) {
1355
			/* Let us wait until the read operation
1356 1357
			completes */

1358
			for (;;) {
unknown's avatar
unknown committed
1359
				mutex_enter(&block->mutex);
1360 1361

				if (block->io_fix == BUF_IO_READ) {
1362

unknown's avatar
unknown committed
1363
					mutex_exit(&block->mutex);
1364

unknown's avatar
unknown committed
1365
					os_thread_sleep(WAIT_FOR_READ);
1366 1367
				} else {

unknown's avatar
unknown committed
1368
					mutex_exit(&block->mutex);
1369 1370

					break;
1371 1372
				}
			}
1373 1374 1375 1376 1377
		}

		fix_type = MTR_MEMO_BUF_FIX;
	} else if (rw_latch == RW_S_LATCH) {

1378 1379
		rw_lock_s_lock_func(&(block->lock), 0, file, line);

1380 1381
		fix_type = MTR_MEMO_PAGE_S_FIX;
	} else {
1382 1383
		rw_lock_x_lock_func(&(block->lock), 0, file, line);

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
		fix_type = MTR_MEMO_PAGE_X_FIX;
	}

	mtr_memo_push(mtr, block, fix_type);

	if (!accessed) {
		/* In the case of a first access, try to apply linear
		read-ahead */

		buf_read_ahead_linear(space, offset);
	}

#ifdef UNIV_IBUF_DEBUG
	ut_a(ibuf_count_get(block->space, block->offset) == 0);
#endif
1399
	return(block->frame);
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
}

/************************************************************************
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 */
unknown's avatar
unknown committed
1411 1412 1413
	buf_block_t*	block,	/* in: guessed buffer block */
	buf_frame_t*	guess,	/* in: guessed frame; note that AWE may move
				frames */
1414 1415
	dulint		modify_clock,/* in: modify clock value if mode is
				..._GUESS_ON_CLOCK */
1416
	const char*	file,	/* in: file name */
1417 1418 1419 1420 1421 1422 1423
	ulint		line,	/* in: line where called */
	mtr_t*		mtr)	/* in: mini-transaction */
{
	ibool		accessed;
	ibool		success;
	ulint		fix_type;

unknown's avatar
unknown committed
1424
	ut_ad(mtr && block);
1425
	ut_ad((rw_latch == RW_S_LATCH) || (rw_latch == RW_X_LATCH));
1426

unknown's avatar
unknown committed
1427
	/* If AWE is used, block may have a different frame now, e.g., NULL */
1428

unknown's avatar
unknown committed
1429 1430
	mutex_enter(&block->mutex);

unknown's avatar
unknown committed
1431
	if (UNIV_UNLIKELY(block->state != BUF_BLOCK_FILE_PAGE)
unknown's avatar
unknown committed
1432
	    || UNIV_UNLIKELY(block->frame != guess)) {
unknown's avatar
unknown committed
1433 1434

		mutex_exit(&block->mutex);
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446

		return(FALSE);
	}

#ifdef UNIV_SYNC_DEBUG
	buf_block_buf_fix_inc_debug(block, file, line);
#else
	buf_block_buf_fix_inc(block);
#endif
	accessed = block->accessed;
	block->accessed = TRUE;

unknown's avatar
unknown committed
1447 1448 1449 1450 1451
	mutex_exit(&block->mutex);

	buf_block_make_young(block);

	/* Check if this is the first access to the page */
1452 1453 1454 1455

	ut_ad(!ibuf_inside() || ibuf_page(block->space, block->offset));

	if (rw_latch == RW_S_LATCH) {
1456
		success = rw_lock_s_lock_func_nowait(&(block->lock),
unknown's avatar
unknown committed
1457
						     file, line);
1458 1459
		fix_type = MTR_MEMO_PAGE_S_FIX;
	} else {
1460
		success = rw_lock_x_lock_func_nowait(&(block->lock),
unknown's avatar
unknown committed
1461
						     file, line);
1462 1463 1464
		fix_type = MTR_MEMO_PAGE_X_FIX;
	}

unknown's avatar
unknown committed
1465
	if (UNIV_UNLIKELY(!success)) {
unknown's avatar
unknown committed
1466
		mutex_enter(&block->mutex);
1467

1468
		block->buf_fix_count--;
unknown's avatar
unknown committed
1469 1470 1471

		mutex_exit(&block->mutex);

1472 1473
#ifdef UNIV_SYNC_DEBUG
		rw_lock_s_unlock(&(block->debug_latch));
unknown's avatar
unknown committed
1474
#endif
unknown's avatar
unknown committed
1475
		return(FALSE);
1476 1477
	}

unknown's avatar
unknown committed
1478
	if (UNIV_UNLIKELY(!UT_DULINT_EQ(modify_clock, block->modify_clock))) {
1479
#ifdef UNIV_SYNC_DEBUG
1480
		buf_page_dbg_add_level(block->frame, SYNC_NO_ORDER_CHECK);
1481
#endif /* UNIV_SYNC_DEBUG */
1482 1483 1484 1485 1486 1487
		if (rw_latch == RW_S_LATCH) {
			rw_lock_s_unlock(&(block->lock));
		} else {
			rw_lock_x_unlock(&(block->lock));
		}

unknown's avatar
unknown committed
1488
		mutex_enter(&block->mutex);
1489

1490
		block->buf_fix_count--;
unknown's avatar
unknown committed
1491 1492 1493

		mutex_exit(&block->mutex);

1494 1495
#ifdef UNIV_SYNC_DEBUG
		rw_lock_s_unlock(&(block->debug_latch));
unknown's avatar
unknown committed
1496
#endif
unknown's avatar
unknown committed
1497
		return(FALSE);
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
	}

	mtr_memo_push(mtr, block, fix_type);

#ifdef UNIV_DEBUG
	buf_dbg_counter++;

	if (buf_dbg_counter % 5771 == 0) {
		ut_ad(buf_validate());
	}
#endif
	ut_ad(block->buf_fix_count > 0);
	ut_ad(block->state == BUF_BLOCK_FILE_PAGE);

unknown's avatar
unknown committed
1512 1513 1514
#ifdef UNIV_DEBUG_FILE_ACCESSES
	ut_a(block->file_page_was_freed == FALSE);
#endif
unknown's avatar
unknown committed
1515
	if (UNIV_UNLIKELY(!accessed)) {
1516 1517 1518 1519
		/* In the case of a first access, try to apply linear
		read-ahead */

		buf_read_ahead_linear(buf_frame_get_space_id(guess),
unknown's avatar
unknown committed
1520
				      buf_frame_get_page_no(guess));
1521 1522 1523 1524 1525
	}

#ifdef UNIV_IBUF_DEBUG
	ut_a(ibuf_count_get(block->space, block->offset) == 0);
#endif
unknown's avatar
unknown committed
1526 1527
	buf_pool->n_page_gets++;

1528 1529 1530 1531 1532
	return(TRUE);
}

/************************************************************************
This is used to get access to a known database page, when no waiting can be
unknown's avatar
unknown committed
1533 1534
done. For example, if a search in an adaptive hash index leads us to this
frame. */
1535 1536 1537 1538 1539 1540 1541 1542

ibool
buf_page_get_known_nowait(
/*======================*/
				/* out: TRUE if success */
	ulint		rw_latch,/* in: RW_S_LATCH, RW_X_LATCH */
	buf_frame_t*	guess,	/* in: the known page frame */
	ulint		mode,	/* in: BUF_MAKE_YOUNG or BUF_KEEP_OLD */
1543
	const char*	file,	/* in: file name */
1544 1545 1546 1547 1548 1549 1550 1551 1552
	ulint		line,	/* in: line where called */
	mtr_t*		mtr)	/* in: mini-transaction */
{
	buf_block_t*	block;
	ibool		success;
	ulint		fix_type;

	ut_ad(mtr);
	ut_ad((rw_latch == RW_S_LATCH) || (rw_latch == RW_X_LATCH));
1553

unknown's avatar
unknown committed
1554 1555
	block = buf_block_align(guess);

unknown's avatar
unknown committed
1556 1557
	mutex_enter(&block->mutex);

1558
	if (block->state == BUF_BLOCK_REMOVE_HASH) {
1559 1560
		/* Another thread is just freeing the block from the LRU list
		of the buffer pool: do not try to access this page; this
1561 1562 1563 1564 1565
		attempt to access the page can only come through the hash
		index because when the buffer block state is ..._REMOVE_HASH,
		we have already removed it from the page address hash table
		of the buffer pool. */

unknown's avatar
unknown committed
1566
		mutex_exit(&block->mutex);
1567 1568 1569 1570

		return(FALSE);
	}

unknown's avatar
unknown committed
1571 1572
	ut_a(block->state == BUF_BLOCK_FILE_PAGE);

1573 1574 1575 1576 1577
#ifdef UNIV_SYNC_DEBUG
	buf_block_buf_fix_inc_debug(block, file, line);
#else
	buf_block_buf_fix_inc(block);
#endif
unknown's avatar
unknown committed
1578 1579
	mutex_exit(&block->mutex);

1580 1581 1582 1583 1584 1585 1586
	if (mode == BUF_MAKE_YOUNG) {
		buf_block_make_young(block);
	}

	ut_ad(!ibuf_inside() || (mode == BUF_KEEP_OLD));

	if (rw_latch == RW_S_LATCH) {
1587
		success = rw_lock_s_lock_func_nowait(&(block->lock),
unknown's avatar
unknown committed
1588
						     file, line);
1589 1590
		fix_type = MTR_MEMO_PAGE_S_FIX;
	} else {
1591
		success = rw_lock_x_lock_func_nowait(&(block->lock),
unknown's avatar
unknown committed
1592
						     file, line);
1593 1594
		fix_type = MTR_MEMO_PAGE_X_FIX;
	}
1595

1596
	if (!success) {
unknown's avatar
unknown committed
1597
		mutex_enter(&block->mutex);
1598

1599
		block->buf_fix_count--;
unknown's avatar
unknown committed
1600 1601 1602

		mutex_exit(&block->mutex);

1603 1604
#ifdef UNIV_SYNC_DEBUG
		rw_lock_s_unlock(&(block->debug_latch));
1605
#endif
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620

		return(FALSE);
	}

	mtr_memo_push(mtr, block, fix_type);

#ifdef UNIV_DEBUG
	buf_dbg_counter++;

	if (buf_dbg_counter % 5771 == 0) {
		ut_ad(buf_validate());
	}
#endif
	ut_ad(block->buf_fix_count > 0);
	ut_ad(block->state == BUF_BLOCK_FILE_PAGE);
unknown's avatar
unknown committed
1621 1622 1623
#ifdef UNIV_DEBUG_FILE_ACCESSES
	ut_a(block->file_page_was_freed == FALSE);
#endif
1624 1625 1626

#ifdef UNIV_IBUF_DEBUG
	ut_a((mode == BUF_KEEP_OLD)
unknown's avatar
unknown committed
1627
	     || (ibuf_count_get(block->space, block->offset) == 0));
1628
#endif
unknown's avatar
unknown committed
1629 1630
	buf_pool->n_page_gets++;

1631 1632 1633
	return(TRUE);
}

unknown's avatar
unknown committed
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
/************************************************************************
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 */
	buf_block_t*	block)	/* in: block to init */
{
	/* Set the state of the block */
	block->magic_n		= BUF_BLOCK_MAGIC_N;

1648 1649 1650
	block->state		= BUF_BLOCK_FILE_PAGE;
	block->space		= space;
	block->offset		= offset;
unknown's avatar
unknown committed
1651 1652

	block->lock_hash_val	= 0;
1653

unknown's avatar
unknown committed
1654 1655 1656 1657
	block->freed_page_clock = 0;

	block->newest_modification = ut_dulint_zero;
	block->oldest_modification = ut_dulint_zero;
1658

unknown's avatar
unknown committed
1659
	block->accessed		= FALSE;
1660
	block->buf_fix_count	= 0;
unknown's avatar
unknown committed
1661 1662 1663 1664
	block->io_fix		= 0;

	block->n_hash_helps	= 0;
	block->is_hashed	= FALSE;
1665 1666
	block->n_fields		= 1;
	block->n_bytes		= 0;
1667
	block->left_side	= TRUE;
unknown's avatar
unknown committed
1668 1669 1670 1671

	block->file_page_was_freed = FALSE;
}

1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
/************************************************************************
Inits a page to the buffer buf_pool. */
static
void
buf_page_init(
/*==========*/
	ulint		space,	/* in: space id */
	ulint		offset,	/* in: offset of the page within space
				in units of a page */
	buf_block_t*	block)	/* in: block to init */
{
1683

1684
	ut_ad(mutex_own(&(buf_pool->mutex)));
unknown's avatar
unknown committed
1685
	ut_ad(mutex_own(&(block->mutex)));
unknown's avatar
unknown committed
1686
	ut_a(block->state != BUF_BLOCK_FILE_PAGE);
1687 1688

	/* Set the state of the block */
unknown's avatar
unknown committed
1689 1690
	block->magic_n		= BUF_BLOCK_MAGIC_N;

1691 1692 1693
	block->state		= BUF_BLOCK_FILE_PAGE;
	block->space		= space;
	block->offset		= offset;
1694

unknown's avatar
unknown committed
1695
	block->check_index_page_at_flush = FALSE;
unknown's avatar
unknown committed
1696
	block->index		= NULL;
1697

1698
	block->lock_hash_val	= lock_rec_hash(space, offset);
1699

unknown's avatar
unknown committed
1700 1701 1702 1703 1704 1705 1706 1707 1708
#ifdef UNIV_DEBUG_VALGRIND
	if (!space) {
		/* Silence valid Valgrind warnings about uninitialized
		data being written to data files.  There are some unused
		bytes on some pages that InnoDB does not initialize. */
		UNIV_MEM_VALID(block->frame, UNIV_PAGE_SIZE);
	}
#endif /* UNIV_DEBUG_VALGRIND */

1709 1710
	/* Insert into the hash table of file pages */

1711 1712
	if (buf_page_hash_get(space, offset)) {
		fprintf(stderr,
unknown's avatar
unknown committed
1713 1714
			"InnoDB: Error: page %lu %lu already found"
			" in the hash table\n",
1715 1716
			(ulong) space,
			(ulong) offset);
unknown's avatar
unknown committed
1717
#ifdef UNIV_DEBUG
1718 1719 1720 1721
		buf_print();
		buf_LRU_print();
		buf_validate();
		buf_LRU_validate();
unknown's avatar
unknown committed
1722
#endif /* UNIV_DEBUG */
1723 1724
		ut_a(0);
	}
unknown's avatar
unknown committed
1725

1726
	HASH_INSERT(buf_block_t, hash, buf_pool->page_hash,
unknown's avatar
unknown committed
1727
		    buf_page_address_fold(space, offset), block);
1728 1729 1730 1731 1732

	block->freed_page_clock = 0;

	block->newest_modification = ut_dulint_zero;
	block->oldest_modification = ut_dulint_zero;
1733

1734
	block->accessed		= FALSE;
1735
	block->buf_fix_count	= 0;
1736 1737 1738 1739
	block->io_fix		= 0;

	block->n_hash_helps	= 0;
	block->is_hashed	= FALSE;
1740 1741
	block->n_fields		= 1;
	block->n_bytes		= 0;
1742
	block->left_side	= TRUE;
unknown's avatar
unknown committed
1743 1744

	block->file_page_was_freed = FALSE;
1745 1746 1747 1748
}

/************************************************************************
Function which inits a page for read to the buffer buf_pool. If the page is
unknown's avatar
unknown committed
1749 1750 1751 1752 1753 1754 1755 1756
(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
1757
buf_page_create). */
1758 1759 1760 1761

buf_block_t*
buf_page_init_for_read(
/*===================*/
unknown's avatar
unknown committed
1762 1763 1764 1765 1766 1767 1768 1769
				/* 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 */
	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 */
1770 1771 1772
{
	buf_block_t*	block;
	mtr_t		mtr;
unknown's avatar
unknown committed
1773

1774 1775
	ut_ad(buf_pool);

unknown's avatar
unknown committed
1776 1777
	*err = DB_SUCCESS;

1778 1779 1780 1781 1782
	if (mode == BUF_READ_IBUF_PAGES_ONLY) {
		/* It is a read-ahead within an ibuf routine */

		ut_ad(!ibuf_bitmap_page(offset));
		ut_ad(ibuf_inside());
1783

1784
		mtr_start(&mtr);
1785

1786 1787 1788 1789 1790 1791 1792 1793 1794
		if (!ibuf_page_low(space, offset, &mtr)) {

			mtr_commit(&mtr);

			return(NULL);
		}
	} else {
		ut_ad(mode == BUF_READ_ANY_PAGE);
	}
1795

1796 1797
	block = buf_block_alloc();

unknown's avatar
unknown committed
1798
	ut_a(block);
1799 1800

	mutex_enter(&(buf_pool->mutex));
unknown's avatar
unknown committed
1801
	mutex_enter(&block->mutex);
1802

1803 1804
	if (fil_tablespace_deleted_or_being_deleted_in_mem(
		    space, tablespace_version)) {
unknown's avatar
unknown committed
1805 1806 1807 1808
		*err = DB_TABLESPACE_DELETED;
	}

	if (*err == DB_TABLESPACE_DELETED
unknown's avatar
unknown committed
1809
	    || NULL != buf_page_hash_get(space, offset)) {
unknown's avatar
unknown committed
1810

1811 1812 1813
		/* The page belongs to a space which has been
		deleted or is being deleted, or the page is
		already in buf_pool, return */
1814

unknown's avatar
unknown committed
1815
		mutex_exit(&block->mutex);
1816
		mutex_exit(&(buf_pool->mutex));
unknown's avatar
unknown committed
1817

1818
		buf_block_free(block);
1819

1820
		if (mode == BUF_READ_IBUF_PAGES_ONLY) {
1821

1822 1823
			mtr_commit(&mtr);
		}
1824

1825 1826
		return(NULL);
	}
1827 1828

	ut_ad(block);
1829

1830 1831 1832 1833
	buf_page_init(space, offset, block);

	/* The block must be put to the LRU list, to the old blocks */

1834 1835
	buf_LRU_add_block(block, TRUE);		/* TRUE == to old blocks */

1836
	block->io_fix = BUF_IO_READ;
unknown's avatar
unknown committed
1837

1838
	buf_pool->n_pend_reads++;
1839

1840 1841 1842 1843 1844 1845
	/* We set a pass-type x-lock on the frame because then the same
	thread which called for the read operation (and is running now at
	this point of code) can wait for the read to complete by waiting
	for the x-lock on the frame; if the x-lock were recursive, the
	same thread would illegally get the x-lock before the page read
	is completed. The x-lock is cleared by the io-handler thread. */
1846

1847
	rw_lock_x_lock_gen(&(block->lock), BUF_IO_READ);
1848

unknown's avatar
unknown committed
1849
	mutex_exit(&block->mutex);
1850
	mutex_exit(&(buf_pool->mutex));
1851 1852 1853 1854 1855 1856 1857

	if (mode == BUF_READ_IBUF_PAGES_ONLY) {

		mtr_commit(&mtr);
	}

	return(block);
1858
}
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877

/************************************************************************
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). */

buf_frame_t*
buf_page_create(
/*============*/
			/* out: pointer to the frame, page bufferfixed */
	ulint	space,	/* in: space id */
	ulint	offset,	/* in: offset of the page within space in units of
			a page */
	mtr_t*	mtr)	/* in: mini-transaction handle */
{
	buf_frame_t*	frame;
	buf_block_t*	block;
	buf_block_t*	free_block	= NULL;
1878

1879 1880 1881
	ut_ad(mtr);

	free_block = buf_LRU_get_free_block();
1882

1883 1884 1885 1886 1887 1888 1889 1890
	mutex_enter(&(buf_pool->mutex));

	block = buf_page_hash_get(space, offset);

	if (block != NULL) {
#ifdef UNIV_IBUF_DEBUG
		ut_a(ibuf_count_get(block->space, block->offset) == 0);
#endif
unknown's avatar
unknown committed
1891 1892
		block->file_page_was_freed = FALSE;

1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
		/* Page can be found in buf_pool */
		mutex_exit(&(buf_pool->mutex));

		buf_block_free(free_block);

		frame = buf_page_get_with_no_latch(space, offset, mtr);

		return(frame);
	}

	/* If we get here, the page was not in buf_pool: init it there */

1905
#ifdef UNIV_DEBUG
1906
	if (buf_debug_prints) {
1907
		fprintf(stderr, "Creating space %lu page %lu to buffer\n",
unknown's avatar
unknown committed
1908
			(ulong) space, (ulong) offset);
1909
	}
1910
#endif /* UNIV_DEBUG */
1911 1912

	block = free_block;
1913

unknown's avatar
unknown committed
1914 1915
	mutex_enter(&block->mutex);

1916 1917 1918 1919
	buf_page_init(space, offset, block);

	/* The block must be put to the LRU list */
	buf_LRU_add_block(block, FALSE);
1920

1921
#ifdef UNIV_SYNC_DEBUG
1922
	buf_block_buf_fix_inc_debug(block, __FILE__, __LINE__);
1923 1924 1925
#else
	buf_block_buf_fix_inc(block);
#endif
unknown's avatar
unknown committed
1926 1927 1928 1929
	buf_pool->n_pages_created++;

	mutex_exit(&(buf_pool->mutex));

1930 1931 1932
	mtr_memo_push(mtr, block, MTR_MEMO_BUF_FIX);

	block->accessed = TRUE;
1933

unknown's avatar
unknown committed
1934
	mutex_exit(&block->mutex);
1935

unknown's avatar
unknown committed
1936 1937 1938
	/* Delete possible entries for the page from the insert buffer:
	such can exist if the page belonged to an index which was dropped */

unknown's avatar
unknown committed
1939
	ibuf_merge_or_delete_for_page(NULL, space, offset, TRUE);
unknown's avatar
unknown committed
1940

1941 1942 1943 1944
	/* Flush pages from the end of the LRU list if necessary */
	buf_flush_free_margin();

	frame = block->frame;
unknown's avatar
unknown committed
1945

1946 1947 1948 1949
	memset(frame + FIL_PAGE_PREV, 0xff, 4);
	memset(frame + FIL_PAGE_NEXT, 0xff, 4);
	mach_write_to_2(frame + FIL_PAGE_TYPE, FIL_PAGE_TYPE_ALLOCATED);

unknown's avatar
unknown committed
1950 1951 1952 1953 1954 1955 1956 1957
	/* Reset to zero the file flush lsn field in the page; if the first
	page of an ibdata file is 'created' in this function into the buffer
	pool then we lose the original contents of the file flush lsn stamp.
	Then InnoDB could in a crash recovery print a big, false, corruption
	warning if the stamp contains an lsn bigger than the ib_logfile lsn. */

	memset(frame + FIL_PAGE_FILE_FLUSH_LSN, 0, 8);

1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
#ifdef UNIV_DEBUG
	buf_dbg_counter++;

	if (buf_dbg_counter % 357 == 0) {
		ut_ad(buf_validate());
	}
#endif
#ifdef UNIV_IBUF_DEBUG
	ut_a(ibuf_count_get(block->space, block->offset) == 0);
#endif
	return(frame);
}

/************************************************************************
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 */
{
	ulint		io_type;
1981

1982 1983
	ut_ad(block);

unknown's avatar
unknown committed
1984 1985
	ut_a(block->state == BUF_BLOCK_FILE_PAGE);

unknown's avatar
unknown committed
1986 1987 1988 1989 1990 1991
	/* We do not need protect block->io_fix here by block->mutex to read
	it because this is the only function where we can change the value
	from BUF_IO_READ or BUF_IO_WRITE to some other value, and our code
	ensures that this is the only thread that handles the i/o for this
	block. */

1992 1993 1994
	io_type = block->io_fix;

	if (io_type == BUF_IO_READ) {
unknown's avatar
unknown committed
1995
		/* If this page is not uninitialized and not in the
unknown's avatar
unknown committed
1996 1997
		doublewrite buffer, then the page number and space id
		should be the same as in block. */
1998 1999 2000 2001
		ulint	read_page_no = mach_read_from_4(
			block->frame + FIL_PAGE_OFFSET);
		ulint	read_space_id = mach_read_from_4(
			block->frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
unknown's avatar
unknown committed
2002

unknown's avatar
unknown committed
2003 2004
		if (!block->space
		    && trx_doublewrite_page_inside(block->offset)) {
unknown's avatar
unknown committed
2005

unknown's avatar
unknown committed
2006
			ut_print_timestamp(stderr);
unknown's avatar
unknown committed
2007
			fprintf(stderr,
unknown's avatar
unknown committed
2008 2009 2010
				"  InnoDB: Error: reading page %lu\n"
				"InnoDB: which is in the"
				" doublewrite buffer!\n",
unknown's avatar
unknown committed
2011 2012 2013 2014
				(ulong) block->offset);
		} else if (!read_space_id && !read_page_no) {
			/* This is likely an uninitialized page. */
		} else if ((block->space && block->space != read_space_id)
unknown's avatar
unknown committed
2015
			   || block->offset != read_page_no) {
unknown's avatar
unknown committed
2016 2017 2018 2019 2020 2021 2022
			/* We did not compare space_id to read_space_id
			if block->space == 0, because the field on the
			page may contain garbage in MySQL < 4.1.1,
			which only supported block->space == 0. */

			ut_print_timestamp(stderr);
			fprintf(stderr,
unknown's avatar
unknown committed
2023 2024 2025 2026
				"  InnoDB: Error: space id and page n:o"
				" stored in the page\n"
				"InnoDB: read in are %lu:%lu,"
				" should be %lu:%lu!\n",
unknown's avatar
unknown committed
2027 2028
				(ulong) read_space_id, (ulong) read_page_no,
				(ulong) block->space, (ulong) block->offset);
unknown's avatar
unknown committed
2029
		}
2030
		/* From version 3.23.38 up we store the page checksum
unknown's avatar
unknown committed
2031
		to the 4 first bytes of the page end lsn field */
2032

2033
		if (buf_page_is_corrupted(block->frame)) {
2034
			fprintf(stderr,
unknown's avatar
unknown committed
2035 2036 2037 2038
				"InnoDB: Database page corruption on disk"
				" or a failed\n"
				"InnoDB: file read of page %lu.\n",
				(ulong) block->offset);
2039

unknown's avatar
unknown committed
2040 2041
			fputs("InnoDB: You may have to recover"
			      " from a backup.\n", stderr);
2042 2043 2044

			buf_page_print(block->frame);

2045
			fprintf(stderr,
unknown's avatar
unknown committed
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
				"InnoDB: Database page corruption on disk"
				" or a failed\n"
				"InnoDB: file read of page %lu.\n",
				(ulong) block->offset);
			fputs("InnoDB: You may have to recover"
			      " from a backup.\n", stderr);
			fputs("InnoDB: It is also possible that"
			      " your operating\n"
			      "InnoDB: system has corrupted its"
			      " own file cache\n"
			      "InnoDB: and rebooting your computer"
			      " removes the\n"
			      "InnoDB: error.\n"
			      "InnoDB: If the corrupt page is an index page\n"
			      "InnoDB: you can also try to"
			      " fix the corruption\n"
			      "InnoDB: by dumping, dropping,"
			      " and reimporting\n"
			      "InnoDB: the corrupt table."
			      " You can use CHECK\n"
			      "InnoDB: TABLE to scan your"
			      " table for corruption.\n"
			      "InnoDB: See also"
			      " http://dev.mysql.com/doc/refman/5.1/en/"
			      "forcing-recovery.html\n"
			      "InnoDB: about forcing recovery.\n", stderr);
2072

unknown's avatar
unknown committed
2073
			if (srv_force_recovery < SRV_FORCE_IGNORE_CORRUPT) {
unknown's avatar
unknown committed
2074 2075 2076
				fputs("InnoDB: Ending processing because of"
				      " a corrupt database page.\n",
				      stderr);
2077 2078
				exit(1);
			}
2079 2080
		}

2081
		if (recv_recovery_is_on()) {
unknown's avatar
unknown committed
2082
			recv_recover_page(FALSE, TRUE, block->frame,
unknown's avatar
unknown committed
2083
					  block->space, block->offset);
2084 2085 2086
		}

		if (!recv_no_ibuf_operations) {
2087 2088 2089
			ibuf_merge_or_delete_for_page(
				block->frame, block->space, block->offset,
				TRUE);
2090 2091
		}
	}
2092

unknown's avatar
unknown committed
2093 2094 2095
	mutex_enter(&(buf_pool->mutex));
	mutex_enter(&block->mutex);

2096 2097 2098 2099 2100 2101 2102 2103 2104
#ifdef UNIV_IBUF_DEBUG
	ut_a(ibuf_count_get(block->space, block->offset) == 0);
#endif
	/* Because this thread which does the unlocking is not the same that
	did the locking, we use a pass value != 0 in unlock, which simply
	removes the newest lock debug record, without checking the thread
	id. */

	block->io_fix = 0;
2105

2106 2107 2108
	if (io_type == BUF_IO_READ) {
		/* NOTE that the call to ibuf may have moved the ownership of
		the x-latch to this OS thread: do not let this confuse you in
2109 2110
		debugging! */

2111 2112 2113 2114 2115 2116
		ut_ad(buf_pool->n_pend_reads > 0);
		buf_pool->n_pend_reads--;
		buf_pool->n_pages_read++;

		rw_lock_x_unlock_gen(&(block->lock), BUF_IO_READ);

2117
#ifdef UNIV_DEBUG
2118
		if (buf_debug_prints) {
2119
			fputs("Has read ", stderr);
2120
		}
2121
#endif /* UNIV_DEBUG */
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
	} else {
		ut_ad(io_type == BUF_IO_WRITE);

		/* Write means a flush operation: call the completion
		routine in the flush system */

		buf_flush_write_complete(block);

		rw_lock_s_unlock_gen(&(block->lock), BUF_IO_WRITE);

		buf_pool->n_pages_written++;

2134
#ifdef UNIV_DEBUG
2135
		if (buf_debug_prints) {
2136
			fputs("Has written ", stderr);
2137
		}
2138
#endif /* UNIV_DEBUG */
2139
	}
2140

unknown's avatar
unknown committed
2141
	mutex_exit(&block->mutex);
2142 2143
	mutex_exit(&(buf_pool->mutex));

2144
#ifdef UNIV_DEBUG
2145
	if (buf_debug_prints) {
2146
		fprintf(stderr, "page space %lu page no %lu\n",
unknown's avatar
unknown committed
2147
			(ulong) block->space, (ulong) block->offset);
2148
	}
2149
#endif /* UNIV_DEBUG */
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
}

/*************************************************************************
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)
/*=====================*/
{
	ibool	freed;

	ut_ad(buf_all_freed());
2164

2165 2166 2167
	freed = TRUE;

	while (freed) {
2168
		freed = buf_LRU_search_and_free_block(100);
2169
	}
2170

2171 2172 2173 2174 2175 2176 2177
	mutex_enter(&(buf_pool->mutex));

	ut_ad(UT_LIST_GET_LEN(buf_pool->LRU) == 0);

	mutex_exit(&(buf_pool->mutex));
}

2178
#ifdef UNIV_DEBUG
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194
/*************************************************************************
Validates the buffer buf_pool data structure. */

ibool
buf_validate(void)
/*==============*/
{
	buf_block_t*	block;
	ulint		i;
	ulint		n_single_flush	= 0;
	ulint		n_lru_flush	= 0;
	ulint		n_list_flush	= 0;
	ulint		n_lru		= 0;
	ulint		n_flush		= 0;
	ulint		n_free		= 0;
	ulint		n_page		= 0;
2195

2196 2197 2198 2199 2200 2201 2202 2203
	ut_ad(buf_pool);

	mutex_enter(&(buf_pool->mutex));

	for (i = 0; i < buf_pool->curr_size; i++) {

		block = buf_pool_get_nth_block(buf_pool, i);

unknown's avatar
unknown committed
2204 2205
		mutex_enter(&block->mutex);

2206 2207 2208
		if (block->state == BUF_BLOCK_FILE_PAGE) {

			ut_a(buf_page_hash_get(block->space,
unknown's avatar
unknown committed
2209
					       block->offset) == block);
2210 2211 2212 2213
			n_page++;

#ifdef UNIV_IBUF_DEBUG
			ut_a((block->io_fix == BUF_IO_READ)
unknown's avatar
unknown committed
2214 2215
			     || ibuf_count_get(block->space, block->offset)
			     == 0);
2216 2217 2218 2219 2220
#endif
			if (block->io_fix == BUF_IO_WRITE) {

				if (block->flush_type == BUF_FLUSH_LRU) {
					n_lru_flush++;
2221 2222 2223
					ut_a(rw_lock_is_locked(
						     &block->lock,
						     RW_LOCK_SHARED));
unknown's avatar
unknown committed
2224 2225
				} else if (block->flush_type
					   == BUF_FLUSH_LIST) {
2226
					n_list_flush++;
unknown's avatar
unknown committed
2227 2228
				} else if (block->flush_type
					   == BUF_FLUSH_SINGLE_PAGE) {
2229 2230 2231 2232 2233 2234 2235 2236
					n_single_flush++;
				} else {
					ut_error;
				}

			} else if (block->io_fix == BUF_IO_READ) {

				ut_a(rw_lock_is_locked(&(block->lock),
unknown's avatar
unknown committed
2237
						       RW_LOCK_EX));
2238
			}
2239

2240 2241 2242
			n_lru++;

			if (ut_dulint_cmp(block->oldest_modification,
unknown's avatar
unknown committed
2243 2244
					  ut_dulint_zero) > 0) {
				n_flush++;
2245 2246
			}

2247 2248 2249
		} else if (block->state == BUF_BLOCK_NOT_USED) {
			n_free++;
		}
unknown's avatar
unknown committed
2250 2251

		mutex_exit(&block->mutex);
2252
	}
2253 2254

	if (n_lru + n_free > buf_pool->curr_size) {
unknown's avatar
unknown committed
2255 2256
		fprintf(stderr, "n LRU %lu, n free %lu\n",
			(ulong) n_lru, (ulong) n_free);
2257 2258 2259 2260 2261
		ut_error;
	}

	ut_a(UT_LIST_GET_LEN(buf_pool->LRU) == n_lru);
	if (UT_LIST_GET_LEN(buf_pool->free) != n_free) {
2262
		fprintf(stderr, "Free list len %lu, free blocks %lu\n",
unknown's avatar
unknown committed
2263 2264
			(ulong) UT_LIST_GET_LEN(buf_pool->free),
			(ulong) n_free);
2265 2266 2267 2268 2269 2270 2271
		ut_error;
	}
	ut_a(UT_LIST_GET_LEN(buf_pool->flush_list) == n_flush);

	ut_a(buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE] == n_single_flush);
	ut_a(buf_pool->n_flush[BUF_FLUSH_LIST] == n_list_flush);
	ut_a(buf_pool->n_flush[BUF_FLUSH_LRU] == n_lru_flush);
2272

2273 2274 2275 2276 2277 2278
	mutex_exit(&(buf_pool->mutex));

	ut_a(buf_LRU_validate());
	ut_a(buf_flush_validate());

	return(TRUE);
2279
}
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294

/*************************************************************************
Prints info of the buffer buf_pool data structure. */

void
buf_print(void)
/*===========*/
{
	dulint*		index_ids;
	ulint*		counts;
	ulint		size;
	ulint		i;
	ulint		j;
	dulint		id;
	ulint		n_found;
2295
	buf_frame_t*	frame;
2296
	dict_index_t*	index;
2297

2298 2299
	ut_ad(buf_pool);

unknown's avatar
unknown committed
2300
	size = buf_pool->curr_size;
2301 2302 2303 2304 2305

	index_ids = mem_alloc(sizeof(dulint) * size);
	counts = mem_alloc(sizeof(ulint) * size);

	mutex_enter(&(buf_pool->mutex));
2306

2307
	fprintf(stderr,
unknown's avatar
unknown committed
2308 2309 2310 2311 2312
		"buf_pool size %lu\n"
		"database pages %lu\n"
		"free pages %lu\n"
		"modified database pages %lu\n"
		"n pending reads %lu\n"
2313 2314
		"n pending flush LRU %lu list %lu single page %lu\n"
		"pages read %lu, created %lu, written %lu\n",
unknown's avatar
unknown committed
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
		(ulong) size,
		(ulong) UT_LIST_GET_LEN(buf_pool->LRU),
		(ulong) UT_LIST_GET_LEN(buf_pool->free),
		(ulong) UT_LIST_GET_LEN(buf_pool->flush_list),
		(ulong) buf_pool->n_pend_reads,
		(ulong) buf_pool->n_flush[BUF_FLUSH_LRU],
		(ulong) buf_pool->n_flush[BUF_FLUSH_LIST],
		(ulong) buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE],
		(ulong) buf_pool->n_pages_read, buf_pool->n_pages_created,
		(ulong) buf_pool->n_pages_written);
2325 2326

	/* Count the number of blocks belonging to each index in the buffer */
2327

2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
	n_found = 0;

	for (i = 0; i < size; i++) {
		frame = buf_pool_get_nth_block(buf_pool, i)->frame;

		if (fil_page_get_type(frame) == FIL_PAGE_INDEX) {

			id = btr_page_get_index_id(frame);

			/* Look for the id in the index_ids array */
			j = 0;

			while (j < n_found) {

				if (ut_dulint_cmp(index_ids[j], id) == 0) {
					(counts[j])++;

					break;
				}
				j++;
			}

			if (j == n_found) {
				n_found++;
				index_ids[j] = id;
				counts[j] = 1;
			}
		}
	}

	mutex_exit(&(buf_pool->mutex));

	for (i = 0; i < n_found; i++) {
		index = dict_index_get_if_in_cache(index_ids[i]);

2363 2364
		fprintf(stderr,
			"Block count for index %lu in buffer is about %lu",
2365 2366
			(ulong) ut_dulint_get_low(index_ids[i]),
			(ulong) counts[i]);
2367 2368

		if (index) {
2369
			putc(' ', stderr);
2370
			dict_index_name_print(stderr, NULL, index);
2371 2372
		}

2373
		putc('\n', stderr);
2374
	}
2375

2376 2377 2378 2379
	mem_free(index_ids);
	mem_free(counts);

	ut_a(buf_validate());
2380
}
2381

2382 2383 2384 2385 2386 2387
/*************************************************************************
Returns the number of latched pages in the buffer pool. */

ulint
buf_get_latched_pages_number(void)
{
unknown's avatar
unknown committed
2388 2389 2390
	buf_block_t*	block;
	ulint		i;
	ulint		fixed_pages_number = 0;
2391

2392
	mutex_enter(&(buf_pool->mutex));
2393

2394
	for (i = 0; i < buf_pool->curr_size; i++) {
2395

2396
		block = buf_pool_get_nth_block(buf_pool, i);
2397

unknown's avatar
unknown committed
2398 2399 2400 2401 2402 2403 2404 2405
		if (block->magic_n == BUF_BLOCK_MAGIC_N) {
			mutex_enter(&block->mutex);

			if (block->buf_fix_count != 0 || block->io_fix != 0) {
				fixed_pages_number++;
			}

			mutex_exit(&block->mutex);
unknown's avatar
unknown committed
2406
		}
2407
	}
2408

2409
	mutex_exit(&(buf_pool->mutex));
unknown's avatar
unknown committed
2410 2411

	return(fixed_pages_number);
2412
}
2413
#endif /* UNIV_DEBUG */
2414

2415 2416 2417 2418 2419 2420 2421 2422
/*************************************************************************
Returns the number of pending buf pool ios. */

ulint
buf_get_n_pending_ios(void)
/*=======================*/
{
	return(buf_pool->n_pend_reads
unknown's avatar
unknown committed
2423 2424 2425
	       + buf_pool->n_flush[BUF_FLUSH_LRU]
	       + buf_pool->n_flush[BUF_FLUSH_LIST]
	       + buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]);
2426 2427
}

2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
/*************************************************************************
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)
/*============================*/
{
	ulint	ratio;

	mutex_enter(&(buf_pool->mutex));

	ratio = (100 * UT_LIST_GET_LEN(buf_pool->flush_list))
2441
		/ (1 + UT_LIST_GET_LEN(buf_pool->LRU)
unknown's avatar
unknown committed
2442
		   + UT_LIST_GET_LEN(buf_pool->free));
2443

2444
	/* 1 + is there to avoid division by zero */
2445 2446 2447 2448 2449 2450

	mutex_exit(&(buf_pool->mutex));

	return(ratio);
}

2451 2452 2453 2454
/*************************************************************************
Prints info of the buffer i/o. */

void
unknown's avatar
unknown committed
2455 2456
buf_print_io(
/*=========*/
2457
	FILE*	file)	/* in/out: buffer where to print */
2458
{
2459 2460
	time_t	current_time;
	double	time_elapsed;
2461
	ulint	size;
2462

2463
	ut_ad(buf_pool);
unknown's avatar
unknown committed
2464
	size = buf_pool->curr_size;
2465

2466
	mutex_enter(&(buf_pool->mutex));
2467

unknown's avatar
unknown committed
2468
	if (srv_use_awe) {
unknown's avatar
unknown committed
2469
		fprintf(stderr,
unknown's avatar
unknown committed
2470 2471
			"AWE: Buffer pool memory frames %lu\n",
			(ulong) buf_pool->n_frames);
2472

unknown's avatar
unknown committed
2473
		fprintf(stderr,
unknown's avatar
unknown committed
2474 2475 2476 2477
			"AWE: Database pages and free buffers"
			" mapped in frames %lu\n",
			(ulong)
			UT_LIST_GET_LEN(buf_pool->awe_LRU_free_mapped));
unknown's avatar
unknown committed
2478
	}
2479 2480 2481 2482 2483
	fprintf(file,
		"Buffer pool size   %lu\n"
		"Free buffers       %lu\n"
		"Database pages     %lu\n"
		"Modified db pages  %lu\n"
unknown's avatar
unknown committed
2484
		"Pending reads %lu\n"
unknown's avatar
unknown committed
2485
		"Pending writes: LRU %lu, flush list %lu, single page %lu\n",
unknown's avatar
unknown committed
2486 2487 2488 2489 2490 2491
		(ulong) size,
		(ulong) UT_LIST_GET_LEN(buf_pool->free),
		(ulong) UT_LIST_GET_LEN(buf_pool->LRU),
		(ulong) UT_LIST_GET_LEN(buf_pool->flush_list),
		(ulong) buf_pool->n_pend_reads,
		(ulong) buf_pool->n_flush[BUF_FLUSH_LRU]
unknown's avatar
unknown committed
2492
		+ buf_pool->init_flush[BUF_FLUSH_LRU],
unknown's avatar
unknown committed
2493
		(ulong) buf_pool->n_flush[BUF_FLUSH_LIST]
unknown's avatar
unknown committed
2494
		+ buf_pool->init_flush[BUF_FLUSH_LIST],
2495
		(ulong) buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]);
2496

2497
	current_time = time(NULL);
unknown's avatar
unknown committed
2498
	time_elapsed = 0.001 + difftime(current_time,
unknown's avatar
unknown committed
2499
					buf_pool->last_printout_time);
2500 2501
	buf_pool->last_printout_time = current_time;

2502 2503 2504
	fprintf(file,
		"Pages read %lu, created %lu, written %lu\n"
		"%.2f reads/s, %.2f creates/s, %.2f writes/s\n",
unknown's avatar
unknown committed
2505 2506 2507
		(ulong) buf_pool->n_pages_read,
		(ulong) buf_pool->n_pages_created,
		(ulong) buf_pool->n_pages_written,
2508 2509 2510 2511 2512 2513
		(buf_pool->n_pages_read - buf_pool->n_pages_read_old)
		/ time_elapsed,
		(buf_pool->n_pages_created - buf_pool->n_pages_created_old)
		/ time_elapsed,
		(buf_pool->n_pages_written - buf_pool->n_pages_written_old)
		/ time_elapsed);
2514

unknown's avatar
unknown committed
2515
	if (srv_use_awe) {
unknown's avatar
unknown committed
2516
		fprintf(file, "AWE: %.2f page remaps/s\n",
unknown's avatar
unknown committed
2517 2518
			(buf_pool->n_pages_awe_remapped
			 - buf_pool->n_pages_awe_remapped_old)
unknown's avatar
unknown committed
2519 2520
			/ time_elapsed);
	}
2521

2522
	if (buf_pool->n_page_gets > buf_pool->n_page_gets_old) {
2523
		fprintf(file, "Buffer pool hit rate %lu / 1000\n",
unknown's avatar
unknown committed
2524 2525 2526 2527 2528
			(ulong)
			(1000 - ((1000 * (buf_pool->n_pages_read
					  - buf_pool->n_pages_read_old))
				 / (buf_pool->n_page_gets
				    - buf_pool->n_page_gets_old))));
2529
	} else {
2530
		fputs("No buffer pool page gets since the last printout\n",
unknown's avatar
unknown committed
2531
		      file);
2532 2533 2534 2535
	}

	buf_pool->n_page_gets_old = buf_pool->n_page_gets;
	buf_pool->n_pages_read_old = buf_pool->n_pages_read;
2536 2537
	buf_pool->n_pages_created_old = buf_pool->n_pages_created;
	buf_pool->n_pages_written_old = buf_pool->n_pages_written;
unknown's avatar
unknown committed
2538
	buf_pool->n_pages_awe_remapped_old = buf_pool->n_pages_awe_remapped;
2539

2540 2541 2542
	mutex_exit(&(buf_pool->mutex));
}

unknown's avatar
unknown committed
2543 2544 2545 2546 2547 2548 2549
/**************************************************************************
Refreshes the statistics used to print per-second averages. */

void
buf_refresh_io_stats(void)
/*======================*/
{
2550
	buf_pool->last_printout_time = time(NULL);
unknown's avatar
unknown committed
2551 2552 2553 2554
	buf_pool->n_page_gets_old = buf_pool->n_page_gets;
	buf_pool->n_pages_read_old = buf_pool->n_pages_read;
	buf_pool->n_pages_created_old = buf_pool->n_pages_created;
	buf_pool->n_pages_written_old = buf_pool->n_pages_written;
2555
	buf_pool->n_pages_awe_remapped_old = buf_pool->n_pages_awe_remapped;
unknown's avatar
unknown committed
2556 2557
}

2558 2559 2560 2561 2562 2563 2564 2565 2566
/*************************************************************************
Checks that all file pages in the buffer are in a replaceable state. */

ibool
buf_all_freed(void)
/*===============*/
{
	buf_block_t*	block;
	ulint		i;
2567

2568 2569 2570 2571 2572 2573 2574 2575
	ut_ad(buf_pool);

	mutex_enter(&(buf_pool->mutex));

	for (i = 0; i < buf_pool->curr_size; i++) {

		block = buf_pool_get_nth_block(buf_pool, i);

unknown's avatar
unknown committed
2576 2577
		mutex_enter(&block->mutex);

2578 2579 2580 2581
		if (block->state == BUF_BLOCK_FILE_PAGE) {

			if (!buf_flush_ready_for_replace(block)) {

2582 2583
				fprintf(stderr,
					"Page %lu %lu still fixed or dirty\n",
unknown's avatar
unknown committed
2584 2585
					(ulong) block->space,
					(ulong) block->offset);
2586
				ut_error;
2587 2588
			}
		}
unknown's avatar
unknown committed
2589 2590

		mutex_exit(&block->mutex);
2591
	}
2592 2593 2594 2595

	mutex_exit(&(buf_pool->mutex));

	return(TRUE);
2596
}
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611

/*************************************************************************
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 */
{
	ibool	ret;

	mutex_enter(&(buf_pool->mutex));

	if (buf_pool->n_pend_reads + buf_pool->n_flush[BUF_FLUSH_LRU]
unknown's avatar
unknown committed
2612 2613
	    + buf_pool->n_flush[BUF_FLUSH_LIST]
	    + buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]) {
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640
		ret = FALSE;
	} else {
		ret = TRUE;
	}

	mutex_exit(&(buf_pool->mutex));

	return(ret);
}

/*************************************************************************
Gets the current length of the free list of buffer blocks. */

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

	mutex_enter(&(buf_pool->mutex));

	len = UT_LIST_GET_LEN(buf_pool->free);

	mutex_exit(&(buf_pool->mutex));

	return(len);
}