1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2013, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation.  The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /**************************************************//**
28 @file include/buf0rea.h
29 The database buffer read
30 
31 Created 11/5/1995 Heikki Tuuri
32 *******************************************************/
33 
34 #ifndef buf0rea_h
35 #define buf0rea_h
36 
37 #include "univ.i"
38 #include "buf0types.h"
39 
40 /********************************************************************//**
41 High-level function which reads a page asynchronously from a file to the
42 buffer buf_pool if it is not already there. Sets the io_fix flag and sets
43 an exclusive lock on the buffer frame. The flag is cleared and the x-lock
44 released by the i/o-handler thread.
45 @return TRUE if page has been read in, FALSE in case of failure */
46 UNIV_INTERN
47 ibool
48 buf_read_page(
49 /*==========*/
50 	ulint	space,	/*!< in: space id */
51 	ulint	zip_size,/*!< in: compressed page size in bytes, or 0 */
52 	ulint	offset);/*!< in: page number */
53 /********************************************************************//**
54 High-level function which reads a page asynchronously from a file to the
55 buffer buf_pool if it is not already there. Sets the io_fix flag and sets
56 an exclusive lock on the buffer frame. The flag is cleared and the x-lock
57 released by the i/o-handler thread.
58 @return TRUE if page has been read in, FALSE in case of failure */
59 UNIV_INTERN
60 ibool
61 buf_read_page_async(
62 /*================*/
63 	ulint	space,	/*!< in: space id */
64 	ulint	offset);/*!< in: page number */
65 /********************************************************************//**
66 Applies a random read-ahead in buf_pool if there are at least a threshold
67 value of accessed pages from the random read-ahead area. Does not read any
68 page, not even the one at the position (space, offset), if the read-ahead
69 mechanism is not activated. NOTE 1: the calling thread may own latches on
70 pages: to avoid deadlocks this function must be written such that it cannot
71 end up waiting for these latches! NOTE 2: the calling thread must want
72 access to the page given: this rule is set to prevent unintended read-aheads
73 performed by ibuf routines, a situation which could result in a deadlock if
74 the OS does not support asynchronous i/o.
75 @return number of page read requests issued; NOTE that if we read ibuf
76 pages, it may happen that the page at the given page number does not
77 get read even if we return a positive value!
78 @return	number of page read requests issued */
79 UNIV_INTERN
80 ulint
81 buf_read_ahead_random(
82 /*==================*/
83 	ulint	space,		/*!< in: space id */
84 	ulint	zip_size,	/*!< in: compressed page size in bytes,
85 				or 0 */
86 	ulint	offset,		/*!< in: page number of a page which
87 				the current thread wants to access */
88 	ibool	inside_ibuf);	/*!< in: TRUE if we are inside ibuf
89 				routine */
90 /********************************************************************//**
91 Applies linear read-ahead if in the buf_pool the page is a border page of
92 a linear read-ahead area and all the pages in the area have been accessed.
93 Does not read any page if the read-ahead mechanism is not activated. Note
94 that the algorithm looks at the 'natural' adjacent successor and
95 predecessor of the page, which on the leaf level of a B-tree are the next
96 and previous page in the chain of leaves. To know these, the page specified
97 in (space, offset) must already be present in the buf_pool. Thus, the
98 natural way to use this function is to call it when a page in the buf_pool
99 is accessed the first time, calling this function just after it has been
100 bufferfixed.
101 NOTE 1: as this function looks at the natural predecessor and successor
102 fields on the page, what happens, if these are not initialized to any
103 sensible value? No problem, before applying read-ahead we check that the
104 area to read is within the span of the space, if not, read-ahead is not
105 applied. An uninitialized value may result in a useless read operation, but
106 only very improbably.
107 NOTE 2: the calling thread may own latches on pages: to avoid deadlocks this
108 function must be written such that it cannot end up waiting for these
109 latches!
110 NOTE 3: the calling thread must want access to the page given: this rule is
111 set to prevent unintended read-aheads performed by ibuf routines, a situation
112 which could result in a deadlock if the OS does not support asynchronous io.
113 @return	number of page read requests issued */
114 UNIV_INTERN
115 ulint
116 buf_read_ahead_linear(
117 /*==================*/
118 	ulint	space,		/*!< in: space id */
119 	ulint	zip_size,	/*!< in: compressed page size in bytes, or 0 */
120 	ulint	offset,		/*!< in: page number; see NOTE 3 above */
121 	ibool	inside_ibuf);	/*!< in: TRUE if we are inside ibuf routine */
122 /********************************************************************//**
123 Issues read requests for pages which the ibuf module wants to read in, in
124 order to contract the insert buffer tree. Technically, this function is like
125 a read-ahead function. */
126 UNIV_INTERN
127 void
128 buf_read_ibuf_merge_pages(
129 /*======================*/
130 	bool		sync,		/*!< in: true if the caller
131 					wants this function to wait
132 					for the highest address page
133 					to get read in, before this
134 					function returns */
135 	const ulint*	space_ids,	/*!< in: array of space ids */
136 	const ib_int64_t* space_versions,/*!< in: the spaces must have
137 					this version number
138 					(timestamp), otherwise we
139 					discard the read; we use this
140 					to cancel reads if DISCARD +
141 					IMPORT may have changed the
142 					tablespace size */
143 	const ulint*	page_nos,	/*!< in: array of page numbers
144 					to read, with the highest page
145 					number the last in the
146 					array */
147 	ulint		n_stored);	/*!< in: number of elements
148 					in the arrays */
149 /********************************************************************//**
150 Issues read requests for pages which recovery wants to read in. */
151 UNIV_INTERN
152 void
153 buf_read_recv_pages(
154 /*================*/
155 	ibool		sync,		/*!< in: TRUE if the caller
156 					wants this function to wait
157 					for the highest address page
158 					to get read in, before this
159 					function returns */
160 	ulint		space,		/*!< in: space id */
161 	ulint		zip_size,	/*!< in: compressed page size in
162 					bytes, or 0 */
163 	const ulint*	page_nos,	/*!< in: array of page numbers
164 					to read, with the highest page
165 					number the last in the
166 					array */
167 	ulint		n_stored);	/*!< in: number of page numbers
168 					in the array */
169 
170 /** The size in pages of the area which the read-ahead algorithms read if
171 invoked */
172 #define	BUF_READ_AHEAD_AREA(b)					\
173 	ut_min(64, ut_2_power_up((b)->curr_size / 32))
174 
175 /** @name Modes used in read-ahead @{ */
176 /** read only pages belonging to the insert buffer tree */
177 #define BUF_READ_IBUF_PAGES_ONLY	131
178 /** read any page */
179 #define BUF_READ_ANY_PAGE		132
180 /** read any page, but ignore (return an error) if a page does not exist
181 instead of crashing like BUF_READ_ANY_PAGE does */
182 #define BUF_READ_IGNORE_NONEXISTENT_PAGES 1024
183 /* @} */
184 
185 #endif
186