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.

267 lines
10 KiB

17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
14 years ago
16 years ago
16 years ago
14 years ago
16 years ago
14 years ago
14 years ago
16 years ago
16 years ago
16 years ago
14 years ago
14 years ago
16 years ago
14 years ago
16 years ago
14 years ago
16 years ago
16 years ago
16 years ago
14 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
14 years ago
16 years ago
16 years ago
14 years ago
14 years ago
14 years ago
16 years ago
14 years ago
16 years ago
14 years ago
16 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1995, 2010, Innobase Oy. 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 St, Fifth Floor, Boston, MA 02110-1301 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file include/buf0flu.h
  15. The database buffer pool flush algorithm
  16. Created 11/5/1995 Heikki Tuuri
  17. *******************************************************/
  18. #ifndef buf0flu_h
  19. #define buf0flu_h
  20. #include "univ.i"
  21. #include "ut0byte.h"
  22. #ifndef UNIV_HOTBACKUP
  23. #include "mtr0types.h"
  24. #include "buf0types.h"
  25. #include "log0log.h"
  26. /********************************************************************//**
  27. Remove a block from the flush list of modified blocks. */
  28. UNIV_INTERN
  29. void
  30. buf_flush_remove(
  31. /*=============*/
  32. buf_page_t* bpage); /*!< in: pointer to the block in question */
  33. /*******************************************************************//**
  34. Relocates a buffer control block on the flush_list.
  35. Note that it is assumed that the contents of bpage has already been
  36. copied to dpage. */
  37. UNIV_INTERN
  38. void
  39. buf_flush_relocate_on_flush_list(
  40. /*=============================*/
  41. buf_page_t* bpage, /*!< in/out: control block being moved */
  42. buf_page_t* dpage); /*!< in/out: destination block */
  43. /********************************************************************//**
  44. Updates the flush system data structures when a write is completed. */
  45. UNIV_INTERN
  46. void
  47. buf_flush_write_complete(
  48. /*=====================*/
  49. buf_page_t* bpage); /*!< in: pointer to the block in question */
  50. /*********************************************************************//**
  51. Flushes pages from the end of the LRU list if there is too small
  52. a margin of replaceable pages there. If buffer pool is NULL it
  53. means flush free margin on all buffer pool instances. */
  54. UNIV_INTERN
  55. void
  56. buf_flush_free_margin(
  57. /*==================*/
  58. buf_pool_t* buf_pool,
  59. ibool wait);
  60. /*********************************************************************//**
  61. Flushes pages from the end of all the LRU lists. */
  62. UNIV_INTERN
  63. void
  64. buf_flush_free_margins(
  65. /*=========================*/
  66. ibool wait);
  67. #endif /* !UNIV_HOTBACKUP */
  68. /********************************************************************//**
  69. Initializes a page for writing to the tablespace. */
  70. UNIV_INTERN
  71. void
  72. buf_flush_init_for_writing(
  73. /*=======================*/
  74. byte* page, /*!< in/out: page */
  75. void* page_zip_, /*!< in/out: compressed page, or NULL */
  76. ib_uint64_t newest_lsn); /*!< in: newest modification lsn
  77. to the page */
  78. #ifndef UNIV_HOTBACKUP
  79. # if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG
  80. /********************************************************************//**
  81. Writes a flushable page asynchronously from the buffer pool to a file.
  82. NOTE: buf_pool->mutex and block->mutex must be held upon entering this
  83. function, and they will be released by this function after flushing.
  84. This is loosely based on buf_flush_batch() and buf_flush_page().
  85. @return TRUE if the page was flushed and the mutexes released */
  86. UNIV_INTERN
  87. ibool
  88. buf_flush_page_try(
  89. /*===============*/
  90. buf_pool_t* buf_pool, /*!< in/out: buffer pool instance */
  91. buf_block_t* block) /*!< in/out: buffer control block */
  92. __attribute__((nonnull, warn_unused_result));
  93. # endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */
  94. /*******************************************************************//**
  95. This utility flushes dirty blocks from the end of the LRU list.
  96. NOTE: The calling thread may own latches to pages: to avoid deadlocks,
  97. this function must be written so that it cannot end up waiting for these
  98. latches!
  99. @return number of blocks for which the write request was queued;
  100. ULINT_UNDEFINED if there was a flush of the same type already running */
  101. UNIV_INTERN
  102. ulint
  103. buf_flush_LRU(
  104. /*==========*/
  105. buf_pool_t* buf_pool, /*!< in: buffer pool instance */
  106. ulint min_n); /*!< in: wished minimum mumber of blocks
  107. flushed (it is not guaranteed that the
  108. actual number is that big, though) */
  109. /*******************************************************************//**
  110. This utility flushes dirty blocks from the end of the flush_list of
  111. all buffer pool instances.
  112. NOTE: The calling thread is not allowed to own any latches on pages!
  113. @return number of blocks for which the write request was queued;
  114. ULINT_UNDEFINED if there was a flush of the same type already running */
  115. UNIV_INTERN
  116. ulint
  117. buf_flush_list(
  118. /*============*/
  119. ulint min_n, /*!< in: wished minimum mumber of blocks
  120. flushed (it is not guaranteed that the
  121. actual number is that big, though) */
  122. ib_uint64_t lsn_limit); /*!< in the case BUF_FLUSH_LIST all
  123. blocks whose oldest_modification is
  124. smaller than this should be flushed
  125. (if their number does not exceed
  126. min_n), otherwise ignored */
  127. /******************************************************************//**
  128. Waits until a flush batch of the given type ends */
  129. UNIV_INTERN
  130. void
  131. buf_flush_wait_batch_end(
  132. /*=====================*/
  133. buf_pool_t* buf_pool, /*!< in: buffer pool instance */
  134. enum buf_flush type); /*!< in: BUF_FLUSH_LRU
  135. or BUF_FLUSH_LIST */
  136. /******************************************************************//**
  137. Waits until a flush batch of the given type ends. This is called by
  138. a thread that only wants to wait for a flush to end but doesn't do
  139. any flushing itself. */
  140. UNIV_INTERN
  141. void
  142. buf_flush_wait_batch_end_wait_only(
  143. /*===============================*/
  144. buf_pool_t* buf_pool, /*!< in: buffer pool instance */
  145. enum buf_flush type); /*!< in: BUF_FLUSH_LRU
  146. or BUF_FLUSH_LIST */
  147. /********************************************************************//**
  148. This function should be called at a mini-transaction commit, if a page was
  149. modified in it. Puts the block to the list of modified blocks, if it not
  150. already in it. */
  151. UNIV_INLINE
  152. void
  153. buf_flush_note_modification(
  154. /*========================*/
  155. buf_block_t* block, /*!< in: block which is modified */
  156. mtr_t* mtr); /*!< in: mtr */
  157. /********************************************************************//**
  158. This function should be called when recovery has modified a buffer page. */
  159. UNIV_INLINE
  160. void
  161. buf_flush_recv_note_modification(
  162. /*=============================*/
  163. buf_block_t* block, /*!< in: block which is modified */
  164. ib_uint64_t start_lsn, /*!< in: start lsn of the first mtr in a
  165. set of mtr's */
  166. ib_uint64_t end_lsn); /*!< in: end lsn of the last mtr in the
  167. set of mtr's */
  168. /********************************************************************//**
  169. Returns TRUE if the file page block is immediately suitable for replacement,
  170. i.e., transition FILE_PAGE => NOT_USED allowed.
  171. @return TRUE if can replace immediately */
  172. UNIV_INTERN
  173. ibool
  174. buf_flush_ready_for_replace(
  175. /*========================*/
  176. buf_page_t* bpage); /*!< in: buffer control block, must be
  177. buf_page_in_file(bpage) and in the LRU list */
  178. /** @brief Statistics for selecting flush rate based on redo log
  179. generation speed.
  180. These statistics are generated for heuristics used in estimating the
  181. rate at which we should flush the dirty blocks to avoid bursty IO
  182. activity. Note that the rate of flushing not only depends on how many
  183. dirty pages we have in the buffer pool but it is also a fucntion of
  184. how much redo the workload is generating and at what rate. */
  185. struct buf_flush_stat_struct
  186. {
  187. ib_uint64_t redo; /**< amount of redo generated. */
  188. ulint n_flushed; /**< number of pages flushed. */
  189. };
  190. /** Statistics for selecting flush rate of dirty pages. */
  191. typedef struct buf_flush_stat_struct buf_flush_stat_t;
  192. /*********************************************************************
  193. Update the historical stats that we are collecting for flush rate
  194. heuristics at the end of each interval. */
  195. UNIV_INTERN
  196. void
  197. buf_flush_stat_update(void);
  198. /*=======================*/
  199. /*********************************************************************
  200. Determines the fraction of dirty pages that need to be flushed based
  201. on the speed at which we generate redo log. Note that if redo log
  202. is generated at significant rate without a corresponding increase
  203. in the number of dirty pages (for example, an in-memory workload)
  204. it can cause IO bursts of flushing. This function implements heuristics
  205. to avoid this burstiness.
  206. @return number of dirty pages to be flushed / second */
  207. UNIV_INTERN
  208. ulint
  209. buf_flush_get_desired_flush_rate(void);
  210. /*==================================*/
  211. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  212. /******************************************************************//**
  213. Validates the flush list.
  214. @return TRUE if ok */
  215. UNIV_INTERN
  216. ibool
  217. buf_flush_validate(
  218. /*===============*/
  219. buf_pool_t* buf_pool);
  220. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  221. /********************************************************************//**
  222. Initialize the red-black tree to speed up insertions into the flush_list
  223. during recovery process. Should be called at the start of recovery
  224. process before any page has been read/written. */
  225. UNIV_INTERN
  226. void
  227. buf_flush_init_flush_rbt(void);
  228. /*==========================*/
  229. /********************************************************************//**
  230. Frees up the red-black tree. */
  231. UNIV_INTERN
  232. void
  233. buf_flush_free_flush_rbt(void);
  234. /*==========================*/
  235. /** When buf_flush_free_margin is called, it tries to make this many blocks
  236. available to replacement in the free list and at the end of the LRU list (to
  237. make sure that a read-ahead batch can be read efficiently in a single
  238. sweep). */
  239. #define BUF_FLUSH_FREE_BLOCK_MARGIN(b) (5 + BUF_READ_AHEAD_AREA(b))
  240. /** Extra margin to apply above BUF_FLUSH_FREE_BLOCK_MARGIN */
  241. #define BUF_FLUSH_EXTRA_MARGIN(b) ((BUF_FLUSH_FREE_BLOCK_MARGIN(b) / 4 \
  242. + 100) / srv_buf_pool_instances)
  243. #endif /* !UNIV_HOTBACKUP */
  244. #ifndef UNIV_NONINL
  245. #include "buf0flu.ic"
  246. #endif
  247. #endif