~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/buf/buf0flu.cc

  • Committer: Olaf van der Spek
  • Date: 2011-02-12 18:24:24 UTC
  • mto: (2167.1.2 build) (2172.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2168.
  • Revision ID: olafvdspek@gmail.com-20110212182424-kgnm9osi7qo97at2
casts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
2
 
3
 
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
 
3
Copyright (C) 1995, 2010, Innobase Oy. All Rights Reserved.
4
4
 
5
5
This program is free software; you can redistribute it and/or modify it under
6
6
the terms of the GNU General Public License as published by the Free Software
11
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
12
 
13
13
You should have received a copy of the GNU General Public License along with
14
 
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
 
Place, Suite 330, Boston, MA 02111-1307 USA
 
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
15
St, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
*****************************************************************************/
18
18
 
83
83
@return TRUE if ok */
84
84
static
85
85
ibool
86
 
buf_flush_validate_low(void);
87
 
/*========================*/
88
 
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
 
86
buf_flush_validate_low(
 
87
/*===================*/
 
88
        buf_pool_t*     buf_pool);      /*!< in: Buffer pool instance */
 
89
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
 
90
 
 
91
/******************************************************************//**
 
92
Insert a block in the flush_rbt and returns a pointer to its
 
93
predecessor or NULL if no predecessor. The ordering is maintained
 
94
on the basis of the <oldest_modification, space, offset> key.
 
95
@return pointer to the predecessor or NULL if no predecessor. */
 
96
static
 
97
buf_page_t*
 
98
buf_flush_insert_in_flush_rbt(
 
99
/*==========================*/
 
100
        buf_page_t*     bpage)  /*!< in: bpage to be inserted. */
 
101
{
 
102
        const ib_rbt_node_t*    c_node;
 
103
        const ib_rbt_node_t*    p_node;
 
104
        buf_page_t*             prev = NULL;
 
105
        buf_pool_t*             buf_pool = buf_pool_from_bpage(bpage);
 
106
 
 
107
        ut_ad(buf_flush_list_mutex_own(buf_pool));
 
108
 
 
109
        /* Insert this buffer into the rbt. */
 
110
        c_node = rbt_insert(buf_pool->flush_rbt, &bpage, &bpage);
 
111
        ut_a(c_node != NULL);
 
112
 
 
113
        /* Get the predecessor. */
 
114
        p_node = rbt_prev(buf_pool->flush_rbt, c_node);
 
115
 
 
116
        if (p_node != NULL) {
 
117
                buf_page_t**    value;
 
118
                value = rbt_value(buf_page_t*, p_node);
 
119
                prev = *value;
 
120
                ut_a(prev != NULL);
 
121
        }
 
122
 
 
123
        return(prev);
 
124
}
 
125
 
 
126
/*********************************************************//**
 
127
Delete a bpage from the flush_rbt. */
 
128
static
 
129
void
 
130
buf_flush_delete_from_flush_rbt(
 
131
/*============================*/
 
132
        buf_page_t*     bpage)  /*!< in: bpage to be removed. */
 
133
{
 
134
#ifdef UNIV_DEBUG
 
135
        ibool           ret = FALSE;
 
136
#endif /* UNIV_DEBUG */
 
137
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
 
138
 
 
139
        ut_ad(buf_flush_list_mutex_own(buf_pool));
 
140
 
 
141
#ifdef UNIV_DEBUG
 
142
        ret =
 
143
#endif /* UNIV_DEBUG */
 
144
        rbt_delete(buf_pool->flush_rbt, &bpage);
 
145
        ut_ad(ret);
 
146
}
 
147
 
 
148
/*****************************************************************//**
 
149
Compare two modified blocks in the buffer pool. The key for comparison
 
150
is:
 
151
key = <oldest_modification, space, offset>
 
152
This comparison is used to maintian ordering of blocks in the
 
153
buf_pool->flush_rbt.
 
154
Note that for the purpose of flush_rbt, we only need to order blocks
 
155
on the oldest_modification. The other two fields are used to uniquely
 
156
identify the blocks.
 
157
@return  < 0 if b2 < b1, 0 if b2 == b1, > 0 if b2 > b1 */
 
158
static
 
159
int
 
160
buf_flush_block_cmp(
 
161
/*================*/
 
162
        const void*     p1,             /*!< in: block1 */
 
163
        const void*     p2)             /*!< in: block2 */
 
164
{
 
165
        int                     ret;
 
166
        const buf_page_t*       b1 = *(const buf_page_t**) p1;
 
167
        const buf_page_t*       b2 = *(const buf_page_t**) p2;
 
168
#ifdef UNIV_DEBUG
 
169
        buf_pool_t*             buf_pool = buf_pool_from_bpage(b1);
 
170
#endif /* UNIV_DEBUG */
 
171
 
 
172
        ut_ad(b1 != NULL);
 
173
        ut_ad(b2 != NULL);
 
174
 
 
175
        ut_ad(buf_flush_list_mutex_own(buf_pool));
 
176
 
 
177
        ut_ad(b1->in_flush_list);
 
178
        ut_ad(b2->in_flush_list);
 
179
 
 
180
        if (b2->oldest_modification > b1->oldest_modification) {
 
181
                return(1);
 
182
        } else if (b2->oldest_modification < b1->oldest_modification) {
 
183
                return(-1);
 
184
        }
 
185
 
 
186
        /* If oldest_modification is same then decide on the space. */
 
187
        ret = (int)(b2->space - b1->space);
 
188
 
 
189
        /* Or else decide ordering on the offset field. */
 
190
        return(ret ? ret : (int)(b2->offset - b1->offset));
 
191
}
 
192
 
 
193
/********************************************************************//**
 
194
Initialize the red-black tree to speed up insertions into the flush_list
 
195
during recovery process. Should be called at the start of recovery
 
196
process before any page has been read/written. */
 
197
UNIV_INTERN
 
198
void
 
199
buf_flush_init_flush_rbt(void)
 
200
/*==========================*/
 
201
{
 
202
        ulint   i;
 
203
 
 
204
        for (i = 0; i < srv_buf_pool_instances; i++) {
 
205
                buf_pool_t*     buf_pool;
 
206
 
 
207
                buf_pool = buf_pool_from_array(i);
 
208
 
 
209
                buf_flush_list_mutex_enter(buf_pool);
 
210
 
 
211
                /* Create red black tree for speedy insertions in flush list. */
 
212
                buf_pool->flush_rbt = rbt_create(
 
213
                        sizeof(buf_page_t*), buf_flush_block_cmp);
 
214
 
 
215
                buf_flush_list_mutex_exit(buf_pool);
 
216
        }
 
217
}
 
218
 
 
219
/********************************************************************//**
 
220
Frees up the red-black tree. */
 
221
UNIV_INTERN
 
222
void
 
223
buf_flush_free_flush_rbt(void)
 
224
/*==========================*/
 
225
{
 
226
        ulint   i;
 
227
 
 
228
        for (i = 0; i < srv_buf_pool_instances; i++) {
 
229
                buf_pool_t*     buf_pool;
 
230
 
 
231
                buf_pool = buf_pool_from_array(i);
 
232
 
 
233
                buf_flush_list_mutex_enter(buf_pool);
 
234
 
 
235
#ifdef UNIV_DEBUG_VALGRIND
 
236
        {
 
237
                ulint   zip_size = buf_block_get_zip_size(block);
 
238
 
 
239
                if (UNIV_UNLIKELY(zip_size)) {
 
240
                        UNIV_MEM_ASSERT_RW(block->page.zip.data, zip_size);
 
241
                } else {
 
242
                        UNIV_MEM_ASSERT_RW(block->frame, UNIV_PAGE_SIZE);
 
243
                }
 
244
        }
 
245
#endif /* UNIV_DEBUG_VALGRIND */
 
246
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
 
247
                ut_a(buf_flush_validate_low(buf_pool));
 
248
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
 
249
 
 
250
                rbt_free(buf_pool->flush_rbt);
 
251
                buf_pool->flush_rbt = NULL;
 
252
 
 
253
                buf_flush_list_mutex_exit(buf_pool);
 
254
        }
 
255
}
89
256
 
90
257
/********************************************************************//**
91
258
Inserts a modified block into the flush list. */
93
260
void
94
261
buf_flush_insert_into_flush_list(
95
262
/*=============================*/
96
 
        buf_block_t*    block)  /*!< in/out: block which is modified */
 
263
        buf_pool_t*     buf_pool,       /*!< buffer pool instance */
 
264
        buf_block_t*    block,          /*!< in/out: block which is modified */
 
265
        ib_uint64_t     lsn)            /*!< in: oldest modification */
97
266
{
98
 
        ut_ad(buf_pool_mutex_own());
 
267
        ut_ad(!buf_pool_mutex_own(buf_pool));
 
268
        ut_ad(log_flush_order_mutex_own());
 
269
        ut_ad(mutex_own(&block->mutex));
 
270
 
 
271
        buf_flush_list_mutex_enter(buf_pool);
 
272
 
99
273
        ut_ad((UT_LIST_GET_FIRST(buf_pool->flush_list) == NULL)
100
274
              || (UT_LIST_GET_FIRST(buf_pool->flush_list)->oldest_modification
101
 
                  <= block->page.oldest_modification));
 
275
                  <= lsn));
 
276
 
 
277
        /* If we are in the recovery then we need to update the flush
 
278
        red-black tree as well. */
 
279
        if (UNIV_LIKELY_NULL(buf_pool->flush_rbt)) {
 
280
                buf_flush_list_mutex_exit(buf_pool);
 
281
                buf_flush_insert_sorted_into_flush_list(buf_pool, block, lsn);
 
282
                return;
 
283
        }
102
284
 
103
285
        ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
104
 
        ut_ad(block->page.in_LRU_list);
105
 
        ut_ad(block->page.in_page_hash);
106
 
        ut_ad(!block->page.in_zip_hash);
107
286
        ut_ad(!block->page.in_flush_list);
 
287
 
108
288
        ut_d(block->page.in_flush_list = TRUE);
 
289
        block->page.oldest_modification = lsn;
109
290
        UT_LIST_ADD_FIRST(list, buf_pool->flush_list, &block->page);
110
291
 
 
292
#ifdef UNIV_DEBUG_VALGRIND
 
293
        {
 
294
                ulint   zip_size = buf_block_get_zip_size(block);
 
295
 
 
296
                if (UNIV_UNLIKELY(zip_size)) {
 
297
                        UNIV_MEM_ASSERT_RW(block->page.zip.data, zip_size);
 
298
                } else {
 
299
                        UNIV_MEM_ASSERT_RW(block->frame, UNIV_PAGE_SIZE);
 
300
                }
 
301
        }
 
302
#endif /* UNIV_DEBUG_VALGRIND */
111
303
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
112
 
        ut_a(buf_flush_validate_low());
 
304
        ut_a(buf_flush_validate_low(buf_pool));
113
305
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
 
306
 
 
307
        buf_flush_list_mutex_exit(buf_pool);
114
308
}
115
309
 
116
310
/********************************************************************//**
121
315
void
122
316
buf_flush_insert_sorted_into_flush_list(
123
317
/*====================================*/
124
 
        buf_block_t*    block)  /*!< in/out: block which is modified */
 
318
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
 
319
        buf_block_t*    block,          /*!< in/out: block which is modified */
 
320
        ib_uint64_t     lsn)            /*!< in: oldest modification */
125
321
{
126
322
        buf_page_t*     prev_b;
127
323
        buf_page_t*     b;
128
324
 
129
 
        ut_ad(buf_pool_mutex_own());
 
325
        ut_ad(!buf_pool_mutex_own(buf_pool));
 
326
        ut_ad(log_flush_order_mutex_own());
 
327
        ut_ad(mutex_own(&block->mutex));
130
328
        ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
131
329
 
 
330
        buf_flush_list_mutex_enter(buf_pool);
 
331
 
 
332
        /* The field in_LRU_list is protected by buf_pool->mutex, which
 
333
        we are not holding.  However, while a block is in the flush
 
334
        list, it is dirty and cannot be discarded, not from the
 
335
        page_hash or from the LRU list.  At most, the uncompressed
 
336
        page frame of a compressed block may be discarded or created
 
337
        (copying the block->page to or from a buf_page_t that is
 
338
        dynamically allocated from buf_buddy_alloc()).  Because those
 
339
        transitions hold block->mutex and the flush list mutex (via
 
340
        buf_flush_relocate_on_flush_list()), there is no possibility
 
341
        of a race condition in the assertions below. */
132
342
        ut_ad(block->page.in_LRU_list);
133
343
        ut_ad(block->page.in_page_hash);
 
344
        /* buf_buddy_block_register() will take a block in the
 
345
        BUF_BLOCK_MEMORY state, not a file page. */
134
346
        ut_ad(!block->page.in_zip_hash);
 
347
 
135
348
        ut_ad(!block->page.in_flush_list);
136
349
        ut_d(block->page.in_flush_list = TRUE);
 
350
        block->page.oldest_modification = lsn;
 
351
 
 
352
#ifdef UNIV_DEBUG_VALGRIND
 
353
        {
 
354
                ulint   zip_size = buf_block_get_zip_size(block);
 
355
 
 
356
                if (UNIV_UNLIKELY(zip_size)) {
 
357
                        UNIV_MEM_ASSERT_RW(block->page.zip.data, zip_size);
 
358
                } else {
 
359
                        UNIV_MEM_ASSERT_RW(block->frame, UNIV_PAGE_SIZE);
 
360
                }
 
361
        }
 
362
#endif /* UNIV_DEBUG_VALGRIND */
 
363
 
 
364
#ifdef UNIV_DEBUG_VALGRIND
 
365
        {
 
366
                ulint   zip_size = buf_block_get_zip_size(block);
 
367
 
 
368
                if (UNIV_UNLIKELY(zip_size)) {
 
369
                        UNIV_MEM_ASSERT_RW(block->page.zip.data, zip_size);
 
370
                } else {
 
371
                        UNIV_MEM_ASSERT_RW(block->frame, UNIV_PAGE_SIZE);
 
372
                }
 
373
        }
 
374
#endif /* UNIV_DEBUG_VALGRIND */
137
375
 
138
376
        prev_b = NULL;
139
 
        b = UT_LIST_GET_FIRST(buf_pool->flush_list);
140
 
 
141
 
        while (b && b->oldest_modification > block->page.oldest_modification) {
142
 
                ut_ad(b->in_flush_list);
143
 
                prev_b = b;
144
 
                b = UT_LIST_GET_NEXT(list, b);
 
377
 
 
378
        /* For the most part when this function is called the flush_rbt
 
379
        should not be NULL. In a very rare boundary case it is possible
 
380
        that the flush_rbt has already been freed by the recovery thread
 
381
        before the last page was hooked up in the flush_list by the
 
382
        io-handler thread. In that case we'll  just do a simple
 
383
        linear search in the else block. */
 
384
        if (buf_pool->flush_rbt) {
 
385
 
 
386
                prev_b = buf_flush_insert_in_flush_rbt(&block->page);
 
387
 
 
388
        } else {
 
389
 
 
390
                b = UT_LIST_GET_FIRST(buf_pool->flush_list);
 
391
 
 
392
                while (b && b->oldest_modification
 
393
                       > block->page.oldest_modification) {
 
394
                        ut_ad(b->in_flush_list);
 
395
                        prev_b = b;
 
396
                        b = UT_LIST_GET_NEXT(list, b);
 
397
                }
145
398
        }
146
399
 
147
400
        if (prev_b == NULL) {
152
405
        }
153
406
 
154
407
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
155
 
        ut_a(buf_flush_validate_low());
 
408
        ut_a(buf_flush_validate_low(buf_pool));
156
409
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
 
410
 
 
411
        buf_flush_list_mutex_exit(buf_pool);
157
412
}
158
413
 
159
414
/********************************************************************//**
167
422
        buf_page_t*     bpage)  /*!< in: buffer control block, must be
168
423
                                buf_page_in_file(bpage) and in the LRU list */
169
424
{
170
 
        ut_ad(buf_pool_mutex_own());
 
425
#ifdef UNIV_DEBUG
 
426
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
 
427
        ut_ad(buf_pool_mutex_own(buf_pool));
 
428
#endif
171
429
        ut_ad(mutex_own(buf_page_get_mutex(bpage)));
172
430
        ut_ad(bpage->in_LRU_list);
173
431
 
200
458
                                buf_page_in_file(bpage) */
201
459
        enum buf_flush  flush_type)/*!< in: BUF_FLUSH_LRU or BUF_FLUSH_LIST */
202
460
{
 
461
#ifdef UNIV_DEBUG
 
462
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
 
463
        ut_ad(buf_pool_mutex_own(buf_pool));
 
464
#endif
203
465
        ut_a(buf_page_in_file(bpage));
204
 
        ut_ad(buf_pool_mutex_own());
205
466
        ut_ad(mutex_own(buf_page_get_mutex(bpage)));
206
467
        ut_ad(flush_type == BUF_FLUSH_LRU || BUF_FLUSH_LIST);
207
468
 
234
495
/*=============*/
235
496
        buf_page_t*     bpage)  /*!< in: pointer to the block in question */
236
497
{
237
 
        ut_ad(buf_pool_mutex_own());
 
498
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
 
499
 
 
500
        ut_ad(buf_pool_mutex_own(buf_pool));
238
501
        ut_ad(mutex_own(buf_page_get_mutex(bpage)));
239
502
        ut_ad(bpage->in_flush_list);
240
 
        ut_d(bpage->in_flush_list = FALSE);
 
503
 
 
504
        buf_flush_list_mutex_enter(buf_pool);
241
505
 
242
506
        switch (buf_page_get_state(bpage)) {
243
507
        case BUF_BLOCK_ZIP_PAGE:
244
 
                /* clean compressed pages should not be on the flush list */
 
508
                /* Clean compressed pages should not be on the flush list */
245
509
        case BUF_BLOCK_ZIP_FREE:
246
510
        case BUF_BLOCK_NOT_USED:
247
511
        case BUF_BLOCK_READY_FOR_USE:
259
523
                break;
260
524
        }
261
525
 
 
526
        /* If the flush_rbt is active then delete from there as well. */
 
527
        if (UNIV_LIKELY_NULL(buf_pool->flush_rbt)) {
 
528
                buf_flush_delete_from_flush_rbt(bpage);
 
529
        }
 
530
 
 
531
        /* Must be done after we have removed it from the flush_rbt
 
532
        because we assert on in_flush_list in comparison function. */
 
533
        ut_d(bpage->in_flush_list = FALSE);
 
534
 
262
535
        bpage->oldest_modification = 0;
263
536
 
264
 
        ut_d(UT_LIST_VALIDATE(list, buf_page_t, buf_pool->flush_list,
265
 
                              ut_ad(ut_list_node_313->in_flush_list)));
 
537
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
 
538
        ut_a(buf_flush_validate_low(buf_pool));
 
539
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
 
540
 
 
541
        buf_flush_list_mutex_exit(buf_pool);
 
542
}
 
543
 
 
544
/*******************************************************************//**
 
545
Relocates a buffer control block on the flush_list.
 
546
Note that it is assumed that the contents of bpage have already been
 
547
copied to dpage.
 
548
IMPORTANT: When this function is called bpage and dpage are not
 
549
exact copies of each other. For example, they both will have different
 
550
::state. Also the ::list pointers in dpage may be stale. We need to
 
551
use the current list node (bpage) to do the list manipulation because
 
552
the list pointers could have changed between the time that we copied
 
553
the contents of bpage to the dpage and the flush list manipulation
 
554
below. */
 
555
UNIV_INTERN
 
556
void
 
557
buf_flush_relocate_on_flush_list(
 
558
/*=============================*/
 
559
        buf_page_t*     bpage,  /*!< in/out: control block being moved */
 
560
        buf_page_t*     dpage)  /*!< in/out: destination block */
 
561
{
 
562
        buf_page_t*     prev;
 
563
        buf_page_t*     prev_b = NULL;
 
564
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
 
565
 
 
566
        ut_ad(buf_pool_mutex_own(buf_pool));
 
567
        /* Must reside in the same buffer pool. */
 
568
        ut_ad(buf_pool == buf_pool_from_bpage(dpage));
 
569
 
 
570
        ut_ad(mutex_own(buf_page_get_mutex(bpage)));
 
571
 
 
572
        buf_flush_list_mutex_enter(buf_pool);
 
573
 
 
574
        /* FIXME: At this point we have both buf_pool and flush_list
 
575
        mutexes. Theoretically removal of a block from flush list is
 
576
        only covered by flush_list mutex but currently we do
 
577
        have buf_pool mutex in buf_flush_remove() therefore this block
 
578
        is guaranteed to be in the flush list. We need to check if
 
579
        this will work without the assumption of block removing code
 
580
        having the buf_pool mutex. */
 
581
        ut_ad(bpage->in_flush_list);
 
582
        ut_ad(dpage->in_flush_list);
 
583
 
 
584
        /* If recovery is active we must swap the control blocks in
 
585
        the flush_rbt as well. */
 
586
        if (UNIV_LIKELY_NULL(buf_pool->flush_rbt)) {
 
587
                buf_flush_delete_from_flush_rbt(bpage);
 
588
                prev_b = buf_flush_insert_in_flush_rbt(dpage);
 
589
        }
 
590
 
 
591
        /* Must be done after we have removed it from the flush_rbt
 
592
        because we assert on in_flush_list in comparison function. */
 
593
        ut_d(bpage->in_flush_list = FALSE);
 
594
 
 
595
        prev = UT_LIST_GET_PREV(list, bpage);
 
596
        UT_LIST_REMOVE(list, buf_pool->flush_list, bpage);
 
597
 
 
598
        if (prev) {
 
599
                ut_ad(prev->in_flush_list);
 
600
                UT_LIST_INSERT_AFTER(
 
601
                        list,
 
602
                        buf_pool->flush_list,
 
603
                        prev, dpage);
 
604
        } else {
 
605
                UT_LIST_ADD_FIRST(
 
606
                        list,
 
607
                        buf_pool->flush_list,
 
608
                        dpage);
 
609
        }
 
610
 
 
611
        /* Just an extra check. Previous in flush_list
 
612
        should be the same control block as in flush_rbt. */
 
613
        ut_a(!buf_pool->flush_rbt || prev_b == prev);
 
614
 
 
615
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
 
616
        ut_a(buf_flush_validate_low(buf_pool));
 
617
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
 
618
 
 
619
        buf_flush_list_mutex_exit(buf_pool);
266
620
}
267
621
 
268
622
/********************************************************************//**
274
628
        buf_page_t*     bpage)  /*!< in: pointer to the block in question */
275
629
{
276
630
        enum buf_flush  flush_type;
 
631
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
277
632
 
278
633
        ut_ad(bpage);
279
634
 
294
649
        /* fprintf(stderr, "n pending flush %lu\n",
295
650
        buf_pool->n_flush[flush_type]); */
296
651
 
297
 
        if ((buf_pool->n_flush[flush_type] == 0)
298
 
            && (buf_pool->init_flush[flush_type] == FALSE)) {
 
652
        if (buf_pool->n_flush[flush_type] == 0
 
653
            && buf_pool->init_flush[flush_type] == FALSE) {
299
654
 
300
655
                /* The running flush batch has ended */
301
656
 
304
659
}
305
660
 
306
661
/********************************************************************//**
 
662
Flush a batch of writes to the datafiles that have already been
 
663
written by the OS. */
 
664
static
 
665
void
 
666
buf_flush_sync_datafiles(void)
 
667
/*==========================*/
 
668
{
 
669
        /* Wake possible simulated aio thread to actually post the
 
670
        writes to the operating system */
 
671
        os_aio_simulated_wake_handler_threads();
 
672
 
 
673
        /* Wait that all async writes to tablespaces have been posted to
 
674
        the OS */
 
675
        os_aio_wait_until_no_pending_writes();
 
676
 
 
677
        /* Now we flush the data to disk (for example, with fsync) */
 
678
        fil_flush_file_spaces(FIL_TABLESPACE);
 
679
 
 
680
        return;
 
681
}
 
682
 
 
683
/********************************************************************//**
307
684
Flushes possible buffered writes from the doublewrite memory buffer to disk,
308
685
and also wakes up the aio thread if simulated aio is used. It is very
309
686
important to call this function after a batch of writes has been posted,
320
697
        ulint           i;
321
698
 
322
699
        if (!srv_use_doublewrite_buf || trx_doublewrite == NULL) {
323
 
                os_aio_simulated_wake_handler_threads();
324
 
 
 
700
                /* Sync the writes to the disk. */
 
701
                buf_flush_sync_datafiles();
325
702
                return;
326
703
        }
327
704
 
529
906
                buf_LRU_stat_inc_io();
530
907
        }
531
908
 
532
 
        /* Wake possible simulated aio thread to actually post the
533
 
        writes to the operating system */
534
 
 
535
 
        os_aio_simulated_wake_handler_threads();
536
 
 
537
 
        /* Wait that all async writes to tablespaces have been posted to
538
 
        the OS */
539
 
 
540
 
        os_aio_wait_until_no_pending_writes();
541
 
 
542
 
        /* Now we flush the data to disk (for example, with fsync) */
543
 
 
544
 
        fil_flush_file_spaces(FIL_TABLESPACE);
 
909
        /* Sync the writes to the disk. */
 
910
        buf_flush_sync_datafiles();
545
911
 
546
912
        /* We can now reuse the doublewrite memory buffer: */
547
 
 
548
913
        trx_doublewrite->first_free = 0;
549
914
 
550
915
        mutex_exit(&(trx_doublewrite->mutex));
578
943
        zip_size = buf_page_get_zip_size(bpage);
579
944
 
580
945
        if (UNIV_UNLIKELY(zip_size)) {
 
946
                UNIV_MEM_ASSERT_RW(bpage->zip.data, zip_size);
581
947
                /* Copy the compressed page and clear the rest. */
582
948
                memcpy(trx_doublewrite->write_buf
583
949
                       + UNIV_PAGE_SIZE * trx_doublewrite->first_free,
587
953
                       + zip_size, 0, UNIV_PAGE_SIZE - zip_size);
588
954
        } else {
589
955
                ut_a(buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE);
 
956
                UNIV_MEM_ASSERT_RW(((buf_block_t*) bpage)->frame,
 
957
                                   UNIV_PAGE_SIZE);
590
958
 
591
959
                memcpy(trx_doublewrite->write_buf
592
960
                       + UNIV_PAGE_SIZE * trx_doublewrite->first_free,
624
992
        ut_ad(page);
625
993
 
626
994
        if (page_zip_) {
627
 
                page_zip_des_t* page_zip = page_zip_;
 
995
                page_zip_des_t* page_zip = static_cast<page_zip_des_t *>(page_zip_);
628
996
                ulint           zip_size = page_zip_get_size(page_zip);
629
997
                ut_ad(zip_size);
630
998
                ut_ad(ut_is_2pow(zip_size));
642
1010
                case FIL_PAGE_TYPE_ZBLOB:
643
1011
                case FIL_PAGE_TYPE_ZBLOB2:
644
1012
                case FIL_PAGE_INDEX:
645
 
                        mach_write_ull(page_zip->data
646
 
                                       + FIL_PAGE_LSN, newest_lsn);
 
1013
                        mach_write_to_8(page_zip->data
 
1014
                                        + FIL_PAGE_LSN, newest_lsn);
647
1015
                        memset(page_zip->data + FIL_PAGE_FILE_FLUSH_LSN, 0, 8);
648
1016
                        mach_write_to_4(page_zip->data
649
1017
                                        + FIL_PAGE_SPACE_OR_CHKSUM,
665
1033
        }
666
1034
 
667
1035
        /* Write the newest modification lsn to the page header and trailer */
668
 
        mach_write_ull(page + FIL_PAGE_LSN, newest_lsn);
 
1036
        mach_write_to_8(page + FIL_PAGE_LSN, newest_lsn);
669
1037
 
670
 
        mach_write_ull(page + UNIV_PAGE_SIZE - FIL_PAGE_END_LSN_OLD_CHKSUM,
671
 
                       newest_lsn);
 
1038
        mach_write_to_8(page + UNIV_PAGE_SIZE - FIL_PAGE_END_LSN_OLD_CHKSUM,
 
1039
                        newest_lsn);
672
1040
 
673
1041
        /* Store the new formula checksum */
674
1042
 
701
1069
{
702
1070
        ulint   zip_size        = buf_page_get_zip_size(bpage);
703
1071
        page_t* frame           = NULL;
 
1072
 
 
1073
#ifdef UNIV_DEBUG
 
1074
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
 
1075
        ut_ad(!buf_pool_mutex_own(buf_pool));
 
1076
#endif
 
1077
 
704
1078
#ifdef UNIV_LOG_DEBUG
705
1079
        static ibool univ_log_debug_warned;
706
1080
#endif /* UNIV_LOG_DEBUG */
707
1081
 
708
1082
        ut_ad(buf_page_in_file(bpage));
709
1083
 
710
 
        /* We are not holding buf_pool_mutex or block_mutex here.
 
1084
        /* We are not holding buf_pool->mutex or block_mutex here.
711
1085
        Nevertheless, it is safe to access bpage, because it is
712
1086
        io_fixed and oldest_modification != 0.  Thus, it cannot be
713
1087
        relocated in the buffer pool or removed from flush_list or
714
1088
        LRU_list. */
715
 
        ut_ad(!buf_pool_mutex_own());
 
1089
        ut_ad(!buf_pool_mutex_own(buf_pool));
 
1090
        ut_ad(!buf_flush_list_mutex_own(buf_pool));
716
1091
        ut_ad(!mutex_own(buf_page_get_mutex(bpage)));
717
1092
        ut_ad(buf_page_get_io_fix(bpage) == BUF_IO_WRITE);
718
1093
        ut_ad(bpage->oldest_modification != 0);
749
1124
                        ut_a(mach_read_from_4(frame + FIL_PAGE_SPACE_OR_CHKSUM)
750
1125
                             == page_zip_calc_checksum(frame, zip_size));
751
1126
                }
752
 
                mach_write_ull(frame + FIL_PAGE_LSN,
753
 
                               bpage->newest_modification);
 
1127
                mach_write_to_8(frame + FIL_PAGE_LSN,
 
1128
                                bpage->newest_modification);
754
1129
                memset(frame + FIL_PAGE_FILE_FLUSH_LSN, 0, 8);
755
1130
                break;
756
1131
        case BUF_BLOCK_FILE_PAGE:
777
1152
        }
778
1153
}
779
1154
 
 
1155
# if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG
 
1156
/********************************************************************//**
 
1157
Writes a flushable page asynchronously from the buffer pool to a file.
 
1158
NOTE: buf_pool->mutex and block->mutex must be held upon entering this
 
1159
function, and they will be released by this function after flushing.
 
1160
This is loosely based on buf_flush_batch() and buf_flush_page().
 
1161
@return TRUE if the page was flushed and the mutexes released */
 
1162
UNIV_INTERN
 
1163
ibool
 
1164
buf_flush_page_try(
 
1165
/*===============*/
 
1166
        buf_pool_t*     buf_pool,       /*!< in/out: buffer pool instance */
 
1167
        buf_block_t*    block)          /*!< in/out: buffer control block */
 
1168
{
 
1169
        ut_ad(buf_pool_mutex_own(buf_pool));
 
1170
        ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
 
1171
        ut_ad(mutex_own(&block->mutex));
 
1172
 
 
1173
        if (!buf_flush_ready_for_flush(&block->page, BUF_FLUSH_LRU)) {
 
1174
                return(FALSE);
 
1175
        }
 
1176
 
 
1177
        if (buf_pool->n_flush[BUF_FLUSH_LRU] > 0
 
1178
            || buf_pool->init_flush[BUF_FLUSH_LRU]) {
 
1179
                /* There is already a flush batch of the same type running */
 
1180
                return(FALSE);
 
1181
        }
 
1182
 
 
1183
        buf_pool->init_flush[BUF_FLUSH_LRU] = TRUE;
 
1184
 
 
1185
        buf_page_set_io_fix(&block->page, BUF_IO_WRITE);
 
1186
 
 
1187
        buf_page_set_flush_type(&block->page, BUF_FLUSH_LRU);
 
1188
 
 
1189
        if (buf_pool->n_flush[BUF_FLUSH_LRU]++ == 0) {
 
1190
 
 
1191
                os_event_reset(buf_pool->no_flush[BUF_FLUSH_LRU]);
 
1192
        }
 
1193
 
 
1194
        /* VERY IMPORTANT:
 
1195
        Because any thread may call the LRU flush, even when owning
 
1196
        locks on pages, to avoid deadlocks, we must make sure that the
 
1197
        s-lock is acquired on the page without waiting: this is
 
1198
        accomplished because buf_flush_ready_for_flush() must hold,
 
1199
        and that requires the page not to be bufferfixed. */
 
1200
 
 
1201
        rw_lock_s_lock_gen(&block->lock, BUF_IO_WRITE);
 
1202
 
 
1203
        /* Note that the s-latch is acquired before releasing the
 
1204
        buf_pool mutex: this ensures that the latch is acquired
 
1205
        immediately. */
 
1206
 
 
1207
        mutex_exit(&block->mutex);
 
1208
        buf_pool_mutex_exit(buf_pool);
 
1209
 
 
1210
        /* Even though block is not protected by any mutex at this
 
1211
        point, it is safe to access block, because it is io_fixed and
 
1212
        oldest_modification != 0.  Thus, it cannot be relocated in the
 
1213
        buffer pool or removed from flush_list or LRU_list. */
 
1214
 
 
1215
        buf_flush_write_block_low(&block->page);
 
1216
 
 
1217
        buf_pool_mutex_enter(buf_pool);
 
1218
        buf_pool->init_flush[BUF_FLUSH_LRU] = FALSE;
 
1219
 
 
1220
        if (buf_pool->n_flush[BUF_FLUSH_LRU] == 0) {
 
1221
                /* The running flush batch has ended */
 
1222
                os_event_set(buf_pool->no_flush[BUF_FLUSH_LRU]);
 
1223
        }
 
1224
 
 
1225
        buf_pool_mutex_exit(buf_pool);
 
1226
        buf_flush_buffered_writes();
 
1227
 
 
1228
        return(TRUE);
 
1229
}
 
1230
# endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */
 
1231
 
780
1232
/********************************************************************//**
781
1233
Writes a flushable page asynchronously from the buffer pool to a file.
782
1234
NOTE: in simulated aio we must call
783
1235
os_aio_simulated_wake_handler_threads after we have posted a batch of
784
 
writes! NOTE: buf_pool_mutex and buf_page_get_mutex(bpage) must be
 
1236
writes! NOTE: buf_pool->mutex and buf_page_get_mutex(bpage) must be
785
1237
held upon entering this function, and they will be released by this
786
1238
function. */
787
1239
static
788
1240
void
789
1241
buf_flush_page(
790
1242
/*===========*/
 
1243
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
791
1244
        buf_page_t*     bpage,          /*!< in: buffer control block */
792
1245
        enum buf_flush  flush_type)     /*!< in: BUF_FLUSH_LRU
793
1246
                                        or BUF_FLUSH_LIST */
796
1249
        ibool           is_uncompressed;
797
1250
 
798
1251
        ut_ad(flush_type == BUF_FLUSH_LRU || flush_type == BUF_FLUSH_LIST);
799
 
        ut_ad(buf_pool_mutex_own());
 
1252
        ut_ad(buf_pool_mutex_own(buf_pool));
800
1253
        ut_ad(buf_page_in_file(bpage));
801
1254
 
802
1255
        block_mutex = buf_page_get_mutex(bpage);
816
1269
        buf_pool->n_flush[flush_type]++;
817
1270
 
818
1271
        is_uncompressed = (buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE);
819
 
        ut_ad(is_uncompressed == (block_mutex != &buf_pool_zip_mutex));
 
1272
        ut_ad(is_uncompressed == (block_mutex != &buf_pool->zip_mutex));
820
1273
 
821
1274
        switch (flush_type) {
822
1275
                ibool   is_s_latched;
832
1285
                }
833
1286
 
834
1287
                mutex_exit(block_mutex);
835
 
                buf_pool_mutex_exit();
 
1288
                buf_pool_mutex_exit(buf_pool);
836
1289
 
837
1290
                /* Even though bpage is not protected by any mutex at
838
1291
                this point, it is safe to access bpage, because it is
869
1322
                immediately. */
870
1323
 
871
1324
                mutex_exit(block_mutex);
872
 
                buf_pool_mutex_exit();
 
1325
                buf_pool_mutex_exit(buf_pool);
873
1326
                break;
874
1327
 
875
1328
        default:
900
1353
/*====================*/
901
1354
        ulint           space,          /*!< in: space id */
902
1355
        ulint           offset,         /*!< in: page offset */
903
 
        enum buf_flush  flush_type)     /*!< in: BUF_FLUSH_LRU or
 
1356
        enum buf_flush  flush_type,     /*!< in: BUF_FLUSH_LRU or
904
1357
                                        BUF_FLUSH_LIST */
 
1358
        ulint           n_flushed,      /*!< in: number of pages
 
1359
                                        flushed so far in this batch */
 
1360
        ulint           n_to_flush)     /*!< in: maximum number of pages
 
1361
                                        we are allowed to flush */
905
1362
{
906
 
        buf_page_t*     bpage;
907
 
        ulint           low, high;
908
 
        ulint           count           = 0;
909
1363
        ulint           i;
 
1364
        ulint           low;
 
1365
        ulint           high;
 
1366
        ulint           count = 0;
 
1367
        buf_pool_t*     buf_pool = buf_pool_get(space, offset);
910
1368
 
911
1369
        ut_ad(flush_type == BUF_FLUSH_LRU || flush_type == BUF_FLUSH_LIST);
912
1370
 
913
1371
        if (UT_LIST_GET_LEN(buf_pool->LRU) < BUF_LRU_OLD_MIN_LEN) {
914
 
                /* If there is little space, it is better not to flush any
915
 
                block except from the end of the LRU list */
 
1372
                /* If there is little space, it is better not to flush
 
1373
                any block except from the end of the LRU list */
916
1374
 
917
1375
                low = offset;
918
1376
                high = offset + 1;
919
1377
        } else {
920
 
                /* When flushed, dirty blocks are searched in neighborhoods of
921
 
                this size, and flushed along with the original page. */
 
1378
                /* When flushed, dirty blocks are searched in
 
1379
                neighborhoods of this size, and flushed along with the
 
1380
                original page. */
922
1381
 
923
 
                ulint   buf_flush_area  = ut_min(BUF_READ_AHEAD_AREA,
924
 
                                                 buf_pool->curr_size / 16);
 
1382
                ulint   buf_flush_area;
 
1383
        
 
1384
                buf_flush_area  = ut_min(
 
1385
                        BUF_READ_AHEAD_AREA(buf_pool),
 
1386
                        buf_pool->curr_size / 16);
925
1387
 
926
1388
                low = (offset / buf_flush_area) * buf_flush_area;
927
1389
                high = (offset / buf_flush_area + 1) * buf_flush_area;
933
1395
                high = fil_space_get_size(space);
934
1396
        }
935
1397
 
936
 
        buf_pool_mutex_enter();
937
 
 
938
1398
        for (i = low; i < high; i++) {
939
1399
 
940
 
                bpage = buf_page_hash_get(space, i);
 
1400
                buf_page_t*     bpage;
 
1401
 
 
1402
                if ((count + n_flushed) >= n_to_flush) {
 
1403
 
 
1404
                        /* We have already flushed enough pages and
 
1405
                        should call it a day. There is, however, one
 
1406
                        exception. If the page whose neighbors we
 
1407
                        are flushing has not been flushed yet then
 
1408
                        we'll try to flush the victim that we
 
1409
                        selected originally. */
 
1410
                        if (i <= offset) {
 
1411
                                i = offset;
 
1412
                        } else {
 
1413
                                break;
 
1414
                        }
 
1415
                }
 
1416
 
 
1417
                buf_pool = buf_pool_get(space, i);
 
1418
 
 
1419
                buf_pool_mutex_enter(buf_pool);
 
1420
 
 
1421
                /* We only want to flush pages from this buffer pool. */
 
1422
                bpage = buf_page_hash_get(buf_pool, space, i);
941
1423
 
942
1424
                if (!bpage) {
943
1425
 
 
1426
                        buf_pool_mutex_exit(buf_pool);
944
1427
                        continue;
945
1428
                }
946
1429
 
959
1442
                        if (buf_flush_ready_for_flush(bpage, flush_type)
960
1443
                            && (i == offset || !bpage->buf_fix_count)) {
961
1444
                                /* We only try to flush those
962
 
                                neighbors != offset where the buf fix count is
963
 
                                zero, as we then know that we probably can
964
 
                                latch the page without a semaphore wait.
965
 
                                Semaphore waits are expensive because we must
966
 
                                flush the doublewrite buffer before we start
 
1445
                                neighbors != offset where the buf fix
 
1446
                                count is zero, as we then know that we
 
1447
                                probably can latch the page without a
 
1448
                                semaphore wait. Semaphore waits are
 
1449
                                expensive because we must flush the
 
1450
                                doublewrite buffer before we start
967
1451
                                waiting. */
968
1452
 
969
 
                                buf_flush_page(bpage, flush_type);
 
1453
                                buf_flush_page(buf_pool, bpage, flush_type);
970
1454
                                ut_ad(!mutex_own(block_mutex));
 
1455
                                ut_ad(!buf_pool_mutex_own(buf_pool));
971
1456
                                count++;
972
 
 
973
 
                                buf_pool_mutex_enter();
 
1457
                                continue;
974
1458
                        } else {
975
1459
                                mutex_exit(block_mutex);
976
1460
                        }
977
1461
                }
978
 
        }
979
 
 
980
 
        buf_pool_mutex_exit();
 
1462
                buf_pool_mutex_exit(buf_pool);
 
1463
        }
 
1464
 
 
1465
        return(count);
 
1466
}
 
1467
 
 
1468
/********************************************************************//**
 
1469
Check if the block is modified and ready for flushing. If the the block
 
1470
is ready to flush then flush the page and try o flush its neighbors.
 
1471
 
 
1472
@return TRUE if buf_pool mutex was not released during this function.
 
1473
This does not guarantee that some pages were written as well.
 
1474
Number of pages written are incremented to the count. */
 
1475
static
 
1476
ibool
 
1477
buf_flush_page_and_try_neighbors(
 
1478
/*=============================*/
 
1479
        buf_page_t*     bpage,          /*!< in: buffer control block,
 
1480
                                        must be
 
1481
                                        buf_page_in_file(bpage) */
 
1482
        enum buf_flush  flush_type,     /*!< in: BUF_FLUSH_LRU
 
1483
                                        or BUF_FLUSH_LIST */
 
1484
        ulint           n_to_flush,     /*!< in: number of pages to
 
1485
                                        flush */
 
1486
        ulint*          count)          /*!< in/out: number of pages
 
1487
                                        flushed */
 
1488
{
 
1489
        mutex_t*        block_mutex;
 
1490
        ibool           flushed = FALSE;
 
1491
#ifdef UNIV_DEBUG
 
1492
        buf_pool_t*     buf_pool = buf_pool_from_bpage(bpage);
 
1493
#endif /* UNIV_DEBUG */
 
1494
 
 
1495
        ut_ad(buf_pool_mutex_own(buf_pool));
 
1496
 
 
1497
        block_mutex = buf_page_get_mutex(bpage);
 
1498
        mutex_enter(block_mutex);
 
1499
 
 
1500
        ut_a(buf_page_in_file(bpage));
 
1501
 
 
1502
        if (buf_flush_ready_for_flush(bpage, flush_type)) {
 
1503
                ulint           space;
 
1504
                ulint           offset;
 
1505
                buf_pool_t*     buf_pool;
 
1506
 
 
1507
                buf_pool = buf_pool_from_bpage(bpage);
 
1508
 
 
1509
                buf_pool_mutex_exit(buf_pool);
 
1510
 
 
1511
                /* These fields are protected by both the
 
1512
                buffer pool mutex and block mutex. */
 
1513
                space = buf_page_get_space(bpage);
 
1514
                offset = buf_page_get_page_no(bpage);
 
1515
 
 
1516
                mutex_exit(block_mutex);
 
1517
 
 
1518
                /* Try to flush also all the neighbors */
 
1519
                *count += buf_flush_try_neighbors(space,
 
1520
                                                  offset,
 
1521
                                                  flush_type,
 
1522
                                                  *count,
 
1523
                                                  n_to_flush);
 
1524
 
 
1525
                buf_pool_mutex_enter(buf_pool);
 
1526
                flushed = TRUE;
 
1527
        } else {
 
1528
                mutex_exit(block_mutex);
 
1529
        }
 
1530
 
 
1531
        ut_ad(buf_pool_mutex_own(buf_pool));
 
1532
 
 
1533
        return(flushed);
 
1534
}
 
1535
 
 
1536
/*******************************************************************//**
 
1537
This utility flushes dirty blocks from the end of the LRU list.
 
1538
In the case of an LRU flush the calling thread may own latches to
 
1539
pages: to avoid deadlocks, this function must be written so that it
 
1540
cannot end up waiting for these latches!
 
1541
@return number of blocks for which the write request was queued. */
 
1542
static
 
1543
ulint
 
1544
buf_flush_LRU_list_batch(
 
1545
/*=====================*/
 
1546
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
 
1547
        ulint           max)            /*!< in: max of blocks to flush */
 
1548
{
 
1549
        buf_page_t*     bpage;
 
1550
        ulint           count = 0;
 
1551
 
 
1552
        ut_ad(buf_pool_mutex_own(buf_pool));
 
1553
 
 
1554
        do {
 
1555
                /* Start from the end of the list looking for a
 
1556
                suitable block to be flushed. */
 
1557
                bpage = UT_LIST_GET_LAST(buf_pool->LRU);
 
1558
 
 
1559
                /* Iterate backwards over the flush list till we find
 
1560
                a page that isn't ready for flushing. */
 
1561
                while (bpage != NULL
 
1562
                       && !buf_flush_page_and_try_neighbors(
 
1563
                                bpage, BUF_FLUSH_LRU, max, &count)) {
 
1564
 
 
1565
                        bpage = UT_LIST_GET_PREV(LRU, bpage);
 
1566
                }
 
1567
        } while (bpage != NULL && count < max);
 
1568
 
 
1569
        /* We keep track of all flushes happening as part of LRU
 
1570
        flush. When estimating the desired rate at which flush_list
 
1571
        should be flushed, we factor in this value. */
 
1572
        buf_lru_flush_page_count += count;
 
1573
 
 
1574
        ut_ad(buf_pool_mutex_own(buf_pool));
 
1575
 
 
1576
        return(count);
 
1577
}
 
1578
 
 
1579
/*******************************************************************//**
 
1580
This utility flushes dirty blocks from the end of the flush_list.
 
1581
the calling thread is not allowed to own any latches on pages!
 
1582
@return number of blocks for which the write request was queued;
 
1583
ULINT_UNDEFINED if there was a flush of the same type already
 
1584
running */
 
1585
static
 
1586
ulint
 
1587
buf_flush_flush_list_batch(
 
1588
/*=======================*/
 
1589
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
 
1590
        ulint           min_n,          /*!< in: wished minimum mumber
 
1591
                                        of blocks flushed (it is not
 
1592
                                        guaranteed that the actual
 
1593
                                        number is that big, though) */
 
1594
        ib_uint64_t     lsn_limit)      /*!< all blocks whose
 
1595
                                        oldest_modification is smaller
 
1596
                                        than this should be flushed (if
 
1597
                                        their number does not exceed
 
1598
                                        min_n) */
 
1599
{
 
1600
        ulint           len;
 
1601
        buf_page_t*     bpage;
 
1602
        ulint           count = 0;
 
1603
 
 
1604
        ut_ad(buf_pool_mutex_own(buf_pool));
 
1605
 
 
1606
        /* If we have flushed enough, leave the loop */
 
1607
        do {
 
1608
                /* Start from the end of the list looking for a suitable
 
1609
                block to be flushed. */
 
1610
 
 
1611
                buf_flush_list_mutex_enter(buf_pool);
 
1612
 
 
1613
                /* We use len here because theoretically insertions can
 
1614
                happen in the flush_list below while we are traversing
 
1615
                it for a suitable candidate for flushing. We'd like to
 
1616
                set a limit on how farther we are willing to traverse
 
1617
                the list. */
 
1618
                len = UT_LIST_GET_LEN(buf_pool->flush_list);
 
1619
                bpage = UT_LIST_GET_LAST(buf_pool->flush_list);
 
1620
 
 
1621
                if (bpage) {
 
1622
                        ut_a(bpage->oldest_modification > 0);
 
1623
                }
 
1624
 
 
1625
                if (!bpage || bpage->oldest_modification >= lsn_limit) {
 
1626
 
 
1627
                        /* We have flushed enough */
 
1628
                        buf_flush_list_mutex_exit(buf_pool);
 
1629
                        break;
 
1630
                }
 
1631
 
 
1632
                ut_a(bpage->oldest_modification > 0);
 
1633
 
 
1634
                ut_ad(bpage->in_flush_list);
 
1635
 
 
1636
                buf_flush_list_mutex_exit(buf_pool);
 
1637
 
 
1638
                /* The list may change during the flushing and we cannot
 
1639
                safely preserve within this function a pointer to a
 
1640
                block in the list! */
 
1641
                while (bpage != NULL
 
1642
                       && len > 0
 
1643
                       && !buf_flush_page_and_try_neighbors(
 
1644
                                bpage, BUF_FLUSH_LIST, min_n, &count)) {
 
1645
 
 
1646
                        buf_flush_list_mutex_enter(buf_pool);
 
1647
 
 
1648
                        /* If we are here that means that buf_pool->mutex
 
1649
                         was not released in buf_flush_page_and_try_neighbors()
 
1650
                        above and this guarantees that bpage didn't get
 
1651
                        relocated since we released the flush_list
 
1652
                        mutex above. There is a chance, however, that
 
1653
                        the bpage got removed from flush_list (not
 
1654
                        currently possible because flush_list_remove()
 
1655
                        also obtains buf_pool mutex but that may change
 
1656
                        in future). To avoid this scenario we check
 
1657
                        the oldest_modification and if it is zero
 
1658
                        we start all over again. */
 
1659
                        if (bpage->oldest_modification == 0) {
 
1660
                                buf_flush_list_mutex_exit(buf_pool);
 
1661
                                break;
 
1662
                        }
 
1663
 
 
1664
                        bpage = UT_LIST_GET_PREV(list, bpage);
 
1665
 
 
1666
                        ut_ad(!bpage || bpage->in_flush_list);
 
1667
 
 
1668
                        buf_flush_list_mutex_exit(buf_pool);
 
1669
 
 
1670
                        --len;
 
1671
                }
 
1672
 
 
1673
        } while (count < min_n && bpage != NULL && len > 0);
 
1674
 
 
1675
        ut_ad(buf_pool_mutex_own(buf_pool));
981
1676
 
982
1677
        return(count);
983
1678
}
990
1685
the calling thread is not allowed to own any latches on pages!
991
1686
@return number of blocks for which the write request was queued;
992
1687
ULINT_UNDEFINED if there was a flush of the same type already running */
993
 
UNIV_INTERN
 
1688
static
994
1689
ulint
995
1690
buf_flush_batch(
996
1691
/*============*/
 
1692
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
997
1693
        enum buf_flush  flush_type,     /*!< in: BUF_FLUSH_LRU or
998
1694
                                        BUF_FLUSH_LIST; if BUF_FLUSH_LIST,
999
1695
                                        then the caller must not own any
1001
1697
        ulint           min_n,          /*!< in: wished minimum mumber of blocks
1002
1698
                                        flushed (it is not guaranteed that the
1003
1699
                                        actual number is that big, though) */
1004
 
        ib_uint64_t     lsn_limit)      /*!< in the case BUF_FLUSH_LIST all
1005
 
                                        blocks whose oldest_modification is
 
1700
        ib_uint64_t     lsn_limit)      /*!< in: in the case of BUF_FLUSH_LIST
 
1701
                                        all blocks whose oldest_modification is
1006
1702
                                        smaller than this should be flushed
1007
1703
                                        (if their number does not exceed
1008
1704
                                        min_n), otherwise ignored */
1009
1705
{
1010
 
        buf_page_t*     bpage;
1011
 
        ulint           page_count      = 0;
1012
 
        ulint           old_page_count;
1013
 
        ulint           space;
1014
 
        ulint           offset;
 
1706
        ulint           count   = 0;
1015
1707
 
1016
 
        ut_ad((flush_type == BUF_FLUSH_LRU)
1017
 
              || (flush_type == BUF_FLUSH_LIST));
 
1708
        ut_ad(flush_type == BUF_FLUSH_LRU || flush_type == BUF_FLUSH_LIST);
1018
1709
#ifdef UNIV_SYNC_DEBUG
1019
1710
        ut_ad((flush_type != BUF_FLUSH_LIST)
1020
1711
              || sync_thread_levels_empty_gen(TRUE));
1021
1712
#endif /* UNIV_SYNC_DEBUG */
1022
 
        buf_pool_mutex_enter();
1023
 
 
1024
 
        if ((buf_pool->n_flush[flush_type] > 0)
1025
 
            || (buf_pool->init_flush[flush_type] == TRUE)) {
1026
 
 
1027
 
                /* There is already a flush batch of the same type running */
1028
 
 
1029
 
                buf_pool_mutex_exit();
1030
 
 
1031
 
                return(ULINT_UNDEFINED);
1032
 
        }
1033
 
 
1034
 
        buf_pool->init_flush[flush_type] = TRUE;
1035
 
 
1036
 
        bool done_with_loop= false;
1037
 
        for (;done_with_loop != true;) {
1038
 
flush_next:
1039
 
                /* If we have flushed enough, leave the loop */
1040
 
                if (page_count >= min_n) {
1041
 
 
1042
 
                        break;
1043
 
                }
1044
 
 
1045
 
                /* Start from the end of the list looking for a suitable
1046
 
                block to be flushed. */
1047
 
 
1048
 
                if (flush_type == BUF_FLUSH_LRU) {
1049
 
                        bpage = UT_LIST_GET_LAST(buf_pool->LRU);
1050
 
                } else {
1051
 
                        ut_ad(flush_type == BUF_FLUSH_LIST);
1052
 
 
1053
 
                        bpage = UT_LIST_GET_LAST(buf_pool->flush_list);
1054
 
                        if (!bpage
1055
 
                            || bpage->oldest_modification >= lsn_limit) {
1056
 
                                /* We have flushed enough */
1057
 
 
1058
 
                                break;
1059
 
                        }
1060
 
                        ut_ad(bpage->in_flush_list);
1061
 
                }
1062
 
 
1063
 
                /* Note that after finding a single flushable page, we try to
1064
 
                flush also all its neighbors, and after that start from the
1065
 
                END of the LRU list or flush list again: the list may change
1066
 
                during the flushing and we cannot safely preserve within this
1067
 
                function a pointer to a block in the list! */
1068
 
 
1069
 
                do {
1070
 
                        mutex_t*block_mutex = buf_page_get_mutex(bpage);
1071
 
                        ibool   ready;
1072
 
 
1073
 
                        ut_a(buf_page_in_file(bpage));
1074
 
 
1075
 
                        mutex_enter(block_mutex);
1076
 
                        ready = buf_flush_ready_for_flush(bpage, flush_type);
1077
 
                        mutex_exit(block_mutex);
1078
 
 
1079
 
                        if (ready) {
1080
 
                                space = buf_page_get_space(bpage);
1081
 
                                offset = buf_page_get_page_no(bpage);
1082
 
 
1083
 
                                buf_pool_mutex_exit();
1084
 
 
1085
 
                                old_page_count = page_count;
1086
 
 
1087
 
                                /* Try to flush also all the neighbors */
1088
 
                                page_count += buf_flush_try_neighbors(
1089
 
                                        space, offset, flush_type);
1090
 
                                /* fprintf(stderr,
1091
 
                                "Flush type %lu, page no %lu, neighb %lu\n",
1092
 
                                flush_type, offset,
1093
 
                                page_count - old_page_count); */
1094
 
 
1095
 
                                buf_pool_mutex_enter();
1096
 
                                goto flush_next;
1097
 
 
1098
 
                        } else if (flush_type == BUF_FLUSH_LRU) {
1099
 
                                bpage = UT_LIST_GET_PREV(LRU, bpage);
1100
 
                        } else {
1101
 
                                ut_ad(flush_type == BUF_FLUSH_LIST);
1102
 
 
1103
 
                                bpage = UT_LIST_GET_PREV(list, bpage);
1104
 
                                ut_ad(!bpage || bpage->in_flush_list);
1105
 
                        }
1106
 
                } while (bpage != NULL);
1107
 
 
1108
 
                /* If we could not find anything to flush, leave the loop */
1109
 
 
1110
 
                done_with_loop= true;
1111
 
 
1112
 
        }
1113
 
 
1114
 
        buf_pool->init_flush[flush_type] = FALSE;
1115
 
 
1116
 
        if (buf_pool->n_flush[flush_type] == 0) {
1117
 
 
1118
 
                /* The running flush batch has ended */
1119
 
 
1120
 
                os_event_set(buf_pool->no_flush[flush_type]);
1121
 
        }
1122
 
 
1123
 
        buf_pool_mutex_exit();
1124
 
 
1125
 
        buf_flush_buffered_writes();
 
1713
 
 
1714
        buf_pool_mutex_enter(buf_pool);
 
1715
 
 
1716
        /* Note: The buffer pool mutex is released and reacquired within
 
1717
        the flush functions. */
 
1718
        switch(flush_type) {
 
1719
        case BUF_FLUSH_LRU:
 
1720
                count = buf_flush_LRU_list_batch(buf_pool, min_n);
 
1721
                break;
 
1722
        case BUF_FLUSH_LIST:
 
1723
                count = buf_flush_flush_list_batch(buf_pool, min_n, lsn_limit);
 
1724
                break;
 
1725
        default:
 
1726
                ut_error;
 
1727
        }
 
1728
 
 
1729
        buf_pool_mutex_exit(buf_pool);
 
1730
 
 
1731
        buf_flush_buffered_writes();
 
1732
 
 
1733
#ifdef UNIV_DEBUG
 
1734
        if (buf_debug_prints && count > 0) {
 
1735
                fprintf(stderr, flush_type == BUF_FLUSH_LRU
 
1736
                        ? "Flushed %lu pages in LRU flush\n"
 
1737
                        : "Flushed %lu pages in flush list flush\n",
 
1738
                        (ulong) count);
 
1739
        }
 
1740
#endif /* UNIV_DEBUG */
 
1741
 
 
1742
        srv_buf_pool_flushed += count;
 
1743
 
 
1744
        return(count);
 
1745
}
 
1746
 
 
1747
/******************************************************************//**
 
1748
Gather the aggregated stats for both flush list and LRU list flushing */
 
1749
static
 
1750
void
 
1751
buf_flush_common(
 
1752
/*=============*/
 
1753
        enum buf_flush  flush_type,     /*!< in: type of flush */
 
1754
        ulint           page_count)     /*!< in: number of pages flushed */
 
1755
{
 
1756
        buf_flush_buffered_writes();
 
1757
 
 
1758
        ut_a(flush_type == BUF_FLUSH_LRU || flush_type == BUF_FLUSH_LIST);
1126
1759
 
1127
1760
#ifdef UNIV_DEBUG
1128
1761
        if (buf_debug_prints && page_count > 0) {
1129
 
                ut_a(flush_type == BUF_FLUSH_LRU
1130
 
                     || flush_type == BUF_FLUSH_LIST);
1131
1762
                fprintf(stderr, flush_type == BUF_FLUSH_LRU
1132
1763
                        ? "Flushed %lu pages in LRU flush\n"
1133
1764
                        : "Flushed %lu pages in flush list flush\n",
1137
1768
 
1138
1769
        srv_buf_pool_flushed += page_count;
1139
1770
 
1140
 
        /* We keep track of all flushes happening as part of LRU
1141
 
        flush. When estimating the desired rate at which flush_list
1142
 
        should be flushed we factor in this value. */
1143
1771
        if (flush_type == BUF_FLUSH_LRU) {
 
1772
                /* We keep track of all flushes happening as part of LRU
 
1773
                flush. When estimating the desired rate at which flush_list
 
1774
                should be flushed we factor in this value. */
1144
1775
                buf_lru_flush_page_count += page_count;
1145
1776
        }
1146
 
 
1147
 
        return(page_count);
 
1777
}
 
1778
 
 
1779
/******************************************************************//**
 
1780
Start a buffer flush batch for LRU or flush list */
 
1781
static
 
1782
ibool
 
1783
buf_flush_start(
 
1784
/*============*/
 
1785
        buf_pool_t*     buf_pool,       /*!< buffer pool instance */
 
1786
        enum buf_flush  flush_type)     /*!< in: BUF_FLUSH_LRU
 
1787
                                        or BUF_FLUSH_LIST */
 
1788
{
 
1789
        buf_pool_mutex_enter(buf_pool);
 
1790
 
 
1791
        if (buf_pool->n_flush[flush_type] > 0
 
1792
           || buf_pool->init_flush[flush_type] == TRUE) {
 
1793
 
 
1794
                /* There is already a flush batch of the same type running */
 
1795
 
 
1796
                buf_pool_mutex_exit(buf_pool);
 
1797
 
 
1798
                return(FALSE);
 
1799
        }
 
1800
 
 
1801
        buf_pool->init_flush[flush_type] = TRUE;
 
1802
 
 
1803
        buf_pool_mutex_exit(buf_pool);
 
1804
 
 
1805
        return(TRUE);
 
1806
}
 
1807
 
 
1808
/******************************************************************//**
 
1809
End a buffer flush batch for LRU or flush list */
 
1810
static
 
1811
void
 
1812
buf_flush_end(
 
1813
/*==========*/
 
1814
        buf_pool_t*     buf_pool,       /*!< buffer pool instance */
 
1815
        enum buf_flush  flush_type)     /*!< in: BUF_FLUSH_LRU
 
1816
                                        or BUF_FLUSH_LIST */
 
1817
{
 
1818
        buf_pool_mutex_enter(buf_pool);
 
1819
 
 
1820
        buf_pool->init_flush[flush_type] = FALSE;
 
1821
 
 
1822
        if (buf_pool->n_flush[flush_type] == 0) {
 
1823
 
 
1824
                /* The running flush batch has ended */
 
1825
 
 
1826
                os_event_set(buf_pool->no_flush[flush_type]);
 
1827
        }
 
1828
 
 
1829
        buf_pool_mutex_exit(buf_pool);
1148
1830
}
1149
1831
 
1150
1832
/******************************************************************//**
1153
1835
void
1154
1836
buf_flush_wait_batch_end(
1155
1837
/*=====================*/
1156
 
        enum buf_flush  type)   /*!< in: BUF_FLUSH_LRU or BUF_FLUSH_LIST */
1157
 
{
1158
 
        ut_ad((type == BUF_FLUSH_LRU) || (type == BUF_FLUSH_LIST));
1159
 
 
1160
 
        os_event_wait(buf_pool->no_flush[type]);
1161
 
}
1162
 
 
 
1838
        buf_pool_t*     buf_pool,       /*!< buffer pool instance */
 
1839
        enum buf_flush  type)           /*!< in: BUF_FLUSH_LRU
 
1840
                                        or BUF_FLUSH_LIST */
 
1841
{
 
1842
        ut_ad(type == BUF_FLUSH_LRU || type == BUF_FLUSH_LIST);
 
1843
 
 
1844
        if (buf_pool == NULL) {
 
1845
                ulint   i;
 
1846
 
 
1847
                for (i = 0; i < srv_buf_pool_instances; ++i) {
 
1848
                        buf_pool_t*     i_buf_pool = buf_pool_from_array(i);
 
1849
 
 
1850
                        os_event_wait(i_buf_pool->no_flush[type]);
 
1851
                }
 
1852
        } else {
 
1853
                os_event_wait(buf_pool->no_flush[type]);
 
1854
        }
 
1855
}
 
1856
 
 
1857
/*******************************************************************//**
 
1858
This utility flushes dirty blocks from the end of the LRU list.
 
1859
NOTE: The calling thread may own latches to pages: to avoid deadlocks,
 
1860
this function must be written so that it cannot end up waiting for these
 
1861
latches!
 
1862
@return number of blocks for which the write request was queued;
 
1863
ULINT_UNDEFINED if there was a flush of the same type already running */
 
1864
UNIV_INTERN
 
1865
ulint
 
1866
buf_flush_LRU(
 
1867
/*==========*/
 
1868
        buf_pool_t*     buf_pool,       /*!< in: buffer pool instance */
 
1869
        ulint           min_n)          /*!< in: wished minimum mumber of blocks
 
1870
                                        flushed (it is not guaranteed that the
 
1871
                                        actual number is that big, though) */
 
1872
{
 
1873
        ulint           page_count;
 
1874
 
 
1875
        if (!buf_flush_start(buf_pool, BUF_FLUSH_LRU)) {
 
1876
                return(ULINT_UNDEFINED);
 
1877
        }
 
1878
 
 
1879
        page_count = buf_flush_batch(buf_pool, BUF_FLUSH_LRU, min_n, 0);
 
1880
 
 
1881
        buf_flush_end(buf_pool, BUF_FLUSH_LRU);
 
1882
 
 
1883
        buf_flush_common(BUF_FLUSH_LRU, page_count);
 
1884
 
 
1885
        return(page_count);
 
1886
}
 
1887
 
 
1888
/*******************************************************************//**
 
1889
This utility flushes dirty blocks from the end of the flush list of
 
1890
all buffer pool instances.
 
1891
NOTE: The calling thread is not allowed to own any latches on pages!
 
1892
@return number of blocks for which the write request was queued;
 
1893
ULINT_UNDEFINED if there was a flush of the same type already running */
 
1894
UNIV_INTERN
 
1895
ulint
 
1896
buf_flush_list(
 
1897
/*===========*/
 
1898
        ulint           min_n,          /*!< in: wished minimum mumber of blocks
 
1899
                                        flushed (it is not guaranteed that the
 
1900
                                        actual number is that big, though) */
 
1901
        ib_uint64_t     lsn_limit)      /*!< in the case BUF_FLUSH_LIST all
 
1902
                                        blocks whose oldest_modification is
 
1903
                                        smaller than this should be flushed
 
1904
                                        (if their number does not exceed
 
1905
                                        min_n), otherwise ignored */
 
1906
{
 
1907
        ulint           i;
 
1908
        ulint           total_page_count = 0;
 
1909
        ibool           skipped = FALSE;
 
1910
 
 
1911
        if (min_n != ULINT_MAX) {
 
1912
                /* Ensure that flushing is spread evenly amongst the
 
1913
                buffer pool instances. When min_n is ULINT_MAX
 
1914
                we need to flush everything up to the lsn limit
 
1915
                so no limit here. */
 
1916
                min_n = (min_n + srv_buf_pool_instances - 1)
 
1917
                         / srv_buf_pool_instances;
 
1918
        }
 
1919
 
 
1920
        /* Flush to lsn_limit in all buffer pool instances */
 
1921
        for (i = 0; i < srv_buf_pool_instances; i++) {
 
1922
                buf_pool_t*     buf_pool;
 
1923
                ulint           page_count = 0;
 
1924
 
 
1925
                buf_pool = buf_pool_from_array(i);
 
1926
 
 
1927
                if (!buf_flush_start(buf_pool, BUF_FLUSH_LIST)) {
 
1928
                        /* We have two choices here. If lsn_limit was
 
1929
                        specified then skipping an instance of buffer
 
1930
                        pool means we cannot guarantee that all pages
 
1931
                        up to lsn_limit has been flushed. We can
 
1932
                        return right now with failure or we can try
 
1933
                        to flush remaining buffer pools up to the
 
1934
                        lsn_limit. We attempt to flush other buffer
 
1935
                        pools based on the assumption that it will
 
1936
                        help in the retry which will follow the
 
1937
                        failure. */
 
1938
                        skipped = TRUE;
 
1939
 
 
1940
                        continue;
 
1941
                }
 
1942
 
 
1943
                page_count = buf_flush_batch(
 
1944
                        buf_pool, BUF_FLUSH_LIST, min_n, lsn_limit);
 
1945
 
 
1946
                buf_flush_end(buf_pool, BUF_FLUSH_LIST);
 
1947
 
 
1948
                buf_flush_common(BUF_FLUSH_LIST, page_count);
 
1949
 
 
1950
                total_page_count += page_count;
 
1951
        }
 
1952
 
 
1953
        return(lsn_limit != IB_ULONGLONG_MAX && skipped
 
1954
               ? ULINT_UNDEFINED : total_page_count);
 
1955
}
 
1956
 
1163
1957
/******************************************************************//**
1164
1958
Gives a recommendation of how many blocks should be flushed to establish
1165
1959
a big enough margin of replaceable blocks near the end of the LRU list
1168
1962
LRU list */
1169
1963
static
1170
1964
ulint
1171
 
buf_flush_LRU_recommendation(void)
1172
 
/*==============================*/
 
1965
buf_flush_LRU_recommendation(
 
1966
/*=========================*/
 
1967
        buf_pool_t*     buf_pool)               /*!< in: Buffer pool instance */
1173
1968
{
1174
1969
        buf_page_t*     bpage;
1175
1970
        ulint           n_replaceable;
1176
1971
        ulint           distance        = 0;
1177
1972
 
1178
 
        buf_pool_mutex_enter();
 
1973
        buf_pool_mutex_enter(buf_pool);
1179
1974
 
1180
1975
        n_replaceable = UT_LIST_GET_LEN(buf_pool->free);
1181
1976
 
1182
1977
        bpage = UT_LIST_GET_LAST(buf_pool->LRU);
1183
1978
 
1184
1979
        while ((bpage != NULL)
1185
 
               && (n_replaceable < BUF_FLUSH_FREE_BLOCK_MARGIN
1186
 
                   + BUF_FLUSH_EXTRA_MARGIN)
1187
 
               && (distance < BUF_LRU_FREE_SEARCH_LEN)) {
 
1980
               && (n_replaceable < BUF_FLUSH_FREE_BLOCK_MARGIN(buf_pool)
 
1981
                   + BUF_FLUSH_EXTRA_MARGIN(buf_pool))
 
1982
               && (distance < BUF_LRU_FREE_SEARCH_LEN(buf_pool))) {
1188
1983
 
1189
1984
                mutex_t* block_mutex = buf_page_get_mutex(bpage);
1190
1985
 
1201
1996
                bpage = UT_LIST_GET_PREV(LRU, bpage);
1202
1997
        }
1203
1998
 
1204
 
        buf_pool_mutex_exit();
 
1999
        buf_pool_mutex_exit(buf_pool);
1205
2000
 
1206
 
        if (n_replaceable >= BUF_FLUSH_FREE_BLOCK_MARGIN) {
 
2001
        if (n_replaceable >= BUF_FLUSH_FREE_BLOCK_MARGIN(buf_pool)) {
1207
2002
 
1208
2003
                return(0);
1209
2004
        }
1210
2005
 
1211
 
        return(BUF_FLUSH_FREE_BLOCK_MARGIN + BUF_FLUSH_EXTRA_MARGIN
 
2006
        return(BUF_FLUSH_FREE_BLOCK_MARGIN(buf_pool)
 
2007
               + BUF_FLUSH_EXTRA_MARGIN(buf_pool)
1212
2008
               - n_replaceable);
1213
2009
}
1214
2010
 
1220
2016
immediately, without waiting. */
1221
2017
UNIV_INTERN
1222
2018
void
1223
 
buf_flush_free_margin(void)
1224
 
/*=======================*/
 
2019
buf_flush_free_margin(
 
2020
/*==================*/
 
2021
        buf_pool_t*     buf_pool)               /*!< in: Buffer pool instance */
1225
2022
{
1226
2023
        ulint   n_to_flush;
1227
 
        ulint   n_flushed;
1228
2024
 
1229
 
        n_to_flush = buf_flush_LRU_recommendation();
 
2025
        n_to_flush = buf_flush_LRU_recommendation(buf_pool);
1230
2026
 
1231
2027
        if (n_to_flush > 0) {
1232
 
                n_flushed = buf_flush_batch(BUF_FLUSH_LRU, n_to_flush, 0);
 
2028
                ulint   n_flushed;
 
2029
 
 
2030
                n_flushed = buf_flush_LRU(buf_pool, n_to_flush);
 
2031
 
1233
2032
                if (n_flushed == ULINT_UNDEFINED) {
1234
2033
                        /* There was an LRU type flush batch already running;
1235
2034
                        let us wait for it to end */
1236
2035
 
1237
 
                        buf_flush_wait_batch_end(BUF_FLUSH_LRU);
 
2036
                        buf_flush_wait_batch_end(buf_pool, BUF_FLUSH_LRU);
1238
2037
                }
1239
2038
        }
1240
2039
}
1241
2040
 
 
2041
/*********************************************************************//**
 
2042
Flushes pages from the end of all the LRU lists. */
 
2043
UNIV_INTERN
 
2044
void
 
2045
buf_flush_free_margins(void)
 
2046
/*========================*/
 
2047
{
 
2048
        ulint   i;
 
2049
 
 
2050
        for (i = 0; i < srv_buf_pool_instances; i++) {
 
2051
                buf_pool_t*     buf_pool;
 
2052
 
 
2053
                buf_pool = buf_pool_from_array(i);
 
2054
 
 
2055
                buf_flush_free_margin(buf_pool);
 
2056
        }
 
2057
}
 
2058
 
1242
2059
/*********************************************************************
1243
2060
Update the historical stats that we are collecting for flush rate
1244
2061
heuristics at the end of each interval.
1299
2116
buf_flush_get_desired_flush_rate(void)
1300
2117
/*==================================*/
1301
2118
{
1302
 
        ulint                   redo_avg;
1303
 
        ulint                   lru_flush_avg;
1304
 
        ulint                   n_dirty;
1305
 
        ulint                   n_flush_req;
1306
 
        lint                    rate;
1307
 
        ib_uint64_t             lsn = log_get_lsn();
1308
 
        ulint                   log_capacity = log_get_capacity();
 
2119
        ulint           i;
 
2120
        lint            rate;
 
2121
        ulint           redo_avg;
 
2122
        ulint           n_dirty = 0;
 
2123
        ulint           n_flush_req;
 
2124
        ulint           lru_flush_avg;
 
2125
        ib_uint64_t     lsn = log_get_lsn();
 
2126
        ulint           log_capacity = log_get_capacity();
1309
2127
 
1310
2128
        /* log_capacity should never be zero after the initialization
1311
2129
        of log subsystem. */
1312
2130
        ut_ad(log_capacity != 0);
1313
2131
 
1314
2132
        /* Get total number of dirty pages. It is OK to access
1315
 
        flush_list without holding any mtex as we are using this
 
2133
        flush_list without holding any mutex as we are using this
1316
2134
        only for heuristics. */
1317
 
        n_dirty = UT_LIST_GET_LEN(buf_pool->flush_list);
 
2135
        for (i = 0; i < srv_buf_pool_instances; i++) {
 
2136
                buf_pool_t*     buf_pool;
 
2137
 
 
2138
                buf_pool = buf_pool_from_array(i);
 
2139
                n_dirty += UT_LIST_GET_LEN(buf_pool->flush_list);
 
2140
        }
1318
2141
 
1319
2142
        /* An overflow can happen if we generate more than 2^32 bytes
1320
2143
        of redo in this interval i.e.: 4G of redo in 1 second. We can
1356
2179
@return TRUE if ok */
1357
2180
static
1358
2181
ibool
1359
 
buf_flush_validate_low(void)
1360
 
/*========================*/
 
2182
buf_flush_validate_low(
 
2183
/*===================*/
 
2184
        buf_pool_t*     buf_pool)               /*!< in: Buffer pool instance */
1361
2185
{
1362
 
        buf_page_t*     bpage;
 
2186
        buf_page_t*             bpage;
 
2187
        const ib_rbt_node_t*    rnode = NULL;
 
2188
 
 
2189
        ut_ad(buf_flush_list_mutex_own(buf_pool));
1363
2190
 
1364
2191
        UT_LIST_VALIDATE(list, buf_page_t, buf_pool->flush_list,
1365
2192
                         ut_ad(ut_list_node_313->in_flush_list));
1366
2193
 
1367
2194
        bpage = UT_LIST_GET_FIRST(buf_pool->flush_list);
1368
2195
 
 
2196
        /* If we are in recovery mode i.e.: flush_rbt != NULL
 
2197
        then each block in the flush_list must also be present
 
2198
        in the flush_rbt. */
 
2199
        if (UNIV_LIKELY_NULL(buf_pool->flush_rbt)) {
 
2200
                rnode = rbt_first(buf_pool->flush_rbt);
 
2201
        }
 
2202
 
1369
2203
        while (bpage != NULL) {
1370
2204
                const ib_uint64_t om = bpage->oldest_modification;
 
2205
 
 
2206
                ut_ad(buf_pool_from_bpage(bpage) == buf_pool);
 
2207
 
1371
2208
                ut_ad(bpage->in_flush_list);
1372
 
                ut_a(buf_page_in_file(bpage));
 
2209
 
 
2210
                /* A page in buf_pool->flush_list can be in
 
2211
                BUF_BLOCK_REMOVE_HASH state. This happens when a page
 
2212
                is in the middle of being relocated. In that case the
 
2213
                original descriptor can have this state and still be
 
2214
                in the flush list waiting to acquire the
 
2215
                buf_pool->flush_list_mutex to complete the relocation. */
 
2216
                ut_a(buf_page_in_file(bpage)
 
2217
                     || buf_page_get_state(bpage) == BUF_BLOCK_REMOVE_HASH);
1373
2218
                ut_a(om > 0);
1374
2219
 
 
2220
                if (UNIV_LIKELY_NULL(buf_pool->flush_rbt)) {
 
2221
                        buf_page_t** prpage;
 
2222
 
 
2223
                        ut_a(rnode);
 
2224
                        prpage = rbt_value(buf_page_t*, rnode);
 
2225
 
 
2226
                        ut_a(*prpage);
 
2227
                        ut_a(*prpage == bpage);
 
2228
                        rnode = rbt_next(buf_pool->flush_rbt, rnode);
 
2229
                }
 
2230
 
1375
2231
                bpage = UT_LIST_GET_NEXT(list, bpage);
1376
2232
 
1377
2233
                ut_a(!bpage || om >= bpage->oldest_modification);
1378
2234
        }
1379
2235
 
 
2236
        /* By this time we must have exhausted the traversal of
 
2237
        flush_rbt (if active) as well. */
 
2238
        ut_a(rnode == NULL);
 
2239
 
1380
2240
        return(TRUE);
1381
2241
}
1382
2242
 
1385
2245
@return TRUE if ok */
1386
2246
UNIV_INTERN
1387
2247
ibool
1388
 
buf_flush_validate(void)
1389
 
/*====================*/
 
2248
buf_flush_validate(
 
2249
/*===============*/
 
2250
        buf_pool_t*     buf_pool)       /*!< buffer pool instance */
1390
2251
{
1391
2252
        ibool   ret;
1392
2253
 
1393
 
        buf_pool_mutex_enter();
1394
 
 
1395
 
        ret = buf_flush_validate_low();
1396
 
 
1397
 
        buf_pool_mutex_exit();
 
2254
        buf_flush_list_mutex_enter(buf_pool);
 
2255
 
 
2256
        ret = buf_flush_validate_low(buf_pool);
 
2257
 
 
2258
        buf_flush_list_mutex_exit(buf_pool);
1398
2259
 
1399
2260
        return(ret);
1400
2261
}