/
buf0flu.cc
4061 lines (3275 loc) · 110 KB
/
buf0flu.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 1995, 2023, Oracle and/or its affiliates.
Copyright (c) 2016, Percona Inc. All Rights Reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file buf/buf0flu.cc
The database buffer buf_pool flush algorithm
Created 11/11/1995 Heikki Tuuri
*******************************************************/
#include "ha_prototypes.h"
#include <mysql/service_thd_wait.h>
#include <my_dbug.h>
#include "buf0flu.h"
#ifdef UNIV_NONINL
#include "buf0flu.ic"
#endif
#include "buf0buf.h"
#include "buf0checksum.h"
#include "srv0start.h"
#include "srv0srv.h"
#include "page0zip.h"
#ifndef UNIV_HOTBACKUP
#include "ut0byte.h"
#include "page0page.h"
#include "fil0fil.h"
#include "buf0lru.h"
#include "buf0rea.h"
#include "ibuf0ibuf.h"
#include "log0log.h"
#include "os0file.h"
#include "trx0sys.h"
#include "srv0mon.h"
#include "fsp0sysspace.h"
#include "ut0stage.h"
#ifdef UNIV_LINUX
/* include defs for CPU time priority settings */
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/resource.h>
static const int buf_flush_page_cleaner_priority = -20;
#endif /* UNIV_LINUX */
/** Sleep time in microseconds for loop waiting for the oldest
modification lsn */
static const ulint buf_flush_wait_flushed_sleep_time = 10000;
/** Number of pages flushed through non flush_list flushes. */
static ulint buf_lru_flush_page_count = 0;
/** Flag indicating if the page_cleaner is in active state. This flag
is set to TRUE by the page_cleaner thread when it is spawned and is set
back to FALSE at shutdown by the page_cleaner as well. Therefore no
need to protect it by a mutex. It is only ever read by the thread
doing the shutdown */
bool buf_page_cleaner_is_active = false;
/** The number of running LRU manager threads. 0 if LRU manager is inactive. */
ulint buf_lru_manager_running_threads = 0;
/** Factor for scan length to determine n_pages for intended oldest LSN
progress */
static ulint buf_flush_lsn_scan_factor = 3;
/** Average redo generation rate */
static lsn_t lsn_avg_rate = 0;
/** Target oldest LSN for the requested flush_sync */
static lsn_t buf_flush_sync_lsn = 0;
#ifdef UNIV_PFS_THREAD
mysql_pfs_key_t page_cleaner_thread_key;
mysql_pfs_key_t buf_lru_manager_thread_key;
#endif /* UNIV_PFS_THREAD */
/** Event to synchronise with the flushing. */
os_event_t buf_flush_event;
/** State for page cleaner array slot */
enum page_cleaner_state_t {
/** Not requested any yet.
Moved from FINISHED by the coordinator. */
PAGE_CLEANER_STATE_NONE = 0,
/** Requested but not started flushing.
Moved from NONE by the coordinator. */
PAGE_CLEANER_STATE_REQUESTED,
/** Flushing is on going.
Moved from REQUESTED by the worker. */
PAGE_CLEANER_STATE_FLUSHING,
/** Flushing was finished.
Moved from FLUSHING by the worker. */
PAGE_CLEANER_STATE_FINISHED
};
/** Page cleaner request state for each buffer pool instance */
struct page_cleaner_slot_t {
page_cleaner_state_t state; /*!< state of the request.
protected by page_cleaner_t::mutex
if the worker thread got the slot and
set to PAGE_CLEANER_STATE_FLUSHING,
n_flushed_list can be updated only by
the worker thread */
/* This value is set during state==PAGE_CLEANER_STATE_NONE */
ulint n_pages_requested;
/*!< number of requested pages
for the slot */
/* These values are updated during state==PAGE_CLEANER_STATE_FLUSHING,
and commited with state==PAGE_CLEANER_STATE_FINISHED.
The consistency is protected by the 'state' */
ulint n_flushed_list;
/*!< number of flushed pages
by flush_list flushing */
bool succeeded_list;
/*!< true if flush_list flushing
succeeded. */
/*!< elapsed time for LRU flushing */
uint64_t flush_list_time;
/*!< elapsed time for flush_list
flushing */
ulint flush_list_pass;
/*!< count to attempt flush_list
flushing */
};
/** Page cleaner structure common for all threads */
struct page_cleaner_t {
ib_mutex_t mutex; /*!< mutex to protect whole of
page_cleaner_t struct and
page_cleaner_slot_t slots. */
os_event_t is_requested; /*!< event to activate worker
threads. */
os_event_t is_finished; /*!< event to signal that all
slots were finished. */
volatile ulint n_workers; /*!< number of worker threads
in existence */
bool requested; /*!< true if requested pages
to flush */
lsn_t lsn_limit; /*!< upper limit of LSN to be
flushed */
ulint n_slots; /*!< total number of slots */
ulint n_slots_requested;
/*!< number of slots
in the state
PAGE_CLEANER_STATE_REQUESTED */
ulint n_slots_flushing;
/*!< number of slots
in the state
PAGE_CLEANER_STATE_FLUSHING */
ulint n_slots_finished;
/*!< number of slots
in the state
PAGE_CLEANER_STATE_FINISHED */
uint64_t flush_time; /*!< elapsed time to flush
requests for all slots */
ulint flush_pass; /*!< count to finish to flush
requests for all slots */
page_cleaner_slot_t* slots; /*!< pointer to the slots */
bool is_running; /*!< false if attempt
to shutdown */
#ifdef UNIV_DEBUG
ulint n_disabled_debug;
/*<! how many of pc threads
have been disabled */
#endif /* UNIV_DEBUG */
};
static page_cleaner_t* page_cleaner = NULL;
#ifdef UNIV_DEBUG
my_bool innodb_page_cleaner_disabled_debug;
#endif /* UNIV_DEBUG */
/* @} */
/******************************************************************//**
Increases flush_list size in bytes with the page size in inline function */
static inline
void
incr_flush_list_size_in_bytes(
/*==========================*/
buf_block_t* block, /*!< in: control block */
buf_pool_t* buf_pool) /*!< in: buffer pool instance */
{
ut_ad(buf_flush_list_mutex_own(buf_pool));
buf_pool->stat.flush_list_bytes += block->page.size.physical();
ut_ad(buf_pool->stat.flush_list_bytes <= buf_pool->curr_pool_size);
}
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
/******************************************************************//**
Validates the flush list.
@return TRUE if ok */
static
ibool
buf_flush_validate_low(
/*===================*/
buf_pool_t* buf_pool); /*!< in: Buffer pool instance */
/******************************************************************//**
Validates the flush list some of the time.
@return TRUE if ok or the check was skipped */
static
ibool
buf_flush_validate_skip(
/*====================*/
buf_pool_t* buf_pool) /*!< in: Buffer pool instance */
{
/** Try buf_flush_validate_low() every this many times */
# define BUF_FLUSH_VALIDATE_SKIP 23
/** The buf_flush_validate_low() call skip counter.
Use a signed type because of the race condition below. */
static int buf_flush_validate_count = BUF_FLUSH_VALIDATE_SKIP;
/* There is a race condition below, but it does not matter,
because this call is only for heuristic purposes. We want to
reduce the call frequency of the costly buf_flush_validate_low()
check in debug builds. */
if (--buf_flush_validate_count > 0) {
return(TRUE);
}
buf_flush_validate_count = BUF_FLUSH_VALIDATE_SKIP;
return(buf_flush_validate_low(buf_pool));
}
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
/******************************************************************//**
Insert a block in the flush_rbt and returns a pointer to its
predecessor or NULL if no predecessor. The ordering is maintained
on the basis of the <oldest_modification, space, offset> key.
@return pointer to the predecessor or NULL if no predecessor. */
static
buf_page_t*
buf_flush_insert_in_flush_rbt(
/*==========================*/
buf_page_t* bpage) /*!< in: bpage to be inserted. */
{
const ib_rbt_node_t* c_node;
const ib_rbt_node_t* p_node;
buf_page_t* prev = NULL;
buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
ut_ad(buf_flush_list_mutex_own(buf_pool));
/* Insert this buffer into the rbt. */
c_node = rbt_insert(buf_pool->flush_rbt, &bpage, &bpage);
ut_a(c_node != NULL);
/* Get the predecessor. */
p_node = rbt_prev(buf_pool->flush_rbt, c_node);
if (p_node != NULL) {
buf_page_t** value;
value = rbt_value(buf_page_t*, p_node);
prev = *value;
ut_a(prev != NULL);
}
return(prev);
}
/*********************************************************//**
Delete a bpage from the flush_rbt. */
static
void
buf_flush_delete_from_flush_rbt(
/*============================*/
buf_page_t* bpage) /*!< in: bpage to be removed. */
{
#ifdef UNIV_DEBUG
ibool ret = FALSE;
#endif /* UNIV_DEBUG */
buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
ut_ad(buf_flush_list_mutex_own(buf_pool));
#ifdef UNIV_DEBUG
ret =
#endif /* UNIV_DEBUG */
rbt_delete(buf_pool->flush_rbt, &bpage);
ut_ad(ret);
}
/*****************************************************************//**
Compare two modified blocks in the buffer pool. The key for comparison
is:
key = <oldest_modification, space, offset>
This comparison is used to maintian ordering of blocks in the
buf_pool->flush_rbt.
Note that for the purpose of flush_rbt, we only need to order blocks
on the oldest_modification. The other two fields are used to uniquely
identify the blocks.
@return < 0 if b2 < b1, 0 if b2 == b1, > 0 if b2 > b1 */
static
int
buf_flush_block_cmp(
/*================*/
const void* p1, /*!< in: block1 */
const void* p2) /*!< in: block2 */
{
int ret;
const buf_page_t* b1 = *(const buf_page_t**) p1;
const buf_page_t* b2 = *(const buf_page_t**) p2;
ut_ad(b1 != NULL);
ut_ad(b2 != NULL);
#ifdef UNIV_DEBUG
buf_pool_t* buf_pool = buf_pool_from_bpage(b1);
#endif /* UNIV_DEBUG */
ut_ad(buf_flush_list_mutex_own(buf_pool));
ut_ad(b1->in_flush_list);
ut_ad(b2->in_flush_list);
if (b2->oldest_modification > b1->oldest_modification) {
return(1);
} else if (b2->oldest_modification < b1->oldest_modification) {
return(-1);
}
/* If oldest_modification is same then decide on the space. */
ret = (int)(b2->id.space() - b1->id.space());
/* Or else decide ordering on the page number. */
return(ret ? ret : (int) (b2->id.page_no() - b1->id.page_no()));
}
/********************************************************************//**
Initialize the red-black tree to speed up insertions into the flush_list
during recovery process. Should be called at the start of recovery
process before any page has been read/written. */
void
buf_flush_init_flush_rbt(void)
/*==========================*/
{
ulint i;
for (i = 0; i < srv_buf_pool_instances; i++) {
buf_pool_t* buf_pool;
buf_pool = buf_pool_from_array(i);
buf_flush_list_mutex_enter(buf_pool);
ut_ad(buf_pool->flush_rbt == NULL);
/* Create red black tree for speedy insertions in flush list. */
buf_pool->flush_rbt = rbt_create(
sizeof(buf_page_t*), buf_flush_block_cmp);
buf_flush_list_mutex_exit(buf_pool);
}
}
/********************************************************************//**
Frees up the red-black tree. */
void
buf_flush_free_flush_rbt(void)
/*==========================*/
{
ulint i;
for (i = 0; i < srv_buf_pool_instances; i++) {
buf_pool_t* buf_pool;
buf_pool = buf_pool_from_array(i);
buf_flush_list_mutex_enter(buf_pool);
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
ut_a(buf_flush_validate_low(buf_pool));
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
rbt_free(buf_pool->flush_rbt);
buf_pool->flush_rbt = NULL;
buf_flush_list_mutex_exit(buf_pool);
}
}
/********************************************************************//**
Inserts a modified block into the flush list. */
void
buf_flush_insert_into_flush_list(
/*=============================*/
buf_pool_t* buf_pool, /*!< buffer pool instance */
buf_block_t* block, /*!< in/out: block which is modified */
lsn_t lsn) /*!< in: oldest modification */
{
ut_ad(srv_shutdown_state != SRV_SHUTDOWN_FLUSH_PHASE);
ut_ad(log_flush_order_mutex_own());
ut_ad(mutex_own(buf_page_get_mutex(&block->page)));
buf_flush_list_mutex_enter(buf_pool);
ut_ad((UT_LIST_GET_FIRST(buf_pool->flush_list) == NULL)
|| (UT_LIST_GET_FIRST(buf_pool->flush_list)->oldest_modification
<= lsn));
/* If we are in the recovery then we need to update the flush
red-black tree as well. */
if (buf_pool->flush_rbt != NULL) {
buf_flush_list_mutex_exit(buf_pool);
buf_flush_insert_sorted_into_flush_list(buf_pool, block, lsn);
return;
}
ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
ut_ad(!block->page.in_flush_list);
ut_d(block->page.in_flush_list = TRUE);
block->page.oldest_modification = lsn;
UT_LIST_ADD_FIRST(buf_pool->flush_list, &block->page);
incr_flush_list_size_in_bytes(block, buf_pool);
#ifdef UNIV_DEBUG_VALGRIND
void* p;
if (block->page.size.is_compressed()) {
p = block->page.zip.data;
} else {
p = block->frame;
}
UNIV_MEM_ASSERT_RW(p, block->page.size.physical());
#endif /* UNIV_DEBUG_VALGRIND */
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
ut_a(buf_flush_validate_skip(buf_pool));
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
buf_flush_list_mutex_exit(buf_pool);
}
/********************************************************************//**
Inserts a modified block into the flush list in the right sorted position.
This function is used by recovery, because there the modifications do not
necessarily come in the order of lsn's. */
void
buf_flush_insert_sorted_into_flush_list(
/*====================================*/
buf_pool_t* buf_pool, /*!< in: buffer pool instance */
buf_block_t* block, /*!< in/out: block which is modified */
lsn_t lsn) /*!< in: oldest modification */
{
buf_page_t* prev_b;
buf_page_t* b;
ut_ad(srv_shutdown_state != SRV_SHUTDOWN_FLUSH_PHASE);
ut_ad(log_flush_order_mutex_own());
ut_ad(mutex_own(buf_page_get_mutex(&block->page)));
ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
buf_flush_list_mutex_enter(buf_pool);
/* The field in_LRU_list is protected by buf_pool->LRU_list_mutex,
which we are not holding. However, while a block is in the flush
list, it is dirty and cannot be discarded, not from the
page_hash or from the LRU list. At most, the uncompressed
page frame of a compressed block may be discarded or created
(copying the block->page to or from a buf_page_t that is
dynamically allocated from buf_buddy_alloc()). Because those
transitions hold block->mutex and the flush list mutex (via
buf_flush_relocate_on_flush_list()), there is no possibility
of a race condition in the assertions below. */
ut_ad(block->page.in_LRU_list);
ut_ad(block->page.in_page_hash);
/* buf_buddy_block_register() will take a block in the
BUF_BLOCK_MEMORY state, not a file page. */
ut_ad(!block->page.in_zip_hash);
ut_ad(!block->page.in_flush_list);
ut_d(block->page.in_flush_list = TRUE);
block->page.oldest_modification = lsn;
#ifdef UNIV_DEBUG_VALGRIND
void* p;
if (block->page.size.is_compressed()) {
p = block->page.zip.data;
} else {
p = block->frame;
}
UNIV_MEM_ASSERT_RW(p, block->page.size.physical());
#endif /* UNIV_DEBUG_VALGRIND */
prev_b = NULL;
/* For the most part when this function is called the flush_rbt
should not be NULL. In a very rare boundary case it is possible
that the flush_rbt has already been freed by the recovery thread
before the last page was hooked up in the flush_list by the
io-handler thread. In that case we'll just do a simple
linear search in the else block. */
if (buf_pool->flush_rbt != NULL) {
prev_b = buf_flush_insert_in_flush_rbt(&block->page);
} else {
b = UT_LIST_GET_FIRST(buf_pool->flush_list);
while (b != NULL && b->oldest_modification
> block->page.oldest_modification) {
ut_ad(b->in_flush_list);
prev_b = b;
b = UT_LIST_GET_NEXT(list, b);
}
}
if (prev_b == NULL) {
UT_LIST_ADD_FIRST(buf_pool->flush_list, &block->page);
} else {
UT_LIST_INSERT_AFTER(buf_pool->flush_list, prev_b, &block->page);
}
if (buf_pool->oldest_hp.get() != NULL) {
/* clear oldest_hp */
buf_pool->oldest_hp.set(NULL);
}
incr_flush_list_size_in_bytes(block, buf_pool);
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
ut_a(buf_flush_validate_low(buf_pool));
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
buf_flush_list_mutex_exit(buf_pool);
}
/********************************************************************//**
Returns TRUE if the file page block is immediately suitable for replacement,
i.e., the transition FILE_PAGE => NOT_USED allowed. The caller must hold the
LRU list and block mutexes.
@return TRUE if can replace immediately */
ibool
buf_flush_ready_for_replace(
/*========================*/
buf_page_t* bpage) /*!< in: buffer control block, must be
buf_page_in_file(bpage) and in the LRU list */
{
#ifdef UNIV_DEBUG
buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
#endif /* UNIV_DEBUG */
ut_ad(mutex_own(buf_page_get_mutex(bpage)));
ut_ad(bpage->in_LRU_list);
if (buf_page_in_file(bpage)) {
return(bpage->oldest_modification == 0
&& bpage->buf_fix_count == 0
&& buf_page_get_io_fix(bpage) == BUF_IO_NONE);
}
ib::fatal() << "Buffer block " << bpage << " state " << bpage->state
<< " in the LRU list!";
return(FALSE);
}
/********************************************************************//**
Returns true if the block is modified and ready for flushing.
@return true if can flush immediately */
bool
buf_flush_ready_for_flush(
/*======================*/
buf_page_t* bpage, /*!< in: buffer control block, must be
buf_page_in_file(bpage) */
buf_flush_t flush_type)/*!< in: type of flush */
{
#ifdef UNIV_DEBUG
buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
#endif
ut_a(buf_page_in_file(bpage)
|| (buf_page_get_state(bpage) == BUF_BLOCK_REMOVE_HASH
#ifdef UNIV_DEBUG
&& !mutex_own(&buf_pool->LRU_list_mutex)
#endif
));
ut_ad(mutex_own(buf_page_get_mutex(bpage))
|| (flush_type == BUF_FLUSH_LIST
&& buf_flush_list_mutex_own(buf_pool)));
ut_ad(flush_type < BUF_FLUSH_N_TYPES);
if (bpage->oldest_modification == 0
|| buf_page_get_io_fix_unlocked(bpage) != BUF_IO_NONE) {
return(false);
}
ut_ad(bpage->in_flush_list);
switch (flush_type) {
case BUF_FLUSH_LIST:
return(buf_page_get_state(bpage) != BUF_BLOCK_REMOVE_HASH);
case BUF_FLUSH_LRU:
case BUF_FLUSH_SINGLE_PAGE:
return(true);
case BUF_FLUSH_N_TYPES:
break;
}
ut_error;
return(false);
}
/********************************************************************//**
Remove a block from the flush list of modified blocks. */
void
buf_flush_remove(
/*=============*/
buf_page_t* bpage) /*!< in: pointer to the block in question */
{
buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
ut_ad(mutex_own(buf_page_get_mutex(bpage)));
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
ut_ad(buf_page_get_state(bpage) != BUF_BLOCK_ZIP_DIRTY
|| mutex_own(&buf_pool->LRU_list_mutex));
#endif
ut_ad(bpage->in_flush_list);
buf_flush_list_mutex_enter(buf_pool);
/* Important that we adjust the hazard pointer before removing
the bpage from flush list. */
buf_pool->flush_hp.adjust(bpage);
buf_pool->oldest_hp.adjust(bpage);
switch (buf_page_get_state(bpage)) {
case BUF_BLOCK_POOL_WATCH:
case BUF_BLOCK_ZIP_PAGE:
/* Clean compressed pages should not be on the flush list */
case BUF_BLOCK_NOT_USED:
case BUF_BLOCK_READY_FOR_USE:
case BUF_BLOCK_MEMORY:
case BUF_BLOCK_REMOVE_HASH:
ut_error;
return;
case BUF_BLOCK_ZIP_DIRTY:
buf_page_set_state(bpage, BUF_BLOCK_ZIP_PAGE);
UT_LIST_REMOVE(buf_pool->flush_list, bpage);
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
buf_LRU_insert_zip_clean(bpage);
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
break;
case BUF_BLOCK_FILE_PAGE:
UT_LIST_REMOVE(buf_pool->flush_list, bpage);
break;
}
/* If the flush_rbt is active then delete from there as well. */
if (buf_pool->flush_rbt != NULL) {
buf_flush_delete_from_flush_rbt(bpage);
}
/* Must be done after we have removed it from the flush_rbt
because we assert on in_flush_list in comparison function. */
ut_d(bpage->in_flush_list = FALSE);
buf_pool->stat.flush_list_bytes -= bpage->size.physical();
bpage->oldest_modification = 0;
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
ut_a(buf_flush_validate_skip(buf_pool));
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
/* If there is an observer that want to know if the asynchronous
flushing was done then notify it. */
if (bpage->flush_observer != NULL) {
bpage->flush_observer->notify_remove(buf_pool, bpage);
bpage->flush_observer = NULL;
}
buf_flush_list_mutex_exit(buf_pool);
}
/*******************************************************************//**
Relocates a buffer control block on the flush_list.
Note that it is assumed that the contents of bpage have already been
copied to dpage.
IMPORTANT: When this function is called bpage and dpage are not
exact copies of each other. For example, they both will have different
::state. Also the ::list pointers in dpage may be stale. We need to
use the current list node (bpage) to do the list manipulation because
the list pointers could have changed between the time that we copied
the contents of bpage to the dpage and the flush list manipulation
below. */
void
buf_flush_relocate_on_flush_list(
/*=============================*/
buf_page_t* bpage, /*!< in/out: control block being moved */
buf_page_t* dpage) /*!< in/out: destination block */
{
buf_page_t* prev;
buf_page_t* prev_b = NULL;
buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
/* Must reside in the same buffer pool. */
ut_ad(buf_pool == buf_pool_from_bpage(dpage));
ut_ad(mutex_own(buf_page_get_mutex(bpage)));
buf_flush_list_mutex_enter(buf_pool);
ut_ad(bpage->in_flush_list);
ut_ad(dpage->in_flush_list);
/* If recovery is active we must swap the control blocks in
the flush_rbt as well. */
if (buf_pool->flush_rbt != NULL) {
buf_flush_delete_from_flush_rbt(bpage);
prev_b = buf_flush_insert_in_flush_rbt(dpage);
}
/* Important that we adjust the hazard pointer before removing
the bpage from the flush list. */
buf_pool->flush_hp.move(bpage, dpage);
buf_pool->oldest_hp.move(bpage, dpage);
/* Must be done after we have removed it from the flush_rbt
because we assert on in_flush_list in comparison function. */
ut_d(bpage->in_flush_list = FALSE);
prev = UT_LIST_GET_PREV(list, bpage);
UT_LIST_REMOVE(buf_pool->flush_list, bpage);
if (prev) {
ut_ad(prev->in_flush_list);
UT_LIST_INSERT_AFTER( buf_pool->flush_list, prev, dpage);
} else {
UT_LIST_ADD_FIRST(buf_pool->flush_list, dpage);
}
/* Just an extra check. Previous in flush_list
should be the same control block as in flush_rbt. */
ut_a(buf_pool->flush_rbt == NULL || prev_b == prev);
#if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
ut_a(buf_flush_validate_low(buf_pool));
#endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
buf_flush_list_mutex_exit(buf_pool);
}
/********************************************************************//**
Updates the flush system data structures when a write is completed. */
void
buf_flush_write_complete(
/*=====================*/
buf_page_t* bpage) /*!< in: pointer to the block in question */
{
buf_flush_t flush_type = buf_page_get_flush_type(bpage);
buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
mutex_enter(&buf_pool->flush_state_mutex);
buf_flush_remove(bpage);
buf_page_set_io_fix(bpage, BUF_IO_NONE);
buf_pool->n_flush[flush_type]--;
ut_ad(buf_pool->n_flush[flush_type] != ULINT_MAX);
if (buf_pool->n_flush[flush_type] == 0
&& buf_pool->init_flush[flush_type] == FALSE) {
/* The running flush batch has ended */
os_event_set(buf_pool->no_flush[flush_type]);
}
mutex_exit(&buf_pool->flush_state_mutex);
buf_dblwr_update(bpage, flush_type);
}
#endif /* !UNIV_HOTBACKUP */
/** Calculate the checksum of a page from compressed table and update
the page.
@param[in,out] page page to update
@param[in] size compressed page size
@param[in] lsn LSN to stamp on the page */
void
buf_flush_update_zip_checksum(
buf_frame_t* page,
ulint size,
lsn_t lsn)
{
ut_a(size > 0);
const uint32_t checksum = page_zip_calc_checksum(
page, size,
static_cast<srv_checksum_algorithm_t>(srv_checksum_algorithm));
mach_write_to_8(page + FIL_PAGE_LSN, lsn);
mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM, checksum);
}
/** Initialize a page for writing to the tablespace.
@param[in] block buffer block; NULL if bypassing the buffer pool
@param[in,out] page page frame
@param[in,out] page_zip_ compressed page, or NULL if uncompressed
@param[in] newest_lsn newest modification LSN to the page
@param[in] skip_checksum whether to disable the page checksum */
void
buf_flush_init_for_writing(
const buf_block_t* block,
byte* page,
void* page_zip_,
lsn_t newest_lsn,
bool skip_checksum)
{
ib_uint32_t checksum = BUF_NO_CHECKSUM_MAGIC;
ut_ad(block == NULL || block->frame == page);
ut_ad(block == NULL || page_zip_ == NULL
|| &block->page.zip == page_zip_);
ut_ad(page);
if (page_zip_) {
page_zip_des_t* page_zip;
ulint size;
page_zip = static_cast<page_zip_des_t*>(page_zip_);
size = page_zip_get_size(page_zip);
ut_ad(size);
ut_ad(ut_is_2pow(size));
ut_ad(size <= UNIV_ZIP_SIZE_MAX);
switch (fil_page_get_type(page)) {
case FIL_PAGE_TYPE_ALLOCATED:
case FIL_PAGE_INODE:
case FIL_PAGE_IBUF_BITMAP:
case FIL_PAGE_TYPE_FSP_HDR:
case FIL_PAGE_TYPE_XDES:
/* These are essentially uncompressed pages. */
memcpy(page_zip->data, page, size);
/* fall through */
case FIL_PAGE_TYPE_ZBLOB:
case FIL_PAGE_TYPE_ZBLOB2:
case FIL_PAGE_INDEX:
case FIL_PAGE_RTREE:
buf_flush_update_zip_checksum(
page_zip->data, size, newest_lsn);
return;
}
ib::error() << "The compressed page to be written"
" seems corrupt:";
ut_print_buf(stderr, page, size);
fputs("\nInnoDB: Possibly older version of the page:", stderr);
ut_print_buf(stderr, page_zip->data, size);
putc('\n', stderr);
ut_error;
}
/* Write the newest modification lsn to the page header and trailer */
mach_write_to_8(page + FIL_PAGE_LSN, newest_lsn);
mach_write_to_8(page + UNIV_PAGE_SIZE - FIL_PAGE_END_LSN_OLD_CHKSUM,
newest_lsn);
if (skip_checksum) {
mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM, checksum);
} else {
if (block != NULL && UNIV_PAGE_SIZE == 16384) {
/* The page type could be garbage in old files
created before MySQL 5.5. Such files always
had a page size of 16 kilobytes. */
ulint page_type = fil_page_get_type(page);
ulint reset_type = page_type;
switch (block->page.id.page_no() % 16384) {
case 0:
reset_type = block->page.id.page_no() == 0
? FIL_PAGE_TYPE_FSP_HDR
: FIL_PAGE_TYPE_XDES;
break;
case 1:
reset_type = FIL_PAGE_IBUF_BITMAP;
break;
default:
switch (page_type) {
case FIL_PAGE_INDEX:
case FIL_PAGE_RTREE:
case FIL_PAGE_UNDO_LOG:
case FIL_PAGE_INODE:
case FIL_PAGE_IBUF_FREE_LIST:
case FIL_PAGE_TYPE_ALLOCATED:
case FIL_PAGE_TYPE_SYS:
case FIL_PAGE_TYPE_TRX_SYS:
case FIL_PAGE_TYPE_BLOB:
case FIL_PAGE_TYPE_ZBLOB:
case FIL_PAGE_TYPE_ZBLOB2:
break;
case FIL_PAGE_TYPE_FSP_HDR:
case FIL_PAGE_TYPE_XDES:
case FIL_PAGE_IBUF_BITMAP:
/* These pages should have
predetermined page numbers
(see above). */
default:
reset_type = FIL_PAGE_TYPE_UNKNOWN;
break;
}
}
if (UNIV_UNLIKELY(page_type != reset_type)) {
ib::info()
<< "Resetting invalid page "
<< block->page.id << " type "
<< page_type << " to "
<< reset_type << " when flushing.";
fil_page_set_type(page, reset_type);
}
}
switch ((srv_checksum_algorithm_t) srv_checksum_algorithm) {
case SRV_CHECKSUM_ALGORITHM_CRC32:
case SRV_CHECKSUM_ALGORITHM_STRICT_CRC32:
checksum = buf_calc_page_crc32(page);
mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM,
checksum);
break;
case SRV_CHECKSUM_ALGORITHM_INNODB:
case SRV_CHECKSUM_ALGORITHM_STRICT_INNODB:
checksum = (ib_uint32_t) buf_calc_page_new_checksum(
page);
mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM,
checksum);
checksum = (ib_uint32_t) buf_calc_page_old_checksum(
page);
break;
case SRV_CHECKSUM_ALGORITHM_NONE:
case SRV_CHECKSUM_ALGORITHM_STRICT_NONE:
mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM,
checksum);
break;
/* no default so the compiler will emit a warning if
new enum is added and not handled here */
}
}
/* With the InnoDB checksum, we overwrite the first 4 bytes of
the end lsn field to store the old formula checksum. Since it
depends also on the field FIL_PAGE_SPACE_OR_CHKSUM, it has to
be calculated after storing the new formula checksum.
In other cases we write the same value to both fields.
If CRC32 is used then it is faster to use that checksum
(calculated above) instead of calculating another one.