84
84
The buffer buf_pool contains a single mutex which protects all the
85
85
control data structures of the buf_pool. The content of a buffer frame is
86
86
protected by a separate read-write lock in its control block, though.
87
These locks can be locked and unlocked without owning the buf_pool->mutex.
87
These locks can be locked and unlocked without owning the buf_pool mutex.
88
88
The OS events in the buf_pool struct can be waited for without owning the
91
The buf_pool->mutex is a hot-spot in main memory, causing a lot of
91
The buf_pool mutex is a hot-spot in main memory, causing a lot of
92
92
memory bus traffic on multiprocessor systems when processors
93
93
alternately access the mutex. On our Pentium, the mutex is accessed
94
94
maybe every 10 microseconds. We gave up the solution to have mutexes
95
95
for each control block, for instance, because it seemed to be
98
A solution to reduce mutex contention of the buf_pool->mutex is to
98
A solution to reduce mutex contention of the buf_pool mutex is to
99
99
create a separate mutex for the page hash table. On Pentium,
100
100
accessing the hash table takes 2 microseconds, about half
101
of the total buf_pool->mutex hold time.
101
of the total buf_pool mutex hold time.
153
153
which we can use when we want to artificially age a page in the
154
154
buf_pool. This is used if we know that some page is not needed
155
155
again for some time: we insert the block right after the pointer,
156
causing it to be replaced sooner than would normally be the case.
156
causing it to be replaced sooner than would noramlly be the case.
157
157
Currently this aging mechanism is used for read-ahead mechanism
158
158
of pages, and it can also be used when there is a scan of a full
159
159
table which cannot fit in the memory. Putting the pages near the
160
end of the LRU list, we make sure that most of the buf_pool stays
161
in the main memory, undisturbed.
160
of the LRU list, we make sure that most of the buf_pool stays in the
161
main memory, undisturbed.
163
163
The unzip_LRU list contains a subset of the common LRU list. The
164
164
blocks on the unzip_LRU list hold a compressed file page and the
247
246
static const ulint BUF_PAGE_READ_MAX_RETRIES = 100;
249
248
/** The buffer buf_pool of the database */
250
UNIV_INTERN buf_pool_t* buf_pool_ptr[MAX_BUFFER_POOLS];
249
UNIV_INTERN buf_pool_t* buf_pool = NULL;
251
/** mutex protecting the buffer pool struct and control blocks, except the
252
read-write lock in them */
253
UNIV_INTERN mutex_t buf_pool_mutex;
254
/** mutex protecting the control blocks of compressed-only pages
255
(of type buf_page_t, not buf_block_t) */
256
UNIV_INTERN mutex_t buf_pool_zip_mutex;
252
258
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
253
259
static ulint buf_dbg_counter = 0; /*!< This is used to insert validation
254
operations in execution in the
260
operations in excution in the
262
/** Flag to forbid the release of the buffer pool mutex.
263
Protected by buf_pool_mutex. */
264
UNIV_INTERN ulint buf_pool_mutex_exit_forbidden = 0;
256
265
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
257
266
#ifdef UNIV_DEBUG
258
267
/** If this is set TRUE, the program prints info whenever
260
269
UNIV_INTERN ibool buf_debug_prints = FALSE;
261
270
#endif /* UNIV_DEBUG */
263
#ifdef UNIV_PFS_RWLOCK
264
/* Keys to register buffer block related rwlocks and mutexes with
265
performance schema */
266
UNIV_INTERN mysql_pfs_key_t buf_block_lock_key;
267
# ifdef UNIV_SYNC_DEBUG
268
UNIV_INTERN mysql_pfs_key_t buf_block_debug_latch_key;
269
# endif /* UNIV_SYNC_DEBUG */
270
#endif /* UNIV_PFS_RWLOCK */
272
#ifdef UNIV_PFS_MUTEX
273
UNIV_INTERN mysql_pfs_key_t buffer_block_mutex_key;
274
UNIV_INTERN mysql_pfs_key_t buf_pool_mutex_key;
275
UNIV_INTERN mysql_pfs_key_t buf_pool_zip_mutex_key;
276
UNIV_INTERN mysql_pfs_key_t flush_list_mutex_key;
277
#endif /* UNIV_PFS_MUTEX */
279
#if defined UNIV_PFS_MUTEX || defined UNIV_PFS_RWLOCK
280
# ifndef PFS_SKIP_BUFFER_MUTEX_RWLOCK
282
/* Buffer block mutexes and rwlocks can be registered
283
in one group rather than individually. If PFS_GROUP_BUFFER_SYNC
284
is defined, register buffer block mutex and rwlock
285
in one group after their initialization. */
286
# define PFS_GROUP_BUFFER_SYNC
288
/* This define caps the number of mutexes/rwlocks can
289
be registered with performance schema. Developers can
290
modify this define if necessary. Please note, this would
291
be effective only if PFS_GROUP_BUFFER_SYNC is defined. */
292
# define PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER ULINT_MAX
294
# endif /* !PFS_SKIP_BUFFER_MUTEX_RWLOCK */
295
#endif /* UNIV_PFS_MUTEX || UNIV_PFS_RWLOCK */
297
272
/** A chunk of buffers. The buffer pool is allocated in chunks. */
298
273
struct buf_chunk_struct{
299
274
ulint mem_size; /*!< allocated size of the chunk */
305
280
#endif /* !UNIV_HOTBACKUP */
307
282
/********************************************************************//**
308
Gets the smallest oldest_modification lsn for any page in the pool. Returns
309
zero if all modified pages have been flushed to disk.
310
@return oldest modification in pool, zero if none */
313
buf_pool_get_oldest_modification(void)
314
/*==================================*/
319
ib_uint64_t oldest_lsn = 0;
321
/* When we traverse all the flush lists we don't want another
322
thread to add a dirty page to any flush list. */
323
log_flush_order_mutex_enter();
325
for (i = 0; i < srv_buf_pool_instances; i++) {
326
buf_pool_t* buf_pool;
328
buf_pool = buf_pool_from_array(i);
330
buf_flush_list_mutex_enter(buf_pool);
332
bpage = UT_LIST_GET_LAST(buf_pool->flush_list);
335
ut_ad(bpage->in_flush_list);
336
lsn = bpage->oldest_modification;
339
buf_flush_list_mutex_exit(buf_pool);
341
if (!oldest_lsn || oldest_lsn > lsn) {
346
log_flush_order_mutex_exit();
348
/* The returned answer may be out of date: the flush_list can
349
change after the mutex has been released. */
354
/********************************************************************//**
355
Get total buffer pool statistics. */
358
buf_get_total_list_len(
359
/*===================*/
360
ulint* LRU_len, /*!< out: length of all LRU lists */
361
ulint* free_len, /*!< out: length of all free lists */
362
ulint* flush_list_len) /*!< out: length of all flush lists */
370
for (i = 0; i < srv_buf_pool_instances; i++) {
371
buf_pool_t* buf_pool;
373
buf_pool = buf_pool_from_array(i);
374
*LRU_len += UT_LIST_GET_LEN(buf_pool->LRU);
375
*free_len += UT_LIST_GET_LEN(buf_pool->free);
376
*flush_list_len += UT_LIST_GET_LEN(buf_pool->flush_list);
380
/********************************************************************//**
381
Get total buffer pool statistics. */
386
buf_pool_stat_t* tot_stat) /*!< out: buffer pool stats */
390
memset(tot_stat, 0, sizeof(*tot_stat));
392
for (i = 0; i < srv_buf_pool_instances; i++) {
393
buf_pool_stat_t*buf_stat;
394
buf_pool_t* buf_pool;
396
buf_pool = buf_pool_from_array(i);
398
buf_stat = &buf_pool->stat;
399
tot_stat->n_page_gets += buf_stat->n_page_gets;
400
tot_stat->n_pages_read += buf_stat->n_pages_read;
401
tot_stat->n_pages_written += buf_stat->n_pages_written;
402
tot_stat->n_pages_created += buf_stat->n_pages_created;
403
tot_stat->n_ra_pages_read += buf_stat->n_ra_pages_read;
404
tot_stat->n_ra_pages_evicted += buf_stat->n_ra_pages_evicted;
405
tot_stat->n_pages_made_young += buf_stat->n_pages_made_young;
407
tot_stat->n_pages_not_made_young +=
408
buf_stat->n_pages_not_made_young;
412
/********************************************************************//**
413
Allocates a buffer block.
414
@return own: the allocated block, in state BUF_BLOCK_MEMORY */
419
buf_pool_t* buf_pool, /*!< in: buffer pool instance */
420
ulint zip_size) /*!< in: compressed page size in bytes,
421
or 0 if uncompressed tablespace */
425
static ulint buf_pool_index;
427
if (buf_pool == NULL) {
428
/* We are allocating memory from any buffer pool, ensure
429
we spread the grace on all buffer pool instances. */
430
index = buf_pool_index++ % srv_buf_pool_instances;
431
buf_pool = buf_pool_from_array(index);
434
block = buf_LRU_get_free_block(buf_pool, zip_size);
436
buf_block_set_state(block, BUF_BLOCK_MEMORY);
441
/********************************************************************//**
442
283
Calculates a page checksum which is stored to the page when it is written
443
284
to a file. Note that we must be careful to calculate the same value on
444
285
32-bit and 64-bit architectures.
799
640
#ifndef UNIV_HOTBACKUP
801
# ifdef PFS_GROUP_BUFFER_SYNC
802
/********************************************************************//**
803
This function registers mutexes and rwlocks in buffer blocks with
804
performance schema. If PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER is
805
defined to be a value less than chunk->size, then only mutexes
806
and rwlocks in the first PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER
807
blocks are registered. */
810
pfs_register_buffer_block(
811
/*======================*/
812
buf_chunk_t* chunk) /*!< in/out: chunk of buffers */
815
ulint num_to_register;
818
block = chunk->blocks;
820
num_to_register = ut_min(chunk->size,
821
PFS_MAX_BUFFER_MUTEX_LOCK_REGISTER);
823
for (i = 0; i < num_to_register; i++) {
827
# ifdef UNIV_PFS_MUTEX
828
mutex = &block->mutex;
829
ut_a(!mutex->pfs_psi);
830
mutex->pfs_psi = (PSI_server)
831
? PSI_server->init_mutex(buffer_block_mutex_key, mutex)
833
# endif /* UNIV_PFS_MUTEX */
835
# ifdef UNIV_PFS_RWLOCK
836
rwlock = &block->lock;
837
ut_a(!rwlock->pfs_psi);
838
rwlock->pfs_psi = (PSI_server)
839
? PSI_server->init_rwlock(buf_block_lock_key, rwlock)
841
# endif /* UNIV_PFS_RWLOCK */
845
# endif /* PFS_GROUP_BUFFER_SYNC */
847
641
/********************************************************************//**
848
642
Initializes a buffer control block when the buf_pool is created. */
853
buf_pool_t* buf_pool, /*!< in: buffer pool instance */
854
buf_block_t* block, /*!< in: pointer to control block */
855
byte* frame) /*!< in: pointer to buffer frame */
647
buf_block_t* block, /*!< in: pointer to control block */
648
byte* frame) /*!< in: pointer to buffer frame */
857
650
UNIV_MEM_DESC(frame, UNIV_PAGE_SIZE, block);
859
652
block->frame = frame;
861
block->page.buf_pool = buf_pool;
862
654
block->page.state = BUF_BLOCK_NOT_USED;
863
655
block->page.buf_fix_count = 0;
864
656
block->page.io_fix = BUF_IO_NONE;
887
677
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
888
678
page_zip_des_init(&block->page.zip);
890
#if defined PFS_SKIP_BUFFER_MUTEX_RWLOCK || defined PFS_GROUP_BUFFER_SYNC
891
/* If PFS_SKIP_BUFFER_MUTEX_RWLOCK is defined, skip registration
892
of buffer block mutex/rwlock with performance schema. If
893
PFS_GROUP_BUFFER_SYNC is defined, skip the registration
894
since buffer block mutex/rwlock will be registered later in
895
pfs_register_buffer_block() */
897
mutex_create(PFS_NOT_INSTRUMENTED, &block->mutex, SYNC_BUF_BLOCK);
898
rw_lock_create(PFS_NOT_INSTRUMENTED, &block->lock, SYNC_LEVEL_VARYING);
899
#else /* PFS_SKIP_BUFFER_MUTEX_RWLOCK || PFS_GROUP_BUFFER_SYNC */
900
mutex_create(buffer_block_mutex_key, &block->mutex, SYNC_BUF_BLOCK);
901
rw_lock_create(buf_block_lock_key, &block->lock, SYNC_LEVEL_VARYING);
902
#endif /* PFS_SKIP_BUFFER_MUTEX_RWLOCK || PFS_GROUP_BUFFER_SYNC */
680
mutex_create(&block->mutex, SYNC_BUF_BLOCK);
682
rw_lock_create(&block->lock, SYNC_LEVEL_VARYING);
904
683
ut_ad(rw_lock_validate(&(block->lock)));
906
685
#ifdef UNIV_SYNC_DEBUG
907
rw_lock_create(buf_block_debug_latch_key,
908
&block->debug_latch, SYNC_NO_ORDER_CHECK);
686
rw_lock_create(&block->debug_latch, SYNC_NO_ORDER_CHECK);
909
687
#endif /* UNIV_SYNC_DEBUG */
1166
942
/********************************************************************//**
1167
Set buffer pool size variables after resizing it */
1170
buf_pool_set_sizes(void)
1171
/*====================*/
1174
ulint curr_size = 0;
1176
buf_pool_mutex_enter_all();
1178
for (i = 0; i < srv_buf_pool_instances; i++) {
1179
buf_pool_t* buf_pool;
1181
buf_pool = buf_pool_from_array(i);
1182
curr_size += buf_pool->curr_pool_size;
1185
srv_buf_pool_curr_size = curr_size;
1186
srv_buf_pool_old_size = srv_buf_pool_size;
1188
buf_pool_mutex_exit_all();
1191
/********************************************************************//**
1192
Initialize a buffer pool instance.
1193
@return DB_SUCCESS if all goes well. */
1196
buf_pool_init_instance(
1197
/*===================*/
1198
buf_pool_t* buf_pool, /*!< in: buffer pool instance */
1199
ulint buf_pool_size, /*!< in: size in bytes */
1200
ulint instance_no) /*!< in: id of the instance */
943
Creates the buffer pool.
944
@return own: buf_pool object, NULL if not enough memory or error */
953
buf_pool = mem_zalloc(sizeof(buf_pool_t));
1205
955
/* 1. Initialize general fields
1206
956
------------------------------- */
1207
mutex_create(buf_pool_mutex_key,
1208
&buf_pool->mutex, SYNC_BUF_POOL);
1209
mutex_create(buf_pool_zip_mutex_key,
1210
&buf_pool->zip_mutex, SYNC_BUF_BLOCK);
1212
buf_pool_mutex_enter(buf_pool);
1214
if (buf_pool_size > 0) {
1215
buf_pool->n_chunks = 1;
1216
buf_pool->chunks = chunk = mem_zalloc(sizeof *chunk);
1218
UT_LIST_INIT(buf_pool->free);
1220
if (!buf_chunk_init(buf_pool, chunk, buf_pool_size)) {
1224
buf_pool_mutex_exit(buf_pool);
1229
buf_pool->instance_no = instance_no;
1230
buf_pool->old_pool_size = buf_pool_size;
1231
buf_pool->curr_size = chunk->size;
1232
buf_pool->curr_pool_size = buf_pool->curr_size * UNIV_PAGE_SIZE;
1234
buf_pool->page_hash = hash_create(2 * buf_pool->curr_size);
1235
buf_pool->zip_hash = hash_create(2 * buf_pool->curr_size);
1237
buf_pool->last_printout_time = ut_time();
957
mutex_create(&buf_pool_mutex, SYNC_BUF_POOL);
958
mutex_create(&buf_pool_zip_mutex, SYNC_BUF_BLOCK);
960
buf_pool_mutex_enter();
962
buf_pool->n_chunks = 1;
963
buf_pool->chunks = chunk = mem_alloc(sizeof *chunk);
965
UT_LIST_INIT(buf_pool->free);
967
if (!buf_chunk_init(chunk, srv_buf_pool_size)) {
974
srv_buf_pool_old_size = srv_buf_pool_size;
975
buf_pool->curr_size = chunk->size;
976
srv_buf_pool_curr_size = buf_pool->curr_size * UNIV_PAGE_SIZE;
978
buf_pool->page_hash = hash_create(2 * buf_pool->curr_size);
979
buf_pool->zip_hash = hash_create(2 * buf_pool->curr_size);
981
buf_pool->last_printout_time = time(NULL);
1239
983
/* 2. Initialize flushing fields
1240
984
-------------------------------- */
1242
mutex_create(flush_list_mutex_key, &buf_pool->flush_list_mutex,
1243
SYNC_BUF_FLUSH_LIST);
1245
986
for (i = BUF_FLUSH_LRU; i < BUF_FLUSH_N_TYPES; i++) {
1246
987
buf_pool->no_flush[i] = os_event_create(NULL);
1249
990
/* 3. Initialize LRU fields
1250
991
--------------------------- */
1252
/* All fields are initialized by mem_zalloc(). */
1254
buf_pool_mutex_exit(buf_pool);
992
/* All fields are initialized by mem_zalloc(). */
994
buf_pool_mutex_exit();
996
btr_search_sys_create(buf_pool->curr_size
997
* UNIV_PAGE_SIZE / sizeof(void*) / 64);
999
/* 4. Initialize the buddy allocator fields */
1000
/* All fields are initialized by mem_zalloc(). */
1259
1005
/********************************************************************//**
1260
free one buffer pool instance */
1006
Frees the buffer pool at shutdown. This must not be invoked before
1007
freeing all mutexes. */
1263
buf_pool_free_instance(
1264
/*===================*/
1265
buf_pool_t* buf_pool) /* in,own: buffer pool instance
1268
1013
buf_chunk_t* chunk;
1269
1014
buf_chunk_t* chunks;
1287
1032
/********************************************************************//**
1288
Creates the buffer pool.
1289
@return DB_SUCCESS if success, DB_ERROR if not enough memory or error */
1294
ulint total_size, /*!< in: size of the total pool in bytes */
1295
ulint n_instances) /*!< in: number of instances */
1299
/* We create an extra buffer pool instance, this instance is used
1300
for flushing the flush lists, to keep track of n_flush for all
1301
the buffer pools and also used as a waiting object during flushing. */
1302
for (i = 0; i < n_instances; i++) {
1306
ptr = mem_zalloc(sizeof(*ptr));
1308
size = total_size / n_instances;
1310
buf_pool_ptr[i] = ptr;
1312
if (buf_pool_init_instance(ptr, size, i) != DB_SUCCESS) {
1314
mem_free(buf_pool_ptr[i]);
1316
/* Free all the instances created so far. */
1323
buf_pool_set_sizes();
1324
buf_LRU_old_ratio_update(100 * 3/ 8, FALSE);
1326
btr_search_sys_create(buf_pool_get_curr_size() / sizeof(void*) / 64);
1331
/********************************************************************//**
1332
Frees the buffer pool at shutdown. This must not be invoked before
1333
freeing all mutexes. */
1338
ulint n_instances) /*!< in: numbere of instances to free */
1342
for (i = 0; i < n_instances; i++) {
1343
buf_pool_free_instance(buf_pool_from_array(i));
1344
buf_pool_ptr[i] = NULL;
1348
/********************************************************************//**
1349
Drops adaptive hash index for a buffer pool instance. */
1352
buf_pool_drop_hash_index_instance(
1353
/*==============================*/
1354
buf_pool_t* buf_pool, /*!< in: buffer pool instance */
1355
ibool* released_search_latch) /*!< out: flag for signalling
1356
whether the search latch was
1359
buf_chunk_t* chunks = buf_pool->chunks;
1360
buf_chunk_t* chunk = chunks + buf_pool->n_chunks;
1362
while (--chunk >= chunks) {
1364
buf_block_t* block = chunk->blocks;
1366
for (i = chunk->size; i--; block++) {
1367
/* block->is_hashed cannot be modified
1368
when we have an x-latch on btr_search_latch;
1369
see the comment in buf0buf.h */
1371
if (!block->is_hashed) {
1375
/* To follow the latching order, we
1376
have to release btr_search_latch
1377
before acquiring block->latch. */
1378
rw_lock_x_unlock(&btr_search_latch);
1379
/* When we release the search latch,
1380
we must rescan all blocks, because
1381
some may become hashed again. */
1382
*released_search_latch = TRUE;
1384
rw_lock_x_lock(&block->lock);
1386
/* This should be guaranteed by the
1387
callers, which will be holding
1388
btr_search_enabled_mutex. */
1389
ut_ad(!btr_search_enabled);
1391
/* Because we did not buffer-fix the
1392
block by calling buf_block_get_gen(),
1393
it is possible that the block has been
1394
allocated for some other use after
1395
btr_search_latch was released above.
1396
We do not care which file page the
1397
block is mapped to. All we want to do
1398
is to drop any hash entries referring
1401
/* It is possible that
1402
block->page.state != BUF_FILE_PAGE.
1403
Even that does not matter, because
1404
btr_search_drop_page_hash_index() will
1405
check block->is_hashed before doing
1406
anything. block->is_hashed can only
1407
be set on uncompressed file pages. */
1409
btr_search_drop_page_hash_index(block);
1411
rw_lock_x_unlock(&block->lock);
1413
rw_lock_x_lock(&btr_search_latch);
1415
ut_ad(!btr_search_enabled);
1420
/********************************************************************//**
1421
1033
Drops the adaptive hash index. To prevent a livelock, this function
1422
1034
is only to be called while holding btr_search_latch and while
1423
1035
btr_search_enabled == FALSE. */
1434
1046
ut_ad(!btr_search_enabled);
1049
buf_chunk_t* chunks = buf_pool->chunks;
1050
buf_chunk_t* chunk = chunks + buf_pool->n_chunks;
1439
1052
released_search_latch = FALSE;
1441
for (i = 0; i < srv_buf_pool_instances; i++) {
1442
buf_pool_t* buf_pool;
1444
buf_pool = buf_pool_from_array(i);
1446
buf_pool_drop_hash_index_instance(
1447
buf_pool, &released_search_latch);
1054
while (--chunk >= chunks) {
1055
buf_block_t* block = chunk->blocks;
1056
ulint i = chunk->size;
1058
for (; i--; block++) {
1059
/* block->is_hashed cannot be modified
1060
when we have an x-latch on btr_search_latch;
1061
see the comment in buf0buf.h */
1063
if (buf_block_get_state(block)
1064
!= BUF_BLOCK_FILE_PAGE
1065
|| !block->is_hashed) {
1069
/* To follow the latching order, we
1070
have to release btr_search_latch
1071
before acquiring block->latch. */
1072
rw_lock_x_unlock(&btr_search_latch);
1073
/* When we release the search latch,
1074
we must rescan all blocks, because
1075
some may become hashed again. */
1076
released_search_latch = TRUE;
1078
rw_lock_x_lock(&block->lock);
1080
/* This should be guaranteed by the
1081
callers, which will be holding
1082
btr_search_enabled_mutex. */
1083
ut_ad(!btr_search_enabled);
1085
/* Because we did not buffer-fix the
1086
block by calling buf_block_get_gen(),
1087
it is possible that the block has been
1088
allocated for some other use after
1089
btr_search_latch was released above.
1090
We do not care which file page the
1091
block is mapped to. All we want to do
1092
is to drop any hash entries referring
1095
/* It is possible that
1096
block->page.state != BUF_FILE_PAGE.
1097
Even that does not matter, because
1098
btr_search_drop_page_hash_index() will
1099
check block->is_hashed before doing
1100
anything. block->is_hashed can only
1101
be set on uncompressed file pages. */
1103
btr_search_drop_page_hash_index(block);
1105
rw_lock_x_unlock(&block->lock);
1107
rw_lock_x_lock(&btr_search_latch);
1109
ut_ad(!btr_search_enabled);
1450
1112
} while (released_search_latch);
1679
1337
goto shrink_again;
1684
buf_pool->old_pool_size = buf_pool->curr_pool_size;
1341
srv_buf_pool_old_size = srv_buf_pool_size;
1686
buf_pool_mutex_exit(buf_pool);
1343
buf_pool_mutex_exit();
1687
1344
btr_search_enable();
1690
1347
/********************************************************************//**
1691
Shrinks the buffer pool. */
1696
ulint chunk_size) /*!< in: number of pages to remove */
1700
for (i = 0; i < srv_buf_pool_instances; i++) {
1701
buf_pool_t* buf_pool;
1702
ulint instance_chunk_size;
1704
instance_chunk_size = chunk_size / srv_buf_pool_instances;
1705
buf_pool = buf_pool_from_array(i);
1706
buf_pool_shrink_instance(buf_pool, instance_chunk_size);
1709
buf_pool_set_sizes();
1712
/********************************************************************//**
1713
Rebuild buf_pool->page_hash for a buffer pool instance. */
1716
buf_pool_page_hash_rebuild_instance(
1717
/*================================*/
1718
buf_pool_t* buf_pool) /*!< in: buffer pool instance */
1348
Rebuild buf_pool->page_hash. */
1351
buf_pool_page_hash_rebuild(void)
1352
/*============================*/
1723
1355
ulint n_chunks;
1357
hash_table_t* page_hash;
1724
1358
hash_table_t* zip_hash;
1725
hash_table_t* page_hash;
1727
buf_pool_mutex_enter(buf_pool);
1361
buf_pool_mutex_enter();
1729
1363
/* Free, create, and populate the hash table. */
1730
1364
hash_table_free(buf_pool->page_hash);
1808
buf_flush_list_mutex_exit(buf_pool);
1809
buf_pool_mutex_exit(buf_pool);
1812
/********************************************************************
1813
Determine if a block is a sentinel for a buffer pool watch.
1814
@return TRUE if a sentinel for a buffer pool watch, FALSE if not */
1817
buf_pool_watch_is_sentinel(
1818
/*=======================*/
1819
buf_pool_t* buf_pool, /*!< buffer pool instance */
1820
const buf_page_t* bpage) /*!< in: block */
1822
ut_ad(buf_page_in_file(bpage));
1824
if (bpage < &buf_pool->watch[0]
1825
|| bpage >= &buf_pool->watch[BUF_POOL_WATCH_SIZE]) {
1827
ut_ad(buf_page_get_state(bpage) != BUF_BLOCK_ZIP_PAGE
1828
|| bpage->zip.data != NULL);
1833
ut_ad(buf_page_get_state(bpage) == BUF_BLOCK_ZIP_PAGE);
1834
ut_ad(!bpage->in_zip_hash);
1835
ut_ad(bpage->in_page_hash);
1836
ut_ad(bpage->zip.data == NULL);
1837
ut_ad(bpage->buf_fix_count > 0);
1841
/****************************************************************//**
1842
Add watch for the given page to be read in. Caller must have the buffer pool
1844
@return NULL if watch set, block if the page is in the buffer pool */
1849
ulint space, /*!< in: space id */
1850
ulint offset, /*!< in: page number */
1851
ulint fold) /*!< in: buf_page_address_fold(space, offset) */
1855
buf_pool_t* buf_pool = buf_pool_get(space, offset);
1857
ut_ad(buf_pool_mutex_own(buf_pool));
1859
bpage = buf_page_hash_get_low(buf_pool, space, offset, fold);
1861
if (UNIV_LIKELY_NULL(bpage)) {
1862
if (!buf_pool_watch_is_sentinel(buf_pool, bpage)) {
1863
/* The page was loaded meanwhile. */
1866
/* Add to an existing watch. */
1867
bpage->buf_fix_count++;
1871
for (i = 0; i < BUF_POOL_WATCH_SIZE; i++) {
1872
bpage = &buf_pool->watch[i];
1874
ut_ad(bpage->access_time == 0);
1875
ut_ad(bpage->newest_modification == 0);
1876
ut_ad(bpage->oldest_modification == 0);
1877
ut_ad(bpage->zip.data == NULL);
1878
ut_ad(!bpage->in_zip_hash);
1880
switch (bpage->state) {
1881
case BUF_BLOCK_POOL_WATCH:
1882
ut_ad(!bpage->in_page_hash);
1883
ut_ad(bpage->buf_fix_count == 0);
1885
/* bpage is pointing to buf_pool_watch[],
1886
which is protected by buf_pool_mutex.
1887
Normally, buf_page_t objects are protected by
1888
buf_block_t::mutex or buf_pool->zip_mutex or both. */
1890
bpage->state = BUF_BLOCK_ZIP_PAGE;
1891
bpage->space = space;
1892
bpage->offset = offset;
1893
bpage->buf_fix_count = 1;
1895
ut_d(bpage->in_page_hash = TRUE);
1896
HASH_INSERT(buf_page_t, hash, buf_pool->page_hash,
1899
case BUF_BLOCK_ZIP_PAGE:
1900
ut_ad(bpage->in_page_hash);
1901
ut_ad(bpage->buf_fix_count > 0);
1908
/* Allocation failed. Either the maximum number of purge
1909
threads should never exceed BUF_POOL_WATCH_SIZE, or this code
1910
should be modified to return a special non-NULL value and the
1911
caller should purge the record directly. */
1914
/* Fix compiler warning */
1918
/********************************************************************//**
1919
Rebuild buf_pool->page_hash. */
1922
buf_pool_page_hash_rebuild(void)
1923
/*============================*/
1927
for (i = 0; i < srv_buf_pool_instances; i++) {
1928
buf_pool_page_hash_rebuild_instance(buf_pool_from_array(i));
1932
/********************************************************************//**
1933
Increase the buffer pool size of one buffer pool instance. */
1936
buf_pool_increase_instance(
1937
/*=======================*/
1938
buf_pool_t* buf_pool, /*!< in: buffer pool instane */
1939
ulint change_size) /*!< in: new size of the pool */
1941
buf_chunk_t* chunks;
1944
buf_pool_mutex_enter(buf_pool);
1945
chunks = mem_alloc((buf_pool->n_chunks + 1) * sizeof *chunks);
1947
memcpy(chunks, buf_pool->chunks, buf_pool->n_chunks * sizeof *chunks);
1949
chunk = &chunks[buf_pool->n_chunks];
1951
if (!buf_chunk_init(buf_pool, chunk, change_size)) {
1954
buf_pool->old_pool_size = buf_pool->curr_pool_size;
1955
buf_pool->curr_size += chunk->size;
1956
buf_pool->curr_pool_size = buf_pool->curr_size * UNIV_PAGE_SIZE;
1957
mem_free(buf_pool->chunks);
1958
buf_pool->chunks = chunks;
1959
buf_pool->n_chunks++;
1962
buf_pool_mutex_exit(buf_pool);
1965
/********************************************************************//**
1966
Increase the buffer pool size. */
1975
for (i = 0; i < srv_buf_pool_instances; i++) {
1976
buf_pool_increase_instance(
1977
buf_pool_from_array(i),
1978
change_size / srv_buf_pool_instances);
1981
buf_pool_set_sizes();
1441
buf_pool_mutex_exit();
1984
1444
/********************************************************************//**
1988
1448
buf_pool_resize(void)
1989
1449
/*=================*/
1992
ulint min_change_size = 1048576 * srv_buf_pool_instances;
1994
buf_pool_mutex_enter_all();
1996
if (srv_buf_pool_old_size == srv_buf_pool_size) {
1998
buf_pool_mutex_exit_all();
2002
} else if (srv_buf_pool_curr_size + min_change_size
2003
> srv_buf_pool_size) {
2005
change_size = (srv_buf_pool_curr_size - srv_buf_pool_size)
2008
buf_pool_mutex_exit_all();
2010
/* Disable adaptive hash indexes and empty the index
2011
in order to free up memory in the buffer pool chunks. */
2012
buf_pool_shrink(change_size);
2014
} else if (srv_buf_pool_curr_size + min_change_size
2015
< srv_buf_pool_size) {
2017
/* Enlarge the buffer pool by at least one megabyte */
2019
change_size = srv_buf_pool_size - srv_buf_pool_curr_size;
2021
buf_pool_mutex_exit_all();
2023
buf_pool_increase(change_size);
2025
srv_buf_pool_size = srv_buf_pool_old_size;
2027
buf_pool_mutex_exit_all();
1451
buf_pool_mutex_enter();
1453
if (srv_buf_pool_old_size == srv_buf_pool_size) {
1455
buf_pool_mutex_exit();
2032
buf_pool_page_hash_rebuild();
2035
/****************************************************************//**
2036
Remove the sentinel block for the watch before replacing it with a real block.
2037
buf_page_watch_clear() or buf_page_watch_occurred() will notice that
2038
the block has been replaced with the real block.
2039
@return reference count, to be added to the replacement block */
2042
buf_pool_watch_remove(
2043
/*==================*/
2044
buf_pool_t* buf_pool, /*!< buffer pool instance */
2045
ulint fold, /*!< in: buf_page_address_fold(
2047
buf_page_t* watch) /*!< in/out: sentinel for watch */
2049
ut_ad(buf_pool_mutex_own(buf_pool));
2051
HASH_DELETE(buf_page_t, hash, buf_pool->page_hash, fold, watch);
2052
ut_d(watch->in_page_hash = FALSE);
2053
watch->buf_fix_count = 0;
2054
watch->state = BUF_BLOCK_POOL_WATCH;
2057
/****************************************************************//**
2058
Stop watching if the page has been read in.
2059
buf_pool_watch_set(space,offset) must have returned NULL before. */
2062
buf_pool_watch_unset(
2063
/*=================*/
2064
ulint space, /*!< in: space id */
2065
ulint offset) /*!< in: page number */
2068
buf_pool_t* buf_pool = buf_pool_get(space, offset);
2069
ulint fold = buf_page_address_fold(space, offset);
2071
buf_pool_mutex_enter(buf_pool);
2072
bpage = buf_page_hash_get_low(buf_pool, space, offset, fold);
2073
/* The page must exist because buf_pool_watch_set()
2074
increments buf_fix_count. */
2077
if (UNIV_UNLIKELY(!buf_pool_watch_is_sentinel(buf_pool, bpage))) {
2078
mutex_t* mutex = buf_page_get_mutex(bpage);
2081
ut_a(bpage->buf_fix_count > 0);
2082
bpage->buf_fix_count--;
2085
ut_a(bpage->buf_fix_count > 0);
2087
if (UNIV_LIKELY(!--bpage->buf_fix_count)) {
2088
buf_pool_watch_remove(buf_pool, fold, bpage);
1459
if (srv_buf_pool_curr_size + 1048576 > srv_buf_pool_size) {
1461
buf_pool_mutex_exit();
1463
/* Disable adaptive hash indexes and empty the index
1464
in order to free up memory in the buffer pool chunks. */
1465
buf_pool_shrink((srv_buf_pool_curr_size - srv_buf_pool_size)
1467
} else if (srv_buf_pool_curr_size + 1048576 < srv_buf_pool_size) {
1469
/* Enlarge the buffer pool by at least one megabyte */
1472
= srv_buf_pool_size - srv_buf_pool_curr_size;
1473
buf_chunk_t* chunks;
1476
chunks = mem_alloc((buf_pool->n_chunks + 1) * sizeof *chunks);
1478
memcpy(chunks, buf_pool->chunks, buf_pool->n_chunks
1481
chunk = &chunks[buf_pool->n_chunks];
1483
if (!buf_chunk_init(chunk, mem_size)) {
1486
buf_pool->curr_size += chunk->size;
1487
srv_buf_pool_curr_size = buf_pool->curr_size
1489
mem_free(buf_pool->chunks);
1490
buf_pool->chunks = chunks;
1491
buf_pool->n_chunks++;
1494
srv_buf_pool_old_size = srv_buf_pool_size;
1495
buf_pool_mutex_exit();
2092
buf_pool_mutex_exit(buf_pool);
2095
/****************************************************************//**
2096
Check if the page has been read in.
2097
This may only be called after buf_pool_watch_set(space,offset)
2098
has returned NULL and before invoking buf_pool_watch_unset(space,offset).
2099
@return FALSE if the given page was not read in, TRUE if it was */
2102
buf_pool_watch_occurred(
2103
/*====================*/
2104
ulint space, /*!< in: space id */
2105
ulint offset) /*!< in: page number */
2109
buf_pool_t* buf_pool = buf_pool_get(space, offset);
2110
ulint fold = buf_page_address_fold(space, offset);
2112
buf_pool_mutex_enter(buf_pool);
2114
bpage = buf_page_hash_get_low(buf_pool, space, offset, fold);
2115
/* The page must exist because buf_pool_watch_set()
2116
increments buf_fix_count. */
2118
ret = !buf_pool_watch_is_sentinel(buf_pool, bpage);
2119
buf_pool_mutex_exit(buf_pool);
1498
buf_pool_page_hash_rebuild();
2124
1501
/********************************************************************//**
2663
1996
/********************************************************************//**
2664
Find out if a pointer belongs to a buf_block_t. It can be a pointer to
2665
the buf_block_t itself or a member of it
2666
@return TRUE if ptr belongs to a buf_block_t struct */
2669
buf_pointer_is_block_field(
2670
/*=======================*/
2671
const void* ptr) /*!< in: pointer not dereferenced */
2675
for (i = 0; i < srv_buf_pool_instances; i++) {
2678
found = buf_pointer_is_block_field_instance(
2679
buf_pool_from_array(i), ptr);
2688
/********************************************************************//**
2689
1997
Find out if a buffer block was created by buf_chunk_init().
2690
1998
@return TRUE if "block" has been added to buf_pool->free by buf_chunk_init() */
2693
2001
buf_block_is_uncompressed(
2694
2002
/*======================*/
2695
buf_pool_t* buf_pool, /*!< in: buffer pool instance */
2696
const buf_block_t* block) /*!< in: pointer to block,
2003
const buf_block_t* block) /*!< in: pointer to block,
2699
ut_ad(buf_pool_mutex_own(buf_pool));
2006
ut_ad(buf_pool_mutex_own());
2701
2008
if (UNIV_UNLIKELY((((ulint) block) % sizeof *block) != 0)) {
2702
2009
/* The pointer should be aligned. */
2706
return(buf_pointer_is_block_field_instance(buf_pool, (void *)block));
2013
return(buf_pointer_is_block_field((void *)block));
2709
2016
/********************************************************************//**
2740
2044
|| (rw_latch == RW_X_LATCH)
2741
2045
|| (rw_latch == RW_NO_LATCH));
2742
2046
ut_ad((mode != BUF_GET_NO_LATCH) || (rw_latch == RW_NO_LATCH));
2743
ut_ad(mode == BUF_GET
2744
|| mode == BUF_GET_IF_IN_POOL
2745
|| mode == BUF_GET_NO_LATCH
2746
|| mode == BUF_GET_IF_IN_POOL_OR_WATCH);
2047
ut_ad((mode == BUF_GET) || (mode == BUF_GET_IF_IN_POOL)
2048
|| (mode == BUF_GET_NO_LATCH));
2747
2049
ut_ad(zip_size == fil_space_get_zip_size(space));
2748
2050
ut_ad(ut_is_2pow(zip_size));
2749
2051
#ifndef UNIV_LOG_DEBUG
2750
2052
ut_ad(!ibuf_inside() || ibuf_page(space, zip_size, offset, NULL));
2752
2054
buf_pool->stat.n_page_gets++;
2753
fold = buf_page_address_fold(space, offset);
2756
buf_pool_mutex_enter(buf_pool);
2057
buf_pool_mutex_enter();
2759
2060
/* If the guess is a compressed page descriptor that
2863
2144
case BUF_BLOCK_ZIP_DIRTY:
2864
2145
bpage = &block->page;
2865
2146
/* Protect bpage->buf_fix_count. */
2866
mutex_enter(&buf_pool->zip_mutex);
2147
mutex_enter(&buf_pool_zip_mutex);
2868
2149
if (bpage->buf_fix_count
2869
2150
|| buf_page_get_io_fix(bpage) != BUF_IO_NONE) {
2870
2151
/* This condition often occurs when the buffer
2871
2152
is not buffer-fixed, but I/O-fixed by
2872
2153
buf_page_init_for_read(). */
2873
mutex_exit(&buf_pool->zip_mutex);
2154
mutex_exit(&buf_pool_zip_mutex);
2874
2155
wait_until_unfixed:
2875
2156
/* The block is buffer-fixed or I/O-fixed.
2876
2157
Try again later. */
2877
buf_pool_mutex_exit(buf_pool);
2158
buf_pool_mutex_exit();
2878
2159
os_thread_sleep(WAIT_FOR_READ);
2883
2164
/* Allocate an uncompressed page. */
2884
buf_pool_mutex_exit(buf_pool);
2885
mutex_exit(&buf_pool->zip_mutex);
2165
buf_pool_mutex_exit();
2166
mutex_exit(&buf_pool_zip_mutex);
2887
block = buf_LRU_get_free_block(buf_pool, 0);
2168
block = buf_LRU_get_free_block(0);
2890
buf_pool_mutex_enter(buf_pool);
2171
buf_pool_mutex_enter();
2891
2172
mutex_enter(&block->mutex);
2894
buf_page_t* hash_bpage;
2896
hash_bpage = buf_page_hash_get_low(
2897
buf_pool, space, offset, fold);
2175
buf_page_t* hash_bpage
2176
= buf_page_hash_get(space, offset);
2899
2178
if (UNIV_UNLIKELY(bpage != hash_bpage)) {
2900
2179
/* The buf_pool->page_hash was modified
2901
while buf_pool->mutex was released.
2180
while buf_pool_mutex was released.
2902
2181
Free the block that was allocated. */
2904
2183
buf_LRU_block_free_non_file_page(block);
2958
2237
block->page.buf_fix_count = 1;
2959
2238
buf_block_set_io_fix(block, BUF_IO_READ);
2960
rw_lock_x_lock_func(&block->lock, 0, file, line);
2239
rw_lock_x_lock(&block->lock);
2962
2241
UNIV_MEM_INVALID(bpage, sizeof *bpage);
2964
2243
mutex_exit(&block->mutex);
2965
mutex_exit(&buf_pool->zip_mutex);
2244
mutex_exit(&buf_pool_zip_mutex);
2966
2245
buf_pool->n_pend_unzip++;
2968
buf_buddy_free(buf_pool, bpage, sizeof *bpage);
2247
buf_buddy_free(bpage, sizeof *bpage);
2970
buf_pool_mutex_exit(buf_pool);
2249
buf_pool_mutex_exit();
2972
2251
/* Decompress the page and apply buffered operations
2973
while not holding buf_pool->mutex or block->mutex. */
2252
while not holding buf_pool_mutex or block->mutex. */
2974
2253
success = buf_zip_decompress(block, srv_use_checksums);
3448
2716
buf_block_init_low(block);
3450
block->lock_hash_val = lock_rec_hash(space, offset);
3452
buf_page_init_low(&block->page);
2718
block->lock_hash_val = lock_rec_hash(space, offset);
3454
2720
/* Insert into the hash table of file pages */
3456
hash_page = buf_page_hash_get_low(buf_pool, space, offset, fold);
3458
if (UNIV_LIKELY(!hash_page)) {
3459
} else if (buf_pool_watch_is_sentinel(buf_pool, hash_page)) {
3460
/* Preserve the reference count. */
3461
ulint buf_fix_count = hash_page->buf_fix_count;
3463
ut_a(buf_fix_count > 0);
3464
block->page.buf_fix_count += buf_fix_count;
3465
buf_pool_watch_remove(buf_pool, fold, hash_page);
2722
hash_page = buf_page_hash_get(space, offset);
2724
if (UNIV_LIKELY_NULL(hash_page)) {
3467
2725
fprintf(stderr,
3468
2726
"InnoDB: Error: page %lu %lu already found"
3469
2727
" in the hash table: %p, %p\n",
3608
2856
if (UNIV_UNLIKELY(zip_size)) {
3609
2857
page_zip_set_size(&block->page.zip, zip_size);
3611
/* buf_pool->mutex may be released and
2859
/* buf_pool_mutex may be released and
3612
2860
reacquired by buf_buddy_alloc(). Thus, we
3613
2861
must release block->mutex in order not to
3614
2862
break the latching order in the reacquisition
3615
of buf_pool->mutex. We also must defer this
2863
of buf_pool_mutex. We also must defer this
3616
2864
operation until after the block descriptor has
3617
2865
been added to buf_pool->LRU and
3618
2866
buf_pool->page_hash. */
3619
2867
mutex_exit(&block->mutex);
3620
data = buf_buddy_alloc(buf_pool, zip_size, &lru);
2868
data = buf_buddy_alloc(zip_size, &lru);
3621
2869
mutex_enter(&block->mutex);
3622
2870
block->page.zip.data = data;
3641
2889
control block (bpage), in order to avoid the
3642
2890
invocation of buf_buddy_relocate_block() on
3643
2891
uninitialized data. */
3644
data = buf_buddy_alloc(buf_pool, zip_size, &lru);
3645
bpage = buf_buddy_alloc(buf_pool, sizeof *bpage, &lru);
3647
/* Initialize the buf_pool pointer. */
3648
bpage->buf_pool = buf_pool;
2892
data = buf_buddy_alloc(zip_size, &lru);
2893
bpage = buf_buddy_alloc(sizeof *bpage, &lru);
3650
2895
/* If buf_buddy_alloc() allocated storage from the LRU list,
3651
it released and reacquired buf_pool->mutex. Thus, we must
2896
it released and reacquired buf_pool_mutex. Thus, we must
3652
2897
check the page_hash again, as it may have been modified. */
3653
if (UNIV_UNLIKELY(lru)) {
3655
watch_page = buf_page_hash_get_low(
3656
buf_pool, space, offset, fold);
3659
&& !buf_pool_watch_is_sentinel(buf_pool,
3662
/* The block was added by some other thread. */
3664
buf_buddy_free(buf_pool, bpage, sizeof *bpage);
3665
buf_buddy_free(buf_pool, data, zip_size);
2898
if (UNIV_UNLIKELY(lru)
2899
&& UNIV_LIKELY_NULL(buf_page_hash_get(space, offset))) {
2901
/* The block was added by some other thread. */
2902
buf_buddy_free(bpage, sizeof *bpage);
2903
buf_buddy_free(data, zip_size);
3672
2909
page_zip_des_init(&bpage->zip);
3673
2910
page_zip_set_size(&bpage->zip, zip_size);
3674
2911
bpage->zip.data = data;
3676
mutex_enter(&buf_pool->zip_mutex);
2913
mutex_enter(&buf_pool_zip_mutex);
3677
2914
UNIV_MEM_DESC(bpage->zip.data,
3678
2915
page_zip_get_size(&bpage->zip), bpage);
3680
2916
buf_page_init_low(bpage);
3682
2917
bpage->state = BUF_BLOCK_ZIP_PAGE;
3683
2918
bpage->space = space;
3684
2919
bpage->offset = offset;
3687
2921
#ifdef UNIV_DEBUG
3688
2922
bpage->in_page_hash = FALSE;
3689
2923
bpage->in_zip_hash = FALSE;
3693
2927
#endif /* UNIV_DEBUG */
3695
2929
ut_d(bpage->in_page_hash = TRUE);
3697
if (UNIV_LIKELY_NULL(watch_page)) {
3698
/* Preserve the reference count. */
3699
ulint buf_fix_count = watch_page->buf_fix_count;
3700
ut_a(buf_fix_count > 0);
3701
bpage->buf_fix_count += buf_fix_count;
3702
ut_ad(buf_pool_watch_is_sentinel(buf_pool, watch_page));
3703
buf_pool_watch_remove(buf_pool, fold, watch_page);
3706
HASH_INSERT(buf_page_t, hash, buf_pool->page_hash, fold,
2930
HASH_INSERT(buf_page_t, hash, buf_pool->page_hash,
2931
buf_page_address_fold(space, offset), bpage);
3709
2933
/* The block must be put to the LRU list, to the old blocks */
3710
2934
buf_LRU_add_block(bpage, TRUE/* to old blocks */);
3747
2971
buf_frame_t* frame;
3748
2972
buf_block_t* block;
3750
2973
buf_block_t* free_block = NULL;
3751
2974
ulint time_ms = ut_time_ms();
3752
buf_pool_t* buf_pool = buf_pool_get(space, offset);
3755
2977
ut_ad(mtr->state == MTR_ACTIVE);
3756
2978
ut_ad(space || !zip_size);
3758
free_block = buf_LRU_get_free_block(buf_pool, 0);
3760
fold = buf_page_address_fold(space, offset);
3762
buf_pool_mutex_enter(buf_pool);
3764
block = (buf_block_t*) buf_page_hash_get_low(
3765
buf_pool, space, offset, fold);
3768
&& buf_page_in_file(&block->page)
3769
&& !buf_pool_watch_is_sentinel(buf_pool, &block->page)) {
2980
free_block = buf_LRU_get_free_block(0);
2982
buf_pool_mutex_enter();
2984
block = (buf_block_t*) buf_page_hash_get(space, offset);
2986
if (block && buf_page_in_file(&block->page)) {
3770
2987
#ifdef UNIV_IBUF_COUNT_DEBUG
3771
2988
ut_a(ibuf_count_get(space, offset) == 0);
3818
3035
page_zip_set_size(&block->page.zip, zip_size);
3819
3036
mutex_exit(&block->mutex);
3820
/* buf_pool->mutex may be released and reacquired by
3037
/* buf_pool_mutex may be released and reacquired by
3821
3038
buf_buddy_alloc(). Thus, we must release block->mutex
3822
3039
in order not to break the latching order in
3823
the reacquisition of buf_pool->mutex. We also must
3040
the reacquisition of buf_pool_mutex. We also must
3824
3041
defer this operation until after the block descriptor
3825
3042
has been added to buf_pool->LRU and buf_pool->page_hash. */
3826
data = buf_buddy_alloc(buf_pool, zip_size, &lru);
3043
data = buf_buddy_alloc(zip_size, &lru);
3827
3044
mutex_enter(&block->mutex);
3828
3045
block->page.zip.data = data;
4089
3305
#endif /* UNIV_DEBUG */
4091
3307
mutex_exit(buf_page_get_mutex(bpage));
4092
buf_pool_mutex_exit(buf_pool);
4095
/*********************************************************************//**
4096
Asserts that all file pages in the buffer are in a replaceable state.
4100
buf_all_freed_instance(
4101
/*===================*/
4102
buf_pool_t* buf_pool) /*!< in: buffer pool instancce */
4109
buf_pool_mutex_enter(buf_pool);
4111
chunk = buf_pool->chunks;
4113
for (i = buf_pool->n_chunks; i--; chunk++) {
4115
const buf_block_t* block = buf_chunk_not_freed(chunk);
4117
if (UNIV_LIKELY_NULL(block)) {
4119
"Page %lu %lu still fixed or dirty\n",
4120
(ulong) block->page.space,
4121
(ulong) block->page.offset);
4126
buf_pool_mutex_exit(buf_pool);
4131
/*********************************************************************//**
4132
Invalidates file pages in one buffer pool instance */
3308
buf_pool_mutex_exit();
3311
/*********************************************************************//**
3312
Invalidates the file pages in the buffer pool when an archive recovery is
3313
completed. All the file pages buffered must be in a replaceable state when
3314
this function is called: not latched and not modified. */
4135
buf_pool_invalidate_instance(
4136
/*=========================*/
4137
buf_pool_t* buf_pool) /*!< in: buffer pool instance */
3317
buf_pool_invalidate(void)
3318
/*=====================*/
4140
3321
enum buf_flush i;
4142
buf_pool_mutex_enter(buf_pool);
3323
buf_pool_mutex_enter();
4144
3325
for (i = BUF_FLUSH_LRU; i < BUF_FLUSH_N_TYPES; i++) {
4182
3363
buf_pool->LRU_flush_ended = 0;
4184
3365
memset(&buf_pool->stat, 0x00, sizeof(buf_pool->stat));
4185
buf_refresh_io_stats(buf_pool);
4187
buf_pool_mutex_exit(buf_pool);
4190
/*********************************************************************//**
4191
Invalidates the file pages in the buffer pool when an archive recovery is
4192
completed. All the file pages buffered must be in a replaceable state when
4193
this function is called: not latched and not modified. */
4196
buf_pool_invalidate(void)
4197
/*=====================*/
4201
for (i = 0; i < srv_buf_pool_instances; i++) {
4202
buf_pool_invalidate_instance(buf_pool_from_array(i));
3366
buf_refresh_io_stats();
3368
buf_pool_mutex_exit();
4206
3371
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
4207
3372
/*********************************************************************//**
4208
Validates data in one buffer pool instance
3373
Validates the buffer buf_pool data structure.
4209
3374
@return TRUE */
4212
buf_pool_validate_instance(
4213
/*=======================*/
4214
buf_pool_t* buf_pool) /*!< in: buffer pool instance */
4217
3381
buf_chunk_t* chunk;
4341
/* It is OK to read oldest_modification here because
4342
we have acquired buf_pool->zip_mutex above which acts
4343
as the 'block->mutex' for these bpages. */
4344
3508
ut_a(!b->oldest_modification);
4345
ut_a(buf_page_hash_get(buf_pool, b->space, b->offset) == b);
3509
ut_a(buf_page_hash_get(b->space, b->offset) == b);
4351
/* Check dirty blocks. */
3515
/* Check dirty compressed-only blocks. */
4353
buf_flush_list_mutex_enter(buf_pool);
4354
3517
for (b = UT_LIST_GET_FIRST(buf_pool->flush_list); b;
4355
3518
b = UT_LIST_GET_NEXT(list, b)) {
4356
3519
ut_ad(b->in_flush_list);
4357
ut_a(b->oldest_modification);
4360
3521
switch (buf_page_get_state(b)) {
4361
3522
case BUF_BLOCK_ZIP_DIRTY:
3523
ut_a(b->oldest_modification);
4364
3527
switch (buf_page_get_io_fix(b)) {
4365
3528
case BUF_IO_NONE:
4366
3529
case BUF_IO_READ:
4368
3532
case BUF_IO_WRITE:
4369
3533
switch (buf_page_get_flush_type(b)) {
4370
3534
case BUF_FLUSH_LRU:
4417
3577
(ulong) n_free);
3580
ut_a(UT_LIST_GET_LEN(buf_pool->flush_list) == n_flush);
4421
3582
ut_a(buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE] == n_single_flush);
4422
3583
ut_a(buf_pool->n_flush[BUF_FLUSH_LIST] == n_list_flush);
4423
3584
ut_a(buf_pool->n_flush[BUF_FLUSH_LRU] == n_lru_flush);
4425
buf_pool_mutex_exit(buf_pool);
3586
buf_pool_mutex_exit();
4427
3588
ut_a(buf_LRU_validate());
4428
ut_a(buf_flush_validate(buf_pool));
4433
/*********************************************************************//**
4434
Validates the buffer buf_pool data structure.
4443
for (i = 0; i < srv_buf_pool_instances; i++) {
4444
buf_pool_t* buf_pool;
4446
buf_pool = buf_pool_from_array(i);
4448
buf_pool_validate_instance(buf_pool);
3589
ut_a(buf_flush_validate());
4453
3593
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
4455
3595
#if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
4456
3596
/*********************************************************************//**
4457
Prints info of the buffer buf_pool data structure for one instance. */
3597
Prints info of the buffer buf_pool data structure. */
4462
buf_pool_t* buf_pool)
4464
index_id_t* index_ids;
4471
3610
buf_chunk_t* chunk;
4472
3611
dict_index_t* index;
4870
3947
buf_LRU_stat_sum.io, buf_LRU_stat_cur.io,
4871
3948
buf_LRU_stat_sum.unzip, buf_LRU_stat_cur.unzip);
4873
buf_refresh_io_stats(buf_pool);
4874
buf_pool_mutex_exit(buf_pool);
4877
/*********************************************************************//**
4878
Prints info of the buffer i/o. */
4883
FILE* file) /*!< in/out: buffer where to print */
4887
for (i = 0; i < srv_buf_pool_instances; i++) {
4888
buf_pool_t* buf_pool;
4890
buf_pool = buf_pool_from_array(i);
4891
buf_print_io_instance(buf_pool, file);
3950
buf_refresh_io_stats();
3951
buf_pool_mutex_exit();
4895
3954
/**********************************************************************//**
4896
3955
Refreshes the statistics used to print per-second averages. */
4899
buf_refresh_io_stats(
4900
/*=================*/
4901
buf_pool_t* buf_pool) /*!< in: buffer pool instance */
3958
buf_refresh_io_stats(void)
3959
/*======================*/
4903
buf_pool->last_printout_time = ut_time();
3961
buf_pool->last_printout_time = time(NULL);
4904
3962
buf_pool->old_stat = buf_pool->stat;
4907
/**********************************************************************//**
4908
Refreshes the statistics used to print per-second averages. */
4911
buf_refresh_io_stats_all(void)
4912
/*==========================*/
4916
for (i = 0; i < srv_buf_pool_instances; i++) {
4917
buf_pool_t* buf_pool;
4919
buf_pool = buf_pool_from_array(i);
4921
buf_refresh_io_stats(buf_pool);
4925
/**********************************************************************//**
4926
Check if all pages in all buffer pools are in a replacable state.
4927
@return FALSE if not */
3965
/*********************************************************************//**
3966
Asserts that all file pages in the buffer are in a replaceable state.
4930
3970
buf_all_freed(void)
4931
3971
/*===============*/
4935
for (i = 0; i < srv_buf_pool_instances; i++) {
4936
buf_pool_t* buf_pool;
4938
buf_pool = buf_pool_from_array(i);
4940
if (!buf_all_freed_instance(buf_pool)) {
3978
buf_pool_mutex_enter();
3980
chunk = buf_pool->chunks;
3982
for (i = buf_pool->n_chunks; i--; chunk++) {
3984
const buf_block_t* block = buf_chunk_not_freed(chunk);
3986
if (UNIV_LIKELY_NULL(block)) {
3988
"Page %lu %lu still fixed or dirty\n",
3989
(ulong) block->page.space,
3990
(ulong) block->page.offset);
3995
buf_pool_mutex_exit();
4948
4000
/*********************************************************************//**
4949
4001
Checks that there currently are no pending i/o-operations for the buffer
4954
4006
buf_pool_check_no_pending_io(void)
4955
4007
/*==============================*/
4960
buf_pool_mutex_enter_all();
4962
for (i = 0; i < srv_buf_pool_instances && ret; i++) {
4963
const buf_pool_t* buf_pool;
4965
buf_pool = buf_pool_from_array(i);
4967
if (buf_pool->n_pend_reads
4968
+ buf_pool->n_flush[BUF_FLUSH_LRU]
4969
+ buf_pool->n_flush[BUF_FLUSH_LIST]
4970
+ buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]) {
4011
buf_pool_mutex_enter();
4013
if (buf_pool->n_pend_reads + buf_pool->n_flush[BUF_FLUSH_LRU]
4014
+ buf_pool->n_flush[BUF_FLUSH_LIST]
4015
+ buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]) {
4976
buf_pool_mutex_exit_all();
4021
buf_pool_mutex_exit();
4982
Code currently not used
4983
4026
/*********************************************************************//**
4984
4027
Gets the current length of the free list of buffer blocks.
4985
4028
@return length of the free list */