You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4052 lines
109 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
  3. Copyright (c) 2008, Google Inc.
  4. Portions of this file contain modifications contributed and copyrighted by
  5. Google, Inc. Those modifications are gratefully acknowledged and are described
  6. briefly in the InnoDB documentation. The contributions by Google are
  7. incorporated with their permission, and subject to the conditions contained in
  8. the file COPYING.Google.
  9. This program is free software; you can redistribute it and/or modify it under
  10. the terms of the GNU General Public License as published by the Free Software
  11. Foundation; version 2 of the License.
  12. This program is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along with
  16. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  17. Place, Suite 330, Boston, MA 02111-1307 USA
  18. *****************************************************************************/
  19. /**************************************************//**
  20. @file buf/buf0buf.c
  21. The database buffer buf_pool
  22. Created 11/5/1995 Heikki Tuuri
  23. *******************************************************/
  24. #include "buf0buf.h"
  25. #ifdef UNIV_NONINL
  26. #include "buf0buf.ic"
  27. #endif
  28. #include "mem0mem.h"
  29. #include "btr0btr.h"
  30. #include "fil0fil.h"
  31. #ifndef UNIV_HOTBACKUP
  32. #include "buf0buddy.h"
  33. #include "lock0lock.h"
  34. #include "btr0sea.h"
  35. #include "ibuf0ibuf.h"
  36. #include "trx0undo.h"
  37. #include "log0log.h"
  38. #endif /* !UNIV_HOTBACKUP */
  39. #include "srv0srv.h"
  40. #include "dict0dict.h"
  41. #include "log0recv.h"
  42. #include "page0zip.h"
  43. /*
  44. IMPLEMENTATION OF THE BUFFER POOL
  45. =================================
  46. Performance improvement:
  47. ------------------------
  48. Thread scheduling in NT may be so slow that the OS wait mechanism should
  49. not be used even in waiting for disk reads to complete.
  50. Rather, we should put waiting query threads to the queue of
  51. waiting jobs, and let the OS thread do something useful while the i/o
  52. is processed. In this way we could remove most OS thread switches in
  53. an i/o-intensive benchmark like TPC-C.
  54. A possibility is to put a user space thread library between the database
  55. and NT. User space thread libraries might be very fast.
  56. SQL Server 7.0 can be configured to use 'fibers' which are lightweight
  57. threads in NT. These should be studied.
  58. Buffer frames and blocks
  59. ------------------------
  60. Following the terminology of Gray and Reuter, we call the memory
  61. blocks where file pages are loaded buffer frames. For each buffer
  62. frame there is a control block, or shortly, a block, in the buffer
  63. control array. The control info which does not need to be stored
  64. in the file along with the file page, resides in the control block.
  65. Buffer pool struct
  66. ------------------
  67. The buffer buf_pool contains a single mutex which protects all the
  68. control data structures of the buf_pool. The content of a buffer frame is
  69. protected by a separate read-write lock in its control block, though.
  70. These locks can be locked and unlocked without owning the buf_pool mutex.
  71. The OS events in the buf_pool struct can be waited for without owning the
  72. buf_pool mutex.
  73. The buf_pool mutex is a hot-spot in main memory, causing a lot of
  74. memory bus traffic on multiprocessor systems when processors
  75. alternately access the mutex. On our Pentium, the mutex is accessed
  76. maybe every 10 microseconds. We gave up the solution to have mutexes
  77. for each control block, for instance, because it seemed to be
  78. complicated.
  79. A solution to reduce mutex contention of the buf_pool mutex is to
  80. create a separate mutex for the page hash table. On Pentium,
  81. accessing the hash table takes 2 microseconds, about half
  82. of the total buf_pool mutex hold time.
  83. Control blocks
  84. --------------
  85. The control block contains, for instance, the bufferfix count
  86. which is incremented when a thread wants a file page to be fixed
  87. in a buffer frame. The bufferfix operation does not lock the
  88. contents of the frame, however. For this purpose, the control
  89. block contains a read-write lock.
  90. The buffer frames have to be aligned so that the start memory
  91. address of a frame is divisible by the universal page size, which
  92. is a power of two.
  93. We intend to make the buffer buf_pool size on-line reconfigurable,
  94. that is, the buf_pool size can be changed without closing the database.
  95. Then the database administarator may adjust it to be bigger
  96. at night, for example. The control block array must
  97. contain enough control blocks for the maximum buffer buf_pool size
  98. which is used in the particular database.
  99. If the buf_pool size is cut, we exploit the virtual memory mechanism of
  100. the OS, and just refrain from using frames at high addresses. Then the OS
  101. can swap them to disk.
  102. The control blocks containing file pages are put to a hash table
  103. according to the file address of the page.
  104. We could speed up the access to an individual page by using
  105. "pointer swizzling": we could replace the page references on
  106. non-leaf index pages by direct pointers to the page, if it exists
  107. in the buf_pool. We could make a separate hash table where we could
  108. chain all the page references in non-leaf pages residing in the buf_pool,
  109. using the page reference as the hash key,
  110. and at the time of reading of a page update the pointers accordingly.
  111. Drawbacks of this solution are added complexity and,
  112. possibly, extra space required on non-leaf pages for memory pointers.
  113. A simpler solution is just to speed up the hash table mechanism
  114. in the database, using tables whose size is a power of 2.
  115. Lists of blocks
  116. ---------------
  117. There are several lists of control blocks.
  118. The free list (buf_pool->free) contains blocks which are currently not
  119. used.
  120. The common LRU list contains all the blocks holding a file page
  121. except those for which the bufferfix count is non-zero.
  122. The pages are in the LRU list roughly in the order of the last
  123. access to the page, so that the oldest pages are at the end of the
  124. list. We also keep a pointer to near the end of the LRU list,
  125. which we can use when we want to artificially age a page in the
  126. buf_pool. This is used if we know that some page is not needed
  127. again for some time: we insert the block right after the pointer,
  128. causing it to be replaced sooner than would noramlly be the case.
  129. Currently this aging mechanism is used for read-ahead mechanism
  130. of pages, and it can also be used when there is a scan of a full
  131. table which cannot fit in the memory. Putting the pages near the
  132. of the LRU list, we make sure that most of the buf_pool stays in the
  133. main memory, undisturbed.
  134. The unzip_LRU list contains a subset of the common LRU list. The
  135. blocks on the unzip_LRU list hold a compressed file page and the
  136. corresponding uncompressed page frame. A block is in unzip_LRU if and
  137. only if the predicate buf_page_belongs_to_unzip_LRU(&block->page)
  138. holds. The blocks in unzip_LRU will be in same order as they are in
  139. the common LRU list. That is, each manipulation of the common LRU
  140. list will result in the same manipulation of the unzip_LRU list.
  141. The chain of modified blocks (buf_pool->flush_list) contains the blocks
  142. holding file pages that have been modified in the memory
  143. but not written to disk yet. The block with the oldest modification
  144. which has not yet been written to disk is at the end of the chain.
  145. The chain of unmodified compressed blocks (buf_pool->zip_clean)
  146. contains the control blocks (buf_page_t) of those compressed pages
  147. that are not in buf_pool->flush_list and for which no uncompressed
  148. page has been allocated in the buffer pool. The control blocks for
  149. uncompressed pages are accessible via buf_block_t objects that are
  150. reachable via buf_pool->chunks[].
  151. The chains of free memory blocks (buf_pool->zip_free[]) are used by
  152. the buddy allocator (buf0buddy.c) to keep track of currently unused
  153. memory blocks of size sizeof(buf_page_t)..UNIV_PAGE_SIZE / 2. These
  154. blocks are inside the UNIV_PAGE_SIZE-sized memory blocks of type
  155. BUF_BLOCK_MEMORY that the buddy allocator requests from the buffer
  156. pool. The buddy allocator is solely used for allocating control
  157. blocks for compressed pages (buf_page_t) and compressed page frames.
  158. Loading a file page
  159. -------------------
  160. First, a victim block for replacement has to be found in the
  161. buf_pool. It is taken from the free list or searched for from the
  162. end of the LRU-list. An exclusive lock is reserved for the frame,
  163. the io_fix field is set in the block fixing the block in buf_pool,
  164. and the io-operation for loading the page is queued. The io-handler thread
  165. releases the X-lock on the frame and resets the io_fix field
  166. when the io operation completes.
  167. A thread may request the above operation using the function
  168. buf_page_get(). It may then continue to request a lock on the frame.
  169. The lock is granted when the io-handler releases the x-lock.
  170. Read-ahead
  171. ----------
  172. The read-ahead mechanism is intended to be intelligent and
  173. isolated from the semantically higher levels of the database
  174. index management. From the higher level we only need the
  175. information if a file page has a natural successor or
  176. predecessor page. On the leaf level of a B-tree index,
  177. these are the next and previous pages in the natural
  178. order of the pages.
  179. Let us first explain the read-ahead mechanism when the leafs
  180. of a B-tree are scanned in an ascending or descending order.
  181. When a read page is the first time referenced in the buf_pool,
  182. the buffer manager checks if it is at the border of a so-called
  183. linear read-ahead area. The tablespace is divided into these
  184. areas of size 64 blocks, for example. So if the page is at the
  185. border of such an area, the read-ahead mechanism checks if
  186. all the other blocks in the area have been accessed in an
  187. ascending or descending order. If this is the case, the system
  188. looks at the natural successor or predecessor of the page,
  189. checks if that is at the border of another area, and in this case
  190. issues read-requests for all the pages in that area. Maybe
  191. we could relax the condition that all the pages in the area
  192. have to be accessed: if data is deleted from a table, there may
  193. appear holes of unused pages in the area.
  194. A different read-ahead mechanism is used when there appears
  195. to be a random access pattern to a file.
  196. If a new page is referenced in the buf_pool, and several pages
  197. of its random access area (for instance, 32 consecutive pages
  198. in a tablespace) have recently been referenced, we may predict
  199. that the whole area may be needed in the near future, and issue
  200. the read requests for the whole area.
  201. */
  202. #ifndef UNIV_HOTBACKUP
  203. /** Value in microseconds */
  204. static const int WAIT_FOR_READ = 5000;
  205. /** The buffer buf_pool of the database */
  206. UNIV_INTERN buf_pool_t* buf_pool = NULL;
  207. /** mutex protecting the buffer pool struct and control blocks, except the
  208. read-write lock in them */
  209. UNIV_INTERN mutex_t buf_pool_mutex;
  210. /** mutex protecting the control blocks of compressed-only pages
  211. (of type buf_page_t, not buf_block_t) */
  212. UNIV_INTERN mutex_t buf_pool_zip_mutex;
  213. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  214. static ulint buf_dbg_counter = 0; /*!< This is used to insert validation
  215. operations in excution in the
  216. debug version */
  217. /** Flag to forbid the release of the buffer pool mutex.
  218. Protected by buf_pool_mutex. */
  219. UNIV_INTERN ulint buf_pool_mutex_exit_forbidden = 0;
  220. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  221. #ifdef UNIV_DEBUG
  222. /** If this is set TRUE, the program prints info whenever
  223. read-ahead or flush occurs */
  224. UNIV_INTERN ibool buf_debug_prints = FALSE;
  225. #endif /* UNIV_DEBUG */
  226. /** A chunk of buffers. The buffer pool is allocated in chunks. */
  227. struct buf_chunk_struct{
  228. ulint mem_size; /*!< allocated size of the chunk */
  229. ulint size; /*!< size of frames[] and blocks[] */
  230. void* mem; /*!< pointer to the memory area which
  231. was allocated for the frames */
  232. buf_block_t* blocks; /*!< array of buffer control blocks */
  233. };
  234. #endif /* !UNIV_HOTBACKUP */
  235. /********************************************************************//**
  236. Calculates a page checksum which is stored to the page when it is written
  237. to a file. Note that we must be careful to calculate the same value on
  238. 32-bit and 64-bit architectures.
  239. @return checksum */
  240. UNIV_INTERN
  241. ulint
  242. buf_calc_page_new_checksum(
  243. /*=======================*/
  244. const byte* page) /*!< in: buffer page */
  245. {
  246. ulint checksum;
  247. /* Since the field FIL_PAGE_FILE_FLUSH_LSN, and in versions <= 4.1.x
  248. ..._ARCH_LOG_NO, are written outside the buffer pool to the first
  249. pages of data files, we have to skip them in the page checksum
  250. calculation.
  251. We must also skip the field FIL_PAGE_SPACE_OR_CHKSUM where the
  252. checksum is stored, and also the last 8 bytes of page because
  253. there we store the old formula checksum. */
  254. checksum = ut_fold_binary(page + FIL_PAGE_OFFSET,
  255. FIL_PAGE_FILE_FLUSH_LSN - FIL_PAGE_OFFSET)
  256. + ut_fold_binary(page + FIL_PAGE_DATA,
  257. UNIV_PAGE_SIZE - FIL_PAGE_DATA
  258. - FIL_PAGE_END_LSN_OLD_CHKSUM);
  259. checksum = checksum & 0xFFFFFFFFUL;
  260. return(checksum);
  261. }
  262. /********************************************************************//**
  263. In versions < 4.0.14 and < 4.1.1 there was a bug that the checksum only
  264. looked at the first few bytes of the page. This calculates that old
  265. checksum.
  266. NOTE: we must first store the new formula checksum to
  267. FIL_PAGE_SPACE_OR_CHKSUM before calculating and storing this old checksum
  268. because this takes that field as an input!
  269. @return checksum */
  270. UNIV_INTERN
  271. ulint
  272. buf_calc_page_old_checksum(
  273. /*=======================*/
  274. const byte* page) /*!< in: buffer page */
  275. {
  276. ulint checksum;
  277. checksum = ut_fold_binary(page, FIL_PAGE_FILE_FLUSH_LSN);
  278. checksum = checksum & 0xFFFFFFFFUL;
  279. return(checksum);
  280. }
  281. /********************************************************************//**
  282. Checks if a page is corrupt.
  283. @return TRUE if corrupted */
  284. UNIV_INTERN
  285. ibool
  286. buf_page_is_corrupted(
  287. /*==================*/
  288. const byte* read_buf, /*!< in: a database page */
  289. ulint zip_size) /*!< in: size of compressed page;
  290. 0 for uncompressed pages */
  291. {
  292. ulint checksum_field;
  293. ulint old_checksum_field;
  294. if (UNIV_LIKELY(!zip_size)
  295. && memcmp(read_buf + FIL_PAGE_LSN + 4,
  296. read_buf + UNIV_PAGE_SIZE
  297. - FIL_PAGE_END_LSN_OLD_CHKSUM + 4, 4)) {
  298. /* Stored log sequence numbers at the start and the end
  299. of page do not match */
  300. return(TRUE);
  301. }
  302. #ifndef UNIV_HOTBACKUP
  303. if (recv_lsn_checks_on) {
  304. ib_uint64_t current_lsn;
  305. if (log_peek_lsn(&current_lsn)
  306. && current_lsn < mach_read_ull(read_buf + FIL_PAGE_LSN)) {
  307. ut_print_timestamp(stderr);
  308. fprintf(stderr,
  309. " InnoDB: Error: page %lu log sequence number"
  310. " %llu\n"
  311. "InnoDB: is in the future! Current system "
  312. "log sequence number %llu.\n"
  313. "InnoDB: Your database may be corrupt or "
  314. "you may have copied the InnoDB\n"
  315. "InnoDB: tablespace but not the InnoDB "
  316. "log files. See\n"
  317. "InnoDB: " REFMAN "forcing-recovery.html\n"
  318. "InnoDB: for more information.\n",
  319. (ulong) mach_read_from_4(read_buf
  320. + FIL_PAGE_OFFSET),
  321. mach_read_ull(read_buf + FIL_PAGE_LSN),
  322. current_lsn);
  323. }
  324. }
  325. #endif
  326. /* If we use checksums validation, make additional check before
  327. returning TRUE to ensure that the checksum is not equal to
  328. BUF_NO_CHECKSUM_MAGIC which might be stored by InnoDB with checksums
  329. disabled. Otherwise, skip checksum calculation and return FALSE */
  330. if (UNIV_LIKELY(srv_use_checksums)) {
  331. checksum_field = mach_read_from_4(read_buf
  332. + FIL_PAGE_SPACE_OR_CHKSUM);
  333. if (UNIV_UNLIKELY(zip_size)) {
  334. return(checksum_field != BUF_NO_CHECKSUM_MAGIC
  335. && checksum_field
  336. != page_zip_calc_checksum(read_buf, zip_size));
  337. }
  338. old_checksum_field = mach_read_from_4(
  339. read_buf + UNIV_PAGE_SIZE
  340. - FIL_PAGE_END_LSN_OLD_CHKSUM);
  341. /* There are 2 valid formulas for old_checksum_field:
  342. 1. Very old versions of InnoDB only stored 8 byte lsn to the
  343. start and the end of the page.
  344. 2. Newer InnoDB versions store the old formula checksum
  345. there. */
  346. if (old_checksum_field != mach_read_from_4(read_buf
  347. + FIL_PAGE_LSN)
  348. && old_checksum_field != BUF_NO_CHECKSUM_MAGIC
  349. && old_checksum_field
  350. != buf_calc_page_old_checksum(read_buf)) {
  351. return(TRUE);
  352. }
  353. /* InnoDB versions < 4.0.14 and < 4.1.1 stored the space id
  354. (always equal to 0), to FIL_PAGE_SPACE_OR_CHKSUM */
  355. if (checksum_field != 0
  356. && checksum_field != BUF_NO_CHECKSUM_MAGIC
  357. && checksum_field
  358. != buf_calc_page_new_checksum(read_buf)) {
  359. return(TRUE);
  360. }
  361. }
  362. return(FALSE);
  363. }
  364. /********************************************************************//**
  365. Prints a page to stderr. */
  366. UNIV_INTERN
  367. void
  368. buf_page_print(
  369. /*===========*/
  370. const byte* read_buf, /*!< in: a database page */
  371. ulint zip_size) /*!< in: compressed page size, or
  372. 0 for uncompressed pages */
  373. {
  374. #ifndef UNIV_HOTBACKUP
  375. dict_index_t* index;
  376. #endif /* !UNIV_HOTBACKUP */
  377. ulint checksum;
  378. ulint old_checksum;
  379. ulint size = zip_size;
  380. if (!size) {
  381. size = UNIV_PAGE_SIZE;
  382. }
  383. ut_print_timestamp(stderr);
  384. fprintf(stderr, " InnoDB: Page dump in ascii and hex (%lu bytes):\n",
  385. (ulong) size);
  386. ut_print_buf(stderr, read_buf, size);
  387. fputs("\nInnoDB: End of page dump\n", stderr);
  388. if (zip_size) {
  389. /* Print compressed page. */
  390. switch (fil_page_get_type(read_buf)) {
  391. case FIL_PAGE_TYPE_ZBLOB:
  392. case FIL_PAGE_TYPE_ZBLOB2:
  393. checksum = srv_use_checksums
  394. ? page_zip_calc_checksum(read_buf, zip_size)
  395. : BUF_NO_CHECKSUM_MAGIC;
  396. ut_print_timestamp(stderr);
  397. fprintf(stderr,
  398. " InnoDB: Compressed BLOB page"
  399. " checksum %lu, stored %lu\n"
  400. "InnoDB: Page lsn %lu %lu\n"
  401. "InnoDB: Page number (if stored"
  402. " to page already) %lu,\n"
  403. "InnoDB: space id (if stored"
  404. " to page already) %lu\n",
  405. (ulong) checksum,
  406. (ulong) mach_read_from_4(
  407. read_buf + FIL_PAGE_SPACE_OR_CHKSUM),
  408. (ulong) mach_read_from_4(
  409. read_buf + FIL_PAGE_LSN),
  410. (ulong) mach_read_from_4(
  411. read_buf + (FIL_PAGE_LSN + 4)),
  412. (ulong) mach_read_from_4(
  413. read_buf + FIL_PAGE_OFFSET),
  414. (ulong) mach_read_from_4(
  415. read_buf
  416. + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID));
  417. return;
  418. default:
  419. ut_print_timestamp(stderr);
  420. fprintf(stderr,
  421. " InnoDB: unknown page type %lu,"
  422. " assuming FIL_PAGE_INDEX\n",
  423. fil_page_get_type(read_buf));
  424. /* fall through */
  425. case FIL_PAGE_INDEX:
  426. checksum = srv_use_checksums
  427. ? page_zip_calc_checksum(read_buf, zip_size)
  428. : BUF_NO_CHECKSUM_MAGIC;
  429. ut_print_timestamp(stderr);
  430. fprintf(stderr,
  431. " InnoDB: Compressed page checksum %lu,"
  432. " stored %lu\n"
  433. "InnoDB: Page lsn %lu %lu\n"
  434. "InnoDB: Page number (if stored"
  435. " to page already) %lu,\n"
  436. "InnoDB: space id (if stored"
  437. " to page already) %lu\n",
  438. (ulong) checksum,
  439. (ulong) mach_read_from_4(
  440. read_buf + FIL_PAGE_SPACE_OR_CHKSUM),
  441. (ulong) mach_read_from_4(
  442. read_buf + FIL_PAGE_LSN),
  443. (ulong) mach_read_from_4(
  444. read_buf + (FIL_PAGE_LSN + 4)),
  445. (ulong) mach_read_from_4(
  446. read_buf + FIL_PAGE_OFFSET),
  447. (ulong) mach_read_from_4(
  448. read_buf
  449. + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID));
  450. return;
  451. case FIL_PAGE_TYPE_XDES:
  452. /* This is an uncompressed page. */
  453. break;
  454. }
  455. }
  456. checksum = srv_use_checksums
  457. ? buf_calc_page_new_checksum(read_buf) : BUF_NO_CHECKSUM_MAGIC;
  458. old_checksum = srv_use_checksums
  459. ? buf_calc_page_old_checksum(read_buf) : BUF_NO_CHECKSUM_MAGIC;
  460. ut_print_timestamp(stderr);
  461. fprintf(stderr,
  462. " InnoDB: Page checksum %lu, prior-to-4.0.14-form"
  463. " checksum %lu\n"
  464. "InnoDB: stored checksum %lu, prior-to-4.0.14-form"
  465. " stored checksum %lu\n"
  466. "InnoDB: Page lsn %lu %lu, low 4 bytes of lsn"
  467. " at page end %lu\n"
  468. "InnoDB: Page number (if stored to page already) %lu,\n"
  469. "InnoDB: space id (if created with >= MySQL-4.1.1"
  470. " and stored already) %lu\n",
  471. (ulong) checksum, (ulong) old_checksum,
  472. (ulong) mach_read_from_4(read_buf + FIL_PAGE_SPACE_OR_CHKSUM),
  473. (ulong) mach_read_from_4(read_buf + UNIV_PAGE_SIZE
  474. - FIL_PAGE_END_LSN_OLD_CHKSUM),
  475. (ulong) mach_read_from_4(read_buf + FIL_PAGE_LSN),
  476. (ulong) mach_read_from_4(read_buf + FIL_PAGE_LSN + 4),
  477. (ulong) mach_read_from_4(read_buf + UNIV_PAGE_SIZE
  478. - FIL_PAGE_END_LSN_OLD_CHKSUM + 4),
  479. (ulong) mach_read_from_4(read_buf + FIL_PAGE_OFFSET),
  480. (ulong) mach_read_from_4(read_buf
  481. + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID));
  482. #ifndef UNIV_HOTBACKUP
  483. if (mach_read_from_2(read_buf + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_TYPE)
  484. == TRX_UNDO_INSERT) {
  485. fprintf(stderr,
  486. "InnoDB: Page may be an insert undo log page\n");
  487. } else if (mach_read_from_2(read_buf + TRX_UNDO_PAGE_HDR
  488. + TRX_UNDO_PAGE_TYPE)
  489. == TRX_UNDO_UPDATE) {
  490. fprintf(stderr,
  491. "InnoDB: Page may be an update undo log page\n");
  492. }
  493. #endif /* !UNIV_HOTBACKUP */
  494. switch (fil_page_get_type(read_buf)) {
  495. case FIL_PAGE_INDEX:
  496. fprintf(stderr,
  497. "InnoDB: Page may be an index page where"
  498. " index id is %lu %lu\n",
  499. (ulong) ut_dulint_get_high(
  500. btr_page_get_index_id(read_buf)),
  501. (ulong) ut_dulint_get_low(
  502. btr_page_get_index_id(read_buf)));
  503. #ifndef UNIV_HOTBACKUP
  504. index = dict_index_find_on_id_low(
  505. btr_page_get_index_id(read_buf));
  506. if (index) {
  507. fputs("InnoDB: (", stderr);
  508. dict_index_name_print(stderr, NULL, index);
  509. fputs(")\n", stderr);
  510. }
  511. #endif /* !UNIV_HOTBACKUP */
  512. break;
  513. case FIL_PAGE_INODE:
  514. fputs("InnoDB: Page may be an 'inode' page\n", stderr);
  515. break;
  516. case FIL_PAGE_IBUF_FREE_LIST:
  517. fputs("InnoDB: Page may be an insert buffer free list page\n",
  518. stderr);
  519. break;
  520. case FIL_PAGE_TYPE_ALLOCATED:
  521. fputs("InnoDB: Page may be a freshly allocated page\n",
  522. stderr);
  523. break;
  524. case FIL_PAGE_IBUF_BITMAP:
  525. fputs("InnoDB: Page may be an insert buffer bitmap page\n",
  526. stderr);
  527. break;
  528. case FIL_PAGE_TYPE_SYS:
  529. fputs("InnoDB: Page may be a system page\n",
  530. stderr);
  531. break;
  532. case FIL_PAGE_TYPE_TRX_SYS:
  533. fputs("InnoDB: Page may be a transaction system page\n",
  534. stderr);
  535. break;
  536. case FIL_PAGE_TYPE_FSP_HDR:
  537. fputs("InnoDB: Page may be a file space header page\n",
  538. stderr);
  539. break;
  540. case FIL_PAGE_TYPE_XDES:
  541. fputs("InnoDB: Page may be an extent descriptor page\n",
  542. stderr);
  543. break;
  544. case FIL_PAGE_TYPE_BLOB:
  545. fputs("InnoDB: Page may be a BLOB page\n",
  546. stderr);
  547. break;
  548. case FIL_PAGE_TYPE_ZBLOB:
  549. case FIL_PAGE_TYPE_ZBLOB2:
  550. fputs("InnoDB: Page may be a compressed BLOB page\n",
  551. stderr);
  552. break;
  553. }
  554. }
  555. #ifndef UNIV_HOTBACKUP
  556. /********************************************************************//**
  557. Initializes a buffer control block when the buf_pool is created. */
  558. static
  559. void
  560. buf_block_init(
  561. /*===========*/
  562. buf_block_t* block, /*!< in: pointer to control block */
  563. byte* frame) /*!< in: pointer to buffer frame */
  564. {
  565. UNIV_MEM_DESC(frame, UNIV_PAGE_SIZE, block);
  566. block->frame = frame;
  567. block->page.state = BUF_BLOCK_NOT_USED;
  568. block->page.buf_fix_count = 0;
  569. block->page.io_fix = BUF_IO_NONE;
  570. block->modify_clock = 0;
  571. #ifdef UNIV_DEBUG_FILE_ACCESSES
  572. block->page.file_page_was_freed = FALSE;
  573. #endif /* UNIV_DEBUG_FILE_ACCESSES */
  574. block->check_index_page_at_flush = FALSE;
  575. block->index = NULL;
  576. #ifdef UNIV_DEBUG
  577. block->page.in_page_hash = FALSE;
  578. block->page.in_zip_hash = FALSE;
  579. block->page.in_flush_list = FALSE;
  580. block->page.in_free_list = FALSE;
  581. block->page.in_LRU_list = FALSE;
  582. block->in_unzip_LRU_list = FALSE;
  583. #endif /* UNIV_DEBUG */
  584. #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
  585. block->n_pointers = 0;
  586. #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
  587. page_zip_des_init(&block->page.zip);
  588. mutex_create(&block->mutex, SYNC_BUF_BLOCK);
  589. rw_lock_create(&block->lock, SYNC_LEVEL_VARYING);
  590. ut_ad(rw_lock_validate(&(block->lock)));
  591. #ifdef UNIV_SYNC_DEBUG
  592. rw_lock_create(&block->debug_latch, SYNC_NO_ORDER_CHECK);
  593. #endif /* UNIV_SYNC_DEBUG */
  594. }
  595. /********************************************************************//**
  596. Allocates a chunk of buffer frames.
  597. @return chunk, or NULL on failure */
  598. static
  599. buf_chunk_t*
  600. buf_chunk_init(
  601. /*===========*/
  602. buf_chunk_t* chunk, /*!< out: chunk of buffers */
  603. ulint mem_size) /*!< in: requested size in bytes */
  604. {
  605. buf_block_t* block;
  606. byte* frame;
  607. ulint i;
  608. /* Round down to a multiple of page size,
  609. although it already should be. */
  610. mem_size = ut_2pow_round(mem_size, UNIV_PAGE_SIZE);
  611. /* Reserve space for the block descriptors. */
  612. mem_size += ut_2pow_round((mem_size / UNIV_PAGE_SIZE) * (sizeof *block)
  613. + (UNIV_PAGE_SIZE - 1), UNIV_PAGE_SIZE);
  614. chunk->mem_size = mem_size;
  615. chunk->mem = os_mem_alloc_large(&chunk->mem_size);
  616. if (UNIV_UNLIKELY(chunk->mem == NULL)) {
  617. return(NULL);
  618. }
  619. /* Allocate the block descriptors from
  620. the start of the memory block. */
  621. chunk->blocks = chunk->mem;
  622. /* Align a pointer to the first frame. Note that when
  623. os_large_page_size is smaller than UNIV_PAGE_SIZE,
  624. we may allocate one fewer block than requested. When
  625. it is bigger, we may allocate more blocks than requested. */
  626. frame = ut_align(chunk->mem, UNIV_PAGE_SIZE);
  627. chunk->size = chunk->mem_size / UNIV_PAGE_SIZE
  628. - (frame != chunk->mem);
  629. /* Subtract the space needed for block descriptors. */
  630. {
  631. ulint size = chunk->size;
  632. while (frame < (byte*) (chunk->blocks + size)) {
  633. frame += UNIV_PAGE_SIZE;
  634. size--;
  635. }
  636. chunk->size = size;
  637. }
  638. /* Init block structs and assign frames for them. Then we
  639. assign the frames to the first blocks (we already mapped the
  640. memory above). */
  641. block = chunk->blocks;
  642. for (i = chunk->size; i--; ) {
  643. buf_block_init(block, frame);
  644. #ifdef HAVE_purify
  645. /* Wipe contents of frame to eliminate a Purify warning */
  646. memset(block->frame, '\0', UNIV_PAGE_SIZE);
  647. #endif
  648. /* Add the block to the free list */
  649. UT_LIST_ADD_LAST(list, buf_pool->free, (&block->page));
  650. ut_d(block->page.in_free_list = TRUE);
  651. block++;
  652. frame += UNIV_PAGE_SIZE;
  653. }
  654. return(chunk);
  655. }
  656. #ifdef UNIV_DEBUG
  657. /*********************************************************************//**
  658. Finds a block in the given buffer chunk that points to a
  659. given compressed page.
  660. @return buffer block pointing to the compressed page, or NULL */
  661. static
  662. buf_block_t*
  663. buf_chunk_contains_zip(
  664. /*===================*/
  665. buf_chunk_t* chunk, /*!< in: chunk being checked */
  666. const void* data) /*!< in: pointer to compressed page */
  667. {
  668. buf_block_t* block;
  669. ulint i;
  670. ut_ad(buf_pool);
  671. ut_ad(buf_pool_mutex_own());
  672. block = chunk->blocks;
  673. for (i = chunk->size; i--; block++) {
  674. if (block->page.zip.data == data) {
  675. return(block);
  676. }
  677. }
  678. return(NULL);
  679. }
  680. /*********************************************************************//**
  681. Finds a block in the buffer pool that points to a
  682. given compressed page.
  683. @return buffer block pointing to the compressed page, or NULL */
  684. UNIV_INTERN
  685. buf_block_t*
  686. buf_pool_contains_zip(
  687. /*==================*/
  688. const void* data) /*!< in: pointer to compressed page */
  689. {
  690. ulint n;
  691. buf_chunk_t* chunk = buf_pool->chunks;
  692. for (n = buf_pool->n_chunks; n--; chunk++) {
  693. buf_block_t* block = buf_chunk_contains_zip(chunk, data);
  694. if (block) {
  695. return(block);
  696. }
  697. }
  698. return(NULL);
  699. }
  700. #endif /* UNIV_DEBUG */
  701. /*********************************************************************//**
  702. Checks that all file pages in the buffer chunk are in a replaceable state.
  703. @return address of a non-free block, or NULL if all freed */
  704. static
  705. const buf_block_t*
  706. buf_chunk_not_freed(
  707. /*================*/
  708. buf_chunk_t* chunk) /*!< in: chunk being checked */
  709. {
  710. buf_block_t* block;
  711. ulint i;
  712. ut_ad(buf_pool);
  713. ut_ad(buf_pool_mutex_own());
  714. block = chunk->blocks;
  715. for (i = chunk->size; i--; block++) {
  716. ibool ready;
  717. switch (buf_block_get_state(block)) {
  718. case BUF_BLOCK_ZIP_FREE:
  719. case BUF_BLOCK_ZIP_PAGE:
  720. case BUF_BLOCK_ZIP_DIRTY:
  721. /* The uncompressed buffer pool should never
  722. contain compressed block descriptors. */
  723. ut_error;
  724. break;
  725. case BUF_BLOCK_NOT_USED:
  726. case BUF_BLOCK_READY_FOR_USE:
  727. case BUF_BLOCK_MEMORY:
  728. case BUF_BLOCK_REMOVE_HASH:
  729. /* Skip blocks that are not being used for
  730. file pages. */
  731. break;
  732. case BUF_BLOCK_FILE_PAGE:
  733. mutex_enter(&block->mutex);
  734. ready = buf_flush_ready_for_replace(&block->page);
  735. mutex_exit(&block->mutex);
  736. if (!ready) {
  737. return(block);
  738. }
  739. break;
  740. }
  741. }
  742. return(NULL);
  743. }
  744. /*********************************************************************//**
  745. Checks that all blocks in the buffer chunk are in BUF_BLOCK_NOT_USED state.
  746. @return TRUE if all freed */
  747. static
  748. ibool
  749. buf_chunk_all_free(
  750. /*===============*/
  751. const buf_chunk_t* chunk) /*!< in: chunk being checked */
  752. {
  753. const buf_block_t* block;
  754. ulint i;
  755. ut_ad(buf_pool);
  756. ut_ad(buf_pool_mutex_own());
  757. block = chunk->blocks;
  758. for (i = chunk->size; i--; block++) {
  759. if (buf_block_get_state(block) != BUF_BLOCK_NOT_USED) {
  760. return(FALSE);
  761. }
  762. }
  763. return(TRUE);
  764. }
  765. /********************************************************************//**
  766. Frees a chunk of buffer frames. */
  767. static
  768. void
  769. buf_chunk_free(
  770. /*===========*/
  771. buf_chunk_t* chunk) /*!< out: chunk of buffers */
  772. {
  773. buf_block_t* block;
  774. const buf_block_t* block_end;
  775. ut_ad(buf_pool_mutex_own());
  776. block_end = chunk->blocks + chunk->size;
  777. for (block = chunk->blocks; block < block_end; block++) {
  778. ut_a(buf_block_get_state(block) == BUF_BLOCK_NOT_USED);
  779. ut_a(!block->page.zip.data);
  780. ut_ad(!block->page.in_LRU_list);
  781. ut_ad(!block->in_unzip_LRU_list);
  782. ut_ad(!block->page.in_flush_list);
  783. /* Remove the block from the free list. */
  784. ut_ad(block->page.in_free_list);
  785. UT_LIST_REMOVE(list, buf_pool->free, (&block->page));
  786. /* Free the latches. */
  787. mutex_free(&block->mutex);
  788. rw_lock_free(&block->lock);
  789. #ifdef UNIV_SYNC_DEBUG
  790. rw_lock_free(&block->debug_latch);
  791. #endif /* UNIV_SYNC_DEBUG */
  792. UNIV_MEM_UNDESC(block);
  793. }
  794. os_mem_free_large(chunk->mem, chunk->mem_size);
  795. }
  796. /********************************************************************//**
  797. Creates the buffer pool.
  798. @return own: buf_pool object, NULL if not enough memory or error */
  799. UNIV_INTERN
  800. buf_pool_t*
  801. buf_pool_init(void)
  802. /*===============*/
  803. {
  804. buf_chunk_t* chunk;
  805. ulint i;
  806. buf_pool = mem_zalloc(sizeof(buf_pool_t));
  807. /* 1. Initialize general fields
  808. ------------------------------- */
  809. mutex_create(&buf_pool_mutex, SYNC_BUF_POOL);
  810. mutex_create(&buf_pool_zip_mutex, SYNC_BUF_BLOCK);
  811. buf_pool_mutex_enter();
  812. buf_pool->n_chunks = 1;
  813. buf_pool->chunks = chunk = mem_alloc(sizeof *chunk);
  814. UT_LIST_INIT(buf_pool->free);
  815. if (!buf_chunk_init(chunk, srv_buf_pool_size)) {
  816. mem_free(chunk);
  817. mem_free(buf_pool);
  818. buf_pool = NULL;
  819. return(NULL);
  820. }
  821. srv_buf_pool_old_size = srv_buf_pool_size;
  822. buf_pool->curr_size = chunk->size;
  823. srv_buf_pool_curr_size = buf_pool->curr_size * UNIV_PAGE_SIZE;
  824. buf_pool->page_hash = hash_create(2 * buf_pool->curr_size);
  825. buf_pool->zip_hash = hash_create(2 * buf_pool->curr_size);
  826. buf_pool->last_printout_time = time(NULL);
  827. /* 2. Initialize flushing fields
  828. -------------------------------- */
  829. for (i = BUF_FLUSH_LRU; i < BUF_FLUSH_N_TYPES; i++) {
  830. buf_pool->no_flush[i] = os_event_create(NULL);
  831. }
  832. /* 3. Initialize LRU fields
  833. --------------------------- */
  834. /* All fields are initialized by mem_zalloc(). */
  835. buf_pool_mutex_exit();
  836. btr_search_sys_create(buf_pool->curr_size
  837. * UNIV_PAGE_SIZE / sizeof(void*) / 64);
  838. /* 4. Initialize the buddy allocator fields */
  839. /* All fields are initialized by mem_zalloc(). */
  840. return(buf_pool);
  841. }
  842. /********************************************************************//**
  843. Frees the buffer pool at shutdown. This must not be invoked before
  844. freeing all mutexes. */
  845. UNIV_INTERN
  846. void
  847. buf_pool_free(void)
  848. /*===============*/
  849. {
  850. buf_chunk_t* chunk;
  851. buf_chunk_t* chunks;
  852. chunks = buf_pool->chunks;
  853. chunk = chunks + buf_pool->n_chunks;
  854. while (--chunk >= chunks) {
  855. /* Bypass the checks of buf_chunk_free(), since they
  856. would fail at shutdown. */
  857. os_mem_free_large(chunk->mem, chunk->mem_size);
  858. }
  859. mem_free(buf_pool->chunks);
  860. hash_table_free(buf_pool->page_hash);
  861. hash_table_free(buf_pool->zip_hash);
  862. mem_free(buf_pool);
  863. buf_pool = NULL;
  864. }
  865. /********************************************************************//**
  866. Drops the adaptive hash index. To prevent a livelock, this function
  867. is only to be called while holding btr_search_latch and while
  868. btr_search_enabled == FALSE. */
  869. UNIV_INTERN
  870. void
  871. buf_pool_drop_hash_index(void)
  872. /*==========================*/
  873. {
  874. ibool released_search_latch;
  875. #ifdef UNIV_SYNC_DEBUG
  876. ut_ad(rw_lock_own(&btr_search_latch, RW_LOCK_EX));
  877. #endif /* UNIV_SYNC_DEBUG */
  878. ut_ad(!btr_search_enabled);
  879. do {
  880. buf_chunk_t* chunks = buf_pool->chunks;
  881. buf_chunk_t* chunk = chunks + buf_pool->n_chunks;
  882. released_search_latch = FALSE;
  883. while (--chunk >= chunks) {
  884. buf_block_t* block = chunk->blocks;
  885. ulint i = chunk->size;
  886. for (; i--; block++) {
  887. /* block->is_hashed cannot be modified
  888. when we have an x-latch on btr_search_latch;
  889. see the comment in buf0buf.h */
  890. if (!block->is_hashed) {
  891. continue;
  892. }
  893. /* To follow the latching order, we
  894. have to release btr_search_latch
  895. before acquiring block->latch. */
  896. rw_lock_x_unlock(&btr_search_latch);
  897. /* When we release the search latch,
  898. we must rescan all blocks, because
  899. some may become hashed again. */
  900. released_search_latch = TRUE;
  901. rw_lock_x_lock(&block->lock);
  902. /* This should be guaranteed by the
  903. callers, which will be holding
  904. btr_search_enabled_mutex. */
  905. ut_ad(!btr_search_enabled);
  906. /* Because we did not buffer-fix the
  907. block by calling buf_block_get_gen(),
  908. it is possible that the block has been
  909. allocated for some other use after
  910. btr_search_latch was released above.
  911. We do not care which file page the
  912. block is mapped to. All we want to do
  913. is to drop any hash entries referring
  914. to the page. */
  915. /* It is possible that
  916. block->page.state != BUF_FILE_PAGE.
  917. Even that does not matter, because
  918. btr_search_drop_page_hash_index() will
  919. check block->is_hashed before doing
  920. anything. block->is_hashed can only
  921. be set on uncompressed file pages. */
  922. btr_search_drop_page_hash_index(block);
  923. rw_lock_x_unlock(&block->lock);
  924. rw_lock_x_lock(&btr_search_latch);
  925. ut_ad(!btr_search_enabled);
  926. }
  927. }
  928. } while (released_search_latch);
  929. }
  930. /********************************************************************//**
  931. Relocate a buffer control block. Relocates the block on the LRU list
  932. and in buf_pool->page_hash. Does not relocate bpage->list.
  933. The caller must take care of relocating bpage->list. */
  934. UNIV_INTERN
  935. void
  936. buf_relocate(
  937. /*=========*/
  938. buf_page_t* bpage, /*!< in/out: control block being relocated;
  939. buf_page_get_state(bpage) must be
  940. BUF_BLOCK_ZIP_DIRTY or BUF_BLOCK_ZIP_PAGE */
  941. buf_page_t* dpage) /*!< in/out: destination control block */
  942. {
  943. buf_page_t* b;
  944. ulint fold;
  945. ut_ad(buf_pool_mutex_own());
  946. ut_ad(mutex_own(buf_page_get_mutex(bpage)));
  947. ut_a(buf_page_get_io_fix(bpage) == BUF_IO_NONE);
  948. ut_a(bpage->buf_fix_count == 0);
  949. ut_ad(bpage->in_LRU_list);
  950. ut_ad(!bpage->in_zip_hash);
  951. ut_ad(bpage->in_page_hash);
  952. ut_ad(bpage == buf_page_hash_get(bpage->space, bpage->offset));
  953. #ifdef UNIV_DEBUG
  954. switch (buf_page_get_state(bpage)) {
  955. case BUF_BLOCK_ZIP_FREE:
  956. case BUF_BLOCK_NOT_USED:
  957. case BUF_BLOCK_READY_FOR_USE:
  958. case BUF_BLOCK_FILE_PAGE:
  959. case BUF_BLOCK_MEMORY:
  960. case BUF_BLOCK_REMOVE_HASH:
  961. ut_error;
  962. case BUF_BLOCK_ZIP_DIRTY:
  963. case BUF_BLOCK_ZIP_PAGE:
  964. break;
  965. }
  966. #endif /* UNIV_DEBUG */
  967. memcpy(dpage, bpage, sizeof *dpage);
  968. ut_d(bpage->in_LRU_list = FALSE);
  969. ut_d(bpage->in_page_hash = FALSE);
  970. /* relocate buf_pool->LRU */
  971. b = UT_LIST_GET_PREV(LRU, bpage);
  972. UT_LIST_REMOVE(LRU, buf_pool->LRU, bpage);
  973. if (b) {
  974. UT_LIST_INSERT_AFTER(LRU, buf_pool->LRU, b, dpage);
  975. } else {
  976. UT_LIST_ADD_FIRST(LRU, buf_pool->LRU, dpage);
  977. }
  978. if (UNIV_UNLIKELY(buf_pool->LRU_old == bpage)) {
  979. buf_pool->LRU_old = dpage;
  980. #ifdef UNIV_LRU_DEBUG
  981. /* buf_pool->LRU_old must be the first item in the LRU list
  982. whose "old" flag is set. */
  983. ut_a(buf_pool->LRU_old->old);
  984. ut_a(!UT_LIST_GET_PREV(LRU, buf_pool->LRU_old)
  985. || !UT_LIST_GET_PREV(LRU, buf_pool->LRU_old)->old);
  986. ut_a(!UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old)
  987. || UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old)->old);
  988. } else {
  989. /* Check that the "old" flag is consistent in
  990. the block and its neighbours. */
  991. buf_page_set_old(dpage, buf_page_is_old(dpage));
  992. #endif /* UNIV_LRU_DEBUG */
  993. }
  994. ut_d(UT_LIST_VALIDATE(LRU, buf_page_t, buf_pool->LRU,
  995. ut_ad(ut_list_node_313->in_LRU_list)));
  996. /* relocate buf_pool->page_hash */
  997. fold = buf_page_address_fold(bpage->space, bpage->offset);
  998. HASH_DELETE(buf_page_t, hash, buf_pool->page_hash, fold, bpage);
  999. HASH_INSERT(buf_page_t, hash, buf_pool->page_hash, fold, dpage);
  1000. UNIV_MEM_INVALID(bpage, sizeof *bpage);
  1001. }
  1002. /********************************************************************//**
  1003. Shrinks the buffer pool. */
  1004. static
  1005. void
  1006. buf_pool_shrink(
  1007. /*============*/
  1008. ulint chunk_size) /*!< in: number of pages to remove */
  1009. {
  1010. buf_chunk_t* chunks;
  1011. buf_chunk_t* chunk;
  1012. ulint max_size;
  1013. ulint max_free_size;
  1014. buf_chunk_t* max_chunk;
  1015. buf_chunk_t* max_free_chunk;
  1016. ut_ad(!buf_pool_mutex_own());
  1017. try_again:
  1018. btr_search_disable(); /* Empty the adaptive hash index again */
  1019. buf_pool_mutex_enter();
  1020. shrink_again:
  1021. if (buf_pool->n_chunks <= 1) {
  1022. /* Cannot shrink if there is only one chunk */
  1023. goto func_done;
  1024. }
  1025. /* Search for the largest free chunk
  1026. not larger than the size difference */
  1027. chunks = buf_pool->chunks;
  1028. chunk = chunks + buf_pool->n_chunks;
  1029. max_size = max_free_size = 0;
  1030. max_chunk = max_free_chunk = NULL;
  1031. while (--chunk >= chunks) {
  1032. if (chunk->size <= chunk_size
  1033. && chunk->size > max_free_size) {
  1034. if (chunk->size > max_size) {
  1035. max_size = chunk->size;
  1036. max_chunk = chunk;
  1037. }
  1038. if (buf_chunk_all_free(chunk)) {
  1039. max_free_size = chunk->size;
  1040. max_free_chunk = chunk;
  1041. }
  1042. }
  1043. }
  1044. if (!max_free_size) {
  1045. ulint dirty = 0;
  1046. ulint nonfree = 0;
  1047. buf_block_t* block;
  1048. buf_block_t* bend;
  1049. /* Cannot shrink: try again later
  1050. (do not assign srv_buf_pool_old_size) */
  1051. if (!max_chunk) {
  1052. goto func_exit;
  1053. }
  1054. block = max_chunk->blocks;
  1055. bend = block + max_chunk->size;
  1056. /* Move the blocks of chunk to the end of the
  1057. LRU list and try to flush them. */
  1058. for (; block < bend; block++) {
  1059. switch (buf_block_get_state(block)) {
  1060. case BUF_BLOCK_NOT_USED:
  1061. continue;
  1062. case BUF_BLOCK_FILE_PAGE:
  1063. break;
  1064. default:
  1065. nonfree++;
  1066. continue;
  1067. }
  1068. mutex_enter(&block->mutex);
  1069. /* The following calls will temporarily
  1070. release block->mutex and buf_pool_mutex.
  1071. Therefore, we have to always retry,
  1072. even if !dirty && !nonfree. */
  1073. if (!buf_flush_ready_for_replace(&block->page)) {
  1074. buf_LRU_make_block_old(&block->page);
  1075. dirty++;
  1076. } else if (buf_LRU_free_block(&block->page, TRUE, NULL)
  1077. != BUF_LRU_FREED) {
  1078. nonfree++;
  1079. }
  1080. mutex_exit(&block->mutex);
  1081. }
  1082. buf_pool_mutex_exit();
  1083. /* Request for a flush of the chunk if it helps.
  1084. Do not flush if there are non-free blocks, since
  1085. flushing will not make the chunk freeable. */
  1086. if (nonfree) {
  1087. /* Avoid busy-waiting. */
  1088. os_thread_sleep(100000);
  1089. } else if (dirty
  1090. && buf_flush_batch(BUF_FLUSH_LRU, dirty, 0)
  1091. == ULINT_UNDEFINED) {
  1092. buf_flush_wait_batch_end(BUF_FLUSH_LRU);
  1093. }
  1094. goto try_again;
  1095. }
  1096. max_size = max_free_size;
  1097. max_chunk = max_free_chunk;
  1098. srv_buf_pool_old_size = srv_buf_pool_size;
  1099. /* Rewrite buf_pool->chunks. Copy everything but max_chunk. */
  1100. chunks = mem_alloc((buf_pool->n_chunks - 1) * sizeof *chunks);
  1101. memcpy(chunks, buf_pool->chunks,
  1102. (max_chunk - buf_pool->chunks) * sizeof *chunks);
  1103. memcpy(chunks + (max_chunk - buf_pool->chunks),
  1104. max_chunk + 1,
  1105. buf_pool->chunks + buf_pool->n_chunks
  1106. - (max_chunk + 1));
  1107. ut_a(buf_pool->curr_size > max_chunk->size);
  1108. buf_pool->curr_size -= max_chunk->size;
  1109. srv_buf_pool_curr_size = buf_pool->curr_size * UNIV_PAGE_SIZE;
  1110. chunk_size -= max_chunk->size;
  1111. buf_chunk_free(max_chunk);
  1112. mem_free(buf_pool->chunks);
  1113. buf_pool->chunks = chunks;
  1114. buf_pool->n_chunks--;
  1115. /* Allow a slack of one megabyte. */
  1116. if (chunk_size > 1048576 / UNIV_PAGE_SIZE) {
  1117. goto shrink_again;
  1118. }
  1119. func_done:
  1120. srv_buf_pool_old_size = srv_buf_pool_size;
  1121. func_exit:
  1122. buf_pool_mutex_exit();
  1123. btr_search_enable();
  1124. }
  1125. /********************************************************************//**
  1126. Rebuild buf_pool->page_hash. */
  1127. static
  1128. void
  1129. buf_pool_page_hash_rebuild(void)
  1130. /*============================*/
  1131. {
  1132. ulint i;
  1133. ulint n_chunks;
  1134. buf_chunk_t* chunk;
  1135. hash_table_t* page_hash;
  1136. hash_table_t* zip_hash;
  1137. buf_page_t* b;
  1138. buf_pool_mutex_enter();
  1139. /* Free, create, and populate the hash table. */
  1140. hash_table_free(buf_pool->page_hash);
  1141. buf_pool->page_hash = page_hash = hash_create(2 * buf_pool->curr_size);
  1142. zip_hash = hash_create(2 * buf_pool->curr_size);
  1143. HASH_MIGRATE(buf_pool->zip_hash, zip_hash, buf_page_t, hash,
  1144. BUF_POOL_ZIP_FOLD_BPAGE);
  1145. hash_table_free(buf_pool->zip_hash);
  1146. buf_pool->zip_hash = zip_hash;
  1147. /* Insert the uncompressed file pages to buf_pool->page_hash. */
  1148. chunk = buf_pool->chunks;
  1149. n_chunks = buf_pool->n_chunks;
  1150. for (i = 0; i < n_chunks; i++, chunk++) {
  1151. ulint j;
  1152. buf_block_t* block = chunk->blocks;
  1153. for (j = 0; j < chunk->size; j++, block++) {
  1154. if (buf_block_get_state(block)
  1155. == BUF_BLOCK_FILE_PAGE) {
  1156. ut_ad(!block->page.in_zip_hash);
  1157. ut_ad(block->page.in_page_hash);
  1158. HASH_INSERT(buf_page_t, hash, page_hash,
  1159. buf_page_address_fold(
  1160. block->page.space,
  1161. block->page.offset),
  1162. &block->page);
  1163. }
  1164. }
  1165. }
  1166. /* Insert the compressed-only pages to buf_pool->page_hash.
  1167. All such blocks are either in buf_pool->zip_clean or
  1168. in buf_pool->flush_list. */
  1169. for (b = UT_LIST_GET_FIRST(buf_pool->zip_clean); b;
  1170. b = UT_LIST_GET_NEXT(list, b)) {
  1171. ut_a(buf_page_get_state(b) == BUF_BLOCK_ZIP_PAGE);
  1172. ut_ad(!b->in_flush_list);
  1173. ut_ad(b->in_LRU_list);
  1174. ut_ad(b->in_page_hash);
  1175. ut_ad(!b->in_zip_hash);
  1176. HASH_INSERT(buf_page_t, hash, page_hash,
  1177. buf_page_address_fold(b->space, b->offset), b);
  1178. }
  1179. for (b = UT_LIST_GET_FIRST(buf_pool->flush_list); b;
  1180. b = UT_LIST_GET_NEXT(list, b)) {
  1181. ut_ad(b->in_flush_list);
  1182. ut_ad(b->in_LRU_list);
  1183. ut_ad(b->in_page_hash);
  1184. ut_ad(!b->in_zip_hash);
  1185. switch (buf_page_get_state(b)) {
  1186. case BUF_BLOCK_ZIP_DIRTY:
  1187. HASH_INSERT(buf_page_t, hash, page_hash,
  1188. buf_page_address_fold(b->space,
  1189. b->offset), b);
  1190. break;
  1191. case BUF_BLOCK_FILE_PAGE:
  1192. /* uncompressed page */
  1193. break;
  1194. case BUF_BLOCK_ZIP_FREE:
  1195. case BUF_BLOCK_ZIP_PAGE:
  1196. case BUF_BLOCK_NOT_USED:
  1197. case BUF_BLOCK_READY_FOR_USE:
  1198. case BUF_BLOCK_MEMORY:
  1199. case BUF_BLOCK_REMOVE_HASH:
  1200. ut_error;
  1201. break;
  1202. }
  1203. }
  1204. buf_pool_mutex_exit();
  1205. }
  1206. /********************************************************************//**
  1207. Resizes the buffer pool. */
  1208. UNIV_INTERN
  1209. void
  1210. buf_pool_resize(void)
  1211. /*=================*/
  1212. {
  1213. buf_pool_mutex_enter();
  1214. if (srv_buf_pool_old_size == srv_buf_pool_size) {
  1215. buf_pool_mutex_exit();
  1216. return;
  1217. }
  1218. if (srv_buf_pool_curr_size + 1048576 > srv_buf_pool_size) {
  1219. buf_pool_mutex_exit();
  1220. /* Disable adaptive hash indexes and empty the index
  1221. in order to free up memory in the buffer pool chunks. */
  1222. buf_pool_shrink((srv_buf_pool_curr_size - srv_buf_pool_size)
  1223. / UNIV_PAGE_SIZE);
  1224. } else if (srv_buf_pool_curr_size + 1048576 < srv_buf_pool_size) {
  1225. /* Enlarge the buffer pool by at least one megabyte */
  1226. ulint mem_size
  1227. = srv_buf_pool_size - srv_buf_pool_curr_size;
  1228. buf_chunk_t* chunks;
  1229. buf_chunk_t* chunk;
  1230. chunks = mem_alloc((buf_pool->n_chunks + 1) * sizeof *chunks);
  1231. memcpy(chunks, buf_pool->chunks, buf_pool->n_chunks
  1232. * sizeof *chunks);
  1233. chunk = &chunks[buf_pool->n_chunks];
  1234. if (!buf_chunk_init(chunk, mem_size)) {
  1235. mem_free(chunks);
  1236. } else {
  1237. buf_pool->curr_size += chunk->size;
  1238. srv_buf_pool_curr_size = buf_pool->curr_size
  1239. * UNIV_PAGE_SIZE;
  1240. mem_free(buf_pool->chunks);
  1241. buf_pool->chunks = chunks;
  1242. buf_pool->n_chunks++;
  1243. }
  1244. srv_buf_pool_old_size = srv_buf_pool_size;
  1245. buf_pool_mutex_exit();
  1246. }
  1247. buf_pool_page_hash_rebuild();
  1248. }
  1249. /********************************************************************//**
  1250. Moves a page to the start of the buffer pool LRU list. This high-level
  1251. function can be used to prevent an important page from slipping out of
  1252. the buffer pool. */
  1253. UNIV_INTERN
  1254. void
  1255. buf_page_make_young(
  1256. /*================*/
  1257. buf_page_t* bpage) /*!< in: buffer block of a file page */
  1258. {
  1259. buf_pool_mutex_enter();
  1260. ut_a(buf_page_in_file(bpage));
  1261. buf_LRU_make_block_young(bpage);
  1262. buf_pool_mutex_exit();
  1263. }
  1264. /********************************************************************//**
  1265. Sets the time of the first access of a page and moves a page to the
  1266. start of the buffer pool LRU list if it is too old. This high-level
  1267. function can be used to prevent an important page from slipping
  1268. out of the buffer pool. */
  1269. static
  1270. void
  1271. buf_page_set_accessed_make_young(
  1272. /*=============================*/
  1273. buf_page_t* bpage, /*!< in/out: buffer block of a
  1274. file page */
  1275. unsigned access_time) /*!< in: bpage->access_time
  1276. read under mutex protection,
  1277. or 0 if unknown */
  1278. {
  1279. ut_ad(!buf_pool_mutex_own());
  1280. ut_a(buf_page_in_file(bpage));
  1281. if (buf_page_peek_if_too_old(bpage)) {
  1282. buf_pool_mutex_enter();
  1283. buf_LRU_make_block_young(bpage);
  1284. buf_pool_mutex_exit();
  1285. } else if (!access_time) {
  1286. ulint time_ms = ut_time_ms();
  1287. buf_pool_mutex_enter();
  1288. buf_page_set_accessed(bpage, time_ms);
  1289. buf_pool_mutex_exit();
  1290. }
  1291. }
  1292. /********************************************************************//**
  1293. Resets the check_index_page_at_flush field of a page if found in the buffer
  1294. pool. */
  1295. UNIV_INTERN
  1296. void
  1297. buf_reset_check_index_page_at_flush(
  1298. /*================================*/
  1299. ulint space, /*!< in: space id */
  1300. ulint offset) /*!< in: page number */
  1301. {
  1302. buf_block_t* block;
  1303. buf_pool_mutex_enter();
  1304. block = (buf_block_t*) buf_page_hash_get(space, offset);
  1305. if (block && buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE) {
  1306. block->check_index_page_at_flush = FALSE;
  1307. }
  1308. buf_pool_mutex_exit();
  1309. }
  1310. /********************************************************************//**
  1311. Returns the current state of is_hashed of a page. FALSE if the page is
  1312. not in the pool. NOTE that this operation does not fix the page in the
  1313. pool if it is found there.
  1314. @return TRUE if page hash index is built in search system */
  1315. UNIV_INTERN
  1316. ibool
  1317. buf_page_peek_if_search_hashed(
  1318. /*===========================*/
  1319. ulint space, /*!< in: space id */
  1320. ulint offset) /*!< in: page number */
  1321. {
  1322. buf_block_t* block;
  1323. ibool is_hashed;
  1324. buf_pool_mutex_enter();
  1325. block = (buf_block_t*) buf_page_hash_get(space, offset);
  1326. if (!block || buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE) {
  1327. is_hashed = FALSE;
  1328. } else {
  1329. is_hashed = block->is_hashed;
  1330. }
  1331. buf_pool_mutex_exit();
  1332. return(is_hashed);
  1333. }
  1334. #ifdef UNIV_DEBUG_FILE_ACCESSES
  1335. /********************************************************************//**
  1336. Sets file_page_was_freed TRUE if the page is found in the buffer pool.
  1337. This function should be called when we free a file page and want the
  1338. debug version to check that it is not accessed any more unless
  1339. reallocated.
  1340. @return control block if found in page hash table, otherwise NULL */
  1341. UNIV_INTERN
  1342. buf_page_t*
  1343. buf_page_set_file_page_was_freed(
  1344. /*=============================*/
  1345. ulint space, /*!< in: space id */
  1346. ulint offset) /*!< in: page number */
  1347. {
  1348. buf_page_t* bpage;
  1349. buf_pool_mutex_enter();
  1350. bpage = buf_page_hash_get(space, offset);
  1351. if (bpage) {
  1352. bpage->file_page_was_freed = TRUE;
  1353. }
  1354. buf_pool_mutex_exit();
  1355. return(bpage);
  1356. }
  1357. /********************************************************************//**
  1358. Sets file_page_was_freed FALSE if the page is found in the buffer pool.
  1359. This function should be called when we free a file page and want the
  1360. debug version to check that it is not accessed any more unless
  1361. reallocated.
  1362. @return control block if found in page hash table, otherwise NULL */
  1363. UNIV_INTERN
  1364. buf_page_t*
  1365. buf_page_reset_file_page_was_freed(
  1366. /*===============================*/
  1367. ulint space, /*!< in: space id */
  1368. ulint offset) /*!< in: page number */
  1369. {
  1370. buf_page_t* bpage;
  1371. buf_pool_mutex_enter();
  1372. bpage = buf_page_hash_get(space, offset);
  1373. if (bpage) {
  1374. bpage->file_page_was_freed = FALSE;
  1375. }
  1376. buf_pool_mutex_exit();
  1377. return(bpage);
  1378. }
  1379. #endif /* UNIV_DEBUG_FILE_ACCESSES */
  1380. /********************************************************************//**
  1381. Get read access to a compressed page (usually of type
  1382. FIL_PAGE_TYPE_ZBLOB or FIL_PAGE_TYPE_ZBLOB2).
  1383. The page must be released with buf_page_release_zip().
  1384. NOTE: the page is not protected by any latch. Mutual exclusion has to
  1385. be implemented at a higher level. In other words, all possible
  1386. accesses to a given page through this function must be protected by
  1387. the same set of mutexes or latches.
  1388. @return pointer to the block */
  1389. UNIV_INTERN
  1390. buf_page_t*
  1391. buf_page_get_zip(
  1392. /*=============*/
  1393. ulint space, /*!< in: space id */
  1394. ulint zip_size,/*!< in: compressed page size */
  1395. ulint offset) /*!< in: page number */
  1396. {
  1397. buf_page_t* bpage;
  1398. mutex_t* block_mutex;
  1399. ibool must_read;
  1400. unsigned access_time;
  1401. #ifndef UNIV_LOG_DEBUG
  1402. ut_ad(!ibuf_inside());
  1403. #endif
  1404. buf_pool->stat.n_page_gets++;
  1405. for (;;) {
  1406. buf_pool_mutex_enter();
  1407. lookup:
  1408. bpage = buf_page_hash_get(space, offset);
  1409. if (bpage) {
  1410. break;
  1411. }
  1412. /* Page not in buf_pool: needs to be read from file */
  1413. buf_pool_mutex_exit();
  1414. buf_read_page(space, zip_size, offset);
  1415. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  1416. ut_a(++buf_dbg_counter % 37 || buf_validate());
  1417. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  1418. }
  1419. if (UNIV_UNLIKELY(!bpage->zip.data)) {
  1420. /* There is no compressed page. */
  1421. err_exit:
  1422. buf_pool_mutex_exit();
  1423. return(NULL);
  1424. }
  1425. switch (buf_page_get_state(bpage)) {
  1426. case BUF_BLOCK_NOT_USED:
  1427. case BUF_BLOCK_READY_FOR_USE:
  1428. case BUF_BLOCK_MEMORY:
  1429. case BUF_BLOCK_REMOVE_HASH:
  1430. case BUF_BLOCK_ZIP_FREE:
  1431. break;
  1432. case BUF_BLOCK_ZIP_PAGE:
  1433. case BUF_BLOCK_ZIP_DIRTY:
  1434. block_mutex = &buf_pool_zip_mutex;
  1435. mutex_enter(block_mutex);
  1436. bpage->buf_fix_count++;
  1437. goto got_block;
  1438. case BUF_BLOCK_FILE_PAGE:
  1439. block_mutex = &((buf_block_t*) bpage)->mutex;
  1440. mutex_enter(block_mutex);
  1441. /* Discard the uncompressed page frame if possible. */
  1442. if (buf_LRU_free_block(bpage, FALSE, NULL)
  1443. == BUF_LRU_FREED) {
  1444. mutex_exit(block_mutex);
  1445. goto lookup;
  1446. }
  1447. buf_block_buf_fix_inc((buf_block_t*) bpage,
  1448. __FILE__, __LINE__);
  1449. goto got_block;
  1450. }
  1451. ut_error;
  1452. goto err_exit;
  1453. got_block:
  1454. must_read = buf_page_get_io_fix(bpage) == BUF_IO_READ;
  1455. access_time = buf_page_is_accessed(bpage);
  1456. buf_pool_mutex_exit();
  1457. mutex_exit(block_mutex);
  1458. buf_page_set_accessed_make_young(bpage, access_time);
  1459. #ifdef UNIV_DEBUG_FILE_ACCESSES
  1460. ut_a(!bpage->file_page_was_freed);
  1461. #endif
  1462. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  1463. ut_a(++buf_dbg_counter % 5771 || buf_validate());
  1464. ut_a(bpage->buf_fix_count > 0);
  1465. ut_a(buf_page_in_file(bpage));
  1466. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  1467. if (must_read) {
  1468. /* Let us wait until the read operation
  1469. completes */
  1470. for (;;) {
  1471. enum buf_io_fix io_fix;
  1472. mutex_enter(block_mutex);
  1473. io_fix = buf_page_get_io_fix(bpage);
  1474. mutex_exit(block_mutex);
  1475. if (io_fix == BUF_IO_READ) {
  1476. os_thread_sleep(WAIT_FOR_READ);
  1477. } else {
  1478. break;
  1479. }
  1480. }
  1481. }
  1482. #ifdef UNIV_IBUF_COUNT_DEBUG
  1483. ut_a(ibuf_count_get(buf_page_get_space(bpage),
  1484. buf_page_get_page_no(bpage)) == 0);
  1485. #endif
  1486. return(bpage);
  1487. }
  1488. /********************************************************************//**
  1489. Initialize some fields of a control block. */
  1490. UNIV_INLINE
  1491. void
  1492. buf_block_init_low(
  1493. /*===============*/
  1494. buf_block_t* block) /*!< in: block to init */
  1495. {
  1496. block->check_index_page_at_flush = FALSE;
  1497. block->index = NULL;
  1498. block->n_hash_helps = 0;
  1499. block->is_hashed = FALSE;
  1500. block->n_fields = 1;
  1501. block->n_bytes = 0;
  1502. block->left_side = TRUE;
  1503. }
  1504. #endif /* !UNIV_HOTBACKUP */
  1505. /********************************************************************//**
  1506. Decompress a block.
  1507. @return TRUE if successful */
  1508. UNIV_INTERN
  1509. ibool
  1510. buf_zip_decompress(
  1511. /*===============*/
  1512. buf_block_t* block, /*!< in/out: block */
  1513. ibool check) /*!< in: TRUE=verify the page checksum */
  1514. {
  1515. const byte* frame = block->page.zip.data;
  1516. ut_ad(buf_block_get_zip_size(block));
  1517. ut_a(buf_block_get_space(block) != 0);
  1518. if (UNIV_LIKELY(check)) {
  1519. ulint stamp_checksum = mach_read_from_4(
  1520. frame + FIL_PAGE_SPACE_OR_CHKSUM);
  1521. ulint calc_checksum = page_zip_calc_checksum(
  1522. frame, page_zip_get_size(&block->page.zip));
  1523. if (UNIV_UNLIKELY(stamp_checksum != calc_checksum)) {
  1524. ut_print_timestamp(stderr);
  1525. fprintf(stderr,
  1526. " InnoDB: compressed page checksum mismatch"
  1527. " (space %u page %u): %lu != %lu\n",
  1528. block->page.space, block->page.offset,
  1529. stamp_checksum, calc_checksum);
  1530. return(FALSE);
  1531. }
  1532. }
  1533. switch (fil_page_get_type(frame)) {
  1534. case FIL_PAGE_INDEX:
  1535. if (page_zip_decompress(&block->page.zip,
  1536. block->frame, TRUE)) {
  1537. return(TRUE);
  1538. }
  1539. fprintf(stderr,
  1540. "InnoDB: unable to decompress space %lu page %lu\n",
  1541. (ulong) block->page.space,
  1542. (ulong) block->page.offset);
  1543. return(FALSE);
  1544. case FIL_PAGE_TYPE_ALLOCATED:
  1545. case FIL_PAGE_INODE:
  1546. case FIL_PAGE_IBUF_BITMAP:
  1547. case FIL_PAGE_TYPE_FSP_HDR:
  1548. case FIL_PAGE_TYPE_XDES:
  1549. case FIL_PAGE_TYPE_ZBLOB:
  1550. case FIL_PAGE_TYPE_ZBLOB2:
  1551. /* Copy to uncompressed storage. */
  1552. memcpy(block->frame, frame,
  1553. buf_block_get_zip_size(block));
  1554. return(TRUE);
  1555. }
  1556. ut_print_timestamp(stderr);
  1557. fprintf(stderr,
  1558. " InnoDB: unknown compressed page"
  1559. " type %lu\n",
  1560. fil_page_get_type(frame));
  1561. return(FALSE);
  1562. }
  1563. #ifndef UNIV_HOTBACKUP
  1564. /*******************************************************************//**
  1565. Gets the block to whose frame the pointer is pointing to.
  1566. @return pointer to block, never NULL */
  1567. UNIV_INTERN
  1568. buf_block_t*
  1569. buf_block_align(
  1570. /*============*/
  1571. const byte* ptr) /*!< in: pointer to a frame */
  1572. {
  1573. buf_chunk_t* chunk;
  1574. ulint i;
  1575. /* TODO: protect buf_pool->chunks with a mutex (it will
  1576. currently remain constant after buf_pool_init()) */
  1577. for (chunk = buf_pool->chunks, i = buf_pool->n_chunks; i--; chunk++) {
  1578. lint offs = ptr - chunk->blocks->frame;
  1579. if (UNIV_UNLIKELY(offs < 0)) {
  1580. continue;
  1581. }
  1582. offs >>= UNIV_PAGE_SIZE_SHIFT;
  1583. if (UNIV_LIKELY((ulint) offs < chunk->size)) {
  1584. buf_block_t* block = &chunk->blocks[offs];
  1585. /* The function buf_chunk_init() invokes
  1586. buf_block_init() so that block[n].frame ==
  1587. block->frame + n * UNIV_PAGE_SIZE. Check it. */
  1588. ut_ad(block->frame == page_align(ptr));
  1589. #ifdef UNIV_DEBUG
  1590. /* A thread that updates these fields must
  1591. hold buf_pool_mutex and block->mutex. Acquire
  1592. only the latter. */
  1593. mutex_enter(&block->mutex);
  1594. switch (buf_block_get_state(block)) {
  1595. case BUF_BLOCK_ZIP_FREE:
  1596. case BUF_BLOCK_ZIP_PAGE:
  1597. case BUF_BLOCK_ZIP_DIRTY:
  1598. /* These types should only be used in
  1599. the compressed buffer pool, whose
  1600. memory is allocated from
  1601. buf_pool->chunks, in UNIV_PAGE_SIZE
  1602. blocks flagged as BUF_BLOCK_MEMORY. */
  1603. ut_error;
  1604. break;
  1605. case BUF_BLOCK_NOT_USED:
  1606. case BUF_BLOCK_READY_FOR_USE:
  1607. case BUF_BLOCK_MEMORY:
  1608. /* Some data structures contain
  1609. "guess" pointers to file pages. The
  1610. file pages may have been freed and
  1611. reused. Do not complain. */
  1612. break;
  1613. case BUF_BLOCK_REMOVE_HASH:
  1614. /* buf_LRU_block_remove_hashed_page()
  1615. will overwrite the FIL_PAGE_OFFSET and
  1616. FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID with
  1617. 0xff and set the state to
  1618. BUF_BLOCK_REMOVE_HASH. */
  1619. ut_ad(page_get_space_id(page_align(ptr))
  1620. == 0xffffffff);
  1621. ut_ad(page_get_page_no(page_align(ptr))
  1622. == 0xffffffff);
  1623. break;
  1624. case BUF_BLOCK_FILE_PAGE:
  1625. ut_ad(block->page.space
  1626. == page_get_space_id(page_align(ptr)));
  1627. ut_ad(block->page.offset
  1628. == page_get_page_no(page_align(ptr)));
  1629. break;
  1630. }
  1631. mutex_exit(&block->mutex);
  1632. #endif /* UNIV_DEBUG */
  1633. return(block);
  1634. }
  1635. }
  1636. /* The block should always be found. */
  1637. ut_error;
  1638. return(NULL);
  1639. }
  1640. /********************************************************************//**
  1641. Find out if a pointer belongs to a buf_block_t. It can be a pointer to
  1642. the buf_block_t itself or a member of it
  1643. @return TRUE if ptr belongs to a buf_block_t struct */
  1644. UNIV_INTERN
  1645. ibool
  1646. buf_pointer_is_block_field(
  1647. /*=======================*/
  1648. const void* ptr) /*!< in: pointer not
  1649. dereferenced */
  1650. {
  1651. const buf_chunk_t* chunk = buf_pool->chunks;
  1652. const buf_chunk_t* const echunk = chunk + buf_pool->n_chunks;
  1653. /* TODO: protect buf_pool->chunks with a mutex (it will
  1654. currently remain constant after buf_pool_init()) */
  1655. while (chunk < echunk) {
  1656. if (ptr >= (void *)chunk->blocks
  1657. && ptr < (void *)(chunk->blocks + chunk->size)) {
  1658. return(TRUE);
  1659. }
  1660. chunk++;
  1661. }
  1662. return(FALSE);
  1663. }
  1664. /********************************************************************//**
  1665. Find out if a buffer block was created by buf_chunk_init().
  1666. @return TRUE if "block" has been added to buf_pool->free by buf_chunk_init() */
  1667. static
  1668. ibool
  1669. buf_block_is_uncompressed(
  1670. /*======================*/
  1671. const buf_block_t* block) /*!< in: pointer to block,
  1672. not dereferenced */
  1673. {
  1674. ut_ad(buf_pool_mutex_own());
  1675. if (UNIV_UNLIKELY((((ulint) block) % sizeof *block) != 0)) {
  1676. /* The pointer should be aligned. */
  1677. return(FALSE);
  1678. }
  1679. return(buf_pointer_is_block_field((void *)block));
  1680. }
  1681. /********************************************************************//**
  1682. This is the general function used to get access to a database page.
  1683. @return pointer to the block or NULL */
  1684. UNIV_INTERN
  1685. buf_block_t*
  1686. buf_page_get_gen(
  1687. /*=============*/
  1688. ulint space, /*!< in: space id */
  1689. ulint zip_size,/*!< in: compressed page size in bytes
  1690. or 0 for uncompressed pages */
  1691. ulint offset, /*!< in: page number */
  1692. ulint rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
  1693. buf_block_t* guess, /*!< in: guessed block or NULL */
  1694. ulint mode, /*!< in: BUF_GET, BUF_GET_IF_IN_POOL,
  1695. BUF_GET_NO_LATCH */
  1696. const char* file, /*!< in: file name */
  1697. ulint line, /*!< in: line where called */
  1698. mtr_t* mtr) /*!< in: mini-transaction */
  1699. {
  1700. buf_block_t* block;
  1701. unsigned access_time;
  1702. ulint fix_type;
  1703. ibool must_read;
  1704. ut_ad(mtr);
  1705. ut_ad((rw_latch == RW_S_LATCH)
  1706. || (rw_latch == RW_X_LATCH)
  1707. || (rw_latch == RW_NO_LATCH));
  1708. ut_ad((mode != BUF_GET_NO_LATCH) || (rw_latch == RW_NO_LATCH));
  1709. ut_ad((mode == BUF_GET) || (mode == BUF_GET_IF_IN_POOL)
  1710. || (mode == BUF_GET_NO_LATCH));
  1711. ut_ad(zip_size == fil_space_get_zip_size(space));
  1712. ut_ad(ut_is_2pow(zip_size));
  1713. #ifndef UNIV_LOG_DEBUG
  1714. ut_ad(!ibuf_inside() || ibuf_page(space, zip_size, offset, NULL));
  1715. #endif
  1716. buf_pool->stat.n_page_gets++;
  1717. loop:
  1718. block = guess;
  1719. buf_pool_mutex_enter();
  1720. if (block) {
  1721. /* If the guess is a compressed page descriptor that
  1722. has been allocated by buf_buddy_alloc(), it may have
  1723. been invalidated by buf_buddy_relocate(). In that
  1724. case, block could point to something that happens to
  1725. contain the expected bits in block->page. Similarly,
  1726. the guess may be pointing to a buffer pool chunk that
  1727. has been released when resizing the buffer pool. */
  1728. if (!buf_block_is_uncompressed(block)
  1729. || offset != block->page.offset
  1730. || space != block->page.space
  1731. || buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE) {
  1732. block = guess = NULL;
  1733. } else {
  1734. ut_ad(!block->page.in_zip_hash);
  1735. ut_ad(block->page.in_page_hash);
  1736. }
  1737. }
  1738. if (block == NULL) {
  1739. block = (buf_block_t*) buf_page_hash_get(space, offset);
  1740. }
  1741. loop2:
  1742. if (block == NULL) {
  1743. /* Page not in buf_pool: needs to be read from file */
  1744. buf_pool_mutex_exit();
  1745. if (mode == BUF_GET_IF_IN_POOL) {
  1746. return(NULL);
  1747. }
  1748. buf_read_page(space, zip_size, offset);
  1749. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  1750. ut_a(++buf_dbg_counter % 37 || buf_validate());
  1751. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  1752. goto loop;
  1753. }
  1754. ut_ad(page_zip_get_size(&block->page.zip) == zip_size);
  1755. must_read = buf_block_get_io_fix(block) == BUF_IO_READ;
  1756. if (must_read && mode == BUF_GET_IF_IN_POOL) {
  1757. /* The page is only being read to buffer */
  1758. buf_pool_mutex_exit();
  1759. return(NULL);
  1760. }
  1761. switch (buf_block_get_state(block)) {
  1762. buf_page_t* bpage;
  1763. ibool success;
  1764. case BUF_BLOCK_FILE_PAGE:
  1765. break;
  1766. case BUF_BLOCK_ZIP_PAGE:
  1767. case BUF_BLOCK_ZIP_DIRTY:
  1768. bpage = &block->page;
  1769. /* Protect bpage->buf_fix_count. */
  1770. mutex_enter(&buf_pool_zip_mutex);
  1771. if (bpage->buf_fix_count
  1772. || buf_page_get_io_fix(bpage) != BUF_IO_NONE) {
  1773. /* This condition often occurs when the buffer
  1774. is not buffer-fixed, but I/O-fixed by
  1775. buf_page_init_for_read(). */
  1776. mutex_exit(&buf_pool_zip_mutex);
  1777. wait_until_unfixed:
  1778. /* The block is buffer-fixed or I/O-fixed.
  1779. Try again later. */
  1780. buf_pool_mutex_exit();
  1781. os_thread_sleep(WAIT_FOR_READ);
  1782. goto loop;
  1783. }
  1784. /* Allocate an uncompressed page. */
  1785. buf_pool_mutex_exit();
  1786. mutex_exit(&buf_pool_zip_mutex);
  1787. block = buf_LRU_get_free_block(0);
  1788. ut_a(block);
  1789. buf_pool_mutex_enter();
  1790. mutex_enter(&block->mutex);
  1791. {
  1792. buf_page_t* hash_bpage
  1793. = buf_page_hash_get(space, offset);
  1794. if (UNIV_UNLIKELY(bpage != hash_bpage)) {
  1795. /* The buf_pool->page_hash was modified
  1796. while buf_pool_mutex was released.
  1797. Free the block that was allocated. */
  1798. buf_LRU_block_free_non_file_page(block);
  1799. mutex_exit(&block->mutex);
  1800. block = (buf_block_t*) hash_bpage;
  1801. goto loop2;
  1802. }
  1803. }
  1804. if (UNIV_UNLIKELY
  1805. (bpage->buf_fix_count
  1806. || buf_page_get_io_fix(bpage) != BUF_IO_NONE)) {
  1807. /* The block was buffer-fixed or I/O-fixed
  1808. while buf_pool_mutex was not held by this thread.
  1809. Free the block that was allocated and try again.
  1810. This should be extremely unlikely. */
  1811. buf_LRU_block_free_non_file_page(block);
  1812. mutex_exit(&block->mutex);
  1813. goto wait_until_unfixed;
  1814. }
  1815. /* Move the compressed page from bpage to block,
  1816. and uncompress it. */
  1817. mutex_enter(&buf_pool_zip_mutex);
  1818. buf_relocate(bpage, &block->page);
  1819. buf_block_init_low(block);
  1820. block->lock_hash_val = lock_rec_hash(space, offset);
  1821. UNIV_MEM_DESC(&block->page.zip.data,
  1822. page_zip_get_size(&block->page.zip), block);
  1823. if (buf_page_get_state(&block->page)
  1824. == BUF_BLOCK_ZIP_PAGE) {
  1825. UT_LIST_REMOVE(list, buf_pool->zip_clean,
  1826. &block->page);
  1827. ut_ad(!block->page.in_flush_list);
  1828. } else {
  1829. /* Relocate buf_pool->flush_list. */
  1830. buf_page_t* b;
  1831. b = UT_LIST_GET_PREV(list, &block->page);
  1832. ut_ad(block->page.in_flush_list);
  1833. UT_LIST_REMOVE(list, buf_pool->flush_list,
  1834. &block->page);
  1835. if (b) {
  1836. UT_LIST_INSERT_AFTER(
  1837. list, buf_pool->flush_list, b,
  1838. &block->page);
  1839. } else {
  1840. UT_LIST_ADD_FIRST(
  1841. list, buf_pool->flush_list,
  1842. &block->page);
  1843. }
  1844. }
  1845. /* Buffer-fix, I/O-fix, and X-latch the block
  1846. for the duration of the decompression.
  1847. Also add the block to the unzip_LRU list. */
  1848. block->page.state = BUF_BLOCK_FILE_PAGE;
  1849. /* Insert at the front of unzip_LRU list */
  1850. buf_unzip_LRU_add_block(block, FALSE);
  1851. block->page.buf_fix_count = 1;
  1852. buf_block_set_io_fix(block, BUF_IO_READ);
  1853. rw_lock_x_lock(&block->lock);
  1854. mutex_exit(&block->mutex);
  1855. mutex_exit(&buf_pool_zip_mutex);
  1856. buf_pool->n_pend_unzip++;
  1857. buf_buddy_free(bpage, sizeof *bpage);
  1858. buf_pool_mutex_exit();
  1859. /* Decompress the page and apply buffered operations
  1860. while not holding buf_pool_mutex or block->mutex. */
  1861. success = buf_zip_decompress(block, srv_use_checksums);
  1862. if (UNIV_LIKELY(success)) {
  1863. ibuf_merge_or_delete_for_page(block, space, offset,
  1864. zip_size, TRUE);
  1865. }
  1866. /* Unfix and unlatch the block. */
  1867. buf_pool_mutex_enter();
  1868. mutex_enter(&block->mutex);
  1869. block->page.buf_fix_count--;
  1870. buf_block_set_io_fix(block, BUF_IO_NONE);
  1871. mutex_exit(&block->mutex);
  1872. buf_pool->n_pend_unzip--;
  1873. rw_lock_x_unlock(&block->lock);
  1874. if (UNIV_UNLIKELY(!success)) {
  1875. buf_pool_mutex_exit();
  1876. return(NULL);
  1877. }
  1878. break;
  1879. case BUF_BLOCK_ZIP_FREE:
  1880. case BUF_BLOCK_NOT_USED:
  1881. case BUF_BLOCK_READY_FOR_USE:
  1882. case BUF_BLOCK_MEMORY:
  1883. case BUF_BLOCK_REMOVE_HASH:
  1884. ut_error;
  1885. break;
  1886. }
  1887. ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  1888. mutex_enter(&block->mutex);
  1889. UNIV_MEM_ASSERT_RW(&block->page, sizeof block->page);
  1890. buf_block_buf_fix_inc(block, file, line);
  1891. mutex_exit(&block->mutex);
  1892. /* Check if this is the first access to the page */
  1893. access_time = buf_page_is_accessed(&block->page);
  1894. buf_pool_mutex_exit();
  1895. buf_page_set_accessed_make_young(&block->page, access_time);
  1896. #ifdef UNIV_DEBUG_FILE_ACCESSES
  1897. ut_a(!block->page.file_page_was_freed);
  1898. #endif
  1899. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  1900. ut_a(++buf_dbg_counter % 5771 || buf_validate());
  1901. ut_a(block->page.buf_fix_count > 0);
  1902. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  1903. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  1904. switch (rw_latch) {
  1905. case RW_NO_LATCH:
  1906. if (must_read) {
  1907. /* Let us wait until the read operation
  1908. completes */
  1909. for (;;) {
  1910. enum buf_io_fix io_fix;
  1911. mutex_enter(&block->mutex);
  1912. io_fix = buf_block_get_io_fix(block);
  1913. mutex_exit(&block->mutex);
  1914. if (io_fix == BUF_IO_READ) {
  1915. os_thread_sleep(WAIT_FOR_READ);
  1916. } else {
  1917. break;
  1918. }
  1919. }
  1920. }
  1921. fix_type = MTR_MEMO_BUF_FIX;
  1922. break;
  1923. case RW_S_LATCH:
  1924. rw_lock_s_lock_func(&(block->lock), 0, file, line);
  1925. fix_type = MTR_MEMO_PAGE_S_FIX;
  1926. break;
  1927. default:
  1928. ut_ad(rw_latch == RW_X_LATCH);
  1929. rw_lock_x_lock_func(&(block->lock), 0, file, line);
  1930. fix_type = MTR_MEMO_PAGE_X_FIX;
  1931. break;
  1932. }
  1933. mtr_memo_push(mtr, block, fix_type);
  1934. if (!access_time) {
  1935. /* In the case of a first access, try to apply linear
  1936. read-ahead */
  1937. buf_read_ahead_linear(space, zip_size, offset);
  1938. }
  1939. #ifdef UNIV_IBUF_COUNT_DEBUG
  1940. ut_a(ibuf_count_get(buf_block_get_space(block),
  1941. buf_block_get_page_no(block)) == 0);
  1942. #endif
  1943. return(block);
  1944. }
  1945. /********************************************************************//**
  1946. This is the general function used to get optimistic access to a database
  1947. page.
  1948. @return TRUE if success */
  1949. UNIV_INTERN
  1950. ibool
  1951. buf_page_optimistic_get_func(
  1952. /*=========================*/
  1953. ulint rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH */
  1954. buf_block_t* block, /*!< in: guessed buffer block */
  1955. ib_uint64_t modify_clock,/*!< in: modify clock value if mode is
  1956. ..._GUESS_ON_CLOCK */
  1957. const char* file, /*!< in: file name */
  1958. ulint line, /*!< in: line where called */
  1959. mtr_t* mtr) /*!< in: mini-transaction */
  1960. {
  1961. unsigned access_time;
  1962. ibool success;
  1963. ulint fix_type;
  1964. ut_ad(mtr && block);
  1965. ut_ad((rw_latch == RW_S_LATCH) || (rw_latch == RW_X_LATCH));
  1966. mutex_enter(&block->mutex);
  1967. if (UNIV_UNLIKELY(buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE)) {
  1968. mutex_exit(&block->mutex);
  1969. return(FALSE);
  1970. }
  1971. buf_block_buf_fix_inc(block, file, line);
  1972. mutex_exit(&block->mutex);
  1973. /* Check if this is the first access to the page.
  1974. We do a dirty read on purpose, to avoid mutex contention.
  1975. This field is only used for heuristic purposes; it does not
  1976. affect correctness. */
  1977. access_time = buf_page_is_accessed(&block->page);
  1978. buf_page_set_accessed_make_young(&block->page, access_time);
  1979. ut_ad(!ibuf_inside()
  1980. || ibuf_page(buf_block_get_space(block),
  1981. buf_block_get_zip_size(block),
  1982. buf_block_get_page_no(block), NULL));
  1983. if (rw_latch == RW_S_LATCH) {
  1984. success = rw_lock_s_lock_nowait(&(block->lock),
  1985. file, line);
  1986. fix_type = MTR_MEMO_PAGE_S_FIX;
  1987. } else {
  1988. success = rw_lock_x_lock_func_nowait(&(block->lock),
  1989. file, line);
  1990. fix_type = MTR_MEMO_PAGE_X_FIX;
  1991. }
  1992. if (UNIV_UNLIKELY(!success)) {
  1993. mutex_enter(&block->mutex);
  1994. buf_block_buf_fix_dec(block);
  1995. mutex_exit(&block->mutex);
  1996. return(FALSE);
  1997. }
  1998. if (UNIV_UNLIKELY(modify_clock != block->modify_clock)) {
  1999. buf_block_dbg_add_level(block, SYNC_NO_ORDER_CHECK);
  2000. if (rw_latch == RW_S_LATCH) {
  2001. rw_lock_s_unlock(&(block->lock));
  2002. } else {
  2003. rw_lock_x_unlock(&(block->lock));
  2004. }
  2005. mutex_enter(&block->mutex);
  2006. buf_block_buf_fix_dec(block);
  2007. mutex_exit(&block->mutex);
  2008. return(FALSE);
  2009. }
  2010. mtr_memo_push(mtr, block, fix_type);
  2011. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2012. ut_a(++buf_dbg_counter % 5771 || buf_validate());
  2013. ut_a(block->page.buf_fix_count > 0);
  2014. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  2015. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  2016. #ifdef UNIV_DEBUG_FILE_ACCESSES
  2017. ut_a(block->page.file_page_was_freed == FALSE);
  2018. #endif
  2019. if (UNIV_UNLIKELY(!access_time)) {
  2020. /* In the case of a first access, try to apply linear
  2021. read-ahead */
  2022. buf_read_ahead_linear(buf_block_get_space(block),
  2023. buf_block_get_zip_size(block),
  2024. buf_block_get_page_no(block));
  2025. }
  2026. #ifdef UNIV_IBUF_COUNT_DEBUG
  2027. ut_a(ibuf_count_get(buf_block_get_space(block),
  2028. buf_block_get_page_no(block)) == 0);
  2029. #endif
  2030. buf_pool->stat.n_page_gets++;
  2031. return(TRUE);
  2032. }
  2033. /********************************************************************//**
  2034. This is used to get access to a known database page, when no waiting can be
  2035. done. For example, if a search in an adaptive hash index leads us to this
  2036. frame.
  2037. @return TRUE if success */
  2038. UNIV_INTERN
  2039. ibool
  2040. buf_page_get_known_nowait(
  2041. /*======================*/
  2042. ulint rw_latch,/*!< in: RW_S_LATCH, RW_X_LATCH */
  2043. buf_block_t* block, /*!< in: the known page */
  2044. ulint mode, /*!< in: BUF_MAKE_YOUNG or BUF_KEEP_OLD */
  2045. const char* file, /*!< in: file name */
  2046. ulint line, /*!< in: line where called */
  2047. mtr_t* mtr) /*!< in: mini-transaction */
  2048. {
  2049. ibool success;
  2050. ulint fix_type;
  2051. ut_ad(mtr);
  2052. ut_ad((rw_latch == RW_S_LATCH) || (rw_latch == RW_X_LATCH));
  2053. mutex_enter(&block->mutex);
  2054. if (buf_block_get_state(block) == BUF_BLOCK_REMOVE_HASH) {
  2055. /* Another thread is just freeing the block from the LRU list
  2056. of the buffer pool: do not try to access this page; this
  2057. attempt to access the page can only come through the hash
  2058. index because when the buffer block state is ..._REMOVE_HASH,
  2059. we have already removed it from the page address hash table
  2060. of the buffer pool. */
  2061. mutex_exit(&block->mutex);
  2062. return(FALSE);
  2063. }
  2064. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  2065. buf_block_buf_fix_inc(block, file, line);
  2066. mutex_exit(&block->mutex);
  2067. if (mode == BUF_MAKE_YOUNG && buf_page_peek_if_too_old(&block->page)) {
  2068. buf_pool_mutex_enter();
  2069. buf_LRU_make_block_young(&block->page);
  2070. buf_pool_mutex_exit();
  2071. } else if (!buf_page_is_accessed(&block->page)) {
  2072. /* Above, we do a dirty read on purpose, to avoid
  2073. mutex contention. The field buf_page_t::access_time
  2074. is only used for heuristic purposes. Writes to the
  2075. field must be protected by mutex, however. */
  2076. ulint time_ms = ut_time_ms();
  2077. buf_pool_mutex_enter();
  2078. buf_page_set_accessed(&block->page, time_ms);
  2079. buf_pool_mutex_exit();
  2080. }
  2081. ut_ad(!ibuf_inside() || (mode == BUF_KEEP_OLD));
  2082. if (rw_latch == RW_S_LATCH) {
  2083. success = rw_lock_s_lock_nowait(&(block->lock),
  2084. file, line);
  2085. fix_type = MTR_MEMO_PAGE_S_FIX;
  2086. } else {
  2087. success = rw_lock_x_lock_func_nowait(&(block->lock),
  2088. file, line);
  2089. fix_type = MTR_MEMO_PAGE_X_FIX;
  2090. }
  2091. if (!success) {
  2092. mutex_enter(&block->mutex);
  2093. buf_block_buf_fix_dec(block);
  2094. mutex_exit(&block->mutex);
  2095. return(FALSE);
  2096. }
  2097. mtr_memo_push(mtr, block, fix_type);
  2098. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2099. ut_a(++buf_dbg_counter % 5771 || buf_validate());
  2100. ut_a(block->page.buf_fix_count > 0);
  2101. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  2102. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  2103. #ifdef UNIV_DEBUG_FILE_ACCESSES
  2104. ut_a(block->page.file_page_was_freed == FALSE);
  2105. #endif
  2106. #ifdef UNIV_IBUF_COUNT_DEBUG
  2107. ut_a((mode == BUF_KEEP_OLD)
  2108. || (ibuf_count_get(buf_block_get_space(block),
  2109. buf_block_get_page_no(block)) == 0));
  2110. #endif
  2111. buf_pool->stat.n_page_gets++;
  2112. return(TRUE);
  2113. }
  2114. /*******************************************************************//**
  2115. Given a tablespace id and page number tries to get that page. If the
  2116. page is not in the buffer pool it is not loaded and NULL is returned.
  2117. Suitable for using when holding the kernel mutex.
  2118. @return pointer to a page or NULL */
  2119. UNIV_INTERN
  2120. const buf_block_t*
  2121. buf_page_try_get_func(
  2122. /*==================*/
  2123. ulint space_id,/*!< in: tablespace id */
  2124. ulint page_no,/*!< in: page number */
  2125. const char* file, /*!< in: file name */
  2126. ulint line, /*!< in: line where called */
  2127. mtr_t* mtr) /*!< in: mini-transaction */
  2128. {
  2129. buf_block_t* block;
  2130. ibool success;
  2131. ulint fix_type;
  2132. buf_pool_mutex_enter();
  2133. block = buf_block_hash_get(space_id, page_no);
  2134. if (!block) {
  2135. buf_pool_mutex_exit();
  2136. return(NULL);
  2137. }
  2138. mutex_enter(&block->mutex);
  2139. buf_pool_mutex_exit();
  2140. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2141. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  2142. ut_a(buf_block_get_space(block) == space_id);
  2143. ut_a(buf_block_get_page_no(block) == page_no);
  2144. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  2145. buf_block_buf_fix_inc(block, file, line);
  2146. mutex_exit(&block->mutex);
  2147. fix_type = MTR_MEMO_PAGE_S_FIX;
  2148. success = rw_lock_s_lock_nowait(&block->lock, file, line);
  2149. if (!success) {
  2150. /* Let us try to get an X-latch. If the current thread
  2151. is holding an X-latch on the page, we cannot get an
  2152. S-latch. */
  2153. fix_type = MTR_MEMO_PAGE_X_FIX;
  2154. success = rw_lock_x_lock_func_nowait(&block->lock,
  2155. file, line);
  2156. }
  2157. if (!success) {
  2158. mutex_enter(&block->mutex);
  2159. buf_block_buf_fix_dec(block);
  2160. mutex_exit(&block->mutex);
  2161. return(NULL);
  2162. }
  2163. mtr_memo_push(mtr, block, fix_type);
  2164. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2165. ut_a(++buf_dbg_counter % 5771 || buf_validate());
  2166. ut_a(block->page.buf_fix_count > 0);
  2167. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  2168. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  2169. #ifdef UNIV_DEBUG_FILE_ACCESSES
  2170. ut_a(block->page.file_page_was_freed == FALSE);
  2171. #endif /* UNIV_DEBUG_FILE_ACCESSES */
  2172. buf_block_dbg_add_level(block, SYNC_NO_ORDER_CHECK);
  2173. buf_pool->stat.n_page_gets++;
  2174. #ifdef UNIV_IBUF_COUNT_DEBUG
  2175. ut_a(ibuf_count_get(buf_block_get_space(block),
  2176. buf_block_get_page_no(block)) == 0);
  2177. #endif
  2178. return(block);
  2179. }
  2180. /********************************************************************//**
  2181. Initialize some fields of a control block. */
  2182. UNIV_INLINE
  2183. void
  2184. buf_page_init_low(
  2185. /*==============*/
  2186. buf_page_t* bpage) /*!< in: block to init */
  2187. {
  2188. bpage->flush_type = BUF_FLUSH_LRU;
  2189. bpage->io_fix = BUF_IO_NONE;
  2190. bpage->buf_fix_count = 0;
  2191. bpage->freed_page_clock = 0;
  2192. bpage->access_time = 0;
  2193. bpage->newest_modification = 0;
  2194. bpage->oldest_modification = 0;
  2195. HASH_INVALIDATE(bpage, hash);
  2196. #ifdef UNIV_DEBUG_FILE_ACCESSES
  2197. bpage->file_page_was_freed = FALSE;
  2198. #endif /* UNIV_DEBUG_FILE_ACCESSES */
  2199. }
  2200. /********************************************************************//**
  2201. Inits a page to the buffer buf_pool. */
  2202. static
  2203. void
  2204. buf_page_init(
  2205. /*==========*/
  2206. ulint space, /*!< in: space id */
  2207. ulint offset, /*!< in: offset of the page within space
  2208. in units of a page */
  2209. buf_block_t* block) /*!< in: block to init */
  2210. {
  2211. buf_page_t* hash_page;
  2212. ut_ad(buf_pool_mutex_own());
  2213. ut_ad(mutex_own(&(block->mutex)));
  2214. ut_a(buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE);
  2215. /* Set the state of the block */
  2216. buf_block_set_file_page(block, space, offset);
  2217. #ifdef UNIV_DEBUG_VALGRIND
  2218. if (!space) {
  2219. /* Silence valid Valgrind warnings about uninitialized
  2220. data being written to data files. There are some unused
  2221. bytes on some pages that InnoDB does not initialize. */
  2222. UNIV_MEM_VALID(block->frame, UNIV_PAGE_SIZE);
  2223. }
  2224. #endif /* UNIV_DEBUG_VALGRIND */
  2225. buf_block_init_low(block);
  2226. block->lock_hash_val = lock_rec_hash(space, offset);
  2227. /* Insert into the hash table of file pages */
  2228. hash_page = buf_page_hash_get(space, offset);
  2229. if (UNIV_LIKELY_NULL(hash_page)) {
  2230. fprintf(stderr,
  2231. "InnoDB: Error: page %lu %lu already found"
  2232. " in the hash table: %p, %p\n",
  2233. (ulong) space,
  2234. (ulong) offset,
  2235. (const void*) hash_page, (const void*) block);
  2236. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2237. mutex_exit(&block->mutex);
  2238. buf_pool_mutex_exit();
  2239. buf_print();
  2240. buf_LRU_print();
  2241. buf_validate();
  2242. buf_LRU_validate();
  2243. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  2244. ut_error;
  2245. }
  2246. buf_page_init_low(&block->page);
  2247. ut_ad(!block->page.in_zip_hash);
  2248. ut_ad(!block->page.in_page_hash);
  2249. ut_d(block->page.in_page_hash = TRUE);
  2250. HASH_INSERT(buf_page_t, hash, buf_pool->page_hash,
  2251. buf_page_address_fold(space, offset), &block->page);
  2252. }
  2253. /********************************************************************//**
  2254. Function which inits a page for read to the buffer buf_pool. If the page is
  2255. (1) already in buf_pool, or
  2256. (2) if we specify to read only ibuf pages and the page is not an ibuf page, or
  2257. (3) if the space is deleted or being deleted,
  2258. then this function does nothing.
  2259. Sets the io_fix flag to BUF_IO_READ and sets a non-recursive exclusive lock
  2260. on the buffer frame. The io-handler must take care that the flag is cleared
  2261. and the lock released later.
  2262. @return pointer to the block or NULL */
  2263. UNIV_INTERN
  2264. buf_page_t*
  2265. buf_page_init_for_read(
  2266. /*===================*/
  2267. ulint* err, /*!< out: DB_SUCCESS or DB_TABLESPACE_DELETED */
  2268. ulint mode, /*!< in: BUF_READ_IBUF_PAGES_ONLY, ... */
  2269. ulint space, /*!< in: space id */
  2270. ulint zip_size,/*!< in: compressed page size, or 0 */
  2271. ibool unzip, /*!< in: TRUE=request uncompressed page */
  2272. ib_int64_t tablespace_version,/*!< in: prevents reading from a wrong
  2273. version of the tablespace in case we have done
  2274. DISCARD + IMPORT */
  2275. ulint offset) /*!< in: page number */
  2276. {
  2277. buf_block_t* block;
  2278. buf_page_t* bpage;
  2279. mtr_t mtr;
  2280. ibool lru = FALSE;
  2281. void* data;
  2282. ut_ad(buf_pool);
  2283. *err = DB_SUCCESS;
  2284. if (mode == BUF_READ_IBUF_PAGES_ONLY) {
  2285. /* It is a read-ahead within an ibuf routine */
  2286. ut_ad(!ibuf_bitmap_page(zip_size, offset));
  2287. ut_ad(ibuf_inside());
  2288. mtr_start(&mtr);
  2289. if (!recv_no_ibuf_operations
  2290. && !ibuf_page(space, zip_size, offset, &mtr)) {
  2291. mtr_commit(&mtr);
  2292. return(NULL);
  2293. }
  2294. } else {
  2295. ut_ad(mode == BUF_READ_ANY_PAGE);
  2296. }
  2297. if (zip_size && UNIV_LIKELY(!unzip)
  2298. && UNIV_LIKELY(!recv_recovery_is_on())) {
  2299. block = NULL;
  2300. } else {
  2301. block = buf_LRU_get_free_block(0);
  2302. ut_ad(block);
  2303. }
  2304. buf_pool_mutex_enter();
  2305. if (buf_page_hash_get(space, offset)) {
  2306. /* The page is already in the buffer pool. */
  2307. err_exit:
  2308. if (block) {
  2309. mutex_enter(&block->mutex);
  2310. buf_LRU_block_free_non_file_page(block);
  2311. mutex_exit(&block->mutex);
  2312. }
  2313. bpage = NULL;
  2314. goto func_exit;
  2315. }
  2316. if (fil_tablespace_deleted_or_being_deleted_in_mem(
  2317. space, tablespace_version)) {
  2318. /* The page belongs to a space which has been
  2319. deleted or is being deleted. */
  2320. *err = DB_TABLESPACE_DELETED;
  2321. goto err_exit;
  2322. }
  2323. if (block) {
  2324. bpage = &block->page;
  2325. mutex_enter(&block->mutex);
  2326. buf_page_init(space, offset, block);
  2327. /* The block must be put to the LRU list, to the old blocks */
  2328. buf_LRU_add_block(bpage, TRUE/* to old blocks */);
  2329. /* We set a pass-type x-lock on the frame because then
  2330. the same thread which called for the read operation
  2331. (and is running now at this point of code) can wait
  2332. for the read to complete by waiting for the x-lock on
  2333. the frame; if the x-lock were recursive, the same
  2334. thread would illegally get the x-lock before the page
  2335. read is completed. The x-lock is cleared by the
  2336. io-handler thread. */
  2337. rw_lock_x_lock_gen(&block->lock, BUF_IO_READ);
  2338. buf_page_set_io_fix(bpage, BUF_IO_READ);
  2339. if (UNIV_UNLIKELY(zip_size)) {
  2340. page_zip_set_size(&block->page.zip, zip_size);
  2341. /* buf_pool_mutex may be released and
  2342. reacquired by buf_buddy_alloc(). Thus, we
  2343. must release block->mutex in order not to
  2344. break the latching order in the reacquisition
  2345. of buf_pool_mutex. We also must defer this
  2346. operation until after the block descriptor has
  2347. been added to buf_pool->LRU and
  2348. buf_pool->page_hash. */
  2349. mutex_exit(&block->mutex);
  2350. data = buf_buddy_alloc(zip_size, &lru);
  2351. mutex_enter(&block->mutex);
  2352. block->page.zip.data = data;
  2353. /* To maintain the invariant
  2354. block->in_unzip_LRU_list
  2355. == buf_page_belongs_to_unzip_LRU(&block->page)
  2356. we have to add this block to unzip_LRU
  2357. after block->page.zip.data is set. */
  2358. ut_ad(buf_page_belongs_to_unzip_LRU(&block->page));
  2359. buf_unzip_LRU_add_block(block, TRUE);
  2360. }
  2361. mutex_exit(&block->mutex);
  2362. } else {
  2363. /* Defer buf_buddy_alloc() until after the block has
  2364. been found not to exist. The buf_buddy_alloc() and
  2365. buf_buddy_free() calls may be expensive because of
  2366. buf_buddy_relocate(). */
  2367. /* The compressed page must be allocated before the
  2368. control block (bpage), in order to avoid the
  2369. invocation of buf_buddy_relocate_block() on
  2370. uninitialized data. */
  2371. data = buf_buddy_alloc(zip_size, &lru);
  2372. bpage = buf_buddy_alloc(sizeof *bpage, &lru);
  2373. /* If buf_buddy_alloc() allocated storage from the LRU list,
  2374. it released and reacquired buf_pool_mutex. Thus, we must
  2375. check the page_hash again, as it may have been modified. */
  2376. if (UNIV_UNLIKELY(lru)
  2377. && UNIV_LIKELY_NULL(buf_page_hash_get(space, offset))) {
  2378. /* The block was added by some other thread. */
  2379. buf_buddy_free(bpage, sizeof *bpage);
  2380. buf_buddy_free(data, zip_size);
  2381. bpage = NULL;
  2382. goto func_exit;
  2383. }
  2384. page_zip_des_init(&bpage->zip);
  2385. page_zip_set_size(&bpage->zip, zip_size);
  2386. bpage->zip.data = data;
  2387. mutex_enter(&buf_pool_zip_mutex);
  2388. UNIV_MEM_DESC(bpage->zip.data,
  2389. page_zip_get_size(&bpage->zip), bpage);
  2390. buf_page_init_low(bpage);
  2391. bpage->state = BUF_BLOCK_ZIP_PAGE;
  2392. bpage->space = space;
  2393. bpage->offset = offset;
  2394. #ifdef UNIV_DEBUG
  2395. bpage->in_page_hash = FALSE;
  2396. bpage->in_zip_hash = FALSE;
  2397. bpage->in_flush_list = FALSE;
  2398. bpage->in_free_list = FALSE;
  2399. bpage->in_LRU_list = FALSE;
  2400. #endif /* UNIV_DEBUG */
  2401. ut_d(bpage->in_page_hash = TRUE);
  2402. HASH_INSERT(buf_page_t, hash, buf_pool->page_hash,
  2403. buf_page_address_fold(space, offset), bpage);
  2404. /* The block must be put to the LRU list, to the old blocks */
  2405. buf_LRU_add_block(bpage, TRUE/* to old blocks */);
  2406. buf_LRU_insert_zip_clean(bpage);
  2407. buf_page_set_io_fix(bpage, BUF_IO_READ);
  2408. mutex_exit(&buf_pool_zip_mutex);
  2409. }
  2410. buf_pool->n_pend_reads++;
  2411. func_exit:
  2412. buf_pool_mutex_exit();
  2413. if (mode == BUF_READ_IBUF_PAGES_ONLY) {
  2414. mtr_commit(&mtr);
  2415. }
  2416. ut_ad(!bpage || buf_page_in_file(bpage));
  2417. return(bpage);
  2418. }
  2419. /********************************************************************//**
  2420. Initializes a page to the buffer buf_pool. The page is usually not read
  2421. from a file even if it cannot be found in the buffer buf_pool. This is one
  2422. of the functions which perform to a block a state transition NOT_USED =>
  2423. FILE_PAGE (the other is buf_page_get_gen).
  2424. @return pointer to the block, page bufferfixed */
  2425. UNIV_INTERN
  2426. buf_block_t*
  2427. buf_page_create(
  2428. /*============*/
  2429. ulint space, /*!< in: space id */
  2430. ulint offset, /*!< in: offset of the page within space in units of
  2431. a page */
  2432. ulint zip_size,/*!< in: compressed page size, or 0 */
  2433. mtr_t* mtr) /*!< in: mini-transaction handle */
  2434. {
  2435. buf_frame_t* frame;
  2436. buf_block_t* block;
  2437. buf_block_t* free_block = NULL;
  2438. ulint time_ms = ut_time_ms();
  2439. ut_ad(mtr);
  2440. ut_ad(space || !zip_size);
  2441. free_block = buf_LRU_get_free_block(0);
  2442. buf_pool_mutex_enter();
  2443. block = (buf_block_t*) buf_page_hash_get(space, offset);
  2444. if (block && buf_page_in_file(&block->page)) {
  2445. #ifdef UNIV_IBUF_COUNT_DEBUG
  2446. ut_a(ibuf_count_get(space, offset) == 0);
  2447. #endif
  2448. #ifdef UNIV_DEBUG_FILE_ACCESSES
  2449. block->page.file_page_was_freed = FALSE;
  2450. #endif /* UNIV_DEBUG_FILE_ACCESSES */
  2451. /* Page can be found in buf_pool */
  2452. buf_pool_mutex_exit();
  2453. buf_block_free(free_block);
  2454. return(buf_page_get_with_no_latch(space, zip_size,
  2455. offset, mtr));
  2456. }
  2457. /* If we get here, the page was not in buf_pool: init it there */
  2458. #ifdef UNIV_DEBUG
  2459. if (buf_debug_prints) {
  2460. fprintf(stderr, "Creating space %lu page %lu to buffer\n",
  2461. (ulong) space, (ulong) offset);
  2462. }
  2463. #endif /* UNIV_DEBUG */
  2464. block = free_block;
  2465. mutex_enter(&block->mutex);
  2466. buf_page_init(space, offset, block);
  2467. /* The block must be put to the LRU list */
  2468. buf_LRU_add_block(&block->page, FALSE);
  2469. buf_block_buf_fix_inc(block, __FILE__, __LINE__);
  2470. buf_pool->stat.n_pages_created++;
  2471. if (zip_size) {
  2472. void* data;
  2473. ibool lru;
  2474. /* Prevent race conditions during buf_buddy_alloc(),
  2475. which may release and reacquire buf_pool_mutex,
  2476. by IO-fixing and X-latching the block. */
  2477. buf_page_set_io_fix(&block->page, BUF_IO_READ);
  2478. rw_lock_x_lock(&block->lock);
  2479. page_zip_set_size(&block->page.zip, zip_size);
  2480. mutex_exit(&block->mutex);
  2481. /* buf_pool_mutex may be released and reacquired by
  2482. buf_buddy_alloc(). Thus, we must release block->mutex
  2483. in order not to break the latching order in
  2484. the reacquisition of buf_pool_mutex. We also must
  2485. defer this operation until after the block descriptor
  2486. has been added to buf_pool->LRU and buf_pool->page_hash. */
  2487. data = buf_buddy_alloc(zip_size, &lru);
  2488. mutex_enter(&block->mutex);
  2489. block->page.zip.data = data;
  2490. /* To maintain the invariant
  2491. block->in_unzip_LRU_list
  2492. == buf_page_belongs_to_unzip_LRU(&block->page)
  2493. we have to add this block to unzip_LRU after
  2494. block->page.zip.data is set. */
  2495. ut_ad(buf_page_belongs_to_unzip_LRU(&block->page));
  2496. buf_unzip_LRU_add_block(block, FALSE);
  2497. buf_page_set_io_fix(&block->page, BUF_IO_NONE);
  2498. rw_lock_x_unlock(&block->lock);
  2499. }
  2500. buf_page_set_accessed(&block->page, time_ms);
  2501. buf_pool_mutex_exit();
  2502. mtr_memo_push(mtr, block, MTR_MEMO_BUF_FIX);
  2503. mutex_exit(&block->mutex);
  2504. /* Delete possible entries for the page from the insert buffer:
  2505. such can exist if the page belonged to an index which was dropped */
  2506. ibuf_merge_or_delete_for_page(NULL, space, offset, zip_size, TRUE);
  2507. /* Flush pages from the end of the LRU list if necessary */
  2508. buf_flush_free_margin();
  2509. frame = block->frame;
  2510. memset(frame + FIL_PAGE_PREV, 0xff, 4);
  2511. memset(frame + FIL_PAGE_NEXT, 0xff, 4);
  2512. mach_write_to_2(frame + FIL_PAGE_TYPE, FIL_PAGE_TYPE_ALLOCATED);
  2513. /* Reset to zero the file flush lsn field in the page; if the first
  2514. page of an ibdata file is 'created' in this function into the buffer
  2515. pool then we lose the original contents of the file flush lsn stamp.
  2516. Then InnoDB could in a crash recovery print a big, false, corruption
  2517. warning if the stamp contains an lsn bigger than the ib_logfile lsn. */
  2518. memset(frame + FIL_PAGE_FILE_FLUSH_LSN, 0, 8);
  2519. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2520. ut_a(++buf_dbg_counter % 357 || buf_validate());
  2521. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  2522. #ifdef UNIV_IBUF_COUNT_DEBUG
  2523. ut_a(ibuf_count_get(buf_block_get_space(block),
  2524. buf_block_get_page_no(block)) == 0);
  2525. #endif
  2526. return(block);
  2527. }
  2528. /********************************************************************//**
  2529. Completes an asynchronous read or write request of a file page to or from
  2530. the buffer pool. */
  2531. UNIV_INTERN
  2532. void
  2533. buf_page_io_complete(
  2534. /*=================*/
  2535. buf_page_t* bpage) /*!< in: pointer to the block in question */
  2536. {
  2537. enum buf_io_fix io_type;
  2538. const ibool uncompressed = (buf_page_get_state(bpage)
  2539. == BUF_BLOCK_FILE_PAGE);
  2540. ut_a(buf_page_in_file(bpage));
  2541. /* We do not need protect io_fix here by mutex to read
  2542. it because this is the only function where we can change the value
  2543. from BUF_IO_READ or BUF_IO_WRITE to some other value, and our code
  2544. ensures that this is the only thread that handles the i/o for this
  2545. block. */
  2546. io_type = buf_page_get_io_fix(bpage);
  2547. ut_ad(io_type == BUF_IO_READ || io_type == BUF_IO_WRITE);
  2548. if (io_type == BUF_IO_READ) {
  2549. ulint read_page_no;
  2550. ulint read_space_id;
  2551. byte* frame;
  2552. if (buf_page_get_zip_size(bpage)) {
  2553. frame = bpage->zip.data;
  2554. buf_pool->n_pend_unzip++;
  2555. if (uncompressed
  2556. && !buf_zip_decompress((buf_block_t*) bpage,
  2557. FALSE)) {
  2558. buf_pool->n_pend_unzip--;
  2559. goto corrupt;
  2560. }
  2561. buf_pool->n_pend_unzip--;
  2562. } else {
  2563. ut_a(uncompressed);
  2564. frame = ((buf_block_t*) bpage)->frame;
  2565. }
  2566. /* If this page is not uninitialized and not in the
  2567. doublewrite buffer, then the page number and space id
  2568. should be the same as in block. */
  2569. read_page_no = mach_read_from_4(frame + FIL_PAGE_OFFSET);
  2570. read_space_id = mach_read_from_4(
  2571. frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
  2572. if (bpage->space == TRX_SYS_SPACE
  2573. && trx_doublewrite_page_inside(bpage->offset)) {
  2574. ut_print_timestamp(stderr);
  2575. fprintf(stderr,
  2576. " InnoDB: Error: reading page %lu\n"
  2577. "InnoDB: which is in the"
  2578. " doublewrite buffer!\n",
  2579. (ulong) bpage->offset);
  2580. } else if (!read_space_id && !read_page_no) {
  2581. /* This is likely an uninitialized page. */
  2582. } else if ((bpage->space
  2583. && bpage->space != read_space_id)
  2584. || bpage->offset != read_page_no) {
  2585. /* We did not compare space_id to read_space_id
  2586. if bpage->space == 0, because the field on the
  2587. page may contain garbage in MySQL < 4.1.1,
  2588. which only supported bpage->space == 0. */
  2589. ut_print_timestamp(stderr);
  2590. fprintf(stderr,
  2591. " InnoDB: Error: space id and page n:o"
  2592. " stored in the page\n"
  2593. "InnoDB: read in are %lu:%lu,"
  2594. " should be %lu:%lu!\n",
  2595. (ulong) read_space_id, (ulong) read_page_no,
  2596. (ulong) bpage->space,
  2597. (ulong) bpage->offset);
  2598. }
  2599. /* From version 3.23.38 up we store the page checksum
  2600. to the 4 first bytes of the page end lsn field */
  2601. if (buf_page_is_corrupted(frame,
  2602. buf_page_get_zip_size(bpage))) {
  2603. corrupt:
  2604. fprintf(stderr,
  2605. "InnoDB: Database page corruption on disk"
  2606. " or a failed\n"
  2607. "InnoDB: file read of page %lu.\n"
  2608. "InnoDB: You may have to recover"
  2609. " from a backup.\n",
  2610. (ulong) bpage->offset);
  2611. buf_page_print(frame, buf_page_get_zip_size(bpage));
  2612. fprintf(stderr,
  2613. "InnoDB: Database page corruption on disk"
  2614. " or a failed\n"
  2615. "InnoDB: file read of page %lu.\n"
  2616. "InnoDB: You may have to recover"
  2617. " from a backup.\n",
  2618. (ulong) bpage->offset);
  2619. fputs("InnoDB: It is also possible that"
  2620. " your operating\n"
  2621. "InnoDB: system has corrupted its"
  2622. " own file cache\n"
  2623. "InnoDB: and rebooting your computer"
  2624. " removes the\n"
  2625. "InnoDB: error.\n"
  2626. "InnoDB: If the corrupt page is an index page\n"
  2627. "InnoDB: you can also try to"
  2628. " fix the corruption\n"
  2629. "InnoDB: by dumping, dropping,"
  2630. " and reimporting\n"
  2631. "InnoDB: the corrupt table."
  2632. " You can use CHECK\n"
  2633. "InnoDB: TABLE to scan your"
  2634. " table for corruption.\n"
  2635. "InnoDB: See also "
  2636. REFMAN "forcing-recovery.html\n"
  2637. "InnoDB: about forcing recovery.\n", stderr);
  2638. if (srv_force_recovery < SRV_FORCE_IGNORE_CORRUPT) {
  2639. fputs("InnoDB: Ending processing because of"
  2640. " a corrupt database page.\n",
  2641. stderr);
  2642. exit(1);
  2643. }
  2644. }
  2645. if (recv_recovery_is_on()) {
  2646. /* Pages must be uncompressed for crash recovery. */
  2647. ut_a(uncompressed);
  2648. recv_recover_page(TRUE, (buf_block_t*) bpage);
  2649. }
  2650. if (uncompressed && !recv_no_ibuf_operations) {
  2651. ibuf_merge_or_delete_for_page(
  2652. (buf_block_t*) bpage, bpage->space,
  2653. bpage->offset, buf_page_get_zip_size(bpage),
  2654. TRUE);
  2655. }
  2656. }
  2657. buf_pool_mutex_enter();
  2658. mutex_enter(buf_page_get_mutex(bpage));
  2659. #ifdef UNIV_IBUF_COUNT_DEBUG
  2660. if (io_type == BUF_IO_WRITE || uncompressed) {
  2661. /* For BUF_IO_READ of compressed-only blocks, the
  2662. buffered operations will be merged by buf_page_get_gen()
  2663. after the block has been uncompressed. */
  2664. ut_a(ibuf_count_get(bpage->space, bpage->offset) == 0);
  2665. }
  2666. #endif
  2667. /* Because this thread which does the unlocking is not the same that
  2668. did the locking, we use a pass value != 0 in unlock, which simply
  2669. removes the newest lock debug record, without checking the thread
  2670. id. */
  2671. buf_page_set_io_fix(bpage, BUF_IO_NONE);
  2672. switch (io_type) {
  2673. case BUF_IO_READ:
  2674. /* NOTE that the call to ibuf may have moved the ownership of
  2675. the x-latch to this OS thread: do not let this confuse you in
  2676. debugging! */
  2677. ut_ad(buf_pool->n_pend_reads > 0);
  2678. buf_pool->n_pend_reads--;
  2679. buf_pool->stat.n_pages_read++;
  2680. if (uncompressed) {
  2681. rw_lock_x_unlock_gen(&((buf_block_t*) bpage)->lock,
  2682. BUF_IO_READ);
  2683. }
  2684. break;
  2685. case BUF_IO_WRITE:
  2686. /* Write means a flush operation: call the completion
  2687. routine in the flush system */
  2688. buf_flush_write_complete(bpage);
  2689. if (uncompressed) {
  2690. rw_lock_s_unlock_gen(&((buf_block_t*) bpage)->lock,
  2691. BUF_IO_WRITE);
  2692. }
  2693. buf_pool->stat.n_pages_written++;
  2694. break;
  2695. default:
  2696. ut_error;
  2697. }
  2698. #ifdef UNIV_DEBUG
  2699. if (buf_debug_prints) {
  2700. fprintf(stderr, "Has %s page space %lu page no %lu\n",
  2701. io_type == BUF_IO_READ ? "read" : "written",
  2702. (ulong) buf_page_get_space(bpage),
  2703. (ulong) buf_page_get_page_no(bpage));
  2704. }
  2705. #endif /* UNIV_DEBUG */
  2706. mutex_exit(buf_page_get_mutex(bpage));
  2707. buf_pool_mutex_exit();
  2708. }
  2709. /*********************************************************************//**
  2710. Invalidates the file pages in the buffer pool when an archive recovery is
  2711. completed. All the file pages buffered must be in a replaceable state when
  2712. this function is called: not latched and not modified. */
  2713. UNIV_INTERN
  2714. void
  2715. buf_pool_invalidate(void)
  2716. /*=====================*/
  2717. {
  2718. ibool freed;
  2719. enum buf_flush i;
  2720. buf_pool_mutex_enter();
  2721. for (i = BUF_FLUSH_LRU; i < BUF_FLUSH_N_TYPES; i++) {
  2722. /* As this function is called during startup and
  2723. during redo application phase during recovery, InnoDB
  2724. is single threaded (apart from IO helper threads) at
  2725. this stage. No new write batch can be in intialization
  2726. stage at this point. */
  2727. ut_ad(buf_pool->init_flush[i] == FALSE);
  2728. /* However, it is possible that a write batch that has
  2729. been posted earlier is still not complete. For buffer
  2730. pool invalidation to proceed we must ensure there is NO
  2731. write activity happening. */
  2732. if (buf_pool->n_flush[i] > 0) {
  2733. buf_pool_mutex_exit();
  2734. buf_flush_wait_batch_end(i);
  2735. buf_pool_mutex_enter();
  2736. }
  2737. }
  2738. buf_pool_mutex_exit();
  2739. ut_ad(buf_all_freed());
  2740. freed = TRUE;
  2741. while (freed) {
  2742. freed = buf_LRU_search_and_free_block(100);
  2743. }
  2744. buf_pool_mutex_enter();
  2745. ut_ad(UT_LIST_GET_LEN(buf_pool->LRU) == 0);
  2746. ut_ad(UT_LIST_GET_LEN(buf_pool->unzip_LRU) == 0);
  2747. buf_pool->freed_page_clock = 0;
  2748. buf_pool->LRU_old = NULL;
  2749. buf_pool->LRU_old_len = 0;
  2750. buf_pool->LRU_flush_ended = 0;
  2751. memset(&buf_pool->stat, 0x00, sizeof(buf_pool->stat));
  2752. buf_refresh_io_stats();
  2753. buf_pool_mutex_exit();
  2754. }
  2755. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2756. /*********************************************************************//**
  2757. Validates the buffer buf_pool data structure.
  2758. @return TRUE */
  2759. UNIV_INTERN
  2760. ibool
  2761. buf_validate(void)
  2762. /*==============*/
  2763. {
  2764. buf_page_t* b;
  2765. buf_chunk_t* chunk;
  2766. ulint i;
  2767. ulint n_single_flush = 0;
  2768. ulint n_lru_flush = 0;
  2769. ulint n_list_flush = 0;
  2770. ulint n_lru = 0;
  2771. ulint n_flush = 0;
  2772. ulint n_free = 0;
  2773. ulint n_zip = 0;
  2774. ut_ad(buf_pool);
  2775. buf_pool_mutex_enter();
  2776. chunk = buf_pool->chunks;
  2777. /* Check the uncompressed blocks. */
  2778. for (i = buf_pool->n_chunks; i--; chunk++) {
  2779. ulint j;
  2780. buf_block_t* block = chunk->blocks;
  2781. for (j = chunk->size; j--; block++) {
  2782. mutex_enter(&block->mutex);
  2783. switch (buf_block_get_state(block)) {
  2784. case BUF_BLOCK_ZIP_FREE:
  2785. case BUF_BLOCK_ZIP_PAGE:
  2786. case BUF_BLOCK_ZIP_DIRTY:
  2787. /* These should only occur on
  2788. zip_clean, zip_free[], or flush_list. */
  2789. ut_error;
  2790. break;
  2791. case BUF_BLOCK_FILE_PAGE:
  2792. ut_a(buf_page_hash_get(buf_block_get_space(
  2793. block),
  2794. buf_block_get_page_no(
  2795. block))
  2796. == &block->page);
  2797. #ifdef UNIV_IBUF_COUNT_DEBUG
  2798. ut_a(buf_page_get_io_fix(&block->page)
  2799. == BUF_IO_READ
  2800. || !ibuf_count_get(buf_block_get_space(
  2801. block),
  2802. buf_block_get_page_no(
  2803. block)));
  2804. #endif
  2805. switch (buf_page_get_io_fix(&block->page)) {
  2806. case BUF_IO_NONE:
  2807. break;
  2808. case BUF_IO_WRITE:
  2809. switch (buf_page_get_flush_type(
  2810. &block->page)) {
  2811. case BUF_FLUSH_LRU:
  2812. n_lru_flush++;
  2813. ut_a(rw_lock_is_locked(
  2814. &block->lock,
  2815. RW_LOCK_SHARED));
  2816. break;
  2817. case BUF_FLUSH_LIST:
  2818. n_list_flush++;
  2819. break;
  2820. case BUF_FLUSH_SINGLE_PAGE:
  2821. n_single_flush++;
  2822. break;
  2823. default:
  2824. ut_error;
  2825. }
  2826. break;
  2827. case BUF_IO_READ:
  2828. ut_a(rw_lock_is_locked(&block->lock,
  2829. RW_LOCK_EX));
  2830. break;
  2831. }
  2832. n_lru++;
  2833. if (block->page.oldest_modification > 0) {
  2834. n_flush++;
  2835. }
  2836. break;
  2837. case BUF_BLOCK_NOT_USED:
  2838. n_free++;
  2839. break;
  2840. case BUF_BLOCK_READY_FOR_USE:
  2841. case BUF_BLOCK_MEMORY:
  2842. case BUF_BLOCK_REMOVE_HASH:
  2843. /* do nothing */
  2844. break;
  2845. }
  2846. mutex_exit(&block->mutex);
  2847. }
  2848. }
  2849. mutex_enter(&buf_pool_zip_mutex);
  2850. /* Check clean compressed-only blocks. */
  2851. for (b = UT_LIST_GET_FIRST(buf_pool->zip_clean); b;
  2852. b = UT_LIST_GET_NEXT(list, b)) {
  2853. ut_a(buf_page_get_state(b) == BUF_BLOCK_ZIP_PAGE);
  2854. switch (buf_page_get_io_fix(b)) {
  2855. case BUF_IO_NONE:
  2856. /* All clean blocks should be I/O-unfixed. */
  2857. break;
  2858. case BUF_IO_READ:
  2859. /* In buf_LRU_free_block(), we temporarily set
  2860. b->io_fix = BUF_IO_READ for a newly allocated
  2861. control block in order to prevent
  2862. buf_page_get_gen() from decompressing the block. */
  2863. break;
  2864. default:
  2865. ut_error;
  2866. break;
  2867. }
  2868. ut_a(!b->oldest_modification);
  2869. ut_a(buf_page_hash_get(b->space, b->offset) == b);
  2870. n_lru++;
  2871. n_zip++;
  2872. }
  2873. /* Check dirty compressed-only blocks. */
  2874. for (b = UT_LIST_GET_FIRST(buf_pool->flush_list); b;
  2875. b = UT_LIST_GET_NEXT(list, b)) {
  2876. ut_ad(b->in_flush_list);
  2877. switch (buf_page_get_state(b)) {
  2878. case BUF_BLOCK_ZIP_DIRTY:
  2879. ut_a(b->oldest_modification);
  2880. n_lru++;
  2881. n_flush++;
  2882. n_zip++;
  2883. switch (buf_page_get_io_fix(b)) {
  2884. case BUF_IO_NONE:
  2885. case BUF_IO_READ:
  2886. break;
  2887. case BUF_IO_WRITE:
  2888. switch (buf_page_get_flush_type(b)) {
  2889. case BUF_FLUSH_LRU:
  2890. n_lru_flush++;
  2891. break;
  2892. case BUF_FLUSH_LIST:
  2893. n_list_flush++;
  2894. break;
  2895. case BUF_FLUSH_SINGLE_PAGE:
  2896. n_single_flush++;
  2897. break;
  2898. default:
  2899. ut_error;
  2900. }
  2901. break;
  2902. }
  2903. break;
  2904. case BUF_BLOCK_FILE_PAGE:
  2905. /* uncompressed page */
  2906. break;
  2907. case BUF_BLOCK_ZIP_FREE:
  2908. case BUF_BLOCK_ZIP_PAGE:
  2909. case BUF_BLOCK_NOT_USED:
  2910. case BUF_BLOCK_READY_FOR_USE:
  2911. case BUF_BLOCK_MEMORY:
  2912. case BUF_BLOCK_REMOVE_HASH:
  2913. ut_error;
  2914. break;
  2915. }
  2916. ut_a(buf_page_hash_get(b->space, b->offset) == b);
  2917. }
  2918. mutex_exit(&buf_pool_zip_mutex);
  2919. if (n_lru + n_free > buf_pool->curr_size + n_zip) {
  2920. fprintf(stderr, "n LRU %lu, n free %lu, pool %lu zip %lu\n",
  2921. (ulong) n_lru, (ulong) n_free,
  2922. (ulong) buf_pool->curr_size, (ulong) n_zip);
  2923. ut_error;
  2924. }
  2925. ut_a(UT_LIST_GET_LEN(buf_pool->LRU) == n_lru);
  2926. if (UT_LIST_GET_LEN(buf_pool->free) != n_free) {
  2927. fprintf(stderr, "Free list len %lu, free blocks %lu\n",
  2928. (ulong) UT_LIST_GET_LEN(buf_pool->free),
  2929. (ulong) n_free);
  2930. ut_error;
  2931. }
  2932. ut_a(UT_LIST_GET_LEN(buf_pool->flush_list) == n_flush);
  2933. ut_a(buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE] == n_single_flush);
  2934. ut_a(buf_pool->n_flush[BUF_FLUSH_LIST] == n_list_flush);
  2935. ut_a(buf_pool->n_flush[BUF_FLUSH_LRU] == n_lru_flush);
  2936. buf_pool_mutex_exit();
  2937. ut_a(buf_LRU_validate());
  2938. ut_a(buf_flush_validate());
  2939. return(TRUE);
  2940. }
  2941. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  2942. #if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2943. /*********************************************************************//**
  2944. Prints info of the buffer buf_pool data structure. */
  2945. UNIV_INTERN
  2946. void
  2947. buf_print(void)
  2948. /*===========*/
  2949. {
  2950. dulint* index_ids;
  2951. ulint* counts;
  2952. ulint size;
  2953. ulint i;
  2954. ulint j;
  2955. dulint id;
  2956. ulint n_found;
  2957. buf_chunk_t* chunk;
  2958. dict_index_t* index;
  2959. ut_ad(buf_pool);
  2960. size = buf_pool->curr_size;
  2961. index_ids = mem_alloc(sizeof(dulint) * size);
  2962. counts = mem_alloc(sizeof(ulint) * size);
  2963. buf_pool_mutex_enter();
  2964. fprintf(stderr,
  2965. "buf_pool size %lu\n"
  2966. "database pages %lu\n"
  2967. "free pages %lu\n"
  2968. "modified database pages %lu\n"
  2969. "n pending decompressions %lu\n"
  2970. "n pending reads %lu\n"
  2971. "n pending flush LRU %lu list %lu single page %lu\n"
  2972. "pages made young %lu, not young %lu\n"
  2973. "pages read %lu, created %lu, written %lu\n",
  2974. (ulong) size,
  2975. (ulong) UT_LIST_GET_LEN(buf_pool->LRU),
  2976. (ulong) UT_LIST_GET_LEN(buf_pool->free),
  2977. (ulong) UT_LIST_GET_LEN(buf_pool->flush_list),
  2978. (ulong) buf_pool->n_pend_unzip,
  2979. (ulong) buf_pool->n_pend_reads,
  2980. (ulong) buf_pool->n_flush[BUF_FLUSH_LRU],
  2981. (ulong) buf_pool->n_flush[BUF_FLUSH_LIST],
  2982. (ulong) buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE],
  2983. (ulong) buf_pool->stat.n_pages_made_young,
  2984. (ulong) buf_pool->stat.n_pages_not_made_young,
  2985. (ulong) buf_pool->stat.n_pages_read,
  2986. (ulong) buf_pool->stat.n_pages_created,
  2987. (ulong) buf_pool->stat.n_pages_written);
  2988. /* Count the number of blocks belonging to each index in the buffer */
  2989. n_found = 0;
  2990. chunk = buf_pool->chunks;
  2991. for (i = buf_pool->n_chunks; i--; chunk++) {
  2992. buf_block_t* block = chunk->blocks;
  2993. ulint n_blocks = chunk->size;
  2994. for (; n_blocks--; block++) {
  2995. const buf_frame_t* frame = block->frame;
  2996. if (fil_page_get_type(frame) == FIL_PAGE_INDEX) {
  2997. id = btr_page_get_index_id(frame);
  2998. /* Look for the id in the index_ids array */
  2999. j = 0;
  3000. while (j < n_found) {
  3001. if (ut_dulint_cmp(index_ids[j],
  3002. id) == 0) {
  3003. counts[j]++;
  3004. break;
  3005. }
  3006. j++;
  3007. }
  3008. if (j == n_found) {
  3009. n_found++;
  3010. index_ids[j] = id;
  3011. counts[j] = 1;
  3012. }
  3013. }
  3014. }
  3015. }
  3016. buf_pool_mutex_exit();
  3017. for (i = 0; i < n_found; i++) {
  3018. index = dict_index_get_if_in_cache(index_ids[i]);
  3019. fprintf(stderr,
  3020. "Block count for index %lu in buffer is about %lu",
  3021. (ulong) ut_dulint_get_low(index_ids[i]),
  3022. (ulong) counts[i]);
  3023. if (index) {
  3024. putc(' ', stderr);
  3025. dict_index_name_print(stderr, NULL, index);
  3026. }
  3027. putc('\n', stderr);
  3028. }
  3029. mem_free(index_ids);
  3030. mem_free(counts);
  3031. ut_a(buf_validate());
  3032. }
  3033. #endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */
  3034. #ifdef UNIV_DEBUG
  3035. /*********************************************************************//**
  3036. Returns the number of latched pages in the buffer pool.
  3037. @return number of latched pages */
  3038. UNIV_INTERN
  3039. ulint
  3040. buf_get_latched_pages_number(void)
  3041. /*==============================*/
  3042. {
  3043. buf_chunk_t* chunk;
  3044. buf_page_t* b;
  3045. ulint i;
  3046. ulint fixed_pages_number = 0;
  3047. buf_pool_mutex_enter();
  3048. chunk = buf_pool->chunks;
  3049. for (i = buf_pool->n_chunks; i--; chunk++) {
  3050. buf_block_t* block;
  3051. ulint j;
  3052. block = chunk->blocks;
  3053. for (j = chunk->size; j--; block++) {
  3054. if (buf_block_get_state(block)
  3055. != BUF_BLOCK_FILE_PAGE) {
  3056. continue;
  3057. }
  3058. mutex_enter(&block->mutex);
  3059. if (block->page.buf_fix_count != 0
  3060. || buf_page_get_io_fix(&block->page)
  3061. != BUF_IO_NONE) {
  3062. fixed_pages_number++;
  3063. }
  3064. mutex_exit(&block->mutex);
  3065. }
  3066. }
  3067. mutex_enter(&buf_pool_zip_mutex);
  3068. /* Traverse the lists of clean and dirty compressed-only blocks. */
  3069. for (b = UT_LIST_GET_FIRST(buf_pool->zip_clean); b;
  3070. b = UT_LIST_GET_NEXT(list, b)) {
  3071. ut_a(buf_page_get_state(b) == BUF_BLOCK_ZIP_PAGE);
  3072. ut_a(buf_page_get_io_fix(b) != BUF_IO_WRITE);
  3073. if (b->buf_fix_count != 0
  3074. || buf_page_get_io_fix(b) != BUF_IO_NONE) {
  3075. fixed_pages_number++;
  3076. }
  3077. }
  3078. for (b = UT_LIST_GET_FIRST(buf_pool->flush_list); b;
  3079. b = UT_LIST_GET_NEXT(list, b)) {
  3080. ut_ad(b->in_flush_list);
  3081. switch (buf_page_get_state(b)) {
  3082. case BUF_BLOCK_ZIP_DIRTY:
  3083. if (b->buf_fix_count != 0
  3084. || buf_page_get_io_fix(b) != BUF_IO_NONE) {
  3085. fixed_pages_number++;
  3086. }
  3087. break;
  3088. case BUF_BLOCK_FILE_PAGE:
  3089. /* uncompressed page */
  3090. break;
  3091. case BUF_BLOCK_ZIP_FREE:
  3092. case BUF_BLOCK_ZIP_PAGE:
  3093. case BUF_BLOCK_NOT_USED:
  3094. case BUF_BLOCK_READY_FOR_USE:
  3095. case BUF_BLOCK_MEMORY:
  3096. case BUF_BLOCK_REMOVE_HASH:
  3097. ut_error;
  3098. break;
  3099. }
  3100. }
  3101. mutex_exit(&buf_pool_zip_mutex);
  3102. buf_pool_mutex_exit();
  3103. return(fixed_pages_number);
  3104. }
  3105. #endif /* UNIV_DEBUG */
  3106. /*********************************************************************//**
  3107. Returns the number of pending buf pool ios.
  3108. @return number of pending I/O operations */
  3109. UNIV_INTERN
  3110. ulint
  3111. buf_get_n_pending_ios(void)
  3112. /*=======================*/
  3113. {
  3114. return(buf_pool->n_pend_reads
  3115. + buf_pool->n_flush[BUF_FLUSH_LRU]
  3116. + buf_pool->n_flush[BUF_FLUSH_LIST]
  3117. + buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]);
  3118. }
  3119. /*********************************************************************//**
  3120. Returns the ratio in percents of modified pages in the buffer pool /
  3121. database pages in the buffer pool.
  3122. @return modified page percentage ratio */
  3123. UNIV_INTERN
  3124. ulint
  3125. buf_get_modified_ratio_pct(void)
  3126. /*============================*/
  3127. {
  3128. ulint ratio;
  3129. buf_pool_mutex_enter();
  3130. ratio = (100 * UT_LIST_GET_LEN(buf_pool->flush_list))
  3131. / (1 + UT_LIST_GET_LEN(buf_pool->LRU)
  3132. + UT_LIST_GET_LEN(buf_pool->free));
  3133. /* 1 + is there to avoid division by zero */
  3134. buf_pool_mutex_exit();
  3135. return(ratio);
  3136. }
  3137. /*********************************************************************//**
  3138. Prints info of the buffer i/o. */
  3139. UNIV_INTERN
  3140. void
  3141. buf_print_io(
  3142. /*=========*/
  3143. FILE* file) /*!< in/out: buffer where to print */
  3144. {
  3145. time_t current_time;
  3146. double time_elapsed;
  3147. ulint n_gets_diff;
  3148. ut_ad(buf_pool);
  3149. buf_pool_mutex_enter();
  3150. fprintf(file,
  3151. "Buffer pool size %lu\n"
  3152. "Free buffers %lu\n"
  3153. "Database pages %lu\n"
  3154. "Old database pages %lu\n"
  3155. "Modified db pages %lu\n"
  3156. "Pending reads %lu\n"
  3157. "Pending writes: LRU %lu, flush list %lu, single page %lu\n",
  3158. (ulong) buf_pool->curr_size,
  3159. (ulong) UT_LIST_GET_LEN(buf_pool->free),
  3160. (ulong) UT_LIST_GET_LEN(buf_pool->LRU),
  3161. (ulong) buf_pool->LRU_old_len,
  3162. (ulong) UT_LIST_GET_LEN(buf_pool->flush_list),
  3163. (ulong) buf_pool->n_pend_reads,
  3164. (ulong) buf_pool->n_flush[BUF_FLUSH_LRU]
  3165. + buf_pool->init_flush[BUF_FLUSH_LRU],
  3166. (ulong) buf_pool->n_flush[BUF_FLUSH_LIST]
  3167. + buf_pool->init_flush[BUF_FLUSH_LIST],
  3168. (ulong) buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]);
  3169. current_time = time(NULL);
  3170. time_elapsed = 0.001 + difftime(current_time,
  3171. buf_pool->last_printout_time);
  3172. fprintf(file,
  3173. "Pages made young %lu, not young %lu\n"
  3174. "%.2f youngs/s, %.2f non-youngs/s\n"
  3175. "Pages read %lu, created %lu, written %lu\n"
  3176. "%.2f reads/s, %.2f creates/s, %.2f writes/s\n",
  3177. (ulong) buf_pool->stat.n_pages_made_young,
  3178. (ulong) buf_pool->stat.n_pages_not_made_young,
  3179. (buf_pool->stat.n_pages_made_young
  3180. - buf_pool->old_stat.n_pages_made_young)
  3181. / time_elapsed,
  3182. (buf_pool->stat.n_pages_not_made_young
  3183. - buf_pool->old_stat.n_pages_not_made_young)
  3184. / time_elapsed,
  3185. (ulong) buf_pool->stat.n_pages_read,
  3186. (ulong) buf_pool->stat.n_pages_created,
  3187. (ulong) buf_pool->stat.n_pages_written,
  3188. (buf_pool->stat.n_pages_read
  3189. - buf_pool->old_stat.n_pages_read)
  3190. / time_elapsed,
  3191. (buf_pool->stat.n_pages_created
  3192. - buf_pool->old_stat.n_pages_created)
  3193. / time_elapsed,
  3194. (buf_pool->stat.n_pages_written
  3195. - buf_pool->old_stat.n_pages_written)
  3196. / time_elapsed);
  3197. n_gets_diff = buf_pool->stat.n_page_gets - buf_pool->old_stat.n_page_gets;
  3198. if (n_gets_diff) {
  3199. fprintf(file,
  3200. "Buffer pool hit rate %lu / 1000,"
  3201. " young-making rate %lu / 1000 not %lu / 1000\n",
  3202. (ulong)
  3203. (1000 - ((1000 * (buf_pool->stat.n_pages_read
  3204. - buf_pool->old_stat.n_pages_read))
  3205. / (buf_pool->stat.n_page_gets
  3206. - buf_pool->old_stat.n_page_gets))),
  3207. (ulong)
  3208. (1000 * (buf_pool->stat.n_pages_made_young
  3209. - buf_pool->old_stat.n_pages_made_young)
  3210. / n_gets_diff),
  3211. (ulong)
  3212. (1000 * (buf_pool->stat.n_pages_not_made_young
  3213. - buf_pool->old_stat.n_pages_not_made_young)
  3214. / n_gets_diff));
  3215. } else {
  3216. fputs("No buffer pool page gets since the last printout\n",
  3217. file);
  3218. }
  3219. /* Statistics about read ahead algorithm */
  3220. fprintf(file, "Pages read ahead %.2f/s,"
  3221. " evicted without access %.2f/s\n",
  3222. (buf_pool->stat.n_ra_pages_read
  3223. - buf_pool->old_stat.n_ra_pages_read)
  3224. / time_elapsed,
  3225. (buf_pool->stat.n_ra_pages_evicted
  3226. - buf_pool->old_stat.n_ra_pages_evicted)
  3227. / time_elapsed);
  3228. /* Print some values to help us with visualizing what is
  3229. happening with LRU eviction. */
  3230. fprintf(file,
  3231. "LRU len: %lu, unzip_LRU len: %lu\n"
  3232. "I/O sum[%lu]:cur[%lu], unzip sum[%lu]:cur[%lu]\n",
  3233. UT_LIST_GET_LEN(buf_pool->LRU),
  3234. UT_LIST_GET_LEN(buf_pool->unzip_LRU),
  3235. buf_LRU_stat_sum.io, buf_LRU_stat_cur.io,
  3236. buf_LRU_stat_sum.unzip, buf_LRU_stat_cur.unzip);
  3237. buf_refresh_io_stats();
  3238. buf_pool_mutex_exit();
  3239. }
  3240. /**********************************************************************//**
  3241. Refreshes the statistics used to print per-second averages. */
  3242. UNIV_INTERN
  3243. void
  3244. buf_refresh_io_stats(void)
  3245. /*======================*/
  3246. {
  3247. buf_pool->last_printout_time = time(NULL);
  3248. buf_pool->old_stat = buf_pool->stat;
  3249. }
  3250. /*********************************************************************//**
  3251. Asserts that all file pages in the buffer are in a replaceable state.
  3252. @return TRUE */
  3253. UNIV_INTERN
  3254. ibool
  3255. buf_all_freed(void)
  3256. /*===============*/
  3257. {
  3258. buf_chunk_t* chunk;
  3259. ulint i;
  3260. ut_ad(buf_pool);
  3261. buf_pool_mutex_enter();
  3262. chunk = buf_pool->chunks;
  3263. for (i = buf_pool->n_chunks; i--; chunk++) {
  3264. const buf_block_t* block = buf_chunk_not_freed(chunk);
  3265. if (UNIV_LIKELY_NULL(block)) {
  3266. fprintf(stderr,
  3267. "Page %lu %lu still fixed or dirty\n",
  3268. (ulong) block->page.space,
  3269. (ulong) block->page.offset);
  3270. ut_error;
  3271. }
  3272. }
  3273. buf_pool_mutex_exit();
  3274. return(TRUE);
  3275. }
  3276. /*********************************************************************//**
  3277. Checks that there currently are no pending i/o-operations for the buffer
  3278. pool.
  3279. @return TRUE if there is no pending i/o */
  3280. UNIV_INTERN
  3281. ibool
  3282. buf_pool_check_no_pending_io(void)
  3283. /*==============================*/
  3284. {
  3285. ibool ret;
  3286. buf_pool_mutex_enter();
  3287. if (buf_pool->n_pend_reads + buf_pool->n_flush[BUF_FLUSH_LRU]
  3288. + buf_pool->n_flush[BUF_FLUSH_LIST]
  3289. + buf_pool->n_flush[BUF_FLUSH_SINGLE_PAGE]) {
  3290. ret = FALSE;
  3291. } else {
  3292. ret = TRUE;
  3293. }
  3294. buf_pool_mutex_exit();
  3295. return(ret);
  3296. }
  3297. /*********************************************************************//**
  3298. Gets the current length of the free list of buffer blocks.
  3299. @return length of the free list */
  3300. UNIV_INTERN
  3301. ulint
  3302. buf_get_free_list_len(void)
  3303. /*=======================*/
  3304. {
  3305. ulint len;
  3306. buf_pool_mutex_enter();
  3307. len = UT_LIST_GET_LEN(buf_pool->free);
  3308. buf_pool_mutex_exit();
  3309. return(len);
  3310. }
  3311. #else /* !UNIV_HOTBACKUP */
  3312. /********************************************************************//**
  3313. Inits a page to the buffer buf_pool, for use in ibbackup --restore. */
  3314. UNIV_INTERN
  3315. void
  3316. buf_page_init_for_backup_restore(
  3317. /*=============================*/
  3318. ulint space, /*!< in: space id */
  3319. ulint offset, /*!< in: offset of the page within space
  3320. in units of a page */
  3321. ulint zip_size,/*!< in: compressed page size in bytes
  3322. or 0 for uncompressed pages */
  3323. buf_block_t* block) /*!< in: block to init */
  3324. {
  3325. block->page.state = BUF_BLOCK_FILE_PAGE;
  3326. block->page.space = space;
  3327. block->page.offset = offset;
  3328. page_zip_des_init(&block->page.zip);
  3329. /* We assume that block->page.data has been allocated
  3330. with zip_size == UNIV_PAGE_SIZE. */
  3331. ut_ad(zip_size <= UNIV_PAGE_SIZE);
  3332. ut_ad(ut_is_2pow(zip_size));
  3333. page_zip_set_size(&block->page.zip, zip_size);
  3334. if (zip_size) {
  3335. block->page.zip.data = block->frame + UNIV_PAGE_SIZE;
  3336. }
  3337. }
  3338. #endif /* !UNIV_HOTBACKUP */