| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2008, Google Inc. |
| 5 |
|
| 6 |
Portions of this file contain modifications contributed and copyrighted by |
| 7 |
Google, Inc. Those modifications are gratefully acknowledged and are described |
| 8 |
briefly in the InnoDB documentation. The contributions by Google are |
| 9 |
incorporated with their permission, and subject to the conditions contained in |
| 10 |
the file COPYING.Google. |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or modify it under |
| 13 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 14 |
Free Software Foundation. |
| 15 |
|
| 16 |
This program is designed to work with certain software (including |
| 17 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 18 |
as designated in a particular file or component or in included license |
| 19 |
documentation. The authors of MySQL hereby grant you an additional |
| 20 |
permission to link the program and your derivative works with the |
| 21 |
separately licensed software that they have either included with |
| 22 |
the program or referenced in the documentation. |
| 23 |
|
| 24 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 25 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 26 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 27 |
for more details. |
| 28 |
|
| 29 |
You should have received a copy of the GNU General Public License along with |
| 30 |
this program; if not, write to the Free Software Foundation, Inc., |
| 31 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 |
|
| 33 |
*****************************************************************************/ |
| 34 |
|
| 35 |
/** @file include/buf0buf.ic |
| 36 |
The database buffer buf_pool |
| 37 |
|
| 38 |
Created 11/5/1995 Heikki Tuuri |
| 39 |
*******************************************************/ |
| 40 |
|
| 41 |
#include "mtr0mtr.h" |
| 42 |
|
| 43 |
#ifndef UNIV_HOTBACKUP |
| 44 |
#include "buf0flu.h" |
| 45 |
#include "buf0lru.h" |
| 46 |
#include "buf0rea.h" |
| 47 |
#include "fsp0types.h" |
| 48 |
#include "sync0debug.h" |
| 49 |
#include "ut0new.h" |
| 50 |
#endif /* !UNIV_HOTBACKUP */ |
| 51 |
|
| 52 |
/** A chunk of buffers. The buffer pool is allocated in chunks. */ |
| 53 |
struct buf_chunk_t { |
| 54 |
ulint size; /*!< size of frames[] and blocks[] */ |
| 55 |
unsigned char *mem; /*!< pointer to the memory area which |
| 56 |
was allocated for the frames */ |
| 57 |
buf_block_t *blocks; /*!< array of buffer control blocks */ |
| 58 |
|
| 59 |
/** Get the size of 'mem' in bytes. */ |
| 60 |
size_t mem_size() const { return ut::large_page_allocation_size(mem); } |
| 61 |
|
| 62 |
/** Advices the OS that this chunk should not be dumped to a core file. |
| 63 |
Emits a warning to the log if could not succeed. |
| 64 |
@return true iff succeeded, false if no OS support or failed */ |
| 65 |
bool madvise_dump(); |
| 66 |
|
| 67 |
/** Advices the OS that this chunk should be dumped to a core file. |
| 68 |
Emits a warning to the log if could not succeed. |
| 69 |
@return true iff succeeded, false if no OS support or failed */ |
| 70 |
bool madvise_dont_dump(); |
| 71 |
|
| 72 |
/** Checks if this chunk contains a given block without dereferencing it |
| 73 |
@param[in] ptr A pointer which we want to check |
| 74 |
@return true iff the ptr points inside the chunk */ |
| 75 |
bool contains(const buf_block_t *ptr) const { |
| 76 |
/* In theory comparing pointers which don't belong to the same array can |
| 77 |
give unspecified results when using < and <=. |
| 78 |
By using std::less(_equal) we get at least the guarantee that std::less |
| 79 |
relation forms a total order, which still doesn't gives us what we really |
| 80 |
need (as it could be a weird total order in which ptr seems to be inside an |
| 81 |
array even though it is not), but at least it is specified and people seem |
| 82 |
to use this in real world applications except for old MS-DOS real-mode. |
| 83 |
Also, we might have an ABA problem here, where the memory of chunk was freed |
| 84 |
and later reallocated at similar address thus the range contains the ptr |
| 85 |
again. I hope this is not a problem because we require the chunk to be |
| 86 |
aligned the same way both times, so the pointer should never point in the |
| 87 |
middle (non-zero offset) of the buf_block_t struct. |
| 88 |
The only 100% sure and compliant way to implement this function would be a |
| 89 |
loop over all blocks inside the chunk, which is too slow. */ |
| 90 |
return std::less_equal<const buf_block_t *>{}(blocks, ptr) && |
| 91 |
std::less<const buf_block_t *>{}(ptr, blocks + size); |
| 92 |
} |
| 93 |
}; |
| 94 |
|
| 95 |
/** Gets the current size of buffer buf_pool in bytes. |
| 96 |
@return size in bytes */ |
| 97 |
static inline ulint buf_pool_get_curr_size(void) { |
| 98 |
return (srv_buf_pool_curr_size); |
| 99 |
} |
| 100 |
|
| 101 |
#ifndef UNIV_HOTBACKUP |
| 102 |
/** Calculates the index of a buffer pool to the buf_pool[] array. |
| 103 |
@return the position of the buffer pool in buf_pool[] */ |
| 104 |
static inline ulint buf_pool_index( |
| 105 |
const buf_pool_t *buf_pool) /*!< in: buffer pool */ |
| 106 |
{ |
| 107 |
ulint i = buf_pool - buf_pool_ptr; |
| 108 |
ut_ad(i < MAX_BUFFER_POOLS); |
| 109 |
ut_ad(i < srv_buf_pool_instances); |
| 110 |
return (i); |
| 111 |
} |
| 112 |
|
| 113 |
/** Returns the buffer pool instance given a page instance |
| 114 |
@return buf_pool */ |
| 115 |
static inline buf_pool_t *buf_pool_from_bpage( |
| 116 |
const buf_page_t *bpage) /*!< in: buffer pool page */ |
| 117 |
{ |
| 118 |
ulint i; |
| 119 |
i = bpage->buf_pool_index; |
| 120 |
ut_ad(i < srv_buf_pool_instances); |
| 121 |
return (&buf_pool_ptr[i]); |
| 122 |
} |
| 123 |
|
| 124 |
/** Returns the buffer pool instance given a block instance |
| 125 |
@return buf_pool */ |
| 126 |
static inline buf_pool_t *buf_pool_from_block( |
| 127 |
const buf_block_t *block) /*!< in: block */ |
| 128 |
{ |
| 129 |
return (buf_pool_from_bpage(&block->page)); |
| 130 |
} |
| 131 |
|
| 132 |
/** Gets the current size of buffer buf_pool in pages. |
| 133 |
@return size in pages*/ |
| 134 |
static inline ulint buf_pool_get_n_pages(void) { |
| 135 |
return (buf_pool_get_curr_size() / UNIV_PAGE_SIZE); |
| 136 |
} |
| 137 |
|
| 138 |
/** Reads the freed_page_clock of a buffer block. |
| 139 |
@return freed_page_clock */ |
| 140 |
static inline ulint buf_page_get_freed_page_clock( |
| 141 |
const buf_page_t *bpage) /*!< in: block */ |
| 142 |
{ |
| 143 |
/* This is sometimes read without holding any buffer pool mutex. */ |
| 144 |
return (bpage->freed_page_clock); |
| 145 |
} |
| 146 |
|
| 147 |
/** Reads the freed_page_clock of a buffer block. |
| 148 |
@return freed_page_clock */ |
| 149 |
static inline ulint buf_block_get_freed_page_clock( |
| 150 |
const buf_block_t *block) /*!< in: block */ |
| 151 |
{ |
| 152 |
return (buf_page_get_freed_page_clock(&block->page)); |
| 153 |
} |
| 154 |
|
| 155 |
/** Tells, for heuristics, if a block is still close enough to the MRU end of |
| 156 |
the LRU list meaning that it is not in danger of getting evicted and also |
| 157 |
implying that it has been accessed recently. |
| 158 |
The page must be either buffer-fixed, either its page hash must be locked. |
| 159 |
@param[in] bpage block |
| 160 |
@return true if block is close to MRU end of LRU */ |
| 161 |
static inline bool buf_page_peek_if_young(const buf_page_t *bpage) { |
| 162 |
buf_pool_t *buf_pool = buf_pool_from_bpage(bpage); |
| 163 |
|
| 164 |
ut_ad(bpage->buf_fix_count > 0 || |
| 165 |
buf_page_hash_lock_held_s_or_x(buf_pool, bpage)); |
| 166 |
|
| 167 |
/* FIXME: bpage->freed_page_clock is 31 bits */ |
| 168 |
return ((buf_pool->freed_page_clock & ((1UL << 31) - 1)) < |
| 169 |
((ulint)bpage->freed_page_clock + |
| 170 |
(buf_pool->curr_size * |
| 171 |
(BUF_LRU_OLD_RATIO_DIV - buf_pool->LRU_old_ratio) / |
| 172 |
(BUF_LRU_OLD_RATIO_DIV * 4)))); |
| 173 |
} |
| 174 |
|
| 175 |
/** Recommends a move of a block to the start of the LRU list if there is |
| 176 |
danger of dropping from the buffer pool. |
| 177 |
NOTE: does not reserve the LRU list mutex. |
| 178 |
@param[in] bpage block to make younger |
| 179 |
@return true if should be made younger */ |
| 180 |
static inline bool buf_page_peek_if_too_old(const buf_page_t *bpage) { |
| 181 |
buf_pool_t *buf_pool = buf_pool_from_bpage(bpage); |
| 182 |
|
| 183 |
if (buf_pool->freed_page_clock == 0) { |
| 184 |
/* If eviction has not started yet, do not update the |
| 185 |
statistics or move blocks in the LRU list. This is |
| 186 |
either the warm-up phase or an in-memory workload. */ |
| 187 |
return false; |
| 188 |
} else if (get_buf_LRU_old_threshold() != std::chrono::seconds::zero() && |
| 189 |
bpage->old) { |
| 190 |
const auto access_time = buf_page_is_accessed(bpage); |
| 191 |
|
| 192 |
if (access_time != std::chrono::steady_clock::time_point{} && |
| 193 |
(std::chrono::steady_clock::now() - access_time) >= |
| 194 |
get_buf_LRU_old_threshold()) { |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
buf_pool->stat.n_pages_not_made_young++; |
| 199 |
return false; |
| 200 |
} else { |
| 201 |
return (!buf_page_peek_if_young(bpage)); |
| 202 |
} |
| 203 |
} |
| 204 |
#endif /* !UNIV_HOTBACKUP */ |
| 205 |
|
| 206 |
/** Gets the state of a block. |
| 207 |
@return state */ |
| 208 |
static inline enum buf_page_state buf_page_get_state( |
| 209 |
const buf_page_t *bpage) /*!< in: pointer to the control block */ |
| 210 |
{ |
| 211 |
enum buf_page_state state = bpage->state; |
| 212 |
|
| 213 |
#ifdef UNIV_DEBUG |
| 214 |
switch (state) { |
| 215 |
case BUF_BLOCK_POOL_WATCH: |
| 216 |
case BUF_BLOCK_ZIP_PAGE: |
| 217 |
case BUF_BLOCK_ZIP_DIRTY: |
| 218 |
case BUF_BLOCK_NOT_USED: |
| 219 |
case BUF_BLOCK_READY_FOR_USE: |
| 220 |
case BUF_BLOCK_FILE_PAGE: |
| 221 |
case BUF_BLOCK_MEMORY: |
| 222 |
case BUF_BLOCK_REMOVE_HASH: |
| 223 |
break; |
| 224 |
default: |
| 225 |
ut_error; |
| 226 |
} |
| 227 |
#endif /* UNIV_DEBUG */ |
| 228 |
|
| 229 |
return (state); |
| 230 |
} |
| 231 |
/** Gets the state of a block. |
| 232 |
@return state */ |
| 233 |
static inline enum buf_page_state buf_block_get_state( |
| 234 |
const buf_block_t *block) /*!< in: pointer to the control block */ |
| 235 |
{ |
| 236 |
return (buf_page_get_state(&block->page)); |
| 237 |
} |
| 238 |
|
| 239 |
#ifndef UNIV_HOTBACKUP |
| 240 |
#ifdef UNIV_DEBUG |
| 241 |
/** Assert that a given buffer pool page is private to the caller: no pointers |
| 242 |
to it exist in any buffer pool list or hash table. Accessing pages by iterating |
| 243 |
over buffer pool chunks is not considered here. Furthermore, assert that no |
| 244 |
buffer pool locks except for LRU list mutex and page hash are held. |
| 245 |
@param[in] bpage pointer to a buffer pool page |
| 246 |
@param[in] hold_block_mutex flag whether holding block mutex |
| 247 |
@param[in] hold_zip_free_mutex flag whether holding zip free mutex */ |
| 248 |
static inline bool buf_page_is_private(const buf_page_t *bpage, |
| 249 |
bool hold_block_mutex, |
| 250 |
bool hold_zip_free_mutex) { |
| 251 |
buf_pool_t *buf_pool = buf_pool_from_bpage(bpage); |
| 252 |
|
| 253 |
ut_a(!bpage->in_page_hash); |
| 254 |
ut_a(!bpage->in_zip_hash); |
| 255 |
ut_a(!bpage->in_flush_list); |
| 256 |
ut_a(!bpage->in_free_list); |
| 257 |
ut_a(!bpage->in_LRU_list); |
| 258 |
if (!hold_block_mutex) { |
| 259 |
ut_a(!mutex_own(buf_page_get_mutex(bpage))); |
| 260 |
} |
| 261 |
ut_a(!mutex_own(&buf_pool->free_list_mutex)); |
| 262 |
if (!hold_zip_free_mutex) { |
| 263 |
ut_a(!mutex_own(&buf_pool->zip_free_mutex)); |
| 264 |
} |
| 265 |
ut_a(!mutex_own(&buf_pool->zip_hash_mutex)); |
| 266 |
|
| 267 |
return (true); |
| 268 |
} |
| 269 |
#endif /* UNIV_DEBUG */ |
| 270 |
#endif /* !UNIV_HOTBACKUP */ |
| 271 |
|
| 272 |
/** Sets the state of a block. |
| 273 |
@param[in,out] bpage pointer to control block |
| 274 |
@param[in] state state */ |
| 275 |
static inline void buf_page_set_state(buf_page_t *bpage, |
| 276 |
enum buf_page_state state) { |
| 277 |
#ifndef UNIV_HOTBACKUP |
| 278 |
#ifdef UNIV_DEBUG |
| 279 |
enum buf_page_state old_state = buf_page_get_state(bpage); |
| 280 |
buf_pool_t *buf_pool = buf_pool_from_bpage(bpage); |
| 281 |
|
| 282 |
/* If the page is IO-fixed, no one can alter the state, but the thread that is |
| 283 |
handling the IO. */ |
| 284 |
ut_a(!bpage->someone_has_io_responsibility() || |
| 285 |
bpage->current_thread_has_io_responsibility()); |
| 286 |
|
| 287 |
switch (old_state) { |
| 288 |
case BUF_BLOCK_POOL_WATCH: |
| 289 |
ut_error; |
| 290 |
break; |
| 291 |
case BUF_BLOCK_ZIP_PAGE: |
| 292 |
ut_a(state == BUF_BLOCK_ZIP_DIRTY); |
| 293 |
break; |
| 294 |
case BUF_BLOCK_ZIP_DIRTY: |
| 295 |
ut_a(state == BUF_BLOCK_ZIP_PAGE); |
| 296 |
ut_a(mutex_own(buf_page_get_mutex(bpage))); |
| 297 |
ut_a(buf_flush_list_mutex_own(buf_pool)); |
| 298 |
ut_a(bpage->in_flush_list); |
| 299 |
break; |
| 300 |
case BUF_BLOCK_NOT_USED: |
| 301 |
ut_a(state == BUF_BLOCK_READY_FOR_USE); |
| 302 |
ut_a(buf_page_is_private(bpage, false, false)); |
| 303 |
break; |
| 304 |
case BUF_BLOCK_READY_FOR_USE: |
| 305 |
ut_a(state == BUF_BLOCK_MEMORY || state == BUF_BLOCK_FILE_PAGE || |
| 306 |
state == BUF_BLOCK_NOT_USED); |
| 307 |
ut_a(buf_page_is_private(bpage, state == BUF_BLOCK_FILE_PAGE, |
| 308 |
state == BUF_BLOCK_NOT_USED)); |
| 309 |
break; |
| 310 |
case BUF_BLOCK_MEMORY: |
| 311 |
ut_a(state == BUF_BLOCK_NOT_USED); |
| 312 |
ut_a(buf_page_is_private(bpage, false, true)); |
| 313 |
break; |
| 314 |
case BUF_BLOCK_FILE_PAGE: |
| 315 |
ut_a(state == BUF_BLOCK_NOT_USED || state == BUF_BLOCK_REMOVE_HASH); |
| 316 |
if (state == BUF_BLOCK_REMOVE_HASH) { |
| 317 |
ut_a(!bpage->in_page_hash); |
| 318 |
ut_a(!bpage->in_zip_hash); |
| 319 |
ut_a(!bpage->in_LRU_list); |
| 320 |
ut_a(!bpage->in_free_list); |
| 321 |
ut_a(mutex_own(buf_page_get_mutex(bpage))); |
| 322 |
ut_a(mutex_own(&buf_pool->LRU_list_mutex)); |
| 323 |
ut_a(buf_page_hash_lock_held_x(buf_pool, bpage)); |
| 324 |
} |
| 325 |
break; |
| 326 |
case BUF_BLOCK_REMOVE_HASH: |
| 327 |
ut_a(state == BUF_BLOCK_MEMORY); |
| 328 |
break; |
| 329 |
} |
| 330 |
#endif /* UNIV_DEBUG */ |
| 331 |
#endif /* !UNIV_HOTBACKUP */ |
| 332 |
bpage->state = state; |
| 333 |
ut_ad(buf_page_get_state(bpage) == state); |
| 334 |
} |
| 335 |
|
| 336 |
/** Sets the state of a block. */ |
| 337 |
static inline void buf_block_set_state( |
| 338 |
buf_block_t *block, /*!< in/out: pointer to control block */ |
| 339 |
enum buf_page_state state) /*!< in: state */ |
| 340 |
{ |
| 341 |
buf_page_set_state(&block->page, state); |
| 342 |
} |
| 343 |
|
| 344 |
/** Determines if a block is mapped to a tablespace. |
| 345 |
@return true if mapped */ |
| 346 |
static inline bool buf_page_in_file( |
| 347 |
const buf_page_t *bpage) /*!< in: pointer to control block */ |
| 348 |
{ |
| 349 |
switch (buf_page_get_state(bpage)) { |
| 350 |
case BUF_BLOCK_POOL_WATCH: |
| 351 |
ut_error; |
| 352 |
break; |
| 353 |
case BUF_BLOCK_ZIP_PAGE: |
| 354 |
case BUF_BLOCK_ZIP_DIRTY: |
| 355 |
case BUF_BLOCK_FILE_PAGE: |
| 356 |
return true; |
| 357 |
case BUF_BLOCK_NOT_USED: |
| 358 |
case BUF_BLOCK_READY_FOR_USE: |
| 359 |
case BUF_BLOCK_MEMORY: |
| 360 |
case BUF_BLOCK_REMOVE_HASH: |
| 361 |
break; |
| 362 |
} |
| 363 |
|
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
#ifndef UNIV_HOTBACKUP |
| 368 |
/** Determines if a block should be on unzip_LRU list. |
| 369 |
@return true if block belongs to unzip_LRU */ |
| 370 |
static inline bool buf_page_belongs_to_unzip_LRU( |
| 371 |
const buf_page_t *bpage) /*!< in: pointer to control block */ |
| 372 |
{ |
| 373 |
ut_ad(buf_page_in_file(bpage)); |
| 374 |
|
| 375 |
return (bpage->zip.data && buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE); |
| 376 |
} |
| 377 |
|
| 378 |
/** Gets the mutex of a block. |
| 379 |
@return pointer to mutex protecting bpage */ |
| 380 |
static inline BPageMutex *buf_page_get_mutex( |
| 381 |
const buf_page_t *bpage) /*!< in: pointer to control block */ |
| 382 |
{ |
| 383 |
buf_pool_t *buf_pool = buf_pool_from_bpage(bpage); |
| 384 |
|
| 385 |
switch (buf_page_get_state(bpage)) { |
| 386 |
case BUF_BLOCK_POOL_WATCH: |
| 387 |
ut_error; |
| 388 |
case BUF_BLOCK_ZIP_PAGE: |
| 389 |
case BUF_BLOCK_ZIP_DIRTY: |
| 390 |
return (&buf_pool->zip_mutex); |
| 391 |
default: |
| 392 |
return (&((buf_block_t *)bpage)->mutex); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
/** Get the flush type of a page. |
| 397 |
@return flush type */ |
| 398 |
static inline buf_flush_t buf_page_get_flush_type( |
| 399 |
const buf_page_t *bpage) /*!< in: buffer page */ |
| 400 |
{ |
| 401 |
buf_flush_t flush_type = (buf_flush_t)bpage->flush_type; |
| 402 |
|
| 403 |
#ifdef UNIV_DEBUG |
| 404 |
switch (flush_type) { |
| 405 |
case BUF_FLUSH_LRU: |
| 406 |
case BUF_FLUSH_LIST: |
| 407 |
case BUF_FLUSH_SINGLE_PAGE: |
| 408 |
return (flush_type); |
| 409 |
case BUF_FLUSH_N_TYPES: |
| 410 |
ut_error; |
| 411 |
} |
| 412 |
ut_error; |
| 413 |
#else /* UNIV_DEBUG */ |
| 414 |
return (flush_type); |
| 415 |
#endif /* UNIV_DEBUG */ |
| 416 |
} |
| 417 |
/** Set the flush type of a page. */ |
| 418 |
static inline void buf_page_set_flush_type( |
| 419 |
buf_page_t *bpage, /*!< in: buffer page */ |
| 420 |
buf_flush_t flush_type) /*!< in: flush type */ |
| 421 |
{ |
| 422 |
ut_ad(mutex_own(buf_page_get_mutex(bpage))); |
| 423 |
ut_ad(mutex_own(&buf_pool_from_bpage(bpage)->flush_state_mutex)); |
| 424 |
bpage->flush_type = flush_type; |
| 425 |
ut_ad(buf_page_get_flush_type(bpage) == flush_type); |
| 426 |
} |
| 427 |
|
| 428 |
/** Map a block to a file page. |
| 429 |
@param[in,out] block pointer to control block |
| 430 |
@param[in] page_id page id */ |
| 431 |
static inline void buf_block_set_file_page(buf_block_t *block, |
| 432 |
const page_id_t &page_id) { |
| 433 |
buf_block_set_state(block, BUF_BLOCK_FILE_PAGE); |
| 434 |
block->page.reset_page_id(page_id); |
| 435 |
} |
| 436 |
|
| 437 |
/** Gets the io_fix state of a block. |
| 438 |
@return io_fix state */ |
| 439 |
static inline enum buf_io_fix buf_page_get_io_fix( |
| 440 |
const buf_page_t *bpage) /*!< in: pointer to the control block */ |
| 441 |
{ |
| 442 |
return bpage->get_io_fix(); |
| 443 |
} |
| 444 |
|
| 445 |
/** Gets the io_fix state of a block. |
| 446 |
@return io_fix state */ |
| 447 |
static inline enum buf_io_fix buf_block_get_io_fix( |
| 448 |
const buf_block_t *block) /*!< in: pointer to the control block */ |
| 449 |
{ |
| 450 |
return (buf_page_get_io_fix(&block->page)); |
| 451 |
} |
| 452 |
|
| 453 |
/** Sets the io_fix state of a block. */ |
| 454 |
static inline void buf_page_set_io_fix( |
| 455 |
buf_page_t *bpage, /*!< in/out: control block */ |
| 456 |
enum buf_io_fix io_fix) /*!< in: io_fix state */ |
| 457 |
{ |
| 458 |
bpage->set_io_fix(io_fix); |
| 459 |
|
| 460 |
ut_ad(buf_page_get_io_fix(bpage) == io_fix); |
| 461 |
} |
| 462 |
|
| 463 |
/** Sets the io_fix state of a block. */ |
| 464 |
static inline void buf_block_set_io_fix( |
| 465 |
buf_block_t *block, /*!< in/out: control block */ |
| 466 |
enum buf_io_fix io_fix) /*!< in: io_fix state */ |
| 467 |
{ |
| 468 |
buf_page_set_io_fix(&block->page, io_fix); |
| 469 |
} |
| 470 |
|
| 471 |
/** Makes a block sticky. A sticky block implies that even after we release |
| 472 |
the buf_pool->LRU_list_mutex and the block->mutex: |
| 473 |
* it cannot be removed from the flush_list |
| 474 |
* the block descriptor cannot be relocated |
| 475 |
* it cannot be removed from the LRU list |
| 476 |
Note that: |
| 477 |
* the block can still change its position in the LRU list |
| 478 |
* the next and previous pointers can change. |
| 479 |
@param[in,out] bpage control block */ |
| 480 |
static inline void buf_page_set_sticky(buf_page_t *bpage) { |
| 481 |
#ifdef UNIV_DEBUG |
| 482 |
buf_pool_t *buf_pool = buf_pool_from_bpage(bpage); |
| 483 |
ut_ad(mutex_own(&buf_pool->LRU_list_mutex)); |
| 484 |
#endif /* UNIV_DEBUG */ |
| 485 |
|
| 486 |
ut_ad(mutex_own(buf_page_get_mutex(bpage))); |
| 487 |
ut_ad(buf_page_get_io_fix(bpage) == BUF_IO_NONE); |
| 488 |
ut_ad(bpage->in_LRU_list); |
| 489 |
bpage->set_io_fix(BUF_IO_PIN); |
| 490 |
} |
| 491 |
|
| 492 |
/** Removes stickiness of a block. */ |
| 493 |
static inline void buf_page_unset_sticky( |
| 494 |
buf_page_t *bpage) /*!< in/out: control block */ |
| 495 |
{ |
| 496 |
ut_ad(mutex_own(buf_page_get_mutex(bpage))); |
| 497 |
ut_ad(buf_page_get_io_fix(bpage) == BUF_IO_PIN); |
| 498 |
bpage->set_io_fix(BUF_IO_NONE); |
| 499 |
} |
| 500 |
|
| 501 |
/** Determine if a buffer block can be relocated in memory. The block |
| 502 |
can be dirty, but it must not be I/O-fixed or bufferfixed. */ |
| 503 |
static inline bool buf_page_can_relocate( |
| 504 |
const buf_page_t *bpage) /*!< control block being relocated */ |
| 505 |
{ |
| 506 |
ut_ad(mutex_own(buf_page_get_mutex(bpage))); |
| 507 |
ut_ad(buf_page_in_file(bpage)); |
| 508 |
ut_ad(bpage->in_LRU_list); |
| 509 |
|
| 510 |
return (buf_page_get_io_fix(bpage) == BUF_IO_NONE && |
| 511 |
bpage->buf_fix_count == 0); |
| 512 |
} |
| 513 |
|
| 514 |
/** Determine if a block has been flagged old. |
| 515 |
@param[in] bpage control block |
| 516 |
@return true if old */ |
| 517 |
static inline bool buf_page_is_old(const buf_page_t *bpage) { |
| 518 |
#ifdef UNIV_DEBUG |
| 519 |
buf_pool_t *buf_pool = buf_pool_from_bpage(bpage); |
| 520 |
/* Buffer page mutex is not strictly required here for heuristic |
| 521 |
purposes even if LRU mutex is not being held. Keep the assertion |
| 522 |
for now since all the callers hold it. */ |
| 523 |
ut_ad(mutex_own(buf_page_get_mutex(bpage)) || |
| 524 |
mutex_own(&buf_pool->LRU_list_mutex)); |
| 525 |
#endif /* UNIV_DEBUG */ |
| 526 |
ut_ad(buf_page_in_file(bpage)); |
| 527 |
|
| 528 |
return (bpage->old); |
| 529 |
} |
| 530 |
|
| 531 |
/** Flag a block old. |
| 532 |
@param[in] bpage control block |
| 533 |
@param[in] old old */ |
| 534 |
static inline void buf_page_set_old(buf_page_t *bpage, bool old) { |
| 535 |
#ifdef UNIV_DEBUG |
| 536 |
buf_pool_t *buf_pool = buf_pool_from_bpage(bpage); |
| 537 |
#endif /* UNIV_DEBUG */ |
| 538 |
ut_a(buf_page_in_file(bpage)); |
| 539 |
ut_ad(mutex_own(&buf_pool->LRU_list_mutex)); |
| 540 |
ut_ad(bpage->in_LRU_list); |
| 541 |
|
| 542 |
#ifdef UNIV_LRU_DEBUG |
| 543 |
ut_a((buf_pool->LRU_old_len == 0) == (buf_pool->LRU_old == NULL)); |
| 544 |
/* If a block is flagged "old", the LRU_old list must exist. */ |
| 545 |
ut_a(!old || buf_pool->LRU_old); |
| 546 |
|
| 547 |
if (UT_LIST_GET_PREV(LRU, bpage) && UT_LIST_GET_NEXT(LRU, bpage)) { |
| 548 |
const buf_page_t *prev = UT_LIST_GET_PREV(LRU, bpage); |
| 549 |
const buf_page_t *next = UT_LIST_GET_NEXT(LRU, bpage); |
| 550 |
if (prev->old == next->old) { |
| 551 |
ut_a(prev->old == old); |
| 552 |
} else { |
| 553 |
ut_a(!prev->old); |
| 554 |
ut_a(buf_pool->LRU_old == (old ? bpage : next)); |
| 555 |
} |
| 556 |
} |
| 557 |
#endif /* UNIV_LRU_DEBUG */ |
| 558 |
|
| 559 |
bpage->old = old; |
| 560 |
} |
| 561 |
|
| 562 |
static inline std::chrono::steady_clock::time_point buf_page_is_accessed( |
| 563 |
const buf_page_t *bpage) { |
| 564 |
ut_ad(buf_page_in_file(bpage)); |
| 565 |
|
| 566 |
return (bpage->access_time); |
| 567 |
} |
| 568 |
|
| 569 |
/** Flag a block accessed. */ |
| 570 |
static inline void buf_page_set_accessed( |
| 571 |
buf_page_t *bpage) /*!< in/out: control block */ |
| 572 |
{ |
| 573 |
ut_ad(mutex_own(buf_page_get_mutex(bpage))); |
| 574 |
|
| 575 |
ut_a(buf_page_in_file(bpage)); |
| 576 |
|
| 577 |
if (bpage->access_time == std::chrono::steady_clock::time_point{}) { |
| 578 |
/* Make this the time of the first access. */ |
| 579 |
bpage->access_time = std::chrono::steady_clock::now(); |
| 580 |
} |
| 581 |
} |
| 582 |
#endif /* !UNIV_HOTBACKUP */ |
| 583 |
|
| 584 |
/** Gets the buf_block_t handle of a buffered file block if an uncompressed |
| 585 |
page frame exists, or NULL. page frame exists, or NULL. The caller must hold |
| 586 |
either the appropriate hash lock in any mode, either the LRU list mutex. Note: |
| 587 |
even though bpage is not declared a const we don't update its value. It is safe |
| 588 |
to make this pure. |
| 589 |
@param[in] bpage control block, or NULL |
| 590 |
@return control block, or NULL */ |
| 591 |
static inline buf_block_t *buf_page_get_block(buf_page_t *bpage) { |
| 592 |
if (bpage != nullptr) { |
| 593 |
#ifndef UNIV_HOTBACKUP |
| 594 |
#ifdef UNIV_DEBUG |
| 595 |
buf_pool_t *buf_pool = buf_pool_from_bpage(bpage); |
| 596 |
ut_ad(buf_page_hash_lock_held_s_or_x(buf_pool, bpage) || |
| 597 |
mutex_own(&buf_pool->LRU_list_mutex)); |
| 598 |
#endif /* UNIV_DEBUG */ |
| 599 |
#endif /* !UNIV_HOTBACKUP */ |
| 600 |
ut_ad(buf_page_in_file(bpage)); |
| 601 |
|
| 602 |
if (buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE) { |
| 603 |
return ((buf_block_t *)bpage); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
return (nullptr); |
| 608 |
} |
| 609 |
|
| 610 |
#ifndef UNIV_HOTBACKUP |
| 611 |
#ifdef UNIV_DEBUG |
| 612 |
/** Gets a pointer to the memory frame of a block. |
| 613 |
@return pointer to the frame */ |
| 614 |
static inline buf_frame_t *buf_block_get_frame( |
| 615 |
const buf_block_t *block) /*!< in: pointer to the control block */ |
| 616 |
{ |
| 617 |
ut_ad(block); |
| 618 |
|
| 619 |
switch (buf_block_get_state(block)) { |
| 620 |
case BUF_BLOCK_POOL_WATCH: |
| 621 |
case BUF_BLOCK_ZIP_PAGE: |
| 622 |
case BUF_BLOCK_ZIP_DIRTY: |
| 623 |
case BUF_BLOCK_NOT_USED: |
| 624 |
ut_error; |
| 625 |
break; |
| 626 |
case BUF_BLOCK_FILE_PAGE: |
| 627 |
ut_a(block->page.buf_fix_count > 0); |
| 628 |
[[fallthrough]]; |
| 629 |
case BUF_BLOCK_READY_FOR_USE: |
| 630 |
case BUF_BLOCK_MEMORY: |
| 631 |
case BUF_BLOCK_REMOVE_HASH: |
| 632 |
goto ok; |
| 633 |
} |
| 634 |
ut_error; |
| 635 |
ok: |
| 636 |
return ((buf_frame_t *)block->frame); |
| 637 |
} |
| 638 |
#endif /* UNIV_DEBUG */ |
| 639 |
#endif /* !UNIV_HOTBACKUP */ |
| 640 |
|
| 641 |
/*********************************************************************** |
| 642 |
FIXME_FTS Gets the frame the pointer is pointing to. */ |
| 643 |
static inline buf_frame_t *buf_frame_align( |
| 644 |
/* out: pointer to frame */ |
| 645 |
byte *ptr) /* in: pointer to a frame */ |
| 646 |
{ |
| 647 |
buf_frame_t *frame; |
| 648 |
|
| 649 |
ut_ad(ptr); |
| 650 |
|
| 651 |
frame = (buf_frame_t *)ut_align_down(ptr, UNIV_PAGE_SIZE); |
| 652 |
|
| 653 |
return (frame); |
| 654 |
} |
| 655 |
|
| 656 |
/** Gets the space id, page offset, and byte offset within page of a |
| 657 |
pointer pointing to a buffer frame containing a file page. */ |
| 658 |
static inline void buf_ptr_get_fsp_addr( |
| 659 |
const void *ptr, /*!< in: pointer to a buffer frame */ |
| 660 |
space_id_t *space, /*!< out: space id */ |
| 661 |
fil_addr_t *addr) /*!< out: page offset and byte offset */ |
| 662 |
{ |
| 663 |
const page_t *page = (const page_t *)ut_align_down(ptr, UNIV_PAGE_SIZE); |
| 664 |
|
| 665 |
*space = mach_read_from_4(page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID); |
| 666 |
addr->page = mach_read_from_4(page + FIL_PAGE_OFFSET); |
| 667 |
addr->boffset = static_cast<uint32_t>(ut_align_offset(ptr, UNIV_PAGE_SIZE)); |
| 668 |
} |
| 669 |
|
| 670 |
#ifndef UNIV_HOTBACKUP |
| 671 |
|
| 672 |
/** Allocates a buf_page_t descriptor. This function must succeed. In case |
| 673 |
of failure we assert in this function. |
| 674 |
@return: the allocated descriptor. */ |
| 675 |
static inline buf_page_t *buf_page_alloc_descriptor(void) { |
| 676 |
buf_page_t *bpage; |
| 677 |
|
| 678 |
bpage = |
| 679 |
(buf_page_t *)ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof *bpage); |
| 680 |
ut_ad(bpage); |
| 681 |
UNIV_MEM_ALLOC(bpage, sizeof *bpage); |
| 682 |
/* This is required for valgrind to work correctly. */ |
| 683 |
bpage->m_space = nullptr; |
| 684 |
|
| 685 |
return (bpage); |
| 686 |
} |
| 687 |
|
| 688 |
/** Frees a buffer block which does not contain a file page. */ |
| 689 |
static inline void buf_block_free( |
| 690 |
buf_block_t *block) /*!< in, own: block to be freed */ |
| 691 |
{ |
| 692 |
ut_a(buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE); |
| 693 |
|
| 694 |
buf_LRU_block_free_non_file_page(block); |
| 695 |
} |
| 696 |
#endif /* !UNIV_HOTBACKUP */ |
| 697 |
|
| 698 |
/** Copies contents of a buffer frame to a given buffer. |
| 699 |
@return buf */ |
| 700 |
static inline byte *buf_frame_copy( |
| 701 |
byte *buf, /*!< in: buffer to copy to */ |
| 702 |
const buf_frame_t *frame) /*!< in: buffer frame */ |
| 703 |
{ |
| 704 |
ut_ad(buf && frame); |
| 705 |
|
| 706 |
ut_memcpy(buf, frame, UNIV_PAGE_SIZE); |
| 707 |
|
| 708 |
return (buf); |
| 709 |
} |
| 710 |
|
| 711 |
#ifndef UNIV_HOTBACKUP |
| 712 |
static inline lsn_t buf_page_get_newest_modification(const buf_page_t *bpage) { |
| 713 |
auto block_mutex = buf_page_get_mutex(bpage); |
| 714 |
|
| 715 |
mutex_enter(block_mutex); |
| 716 |
|
| 717 |
auto lsn = buf_page_in_file(bpage) ? bpage->get_newest_lsn() : 0; |
| 718 |
|
| 719 |
mutex_exit(block_mutex); |
| 720 |
|
| 721 |
return lsn; |
| 722 |
} |
| 723 |
|
| 724 |
/** Increment the modify clock. |
| 725 |
The caller must |
| 726 |
(1) block bufferfix count has to be zero, |
| 727 |
(2) own X or SX latch on the block->lock, or |
| 728 |
(3) operate on a thread-private temporary table |
| 729 |
@param[in,out] block buffer block */ |
| 730 |
static inline void buf_block_modify_clock_inc(buf_block_t *block) { |
| 731 |
#ifdef UNIV_DEBUG |
| 732 |
buf_pool_t *buf_pool = buf_pool_from_bpage(&block->page); |
| 733 |
#endif /* UNIV_DEBUG */ |
| 734 |
block->ahi.validate(); |
| 735 |
|
| 736 |
/* No block latch is acquired for internal temporary tables. */ |
| 737 |
ut_ad(fsp_is_system_temporary(block->page.id.space()) || |
| 738 |
(block->page.buf_fix_count == 0 && |
| 739 |
mutex_own(&buf_pool->LRU_list_mutex)) || |
| 740 |
rw_lock_own_flagged(&block->lock, RW_LOCK_FLAG_X | RW_LOCK_FLAG_SX)); |
| 741 |
|
| 742 |
block->modify_clock++; |
| 743 |
} |
| 744 |
|
| 745 |
inline void buf_page_t::set_oldest_lsn(lsn_t lsn) noexcept { |
| 746 |
ut_ad(buf_page_get_state(this) == BUF_BLOCK_POOL_WATCH || |
| 747 |
mutex_own(buf_page_get_mutex(this))); |
| 748 |
/* To modify the page we should have matching MDL latches, that will make the |
| 749 |
result of stale guaranteed to be current. */ |
| 750 |
ut_ad(lsn == 0 || !was_stale()); |
| 751 |
oldest_modification = lsn; |
| 752 |
} |
| 753 |
|
| 754 |
/** Increments the bufferfix count. |
| 755 |
@param[in,out] bpage block to bufferfix |
| 756 |
@return the count */ |
| 757 |
static inline ulint buf_block_fix(buf_page_t *bpage) { |
| 758 |
auto count = bpage->buf_fix_count.fetch_add(1) + 1; |
| 759 |
ut_ad(count > 0); |
| 760 |
return (count); |
| 761 |
} |
| 762 |
|
| 763 |
/** Increments the bufferfix count. |
| 764 |
@param[in,out] block block to bufferfix |
| 765 |
@return the count */ |
| 766 |
static inline ulint buf_block_fix(buf_block_t *block) { |
| 767 |
return (buf_block_fix(&block->page)); |
| 768 |
} |
| 769 |
|
| 770 |
static inline void buf_block_buf_fix_inc_func(IF_DEBUG(ut::Location location, ) |
| 771 |
buf_block_t *block) { |
| 772 |
#ifdef UNIV_DEBUG |
| 773 |
/* No debug latch is acquired if block belongs to system temporary. |
| 774 |
Debug latch is not of much help if access to block is single |
| 775 |
threaded. */ |
| 776 |
if (!fsp_is_system_temporary(block->page.id.space())) { |
| 777 |
auto ret = rw_lock_s_lock_nowait(&block->debug_latch, location); |
| 778 |
ut_a(ret); |
| 779 |
} |
| 780 |
#endif /* UNIV_DEBUG */ |
| 781 |
|
| 782 |
buf_block_fix(block); |
| 783 |
} |
| 784 |
|
| 785 |
/** Decrements the bufferfix count. |
| 786 |
@param[in,out] bpage block to bufferunfix |
| 787 |
@return the remaining buffer-fix count */ |
| 788 |
static inline ulint buf_block_unfix(buf_page_t *bpage) { |
| 789 |
ut_ad(!mutex_own(buf_page_get_mutex(bpage))); |
| 790 |
const auto count = bpage->buf_fix_count.fetch_sub(1) - 1; |
| 791 |
static_assert(std::is_unsigned<decltype(count)>::value, "Must be unsigned"); |
| 792 |
ut_ad(count != std::numeric_limits<decltype(count)>::max()); |
| 793 |
return (count); |
| 794 |
} |
| 795 |
|
| 796 |
/** Decrements the bufferfix count. |
| 797 |
@param[in,out] block block to bufferunfix |
| 798 |
@return the remaining buffer-fix count */ |
| 799 |
static inline ulint buf_block_unfix(buf_block_t *block) { |
| 800 |
return (buf_block_unfix(&block->page)); |
| 801 |
} |
| 802 |
|
| 803 |
/** Decrements the bufferfix count. */ |
| 804 |
static inline void buf_block_buf_fix_dec( |
| 805 |
buf_block_t *block) /*!< in/out: block to bufferunfix */ |
| 806 |
{ |
| 807 |
#ifdef UNIV_DEBUG |
| 808 |
/* No debug latch is acquired if block belongs to system temporary. |
| 809 |
Debug latch is not of much help if access to block is single |
| 810 |
threaded. */ |
| 811 |
if (!fsp_is_system_temporary(block->page.id.space())) { |
| 812 |
rw_lock_s_unlock(&block->debug_latch); |
| 813 |
} |
| 814 |
#endif /* UNIV_DEBUG */ |
| 815 |
|
| 816 |
buf_block_unfix(block); |
| 817 |
} |
| 818 |
|
| 819 |
/** Returns the buffer pool instance given a page id. |
| 820 |
@param[in] page_id page id |
| 821 |
@return buffer pool */ |
| 822 |
static inline buf_pool_t *buf_pool_get(const page_id_t &page_id) { |
| 823 |
/* 2log of BUF_READ_AHEAD_AREA (64) */ |
| 824 |
page_no_t ignored_page_no = page_id.page_no() >> 6; |
| 825 |
|
| 826 |
page_id_t id(page_id.space(), ignored_page_no); |
| 827 |
|
| 828 |
ulint i = id.hash() % srv_buf_pool_instances; |
| 829 |
|
| 830 |
return (&buf_pool_ptr[i]); |
| 831 |
} |
| 832 |
|
| 833 |
/** Returns the buffer pool instance given its array index |
| 834 |
@return buffer pool */ |
| 835 |
static inline buf_pool_t *buf_pool_from_array( |
| 836 |
ulint index) /*!< in: array index to get |
| 837 |
buffer pool instance from */ |
| 838 |
{ |
| 839 |
ut_ad(index < MAX_BUFFER_POOLS); |
| 840 |
ut_ad(index < srv_buf_pool_instances); |
| 841 |
return (&buf_pool_ptr[index]); |
| 842 |
} |
| 843 |
|
| 844 |
/** Returns the control block of a file page, NULL if not found. |
| 845 |
@param[in] buf_pool buffer pool instance |
| 846 |
@param[in] page_id page id |
| 847 |
@return block, NULL if not found */ |
| 848 |
static inline buf_page_t *buf_page_hash_get_low(buf_pool_t *buf_pool, |
| 849 |
const page_id_t &page_id) { |
| 850 |
buf_page_t *bpage; |
| 851 |
|
| 852 |
#ifdef UNIV_DEBUG |
| 853 |
rw_lock_t *hash_lock; |
| 854 |
|
| 855 |
hash_lock = hash_get_lock(buf_pool->page_hash, page_id.hash()); |
| 856 |
ut_ad(rw_lock_own(hash_lock, RW_LOCK_X) || rw_lock_own(hash_lock, RW_LOCK_S)); |
| 857 |
#endif /* UNIV_DEBUG */ |
| 858 |
|
| 859 |
/* Look for the page in the hash table */ |
| 860 |
|
| 861 |
HASH_SEARCH(hash, buf_pool->page_hash, page_id.hash(), buf_page_t *, bpage, |
| 862 |
ut_ad(bpage->in_page_hash && !bpage->in_zip_hash && |
| 863 |
buf_page_in_file(bpage)), |
| 864 |
page_id == bpage->id); |
| 865 |
if (bpage) { |
| 866 |
ut_a(buf_page_in_file(bpage)); |
| 867 |
ut_ad(bpage->in_page_hash); |
| 868 |
ut_ad(!bpage->in_zip_hash); |
| 869 |
ut_ad(buf_pool_from_bpage(bpage) == buf_pool); |
| 870 |
} |
| 871 |
|
| 872 |
return (bpage); |
| 873 |
} |
| 874 |
|
| 875 |
/** Returns the control block of a file page, NULL if not found. |
| 876 |
If the block is found and lock is not NULL then the appropriate |
| 877 |
page_hash lock is acquired in the specified lock mode. Otherwise, |
| 878 |
mode value is ignored. It is up to the caller to release the |
| 879 |
lock. If the block is found and the lock is NULL then the page_hash |
| 880 |
lock is released by this function. |
| 881 |
@param[in] buf_pool buffer pool instance |
| 882 |
@param[in] page_id page id |
| 883 |
@param[in,out] lock lock of the page hash acquired if bpage is |
| 884 |
found, NULL otherwise. If NULL is passed then the hash_lock is released by |
| 885 |
this function. |
| 886 |
@param[in] lock_mode RW_LOCK_X or RW_LOCK_S. Ignored if |
| 887 |
lock == NULL |
| 888 |
@param[in] watch if true, return watch sentinel also. |
| 889 |
@return pointer to the bpage or NULL; if NULL, lock is also NULL or |
| 890 |
a watch sentinel. */ |
| 891 |
static inline buf_page_t *buf_page_hash_get_locked(buf_pool_t *buf_pool, |
| 892 |
const page_id_t &page_id, |
| 893 |
rw_lock_t **lock, |
| 894 |
ulint lock_mode, |
| 895 |
bool watch) { |
| 896 |
buf_page_t *bpage = nullptr; |
| 897 |
rw_lock_t *hash_lock; |
| 898 |
ulint mode = RW_LOCK_S; |
| 899 |
|
| 900 |
if (lock != nullptr) { |
| 901 |
*lock = nullptr; |
| 902 |
ut_ad(lock_mode == RW_LOCK_X || lock_mode == RW_LOCK_S); |
| 903 |
mode = lock_mode; |
| 904 |
} |
| 905 |
|
| 906 |
hash_lock = hash_get_lock(buf_pool->page_hash, page_id.hash()); |
| 907 |
|
| 908 |
ut_ad(!rw_lock_own(hash_lock, RW_LOCK_X) && |
| 909 |
!rw_lock_own(hash_lock, RW_LOCK_S)); |
| 910 |
|
| 911 |
if (mode == RW_LOCK_S) { |
| 912 |
rw_lock_s_lock(hash_lock, UT_LOCATION_HERE); |
| 913 |
|
| 914 |
/* If not own LRU_list_mutex, page_hash can be changed. */ |
| 915 |
hash_lock = |
| 916 |
hash_lock_s_confirm(hash_lock, buf_pool->page_hash, page_id.hash()); |
| 917 |
} else { |
| 918 |
rw_lock_x_lock(hash_lock, UT_LOCATION_HERE); |
| 919 |
/* If not own LRU_list_mutex, page_hash can be changed. */ |
| 920 |
hash_lock = |
| 921 |
hash_lock_x_confirm(hash_lock, buf_pool->page_hash, page_id.hash()); |
| 922 |
} |
| 923 |
|
| 924 |
bpage = buf_page_hash_get_low(buf_pool, page_id); |
| 925 |
|
| 926 |
if (!bpage || buf_pool_watch_is_sentinel(buf_pool, bpage)) { |
| 927 |
if (!watch) { |
| 928 |
bpage = nullptr; |
| 929 |
} |
| 930 |
goto unlock_and_exit; |
| 931 |
} |
| 932 |
|
| 933 |
ut_ad(buf_page_in_file(bpage)); |
| 934 |
ut_ad(page_id == bpage->id); |
| 935 |
|
| 936 |
if (lock == nullptr) { |
| 937 |
/* The caller wants us to release the page_hash lock */ |
| 938 |
goto unlock_and_exit; |
| 939 |
} else { |
| 940 |
/* To be released by the caller */ |
| 941 |
*lock = hash_lock; |
| 942 |
goto exit; |
| 943 |
} |
| 944 |
|
| 945 |
unlock_and_exit: |
| 946 |
if (mode == RW_LOCK_S) { |
| 947 |
rw_lock_s_unlock(hash_lock); |
| 948 |
} else { |
| 949 |
ut_ad(mode == RW_LOCK_X); |
| 950 |
rw_lock_x_unlock(hash_lock); |
| 951 |
} |
| 952 |
exit: |
| 953 |
return (bpage); |
| 954 |
} |
| 955 |
|
| 956 |
/** Returns the control block of a file page, NULL if not found. |
| 957 |
If the block is found and lock is not NULL then the appropriate |
| 958 |
page_hash lock is acquired in the specified lock mode. Otherwise, |
| 959 |
mode value is ignored. It is up to the caller to release the |
| 960 |
lock. If the block is found and the lock is NULL then the page_hash |
| 961 |
lock is released by this function. |
| 962 |
@param[in] buf_pool buffer pool instance |
| 963 |
@param[in] page_id page id |
| 964 |
@param[in,out] lock lock of the page hash acquired if bpage is |
| 965 |
found, NULL otherwise. If NULL is passed then the hash_lock is released by |
| 966 |
this function. |
| 967 |
@param[in] lock_mode RW_LOCK_X or RW_LOCK_S. Ignored if |
| 968 |
lock == NULL |
| 969 |
@return pointer to the block or NULL; if NULL, lock is also NULL. */ |
| 970 |
static inline buf_block_t *buf_block_hash_get_locked(buf_pool_t *buf_pool, |
| 971 |
const page_id_t &page_id, |
| 972 |
rw_lock_t **lock, |
| 973 |
ulint lock_mode) { |
| 974 |
buf_page_t *bpage = |
| 975 |
buf_page_hash_get_locked(buf_pool, page_id, lock, lock_mode); |
| 976 |
buf_block_t *block = buf_page_get_block(bpage); |
| 977 |
|
| 978 |
if (block != nullptr) { |
| 979 |
ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE); |
| 980 |
ut_ad(!lock || rw_lock_own(*lock, lock_mode)); |
| 981 |
|
| 982 |
return (block); |
| 983 |
} else if (bpage) { |
| 984 |
/* It is not a block. Just a bpage */ |
| 985 |
ut_ad(buf_page_in_file(bpage)); |
| 986 |
|
| 987 |
if (lock) { |
| 988 |
if (lock_mode == RW_LOCK_S) { |
| 989 |
rw_lock_s_unlock(*lock); |
| 990 |
} else { |
| 991 |
ut_ad(lock_mode == RW_LOCK_X); |
| 992 |
rw_lock_x_unlock(*lock); |
| 993 |
} |
| 994 |
*lock = nullptr; |
| 995 |
} |
| 996 |
return (nullptr); |
| 997 |
} |
| 998 |
|
| 999 |
ut_ad(!bpage); |
| 1000 |
ut_ad(lock == nullptr || *lock == nullptr); |
| 1001 |
return (nullptr); |
| 1002 |
} |
| 1003 |
|
| 1004 |
/** Returns true if the page can be found in the buffer pool hash table. |
| 1005 |
NOTE that it is possible that the page is not yet read from disk, |
| 1006 |
though. |
| 1007 |
@param[in] page_id page id |
| 1008 |
@return true if found in the page hash table */ |
| 1009 |
static inline bool buf_page_peek(const page_id_t &page_id) { |
| 1010 |
buf_pool_t *buf_pool = buf_pool_get(page_id); |
| 1011 |
|
| 1012 |
return (buf_page_hash_get(buf_pool, page_id) != nullptr); |
| 1013 |
} |
| 1014 |
|
| 1015 |
/** Releases a compressed-only page acquired with buf_page_get_zip(). */ |
| 1016 |
static inline void buf_page_release_zip( |
| 1017 |
buf_page_t *bpage) /*!< in: buffer block */ |
| 1018 |
{ |
| 1019 |
ut_ad(bpage); |
| 1020 |
ut_a(bpage->buf_fix_count > 0); |
| 1021 |
|
| 1022 |
switch (buf_page_get_state(bpage)) { |
| 1023 |
case BUF_BLOCK_FILE_PAGE: |
| 1024 |
#ifdef UNIV_DEBUG |
| 1025 |
{ |
| 1026 |
/* No debug latch is acquired if block belongs to system |
| 1027 |
temporary. Debug latch is not of much help if access to block |
| 1028 |
is single threaded. */ |
| 1029 |
buf_block_t *block = reinterpret_cast<buf_block_t *>(bpage); |
| 1030 |
if (!fsp_is_system_temporary(block->page.id.space())) { |
| 1031 |
rw_lock_s_unlock(&block->debug_latch); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
#endif /* UNIV_DEBUG */ |
| 1035 |
[[fallthrough]]; |
| 1036 |
|
| 1037 |
case BUF_BLOCK_ZIP_PAGE: |
| 1038 |
case BUF_BLOCK_ZIP_DIRTY: |
| 1039 |
buf_block_unfix(reinterpret_cast<buf_block_t *>(bpage)); |
| 1040 |
return; |
| 1041 |
|
| 1042 |
case BUF_BLOCK_POOL_WATCH: |
| 1043 |
case BUF_BLOCK_NOT_USED: |
| 1044 |
case BUF_BLOCK_READY_FOR_USE: |
| 1045 |
case BUF_BLOCK_MEMORY: |
| 1046 |
case BUF_BLOCK_REMOVE_HASH: |
| 1047 |
break; |
| 1048 |
} |
| 1049 |
|
| 1050 |
ut_error; |
| 1051 |
} |
| 1052 |
|
| 1053 |
/** Releases a latch, if specified. */ |
| 1054 |
static inline void buf_page_release_latch( |
| 1055 |
buf_block_t *block, /*!< in: buffer block */ |
| 1056 |
ulint rw_latch) /*!< in: RW_S_LATCH, RW_X_LATCH, |
| 1057 |
RW_NO_LATCH */ |
| 1058 |
{ |
| 1059 |
#ifdef UNIV_DEBUG |
| 1060 |
/* No debug latch is acquired if block belongs to system |
| 1061 |
temporary. Debug latch is not of much help if access to block |
| 1062 |
is single threaded. */ |
| 1063 |
if (!fsp_is_system_temporary(block->page.id.space())) { |
| 1064 |
rw_lock_s_unlock(&block->debug_latch); |
| 1065 |
} |
| 1066 |
#endif /* UNIV_DEBUG */ |
| 1067 |
|
| 1068 |
if (rw_latch == RW_S_LATCH) { |
| 1069 |
rw_lock_s_unlock(&block->lock); |
| 1070 |
} else if (rw_latch == RW_SX_LATCH) { |
| 1071 |
rw_lock_sx_unlock(&block->lock); |
| 1072 |
} else if (rw_latch == RW_X_LATCH) { |
| 1073 |
rw_lock_x_unlock(&block->lock); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
#ifdef UNIV_DEBUG |
| 1078 |
/** Adds latch level info for the rw-lock protecting the buffer frame. This |
| 1079 |
should be called in the debug version after a successful latching of a |
| 1080 |
page if we know the latching order level of the acquired latch. */ |
| 1081 |
static inline void buf_block_dbg_add_level( |
| 1082 |
buf_block_t *block, /*!< in: buffer page |
| 1083 |
where we have acquired latch */ |
| 1084 |
latch_level_t level) /*!< in: latching order level */ |
| 1085 |
{ |
| 1086 |
sync_check_lock(&block->lock, level); |
| 1087 |
} |
| 1088 |
#endif /* UNIV_DEBUG */ |
| 1089 |
|
| 1090 |
/** Get the nth chunk's buffer block in the specified buffer pool. |
| 1091 |
@return the nth chunk's buffer block. */ |
| 1092 |
static inline buf_block_t *buf_get_nth_chunk_block( |
| 1093 |
const buf_pool_t *buf_pool, /*!< in: buffer pool instance */ |
| 1094 |
ulint n, /*!< in: nth chunk in the buffer pool */ |
| 1095 |
ulint *chunk_size) /*!< in: chunk size */ |
| 1096 |
{ |
| 1097 |
const buf_chunk_t *chunk; |
| 1098 |
|
| 1099 |
chunk = buf_pool->chunks + n; |
| 1100 |
*chunk_size = chunk->size; |
| 1101 |
return (chunk->blocks); |
| 1102 |
} |
| 1103 |
|
| 1104 |
/** Calculate aligned buffer pool size based on srv_buf_pool_chunk_unit, |
| 1105 |
if needed. |
| 1106 |
@param[in] size size in bytes |
| 1107 |
@return aligned size */ |
| 1108 |
static inline ulint buf_pool_size_align(ulint size) { |
| 1109 |
const ulint m = srv_buf_pool_instances * srv_buf_pool_chunk_unit; |
| 1110 |
size = std::max(size, srv_buf_pool_min_size); |
| 1111 |
|
| 1112 |
if (size % m == 0) { |
| 1113 |
return (size); |
| 1114 |
} else { |
| 1115 |
return ((size / m + 1) * m); |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
/** Return how many more pages must be added to the withdraw list to reach the |
| 1120 |
withdraw target of the currently ongoing buffer pool resize. |
| 1121 |
@param[in] buf_pool buffer pool instance |
| 1122 |
@return page count to be withdrawn or zero if the target is already achieved or |
| 1123 |
if the buffer pool is not currently being resized. */ |
| 1124 |
static inline ulint buf_get_withdraw_depth(buf_pool_t *buf_pool) { |
| 1125 |
os_rmb; |
| 1126 |
if (buf_pool->curr_size >= buf_pool->old_size) return 0; |
| 1127 |
ulint withdraw_len = UT_LIST_GET_LEN(buf_pool->withdraw); |
| 1128 |
return (buf_pool->withdraw_target > withdraw_len |
| 1129 |
? buf_pool->withdraw_target - withdraw_len |
| 1130 |
: 0); |
| 1131 |
} |
| 1132 |
|
| 1133 |
#endif /* !UNIV_HOTBACKUP */ |