~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/innobase/include/buf0buf.h

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 
 
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
 
4
 
 
5
This program is free software; you can redistribute it and/or modify it under
 
6
the terms of the GNU General Public License as published by the Free Software
 
7
Foundation; version 2 of the License.
 
8
 
 
9
This program is distributed in the hope that it will be useful, but WITHOUT
 
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
12
 
 
13
You should have received a copy of the GNU General Public License along with
 
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
15
Place, Suite 330, Boston, MA 02111-1307 USA
 
16
 
 
17
*****************************************************************************/
 
18
 
 
19
/**************************************************//**
 
20
@file include/buf0buf.h
 
21
The database buffer pool high-level routines
 
22
 
 
23
Created 11/5/1995 Heikki Tuuri
 
24
*******************************************************/
 
25
 
 
26
#ifndef buf0buf_h
 
27
#define buf0buf_h
 
28
 
 
29
#include "univ.i"
 
30
#include "fil0fil.h"
 
31
#include "mtr0types.h"
 
32
#include "buf0types.h"
 
33
#include "hash0hash.h"
 
34
#include "ut0byte.h"
 
35
#include "page0types.h"
 
36
#ifndef UNIV_HOTBACKUP
 
37
#include "os0proc.h"
 
38
 
 
39
/** @name Modes for buf_page_get_gen */
 
40
/* @{ */
 
41
#define BUF_GET                 10      /*!< get always */
 
42
#define BUF_GET_IF_IN_POOL      11      /*!< get if in pool */
 
43
#define BUF_GET_NO_LATCH        14      /*!< get and bufferfix, but
 
44
                                        set no latch; we have
 
45
                                        separated this case, because
 
46
                                        it is error-prone programming
 
47
                                        not to set a latch, and it
 
48
                                        should be used with care */
 
49
/* @} */
 
50
/** @name Modes for buf_page_get_known_nowait */
 
51
/* @{ */
 
52
#define BUF_MAKE_YOUNG  51              /*!< Move the block to the
 
53
                                        start of the LRU list if there
 
54
                                        is a danger that the block
 
55
                                        would drift out of the buffer
 
56
                                        pool*/
 
57
#define BUF_KEEP_OLD    52              /*!< Preserve the current LRU
 
58
                                        position of the block. */
 
59
/* @} */
 
60
 
 
61
extern buf_pool_t*      buf_pool;       /*!< The buffer pool of the database */
 
62
#ifdef UNIV_DEBUG
 
63
extern ibool            buf_debug_prints;/*!< If this is set TRUE, the program
 
64
                                        prints info whenever read or flush
 
65
                                        occurs */
 
66
#endif /* UNIV_DEBUG */
 
67
#else /* !UNIV_HOTBACKUP */
 
68
extern buf_block_t*     back_block1;    /*!< first block, for --apply-log */
 
69
extern buf_block_t*     back_block2;    /*!< second block, for page reorganize */
 
70
#endif /* !UNIV_HOTBACKUP */
 
71
 
 
72
/** Magic value to use instead of checksums when they are disabled */
 
73
#define BUF_NO_CHECKSUM_MAGIC 0xDEADBEEFUL
 
74
 
 
75
/** @brief States of a control block
 
76
@see buf_page_struct
 
77
 
 
78
The enumeration values must be 0..7. */
 
79
enum buf_page_state {
 
80
        BUF_BLOCK_ZIP_FREE = 0,         /*!< contains a free
 
81
                                        compressed page */
 
82
        BUF_BLOCK_ZIP_PAGE,             /*!< contains a clean
 
83
                                        compressed page */
 
84
        BUF_BLOCK_ZIP_DIRTY,            /*!< contains a compressed
 
85
                                        page that is in the
 
86
                                        buf_pool->flush_list */
 
87
 
 
88
        BUF_BLOCK_NOT_USED,             /*!< is in the free list;
 
89
                                        must be after the BUF_BLOCK_ZIP_
 
90
                                        constants for compressed-only pages
 
91
                                        @see buf_block_state_valid() */
 
92
        BUF_BLOCK_READY_FOR_USE,        /*!< when buf_LRU_get_free_block
 
93
                                        returns a block, it is in this state */
 
94
        BUF_BLOCK_FILE_PAGE,            /*!< contains a buffered file page */
 
95
        BUF_BLOCK_MEMORY,               /*!< contains some main memory
 
96
                                        object */
 
97
        BUF_BLOCK_REMOVE_HASH           /*!< hash index should be removed
 
98
                                        before putting to the free list */
 
99
};
 
100
 
 
101
#ifndef UNIV_HOTBACKUP
 
102
/********************************************************************//**
 
103
Creates the buffer pool.
 
104
@return own: buf_pool object, NULL if not enough memory or error */
 
105
UNIV_INTERN
 
106
buf_pool_t*
 
107
buf_pool_init(void);
 
108
/*===============*/
 
109
/********************************************************************//**
 
110
Frees the buffer pool at shutdown.  This must not be invoked before
 
111
freeing all mutexes. */
 
112
UNIV_INTERN
 
113
void
 
114
buf_pool_free(void);
 
115
/*===============*/
 
116
 
 
117
/********************************************************************//**
 
118
Drops the adaptive hash index.  To prevent a livelock, this function
 
119
is only to be called while holding btr_search_latch and while
 
120
btr_search_enabled == FALSE. */
 
121
UNIV_INTERN
 
122
void
 
123
buf_pool_drop_hash_index(void);
 
124
/*==========================*/
 
125
 
 
126
/********************************************************************//**
 
127
Relocate a buffer control block.  Relocates the block on the LRU list
 
128
and in buf_pool->page_hash.  Does not relocate bpage->list.
 
129
The caller must take care of relocating bpage->list. */
 
130
UNIV_INTERN
 
131
void
 
132
buf_relocate(
 
133
/*=========*/
 
134
        buf_page_t*     bpage,  /*!< in/out: control block being relocated;
 
135
                                buf_page_get_state(bpage) must be
 
136
                                BUF_BLOCK_ZIP_DIRTY or BUF_BLOCK_ZIP_PAGE */
 
137
        buf_page_t*     dpage)  /*!< in/out: destination control block */
 
138
        __attribute__((nonnull));
 
139
/********************************************************************//**
 
140
Resizes the buffer pool. */
 
141
UNIV_INTERN
 
142
void
 
143
buf_pool_resize(void);
 
144
/*=================*/
 
145
/*********************************************************************//**
 
146
Gets the current size of buffer buf_pool in bytes.
 
147
@return size in bytes */
 
148
UNIV_INLINE
 
149
ulint
 
150
buf_pool_get_curr_size(void);
 
151
/*========================*/
 
152
/********************************************************************//**
 
153
Gets the smallest oldest_modification lsn for any page in the pool. Returns
 
154
zero if all modified pages have been flushed to disk.
 
155
@return oldest modification in pool, zero if none */
 
156
UNIV_INLINE
 
157
ib_uint64_t
 
158
buf_pool_get_oldest_modification(void);
 
159
/*==================================*/
 
160
/********************************************************************//**
 
161
Allocates a buffer block.
 
162
@return own: the allocated block, in state BUF_BLOCK_MEMORY */
 
163
UNIV_INLINE
 
164
buf_block_t*
 
165
buf_block_alloc(
 
166
/*============*/
 
167
        ulint   zip_size);      /*!< in: compressed page size in bytes,
 
168
                                or 0 if uncompressed tablespace */
 
169
/********************************************************************//**
 
170
Frees a buffer block which does not contain a file page. */
 
171
UNIV_INLINE
 
172
void
 
173
buf_block_free(
 
174
/*===========*/
 
175
        buf_block_t*    block); /*!< in, own: block to be freed */
 
176
#endif /* !UNIV_HOTBACKUP */
 
177
/*********************************************************************//**
 
178
Copies contents of a buffer frame to a given buffer.
 
179
@return buf */
 
180
UNIV_INLINE
 
181
byte*
 
182
buf_frame_copy(
 
183
/*===========*/
 
184
        byte*                   buf,    /*!< in: buffer to copy to */
 
185
        const buf_frame_t*      frame); /*!< in: buffer frame */
 
186
#ifndef UNIV_HOTBACKUP
 
187
/**************************************************************//**
 
188
NOTE! The following macros should be used instead of buf_page_get_gen,
 
189
to improve debugging. Only values RW_S_LATCH and RW_X_LATCH are allowed
 
190
in LA! */
 
191
#define buf_page_get(SP, ZS, OF, LA, MTR)        buf_page_get_gen(\
 
192
                                SP, ZS, OF, LA, NULL,\
 
193
                                BUF_GET, __FILE__, __LINE__, MTR)
 
194
/**************************************************************//**
 
195
Use these macros to bufferfix a page with no latching. Remember not to
 
196
read the contents of the page unless you know it is safe. Do not modify
 
197
the contents of the page! We have separated this case, because it is
 
198
error-prone programming not to set a latch, and it should be used
 
199
with care. */
 
200
#define buf_page_get_with_no_latch(SP, ZS, OF, MTR)        buf_page_get_gen(\
 
201
                                SP, ZS, OF, RW_NO_LATCH, NULL,\
 
202
                                BUF_GET_NO_LATCH, __FILE__, __LINE__, MTR)
 
203
/**************************************************************//**
 
204
NOTE! The following macros should be used instead of
 
205
buf_page_optimistic_get_func, to improve debugging. Only values RW_S_LATCH and
 
206
RW_X_LATCH are allowed as LA! */
 
207
#define buf_page_optimistic_get(LA, BL, MC, MTR)                             \
 
208
        buf_page_optimistic_get_func(LA, BL, MC, __FILE__, __LINE__, MTR)
 
209
/********************************************************************//**
 
210
This is the general function used to get optimistic access to a database
 
211
page.
 
212
@return TRUE if success */
 
213
UNIV_INTERN
 
214
ibool
 
215
buf_page_optimistic_get_func(
 
216
/*=========================*/
 
217
        ulint           rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH */
 
218
        buf_block_t*    block,  /*!< in: guessed block */
 
219
        ib_uint64_t     modify_clock,/*!< in: modify clock value if mode is
 
220
                                ..._GUESS_ON_CLOCK */
 
221
        const char*     file,   /*!< in: file name */
 
222
        ulint           line,   /*!< in: line where called */
 
223
        mtr_t*          mtr);   /*!< in: mini-transaction */
 
224
/********************************************************************//**
 
225
This is used to get access to a known database page, when no waiting can be
 
226
done.
 
227
@return TRUE if success */
 
228
UNIV_INTERN
 
229
ibool
 
230
buf_page_get_known_nowait(
 
231
/*======================*/
 
232
        ulint           rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH */
 
233
        buf_block_t*    block,  /*!< in: the known page */
 
234
        ulint           mode,   /*!< in: BUF_MAKE_YOUNG or BUF_KEEP_OLD */
 
235
        const char*     file,   /*!< in: file name */
 
236
        ulint           line,   /*!< in: line where called */
 
237
        mtr_t*          mtr);   /*!< in: mini-transaction */
 
238
 
 
239
/*******************************************************************//**
 
240
Given a tablespace id and page number tries to get that page. If the
 
241
page is not in the buffer pool it is not loaded and NULL is returned.
 
242
Suitable for using when holding the kernel mutex. */
 
243
UNIV_INTERN
 
244
const buf_block_t*
 
245
buf_page_try_get_func(
 
246
/*==================*/
 
247
        ulint           space_id,/*!< in: tablespace id */
 
248
        ulint           page_no,/*!< in: page number */
 
249
        const char*     file,   /*!< in: file name */
 
250
        ulint           line,   /*!< in: line where called */
 
251
        mtr_t*          mtr);   /*!< in: mini-transaction */
 
252
 
 
253
/** Tries to get a page. If the page is not in the buffer pool it is
 
254
not loaded.  Suitable for using when holding the kernel mutex.
 
255
@param space_id in: tablespace id
 
256
@param page_no  in: page number
 
257
@param mtr      in: mini-transaction
 
258
@return         the page if in buffer pool, NULL if not */
 
259
#define buf_page_try_get(space_id, page_no, mtr)        \
 
260
        buf_page_try_get_func(space_id, page_no, __FILE__, __LINE__, mtr);
 
261
 
 
262
/********************************************************************//**
 
263
Get read access to a compressed page (usually of type
 
264
FIL_PAGE_TYPE_ZBLOB or FIL_PAGE_TYPE_ZBLOB2).
 
265
The page must be released with buf_page_release_zip().
 
266
NOTE: the page is not protected by any latch.  Mutual exclusion has to
 
267
be implemented at a higher level.  In other words, all possible
 
268
accesses to a given page through this function must be protected by
 
269
the same set of mutexes or latches.
 
270
@return pointer to the block, or NULL if not compressed */
 
271
UNIV_INTERN
 
272
buf_page_t*
 
273
buf_page_get_zip(
 
274
/*=============*/
 
275
        ulint           space,  /*!< in: space id */
 
276
        ulint           zip_size,/*!< in: compressed page size */
 
277
        ulint           offset);/*!< in: page number */
 
278
/********************************************************************//**
 
279
This is the general function used to get access to a database page.
 
280
@return pointer to the block or NULL */
 
281
UNIV_INTERN
 
282
buf_block_t*
 
283
buf_page_get_gen(
 
284
/*=============*/
 
285
        ulint           space,  /*!< in: space id */
 
286
        ulint           zip_size,/*!< in: compressed page size in bytes
 
287
                                or 0 for uncompressed pages */
 
288
        ulint           offset, /*!< in: page number */
 
289
        ulint           rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
 
290
        buf_block_t*    guess,  /*!< in: guessed block or NULL */
 
291
        ulint           mode,   /*!< in: BUF_GET, BUF_GET_IF_IN_POOL,
 
292
                                BUF_GET_NO_LATCH */
 
293
        const char*     file,   /*!< in: file name */
 
294
        ulint           line,   /*!< in: line where called */
 
295
        mtr_t*          mtr);   /*!< in: mini-transaction */
 
296
/********************************************************************//**
 
297
Initializes a page to the buffer buf_pool. The page is usually not read
 
298
from a file even if it cannot be found in the buffer buf_pool. This is one
 
299
of the functions which perform to a block a state transition NOT_USED =>
 
300
FILE_PAGE (the other is buf_page_get_gen).
 
301
@return pointer to the block, page bufferfixed */
 
302
UNIV_INTERN
 
303
buf_block_t*
 
304
buf_page_create(
 
305
/*============*/
 
306
        ulint   space,  /*!< in: space id */
 
307
        ulint   offset, /*!< in: offset of the page within space in units of
 
308
                        a page */
 
309
        ulint   zip_size,/*!< in: compressed page size, or 0 */
 
310
        mtr_t*  mtr);   /*!< in: mini-transaction handle */
 
311
#else /* !UNIV_HOTBACKUP */
 
312
/********************************************************************//**
 
313
Inits a page to the buffer buf_pool, for use in ibbackup --restore. */
 
314
UNIV_INTERN
 
315
void
 
316
buf_page_init_for_backup_restore(
 
317
/*=============================*/
 
318
        ulint           space,  /*!< in: space id */
 
319
        ulint           offset, /*!< in: offset of the page within space
 
320
                                in units of a page */
 
321
        ulint           zip_size,/*!< in: compressed page size in bytes
 
322
                                or 0 for uncompressed pages */
 
323
        buf_block_t*    block); /*!< in: block to init */
 
324
#endif /* !UNIV_HOTBACKUP */
 
325
 
 
326
#ifndef UNIV_HOTBACKUP
 
327
/********************************************************************//**
 
328
Releases a compressed-only page acquired with buf_page_get_zip(). */
 
329
UNIV_INLINE
 
330
void
 
331
buf_page_release_zip(
 
332
/*=================*/
 
333
        buf_page_t*     bpage);         /*!< in: buffer block */
 
334
/********************************************************************//**
 
335
Decrements the bufferfix count of a buffer control block and releases
 
336
a latch, if specified. */
 
337
UNIV_INLINE
 
338
void
 
339
buf_page_release(
 
340
/*=============*/
 
341
        buf_block_t*    block,          /*!< in: buffer block */
 
342
        ulint           rw_latch,       /*!< in: RW_S_LATCH, RW_X_LATCH,
 
343
                                        RW_NO_LATCH */
 
344
        mtr_t*          mtr);           /*!< in: mtr */
 
345
/********************************************************************//**
 
346
Moves a page to the start of the buffer pool LRU list. This high-level
 
347
function can be used to prevent an important page from from slipping out of
 
348
the buffer pool. */
 
349
UNIV_INTERN
 
350
void
 
351
buf_page_make_young(
 
352
/*================*/
 
353
        buf_page_t*     bpage); /*!< in: buffer block of a file page */
 
354
/********************************************************************//**
 
355
Returns TRUE if the page can be found in the buffer pool hash table.
 
356
 
 
357
NOTE that it is possible that the page is not yet read from disk,
 
358
though.
 
359
 
 
360
@return TRUE if found in the page hash table */
 
361
UNIV_INLINE
 
362
ibool
 
363
buf_page_peek(
 
364
/*==========*/
 
365
        ulint   space,  /*!< in: space id */
 
366
        ulint   offset);/*!< in: page number */
 
367
/********************************************************************//**
 
368
Resets the check_index_page_at_flush field of a page if found in the buffer
 
369
pool. */
 
370
UNIV_INTERN
 
371
void
 
372
buf_reset_check_index_page_at_flush(
 
373
/*================================*/
 
374
        ulint   space,  /*!< in: space id */
 
375
        ulint   offset);/*!< in: page number */
 
376
#ifdef UNIV_DEBUG_FILE_ACCESSES
 
377
/********************************************************************//**
 
378
Sets file_page_was_freed TRUE if the page is found in the buffer pool.
 
379
This function should be called when we free a file page and want the
 
380
debug version to check that it is not accessed any more unless
 
381
reallocated.
 
382
@return control block if found in page hash table, otherwise NULL */
 
383
UNIV_INTERN
 
384
buf_page_t*
 
385
buf_page_set_file_page_was_freed(
 
386
/*=============================*/
 
387
        ulint   space,  /*!< in: space id */
 
388
        ulint   offset);/*!< in: page number */
 
389
/********************************************************************//**
 
390
Sets file_page_was_freed FALSE if the page is found in the buffer pool.
 
391
This function should be called when we free a file page and want the
 
392
debug version to check that it is not accessed any more unless
 
393
reallocated.
 
394
@return control block if found in page hash table, otherwise NULL */
 
395
UNIV_INTERN
 
396
buf_page_t*
 
397
buf_page_reset_file_page_was_freed(
 
398
/*===============================*/
 
399
        ulint   space,  /*!< in: space id */
 
400
        ulint   offset);        /*!< in: page number */
 
401
#endif /* UNIV_DEBUG_FILE_ACCESSES */
 
402
/********************************************************************//**
 
403
Reads the freed_page_clock of a buffer block.
 
404
@return freed_page_clock */
 
405
UNIV_INLINE
 
406
ulint
 
407
buf_page_get_freed_page_clock(
 
408
/*==========================*/
 
409
        const buf_page_t*       bpage)  /*!< in: block */
 
410
        __attribute__((pure));
 
411
/********************************************************************//**
 
412
Reads the freed_page_clock of a buffer block.
 
413
@return freed_page_clock */
 
414
UNIV_INLINE
 
415
ulint
 
416
buf_block_get_freed_page_clock(
 
417
/*===========================*/
 
418
        const buf_block_t*      block)  /*!< in: block */
 
419
        __attribute__((pure));
 
420
 
 
421
/********************************************************************//**
 
422
Recommends a move of a block to the start of the LRU list if there is danger
 
423
of dropping from the buffer pool. NOTE: does not reserve the buffer pool
 
424
mutex.
 
425
@return TRUE if should be made younger */
 
426
UNIV_INLINE
 
427
ibool
 
428
buf_page_peek_if_too_old(
 
429
/*=====================*/
 
430
        const buf_page_t*       bpage); /*!< in: block to make younger */
 
431
/********************************************************************//**
 
432
Returns the current state of is_hashed of a page. FALSE if the page is
 
433
not in the pool. NOTE that this operation does not fix the page in the
 
434
pool if it is found there.
 
435
@return TRUE if page hash index is built in search system */
 
436
UNIV_INTERN
 
437
ibool
 
438
buf_page_peek_if_search_hashed(
 
439
/*===========================*/
 
440
        ulint   space,  /*!< in: space id */
 
441
        ulint   offset);/*!< in: page number */
 
442
/********************************************************************//**
 
443
Gets the youngest modification log sequence number for a frame.
 
444
Returns zero if not file page or no modification occurred yet.
 
445
@return newest modification to page */
 
446
UNIV_INLINE
 
447
ib_uint64_t
 
448
buf_page_get_newest_modification(
 
449
/*=============================*/
 
450
        const buf_page_t*       bpage); /*!< in: block containing the
 
451
                                        page frame */
 
452
/********************************************************************//**
 
453
Increments the modify clock of a frame by 1. The caller must (1) own the
 
454
buf_pool mutex and block bufferfix count has to be zero, (2) or own an x-lock
 
455
on the block. */
 
456
UNIV_INLINE
 
457
void
 
458
buf_block_modify_clock_inc(
 
459
/*=======================*/
 
460
        buf_block_t*    block); /*!< in: block */
 
461
/********************************************************************//**
 
462
Returns the value of the modify clock. The caller must have an s-lock
 
463
or x-lock on the block.
 
464
@return value */
 
465
UNIV_INLINE
 
466
ib_uint64_t
 
467
buf_block_get_modify_clock(
 
468
/*=======================*/
 
469
        buf_block_t*    block); /*!< in: block */
 
470
#else /* !UNIV_HOTBACKUP */
 
471
# define buf_block_modify_clock_inc(block) ((void) 0)
 
472
#endif /* !UNIV_HOTBACKUP */
 
473
/********************************************************************//**
 
474
Calculates a page checksum which is stored to the page when it is written
 
475
to a file. Note that we must be careful to calculate the same value
 
476
on 32-bit and 64-bit architectures.
 
477
@return checksum */
 
478
UNIV_INTERN
 
479
ulint
 
480
buf_calc_page_new_checksum(
 
481
/*=======================*/
 
482
        const byte*     page);  /*!< in: buffer page */
 
483
/********************************************************************//**
 
484
In versions < 4.0.14 and < 4.1.1 there was a bug that the checksum only
 
485
looked at the first few bytes of the page. This calculates that old
 
486
checksum.
 
487
NOTE: we must first store the new formula checksum to
 
488
FIL_PAGE_SPACE_OR_CHKSUM before calculating and storing this old checksum
 
489
because this takes that field as an input!
 
490
@return checksum */
 
491
UNIV_INTERN
 
492
ulint
 
493
buf_calc_page_old_checksum(
 
494
/*=======================*/
 
495
        const byte*      page); /*!< in: buffer page */
 
496
/********************************************************************//**
 
497
Checks if a page is corrupt.
 
498
@return TRUE if corrupted */
 
499
UNIV_INTERN
 
500
ibool
 
501
buf_page_is_corrupted(
 
502
/*==================*/
 
503
        const byte*     read_buf,       /*!< in: a database page */
 
504
        ulint           zip_size);      /*!< in: size of compressed page;
 
505
                                        0 for uncompressed pages */
 
506
#ifndef UNIV_HOTBACKUP
 
507
/**********************************************************************//**
 
508
Gets the space id, page offset, and byte offset within page of a
 
509
pointer pointing to a buffer frame containing a file page. */
 
510
UNIV_INLINE
 
511
void
 
512
buf_ptr_get_fsp_addr(
 
513
/*=================*/
 
514
        const void*     ptr,    /*!< in: pointer to a buffer frame */
 
515
        ulint*          space,  /*!< out: space id */
 
516
        fil_addr_t*     addr);  /*!< out: page offset and byte offset */
 
517
/**********************************************************************//**
 
518
Gets the hash value of a block. This can be used in searches in the
 
519
lock hash table.
 
520
@return lock hash value */
 
521
UNIV_INLINE
 
522
ulint
 
523
buf_block_get_lock_hash_val(
 
524
/*========================*/
 
525
        const buf_block_t*      block)  /*!< in: block */
 
526
        __attribute__((pure));
 
527
#ifdef UNIV_DEBUG
 
528
/*********************************************************************//**
 
529
Finds a block in the buffer pool that points to a
 
530
given compressed page.
 
531
@return buffer block pointing to the compressed page, or NULL */
 
532
UNIV_INTERN
 
533
buf_block_t*
 
534
buf_pool_contains_zip(
 
535
/*==================*/
 
536
        const void*     data);  /*!< in: pointer to compressed page */
 
537
#endif /* UNIV_DEBUG */
 
538
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
 
539
/*********************************************************************//**
 
540
Validates the buffer pool data structure.
 
541
@return TRUE */
 
542
UNIV_INTERN
 
543
ibool
 
544
buf_validate(void);
 
545
/*==============*/
 
546
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
 
547
#if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
 
548
/*********************************************************************//**
 
549
Prints info of the buffer pool data structure. */
 
550
UNIV_INTERN
 
551
void
 
552
buf_print(void);
 
553
/*============*/
 
554
#endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */
 
555
#endif /* !UNIV_HOTBACKUP */
 
556
/********************************************************************//**
 
557
Prints a page to stderr. */
 
558
UNIV_INTERN
 
559
void
 
560
buf_page_print(
 
561
/*===========*/
 
562
        const byte*     read_buf,       /*!< in: a database page */
 
563
        ulint           zip_size);      /*!< in: compressed page size, or
 
564
                                        0 for uncompressed pages */
 
565
/********************************************************************//**
 
566
Decompress a block.
 
567
@return TRUE if successful */
 
568
UNIV_INTERN
 
569
ibool
 
570
buf_zip_decompress(
 
571
/*===============*/
 
572
        buf_block_t*    block,  /*!< in/out: block */
 
573
        ibool           check); /*!< in: TRUE=verify the page checksum */
 
574
#ifndef UNIV_HOTBACKUP
 
575
#ifdef UNIV_DEBUG
 
576
/*********************************************************************//**
 
577
Returns the number of latched pages in the buffer pool.
 
578
@return number of latched pages */
 
579
UNIV_INTERN
 
580
ulint
 
581
buf_get_latched_pages_number(void);
 
582
/*==============================*/
 
583
#endif /* UNIV_DEBUG */
 
584
/*********************************************************************//**
 
585
Returns the number of pending buf pool ios.
 
586
@return number of pending I/O operations */
 
587
UNIV_INTERN
 
588
ulint
 
589
buf_get_n_pending_ios(void);
 
590
/*=======================*/
 
591
/*********************************************************************//**
 
592
Prints info of the buffer i/o. */
 
593
UNIV_INTERN
 
594
void
 
595
buf_print_io(
 
596
/*=========*/
 
597
        FILE*   file);  /*!< in: file where to print */
 
598
/*********************************************************************//**
 
599
Returns the ratio in percents of modified pages in the buffer pool /
 
600
database pages in the buffer pool.
 
601
@return modified page percentage ratio */
 
602
UNIV_INTERN
 
603
ulint
 
604
buf_get_modified_ratio_pct(void);
 
605
/*============================*/
 
606
/**********************************************************************//**
 
607
Refreshes the statistics used to print per-second averages. */
 
608
UNIV_INTERN
 
609
void
 
610
buf_refresh_io_stats(void);
 
611
/*======================*/
 
612
/*********************************************************************//**
 
613
Asserts that all file pages in the buffer are in a replaceable state.
 
614
@return TRUE */
 
615
UNIV_INTERN
 
616
ibool
 
617
buf_all_freed(void);
 
618
/*===============*/
 
619
/*********************************************************************//**
 
620
Checks that there currently are no pending i/o-operations for the buffer
 
621
pool.
 
622
@return TRUE if there is no pending i/o */
 
623
UNIV_INTERN
 
624
ibool
 
625
buf_pool_check_no_pending_io(void);
 
626
/*==============================*/
 
627
/*********************************************************************//**
 
628
Invalidates the file pages in the buffer pool when an archive recovery is
 
629
completed. All the file pages buffered must be in a replaceable state when
 
630
this function is called: not latched and not modified. */
 
631
UNIV_INTERN
 
632
void
 
633
buf_pool_invalidate(void);
 
634
/*=====================*/
 
635
#endif /* !UNIV_HOTBACKUP */
 
636
 
 
637
/*========================================================================
 
638
--------------------------- LOWER LEVEL ROUTINES -------------------------
 
639
=========================================================================*/
 
640
 
 
641
#ifdef UNIV_SYNC_DEBUG
 
642
/*********************************************************************//**
 
643
Adds latch level info for the rw-lock protecting the buffer frame. This
 
644
should be called in the debug version after a successful latching of a
 
645
page if we know the latching order level of the acquired latch. */
 
646
UNIV_INLINE
 
647
void
 
648
buf_block_dbg_add_level(
 
649
/*====================*/
 
650
        buf_block_t*    block,  /*!< in: buffer page
 
651
                                where we have acquired latch */
 
652
        ulint           level); /*!< in: latching order level */
 
653
#else /* UNIV_SYNC_DEBUG */
 
654
# define buf_block_dbg_add_level(block, level) /* nothing */
 
655
#endif /* UNIV_SYNC_DEBUG */
 
656
/*********************************************************************//**
 
657
Gets the state of a block.
 
658
@return state */
 
659
UNIV_INLINE
 
660
enum buf_page_state
 
661
buf_page_get_state(
 
662
/*===============*/
 
663
        const buf_page_t*       bpage); /*!< in: pointer to the control block */
 
664
/*********************************************************************//**
 
665
Gets the state of a block.
 
666
@return state */
 
667
UNIV_INLINE
 
668
enum buf_page_state
 
669
buf_block_get_state(
 
670
/*================*/
 
671
        const buf_block_t*      block)  /*!< in: pointer to the control block */
 
672
        __attribute__((pure));
 
673
/*********************************************************************//**
 
674
Sets the state of a block. */
 
675
UNIV_INLINE
 
676
void
 
677
buf_page_set_state(
 
678
/*===============*/
 
679
        buf_page_t*             bpage,  /*!< in/out: pointer to control block */
 
680
        enum buf_page_state     state); /*!< in: state */
 
681
/*********************************************************************//**
 
682
Sets the state of a block. */
 
683
UNIV_INLINE
 
684
void
 
685
buf_block_set_state(
 
686
/*================*/
 
687
        buf_block_t*            block,  /*!< in/out: pointer to control block */
 
688
        enum buf_page_state     state); /*!< in: state */
 
689
/*********************************************************************//**
 
690
Determines if a block is mapped to a tablespace.
 
691
@return TRUE if mapped */
 
692
UNIV_INLINE
 
693
ibool
 
694
buf_page_in_file(
 
695
/*=============*/
 
696
        const buf_page_t*       bpage)  /*!< in: pointer to control block */
 
697
        __attribute__((pure));
 
698
#ifndef UNIV_HOTBACKUP
 
699
/*********************************************************************//**
 
700
Determines if a block should be on unzip_LRU list.
 
701
@return TRUE if block belongs to unzip_LRU */
 
702
UNIV_INLINE
 
703
ibool
 
704
buf_page_belongs_to_unzip_LRU(
 
705
/*==========================*/
 
706
        const buf_page_t*       bpage)  /*!< in: pointer to control block */
 
707
        __attribute__((pure));
 
708
/*********************************************************************//**
 
709
Determine the approximate LRU list position of a block.
 
710
@return LRU list position */
 
711
UNIV_INLINE
 
712
ulint
 
713
buf_page_get_LRU_position(
 
714
/*======================*/
 
715
        const buf_page_t*       bpage)  /*!< in: control block */
 
716
        __attribute__((pure));
 
717
 
 
718
/*********************************************************************//**
 
719
Gets the mutex of a block.
 
720
@return pointer to mutex protecting bpage */
 
721
UNIV_INLINE
 
722
mutex_t*
 
723
buf_page_get_mutex(
 
724
/*===============*/
 
725
        const buf_page_t*       bpage)  /*!< in: pointer to control block */
 
726
        __attribute__((pure));
 
727
 
 
728
/*********************************************************************//**
 
729
Get the flush type of a page.
 
730
@return flush type */
 
731
UNIV_INLINE
 
732
enum buf_flush
 
733
buf_page_get_flush_type(
 
734
/*====================*/
 
735
        const buf_page_t*       bpage)  /*!< in: buffer page */
 
736
        __attribute__((pure));
 
737
/*********************************************************************//**
 
738
Set the flush type of a page. */
 
739
UNIV_INLINE
 
740
void
 
741
buf_page_set_flush_type(
 
742
/*====================*/
 
743
        buf_page_t*     bpage,          /*!< in: buffer page */
 
744
        enum buf_flush  flush_type);    /*!< in: flush type */
 
745
/*********************************************************************//**
 
746
Map a block to a file page. */
 
747
UNIV_INLINE
 
748
void
 
749
buf_block_set_file_page(
 
750
/*====================*/
 
751
        buf_block_t*            block,  /*!< in/out: pointer to control block */
 
752
        ulint                   space,  /*!< in: tablespace id */
 
753
        ulint                   page_no);/*!< in: page number */
 
754
/*********************************************************************//**
 
755
Gets the io_fix state of a block.
 
756
@return io_fix state */
 
757
UNIV_INLINE
 
758
enum buf_io_fix
 
759
buf_page_get_io_fix(
 
760
/*================*/
 
761
        const buf_page_t*       bpage)  /*!< in: pointer to the control block */
 
762
        __attribute__((pure));
 
763
/*********************************************************************//**
 
764
Gets the io_fix state of a block.
 
765
@return io_fix state */
 
766
UNIV_INLINE
 
767
enum buf_io_fix
 
768
buf_block_get_io_fix(
 
769
/*================*/
 
770
        const buf_block_t*      block)  /*!< in: pointer to the control block */
 
771
        __attribute__((pure));
 
772
/*********************************************************************//**
 
773
Sets the io_fix state of a block. */
 
774
UNIV_INLINE
 
775
void
 
776
buf_page_set_io_fix(
 
777
/*================*/
 
778
        buf_page_t*     bpage,  /*!< in/out: control block */
 
779
        enum buf_io_fix io_fix);/*!< in: io_fix state */
 
780
/*********************************************************************//**
 
781
Sets the io_fix state of a block. */
 
782
UNIV_INLINE
 
783
void
 
784
buf_block_set_io_fix(
 
785
/*=================*/
 
786
        buf_block_t*    block,  /*!< in/out: control block */
 
787
        enum buf_io_fix io_fix);/*!< in: io_fix state */
 
788
 
 
789
/********************************************************************//**
 
790
Determine if a buffer block can be relocated in memory.  The block
 
791
can be dirty, but it must not be I/O-fixed or bufferfixed. */
 
792
UNIV_INLINE
 
793
ibool
 
794
buf_page_can_relocate(
 
795
/*==================*/
 
796
        const buf_page_t*       bpage)  /*!< control block being relocated */
 
797
        __attribute__((pure));
 
798
 
 
799
/*********************************************************************//**
 
800
Determine if a block has been flagged old.
 
801
@return TRUE if old */
 
802
UNIV_INLINE
 
803
ibool
 
804
buf_page_is_old(
 
805
/*============*/
 
806
        const buf_page_t*       bpage)  /*!< in: control block */
 
807
        __attribute__((pure));
 
808
/*********************************************************************//**
 
809
Flag a block old. */
 
810
UNIV_INLINE
 
811
void
 
812
buf_page_set_old(
 
813
/*=============*/
 
814
        buf_page_t*     bpage,  /*!< in/out: control block */
 
815
        ibool           old);   /*!< in: old */
 
816
/*********************************************************************//**
 
817
Determine if a block has been accessed in the buffer pool.
 
818
@return TRUE if accessed */
 
819
UNIV_INLINE
 
820
ibool
 
821
buf_page_is_accessed(
 
822
/*=================*/
 
823
        const buf_page_t*       bpage)  /*!< in: control block */
 
824
        __attribute__((pure));
 
825
/*********************************************************************//**
 
826
Flag a block accessed. */
 
827
UNIV_INLINE
 
828
void
 
829
buf_page_set_accessed(
 
830
/*==================*/
 
831
        buf_page_t*     bpage,          /*!< in/out: control block */
 
832
        ibool           accessed);      /*!< in: accessed */
 
833
/*********************************************************************//**
 
834
Gets the buf_block_t handle of a buffered file block if an uncompressed
 
835
page frame exists, or NULL.
 
836
@return control block, or NULL */
 
837
UNIV_INLINE
 
838
buf_block_t*
 
839
buf_page_get_block(
 
840
/*===============*/
 
841
        buf_page_t*     bpage)  /*!< in: control block, or NULL */
 
842
        __attribute__((pure));
 
843
#endif /* !UNIV_HOTBACKUP */
 
844
#ifdef UNIV_DEBUG
 
845
/*********************************************************************//**
 
846
Gets a pointer to the memory frame of a block.
 
847
@return pointer to the frame */
 
848
UNIV_INLINE
 
849
buf_frame_t*
 
850
buf_block_get_frame(
 
851
/*================*/
 
852
        const buf_block_t*      block)  /*!< in: pointer to the control block */
 
853
        __attribute__((pure));
 
854
#else /* UNIV_DEBUG */
 
855
# define buf_block_get_frame(block) (block)->frame
 
856
#endif /* UNIV_DEBUG */
 
857
/*********************************************************************//**
 
858
Gets the space id of a block.
 
859
@return space id */
 
860
UNIV_INLINE
 
861
ulint
 
862
buf_page_get_space(
 
863
/*===============*/
 
864
        const buf_page_t*       bpage)  /*!< in: pointer to the control block */
 
865
        __attribute__((pure));
 
866
/*********************************************************************//**
 
867
Gets the space id of a block.
 
868
@return space id */
 
869
UNIV_INLINE
 
870
ulint
 
871
buf_block_get_space(
 
872
/*================*/
 
873
        const buf_block_t*      block)  /*!< in: pointer to the control block */
 
874
        __attribute__((pure));
 
875
/*********************************************************************//**
 
876
Gets the page number of a block.
 
877
@return page number */
 
878
UNIV_INLINE
 
879
ulint
 
880
buf_page_get_page_no(
 
881
/*=================*/
 
882
        const buf_page_t*       bpage)  /*!< in: pointer to the control block */
 
883
        __attribute__((pure));
 
884
/*********************************************************************//**
 
885
Gets the page number of a block.
 
886
@return page number */
 
887
UNIV_INLINE
 
888
ulint
 
889
buf_block_get_page_no(
 
890
/*==================*/
 
891
        const buf_block_t*      block)  /*!< in: pointer to the control block */
 
892
        __attribute__((pure));
 
893
/*********************************************************************//**
 
894
Gets the compressed page size of a block.
 
895
@return compressed page size, or 0 */
 
896
UNIV_INLINE
 
897
ulint
 
898
buf_page_get_zip_size(
 
899
/*==================*/
 
900
        const buf_page_t*       bpage)  /*!< in: pointer to the control block */
 
901
        __attribute__((pure));
 
902
/*********************************************************************//**
 
903
Gets the compressed page size of a block.
 
904
@return compressed page size, or 0 */
 
905
UNIV_INLINE
 
906
ulint
 
907
buf_block_get_zip_size(
 
908
/*===================*/
 
909
        const buf_block_t*      block)  /*!< in: pointer to the control block */
 
910
        __attribute__((pure));
 
911
/*********************************************************************//**
 
912
Gets the compressed page descriptor corresponding to an uncompressed page
 
913
if applicable. */
 
914
#define buf_block_get_page_zip(block) \
 
915
        (UNIV_LIKELY_NULL((block)->page.zip.data) ? &(block)->page.zip : NULL)
 
916
#ifndef UNIV_HOTBACKUP
 
917
/*******************************************************************//**
 
918
Gets the block to whose frame the pointer is pointing to.
 
919
@return pointer to block, never NULL */
 
920
UNIV_INTERN
 
921
buf_block_t*
 
922
buf_block_align(
 
923
/*============*/
 
924
        const byte*     ptr);   /*!< in: pointer to a frame */
 
925
/********************************************************************//**
 
926
Find out if a pointer belongs to a buf_block_t. It can be a pointer to
 
927
the buf_block_t itself or a member of it
 
928
@return TRUE if ptr belongs to a buf_block_t struct */
 
929
UNIV_INTERN
 
930
ibool
 
931
buf_pointer_is_block_field(
 
932
/*=======================*/
 
933
        const void*             ptr);   /*!< in: pointer not
 
934
                                        dereferenced */
 
935
/** Find out if a pointer corresponds to a buf_block_t::mutex.
 
936
@param m        in: mutex candidate
 
937
@return         TRUE if m is a buf_block_t::mutex */
 
938
#define buf_pool_is_block_mutex(m)                      \
 
939
        buf_pointer_is_block_field((const void*)(m))
 
940
/** Find out if a pointer corresponds to a buf_block_t::lock.
 
941
@param l        in: rw-lock candidate
 
942
@return         TRUE if l is a buf_block_t::lock */
 
943
#define buf_pool_is_block_lock(l)                       \
 
944
        buf_pointer_is_block_field((const void*)(l))
 
945
 
 
946
#if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
 
947
/*********************************************************************//**
 
948
Gets the compressed page descriptor corresponding to an uncompressed page
 
949
if applicable.
 
950
@return compressed page descriptor, or NULL */
 
951
UNIV_INLINE
 
952
const page_zip_des_t*
 
953
buf_frame_get_page_zip(
 
954
/*===================*/
 
955
        const byte*     ptr);   /*!< in: pointer to the page */
 
956
#endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
 
957
/********************************************************************//**
 
958
Function which inits a page for read to the buffer buf_pool. If the page is
 
959
(1) already in buf_pool, or
 
960
(2) if we specify to read only ibuf pages and the page is not an ibuf page, or
 
961
(3) if the space is deleted or being deleted,
 
962
then this function does nothing.
 
963
Sets the io_fix flag to BUF_IO_READ and sets a non-recursive exclusive lock
 
964
on the buffer frame. The io-handler must take care that the flag is cleared
 
965
and the lock released later.
 
966
@return pointer to the block or NULL */
 
967
UNIV_INTERN
 
968
buf_page_t*
 
969
buf_page_init_for_read(
 
970
/*===================*/
 
971
        ulint*          err,    /*!< out: DB_SUCCESS or DB_TABLESPACE_DELETED */
 
972
        ulint           mode,   /*!< in: BUF_READ_IBUF_PAGES_ONLY, ... */
 
973
        ulint           space,  /*!< in: space id */
 
974
        ulint           zip_size,/*!< in: compressed page size, or 0 */
 
975
        ibool           unzip,  /*!< in: TRUE=request uncompressed page */
 
976
        ib_int64_t      tablespace_version,/*!< in: prevents reading from a wrong
 
977
                                version of the tablespace in case we have done
 
978
                                DISCARD + IMPORT */
 
979
        ulint           offset);/*!< in: page number */
 
980
/********************************************************************//**
 
981
Completes an asynchronous read or write request of a file page to or from
 
982
the buffer pool. */
 
983
UNIV_INTERN
 
984
void
 
985
buf_page_io_complete(
 
986
/*=================*/
 
987
        buf_page_t*     bpage); /*!< in: pointer to the block in question */
 
988
/********************************************************************//**
 
989
Calculates a folded value of a file page address to use in the page hash
 
990
table.
 
991
@return the folded value */
 
992
UNIV_INLINE
 
993
ulint
 
994
buf_page_address_fold(
 
995
/*==================*/
 
996
        ulint   space,  /*!< in: space id */
 
997
        ulint   offset) /*!< in: offset of the page within space */
 
998
        __attribute__((const));
 
999
/******************************************************************//**
 
1000
Returns the control block of a file page, NULL if not found.
 
1001
@return block, NULL if not found */
 
1002
UNIV_INLINE
 
1003
buf_page_t*
 
1004
buf_page_hash_get(
 
1005
/*==============*/
 
1006
        ulint   space,  /*!< in: space id */
 
1007
        ulint   offset);/*!< in: offset of the page within space */
 
1008
/******************************************************************//**
 
1009
Returns the control block of a file page, NULL if not found
 
1010
or an uncompressed page frame does not exist.
 
1011
@return block, NULL if not found */
 
1012
UNIV_INLINE
 
1013
buf_block_t*
 
1014
buf_block_hash_get(
 
1015
/*===============*/
 
1016
        ulint   space,  /*!< in: space id */
 
1017
        ulint   offset);/*!< in: offset of the page within space */
 
1018
/*******************************************************************//**
 
1019
Increments the pool clock by one and returns its new value. Remember that
 
1020
in the 32 bit version the clock wraps around at 4 billion!
 
1021
@return new clock value */
 
1022
UNIV_INLINE
 
1023
ulint
 
1024
buf_pool_clock_tic(void);
 
1025
/*====================*/
 
1026
/*********************************************************************//**
 
1027
Gets the current length of the free list of buffer blocks.
 
1028
@return length of the free list */
 
1029
UNIV_INTERN
 
1030
ulint
 
1031
buf_get_free_list_len(void);
 
1032
/*=======================*/
 
1033
#endif /* !UNIV_HOTBACKUP */
 
1034
 
 
1035
 
 
1036
/** The common buffer control block structure
 
1037
for compressed and uncompressed frames */
 
1038
 
 
1039
struct buf_page_struct{
 
1040
        /** @name General fields
 
1041
        None of these bit-fields must be modified without holding
 
1042
        buf_page_get_mutex() [buf_block_struct::mutex or
 
1043
        buf_pool_zip_mutex], since they can be stored in the same
 
1044
        machine word.  Some of these fields are additionally protected
 
1045
        by buf_pool_mutex. */
 
1046
        /* @{ */
 
1047
 
 
1048
        unsigned        space:32;       /*!< tablespace id; also protected
 
1049
                                        by buf_pool_mutex. */
 
1050
        unsigned        offset:32;      /*!< page number; also protected
 
1051
                                        by buf_pool_mutex. */
 
1052
 
 
1053
        unsigned        state:3;        /*!< state of the control block; also
 
1054
                                        protected by buf_pool_mutex.
 
1055
                                        State transitions from
 
1056
                                        BUF_BLOCK_READY_FOR_USE to
 
1057
                                        BUF_BLOCK_MEMORY need not be
 
1058
                                        protected by buf_page_get_mutex().
 
1059
                                        @see enum buf_page_state */
 
1060
#ifndef UNIV_HOTBACKUP
 
1061
        unsigned        flush_type:2;   /*!< if this block is currently being
 
1062
                                        flushed to disk, this tells the
 
1063
                                        flush_type.
 
1064
                                        @see enum buf_flush */
 
1065
        unsigned        accessed:1;     /*!< TRUE if the page has been accessed
 
1066
                                        while in the buffer pool: read-ahead
 
1067
                                        may read in pages which have not been
 
1068
                                        accessed yet; a thread is allowed to
 
1069
                                        read this for heuristic purposes
 
1070
                                        without holding any mutex or latch */
 
1071
        unsigned        io_fix:2;       /*!< type of pending I/O operation;
 
1072
                                        also protected by buf_pool_mutex
 
1073
                                        @see enum buf_io_fix */
 
1074
        unsigned        buf_fix_count:24;/*!< count of how manyfold this block
 
1075
                                        is currently bufferfixed */
 
1076
        /* @} */
 
1077
#endif /* !UNIV_HOTBACKUP */
 
1078
        page_zip_des_t  zip;            /*!< compressed page; zip.data
 
1079
                                        (but not the data it points to) is
 
1080
                                        also protected by buf_pool_mutex */
 
1081
#ifndef UNIV_HOTBACKUP
 
1082
        buf_page_t*     hash;           /*!< node used in chaining to
 
1083
                                        buf_pool->page_hash or
 
1084
                                        buf_pool->zip_hash */
 
1085
#ifdef UNIV_DEBUG
 
1086
        ibool           in_page_hash;   /*!< TRUE if in buf_pool->page_hash */
 
1087
        ibool           in_zip_hash;    /*!< TRUE if in buf_pool->zip_hash */
 
1088
#endif /* UNIV_DEBUG */
 
1089
 
 
1090
        /** @name Page flushing fields
 
1091
        All these are protected by buf_pool_mutex. */
 
1092
        /* @{ */
 
1093
 
 
1094
        UT_LIST_NODE_T(buf_page_t) list;
 
1095
                                        /*!< based on state, this is a
 
1096
                                        list node, protected only by
 
1097
                                        buf_pool_mutex, in one of the
 
1098
                                        following lists in buf_pool:
 
1099
 
 
1100
                                        - BUF_BLOCK_NOT_USED:   free
 
1101
                                        - BUF_BLOCK_FILE_PAGE:  flush_list
 
1102
                                        - BUF_BLOCK_ZIP_DIRTY:  flush_list
 
1103
                                        - BUF_BLOCK_ZIP_PAGE:   zip_clean
 
1104
                                        - BUF_BLOCK_ZIP_FREE:   zip_free[] */
 
1105
#ifdef UNIV_DEBUG
 
1106
        ibool           in_flush_list;  /*!< TRUE if in buf_pool->flush_list;
 
1107
                                        when buf_pool_mutex is free, the
 
1108
                                        following should hold: in_flush_list
 
1109
                                        == (state == BUF_BLOCK_FILE_PAGE
 
1110
                                            || state == BUF_BLOCK_ZIP_DIRTY) */
 
1111
        ibool           in_free_list;   /*!< TRUE if in buf_pool->free; when
 
1112
                                        buf_pool_mutex is free, the following
 
1113
                                        should hold: in_free_list
 
1114
                                        == (state == BUF_BLOCK_NOT_USED) */
 
1115
#endif /* UNIV_DEBUG */
 
1116
        ib_uint64_t     newest_modification;
 
1117
                                        /*!< log sequence number of
 
1118
                                        the youngest modification to
 
1119
                                        this block, zero if not
 
1120
                                        modified */
 
1121
        ib_uint64_t     oldest_modification;
 
1122
                                        /*!< log sequence number of
 
1123
                                        the START of the log entry
 
1124
                                        written of the oldest
 
1125
                                        modification to this block
 
1126
                                        which has not yet been flushed
 
1127
                                        on disk; zero if all
 
1128
                                        modifications are on disk */
 
1129
        /* @} */
 
1130
        /** @name LRU replacement algorithm fields
 
1131
        These fields are protected by buf_pool_mutex only (not
 
1132
        buf_pool_zip_mutex or buf_block_struct::mutex). */
 
1133
        /* @{ */
 
1134
 
 
1135
        UT_LIST_NODE_T(buf_page_t) LRU;
 
1136
                                        /*!< node of the LRU list */
 
1137
#ifdef UNIV_DEBUG
 
1138
        ibool           in_LRU_list;    /*!< TRUE if the page is in
 
1139
                                        the LRU list; used in
 
1140
                                        debugging */
 
1141
#endif /* UNIV_DEBUG */
 
1142
        unsigned        old:1;          /*!< TRUE if the block is in the old
 
1143
                                        blocks in the LRU list */
 
1144
        unsigned        LRU_position:31;/*!< value which monotonically
 
1145
                                        decreases (or may stay
 
1146
                                        constant if old==TRUE) toward
 
1147
                                        the end of the LRU list, if
 
1148
                                        buf_pool->ulint_clock has not
 
1149
                                        wrapped around: NOTE that this
 
1150
                                        value can only be used in
 
1151
                                        heuristic algorithms, because
 
1152
                                        of the possibility of a
 
1153
                                        wrap-around! */
 
1154
        unsigned        freed_page_clock:32;/*!< the value of
 
1155
                                        buf_pool->freed_page_clock
 
1156
                                        when this block was the last
 
1157
                                        time put to the head of the
 
1158
                                        LRU list; a thread is allowed
 
1159
                                        to read this for heuristic
 
1160
                                        purposes without holding any
 
1161
                                        mutex or latch */
 
1162
        /* @} */
 
1163
# ifdef UNIV_DEBUG_FILE_ACCESSES
 
1164
        ibool           file_page_was_freed;
 
1165
                                        /*!< this is set to TRUE when fsp
 
1166
                                        frees a page in buffer pool */
 
1167
# endif /* UNIV_DEBUG_FILE_ACCESSES */
 
1168
#endif /* !UNIV_HOTBACKUP */
 
1169
};
 
1170
 
 
1171
/** The buffer control block structure */
 
1172
 
 
1173
struct buf_block_struct{
 
1174
 
 
1175
        /** @name General fields */
 
1176
        /* @{ */
 
1177
 
 
1178
        buf_page_t      page;           /*!< page information; this must
 
1179
                                        be the first field, so that
 
1180
                                        buf_pool->page_hash can point
 
1181
                                        to buf_page_t or buf_block_t */
 
1182
        byte*           frame;          /*!< pointer to buffer frame which
 
1183
                                        is of size UNIV_PAGE_SIZE, and
 
1184
                                        aligned to an address divisible by
 
1185
                                        UNIV_PAGE_SIZE */
 
1186
#ifndef UNIV_HOTBACKUP
 
1187
        UT_LIST_NODE_T(buf_block_t) unzip_LRU;
 
1188
                                        /*!< node of the decompressed LRU list;
 
1189
                                        a block is in the unzip_LRU list
 
1190
                                        if page.state == BUF_BLOCK_FILE_PAGE
 
1191
                                        and page.zip.data != NULL */
 
1192
#ifdef UNIV_DEBUG
 
1193
        ibool           in_unzip_LRU_list;/*!< TRUE if the page is in the
 
1194
                                        decompressed LRU list;
 
1195
                                        used in debugging */
 
1196
#endif /* UNIV_DEBUG */
 
1197
        mutex_t         mutex;          /*!< mutex protecting this block:
 
1198
                                        state (also protected by the buffer
 
1199
                                        pool mutex), io_fix, buf_fix_count,
 
1200
                                        and accessed; we introduce this new
 
1201
                                        mutex in InnoDB-5.1 to relieve
 
1202
                                        contention on the buffer pool mutex */
 
1203
        rw_lock_t       lock;           /*!< read-write lock of the buffer
 
1204
                                        frame */
 
1205
        unsigned        lock_hash_val:32;/*!< hashed value of the page address
 
1206
                                        in the record lock hash table */
 
1207
        unsigned        check_index_page_at_flush:1;
 
1208
                                        /*!< TRUE if we know that this is
 
1209
                                        an index page, and want the database
 
1210
                                        to check its consistency before flush;
 
1211
                                        note that there may be pages in the
 
1212
                                        buffer pool which are index pages,
 
1213
                                        but this flag is not set because
 
1214
                                        we do not keep track of all pages */
 
1215
        /* @} */
 
1216
        /** @name Optimistic search field */
 
1217
        /* @{ */
 
1218
 
 
1219
        ib_uint64_t     modify_clock;   /*!< this clock is incremented every
 
1220
                                        time a pointer to a record on the
 
1221
                                        page may become obsolete; this is
 
1222
                                        used in the optimistic cursor
 
1223
                                        positioning: if the modify clock has
 
1224
                                        not changed, we know that the pointer
 
1225
                                        is still valid; this field may be
 
1226
                                        changed if the thread (1) owns the
 
1227
                                        pool mutex and the page is not
 
1228
                                        bufferfixed, or (2) the thread has an
 
1229
                                        x-latch on the block */
 
1230
        /* @} */
 
1231
        /** @name Hash search fields (unprotected)
 
1232
        NOTE that these fields are NOT protected by any semaphore! */
 
1233
        /* @{ */
 
1234
 
 
1235
        ulint           n_hash_helps;   /*!< counter which controls building
 
1236
                                        of a new hash index for the page */
 
1237
        ulint           n_fields;       /*!< recommended prefix length for hash
 
1238
                                        search: number of full fields */
 
1239
        ulint           n_bytes;        /*!< recommended prefix: number of bytes
 
1240
                                        in an incomplete field */
 
1241
        ibool           left_side;      /*!< TRUE or FALSE, depending on
 
1242
                                        whether the leftmost record of several
 
1243
                                        records with the same prefix should be
 
1244
                                        indexed in the hash index */
 
1245
        /* @} */
 
1246
 
 
1247
        /** @name Hash search fields
 
1248
        These 6 fields may only be modified when we have
 
1249
        an x-latch on btr_search_latch AND
 
1250
        - we are holding an s-latch or x-latch on buf_block_struct::lock or
 
1251
        - we know that buf_block_struct::buf_fix_count == 0.
 
1252
 
 
1253
        An exception to this is when we init or create a page
 
1254
        in the buffer pool in buf0buf.c. */
 
1255
 
 
1256
        /* @{ */
 
1257
 
 
1258
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
 
1259
        ulint           n_pointers;     /*!< used in debugging: the number of
 
1260
                                        pointers in the adaptive hash index
 
1261
                                        pointing to this frame */
 
1262
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
 
1263
        unsigned        is_hashed:1;    /*!< TRUE if hash index has
 
1264
                                        already been built on this
 
1265
                                        page; note that it does not
 
1266
                                        guarantee that the index is
 
1267
                                        complete, though: there may
 
1268
                                        have been hash collisions,
 
1269
                                        record deletions, etc. */
 
1270
        unsigned        curr_n_fields:10;/*!< prefix length for hash indexing:
 
1271
                                        number of full fields */
 
1272
        unsigned        curr_n_bytes:15;/*!< number of bytes in hash
 
1273
                                        indexing */
 
1274
        unsigned        curr_left_side:1;/*!< TRUE or FALSE in hash indexing */
 
1275
        dict_index_t*   index;          /*!< Index for which the adaptive
 
1276
                                        hash index has been created. */
 
1277
        /* @} */
 
1278
# ifdef UNIV_SYNC_DEBUG
 
1279
        /** @name Debug fields */
 
1280
        /* @{ */
 
1281
        rw_lock_t       debug_latch;    /*!< in the debug version, each thread
 
1282
                                        which bufferfixes the block acquires
 
1283
                                        an s-latch here; so we can use the
 
1284
                                        debug utilities in sync0rw */
 
1285
        /* @} */
 
1286
# endif
 
1287
#endif /* !UNIV_HOTBACKUP */
 
1288
};
 
1289
 
 
1290
/** Check if a buf_block_t object is in a valid state
 
1291
@param block    buffer block
 
1292
@return         TRUE if valid */
 
1293
#define buf_block_state_valid(block)                            \
 
1294
(buf_block_get_state(block) >= BUF_BLOCK_NOT_USED               \
 
1295
 && (buf_block_get_state(block) <= BUF_BLOCK_REMOVE_HASH))
 
1296
 
 
1297
#ifndef UNIV_HOTBACKUP
 
1298
/**********************************************************************//**
 
1299
Compute the hash fold value for blocks in buf_pool->zip_hash. */
 
1300
/* @{ */
 
1301
#define BUF_POOL_ZIP_FOLD_PTR(ptr) ((ulint) (ptr) / UNIV_PAGE_SIZE)
 
1302
#define BUF_POOL_ZIP_FOLD(b) BUF_POOL_ZIP_FOLD_PTR((b)->frame)
 
1303
#define BUF_POOL_ZIP_FOLD_BPAGE(b) BUF_POOL_ZIP_FOLD((buf_block_t*) (b))
 
1304
/* @} */
 
1305
 
 
1306
/** @brief The buffer pool structure.
 
1307
 
 
1308
NOTE! The definition appears here only for other modules of this
 
1309
directory (buf) to see it. Do not use from outside! */
 
1310
 
 
1311
struct buf_pool_struct{
 
1312
 
 
1313
        /** @name General fields */
 
1314
        /* @{ */
 
1315
 
 
1316
        ulint           n_chunks;       /*!< number of buffer pool chunks */
 
1317
        buf_chunk_t*    chunks;         /*!< buffer pool chunks */
 
1318
        ulint           curr_size;      /*!< current pool size in pages */
 
1319
        hash_table_t*   page_hash;      /*!< hash table of buf_page_t or
 
1320
                                        buf_block_t file pages,
 
1321
                                        buf_page_in_file() == TRUE,
 
1322
                                        indexed by (space_id, offset) */
 
1323
        hash_table_t*   zip_hash;       /*!< hash table of buf_block_t blocks
 
1324
                                        whose frames are allocated to the
 
1325
                                        zip buddy system,
 
1326
                                        indexed by block->frame */
 
1327
        ulint           n_pend_reads;   /*!< number of pending read operations */
 
1328
        ulint           n_pend_unzip;   /*!< number of pending decompressions */
 
1329
 
 
1330
        time_t          last_printout_time; /*!< when buf_print was last time
 
1331
                                        called */
 
1332
        ulint           n_pages_read;   /*!< number read operations */
 
1333
        ulint           n_pages_written;/*!< number write operations */
 
1334
        ulint           n_pages_created;/*!< number of pages created
 
1335
                                        in the pool with no read */
 
1336
        ulint           n_page_gets;    /*!< number of page gets performed;
 
1337
                                        also successful searches through
 
1338
                                        the adaptive hash index are
 
1339
                                        counted as page gets; this field
 
1340
                                        is NOT protected by the buffer
 
1341
                                        pool mutex */
 
1342
        ulint           n_page_gets_old;/*!< n_page_gets when buf_print was
 
1343
                                        last time called: used to calculate
 
1344
                                        hit rate */
 
1345
        ulint           n_pages_read_old;/*!< n_pages_read when buf_print was
 
1346
                                        last time called */
 
1347
        ulint           n_pages_written_old;/*!< number write operations */
 
1348
        ulint           n_pages_created_old;/*!< number of pages created in
 
1349
                                        the pool with no read */
 
1350
        /* @} */
 
1351
        /** @name Page flushing algorithm fields */
 
1352
        /* @{ */
 
1353
 
 
1354
        UT_LIST_BASE_NODE_T(buf_page_t) flush_list;
 
1355
                                        /*!< base node of the modified block
 
1356
                                        list */
 
1357
        ibool           init_flush[BUF_FLUSH_N_TYPES];
 
1358
                                        /*!< this is TRUE when a flush of the
 
1359
                                        given type is being initialized */
 
1360
        ulint           n_flush[BUF_FLUSH_N_TYPES];
 
1361
                                        /*!< this is the number of pending
 
1362
                                        writes in the given flush type */
 
1363
        os_event_t      no_flush[BUF_FLUSH_N_TYPES];
 
1364
                                        /*!< this is in the set state
 
1365
                                        when there is no flush batch
 
1366
                                        of the given type running */
 
1367
        ulint           ulint_clock;    /*!< a sequence number used to count
 
1368
                                        time. NOTE! This counter wraps
 
1369
                                        around at 4 billion (if ulint ==
 
1370
                                        32 bits)! */
 
1371
        ulint           freed_page_clock;/*!< a sequence number used
 
1372
                                        to count the number of buffer
 
1373
                                        blocks removed from the end of
 
1374
                                        the LRU list; NOTE that this
 
1375
                                        counter may wrap around at 4
 
1376
                                        billion! A thread is allowed
 
1377
                                        to read this for heuristic
 
1378
                                        purposes without holding any
 
1379
                                        mutex or latch */
 
1380
        ulint           LRU_flush_ended;/*!< when an LRU flush ends for a page,
 
1381
                                        this is incremented by one; this is
 
1382
                                        set to zero when a buffer block is
 
1383
                                        allocated */
 
1384
 
 
1385
        /* @} */
 
1386
        /** @name LRU replacement algorithm fields */
 
1387
        /* @{ */
 
1388
 
 
1389
        UT_LIST_BASE_NODE_T(buf_page_t) free;
 
1390
                                        /*!< base node of the free
 
1391
                                        block list */
 
1392
        UT_LIST_BASE_NODE_T(buf_page_t) LRU;
 
1393
                                        /*!< base node of the LRU list */
 
1394
        buf_page_t*     LRU_old;        /*!< pointer to the about 3/8 oldest
 
1395
                                        blocks in the LRU list; NULL if LRU
 
1396
                                        length less than BUF_LRU_OLD_MIN_LEN;
 
1397
                                        NOTE: when LRU_old != NULL, its length
 
1398
                                        should always equal LRU_old_len */
 
1399
        ulint           LRU_old_len;    /*!< length of the LRU list from
 
1400
                                        the block to which LRU_old points
 
1401
                                        onward, including that block;
 
1402
                                        see buf0lru.c for the restrictions
 
1403
                                        on this value; not defined if
 
1404
                                        LRU_old == NULL;
 
1405
                                        NOTE: LRU_old_len must be adjusted
 
1406
                                        whenever LRU_old shrinks or grows! */
 
1407
 
 
1408
        UT_LIST_BASE_NODE_T(buf_block_t) unzip_LRU;
 
1409
                                        /*!< base node of the
 
1410
                                        unzip_LRU list */
 
1411
 
 
1412
        /* @} */
 
1413
        /** @name Buddy allocator fields
 
1414
        The buddy allocator is used for allocating compressed page
 
1415
        frames and buf_page_t descriptors of blocks that exist
 
1416
        in the buffer pool only in compressed form. */
 
1417
        /* @{ */
 
1418
        UT_LIST_BASE_NODE_T(buf_page_t) zip_clean;
 
1419
                                        /*!< unmodified compressed pages */
 
1420
        UT_LIST_BASE_NODE_T(buf_page_t) zip_free[BUF_BUDDY_SIZES];
 
1421
                                        /*!< buddy free lists */
 
1422
#if BUF_BUDDY_HIGH != UNIV_PAGE_SIZE
 
1423
# error "BUF_BUDDY_HIGH != UNIV_PAGE_SIZE"
 
1424
#endif
 
1425
#if BUF_BUDDY_LOW > PAGE_ZIP_MIN_SIZE
 
1426
# error "BUF_BUDDY_LOW > PAGE_ZIP_MIN_SIZE"
 
1427
#endif
 
1428
        /* @} */
 
1429
};
 
1430
 
 
1431
/** mutex protecting the buffer pool struct and control blocks, except the
 
1432
read-write lock in them */
 
1433
extern mutex_t  buf_pool_mutex;
 
1434
/** mutex protecting the control blocks of compressed-only pages
 
1435
(of type buf_page_t, not buf_block_t) */
 
1436
extern mutex_t  buf_pool_zip_mutex;
 
1437
 
 
1438
/** @name Accessors for buf_pool_mutex.
 
1439
Use these instead of accessing buf_pool_mutex directly. */
 
1440
/* @{ */
 
1441
 
 
1442
/** Test if buf_pool_mutex is owned. */
 
1443
#define buf_pool_mutex_own() mutex_own(&buf_pool_mutex)
 
1444
/** Acquire the buffer pool mutex. */
 
1445
#define buf_pool_mutex_enter() do {             \
 
1446
        ut_ad(!mutex_own(&buf_pool_zip_mutex)); \
 
1447
        mutex_enter(&buf_pool_mutex);           \
 
1448
} while (0)
 
1449
 
 
1450
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
 
1451
/** Flag to forbid the release of the buffer pool mutex.
 
1452
Protected by buf_pool_mutex. */
 
1453
extern ulint    buf_pool_mutex_exit_forbidden;
 
1454
/** Forbid the release of the buffer pool mutex. */
 
1455
# define buf_pool_mutex_exit_forbid() do {      \
 
1456
        ut_ad(buf_pool_mutex_own());            \
 
1457
        buf_pool_mutex_exit_forbidden++;        \
 
1458
} while (0)
 
1459
/** Allow the release of the buffer pool mutex. */
 
1460
# define buf_pool_mutex_exit_allow() do {       \
 
1461
        ut_ad(buf_pool_mutex_own());            \
 
1462
        ut_a(buf_pool_mutex_exit_forbidden);    \
 
1463
        buf_pool_mutex_exit_forbidden--;        \
 
1464
} while (0)
 
1465
/** Release the buffer pool mutex. */
 
1466
# define buf_pool_mutex_exit() do {             \
 
1467
        ut_a(!buf_pool_mutex_exit_forbidden);   \
 
1468
        mutex_exit(&buf_pool_mutex);            \
 
1469
} while (0)
 
1470
#else
 
1471
/** Forbid the release of the buffer pool mutex. */
 
1472
# define buf_pool_mutex_exit_forbid() ((void) 0)
 
1473
/** Allow the release of the buffer pool mutex. */
 
1474
# define buf_pool_mutex_exit_allow() ((void) 0)
 
1475
/** Release the buffer pool mutex. */
 
1476
# define buf_pool_mutex_exit() mutex_exit(&buf_pool_mutex)
 
1477
#endif
 
1478
#endif /* !UNIV_HOTBACKUP */
 
1479
/* @} */
 
1480
 
 
1481
/**********************************************************************
 
1482
Let us list the consistency conditions for different control block states.
 
1483
 
 
1484
NOT_USED:       is in free list, not in LRU list, not in flush list, nor
 
1485
                page hash table
 
1486
READY_FOR_USE:  is not in free list, LRU list, or flush list, nor page
 
1487
                hash table
 
1488
MEMORY:         is not in free list, LRU list, or flush list, nor page
 
1489
                hash table
 
1490
FILE_PAGE:      space and offset are defined, is in page hash table
 
1491
                if io_fix == BUF_IO_WRITE,
 
1492
                        pool: no_flush[flush_type] is in reset state,
 
1493
                        pool: n_flush[flush_type] > 0
 
1494
 
 
1495
                (1) if buf_fix_count == 0, then
 
1496
                        is in LRU list, not in free list
 
1497
                        is in flush list,
 
1498
                                if and only if oldest_modification > 0
 
1499
                        is x-locked,
 
1500
                                if and only if io_fix == BUF_IO_READ
 
1501
                        is s-locked,
 
1502
                                if and only if io_fix == BUF_IO_WRITE
 
1503
 
 
1504
                (2) if buf_fix_count > 0, then
 
1505
                        is not in LRU list, not in free list
 
1506
                        is in flush list,
 
1507
                                if and only if oldest_modification > 0
 
1508
                        if io_fix == BUF_IO_READ,
 
1509
                                is x-locked
 
1510
                        if io_fix == BUF_IO_WRITE,
 
1511
                                is s-locked
 
1512
 
 
1513
State transitions:
 
1514
 
 
1515
NOT_USED => READY_FOR_USE
 
1516
READY_FOR_USE => MEMORY
 
1517
READY_FOR_USE => FILE_PAGE
 
1518
MEMORY => NOT_USED
 
1519
FILE_PAGE => NOT_USED   NOTE: This transition is allowed if and only if
 
1520
                                (1) buf_fix_count == 0,
 
1521
                                (2) oldest_modification == 0, and
 
1522
                                (3) io_fix == 0.
 
1523
*/
 
1524
 
 
1525
#ifndef UNIV_NONINL
 
1526
#include "buf0buf.ic"
 
1527
#endif
 
1528
 
 
1529
#endif