| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2011, 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/buf0dump.cc |
| 29 |
Implements a buffer pool dump/load. |
| 30 |
|
| 31 |
Created April 08, 2011 Vasil Dimov |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include <errno.h> |
| 35 |
#include <stdarg.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <algorithm> |
| 38 |
|
| 39 |
#include "buf0buf.h" |
| 40 |
#include "buf0dump.h" |
| 41 |
#include "dict0dict.h" |
| 42 |
|
| 43 |
#include "my_io.h" |
| 44 |
#include "my_psi_config.h" |
| 45 |
#include "my_sys.h" |
| 46 |
#include "my_thread.h" |
| 47 |
#include "mysql/psi/mysql_stage.h" |
| 48 |
#include "os0file.h" |
| 49 |
#include "os0thread-create.h" |
| 50 |
#include "os0thread.h" |
| 51 |
#include "srv0srv.h" |
| 52 |
#include "srv0start.h" |
| 53 |
#include "sync0rw.h" |
| 54 |
#include "univ.i" |
| 55 |
#include "ut0byte.h" |
| 56 |
|
| 57 |
enum status_severity { STATUS_VERBOSE, STATUS_INFO, STATUS_ERR }; |
| 58 |
|
| 59 |
static inline bool SHUTTING_DOWN() { |
| 60 |
return srv_shutdown_state.load() >= SRV_SHUTDOWN_CLEANUP; |
| 61 |
} |
| 62 |
|
| 63 |
/* Flags that tell the buffer pool dump/load thread which action should it |
| 64 |
take after being waked up. */ |
| 65 |
static bool buf_dump_should_start = false; |
| 66 |
static bool buf_load_should_start = false; |
| 67 |
|
| 68 |
static bool buf_load_abort_flag = false; |
| 69 |
|
| 70 |
/* Used to temporary store dump info in order to avoid IO while holding |
| 71 |
buffer pool LRU list mutex during dump and also to sort the contents of the |
| 72 |
dump before reading the pages from disk during load. |
| 73 |
We store the space id in the high 32 bits and page no in low 32 bits. */ |
| 74 |
typedef uint64_t buf_dump_t; |
| 75 |
|
| 76 |
/* Aux macros to create buf_dump_t and to extract space and page from it */ |
| 77 |
inline uint64_t BUF_DUMP_CREATE(space_id_t space, page_no_t page) { |
| 78 |
return ut_ull_create(space, page); |
| 79 |
} |
| 80 |
constexpr space_id_t BUF_DUMP_SPACE(uint64_t a) { |
| 81 |
return static_cast<space_id_t>((a) >> 32); |
| 82 |
} |
| 83 |
constexpr page_no_t BUF_DUMP_PAGE(uint64_t a) { |
| 84 |
return static_cast<page_no_t>((a)&0xFFFFFFFFUL); |
| 85 |
} |
| 86 |
/** Wakes up the buffer pool dump/load thread and instructs it to start |
| 87 |
a dump. This function is called by MySQL code via buffer_pool_dump_now() |
| 88 |
and it should return immediately because the whole MySQL is frozen during |
| 89 |
its execution. */ |
| 90 |
void buf_dump_start() { |
| 91 |
buf_dump_should_start = true; |
| 92 |
os_event_set(srv_buf_dump_event); |
| 93 |
} |
| 94 |
|
| 95 |
/** Wakes up the buffer pool dump/load thread and instructs it to start |
| 96 |
a load. This function is called by MySQL code via buffer_pool_load_now() |
| 97 |
and it should return immediately because the whole MySQL is frozen during |
| 98 |
its execution. */ |
| 99 |
void buf_load_start() { |
| 100 |
buf_load_should_start = true; |
| 101 |
os_event_set(srv_buf_dump_event); |
| 102 |
} |
| 103 |
|
| 104 |
/** Sets the global variable that feeds MySQL's innodb_buffer_pool_dump_status |
| 105 |
to the specified string. The format and the following parameters are the |
| 106 |
same as the ones used for printf(3). The value of this variable can be |
| 107 |
retrieved by: |
| 108 |
SELECT variable_value FROM performance_schema.global_status WHERE |
| 109 |
variable_name = 'INNODB_BUFFER_POOL_DUMP_STATUS'; |
| 110 |
or by: |
| 111 |
SHOW STATUS LIKE 'innodb_buffer_pool_dump_status'; */ |
| 112 |
static MY_ATTRIBUTE((format(printf, 2, 3))) void buf_dump_status( |
| 113 |
enum status_severity severity, /*!< in: status severity */ |
| 114 |
const char *fmt, /*!< in: format */ |
| 115 |
...) /*!< in: extra parameters according |
| 116 |
to fmt */ |
| 117 |
{ |
| 118 |
va_list ap; |
| 119 |
|
| 120 |
va_start(ap, fmt); |
| 121 |
|
| 122 |
ut_vsnprintf(export_vars.innodb_buffer_pool_dump_status, |
| 123 |
sizeof(export_vars.innodb_buffer_pool_dump_status), fmt, ap); |
| 124 |
|
| 125 |
switch (severity) { |
| 126 |
case STATUS_INFO: |
| 127 |
ib::info(ER_IB_MSG_119) << export_vars.innodb_buffer_pool_dump_status; |
| 128 |
break; |
| 129 |
|
| 130 |
case STATUS_ERR: |
| 131 |
ib::error(ER_IB_MSG_120) << export_vars.innodb_buffer_pool_dump_status; |
| 132 |
break; |
| 133 |
|
| 134 |
case STATUS_VERBOSE: |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
va_end(ap); |
| 139 |
} |
| 140 |
|
| 141 |
/** Sets the global variable that feeds MySQL's innodb_buffer_pool_load_status |
| 142 |
to the specified string. The format and the following parameters are the |
| 143 |
same as the ones used for printf(3). The value of this variable can be |
| 144 |
retrieved by: |
| 145 |
SELECT variable_value FROM performance_schema.global_status WHERE |
| 146 |
variable_name = 'INNODB_BUFFER_POOL_LOAD_STATUS'; |
| 147 |
or by: |
| 148 |
SHOW STATUS LIKE 'innodb_buffer_pool_load_status'; */ |
| 149 |
static MY_ATTRIBUTE((format(printf, 2, 3))) void buf_load_status( |
| 150 |
enum status_severity severity, /*!< in: status severity */ |
| 151 |
const char *fmt, /*!< in: format */ |
| 152 |
...) /*!< in: extra parameters according to fmt */ |
| 153 |
{ |
| 154 |
va_list ap; |
| 155 |
|
| 156 |
va_start(ap, fmt); |
| 157 |
|
| 158 |
ut_vsnprintf(export_vars.innodb_buffer_pool_load_status, |
| 159 |
sizeof(export_vars.innodb_buffer_pool_load_status), fmt, ap); |
| 160 |
|
| 161 |
switch (severity) { |
| 162 |
case STATUS_INFO: |
| 163 |
ib::info(ER_IB_MSG_121) << export_vars.innodb_buffer_pool_load_status; |
| 164 |
break; |
| 165 |
|
| 166 |
case STATUS_ERR: |
| 167 |
ib::error(ER_IB_MSG_122) << export_vars.innodb_buffer_pool_load_status; |
| 168 |
break; |
| 169 |
|
| 170 |
case STATUS_VERBOSE: |
| 171 |
break; |
| 172 |
} |
| 173 |
|
| 174 |
va_end(ap); |
| 175 |
} |
| 176 |
|
| 177 |
/** Returns the directory path where the buffer pool dump file will be created. |
| 178 |
@return directory path */ |
| 179 |
static const char *get_buf_dump_dir() { |
| 180 |
const char *dump_dir; |
| 181 |
|
| 182 |
/* The dump file should be created in the default data directory if |
| 183 |
innodb_data_home_dir is set as an empty string. */ |
| 184 |
if (strcmp(srv_data_home, "") == 0) { |
| 185 |
dump_dir = MySQL_datadir_path; |
| 186 |
} else { |
| 187 |
dump_dir = srv_data_home; |
| 188 |
} |
| 189 |
|
| 190 |
return (dump_dir); |
| 191 |
} |
| 192 |
|
| 193 |
/** Generate the path to the buffer pool dump/load file. |
| 194 |
@param[out] path generated path |
| 195 |
@param[in] path_size size of 'path', used as in snprintf(3). */ |
| 196 |
void buf_dump_generate_path(char *path, size_t path_size) { |
| 197 |
char buf[FN_REFLEN]; |
| 198 |
|
| 199 |
snprintf(buf, sizeof(buf), "%s%c%s", get_buf_dump_dir(), OS_PATH_SEPARATOR, |
| 200 |
srv_buf_dump_filename); |
| 201 |
|
| 202 |
/* Use this file if it exists. */ |
| 203 |
if (os_file_exists(buf)) { |
| 204 |
/* my_realpath() assumes the destination buffer is big enough |
| 205 |
to hold FN_REFLEN bytes. */ |
| 206 |
ut_a(path_size >= FN_REFLEN); |
| 207 |
|
| 208 |
my_realpath(path, buf, 0); |
| 209 |
} else { |
| 210 |
/* If it does not exist, then resolve only srv_data_home |
| 211 |
and append srv_buf_dump_filename to it. */ |
| 212 |
char srv_data_home_full[FN_REFLEN]; |
| 213 |
|
| 214 |
my_realpath(srv_data_home_full, get_buf_dump_dir(), 0); |
| 215 |
|
| 216 |
if (srv_data_home_full[strlen(srv_data_home_full) - 1] == |
| 217 |
OS_PATH_SEPARATOR) { |
| 218 |
snprintf(path, path_size, "%s%s", srv_data_home_full, |
| 219 |
srv_buf_dump_filename); |
| 220 |
} else { |
| 221 |
snprintf(path, path_size, "%s%c%s", srv_data_home_full, OS_PATH_SEPARATOR, |
| 222 |
srv_buf_dump_filename); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** Perform a buffer pool dump into the file specified by |
| 228 |
innodb_buffer_pool_filename. If any errors occur then the value of |
| 229 |
innodb_buffer_pool_dump_status will be set accordingly, see buf_dump_status(). |
| 230 |
The dump filename can be specified by (relative to srv_data_home): |
| 231 |
SET GLOBAL innodb_buffer_pool_filename='filename'; |
| 232 |
@param[in] obey_shutdown quit if we are in a shutting down state */ |
| 233 |
static void buf_dump(bool obey_shutdown) { |
| 234 |
#define SHOULD_QUIT() (SHUTTING_DOWN() && obey_shutdown) |
| 235 |
|
| 236 |
char full_filename[OS_FILE_MAX_PATH]; |
| 237 |
char tmp_filename[OS_FILE_MAX_PATH + 11]; |
| 238 |
char now[32]; |
| 239 |
FILE *f; |
| 240 |
ulint i; |
| 241 |
int ret; |
| 242 |
|
| 243 |
buf_dump_generate_path(full_filename, sizeof(full_filename)); |
| 244 |
|
| 245 |
snprintf(tmp_filename, sizeof(tmp_filename), "%s.incomplete", full_filename); |
| 246 |
|
| 247 |
buf_dump_status(STATUS_INFO, "Dumping buffer pool(s) to %s", full_filename); |
| 248 |
|
| 249 |
f = fopen(tmp_filename, "w"); |
| 250 |
if (f == nullptr) { |
| 251 |
buf_dump_status(STATUS_ERR, "Cannot open '%s' for writing: %s", |
| 252 |
tmp_filename, strerror(errno)); |
| 253 |
return; |
| 254 |
} |
| 255 |
/* else */ |
| 256 |
|
| 257 |
/* walk through each buffer pool */ |
| 258 |
for (i = 0; i < srv_buf_pool_instances && !SHOULD_QUIT(); i++) { |
| 259 |
buf_pool_t *buf_pool; |
| 260 |
buf_dump_t *dump; |
| 261 |
|
| 262 |
buf_pool = buf_pool_from_array(i); |
| 263 |
|
| 264 |
/* obtain buf_pool LRU list mutex before allocate, since |
| 265 |
UT_LIST_GET_LEN(buf_pool->LRU) could change */ |
| 266 |
mutex_enter(&buf_pool->LRU_list_mutex); |
| 267 |
|
| 268 |
size_t n_pages = UT_LIST_GET_LEN(buf_pool->LRU); |
| 269 |
|
| 270 |
/* skip empty buffer pools */ |
| 271 |
if (n_pages == 0) { |
| 272 |
mutex_exit(&buf_pool->LRU_list_mutex); |
| 273 |
continue; |
| 274 |
} |
| 275 |
|
| 276 |
if (srv_buf_pool_dump_pct != 100) { |
| 277 |
ut_ad(srv_buf_pool_dump_pct < 100); |
| 278 |
|
| 279 |
n_pages = n_pages * srv_buf_pool_dump_pct / 100; |
| 280 |
|
| 281 |
if (n_pages == 0) { |
| 282 |
n_pages = 1; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
dump = static_cast<buf_dump_t *>( |
| 287 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, n_pages * sizeof(*dump))); |
| 288 |
|
| 289 |
if (dump == nullptr) { |
| 290 |
mutex_exit(&buf_pool->LRU_list_mutex); |
| 291 |
fclose(f); |
| 292 |
buf_dump_status(STATUS_ERR, "Cannot allocate %zu bytes: %s", |
| 293 |
n_pages * sizeof(*dump), strerror(errno)); |
| 294 |
/* leave tmp_filename to exist */ |
| 295 |
return; |
| 296 |
} |
| 297 |
{ |
| 298 |
size_t j{0}; |
| 299 |
for (auto bpage : buf_pool->LRU) { |
| 300 |
if (n_pages <= j) break; |
| 301 |
ut_a(buf_page_in_file(bpage)); |
| 302 |
|
| 303 |
dump[j++] = BUF_DUMP_CREATE(bpage->id.space(), bpage->id.page_no()); |
| 304 |
} |
| 305 |
|
| 306 |
ut_a(j == n_pages); |
| 307 |
} |
| 308 |
|
| 309 |
mutex_exit(&buf_pool->LRU_list_mutex); |
| 310 |
|
| 311 |
for (size_t j = 0; j < n_pages && !SHOULD_QUIT(); j++) { |
| 312 |
ret = fprintf(f, SPACE_ID_PF "," PAGE_NO_PF "\n", BUF_DUMP_SPACE(dump[j]), |
| 313 |
BUF_DUMP_PAGE(dump[j])); |
| 314 |
if (ret < 0) { |
| 315 |
ut::free(dump); |
| 316 |
fclose(f); |
| 317 |
buf_dump_status(STATUS_ERR, "Cannot write to '%s': %s", tmp_filename, |
| 318 |
strerror(errno)); |
| 319 |
/* leave tmp_filename to exist */ |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
if (j % 128 == 0) { |
| 324 |
buf_dump_status( |
| 325 |
STATUS_VERBOSE, |
| 326 |
"Dumping buffer pool " ULINTPF "/" ULINTPF ", page %zu/%zu", i + 1, |
| 327 |
static_cast<ulint>(srv_buf_pool_instances), j + 1, n_pages); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
ut::free(dump); |
| 332 |
} |
| 333 |
|
| 334 |
ret = fclose(f); |
| 335 |
if (ret != 0) { |
| 336 |
buf_dump_status(STATUS_ERR, "Cannot close '%s': %s", tmp_filename, |
| 337 |
strerror(errno)); |
| 338 |
return; |
| 339 |
} |
| 340 |
/* else */ |
| 341 |
|
| 342 |
ret = unlink(full_filename); |
| 343 |
if (ret != 0 && errno != ENOENT) { |
| 344 |
buf_dump_status(STATUS_ERR, "Cannot delete '%s': %s", full_filename, |
| 345 |
strerror(errno)); |
| 346 |
/* leave tmp_filename to exist */ |
| 347 |
return; |
| 348 |
} |
| 349 |
/* else */ |
| 350 |
|
| 351 |
ret = rename(tmp_filename, full_filename); |
| 352 |
if (ret != 0) { |
| 353 |
buf_dump_status(STATUS_ERR, "Cannot rename '%s' to '%s': %s", tmp_filename, |
| 354 |
full_filename, strerror(errno)); |
| 355 |
/* leave tmp_filename to exist */ |
| 356 |
return; |
| 357 |
} |
| 358 |
/* else */ |
| 359 |
|
| 360 |
/* success */ |
| 361 |
|
| 362 |
ut_sprintf_timestamp(now); |
| 363 |
|
| 364 |
buf_dump_status(STATUS_INFO, "Buffer pool(s) dump completed at %s", now); |
| 365 |
} |
| 366 |
|
| 367 |
/** Artificially delay the buffer pool loading if necessary. The idea of this |
| 368 |
function is to prevent hogging the server with IO and slowing down too much |
| 369 |
normal client queries. |
| 370 |
@param[in,out] last_check_time milliseconds since epoch of the last |
| 371 |
time we did check if throttling is |
| 372 |
needed, we do the check every |
| 373 |
srv_io_capacity IO ops. |
| 374 |
@param[in] last_activity_count activity count |
| 375 |
@param[in] n_io number of IO ops done since buffer |
| 376 |
pool load has started */ |
| 377 |
static inline void buf_load_throttle_if_needed( |
| 378 |
std::chrono::steady_clock::time_point *last_check_time, |
| 379 |
ulint *last_activity_count, ulint n_io) { |
| 380 |
if (n_io % srv_io_capacity < srv_io_capacity - 1) { |
| 381 |
return; |
| 382 |
} |
| 383 |
|
| 384 |
if (*last_check_time == std::chrono::steady_clock::time_point{} || |
| 385 |
*last_activity_count == 0) { |
| 386 |
*last_check_time = std::chrono::steady_clock::now(); |
| 387 |
*last_activity_count = srv_get_activity_count(); |
| 388 |
return; |
| 389 |
} |
| 390 |
|
| 391 |
/* srv_io_capacity IO operations have been performed by buffer pool |
| 392 |
load since the last time we were here. */ |
| 393 |
|
| 394 |
/* If no other activity, then keep going without any delay. */ |
| 395 |
if (srv_get_activity_count() == *last_activity_count) { |
| 396 |
return; |
| 397 |
} |
| 398 |
|
| 399 |
/* There has been other activity, throttle. */ |
| 400 |
|
| 401 |
const auto elapsed_time = std::chrono::steady_clock::now() - *last_check_time; |
| 402 |
|
| 403 |
/* Notice that elapsed_time is not the time for the last |
| 404 |
srv_io_capacity IO operations performed by BP load. It is the |
| 405 |
time elapsed since the last time we detected that there has been |
| 406 |
other activity. This has a small and acceptable deficiency, e.g.: |
| 407 |
1. BP load runs and there is no other activity. |
| 408 |
2. Other activity occurs, we run N IO operations after that and |
| 409 |
enter here (where 0 <= N < srv_io_capacity). |
| 410 |
3. last_check_time is very old and we do not sleep at this time, but |
| 411 |
only update last_check_time and last_activity_count. |
| 412 |
4. We run srv_io_capacity more IO operations and call this function |
| 413 |
again. |
| 414 |
5. There has been more other activity and thus we enter here. |
| 415 |
6. Now last_check_time is recent and we sleep if necessary to prevent |
| 416 |
more than srv_io_capacity IO operations per second. |
| 417 |
The deficiency is that we could have slept at 3., but for this we |
| 418 |
would have to update last_check_time before the |
| 419 |
"cur_activity_count == *last_activity_count" check and calling |
| 420 |
ut_time_monotonic_ms() that often may turn out to be too expensive. */ |
| 421 |
|
| 422 |
if (elapsed_time < std::chrono::seconds{1}) { |
| 423 |
std::this_thread::sleep_for(std::chrono::seconds{1} - elapsed_time); |
| 424 |
} |
| 425 |
|
| 426 |
*last_check_time = std::chrono::steady_clock::now(); |
| 427 |
*last_activity_count = srv_get_activity_count(); |
| 428 |
} |
| 429 |
|
| 430 |
/** Perform a buffer pool load from the file specified by |
| 431 |
innodb_buffer_pool_filename. If any errors occur then the value of |
| 432 |
innodb_buffer_pool_load_status will be set accordingly, see buf_load_status(). |
| 433 |
The dump filename can be specified by (relative to srv_data_home): |
| 434 |
SET GLOBAL innodb_buffer_pool_filename='filename'; */ |
| 435 |
static void buf_load() { |
| 436 |
char full_filename[OS_FILE_MAX_PATH]; |
| 437 |
char now[32]; |
| 438 |
FILE *f; |
| 439 |
buf_dump_t *dump; |
| 440 |
ulint dump_n; |
| 441 |
ulint total_buffer_pools_pages; |
| 442 |
ulint i; |
| 443 |
ulint space_id; |
| 444 |
ulint page_no; |
| 445 |
int fscanf_ret; |
| 446 |
|
| 447 |
/* Ignore any leftovers from before */ |
| 448 |
buf_load_abort_flag = false; |
| 449 |
|
| 450 |
buf_dump_generate_path(full_filename, sizeof(full_filename)); |
| 451 |
|
| 452 |
buf_load_status(STATUS_INFO, "Loading buffer pool(s) from %s", full_filename); |
| 453 |
|
| 454 |
f = fopen(full_filename, "r"); |
| 455 |
if (f == nullptr) { |
| 456 |
buf_load_status(STATUS_ERR, "Cannot open '%s' for reading: %s", |
| 457 |
full_filename, strerror(errno)); |
| 458 |
return; |
| 459 |
} |
| 460 |
/* else */ |
| 461 |
|
| 462 |
/* First scan the file to estimate how many entries are in it. |
| 463 |
This file is tiny (approx 500KB per 1GB buffer pool), reading it |
| 464 |
two times is fine. */ |
| 465 |
dump_n = 0; |
| 466 |
while (fscanf(f, ULINTPF "," ULINTPF, &space_id, &page_no) == 2 && |
| 467 |
!SHUTTING_DOWN()) { |
| 468 |
dump_n++; |
| 469 |
} |
| 470 |
|
| 471 |
if (!SHUTTING_DOWN() && !feof(f)) { |
| 472 |
/* fscanf() returned != 2 */ |
| 473 |
const char *what; |
| 474 |
if (ferror(f)) { |
| 475 |
what = "reading"; |
| 476 |
} else { |
| 477 |
what = "parsing"; |
| 478 |
} |
| 479 |
fclose(f); |
| 480 |
buf_load_status(STATUS_ERR, |
| 481 |
"Error %s '%s'," |
| 482 |
" unable to load buffer pool (stage 1)", |
| 483 |
what, full_filename); |
| 484 |
return; |
| 485 |
} |
| 486 |
|
| 487 |
/* If dump is larger than the buffer pool(s), then we ignore the |
| 488 |
extra trailing. This could happen if a dump is made, then buffer |
| 489 |
pool is shrunk and then load is attempted. */ |
| 490 |
total_buffer_pools_pages = buf_pool_get_n_pages() * srv_buf_pool_instances; |
| 491 |
if (dump_n > total_buffer_pools_pages) { |
| 492 |
dump_n = total_buffer_pools_pages; |
| 493 |
} |
| 494 |
|
| 495 |
if (dump_n != 0) { |
| 496 |
dump = static_cast<buf_dump_t *>( |
| 497 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, dump_n * sizeof(*dump))); |
| 498 |
} else { |
| 499 |
fclose(f); |
| 500 |
ut_sprintf_timestamp(now); |
| 501 |
buf_load_status(STATUS_INFO, |
| 502 |
"Buffer pool(s) load completed at %s" |
| 503 |
" (%s was empty)", |
| 504 |
now, full_filename); |
| 505 |
return; |
| 506 |
} |
| 507 |
|
| 508 |
if (dump == nullptr) { |
| 509 |
fclose(f); |
| 510 |
buf_load_status(STATUS_ERR, "Cannot allocate " ULINTPF " bytes: %s", |
| 511 |
(ulint)(dump_n * sizeof(*dump)), strerror(errno)); |
| 512 |
return; |
| 513 |
} |
| 514 |
|
| 515 |
rewind(f); |
| 516 |
|
| 517 |
for (i = 0; i < dump_n && !SHUTTING_DOWN(); i++) { |
| 518 |
fscanf_ret = fscanf(f, ULINTPF "," ULINTPF, &space_id, &page_no); |
| 519 |
|
| 520 |
if (fscanf_ret != 2) { |
| 521 |
if (feof(f)) { |
| 522 |
break; |
| 523 |
} |
| 524 |
/* else */ |
| 525 |
|
| 526 |
ut::free(dump); |
| 527 |
fclose(f); |
| 528 |
buf_load_status(STATUS_ERR, |
| 529 |
"Error parsing '%s', unable" |
| 530 |
" to load buffer pool (stage 2)", |
| 531 |
full_filename); |
| 532 |
return; |
| 533 |
} |
| 534 |
|
| 535 |
if (space_id > UINT32_MASK || page_no > UINT32_MASK) { |
| 536 |
ut::free(dump); |
| 537 |
fclose(f); |
| 538 |
buf_load_status(STATUS_ERR, |
| 539 |
"Error parsing '%s': bogus" |
| 540 |
" space,page " ULINTPF "," ULINTPF " at line " ULINTPF |
| 541 |
"," |
| 542 |
" unable to load buffer pool", |
| 543 |
full_filename, space_id, page_no, i); |
| 544 |
return; |
| 545 |
} |
| 546 |
|
| 547 |
dump[i] = BUF_DUMP_CREATE(space_id, page_no); |
| 548 |
} |
| 549 |
|
| 550 |
/* Set dump_n to the actual number of initialized elements, |
| 551 |
i could be smaller than dump_n here if the file got truncated after |
| 552 |
we read it the first time. */ |
| 553 |
dump_n = i; |
| 554 |
|
| 555 |
fclose(f); |
| 556 |
|
| 557 |
if (dump_n == 0) { |
| 558 |
ut::free(dump); |
| 559 |
ut_sprintf_timestamp(now); |
| 560 |
buf_load_status(STATUS_INFO, |
| 561 |
"Buffer pool(s) load completed at %s" |
| 562 |
" (%s was empty)", |
| 563 |
now, full_filename); |
| 564 |
return; |
| 565 |
} |
| 566 |
|
| 567 |
if (!SHUTTING_DOWN()) { |
| 568 |
std::sort(dump, dump + dump_n); |
| 569 |
} |
| 570 |
|
| 571 |
std::chrono::steady_clock::time_point last_check_time; |
| 572 |
ulint last_activity_cnt = 0; |
| 573 |
|
| 574 |
/* Avoid calling the expensive fil_space_acquire_silent() for each |
| 575 |
page within the same tablespace. dump[] is sorted by (space, page), |
| 576 |
so all pages from a given tablespace are consecutive. */ |
| 577 |
space_id_t cur_space_id = BUF_DUMP_SPACE(dump[0]); |
| 578 |
fil_space_t *space = fil_space_acquire_silent(cur_space_id); |
| 579 |
page_size_t page_size(space ? space->flags : 0); |
| 580 |
|
| 581 |
#ifdef HAVE_PSI_STAGE_INTERFACE |
| 582 |
PSI_stage_progress *pfs_stage_progress = |
| 583 |
mysql_set_stage(srv_stage_buffer_pool_load.m_key); |
| 584 |
#endif /* HAVE_PSI_STAGE_INTERFACE */ |
| 585 |
|
| 586 |
mysql_stage_set_work_estimated(pfs_stage_progress, dump_n); |
| 587 |
mysql_stage_set_work_completed(pfs_stage_progress, 0); |
| 588 |
|
| 589 |
for (i = 0; i < dump_n && !SHUTTING_DOWN(); i++) { |
| 590 |
/* space_id for this iteration of the loop */ |
| 591 |
const space_id_t this_space_id = BUF_DUMP_SPACE(dump[i]); |
| 592 |
|
| 593 |
if (this_space_id != cur_space_id) { |
| 594 |
if (space != nullptr) { |
| 595 |
fil_space_release(space); |
| 596 |
} |
| 597 |
|
| 598 |
cur_space_id = this_space_id; |
| 599 |
space = fil_space_acquire_silent(cur_space_id); |
| 600 |
|
| 601 |
if (space != nullptr) { |
| 602 |
const page_size_t cur_page_size(space->flags); |
| 603 |
page_size.copy_from(cur_page_size); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
if (space == nullptr) { |
| 608 |
continue; |
| 609 |
} |
| 610 |
|
| 611 |
buf_read_page_background(page_id_t(this_space_id, BUF_DUMP_PAGE(dump[i])), |
| 612 |
page_size, true); |
| 613 |
|
| 614 |
if (i % 64 == 63) { |
| 615 |
os_aio_simulated_wake_handler_threads(); |
| 616 |
} |
| 617 |
|
| 618 |
/* Update the progress every 32 MiB, which is every Nth page, |
| 619 |
where N = 32*1024^2 / page_size. */ |
| 620 |
static const ulint update_status_every_n_mb = 32; |
| 621 |
static const ulint update_status_every_n_pages = |
| 622 |
update_status_every_n_mb * 1024 * 1024 / page_size.physical(); |
| 623 |
|
| 624 |
if (i % update_status_every_n_pages == 0) { |
| 625 |
buf_load_status(STATUS_VERBOSE, "Loaded " ULINTPF "/" ULINTPF " pages", |
| 626 |
i + 1, dump_n); |
| 627 |
mysql_stage_set_work_completed(pfs_stage_progress, i); |
| 628 |
} |
| 629 |
|
| 630 |
if (buf_load_abort_flag) { |
| 631 |
if (space != nullptr) { |
| 632 |
fil_space_release(space); |
| 633 |
} |
| 634 |
buf_load_abort_flag = false; |
| 635 |
ut::free(dump); |
| 636 |
buf_load_status(STATUS_INFO, "Buffer pool(s) load aborted on request"); |
| 637 |
/* Premature end, set estimated = completed = i and |
| 638 |
end the current stage event. */ |
| 639 |
mysql_stage_set_work_estimated(pfs_stage_progress, i); |
| 640 |
mysql_stage_set_work_completed(pfs_stage_progress, i); |
| 641 |
#ifdef HAVE_PSI_STAGE_INTERFACE |
| 642 |
mysql_end_stage(); |
| 643 |
#endif /* HAVE_PSI_STAGE_INTERFACE */ |
| 644 |
return; |
| 645 |
} |
| 646 |
|
| 647 |
buf_load_throttle_if_needed(&last_check_time, &last_activity_cnt, i); |
| 648 |
} |
| 649 |
|
| 650 |
if (space != nullptr) { |
| 651 |
fil_space_release(space); |
| 652 |
} |
| 653 |
|
| 654 |
ut::free(dump); |
| 655 |
|
| 656 |
ut_sprintf_timestamp(now); |
| 657 |
|
| 658 |
buf_load_status(STATUS_INFO, "Buffer pool(s) load completed at %s", now); |
| 659 |
|
| 660 |
/* Make sure that estimated = completed when we end. */ |
| 661 |
mysql_stage_set_work_completed(pfs_stage_progress, dump_n); |
| 662 |
/* End the stage progress event. */ |
| 663 |
#ifdef HAVE_PSI_STAGE_INTERFACE |
| 664 |
mysql_end_stage(); |
| 665 |
#endif /* HAVE_PSI_STAGE_INTERFACE */ |
| 666 |
} |
| 667 |
|
| 668 |
/** Aborts a currently running buffer pool load. This function is called by |
| 669 |
MySQL code via buffer_pool_load_abort() and it should return immediately |
| 670 |
because the whole MySQL is frozen during its execution. */ |
| 671 |
void buf_load_abort() { buf_load_abort_flag = true; } |
| 672 |
|
| 673 |
/** This is the main thread for buffer pool dump/load. It waits for an |
| 674 |
event and when waked up either performs a dump or load and sleeps |
| 675 |
again. */ |
| 676 |
void buf_dump_thread() { |
| 677 |
ut_ad(!srv_read_only_mode); |
| 678 |
|
| 679 |
buf_dump_status(STATUS_VERBOSE, "Dumping of buffer pool not started"); |
| 680 |
buf_load_status(STATUS_VERBOSE, "Loading of buffer pool not started"); |
| 681 |
|
| 682 |
if (srv_buffer_pool_load_at_startup) { |
| 683 |
buf_load(); |
| 684 |
} |
| 685 |
|
| 686 |
while (!SHUTTING_DOWN()) { |
| 687 |
os_event_wait(srv_buf_dump_event); |
| 688 |
|
| 689 |
if (buf_dump_should_start) { |
| 690 |
buf_dump_should_start = false; |
| 691 |
buf_dump(true /* quit on shutdown */); |
| 692 |
} |
| 693 |
|
| 694 |
if (buf_load_should_start) { |
| 695 |
buf_load_should_start = false; |
| 696 |
buf_load(); |
| 697 |
} |
| 698 |
|
| 699 |
os_event_reset(srv_buf_dump_event); |
| 700 |
} |
| 701 |
|
| 702 |
if (srv_buffer_pool_dump_at_shutdown && srv_fast_shutdown != 2) { |
| 703 |
buf_dump(false /* ignore shutdown down flag, |
| 704 |
keep going even if we are in a shutdown state */); |
| 705 |
} |
| 706 |
} |