47
47
it is error-prone programming
48
48
not to set a latch, and it
49
49
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
51
55
/** @name Modes for buf_page_get_known_nowait */
59
63
position of the block. */
62
extern buf_pool_t* buf_pool; /*!< The buffer pool of the database */
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
64
75
extern ibool buf_debug_prints;/*!< If this is set TRUE, the program
65
76
prints info whenever read or flush
102
115
#ifndef UNIV_HOTBACKUP
103
116
/********************************************************************//**
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
/********************************************************************//**
104
131
Creates the buffer pool.
105
132
@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 */
110
139
/********************************************************************//**
111
140
Frees the buffer pool at shutdown. This must not be invoked before
112
141
freeing all mutexes. */
146
ulint n_instances); /*!< in: numbere of instances to free */
118
148
/********************************************************************//**
119
149
Drops the adaptive hash index. To prevent a livelock, this function
151
181
buf_pool_get_curr_size(void);
152
182
/*========================*/
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
/*=======================*/
153
190
/********************************************************************//**
154
191
Gets the smallest oldest_modification lsn for any page in the pool. Returns
155
192
zero if all modified pages have been flushed to disk.
156
193
@return oldest modification in pool, zero if none */
159
196
buf_pool_get_oldest_modification(void);
160
197
/*==================================*/
161
198
/********************************************************************//**
162
199
Allocates a buffer block.
163
200
@return own: the allocated block, in state BUF_BLOCK_MEMORY */
168
ulint zip_size); /*!< in: compressed page size in bytes,
169
or 0 if uncompressed tablespace */
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 */
170
208
/********************************************************************//**
171
209
Frees a buffer block which does not contain a file page. */
284
322
ulint rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
285
323
buf_block_t* guess, /*!< in: guessed block or NULL */
286
324
ulint mode, /*!< in: BUF_GET, BUF_GET_IF_IN_POOL,
326
BUF_GET_IF_IN_POOL_OR_WATCH */
288
327
const char* file, /*!< in: file name */
289
328
ulint line, /*!< in: line where called */
290
329
mtr_t* mtr); /*!< in: mini-transaction */
334
373
buf_page_release(
335
374
/*=============*/
336
375
buf_block_t* block, /*!< in: buffer block */
337
ulint rw_latch, /*!< in: RW_S_LATCH, RW_X_LATCH,
376
ulint rw_latch); /*!< in: RW_S_LATCH, RW_X_LATCH,
339
mtr_t* mtr); /*!< in: mtr */
340
378
/********************************************************************//**
341
379
Moves a page to the start of the buffer pool LRU list. This high-level
342
380
function can be used to prevent an important page from slipping out of
602
641
Refreshes the statistics used to print per-second averages. */
605
buf_refresh_io_stats(void);
606
/*======================*/
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
/*=================*/
607
653
/*********************************************************************//**
608
654
Asserts that all file pages in the buffer are in a replaceable state.
984
1030
ulint offset) /*!< in: offset of the page within space */
985
1031
__attribute__((const));
986
1032
/******************************************************************//**
1033
Returns the buffer pool instance given a page instance
1037
buf_pool_from_bpage(
1038
/*================*/
1039
const buf_page_t* bpage); /*!< in: buffer pool page */
1040
/******************************************************************//**
1041
Returns the buffer pool instance given a block instance
1045
buf_pool_from_block(
1046
/*================*/
1047
const buf_block_t* block); /*!< in: block */
1048
/******************************************************************//**
1049
Returns the buffer pool instance given space and offset of page
1050
@return buffer pool */
1055
ulint space, /*!< in: space id */
1056
ulint offset);/*!< in: offset of the page within space */
1057
/******************************************************************//**
1058
Returns the buffer pool instance given its array index
1059
@return buffer pool */
1062
buf_pool_from_array(
1063
/*====================*/
1064
ulint index); /*!< in: array index to get buffer pool instance from */
1065
/******************************************************************//**
987
1066
Returns the control block of a file page, NULL if not found.
988
1067
@return block, NULL if not found */
1070
buf_page_hash_get_low(
1071
/*==================*/
1072
buf_pool_t* buf_pool, /*!< buffer pool instance */
1073
ulint space, /*!< in: space id */
1074
ulint offset, /*!< in: offset of the page
1076
ulint fold); /*!< in: buf_page_address_fold(
1078
/******************************************************************//**
1079
Returns the control block of a file page, NULL if not found.
1080
@return block, NULL if not found or not a real control block */
991
1083
buf_page_hash_get(
992
1084
/*==============*/
993
ulint space, /*!< in: space id */
994
ulint offset);/*!< in: offset of the page within space */
1085
buf_pool_t* buf_pool, /*!< in: buffer pool instance */
1086
ulint space, /*!< in: space id */
1087
ulint offset); /*!< in: offset of the page
995
1089
/******************************************************************//**
996
1090
Returns the control block of a file page, NULL if not found
997
1091
or an uncompressed page frame does not exist.
1001
1095
buf_block_hash_get(
1002
1096
/*===============*/
1003
ulint space, /*!< in: space id */
1004
ulint offset);/*!< in: offset of the page within space */
1097
buf_pool_t* buf_pool, /*!< in: buffer pool instance */
1098
ulint space, /*!< in: space id */
1099
ulint offset); /*!< in: offset of the page
1005
1101
/*********************************************************************//**
1006
1102
Gets the current length of the free list of buffer blocks.
1007
1103
@return length of the free list */
1010
1106
buf_get_free_list_len(void);
1011
1107
/*=======================*/
1109
/********************************************************************
1110
Determine if a block is a sentinel for a buffer pool watch.
1111
@return TRUE if a sentinel for a buffer pool watch, FALSE if not */
1114
buf_pool_watch_is_sentinel(
1115
/*=======================*/
1116
buf_pool_t* buf_pool, /*!< buffer pool instance */
1117
const buf_page_t* bpage) /*!< in: block */
1118
__attribute__((nonnull, warn_unused_result));
1119
/****************************************************************//**
1120
Add watch for the given page to be read in. Caller must have the buffer pool
1121
@return NULL if watch set, block if the page is in the buffer pool */
1126
ulint space, /*!< in: space id */
1127
ulint offset, /*!< in: page number */
1128
ulint fold) /*!< in: buf_page_address_fold(space, offset) */
1129
__attribute__((warn_unused_result));
1130
/****************************************************************//**
1131
Stop watching if the page has been read in.
1132
buf_pool_watch_set(space,offset) must have returned NULL before. */
1135
buf_pool_watch_unset(
1136
/*=================*/
1137
ulint space, /*!< in: space id */
1138
ulint offset);/*!< in: page number */
1139
/****************************************************************//**
1140
Check if the page has been read in.
1141
This may only be called after buf_pool_watch_set(space,offset)
1142
has returned NULL and before invoking buf_pool_watch_unset(space,offset).
1143
@return FALSE if the given page was not read in, TRUE if it was */
1146
buf_pool_watch_occurred(
1147
/*====================*/
1148
ulint space, /*!< in: space id */
1149
ulint offset) /*!< in: page number */
1150
__attribute__((warn_unused_result));
1151
/********************************************************************//**
1152
Get total buffer pool statistics. */
1155
buf_get_total_list_len(
1156
/*===================*/
1157
ulint* LRU_len, /*!< out: length of all LRU lists */
1158
ulint* free_len, /*!< out: length of all free lists */
1159
ulint* flush_list_len);/*!< out: length of all flush lists */
1160
/********************************************************************//**
1161
Get total buffer pool statistics. */
1166
buf_pool_stat_t*tot_stat); /*!< out: buffer pool stats */
1012
1168
#endif /* !UNIV_HOTBACKUP */
1015
1170
/** The common buffer control block structure
1016
1171
for compressed and uncompressed frames */
1019
1174
/** @name General fields
1020
1175
None of these bit-fields must be modified without holding
1021
1176
buf_page_get_mutex() [buf_block_struct::mutex or
1022
buf_pool_zip_mutex], since they can be stored in the same
1177
buf_pool->zip_mutex], since they can be stored in the same
1023
1178
machine word. Some of these fields are additionally protected
1024
by buf_pool_mutex. */
1179
by buf_pool->mutex. */
1027
1182
unsigned space:32; /*!< tablespace id; also protected
1028
by buf_pool_mutex. */
1183
by buf_pool->mutex. */
1029
1184
unsigned offset:32; /*!< page number; also protected
1030
by buf_pool_mutex. */
1185
by buf_pool->mutex. */
1032
1187
unsigned state:3; /*!< state of the control block; also
1033
protected by buf_pool_mutex.
1188
protected by buf_pool->mutex.
1034
1189
State transitions from
1035
1190
BUF_BLOCK_READY_FOR_USE to
1036
1191
BUF_BLOCK_MEMORY need not be
1050
1205
#endif /* !UNIV_HOTBACKUP */
1051
1206
page_zip_des_t zip; /*!< compressed page; zip.data
1052
1207
(but not the data it points to) is
1053
also protected by buf_pool_mutex */
1208
also protected by buf_pool_mutex;
1209
state == BUF_BLOCK_ZIP_PAGE and
1210
zip.data == NULL means an active
1054
1212
#ifndef UNIV_HOTBACKUP
1055
1213
buf_page_t* hash; /*!< node used in chaining to
1056
1214
buf_pool->page_hash or
1067
1225
UT_LIST_NODE_T(buf_page_t) list;
1068
1226
/*!< based on state, this is a
1069
list node, protected only by
1070
buf_pool_mutex, in one of the
1227
list node, protected either by
1228
buf_pool_mutex or by
1229
flush_list_mutex, in one of the
1071
1230
following lists in buf_pool:
1073
1232
- BUF_BLOCK_NOT_USED: free
1076
1235
- BUF_BLOCK_ZIP_PAGE: zip_clean
1077
1236
- BUF_BLOCK_ZIP_FREE: zip_free[]
1238
If bpage is part of flush_list
1239
then the node pointers are
1240
covered by flush_list_mutex.
1241
Otherwise these pointers are
1242
protected by buf_pool_mutex.
1079
1244
The contents of the list node
1080
1245
is undefined if !in_flush_list
1081
1246
&& state == BUF_BLOCK_FILE_PAGE,
1087
1252
#ifdef UNIV_DEBUG
1088
1253
ibool in_flush_list; /*!< TRUE if in buf_pool->flush_list;
1089
when buf_pool_mutex is free, the
1254
when flush_list_mutex is free, the
1090
1255
following should hold: in_flush_list
1091
1256
== (state == BUF_BLOCK_FILE_PAGE
1092
|| state == BUF_BLOCK_ZIP_DIRTY) */
1257
|| state == BUF_BLOCK_ZIP_DIRTY)
1258
Writes to this field must be
1259
covered by both block->mutex
1260
and flush_list_mutex. Hence
1261
reads can happen while holding
1262
any one of the two mutexes */
1093
1263
ibool in_free_list; /*!< TRUE if in buf_pool->free; when
1094
1264
buf_pool_mutex is free, the following
1095
1265
should hold: in_free_list
1107
1278
modification to this block
1108
1279
which has not yet been flushed
1109
1280
on disk; zero if all
1110
modifications are on disk */
1281
modifications are on disk.
1282
Writes to this field must be
1283
covered by both block->mutex
1284
and flush_list_mutex. Hence
1285
reads can happen while holding
1286
any one of the two mutexes */
1112
1288
/** @name LRU replacement algorithm fields
1113
These fields are protected by buf_pool_mutex only (not
1114
buf_pool_zip_mutex or buf_block_struct::mutex). */
1289
These fields are protected by buf_pool->mutex only (not
1290
buf_pool->zip_mutex or buf_block_struct::mutex). */
1117
1293
UT_LIST_NODE_T(buf_page_t) LRU;
1309
1487
buf_page_peek_if_too_old() */
1490
/** Statistics of buddy blocks of a given size. */
1491
struct buf_buddy_stat_struct {
1492
/** Number of blocks allocated from the buddy system. */
1494
/** Number of blocks relocated by the buddy system. */
1495
ib_uint64_t relocated;
1496
/** Total duration of block relocations, in microseconds. */
1497
ib_uint64_t relocated_usec;
1312
1500
/** @brief The buffer pool structure.
1314
1502
NOTE! The definition appears here only for other modules of this
1319
1507
/** @name General fields */
1509
mutex_t mutex; /*!< Buffer pool mutex of this
1511
mutex_t zip_mutex; /*!< Zip mutex of this buffer
1512
pool instance, protects compressed
1513
only pages (of type buf_page_t, not
1515
ulint instance_no; /*!< Array index of this buffer
1517
ulint old_pool_size; /*!< Old pool size in bytes */
1518
ulint curr_pool_size; /*!< Current pool size in bytes */
1519
ulint LRU_old_ratio; /*!< Reserve this much of the buffer
1520
pool for "old" blocks */
1522
ulint buddy_n_frames; /*!< Number of frames allocated from
1523
the buffer pool to the buddy system */
1525
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
1526
ulint mutex_exit_forbidden; /*!< Forbid release mutex */
1322
1528
ulint n_chunks; /*!< number of buffer pool chunks */
1323
1529
buf_chunk_t* chunks; /*!< buffer pool chunks */
1324
1530
ulint curr_size; /*!< current pool size in pages */
1330
1536
whose frames are allocated to the
1331
1537
zip buddy system,
1332
1538
indexed by block->frame */
1333
ulint n_pend_reads; /*!< number of pending read operations */
1539
ulint n_pend_reads; /*!< number of pending read
1334
1541
ulint n_pend_unzip; /*!< number of pending decompressions */
1336
1543
time_t last_printout_time;
1337
1544
/*!< when buf_print_io was last time
1546
buf_buddy_stat_t buddy_stat[BUF_BUDDY_SIZES + 1];
1547
/*!< Statistics of buddy system,
1548
indexed by block size */
1339
1549
buf_pool_stat_t stat; /*!< current statistics */
1340
1550
buf_pool_stat_t old_stat; /*!< old statistics */
1558
mutex_t flush_list_mutex;/*!< mutex protecting the
1559
flush list access. This mutex
1560
protects flush_list, flush_rbt
1561
and bpage::list pointers when
1562
the bpage is on flush_list. It
1563
also protects writes to
1564
bpage::oldest_modification */
1348
1565
UT_LIST_BASE_NODE_T(buf_page_t) flush_list;
1349
1566
/*!< base node of the modified block
1358
1575
/*!< this is in the set state
1359
1576
when there is no flush batch
1360
1577
of the given type running */
1361
ib_rbt_t* flush_rbt; /* !< a red-black tree is used
1578
ib_rbt_t* flush_rbt; /*!< a red-black tree is used
1362
1579
exclusively during recovery to
1363
1580
speed up insertions in the
1364
1581
flush_list. This tree contains
1370
1587
also be on the flush_list.
1371
1588
This tree is relevant only in
1372
1589
recovery and is set to NULL
1373
once the recovery is over. */
1590
once the recovery is over.
1591
Protected by flush_list_mutex */
1374
1592
ulint freed_page_clock;/*!< a sequence number used
1375
1593
to count the number of buffer
1376
1594
blocks removed from the end of
1446
1670
/** Test if buf_pool_mutex is owned. */
1447
#define buf_pool_mutex_own() mutex_own(&buf_pool_mutex)
1671
#define buf_pool_mutex_own(b) mutex_own(&b->mutex)
1448
1672
/** Acquire the buffer pool mutex. */
1449
#define buf_pool_mutex_enter() do { \
1450
ut_ad(!mutex_own(&buf_pool_zip_mutex)); \
1451
mutex_enter(&buf_pool_mutex); \
1673
#define buf_pool_mutex_enter(b) do { \
1674
ut_ad(!mutex_own(&b->zip_mutex)); \
1675
mutex_enter(&b->mutex); \
1678
/** Test if flush list mutex is owned. */
1679
#define buf_flush_list_mutex_own(b) mutex_own(&b->flush_list_mutex)
1681
/** Acquire the flush list mutex. */
1682
#define buf_flush_list_mutex_enter(b) do { \
1683
mutex_enter(&b->flush_list_mutex); \
1685
/** Release the flush list mutex. */
1686
# define buf_flush_list_mutex_exit(b) do { \
1687
mutex_exit(&b->flush_list_mutex); \
1454
1692
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
1455
/** Flag to forbid the release of the buffer pool mutex.
1456
Protected by buf_pool_mutex. */
1457
extern ulint buf_pool_mutex_exit_forbidden;
1458
1693
/** Forbid the release of the buffer pool mutex. */
1459
# define buf_pool_mutex_exit_forbid() do { \
1460
ut_ad(buf_pool_mutex_own()); \
1461
buf_pool_mutex_exit_forbidden++; \
1694
# define buf_pool_mutex_exit_forbid(b) do { \
1695
ut_ad(buf_pool_mutex_own(b)); \
1696
b->mutex_exit_forbidden++; \
1463
1698
/** Allow the release of the buffer pool mutex. */
1464
# define buf_pool_mutex_exit_allow() do { \
1465
ut_ad(buf_pool_mutex_own()); \
1466
ut_a(buf_pool_mutex_exit_forbidden); \
1467
buf_pool_mutex_exit_forbidden--; \
1699
# define buf_pool_mutex_exit_allow(b) do { \
1700
ut_ad(buf_pool_mutex_own(b)); \
1701
ut_a(b->mutex_exit_forbidden); \
1702
b->mutex_exit_forbidden--; \
1469
1704
/** Release the buffer pool mutex. */
1470
# define buf_pool_mutex_exit() do { \
1471
ut_a(!buf_pool_mutex_exit_forbidden); \
1472
mutex_exit(&buf_pool_mutex); \
1705
# define buf_pool_mutex_exit(b) do { \
1706
ut_a(!b->mutex_exit_forbidden); \
1707
mutex_exit(&b->mutex); \
1475
1710
/** Forbid the release of the buffer pool mutex. */
1476
# define buf_pool_mutex_exit_forbid() ((void) 0)
1711
# define buf_pool_mutex_exit_forbid(b) ((void) 0)
1477
1712
/** Allow the release of the buffer pool mutex. */
1478
# define buf_pool_mutex_exit_allow() ((void) 0)
1713
# define buf_pool_mutex_exit_allow(b) ((void) 0)
1479
1714
/** Release the buffer pool mutex. */
1480
# define buf_pool_mutex_exit() mutex_exit(&buf_pool_mutex)
1715
# define buf_pool_mutex_exit(b) mutex_exit(&b->mutex)
1482
1717
#endif /* !UNIV_HOTBACKUP */