47
46
it is error-prone programming
48
47
not to set a latch, and it
49
48
should be used with care */
50
#define BUF_GET_IF_IN_POOL_OR_WATCH 15
51
/*!< Get the page only if it's in the
52
buffer pool, if not then set a watch
55
50
/** @name Modes for buf_page_get_known_nowait */
63
58
position of the block. */
66
#define MAX_BUFFER_POOLS 64 /*!< The maximum number of buffer
67
pools that can be defined */
69
#define BUF_POOL_WATCH_SIZE 1 /*!< Maximum number of concurrent
70
buffer pool watches */
72
extern buf_pool_t* buf_pool_ptr[MAX_BUFFER_POOLS]; /*!< The buffer pools
61
extern buf_pool_t* buf_pool; /*!< The buffer pool of the database */
75
63
extern ibool buf_debug_prints;/*!< If this is set TRUE, the program
76
64
prints info whenever read or flush
115
101
#ifndef UNIV_HOTBACKUP
116
102
/********************************************************************//**
117
Acquire mutex on all buffer pool instances */
120
buf_pool_mutex_enter_all(void);
121
/*===========================*/
123
/********************************************************************//**
124
Release mutex on all buffer pool instances */
127
buf_pool_mutex_exit_all(void);
128
/*==========================*/
130
/********************************************************************//**
131
103
Creates the buffer pool.
132
104
@return own: buf_pool object, NULL if not enough memory or error */
137
ulint size, /*!< in: Size of the total pool in bytes */
138
ulint n_instances); /*!< in: Number of instances */
139
109
/********************************************************************//**
140
110
Frees the buffer pool at shutdown. This must not be invoked before
141
111
freeing all mutexes. */
146
ulint n_instances); /*!< in: numbere of instances to free */
148
117
/********************************************************************//**
149
118
Drops the adaptive hash index. To prevent a livelock, this function
181
150
buf_pool_get_curr_size(void);
182
151
/*========================*/
183
/*********************************************************************//**
184
Gets the current size of buffer buf_pool in frames.
185
@return size in pages */
188
buf_pool_get_n_pages(void);
189
/*=======================*/
190
152
/********************************************************************//**
191
153
Gets the smallest oldest_modification lsn for any page in the pool. Returns
192
154
zero if all modified pages have been flushed to disk.
193
155
@return oldest modification in pool, zero if none */
196
158
buf_pool_get_oldest_modification(void);
197
159
/*==================================*/
198
160
/********************************************************************//**
199
161
Allocates a buffer block.
200
162
@return own: the allocated block, in state BUF_BLOCK_MEMORY */
205
buf_pool_t* buf_pool, /*!< buffer pool instance */
206
ulint zip_size); /*!< in: compressed page size in bytes,
207
or 0 if uncompressed tablespace */
167
ulint zip_size); /*!< in: compressed page size in bytes,
168
or 0 if uncompressed tablespace */
208
169
/********************************************************************//**
209
170
Frees a buffer block which does not contain a file page. */
239
200
#define buf_page_get_with_no_latch(SP, ZS, OF, MTR) buf_page_get_gen(\
240
201
SP, ZS, OF, RW_NO_LATCH, NULL,\
241
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)
242
209
/********************************************************************//**
243
210
This is the general function used to get optimistic access to a database
245
212
@return TRUE if success */
248
buf_page_optimistic_get(
249
/*====================*/
215
buf_page_optimistic_get_func(
216
/*=========================*/
250
217
ulint rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH */
251
218
buf_block_t* block, /*!< in: guessed block */
252
219
ib_uint64_t modify_clock,/*!< in: modify clock value if mode is
322
289
ulint rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
323
290
buf_block_t* guess, /*!< in: guessed block or NULL */
324
291
ulint mode, /*!< in: BUF_GET, BUF_GET_IF_IN_POOL,
326
BUF_GET_IF_IN_POOL_OR_WATCH */
327
293
const char* file, /*!< in: file name */
328
294
ulint line, /*!< in: line where called */
329
295
mtr_t* mtr); /*!< in: mini-transaction */
373
339
buf_page_release(
374
340
/*=============*/
375
341
buf_block_t* block, /*!< in: buffer block */
376
ulint rw_latch); /*!< in: RW_S_LATCH, RW_X_LATCH,
342
ulint rw_latch, /*!< in: RW_S_LATCH, RW_X_LATCH,
344
mtr_t* mtr); /*!< in: mtr */
378
345
/********************************************************************//**
379
346
Moves a page to the start of the buffer pool LRU list. This high-level
380
347
function can be used to prevent an important page from slipping out of
641
607
Refreshes the statistics used to print per-second averages. */
644
buf_refresh_io_stats(
645
/*=================*/
646
buf_pool_t* buf_pool); /*!< buffer pool instance */
647
/**********************************************************************//**
648
Refreshes the statistics used to print per-second averages. */
651
buf_refresh_io_stats_all(void);
652
/*=================*/
610
buf_refresh_io_stats(void);
611
/*======================*/
653
612
/*********************************************************************//**
654
613
Asserts that all file pages in the buffer are in a replaceable state.
865
824
__attribute__((nonnull));
866
825
/*********************************************************************//**
867
826
Gets the buf_block_t handle of a buffered file block if an uncompressed
868
page frame exists, or NULL. Note: even though bpage is not declared a
869
const we don't update its value. It is safe to make this pure.
827
page frame exists, or NULL.
870
828
@return control block, or NULL */
1031
989
ulint offset) /*!< in: offset of the page within space */
1032
990
__attribute__((const));
1033
991
/******************************************************************//**
1034
Returns the buffer pool instance given a page instance
1038
buf_pool_from_bpage(
1039
/*================*/
1040
const buf_page_t* bpage); /*!< in: buffer pool page */
1041
/******************************************************************//**
1042
Returns the buffer pool instance given a block instance
1046
buf_pool_from_block(
1047
/*================*/
1048
const buf_block_t* block); /*!< in: block */
1049
/******************************************************************//**
1050
Returns the buffer pool instance given space and offset of page
1051
@return buffer pool */
1056
ulint space, /*!< in: space id */
1057
ulint offset);/*!< in: offset of the page within space */
1058
/******************************************************************//**
1059
Returns the buffer pool instance given its array index
1060
@return buffer pool */
1063
buf_pool_from_array(
1064
/*====================*/
1065
ulint index); /*!< in: array index to get buffer pool instance from */
1066
/******************************************************************//**
1067
992
Returns the control block of a file page, NULL if not found.
1068
993
@return block, NULL if not found */
1071
buf_page_hash_get_low(
1072
/*==================*/
1073
buf_pool_t* buf_pool, /*!< buffer pool instance */
1074
ulint space, /*!< in: space id */
1075
ulint offset, /*!< in: offset of the page
1077
ulint fold); /*!< in: buf_page_address_fold(
1079
/******************************************************************//**
1080
Returns the control block of a file page, NULL if not found.
1081
@return block, NULL if not found or not a real control block */
1084
996
buf_page_hash_get(
1085
997
/*==============*/
1086
buf_pool_t* buf_pool, /*!< in: buffer pool instance */
1087
ulint space, /*!< in: space id */
1088
ulint offset); /*!< in: offset of the page
998
ulint space, /*!< in: space id */
999
ulint offset);/*!< in: offset of the page within space */
1090
1000
/******************************************************************//**
1091
1001
Returns the control block of a file page, NULL if not found
1092
1002
or an uncompressed page frame does not exist.
1096
1006
buf_block_hash_get(
1097
1007
/*===============*/
1098
buf_pool_t* buf_pool, /*!< in: buffer pool instance */
1099
ulint space, /*!< in: space id */
1100
ulint offset); /*!< in: offset of the page
1008
ulint space, /*!< in: space id */
1009
ulint offset);/*!< in: offset of the page within space */
1102
1010
/*********************************************************************//**
1103
1011
Gets the current length of the free list of buffer blocks.
1104
1012
@return length of the free list */
1107
1015
buf_get_free_list_len(void);
1108
1016
/*=======================*/
1110
/********************************************************************
1111
Determine if a block is a sentinel for a buffer pool watch.
1112
@return TRUE if a sentinel for a buffer pool watch, FALSE if not */
1115
buf_pool_watch_is_sentinel(
1116
/*=======================*/
1117
buf_pool_t* buf_pool, /*!< buffer pool instance */
1118
const buf_page_t* bpage) /*!< in: block */
1119
UNIV_WARN_UNUSED_RESULT;
1120
/****************************************************************//**
1121
Add watch for the given page to be read in. Caller must have the buffer pool
1122
@return NULL if watch set, block if the page is in the buffer pool */
1127
ulint space, /*!< in: space id */
1128
ulint offset, /*!< in: page number */
1129
ulint fold) /*!< in: buf_page_address_fold(space, offset) */
1130
UNIV_WARN_UNUSED_RESULT;
1131
/****************************************************************//**
1132
Stop watching if the page has been read in.
1133
buf_pool_watch_set(space,offset) must have returned NULL before. */
1136
buf_pool_watch_unset(
1137
/*=================*/
1138
ulint space, /*!< in: space id */
1139
ulint offset);/*!< in: page number */
1140
/****************************************************************//**
1141
Check if the page has been read in.
1142
This may only be called after buf_pool_watch_set(space,offset)
1143
has returned NULL and before invoking buf_pool_watch_unset(space,offset).
1144
@return FALSE if the given page was not read in, TRUE if it was */
1147
buf_pool_watch_occurred(
1148
/*====================*/
1149
ulint space, /*!< in: space id */
1150
ulint offset) /*!< in: page number */
1151
UNIV_WARN_UNUSED_RESULT;
1152
/********************************************************************//**
1153
Get total buffer pool statistics. */
1156
buf_get_total_list_len(
1157
/*===================*/
1158
ulint* LRU_len, /*!< out: length of all LRU lists */
1159
ulint* free_len, /*!< out: length of all free lists */
1160
ulint* flush_list_len);/*!< out: length of all flush lists */
1161
/********************************************************************//**
1162
Get total buffer pool statistics. */
1167
buf_pool_stat_t*tot_stat); /*!< out: buffer pool stats */
1169
1017
#endif /* !UNIV_HOTBACKUP */
1171
1020
/** The common buffer control block structure
1172
1021
for compressed and uncompressed frames */
1175
1024
/** @name General fields
1176
1025
None of these bit-fields must be modified without holding
1177
1026
buf_page_get_mutex() [buf_block_struct::mutex or
1178
buf_pool->zip_mutex], since they can be stored in the same
1027
buf_pool_zip_mutex], since they can be stored in the same
1179
1028
machine word. Some of these fields are additionally protected
1180
by buf_pool->mutex. */
1029
by buf_pool_mutex. */
1183
1032
unsigned space:32; /*!< tablespace id; also protected
1184
by buf_pool->mutex. */
1033
by buf_pool_mutex. */
1185
1034
unsigned offset:32; /*!< page number; also protected
1186
by buf_pool->mutex. */
1035
by buf_pool_mutex. */
1188
1037
unsigned state:3; /*!< state of the control block; also
1189
protected by buf_pool->mutex.
1038
protected by buf_pool_mutex.
1190
1039
State transitions from
1191
1040
BUF_BLOCK_READY_FOR_USE to
1192
1041
BUF_BLOCK_MEMORY need not be
1206
1055
#endif /* !UNIV_HOTBACKUP */
1207
1056
page_zip_des_t zip; /*!< compressed page; zip.data
1208
1057
(but not the data it points to) is
1209
also protected by buf_pool_mutex;
1210
state == BUF_BLOCK_ZIP_PAGE and
1211
zip.data == NULL means an active
1058
also protected by buf_pool_mutex */
1213
1059
#ifndef UNIV_HOTBACKUP
1214
1060
buf_page_t* hash; /*!< node used in chaining to
1215
1061
buf_pool->page_hash or
1226
1072
UT_LIST_NODE_T(buf_page_t) list;
1227
1073
/*!< based on state, this is a
1228
list node, protected either by
1229
buf_pool_mutex or by
1230
flush_list_mutex, in one of the
1074
list node, protected only by
1075
buf_pool_mutex, in one of the
1231
1076
following lists in buf_pool:
1233
1078
- BUF_BLOCK_NOT_USED: free
1236
1081
- BUF_BLOCK_ZIP_PAGE: zip_clean
1237
1082
- BUF_BLOCK_ZIP_FREE: zip_free[]
1239
If bpage is part of flush_list
1240
then the node pointers are
1241
covered by flush_list_mutex.
1242
Otherwise these pointers are
1243
protected by buf_pool_mutex.
1245
1084
The contents of the list node
1246
1085
is undefined if !in_flush_list
1247
1086
&& state == BUF_BLOCK_FILE_PAGE,
1253
1092
#ifdef UNIV_DEBUG
1254
1093
ibool in_flush_list; /*!< TRUE if in buf_pool->flush_list;
1255
when flush_list_mutex is free, the
1094
when buf_pool_mutex is free, the
1256
1095
following should hold: in_flush_list
1257
1096
== (state == BUF_BLOCK_FILE_PAGE
1258
|| state == BUF_BLOCK_ZIP_DIRTY)
1259
Writes to this field must be
1260
covered by both block->mutex
1261
and flush_list_mutex. Hence
1262
reads can happen while holding
1263
any one of the two mutexes */
1097
|| state == BUF_BLOCK_ZIP_DIRTY) */
1264
1098
ibool in_free_list; /*!< TRUE if in buf_pool->free; when
1265
1099
buf_pool_mutex is free, the following
1266
1100
should hold: in_free_list
1279
1112
modification to this block
1280
1113
which has not yet been flushed
1281
1114
on disk; zero if all
1282
modifications are on disk.
1283
Writes to this field must be
1284
covered by both block->mutex
1285
and flush_list_mutex. Hence
1286
reads can happen while holding
1287
any one of the two mutexes */
1115
modifications are on disk */
1289
1117
/** @name LRU replacement algorithm fields
1290
These fields are protected by buf_pool->mutex only (not
1291
buf_pool->zip_mutex or buf_block_struct::mutex). */
1118
These fields are protected by buf_pool_mutex only (not
1119
buf_pool_zip_mutex or buf_block_struct::mutex). */
1294
1122
UT_LIST_NODE_T(buf_page_t) LRU;
1357
1183
rw_lock_t lock; /*!< read-write lock of the buffer
1359
1185
unsigned lock_hash_val:32;/*!< hashed value of the page address
1360
in the record lock hash table;
1361
protected by buf_block_t::lock
1362
(or buf_block_t::mutex, buf_pool->mutex
1363
in buf_page_get_gen(),
1364
buf_page_init_for_read()
1365
and buf_page_create()) */
1366
ibool check_index_page_at_flush;
1186
in the record lock hash table */
1187
unsigned check_index_page_at_flush:1;
1367
1188
/*!< TRUE if we know that this is
1368
1189
an index page, and want the database
1369
1190
to check its consistency before flush;
1370
1191
note that there may be pages in the
1371
1192
buffer pool which are index pages,
1372
1193
but this flag is not set because
1373
we do not keep track of all pages;
1374
NOT protected by any mutex */
1194
we do not keep track of all pages */
1376
1196
/** @name Optimistic search field */
1488
1308
buf_page_peek_if_too_old() */
1491
/** Statistics of buddy blocks of a given size. */
1492
struct buf_buddy_stat_struct {
1493
/** Number of blocks allocated from the buddy system. */
1495
/** Number of blocks relocated by the buddy system. */
1496
ib_uint64_t relocated;
1497
/** Total duration of block relocations, in microseconds. */
1498
ib_uint64_t relocated_usec;
1501
1311
/** @brief The buffer pool structure.
1503
1313
NOTE! The definition appears here only for other modules of this
1508
1318
/** @name General fields */
1510
mutex_t mutex; /*!< Buffer pool mutex of this
1512
mutex_t zip_mutex; /*!< Zip mutex of this buffer
1513
pool instance, protects compressed
1514
only pages (of type buf_page_t, not
1516
ulint instance_no; /*!< Array index of this buffer
1518
ulint old_pool_size; /*!< Old pool size in bytes */
1519
ulint curr_pool_size; /*!< Current pool size in bytes */
1520
ulint LRU_old_ratio; /*!< Reserve this much of the buffer
1521
pool for "old" blocks */
1523
ulint buddy_n_frames; /*!< Number of frames allocated from
1524
the buffer pool to the buddy system */
1526
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
1527
ulint mutex_exit_forbidden; /*!< Forbid release mutex */
1529
1321
ulint n_chunks; /*!< number of buffer pool chunks */
1530
1322
buf_chunk_t* chunks; /*!< buffer pool chunks */
1531
1323
ulint curr_size; /*!< current pool size in pages */
1537
1329
whose frames are allocated to the
1538
1330
zip buddy system,
1539
1331
indexed by block->frame */
1540
ulint n_pend_reads; /*!< number of pending read
1332
ulint n_pend_reads; /*!< number of pending read operations */
1542
1333
ulint n_pend_unzip; /*!< number of pending decompressions */
1544
1335
time_t last_printout_time;
1545
1336
/*!< when buf_print_io was last time
1547
buf_buddy_stat_t buddy_stat[BUF_BUDDY_SIZES + 1];
1548
/*!< Statistics of buddy system,
1549
indexed by block size */
1550
1338
buf_pool_stat_t stat; /*!< current statistics */
1551
1339
buf_pool_stat_t old_stat; /*!< old statistics */
1559
mutex_t flush_list_mutex;/*!< mutex protecting the
1560
flush list access. This mutex
1561
protects flush_list, flush_rbt
1562
and bpage::list pointers when
1563
the bpage is on flush_list. It
1564
also protects writes to
1565
bpage::oldest_modification */
1566
1347
UT_LIST_BASE_NODE_T(buf_page_t) flush_list;
1567
1348
/*!< base node of the modified block
1576
1357
/*!< this is in the set state
1577
1358
when there is no flush batch
1578
1359
of the given type running */
1579
ib_rbt_t* flush_rbt; /*!< a red-black tree is used
1580
exclusively during recovery to
1581
speed up insertions in the
1582
flush_list. This tree contains
1584
oldest_modification LSN and is
1585
kept in sync with the
1587
Each member of the tree MUST
1588
also be on the flush_list.
1589
This tree is relevant only in
1590
recovery and is set to NULL
1591
once the recovery is over.
1592
Protected by flush_list_mutex */
1593
1360
ulint freed_page_clock;/*!< a sequence number used
1594
1361
to count the number of buffer
1595
1362
blocks removed from the end of
1671
1432
/** Test if buf_pool_mutex is owned. */
1672
#define buf_pool_mutex_own(b) mutex_own(&b->mutex)
1433
#define buf_pool_mutex_own() mutex_own(&buf_pool_mutex)
1673
1434
/** Acquire the buffer pool mutex. */
1674
#define buf_pool_mutex_enter(b) do { \
1675
ut_ad(!mutex_own(&b->zip_mutex)); \
1676
mutex_enter(&b->mutex); \
1679
/** Test if flush list mutex is owned. */
1680
#define buf_flush_list_mutex_own(b) mutex_own(&b->flush_list_mutex)
1682
/** Acquire the flush list mutex. */
1683
#define buf_flush_list_mutex_enter(b) do { \
1684
mutex_enter(&b->flush_list_mutex); \
1686
/** Release the flush list mutex. */
1687
# define buf_flush_list_mutex_exit(b) do { \
1688
mutex_exit(&b->flush_list_mutex); \
1435
#define buf_pool_mutex_enter() do { \
1436
ut_ad(!mutex_own(&buf_pool_zip_mutex)); \
1437
mutex_enter(&buf_pool_mutex); \
1693
1440
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
1441
/** Flag to forbid the release of the buffer pool mutex.
1442
Protected by buf_pool_mutex. */
1443
extern ulint buf_pool_mutex_exit_forbidden;
1694
1444
/** Forbid the release of the buffer pool mutex. */
1695
# define buf_pool_mutex_exit_forbid(b) do { \
1696
ut_ad(buf_pool_mutex_own(b)); \
1697
b->mutex_exit_forbidden++; \
1445
# define buf_pool_mutex_exit_forbid() do { \
1446
ut_ad(buf_pool_mutex_own()); \
1447
buf_pool_mutex_exit_forbidden++; \
1699
1449
/** Allow the release of the buffer pool mutex. */
1700
# define buf_pool_mutex_exit_allow(b) do { \
1701
ut_ad(buf_pool_mutex_own(b)); \
1702
ut_a(b->mutex_exit_forbidden); \
1703
b->mutex_exit_forbidden--; \
1450
# define buf_pool_mutex_exit_allow() do { \
1451
ut_ad(buf_pool_mutex_own()); \
1452
ut_a(buf_pool_mutex_exit_forbidden); \
1453
buf_pool_mutex_exit_forbidden--; \
1705
1455
/** Release the buffer pool mutex. */
1706
# define buf_pool_mutex_exit(b) do { \
1707
ut_a(!b->mutex_exit_forbidden); \
1708
mutex_exit(&b->mutex); \
1456
# define buf_pool_mutex_exit() do { \
1457
ut_a(!buf_pool_mutex_exit_forbidden); \
1458
mutex_exit(&buf_pool_mutex); \
1711
1461
/** Forbid the release of the buffer pool mutex. */
1712
# define buf_pool_mutex_exit_forbid(b) ((void) 0)
1462
# define buf_pool_mutex_exit_forbid() ((void) 0)
1713
1463
/** Allow the release of the buffer pool mutex. */
1714
# define buf_pool_mutex_exit_allow(b) ((void) 0)
1464
# define buf_pool_mutex_exit_allow() ((void) 0)
1715
1465
/** Release the buffer pool mutex. */
1716
# define buf_pool_mutex_exit(b) mutex_exit(&b->mutex)
1466
# define buf_pool_mutex_exit() mutex_exit(&buf_pool_mutex)
1718
1468
#endif /* !UNIV_HOTBACKUP */