buf0buf.c 152 KB
Newer Older
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1
/*****************************************************************************
2

3
Copyright (c) 1995, 2011, Oracle and/or its affiliates. All Rights Reserved.
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4
Copyright (c) 2008, Google Inc.
5

Vadim Tkachenko's avatar
Vadim Tkachenko committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
briefly in the InnoDB documentation. The contributions by Google are
incorporated with their permission, and subject to the conditions contained in
the file COPYING.Google.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

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.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

*****************************************************************************/
25

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
26 27
/**************************************************//**
@file buf/buf0buf.c
28 29 30 31 32 33 34 35 36 37 38 39 40 41
The database buffer buf_pool

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"
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
42 43
#ifndef UNIV_HOTBACKUP
#include "buf0buddy.h"
44 45 46 47
#include "lock0lock.h"
#include "btr0sea.h"
#include "ibuf0ibuf.h"
#include "trx0undo.h"
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
48 49
#include "log0log.h"
#endif /* !UNIV_HOTBACKUP */
50
#include "srv0srv.h"
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
51 52
#include "dict0dict.h"
#include "log0recv.h"
53
#include "page0zip.h"
54
#include "trx0trx.h"
55
#include "srv0start.h"
56

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
57 58 59
/* prototypes for new functions added to ha_innodb.cc */
trx_t* innobase_get_trx();

60
static inline void _increment_page_get_statistics(buf_block_t* block, trx_t* trx)
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
{
	ulint           block_hash;
	ulint           block_hash_byte;
	byte            block_hash_offset;

	ut_ad(block);

	if (!innobase_get_slow_log() || !trx || !trx->take_stats)
		return;

	if (!trx->distinct_page_access_hash) {
		trx->distinct_page_access_hash = mem_alloc(DPAH_SIZE);
		memset(trx->distinct_page_access_hash, 0, DPAH_SIZE);
	}

	block_hash = ut_hash_ulint((block->page.space << 20) + block->page.space +
					block->page.offset, DPAH_SIZE << 3);
	block_hash_byte = block_hash >> 3;
	block_hash_offset = (byte) block_hash & 0x07;
80
	if (block_hash_byte >= DPAH_SIZE)
81
		fprintf(stderr, "!!! block_hash_byte = %lu  block_hash_offset = %d !!!\n", block_hash_byte, block_hash_offset);
82
	if (block_hash_offset > 7)
83
		fprintf(stderr, "!!! block_hash_byte = %lu  block_hash_offset = %d !!!\n", block_hash_byte, block_hash_offset);
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
84 85 86 87 88 89
	if ((trx->distinct_page_access_hash[block_hash_byte] & ((byte) 0x01 << block_hash_offset)) == 0)
		trx->distinct_page_access++;
	trx->distinct_page_access_hash[block_hash_byte] |= (byte) 0x01 << block_hash_offset;
	return;
}

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
/*
		IMPLEMENTATION OF THE BUFFER POOL
		=================================

Performance improvement:
------------------------
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.
Sergei Golubchik's avatar
Sergei Golubchik committed
122
These locks can be locked and unlocked without owning the buf_pool->mutex.
123
The OS events in the buf_pool struct can be waited for without owning the
Sergei Golubchik's avatar
Sergei Golubchik committed
124
buf_pool->mutex.
125

Sergei Golubchik's avatar
Sergei Golubchik committed
126
The buf_pool->mutex is a hot-spot in main memory, causing a lot of
127 128 129 130 131 132
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.

Sergei Golubchik's avatar
Sergei Golubchik committed
133
A solution to reduce mutex contention of the buf_pool->mutex is to
134 135
create a separate mutex for the page hash table. On Pentium,
accessing the hash table takes 2 microseconds, about half
Sergei Golubchik's avatar
Sergei Golubchik committed
136
of the total buf_pool->mutex hold time.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

		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 (buf_pool->free) contains blocks which are currently not
used.

The common 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,
Sergei Golubchik's avatar
Sergei Golubchik committed
191
causing it to be replaced sooner than would normally be the case.
192 193 194
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
Sergei Golubchik's avatar
Sergei Golubchik committed
195 196
end of the LRU list, we make sure that most of the buf_pool stays
in the main memory, undisturbed.
197 198 199 200 201 202 203 204 205 206 207 208 209

The unzip_LRU list contains a subset of the common LRU list.  The
blocks on the unzip_LRU list hold a compressed file page and the
corresponding uncompressed page frame.  A block is in unzip_LRU if and
only if the predicate buf_page_belongs_to_unzip_LRU(&block->page)
holds.  The blocks in unzip_LRU will be in same order as they are in
the common LRU list.  That is, each manipulation of the common LRU
list will result in the same manipulation of the unzip_LRU list.

The chain of modified blocks (buf_pool->flush_list) 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.
Sergei Golubchik's avatar
Sergei Golubchik committed
210
The access to this list is protected by buf_pool->flush_list_mutex.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

The chain of unmodified compressed blocks (buf_pool->zip_clean)
contains the control blocks (buf_page_t) of those compressed pages
that are not in buf_pool->flush_list and for which no uncompressed
page has been allocated in the buffer pool.  The control blocks for
uncompressed pages are accessible via buf_block_t objects that are
reachable via buf_pool->chunks[].

The chains of free memory blocks (buf_pool->zip_free[]) are used by
the buddy allocator (buf0buddy.c) to keep track of currently unused
memory blocks of size sizeof(buf_page_t)..UNIV_PAGE_SIZE / 2.  These
blocks are inside the UNIV_PAGE_SIZE-sized memory blocks of type
BUF_BLOCK_MEMORY that the buddy allocator requests from the buffer
pool.  The buddy allocator is solely used for allocating control
blocks for compressed pages (buf_page_t) and compressed page frames.

		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.

A thread may request the above operation using the function
buf_page_get(). It may then continue to request a lock on the frame.
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
the read requests for the whole area.
*/

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
278 279
#ifndef UNIV_HOTBACKUP
/** Value in microseconds */
280
static const int WAIT_FOR_READ	= 5000;
281 282
/** Number of attemtps made to read in a page in the buffer pool */
static const ulint BUF_PAGE_READ_MAX_RETRIES = 100;
283

Sergei Golubchik's avatar
Sergei Golubchik committed
284 285
/** The buffer pools of the database */
UNIV_INTERN buf_pool_t*	buf_pool_ptr;
286 287

#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
288
static ulint	buf_dbg_counter	= 0; /*!< This is used to insert validation
Sergei Golubchik's avatar
Sergei Golubchik committed
289
					operations in execution in the
290 291 292
					debug version */
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
#ifdef UNIV_DEBUG
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
293
/** If this is set TRUE, the program prints info whenever
294 295 296 297
read-ahead or flush occurs */
UNIV_INTERN ibool		buf_debug_prints = FALSE;
#endif /* UNIV_DEBUG */

Sergei Golubchik's avatar
Sergei Golubchik committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
#ifdef UNIV_PFS_RWLOCK
/* Keys to register buffer block related rwlocks and mutexes with
performance schema */
UNIV_INTERN mysql_pfs_key_t	buf_pool_page_hash_key;
UNIV_INTERN mysql_pfs_key_t	buf_block_lock_key;
# ifdef UNIV_SYNC_DEBUG
UNIV_INTERN mysql_pfs_key_t	buf_block_debug_latch_key;
# endif /* UNIV_SYNC_DEBUG */
#endif /* UNIV_PFS_RWLOCK */

#ifdef UNIV_PFS_MUTEX
UNIV_INTERN mysql_pfs_key_t	buffer_block_mutex_key;
UNIV_INTERN mysql_pfs_key_t	buf_pool_mutex_key;
UNIV_INTERN mysql_pfs_key_t	buf_pool_zip_mutex_key;
UNIV_INTERN mysql_pfs_key_t	buf_pool_LRU_list_mutex_key;
UNIV_INTERN mysql_pfs_key_t	buf_pool_free_list_mutex_key;
UNIV_INTERN mysql_pfs_key_t	buf_pool_zip_free_mutex_key;
UNIV_INTERN mysql_pfs_key_t	buf_pool_zip_hash_mutex_key;
UNIV_INTERN mysql_pfs_key_t	flush_list_mutex_key;
#endif /* UNIV_PFS_MUTEX */

#if defined UNIV_PFS_MUTEX || defined UNIV_PFS_RWLOCK
# ifndef PFS_SKIP_BUFFER_MUTEX_RWLOCK

/* Buffer block mutexes and rwlocks can be registered
in one group rather than individually. If PFS_GROUP_BUFFER_SYNC
is defined, register buffer block mutex and rwlock
in one group after their initialization. */
#  define PFS_GROUP_BUFFER_SYNC

/* This define caps the number of mutexes/rwlocks can
be registered with performance schema. Developers can
modify this define if necessary. Please note, this would
be effective only if PFS_GROUP_BUFFER_SYNC is defined. */
#  define PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER	ULINT_MAX

# endif /* !PFS_SKIP_BUFFER_MUTEX_RWLOCK */
#endif /* UNIV_PFS_MUTEX || UNIV_PFS_RWLOCK */

/** A chunk of buffers.  The buffer pool is allocated in chunks. (moved to buf0buf.h)*/
//struct buf_chunk_struct{
//	ulint		mem_size;	/*!< allocated size of the chunk */
//	ulint		size;		/*!< size of frames[] and blocks[] */
//	void*		mem;		/*!< pointer to the memory area which
//					was allocated for the frames */
//	buf_block_t*	blocks;		/*!< array of buffer control blocks */
//};
#endif /* !UNIV_HOTBACKUP */

/********************************************************************//**
Gets the smallest oldest_modification lsn for any page in the pool. Returns
zero if all modified pages have been flushed to disk.
@return oldest modification in pool, zero if none */
UNIV_INTERN
ib_uint64_t
buf_pool_get_oldest_modification(void)
/*==================================*/
{
	ulint		i;
	buf_page_t*	bpage;
	ib_uint64_t	lsn = 0;
	ib_uint64_t	oldest_lsn = 0;
360

Sergei Golubchik's avatar
Sergei Golubchik committed
361 362 363 364
	/* When we traverse all the flush lists we don't want another
	thread to add a dirty page to any flush list. */
	if (srv_buf_pool_instances > 1)
	log_flush_order_mutex_enter();
365

Sergei Golubchik's avatar
Sergei Golubchik committed
366 367
	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_t*	buf_pool;
368

Sergei Golubchik's avatar
Sergei Golubchik committed
369
		buf_pool = buf_pool_from_array(i);
370

Sergei Golubchik's avatar
Sergei Golubchik committed
371
		buf_flush_list_mutex_enter(buf_pool);
372

Sergei Golubchik's avatar
Sergei Golubchik committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
		bpage = UT_LIST_GET_LAST(buf_pool->flush_list);

		if (bpage != NULL) {
			ut_ad(bpage->in_flush_list);
			lsn = bpage->oldest_modification;
		}

		buf_flush_list_mutex_exit(buf_pool);

		if (!oldest_lsn || oldest_lsn > lsn) {
			oldest_lsn = lsn;
		}
	}

	if (srv_buf_pool_instances > 1)
	log_flush_order_mutex_exit();

	/* The returned answer may be out of date: the flush_list can
	change after the mutex has been released. */

	return(oldest_lsn);
}

/********************************************************************//**
Get total buffer pool statistics. */
UNIV_INTERN
void
buf_get_total_list_len(
/*===================*/
	ulint*		LRU_len,	/*!< out: length of all LRU lists */
	ulint*		free_len,	/*!< out: length of all free lists */
	ulint*		flush_list_len)	/*!< out: length of all flush lists */
{
	ulint		i;

	*LRU_len = 0;
	*free_len = 0;
	*flush_list_len = 0;

	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_t*	buf_pool;

		buf_pool = buf_pool_from_array(i);
		*LRU_len += UT_LIST_GET_LEN(buf_pool->LRU);
		*free_len += UT_LIST_GET_LEN(buf_pool->free);
		*flush_list_len += UT_LIST_GET_LEN(buf_pool->flush_list);
	}
}

/********************************************************************//**
Get total buffer pool statistics. */
UNIV_INTERN
void
buf_get_total_stat(
/*===============*/
	buf_pool_stat_t*	tot_stat)	/*!< out: buffer pool stats */
{
	ulint			i;

	memset(tot_stat, 0, sizeof(*tot_stat));

	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_stat_t*buf_stat;
		buf_pool_t*	buf_pool;

		buf_pool = buf_pool_from_array(i);

		buf_stat = &buf_pool->stat;
		tot_stat->n_page_gets += buf_stat->n_page_gets;
		tot_stat->n_pages_read += buf_stat->n_pages_read;
		tot_stat->n_pages_written += buf_stat->n_pages_written;
		tot_stat->n_pages_created += buf_stat->n_pages_created;
445
		tot_stat->n_ra_pages_read_rnd += buf_stat->n_ra_pages_read_rnd;
Sergei Golubchik's avatar
Sergei Golubchik committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
		tot_stat->n_ra_pages_read += buf_stat->n_ra_pages_read;
		tot_stat->n_ra_pages_evicted += buf_stat->n_ra_pages_evicted;
		tot_stat->n_pages_made_young += buf_stat->n_pages_made_young;

		tot_stat->n_pages_not_made_young +=
			buf_stat->n_pages_not_made_young;
	}
}

/********************************************************************//**
Allocates a buffer block.
@return own: the allocated block, in state BUF_BLOCK_MEMORY */
UNIV_INTERN
buf_block_t*
buf_block_alloc(
/*============*/
	buf_pool_t*	buf_pool)	/*!< in/out: buffer pool instance,
					or NULL for round-robin selection
					of the buffer pool */
{
	buf_block_t*	block;
	ulint		index;
	static ulint	buf_pool_index;

	if (buf_pool == NULL) {
		/* We are allocating memory from any buffer pool, ensure
		we spread the grace on all buffer pool instances. */
		index = buf_pool_index++ % srv_buf_pool_instances;
		buf_pool = buf_pool_from_array(index);
	}

	block = buf_LRU_get_free_block(buf_pool);

	buf_block_set_state(block, BUF_BLOCK_MEMORY);

	return(block);
}
483

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
484
/********************************************************************//**
485 486
Calculates a page checksum which is stored to the page when it is written
to a file. Note that we must be careful to calculate the same value on
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
487 488
32-bit and 64-bit architectures.
@return	checksum */
489 490 491 492
UNIV_INTERN
ulint
buf_calc_page_new_checksum(
/*=======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
493
	const byte*	page)	/*!< in: buffer page */
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
{
	ulint checksum;

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

	checksum = ut_fold_binary(page + FIL_PAGE_OFFSET,
				  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);
	checksum = checksum & 0xFFFFFFFFUL;

	return(checksum);
}

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
UNIV_INTERN
ulint
buf_calc_page_new_checksum_32(
/*==========================*/
	const byte*	page)	/*!< in: buffer page */
{
	ulint checksum;

	checksum = ut_fold_binary(page + FIL_PAGE_OFFSET,
				  FIL_PAGE_FILE_FLUSH_LSN - FIL_PAGE_OFFSET)
		+ ut_fold_binary(page + FIL_PAGE_DATA,
				 FIL_PAGE_DATA_ALIGN_32 - FIL_PAGE_DATA)
		+ ut_fold_binary_32(page + FIL_PAGE_DATA_ALIGN_32,
				    UNIV_PAGE_SIZE - FIL_PAGE_DATA_ALIGN_32
				    - FIL_PAGE_END_LSN_OLD_CHKSUM);

	checksum = checksum & 0xFFFFFFFFUL;

	return(checksum);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
536
/********************************************************************//**
537 538 539 540 541
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
checksum.
NOTE: we must first store the new formula checksum to
FIL_PAGE_SPACE_OR_CHKSUM before calculating and storing this old checksum
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
542 543
because this takes that field as an input!
@return	checksum */
544 545 546 547
UNIV_INTERN
ulint
buf_calc_page_old_checksum(
/*=======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
548
	const byte*	page)	/*!< in: buffer page */
549 550 551 552 553 554 555 556 557 558
{
	ulint checksum;

	checksum = ut_fold_binary(page, FIL_PAGE_FILE_FLUSH_LSN);

	checksum = checksum & 0xFFFFFFFFUL;

	return(checksum);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
559 560 561
/********************************************************************//**
Checks if a page is corrupt.
@return	TRUE if corrupted */
562 563 564 565
UNIV_INTERN
ibool
buf_page_is_corrupted(
/*==================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
566 567
	const byte*	read_buf,	/*!< in: a database page */
	ulint		zip_size)	/*!< in: size of compressed page;
568 569 570 571
					0 for uncompressed pages */
{
	ulint		checksum_field;
	ulint		old_checksum_field;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
572

573 574 575 576 577 578 579 580 581 582 583 584
	if (UNIV_LIKELY(!zip_size)
	    && memcmp(read_buf + FIL_PAGE_LSN + 4,
		      read_buf + UNIV_PAGE_SIZE
		      - FIL_PAGE_END_LSN_OLD_CHKSUM + 4, 4)) {

		/* Stored log sequence numbers at the start and the end
		of page do not match */

		return(TRUE);
	}

#ifndef UNIV_HOTBACKUP
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
585 586 587 588
	if (recv_lsn_checks_on) {
		ib_uint64_t	current_lsn;

		if (log_peek_lsn(&current_lsn)
Sergei Golubchik's avatar
Sergei Golubchik committed
589 590 591
		    && UNIV_UNLIKELY
		    (current_lsn
		     < mach_read_from_8(read_buf + FIL_PAGE_LSN))) {
592 593 594 595 596 597 598 599 600 601 602
			ut_print_timestamp(stderr);

			fprintf(stderr,
				"  InnoDB: Error: page %lu log sequence number"
				" %llu\n"
				"InnoDB: is in the future! Current system "
				"log sequence number %llu.\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"
603
				"InnoDB: " REFMAN "forcing-innodb-recovery.html\n"
604 605 606
				"InnoDB: for more information.\n",
				(ulong) mach_read_from_4(read_buf
							 + FIL_PAGE_OFFSET),
Sergei Golubchik's avatar
Sergei Golubchik committed
607
				mach_read_from_8(read_buf + FIL_PAGE_LSN),
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
				current_lsn);
		}
	}
#endif

	/* 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 (UNIV_LIKELY(srv_use_checksums)) {
		checksum_field = mach_read_from_4(read_buf
						  + FIL_PAGE_SPACE_OR_CHKSUM);

		if (UNIV_UNLIKELY(zip_size)) {
			return(checksum_field != BUF_NO_CHECKSUM_MAGIC
			       && checksum_field
			       != page_zip_calc_checksum(read_buf, zip_size));
		}

		old_checksum_field = mach_read_from_4(
			read_buf + UNIV_PAGE_SIZE
			- FIL_PAGE_END_LSN_OLD_CHKSUM);

		/* 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
							   + FIL_PAGE_LSN)
		    && old_checksum_field != BUF_NO_CHECKSUM_MAGIC
		    && old_checksum_field
		    != buf_calc_page_old_checksum(read_buf)) {

			return(TRUE);
		}

		/* InnoDB versions < 4.0.14 and < 4.1.1 stored the space id
Vadim Tkachenko's avatar
Vadim Tkachenko committed
650
		(always equal to 0), to FIL_PAGE_SPACE_OR_CHKSUM */
651

652 653
		if (!srv_fast_checksum
		    && checksum_field != 0
654 655 656 657 658 659
		    && checksum_field != BUF_NO_CHECKSUM_MAGIC
		    && checksum_field
		    != buf_calc_page_new_checksum(read_buf)) {

			return(TRUE);
		}
660 661 662 663 664 665 666 667 668 669 670

		if (srv_fast_checksum
		    && checksum_field != 0
		    && checksum_field != BUF_NO_CHECKSUM_MAGIC
		    && checksum_field
		    != buf_calc_page_new_checksum_32(read_buf)
		    && checksum_field
		    != buf_calc_page_new_checksum(read_buf)) {

			return(TRUE);
		}
671 672 673 674 675
	}

	return(FALSE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
676
/********************************************************************//**
677 678 679 680 681
Prints a page to stderr. */
UNIV_INTERN
void
buf_page_print(
/*===========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
682 683
	const byte*	read_buf,	/*!< in: a database page */
	ulint		zip_size)	/*!< in: compressed page size, or
684 685
				0 for uncompressed pages */
{
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
686
#ifndef UNIV_HOTBACKUP
687
	dict_index_t*	index;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
688
#endif /* !UNIV_HOTBACKUP */
689
	ulint		checksum;
690
	ulint		checksum_32;
691 692 693 694 695 696 697 698 699 700 701
	ulint		old_checksum;
	ulint		size	= zip_size;

	if (!size) {
		size = UNIV_PAGE_SIZE;
	}

	ut_print_timestamp(stderr);
	fprintf(stderr, "  InnoDB: Page dump in ascii and hex (%lu bytes):\n",
		(ulong) size);
	ut_print_buf(stderr, read_buf, size);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
702
	fputs("\nInnoDB: End of page dump\n", stderr);
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

	if (zip_size) {
		/* Print compressed page. */

		switch (fil_page_get_type(read_buf)) {
		case FIL_PAGE_TYPE_ZBLOB:
		case FIL_PAGE_TYPE_ZBLOB2:
			checksum = srv_use_checksums
				? page_zip_calc_checksum(read_buf, zip_size)
				: BUF_NO_CHECKSUM_MAGIC;
			ut_print_timestamp(stderr);
			fprintf(stderr,
				"  InnoDB: Compressed BLOB page"
				" checksum %lu, stored %lu\n"
				"InnoDB: Page lsn %lu %lu\n"
				"InnoDB: Page number (if stored"
				" to page already) %lu,\n"
				"InnoDB: space id (if stored"
				" to page already) %lu\n",
				(ulong) checksum,
				(ulong) mach_read_from_4(
					read_buf + FIL_PAGE_SPACE_OR_CHKSUM),
				(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 + FIL_PAGE_OFFSET),
				(ulong) mach_read_from_4(
					read_buf
					+ FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID));
			return;
		default:
			ut_print_timestamp(stderr);
			fprintf(stderr,
				"  InnoDB: unknown page type %lu,"
				" assuming FIL_PAGE_INDEX\n",
				fil_page_get_type(read_buf));
			/* fall through */
		case FIL_PAGE_INDEX:
			checksum = srv_use_checksums
				? page_zip_calc_checksum(read_buf, zip_size)
				: BUF_NO_CHECKSUM_MAGIC;

			ut_print_timestamp(stderr);
			fprintf(stderr,
				"  InnoDB: Compressed page checksum %lu,"
				" stored %lu\n"
				"InnoDB: Page lsn %lu %lu\n"
				"InnoDB: Page number (if stored"
				" to page already) %lu,\n"
				"InnoDB: space id (if stored"
				" to page already) %lu\n",
				(ulong) checksum,
				(ulong) mach_read_from_4(
					read_buf + FIL_PAGE_SPACE_OR_CHKSUM),
				(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 + FIL_PAGE_OFFSET),
				(ulong) mach_read_from_4(
					read_buf
					+ FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID));
			return;
		case FIL_PAGE_TYPE_XDES:
			/* This is an uncompressed page. */
			break;
		}
	}

	checksum = srv_use_checksums
		? buf_calc_page_new_checksum(read_buf) : BUF_NO_CHECKSUM_MAGIC;
777 778
	checksum_32 = srv_use_checksums
		? buf_calc_page_new_checksum_32(read_buf) : BUF_NO_CHECKSUM_MAGIC;
779 780 781 782 783
	old_checksum = srv_use_checksums
		? buf_calc_page_old_checksum(read_buf) : BUF_NO_CHECKSUM_MAGIC;

	ut_print_timestamp(stderr);
	fprintf(stderr,
784
		"  InnoDB: Page checksum %lu (32bit_calc: %lu), prior-to-4.0.14-form"
785 786 787 788 789 790 791 792
		" 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",
793
		(ulong) checksum, (ulong) checksum_32, (ulong) old_checksum,
794 795 796 797 798 799 800 801 802 803 804
		(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),
		(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
					 - FIL_PAGE_END_LSN_OLD_CHKSUM + 4),
		(ulong) mach_read_from_4(read_buf + FIL_PAGE_OFFSET),
		(ulong) mach_read_from_4(read_buf
					 + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID));

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
805
#ifndef UNIV_HOTBACKUP
806 807 808 809 810 811 812 813 814 815
	if (mach_read_from_2(read_buf + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_TYPE)
	    == TRX_UNDO_INSERT) {
		fprintf(stderr,
			"InnoDB: Page may be an insert undo log page\n");
	} else if (mach_read_from_2(read_buf + TRX_UNDO_PAGE_HDR
				    + TRX_UNDO_PAGE_TYPE)
		   == TRX_UNDO_UPDATE) {
		fprintf(stderr,
			"InnoDB: Page may be an update undo log page\n");
	}
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
816
#endif /* !UNIV_HOTBACKUP */
817 818

	switch (fil_page_get_type(read_buf)) {
Sergei Golubchik's avatar
Sergei Golubchik committed
819
		index_id_t	index_id;
820
	case FIL_PAGE_INDEX:
Sergei Golubchik's avatar
Sergei Golubchik committed
821
		index_id = btr_page_get_index_id(read_buf);
822 823
		fprintf(stderr,
			"InnoDB: Page may be an index page where"
Sergei Golubchik's avatar
Sergei Golubchik committed
824 825
			" index id is %llu\n",
			(ullint) index_id);
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
826
#ifndef UNIV_HOTBACKUP
Sergei Golubchik's avatar
Sergei Golubchik committed
827
		index = dict_index_find_on_id_low(index_id);
828 829 830 831 832
		if (index) {
			fputs("InnoDB: (", stderr);
			dict_index_name_print(stderr, NULL, index);
			fputs(")\n", stderr);
		}
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
833
#endif /* !UNIV_HOTBACKUP */
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
		break;
	case FIL_PAGE_INODE:
		fputs("InnoDB: Page may be an 'inode' page\n", stderr);
		break;
	case FIL_PAGE_IBUF_FREE_LIST:
		fputs("InnoDB: Page may be an insert buffer free list page\n",
		      stderr);
		break;
	case FIL_PAGE_TYPE_ALLOCATED:
		fputs("InnoDB: Page may be a freshly allocated page\n",
		      stderr);
		break;
	case FIL_PAGE_IBUF_BITMAP:
		fputs("InnoDB: Page may be an insert buffer bitmap page\n",
		      stderr);
		break;
	case FIL_PAGE_TYPE_SYS:
		fputs("InnoDB: Page may be a system page\n",
		      stderr);
		break;
	case FIL_PAGE_TYPE_TRX_SYS:
		fputs("InnoDB: Page may be a transaction system page\n",
		      stderr);
		break;
	case FIL_PAGE_TYPE_FSP_HDR:
		fputs("InnoDB: Page may be a file space header page\n",
		      stderr);
		break;
	case FIL_PAGE_TYPE_XDES:
		fputs("InnoDB: Page may be an extent descriptor page\n",
		      stderr);
		break;
	case FIL_PAGE_TYPE_BLOB:
		fputs("InnoDB: Page may be a BLOB page\n",
		      stderr);
		break;
	case FIL_PAGE_TYPE_ZBLOB:
	case FIL_PAGE_TYPE_ZBLOB2:
		fputs("InnoDB: Page may be a compressed BLOB page\n",
		      stderr);
		break;
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
878
#ifndef UNIV_HOTBACKUP
Sergei Golubchik's avatar
Sergei Golubchik committed
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925

# ifdef PFS_GROUP_BUFFER_SYNC
/********************************************************************//**
This function registers mutexes and rwlocks in buffer blocks with
performance schema. If PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER is
defined to be a value less than chunk->size, then only mutexes
and rwlocks in the first PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER
blocks are registered. */
static
void
pfs_register_buffer_block(
/*======================*/
	buf_chunk_t*	chunk)		/*!< in/out: chunk of buffers */
{
	ulint		i;
	ulint		num_to_register;
	buf_block_t*    block;

	block = chunk->blocks;

	num_to_register = ut_min(chunk->size,
				 PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER);

	for (i = 0; i < num_to_register; i++) {
		mutex_t*	mutex;
		rw_lock_t*	rwlock;

#  ifdef UNIV_PFS_MUTEX
		mutex = &block->mutex;
		ut_a(!mutex->pfs_psi);
		mutex->pfs_psi = (PSI_server)
			? PSI_server->init_mutex(buffer_block_mutex_key, mutex)
			: NULL;
#  endif /* UNIV_PFS_MUTEX */

#  ifdef UNIV_PFS_RWLOCK
		rwlock = &block->lock;
		ut_a(!rwlock->pfs_psi);
		rwlock->pfs_psi = (PSI_server)
			? PSI_server->init_rwlock(buf_block_lock_key, rwlock)
			: NULL;
#  endif /* UNIV_PFS_RWLOCK */
		block++;
	}
}
# endif /* PFS_GROUP_BUFFER_SYNC */

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
926
/********************************************************************//**
927 928 929 930 931
Initializes a buffer control block when the buf_pool is created. */
static
void
buf_block_init(
/*===========*/
Sergei Golubchik's avatar
Sergei Golubchik committed
932 933 934
	buf_pool_t*	buf_pool,	/*!< in: buffer pool instance */
	buf_block_t*	block,		/*!< in: pointer to control block */
	byte*		frame)		/*!< in: pointer to buffer frame */
935 936 937 938 939
{
	UNIV_MEM_DESC(frame, UNIV_PAGE_SIZE, block);

	block->frame = frame;

Sergei Golubchik's avatar
Sergei Golubchik committed
940
	block->page.buf_pool_index = buf_pool_index(buf_pool);
941 942 943 944 945 946
	block->page.state = BUF_BLOCK_NOT_USED;
	block->page.buf_fix_count = 0;
	block->page.io_fix = BUF_IO_NONE;

	block->modify_clock = 0;

947
#if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
948
	block->page.file_page_was_freed = FALSE;
949
#endif /* UNIV_DEBUG_FILE_ACCESSES || UNIV_DEBUG */
950 951 952

	block->check_index_page_at_flush = FALSE;
	block->index = NULL;
Sergei Golubchik's avatar
Sergei Golubchik committed
953 954 955
	block->btr_search_latch = NULL;

	block->is_hashed = FALSE;
956 957 958 959 960 961

#ifdef UNIV_DEBUG
	block->page.in_page_hash = FALSE;
	block->page.in_zip_hash = FALSE;
	block->page.in_flush_list = FALSE;
	block->page.in_free_list = FALSE;
962
#endif /* UNIV_DEBUG */
Sergei Golubchik's avatar
Sergei Golubchik committed
963 964 965 966
	block->page.flush_list.prev = NULL;
	block->page.flush_list.next = NULL;
	block->page.zip_list.prev = NULL;
	block->page.zip_list.next = NULL;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
967
	block->page.in_LRU_list = FALSE;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
968
	block->in_unzip_LRU_list = FALSE;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
969 970 971
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
	block->n_pointers = 0;
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
972 973
	page_zip_des_init(&block->page.zip);

Sergei Golubchik's avatar
Sergei Golubchik committed
974 975 976 977 978 979
#if defined PFS_SKIP_BUFFER_MUTEX_RWLOCK || defined PFS_GROUP_BUFFER_SYNC
	/* If PFS_SKIP_BUFFER_MUTEX_RWLOCK is defined, skip registration
	of buffer block mutex/rwlock with performance schema. If
	PFS_GROUP_BUFFER_SYNC is defined, skip the registration
	since buffer block mutex/rwlock will be registered later in
	pfs_register_buffer_block() */
980

Sergei Golubchik's avatar
Sergei Golubchik committed
981 982 983 984 985 986
	mutex_create(PFS_NOT_INSTRUMENTED, &block->mutex, SYNC_BUF_BLOCK);
	rw_lock_create(PFS_NOT_INSTRUMENTED, &block->lock, SYNC_LEVEL_VARYING);
#else /* PFS_SKIP_BUFFER_MUTEX_RWLOCK || PFS_GROUP_BUFFER_SYNC */
	mutex_create(buffer_block_mutex_key, &block->mutex, SYNC_BUF_BLOCK);
	rw_lock_create(buf_block_lock_key, &block->lock, SYNC_LEVEL_VARYING);
#endif /* PFS_SKIP_BUFFER_MUTEX_RWLOCK || PFS_GROUP_BUFFER_SYNC */
987 988 989 990

	ut_ad(rw_lock_validate(&(block->lock)));

#ifdef UNIV_SYNC_DEBUG
Sergei Golubchik's avatar
Sergei Golubchik committed
991 992
	rw_lock_create(buf_block_debug_latch_key,
		       &block->debug_latch, SYNC_NO_ORDER_CHECK);
993 994 995
#endif /* UNIV_SYNC_DEBUG */
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
996 997 998
/********************************************************************//**
Allocates a chunk of buffer frames.
@return	chunk, or NULL on failure */
999 1000 1001 1002
static
buf_chunk_t*
buf_chunk_init(
/*===========*/
Sergei Golubchik's avatar
Sergei Golubchik committed
1003
	buf_pool_t*	buf_pool,	/*!< in: buffer pool instance */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1004 1005
	buf_chunk_t*	chunk,		/*!< out: chunk of buffers */
	ulint		mem_size)	/*!< in: requested size in bytes */
1006 1007 1008 1009
{
	buf_block_t*	block;
	byte*		frame;
	ulint		i;
1010
	ulint		size_target;
1011 1012 1013 1014

	/* Round down to a multiple of page size,
	although it already should be. */
	mem_size = ut_2pow_round(mem_size, UNIV_PAGE_SIZE);
1015
	size_target = (mem_size / UNIV_PAGE_SIZE) - 1;
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
	/* Reserve space for the block descriptors. */
	mem_size += ut_2pow_round((mem_size / UNIV_PAGE_SIZE) * (sizeof *block)
				  + (UNIV_PAGE_SIZE - 1), UNIV_PAGE_SIZE);

	chunk->mem_size = mem_size;
	chunk->mem = os_mem_alloc_large(&chunk->mem_size);

	if (UNIV_UNLIKELY(chunk->mem == NULL)) {

		return(NULL);
	}

	/* Allocate the block descriptors from
	the start of the memory block. */
	chunk->blocks = chunk->mem;

	/* Align a pointer to the first frame.  Note that when
	os_large_page_size is smaller than UNIV_PAGE_SIZE,
	we may allocate one fewer block than requested.  When
	it is bigger, we may allocate more blocks than requested. */

	frame = ut_align(chunk->mem, UNIV_PAGE_SIZE);
	chunk->size = chunk->mem_size / UNIV_PAGE_SIZE
		- (frame != chunk->mem);

	/* Subtract the space needed for block descriptors. */
	{
		ulint	size = chunk->size;

		while (frame < (byte*) (chunk->blocks + size)) {
			frame += UNIV_PAGE_SIZE;
			size--;
		}

		chunk->size = size;
	}

1053 1054 1055
	if (chunk->size > size_target) {
		chunk->size = size_target;
	}
1056

1057 1058 1059 1060 1061 1062 1063 1064
	/* Init block structs and assign frames for them. Then we
	assign the frames to the first blocks (we already mapped the
	memory above). */

	block = chunk->blocks;

	for (i = chunk->size; i--; ) {

Sergei Golubchik's avatar
Sergei Golubchik committed
1065
		buf_block_init(buf_pool, block, frame);
1066

1067
#ifdef HAVE_valgrind
1068 1069 1070 1071
		/* Wipe contents of frame to eliminate a Purify warning */
		memset(block->frame, '\0', UNIV_PAGE_SIZE);
#endif
		/* Add the block to the free list */
Sergei Golubchik's avatar
Sergei Golubchik committed
1072
		mutex_enter(&buf_pool->free_list_mutex);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1073
		UT_LIST_ADD_LAST(free, buf_pool->free, (&block->page));
Sergei Golubchik's avatar
Sergei Golubchik committed
1074

1075
		ut_d(block->page.in_free_list = TRUE);
Sergei Golubchik's avatar
Sergei Golubchik committed
1076 1077
		mutex_exit(&buf_pool->free_list_mutex);
		ut_ad(buf_pool_from_block(block) == buf_pool);
1078 1079 1080 1081 1082

		block++;
		frame += UNIV_PAGE_SIZE;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
1083 1084 1085
#ifdef PFS_GROUP_BUFFER_SYNC
	pfs_register_buffer_block(chunk);
#endif
1086 1087 1088 1089
	return(chunk);
}

#ifdef UNIV_DEBUG
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1090
/*********************************************************************//**
1091
Finds a block in the given buffer chunk that points to a
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1092 1093
given compressed page.
@return	buffer block pointing to the compressed page, or NULL */
1094 1095 1096 1097
static
buf_block_t*
buf_chunk_contains_zip(
/*===================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1098 1099
	buf_chunk_t*	chunk,	/*!< in: chunk being checked */
	const void*	data)	/*!< in: pointer to compressed page */
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
{
	buf_block_t*	block;
	ulint		i;

	block = chunk->blocks;

	for (i = chunk->size; i--; block++) {
		if (block->page.zip.data == data) {

			return(block);
		}
	}

	return(NULL);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1116
/*********************************************************************//**
1117
Finds a block in the buffer pool that points to a
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1118 1119
given compressed page.
@return	buffer block pointing to the compressed page, or NULL */
1120 1121 1122 1123
UNIV_INTERN
buf_block_t*
buf_pool_contains_zip(
/*==================*/
Sergei Golubchik's avatar
Sergei Golubchik committed
1124 1125
	buf_pool_t*	buf_pool,	/*!< in: buffer pool instance */
	const void*	data)		/*!< in: pointer to compressed page */
1126 1127 1128 1129
{
	ulint		n;
	buf_chunk_t*	chunk = buf_pool->chunks;

Sergei Golubchik's avatar
Sergei Golubchik committed
1130 1131 1132
	ut_ad(buf_pool);
	//ut_ad(buf_pool_mutex_own(buf_pool));
	ut_ad(mutex_own(&buf_pool->zip_free_mutex));
1133
	for (n = buf_pool->n_chunks; n--; chunk++) {
Sergei Golubchik's avatar
Sergei Golubchik committed
1134

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
		buf_block_t* block = buf_chunk_contains_zip(chunk, data);

		if (block) {
			return(block);
		}
	}

	return(NULL);
}
#endif /* UNIV_DEBUG */

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1146 1147 1148
/*********************************************************************//**
Checks that all file pages in the buffer chunk are in a replaceable state.
@return	address of a non-free block, or NULL if all freed */
1149 1150 1151 1152
static
const buf_block_t*
buf_chunk_not_freed(
/*================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1153
	buf_chunk_t*	chunk)	/*!< in: chunk being checked */
1154 1155 1156 1157 1158 1159 1160
{
	buf_block_t*	block;
	ulint		i;

	block = chunk->blocks;

	for (i = chunk->size; i--; block++) {
1161
		ibool	ready;
1162

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
		switch (buf_block_get_state(block)) {
		case BUF_BLOCK_ZIP_FREE:
		case BUF_BLOCK_ZIP_PAGE:
		case BUF_BLOCK_ZIP_DIRTY:
			/* The uncompressed buffer pool should never
			contain compressed block descriptors. */
			ut_error;
			break;
		case BUF_BLOCK_NOT_USED:
		case BUF_BLOCK_READY_FOR_USE:
		case BUF_BLOCK_MEMORY:
		case BUF_BLOCK_REMOVE_HASH:
			/* Skip blocks that are not being used for
			file pages. */
			break;
		case BUF_BLOCK_FILE_PAGE:
			mutex_enter(&block->mutex);
			ready = buf_flush_ready_for_replace(&block->page);
1181 1182
			mutex_exit(&block->mutex);

1183 1184 1185 1186 1187
			if (block->page.is_corrupt) {
				/* corrupt page may remain, it can be skipped */
				break;
			}

1188 1189 1190 1191 1192 1193 1194
			if (!ready) {

				return(block);
			}

			break;
		}
1195 1196 1197 1198 1199
	}

	return(NULL);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1200
/********************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
1201 1202 1203 1204 1205
Set buffer pool size variables after resizing it */
static
void
buf_pool_set_sizes(void)
/*====================*/
1206
{
Sergei Golubchik's avatar
Sergei Golubchik committed
1207 1208
	ulint	i;
	ulint	curr_size = 0;
1209

Sergei Golubchik's avatar
Sergei Golubchik committed
1210
	buf_pool_mutex_enter_all();
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1211

Sergei Golubchik's avatar
Sergei Golubchik committed
1212 1213
	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_t*	buf_pool;
1214

Sergei Golubchik's avatar
Sergei Golubchik committed
1215 1216 1217
		buf_pool = buf_pool_from_array(i);
		curr_size += buf_pool->curr_pool_size;
	}
1218

Sergei Golubchik's avatar
Sergei Golubchik committed
1219 1220
	srv_buf_pool_curr_size = curr_size;
	srv_buf_pool_old_size = srv_buf_pool_size;
1221

Sergei Golubchik's avatar
Sergei Golubchik committed
1222 1223
	buf_pool_mutex_exit_all();
}
1224

Sergei Golubchik's avatar
Sergei Golubchik committed
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
/********************************************************************//**
Initialize a buffer pool instance.
@return DB_SUCCESS if all goes well. */
UNIV_INTERN
ulint
buf_pool_init_instance(
/*===================*/
	buf_pool_t*	buf_pool,	/*!< in: buffer pool instance */
	ulint		buf_pool_size,	/*!< in: size in bytes */
	ulint		instance_no)	/*!< in: id of the instance */
{
	ulint		i;
	buf_chunk_t*	chunk;
1238

Sergei Golubchik's avatar
Sergei Golubchik committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
	/* 1. Initialize general fields
	------------------------------- */
	mutex_create(buf_pool_mutex_key,
		     &buf_pool->mutex, SYNC_BUF_POOL);
	mutex_create(buf_pool_LRU_list_mutex_key,
		     &buf_pool->LRU_list_mutex, SYNC_BUF_LRU_LIST);
	rw_lock_create(buf_pool_page_hash_key,
		       &buf_pool->page_hash_latch, SYNC_BUF_PAGE_HASH);
	mutex_create(buf_pool_free_list_mutex_key,
		     &buf_pool->free_list_mutex, SYNC_BUF_FREE_LIST);
	mutex_create(buf_pool_zip_free_mutex_key,
		     &buf_pool->zip_free_mutex, SYNC_BUF_ZIP_FREE);
	mutex_create(buf_pool_zip_hash_mutex_key,
		     &buf_pool->zip_hash_mutex, SYNC_BUF_ZIP_HASH);
	mutex_create(buf_pool_zip_mutex_key,
		     &buf_pool->zip_mutex, SYNC_BUF_BLOCK);

	mutex_enter(&buf_pool->LRU_list_mutex);
	rw_lock_x_lock(&buf_pool->page_hash_latch);
	buf_pool_mutex_enter(buf_pool);

	if (buf_pool_size > 0) {
		buf_pool->n_chunks = 1;
		buf_pool->chunks = chunk = mem_zalloc(sizeof *chunk);

		UT_LIST_INIT(buf_pool->free);

		if (!buf_chunk_init(buf_pool, chunk, buf_pool_size)) {
			mem_free(chunk);
			mem_free(buf_pool);

			mutex_exit(&buf_pool->LRU_list_mutex);
			rw_lock_x_unlock(&buf_pool->page_hash_latch);
			buf_pool_mutex_exit(buf_pool);

			return(DB_ERROR);
		}
1276

Sergei Golubchik's avatar
Sergei Golubchik committed
1277 1278 1279 1280
		buf_pool->instance_no = instance_no;
		buf_pool->old_pool_size = buf_pool_size;
		buf_pool->curr_size = chunk->size;
		buf_pool->curr_pool_size = buf_pool->curr_size * UNIV_PAGE_SIZE;
1281

Sergei Golubchik's avatar
Sergei Golubchik committed
1282 1283
		buf_pool->page_hash = hash_create(2 * buf_pool->curr_size);
		buf_pool->zip_hash = hash_create(2 * buf_pool->curr_size);
1284

Sergei Golubchik's avatar
Sergei Golubchik committed
1285 1286
		buf_pool->last_printout_time = ut_time();
	}
1287 1288 1289
	/* 2. Initialize flushing fields
	-------------------------------- */

Sergei Golubchik's avatar
Sergei Golubchik committed
1290 1291 1292
	mutex_create(flush_list_mutex_key, &buf_pool->flush_list_mutex,
		     SYNC_BUF_FLUSH_LIST);

1293 1294 1295 1296 1297 1298
	for (i = BUF_FLUSH_LRU; i < BUF_FLUSH_N_TYPES; i++) {
		buf_pool->no_flush[i] = os_event_create(NULL);
	}

	/* 3. Initialize LRU fields
	--------------------------- */
1299

1300 1301
	/* All fields are initialized by mem_zalloc(). */

Sergei Golubchik's avatar
Sergei Golubchik committed
1302 1303 1304 1305 1306
	mutex_exit(&buf_pool->LRU_list_mutex);
	rw_lock_x_unlock(&buf_pool->page_hash_latch);
	buf_pool_mutex_exit(buf_pool);

	return(DB_SUCCESS);
1307 1308
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1309
/********************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
1310 1311
free one buffer pool instance */
static
1312
void
Sergei Golubchik's avatar
Sergei Golubchik committed
1313 1314 1315 1316
buf_pool_free_instance(
/*===================*/
	buf_pool_t*	buf_pool)	/* in,own: buffer pool instance
					to free */
1317 1318 1319 1320
{
	buf_chunk_t*	chunk;
	buf_chunk_t*	chunks;

Sergei Golubchik's avatar
Sergei Golubchik committed
1321 1322
	chunks = buf_pool->chunks;
	chunk = chunks + buf_pool->n_chunks;
1323 1324 1325 1326 1327

	while (--chunk >= chunks) {
		os_mem_free_large(chunk->mem, chunk->mem_size);
	}

1328 1329 1330
	mem_free(buf_pool->chunks);
	hash_table_free(buf_pool->page_hash);
	hash_table_free(buf_pool->zip_hash);
Sergei Golubchik's avatar
Sergei Golubchik committed
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
}

/********************************************************************//**
Creates the buffer pool.
@return	DB_SUCCESS if success, DB_ERROR if not enough memory or error */
UNIV_INTERN
ulint
buf_pool_init(
/*==========*/
	ulint	total_size,	/*!< in: size of the total pool in bytes */
	ulint	n_instances)	/*!< in: number of instances */
{
	ulint		i;
	const ulint	size	= total_size / n_instances;

	ut_ad(n_instances > 0);
	ut_ad(n_instances <= MAX_BUFFER_POOLS);
	ut_ad(n_instances == srv_buf_pool_instances);

	/* We create an extra buffer pool instance, this instance is used
	for flushing the flush lists, to keep track of n_flush for all
	the buffer pools and also used as a waiting object during flushing. */
	buf_pool_ptr = mem_zalloc(n_instances * sizeof *buf_pool_ptr);

	for (i = 0; i < n_instances; i++) {
		buf_pool_t*	ptr	= &buf_pool_ptr[i];

		if (buf_pool_init_instance(ptr, size, i) != DB_SUCCESS) {

			/* Free all the instances created so far. */
			buf_pool_free(i);

			return(DB_ERROR);
		}
	}

	buf_pool_set_sizes();
	buf_LRU_old_ratio_update(100 * 3/ 8, FALSE);

	btr_search_sys_create(buf_pool_get_curr_size() / sizeof(void*) / 64);

	return(DB_SUCCESS);
}

/********************************************************************//**
Frees the buffer pool at shutdown.  This must not be invoked before
freeing all mutexes. */
UNIV_INTERN
void
buf_pool_free(
/*==========*/
	ulint	n_instances)	/*!< in: numbere of instances to free */
{
	ulint	i;

	for (i = 0; i < n_instances; i++) {
		buf_pool_free_instance(buf_pool_from_array(i));
	}

	mem_free(buf_pool_ptr);
	buf_pool_ptr = NULL;
}

/********************************************************************//**
Drops adaptive hash index for a buffer pool instance. */
static
void
buf_pool_drop_hash_index_instance(
/*==============================*/
	buf_pool_t*	buf_pool,		/*!< in: buffer pool instance */
	ibool*		released_search_latch)	/*!< out: flag for signalling
						whether the search latch was
						released */
{
	buf_chunk_t*	chunks	= buf_pool->chunks;
	buf_chunk_t*	chunk	= chunks + buf_pool->n_chunks;

	while (--chunk >= chunks) {
		ulint		i;
		buf_block_t*	block	= chunk->blocks;

		for (i = chunk->size; i--; block++) {
			/* block->is_hashed cannot be modified
			when we have an x-latch on btr_search_latch;
			see the comment in buf0buf.h */

			if (!block->is_hashed) {
				continue;
			}

			/* To follow the latching order, we
			have to release btr_search_latch
			before acquiring block->latch. */
			btr_search_x_unlock_all();
			/* When we release the search latch,
			we must rescan all blocks, because
			some may become hashed again. */
			*released_search_latch = TRUE;

			rw_lock_x_lock(&block->lock);

			/* This should be guaranteed by the
			callers, which will be holding
			btr_search_enabled_mutex. */
			ut_ad(!btr_search_enabled);

			/* Because we did not buffer-fix the
			block by calling buf_block_get_gen(),
			it is possible that the block has been
			allocated for some other use after
			btr_search_latch was released above.
			We do not care which file page the
			block is mapped to.  All we want to do
			is to drop any hash entries referring
			to the page. */

			/* It is possible that
			block->page.state != BUF_FILE_PAGE.
			Even that does not matter, because
			btr_search_drop_page_hash_index() will
			check block->is_hashed before doing
			anything.  block->is_hashed can only
			be set on uncompressed file pages. */

			btr_search_drop_page_hash_index(block, NULL);

			rw_lock_x_unlock(&block->lock);

			btr_search_x_lock_all();

			ut_ad(!btr_search_enabled);
		}
1463
	}
1464 1465
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1466
/********************************************************************//**
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
Drops the adaptive hash index.  To prevent a livelock, this function
is only to be called while holding btr_search_latch and while
btr_search_enabled == FALSE. */
UNIV_INTERN
void
buf_pool_drop_hash_index(void)
/*==========================*/
{
	ibool		released_search_latch;

#ifdef UNIV_SYNC_DEBUG
Sergei Golubchik's avatar
Sergei Golubchik committed
1478 1479 1480 1481 1482
	ulint	j;

	for (j = 0; j < btr_search_index_num; j++) {
		ut_ad(rw_lock_own(btr_search_latch_part[j], RW_LOCK_EX));
	}
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1483 1484 1485 1486
#endif /* UNIV_SYNC_DEBUG */
	ut_ad(!btr_search_enabled);

	do {
Sergei Golubchik's avatar
Sergei Golubchik committed
1487
 		ulint	i;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1488 1489 1490

		released_search_latch = FALSE;

Sergei Golubchik's avatar
Sergei Golubchik committed
1491 1492
		for (i = 0; i < srv_buf_pool_instances; i++) {
 			buf_pool_t*	buf_pool;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1493

Sergei Golubchik's avatar
Sergei Golubchik committed
1494
			buf_pool = buf_pool_from_array(i);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1495

Sergei Golubchik's avatar
Sergei Golubchik committed
1496 1497
			buf_pool_drop_hash_index_instance(
				buf_pool, &released_search_latch);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1498
		}
Sergei Golubchik's avatar
Sergei Golubchik committed
1499

Vadim Tkachenko's avatar
Vadim Tkachenko committed
1500 1501 1502
	} while (released_search_latch);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1503
/********************************************************************//**
1504 1505 1506 1507 1508 1509 1510
Relocate a buffer control block.  Relocates the block on the LRU list
and in buf_pool->page_hash.  Does not relocate bpage->list.
The caller must take care of relocating bpage->list. */
UNIV_INTERN
void
buf_relocate(
/*=========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1511
	buf_page_t*	bpage,	/*!< in/out: control block being relocated;
1512 1513
				buf_page_get_state(bpage) must be
				BUF_BLOCK_ZIP_DIRTY or BUF_BLOCK_ZIP_PAGE */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1514
	buf_page_t*	dpage)	/*!< in/out: destination control block */
1515 1516 1517
{
	buf_page_t*	b;
	ulint		fold;
Sergei Golubchik's avatar
Sergei Golubchik committed
1518
	buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);
1519

Sergei Golubchik's avatar
Sergei Golubchik committed
1520 1521
	//ut_ad(buf_pool_mutex_own(buf_pool));
	ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1522
#ifdef UNIV_SYNC_DEBUG
Sergei Golubchik's avatar
Sergei Golubchik committed
1523
	ut_ad(rw_lock_own(&buf_pool->page_hash_latch, RW_LOCK_EX));
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1524
#endif
1525 1526 1527 1528 1529 1530
	ut_ad(mutex_own(buf_page_get_mutex(bpage)));
	ut_a(buf_page_get_io_fix(bpage) == BUF_IO_NONE);
	ut_a(bpage->buf_fix_count == 0);
	ut_ad(bpage->in_LRU_list);
	ut_ad(!bpage->in_zip_hash);
	ut_ad(bpage->in_page_hash);
Sergei Golubchik's avatar
Sergei Golubchik committed
1531 1532 1533
	ut_ad(bpage == buf_page_hash_get(buf_pool,
			       		 bpage->space, bpage->offset));
	ut_ad(!buf_pool_watch_is_sentinel(buf_pool, bpage));
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
#ifdef UNIV_DEBUG
	switch (buf_page_get_state(bpage)) {
	case BUF_BLOCK_ZIP_FREE:
	case BUF_BLOCK_NOT_USED:
	case BUF_BLOCK_READY_FOR_USE:
	case BUF_BLOCK_FILE_PAGE:
	case BUF_BLOCK_MEMORY:
	case BUF_BLOCK_REMOVE_HASH:
		ut_error;
	case BUF_BLOCK_ZIP_DIRTY:
	case BUF_BLOCK_ZIP_PAGE:
		break;
	}
#endif /* UNIV_DEBUG */

	memcpy(dpage, bpage, sizeof *dpage);

Vadim Tkachenko's avatar
Vadim Tkachenko committed
1551
	bpage->in_LRU_list = FALSE;
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
	ut_d(bpage->in_page_hash = FALSE);

	/* relocate buf_pool->LRU */
	b = UT_LIST_GET_PREV(LRU, bpage);
	UT_LIST_REMOVE(LRU, buf_pool->LRU, bpage);

	if (b) {
		UT_LIST_INSERT_AFTER(LRU, buf_pool->LRU, b, dpage);
	} else {
		UT_LIST_ADD_FIRST(LRU, buf_pool->LRU, dpage);
	}

	if (UNIV_UNLIKELY(buf_pool->LRU_old == bpage)) {
		buf_pool->LRU_old = dpage;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1566 1567 1568
#ifdef UNIV_LRU_DEBUG
		/* buf_pool->LRU_old must be the first item in the LRU list
		whose "old" flag is set. */
1569
		ut_a(buf_pool->LRU_old->old);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1570 1571 1572 1573
		ut_a(!UT_LIST_GET_PREV(LRU, buf_pool->LRU_old)
		     || !UT_LIST_GET_PREV(LRU, buf_pool->LRU_old)->old);
		ut_a(!UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old)
		     || UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old)->old);
1574 1575 1576 1577
	} else {
		/* Check that the "old" flag is consistent in
		the block and its neighbours. */
		buf_page_set_old(dpage, buf_page_is_old(dpage));
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1578
#endif /* UNIV_LRU_DEBUG */
1579 1580
	}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1581 1582
	ut_d(UT_LIST_VALIDATE(LRU, buf_page_t, buf_pool->LRU,
			      ut_ad(ut_list_node_313->in_LRU_list)));
1583 1584 1585 1586 1587 1588 1589 1590

	/* relocate buf_pool->page_hash */
	fold = buf_page_address_fold(bpage->space, bpage->offset);

	HASH_DELETE(buf_page_t, hash, buf_pool->page_hash, fold, bpage);
	HASH_INSERT(buf_page_t, hash, buf_pool->page_hash, fold, dpage);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1591
/********************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
Determine if a block is a sentinel for a buffer pool watch.
@return	TRUE if a sentinel for a buffer pool watch, FALSE if not */
UNIV_INTERN
ibool
buf_pool_watch_is_sentinel(
/*=======================*/
	buf_pool_t*		buf_pool,	/*!< buffer pool instance */
	const buf_page_t*	bpage)		/*!< in: block */
{
	ut_ad(buf_page_in_file(bpage));

	if (bpage < &buf_pool->watch[0]
	    || bpage >= &buf_pool->watch[BUF_POOL_WATCH_SIZE]) {

		ut_ad(buf_page_get_state(bpage) != BUF_BLOCK_ZIP_PAGE
		      || bpage->zip.data != NULL);

		return(FALSE);
	}

	ut_ad(buf_page_get_state(bpage) == BUF_BLOCK_ZIP_PAGE);
	ut_ad(!bpage->in_zip_hash);
	ut_ad(bpage->in_page_hash);
	ut_ad(bpage->zip.data == NULL);
	ut_ad(bpage->buf_fix_count > 0);
	return(TRUE);
}

/****************************************************************//**
Add watch for the given page to be read in. Caller must have the buffer pool
mutex reserved.
@return NULL if watch set, block if the page is in the buffer pool */
UNIV_INTERN
buf_page_t*
buf_pool_watch_set(
/*===============*/
	ulint	space,	/*!< in: space id */
	ulint	offset,	/*!< in: page number */
	ulint	fold)	/*!< in: buf_page_address_fold(space, offset) */
{
	buf_page_t*	bpage;
	ulint		i;
	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
	mutex_t*	block_mutex;

	//ut_ad(buf_pool_mutex_own(buf_pool));

	rw_lock_x_lock(&buf_pool->page_hash_latch);
	bpage = buf_page_hash_get_low(buf_pool, space, offset, fold);
	if (bpage) {
		block_mutex = buf_page_get_mutex_enter(bpage);
		ut_a(block_mutex);
	}

	if (UNIV_LIKELY_NULL(bpage)) {
		if (!buf_pool_watch_is_sentinel(buf_pool, bpage)) {
			/* The page was loaded meanwhile. */
			rw_lock_x_unlock(&buf_pool->page_hash_latch);
			return(bpage);
		}
		/* Add to an existing watch. */
		bpage->buf_fix_count++;
		rw_lock_x_unlock(&buf_pool->page_hash_latch);
		mutex_exit(block_mutex);
		return(NULL);
	}

	/* buf_pool->watch is protected by zip_mutex for now */
	mutex_enter(&buf_pool->zip_mutex);
	for (i = 0; i < BUF_POOL_WATCH_SIZE; i++) {
		bpage = &buf_pool->watch[i];

		ut_ad(bpage->access_time == 0);
		ut_ad(bpage->newest_modification == 0);
		ut_ad(bpage->oldest_modification == 0);
		ut_ad(bpage->zip.data == NULL);
		ut_ad(!bpage->in_zip_hash);

		switch (bpage->state) {
		case BUF_BLOCK_POOL_WATCH:
			ut_ad(!bpage->in_page_hash);
			ut_ad(bpage->buf_fix_count == 0);

			/* bpage is pointing to buf_pool->watch[],
			which is protected by buf_pool->mutex.
			Normally, buf_page_t objects are protected by
			buf_block_t::mutex or buf_pool->zip_mutex or both. */

			bpage->state = BUF_BLOCK_ZIP_PAGE;
			bpage->space = space;
			bpage->offset = offset;
			bpage->buf_fix_count = 1;
			bpage->buf_pool_index = buf_pool_index(buf_pool);
			ut_d(bpage->in_page_hash = TRUE);
			HASH_INSERT(buf_page_t, hash, buf_pool->page_hash,
				    fold, bpage);
			rw_lock_x_unlock(&buf_pool->page_hash_latch);
			mutex_exit(&buf_pool->zip_mutex);
			return(NULL);
		case BUF_BLOCK_ZIP_PAGE:
			ut_ad(bpage->in_page_hash);
			ut_ad(bpage->buf_fix_count > 0);
			break;
		default:
			ut_error;
		}
	}

	/* Allocation failed.  Either the maximum number of purge
	threads should never exceed BUF_POOL_WATCH_SIZE, or this code
	should be modified to return a special non-NULL value and the
	caller should purge the record directly. */
	ut_error;

	/* Fix compiler warning */
	rw_lock_x_unlock(&buf_pool->page_hash_latch);
	mutex_exit(&buf_pool->zip_mutex);
	return(NULL);
}

/****************************************************************//**
Remove the sentinel block for the watch before replacing it with a real block.
buf_page_watch_clear() or buf_page_watch_occurred() will notice that
the block has been replaced with the real block.
@return reference count, to be added to the replacement block */
static
void
buf_pool_watch_remove(
/*==================*/
	buf_pool_t*	buf_pool,	/*!< buffer pool instance */
	ulint		fold,		/*!< in: buf_page_address_fold(
					space, offset) */
	buf_page_t*	watch)		/*!< in/out: sentinel for watch */
{
	//ut_ad(buf_pool_mutex_own(buf_pool));
#ifdef UNIV_SYNC_DEBUG
	ut_ad(rw_lock_own(&buf_pool->page_hash_latch, RW_LOCK_EX));
#endif
	ut_ad(mutex_own(&buf_pool->zip_mutex)); /* for now */

	HASH_DELETE(buf_page_t, hash, buf_pool->page_hash, fold, watch);
	ut_d(watch->in_page_hash = FALSE);
	watch->buf_fix_count = 0;
	watch->state = BUF_BLOCK_POOL_WATCH;
}

/****************************************************************//**
Stop watching if the page has been read in.
buf_pool_watch_set(space,offset) must have returned NULL before. */
UNIV_INTERN
void
buf_pool_watch_unset(
/*=================*/
	ulint	space,	/*!< in: space id */
	ulint	offset)	/*!< in: page number */
{
	buf_page_t*	bpage;
	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
	ulint		fold = buf_page_address_fold(space, offset);

	//buf_pool_mutex_enter(buf_pool);
	rw_lock_x_lock(&buf_pool->page_hash_latch);
	bpage = buf_page_hash_get_low(buf_pool, space, offset, fold);
	/* The page must exist because buf_pool_watch_set()
	increments buf_fix_count. */
	ut_a(bpage);

	if (UNIV_UNLIKELY(!buf_pool_watch_is_sentinel(buf_pool, bpage))) {
		mutex_t* mutex = buf_page_get_mutex_enter(bpage);

		ut_a(bpage->buf_fix_count > 0);
		bpage->buf_fix_count--;
		mutex_exit(mutex);
	} else {
		mutex_enter(&buf_pool->zip_mutex);
		ut_a(bpage->buf_fix_count > 0);
1768

Sergei Golubchik's avatar
Sergei Golubchik committed
1769 1770 1771 1772
		if (UNIV_LIKELY(!--bpage->buf_fix_count)) {
			buf_pool_watch_remove(buf_pool, fold, bpage);
		}
		mutex_exit(&buf_pool->zip_mutex);
1773 1774
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
	//buf_pool_mutex_exit(buf_pool);
	rw_lock_x_unlock(&buf_pool->page_hash_latch);
}

/****************************************************************//**
Check if the page has been read in.
This may only be called after buf_pool_watch_set(space,offset)
has returned NULL and before invoking buf_pool_watch_unset(space,offset).
@return	FALSE if the given page was not read in, TRUE if it was */
UNIV_INTERN
ibool
buf_pool_watch_occurred(
/*====================*/
	ulint	space,	/*!< in: space id */
	ulint	offset)	/*!< in: page number */
{
	ibool		ret;
	buf_page_t*	bpage;
	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
	ulint		fold	= buf_page_address_fold(space, offset);

	//buf_pool_mutex_enter(buf_pool);
	rw_lock_s_lock(&buf_pool->page_hash_latch);

	bpage = buf_page_hash_get_low(buf_pool, space, offset, fold);
	/* The page must exist because buf_pool_watch_set()
	increments buf_fix_count. */
	ut_a(bpage);
	ret = !buf_pool_watch_is_sentinel(buf_pool, bpage);
	//buf_pool_mutex_exit(buf_pool);
	rw_lock_s_unlock(&buf_pool->page_hash_latch);

	return(ret);
1808 1809
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1810
/********************************************************************//**
1811
Moves a page to the start of the buffer pool LRU list. This high-level
1812
function can be used to prevent an important page from slipping out of
1813 1814 1815 1816 1817
the buffer pool. */
UNIV_INTERN
void
buf_page_make_young(
/*================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1818
	buf_page_t*	bpage)	/*!< in: buffer block of a file page */
1819
{
Sergei Golubchik's avatar
Sergei Golubchik committed
1820 1821 1822 1823
	buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);

	//buf_pool_mutex_enter(buf_pool);
	mutex_enter(&buf_pool->LRU_list_mutex);
1824 1825 1826 1827 1828

	ut_a(buf_page_in_file(bpage));

	buf_LRU_make_block_young(bpage);

Sergei Golubchik's avatar
Sergei Golubchik committed
1829 1830
	//buf_pool_mutex_exit(buf_pool);
	mutex_exit(&buf_pool->LRU_list_mutex);
1831 1832
}

1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
/********************************************************************//**
Sets the time of the first access of a page and moves a page to the
start of the buffer pool LRU list if it is too old.  This high-level
function can be used to prevent an important page from slipping
out of the buffer pool. */
static
void
buf_page_set_accessed_make_young(
/*=============================*/
	buf_page_t*	bpage,		/*!< in/out: buffer block of a
					file page */
	unsigned	access_time)	/*!< in: bpage->access_time
					read under mutex protection,
					or 0 if unknown */
{
Sergei Golubchik's avatar
Sergei Golubchik committed
1848 1849 1850
	buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);

	ut_ad(!buf_pool_mutex_own(buf_pool));
1851 1852 1853
	ut_a(buf_page_in_file(bpage));

	if (buf_page_peek_if_too_old(bpage)) {
Sergei Golubchik's avatar
Sergei Golubchik committed
1854 1855
		//buf_pool_mutex_enter(buf_pool);
		mutex_enter(&buf_pool->LRU_list_mutex);
1856
		buf_LRU_make_block_young(bpage);
Sergei Golubchik's avatar
Sergei Golubchik committed
1857 1858
		//buf_pool_mutex_exit(buf_pool);
		mutex_exit(&buf_pool->LRU_list_mutex);
1859 1860 1861
	} else if (!access_time) {
		ulint	time_ms = ut_time_ms();
		mutex_t*	block_mutex = buf_page_get_mutex_enter(bpage);
Sergei Golubchik's avatar
Sergei Golubchik committed
1862
		//buf_pool_mutex_enter(buf_pool);
1863 1864 1865 1866
		if (block_mutex) {
		buf_page_set_accessed(bpage, time_ms);
		mutex_exit(block_mutex);
		}
Sergei Golubchik's avatar
Sergei Golubchik committed
1867
		//buf_pool_mutex_exit(buf_pool);
1868 1869 1870
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1871
/********************************************************************//**
1872 1873 1874 1875 1876 1877
Resets the check_index_page_at_flush field of a page if found in the buffer
pool. */
UNIV_INTERN
void
buf_reset_check_index_page_at_flush(
/*================================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1878 1879
	ulint	space,	/*!< in: space id */
	ulint	offset)	/*!< in: page number */
1880 1881
{
	buf_block_t*	block;
Sergei Golubchik's avatar
Sergei Golubchik committed
1882
	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
1883

Sergei Golubchik's avatar
Sergei Golubchik committed
1884 1885
	//buf_pool_mutex_enter(buf_pool);
	rw_lock_s_lock(&buf_pool->page_hash_latch);
1886

Sergei Golubchik's avatar
Sergei Golubchik committed
1887
	block = (buf_block_t*) buf_page_hash_get(buf_pool, space, offset);
1888 1889

	if (block && buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE) {
Sergei Golubchik's avatar
Sergei Golubchik committed
1890
		ut_ad(!buf_pool_watch_is_sentinel(buf_pool, &block->page));
1891 1892 1893
		block->check_index_page_at_flush = FALSE;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
1894 1895
	//buf_pool_mutex_exit(buf_pool);
	rw_lock_s_unlock(&buf_pool->page_hash_latch);
1896 1897
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1898
/********************************************************************//**
1899 1900
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
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1901 1902
pool if it is found there.
@return	TRUE if page hash index is built in search system */
1903 1904 1905 1906
UNIV_INTERN
ibool
buf_page_peek_if_search_hashed(
/*===========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1907 1908
	ulint	space,	/*!< in: space id */
	ulint	offset)	/*!< in: page number */
1909 1910 1911
{
	buf_block_t*	block;
	ibool		is_hashed;
Sergei Golubchik's avatar
Sergei Golubchik committed
1912
	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
1913

Sergei Golubchik's avatar
Sergei Golubchik committed
1914 1915
	//buf_pool_mutex_enter(buf_pool);
	rw_lock_s_lock(&buf_pool->page_hash_latch);
1916

Sergei Golubchik's avatar
Sergei Golubchik committed
1917
	block = (buf_block_t*) buf_page_hash_get(buf_pool, space, offset);
1918 1919 1920 1921

	if (!block || buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE) {
		is_hashed = FALSE;
	} else {
Sergei Golubchik's avatar
Sergei Golubchik committed
1922
		ut_ad(!buf_pool_watch_is_sentinel(buf_pool, &block->page));
1923 1924 1925
		is_hashed = block->is_hashed;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
1926 1927
	//buf_pool_mutex_exit(buf_pool);
	rw_lock_s_unlock(&buf_pool->page_hash_latch);
1928 1929 1930 1931

	return(is_hashed);
}

1932
#if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1933
/********************************************************************//**
1934 1935 1936
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
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1937 1938
reallocated.
@return	control block if found in page hash table, otherwise NULL */
1939 1940 1941 1942
UNIV_INTERN
buf_page_t*
buf_page_set_file_page_was_freed(
/*=============================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1943 1944
	ulint	space,	/*!< in: space id */
	ulint	offset)	/*!< in: page number */
1945 1946
{
	buf_page_t*	bpage;
Sergei Golubchik's avatar
Sergei Golubchik committed
1947
	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
1948

Sergei Golubchik's avatar
Sergei Golubchik committed
1949 1950
	//buf_pool_mutex_enter(buf_pool);
	rw_lock_s_lock(&buf_pool->page_hash_latch);
1951

Sergei Golubchik's avatar
Sergei Golubchik committed
1952
	bpage = buf_page_hash_get(buf_pool, space, offset);
1953 1954

	if (bpage) {
Sergei Golubchik's avatar
Sergei Golubchik committed
1955
		ut_ad(!buf_pool_watch_is_sentinel(buf_pool, bpage));
1956 1957
		/* bpage->file_page_was_freed can already hold
		when this code is invoked from dict_drop_index_tree() */
1958 1959 1960
		bpage->file_page_was_freed = TRUE;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
1961 1962
	//buf_pool_mutex_exit(buf_pool);
	rw_lock_s_unlock(&buf_pool->page_hash_latch);
1963 1964 1965 1966

	return(bpage);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1967
/********************************************************************//**
1968 1969 1970
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
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1971 1972
reallocated.
@return	control block if found in page hash table, otherwise NULL */
1973 1974 1975 1976
UNIV_INTERN
buf_page_t*
buf_page_reset_file_page_was_freed(
/*===============================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1977 1978
	ulint	space,	/*!< in: space id */
	ulint	offset)	/*!< in: page number */
1979 1980
{
	buf_page_t*	bpage;
Sergei Golubchik's avatar
Sergei Golubchik committed
1981
	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
1982

Sergei Golubchik's avatar
Sergei Golubchik committed
1983 1984
	//buf_pool_mutex_enter(buf_pool);
	rw_lock_s_lock(&buf_pool->page_hash_latch);
1985

Sergei Golubchik's avatar
Sergei Golubchik committed
1986
	bpage = buf_page_hash_get(buf_pool, space, offset);
1987 1988

	if (bpage) {
Sergei Golubchik's avatar
Sergei Golubchik committed
1989
		ut_ad(!buf_pool_watch_is_sentinel(buf_pool, bpage));
1990 1991 1992
		bpage->file_page_was_freed = FALSE;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
1993 1994
	//buf_pool_mutex_exit(buf_pool);
	rw_lock_s_unlock(&buf_pool->page_hash_latch);
1995 1996 1997

	return(bpage);
}
1998
#endif /* UNIV_DEBUG_FILE_ACCESSES || UNIV_DEBUG */
1999

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2000
/********************************************************************//**
2001 2002 2003 2004 2005 2006
Get read access to a compressed page (usually of type
FIL_PAGE_TYPE_ZBLOB or FIL_PAGE_TYPE_ZBLOB2).
The page must be released with buf_page_release_zip().
NOTE: the page is not protected by any latch.  Mutual exclusion has to
be implemented at a higher level.  In other words, all possible
accesses to a given page through this function must be protected by
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2007 2008
the same set of mutexes or latches.
@return	pointer to the block */
2009 2010 2011 2012
UNIV_INTERN
buf_page_t*
buf_page_get_zip(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2013 2014 2015
	ulint		space,	/*!< in: space id */
	ulint		zip_size,/*!< in: compressed page size */
	ulint		offset)	/*!< in: page number */
2016 2017 2018 2019
{
	buf_page_t*	bpage;
	mutex_t*	block_mutex;
	ibool		must_read;
2020 2021 2022 2023 2024 2025
	unsigned	access_time;
	trx_t*		trx = NULL;
	ulint		sec;
	ulint		ms;
	ib_uint64_t	start_time;
	ib_uint64_t	finish_time;
Sergei Golubchik's avatar
Sergei Golubchik committed
2026
	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
2027

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2028 2029 2030
	if (innobase_get_slow_log()) {
		trx = innobase_get_trx();
	}
2031
	buf_pool->stat.n_page_gets++;
2032 2033

	for (;;) {
Sergei Golubchik's avatar
Sergei Golubchik committed
2034
		//buf_pool_mutex_enter(buf_pool);
2035
lookup:
Sergei Golubchik's avatar
Sergei Golubchik committed
2036 2037
		rw_lock_s_lock(&buf_pool->page_hash_latch);
		bpage = buf_page_hash_get(buf_pool, space, offset);
2038
		if (bpage) {
Sergei Golubchik's avatar
Sergei Golubchik committed
2039
			ut_ad(!buf_pool_watch_is_sentinel(buf_pool, bpage));
2040 2041 2042 2043 2044
			break;
		}

		/* Page not in buf_pool: needs to be read from file */

Sergei Golubchik's avatar
Sergei Golubchik committed
2045 2046
		//buf_pool_mutex_exit(buf_pool);
		rw_lock_s_unlock(&buf_pool->page_hash_latch);
2047

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2048
		buf_read_page(space, zip_size, offset, trx);
2049 2050 2051 2052 2053 2054

#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
		ut_a(++buf_dbg_counter % 37 || buf_validate());
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
	}

2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
	if (UNIV_UNLIKELY(bpage->space_was_being_deleted)) {
		/* This page is obsoleted, should discard and retry */
		rw_lock_s_unlock(&buf_pool->page_hash_latch);

		mutex_enter(&buf_pool->LRU_list_mutex);
		block_mutex = buf_page_get_mutex_enter(bpage);

		if (UNIV_UNLIKELY(!block_mutex)) {
			mutex_exit(&buf_pool->LRU_list_mutex);
			goto lookup;
		}

		buf_LRU_free_block(bpage, TRUE, TRUE);

		mutex_exit(&buf_pool->LRU_list_mutex);
		mutex_exit(block_mutex);
		block_mutex = NULL;

		goto lookup;
	}

2076 2077
	if (UNIV_UNLIKELY(!bpage->zip.data)) {
		/* There is no compressed page. */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2078
err_exit:
Sergei Golubchik's avatar
Sergei Golubchik committed
2079 2080
		//buf_pool_mutex_exit(buf_pool);
		rw_lock_s_unlock(&buf_pool->page_hash_latch);
2081 2082 2083
		return(NULL);
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
2084
	if (srv_pass_corrupt_table <= 1) {
2085
		if (bpage->is_corrupt) {
Sergei Golubchik's avatar
Sergei Golubchik committed
2086
			rw_lock_s_unlock(&buf_pool->page_hash_latch);
2087 2088 2089 2090
			return(NULL);
		}
	}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2091
	block_mutex = buf_page_get_mutex_enter(bpage);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2092

Sergei Golubchik's avatar
Sergei Golubchik committed
2093 2094 2095
	rw_lock_s_unlock(&buf_pool->page_hash_latch);

	ut_ad(!buf_pool_watch_is_sentinel(buf_pool, bpage));
2096 2097 2098 2099 2100 2101 2102

	switch (buf_page_get_state(bpage)) {
	case BUF_BLOCK_NOT_USED:
	case BUF_BLOCK_READY_FOR_USE:
	case BUF_BLOCK_MEMORY:
	case BUF_BLOCK_REMOVE_HASH:
	case BUF_BLOCK_ZIP_FREE:
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2103 2104
		if (block_mutex)
			mutex_exit(block_mutex);
2105 2106 2107
		break;
	case BUF_BLOCK_ZIP_PAGE:
	case BUF_BLOCK_ZIP_DIRTY:
Sergei Golubchik's avatar
Sergei Golubchik committed
2108
		ut_a(block_mutex == &buf_pool->zip_mutex);
2109
		bpage->buf_fix_count++;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2110
		goto got_block;
2111
	case BUF_BLOCK_FILE_PAGE:
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2112 2113
		ut_a(block_mutex == &((buf_block_t*) bpage)->mutex);

Sergei Golubchik's avatar
Sergei Golubchik committed
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
		/* release mutex to obey to latch-order */
		mutex_exit(block_mutex);

		/* get LRU_list_mutex for buf_LRU_free_block() */
		mutex_enter(&buf_pool->LRU_list_mutex);
		mutex_enter(block_mutex);

		if (UNIV_UNLIKELY(bpage->space != space
				  || bpage->offset != offset
				  || !bpage->in_LRU_list
				  || !bpage->zip.data)) {
			/* someone should interrupt, retry */
			mutex_exit(&buf_pool->LRU_list_mutex);
			mutex_exit(block_mutex);
			goto lookup;
		}
2130

Sergei Golubchik's avatar
Sergei Golubchik committed
2131
		/* Discard the uncompressed page frame if possible. */
2132
		if (buf_LRU_free_block(bpage, FALSE, TRUE)) {
Sergei Golubchik's avatar
Sergei Golubchik committed
2133
			mutex_exit(&buf_pool->LRU_list_mutex);
2134 2135 2136 2137
			mutex_exit(block_mutex);
			goto lookup;
		}

Sergei Golubchik's avatar
Sergei Golubchik committed
2138 2139
		mutex_exit(&buf_pool->LRU_list_mutex);

2140 2141
		buf_block_buf_fix_inc((buf_block_t*) bpage,
				      __FILE__, __LINE__);
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2142
		goto got_block;
2143 2144
	}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2145 2146 2147 2148
	ut_error;
	goto err_exit;

got_block:
2149
	must_read = buf_page_get_io_fix(bpage) == BUF_IO_READ;
2150
	access_time = buf_page_is_accessed(bpage);
2151

Sergei Golubchik's avatar
Sergei Golubchik committed
2152
	//buf_pool_mutex_exit(buf_pool);
2153 2154 2155

	mutex_exit(block_mutex);

2156
	buf_page_set_accessed_make_young(bpage, access_time);
2157

2158
#if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
	ut_a(!bpage->file_page_was_freed);
#endif

#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
	ut_a(++buf_dbg_counter % 5771 || buf_validate());
	ut_a(bpage->buf_fix_count > 0);
	ut_a(buf_page_in_file(bpage));
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */

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

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2172 2173 2174 2175 2176 2177 2178
		if (innobase_get_slow_log() && trx && trx->take_stats)
		{
			ut_usectime(&sec, &ms);
			start_time = (ib_uint64_t)sec * 1000000 + ms;
		} else {
			start_time = 0;
		}
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
		for (;;) {
			enum buf_io_fix	io_fix;

			mutex_enter(block_mutex);
			io_fix = buf_page_get_io_fix(bpage);
			mutex_exit(block_mutex);

			if (io_fix == BUF_IO_READ) {

				os_thread_sleep(WAIT_FOR_READ);
			} else {
				break;
			}
		}
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2193 2194 2195 2196 2197 2198
		if (innobase_get_slow_log() && trx && trx->take_stats && start_time)
		{
			ut_usectime(&sec, &ms);
			finish_time = (ib_uint64_t)sec * 1000000 + ms;
			trx->io_reads_wait_timer += (ulint)(finish_time - start_time);
		}
2199 2200 2201 2202 2203 2204 2205 2206 2207
	}

#ifdef UNIV_IBUF_COUNT_DEBUG
	ut_a(ibuf_count_get(buf_page_get_space(bpage),
			    buf_page_get_page_no(bpage)) == 0);
#endif
	return(bpage);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2208
/********************************************************************//**
2209 2210 2211 2212 2213
Initialize some fields of a control block. */
UNIV_INLINE
void
buf_block_init_low(
/*===============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2214
	buf_block_t*	block)	/*!< in: block to init */
2215 2216 2217
{
	block->check_index_page_at_flush = FALSE;
	block->index		= NULL;
Sergei Golubchik's avatar
Sergei Golubchik committed
2218
	block->btr_search_latch	= NULL;
2219 2220 2221 2222 2223 2224 2225

	block->n_hash_helps	= 0;
	block->is_hashed	= FALSE;
	block->n_fields		= 1;
	block->n_bytes		= 0;
	block->left_side	= TRUE;
}
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2226
#endif /* !UNIV_HOTBACKUP */
2227

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2228 2229 2230 2231
/********************************************************************//**
Decompress a block.
@return	TRUE if successful */
UNIV_INTERN
2232 2233 2234
ibool
buf_zip_decompress(
/*===============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2235 2236
	buf_block_t*	block,	/*!< in/out: block */
	ibool		check)	/*!< in: TRUE=verify the page checksum */
2237
{
2238 2239 2240
	const byte*	frame		= block->page.zip.data;
	ulint		stamp_checksum	= mach_read_from_4(
		frame + FIL_PAGE_SPACE_OR_CHKSUM);
2241 2242 2243 2244

	ut_ad(buf_block_get_zip_size(block));
	ut_a(buf_block_get_space(block) != 0);

2245
	if (UNIV_LIKELY(check && stamp_checksum != BUF_NO_CHECKSUM_MAGIC)) {
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
		ulint	calc_checksum	= page_zip_calc_checksum(
			frame, page_zip_get_size(&block->page.zip));

		if (UNIV_UNLIKELY(stamp_checksum != calc_checksum)) {
			ut_print_timestamp(stderr);
			fprintf(stderr,
				"  InnoDB: compressed page checksum mismatch"
				" (space %u page %u): %lu != %lu\n",
				block->page.space, block->page.offset,
				stamp_checksum, calc_checksum);
			return(FALSE);
		}
	}

	switch (fil_page_get_type(frame)) {
	case FIL_PAGE_INDEX:
		if (page_zip_decompress(&block->page.zip,
2263
					block->frame, TRUE)) {
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
			return(TRUE);
		}

		fprintf(stderr,
			"InnoDB: unable to decompress space %lu page %lu\n",
			(ulong) block->page.space,
			(ulong) block->page.offset);
		return(FALSE);

	case FIL_PAGE_TYPE_ALLOCATED:
	case FIL_PAGE_INODE:
	case FIL_PAGE_IBUF_BITMAP:
	case FIL_PAGE_TYPE_FSP_HDR:
	case FIL_PAGE_TYPE_XDES:
	case FIL_PAGE_TYPE_ZBLOB:
	case FIL_PAGE_TYPE_ZBLOB2:
		/* Copy to uncompressed storage. */
		memcpy(block->frame, frame,
		       buf_block_get_zip_size(block));
		return(TRUE);
	}

	ut_print_timestamp(stderr);
	fprintf(stderr,
		"  InnoDB: unknown compressed page"
		" type %lu\n",
		fil_page_get_type(frame));
	return(FALSE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2294 2295
#ifndef UNIV_HOTBACKUP
/*******************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
2296 2297 2298
Gets the block to whose frame the pointer is pointing to if found
in this buffer pool instance.
@return	pointer to block */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2299 2300
UNIV_INTERN
buf_block_t*
Sergei Golubchik's avatar
Sergei Golubchik committed
2301 2302 2303 2304 2305
buf_block_align_instance(
/*=====================*/
 	buf_pool_t*	buf_pool,	/*!< in: buffer in which the block
					resides */
	const byte*	ptr)		/*!< in: pointer to a frame */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2306 2307 2308 2309 2310 2311 2312
{
	buf_chunk_t*	chunk;
	ulint		i;

	/* TODO: protect buf_pool->chunks with a mutex (it will
	currently remain constant after buf_pool_init()) */
	for (chunk = buf_pool->chunks, i = buf_pool->n_chunks; i--; chunk++) {
Sergei Golubchik's avatar
Sergei Golubchik committed
2313
		ulint	offs;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2314

Sergei Golubchik's avatar
Sergei Golubchik committed
2315
		if (UNIV_UNLIKELY(ptr < chunk->blocks->frame)) {
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2316 2317 2318

			continue;
		}
Sergei Golubchik's avatar
Sergei Golubchik committed
2319 2320 2321
		/* else */

		offs = ptr - chunk->blocks->frame;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2322 2323 2324

		offs >>= UNIV_PAGE_SIZE_SHIFT;

Sergei Golubchik's avatar
Sergei Golubchik committed
2325
		if (UNIV_LIKELY(offs < chunk->size)) {
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2326 2327 2328 2329 2330 2331 2332 2333
			buf_block_t*	block = &chunk->blocks[offs];

			/* The function buf_chunk_init() invokes
			buf_block_init() so that block[n].frame ==
			block->frame + n * UNIV_PAGE_SIZE.  Check it. */
			ut_ad(block->frame == page_align(ptr));
#ifdef UNIV_DEBUG
			/* A thread that updates these fields must
Sergei Golubchik's avatar
Sergei Golubchik committed
2334
			hold buf_pool->mutex and block->mutex.  Acquire
Vadim Tkachenko's avatar
Vadim Tkachenko committed
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 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385
			only the latter. */
			mutex_enter(&block->mutex);

			switch (buf_block_get_state(block)) {
			case BUF_BLOCK_ZIP_FREE:
			case BUF_BLOCK_ZIP_PAGE:
			case BUF_BLOCK_ZIP_DIRTY:
				/* These types should only be used in
				the compressed buffer pool, whose
				memory is allocated from
				buf_pool->chunks, in UNIV_PAGE_SIZE
				blocks flagged as BUF_BLOCK_MEMORY. */
				ut_error;
				break;
			case BUF_BLOCK_NOT_USED:
			case BUF_BLOCK_READY_FOR_USE:
			case BUF_BLOCK_MEMORY:
				/* Some data structures contain
				"guess" pointers to file pages.  The
				file pages may have been freed and
				reused.  Do not complain. */
				break;
			case BUF_BLOCK_REMOVE_HASH:
				/* buf_LRU_block_remove_hashed_page()
				will overwrite the FIL_PAGE_OFFSET and
				FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID with
				0xff and set the state to
				BUF_BLOCK_REMOVE_HASH. */
				ut_ad(page_get_space_id(page_align(ptr))
				      == 0xffffffff);
				ut_ad(page_get_page_no(page_align(ptr))
				      == 0xffffffff);
				break;
			case BUF_BLOCK_FILE_PAGE:
				ut_ad(block->page.space
				      == page_get_space_id(page_align(ptr)));
				ut_ad(block->page.offset
				      == page_get_page_no(page_align(ptr)));
				break;
			}

			mutex_exit(&block->mutex);
#endif /* UNIV_DEBUG */

			return(block);
		}
	}

	return(NULL);
}

Sergei Golubchik's avatar
Sergei Golubchik committed
2386 2387 2388
/*******************************************************************//**
Gets the block to whose frame the pointer is pointing to.
@return	pointer to block, never NULL */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2389
UNIV_INTERN
Sergei Golubchik's avatar
Sergei Golubchik committed
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
buf_block_t*
buf_block_align(
/*============*/
	const byte*	ptr)	/*!< in: pointer to a frame */
{
	ulint		i;

	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_block_t*	block;

		block = buf_block_align_instance(
			buf_pool_from_array(i), ptr);
		if (block) {
			return(block);
		}
	}

	/* The block should always be found. */
	ut_error;
	return(NULL);
}

/********************************************************************//**
Find out if a pointer belongs to a buf_block_t. It can be a pointer to
the buf_block_t itself or a member of it. This functions checks one of
the buffer pool instances.
@return	TRUE if ptr belongs to a buf_block_t struct */
static
2418
ibool
Sergei Golubchik's avatar
Sergei Golubchik committed
2419 2420 2421 2422
buf_pointer_is_block_field_instance(
/*================================*/
	buf_pool_t*	buf_pool,	/*!< in: buffer pool instance */
	const void*	ptr)		/*!< in: pointer not dereferenced */
2423 2424 2425 2426
{
	const buf_chunk_t*		chunk	= buf_pool->chunks;
	const buf_chunk_t* const	echunk	= chunk + buf_pool->n_chunks;

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2427 2428
	/* TODO: protect buf_pool->chunks with a mutex (it will
	currently remain constant after buf_pool_init()) */
2429
	while (chunk < echunk) {
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2430 2431
		if (ptr >= (void *)chunk->blocks
		    && ptr < (void *)(chunk->blocks + chunk->size)) {
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441

			return(TRUE);
		}

		chunk++;
	}

	return(FALSE);
}

Sergei Golubchik's avatar
Sergei Golubchik committed
2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
/********************************************************************//**
Find out if a pointer belongs to a buf_block_t. It can be a pointer to
the buf_block_t itself or a member of it
@return	TRUE if ptr belongs to a buf_block_t struct */
UNIV_INTERN
ibool
buf_pointer_is_block_field(
/*=======================*/
	const void*	ptr)	/*!< in: pointer not dereferenced */
{
	ulint	i;

	for (i = 0; i < srv_buf_pool_instances; i++) {
		ibool	found;

		found = buf_pointer_is_block_field_instance(
			buf_pool_from_array(i), ptr);
		if (found) {
			return(TRUE);
		}
	}

	return(FALSE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2467 2468 2469 2470 2471 2472 2473
/********************************************************************//**
Find out if a buffer block was created by buf_chunk_init().
@return	TRUE if "block" has been added to buf_pool->free by buf_chunk_init() */
static
ibool
buf_block_is_uncompressed(
/*======================*/
Sergei Golubchik's avatar
Sergei Golubchik committed
2474 2475 2476
	buf_pool_t*		buf_pool,	/*!< in: buffer pool instance */
	const buf_block_t*	block)		/*!< in: pointer to block,
						not dereferenced */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2477
{
Sergei Golubchik's avatar
Sergei Golubchik committed
2478
	//ut_ad(buf_pool_mutex_own(buf_pool));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2479 2480 2481 2482 2483 2484

	if (UNIV_UNLIKELY((((ulint) block) % sizeof *block) != 0)) {
		/* The pointer should be aligned. */
		return(FALSE);
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
2485
	return(buf_pointer_is_block_field_instance(buf_pool, (void *)block));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2486 2487 2488 2489 2490
}

/********************************************************************//**
This is the general function used to get access to a database page.
@return	pointer to the block or NULL */
2491 2492 2493 2494
UNIV_INTERN
buf_block_t*
buf_page_get_gen(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2495 2496
	ulint		space,	/*!< in: space id */
	ulint		zip_size,/*!< in: compressed page size in bytes
2497
				or 0 for uncompressed pages */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2498 2499 2500 2501
	ulint		offset,	/*!< in: page number */
	ulint		rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
	buf_block_t*	guess,	/*!< in: guessed block or NULL */
	ulint		mode,	/*!< in: BUF_GET, BUF_GET_IF_IN_POOL,
Sergei Golubchik's avatar
Sergei Golubchik committed
2502 2503
				BUF_PEEK_IF_IN_POOL, BUF_GET_NO_LATCH, or
				BUF_GET_IF_IN_POOL_OR_WATCH */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2504 2505 2506
	const char*	file,	/*!< in: file name */
	ulint		line,	/*!< in: line where called */
	mtr_t*		mtr)	/*!< in: mini-transaction */
2507 2508
{
	buf_block_t*	block;
Sergei Golubchik's avatar
Sergei Golubchik committed
2509
	ulint		fold;
2510
	unsigned	access_time;
2511 2512
	ulint		fix_type;
	ibool		must_read;
2513
	ulint		retries = 0;
Sergei Golubchik's avatar
Sergei Golubchik committed
2514 2515 2516 2517 2518 2519 2520
	mutex_t*	block_mutex = NULL;
	trx_t*		trx = NULL;
	ulint		sec;
	ulint		ms;
	ib_uint64_t	start_time;
	ib_uint64_t	finish_time;
	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
2521 2522

	ut_ad(mtr);
2523
	ut_ad(mtr->state == MTR_ACTIVE);
2524 2525 2526
	ut_ad((rw_latch == RW_S_LATCH)
	      || (rw_latch == RW_X_LATCH)
	      || (rw_latch == RW_NO_LATCH));
Sergei Golubchik's avatar
Sergei Golubchik committed
2527 2528 2529 2530 2531 2532 2533 2534 2535
#ifdef UNIV_DEBUG
	switch (mode) {
	case BUF_GET_NO_LATCH:
		ut_ad(rw_latch == RW_NO_LATCH);
		break;
	case BUF_GET:
	case BUF_GET_IF_IN_POOL:
	case BUF_PEEK_IF_IN_POOL:
	case BUF_GET_IF_IN_POOL_OR_WATCH:
2536
	case BUF_GET_POSSIBLY_FREED:
Sergei Golubchik's avatar
Sergei Golubchik committed
2537 2538 2539 2540 2541
		break;
	default:
		ut_error;
	}
#endif /* UNIV_DEBUG */
2542
	ut_ad(zip_size == fil_space_get_zip_size(space));
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2543
	ut_ad(ut_is_2pow(zip_size));
2544
#ifndef UNIV_LOG_DEBUG
Sergei Golubchik's avatar
Sergei Golubchik committed
2545 2546 2547
	ut_ad(!ibuf_inside(mtr)
	      || ibuf_page_low(space, zip_size, offset,
			       FALSE, file, line, NULL));
2548
#endif
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2549 2550 2551
	if (innobase_get_slow_log()) {
		trx = innobase_get_trx();
	}
2552
	buf_pool->stat.n_page_gets++;
Sergei Golubchik's avatar
Sergei Golubchik committed
2553
	fold = buf_page_address_fold(space, offset);
2554 2555
loop:
	block = guess;
Sergei Golubchik's avatar
Sergei Golubchik committed
2556
	//buf_pool_mutex_enter(buf_pool);
2557 2558

	if (block) {
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2559
		block_mutex = buf_page_get_mutex_enter((buf_page_t*)block);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2560

2561
		/* If the guess is a compressed page descriptor that
2562 2563
		has been allocated by buf_page_alloc_descriptor(),
		it may have been freed by buf_relocate(). */
2564

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2565 2566
		if (!block_mutex) {
			block = guess = NULL;
Sergei Golubchik's avatar
Sergei Golubchik committed
2567
		} else if (!buf_block_is_uncompressed(buf_pool, block)
2568 2569 2570 2571
		    || offset != block->page.offset
		    || space != block->page.space
		    || buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE) {

Vadim Tkachenko's avatar
Vadim Tkachenko committed
2572 2573
			mutex_exit(block_mutex);

2574 2575 2576 2577 2578 2579 2580 2581
			block = guess = NULL;
		} else {
			ut_ad(!block->page.in_zip_hash);
			ut_ad(block->page.in_page_hash);
		}
	}

	if (block == NULL) {
Sergei Golubchik's avatar
Sergei Golubchik committed
2582 2583 2584
		rw_lock_s_lock(&buf_pool->page_hash_latch);
		block = (buf_block_t*) buf_page_hash_get_low(
			buf_pool, space, offset, fold);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2585
		if (block) {
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606
			if (UNIV_UNLIKELY(block->page.space_was_being_deleted)) {
				/* This page is obsoleted, should discard and retry */
				rw_lock_s_unlock(&buf_pool->page_hash_latch);

				mutex_enter(&buf_pool->LRU_list_mutex);
				block_mutex = buf_page_get_mutex_enter((buf_page_t*)block);

				if (UNIV_UNLIKELY(!block_mutex)) {
					mutex_exit(&buf_pool->LRU_list_mutex);
					goto loop;
				}

				buf_LRU_free_block((buf_page_t*)block, TRUE, TRUE);

				mutex_exit(&buf_pool->LRU_list_mutex);
				mutex_exit(block_mutex);
				block_mutex = NULL;

				goto loop;
			}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2607 2608
			block_mutex = buf_page_get_mutex_enter((buf_page_t*)block);
			ut_a(block_mutex);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2609
		}
Sergei Golubchik's avatar
Sergei Golubchik committed
2610
		rw_lock_s_unlock(&buf_pool->page_hash_latch);
2611 2612 2613
	}

loop2:
Sergei Golubchik's avatar
Sergei Golubchik committed
2614 2615 2616 2617 2618
	if (block && buf_pool_watch_is_sentinel(buf_pool, &block->page)) {
		mutex_exit(block_mutex);
		block = NULL;
	}

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

Sergei Golubchik's avatar
Sergei Golubchik committed
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
		if (mode == BUF_GET_IF_IN_POOL_OR_WATCH) {
			block = (buf_block_t*) buf_pool_watch_set(
				space, offset, fold);

			if (UNIV_LIKELY_NULL(block)) {
				block_mutex = buf_page_get_mutex((buf_page_t*)block);
				ut_a(block_mutex);
				ut_ad(mutex_own(block_mutex));
				goto got_block;
			}
		}
2633

Sergei Golubchik's avatar
Sergei Golubchik committed
2634
		//buf_pool_mutex_exit(buf_pool);
2635

Sergei Golubchik's avatar
Sergei Golubchik committed
2636 2637 2638
		if (mode == BUF_GET_IF_IN_POOL
		    || mode == BUF_PEEK_IF_IN_POOL
		    || mode == BUF_GET_IF_IN_POOL_OR_WATCH) {
2639 2640 2641 2642

			return(NULL);
		}

2643
		if (buf_read_page(space, zip_size, offset, trx)) {
2644 2645 2646
			buf_read_ahead_random(space, zip_size, offset,
					      ibuf_inside(mtr), trx);

2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668
			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;
		}
2669 2670 2671 2672 2673 2674 2675

#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
		ut_a(++buf_dbg_counter % 37 || buf_validate());
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
		goto loop;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
2676
got_block:
2677 2678 2679 2680
	ut_ad(page_zip_get_size(&block->page.zip) == zip_size);

	must_read = buf_block_get_io_fix(block) == BUF_IO_READ;

Sergei Golubchik's avatar
Sergei Golubchik committed
2681 2682 2683 2684 2685 2686 2687
	if (must_read && (mode == BUF_GET_IF_IN_POOL
			  || mode == BUF_PEEK_IF_IN_POOL)) {

		/* The page is being read to buffer pool,
		but we cannot wait around for the read to
		complete. */
		//buf_pool_mutex_exit(buf_pool);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2688
		mutex_exit(block_mutex);
2689 2690 2691 2692

		return(NULL);
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
2693
	if (srv_pass_corrupt_table <= 1) {
2694 2695 2696 2697 2698 2699
		if (block->page.is_corrupt) {
			mutex_exit(block_mutex);
			return(NULL);
		}
	}

2700 2701 2702 2703 2704
	switch (buf_block_get_state(block)) {
		buf_page_t*	bpage;
		ibool		success;

	case BUF_BLOCK_FILE_PAGE:
Sergei Golubchik's avatar
Sergei Golubchik committed
2705
		if (block_mutex == &buf_pool->zip_mutex) {
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2706 2707 2708 2709
			/* it is wrong mutex... */
			mutex_exit(block_mutex);
			goto loop;
		}
2710 2711 2712 2713
		break;

	case BUF_BLOCK_ZIP_PAGE:
	case BUF_BLOCK_ZIP_DIRTY:
Sergei Golubchik's avatar
Sergei Golubchik committed
2714
		ut_ad(block_mutex == &buf_pool->zip_mutex);
2715
		bpage = &block->page;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2716
		/* Protect bpage->buf_fix_count. */
Sergei Golubchik's avatar
Sergei Golubchik committed
2717
		//mutex_enter(&buf_pool->zip_mutex);
2718 2719 2720 2721 2722 2723

		if (bpage->buf_fix_count
		    || buf_page_get_io_fix(bpage) != BUF_IO_NONE) {
			/* This condition often occurs when the buffer
			is not buffer-fixed, but I/O-fixed by
			buf_page_init_for_read(). */
Sergei Golubchik's avatar
Sergei Golubchik committed
2724
			//mutex_exit(&buf_pool->zip_mutex);
2725 2726 2727
wait_until_unfixed:
			/* The block is buffer-fixed or I/O-fixed.
			Try again later. */
Sergei Golubchik's avatar
Sergei Golubchik committed
2728
			//buf_pool_mutex_exit(buf_pool);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2729
			mutex_exit(block_mutex);
2730 2731 2732 2733 2734 2735
			os_thread_sleep(WAIT_FOR_READ);

			goto loop;
		}

		/* Allocate an uncompressed page. */
Sergei Golubchik's avatar
Sergei Golubchik committed
2736 2737
		//buf_pool_mutex_exit(buf_pool);
		//mutex_exit(&buf_pool->zip_mutex);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2738
		mutex_exit(block_mutex);
2739

Sergei Golubchik's avatar
Sergei Golubchik committed
2740
		block = buf_LRU_get_free_block(buf_pool);
2741
		ut_a(block);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2742
		block_mutex = &block->mutex;
2743

Sergei Golubchik's avatar
Sergei Golubchik committed
2744 2745 2746
		//buf_pool_mutex_enter(buf_pool);
		mutex_enter(&buf_pool->LRU_list_mutex);
		rw_lock_x_lock(&buf_pool->page_hash_latch);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2747
		mutex_enter(block_mutex);
2748 2749

		{
Sergei Golubchik's avatar
Sergei Golubchik committed
2750 2751 2752 2753
			buf_page_t*	hash_bpage;

			hash_bpage = buf_page_hash_get_low(
				buf_pool, space, offset, fold);
2754 2755 2756

			if (UNIV_UNLIKELY(bpage != hash_bpage)) {
				/* The buf_pool->page_hash was modified
Sergei Golubchik's avatar
Sergei Golubchik committed
2757
				while buf_pool->mutex was released.
2758 2759
				Free the block that was allocated. */

Vadim Tkachenko's avatar
Vadim Tkachenko committed
2760 2761
				buf_LRU_block_free_non_file_page(block, TRUE);
				mutex_exit(block_mutex);
2762 2763

				block = (buf_block_t*) hash_bpage;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2764
				if (block) {
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2765 2766
					block_mutex = buf_page_get_mutex_enter((buf_page_t*)block);
					ut_a(block_mutex);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2767
				}
Sergei Golubchik's avatar
Sergei Golubchik committed
2768 2769
				rw_lock_x_unlock(&buf_pool->page_hash_latch);
				mutex_exit(&buf_pool->LRU_list_mutex);
2770 2771 2772 2773
				goto loop2;
			}
		}

Sergei Golubchik's avatar
Sergei Golubchik committed
2774
		mutex_enter(&buf_pool->zip_mutex);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2775

2776 2777 2778 2779
		if (UNIV_UNLIKELY
		    (bpage->buf_fix_count
		     || buf_page_get_io_fix(bpage) != BUF_IO_NONE)) {

Sergei Golubchik's avatar
Sergei Golubchik committed
2780
			mutex_exit(&buf_pool->zip_mutex);
2781
			/* The block was buffer-fixed or I/O-fixed
Sergei Golubchik's avatar
Sergei Golubchik committed
2782
			while buf_pool->mutex was not held by this thread.
2783 2784 2785
			Free the block that was allocated and try again.
			This should be extremely unlikely. */

Vadim Tkachenko's avatar
Vadim Tkachenko committed
2786 2787
			buf_LRU_block_free_non_file_page(block, TRUE);
			//mutex_exit(&block->mutex);
2788

Sergei Golubchik's avatar
Sergei Golubchik committed
2789 2790
			rw_lock_x_unlock(&buf_pool->page_hash_latch);
			mutex_exit(&buf_pool->LRU_list_mutex);
2791 2792 2793 2794 2795 2796 2797
			goto wait_until_unfixed;
		}

		/* Move the compressed page from bpage to block,
		and uncompress it. */

		buf_relocate(bpage, &block->page);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2798

Sergei Golubchik's avatar
Sergei Golubchik committed
2799
		rw_lock_x_unlock(&buf_pool->page_hash_latch);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2800

2801 2802 2803 2804 2805 2806 2807 2808
		buf_block_init_low(block);
		block->lock_hash_val = lock_rec_hash(space, offset);

		UNIV_MEM_DESC(&block->page.zip.data,
			      page_zip_get_size(&block->page.zip), block);

		if (buf_page_get_state(&block->page)
		    == BUF_BLOCK_ZIP_PAGE) {
2809
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2810
			UT_LIST_REMOVE(zip_list, buf_pool->zip_clean,
2811
				       &block->page);
2812
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
2813 2814 2815
			ut_ad(!block->page.in_flush_list);
		} else {
			/* Relocate buf_pool->flush_list. */
2816 2817
			buf_flush_relocate_on_flush_list(bpage,
							 &block->page);
2818 2819 2820 2821 2822 2823 2824 2825 2826 2827
		}

		/* Buffer-fix, I/O-fix, and X-latch the block
		for the duration of the decompression.
		Also add the block to the unzip_LRU list. */
		block->page.state = BUF_BLOCK_FILE_PAGE;

		/* Insert at the front of unzip_LRU list */
		buf_unzip_LRU_add_block(block, FALSE);

Sergei Golubchik's avatar
Sergei Golubchik committed
2828
		mutex_exit(&buf_pool->LRU_list_mutex);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2829

2830 2831
		block->page.buf_fix_count = 1;
		buf_block_set_io_fix(block, BUF_IO_READ);
2832
		rw_lock_x_lock_func(&block->lock, 0, file, line);
2833 2834 2835

		UNIV_MEM_INVALID(bpage, sizeof *bpage);

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2836
		mutex_exit(block_mutex);
Sergei Golubchik's avatar
Sergei Golubchik committed
2837
		mutex_exit(&buf_pool->zip_mutex);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2838

Sergei Golubchik's avatar
Sergei Golubchik committed
2839
		buf_pool_mutex_enter(buf_pool);
2840
		buf_pool->n_pend_unzip++;
Sergei Golubchik's avatar
Sergei Golubchik committed
2841
		buf_pool_mutex_exit(buf_pool);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2842

Sergei Golubchik's avatar
Sergei Golubchik committed
2843
		//buf_pool_mutex_exit(buf_pool);
2844

2845 2846
		buf_page_free_descriptor(bpage);

2847
		/* Decompress the page and apply buffered operations
Sergei Golubchik's avatar
Sergei Golubchik committed
2848
		while not holding buf_pool->mutex or block->mutex. */
2849
		success = buf_zip_decompress(block, srv_use_checksums);
2850
		ut_a(success);
2851

2852
		if (UNIV_LIKELY(!recv_no_ibuf_operations)) {
2853 2854 2855 2856 2857
			ibuf_merge_or_delete_for_page(block, space, offset,
						      zip_size, TRUE);
		}

		/* Unfix and unlatch the block. */
Sergei Golubchik's avatar
Sergei Golubchik committed
2858
		//buf_pool_mutex_enter(buf_pool);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2859 2860
		block_mutex = &block->mutex;
		mutex_enter(block_mutex);
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2861 2862 2863
		block->page.buf_fix_count--;
		buf_block_set_io_fix(block, BUF_IO_NONE);

Sergei Golubchik's avatar
Sergei Golubchik committed
2864
		buf_pool_mutex_enter(buf_pool);
2865
		buf_pool->n_pend_unzip--;
Sergei Golubchik's avatar
Sergei Golubchik committed
2866
		buf_pool_mutex_exit(buf_pool);
2867
		rw_lock_x_unlock(&block->lock);
Sergei Golubchik's avatar
Sergei Golubchik committed
2868

2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881
		break;

	case BUF_BLOCK_ZIP_FREE:
	case BUF_BLOCK_NOT_USED:
	case BUF_BLOCK_READY_FOR_USE:
	case BUF_BLOCK_MEMORY:
	case BUF_BLOCK_REMOVE_HASH:
		ut_error;
		break;
	}

	ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);

Vadim Tkachenko's avatar
Vadim Tkachenko committed
2882
	//mutex_enter(&block->mutex);
2883 2884 2885 2886
#if UNIV_WORD_SIZE == 4
	/* On 32-bit systems, there is no padding in buf_page_t.  On
	other systems, Valgrind could complain about uninitialized pad
	bytes. */
2887
	UNIV_MEM_ASSERT_RW(&block->page, sizeof block->page);
2888
#endif
2889
#if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG
Sergei Golubchik's avatar
Sergei Golubchik committed
2890 2891
	if ((mode == BUF_GET_IF_IN_POOL || mode == BUF_GET_IF_IN_POOL_OR_WATCH)
	    && ibuf_debug) {
2892
		/* Try to evict the block from the buffer pool, to use the
Sergei Golubchik's avatar
Sergei Golubchik committed
2893
		insert buffer (change buffer) as much as possible. */
2894

2895
		if (buf_LRU_free_block(&block->page, TRUE, FALSE)) {
Sergei Golubchik's avatar
Sergei Golubchik committed
2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
			mutex_exit(block_mutex);
			if (mode == BUF_GET_IF_IN_POOL_OR_WATCH) {
				/* Set the watch, as it would have
				been set if the page were not in the
				buffer pool in the first place. */
				block = (buf_block_t*) buf_pool_watch_set(
					space, offset, fold);

				if (UNIV_LIKELY_NULL(block)) {
					block_mutex = buf_page_get_mutex((buf_page_t*)block);
					ut_a(block_mutex);
					ut_ad(mutex_own(block_mutex));
2908

Sergei Golubchik's avatar
Sergei Golubchik committed
2909 2910 2911 2912 2913 2914 2915
					/* The page entered the buffer
					pool for some reason. Try to
					evict it again. */
					goto got_block;
				}
			}
			//buf_pool_mutex_exit(buf_pool);
2916 2917 2918 2919
			fprintf(stderr,
				"innodb_change_buffering_debug evict %u %u\n",
				(unsigned) space, (unsigned) offset);
			return(NULL);
Sergei Golubchik's avatar
Sergei Golubchik committed
2920
		} else if (buf_flush_page_try(buf_pool, block)) {
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930
			fprintf(stderr,
				"innodb_change_buffering_debug flush %u %u\n",
				(unsigned) space, (unsigned) offset);
			guess = block;
			goto loop;
		}

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

	buf_block_buf_fix_inc(block, file, line);
2933 2934 2935 2936
#if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
	ut_a(mode == BUF_GET_POSSIBLY_FREED
	     || !block->page.file_page_was_freed);
#endif
2937
	//mutex_exit(&block->mutex);
2938

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

2941
	access_time = buf_page_is_accessed(&block->page);
2942

Sergei Golubchik's avatar
Sergei Golubchik committed
2943
	//buf_pool_mutex_exit(buf_pool);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2944
	mutex_exit(block_mutex);
2945

Sergei Golubchik's avatar
Sergei Golubchik committed
2946 2947 2948
	if (UNIV_LIKELY(mode != BUF_PEEK_IF_IN_POOL)) {
		buf_page_set_accessed_make_young(&block->page, access_time);
	}
2949 2950 2951 2952 2953 2954 2955

#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
	ut_a(++buf_dbg_counter % 5771 || buf_validate());
	ut_a(block->page.buf_fix_count > 0);
	ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */

Vadim Tkachenko's avatar
Vadim Tkachenko committed
2956 2957
	switch (rw_latch) {
	case RW_NO_LATCH:
2958 2959 2960 2961
		if (must_read) {
			/* Let us wait until the read operation
			completes */

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2962 2963 2964 2965 2966 2967 2968
			if (innobase_get_slow_log() && trx && trx->take_stats)
			{
				ut_usectime(&sec, &ms);
				start_time = (ib_uint64_t)sec * 1000000 + ms;
			} else {
				start_time = 0;
			}
2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982
			for (;;) {
				enum buf_io_fix	io_fix;

				mutex_enter(&block->mutex);
				io_fix = buf_block_get_io_fix(block);
				mutex_exit(&block->mutex);

				if (io_fix == BUF_IO_READ) {

					os_thread_sleep(WAIT_FOR_READ);
				} else {
					break;
				}
			}
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2983 2984 2985 2986 2987 2988
			if (innobase_get_slow_log() && trx && trx->take_stats && start_time)
			{
				ut_usectime(&sec, &ms);
				finish_time = (ib_uint64_t)sec * 1000000 + ms;
				trx->io_reads_wait_timer += (ulint)(finish_time - start_time);
			}
2989 2990 2991
		}

		fix_type = MTR_MEMO_BUF_FIX;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2992
		break;
2993

Vadim Tkachenko's avatar
Vadim Tkachenko committed
2994
	case RW_S_LATCH:
2995 2996 2997
		rw_lock_s_lock_func(&(block->lock), 0, file, line);

		fix_type = MTR_MEMO_PAGE_S_FIX;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
2998 2999 3000 3001
		break;

	default:
		ut_ad(rw_latch == RW_X_LATCH);
3002 3003 3004
		rw_lock_x_lock_func(&(block->lock), 0, file, line);

		fix_type = MTR_MEMO_PAGE_X_FIX;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3005
		break;
3006 3007 3008 3009
	}

	mtr_memo_push(mtr, block, fix_type);

Sergei Golubchik's avatar
Sergei Golubchik committed
3010
	if (UNIV_LIKELY(mode != BUF_PEEK_IF_IN_POOL) && !access_time) {
3011 3012 3013
		/* In the case of a first access, try to apply linear
		read-ahead */

Sergei Golubchik's avatar
Sergei Golubchik committed
3014 3015
		buf_read_ahead_linear(space, zip_size, offset,
				      ibuf_inside(mtr), trx);
3016 3017 3018 3019 3020 3021
	}

#ifdef UNIV_IBUF_COUNT_DEBUG
	ut_a(ibuf_count_get(buf_block_get_space(block),
			    buf_block_get_page_no(block)) == 0);
#endif
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3022 3023 3024 3025
	if (innobase_get_slow_log()) {
		_increment_page_get_statistics(block, trx);
	}

3026 3027 3028
	return(block);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3029
/********************************************************************//**
3030
This is the general function used to get optimistic access to a database
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3031 3032
page.
@return	TRUE if success */
3033 3034
UNIV_INTERN
ibool
3035 3036
buf_page_optimistic_get(
/*====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3037 3038 3039
	ulint		rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH */
	buf_block_t*	block,	/*!< in: guessed buffer block */
	ib_uint64_t	modify_clock,/*!< in: modify clock value if mode is
3040
				..._GUESS_ON_CLOCK */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3041 3042 3043
	const char*	file,	/*!< in: file name */
	ulint		line,	/*!< in: line where called */
	mtr_t*		mtr)	/*!< in: mini-transaction */
3044
{
Sergei Golubchik's avatar
Sergei Golubchik committed
3045
	buf_pool_t*	buf_pool;
3046
	unsigned	access_time;
3047 3048
	ibool		success;
	ulint		fix_type;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3049
	trx_t*		trx = NULL;
3050

3051 3052 3053
	ut_ad(block);
	ut_ad(mtr);
	ut_ad(mtr->state == MTR_ACTIVE);
3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068
	ut_ad((rw_latch == RW_S_LATCH) || (rw_latch == RW_X_LATCH));

	mutex_enter(&block->mutex);

	if (UNIV_UNLIKELY(buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE)) {

		mutex_exit(&block->mutex);

		return(FALSE);
	}

	buf_block_buf_fix_inc(block, file, line);

	mutex_exit(&block->mutex);

3069 3070 3071 3072
	/* Check if this is the first access to the page.
	We do a dirty read on purpose, to avoid mutex contention.
	This field is only used for heuristic purposes; it does not
	affect correctness. */
3073

3074 3075
	access_time = buf_page_is_accessed(&block->page);
	buf_page_set_accessed_make_young(&block->page, access_time);
3076

Sergei Golubchik's avatar
Sergei Golubchik committed
3077
	ut_ad(!ibuf_inside(mtr)
3078 3079
	      || ibuf_page(buf_block_get_space(block),
			   buf_block_get_zip_size(block),
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3080
			   buf_block_get_page_no(block), NULL));
3081 3082

	if (rw_latch == RW_S_LATCH) {
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3083 3084
		success = rw_lock_s_lock_nowait(&(block->lock),
						file, line);
3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101
		fix_type = MTR_MEMO_PAGE_S_FIX;
	} else {
		success = rw_lock_x_lock_func_nowait(&(block->lock),
						     file, line);
		fix_type = MTR_MEMO_PAGE_X_FIX;
	}

	if (UNIV_UNLIKELY(!success)) {
		mutex_enter(&block->mutex);
		buf_block_buf_fix_dec(block);
		mutex_exit(&block->mutex);

		return(FALSE);
	}

	if (UNIV_UNLIKELY(modify_clock != block->modify_clock)) {
		buf_block_dbg_add_level(block, SYNC_NO_ORDER_CHECK);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3102

3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
		if (rw_latch == RW_S_LATCH) {
			rw_lock_s_unlock(&(block->lock));
		} else {
			rw_lock_x_unlock(&(block->lock));
		}

		mutex_enter(&block->mutex);
		buf_block_buf_fix_dec(block);
		mutex_exit(&block->mutex);

		return(FALSE);
	}

	mtr_memo_push(mtr, block, fix_type);

#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
	ut_a(++buf_dbg_counter % 5771 || buf_validate());
	ut_a(block->page.buf_fix_count > 0);
	ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */

3124
#if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
3125 3126
	ut_a(block->page.file_page_was_freed == FALSE);
#endif
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3127 3128 3129 3130
	if (innobase_get_slow_log()) {
		trx = innobase_get_trx();
	}

3131
	if (UNIV_UNLIKELY(!access_time)) {
3132 3133 3134 3135 3136
		/* In the case of a first access, try to apply linear
		read-ahead */

		buf_read_ahead_linear(buf_block_get_space(block),
				      buf_block_get_zip_size(block),
Sergei Golubchik's avatar
Sergei Golubchik committed
3137 3138
				      buf_block_get_page_no(block),
				      ibuf_inside(mtr), trx);
3139 3140 3141 3142 3143 3144
	}

#ifdef UNIV_IBUF_COUNT_DEBUG
	ut_a(ibuf_count_get(buf_block_get_space(block),
			    buf_block_get_page_no(block)) == 0);
#endif
Sergei Golubchik's avatar
Sergei Golubchik committed
3145
	buf_pool = buf_pool_from_block(block);
3146
	buf_pool->stat.n_page_gets++;
3147

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3148 3149 3150
	if (innobase_get_slow_log()) {
		_increment_page_get_statistics(block, trx);
	}
3151 3152 3153
	return(TRUE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3154
/********************************************************************//**
3155 3156
This is used to get access to a known database page, when no waiting can be
done. For example, if a search in an adaptive hash index leads us to this
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3157 3158
frame.
@return	TRUE if success */
3159 3160 3161 3162
UNIV_INTERN
ibool
buf_page_get_known_nowait(
/*======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3163 3164 3165 3166 3167 3168
	ulint		rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH */
	buf_block_t*	block,	/*!< in: the known page */
	ulint		mode,	/*!< in: BUF_MAKE_YOUNG or BUF_KEEP_OLD */
	const char*	file,	/*!< in: file name */
	ulint		line,	/*!< in: line where called */
	mtr_t*		mtr)	/*!< in: mini-transaction */
3169
{
Sergei Golubchik's avatar
Sergei Golubchik committed
3170
	buf_pool_t*	buf_pool;
3171 3172
	ibool		success;
	ulint		fix_type;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3173
	trx_t*		trx = NULL;
3174 3175

	ut_ad(mtr);
3176
	ut_ad(mtr->state == MTR_ACTIVE);
3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199
	ut_ad((rw_latch == RW_S_LATCH) || (rw_latch == RW_X_LATCH));

	mutex_enter(&block->mutex);

	if (buf_block_get_state(block) == BUF_BLOCK_REMOVE_HASH) {
		/* Another thread is just freeing the block from the LRU list
		of the buffer pool: do not try to access this page; this
		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. */

		mutex_exit(&block->mutex);

		return(FALSE);
	}

	ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);

	buf_block_buf_fix_inc(block, file, line);

	mutex_exit(&block->mutex);

Sergei Golubchik's avatar
Sergei Golubchik committed
3200 3201
	buf_pool = buf_pool_from_block(block);

3202
	if (mode == BUF_MAKE_YOUNG && buf_page_peek_if_too_old(&block->page)) {
Sergei Golubchik's avatar
Sergei Golubchik committed
3203 3204
		//buf_pool_mutex_enter(buf_pool);
		mutex_enter(&buf_pool->LRU_list_mutex);
3205
		buf_LRU_make_block_young(&block->page);
Sergei Golubchik's avatar
Sergei Golubchik committed
3206 3207
		//buf_pool_mutex_exit(buf_pool);
		mutex_exit(&buf_pool->LRU_list_mutex);
3208 3209 3210 3211 3212 3213 3214
	} else if (!buf_page_is_accessed(&block->page)) {
		/* Above, we do a dirty read on purpose, to avoid
		mutex contention.  The field buf_page_t::access_time
		is only used for heuristic purposes.  Writes to the
		field must be protected by mutex, however. */
		ulint	time_ms = ut_time_ms();

Sergei Golubchik's avatar
Sergei Golubchik committed
3215
		//buf_pool_mutex_enter(buf_pool);
3216 3217
		mutex_enter(&block->mutex);
		buf_page_set_accessed(&block->page, time_ms);
Sergei Golubchik's avatar
Sergei Golubchik committed
3218
		//buf_pool_mutex_exit(buf_pool);
3219
		mutex_exit(&block->mutex);
3220 3221
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
3222
	ut_ad(!ibuf_inside(mtr) || mode == BUF_KEEP_OLD);
3223 3224

	if (rw_latch == RW_S_LATCH) {
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3225 3226
		success = rw_lock_s_lock_nowait(&(block->lock),
						file, line);
3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248
		fix_type = MTR_MEMO_PAGE_S_FIX;
	} else {
		success = rw_lock_x_lock_func_nowait(&(block->lock),
						     file, line);
		fix_type = MTR_MEMO_PAGE_X_FIX;
	}

	if (!success) {
		mutex_enter(&block->mutex);
		buf_block_buf_fix_dec(block);
		mutex_exit(&block->mutex);

		return(FALSE);
	}

	mtr_memo_push(mtr, block, fix_type);

#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
	ut_a(++buf_dbg_counter % 5771 || buf_validate());
	ut_a(block->page.buf_fix_count > 0);
	ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
3249
#if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
3250 3251 3252 3253 3254 3255 3256 3257
	ut_a(block->page.file_page_was_freed == FALSE);
#endif

#ifdef UNIV_IBUF_COUNT_DEBUG
	ut_a((mode == BUF_KEEP_OLD)
	     || (ibuf_count_get(buf_block_get_space(block),
				buf_block_get_page_no(block)) == 0));
#endif
3258
	buf_pool->stat.n_page_gets++;
3259

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3260 3261 3262 3263 3264
	if (innobase_get_slow_log()) {
		trx = innobase_get_trx();
		_increment_page_get_statistics(block, trx);
	}

3265 3266 3267
	return(TRUE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3268
/*******************************************************************//**
3269 3270
Given a tablespace id and page number tries to get that page. If the
page is not in the buffer pool it is not loaded and NULL is returned.
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3271 3272
Suitable for using when holding the kernel mutex.
@return	pointer to a page or NULL */
3273 3274 3275 3276
UNIV_INTERN
const buf_block_t*
buf_page_try_get_func(
/*==================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3277 3278 3279 3280 3281
	ulint		space_id,/*!< in: tablespace id */
	ulint		page_no,/*!< in: page number */
	const char*	file,	/*!< in: file name */
	ulint		line,	/*!< in: line where called */
	mtr_t*		mtr)	/*!< in: mini-transaction */
3282 3283 3284 3285
{
	buf_block_t*	block;
	ibool		success;
	ulint		fix_type;
Sergei Golubchik's avatar
Sergei Golubchik committed
3286
	buf_pool_t*	buf_pool = buf_pool_get(space_id, page_no);
3287

3288 3289 3290
	ut_ad(mtr);
	ut_ad(mtr->state == MTR_ACTIVE);

Sergei Golubchik's avatar
Sergei Golubchik committed
3291 3292 3293
	//buf_pool_mutex_enter(buf_pool);
	rw_lock_s_lock(&buf_pool->page_hash_latch);
	block = buf_block_hash_get(buf_pool, space_id, page_no);
3294

Sergei Golubchik's avatar
Sergei Golubchik committed
3295 3296 3297
	if (!block || buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE) {
		//buf_pool_mutex_exit(buf_pool);
		rw_lock_s_unlock(&buf_pool->page_hash_latch);
3298 3299 3300
		return(NULL);
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
3301 3302
	ut_ad(!buf_pool_watch_is_sentinel(buf_pool, &block->page));

3303
	mutex_enter(&block->mutex);
Sergei Golubchik's avatar
Sergei Golubchik committed
3304 3305
	//buf_pool_mutex_exit(buf_pool);
	rw_lock_s_unlock(&buf_pool->page_hash_latch);
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316

#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
	ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
	ut_a(buf_block_get_space(block) == space_id);
	ut_a(buf_block_get_page_no(block) == page_no);
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */

	buf_block_buf_fix_inc(block, file, line);
	mutex_exit(&block->mutex);

	fix_type = MTR_MEMO_PAGE_S_FIX;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3317
	success = rw_lock_s_lock_nowait(&block->lock, file, line);
3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342

	if (!success) {
		/* Let us try to get an X-latch. If the current thread
		is holding an X-latch on the page, we cannot get an
		S-latch. */

		fix_type = MTR_MEMO_PAGE_X_FIX;
		success = rw_lock_x_lock_func_nowait(&block->lock,
						     file, line);
	}

	if (!success) {
		mutex_enter(&block->mutex);
		buf_block_buf_fix_dec(block);
		mutex_exit(&block->mutex);

		return(NULL);
	}

	mtr_memo_push(mtr, block, fix_type);
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
	ut_a(++buf_dbg_counter % 5771 || buf_validate());
	ut_a(block->page.buf_fix_count > 0);
	ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
3343
#if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
3344
	ut_a(block->page.file_page_was_freed == FALSE);
3345
#endif /* UNIV_DEBUG_FILE_ACCESSES || UNIV_DEBUG */
3346
	buf_block_dbg_add_level(block, SYNC_NO_ORDER_CHECK);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3347

3348
	buf_pool->stat.n_page_gets++;
3349

Vadim Tkachenko's avatar
Vadim Tkachenko committed
3350 3351 3352 3353 3354
#ifdef UNIV_IBUF_COUNT_DEBUG
	ut_a(ibuf_count_get(buf_block_get_space(block),
			    buf_block_get_page_no(block)) == 0);
#endif

3355 3356 3357
	return(block);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3358
/********************************************************************//**
3359 3360 3361 3362 3363
Initialize some fields of a control block. */
UNIV_INLINE
void
buf_page_init_low(
/*==============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3364
	buf_page_t*	bpage)	/*!< in: block to init */
3365 3366 3367 3368 3369
{
	bpage->flush_type = BUF_FLUSH_LRU;
	bpage->io_fix = BUF_IO_NONE;
	bpage->buf_fix_count = 0;
	bpage->freed_page_clock = 0;
3370
	bpage->access_time = 0;
3371 3372 3373
	bpage->newest_modification = 0;
	bpage->oldest_modification = 0;
	HASH_INVALIDATE(bpage, hash);
3374
	bpage->is_corrupt = FALSE;
3375
#if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
3376
	bpage->file_page_was_freed = FALSE;
3377
#endif /* UNIV_DEBUG_FILE_ACCESSES || UNIV_DEBUG */
3378 3379
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3380
/********************************************************************//**
3381
Inits a page to the buffer buf_pool. */
3382
static __attribute__((nonnull))
3383 3384 3385
void
buf_page_init(
/*==========*/
3386
	buf_pool_t*	buf_pool,/*!< in/out: buffer pool */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3387 3388
	ulint		space,	/*!< in: space id */
	ulint		offset,	/*!< in: offset of the page within space
3389
				in units of a page */
Sergei Golubchik's avatar
Sergei Golubchik committed
3390
	ulint		fold,	/*!< in: buf_page_address_fold(space,offset) */
3391
	buf_block_t*	block)	/*!< in/out: block to init */
3392 3393 3394
{
	buf_page_t*	hash_page;

3395
	ut_ad(buf_pool == buf_pool_get(space, offset));
Sergei Golubchik's avatar
Sergei Golubchik committed
3396
	//ut_ad(buf_pool_mutex_own(buf_pool));
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3397
#ifdef UNIV_SYNC_DEBUG
Sergei Golubchik's avatar
Sergei Golubchik committed
3398
	ut_ad(rw_lock_own(&buf_pool->page_hash_latch, RW_LOCK_EX));
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3399
#endif
3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416
	ut_ad(mutex_own(&(block->mutex)));
	ut_a(buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE);

	/* Set the state of the block */
	buf_block_set_file_page(block, space, offset);

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

	buf_block_init_low(block);

Sergei Golubchik's avatar
Sergei Golubchik committed
3417 3418 3419
	block->lock_hash_val = lock_rec_hash(space, offset);

	buf_page_init_low(&block->page);
3420 3421 3422

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

Sergei Golubchik's avatar
Sergei Golubchik committed
3423
	hash_page = buf_page_hash_get_low(buf_pool, space, offset, fold);
3424

Sergei Golubchik's avatar
Sergei Golubchik committed
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436
	if (UNIV_LIKELY(!hash_page)) {
	} else if (buf_pool_watch_is_sentinel(buf_pool, hash_page)) {
		/* Preserve the reference count. */
		ulint	buf_fix_count;

		mutex_enter(&buf_pool->zip_mutex);
		buf_fix_count = hash_page->buf_fix_count;
		ut_a(buf_fix_count > 0);
		block->page.buf_fix_count += buf_fix_count;
		buf_pool_watch_remove(buf_pool, fold, hash_page);
		mutex_exit(&buf_pool->zip_mutex);
	} else {
3437 3438 3439 3440 3441 3442 3443 3444
		fprintf(stderr,
			"InnoDB: Error: page %lu %lu already found"
			" in the hash table: %p, %p\n",
			(ulong) space,
			(ulong) offset,
			(const void*) hash_page, (const void*) block);
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
		mutex_exit(&block->mutex);
Sergei Golubchik's avatar
Sergei Golubchik committed
3445 3446
		//buf_pool_mutex_exit(buf_pool);
		rw_lock_x_unlock(&buf_pool->page_hash_latch);
3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458
		buf_print();
		buf_LRU_print();
		buf_validate();
		buf_LRU_validate();
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
		ut_error;
	}

	ut_ad(!block->page.in_zip_hash);
	ut_ad(!block->page.in_page_hash);
	ut_d(block->page.in_page_hash = TRUE);
	HASH_INSERT(buf_page_t, hash, buf_pool->page_hash,
Sergei Golubchik's avatar
Sergei Golubchik committed
3459
		    fold, &block->page);
3460 3461
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3462
/********************************************************************//**
3463 3464 3465 3466 3467 3468 3469
Function which inits a page for read to the buffer buf_pool. If the page is
(1) already in buf_pool, or
(2) if we specify to read only ibuf pages and the page is not an ibuf page, or
(3) if the space is deleted or being deleted,
then this function does nothing.
Sets the io_fix flag to BUF_IO_READ and sets a non-recursive exclusive lock
on the buffer frame. The io-handler must take care that the flag is cleared
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3470 3471
and the lock released later.
@return	pointer to the block or NULL */
3472 3473 3474 3475
UNIV_INTERN
buf_page_t*
buf_page_init_for_read(
/*===================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3476 3477 3478 3479 3480
	ulint*		err,	/*!< out: DB_SUCCESS or DB_TABLESPACE_DELETED */
	ulint		mode,	/*!< in: BUF_READ_IBUF_PAGES_ONLY, ... */
	ulint		space,	/*!< in: space id */
	ulint		zip_size,/*!< in: compressed page size, or 0 */
	ibool		unzip,	/*!< in: TRUE=request uncompressed page */
Sergei Golubchik's avatar
Sergei Golubchik committed
3481 3482
	ib_int64_t	tablespace_version,
				/*!< in: prevents reading from a wrong
3483 3484
				version of the tablespace in case we have done
				DISCARD + IMPORT */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3485
	ulint		offset)	/*!< in: page number */
3486 3487
{
	buf_block_t*	block;
Sergei Golubchik's avatar
Sergei Golubchik committed
3488 3489
	buf_page_t*	bpage	= NULL;
	buf_page_t*	watch_page;
3490
	mtr_t		mtr;
Sergei Golubchik's avatar
Sergei Golubchik committed
3491
	ulint		fold;
3492 3493
	ibool		lru	= FALSE;
	void*		data;
Sergei Golubchik's avatar
Sergei Golubchik committed
3494
	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
3495 3496 3497 3498 3499 3500 3501 3502 3503 3504

	ut_ad(buf_pool);

	*err = DB_SUCCESS;

	if (mode == BUF_READ_IBUF_PAGES_ONLY) {
		/* It is a read-ahead within an ibuf routine */

		ut_ad(!ibuf_bitmap_page(zip_size, offset));

Sergei Golubchik's avatar
Sergei Golubchik committed
3505
		ibuf_mtr_start(&mtr);
3506

Vadim Tkachenko's avatar
Vadim Tkachenko committed
3507 3508
		if (!recv_no_ibuf_operations
		    && !ibuf_page(space, zip_size, offset, &mtr)) {
3509

Sergei Golubchik's avatar
Sergei Golubchik committed
3510
			ibuf_mtr_commit(&mtr);
3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521

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

	if (zip_size && UNIV_LIKELY(!unzip)
	    && UNIV_LIKELY(!recv_recovery_is_on())) {
		block = NULL;
	} else {
Sergei Golubchik's avatar
Sergei Golubchik committed
3522
		block = buf_LRU_get_free_block(buf_pool);
3523
		ut_ad(block);
Sergei Golubchik's avatar
Sergei Golubchik committed
3524
		ut_ad(buf_pool_from_block(block) == buf_pool);
3525 3526
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
3527 3528
	fold = buf_page_address_fold(space, offset);

3529
retry:
Sergei Golubchik's avatar
Sergei Golubchik committed
3530 3531 3532
	//buf_pool_mutex_enter(buf_pool);
	mutex_enter(&buf_pool->LRU_list_mutex);
	rw_lock_x_lock(&buf_pool->page_hash_latch);
3533

Sergei Golubchik's avatar
Sergei Golubchik committed
3534
	watch_page = buf_page_hash_get_low(buf_pool, space, offset, fold);
3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550

	if (UNIV_UNLIKELY(watch_page && watch_page->space_was_being_deleted)) {
		mutex_t*	block_mutex = buf_page_get_mutex_enter(watch_page);

		/* This page is obsoleted, should discard and retry */
		rw_lock_x_unlock(&buf_pool->page_hash_latch);
		ut_a(block_mutex);

		buf_LRU_free_block(watch_page, TRUE, TRUE);

		mutex_exit(&buf_pool->LRU_list_mutex);
		mutex_exit(block_mutex);

		goto retry;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
3551
	if (watch_page && !buf_pool_watch_is_sentinel(buf_pool, watch_page)) {
3552
		/* The page is already in the buffer pool. */
Sergei Golubchik's avatar
Sergei Golubchik committed
3553
		watch_page = NULL;
3554 3555 3556
err_exit:
		if (block) {
			mutex_enter(&block->mutex);
Sergei Golubchik's avatar
Sergei Golubchik committed
3557 3558
			mutex_exit(&buf_pool->LRU_list_mutex);
			rw_lock_x_unlock(&buf_pool->page_hash_latch);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3559
			buf_LRU_block_free_non_file_page(block, FALSE);
3560 3561
			mutex_exit(&block->mutex);
		}
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3562
		else {
Sergei Golubchik's avatar
Sergei Golubchik committed
3563 3564
			mutex_exit(&buf_pool->LRU_list_mutex);
			rw_lock_x_unlock(&buf_pool->page_hash_latch);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3565
		}
3566

Vadim Tkachenko's avatar
Vadim Tkachenko committed
3567 3568
		bpage = NULL;
		goto func_exit;
3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583
	}

	if (fil_tablespace_deleted_or_being_deleted_in_mem(
		    space, tablespace_version)) {
		/* The page belongs to a space which has been
		deleted or is being deleted. */
		*err = DB_TABLESPACE_DELETED;

		goto err_exit;
	}

	if (block) {
		bpage = &block->page;
		mutex_enter(&block->mutex);

Sergei Golubchik's avatar
Sergei Golubchik committed
3584 3585
		ut_ad(buf_pool_from_bpage(bpage) == buf_pool);

3586
		buf_page_init(buf_pool, space, offset, fold, block);
Sergei Golubchik's avatar
Sergei Golubchik committed
3587 3588

		rw_lock_x_unlock(&buf_pool->page_hash_latch);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3589

3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607
		/* The block must be put to the LRU list, to the old blocks */
		buf_LRU_add_block(bpage, TRUE/* to old blocks */);

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

		rw_lock_x_lock_gen(&block->lock, BUF_IO_READ);
		buf_page_set_io_fix(bpage, BUF_IO_READ);

		if (UNIV_UNLIKELY(zip_size)) {
			page_zip_set_size(&block->page.zip, zip_size);

Sergei Golubchik's avatar
Sergei Golubchik committed
3608
			/* buf_pool->mutex may be released and
3609 3610 3611
			reacquired by buf_buddy_alloc().  Thus, we
			must release block->mutex in order not to
			break the latching order in the reacquisition
Sergei Golubchik's avatar
Sergei Golubchik committed
3612
			of buf_pool->mutex.  We also must defer this
3613 3614 3615
			operation until after the block descriptor has
			been added to buf_pool->LRU and
			buf_pool->page_hash. */
3616
			mutex_exit(&block->mutex);
Sergei Golubchik's avatar
Sergei Golubchik committed
3617
			data = buf_buddy_alloc(buf_pool, zip_size, &lru, FALSE);
3618
			mutex_enter(&block->mutex);
3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629
			block->page.zip.data = data;

			/* To maintain the invariant
			block->in_unzip_LRU_list
			== buf_page_belongs_to_unzip_LRU(&block->page)
			we have to add this block to unzip_LRU
			after block->page.zip.data is set. */
			ut_ad(buf_page_belongs_to_unzip_LRU(&block->page));
			buf_unzip_LRU_add_block(block, TRUE);
		}

Sergei Golubchik's avatar
Sergei Golubchik committed
3630
		mutex_exit(&buf_pool->LRU_list_mutex);
3631 3632 3633 3634 3635 3636
		mutex_exit(&block->mutex);
	} else {
		/* The compressed page must be allocated before the
		control block (bpage), in order to avoid the
		invocation of buf_buddy_relocate_block() on
		uninitialized data. */
Sergei Golubchik's avatar
Sergei Golubchik committed
3637
		data = buf_buddy_alloc(buf_pool, zip_size, &lru, TRUE);
3638 3639

		/* If buf_buddy_alloc() allocated storage from the LRU list,
Sergei Golubchik's avatar
Sergei Golubchik committed
3640
		it released and reacquired buf_pool->mutex.  Thus, we must
3641
		check the page_hash again, as it may have been modified. */
Sergei Golubchik's avatar
Sergei Golubchik committed
3642
		if (UNIV_UNLIKELY(lru)) {
3643

Sergei Golubchik's avatar
Sergei Golubchik committed
3644 3645
			watch_page = buf_page_hash_get_low(
				buf_pool, space, offset, fold);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3646

Sergei Golubchik's avatar
Sergei Golubchik committed
3647 3648 3649
			if (watch_page
			    && !buf_pool_watch_is_sentinel(buf_pool,
				   			   watch_page)) {
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3650

Sergei Golubchik's avatar
Sergei Golubchik committed
3651 3652 3653 3654 3655 3656 3657 3658 3659 3660
				/* The block was added by some other thread. */
				watch_page = NULL;
				buf_buddy_free(buf_pool, data, zip_size, TRUE);

				mutex_exit(&buf_pool->LRU_list_mutex);
				rw_lock_x_unlock(&buf_pool->page_hash_latch);

				bpage = NULL;
				goto func_exit;
			}
3661
		}
3662

3663 3664 3665 3666 3667
		bpage = buf_page_alloc_descriptor();

		/* Initialize the buf_pool pointer. */
		bpage->buf_pool_index = buf_pool_index(buf_pool);

3668 3669 3670 3671
		page_zip_des_init(&bpage->zip);
		page_zip_set_size(&bpage->zip, zip_size);
		bpage->zip.data = data;

Sergei Golubchik's avatar
Sergei Golubchik committed
3672
		mutex_enter(&buf_pool->zip_mutex);
3673 3674
		UNIV_MEM_DESC(bpage->zip.data,
			      page_zip_get_size(&bpage->zip), bpage);
Sergei Golubchik's avatar
Sergei Golubchik committed
3675

3676
		buf_page_init_low(bpage);
Sergei Golubchik's avatar
Sergei Golubchik committed
3677

3678 3679 3680
		bpage->state	= BUF_BLOCK_ZIP_PAGE;
		bpage->space	= space;
		bpage->offset	= offset;
3681
		bpage->space_was_being_deleted = FALSE;
3682 3683 3684 3685 3686 3687

#ifdef UNIV_DEBUG
		bpage->in_page_hash = FALSE;
		bpage->in_zip_hash = FALSE;
		bpage->in_flush_list = FALSE;
		bpage->in_free_list = FALSE;
3688
#endif /* UNIV_DEBUG */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3689
		bpage->in_LRU_list = FALSE;
3690 3691 3692

		ut_d(bpage->in_page_hash = TRUE);

Sergei Golubchik's avatar
Sergei Golubchik committed
3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705
		if (UNIV_LIKELY_NULL(watch_page)) {
			/* Preserve the reference count. */
			ulint	buf_fix_count = watch_page->buf_fix_count;
			ut_a(buf_fix_count > 0);
			bpage->buf_fix_count += buf_fix_count;
			ut_ad(buf_pool_watch_is_sentinel(buf_pool, watch_page));
			buf_pool_watch_remove(buf_pool, fold, watch_page);
		}

		HASH_INSERT(buf_page_t, hash, buf_pool->page_hash, fold,
			    bpage);

		rw_lock_x_unlock(&buf_pool->page_hash_latch);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3706

3707 3708
		/* The block must be put to the LRU list, to the old blocks */
		buf_LRU_add_block(bpage, TRUE/* to old blocks */);
3709
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
3710
		buf_LRU_insert_zip_clean(bpage);
3711
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3712

Sergei Golubchik's avatar
Sergei Golubchik committed
3713
		mutex_exit(&buf_pool->LRU_list_mutex);
3714 3715 3716

		buf_page_set_io_fix(bpage, BUF_IO_READ);

Sergei Golubchik's avatar
Sergei Golubchik committed
3717
		mutex_exit(&buf_pool->zip_mutex);
3718 3719
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
3720
	buf_pool_mutex_enter(buf_pool);
3721
	buf_pool->n_pend_reads++;
Sergei Golubchik's avatar
Sergei Golubchik committed
3722
	buf_pool_mutex_exit(buf_pool);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3723
func_exit:
Sergei Golubchik's avatar
Sergei Golubchik committed
3724
	//buf_pool_mutex_exit(buf_pool);
3725 3726 3727

	if (mode == BUF_READ_IBUF_PAGES_ONLY) {

Sergei Golubchik's avatar
Sergei Golubchik committed
3728
		ibuf_mtr_commit(&mtr);
3729 3730
	}

Vadim Tkachenko's avatar
Vadim Tkachenko committed
3731
	ut_ad(!bpage || buf_page_in_file(bpage));
3732 3733 3734
	return(bpage);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3735
/********************************************************************//**
3736 3737 3738
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 =>
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3739 3740
FILE_PAGE (the other is buf_page_get_gen).
@return	pointer to the block, page bufferfixed */
3741 3742 3743 3744
UNIV_INTERN
buf_block_t*
buf_page_create(
/*============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3745 3746
	ulint	space,	/*!< in: space id */
	ulint	offset,	/*!< in: offset of the page within space in units of
3747
			a page */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3748 3749
	ulint	zip_size,/*!< in: compressed page size, or 0 */
	mtr_t*	mtr)	/*!< in: mini-transaction handle */
3750 3751 3752
{
	buf_frame_t*	frame;
	buf_block_t*	block;
Sergei Golubchik's avatar
Sergei Golubchik committed
3753
	ulint		fold;
3754
	buf_block_t*	free_block	= NULL;
3755
	ulint		time_ms		= ut_time_ms();
Sergei Golubchik's avatar
Sergei Golubchik committed
3756
	buf_pool_t*	buf_pool 	= buf_pool_get(space, offset);
3757 3758

	ut_ad(mtr);
3759
	ut_ad(mtr->state == MTR_ACTIVE);
3760 3761
	ut_ad(space || !zip_size);

Sergei Golubchik's avatar
Sergei Golubchik committed
3762 3763 3764
	free_block = buf_LRU_get_free_block(buf_pool);

	fold = buf_page_address_fold(space, offset);
3765

3766
retry:
Sergei Golubchik's avatar
Sergei Golubchik committed
3767 3768 3769
	//buf_pool_mutex_enter(buf_pool);
	mutex_enter(&buf_pool->LRU_list_mutex);
	rw_lock_x_lock(&buf_pool->page_hash_latch);
3770

Sergei Golubchik's avatar
Sergei Golubchik committed
3771 3772
	block = (buf_block_t*) buf_page_hash_get_low(
		buf_pool, space, offset, fold);
3773

3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788
	if (UNIV_UNLIKELY(block && block->page.space_was_being_deleted)) {
		mutex_t*	block_mutex = buf_page_get_mutex_enter((buf_page_t*)block);

		/* This page is obsoleted, should discard and retry */
		rw_lock_x_unlock(&buf_pool->page_hash_latch);
		ut_a(block_mutex);

		buf_LRU_free_block((buf_page_t*)block, TRUE, TRUE);

		mutex_exit(&buf_pool->LRU_list_mutex);
		mutex_exit(block_mutex);

		goto retry;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
3789 3790 3791
	if (block
	    && buf_page_in_file(&block->page)
	    && !buf_pool_watch_is_sentinel(buf_pool, &block->page)) {
3792 3793 3794
#ifdef UNIV_IBUF_COUNT_DEBUG
		ut_a(ibuf_count_get(space, offset) == 0);
#endif
3795
#if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
3796
		block->page.file_page_was_freed = FALSE;
3797
#endif /* UNIV_DEBUG_FILE_ACCESSES || UNIV_DEBUG */
3798 3799

		/* Page can be found in buf_pool */
Sergei Golubchik's avatar
Sergei Golubchik committed
3800 3801 3802
		//buf_pool_mutex_exit(buf_pool);
		mutex_exit(&buf_pool->LRU_list_mutex);
		rw_lock_x_unlock(&buf_pool->page_hash_latch);
3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822

		buf_block_free(free_block);

		return(buf_page_get_with_no_latch(space, zip_size,
						  offset, mtr));
	}

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

#ifdef UNIV_DEBUG
	if (buf_debug_prints) {
		fprintf(stderr, "Creating space %lu page %lu to buffer\n",
			(ulong) space, (ulong) offset);
	}
#endif /* UNIV_DEBUG */

	block = free_block;

	mutex_enter(&block->mutex);

3823
	buf_page_init(buf_pool, space, offset, fold, block);
Sergei Golubchik's avatar
Sergei Golubchik committed
3824
	rw_lock_x_unlock(&buf_pool->page_hash_latch);
3825 3826 3827 3828 3829

	/* The block must be put to the LRU list */
	buf_LRU_add_block(&block->page, FALSE);

	buf_block_buf_fix_inc(block, __FILE__, __LINE__);
3830
	buf_pool->stat.n_pages_created++;
3831 3832 3833 3834 3835 3836

	if (zip_size) {
		void*	data;
		ibool	lru;

		/* Prevent race conditions during buf_buddy_alloc(),
Sergei Golubchik's avatar
Sergei Golubchik committed
3837
		which may release and reacquire buf_pool->mutex,
3838 3839 3840 3841 3842 3843
		by IO-fixing and X-latching the block. */

		buf_page_set_io_fix(&block->page, BUF_IO_READ);
		rw_lock_x_lock(&block->lock);

		page_zip_set_size(&block->page.zip, zip_size);
3844
		mutex_exit(&block->mutex);
Sergei Golubchik's avatar
Sergei Golubchik committed
3845
		/* buf_pool->mutex may be released and reacquired by
3846 3847
		buf_buddy_alloc().  Thus, we must release block->mutex
		in order not to break the latching order in
Sergei Golubchik's avatar
Sergei Golubchik committed
3848
		the reacquisition of buf_pool->mutex.  We also must
3849 3850
		defer this operation until after the block descriptor
		has been added to buf_pool->LRU and buf_pool->page_hash. */
Sergei Golubchik's avatar
Sergei Golubchik committed
3851
		data = buf_buddy_alloc(buf_pool, zip_size, &lru, FALSE);
3852
		mutex_enter(&block->mutex);
3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866
		block->page.zip.data = data;

		/* To maintain the invariant
		block->in_unzip_LRU_list
		== buf_page_belongs_to_unzip_LRU(&block->page)
		we have to add this block to unzip_LRU after
		block->page.zip.data is set. */
		ut_ad(buf_page_belongs_to_unzip_LRU(&block->page));
		buf_unzip_LRU_add_block(block, FALSE);

		buf_page_set_io_fix(&block->page, BUF_IO_NONE);
		rw_lock_x_unlock(&block->lock);
	}

3867 3868
	buf_page_set_accessed(&block->page, time_ms);

Sergei Golubchik's avatar
Sergei Golubchik committed
3869 3870
	//buf_pool_mutex_exit(buf_pool);
	mutex_exit(&buf_pool->LRU_list_mutex);
3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881

	mtr_memo_push(mtr, block, MTR_MEMO_BUF_FIX);

	mutex_exit(&block->mutex);

	/* Delete possible entries for the page from the insert buffer:
	such can exist if the page belonged to an index which was dropped */

	ibuf_merge_or_delete_for_page(NULL, space, offset, zip_size, TRUE);

	/* Flush pages from the end of the LRU list if necessary */
Sergei Golubchik's avatar
Sergei Golubchik committed
3882
	buf_flush_free_margin(buf_pool, FALSE);
3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907

	frame = block->frame;

	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);

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

#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
	ut_a(++buf_dbg_counter % 357 || buf_validate());
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
#ifdef UNIV_IBUF_COUNT_DEBUG
	ut_a(ibuf_count_get(buf_block_get_space(block),
			    buf_block_get_page_no(block)) == 0);
#endif
	return(block);
}

3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960
/********************************************************************//**
Mark a table with the specified space pointed by bpage->space corrupted.
Also remove the bpage from LRU list.
@return TRUE if successful */
static
ibool
buf_mark_space_corrupt(
/*===================*/
	buf_page_t*	bpage)	/*!< in: pointer to the block in question */
{
	buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);
	const ibool	uncompressed = (buf_page_get_state(bpage)
					== BUF_BLOCK_FILE_PAGE);
	ulint		space = bpage->space;
	ibool		ret = TRUE;

	/* First unfix and release lock on the bpage */
	//buf_pool_mutex_enter(buf_pool);
	mutex_enter(&buf_pool->LRU_list_mutex);
	rw_lock_x_lock(&buf_pool->page_hash_latch);
	mutex_enter(buf_page_get_mutex(bpage));
	ut_ad(buf_page_get_io_fix(bpage) == BUF_IO_READ);
	ut_ad(bpage->buf_fix_count == 0);

	/* Set BUF_IO_NONE before we remove the block from LRU list */
	buf_page_set_io_fix(bpage, BUF_IO_NONE);

	if (uncompressed) {
		rw_lock_x_unlock_gen(
			&((buf_block_t*) bpage)->lock,
			BUF_IO_READ);
	}

	/* Find the table with specified space id, and mark it corrupted */
	if (dict_set_corrupted_by_space(space)) {
		buf_LRU_free_one_page(bpage);
	} else {
		ret = FALSE;
	}

	buf_pool_mutex_enter(buf_pool);
	ut_ad(buf_pool->n_pend_reads > 0);
	buf_pool->n_pend_reads--;
	buf_pool_mutex_exit(buf_pool);

	mutex_exit(buf_page_get_mutex(bpage));
	//buf_pool_mutex_exit(buf_pool);
	mutex_exit(&buf_pool->LRU_list_mutex);
	rw_lock_x_unlock(&buf_pool->page_hash_latch);

	return(ret);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3961
/********************************************************************//**
3962 3963 3964 3965 3966 3967
Completes an asynchronous read or write request of a file page to or from
the buffer pool. */
UNIV_INTERN
void
buf_page_io_complete(
/*=================*/
Sergei Golubchik's avatar
Sergei Golubchik committed
3968
	buf_page_t*	bpage)	/*!< in: pointer to the block in question */
3969 3970
{
	enum buf_io_fix	io_type;
Sergei Golubchik's avatar
Sergei Golubchik committed
3971
	buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);
3972 3973
	const ibool	uncompressed = (buf_page_get_state(bpage)
					== BUF_BLOCK_FILE_PAGE);
Sergei Golubchik's avatar
Sergei Golubchik committed
3974
	ibool		have_LRU_mutex = FALSE;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3975
	mutex_t*	block_mutex;
3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015

	ut_a(buf_page_in_file(bpage));

	/* We do not need protect io_fix here by 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. */

	io_type = buf_page_get_io_fix(bpage);
	ut_ad(io_type == BUF_IO_READ || io_type == BUF_IO_WRITE);

	if (io_type == BUF_IO_READ) {
		ulint	read_page_no;
		ulint	read_space_id;
		byte*	frame;

		if (buf_page_get_zip_size(bpage)) {
			frame = bpage->zip.data;
			buf_pool->n_pend_unzip++;
			if (uncompressed
			    && !buf_zip_decompress((buf_block_t*) bpage,
						   FALSE)) {

				buf_pool->n_pend_unzip--;
				goto corrupt;
			}
			buf_pool->n_pend_unzip--;
		} else {
			ut_a(uncompressed);
			frame = ((buf_block_t*) bpage)->frame;
		}

		/* If this page is not uninitialized and not in the
		doublewrite buffer, then the page number and space id
		should be the same as in block. */
		read_page_no = mach_read_from_4(frame + FIL_PAGE_OFFSET);
		read_space_id = mach_read_from_4(
			frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);

4016 4017
		if ((bpage->space == TRX_SYS_SPACE
		     || (srv_doublewrite_file && bpage->space == TRX_DOUBLEWRITE_SPACE))
4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046
		    && trx_doublewrite_page_inside(bpage->offset)) {

			ut_print_timestamp(stderr);
			fprintf(stderr,
				"  InnoDB: Error: reading page %lu\n"
				"InnoDB: which is in the"
				" doublewrite buffer!\n",
				(ulong) bpage->offset);
		} else if (!read_space_id && !read_page_no) {
			/* This is likely an uninitialized page. */
		} else if ((bpage->space
			    && bpage->space != read_space_id)
			   || bpage->offset != read_page_no) {
			/* We did not compare space_id to read_space_id
			if bpage->space == 0, because the field on the
			page may contain garbage in MySQL < 4.1.1,
			which only supported bpage->space == 0. */

			ut_print_timestamp(stderr);
			fprintf(stderr,
				"  InnoDB: Error: space id and page n:o"
				" stored in the page\n"
				"InnoDB: read in are %lu:%lu,"
				" should be %lu:%lu!\n",
				(ulong) read_space_id, (ulong) read_page_no,
				(ulong) bpage->space,
				(ulong) bpage->offset);
		}

4047
		if (!srv_pass_corrupt_table || !bpage->is_corrupt) {
4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084
		/* From version 3.23.38 up we store the page checksum
		to the 4 first bytes of the page end lsn field */

		if (buf_page_is_corrupted(frame,
					  buf_page_get_zip_size(bpage))) {
corrupt:
			fprintf(stderr,
				"InnoDB: Database page corruption on disk"
				" or a failed\n"
				"InnoDB: file read of page %lu.\n"
				"InnoDB: You may have to recover"
				" from a backup.\n",
				(ulong) bpage->offset);
			buf_page_print(frame, buf_page_get_zip_size(bpage));
			fprintf(stderr,
				"InnoDB: Database page corruption on disk"
				" or a failed\n"
				"InnoDB: file read of page %lu.\n"
				"InnoDB: You may have to recover"
				" from a backup.\n",
				(ulong) bpage->offset);
			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"
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4085
			      "InnoDB: See also "
4086
			      REFMAN "forcing-innodb-recovery.html\n"
4087 4088
			      "InnoDB: about forcing recovery.\n", stderr);

4089
			if (srv_pass_corrupt_table && !trx_sys_sys_space(bpage->space)
4090
			    && bpage->space < SRV_LOG_SPACE_FIRST_ID) {
Sergei Golubchik's avatar
Sergei Golubchik committed
4091 4092
				trx_t*	trx;

4093
				fprintf(stderr,
4094
					"InnoDB: space %u will be treated as corrupt.\n",
4095 4096
					bpage->space);
				fil_space_set_corrupt(bpage->space);
Sergei Golubchik's avatar
Sergei Golubchik committed
4097 4098 4099

				trx = innobase_get_trx();
				if (trx && trx->dict_operation_lock_mode == RW_X_LATCH) {
4100
					dict_table_set_corrupt_by_space(bpage->space, FALSE);
Sergei Golubchik's avatar
Sergei Golubchik committed
4101 4102
				} else {
					dict_table_set_corrupt_by_space(bpage->space, TRUE);
4103 4104 4105
				}
				bpage->is_corrupt = TRUE;
			} else
4106
			if (srv_force_recovery < SRV_FORCE_IGNORE_CORRUPT) {
4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119
				/* If page space id is larger than TRX_SYS_SPACE
				(0), we will attempt to mark the corresponding
				table as corrupted instead of crashing server */
				if (bpage->space > TRX_SYS_SPACE
				    && buf_mark_space_corrupt(bpage)) {
					return;
				} else {
					fputs("InnoDB: Ending processing"
					      " because of"
					      " a corrupt database page.\n",
					      stderr);
					ut_error;
				}
4120 4121
			}
		}
4122
		} /**/
4123 4124 4125 4126

		if (recv_recovery_is_on()) {
			/* Pages must be uncompressed for crash recovery. */
			ut_a(uncompressed);
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4127
			recv_recover_page(TRUE, (buf_block_t*) bpage);
4128 4129 4130 4131
		}

		if (uncompressed && !recv_no_ibuf_operations) {
			ibuf_merge_or_delete_for_page(
4132 4133
				/* Delete possible entries, if bpage is_corrupt */
				(srv_pass_corrupt_table && bpage->is_corrupt) ? NULL :
4134 4135
				(buf_block_t*) bpage, bpage->space,
				bpage->offset, buf_page_get_zip_size(bpage),
4136
				(srv_pass_corrupt_table && bpage->is_corrupt) ? FALSE :
4137 4138 4139 4140
				TRUE);
		}
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4141
	if (io_type == BUF_IO_WRITE
4142 4143 4144 4145 4146
	    && (
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
		buf_page_get_state(bpage) == BUF_BLOCK_ZIP_DIRTY ||
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
		buf_page_get_flush_type(bpage) == BUF_FLUSH_LRU)) {
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4147
		/* to keep consistency at buf_LRU_insert_zip_clean() */
Sergei Golubchik's avatar
Sergei Golubchik committed
4148
		have_LRU_mutex = TRUE; /* optimistic */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4149
	}
Sergei Golubchik's avatar
Sergei Golubchik committed
4150 4151 4152
retry_mutex:
	if (have_LRU_mutex)
		mutex_enter(&buf_pool->LRU_list_mutex);
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4153 4154
	block_mutex = buf_page_get_mutex_enter(bpage);
	ut_a(block_mutex);
Sergei Golubchik's avatar
Sergei Golubchik committed
4155 4156 4157 4158 4159 4160 4161 4162 4163
	if (io_type == BUF_IO_WRITE
	    && (buf_page_get_state(bpage) == BUF_BLOCK_ZIP_DIRTY
		|| buf_page_get_flush_type(bpage) == BUF_FLUSH_LRU)
	    && !have_LRU_mutex) {
		mutex_exit(block_mutex);
		have_LRU_mutex = TRUE;
		goto retry_mutex;
	}
	buf_pool_mutex_enter(buf_pool);
4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185

#ifdef UNIV_IBUF_COUNT_DEBUG
	if (io_type == BUF_IO_WRITE || uncompressed) {
		/* For BUF_IO_READ of compressed-only blocks, the
		buffered operations will be merged by buf_page_get_gen()
		after the block has been uncompressed. */
		ut_a(ibuf_count_get(bpage->space, bpage->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. */

	buf_page_set_io_fix(bpage, BUF_IO_NONE);

	switch (io_type) {
	case 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
		debugging! */

Sergei Golubchik's avatar
Sergei Golubchik committed
4186
		ut_a(!have_LRU_mutex);
4187 4188
		ut_ad(buf_pool->n_pend_reads > 0);
		buf_pool->n_pend_reads--;
4189
		buf_pool->stat.n_pages_read++;
4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203

		if (uncompressed) {
			rw_lock_x_unlock_gen(&((buf_block_t*) bpage)->lock,
					     BUF_IO_READ);
		}

		break;

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

		buf_flush_write_complete(bpage);

Sergei Golubchik's avatar
Sergei Golubchik committed
4204 4205
		if (have_LRU_mutex)
			mutex_exit(&buf_pool->LRU_list_mutex);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4206

4207 4208 4209 4210 4211
		if (uncompressed) {
			rw_lock_s_unlock_gen(&((buf_block_t*) bpage)->lock,
					     BUF_IO_WRITE);
		}

4212
		buf_pool->stat.n_pages_written++;
4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227

		break;

	default:
		ut_error;
	}

#ifdef UNIV_DEBUG
	if (buf_debug_prints) {
		fprintf(stderr, "Has %s page space %lu page no %lu\n",
			io_type == BUF_IO_READ ? "read" : "written",
			(ulong) buf_page_get_space(bpage),
			(ulong) buf_page_get_page_no(bpage));
	}
#endif /* UNIV_DEBUG */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4228

Sergei Golubchik's avatar
Sergei Golubchik committed
4229
	buf_pool_mutex_exit(buf_pool);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4230
	mutex_exit(block_mutex);
4231 4232
}

Sergei Golubchik's avatar
Sergei Golubchik committed
4233 4234
/********************************************************************//**
*/
4235
UNIV_INTERN
Sergei Golubchik's avatar
Sergei Golubchik committed
4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305
buf_block_t*
buf_page_from_array(
/*================*/
	buf_pool_t*	buf_pool,
	ulint		n_block)
{
	ulint		n_chunks, offset;
	buf_chunk_t*	chunk;

	ut_a(n_block < buf_pool->curr_size);

	chunk = buf_pool->chunks;
	offset = n_block;

	for (n_chunks = buf_pool->n_chunks; n_chunks--; chunk++) {
		if (offset < chunk->size) {
			return(&chunk->blocks[offset]);
		}

		offset -= chunk->size;
	}

	ut_error;

	return(NULL);
}

/*********************************************************************//**
Asserts that all file pages in the buffer are in a replaceable state.
@return	TRUE */
static
ibool
buf_all_freed_instance(
/*===================*/
	buf_pool_t*	buf_pool)	/*!< in: buffer pool instancce */
{
	ulint		i;
	buf_chunk_t*	chunk;

	ut_ad(buf_pool);

	//buf_pool_mutex_enter(buf_pool);
	mutex_enter(&buf_pool->LRU_list_mutex);
	rw_lock_x_lock(&buf_pool->page_hash_latch);

	chunk = buf_pool->chunks;

	for (i = buf_pool->n_chunks; i--; chunk++) {

		const buf_block_t* block = buf_chunk_not_freed(chunk);

		if (UNIV_LIKELY_NULL(block)) {
			fprintf(stderr,
				"Page %lu %lu still fixed or dirty\n",
				(ulong) block->page.space,
				(ulong) block->page.offset);
			ut_error;
		}
	}

	//buf_pool_mutex_exit(buf_pool);
	mutex_exit(&buf_pool->LRU_list_mutex);
	rw_lock_x_unlock(&buf_pool->page_hash_latch);

	return(TRUE);
}

/*********************************************************************//**
Invalidates file pages in one buffer pool instance */
static
4306
void
Sergei Golubchik's avatar
Sergei Golubchik committed
4307 4308 4309
buf_pool_invalidate_instance(
/*=========================*/
	buf_pool_t*	buf_pool)	/*!< in: buffer pool instance */
4310
{
4311 4312 4313
	ibool		freed;
	enum buf_flush	i;

Sergei Golubchik's avatar
Sergei Golubchik committed
4314
	buf_pool_mutex_enter(buf_pool);
4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329

	for (i = BUF_FLUSH_LRU; i < BUF_FLUSH_N_TYPES; i++) {

		/* As this function is called during startup and
		during redo application phase during recovery, InnoDB
		is single threaded (apart from IO helper threads) at
		this stage. No new write batch can be in intialization
		stage at this point. */
		ut_ad(buf_pool->init_flush[i] == FALSE);

		/* However, it is possible that a write batch that has
		been posted earlier is still not complete. For buffer
		pool invalidation to proceed we must ensure there is NO
		write activity happening. */
		if (buf_pool->n_flush[i] > 0) {
Sergei Golubchik's avatar
Sergei Golubchik committed
4330 4331 4332
			buf_pool_mutex_exit(buf_pool);
			buf_flush_wait_batch_end(buf_pool, i);
			buf_pool_mutex_enter(buf_pool);
4333 4334 4335
		}
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4336
	buf_pool_mutex_exit(buf_pool);
4337

Sergei Golubchik's avatar
Sergei Golubchik committed
4338
	ut_ad(buf_all_freed_instance(buf_pool));
4339 4340 4341 4342

	freed = TRUE;

	while (freed) {
Sergei Golubchik's avatar
Sergei Golubchik committed
4343
		freed = buf_LRU_search_and_free_block(buf_pool, 100);
4344 4345
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4346 4347
	//buf_pool_mutex_enter(buf_pool);
	mutex_enter(&buf_pool->LRU_list_mutex);
4348 4349 4350 4351

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

4352 4353 4354 4355 4356 4357
	buf_pool->freed_page_clock = 0;
	buf_pool->LRU_old = NULL;
	buf_pool->LRU_old_len = 0;
	buf_pool->LRU_flush_ended = 0;

	memset(&buf_pool->stat, 0x00, sizeof(buf_pool->stat));
Sergei Golubchik's avatar
Sergei Golubchik committed
4358
	buf_refresh_io_stats(buf_pool);
4359

Sergei Golubchik's avatar
Sergei Golubchik committed
4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377
	//buf_pool_mutex_exit(buf_pool);
	mutex_exit(&buf_pool->LRU_list_mutex);
}

/*********************************************************************//**
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. */
UNIV_INTERN
void
buf_pool_invalidate(void)
/*=====================*/
{
	ulint   i;

	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_invalidate_instance(buf_pool_from_array(i));
	}
4378 4379 4380
}

#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4381
/*********************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
4382
Validates data in one buffer pool instance
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4383
@return	TRUE */
Sergei Golubchik's avatar
Sergei Golubchik committed
4384
static
4385
ibool
Sergei Golubchik's avatar
Sergei Golubchik committed
4386 4387 4388
buf_pool_validate_instance(
/*=======================*/
	buf_pool_t*	buf_pool)	/*!< in: buffer pool instance */
4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402
{
	buf_page_t*	b;
	buf_chunk_t*	chunk;
	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_zip		= 0;

	ut_ad(buf_pool);

Sergei Golubchik's avatar
Sergei Golubchik committed
4403 4404 4405
	//buf_pool_mutex_enter(buf_pool);
	mutex_enter(&buf_pool->LRU_list_mutex);
	rw_lock_x_lock(&buf_pool->page_hash_latch);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4406
	/* for keep the new latch order, it cannot validate correctly... */
4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430

	chunk = buf_pool->chunks;

	/* Check the uncompressed blocks. */

	for (i = buf_pool->n_chunks; i--; chunk++) {

		ulint		j;
		buf_block_t*	block = chunk->blocks;

		for (j = chunk->size; j--; block++) {

			mutex_enter(&block->mutex);

			switch (buf_block_get_state(block)) {
			case BUF_BLOCK_ZIP_FREE:
			case BUF_BLOCK_ZIP_PAGE:
			case BUF_BLOCK_ZIP_DIRTY:
				/* These should only occur on
				zip_clean, zip_free[], or flush_list. */
				ut_error;
				break;

			case BUF_BLOCK_FILE_PAGE:
Sergei Golubchik's avatar
Sergei Golubchik committed
4431 4432
				ut_a(buf_page_hash_get(buf_pool,
						       buf_block_get_space(
4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495
							       block),
						       buf_block_get_page_no(
							       block))
				     == &block->page);

#ifdef UNIV_IBUF_COUNT_DEBUG
				ut_a(buf_page_get_io_fix(&block->page)
				     == BUF_IO_READ
				     || !ibuf_count_get(buf_block_get_space(
								block),
							buf_block_get_page_no(
								block)));
#endif
				switch (buf_page_get_io_fix(&block->page)) {
				case BUF_IO_NONE:
					break;

				case BUF_IO_WRITE:
					switch (buf_page_get_flush_type(
							&block->page)) {
					case BUF_FLUSH_LRU:
						n_lru_flush++;
						ut_a(rw_lock_is_locked(
							     &block->lock,
							     RW_LOCK_SHARED));
						break;
					case BUF_FLUSH_LIST:
						n_list_flush++;
						break;
					case BUF_FLUSH_SINGLE_PAGE:
						n_single_flush++;
						break;
					default:
						ut_error;
					}

					break;

				case BUF_IO_READ:

					ut_a(rw_lock_is_locked(&block->lock,
							       RW_LOCK_EX));
					break;
				}

				n_lru++;
				break;

			case BUF_BLOCK_NOT_USED:
				n_free++;
				break;

			case BUF_BLOCK_READY_FOR_USE:
			case BUF_BLOCK_MEMORY:
			case BUF_BLOCK_REMOVE_HASH:
				/* do nothing */
				break;
			}

			mutex_exit(&block->mutex);
		}
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4496
	mutex_enter(&buf_pool->zip_mutex);
4497 4498 4499 4500

	/* Check clean compressed-only blocks. */

	for (b = UT_LIST_GET_FIRST(buf_pool->zip_clean); b;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4501
	     b = UT_LIST_GET_NEXT(zip_list, b)) {
4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516
		ut_a(buf_page_get_state(b) == BUF_BLOCK_ZIP_PAGE);
		switch (buf_page_get_io_fix(b)) {
		case BUF_IO_NONE:
			/* All clean blocks should be I/O-unfixed. */
			break;
		case BUF_IO_READ:
			/* In buf_LRU_free_block(), we temporarily set
			b->io_fix = BUF_IO_READ for a newly allocated
			control block in order to prevent
			buf_page_get_gen() from decompressing the block. */
			break;
		default:
			ut_error;
			break;
		}
Sergei Golubchik's avatar
Sergei Golubchik committed
4517 4518 4519 4520

		/* It is OK to read oldest_modification here because
		we have acquired buf_pool->zip_mutex above which acts
		as the 'block->mutex' for these bpages. */
4521
		ut_a(!b->oldest_modification);
Sergei Golubchik's avatar
Sergei Golubchik committed
4522
		ut_a(buf_page_hash_get(buf_pool, b->space, b->offset) == b);
4523 4524 4525 4526 4527

		n_lru++;
		n_zip++;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4528
	/* Check dirty blocks. */
4529

Sergei Golubchik's avatar
Sergei Golubchik committed
4530
	buf_flush_list_mutex_enter(buf_pool);
4531
	for (b = UT_LIST_GET_FIRST(buf_pool->flush_list); b;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4532
	     b = UT_LIST_GET_NEXT(flush_list, b)) {
4533
		ut_ad(b->in_flush_list);
Sergei Golubchik's avatar
Sergei Golubchik committed
4534 4535
		ut_a(b->oldest_modification);
		n_flush++;
4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573

		switch (buf_page_get_state(b)) {
		case BUF_BLOCK_ZIP_DIRTY:
			n_lru++;
			n_zip++;
			switch (buf_page_get_io_fix(b)) {
			case BUF_IO_NONE:
			case BUF_IO_READ:
				break;
			case BUF_IO_WRITE:
				switch (buf_page_get_flush_type(b)) {
				case BUF_FLUSH_LRU:
					n_lru_flush++;
					break;
				case BUF_FLUSH_LIST:
					n_list_flush++;
					break;
				case BUF_FLUSH_SINGLE_PAGE:
					n_single_flush++;
					break;
				default:
					ut_error;
				}
				break;
			}
			break;
		case BUF_BLOCK_FILE_PAGE:
			/* uncompressed page */
			break;
		case BUF_BLOCK_ZIP_FREE:
		case BUF_BLOCK_ZIP_PAGE:
		case BUF_BLOCK_NOT_USED:
		case BUF_BLOCK_READY_FOR_USE:
		case BUF_BLOCK_MEMORY:
		case BUF_BLOCK_REMOVE_HASH:
			ut_error;
			break;
		}
Sergei Golubchik's avatar
Sergei Golubchik committed
4574
		ut_a(buf_page_hash_get(buf_pool, b->space, b->offset) == b);
4575 4576
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4577 4578 4579 4580 4581
	ut_a(UT_LIST_GET_LEN(buf_pool->flush_list) == n_flush);

	buf_flush_list_mutex_exit(buf_pool);

	mutex_exit(&buf_pool->zip_mutex);
4582 4583 4584 4585 4586 4587 4588 4589 4590

	if (n_lru + n_free > buf_pool->curr_size + n_zip) {
		fprintf(stderr, "n LRU %lu, n free %lu, pool %lu zip %lu\n",
			(ulong) n_lru, (ulong) n_free,
			(ulong) buf_pool->curr_size, (ulong) n_zip);
		ut_error;
	}

	ut_a(UT_LIST_GET_LEN(buf_pool->LRU) == n_lru);
Sergei Golubchik's avatar
Sergei Golubchik committed
4591
	/* because of latching order with block->mutex, we cannot get needed mutexes before that */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4592
/*
4593 4594 4595 4596 4597 4598 4599 4600 4601 4602
	if (UT_LIST_GET_LEN(buf_pool->free) != n_free) {
		fprintf(stderr, "Free list len %lu, free blocks %lu\n",
			(ulong) UT_LIST_GET_LEN(buf_pool->free),
			(ulong) n_free);
		ut_error;
	}

	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);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4603
*/
4604

Sergei Golubchik's avatar
Sergei Golubchik committed
4605 4606 4607
	//buf_pool_mutex_exit(buf_pool);
	mutex_exit(&buf_pool->LRU_list_mutex);
	rw_lock_x_unlock(&buf_pool->page_hash_latch);
4608 4609

	ut_a(buf_LRU_validate());
Sergei Golubchik's avatar
Sergei Golubchik committed
4610
	ut_a(buf_flush_validate(buf_pool));
4611 4612 4613

	return(TRUE);
}
Sergei Golubchik's avatar
Sergei Golubchik committed
4614 4615 4616 4617 4618 4619 4620 4621 4622 4623

/*********************************************************************//**
Validates the buffer buf_pool data structure.
@return	TRUE */
UNIV_INTERN
ibool
buf_validate(void)
/*==============*/
{
	ulint	i;
4624

Sergei Golubchik's avatar
Sergei Golubchik committed
4625 4626 4627 4628 4629 4630 4631
	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_t*	buf_pool;

		buf_pool = buf_pool_from_array(i);

		buf_pool_validate_instance(buf_pool);
	}
4632 4633
	return(TRUE);
}
Sergei Golubchik's avatar
Sergei Golubchik committed
4634

4635 4636 4637
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */

#if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4638
/*********************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
4639 4640
Prints info of the buffer buf_pool data structure for one instance. */
static
4641
void
Sergei Golubchik's avatar
Sergei Golubchik committed
4642 4643 4644
buf_print_instance(
/*===============*/
	buf_pool_t*	buf_pool)
4645
{
Sergei Golubchik's avatar
Sergei Golubchik committed
4646
	index_id_t*	index_ids;
4647 4648 4649 4650
	ulint*		counts;
	ulint		size;
	ulint		i;
	ulint		j;
Sergei Golubchik's avatar
Sergei Golubchik committed
4651
	index_id_t	id;
4652 4653 4654 4655 4656 4657 4658 4659
	ulint		n_found;
	buf_chunk_t*	chunk;
	dict_index_t*	index;

	ut_ad(buf_pool);

	size = buf_pool->curr_size;

Sergei Golubchik's avatar
Sergei Golubchik committed
4660
	index_ids = mem_alloc(size * sizeof *index_ids);
4661 4662
	counts = mem_alloc(sizeof(ulint) * size);

Sergei Golubchik's avatar
Sergei Golubchik committed
4663 4664 4665 4666
	//buf_pool_mutex_enter(buf_pool);
	mutex_enter(&buf_pool->LRU_list_mutex);
	mutex_enter(&buf_pool->free_list_mutex);
	buf_flush_list_mutex_enter(buf_pool);
4667 4668 4669 4670 4671 4672 4673 4674 4675

	fprintf(stderr,
		"buf_pool size %lu\n"
		"database pages %lu\n"
		"free pages %lu\n"
		"modified database pages %lu\n"
		"n pending decompressions %lu\n"
		"n pending reads %lu\n"
		"n pending flush LRU %lu list %lu single page %lu\n"
4676
		"pages made young %lu, not young %lu\n"
4677 4678 4679 4680 4681 4682 4683 4684 4685 4686
		"pages read %lu, created %lu, written %lu\n",
		(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_unzip,
		(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],
4687 4688 4689 4690 4691
		(ulong) buf_pool->stat.n_pages_made_young,
		(ulong) buf_pool->stat.n_pages_not_made_young,
		(ulong) buf_pool->stat.n_pages_read,
		(ulong) buf_pool->stat.n_pages_created,
		(ulong) buf_pool->stat.n_pages_written);
4692

Sergei Golubchik's avatar
Sergei Golubchik committed
4693 4694
	buf_flush_list_mutex_exit(buf_pool);

4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716
	/* Count the number of blocks belonging to each index in the buffer */

	n_found = 0;

	chunk = buf_pool->chunks;

	for (i = buf_pool->n_chunks; i--; chunk++) {
		buf_block_t*	block		= chunk->blocks;
		ulint		n_blocks	= chunk->size;

		for (; n_blocks--; block++) {
			const buf_frame_t* frame = block->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) {

Sergei Golubchik's avatar
Sergei Golubchik committed
4717
					if (index_ids[j] == id) {
4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733
						counts[j]++;

						break;
					}
					j++;
				}

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

Sergei Golubchik's avatar
Sergei Golubchik committed
4734 4735 4736
	//buf_pool_mutex_exit(buf_pool);
	mutex_exit(&buf_pool->LRU_list_mutex);
	mutex_exit(&buf_pool->free_list_mutex);
4737 4738 4739 4740 4741

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

		fprintf(stderr,
Sergei Golubchik's avatar
Sergei Golubchik committed
4742 4743
			"Block count for index %llu in buffer is about %lu",
			(ullint) index_ids[i],
4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756
			(ulong) counts[i]);

		if (index) {
			putc(' ', stderr);
			dict_index_name_print(stderr, NULL, index);
		}

		putc('\n', stderr);
	}

	mem_free(index_ids);
	mem_free(counts);

Sergei Golubchik's avatar
Sergei Golubchik committed
4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774
	ut_a(buf_pool_validate_instance(buf_pool));
}

/*********************************************************************//**
Prints info of the buffer buf_pool data structure. */
UNIV_INTERN
void
buf_print(void)
/*===========*/
{
	ulint   i;

	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_t*	buf_pool;

		buf_pool = buf_pool_from_array(i);
		buf_print_instance(buf_pool);
	}
4775 4776 4777
}
#endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */

Vadim Tkachenko's avatar
Vadim Tkachenko committed
4778
#ifdef UNIV_DEBUG
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4779 4780 4781
/*********************************************************************//**
Returns the number of latched pages in the buffer pool.
@return	number of latched pages */
4782 4783
UNIV_INTERN
ulint
Sergei Golubchik's avatar
Sergei Golubchik committed
4784 4785 4786
buf_get_latched_pages_number_instance(
/*==================================*/
	buf_pool_t*	buf_pool)	/*!< in: buffer pool instance */
4787 4788 4789
{
	buf_page_t*	b;
	ulint		i;
Sergei Golubchik's avatar
Sergei Golubchik committed
4790
	buf_chunk_t*	chunk;
4791 4792
	ulint		fixed_pages_number = 0;

Sergei Golubchik's avatar
Sergei Golubchik committed
4793
	//buf_pool_mutex_enter(buf_pool);
4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821

	chunk = buf_pool->chunks;

	for (i = buf_pool->n_chunks; i--; chunk++) {
		buf_block_t*	block;
		ulint		j;

		block = chunk->blocks;

		for (j = chunk->size; j--; block++) {
			if (buf_block_get_state(block)
			    != BUF_BLOCK_FILE_PAGE) {

				continue;
			}

			mutex_enter(&block->mutex);

			if (block->page.buf_fix_count != 0
			    || buf_page_get_io_fix(&block->page)
			    != BUF_IO_NONE) {
				fixed_pages_number++;
			}

			mutex_exit(&block->mutex);
		}
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4822
	mutex_enter(&buf_pool->zip_mutex);
4823 4824 4825 4826

	/* Traverse the lists of clean and dirty compressed-only blocks. */

	for (b = UT_LIST_GET_FIRST(buf_pool->zip_clean); b;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4827
	     b = UT_LIST_GET_NEXT(zip_list, b)) {
4828 4829 4830 4831 4832 4833 4834 4835 4836
		ut_a(buf_page_get_state(b) == BUF_BLOCK_ZIP_PAGE);
		ut_a(buf_page_get_io_fix(b) != BUF_IO_WRITE);

		if (b->buf_fix_count != 0
		    || buf_page_get_io_fix(b) != BUF_IO_NONE) {
			fixed_pages_number++;
		}
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4837
	buf_flush_list_mutex_enter(buf_pool);
4838
	for (b = UT_LIST_GET_FIRST(buf_pool->flush_list); b;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4839
	     b = UT_LIST_GET_NEXT(flush_list, b)) {
4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862
		ut_ad(b->in_flush_list);

		switch (buf_page_get_state(b)) {
		case BUF_BLOCK_ZIP_DIRTY:
			if (b->buf_fix_count != 0
			    || buf_page_get_io_fix(b) != BUF_IO_NONE) {
				fixed_pages_number++;
			}
			break;
		case BUF_BLOCK_FILE_PAGE:
			/* uncompressed page */
			break;
		case BUF_BLOCK_ZIP_FREE:
		case BUF_BLOCK_ZIP_PAGE:
		case BUF_BLOCK_NOT_USED:
		case BUF_BLOCK_READY_FOR_USE:
		case BUF_BLOCK_MEMORY:
		case BUF_BLOCK_REMOVE_HASH:
			ut_error;
			break;
		}
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4863 4864 4865
	buf_flush_list_mutex_exit(buf_pool);
	mutex_exit(&buf_pool->zip_mutex);
	//buf_pool_mutex_exit(buf_pool);
4866 4867 4868

	return(fixed_pages_number);
}
Sergei Golubchik's avatar
Sergei Golubchik committed
4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892

/*********************************************************************//**
Returns the number of latched pages in all the buffer pools.
@return	number of latched pages */
UNIV_INTERN
ulint
buf_get_latched_pages_number(void)
/*==============================*/
{
	ulint	i;
	ulint	total_latched_pages = 0;

	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_t*	buf_pool;

		buf_pool = buf_pool_from_array(i);

		total_latched_pages += buf_get_latched_pages_number_instance(
			buf_pool);
	}

	return(total_latched_pages);
}

Vadim Tkachenko's avatar
Vadim Tkachenko committed
4893
#endif /* UNIV_DEBUG */
4894

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4895 4896 4897
/*********************************************************************//**
Returns the number of pending buf pool ios.
@return	number of pending I/O operations */
4898 4899 4900 4901 4902
UNIV_INTERN
ulint
buf_get_n_pending_ios(void)
/*=======================*/
{
Sergei Golubchik's avatar
Sergei Golubchik committed
4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918
	ulint	i;
	ulint	pend_ios = 0;

	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_t*	buf_pool;

		buf_pool = buf_pool_from_array(i);

		pend_ios +=
			buf_pool->n_pend_reads
			+ buf_pool->n_flush[BUF_FLUSH_LRU]
			+ buf_pool->n_flush[BUF_FLUSH_LIST]
			+ buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE];
	}

	return(pend_ios);
4919 4920
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4921
/*********************************************************************//**
4922
Returns the ratio in percents of modified pages in the buffer pool /
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4923 4924
database pages in the buffer pool.
@return	modified page percentage ratio */
4925 4926 4927 4928 4929
UNIV_INTERN
ulint
buf_get_modified_ratio_pct(void)
/*============================*/
{
Sergei Golubchik's avatar
Sergei Golubchik committed
4930 4931 4932 4933
	ulint		ratio;
	ulint		lru_len = 0;
	ulint		free_len = 0;
	ulint		flush_list_len = 0;
4934

Sergei Golubchik's avatar
Sergei Golubchik committed
4935
	buf_get_total_list_len(&lru_len, &free_len, &flush_list_len);
4936

Sergei Golubchik's avatar
Sergei Golubchik committed
4937
	ratio = (100 * flush_list_len) / (1 + lru_len + free_len);
4938 4939 4940 4941 4942 4943

	/* 1 + is there to avoid division by zero */

	return(ratio);
}

Sergei Golubchik's avatar
Sergei Golubchik committed
4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980
/*******************************************************************//**
Aggregates a pool stats information with the total buffer pool stats  */
static
void
buf_stats_aggregate_pool_info(
/*==========================*/
	buf_pool_info_t*	total_info,	/*!< in/out: the buffer pool
						info to store aggregated
						result */
	const buf_pool_info_t*	pool_info)	/*!< in: individual buffer pool
						stats info */
{
	ut_a(total_info && pool_info);

	/* Nothing to copy if total_info is the same as pool_info */
	if (total_info == pool_info) {
		return;
	}

	total_info->pool_size += pool_info->pool_size;
	total_info->pool_size_bytes += pool_info->pool_size_bytes;
	total_info->lru_len += pool_info->lru_len;
	total_info->old_lru_len += pool_info->old_lru_len;
	total_info->free_list_len += pool_info->free_list_len;
	total_info->flush_list_len += pool_info->flush_list_len;
	total_info->n_pend_unzip += pool_info->n_pend_unzip;
	total_info->n_pend_reads += pool_info->n_pend_reads;
	total_info->n_pending_flush_lru += pool_info->n_pending_flush_lru;
	total_info->n_pending_flush_list += pool_info->n_pending_flush_list;
	total_info->n_pending_flush_single_page +=
		 pool_info->n_pending_flush_single_page;
	total_info->n_pages_made_young += pool_info->n_pages_made_young;
	total_info->n_pages_not_made_young += pool_info->n_pages_not_made_young;
	total_info->n_pages_read += pool_info->n_pages_read;
	total_info->n_pages_created += pool_info->n_pages_created;
	total_info->n_pages_written += pool_info->n_pages_written;
	total_info->n_page_gets += pool_info->n_page_gets;
4981
	total_info->n_ra_pages_read_rnd += pool_info->n_ra_pages_read_rnd;
Sergei Golubchik's avatar
Sergei Golubchik committed
4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993
	total_info->n_ra_pages_read += pool_info->n_ra_pages_read;
	total_info->n_ra_pages_evicted += pool_info->n_ra_pages_evicted;
	total_info->page_made_young_rate += pool_info->page_made_young_rate;
	total_info->page_not_made_young_rate +=
		pool_info->page_not_made_young_rate;
	total_info->pages_read_rate += pool_info->pages_read_rate;
	total_info->pages_created_rate += pool_info->pages_created_rate;
	total_info->pages_written_rate += pool_info->pages_written_rate;
	total_info->n_page_get_delta += pool_info->n_page_get_delta;
	total_info->page_read_delta += pool_info->page_read_delta;
	total_info->young_making_delta += pool_info->young_making_delta;
	total_info->not_young_making_delta += pool_info->not_young_making_delta;
4994
	total_info->pages_readahead_rnd_rate += pool_info->pages_readahead_rnd_rate;
Sergei Golubchik's avatar
Sergei Golubchik committed
4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075
	total_info->pages_readahead_rate += pool_info->pages_readahead_rate;
	total_info->pages_evicted_rate += pool_info->pages_evicted_rate;
	total_info->unzip_lru_len += pool_info->unzip_lru_len;
	total_info->io_sum += pool_info->io_sum;
	total_info->io_cur += pool_info->io_cur;
	total_info->unzip_sum += pool_info->unzip_sum;
	total_info->unzip_cur += pool_info->unzip_cur;
}
/*******************************************************************//**
Collect buffer pool stats information for a buffer pool. Also
record aggregated stats if there are more than one buffer pool
in the server */
static
void
buf_stats_get_pool_info(
/*====================*/
	buf_pool_t*		buf_pool,	/*!< in: buffer pool */
	ulint			pool_id,	/*!< in: buffer pool ID */
	buf_pool_info_t*	all_pool_info)	/*!< in/out: buffer pool info
						to fill */
{
	buf_pool_info_t*        pool_info;
	time_t			current_time;
	double			time_elapsed;

	/* Find appropriate pool_info to store stats for this buffer pool */
	pool_info = &all_pool_info[pool_id];

	mutex_enter(&buf_pool->LRU_list_mutex);
	mutex_enter(&buf_pool->free_list_mutex);
	buf_pool_mutex_enter(buf_pool);
	buf_flush_list_mutex_enter(buf_pool);

	pool_info->pool_unique_id = pool_id;

	pool_info->pool_size = buf_pool->curr_size;

	pool_info->pool_size_bytes = buf_pool->curr_pool_size;

	pool_info->lru_len = UT_LIST_GET_LEN(buf_pool->LRU);

	pool_info->old_lru_len = buf_pool->LRU_old_len;

	pool_info->free_list_len = UT_LIST_GET_LEN(buf_pool->free);

	pool_info->flush_list_len = UT_LIST_GET_LEN(buf_pool->flush_list);

	pool_info->n_pend_unzip = UT_LIST_GET_LEN(buf_pool->unzip_LRU);

	pool_info->n_pend_reads = buf_pool->n_pend_reads;

	pool_info->n_pending_flush_lru =
		 (buf_pool->n_flush[BUF_FLUSH_LRU]
		  + buf_pool->init_flush[BUF_FLUSH_LRU]);

	pool_info->n_pending_flush_list =
		 (buf_pool->n_flush[BUF_FLUSH_LIST]
		  + buf_pool->init_flush[BUF_FLUSH_LIST]);

	pool_info->n_pending_flush_single_page =
		 buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE];

	buf_flush_list_mutex_exit(buf_pool);

	current_time = time(NULL);
	time_elapsed = 0.001 + difftime(current_time,
					buf_pool->last_printout_time);

	pool_info->n_pages_made_young = buf_pool->stat.n_pages_made_young;

	pool_info->n_pages_not_made_young =
		buf_pool->stat.n_pages_not_made_young;

	pool_info->n_pages_read = buf_pool->stat.n_pages_read;

	pool_info->n_pages_created = buf_pool->stat.n_pages_created;

	pool_info->n_pages_written = buf_pool->stat.n_pages_written;

	pool_info->n_page_gets = buf_pool->stat.n_page_gets;

5076
	pool_info->n_ra_pages_read_rnd = buf_pool->stat.n_ra_pages_read_rnd;
Sergei Golubchik's avatar
Sergei Golubchik committed
5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115
	pool_info->n_ra_pages_read = buf_pool->stat.n_ra_pages_read;

	pool_info->n_ra_pages_evicted = buf_pool->stat.n_ra_pages_evicted;

	pool_info->page_made_young_rate =
		 (buf_pool->stat.n_pages_made_young
		  - buf_pool->old_stat.n_pages_made_young) / time_elapsed;

	pool_info->page_not_made_young_rate =
		 (buf_pool->stat.n_pages_not_made_young
		  - buf_pool->old_stat.n_pages_not_made_young) / time_elapsed;

	pool_info->pages_read_rate =
		(buf_pool->stat.n_pages_read
		  - buf_pool->old_stat.n_pages_read) / time_elapsed;

	pool_info->pages_created_rate =
		(buf_pool->stat.n_pages_created
		 - buf_pool->old_stat.n_pages_created) / time_elapsed;

	pool_info->pages_written_rate =
		(buf_pool->stat.n_pages_written
		 - buf_pool->old_stat.n_pages_written) / time_elapsed;

	pool_info->n_page_get_delta = buf_pool->stat.n_page_gets
				      - buf_pool->old_stat.n_page_gets;

	if (pool_info->n_page_get_delta) {
		pool_info->page_read_delta = buf_pool->stat.n_pages_read
					     - buf_pool->old_stat.n_pages_read;

		pool_info->young_making_delta =
			buf_pool->stat.n_pages_made_young
			- buf_pool->old_stat.n_pages_made_young;

		pool_info->not_young_making_delta =
			buf_pool->stat.n_pages_not_made_young
			- buf_pool->old_stat.n_pages_not_made_young;
	}
5116 5117 5118 5119
	pool_info->pages_readahead_rnd_rate =
		 (buf_pool->stat.n_ra_pages_read_rnd
		  - buf_pool->old_stat.n_ra_pages_read_rnd) / time_elapsed;

Sergei Golubchik's avatar
Sergei Golubchik committed
5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144

	pool_info->pages_readahead_rate =
		 (buf_pool->stat.n_ra_pages_read
		  - buf_pool->old_stat.n_ra_pages_read) / time_elapsed;

	pool_info->pages_evicted_rate =
		(buf_pool->stat.n_ra_pages_evicted
		 - buf_pool->old_stat.n_ra_pages_evicted) / time_elapsed;

	pool_info->unzip_lru_len = UT_LIST_GET_LEN(buf_pool->unzip_LRU);

	pool_info->io_sum = buf_LRU_stat_sum.io;

	pool_info->io_cur = buf_LRU_stat_cur.io;

	pool_info->unzip_sum = buf_LRU_stat_sum.unzip;

	pool_info->unzip_cur = buf_LRU_stat_cur.unzip;

	buf_refresh_io_stats(buf_pool);
	mutex_exit(&buf_pool->LRU_list_mutex);
	mutex_exit(&buf_pool->free_list_mutex);
	buf_pool_mutex_exit(buf_pool);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5145
/*********************************************************************//**
5146 5147 5148
Prints info of the buffer i/o. */
UNIV_INTERN
void
Sergei Golubchik's avatar
Sergei Golubchik committed
5149 5150 5151 5152
buf_print_io_instance(
/*==================*/
	buf_pool_info_t*pool_info,	/*!< in: buffer pool info */
	FILE*		file)		/*!< in/out: buffer where to print */
5153
{
Sergei Golubchik's avatar
Sergei Golubchik committed
5154
	ut_ad(pool_info);
5155 5156

	fprintf(file,
5157 5158 5159 5160
		"Buffer pool size        %lu\n"
		"Buffer pool size, bytes %lu\n"
		"Free buffers            %lu\n"
		"Database pages          %lu\n"
5161
		"Old database pages      %lu\n"
5162
		"Modified db pages       %lu\n"
5163 5164
		"Pending reads %lu\n"
		"Pending writes: LRU %lu, flush list %lu, single page %lu\n",
Sergei Golubchik's avatar
Sergei Golubchik committed
5165 5166 5167 5168 5169 5170 5171 5172 5173 5174
		pool_info->pool_size,
		pool_info->pool_size_bytes,
		pool_info->free_list_len,
		pool_info->lru_len,
		pool_info->old_lru_len,
		pool_info->flush_list_len,
		pool_info->n_pend_reads,
		pool_info->n_pending_flush_lru,
		pool_info->n_pending_flush_list,
		pool_info->n_pending_flush_single_page);
5175 5176

	fprintf(file,
5177 5178
		"Pages made young %lu, not young %lu\n"
		"%.2f youngs/s, %.2f non-youngs/s\n"
5179 5180
		"Pages read %lu, created %lu, written %lu\n"
		"%.2f reads/s, %.2f creates/s, %.2f writes/s\n",
Sergei Golubchik's avatar
Sergei Golubchik committed
5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192
		pool_info->n_pages_made_young,
		pool_info->n_pages_not_made_young,
		pool_info->page_made_young_rate,
		pool_info->page_not_made_young_rate,
		pool_info->n_pages_read,
		pool_info->n_pages_created,
		pool_info->n_pages_written,
		pool_info->pages_read_rate,
		pool_info->pages_created_rate,
		pool_info->pages_written_rate);

	if (pool_info->n_page_get_delta) {
5193 5194 5195
		fprintf(file,
			"Buffer pool hit rate %lu / 1000,"
			" young-making rate %lu / 1000 not %lu / 1000\n",
Sergei Golubchik's avatar
Sergei Golubchik committed
5196 5197 5198 5199 5200 5201
			(ulong) (1000 - (1000 * pool_info->page_read_delta
					 / pool_info->n_page_get_delta)),
			(ulong) (1000 * pool_info->young_making_delta
				 / pool_info->n_page_get_delta),
			(ulong) (1000 * pool_info->not_young_making_delta
				 / pool_info->n_page_get_delta));
5202 5203 5204 5205 5206
	} else {
		fputs("No buffer pool page gets since the last printout\n",
		      file);
	}

5207 5208
	/* Statistics about read ahead algorithm */
	fprintf(file, "Pages read ahead %.2f/s,"
5209 5210 5211
		" evicted without access %.2f/s,"
		" Random read ahead %.2f/s\n",

Sergei Golubchik's avatar
Sergei Golubchik committed
5212
		pool_info->pages_readahead_rate,
5213 5214
		pool_info->pages_evicted_rate,
		pool_info->pages_readahead_rnd_rate);
5215 5216 5217 5218 5219 5220

	/* Print some values to help us with visualizing what is
	happening with LRU eviction. */
	fprintf(file,
		"LRU len: %lu, unzip_LRU len: %lu\n"
		"I/O sum[%lu]:cur[%lu], unzip sum[%lu]:cur[%lu]\n",
Sergei Golubchik's avatar
Sergei Golubchik committed
5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285
		pool_info->lru_len, pool_info->unzip_lru_len,
		pool_info->io_sum, pool_info->io_cur,
		pool_info->unzip_sum, pool_info->unzip_cur);
}

/*********************************************************************//**
Prints info of the buffer i/o. */
UNIV_INTERN
void
buf_print_io(
/*=========*/
	FILE*	file)	/*!< in/out: buffer where to print */
{
	ulint			i;
	buf_pool_info_t*	pool_info;
	buf_pool_info_t*	pool_info_total;

	/* If srv_buf_pool_instances is greater than 1, allocate
	one extra buf_pool_info_t, the last one stores
	aggregated/total values from all pools */
	if (srv_buf_pool_instances > 1) {
		pool_info = (buf_pool_info_t*) mem_zalloc((
			srv_buf_pool_instances + 1) * sizeof *pool_info);

		pool_info_total = &pool_info[srv_buf_pool_instances];
	} else {
		ut_a(srv_buf_pool_instances == 1);
		pool_info_total = pool_info = (buf_pool_info_t*) mem_zalloc(
			sizeof *pool_info)
	}

	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_t*	buf_pool;

		buf_pool = buf_pool_from_array(i);

		/* Fetch individual buffer pool info and calculate
		aggregated stats along the way */
		buf_stats_get_pool_info(buf_pool, i, pool_info);

		/* If we have more than one buffer pool, store
		the aggregated stats  */
		if (srv_buf_pool_instances > 1) {
			buf_stats_aggregate_pool_info(pool_info_total,
						      &pool_info[i]);
		}
	}

	/* Print the aggreate buffer pool info */
	buf_print_io_instance(pool_info_total, file);

	/* If there are more than one buffer pool, print each individual pool
	info */
	if (srv_buf_pool_instances > 1) {
		fputs("----------------------\n"
		"INDIVIDUAL BUFFER POOL INFO\n"
		"----------------------\n", file);

		for (i = 0; i < srv_buf_pool_instances; i++) {
			fprintf(file, "---BUFFER POOL %lu\n", i);
			buf_print_io_instance(&pool_info[i], file);
		}
	}

	mem_free(pool_info);
5286 5287
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5288
/**********************************************************************//**
5289 5290 5291
Refreshes the statistics used to print per-second averages. */
UNIV_INTERN
void
Sergei Golubchik's avatar
Sergei Golubchik committed
5292 5293 5294
buf_refresh_io_stats(
/*=================*/
	buf_pool_t*	buf_pool)	/*!< in: buffer pool instance */
5295
{
Sergei Golubchik's avatar
Sergei Golubchik committed
5296
	buf_pool->last_printout_time = ut_time();
5297
	buf_pool->old_stat = buf_pool->stat;
5298 5299
}

Sergei Golubchik's avatar
Sergei Golubchik committed
5300 5301
/**********************************************************************//**
Refreshes the statistics used to print per-second averages. */
5302
UNIV_INTERN
Sergei Golubchik's avatar
Sergei Golubchik committed
5303 5304 5305
void
buf_refresh_io_stats_all(void)
/*==========================*/
5306 5307 5308
{
	ulint		i;

Sergei Golubchik's avatar
Sergei Golubchik committed
5309 5310
	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_t*	buf_pool;
5311

Sergei Golubchik's avatar
Sergei Golubchik committed
5312
		buf_pool = buf_pool_from_array(i);
5313

Sergei Golubchik's avatar
Sergei Golubchik committed
5314 5315 5316
		buf_refresh_io_stats(buf_pool);
	}
}
5317

Sergei Golubchik's avatar
Sergei Golubchik committed
5318 5319 5320 5321 5322 5323 5324 5325 5326
/**********************************************************************//**
Check if all pages in all buffer pools are in a replacable state.
@return FALSE if not */
UNIV_INTERN
ibool
buf_all_freed(void)
/*===============*/
{
	ulint	i;
5327

Sergei Golubchik's avatar
Sergei Golubchik committed
5328 5329
	for (i = 0; i < srv_buf_pool_instances; i++) {
		buf_pool_t*	buf_pool;
5330

Sergei Golubchik's avatar
Sergei Golubchik committed
5331
		buf_pool = buf_pool_from_array(i);
5332

Sergei Golubchik's avatar
Sergei Golubchik committed
5333 5334 5335 5336
		if (!buf_all_freed_instance(buf_pool)) {
			return(FALSE);
		}
 	}
5337 5338 5339 5340

	return(TRUE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5341
/*********************************************************************//**
5342
Checks that there currently are no pending i/o-operations for the buffer
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5343 5344
pool.
@return	TRUE if there is no pending i/o */
5345 5346 5347 5348 5349
UNIV_INTERN
ibool
buf_pool_check_no_pending_io(void)
/*==============================*/
{
Sergei Golubchik's avatar
Sergei Golubchik committed
5350 5351
	ulint		i;
	ibool		ret = TRUE;
5352

Sergei Golubchik's avatar
Sergei Golubchik committed
5353
	buf_pool_mutex_enter_all();
5354

Sergei Golubchik's avatar
Sergei Golubchik committed
5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366
	for (i = 0; i < srv_buf_pool_instances && ret; i++) {
		const buf_pool_t*	buf_pool;

		buf_pool = buf_pool_from_array(i);

		if (buf_pool->n_pend_reads
		    + buf_pool->n_flush[BUF_FLUSH_LRU]
		    + buf_pool->n_flush[BUF_FLUSH_LIST]
		    + buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]) {

			ret = FALSE;
		}
5367 5368
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
5369
	buf_pool_mutex_exit_all();
5370 5371 5372 5373

	return(ret);
}

Sergei Golubchik's avatar
Sergei Golubchik committed
5374 5375
#if 0
Code currently not used
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5376 5377 5378
/*********************************************************************//**
Gets the current length of the free list of buffer blocks.
@return	length of the free list */
5379 5380 5381 5382 5383 5384 5385
UNIV_INTERN
ulint
buf_get_free_list_len(void)
/*=======================*/
{
	ulint	len;

Sergei Golubchik's avatar
Sergei Golubchik committed
5386 5387
	//buf_pool_mutex_enter(buf_pool);
	mutex_enter(&buf_pool->free_list_mutex);
5388 5389 5390

	len = UT_LIST_GET_LEN(buf_pool->free);

Sergei Golubchik's avatar
Sergei Golubchik committed
5391 5392
	//buf_pool_mutex_exit(buf_pool);
	mutex_exit(&buf_pool->free_list_mutex);
5393 5394 5395

	return(len);
}
Sergei Golubchik's avatar
Sergei Golubchik committed
5396 5397
#endif

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427
#else /* !UNIV_HOTBACKUP */
/********************************************************************//**
Inits a page to the buffer buf_pool, for use in ibbackup --restore. */
UNIV_INTERN
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 */
	ulint		zip_size,/*!< in: compressed page size in bytes
				or 0 for uncompressed pages */
	buf_block_t*	block)	/*!< in: block to init */
{
	block->page.state	= BUF_BLOCK_FILE_PAGE;
	block->page.space	= space;
	block->page.offset	= offset;

	page_zip_des_init(&block->page.zip);

	/* We assume that block->page.data has been allocated
	with zip_size == UNIV_PAGE_SIZE. */
	ut_ad(zip_size <= UNIV_PAGE_SIZE);
	ut_ad(ut_is_2pow(zip_size));
	page_zip_set_size(&block->page.zip, zip_size);
	if (zip_size) {
		block->page.zip.data = block->frame + UNIV_PAGE_SIZE;
	}
}
#endif /* !UNIV_HOTBACKUP */