| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file buf/buf0rea.cc |
| 29 |
The database buffer read |
| 30 |
|
| 31 |
Created 11/5/1995 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include <mysql/service_thd_wait.h> |
| 35 |
#include <stddef.h> |
| 36 |
|
| 37 |
#include "buf0buf.h" |
| 38 |
#include "buf0dblwr.h" |
| 39 |
#include "buf0flu.h" |
| 40 |
#include "buf0lru.h" |
| 41 |
#include "buf0rea.h" |
| 42 |
#include "fil0fil.h" |
| 43 |
#include "ha_prototypes.h" |
| 44 |
#include "ibuf0ibuf.h" |
| 45 |
#include "log0recv.h" |
| 46 |
#include "mtr0mtr.h" |
| 47 |
#include "my_dbug.h" |
| 48 |
|
| 49 |
#include "os0file.h" |
| 50 |
#include "srv0srv.h" |
| 51 |
#include "srv0start.h" |
| 52 |
#include "trx0sys.h" |
| 53 |
#include "ut0new.h" |
| 54 |
|
| 55 |
/** There must be at least this many pages in buf_pool in the area to start |
| 56 |
a random read-ahead */ |
| 57 |
inline page_no_t BUF_READ_AHEAD_RANDOM_THRESHOLD(const buf_pool_t *b) { |
| 58 |
return 5 + b->read_ahead_area / 8; |
| 59 |
} |
| 60 |
|
| 61 |
/** If there are buf_pool->curr_size per the number below pending reads, then |
| 62 |
read-ahead is not done: this is to prevent flooding the buffer pool with |
| 63 |
i/o-fixed buffer blocks */ |
| 64 |
static constexpr uint32_t BUF_READ_AHEAD_PEND_LIMIT = 2; |
| 65 |
|
| 66 |
ulint buf_read_page_low(dberr_t *err, bool sync, ulint type, ulint mode, |
| 67 |
const page_id_t &page_id, const page_size_t &page_size, |
| 68 |
bool unzip) { |
| 69 |
buf_page_t *bpage; |
| 70 |
|
| 71 |
*err = DB_SUCCESS; |
| 72 |
|
| 73 |
if (page_id.space() == TRX_SYS_SPACE && |
| 74 |
dblwr::v1::is_inside(page_id.page_no())) { |
| 75 |
ib::error(ER_IB_MSG_139) |
| 76 |
<< "Trying to read legacy doublewrite buffer page " << page_id; |
| 77 |
|
| 78 |
return (0); |
| 79 |
} |
| 80 |
|
| 81 |
if (ibuf_bitmap_page(page_id, page_size) || trx_sys_hdr_page(page_id)) { |
| 82 |
/* Trx sys header is so low in the latching order that we play |
| 83 |
safe and do not leave the i/o-completion to an asynchronous |
| 84 |
i/o-thread. Ibuf bitmap pages must always be read with |
| 85 |
synchronous i/o, to make sure they do not get involved in |
| 86 |
thread deadlocks. */ |
| 87 |
|
| 88 |
sync = true; |
| 89 |
} |
| 90 |
|
| 91 |
/* The following call will also check if the tablespace does not exist |
| 92 |
or is being dropped; if we succeed in initing the page in the buffer |
| 93 |
pool for read, then DISCARD cannot proceed until the read has |
| 94 |
completed */ |
| 95 |
bpage = buf_page_init_for_read(mode, page_id, page_size, unzip); |
| 96 |
|
| 97 |
ut_a(bpage == nullptr || bpage->get_space()->id == page_id.space()); |
| 98 |
|
| 99 |
if (bpage == nullptr) { |
| 100 |
return (0); |
| 101 |
} |
| 102 |
|
| 103 |
DBUG_PRINT("ib_buf", |
| 104 |
("read page %u:%u size=%u unzip=%u,%s", (unsigned)page_id.space(), |
| 105 |
(unsigned)page_id.page_no(), (unsigned)page_size.physical(), |
| 106 |
(unsigned)unzip, sync ? "sync" : "async")); |
| 107 |
|
| 108 |
ut_ad(buf_page_in_file(bpage)); |
| 109 |
ut_ad(!mutex_own(&buf_pool_from_bpage(bpage)->LRU_list_mutex)); |
| 110 |
|
| 111 |
if (sync) { |
| 112 |
thd_wait_begin(nullptr, THD_WAIT_DISKIO); |
| 113 |
} |
| 114 |
|
| 115 |
void *dst; |
| 116 |
|
| 117 |
if (page_size.is_compressed()) { |
| 118 |
dst = bpage->zip.data; |
| 119 |
} else { |
| 120 |
ut_a(buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE); |
| 121 |
|
| 122 |
dst = ((buf_block_t *)bpage)->frame; |
| 123 |
} |
| 124 |
|
| 125 |
IORequest request(type | IORequest::READ); |
| 126 |
|
| 127 |
*err = fil_io(request, sync, page_id, page_size, 0, page_size.physical(), dst, |
| 128 |
bpage); |
| 129 |
|
| 130 |
if (sync) { |
| 131 |
thd_wait_end(nullptr); |
| 132 |
} |
| 133 |
|
| 134 |
if (*err != DB_SUCCESS) { |
| 135 |
if (IORequest::ignore_missing(type) || *err == DB_TABLESPACE_DELETED) { |
| 136 |
buf_read_page_handle_error(bpage); |
| 137 |
return (0); |
| 138 |
} |
| 139 |
|
| 140 |
ut_error; |
| 141 |
} |
| 142 |
|
| 143 |
if (sync) { |
| 144 |
/* The i/o is already completed when we arrive from fil_read */ |
| 145 |
if (!buf_page_io_complete(bpage, false)) { |
| 146 |
return (0); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return (1); |
| 151 |
} |
| 152 |
|
| 153 |
ulint buf_read_ahead_random(const page_id_t &page_id, |
| 154 |
const page_size_t &page_size, bool inside_ibuf) { |
| 155 |
buf_pool_t *buf_pool = buf_pool_get(page_id); |
| 156 |
ulint recent_blocks = 0; |
| 157 |
ulint ibuf_mode; |
| 158 |
ulint count; |
| 159 |
page_no_t low, high; |
| 160 |
dberr_t err; |
| 161 |
page_no_t i; |
| 162 |
const page_no_t buf_read_ahead_random_area = buf_pool->read_ahead_area; |
| 163 |
|
| 164 |
if (!srv_random_read_ahead) { |
| 165 |
/* Disabled by user */ |
| 166 |
return (0); |
| 167 |
} |
| 168 |
|
| 169 |
if (srv_startup_is_before_trx_rollback_phase) { |
| 170 |
/* No read-ahead to avoid thread deadlocks */ |
| 171 |
return (0); |
| 172 |
} |
| 173 |
|
| 174 |
if (ibuf_bitmap_page(page_id, page_size) || trx_sys_hdr_page(page_id)) { |
| 175 |
/* If it is an ibuf bitmap page or trx sys hdr, we do |
| 176 |
no read-ahead, as that could break the ibuf page access |
| 177 |
order */ |
| 178 |
|
| 179 |
return (0); |
| 180 |
} |
| 181 |
|
| 182 |
low = (page_id.page_no() / buf_read_ahead_random_area) * |
| 183 |
buf_read_ahead_random_area; |
| 184 |
|
| 185 |
high = (page_id.page_no() / buf_read_ahead_random_area + 1) * |
| 186 |
buf_read_ahead_random_area; |
| 187 |
|
| 188 |
/* Remember the tablespace version before we ask the tablespace size |
| 189 |
below: if DISCARD + IMPORT changes the actual .ibd file meanwhile, we |
| 190 |
do not try to read outside the bounds of the tablespace! */ |
| 191 |
if (fil_space_t *space = fil_space_acquire_silent(page_id.space())) { |
| 192 |
if (high > space->size) { |
| 193 |
high = space->size; |
| 194 |
} |
| 195 |
fil_space_release(space); |
| 196 |
} else { |
| 197 |
return (0); |
| 198 |
} |
| 199 |
|
| 200 |
os_rmb; |
| 201 |
if (buf_pool->n_pend_reads > |
| 202 |
buf_pool->curr_size / BUF_READ_AHEAD_PEND_LIMIT) { |
| 203 |
return (0); |
| 204 |
} |
| 205 |
|
| 206 |
/* Count how many blocks in the area have been recently accessed, |
| 207 |
that is, reside near the start of the LRU list. */ |
| 208 |
|
| 209 |
for (i = low; i < high; i++) { |
| 210 |
rw_lock_t *hash_lock; |
| 211 |
const buf_page_t *bpage; |
| 212 |
|
| 213 |
bpage = buf_page_hash_get_s_locked(buf_pool, page_id_t(page_id.space(), i), |
| 214 |
&hash_lock); |
| 215 |
|
| 216 |
if (bpage != nullptr && |
| 217 |
buf_page_is_accessed(bpage) != |
| 218 |
std::chrono::steady_clock::time_point{} && |
| 219 |
buf_page_peek_if_young(bpage)) { |
| 220 |
recent_blocks++; |
| 221 |
|
| 222 |
if (recent_blocks >= BUF_READ_AHEAD_RANDOM_THRESHOLD(buf_pool)) { |
| 223 |
rw_lock_s_unlock(hash_lock); |
| 224 |
goto read_ahead; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (bpage != nullptr) { |
| 229 |
rw_lock_s_unlock(hash_lock); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/* Do nothing */ |
| 234 |
return (0); |
| 235 |
|
| 236 |
read_ahead: |
| 237 |
/* Read all the suitable blocks within the area */ |
| 238 |
|
| 239 |
if (inside_ibuf) { |
| 240 |
ibuf_mode = BUF_READ_IBUF_PAGES_ONLY; |
| 241 |
} else { |
| 242 |
ibuf_mode = BUF_READ_ANY_PAGE; |
| 243 |
} |
| 244 |
|
| 245 |
count = 0; |
| 246 |
|
| 247 |
for (i = low; i < high; i++) { |
| 248 |
/* It is only sensible to do read-ahead in the non-sync aio |
| 249 |
mode: hence false as the first parameter */ |
| 250 |
|
| 251 |
const page_id_t cur_page_id(page_id.space(), i); |
| 252 |
|
| 253 |
if (!ibuf_bitmap_page(cur_page_id, page_size)) { |
| 254 |
count += buf_read_page_low(&err, false, IORequest::DO_NOT_WAKE, ibuf_mode, |
| 255 |
cur_page_id, page_size, false); |
| 256 |
|
| 257 |
if (err == DB_TABLESPACE_DELETED) { |
| 258 |
ib::warn(ER_IB_MSG_140) << "Random readahead trying to" |
| 259 |
" access page " |
| 260 |
<< cur_page_id |
| 261 |
<< " in nonexisting or" |
| 262 |
" being-dropped tablespace"; |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/* In simulated aio we wake the aio handler threads only after |
| 269 |
queuing all aio requests. */ |
| 270 |
|
| 271 |
os_aio_simulated_wake_handler_threads(); |
| 272 |
|
| 273 |
if (count) { |
| 274 |
DBUG_PRINT("ib_buf", |
| 275 |
("random read-ahead %u pages, %u:%u", (unsigned)count, |
| 276 |
(unsigned)page_id.space(), (unsigned)page_id.page_no())); |
| 277 |
} |
| 278 |
|
| 279 |
/* Read ahead is considered one I/O operation for the purpose of |
| 280 |
LRU policy decision. */ |
| 281 |
buf_LRU_stat_inc_io(); |
| 282 |
|
| 283 |
buf_pool->stat.n_ra_pages_read_rnd += count; |
| 284 |
srv_stats.buf_pool_reads.add(count); |
| 285 |
return (count); |
| 286 |
} |
| 287 |
|
| 288 |
bool buf_read_page(const page_id_t &page_id, const page_size_t &page_size) { |
| 289 |
ulint count; |
| 290 |
dberr_t err; |
| 291 |
|
| 292 |
count = buf_read_page_low(&err, true, 0, BUF_READ_ANY_PAGE, page_id, |
| 293 |
page_size, false); |
| 294 |
|
| 295 |
srv_stats.buf_pool_reads.add(count); |
| 296 |
|
| 297 |
if (err == DB_TABLESPACE_DELETED) { |
| 298 |
ib::error(ER_IB_MSG_141) << "trying to read page " << page_id |
| 299 |
<< " in nonexisting or being-dropped tablespace"; |
| 300 |
} |
| 301 |
|
| 302 |
/* Increment number of I/O operations used for LRU policy. */ |
| 303 |
buf_LRU_stat_inc_io(); |
| 304 |
|
| 305 |
return (count > 0); |
| 306 |
} |
| 307 |
|
| 308 |
bool buf_read_page_background(const page_id_t &page_id, |
| 309 |
const page_size_t &page_size, bool sync) { |
| 310 |
ulint count; |
| 311 |
dberr_t err; |
| 312 |
|
| 313 |
count = buf_read_page_low(&err, sync, |
| 314 |
IORequest::DO_NOT_WAKE | IORequest::IGNORE_MISSING, |
| 315 |
BUF_READ_ANY_PAGE, page_id, page_size, false); |
| 316 |
|
| 317 |
srv_stats.buf_pool_reads.add(count); |
| 318 |
|
| 319 |
/* We do not increment number of I/O operations used for LRU policy |
| 320 |
here (buf_LRU_stat_inc_io()). We use this in heuristics to decide |
| 321 |
about evicting uncompressed version of compressed pages from the |
| 322 |
buffer pool. Since this function is called from buffer pool load |
| 323 |
these IOs are deliberate and are not part of normal workload we can |
| 324 |
ignore these in our heuristics. */ |
| 325 |
|
| 326 |
return (count > 0); |
| 327 |
} |
| 328 |
|
| 329 |
ulint buf_read_ahead_linear(const page_id_t &page_id, |
| 330 |
const page_size_t &page_size, bool inside_ibuf) { |
| 331 |
buf_pool_t *buf_pool = buf_pool_get(page_id); |
| 332 |
buf_page_t *bpage; |
| 333 |
buf_frame_t *frame; |
| 334 |
buf_page_t *pred_bpage = nullptr; |
| 335 |
std::chrono::steady_clock::time_point pred_bpage_is_accessed; |
| 336 |
page_no_t pred_offset; |
| 337 |
page_no_t succ_offset; |
| 338 |
int asc_or_desc; |
| 339 |
page_no_t new_offset; |
| 340 |
ulint fail_count; |
| 341 |
page_no_t low, high; |
| 342 |
dberr_t err; |
| 343 |
page_no_t i; |
| 344 |
const page_no_t buf_read_ahead_linear_area = buf_pool->read_ahead_area; |
| 345 |
page_no_t threshold; |
| 346 |
|
| 347 |
/* check if readahead is disabled */ |
| 348 |
if (!srv_read_ahead_threshold) { |
| 349 |
return (0); |
| 350 |
} |
| 351 |
|
| 352 |
if (srv_startup_is_before_trx_rollback_phase) { |
| 353 |
/* No read-ahead to avoid thread deadlocks */ |
| 354 |
return (0); |
| 355 |
} |
| 356 |
|
| 357 |
low = (page_id.page_no() / buf_read_ahead_linear_area) * |
| 358 |
buf_read_ahead_linear_area; |
| 359 |
high = (page_id.page_no() / buf_read_ahead_linear_area + 1) * |
| 360 |
buf_read_ahead_linear_area; |
| 361 |
|
| 362 |
if ((page_id.page_no() != low) && (page_id.page_no() != high - 1)) { |
| 363 |
/* This is not a border page of the area: return */ |
| 364 |
|
| 365 |
return (0); |
| 366 |
} |
| 367 |
|
| 368 |
if (ibuf_bitmap_page(page_id, page_size) || trx_sys_hdr_page(page_id)) { |
| 369 |
/* If it is an ibuf bitmap page or trx sys hdr, we do |
| 370 |
no read-ahead, as that could break the ibuf page access |
| 371 |
order */ |
| 372 |
|
| 373 |
return (0); |
| 374 |
} |
| 375 |
|
| 376 |
/* Remember the tablespace version before we ask the tablespace size |
| 377 |
below: if DISCARD + IMPORT changes the actual .ibd file meanwhile, we |
| 378 |
do not try to read outside the bounds of the tablespace! */ |
| 379 |
ulint space_size; |
| 380 |
|
| 381 |
if (fil_space_t *space = fil_space_acquire_silent(page_id.space())) { |
| 382 |
space_size = space->size; |
| 383 |
|
| 384 |
fil_space_release(space); |
| 385 |
|
| 386 |
if (high > space_size) { |
| 387 |
/* The area is not whole */ |
| 388 |
return (0); |
| 389 |
} |
| 390 |
} else { |
| 391 |
return (0); |
| 392 |
} |
| 393 |
|
| 394 |
/* Read memory barrier */ |
| 395 |
|
| 396 |
os_rmb; |
| 397 |
|
| 398 |
if (buf_pool->n_pend_reads > |
| 399 |
buf_pool->curr_size / BUF_READ_AHEAD_PEND_LIMIT) { |
| 400 |
return (0); |
| 401 |
} |
| 402 |
|
| 403 |
/* Check that almost all pages in the area have been accessed; if |
| 404 |
offset == low, the accesses must be in a descending order, otherwise, |
| 405 |
in an ascending order. */ |
| 406 |
|
| 407 |
asc_or_desc = 1; |
| 408 |
|
| 409 |
if (page_id.page_no() == low) { |
| 410 |
asc_or_desc = -1; |
| 411 |
} |
| 412 |
|
| 413 |
/* How many out of order accessed pages can we ignore |
| 414 |
when working out the access pattern for linear readahead */ |
| 415 |
threshold = std::min(static_cast<page_no_t>(64 - srv_read_ahead_threshold), |
| 416 |
buf_pool->read_ahead_area); |
| 417 |
|
| 418 |
fail_count = 0; |
| 419 |
|
| 420 |
rw_lock_t *hash_lock; |
| 421 |
|
| 422 |
for (i = low; i < high; i++) { |
| 423 |
bpage = buf_page_hash_get_s_locked(buf_pool, page_id_t(page_id.space(), i), |
| 424 |
&hash_lock); |
| 425 |
|
| 426 |
if (bpage == nullptr || buf_page_is_accessed(bpage) == |
| 427 |
std::chrono::steady_clock::time_point{}) { |
| 428 |
/* Not accessed */ |
| 429 |
fail_count++; |
| 430 |
|
| 431 |
} else if (pred_bpage) { |
| 432 |
/* Note that buf_page_is_accessed() returns |
| 433 |
the time of the first access. If some blocks |
| 434 |
of the extent existed in the buffer pool at |
| 435 |
the time of a linear access pattern, the first |
| 436 |
access times may be nonmonotonic, even though |
| 437 |
the latest access times were linear. The |
| 438 |
threshold (srv_read_ahead_factor) should help |
| 439 |
a little against this. */ |
| 440 |
int res = 0; |
| 441 |
if (buf_page_is_accessed(bpage) == pred_bpage_is_accessed) { |
| 442 |
res = 0; |
| 443 |
} else if (buf_page_is_accessed(bpage) < pred_bpage_is_accessed) { |
| 444 |
res = -1; |
| 445 |
} else { |
| 446 |
res = 1; |
| 447 |
} |
| 448 |
/* Accesses not in the right order */ |
| 449 |
if (res != 0 && res != asc_or_desc) { |
| 450 |
fail_count++; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
if (fail_count > threshold) { |
| 455 |
/* Too many failures: return */ |
| 456 |
if (bpage) { |
| 457 |
rw_lock_s_unlock(hash_lock); |
| 458 |
} |
| 459 |
return (0); |
| 460 |
} |
| 461 |
|
| 462 |
if (bpage) { |
| 463 |
if (buf_page_is_accessed(bpage) != |
| 464 |
std::chrono::steady_clock::time_point{}) { |
| 465 |
pred_bpage = bpage; |
| 466 |
pred_bpage_is_accessed = buf_page_is_accessed(bpage); |
| 467 |
} |
| 468 |
|
| 469 |
rw_lock_s_unlock(hash_lock); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
/* If we got this far, we know that enough pages in the area have |
| 474 |
been accessed in the right order: linear read-ahead can be sensible */ |
| 475 |
|
| 476 |
bpage = buf_page_hash_get_s_locked(buf_pool, page_id, &hash_lock); |
| 477 |
|
| 478 |
if (bpage == nullptr) { |
| 479 |
return (0); |
| 480 |
} |
| 481 |
|
| 482 |
switch (buf_page_get_state(bpage)) { |
| 483 |
case BUF_BLOCK_ZIP_PAGE: |
| 484 |
frame = bpage->zip.data; |
| 485 |
break; |
| 486 |
case BUF_BLOCK_FILE_PAGE: |
| 487 |
frame = ((buf_block_t *)bpage)->frame; |
| 488 |
break; |
| 489 |
default: |
| 490 |
ut_error; |
| 491 |
break; |
| 492 |
} |
| 493 |
|
| 494 |
/* Read the natural predecessor and successor page addresses from |
| 495 |
the page; NOTE that because the calling thread may have an x-latch |
| 496 |
on the page, we do not acquire an s-latch on the page, this is to |
| 497 |
prevent deadlocks. Even if we read values which are nonsense, the |
| 498 |
algorithm will work. */ |
| 499 |
|
| 500 |
pred_offset = fil_page_get_prev(frame); |
| 501 |
succ_offset = fil_page_get_next(frame); |
| 502 |
|
| 503 |
rw_lock_s_unlock(hash_lock); |
| 504 |
|
| 505 |
if ((page_id.page_no() == low) && (succ_offset == page_id.page_no() + 1)) { |
| 506 |
/* This is ok, we can continue */ |
| 507 |
new_offset = pred_offset; |
| 508 |
|
| 509 |
} else if ((page_id.page_no() == high - 1) && |
| 510 |
(pred_offset == page_id.page_no() - 1)) { |
| 511 |
/* This is ok, we can continue */ |
| 512 |
new_offset = succ_offset; |
| 513 |
} else { |
| 514 |
/* Successor or predecessor not in the right order */ |
| 515 |
|
| 516 |
return (0); |
| 517 |
} |
| 518 |
|
| 519 |
low = (new_offset / buf_read_ahead_linear_area) * buf_read_ahead_linear_area; |
| 520 |
high = (new_offset / buf_read_ahead_linear_area + 1) * |
| 521 |
buf_read_ahead_linear_area; |
| 522 |
|
| 523 |
if ((new_offset != low) && (new_offset != high - 1)) { |
| 524 |
/* This is not a border page of the area: return */ |
| 525 |
|
| 526 |
return (0); |
| 527 |
} |
| 528 |
|
| 529 |
if (high > space_size) { |
| 530 |
/* The area is not whole, return */ |
| 531 |
|
| 532 |
return (0); |
| 533 |
} |
| 534 |
|
| 535 |
ulint count = 0; |
| 536 |
|
| 537 |
/* If we got this far, read-ahead can be sensible: do it */ |
| 538 |
|
| 539 |
ulint ibuf_mode; |
| 540 |
|
| 541 |
ibuf_mode = inside_ibuf ? BUF_READ_IBUF_PAGES_ONLY : BUF_READ_ANY_PAGE; |
| 542 |
|
| 543 |
/* Since Windows XP seems to schedule the i/o handler thread |
| 544 |
very eagerly, and consequently it does not wait for the |
| 545 |
full read batch to be posted, we use special heuristics here */ |
| 546 |
|
| 547 |
os_aio_simulated_put_read_threads_to_sleep(); |
| 548 |
|
| 549 |
for (i = low; i < high; i++) { |
| 550 |
/* It is only sensible to do read-ahead in the non-sync |
| 551 |
aio mode: hence false as the first parameter */ |
| 552 |
|
| 553 |
const page_id_t cur_page_id(page_id.space(), i); |
| 554 |
|
| 555 |
if (!ibuf_bitmap_page(cur_page_id, page_size)) { |
| 556 |
count += buf_read_page_low(&err, false, IORequest::DO_NOT_WAKE, ibuf_mode, |
| 557 |
cur_page_id, page_size, false); |
| 558 |
|
| 559 |
if (err == DB_TABLESPACE_DELETED) { |
| 560 |
ib::warn(ER_IB_MSG_142) << "linear readahead trying to" |
| 561 |
" access page " |
| 562 |
<< page_id_t(page_id.space(), i) |
| 563 |
<< " in nonexisting or being-dropped" |
| 564 |
" tablespace"; |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
/* In simulated aio we wake the aio handler threads only after |
| 570 |
queuing all aio requests. */ |
| 571 |
|
| 572 |
os_aio_simulated_wake_handler_threads(); |
| 573 |
|
| 574 |
if (count) { |
| 575 |
DBUG_PRINT("ib_buf", ("linear read-ahead %lu pages, " UINT32PF ":" UINT32PF, |
| 576 |
count, page_id.space(), page_id.page_no())); |
| 577 |
} |
| 578 |
|
| 579 |
/* Read ahead is considered one I/O operation for the purpose of |
| 580 |
LRU policy decision. */ |
| 581 |
buf_LRU_stat_inc_io(); |
| 582 |
|
| 583 |
buf_pool->stat.n_ra_pages_read += count; |
| 584 |
return (count); |
| 585 |
} |
| 586 |
|
| 587 |
void buf_read_ibuf_merge_pages(bool sync, const space_id_t *space_ids, |
| 588 |
const page_no_t *page_nos, ulint n_stored) { |
| 589 |
#ifdef UNIV_IBUF_DEBUG |
| 590 |
ut_a(n_stored < UNIV_PAGE_SIZE); |
| 591 |
#endif /* UNIV_IBUF_DBUG */ |
| 592 |
|
| 593 |
ut::unordered_map<space_id_t, fil_space_t *> acquired_spaces; |
| 594 |
|
| 595 |
for (ulint i = 0; i < n_stored; i++) { |
| 596 |
const page_id_t page_id(space_ids[i], page_nos[i]); |
| 597 |
|
| 598 |
buf_pool_t *buf_pool = buf_pool_get(page_id); |
| 599 |
|
| 600 |
fil_space_t *space = nullptr; |
| 601 |
/* Acquire the space once for the pages belongs to it */ |
| 602 |
const auto space_itr = acquired_spaces.find(space_ids[i]); |
| 603 |
if (space_itr != acquired_spaces.end()) { |
| 604 |
space = space_itr->second; |
| 605 |
} else { |
| 606 |
/* If the space is deleted then fil_space_acquire_silent() returns |
| 607 |
nullptr. Cache that information as well so that we remove the subsequent |
| 608 |
ibuf entries for that space without trying to acquire it again. It is safe |
| 609 |
operation to do since the space once deleted will not be available ever.*/ |
| 610 |
space = fil_space_acquire_silent(space_ids[i]); |
| 611 |
acquired_spaces.emplace(space_ids[i], space); |
| 612 |
} |
| 613 |
|
| 614 |
if (space == nullptr) { |
| 615 |
/* The tablespace was not found, remove the entries for that page */ |
| 616 |
ibuf_merge_or_delete_for_page(nullptr, page_id, nullptr, false); |
| 617 |
continue; |
| 618 |
} |
| 619 |
|
| 620 |
const page_size_t page_size(space->flags); |
| 621 |
|
| 622 |
os_rmb; |
| 623 |
while (buf_pool->n_pend_reads > |
| 624 |
buf_pool->curr_size / BUF_READ_AHEAD_PEND_LIMIT) { |
| 625 |
std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 626 |
} |
| 627 |
|
| 628 |
dberr_t err; |
| 629 |
|
| 630 |
buf_read_page_low(&err, sync && (i + 1 == n_stored), |
| 631 |
IORequest::IGNORE_MISSING, BUF_READ_ANY_PAGE, page_id, |
| 632 |
page_size, true); |
| 633 |
|
| 634 |
if (err == DB_TABLESPACE_DELETED) { |
| 635 |
/* We have deleted or are deleting the single-table |
| 636 |
tablespace: remove the entries for that page */ |
| 637 |
ibuf_merge_or_delete_for_page(nullptr, page_id, &page_size, false); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
/* Release the acquired spaces */ |
| 642 |
for (const auto space_entry : acquired_spaces) { |
| 643 |
if (space_entry.second) { |
| 644 |
fil_space_release(space_entry.second); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
os_aio_simulated_wake_handler_threads(); |
| 649 |
|
| 650 |
if (n_stored) { |
| 651 |
DBUG_PRINT("ib_buf", ("ibuf merge read-ahead %u pages, space %u", |
| 652 |
unsigned(n_stored), unsigned(space_ids[0]))); |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
void buf_read_recv_pages(bool sync, space_id_t space_id, |
| 657 |
const page_no_t *page_nos, ulint n_stored) { |
| 658 |
ulint count; |
| 659 |
fil_space_t *space = fil_space_get(space_id); |
| 660 |
|
| 661 |
if (space == nullptr) { |
| 662 |
/* The tablespace is missing: do nothing */ |
| 663 |
return; |
| 664 |
} |
| 665 |
|
| 666 |
fil_space_open_if_needed(space); |
| 667 |
|
| 668 |
auto req_size = page_nos[n_stored - 1] + 1; |
| 669 |
|
| 670 |
/* Extend the tablespace if needed. Required only while |
| 671 |
recovering from cloned database. */ |
| 672 |
if (recv_sys->is_cloned_db && space->size < req_size) { |
| 673 |
/* Align size to multiple of extent size */ |
| 674 |
if (req_size > FSP_EXTENT_SIZE) { |
| 675 |
req_size = ut_calc_align(req_size, FSP_EXTENT_SIZE); |
| 676 |
} |
| 677 |
|
| 678 |
ib::info(ER_IB_MSG_143) << "Extending tablespace : " << space->id |
| 679 |
<< " space name: " << space->name |
| 680 |
<< " from page number: " << space->size << " pages" |
| 681 |
<< " to " << req_size << " pages" |
| 682 |
<< " for page number: " << page_nos[n_stored - 1] |
| 683 |
<< " during recovery."; |
| 684 |
|
| 685 |
if (!fil_space_extend(space, req_size)) { |
| 686 |
ib::error(ER_IB_MSG_144) |
| 687 |
<< "Could not extend tablespace: " << space->id |
| 688 |
<< " space name: " << space->name << " to " << req_size << " pages" |
| 689 |
<< " during recovery."; |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
const page_size_t page_size(space->flags); |
| 694 |
|
| 695 |
for (ulint i = 0; i < n_stored; i++) { |
| 696 |
buf_pool_t *buf_pool; |
| 697 |
const page_id_t cur_page_id(space_id, page_nos[i]); |
| 698 |
|
| 699 |
count = 0; |
| 700 |
|
| 701 |
buf_pool = buf_pool_get(cur_page_id); |
| 702 |
os_rmb; |
| 703 |
|
| 704 |
while (buf_pool->n_pend_reads >= recv_n_pool_free_frames / 2) { |
| 705 |
os_aio_simulated_wake_handler_threads(); |
| 706 |
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 707 |
|
| 708 |
count++; |
| 709 |
|
| 710 |
if (!(count % 1000)) { |
| 711 |
ib::error(ER_IB_MSG_145) |
| 712 |
<< "Waited for " << count / 100 << " seconds for " |
| 713 |
<< buf_pool->n_pend_reads << " pending reads"; |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
dberr_t err; |
| 718 |
|
| 719 |
if ((i + 1 == n_stored) && sync) { |
| 720 |
buf_read_page_low(&err, true, 0, BUF_READ_ANY_PAGE, cur_page_id, |
| 721 |
page_size, true); |
| 722 |
} else { |
| 723 |
buf_read_page_low(&err, false, IORequest::DO_NOT_WAKE, BUF_READ_ANY_PAGE, |
| 724 |
cur_page_id, page_size, true); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
os_aio_simulated_wake_handler_threads(); |
| 729 |
|
| 730 |
DBUG_PRINT("ib_buf", ("recovery read-ahead (%u pages)", unsigned(n_stored))); |
| 731 |
} |