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.

1112 lines
28 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /******************************************************
  2. The database buffer replacement algorithm
  3. (c) 1995 Innobase Oy
  4. Created 11/5/1995 Heikki Tuuri
  5. *******************************************************/
  6. #include "buf0lru.h"
  7. #ifdef UNIV_NONINL
  8. #include "buf0lru.ic"
  9. #endif
  10. #include "ut0byte.h"
  11. #include "ut0lst.h"
  12. #include "ut0rnd.h"
  13. #include "sync0sync.h"
  14. #include "sync0rw.h"
  15. #include "hash0hash.h"
  16. #include "os0sync.h"
  17. #include "fil0fil.h"
  18. #include "btr0btr.h"
  19. #include "buf0buf.h"
  20. #include "buf0flu.h"
  21. #include "buf0rea.h"
  22. #include "btr0sea.h"
  23. #include "os0file.h"
  24. #include "page0zip.h"
  25. #include "log0recv.h"
  26. #include "srv0srv.h"
  27. /* The number of blocks from the LRU_old pointer onward, including the block
  28. pointed to, must be 3/8 of the whole LRU list length, except that the
  29. tolerance defined below is allowed. Note that the tolerance must be small
  30. enough such that for even the BUF_LRU_OLD_MIN_LEN long LRU list, the
  31. LRU_old pointer is not allowed to point to either end of the LRU list. */
  32. #define BUF_LRU_OLD_TOLERANCE 20
  33. /* The whole LRU list length is divided by this number to determine an
  34. initial segment in buf_LRU_get_recent_limit */
  35. #define BUF_LRU_INITIAL_RATIO 8
  36. /* If we switch on the InnoDB monitor because there are too few available
  37. frames in the buffer pool, we set this to TRUE */
  38. ibool buf_lru_switched_on_innodb_mon = FALSE;
  39. /**********************************************************************
  40. Takes a block out of the LRU list and page hash table and sets the block
  41. state to BUF_BLOCK_REMOVE_HASH. */
  42. static
  43. void
  44. buf_LRU_block_remove_hashed_page(
  45. /*=============================*/
  46. buf_block_t* block); /* in: block, must contain a file page and
  47. be in a state where it can be freed; there
  48. may or may not be a hash index to the page */
  49. /**********************************************************************
  50. Puts a file page whose has no hash index to the free list. */
  51. static
  52. void
  53. buf_LRU_block_free_hashed_page(
  54. /*===========================*/
  55. buf_block_t* block); /* in: block, must contain a file page and
  56. be in a state where it can be freed */
  57. /**********************************************************************
  58. Invalidates all pages belonging to a given tablespace when we are deleting
  59. the data file(s) of that tablespace. */
  60. void
  61. buf_LRU_invalidate_tablespace(
  62. /*==========================*/
  63. ulint id) /* in: space id */
  64. {
  65. buf_block_t* block;
  66. ulint page_no;
  67. ibool all_freed;
  68. scan_again:
  69. mutex_enter(&(buf_pool->mutex));
  70. all_freed = TRUE;
  71. block = UT_LIST_GET_LAST(buf_pool->LRU);
  72. while (block != NULL) {
  73. buf_block_t* prev_block;
  74. mutex_enter(&block->mutex);
  75. prev_block = UT_LIST_GET_PREV(LRU, block);
  76. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  77. if (buf_block_get_space(block) == id
  78. && (block->buf_fix_count > 0 || block->io_fix != 0)) {
  79. /* We cannot remove this page during this scan yet;
  80. maybe the system is currently reading it in, or
  81. flushing the modifications to the file */
  82. all_freed = FALSE;
  83. goto next_page;
  84. }
  85. if (buf_block_get_space(block) == id) {
  86. #ifdef UNIV_DEBUG
  87. if (buf_debug_prints) {
  88. fprintf(stderr,
  89. "Dropping space %lu page %lu\n",
  90. (ulong) buf_block_get_space(block),
  91. (ulong) buf_block_get_page_no(block));
  92. }
  93. #endif
  94. if (block->is_hashed) {
  95. page_no = buf_block_get_page_no(block);
  96. mutex_exit(&block->mutex);
  97. mutex_exit(&(buf_pool->mutex));
  98. /* Note that the following call will acquire
  99. an S-latch on the page */
  100. btr_search_drop_page_hash_when_freed(id,
  101. page_no);
  102. goto scan_again;
  103. }
  104. if (block->page.oldest_modification != 0) {
  105. /* Remove from the flush list of modified
  106. blocks */
  107. block->page.oldest_modification = 0;
  108. UT_LIST_REMOVE(flush_list,
  109. buf_pool->flush_list,
  110. &(block->page));
  111. }
  112. /* Remove from the LRU list */
  113. buf_LRU_block_remove_hashed_page(block);
  114. buf_LRU_block_free_hashed_page(block);
  115. }
  116. next_page:
  117. mutex_exit(&block->mutex);
  118. block = prev_block;
  119. }
  120. mutex_exit(&(buf_pool->mutex));
  121. if (!all_freed) {
  122. os_thread_sleep(20000);
  123. goto scan_again;
  124. }
  125. }
  126. /**********************************************************************
  127. Gets the minimum LRU_position field for the blocks in an initial segment
  128. (determined by BUF_LRU_INITIAL_RATIO) of the LRU list. The limit is not
  129. guaranteed to be precise, because the ulint_clock may wrap around. */
  130. ulint
  131. buf_LRU_get_recent_limit(void)
  132. /*==========================*/
  133. /* out: the limit; zero if could not determine it */
  134. {
  135. buf_block_t* block;
  136. ulint len;
  137. ulint limit;
  138. mutex_enter(&(buf_pool->mutex));
  139. len = UT_LIST_GET_LEN(buf_pool->LRU);
  140. if (len < BUF_LRU_OLD_MIN_LEN) {
  141. /* The LRU list is too short to do read-ahead */
  142. mutex_exit(&(buf_pool->mutex));
  143. return(0);
  144. }
  145. block = UT_LIST_GET_FIRST(buf_pool->LRU);
  146. limit = block->LRU_position - len / BUF_LRU_INITIAL_RATIO;
  147. mutex_exit(&(buf_pool->mutex));
  148. return(limit);
  149. }
  150. /**********************************************************************
  151. Try to free a block. */
  152. ibool
  153. buf_LRU_free_block(
  154. /*===============*/
  155. /* out: TRUE if freed */
  156. buf_block_t* block) /* in: block to be freed */
  157. {
  158. #ifdef UNIV_SYNC_DEBUG
  159. ut_ad(mutex_own(&buf_pool->mutex));
  160. ut_ad(mutex_own(&block->mutex));
  161. #endif /* UNIV_SYNC_DEBUG */
  162. ut_a(block->in_LRU_list);
  163. if (!buf_flush_ready_for_replace(block)) {
  164. return(FALSE);
  165. }
  166. #ifdef UNIV_DEBUG
  167. if (buf_debug_prints) {
  168. fprintf(stderr, "Putting space %lu page %lu to free list\n",
  169. (ulong) buf_block_get_space(block),
  170. (ulong) buf_block_get_page_no(block));
  171. }
  172. #endif /* UNIV_DEBUG */
  173. buf_LRU_block_remove_hashed_page(block);
  174. mutex_exit(&(buf_pool->mutex));
  175. mutex_exit(&block->mutex);
  176. /* Remove possible adaptive hash index on the page */
  177. btr_search_drop_page_hash_index(block);
  178. ut_a(block->buf_fix_count == 0);
  179. mutex_enter(&(buf_pool->mutex));
  180. mutex_enter(&block->mutex);
  181. buf_LRU_block_free_hashed_page(block);
  182. return(TRUE);
  183. }
  184. /**********************************************************************
  185. Look for a replaceable block from the end of the LRU list and put it to
  186. the free list if found. */
  187. ibool
  188. buf_LRU_search_and_free_block(
  189. /*==========================*/
  190. /* out: TRUE if freed */
  191. ulint n_iterations) /* in: how many times this has been called
  192. repeatedly without result: a high value means
  193. that we should search farther; if value is
  194. k < 10, then we only search k/10 * [number
  195. of pages in the buffer pool] from the end
  196. of the LRU list */
  197. {
  198. buf_block_t* block;
  199. ulint distance = 0;
  200. ibool freed;
  201. mutex_enter(&(buf_pool->mutex));
  202. freed = FALSE;
  203. block = UT_LIST_GET_LAST(buf_pool->LRU);
  204. while (block != NULL) {
  205. mutex_enter(&block->mutex);
  206. freed = buf_LRU_free_block(block);
  207. mutex_exit(&block->mutex);
  208. if (freed) {
  209. break;
  210. }
  211. block = UT_LIST_GET_PREV(LRU, block);
  212. distance++;
  213. if (n_iterations <= 10
  214. && distance > 100 + (n_iterations * buf_pool->curr_size)
  215. / 10) {
  216. goto func_exit;
  217. }
  218. }
  219. if (buf_pool->LRU_flush_ended > 0) {
  220. buf_pool->LRU_flush_ended--;
  221. }
  222. func_exit:
  223. if (!freed) {
  224. buf_pool->LRU_flush_ended = 0;
  225. }
  226. mutex_exit(&(buf_pool->mutex));
  227. return(freed);
  228. }
  229. /**********************************************************************
  230. Tries to remove LRU flushed blocks from the end of the LRU list and put them
  231. to the free list. This is beneficial for the efficiency of the insert buffer
  232. operation, as flushed pages from non-unique non-clustered indexes are here
  233. taken out of the buffer pool, and their inserts redirected to the insert
  234. buffer. Otherwise, the flushed blocks could get modified again before read
  235. operations need new buffer blocks, and the i/o work done in flushing would be
  236. wasted. */
  237. void
  238. buf_LRU_try_free_flushed_blocks(void)
  239. /*=================================*/
  240. {
  241. mutex_enter(&(buf_pool->mutex));
  242. while (buf_pool->LRU_flush_ended > 0) {
  243. mutex_exit(&(buf_pool->mutex));
  244. buf_LRU_search_and_free_block(1);
  245. mutex_enter(&(buf_pool->mutex));
  246. }
  247. mutex_exit(&(buf_pool->mutex));
  248. }
  249. /**********************************************************************
  250. Returns TRUE if less than 25 % of the buffer pool is available. This can be
  251. used in heuristics to prevent huge transactions eating up the whole buffer
  252. pool for their locks. */
  253. ibool
  254. buf_LRU_buf_pool_running_out(void)
  255. /*==============================*/
  256. /* out: TRUE if less than 25 % of buffer pool
  257. left */
  258. {
  259. ibool ret = FALSE;
  260. mutex_enter(&(buf_pool->mutex));
  261. if (!recv_recovery_on && UT_LIST_GET_LEN(buf_pool->free)
  262. + UT_LIST_GET_LEN(buf_pool->LRU) < buf_pool->curr_size / 4) {
  263. ret = TRUE;
  264. }
  265. mutex_exit(&(buf_pool->mutex));
  266. return(ret);
  267. }
  268. /**********************************************************************
  269. Returns a free block from the buf_pool. The block is taken off the
  270. free list. If it is empty, blocks are moved from the end of the
  271. LRU list to the free list. */
  272. buf_block_t*
  273. buf_LRU_get_free_block(
  274. /*===================*/
  275. /* out: the free control block */
  276. ulint zip_size) /* in: compressed page size in bytes,
  277. or 0 if uncompressed tablespace */
  278. {
  279. buf_block_t* block = NULL;
  280. ibool freed;
  281. ulint n_iterations = 1;
  282. ibool mon_value_was = FALSE;
  283. ibool started_monitor = FALSE;
  284. loop:
  285. mutex_enter(&(buf_pool->mutex));
  286. if (!recv_recovery_on && UT_LIST_GET_LEN(buf_pool->free)
  287. + UT_LIST_GET_LEN(buf_pool->LRU) < buf_pool->curr_size / 20) {
  288. ut_print_timestamp(stderr);
  289. fprintf(stderr,
  290. " InnoDB: ERROR: over 95 percent of the buffer pool"
  291. " is occupied by\n"
  292. "InnoDB: lock heaps or the adaptive hash index!"
  293. " Check that your\n"
  294. "InnoDB: transactions do not set too many row locks.\n"
  295. "InnoDB: Your buffer pool size is %lu MB."
  296. " Maybe you should make\n"
  297. "InnoDB: the buffer pool bigger?\n"
  298. "InnoDB: We intentionally generate a seg fault"
  299. " to print a stack trace\n"
  300. "InnoDB: on Linux!\n",
  301. (ulong) (buf_pool->curr_size
  302. / (1024 * 1024 / UNIV_PAGE_SIZE)));
  303. ut_error;
  304. } else if (!recv_recovery_on
  305. && (UT_LIST_GET_LEN(buf_pool->free)
  306. + UT_LIST_GET_LEN(buf_pool->LRU))
  307. < buf_pool->curr_size / 3) {
  308. if (!buf_lru_switched_on_innodb_mon) {
  309. /* Over 67 % of the buffer pool is occupied by lock
  310. heaps or the adaptive hash index. This may be a memory
  311. leak! */
  312. ut_print_timestamp(stderr);
  313. fprintf(stderr,
  314. " InnoDB: WARNING: over 67 percent of"
  315. " the buffer pool is occupied by\n"
  316. "InnoDB: lock heaps or the adaptive"
  317. " hash index! Check that your\n"
  318. "InnoDB: transactions do not set too many"
  319. " row locks.\n"
  320. "InnoDB: Your buffer pool size is %lu MB."
  321. " Maybe you should make\n"
  322. "InnoDB: the buffer pool bigger?\n"
  323. "InnoDB: Starting the InnoDB Monitor to print"
  324. " diagnostics, including\n"
  325. "InnoDB: lock heap and hash index sizes.\n",
  326. (ulong) (buf_pool->curr_size
  327. / (1024 * 1024 / UNIV_PAGE_SIZE)));
  328. buf_lru_switched_on_innodb_mon = TRUE;
  329. srv_print_innodb_monitor = TRUE;
  330. os_event_set(srv_lock_timeout_thread_event);
  331. }
  332. } else if (buf_lru_switched_on_innodb_mon) {
  333. /* Switch off the InnoDB Monitor; this is a simple way
  334. to stop the monitor if the situation becomes less urgent,
  335. but may also surprise users if the user also switched on the
  336. monitor! */
  337. buf_lru_switched_on_innodb_mon = FALSE;
  338. srv_print_innodb_monitor = FALSE;
  339. }
  340. /* If there is a block in the free list, take it */
  341. if (UT_LIST_GET_LEN(buf_pool->free) > 0) {
  342. block = UT_LIST_GET_FIRST(buf_pool->free);
  343. ut_a(block->in_free_list);
  344. UT_LIST_REMOVE(free, buf_pool->free, block);
  345. block->in_free_list = FALSE;
  346. ut_a(buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE);
  347. ut_a(!block->in_LRU_list);
  348. if (buf_block_get_zip_size(block) != zip_size) {
  349. page_zip_set_size(&block->page.zip, zip_size);
  350. block->page.zip.n_blobs = 0;
  351. block->page.zip.m_start = 0;
  352. block->page.zip.m_end = 0;
  353. if (block->page.zip.data) {
  354. ut_free(block->page.zip.data);
  355. }
  356. if (zip_size) {
  357. /* TODO: allocate zip from an aligned pool */
  358. block->page.zip.data = ut_malloc(zip_size);
  359. } else {
  360. block->page.zip.data = NULL;
  361. }
  362. }
  363. mutex_enter(&block->mutex);
  364. buf_block_set_state(block, BUF_BLOCK_READY_FOR_USE);
  365. UNIV_MEM_VALID(block->frame, UNIV_PAGE_SIZE);
  366. mutex_exit(&block->mutex);
  367. mutex_exit(&(buf_pool->mutex));
  368. if (started_monitor) {
  369. srv_print_innodb_monitor = mon_value_was;
  370. }
  371. return(block);
  372. }
  373. /* If no block was in the free list, search from the end of the LRU
  374. list and try to free a block there */
  375. mutex_exit(&(buf_pool->mutex));
  376. freed = buf_LRU_search_and_free_block(n_iterations);
  377. if (freed > 0) {
  378. goto loop;
  379. }
  380. if (n_iterations > 30) {
  381. ut_print_timestamp(stderr);
  382. fprintf(stderr,
  383. "InnoDB: Warning: difficult to find free blocks from\n"
  384. "InnoDB: the buffer pool (%lu search iterations)!"
  385. " Consider\n"
  386. "InnoDB: increasing the buffer pool size.\n"
  387. "InnoDB: It is also possible that"
  388. " in your Unix version\n"
  389. "InnoDB: fsync is very slow, or"
  390. " completely frozen inside\n"
  391. "InnoDB: the OS kernel. Then upgrading to"
  392. " a newer version\n"
  393. "InnoDB: of your operating system may help."
  394. " Look at the\n"
  395. "InnoDB: number of fsyncs in diagnostic info below.\n"
  396. "InnoDB: Pending flushes (fsync) log: %lu;"
  397. " buffer pool: %lu\n"
  398. "InnoDB: %lu OS file reads, %lu OS file writes,"
  399. " %lu OS fsyncs\n"
  400. "InnoDB: Starting InnoDB Monitor to print further\n"
  401. "InnoDB: diagnostics to the standard output.\n",
  402. (ulong) n_iterations,
  403. (ulong) fil_n_pending_log_flushes,
  404. (ulong) fil_n_pending_tablespace_flushes,
  405. (ulong) os_n_file_reads, (ulong) os_n_file_writes,
  406. (ulong) os_n_fsyncs);
  407. mon_value_was = srv_print_innodb_monitor;
  408. started_monitor = TRUE;
  409. srv_print_innodb_monitor = TRUE;
  410. os_event_set(srv_lock_timeout_thread_event);
  411. }
  412. /* No free block was found: try to flush the LRU list */
  413. buf_flush_free_margin();
  414. ++srv_buf_pool_wait_free;
  415. os_aio_simulated_wake_handler_threads();
  416. mutex_enter(&(buf_pool->mutex));
  417. if (buf_pool->LRU_flush_ended > 0) {
  418. /* We have written pages in an LRU flush. To make the insert
  419. buffer more efficient, we try to move these pages to the free
  420. list. */
  421. mutex_exit(&(buf_pool->mutex));
  422. buf_LRU_try_free_flushed_blocks();
  423. } else {
  424. mutex_exit(&(buf_pool->mutex));
  425. }
  426. if (n_iterations > 10) {
  427. os_thread_sleep(500000);
  428. }
  429. n_iterations++;
  430. goto loop;
  431. }
  432. /***********************************************************************
  433. Moves the LRU_old pointer so that the length of the old blocks list
  434. is inside the allowed limits. */
  435. UNIV_INLINE
  436. void
  437. buf_LRU_old_adjust_len(void)
  438. /*========================*/
  439. {
  440. ulint old_len;
  441. ulint new_len;
  442. ut_a(buf_pool->LRU_old);
  443. #ifdef UNIV_SYNC_DEBUG
  444. ut_ad(mutex_own(&(buf_pool->mutex)));
  445. #endif /* UNIV_SYNC_DEBUG */
  446. ut_ad(3 * (BUF_LRU_OLD_MIN_LEN / 8) > BUF_LRU_OLD_TOLERANCE + 5);
  447. for (;;) {
  448. old_len = buf_pool->LRU_old_len;
  449. new_len = 3 * (UT_LIST_GET_LEN(buf_pool->LRU) / 8);
  450. ut_a(buf_pool->LRU_old->in_LRU_list);
  451. /* Update the LRU_old pointer if necessary */
  452. if (old_len < new_len - BUF_LRU_OLD_TOLERANCE) {
  453. buf_pool->LRU_old = UT_LIST_GET_PREV(
  454. LRU, buf_pool->LRU_old);
  455. buf_pool->LRU_old->old = TRUE;
  456. buf_pool->LRU_old_len++;
  457. } else if (old_len > new_len + BUF_LRU_OLD_TOLERANCE) {
  458. buf_pool->LRU_old->old = FALSE;
  459. buf_pool->LRU_old = UT_LIST_GET_NEXT(
  460. LRU, buf_pool->LRU_old);
  461. buf_pool->LRU_old_len--;
  462. } else {
  463. ut_a(buf_pool->LRU_old); /* Check that we did not
  464. fall out of the LRU list */
  465. return;
  466. }
  467. }
  468. }
  469. /***********************************************************************
  470. Initializes the old blocks pointer in the LRU list. This function should be
  471. called when the LRU list grows to BUF_LRU_OLD_MIN_LEN length. */
  472. static
  473. void
  474. buf_LRU_old_init(void)
  475. /*==================*/
  476. {
  477. buf_block_t* block;
  478. ut_a(UT_LIST_GET_LEN(buf_pool->LRU) == BUF_LRU_OLD_MIN_LEN);
  479. /* We first initialize all blocks in the LRU list as old and then use
  480. the adjust function to move the LRU_old pointer to the right
  481. position */
  482. block = UT_LIST_GET_FIRST(buf_pool->LRU);
  483. while (block != NULL) {
  484. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  485. ut_a(block->in_LRU_list);
  486. block->old = TRUE;
  487. block = UT_LIST_GET_NEXT(LRU, block);
  488. }
  489. buf_pool->LRU_old = UT_LIST_GET_FIRST(buf_pool->LRU);
  490. buf_pool->LRU_old_len = UT_LIST_GET_LEN(buf_pool->LRU);
  491. buf_LRU_old_adjust_len();
  492. }
  493. /**********************************************************************
  494. Removes a block from the LRU list. */
  495. UNIV_INLINE
  496. void
  497. buf_LRU_remove_block(
  498. /*=================*/
  499. buf_block_t* block) /* in: control block */
  500. {
  501. ut_ad(buf_pool);
  502. ut_ad(block);
  503. #ifdef UNIV_SYNC_DEBUG
  504. ut_ad(mutex_own(&(buf_pool->mutex)));
  505. #endif /* UNIV_SYNC_DEBUG */
  506. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  507. ut_a(block->in_LRU_list);
  508. /* If the LRU_old pointer is defined and points to just this block,
  509. move it backward one step */
  510. if (block == buf_pool->LRU_old) {
  511. /* Below: the previous block is guaranteed to exist, because
  512. the LRU_old pointer is only allowed to differ by the
  513. tolerance value from strict 3/8 of the LRU list length. */
  514. buf_pool->LRU_old = UT_LIST_GET_PREV(LRU, block);
  515. buf_pool->LRU_old->old = TRUE;
  516. buf_pool->LRU_old_len++;
  517. ut_a(buf_pool->LRU_old);
  518. }
  519. /* Remove the block from the LRU list */
  520. UT_LIST_REMOVE(LRU, buf_pool->LRU, block);
  521. block->in_LRU_list = FALSE;
  522. /* If the LRU list is so short that LRU_old not defined, return */
  523. if (UT_LIST_GET_LEN(buf_pool->LRU) < BUF_LRU_OLD_MIN_LEN) {
  524. buf_pool->LRU_old = NULL;
  525. return;
  526. }
  527. ut_ad(buf_pool->LRU_old);
  528. /* Update the LRU_old_len field if necessary */
  529. if (block->old) {
  530. buf_pool->LRU_old_len--;
  531. }
  532. /* Adjust the length of the old block list if necessary */
  533. buf_LRU_old_adjust_len();
  534. }
  535. /**********************************************************************
  536. Adds a block to the LRU list end. */
  537. UNIV_INLINE
  538. void
  539. buf_LRU_add_block_to_end_low(
  540. /*=========================*/
  541. buf_block_t* block) /* in: control block */
  542. {
  543. buf_block_t* last_block;
  544. ut_ad(buf_pool);
  545. ut_ad(block);
  546. #ifdef UNIV_SYNC_DEBUG
  547. ut_ad(mutex_own(&(buf_pool->mutex)));
  548. #endif /* UNIV_SYNC_DEBUG */
  549. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  550. block->old = TRUE;
  551. last_block = UT_LIST_GET_LAST(buf_pool->LRU);
  552. if (last_block) {
  553. block->LRU_position = last_block->LRU_position;
  554. } else {
  555. block->LRU_position = buf_pool_clock_tic();
  556. }
  557. ut_a(!block->in_LRU_list);
  558. UT_LIST_ADD_LAST(LRU, buf_pool->LRU, block);
  559. block->in_LRU_list = TRUE;
  560. if (UT_LIST_GET_LEN(buf_pool->LRU) >= BUF_LRU_OLD_MIN_LEN) {
  561. buf_pool->LRU_old_len++;
  562. }
  563. if (UT_LIST_GET_LEN(buf_pool->LRU) > BUF_LRU_OLD_MIN_LEN) {
  564. ut_ad(buf_pool->LRU_old);
  565. /* Adjust the length of the old block list if necessary */
  566. buf_LRU_old_adjust_len();
  567. } else if (UT_LIST_GET_LEN(buf_pool->LRU) == BUF_LRU_OLD_MIN_LEN) {
  568. /* The LRU list is now long enough for LRU_old to become
  569. defined: init it */
  570. buf_LRU_old_init();
  571. }
  572. }
  573. /**********************************************************************
  574. Adds a block to the LRU list. */
  575. UNIV_INLINE
  576. void
  577. buf_LRU_add_block_low(
  578. /*==================*/
  579. buf_block_t* block, /* in: control block */
  580. ibool old) /* in: TRUE if should be put to the old blocks
  581. in the LRU list, else put to the start; if the
  582. LRU list is very short, the block is added to
  583. the start, regardless of this parameter */
  584. {
  585. ulint cl;
  586. ut_ad(buf_pool);
  587. ut_ad(block);
  588. #ifdef UNIV_SYNC_DEBUG
  589. ut_ad(mutex_own(&(buf_pool->mutex)));
  590. #endif /* UNIV_SYNC_DEBUG */
  591. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  592. ut_a(!block->in_LRU_list);
  593. block->old = old;
  594. cl = buf_pool_clock_tic();
  595. if (!old || (UT_LIST_GET_LEN(buf_pool->LRU) < BUF_LRU_OLD_MIN_LEN)) {
  596. UT_LIST_ADD_FIRST(LRU, buf_pool->LRU, block);
  597. block->LRU_position = cl;
  598. block->freed_page_clock = buf_pool->freed_page_clock;
  599. } else {
  600. UT_LIST_INSERT_AFTER(LRU, buf_pool->LRU, buf_pool->LRU_old,
  601. block);
  602. buf_pool->LRU_old_len++;
  603. /* We copy the LRU position field of the previous block
  604. to the new block */
  605. block->LRU_position = (buf_pool->LRU_old)->LRU_position;
  606. }
  607. block->in_LRU_list = TRUE;
  608. if (UT_LIST_GET_LEN(buf_pool->LRU) > BUF_LRU_OLD_MIN_LEN) {
  609. ut_ad(buf_pool->LRU_old);
  610. /* Adjust the length of the old block list if necessary */
  611. buf_LRU_old_adjust_len();
  612. } else if (UT_LIST_GET_LEN(buf_pool->LRU) == BUF_LRU_OLD_MIN_LEN) {
  613. /* The LRU list is now long enough for LRU_old to become
  614. defined: init it */
  615. buf_LRU_old_init();
  616. }
  617. }
  618. /**********************************************************************
  619. Adds a block to the LRU list. */
  620. void
  621. buf_LRU_add_block(
  622. /*==============*/
  623. buf_block_t* block, /* in: control block */
  624. ibool old) /* in: TRUE if should be put to the old
  625. blocks in the LRU list, else put to the start;
  626. if the LRU list is very short, the block is
  627. added to the start, regardless of this
  628. parameter */
  629. {
  630. buf_LRU_add_block_low(block, old);
  631. }
  632. /**********************************************************************
  633. Moves a block to the start of the LRU list. */
  634. void
  635. buf_LRU_make_block_young(
  636. /*=====================*/
  637. buf_block_t* block) /* in: control block */
  638. {
  639. buf_LRU_remove_block(block);
  640. buf_LRU_add_block_low(block, FALSE);
  641. }
  642. /**********************************************************************
  643. Moves a block to the end of the LRU list. */
  644. void
  645. buf_LRU_make_block_old(
  646. /*===================*/
  647. buf_block_t* block) /* in: control block */
  648. {
  649. buf_LRU_remove_block(block);
  650. buf_LRU_add_block_to_end_low(block);
  651. }
  652. /**********************************************************************
  653. Puts a block back to the free list. */
  654. void
  655. buf_LRU_block_free_non_file_page(
  656. /*=============================*/
  657. buf_block_t* block) /* in: block, must not contain a file page */
  658. {
  659. #ifdef UNIV_SYNC_DEBUG
  660. ut_ad(mutex_own(&(buf_pool->mutex)));
  661. ut_ad(mutex_own(&block->mutex));
  662. #endif /* UNIV_SYNC_DEBUG */
  663. ut_ad(block);
  664. switch (buf_block_get_state(block)) {
  665. case BUF_BLOCK_MEMORY:
  666. case BUF_BLOCK_READY_FOR_USE:
  667. break;
  668. default:
  669. ut_error;
  670. }
  671. ut_ad(block->n_pointers == 0);
  672. ut_a(!block->in_free_list);
  673. buf_block_set_state(block, BUF_BLOCK_NOT_USED);
  674. #ifdef UNIV_DEBUG
  675. /* Wipe contents of page to reveal possible stale pointers to it */
  676. memset(block->frame, '\0', UNIV_PAGE_SIZE);
  677. #else
  678. /* Wipe page_no and space_id */
  679. memset(block->frame + FIL_PAGE_OFFSET, 0xfe, 4);
  680. memset(block->frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, 0xfe, 4);
  681. #endif
  682. if (block->page.zip.data) {
  683. /* TODO: return zip to an aligned pool */
  684. ut_free(block->page.zip.data);
  685. block->page.zip.data = NULL;
  686. page_zip_set_size(&block->page.zip, 0);
  687. }
  688. UT_LIST_ADD_FIRST(free, buf_pool->free, block);
  689. block->in_free_list = TRUE;
  690. UNIV_MEM_INVALID(block->frame, UNIV_PAGE_SIZE);
  691. }
  692. /**********************************************************************
  693. Takes a block out of the LRU list and page hash table and sets the block
  694. state to BUF_BLOCK_REMOVE_HASH. */
  695. static
  696. void
  697. buf_LRU_block_remove_hashed_page(
  698. /*=============================*/
  699. buf_block_t* block) /* in: block, must contain a file page and
  700. be in a state where it can be freed; there
  701. may or may not be a hash index to the page */
  702. {
  703. const buf_block_t* hashed_block;
  704. #ifdef UNIV_SYNC_DEBUG
  705. ut_ad(mutex_own(&(buf_pool->mutex)));
  706. ut_ad(mutex_own(&block->mutex));
  707. #endif /* UNIV_SYNC_DEBUG */
  708. ut_ad(block);
  709. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  710. ut_a(block->io_fix == 0);
  711. ut_a(block->buf_fix_count == 0);
  712. ut_a(block->page.oldest_modification == 0);
  713. buf_LRU_remove_block(block);
  714. buf_pool->freed_page_clock += 1;
  715. buf_block_modify_clock_inc(block);
  716. hashed_block = buf_page_hash_get(block->page.space,
  717. block->page.offset);
  718. if (UNIV_UNLIKELY(block != hashed_block)) {
  719. fprintf(stderr,
  720. "InnoDB: Error: page %lu %lu not found"
  721. " in the hash table\n",
  722. (ulong) block->page.space,
  723. (ulong) block->page.offset);
  724. if (hashed_block) {
  725. fprintf(stderr,
  726. "InnoDB: In hash table we find block"
  727. " %p of %lu %lu which is not %p\n",
  728. (const void*) hashed_block,
  729. (ulong) hashed_block->page.space,
  730. (ulong) hashed_block->page.offset,
  731. (void*) block);
  732. }
  733. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  734. buf_print();
  735. buf_LRU_print();
  736. buf_validate();
  737. buf_LRU_validate();
  738. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  739. ut_error;
  740. }
  741. HASH_DELETE(buf_block_t, hash, buf_pool->page_hash,
  742. buf_page_address_fold(block->page.space,
  743. block->page.offset),
  744. block);
  745. memset(block->frame + FIL_PAGE_OFFSET, 0xff, 4);
  746. memset(block->frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, 0xff, 4);
  747. buf_block_set_state(block, BUF_BLOCK_REMOVE_HASH);
  748. }
  749. /**********************************************************************
  750. Puts a file page whose has no hash index to the free list. */
  751. static
  752. void
  753. buf_LRU_block_free_hashed_page(
  754. /*===========================*/
  755. buf_block_t* block) /* in: block, must contain a file page and
  756. be in a state where it can be freed */
  757. {
  758. #ifdef UNIV_SYNC_DEBUG
  759. ut_ad(mutex_own(&(buf_pool->mutex)));
  760. ut_ad(mutex_own(&block->mutex));
  761. #endif /* UNIV_SYNC_DEBUG */
  762. ut_a(buf_block_get_state(block) == BUF_BLOCK_REMOVE_HASH);
  763. buf_block_set_state(block, BUF_BLOCK_MEMORY);
  764. buf_LRU_block_free_non_file_page(block);
  765. }
  766. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  767. /**************************************************************************
  768. Validates the LRU list. */
  769. ibool
  770. buf_LRU_validate(void)
  771. /*==================*/
  772. {
  773. buf_block_t* block;
  774. ulint old_len;
  775. ulint new_len;
  776. ulint LRU_pos;
  777. ut_ad(buf_pool);
  778. mutex_enter(&(buf_pool->mutex));
  779. if (UT_LIST_GET_LEN(buf_pool->LRU) >= BUF_LRU_OLD_MIN_LEN) {
  780. ut_a(buf_pool->LRU_old);
  781. old_len = buf_pool->LRU_old_len;
  782. new_len = 3 * (UT_LIST_GET_LEN(buf_pool->LRU) / 8);
  783. ut_a(old_len >= new_len - BUF_LRU_OLD_TOLERANCE);
  784. ut_a(old_len <= new_len + BUF_LRU_OLD_TOLERANCE);
  785. }
  786. UT_LIST_VALIDATE(LRU, buf_block_t, buf_pool->LRU);
  787. block = UT_LIST_GET_FIRST(buf_pool->LRU);
  788. old_len = 0;
  789. while (block != NULL) {
  790. ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
  791. if (block->old) {
  792. old_len++;
  793. }
  794. if (buf_pool->LRU_old && (old_len == 1)) {
  795. ut_a(buf_pool->LRU_old == block);
  796. }
  797. LRU_pos = block->LRU_position;
  798. block = UT_LIST_GET_NEXT(LRU, block);
  799. if (block) {
  800. /* If the following assert fails, it may
  801. not be an error: just the buf_pool clock
  802. has wrapped around */
  803. ut_a(LRU_pos >= block->LRU_position);
  804. }
  805. }
  806. if (buf_pool->LRU_old) {
  807. ut_a(buf_pool->LRU_old_len == old_len);
  808. }
  809. UT_LIST_VALIDATE(free, buf_block_t, buf_pool->free);
  810. block = UT_LIST_GET_FIRST(buf_pool->free);
  811. while (block != NULL) {
  812. ut_a(buf_block_get_state(block) == BUF_BLOCK_NOT_USED);
  813. block = UT_LIST_GET_NEXT(free, block);
  814. }
  815. mutex_exit(&(buf_pool->mutex));
  816. return(TRUE);
  817. }
  818. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  819. #if defined UNIV_DEBUG_PRINT || defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  820. /**************************************************************************
  821. Prints the LRU list. */
  822. void
  823. buf_LRU_print(void)
  824. /*===============*/
  825. {
  826. buf_block_t* block;
  827. buf_frame_t* frame;
  828. ut_ad(buf_pool);
  829. mutex_enter(&(buf_pool->mutex));
  830. fprintf(stderr, "Pool ulint clock %lu\n",
  831. (ulong) buf_pool->ulint_clock);
  832. block = UT_LIST_GET_FIRST(buf_pool->LRU);
  833. while (block != NULL) {
  834. fprintf(stderr, "BLOCK space %lu page %lu ",
  835. (ulong) buf_block_get_space(block),
  836. (ulong) buf_block_get_page_no(block));
  837. if (block->old) {
  838. fputs("old ", stderr);
  839. }
  840. if (block->buf_fix_count) {
  841. fprintf(stderr, "buffix count %lu ",
  842. (ulong) block->buf_fix_count);
  843. }
  844. if (block->io_fix) {
  845. fprintf(stderr, "io_fix %lu ", (ulong) block->io_fix);
  846. }
  847. if (block->page.oldest_modification) {
  848. fputs("modif. ", stderr);
  849. }
  850. frame = buf_block_get_frame(block);
  851. fprintf(stderr, "\nLRU pos %lu type %lu index id %lu\n",
  852. (ulong) block->LRU_position,
  853. (ulong) fil_page_get_type(frame),
  854. (ulong) ut_dulint_get_low(
  855. btr_page_get_index_id(frame)));
  856. block = UT_LIST_GET_NEXT(LRU, block);
  857. }
  858. mutex_exit(&(buf_pool->mutex));
  859. }
  860. #endif /* UNIV_DEBUG_PRINT || UNIV_DEBUG || UNIV_BUF_DEBUG */