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.

3013 lines
84 KiB

17 years ago
9 years ago
17 years ago
17 years ago
16 years ago
17 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
9 years ago
9 years ago
9 years ago
16 years ago
15 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
15 years ago
15 years ago
17 years ago
17 years ago
15 years ago
15 years ago
9 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
9 years ago
9 years ago
9 years ago
15 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
15 years ago
17 years ago
15 years ago
15 years ago
16 years ago
15 years ago
17 years ago
17 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file buf/buf0lru.cc
  15. The database buffer replacement algorithm
  16. Created 11/5/1995 Heikki Tuuri
  17. *******************************************************/
  18. #include "buf0lru.h"
  19. #ifndef UNIV_HOTBACKUP
  20. #ifdef UNIV_NONINL
  21. #include "buf0lru.ic"
  22. #endif
  23. #include "ut0byte.h"
  24. #include "ut0lst.h"
  25. #include "ut0rnd.h"
  26. #include "sync0sync.h"
  27. #include "sync0rw.h"
  28. #include "hash0hash.h"
  29. #include "os0sync.h"
  30. #include "fil0fil.h"
  31. #include "btr0btr.h"
  32. #include "buf0buddy.h"
  33. #include "buf0buf.h"
  34. #include "buf0dblwr.h"
  35. #include "buf0flu.h"
  36. #include "buf0rea.h"
  37. #include "btr0sea.h"
  38. #include "ibuf0ibuf.h"
  39. #include "os0file.h"
  40. #include "page0zip.h"
  41. #include "log0recv.h"
  42. #include "srv0srv.h"
  43. #include "srv0start.h"
  44. #include "srv0mon.h"
  45. #include "lock0lock.h"
  46. #include "ha_prototypes.h"
  47. /** The number of blocks from the LRU_old pointer onward, including
  48. the block pointed to, must be buf_pool->LRU_old_ratio/BUF_LRU_OLD_RATIO_DIV
  49. of the whole LRU list length, except that the tolerance defined below
  50. is allowed. Note that the tolerance must be small enough such that for
  51. even the BUF_LRU_OLD_MIN_LEN long LRU list, the LRU_old pointer is not
  52. allowed to point to either end of the LRU list. */
  53. #define BUF_LRU_OLD_TOLERANCE 20
  54. /** The minimum amount of non-old blocks when the LRU_old list exists
  55. (that is, when there are more than BUF_LRU_OLD_MIN_LEN blocks).
  56. @see buf_LRU_old_adjust_len */
  57. #define BUF_LRU_NON_OLD_MIN_LEN 5
  58. #if BUF_LRU_NON_OLD_MIN_LEN >= BUF_LRU_OLD_MIN_LEN
  59. # error "BUF_LRU_NON_OLD_MIN_LEN >= BUF_LRU_OLD_MIN_LEN"
  60. #endif
  61. /** When dropping the search hash index entries before deleting an ibd
  62. file, we build a local array of pages belonging to that tablespace
  63. in the buffer pool. Following is the size of that array.
  64. We also release buf_pool->LRU_list_mutex after scanning this many pages of the
  65. flush_list when dropping a table. This is to ensure that other threads
  66. are not blocked for extended period of time when using very large
  67. buffer pools. */
  68. #define BUF_LRU_DROP_SEARCH_SIZE 1024
  69. /** If we switch on the InnoDB monitor because there are too few available
  70. frames in the buffer pool, we set this to TRUE */
  71. static ibool buf_lru_switched_on_innodb_mon = FALSE;
  72. /******************************************************************//**
  73. These statistics are not 'of' LRU but 'for' LRU. We keep count of I/O
  74. and page_zip_decompress() operations. Based on the statistics,
  75. buf_LRU_evict_from_unzip_LRU() decides if we want to evict from
  76. unzip_LRU or the regular LRU. From unzip_LRU, we will only evict the
  77. uncompressed frame (meaning we can evict dirty blocks as well). From
  78. the regular LRU, we will evict the entire block (i.e.: both the
  79. uncompressed and compressed data), which must be clean. */
  80. /* @{ */
  81. /** Number of intervals for which we keep the history of these stats.
  82. Each interval is 1 second, defined by the rate at which
  83. srv_error_monitor_thread() calls buf_LRU_stat_update(). */
  84. #define BUF_LRU_STAT_N_INTERVAL 50
  85. /** Co-efficient with which we multiply I/O operations to equate them
  86. with page_zip_decompress() operations. */
  87. #define BUF_LRU_IO_TO_UNZIP_FACTOR 50
  88. /** Sampled values buf_LRU_stat_cur.
  89. Not protected by any mutex. Updated by buf_LRU_stat_update(). */
  90. static buf_LRU_stat_t buf_LRU_stat_arr[BUF_LRU_STAT_N_INTERVAL];
  91. /** Cursor to buf_LRU_stat_arr[] that is updated in a round-robin fashion. */
  92. static ulint buf_LRU_stat_arr_ind;
  93. /** Current operation counters. Not protected by any mutex. Cleared
  94. by buf_LRU_stat_update(). */
  95. UNIV_INTERN buf_LRU_stat_t buf_LRU_stat_cur;
  96. /** Running sum of past values of buf_LRU_stat_cur.
  97. Updated by buf_LRU_stat_update(). Not Protected by any mutex. */
  98. UNIV_INTERN buf_LRU_stat_t buf_LRU_stat_sum;
  99. /* @} */
  100. /** @name Heuristics for detecting index scan @{ */
  101. /** Move blocks to "new" LRU list only if the first access was at
  102. least this many milliseconds ago. Not protected by any mutex or latch. */
  103. UNIV_INTERN uint buf_LRU_old_threshold_ms;
  104. /* @} */
  105. /******************************************************************//**
  106. Takes a block out of the LRU list and page hash table.
  107. If the block is compressed-only (BUF_BLOCK_ZIP_PAGE),
  108. the object will be freed.
  109. The caller must hold buf_pool->LRU_list_mutex, the buf_page_get_mutex() mutex
  110. and the appropriate hash_lock. This function will release the
  111. buf_page_get_mutex() and the hash_lock.
  112. If a compressed page is freed other compressed pages may be relocated.
  113. @retval true if BUF_BLOCK_FILE_PAGE was removed from page_hash. The
  114. caller needs to free the page to the free list
  115. @retval false if BUF_BLOCK_ZIP_PAGE was removed from page_hash. In
  116. this case the block is already returned to the buddy allocator. */
  117. static MY_ATTRIBUTE((nonnull, warn_unused_result))
  118. bool
  119. buf_LRU_block_remove_hashed(
  120. /*========================*/
  121. buf_page_t* bpage, /*!< in: block, must contain a file page and
  122. be in a state where it can be freed; there
  123. may or may not be a hash index to the page */
  124. bool zip); /*!< in: true if should remove also the
  125. compressed page of an uncompressed page */
  126. /******************************************************************//**
  127. Puts a file page whose has no hash index to the free list. */
  128. static
  129. void
  130. buf_LRU_block_free_hashed_page(
  131. /*===========================*/
  132. buf_block_t* block); /*!< in: block, must contain a file page and
  133. be in a state where it can be freed */
  134. /******************************************************************//**
  135. Increases LRU size in bytes with zip_size for compressed page,
  136. UNIV_PAGE_SIZE for uncompressed page in inline function */
  137. static inline
  138. void
  139. incr_LRU_size_in_bytes(
  140. /*===================*/
  141. buf_page_t* bpage, /*!< in: control block */
  142. buf_pool_t* buf_pool) /*!< in: buffer pool instance */
  143. {
  144. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  145. ulint zip_size = page_zip_get_size(&bpage->zip);
  146. buf_pool->stat.LRU_bytes += zip_size ? zip_size : UNIV_PAGE_SIZE;
  147. ut_ad(buf_pool->stat.LRU_bytes <= buf_pool->curr_pool_size);
  148. }
  149. /******************************************************************//**
  150. Determines if the unzip_LRU list should be used for evicting a victim
  151. instead of the general LRU list.
  152. @return TRUE if should use unzip_LRU */
  153. UNIV_INTERN
  154. ibool
  155. buf_LRU_evict_from_unzip_LRU(
  156. /*=========================*/
  157. buf_pool_t* buf_pool)
  158. {
  159. ulint io_avg;
  160. ulint unzip_avg;
  161. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  162. /* If the unzip_LRU list is empty, we can only use the LRU. */
  163. if (UT_LIST_GET_LEN(buf_pool->unzip_LRU) == 0) {
  164. return(FALSE);
  165. }
  166. /* If unzip_LRU is at most 10% of the size of the LRU list,
  167. then use the LRU. This slack allows us to keep hot
  168. decompressed pages in the buffer pool. */
  169. if (UT_LIST_GET_LEN(buf_pool->unzip_LRU)
  170. <= UT_LIST_GET_LEN(buf_pool->LRU) / 10) {
  171. return(FALSE);
  172. }
  173. /* If eviction hasn't started yet, we assume by default
  174. that a workload is disk bound. */
  175. if (buf_pool->freed_page_clock == 0) {
  176. return(TRUE);
  177. }
  178. /* Calculate the average over past intervals, and add the values
  179. of the current interval. */
  180. io_avg = buf_LRU_stat_sum.io / BUF_LRU_STAT_N_INTERVAL
  181. + buf_LRU_stat_cur.io;
  182. unzip_avg = buf_LRU_stat_sum.unzip / BUF_LRU_STAT_N_INTERVAL
  183. + buf_LRU_stat_cur.unzip;
  184. /* Decide based on our formula. If the load is I/O bound
  185. (unzip_avg is smaller than the weighted io_avg), evict an
  186. uncompressed frame from unzip_LRU. Otherwise we assume that
  187. the load is CPU bound and evict from the regular LRU. */
  188. return(unzip_avg <= io_avg * BUF_LRU_IO_TO_UNZIP_FACTOR);
  189. }
  190. /******************************************************************//**
  191. Attempts to drop page hash index on a batch of pages belonging to a
  192. particular space id. */
  193. static
  194. void
  195. buf_LRU_drop_page_hash_batch(
  196. /*=========================*/
  197. ulint space_id, /*!< in: space id */
  198. ulint zip_size, /*!< in: compressed page size in bytes
  199. or 0 for uncompressed pages */
  200. const ulint* arr, /*!< in: array of page_no */
  201. ulint count) /*!< in: number of entries in array */
  202. {
  203. ulint i;
  204. ut_ad(arr != NULL);
  205. ut_ad(count <= BUF_LRU_DROP_SEARCH_SIZE);
  206. for (i = 0; i < count; ++i) {
  207. btr_search_drop_page_hash_when_freed(space_id, zip_size,
  208. arr[i]);
  209. }
  210. }
  211. /******************************************************************//**
  212. When doing a DROP TABLE/DISCARD TABLESPACE we have to drop all page
  213. hash index entries belonging to that table. This function tries to
  214. do that in batch. Note that this is a 'best effort' attempt and does
  215. not guarantee that ALL hash entries will be removed. */
  216. static
  217. void
  218. buf_LRU_drop_page_hash_for_tablespace(
  219. /*==================================*/
  220. buf_pool_t* buf_pool, /*!< in: buffer pool instance */
  221. ulint id) /*!< in: space id */
  222. {
  223. buf_page_t* bpage;
  224. ulint* page_arr;
  225. ulint num_entries;
  226. ulint zip_size;
  227. zip_size = fil_space_get_zip_size(id);
  228. if (UNIV_UNLIKELY(zip_size == ULINT_UNDEFINED)) {
  229. /* Somehow, the tablespace does not exist. Nothing to drop. */
  230. ut_ad(0);
  231. return;
  232. }
  233. page_arr = static_cast<ulint*>(ut_malloc(
  234. sizeof(ulint) * BUF_LRU_DROP_SEARCH_SIZE));
  235. mutex_enter(&buf_pool->LRU_list_mutex);
  236. num_entries = 0;
  237. scan_again:
  238. bpage = UT_LIST_GET_LAST(buf_pool->LRU);
  239. while (bpage != NULL) {
  240. buf_page_t* prev_bpage;
  241. ibool is_fixed;
  242. ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
  243. prev_bpage = UT_LIST_GET_PREV(LRU, bpage);
  244. ut_a(buf_page_in_file(bpage));
  245. if (buf_page_get_state(bpage) != BUF_BLOCK_FILE_PAGE
  246. || bpage->space != id
  247. || bpage->io_fix != BUF_IO_NONE) {
  248. /* Compressed pages are never hashed.
  249. Skip blocks of other tablespaces.
  250. Skip I/O-fixed blocks (to be dealt with later). */
  251. next_page:
  252. bpage = prev_bpage;
  253. continue;
  254. }
  255. mutex_enter(block_mutex);
  256. is_fixed = bpage->buf_fix_count > 0
  257. || !((buf_block_t*) bpage)->index;
  258. mutex_exit(block_mutex);
  259. if (is_fixed) {
  260. goto next_page;
  261. }
  262. /* Store the page number so that we can drop the hash
  263. index in a batch later. */
  264. page_arr[num_entries] = bpage->offset;
  265. ut_a(num_entries < BUF_LRU_DROP_SEARCH_SIZE);
  266. ++num_entries;
  267. if (num_entries < BUF_LRU_DROP_SEARCH_SIZE) {
  268. goto next_page;
  269. }
  270. /* Array full. We release the buf_pool->LRU_list_mutex to obey
  271. the latching order. */
  272. mutex_exit(&buf_pool->LRU_list_mutex);
  273. buf_LRU_drop_page_hash_batch(
  274. id, zip_size, page_arr, num_entries);
  275. num_entries = 0;
  276. mutex_enter(&buf_pool->LRU_list_mutex);
  277. /* Note that we released the buf_pool->LRU_list_mutex above
  278. after reading the prev_bpage during processing of a
  279. page_hash_batch (i.e.: when the array was full).
  280. Because prev_bpage could belong to a compressed-only
  281. block, it may have been relocated, and thus the
  282. pointer cannot be trusted. Because bpage is of type
  283. buf_block_t, it is safe to dereference.
  284. bpage can change in the LRU list. This is OK because
  285. this function is a 'best effort' to drop as many
  286. search hash entries as possible and it does not
  287. guarantee that ALL such entries will be dropped. */
  288. /* If, however, bpage has been removed from LRU list
  289. to the free list then we should restart the scan. */
  290. if (bpage
  291. && buf_page_get_state(bpage) != BUF_BLOCK_FILE_PAGE) {
  292. goto scan_again;
  293. }
  294. }
  295. mutex_exit(&buf_pool->LRU_list_mutex);
  296. /* Drop any remaining batch of search hashed pages. */
  297. buf_LRU_drop_page_hash_batch(id, zip_size, page_arr, num_entries);
  298. ut_free(page_arr);
  299. }
  300. /******************************************************************//**
  301. While flushing (or removing dirty) pages from a tablespace we don't
  302. want to hog the CPU and resources. Release the buffer pool and block
  303. mutex and try to force a context switch. Then reacquire the same mutexes.
  304. The current page is "fixed" before the release of the mutexes and then
  305. "unfixed" again once we have reacquired the mutexes. */
  306. static MY_ATTRIBUTE((nonnull))
  307. void
  308. buf_flush_yield(
  309. /*============*/
  310. buf_pool_t* buf_pool, /*!< in/out: buffer pool instance */
  311. buf_page_t* bpage) /*!< in/out: current page */
  312. {
  313. ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
  314. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  315. ut_ad(mutex_own(block_mutex));
  316. ut_ad(buf_page_in_file(bpage));
  317. /* "Fix" the block so that the position cannot be
  318. changed after we release the buffer pool and
  319. block mutexes. */
  320. buf_page_set_sticky(bpage);
  321. /* Now it is safe to release the LRU list mutex */
  322. mutex_exit(&buf_pool->LRU_list_mutex);
  323. mutex_exit(block_mutex);
  324. /* Try and force a context switch. */
  325. os_thread_yield();
  326. mutex_enter(&buf_pool->LRU_list_mutex);
  327. mutex_enter(block_mutex);
  328. /* "Unfix" the block now that we have both the
  329. buffer pool and block mutex again. */
  330. buf_page_unset_sticky(bpage);
  331. mutex_exit(block_mutex);
  332. }
  333. /******************************************************************//**
  334. If we have hogged the resources for too long then release the buffer
  335. pool and flush list mutex and do a thread yield. Set the current page
  336. to "sticky" so that it is not relocated during the yield.
  337. @return true if yielded */
  338. static MY_ATTRIBUTE((nonnull(1), warn_unused_result))
  339. bool
  340. buf_flush_try_yield(
  341. /*================*/
  342. buf_pool_t* buf_pool, /*!< in/out: buffer pool instance */
  343. buf_page_t* bpage, /*!< in/out: bpage to remove */
  344. ulint processed, /*!< in: number of pages processed */
  345. bool* must_restart) /*!< in/out: if true, we have to
  346. restart the flush list scan */
  347. {
  348. /* Every BUF_LRU_DROP_SEARCH_SIZE iterations in the
  349. loop we release buf_pool->LRU_list_mutex to let other threads
  350. do their job but only if the block is not IO fixed. This
  351. ensures that the block stays in its position in the
  352. flush_list. */
  353. if (bpage != NULL
  354. && processed >= BUF_LRU_DROP_SEARCH_SIZE
  355. && buf_page_get_io_fix_unlocked(bpage) == BUF_IO_NONE) {
  356. ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
  357. buf_flush_list_mutex_exit(buf_pool);
  358. /* We don't have to worry about bpage becoming a dangling
  359. pointer by a compressed page flush list relocation because
  360. buf_page_get_gen() won't be called for pages from this
  361. tablespace. */
  362. mutex_enter(block_mutex);
  363. /* Recheck the I/O fix and the flush list presence now that we
  364. hold the right mutex */
  365. if (UNIV_UNLIKELY(buf_page_get_io_fix(bpage) != BUF_IO_NONE
  366. || bpage->oldest_modification == 0)) {
  367. mutex_exit(block_mutex);
  368. *must_restart = true;
  369. buf_flush_list_mutex_enter(buf_pool);
  370. return false;
  371. }
  372. *must_restart = false;
  373. /* Release the LRU list and buf_page_get_mutex() mutex
  374. to give the other threads a go. */
  375. buf_flush_yield(buf_pool, bpage);
  376. buf_flush_list_mutex_enter(buf_pool);
  377. /* Should not have been removed from the flush
  378. list during the yield. However, this check is
  379. not sufficient to catch a remove -> add. */
  380. ut_ad(bpage->in_flush_list);
  381. return(true);
  382. }
  383. return(false);
  384. }
  385. /******************************************************************//**
  386. Removes a single page from a given tablespace inside a specific
  387. buffer pool instance.
  388. @return true if page was removed. */
  389. static MY_ATTRIBUTE((nonnull, warn_unused_result))
  390. bool
  391. buf_flush_or_remove_page(
  392. /*=====================*/
  393. buf_pool_t* buf_pool, /*!< in/out: buffer pool instance */
  394. buf_page_t* bpage, /*!< in/out: bpage to remove */
  395. bool flush, /*!< in: flush to disk if true but
  396. don't remove else remove without
  397. flushing to disk */
  398. bool* must_restart) /*!< in/out: if true, must restart the
  399. flush list scan */
  400. {
  401. ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
  402. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  403. ut_ad(buf_flush_list_mutex_own(buf_pool));
  404. /* It is safe to check bpage->space and bpage->io_fix while holding
  405. buf_pool->LRU_list_mutex only. */
  406. if (UNIV_UNLIKELY(buf_page_get_io_fix_unlocked(bpage)
  407. != BUF_IO_NONE)) {
  408. /* We cannot remove this page during this scan
  409. yet; maybe the system is currently reading it
  410. in, or flushing the modifications to the file */
  411. return(false);
  412. }
  413. buf_flush_list_mutex_exit(buf_pool);
  414. /* We don't have to worry about bpage becoming a dangling
  415. pointer by a compressed page flush list relocation because
  416. buf_page_get_gen() won't be called for pages from this
  417. tablespace. */
  418. bool processed;
  419. mutex_enter(block_mutex);
  420. /* Recheck the page I/O fix and the flush list presence now
  421. that we hold the right mutex. */
  422. if (UNIV_UNLIKELY(buf_page_get_io_fix(bpage) != BUF_IO_NONE
  423. || bpage->oldest_modification == 0)) {
  424. /* The page became I/O-fixed or is not on the flush
  425. list anymore, this invalidates any flush-list-page
  426. pointers we have. */
  427. mutex_exit(block_mutex);
  428. *must_restart = true;
  429. processed = false;
  430. } else if (!flush) {
  431. buf_flush_remove(bpage);
  432. mutex_exit(block_mutex);
  433. processed = true;
  434. } else if (buf_flush_ready_for_flush(bpage, BUF_FLUSH_SINGLE_PAGE)) {
  435. if (buf_flush_page(
  436. buf_pool, bpage, BUF_FLUSH_SINGLE_PAGE, false)) {
  437. /* Wake possible simulated aio thread to actually
  438. post the writes to the operating system */
  439. os_aio_simulated_wake_handler_threads();
  440. mutex_enter(&buf_pool->LRU_list_mutex);
  441. processed = true;
  442. } else {
  443. mutex_exit(block_mutex);
  444. processed = false;
  445. }
  446. } else {
  447. mutex_exit(block_mutex);
  448. processed = false;
  449. }
  450. buf_flush_list_mutex_enter(buf_pool);
  451. ut_ad(!mutex_own(block_mutex));
  452. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  453. return(processed);
  454. }
  455. /******************************************************************//**
  456. Remove all dirty pages belonging to a given tablespace inside a specific
  457. buffer pool instance when we are deleting the data file(s) of that
  458. tablespace. The pages still remain a part of LRU and are evicted from
  459. the list as they age towards the tail of the LRU.
  460. @retval DB_SUCCESS if all freed
  461. @retval DB_FAIL if not all freed
  462. @retval DB_INTERRUPTED if the transaction was interrupted */
  463. static MY_ATTRIBUTE((nonnull(1), warn_unused_result))
  464. dberr_t
  465. buf_flush_or_remove_pages(
  466. /*======================*/
  467. buf_pool_t* buf_pool, /*!< buffer pool instance */
  468. ulint id, /*!< in: target space id for which
  469. to remove or flush pages */
  470. bool flush, /*!< in: flush to disk if true but
  471. don't remove else remove without
  472. flushing to disk */
  473. const trx_t* trx) /*!< to check if the operation must
  474. be interrupted, can be 0 */
  475. {
  476. buf_page_t* prev;
  477. buf_page_t* bpage;
  478. ulint processed = 0;
  479. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  480. buf_flush_list_mutex_enter(buf_pool);
  481. rescan:
  482. bool must_restart = false;
  483. bool all_freed = true;
  484. for (bpage = UT_LIST_GET_LAST(buf_pool->flush_list);
  485. bpage != NULL;
  486. bpage = prev) {
  487. ut_ad(!must_restart);
  488. ut_a(buf_page_in_file(bpage));
  489. /* Save the previous link because once we free the
  490. page we can't rely on the links. */
  491. prev = UT_LIST_GET_PREV(list, bpage);
  492. if (buf_page_get_space(bpage) != id) {
  493. /* Skip this block, as it does not belong to
  494. the target space. */
  495. } else if (!buf_flush_or_remove_page(buf_pool, bpage, flush,
  496. &must_restart)) {
  497. /* Remove was unsuccessful, we have to try again
  498. by scanning the entire list from the end.
  499. buf_flush_or_remove_page() released the
  500. flush list mutex but not the LRU list mutex.
  501. Therefore it is possible that a new page was
  502. added to the flush list. For example, in case
  503. where we are at the head of the flush list and
  504. prev == NULL. That is OK because we have the
  505. tablespace quiesced and no new pages for this
  506. space-id should enter flush_list. This is
  507. because the only callers of this function are
  508. DROP TABLE and FLUSH TABLE FOR EXPORT.
  509. We know that we'll have to do at least one more
  510. scan but we don't break out of loop here and
  511. try to do as much work as we can in this
  512. iteration. */
  513. all_freed = false;
  514. if (UNIV_UNLIKELY(must_restart)) {
  515. /* Cannot trust the prev pointer */
  516. break;
  517. }
  518. } else if (flush) {
  519. /* The processing was successful. And during the
  520. processing we have released all the buf_pool mutexes
  521. when calling buf_page_flush(). We cannot trust
  522. prev pointer. */
  523. goto rescan;
  524. }
  525. ut_ad(!must_restart);
  526. ++processed;
  527. /* Yield if we have hogged the CPU and mutexes for too long. */
  528. if (buf_flush_try_yield(buf_pool, prev, processed,
  529. &must_restart)) {
  530. ut_ad(!must_restart);
  531. /* Reset the batch size counter if we had to yield. */
  532. processed = 0;
  533. } else if (UNIV_UNLIKELY(must_restart)) {
  534. /* Cannot trust the prev pointer */
  535. all_freed = false;
  536. break;
  537. }
  538. #ifdef DBUG_OFF
  539. if (flush) {
  540. DBUG_EXECUTE_IF("ib_export_flush_crash",
  541. static ulint n_pages;
  542. if (++n_pages == 4) {DBUG_SUICIDE();});
  543. }
  544. #endif /* DBUG_OFF */
  545. /* The check for trx is interrupted is expensive, we want
  546. to check every N iterations. */
  547. if (!processed && trx && trx_is_interrupted(trx)) {
  548. buf_flush_list_mutex_exit(buf_pool);
  549. return(DB_INTERRUPTED);
  550. }
  551. }
  552. buf_flush_list_mutex_exit(buf_pool);
  553. return(all_freed ? DB_SUCCESS : DB_FAIL);
  554. }
  555. /******************************************************************//**
  556. Remove or flush all the dirty pages that belong to a given tablespace
  557. inside a specific buffer pool instance. The pages will remain in the LRU
  558. list and will be evicted from the LRU list as they age and move towards
  559. the tail of the LRU list. */
  560. static MY_ATTRIBUTE((nonnull(1)))
  561. void
  562. buf_flush_dirty_pages(
  563. /*==================*/
  564. buf_pool_t* buf_pool, /*!< buffer pool instance */
  565. ulint id, /*!< in: space id */
  566. bool flush, /*!< in: flush to disk if true otherwise
  567. remove the pages without flushing */
  568. const trx_t* trx) /*!< to check if the operation must
  569. be interrupted */
  570. {
  571. dberr_t err;
  572. do {
  573. mutex_enter(&buf_pool->LRU_list_mutex);
  574. err = buf_flush_or_remove_pages(buf_pool, id, flush, trx);
  575. mutex_exit(&buf_pool->LRU_list_mutex);
  576. ut_ad(buf_flush_validate(buf_pool));
  577. if (err == DB_FAIL) {
  578. os_thread_sleep(2000);
  579. }
  580. /* DB_FAIL is a soft error, it means that the task wasn't
  581. completed, needs to be retried. */
  582. ut_ad(buf_flush_validate(buf_pool));
  583. } while (err == DB_FAIL);
  584. ut_ad(err == DB_INTERRUPTED
  585. || buf_pool_get_dirty_pages_count(buf_pool, id) == 0);
  586. }
  587. /******************************************************************//**
  588. Remove all pages that belong to a given tablespace inside a specific
  589. buffer pool instance when we are DISCARDing the tablespace. */
  590. static MY_ATTRIBUTE((nonnull))
  591. void
  592. buf_LRU_remove_all_pages(
  593. /*=====================*/
  594. buf_pool_t* buf_pool, /*!< buffer pool instance */
  595. ulint id) /*!< in: space id */
  596. {
  597. buf_page_t* bpage;
  598. ibool all_freed;
  599. scan_again:
  600. mutex_enter(&buf_pool->LRU_list_mutex);
  601. all_freed = TRUE;
  602. for (bpage = UT_LIST_GET_LAST(buf_pool->LRU);
  603. bpage != NULL;
  604. /* No op */) {
  605. prio_rw_lock_t* hash_lock;
  606. buf_page_t* prev_bpage;
  607. ib_mutex_t* block_mutex = NULL;
  608. ut_a(buf_page_in_file(bpage));
  609. ut_ad(bpage->in_LRU_list);
  610. prev_bpage = UT_LIST_GET_PREV(LRU, bpage);
  611. /* It is safe to check bpage->space and bpage->io_fix while
  612. holding buf_pool->LRU_list_mutex only and later recheck
  613. while holding the buf_page_get_mutex() mutex. */
  614. if (buf_page_get_space(bpage) != id) {
  615. /* Skip this block, as it does not belong to
  616. the space that is being invalidated. */
  617. goto next_page;
  618. } else if (UNIV_UNLIKELY(buf_page_get_io_fix_unlocked(bpage)
  619. != BUF_IO_NONE)) {
  620. /* We cannot remove this page during this scan
  621. yet; maybe the system is currently reading it
  622. in, or flushing the modifications to the file */
  623. all_freed = FALSE;
  624. goto next_page;
  625. } else {
  626. ulint fold = buf_page_address_fold(
  627. bpage->space, bpage->offset);
  628. hash_lock = buf_page_hash_lock_get(buf_pool, fold);
  629. rw_lock_x_lock(hash_lock);
  630. block_mutex = buf_page_get_mutex(bpage);
  631. mutex_enter(block_mutex);
  632. if (UNIV_UNLIKELY(
  633. buf_page_get_space(bpage) != id
  634. || bpage->buf_fix_count > 0
  635. || (buf_page_get_io_fix(bpage)
  636. != BUF_IO_NONE))) {
  637. mutex_exit(block_mutex);
  638. rw_lock_x_unlock(hash_lock);
  639. /* We cannot remove this page during
  640. this scan yet; maybe the system is
  641. currently reading it in, or flushing
  642. the modifications to the file */
  643. all_freed = FALSE;
  644. goto next_page;
  645. }
  646. }
  647. ut_ad(mutex_own(block_mutex));
  648. #ifdef UNIV_DEBUG
  649. if (buf_debug_prints) {
  650. fprintf(stderr,
  651. "Dropping space %lu page %lu\n",
  652. (ulong) buf_page_get_space(bpage),
  653. (ulong) buf_page_get_page_no(bpage));
  654. }
  655. #endif
  656. if (buf_page_get_state(bpage) != BUF_BLOCK_FILE_PAGE) {
  657. /* Do nothing, because the adaptive hash index
  658. covers uncompressed pages only. */
  659. } else if (((buf_block_t*) bpage)->index) {
  660. ulint page_no;
  661. ulint zip_size;
  662. mutex_exit(&buf_pool->LRU_list_mutex);
  663. zip_size = buf_page_get_zip_size(bpage);
  664. page_no = buf_page_get_page_no(bpage);
  665. mutex_exit(block_mutex);
  666. rw_lock_x_unlock(hash_lock);
  667. /* Note that the following call will acquire
  668. and release block->lock X-latch. */
  669. btr_search_drop_page_hash_when_freed(
  670. id, zip_size, page_no);
  671. goto scan_again;
  672. }
  673. if (bpage->oldest_modification != 0) {
  674. buf_flush_remove(bpage);
  675. }
  676. ut_ad(!bpage->in_flush_list);
  677. /* Remove from the LRU list. */
  678. if (buf_LRU_block_remove_hashed(bpage, true)) {
  679. mutex_enter(block_mutex);
  680. buf_LRU_block_free_hashed_page((buf_block_t*) bpage);
  681. mutex_exit(block_mutex);
  682. } else {
  683. ut_ad(block_mutex == &buf_pool->zip_mutex);
  684. }
  685. ut_ad(!mutex_own(block_mutex));
  686. #ifdef UNIV_SYNC_DEBUG
  687. /* buf_LRU_block_remove_hashed() releases the hash_lock */
  688. ut_ad(!rw_lock_own(hash_lock, RW_LOCK_EX));
  689. ut_ad(!rw_lock_own(hash_lock, RW_LOCK_SHARED));
  690. #endif /* UNIV_SYNC_DEBUG */
  691. next_page:
  692. bpage = prev_bpage;
  693. }
  694. mutex_exit(&buf_pool->LRU_list_mutex);
  695. if (!all_freed) {
  696. os_thread_sleep(20000);
  697. goto scan_again;
  698. }
  699. }
  700. /******************************************************************//**
  701. Remove pages belonging to a given tablespace inside a specific
  702. buffer pool instance when we are deleting the data file(s) of that
  703. tablespace. The pages still remain a part of LRU and are evicted from
  704. the list as they age towards the tail of the LRU only if buf_remove
  705. is BUF_REMOVE_FLUSH_NO_WRITE. */
  706. static MY_ATTRIBUTE((nonnull(1)))
  707. void
  708. buf_LRU_remove_pages(
  709. /*=================*/
  710. buf_pool_t* buf_pool, /*!< buffer pool instance */
  711. ulint id, /*!< in: space id */
  712. buf_remove_t buf_remove, /*!< in: remove or flush strategy */
  713. const trx_t* trx) /*!< to check if the operation must
  714. be interrupted */
  715. {
  716. switch (buf_remove) {
  717. case BUF_REMOVE_ALL_NO_WRITE:
  718. buf_LRU_remove_all_pages(buf_pool, id);
  719. break;
  720. case BUF_REMOVE_FLUSH_NO_WRITE:
  721. ut_a(trx == 0);
  722. buf_flush_dirty_pages(buf_pool, id, false, NULL);
  723. break;
  724. case BUF_REMOVE_FLUSH_WRITE:
  725. ut_a(trx != 0);
  726. buf_flush_dirty_pages(buf_pool, id, true, trx);
  727. /* Ensure that all asynchronous IO is completed. */
  728. os_aio_wait_until_no_pending_writes();
  729. fil_flush(id);
  730. break;
  731. }
  732. }
  733. /******************************************************************//**
  734. Flushes all dirty pages or removes all pages belonging
  735. to a given tablespace. A PROBLEM: if readahead is being started, what
  736. guarantees that it will not try to read in pages after this operation
  737. has completed? */
  738. UNIV_INTERN
  739. void
  740. buf_LRU_flush_or_remove_pages(
  741. /*==========================*/
  742. ulint id, /*!< in: space id */
  743. buf_remove_t buf_remove, /*!< in: remove or flush strategy */
  744. const trx_t* trx) /*!< to check if the operation must
  745. be interrupted */
  746. {
  747. ulint i;
  748. /* Before we attempt to drop pages one by one we first
  749. attempt to drop page hash index entries in batches to make
  750. it more efficient. The batching attempt is a best effort
  751. attempt and does not guarantee that all pages hash entries
  752. will be dropped. We get rid of remaining page hash entries
  753. one by one below. */
  754. for (i = 0; i < srv_buf_pool_instances; i++) {
  755. buf_pool_t* buf_pool;
  756. buf_pool = buf_pool_from_array(i);
  757. switch (buf_remove) {
  758. case BUF_REMOVE_ALL_NO_WRITE:
  759. buf_LRU_drop_page_hash_for_tablespace(buf_pool, id);
  760. break;
  761. case BUF_REMOVE_FLUSH_NO_WRITE:
  762. /* It is a DROP TABLE for a single table
  763. tablespace. No AHI entries exist because
  764. we already dealt with them when freeing up
  765. extents. */
  766. case BUF_REMOVE_FLUSH_WRITE:
  767. /* We allow read-only queries against the
  768. table, there is no need to drop the AHI entries. */
  769. break;
  770. }
  771. buf_LRU_remove_pages(buf_pool, id, buf_remove, trx);
  772. }
  773. }
  774. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  775. /********************************************************************//**
  776. Insert a compressed block into buf_pool->zip_clean in the LRU order. */
  777. UNIV_INTERN
  778. void
  779. buf_LRU_insert_zip_clean(
  780. /*=====================*/
  781. buf_page_t* bpage) /*!< in: pointer to the block in question */
  782. {
  783. buf_page_t* b;
  784. buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
  785. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  786. ut_ad(mutex_own(&buf_pool->zip_mutex));
  787. ut_ad(buf_page_get_state(bpage) == BUF_BLOCK_ZIP_PAGE);
  788. /* Find the first successor of bpage in the LRU list
  789. that is in the zip_clean list. */
  790. b = bpage;
  791. do {
  792. b = UT_LIST_GET_NEXT(LRU, b);
  793. } while (b && buf_page_get_state(b) != BUF_BLOCK_ZIP_PAGE);
  794. /* Insert bpage before b, i.e., after the predecessor of b. */
  795. if (b) {
  796. b = UT_LIST_GET_PREV(list, b);
  797. }
  798. if (b) {
  799. UT_LIST_INSERT_AFTER(list, buf_pool->zip_clean, b, bpage);
  800. } else {
  801. UT_LIST_ADD_FIRST(list, buf_pool->zip_clean, bpage);
  802. }
  803. }
  804. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  805. /******************************************************************//**
  806. Try to free an uncompressed page of a compressed block from the unzip
  807. LRU list. The compressed page is preserved, and it need not be clean.
  808. @return TRUE if freed */
  809. UNIV_INLINE
  810. ibool
  811. buf_LRU_free_from_unzip_LRU_list(
  812. /*=============================*/
  813. buf_pool_t* buf_pool, /*!< in: buffer pool instance */
  814. ibool scan_all) /*!< in: scan whole LRU list
  815. if TRUE, otherwise scan only
  816. srv_LRU_scan_depth / 2 blocks. */
  817. {
  818. buf_block_t* block;
  819. ibool freed;
  820. ulint scanned;
  821. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  822. if (!buf_LRU_evict_from_unzip_LRU(buf_pool)) {
  823. return(FALSE);
  824. }
  825. for (block = UT_LIST_GET_LAST(buf_pool->unzip_LRU),
  826. scanned = 1, freed = FALSE;
  827. block != NULL && !freed
  828. && (scan_all || scanned < srv_LRU_scan_depth);
  829. ++scanned) {
  830. buf_block_t* prev_block = UT_LIST_GET_PREV(unzip_LRU,
  831. block);
  832. mutex_enter(&block->mutex);
  833. ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  834. ut_ad(block->in_unzip_LRU_list);
  835. ut_ad(block->page.in_LRU_list);
  836. freed = buf_LRU_free_page(&block->page, false);
  837. mutex_exit(&block->mutex);
  838. block = prev_block;
  839. }
  840. MONITOR_INC_VALUE_CUMULATIVE(
  841. MONITOR_LRU_UNZIP_SEARCH_SCANNED,
  842. MONITOR_LRU_UNZIP_SEARCH_SCANNED_NUM_CALL,
  843. MONITOR_LRU_UNZIP_SEARCH_SCANNED_PER_CALL,
  844. scanned);
  845. return(freed);
  846. }
  847. /******************************************************************//**
  848. Try to free a clean page from the common LRU list.
  849. @return TRUE if freed */
  850. UNIV_INLINE
  851. ibool
  852. buf_LRU_free_from_common_LRU_list(
  853. /*==============================*/
  854. buf_pool_t* buf_pool, /*!< in: buffer pool instance */
  855. ibool scan_all) /*!< in: scan whole LRU list
  856. if TRUE, otherwise scan only
  857. srv_LRU_scan_depth / 2 blocks. */
  858. {
  859. buf_page_t* bpage;
  860. ibool freed;
  861. ulint scanned;
  862. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  863. for (bpage = UT_LIST_GET_LAST(buf_pool->LRU),
  864. scanned = 1, freed = FALSE;
  865. bpage != NULL && !freed
  866. && (scan_all || scanned < srv_LRU_scan_depth);
  867. ++scanned) {
  868. unsigned accessed;
  869. buf_page_t* prev_bpage = UT_LIST_GET_PREV(LRU,
  870. bpage);
  871. ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
  872. ut_ad(buf_page_in_file(bpage));
  873. ut_ad(bpage->in_LRU_list);
  874. accessed = buf_page_is_accessed(bpage);
  875. mutex_enter(block_mutex);
  876. freed = buf_LRU_free_page(bpage, true);
  877. mutex_exit(block_mutex);
  878. if (freed && !accessed) {
  879. /* Keep track of pages that are evicted without
  880. ever being accessed. This gives us a measure of
  881. the effectiveness of readahead */
  882. ++buf_pool->stat.n_ra_pages_evicted;
  883. }
  884. bpage = prev_bpage;
  885. }
  886. MONITOR_INC_VALUE_CUMULATIVE(
  887. MONITOR_LRU_SEARCH_SCANNED,
  888. MONITOR_LRU_SEARCH_SCANNED_NUM_CALL,
  889. MONITOR_LRU_SEARCH_SCANNED_PER_CALL,
  890. scanned);
  891. return(freed);
  892. }
  893. /******************************************************************//**
  894. Try to free a replaceable block.
  895. @return TRUE if found and freed */
  896. UNIV_INTERN
  897. ibool
  898. buf_LRU_scan_and_free_block(
  899. /*========================*/
  900. buf_pool_t* buf_pool, /*!< in: buffer pool instance */
  901. ibool scan_all) /*!< in: scan whole LRU list
  902. if TRUE, otherwise scan only
  903. 'old' blocks. */
  904. {
  905. ibool freed = FALSE;
  906. bool use_unzip_list = UT_LIST_GET_LEN(buf_pool->unzip_LRU) > 0;
  907. mutex_enter(&buf_pool->LRU_list_mutex);
  908. if (use_unzip_list) {
  909. freed = buf_LRU_free_from_unzip_LRU_list(buf_pool, scan_all);
  910. }
  911. if (!freed) {
  912. freed = buf_LRU_free_from_common_LRU_list(buf_pool, scan_all);
  913. }
  914. if (!freed) {
  915. mutex_exit(&buf_pool->LRU_list_mutex);
  916. }
  917. return(freed);
  918. }
  919. /******************************************************************//**
  920. Returns TRUE if less than 25 % of the buffer pool in any instance is
  921. available. This can be used in heuristics to prevent huge transactions
  922. eating up the whole buffer pool for their locks.
  923. @return TRUE if less than 25 % of buffer pool left */
  924. UNIV_INTERN
  925. ibool
  926. buf_LRU_buf_pool_running_out(void)
  927. /*==============================*/
  928. {
  929. ulint i;
  930. ibool ret = FALSE;
  931. for (i = 0; i < srv_buf_pool_instances && !ret; i++) {
  932. buf_pool_t* buf_pool;
  933. buf_pool = buf_pool_from_array(i);
  934. if (!recv_recovery_on
  935. && UT_LIST_GET_LEN(buf_pool->free)
  936. + UT_LIST_GET_LEN(buf_pool->LRU)
  937. < buf_pool->curr_size / 4) {
  938. ret = TRUE;
  939. }
  940. }
  941. return(ret);
  942. }
  943. /******************************************************************//**
  944. Returns a free block from the buf_pool. The block is taken off the
  945. free list. If it is empty, returns NULL.
  946. @return a free control block, or NULL if the buf_block->free list is empty */
  947. UNIV_INTERN
  948. buf_block_t*
  949. buf_LRU_get_free_only(
  950. /*==================*/
  951. buf_pool_t* buf_pool)
  952. {
  953. buf_block_t* block;
  954. mutex_enter_last(&buf_pool->free_list_mutex);
  955. block = (buf_block_t*) UT_LIST_GET_LAST(buf_pool->free);
  956. if (block) {
  957. ut_ad(block->page.in_free_list);
  958. ut_d(block->page.in_free_list = FALSE);
  959. ut_ad(!block->page.in_flush_list);
  960. ut_ad(!block->page.in_LRU_list);
  961. ut_a(!buf_page_in_file(&block->page));
  962. UT_LIST_REMOVE(list, buf_pool->free, (&block->page));
  963. buf_block_set_state(block, BUF_BLOCK_READY_FOR_USE);
  964. mutex_exit(&buf_pool->free_list_mutex);
  965. mutex_enter(&block->mutex);
  966. UNIV_MEM_ALLOC(block->frame, UNIV_PAGE_SIZE);
  967. ut_ad(buf_pool_from_block(block) == buf_pool);
  968. mutex_exit(&block->mutex);
  969. return(block);
  970. }
  971. mutex_exit(&buf_pool->free_list_mutex);
  972. return(NULL);
  973. }
  974. /******************************************************************//**
  975. Checks how much of buf_pool is occupied by non-data objects like
  976. AHI, lock heaps etc. Depending on the size of non-data objects this
  977. function will either assert or issue a warning and switch on the
  978. status monitor. */
  979. static
  980. void
  981. buf_LRU_check_size_of_non_data_objects(
  982. /*===================================*/
  983. const buf_pool_t* buf_pool) /*!< in: buffer pool instance */
  984. {
  985. if (!recv_recovery_on && UT_LIST_GET_LEN(buf_pool->free)
  986. + UT_LIST_GET_LEN(buf_pool->LRU) < buf_pool->curr_size / 20) {
  987. ut_print_timestamp(stderr);
  988. fprintf(stderr,
  989. " InnoDB: ERROR: over 95 percent of the buffer pool"
  990. " is occupied by\n"
  991. "InnoDB: lock heaps or the adaptive hash index!"
  992. " Check that your\n"
  993. "InnoDB: transactions do not set too many row locks.\n"
  994. "InnoDB: Your buffer pool size is %lu MB."
  995. " Maybe you should make\n"
  996. "InnoDB: the buffer pool bigger?\n"
  997. "InnoDB: We intentionally generate a seg fault"
  998. " to print a stack trace\n"
  999. "InnoDB: on Linux!\n",
  1000. (ulong) (buf_pool->curr_size
  1001. / (1024 * 1024 / UNIV_PAGE_SIZE)));
  1002. ut_error;
  1003. } else if (!recv_recovery_on
  1004. && (UT_LIST_GET_LEN(buf_pool->free)
  1005. + UT_LIST_GET_LEN(buf_pool->LRU))
  1006. < buf_pool->curr_size / 3) {
  1007. if (!buf_lru_switched_on_innodb_mon) {
  1008. /* Over 67 % of the buffer pool is occupied by lock
  1009. heaps or the adaptive hash index. This may be a memory
  1010. leak! */
  1011. ut_print_timestamp(stderr);
  1012. fprintf(stderr,
  1013. " InnoDB: WARNING: over 67 percent of"
  1014. " the buffer pool is occupied by\n"
  1015. "InnoDB: lock heaps or the adaptive"
  1016. " hash index! Check that your\n"
  1017. "InnoDB: transactions do not set too many"
  1018. " row locks.\n"
  1019. "InnoDB: Your buffer pool size is %lu MB."
  1020. " Maybe you should make\n"
  1021. "InnoDB: the buffer pool bigger?\n"
  1022. "InnoDB: Starting the InnoDB Monitor to print"
  1023. " diagnostics, including\n"
  1024. "InnoDB: lock heap and hash index sizes.\n",
  1025. (ulong) (buf_pool->curr_size
  1026. / (1024 * 1024 / UNIV_PAGE_SIZE)));
  1027. buf_lru_switched_on_innodb_mon = TRUE;
  1028. srv_print_innodb_monitor = TRUE;
  1029. os_event_set(srv_monitor_event);
  1030. }
  1031. } else if (buf_lru_switched_on_innodb_mon) {
  1032. /* Switch off the InnoDB Monitor; this is a simple way
  1033. to stop the monitor if the situation becomes less urgent,
  1034. but may also surprise users if the user also switched on the
  1035. monitor! */
  1036. buf_lru_switched_on_innodb_mon = FALSE;
  1037. srv_print_innodb_monitor = FALSE;
  1038. }
  1039. }
  1040. /** Diagnose failure to get a free page and request InnoDB monitor output in
  1041. the error log if more than two seconds have been spent already.
  1042. @param[in] n_iterations how many buf_LRU_get_free_page iterations
  1043. already completed
  1044. @param[in] started_ms timestamp in ms of when the attempt to get the
  1045. free page started
  1046. @param[in] flush_failures how many times single-page flush, if allowed,
  1047. has failed
  1048. @param[out] mon_value_was previous srv_print_innodb_monitor value
  1049. @param[out] started_monitor whether InnoDB monitor print has been requested
  1050. */
  1051. static
  1052. void
  1053. buf_LRU_handle_lack_of_free_blocks(ulint n_iterations, ulint started_ms,
  1054. ulint flush_failures,
  1055. ibool *mon_value_was,
  1056. ibool *started_monitor)
  1057. {
  1058. static ulint last_printout_ms = 0;
  1059. /* Legacy algorithm started warning after at least 2 seconds, we
  1060. emulate this. */
  1061. const ulint current_ms = ut_time_ms();
  1062. if ((current_ms > started_ms + 2000)
  1063. && (current_ms > last_printout_ms + 2000)) {
  1064. ut_print_timestamp(stderr);
  1065. fprintf(stderr,
  1066. " InnoDB: Warning: difficult to find free blocks in\n"
  1067. "InnoDB: the buffer pool (%lu search iterations)!\n"
  1068. "InnoDB: %lu failed attempts to flush a page!"
  1069. " Consider\n"
  1070. "InnoDB: increasing the buffer pool size.\n"
  1071. "InnoDB: It is also possible that"
  1072. " in your Unix version\n"
  1073. "InnoDB: fsync is very slow, or"
  1074. " completely frozen inside\n"
  1075. "InnoDB: the OS kernel. Then upgrading to"
  1076. " a newer version\n"
  1077. "InnoDB: of your operating system may help."
  1078. " Look at the\n"
  1079. "InnoDB: number of fsyncs in diagnostic info below.\n"
  1080. "InnoDB: Pending flushes (fsync) log: %lu;"
  1081. " buffer pool: %lu\n"
  1082. "InnoDB: %lu OS file reads, %lu OS file writes,"
  1083. " %lu OS fsyncs\n"
  1084. "InnoDB: Starting InnoDB Monitor to print further\n"
  1085. "InnoDB: diagnostics to the standard output.\n",
  1086. (ulong) n_iterations,
  1087. (ulong) flush_failures,
  1088. (ulong) fil_n_pending_log_flushes,
  1089. (ulong) fil_n_pending_tablespace_flushes,
  1090. (ulong) os_n_file_reads, (ulong) os_n_file_writes,
  1091. (ulong) os_n_fsyncs);
  1092. last_printout_ms = current_ms;
  1093. *mon_value_was = srv_print_innodb_monitor;
  1094. *started_monitor = TRUE;
  1095. srv_print_innodb_monitor = TRUE;
  1096. os_event_set(lock_sys->timeout_event);
  1097. }
  1098. }
  1099. /** The maximum allowed backoff sleep time duration, microseconds */
  1100. #define MAX_FREE_LIST_BACKOFF_SLEEP 10000
  1101. /** The sleep reduction factor for high-priority waiter backoff sleeps */
  1102. #define FREE_LIST_BACKOFF_HIGH_PRIO_DIVIDER 100
  1103. /** The sleep reduction factor for low-priority waiter backoff sleeps */
  1104. #define FREE_LIST_BACKOFF_LOW_PRIO_DIVIDER 1
  1105. /******************************************************************//**
  1106. Returns a free block from the buf_pool. The block is taken off the
  1107. free list. If free list is empty, blocks are moved from the end of the
  1108. LRU list to the free list.
  1109. This function is called from a user thread when it needs a clean
  1110. block to read in a page. Note that we only ever get a block from
  1111. the free list. Even when we flush a page or find a page in LRU scan
  1112. we put it to free list to be used.
  1113. * iteration 0:
  1114. * get a block from free list, success:done
  1115. * if there is an LRU flush batch in progress:
  1116. * wait for batch to end: retry free list
  1117. * if buf_pool->try_LRU_scan is set
  1118. * scan LRU up to srv_LRU_scan_depth to find a clean block
  1119. * the above will put the block on free list
  1120. * success:retry the free list
  1121. * flush one dirty page from tail of LRU to disk
  1122. * the above will put the block on free list
  1123. * success: retry the free list
  1124. * iteration 1:
  1125. * same as iteration 0 except:
  1126. * scan whole LRU list
  1127. * scan LRU list even if buf_pool->try_LRU_scan is not set
  1128. * iteration > 1:
  1129. * same as iteration 1 but sleep 100ms
  1130. @return the free control block, in state BUF_BLOCK_READY_FOR_USE */
  1131. UNIV_INTERN
  1132. buf_block_t*
  1133. buf_LRU_get_free_block(
  1134. /*===================*/
  1135. buf_pool_t* buf_pool) /*!< in/out: buffer pool instance */
  1136. {
  1137. buf_block_t* block = NULL;
  1138. ibool freed = FALSE;
  1139. ulint n_iterations = 0;
  1140. ulint flush_failures = 0;
  1141. ibool mon_value_was = FALSE;
  1142. ibool started_monitor = FALSE;
  1143. ulint started_ms = 0;
  1144. ut_ad(!mutex_own(&buf_pool->LRU_list_mutex));
  1145. MONITOR_INC(MONITOR_LRU_GET_FREE_SEARCH);
  1146. loop:
  1147. buf_LRU_check_size_of_non_data_objects(buf_pool);
  1148. /* If there is a block in the free list, take it */
  1149. if (DBUG_EVALUATE_IF("simulate_lack_of_pages", true, false)) {
  1150. block = NULL;
  1151. if (srv_debug_monitor_printed)
  1152. DBUG_SET("-d,simulate_lack_of_pages");
  1153. } else if (DBUG_EVALUATE_IF("simulate_recovery_lack_of_pages",
  1154. recv_recovery_on, false)) {
  1155. block = NULL;
  1156. if (srv_debug_monitor_printed)
  1157. DBUG_SUICIDE();
  1158. } else {
  1159. block = buf_LRU_get_free_only(buf_pool);
  1160. }
  1161. if (block) {
  1162. ut_ad(buf_pool_from_block(block) == buf_pool);
  1163. memset(&block->page.zip, 0, sizeof block->page.zip);
  1164. if (started_monitor) {
  1165. srv_print_innodb_monitor =
  1166. static_cast<my_bool>(mon_value_was);
  1167. }
  1168. return(block);
  1169. }
  1170. if (!started_ms)
  1171. started_ms = ut_time_ms();
  1172. if (srv_empty_free_list_algorithm == SRV_EMPTY_FREE_LIST_BACKOFF
  1173. && buf_lru_manager_is_active
  1174. && (srv_shutdown_state == SRV_SHUTDOWN_NONE
  1175. || srv_shutdown_state == SRV_SHUTDOWN_CLEANUP)) {
  1176. /* Backoff to minimize the free list mutex contention while the
  1177. free list is empty */
  1178. ulint priority = srv_current_thread_priority;
  1179. if (n_iterations < 3) {
  1180. os_thread_yield();
  1181. if (!priority) {
  1182. os_thread_yield();
  1183. }
  1184. } else {
  1185. ulint i, b;
  1186. if (n_iterations < 6) {
  1187. i = n_iterations - 3;
  1188. } else if (n_iterations < 8) {
  1189. i = 4;
  1190. } else if (n_iterations < 11) {
  1191. i = 5;
  1192. } else {
  1193. i = n_iterations - 5;
  1194. }
  1195. b = 1 << i;
  1196. if (b > MAX_FREE_LIST_BACKOFF_SLEEP) {
  1197. b = MAX_FREE_LIST_BACKOFF_SLEEP;
  1198. }
  1199. os_thread_sleep(b / (priority
  1200. ? FREE_LIST_BACKOFF_HIGH_PRIO_DIVIDER
  1201. : FREE_LIST_BACKOFF_LOW_PRIO_DIVIDER));
  1202. }
  1203. buf_LRU_handle_lack_of_free_blocks(n_iterations, started_ms,
  1204. flush_failures,
  1205. &mon_value_was,
  1206. &started_monitor);
  1207. n_iterations++;
  1208. srv_stats.buf_pool_wait_free.add(n_iterations, 1);
  1209. /* In case of backoff, do not ever attempt single page flushes
  1210. and wait for the cleaner to free some pages instead. */
  1211. goto loop;
  1212. } else {
  1213. /* The LRU manager is not running or Oracle MySQL 5.6 algorithm
  1214. was requested, will perform a single page flush */
  1215. ut_ad((srv_empty_free_list_algorithm
  1216. == SRV_EMPTY_FREE_LIST_LEGACY)
  1217. || !buf_lru_manager_is_active
  1218. || (srv_shutdown_state != SRV_SHUTDOWN_NONE
  1219. && srv_shutdown_state != SRV_SHUTDOWN_CLEANUP));
  1220. }
  1221. mutex_enter(&buf_pool->flush_state_mutex);
  1222. if (buf_pool->init_flush[BUF_FLUSH_LRU]
  1223. && srv_use_doublewrite_buf
  1224. && buf_dblwr != NULL) {
  1225. mutex_exit(&buf_pool->flush_state_mutex);
  1226. /* If there is an LRU flush happening in the background
  1227. then we wait for it to end instead of trying a single
  1228. page flush. If, however, we are not using doublewrite
  1229. buffer then it is better to do our own single page
  1230. flush instead of waiting for LRU flush to end. */
  1231. buf_flush_wait_batch_end(buf_pool, BUF_FLUSH_LRU);
  1232. goto loop;
  1233. }
  1234. mutex_exit(&buf_pool->flush_state_mutex);
  1235. if (DBUG_EVALUATE_IF("simulate_recovery_lack_of_pages", true, false)
  1236. || DBUG_EVALUATE_IF("simulate_lack_of_pages", true, false)) {
  1237. buf_pool->try_LRU_scan = false;
  1238. }
  1239. freed = FALSE;
  1240. if (buf_pool->try_LRU_scan || n_iterations > 0) {
  1241. /* If no block was in the free list, search from the
  1242. end of the LRU list and try to free a block there.
  1243. If we are doing for the first time we'll scan only
  1244. tail of the LRU list otherwise we scan the whole LRU
  1245. list. */
  1246. freed = buf_LRU_scan_and_free_block(buf_pool,
  1247. n_iterations > 0);
  1248. if (!freed && n_iterations == 0) {
  1249. /* Tell other threads that there is no point
  1250. in scanning the LRU list. This flag is set to
  1251. TRUE again when we flush a batch from this
  1252. buffer pool. */
  1253. buf_pool->try_LRU_scan = FALSE;
  1254. }
  1255. }
  1256. if (freed) {
  1257. goto loop;
  1258. }
  1259. buf_LRU_handle_lack_of_free_blocks(n_iterations, started_ms,
  1260. flush_failures, &mon_value_was,
  1261. &started_monitor);
  1262. /* If we have scanned the whole LRU and still are unable to
  1263. find a free block then we should sleep here to let the
  1264. page_cleaner do an LRU batch for us.
  1265. TODO: It'd be better if we can signal the page_cleaner. Perhaps
  1266. we should use timed wait for page_cleaner. */
  1267. if (n_iterations > 1) {
  1268. os_thread_sleep(100000);
  1269. }
  1270. /* No free block was found: try to flush the LRU list.
  1271. This call will flush one page from the LRU and put it on the
  1272. free list. That means that the free block is up for grabs for
  1273. all user threads.
  1274. TODO: A more elegant way would have been to return the freed
  1275. up block to the caller here but the code that deals with
  1276. removing the block from page_hash and LRU_list is fairly
  1277. involved (particularly in case of compressed pages). We
  1278. can do that in a separate patch sometime in future. */
  1279. if (!buf_flush_single_page_from_LRU(buf_pool)) {
  1280. MONITOR_INC(MONITOR_LRU_SINGLE_FLUSH_FAILURE_COUNT);
  1281. ++flush_failures;
  1282. }
  1283. srv_stats.buf_pool_wait_free.add(n_iterations, 1);
  1284. n_iterations++;
  1285. goto loop;
  1286. }
  1287. /*******************************************************************//**
  1288. Moves the LRU_old pointer so that the length of the old blocks list
  1289. is inside the allowed limits. */
  1290. UNIV_INLINE
  1291. void
  1292. buf_LRU_old_adjust_len(
  1293. /*===================*/
  1294. buf_pool_t* buf_pool) /*!< in: buffer pool instance */
  1295. {
  1296. ulint old_len;
  1297. ulint new_len;
  1298. ut_a(buf_pool->LRU_old);
  1299. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  1300. ut_ad(buf_pool->LRU_old_ratio >= BUF_LRU_OLD_RATIO_MIN);
  1301. ut_ad(buf_pool->LRU_old_ratio <= BUF_LRU_OLD_RATIO_MAX);
  1302. #if BUF_LRU_OLD_RATIO_MIN * BUF_LRU_OLD_MIN_LEN <= BUF_LRU_OLD_RATIO_DIV * (BUF_LRU_OLD_TOLERANCE + 5)
  1303. # error "BUF_LRU_OLD_RATIO_MIN * BUF_LRU_OLD_MIN_LEN <= BUF_LRU_OLD_RATIO_DIV * (BUF_LRU_OLD_TOLERANCE + 5)"
  1304. #endif
  1305. #ifdef UNIV_LRU_DEBUG
  1306. /* buf_pool->LRU_old must be the first item in the LRU list
  1307. whose "old" flag is set. */
  1308. ut_a(buf_pool->LRU_old->old);
  1309. ut_a(!UT_LIST_GET_PREV(LRU, buf_pool->LRU_old)
  1310. || !UT_LIST_GET_PREV(LRU, buf_pool->LRU_old)->old);
  1311. ut_a(!UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old)
  1312. || UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old)->old);
  1313. #endif /* UNIV_LRU_DEBUG */
  1314. old_len = buf_pool->LRU_old_len;
  1315. new_len = ut_min(UT_LIST_GET_LEN(buf_pool->LRU)
  1316. * buf_pool->LRU_old_ratio / BUF_LRU_OLD_RATIO_DIV,
  1317. UT_LIST_GET_LEN(buf_pool->LRU)
  1318. - (BUF_LRU_OLD_TOLERANCE
  1319. + BUF_LRU_NON_OLD_MIN_LEN));
  1320. for (;;) {
  1321. buf_page_t* LRU_old = buf_pool->LRU_old;
  1322. ut_a(LRU_old);
  1323. ut_ad(LRU_old->in_LRU_list);
  1324. #ifdef UNIV_LRU_DEBUG
  1325. ut_a(LRU_old->old);
  1326. #endif /* UNIV_LRU_DEBUG */
  1327. /* Update the LRU_old pointer if necessary */
  1328. if (old_len + BUF_LRU_OLD_TOLERANCE < new_len) {
  1329. buf_pool->LRU_old = LRU_old = UT_LIST_GET_PREV(
  1330. LRU, LRU_old);
  1331. #ifdef UNIV_LRU_DEBUG
  1332. ut_a(!LRU_old->old);
  1333. #endif /* UNIV_LRU_DEBUG */
  1334. old_len = ++buf_pool->LRU_old_len;
  1335. buf_page_set_old(LRU_old, TRUE);
  1336. } else if (old_len > new_len + BUF_LRU_OLD_TOLERANCE) {
  1337. buf_pool->LRU_old = UT_LIST_GET_NEXT(LRU, LRU_old);
  1338. old_len = --buf_pool->LRU_old_len;
  1339. buf_page_set_old(LRU_old, FALSE);
  1340. } else {
  1341. return;
  1342. }
  1343. }
  1344. }
  1345. /*******************************************************************//**
  1346. Initializes the old blocks pointer in the LRU list. This function should be
  1347. called when the LRU list grows to BUF_LRU_OLD_MIN_LEN length. */
  1348. static
  1349. void
  1350. buf_LRU_old_init(
  1351. /*=============*/
  1352. buf_pool_t* buf_pool)
  1353. {
  1354. buf_page_t* bpage;
  1355. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  1356. ut_a(UT_LIST_GET_LEN(buf_pool->LRU) == BUF_LRU_OLD_MIN_LEN);
  1357. /* We first initialize all blocks in the LRU list as old and then use
  1358. the adjust function to move the LRU_old pointer to the right
  1359. position */
  1360. for (bpage = UT_LIST_GET_LAST(buf_pool->LRU); bpage != NULL;
  1361. bpage = UT_LIST_GET_PREV(LRU, bpage)) {
  1362. ut_ad(bpage->in_LRU_list);
  1363. ut_ad(buf_page_in_file(bpage));
  1364. /* This loop temporarily violates the
  1365. assertions of buf_page_set_old(). */
  1366. bpage->old = TRUE;
  1367. }
  1368. buf_pool->LRU_old = UT_LIST_GET_FIRST(buf_pool->LRU);
  1369. buf_pool->LRU_old_len = UT_LIST_GET_LEN(buf_pool->LRU);
  1370. buf_LRU_old_adjust_len(buf_pool);
  1371. }
  1372. /******************************************************************//**
  1373. Remove a block from the unzip_LRU list if it belonged to the list. */
  1374. static
  1375. void
  1376. buf_unzip_LRU_remove_block_if_needed(
  1377. /*=================================*/
  1378. buf_page_t* bpage) /*!< in/out: control block */
  1379. {
  1380. buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
  1381. ut_ad(buf_pool);
  1382. ut_ad(bpage);
  1383. ut_ad(buf_page_in_file(bpage));
  1384. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  1385. if (buf_page_belongs_to_unzip_LRU(bpage)) {
  1386. buf_block_t* block = (buf_block_t*) bpage;
  1387. ut_ad(block->in_unzip_LRU_list);
  1388. ut_d(block->in_unzip_LRU_list = FALSE);
  1389. UT_LIST_REMOVE(unzip_LRU, buf_pool->unzip_LRU, block);
  1390. }
  1391. }
  1392. /******************************************************************//**
  1393. Removes a block from the LRU list. */
  1394. UNIV_INLINE
  1395. void
  1396. buf_LRU_remove_block(
  1397. /*=================*/
  1398. buf_page_t* bpage) /*!< in: control block */
  1399. {
  1400. buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
  1401. ulint zip_size;
  1402. ut_ad(buf_pool);
  1403. ut_ad(bpage);
  1404. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  1405. ut_a(buf_page_in_file(bpage));
  1406. ut_ad(bpage->in_LRU_list);
  1407. /* If the LRU_old pointer is defined and points to just this block,
  1408. move it backward one step */
  1409. if (UNIV_UNLIKELY(bpage == buf_pool->LRU_old)) {
  1410. /* Below: the previous block is guaranteed to exist,
  1411. because the LRU_old pointer is only allowed to differ
  1412. by BUF_LRU_OLD_TOLERANCE from strict
  1413. buf_pool->LRU_old_ratio/BUF_LRU_OLD_RATIO_DIV of the LRU
  1414. list length. */
  1415. buf_page_t* prev_bpage = UT_LIST_GET_PREV(LRU, bpage);
  1416. ut_a(prev_bpage);
  1417. #ifdef UNIV_LRU_DEBUG
  1418. ut_a(!prev_bpage->old);
  1419. #endif /* UNIV_LRU_DEBUG */
  1420. buf_pool->LRU_old = prev_bpage;
  1421. buf_page_set_old(prev_bpage, TRUE);
  1422. buf_pool->LRU_old_len++;
  1423. }
  1424. /* Remove the block from the LRU list */
  1425. UT_LIST_REMOVE(LRU, buf_pool->LRU, bpage);
  1426. ut_d(bpage->in_LRU_list = FALSE);
  1427. zip_size = page_zip_get_size(&bpage->zip);
  1428. buf_pool->stat.LRU_bytes -= zip_size ? zip_size : UNIV_PAGE_SIZE;
  1429. buf_unzip_LRU_remove_block_if_needed(bpage);
  1430. /* If the LRU list is so short that LRU_old is not defined,
  1431. clear the "old" flags and return */
  1432. if (UT_LIST_GET_LEN(buf_pool->LRU) < BUF_LRU_OLD_MIN_LEN) {
  1433. for (bpage = UT_LIST_GET_FIRST(buf_pool->LRU); bpage != NULL;
  1434. bpage = UT_LIST_GET_NEXT(LRU, bpage)) {
  1435. /* This loop temporarily violates the
  1436. assertions of buf_page_set_old(). */
  1437. bpage->old = FALSE;
  1438. }
  1439. buf_pool->LRU_old = NULL;
  1440. buf_pool->LRU_old_len = 0;
  1441. return;
  1442. }
  1443. ut_ad(buf_pool->LRU_old);
  1444. /* Update the LRU_old_len field if necessary */
  1445. if (buf_page_is_old(bpage)) {
  1446. buf_pool->LRU_old_len--;
  1447. }
  1448. /* Adjust the length of the old block list if necessary */
  1449. buf_LRU_old_adjust_len(buf_pool);
  1450. }
  1451. /******************************************************************//**
  1452. Adds a block to the LRU list of decompressed zip pages. */
  1453. UNIV_INTERN
  1454. void
  1455. buf_unzip_LRU_add_block(
  1456. /*====================*/
  1457. buf_block_t* block, /*!< in: control block */
  1458. ibool old) /*!< in: TRUE if should be put to the end
  1459. of the list, else put to the start */
  1460. {
  1461. buf_pool_t* buf_pool = buf_pool_from_block(block);
  1462. ut_ad(buf_pool);
  1463. ut_ad(block);
  1464. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  1465. ut_a(buf_page_belongs_to_unzip_LRU(&block->page));
  1466. ut_ad(!block->in_unzip_LRU_list);
  1467. ut_d(block->in_unzip_LRU_list = TRUE);
  1468. if (old) {
  1469. UT_LIST_ADD_LAST(unzip_LRU, buf_pool->unzip_LRU, block);
  1470. } else {
  1471. UT_LIST_ADD_FIRST(unzip_LRU, buf_pool->unzip_LRU, block);
  1472. }
  1473. }
  1474. /******************************************************************//**
  1475. Adds a block to the LRU list end. Please make sure that the zip_size is
  1476. already set into the page zip when invoking the function, so that we
  1477. can get correct zip_size from the buffer page when adding a block
  1478. into LRU */
  1479. UNIV_INLINE
  1480. void
  1481. buf_LRU_add_block_to_end_low(
  1482. /*=========================*/
  1483. buf_page_t* bpage) /*!< in: control block */
  1484. {
  1485. buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
  1486. ut_ad(buf_pool);
  1487. ut_ad(bpage);
  1488. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  1489. ut_a(buf_page_in_file(bpage));
  1490. ut_ad(!bpage->in_LRU_list);
  1491. UT_LIST_ADD_LAST(LRU, buf_pool->LRU, bpage);
  1492. ut_d(bpage->in_LRU_list = TRUE);
  1493. incr_LRU_size_in_bytes(bpage, buf_pool);
  1494. if (UT_LIST_GET_LEN(buf_pool->LRU) > BUF_LRU_OLD_MIN_LEN) {
  1495. ut_ad(buf_pool->LRU_old);
  1496. /* Adjust the length of the old block list if necessary */
  1497. buf_page_set_old(bpage, TRUE);
  1498. buf_pool->LRU_old_len++;
  1499. buf_LRU_old_adjust_len(buf_pool);
  1500. } else if (UT_LIST_GET_LEN(buf_pool->LRU) == BUF_LRU_OLD_MIN_LEN) {
  1501. /* The LRU list is now long enough for LRU_old to become
  1502. defined: init it */
  1503. buf_LRU_old_init(buf_pool);
  1504. } else {
  1505. buf_page_set_old(bpage, buf_pool->LRU_old != NULL);
  1506. }
  1507. /* If this is a zipped block with decompressed frame as well
  1508. then put it on the unzip_LRU list */
  1509. if (buf_page_belongs_to_unzip_LRU(bpage)) {
  1510. buf_unzip_LRU_add_block((buf_block_t*) bpage, TRUE);
  1511. }
  1512. }
  1513. /******************************************************************//**
  1514. Adds a block to the LRU list. Please make sure that the zip_size is
  1515. already set into the page zip when invoking the function, so that we
  1516. can get correct zip_size from the buffer page when adding a block
  1517. into LRU */
  1518. UNIV_INLINE
  1519. void
  1520. buf_LRU_add_block_low(
  1521. /*==================*/
  1522. buf_page_t* bpage, /*!< in: control block */
  1523. ibool old) /*!< in: TRUE if should be put to the old blocks
  1524. in the LRU list, else put to the start; if the
  1525. LRU list is very short, the block is added to
  1526. the start, regardless of this parameter */
  1527. {
  1528. buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
  1529. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  1530. ut_a(buf_page_in_file(bpage));
  1531. ut_ad(!bpage->in_LRU_list);
  1532. if (!old || (UT_LIST_GET_LEN(buf_pool->LRU) < BUF_LRU_OLD_MIN_LEN)) {
  1533. UT_LIST_ADD_FIRST(LRU, buf_pool->LRU, bpage);
  1534. bpage->freed_page_clock = buf_pool->freed_page_clock;
  1535. } else {
  1536. #ifdef UNIV_LRU_DEBUG
  1537. /* buf_pool->LRU_old must be the first item in the LRU list
  1538. whose "old" flag is set. */
  1539. ut_a(buf_pool->LRU_old->old);
  1540. ut_a(!UT_LIST_GET_PREV(LRU, buf_pool->LRU_old)
  1541. || !UT_LIST_GET_PREV(LRU, buf_pool->LRU_old)->old);
  1542. ut_a(!UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old)
  1543. || UT_LIST_GET_NEXT(LRU, buf_pool->LRU_old)->old);
  1544. #endif /* UNIV_LRU_DEBUG */
  1545. UT_LIST_INSERT_AFTER(LRU, buf_pool->LRU, buf_pool->LRU_old,
  1546. bpage);
  1547. buf_pool->LRU_old_len++;
  1548. }
  1549. ut_d(bpage->in_LRU_list = TRUE);
  1550. incr_LRU_size_in_bytes(bpage, buf_pool);
  1551. if (UT_LIST_GET_LEN(buf_pool->LRU) > BUF_LRU_OLD_MIN_LEN) {
  1552. ut_ad(buf_pool->LRU_old);
  1553. /* Adjust the length of the old block list if necessary */
  1554. buf_page_set_old(bpage, old);
  1555. buf_LRU_old_adjust_len(buf_pool);
  1556. } else if (UT_LIST_GET_LEN(buf_pool->LRU) == BUF_LRU_OLD_MIN_LEN) {
  1557. /* The LRU list is now long enough for LRU_old to become
  1558. defined: init it */
  1559. buf_LRU_old_init(buf_pool);
  1560. } else {
  1561. buf_page_set_old(bpage, buf_pool->LRU_old != NULL);
  1562. }
  1563. /* If this is a zipped block with decompressed frame as well
  1564. then put it on the unzip_LRU list */
  1565. if (buf_page_belongs_to_unzip_LRU(bpage)) {
  1566. buf_unzip_LRU_add_block((buf_block_t*) bpage, old);
  1567. }
  1568. }
  1569. /******************************************************************//**
  1570. Adds a block to the LRU list. Please make sure that the zip_size is
  1571. already set into the page zip when invoking the function, so that we
  1572. can get correct zip_size from the buffer page when adding a block
  1573. into LRU */
  1574. UNIV_INTERN
  1575. void
  1576. buf_LRU_add_block(
  1577. /*==============*/
  1578. buf_page_t* bpage, /*!< in: control block */
  1579. ibool old) /*!< in: TRUE if should be put to the old
  1580. blocks in the LRU list, else put to the start;
  1581. if the LRU list is very short, the block is
  1582. added to the start, regardless of this
  1583. parameter */
  1584. {
  1585. buf_LRU_add_block_low(bpage, old);
  1586. }
  1587. /******************************************************************//**
  1588. Moves a block to the start of the LRU list. */
  1589. UNIV_INTERN
  1590. void
  1591. buf_LRU_make_block_young(
  1592. /*=====================*/
  1593. buf_page_t* bpage) /*!< in: control block */
  1594. {
  1595. buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
  1596. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  1597. if (bpage->old) {
  1598. buf_pool->stat.n_pages_made_young++;
  1599. }
  1600. buf_LRU_remove_block(bpage);
  1601. buf_LRU_add_block_low(bpage, FALSE);
  1602. }
  1603. /******************************************************************//**
  1604. Moves a block to the end of the LRU list. */
  1605. UNIV_INTERN
  1606. void
  1607. buf_LRU_make_block_old(
  1608. /*===================*/
  1609. buf_page_t* bpage) /*!< in: control block */
  1610. {
  1611. buf_LRU_remove_block(bpage);
  1612. buf_LRU_add_block_to_end_low(bpage);
  1613. }
  1614. /******************************************************************//**
  1615. Try to free a block. If bpage is a descriptor of a compressed-only
  1616. page, the descriptor object will be freed as well.
  1617. NOTE: If this function returns true, it will release the LRU list mutex,
  1618. and temporarily release and relock the buf_page_get_mutex() mutex.
  1619. Furthermore, the page frame will no longer be accessible via bpage. If this
  1620. function returns false, the buf_page_get_mutex() might be temporarily released
  1621. and relocked too.
  1622. The caller must hold the LRU list and buf_page_get_mutex() mutexes.
  1623. @return true if freed, false otherwise. */
  1624. UNIV_INTERN
  1625. bool
  1626. buf_LRU_free_page(
  1627. /*===============*/
  1628. buf_page_t* bpage, /*!< in: block to be freed */
  1629. bool zip) /*!< in: true if should remove also the
  1630. compressed page of an uncompressed page */
  1631. {
  1632. buf_page_t* b = NULL;
  1633. buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
  1634. const ulint fold = buf_page_address_fold(bpage->space,
  1635. bpage->offset);
  1636. prio_rw_lock_t* hash_lock = buf_page_hash_lock_get(buf_pool, fold);
  1637. ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
  1638. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  1639. ut_ad(mutex_own(block_mutex));
  1640. ut_ad(buf_page_in_file(bpage));
  1641. ut_ad(bpage->in_LRU_list);
  1642. if (!buf_page_can_relocate(bpage)) {
  1643. /* Do not free buffer fixed or I/O-fixed blocks. */
  1644. return(false);
  1645. }
  1646. #ifdef UNIV_IBUF_COUNT_DEBUG
  1647. ut_a(ibuf_count_get(bpage->space, bpage->offset) == 0);
  1648. #endif /* UNIV_IBUF_COUNT_DEBUG */
  1649. if (zip || !bpage->zip.data) {
  1650. /* This would completely free the block. */
  1651. /* Do not completely free dirty blocks. */
  1652. if (bpage->oldest_modification) {
  1653. return(false);
  1654. }
  1655. } else if (bpage->oldest_modification > 0
  1656. && buf_page_get_state(bpage) != BUF_BLOCK_FILE_PAGE) {
  1657. ut_ad(buf_page_get_state(bpage) == BUF_BLOCK_ZIP_DIRTY);
  1658. return(false);
  1659. } else if (buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE) {
  1660. b = buf_page_alloc_descriptor();
  1661. ut_a(b);
  1662. }
  1663. ut_ad(buf_page_in_file(bpage));
  1664. ut_ad(bpage->in_LRU_list);
  1665. ut_ad(!bpage->in_flush_list == !bpage->oldest_modification);
  1666. #ifdef UNIV_DEBUG
  1667. if (buf_debug_prints) {
  1668. fprintf(stderr, "Putting space %lu page %lu to free list\n",
  1669. (ulong) buf_page_get_space(bpage),
  1670. (ulong) buf_page_get_page_no(bpage));
  1671. }
  1672. #endif /* UNIV_DEBUG */
  1673. mutex_exit(block_mutex);
  1674. rw_lock_x_lock(hash_lock);
  1675. mutex_enter(block_mutex);
  1676. if (UNIV_UNLIKELY(!buf_page_can_relocate(bpage)
  1677. || ((zip || !bpage->zip.data)
  1678. && bpage->oldest_modification))) {
  1679. not_freed:
  1680. rw_lock_x_unlock(hash_lock);
  1681. if (b) {
  1682. buf_page_free_descriptor(b);
  1683. }
  1684. return(false);
  1685. } else if (UNIV_UNLIKELY(bpage->oldest_modification
  1686. && (buf_page_get_state(bpage)
  1687. != BUF_BLOCK_FILE_PAGE))) {
  1688. ut_ad(buf_page_get_state(bpage)
  1689. == BUF_BLOCK_ZIP_DIRTY);
  1690. goto not_freed;
  1691. }
  1692. if (b) {
  1693. memcpy(b, bpage, sizeof *b);
  1694. }
  1695. if (!buf_LRU_block_remove_hashed(bpage, zip)) {
  1696. mutex_exit(&buf_pool->LRU_list_mutex);
  1697. if (b) {
  1698. buf_page_free_descriptor(b);
  1699. }
  1700. mutex_enter(block_mutex);
  1701. return(true);
  1702. }
  1703. #ifdef UNIV_SYNC_DEBUG
  1704. /* buf_LRU_block_remove_hashed() releases the hash_lock */
  1705. ut_ad(!rw_lock_own(hash_lock, RW_LOCK_EX)
  1706. && !rw_lock_own(hash_lock, RW_LOCK_SHARED));
  1707. #endif /* UNIV_SYNC_DEBUG */
  1708. /* We have just freed a BUF_BLOCK_FILE_PAGE. If b != NULL
  1709. then it was a compressed page with an uncompressed frame and
  1710. we are interested in freeing only the uncompressed frame.
  1711. Therefore we have to reinsert the compressed page descriptor
  1712. into the LRU and page_hash (and possibly flush_list).
  1713. if b == NULL then it was a regular page that has been freed */
  1714. if (b) {
  1715. buf_page_t* prev_b = UT_LIST_GET_PREV(LRU, b);
  1716. rw_lock_x_lock(hash_lock);
  1717. mutex_enter(block_mutex);
  1718. ut_a(!buf_page_hash_get_low(
  1719. buf_pool, b->space, b->offset, fold));
  1720. b->state = b->oldest_modification
  1721. ? BUF_BLOCK_ZIP_DIRTY
  1722. : BUF_BLOCK_ZIP_PAGE;
  1723. UNIV_MEM_DESC(b->zip.data,
  1724. page_zip_get_size(&b->zip));
  1725. /* The fields in_page_hash and in_LRU_list of
  1726. the to-be-freed block descriptor should have
  1727. been cleared in
  1728. buf_LRU_block_remove_hashed(), which
  1729. invokes buf_LRU_remove_block(). */
  1730. ut_ad(!bpage->in_page_hash);
  1731. ut_ad(!bpage->in_LRU_list);
  1732. /* bpage->state was BUF_BLOCK_FILE_PAGE because
  1733. b != NULL. The type cast below is thus valid. */
  1734. ut_ad(!((buf_block_t*) bpage)->in_unzip_LRU_list);
  1735. /* The fields of bpage were copied to b before
  1736. buf_LRU_block_remove_hashed() was invoked. */
  1737. ut_ad(!b->in_zip_hash);
  1738. ut_ad(b->in_page_hash);
  1739. ut_ad(b->in_LRU_list);
  1740. HASH_INSERT(buf_page_t, hash,
  1741. buf_pool->page_hash, fold, b);
  1742. /* Insert b where bpage was in the LRU list. */
  1743. if (UNIV_LIKELY(prev_b != NULL)) {
  1744. ulint lru_len;
  1745. ut_ad(prev_b->in_LRU_list);
  1746. ut_ad(buf_page_in_file(prev_b));
  1747. UT_LIST_INSERT_AFTER(LRU, buf_pool->LRU,
  1748. prev_b, b);
  1749. incr_LRU_size_in_bytes(b, buf_pool);
  1750. if (buf_page_is_old(b)) {
  1751. buf_pool->LRU_old_len++;
  1752. if (UNIV_UNLIKELY
  1753. (buf_pool->LRU_old
  1754. == UT_LIST_GET_NEXT(LRU, b))) {
  1755. buf_pool->LRU_old = b;
  1756. }
  1757. }
  1758. lru_len = UT_LIST_GET_LEN(buf_pool->LRU);
  1759. if (lru_len > BUF_LRU_OLD_MIN_LEN) {
  1760. ut_ad(buf_pool->LRU_old);
  1761. /* Adjust the length of the
  1762. old block list if necessary */
  1763. buf_LRU_old_adjust_len(buf_pool);
  1764. } else if (lru_len == BUF_LRU_OLD_MIN_LEN) {
  1765. /* The LRU list is now long
  1766. enough for LRU_old to become
  1767. defined: init it */
  1768. buf_LRU_old_init(buf_pool);
  1769. }
  1770. #ifdef UNIV_LRU_DEBUG
  1771. /* Check that the "old" flag is consistent
  1772. in the block and its neighbours. */
  1773. buf_page_set_old(b, buf_page_is_old(b));
  1774. #endif /* UNIV_LRU_DEBUG */
  1775. } else {
  1776. ut_d(b->in_LRU_list = FALSE);
  1777. buf_LRU_add_block_low(b, buf_page_is_old(b));
  1778. }
  1779. mutex_enter(&buf_pool->zip_mutex);
  1780. rw_lock_x_unlock(hash_lock);
  1781. if (b->state == BUF_BLOCK_ZIP_PAGE) {
  1782. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  1783. buf_LRU_insert_zip_clean(b);
  1784. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  1785. } else {
  1786. /* Relocate on buf_pool->flush_list. */
  1787. buf_flush_relocate_on_flush_list(bpage, b);
  1788. }
  1789. bpage->zip.data = NULL;
  1790. page_zip_set_size(&bpage->zip, 0);
  1791. /* Prevent buf_page_get_gen() from
  1792. decompressing the block while we release block_mutex. */
  1793. buf_page_set_sticky(b);
  1794. mutex_exit(&buf_pool->zip_mutex);
  1795. mutex_exit(block_mutex);
  1796. }
  1797. mutex_exit(&buf_pool->LRU_list_mutex);
  1798. /* Remove possible adaptive hash index on the page.
  1799. The page was declared uninitialized by
  1800. buf_LRU_block_remove_hashed(). We need to flag
  1801. the contents of the page valid (which it still is) in
  1802. order to avoid bogus Valgrind warnings.*/
  1803. UNIV_MEM_VALID(((buf_block_t*) bpage)->frame,
  1804. UNIV_PAGE_SIZE);
  1805. btr_search_drop_page_hash_index((buf_block_t*) bpage);
  1806. UNIV_MEM_INVALID(((buf_block_t*) bpage)->frame,
  1807. UNIV_PAGE_SIZE);
  1808. if (b) {
  1809. ib_uint32_t checksum;
  1810. /* Compute and stamp the compressed page
  1811. checksum while not holding any mutex. The
  1812. block is already half-freed
  1813. (BUF_BLOCK_REMOVE_HASH) and removed from
  1814. buf_pool->page_hash, thus inaccessible by any
  1815. other thread. */
  1816. checksum = static_cast<ib_uint32_t>(
  1817. page_zip_calc_checksum(
  1818. b->zip.data,
  1819. page_zip_get_size(&b->zip),
  1820. static_cast<srv_checksum_algorithm_t>(
  1821. srv_checksum_algorithm)));
  1822. mach_write_to_4(b->zip.data + FIL_PAGE_SPACE_OR_CHKSUM,
  1823. checksum);
  1824. }
  1825. mutex_enter(block_mutex);
  1826. if (b) {
  1827. mutex_enter(&buf_pool->zip_mutex);
  1828. buf_page_unset_sticky(b);
  1829. mutex_exit(&buf_pool->zip_mutex);
  1830. }
  1831. buf_LRU_block_free_hashed_page((buf_block_t*) bpage);
  1832. ut_ad(mutex_own(block_mutex));
  1833. ut_ad(!mutex_own(&buf_pool->LRU_list_mutex));
  1834. return(true);
  1835. }
  1836. /******************************************************************//**
  1837. Puts a block back to the free list. */
  1838. UNIV_INTERN
  1839. void
  1840. buf_LRU_block_free_non_file_page(
  1841. /*=============================*/
  1842. buf_block_t* block) /*!< in: block, must not contain a file page */
  1843. {
  1844. void* data;
  1845. buf_pool_t* buf_pool = buf_pool_from_block(block);
  1846. ut_ad(block);
  1847. ut_ad(mutex_own(&block->mutex));
  1848. switch (buf_block_get_state(block)) {
  1849. case BUF_BLOCK_MEMORY:
  1850. case BUF_BLOCK_READY_FOR_USE:
  1851. break;
  1852. default:
  1853. ut_error;
  1854. }
  1855. #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
  1856. ut_a(block->n_pointers == 0);
  1857. #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
  1858. ut_ad(!block->page.in_free_list);
  1859. ut_ad(!block->page.in_flush_list);
  1860. ut_ad(!block->page.in_LRU_list);
  1861. UNIV_MEM_ALLOC(block->frame, UNIV_PAGE_SIZE);
  1862. #ifdef UNIV_DEBUG
  1863. /* Wipe contents of page to reveal possible stale pointers to it */
  1864. memset(block->frame, '\0', UNIV_PAGE_SIZE);
  1865. #else
  1866. /* Wipe page_no and space_id */
  1867. memset(block->frame + FIL_PAGE_OFFSET, 0xfe, 4);
  1868. memset(block->frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, 0xfe, 4);
  1869. #endif
  1870. data = block->page.zip.data;
  1871. if (data) {
  1872. block->page.zip.data = NULL;
  1873. mutex_exit(&block->mutex);
  1874. buf_buddy_free(
  1875. buf_pool, data, page_zip_get_size(&block->page.zip));
  1876. mutex_enter(&block->mutex);
  1877. page_zip_set_size(&block->page.zip, 0);
  1878. }
  1879. mutex_enter_first(&buf_pool->free_list_mutex);
  1880. buf_block_set_state(block, BUF_BLOCK_NOT_USED);
  1881. UT_LIST_ADD_FIRST(list, buf_pool->free, (&block->page));
  1882. ut_d(block->page.in_free_list = TRUE);
  1883. mutex_exit(&buf_pool->free_list_mutex);
  1884. UNIV_MEM_ASSERT_AND_FREE(block->frame, UNIV_PAGE_SIZE);
  1885. }
  1886. /******************************************************************//**
  1887. Takes a block out of the LRU list and page hash table.
  1888. If the block is compressed-only (BUF_BLOCK_ZIP_PAGE),
  1889. the object will be freed.
  1890. The caller must hold buf_pool->LRU_list_mutex, the buf_page_get_mutex() mutex
  1891. and the appropriate hash_lock. This function will release the
  1892. buf_page_get_mutex() and the hash_lock.
  1893. If a compressed page is freed other compressed pages may be relocated.
  1894. @retval true if BUF_BLOCK_FILE_PAGE was removed from page_hash. The
  1895. caller needs to free the page to the free list
  1896. @retval false if BUF_BLOCK_ZIP_PAGE was removed from page_hash. In
  1897. this case the block is already returned to the buddy allocator. */
  1898. static
  1899. bool
  1900. buf_LRU_block_remove_hashed(
  1901. /*========================*/
  1902. buf_page_t* bpage, /*!< in: block, must contain a file page and
  1903. be in a state where it can be freed; there
  1904. may or may not be a hash index to the page */
  1905. bool zip) /*!< in: true if should remove also the
  1906. compressed page of an uncompressed page */
  1907. {
  1908. ulint fold;
  1909. const buf_page_t* hashed_bpage;
  1910. buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
  1911. prio_rw_lock_t* hash_lock;
  1912. ut_ad(bpage);
  1913. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  1914. ut_ad(mutex_own(buf_page_get_mutex(bpage)));
  1915. fold = buf_page_address_fold(bpage->space, bpage->offset);
  1916. hash_lock = buf_page_hash_lock_get(buf_pool, fold);
  1917. #ifdef UNIV_SYNC_DEBUG
  1918. ut_ad(rw_lock_own(hash_lock, RW_LOCK_EX));
  1919. #endif /* UNIV_SYNC_DEBUG */
  1920. ut_a(buf_page_get_io_fix(bpage) == BUF_IO_NONE);
  1921. ut_a(bpage->buf_fix_count == 0);
  1922. buf_LRU_remove_block(bpage);
  1923. buf_pool->freed_page_clock += 1;
  1924. switch (buf_page_get_state(bpage)) {
  1925. case BUF_BLOCK_FILE_PAGE:
  1926. UNIV_MEM_ASSERT_W(bpage, sizeof(buf_block_t));
  1927. UNIV_MEM_ASSERT_W(((buf_block_t*) bpage)->frame,
  1928. UNIV_PAGE_SIZE);
  1929. buf_block_modify_clock_inc((buf_block_t*) bpage);
  1930. if (bpage->zip.data) {
  1931. const page_t* page = ((buf_block_t*) bpage)->frame;
  1932. const ulint zip_size
  1933. = page_zip_get_size(&bpage->zip);
  1934. ut_a(!zip || bpage->oldest_modification == 0);
  1935. switch (UNIV_EXPECT(fil_page_get_type(page),
  1936. FIL_PAGE_INDEX)) {
  1937. case FIL_PAGE_TYPE_ALLOCATED:
  1938. case FIL_PAGE_INODE:
  1939. case FIL_PAGE_IBUF_BITMAP:
  1940. case FIL_PAGE_TYPE_FSP_HDR:
  1941. case FIL_PAGE_TYPE_XDES:
  1942. /* These are essentially uncompressed pages. */
  1943. if (!zip) {
  1944. /* InnoDB writes the data to the
  1945. uncompressed page frame. Copy it
  1946. to the compressed page, which will
  1947. be preserved. */
  1948. memcpy(bpage->zip.data, page,
  1949. zip_size);
  1950. }
  1951. break;
  1952. case FIL_PAGE_TYPE_ZBLOB:
  1953. case FIL_PAGE_TYPE_ZBLOB2:
  1954. break;
  1955. case FIL_PAGE_INDEX:
  1956. #ifdef UNIV_ZIP_DEBUG
  1957. ut_a(page_zip_validate(
  1958. &bpage->zip, page,
  1959. ((buf_block_t*) bpage)->index));
  1960. #endif /* UNIV_ZIP_DEBUG */
  1961. break;
  1962. default:
  1963. ut_print_timestamp(stderr);
  1964. fputs(" InnoDB: ERROR: The compressed page"
  1965. " to be evicted seems corrupt:", stderr);
  1966. ut_print_buf(stderr, page, zip_size);
  1967. fputs("\nInnoDB: Possibly older version"
  1968. " of the page:", stderr);
  1969. ut_print_buf(stderr, bpage->zip.data,
  1970. zip_size);
  1971. putc('\n', stderr);
  1972. ut_error;
  1973. }
  1974. break;
  1975. }
  1976. /* fall through */
  1977. case BUF_BLOCK_ZIP_PAGE:
  1978. ut_a(bpage->oldest_modification == 0);
  1979. UNIV_MEM_ASSERT_W(bpage->zip.data,
  1980. page_zip_get_size(&bpage->zip));
  1981. break;
  1982. case BUF_BLOCK_POOL_WATCH:
  1983. case BUF_BLOCK_ZIP_DIRTY:
  1984. case BUF_BLOCK_NOT_USED:
  1985. case BUF_BLOCK_READY_FOR_USE:
  1986. case BUF_BLOCK_MEMORY:
  1987. case BUF_BLOCK_REMOVE_HASH:
  1988. ut_error;
  1989. break;
  1990. }
  1991. hashed_bpage = buf_page_hash_get_low(buf_pool, bpage->space,
  1992. bpage->offset, fold);
  1993. if (UNIV_UNLIKELY(bpage != hashed_bpage)) {
  1994. fprintf(stderr,
  1995. "InnoDB: Error: page %lu %lu not found"
  1996. " in the hash table\n",
  1997. (ulong) bpage->space,
  1998. (ulong) bpage->offset);
  1999. #ifdef UNIV_DEBUG
  2000. fprintf(stderr,
  2001. "InnoDB: in_page_hash %lu in_zip_hash %lu\n"
  2002. " in_free_list %lu in_flush_list %lu in_LRU_list %lu\n"
  2003. " zip.data %p zip_size %lu page_state %d\n",
  2004. bpage->in_page_hash, bpage->in_zip_hash,
  2005. bpage->in_free_list, bpage->in_flush_list,
  2006. bpage->in_LRU_list, bpage->zip.data,
  2007. buf_page_get_zip_size(bpage),
  2008. buf_page_get_state(bpage));
  2009. #else
  2010. fprintf(stderr,
  2011. "InnoDB: zip.data %p zip_size %lu page_state %d\n",
  2012. bpage->zip.data,
  2013. buf_page_get_zip_size(bpage),
  2014. buf_page_get_state(bpage));
  2015. #endif
  2016. if (hashed_bpage) {
  2017. fprintf(stderr,
  2018. "InnoDB: In hash table we find block"
  2019. " %p of %lu %lu which is not %p\n",
  2020. (const void*) hashed_bpage,
  2021. (ulong) hashed_bpage->space,
  2022. (ulong) hashed_bpage->offset,
  2023. (const void*) bpage);
  2024. }
  2025. ut_a(buf_page_get_io_fix(bpage) == BUF_IO_NONE);
  2026. ut_a(bpage->buf_fix_count == 0);
  2027. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2028. mutex_exit(buf_page_get_mutex(bpage));
  2029. rw_lock_x_unlock(hash_lock);
  2030. mutex_exit(&buf_pool->LRU_list_mutex);
  2031. buf_print();
  2032. buf_LRU_print();
  2033. buf_validate();
  2034. buf_LRU_validate();
  2035. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  2036. ut_error;
  2037. }
  2038. ut_ad(!bpage->in_zip_hash);
  2039. ut_ad(bpage->in_page_hash);
  2040. ut_d(bpage->in_page_hash = FALSE);
  2041. HASH_DELETE(buf_page_t, hash, buf_pool->page_hash, fold, bpage);
  2042. switch (buf_page_get_state(bpage)) {
  2043. case BUF_BLOCK_ZIP_PAGE:
  2044. ut_ad(!bpage->in_free_list);
  2045. ut_ad(!bpage->in_flush_list);
  2046. ut_ad(!bpage->in_LRU_list);
  2047. ut_a(bpage->zip.data);
  2048. ut_a(buf_page_get_zip_size(bpage));
  2049. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2050. UT_LIST_REMOVE(list, buf_pool->zip_clean, bpage);
  2051. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  2052. mutex_exit(&buf_pool->zip_mutex);
  2053. rw_lock_x_unlock(hash_lock);
  2054. buf_buddy_free(
  2055. buf_pool, bpage->zip.data,
  2056. page_zip_get_size(&bpage->zip));
  2057. buf_page_free_descriptor(bpage);
  2058. return(false);
  2059. case BUF_BLOCK_FILE_PAGE:
  2060. memset(((buf_block_t*) bpage)->frame
  2061. + FIL_PAGE_OFFSET, 0xff, 4);
  2062. memset(((buf_block_t*) bpage)->frame
  2063. + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, 0xff, 4);
  2064. UNIV_MEM_INVALID(((buf_block_t*) bpage)->frame,
  2065. UNIV_PAGE_SIZE);
  2066. buf_page_set_state(bpage, BUF_BLOCK_REMOVE_HASH);
  2067. if (buf_pool->flush_rbt == NULL) {
  2068. bpage->space = ULINT32_UNDEFINED;
  2069. bpage->offset = ULINT32_UNDEFINED;
  2070. }
  2071. /* Question: If we release bpage and hash mutex here
  2072. then what protects us against:
  2073. 1) Some other thread buffer fixing this page
  2074. 2) Some other thread trying to read this page and
  2075. not finding it in buffer pool attempting to read it
  2076. from the disk.
  2077. Answer:
  2078. 1) Cannot happen because the page is no longer in the
  2079. page_hash. Only possibility is when while invalidating
  2080. a tablespace we buffer fix the prev_page in LRU to
  2081. avoid relocation during the scan. But that is not
  2082. possible because we are holding LRU list mutex.
  2083. 2) Not possible because in buf_page_init_for_read()
  2084. we do a look up of page_hash while holding LRU list
  2085. mutex and since we are holding LRU list mutex here
  2086. and by the time we'll release it in the caller we'd
  2087. have inserted the compressed only descriptor in the
  2088. page_hash. */
  2089. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  2090. rw_lock_x_unlock(hash_lock);
  2091. mutex_exit(&((buf_block_t*) bpage)->mutex);
  2092. if (zip && bpage->zip.data) {
  2093. /* Free the compressed page. */
  2094. void* data = bpage->zip.data;
  2095. bpage->zip.data = NULL;
  2096. ut_ad(!bpage->in_free_list);
  2097. ut_ad(!bpage->in_flush_list);
  2098. ut_ad(!bpage->in_LRU_list);
  2099. buf_buddy_free(
  2100. buf_pool, data,
  2101. page_zip_get_size(&bpage->zip));
  2102. page_zip_set_size(&bpage->zip, 0);
  2103. }
  2104. return(true);
  2105. case BUF_BLOCK_POOL_WATCH:
  2106. case BUF_BLOCK_ZIP_DIRTY:
  2107. case BUF_BLOCK_NOT_USED:
  2108. case BUF_BLOCK_READY_FOR_USE:
  2109. case BUF_BLOCK_MEMORY:
  2110. case BUF_BLOCK_REMOVE_HASH:
  2111. break;
  2112. }
  2113. ut_error;
  2114. return(false);
  2115. }
  2116. /******************************************************************//**
  2117. Puts a file page whose has no hash index to the free list. */
  2118. static
  2119. void
  2120. buf_LRU_block_free_hashed_page(
  2121. /*===========================*/
  2122. buf_block_t* block) /*!< in: block, must contain a file page and
  2123. be in a state where it can be freed */
  2124. {
  2125. ut_ad(mutex_own(&block->mutex));
  2126. buf_block_set_state(block, BUF_BLOCK_MEMORY);
  2127. buf_LRU_block_free_non_file_page(block);
  2128. }
  2129. /******************************************************************//**
  2130. Remove one page from LRU list and put it to free list */
  2131. UNIV_INTERN
  2132. void
  2133. buf_LRU_free_one_page(
  2134. /*==================*/
  2135. buf_page_t* bpage) /*!< in/out: block, must contain a file page and
  2136. be in a state where it can be freed; there
  2137. may or may not be a hash index to the page */
  2138. {
  2139. #if defined(UNIV_DEBUG) || defined(UNIV_SYNC_DEBUG)
  2140. buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
  2141. #endif
  2142. #ifdef UNIV_SYNC_DEBUG
  2143. const ulint fold = buf_page_address_fold(bpage->space,
  2144. bpage->offset);
  2145. prio_rw_lock_t* hash_lock = buf_page_hash_lock_get(buf_pool, fold);
  2146. #endif
  2147. ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
  2148. ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
  2149. ut_ad(mutex_own(block_mutex));
  2150. #ifdef UNIV_SYNC_DEBUG
  2151. ut_ad(rw_lock_own(hash_lock, RW_LOCK_EX));
  2152. #endif
  2153. if (buf_LRU_block_remove_hashed(bpage, true)) {
  2154. mutex_enter(block_mutex);
  2155. buf_LRU_block_free_hashed_page((buf_block_t*) bpage);
  2156. mutex_exit(block_mutex);
  2157. }
  2158. /* buf_LRU_block_remove_hashed() releases hash_lock and block_mutex */
  2159. #ifdef UNIV_SYNC_DEBUG
  2160. ut_ad(!rw_lock_own(hash_lock, RW_LOCK_EX)
  2161. && !rw_lock_own(hash_lock, RW_LOCK_SHARED));
  2162. #endif /* UNIV_SYNC_DEBUG */
  2163. ut_ad(!mutex_own(block_mutex));
  2164. }
  2165. /**********************************************************************//**
  2166. Updates buf_pool->LRU_old_ratio for one buffer pool instance.
  2167. @return updated old_pct */
  2168. static
  2169. uint
  2170. buf_LRU_old_ratio_update_instance(
  2171. /*==============================*/
  2172. buf_pool_t* buf_pool,/*!< in: buffer pool instance */
  2173. uint old_pct,/*!< in: Reserve this percentage of
  2174. the buffer pool for "old" blocks. */
  2175. ibool adjust) /*!< in: TRUE=adjust the LRU list;
  2176. FALSE=just assign buf_pool->LRU_old_ratio
  2177. during the initialization of InnoDB */
  2178. {
  2179. uint ratio;
  2180. ratio = old_pct * BUF_LRU_OLD_RATIO_DIV / 100;
  2181. if (ratio < BUF_LRU_OLD_RATIO_MIN) {
  2182. ratio = BUF_LRU_OLD_RATIO_MIN;
  2183. } else if (ratio > BUF_LRU_OLD_RATIO_MAX) {
  2184. ratio = BUF_LRU_OLD_RATIO_MAX;
  2185. }
  2186. if (adjust) {
  2187. mutex_enter(&buf_pool->LRU_list_mutex);
  2188. if (ratio != buf_pool->LRU_old_ratio) {
  2189. buf_pool->LRU_old_ratio = ratio;
  2190. if (UT_LIST_GET_LEN(buf_pool->LRU)
  2191. >= BUF_LRU_OLD_MIN_LEN) {
  2192. buf_LRU_old_adjust_len(buf_pool);
  2193. }
  2194. }
  2195. mutex_exit(&buf_pool->LRU_list_mutex);
  2196. } else {
  2197. buf_pool->LRU_old_ratio = ratio;
  2198. }
  2199. /* the reverse of
  2200. ratio = old_pct * BUF_LRU_OLD_RATIO_DIV / 100 */
  2201. return((uint) (ratio * 100 / (double) BUF_LRU_OLD_RATIO_DIV + 0.5));
  2202. }
  2203. /**********************************************************************//**
  2204. Updates buf_pool->LRU_old_ratio.
  2205. @return updated old_pct */
  2206. UNIV_INTERN
  2207. ulint
  2208. buf_LRU_old_ratio_update(
  2209. /*=====================*/
  2210. uint old_pct,/*!< in: Reserve this percentage of
  2211. the buffer pool for "old" blocks. */
  2212. ibool adjust) /*!< in: TRUE=adjust the LRU list;
  2213. FALSE=just assign buf_pool->LRU_old_ratio
  2214. during the initialization of InnoDB */
  2215. {
  2216. ulint i;
  2217. ulint new_ratio = 0;
  2218. for (i = 0; i < srv_buf_pool_instances; i++) {
  2219. buf_pool_t* buf_pool;
  2220. buf_pool = buf_pool_from_array(i);
  2221. new_ratio = buf_LRU_old_ratio_update_instance(
  2222. buf_pool, old_pct, adjust);
  2223. }
  2224. return(new_ratio);
  2225. }
  2226. /********************************************************************//**
  2227. Update the historical stats that we are collecting for LRU eviction
  2228. policy at the end of each interval. */
  2229. UNIV_INTERN
  2230. void
  2231. buf_LRU_stat_update(void)
  2232. /*=====================*/
  2233. {
  2234. ulint i;
  2235. buf_LRU_stat_t* item;
  2236. buf_pool_t* buf_pool;
  2237. ibool evict_started = FALSE;
  2238. buf_LRU_stat_t cur_stat;
  2239. /* If we haven't started eviction yet then don't update stats. */
  2240. for (i = 0; i < srv_buf_pool_instances; i++) {
  2241. buf_pool = buf_pool_from_array(i);
  2242. if (buf_pool->freed_page_clock != 0) {
  2243. evict_started = TRUE;
  2244. break;
  2245. }
  2246. }
  2247. if (!evict_started) {
  2248. goto func_exit;
  2249. }
  2250. /* Update the index. */
  2251. item = &buf_LRU_stat_arr[buf_LRU_stat_arr_ind];
  2252. buf_LRU_stat_arr_ind++;
  2253. buf_LRU_stat_arr_ind %= BUF_LRU_STAT_N_INTERVAL;
  2254. /* Add the current value and subtract the obsolete entry.
  2255. Since buf_LRU_stat_cur is not protected by any mutex,
  2256. it can be changing between adding to buf_LRU_stat_sum
  2257. and copying to item. Assign it to local variables to make
  2258. sure the same value assign to the buf_LRU_stat_sum
  2259. and item */
  2260. cur_stat = buf_LRU_stat_cur;
  2261. buf_LRU_stat_sum.io += cur_stat.io - item->io;
  2262. buf_LRU_stat_sum.unzip += cur_stat.unzip - item->unzip;
  2263. /* Put current entry in the array. */
  2264. memcpy(item, &cur_stat, sizeof *item);
  2265. func_exit:
  2266. /* Clear the current entry. */
  2267. memset(&buf_LRU_stat_cur, 0, sizeof buf_LRU_stat_cur);
  2268. }
  2269. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2270. /**********************************************************************//**
  2271. Validates the LRU list for one buffer pool instance. */
  2272. static
  2273. void
  2274. buf_LRU_validate_instance(
  2275. /*======================*/
  2276. buf_pool_t* buf_pool)
  2277. {
  2278. buf_page_t* bpage;
  2279. buf_block_t* block;
  2280. ulint old_len;
  2281. ulint new_len;
  2282. ut_ad(buf_pool);
  2283. mutex_enter(&buf_pool->LRU_list_mutex);
  2284. if (UT_LIST_GET_LEN(buf_pool->LRU) >= BUF_LRU_OLD_MIN_LEN) {
  2285. ut_a(buf_pool->LRU_old);
  2286. old_len = buf_pool->LRU_old_len;
  2287. new_len = ut_min(UT_LIST_GET_LEN(buf_pool->LRU)
  2288. * buf_pool->LRU_old_ratio
  2289. / BUF_LRU_OLD_RATIO_DIV,
  2290. UT_LIST_GET_LEN(buf_pool->LRU)
  2291. - (BUF_LRU_OLD_TOLERANCE
  2292. + BUF_LRU_NON_OLD_MIN_LEN));
  2293. ut_a(old_len >= new_len - BUF_LRU_OLD_TOLERANCE);
  2294. ut_a(old_len <= new_len + BUF_LRU_OLD_TOLERANCE);
  2295. }
  2296. UT_LIST_VALIDATE(LRU, buf_page_t, buf_pool->LRU, CheckInLRUList());
  2297. old_len = 0;
  2298. for (bpage = UT_LIST_GET_FIRST(buf_pool->LRU);
  2299. bpage != NULL;
  2300. bpage = UT_LIST_GET_NEXT(LRU, bpage)) {
  2301. switch (buf_page_get_state(bpage)) {
  2302. case BUF_BLOCK_POOL_WATCH:
  2303. case BUF_BLOCK_NOT_USED:
  2304. case BUF_BLOCK_READY_FOR_USE:
  2305. case BUF_BLOCK_MEMORY:
  2306. case BUF_BLOCK_REMOVE_HASH:
  2307. ut_error;
  2308. break;
  2309. case BUF_BLOCK_FILE_PAGE:
  2310. ut_ad(((buf_block_t*) bpage)->in_unzip_LRU_list
  2311. == buf_page_belongs_to_unzip_LRU(bpage));
  2312. case BUF_BLOCK_ZIP_PAGE:
  2313. case BUF_BLOCK_ZIP_DIRTY:
  2314. break;
  2315. }
  2316. if (buf_page_is_old(bpage)) {
  2317. const buf_page_t* prev
  2318. = UT_LIST_GET_PREV(LRU, bpage);
  2319. const buf_page_t* next
  2320. = UT_LIST_GET_NEXT(LRU, bpage);
  2321. if (!old_len++) {
  2322. ut_a(buf_pool->LRU_old == bpage);
  2323. } else {
  2324. ut_a(!prev || buf_page_is_old(prev));
  2325. }
  2326. ut_a(!next || buf_page_is_old(next));
  2327. }
  2328. }
  2329. ut_a(buf_pool->LRU_old_len == old_len);
  2330. mutex_exit(&buf_pool->LRU_list_mutex);
  2331. mutex_enter(&buf_pool->free_list_mutex);
  2332. UT_LIST_VALIDATE(list, buf_page_t, buf_pool->free, CheckInFreeList());
  2333. for (bpage = UT_LIST_GET_FIRST(buf_pool->free);
  2334. bpage != NULL;
  2335. bpage = UT_LIST_GET_NEXT(list, bpage)) {
  2336. ut_a(buf_page_get_state(bpage) == BUF_BLOCK_NOT_USED);
  2337. }
  2338. mutex_exit(&buf_pool->free_list_mutex);
  2339. mutex_enter(&buf_pool->LRU_list_mutex);
  2340. UT_LIST_VALIDATE(
  2341. unzip_LRU, buf_block_t, buf_pool->unzip_LRU,
  2342. CheckUnzipLRUAndLRUList());
  2343. for (block = UT_LIST_GET_FIRST(buf_pool->unzip_LRU);
  2344. block;
  2345. block = UT_LIST_GET_NEXT(unzip_LRU, block)) {
  2346. ut_ad(block->in_unzip_LRU_list);
  2347. ut_ad(block->page.in_LRU_list);
  2348. ut_a(buf_page_belongs_to_unzip_LRU(&block->page));
  2349. }
  2350. mutex_exit(&buf_pool->LRU_list_mutex);
  2351. }
  2352. /**********************************************************************//**
  2353. Validates the LRU list.
  2354. @return TRUE */
  2355. UNIV_INTERN
  2356. ibool
  2357. buf_LRU_validate(void)
  2358. /*==================*/
  2359. {
  2360. ulint i;
  2361. for (i = 0; i < srv_buf_pool_instances; i++) {
  2362. buf_pool_t* buf_pool;
  2363. buf_pool = buf_pool_from_array(i);
  2364. buf_LRU_validate_instance(buf_pool);
  2365. }
  2366. return(TRUE);
  2367. }
  2368. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  2369. #if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  2370. /**********************************************************************//**
  2371. Prints the LRU list for one buffer pool instance. */
  2372. UNIV_INTERN
  2373. void
  2374. buf_LRU_print_instance(
  2375. /*===================*/
  2376. buf_pool_t* buf_pool)
  2377. {
  2378. const buf_page_t* bpage;
  2379. ut_ad(buf_pool);
  2380. mutex_enter(&buf_pool->LRU_list_mutex);
  2381. bpage = UT_LIST_GET_FIRST(buf_pool->LRU);
  2382. while (bpage != NULL) {
  2383. mutex_enter(buf_page_get_mutex(bpage));
  2384. fprintf(stderr, "BLOCK space %lu page %lu ",
  2385. (ulong) buf_page_get_space(bpage),
  2386. (ulong) buf_page_get_page_no(bpage));
  2387. if (buf_page_is_old(bpage)) {
  2388. fputs("old ", stderr);
  2389. }
  2390. if (bpage->buf_fix_count) {
  2391. fprintf(stderr, "buffix count %lu ",
  2392. (ulong) bpage->buf_fix_count);
  2393. }
  2394. if (buf_page_get_io_fix(bpage)) {
  2395. fprintf(stderr, "io_fix %lu ",
  2396. (ulong) buf_page_get_io_fix(bpage));
  2397. }
  2398. if (bpage->oldest_modification) {
  2399. fputs("modif. ", stderr);
  2400. }
  2401. switch (buf_page_get_state(bpage)) {
  2402. const byte* frame;
  2403. case BUF_BLOCK_FILE_PAGE:
  2404. frame = buf_block_get_frame((buf_block_t*) bpage);
  2405. fprintf(stderr, "\ntype %lu"
  2406. " index id %llu\n",
  2407. (ulong) fil_page_get_type(frame),
  2408. (ullint) btr_page_get_index_id(frame));
  2409. break;
  2410. case BUF_BLOCK_ZIP_PAGE:
  2411. frame = bpage->zip.data;
  2412. fprintf(stderr, "\ntype %lu size %lu"
  2413. " index id %llu\n",
  2414. (ulong) fil_page_get_type(frame),
  2415. (ulong) buf_page_get_zip_size(bpage),
  2416. (ullint) btr_page_get_index_id(frame));
  2417. break;
  2418. default:
  2419. fprintf(stderr, "\n!state %lu!\n",
  2420. (ulong) buf_page_get_state(bpage));
  2421. break;
  2422. }
  2423. mutex_exit(buf_page_get_mutex(bpage));
  2424. bpage = UT_LIST_GET_NEXT(LRU, bpage);
  2425. }
  2426. mutex_exit(&buf_pool->LRU_list_mutex);
  2427. }
  2428. /**********************************************************************//**
  2429. Prints the LRU list. */
  2430. UNIV_INTERN
  2431. void
  2432. buf_LRU_print(void)
  2433. /*===============*/
  2434. {
  2435. ulint i;
  2436. buf_pool_t* buf_pool;
  2437. for (i = 0; i < srv_buf_pool_instances; i++) {
  2438. buf_pool = buf_pool_from_array(i);
  2439. buf_LRU_print_instance(buf_pool);
  2440. }
  2441. }
  2442. #endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */
  2443. #endif /* !UNIV_HOTBACKUP */