160
168
/********************************************************************//**
169
Applies a random read-ahead in buf_pool if there are at least a threshold
170
value of accessed pages from the random read-ahead area. Does not read any
171
page, not even the one at the position (space, offset), if the read-ahead
172
mechanism is not activated. NOTE 1: the calling thread may own latches on
173
pages: to avoid deadlocks this function must be written such that it cannot
174
end up waiting for these latches! NOTE 2: the calling thread must want
175
access to the page given: this rule is set to prevent unintended read-aheads
176
performed by ibuf routines, a situation which could result in a deadlock if
177
the OS does not support asynchronous i/o.
178
@return number of page read requests issued; NOTE that if we read ibuf
179
pages, it may happen that the page at the given page number does not
180
get read even if we return a positive value! */
183
buf_read_ahead_random(
184
/*==================*/
185
ulint space, /*!< in: space id */
186
ulint zip_size,/*!< in: compressed page size in bytes, or 0 */
187
ulint offset) /*!< in: page number of a page which the current thread
190
ib_int64_t tablespace_version;
191
ulint recent_blocks = 0;
197
ulint buf_read_ahead_random_area;
199
if (!srv_random_read_ahead) {
200
/* Disabled by user */
204
if (srv_startup_is_before_trx_rollback_phase) {
205
/* No read-ahead to avoid thread deadlocks */
209
if (ibuf_bitmap_page(zip_size, offset)
210
|| trx_sys_hdr_page(space, offset)) {
212
/* If it is an ibuf bitmap page or trx sys hdr, we do
213
no read-ahead, as that could break the ibuf page access
219
/* Remember the tablespace version before we ask the tablespace size
220
below: if DISCARD + IMPORT changes the actual .ibd file meanwhile, we
221
do not try to read outside the bounds of the tablespace! */
223
tablespace_version = fil_space_get_version(space);
225
buf_read_ahead_random_area = BUF_READ_AHEAD_RANDOM_AREA;
227
low = (offset / buf_read_ahead_random_area)
228
* buf_read_ahead_random_area;
229
high = (offset / buf_read_ahead_random_area + 1)
230
* buf_read_ahead_random_area;
231
if (high > fil_space_get_size(space)) {
233
high = fil_space_get_size(space);
236
buf_pool_mutex_enter();
238
if (buf_pool->n_pend_reads
239
> buf_pool->curr_size / BUF_READ_AHEAD_PEND_LIMIT) {
240
buf_pool_mutex_exit();
245
/* Count how many blocks in the area have been recently accessed,
246
that is, reside near the start of the LRU list. */
248
for (i = low; i < high; i++) {
249
const buf_page_t* bpage = buf_page_hash_get(space, i);
252
&& buf_page_is_accessed(bpage)
253
&& buf_page_peek_if_young(bpage)) {
257
if (recent_blocks >= BUF_READ_AHEAD_RANDOM_THRESHOLD) {
259
buf_pool_mutex_exit();
265
buf_pool_mutex_exit();
270
/* Read all the suitable blocks within the area */
273
ibuf_mode = BUF_READ_IBUF_PAGES_ONLY;
275
ibuf_mode = BUF_READ_ANY_PAGE;
280
for (i = low; i < high; i++) {
281
/* It is only sensible to do read-ahead in the non-sync aio
282
mode: hence FALSE as the first parameter */
284
if (!ibuf_bitmap_page(zip_size, i)) {
285
count += buf_read_page_low(
287
ibuf_mode | OS_AIO_SIMULATED_WAKE_LATER,
288
space, zip_size, FALSE,
289
tablespace_version, i);
290
if (err == DB_TABLESPACE_DELETED) {
291
ut_print_timestamp(stderr);
293
" InnoDB: Warning: in random"
294
" readahead trying to access\n"
295
"InnoDB: tablespace %lu page %lu,\n"
296
"InnoDB: but the tablespace does not"
297
" exist or is just being dropped.\n",
298
(ulong) space, (ulong) i);
303
/* In simulated aio we wake the aio handler threads only after
304
queuing all aio requests, in native aio the following call does
307
os_aio_simulated_wake_handler_threads();
310
if (buf_debug_prints && (count > 0)) {
312
"Random read-ahead space %lu offset %lu pages %lu\n",
313
(ulong) space, (ulong) offset,
316
#endif /* UNIV_DEBUG */
318
/* Read ahead is considered one I/O operation for the purpose of
319
LRU policy decision. */
320
buf_LRU_stat_inc_io();
322
buf_pool->stat.n_ra_pages_read_rnd += count;
327
/********************************************************************//**
161
328
High-level function which reads a page asynchronously from a file to the
162
329
buffer buf_pool if it is not already there. Sets the io_fix flag and sets
163
330
an exclusive lock on the buffer frame. The flag is cleared and the x-lock