forked from yuanrongxi/innodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsp0fsp.cc
2203 lines (1704 loc) · 64.3 KB
/
fsp0fsp.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
#include "fsp0fsp.h"
#include "buf0buf.h"
#include "fil0fil.h"
#include "sync0sync.h"
#include "mtr0log.h"
#include "fut0fut.h"
#include "ut0byte.h"
#include "srv0srv.h"
#include "page0types.h"
#include "ibuf0ibuf.h"
#include "btr0btr.h"
#include "btr0sea.h"
#include "dict0boot.h"
#include "dict0mem.h"
#include "log0log.h"
typedef byte fsp_header_t;
typedef byte fseg_inode_t;
typedef byte xdes_t;
#define FSP_HEADER_OFFSET FIL_PAGE_DATA
#define FSP_NOT_USED 0
#define FSP_SIZE 8 /*当前space最大可容纳的page数,文件扩大时才会改变这个值*/
#define FSP_FREE_LIMIT 12 /*当前space已经分配初始化的page数*/
#define FSP_LOWEST_NO_WRITE 16 /*好像没有什么用处*/
#define FSP_FRAG_N_USED 20 /*FSP_FREE_FRAG列表中已经被使用的page数*/
#define FSP_FREE 24 /*space中可用的extent对象列表*/
/*FLST_BASE_NODE_SIZE = 16*/
#define FSP_FREE_FRAG (24 + FLST_BASE_NODE_SIZE) /*space当前空闲的extent列表*/
#define FSP_FULL_FRAG (24 + 2 * FLST_BASE_NODE_SIZE) /*space当前完全占用的extent列表,里面么有空闲页*/
#define FSP_SEG_ID (24 + 3 * FLST_BASE_NODE_SIZE) /*space中下一个空闲的segment的ID*/
#define FSP_SEG_INODES_FULL (32 + 3 * FLST_BASE_NODE_SIZE) /*space当前完全占满的segment inodes pages*/
#define FSP_SEG_INODES_FREE (32 + 4 * FLST_BASE_NODE_SIZE) /*space当前空闲的segment inode pages*/
/*file space header size*/
#define FSP_HEADER_SIZE (32 + 5 * FLST_BASE_NODE_SIZE) /*space header的头长度*/
#define FSP_FREE_ADD 4
/*segment inode*/
#define FSEG_INODE_PAGE_NODE FSEG_PAGE_DATA
/*FLST_NODE_SIZE = 12*/
#define FSEG_ARR_OFFSET (FSEG_PAGE_DATA + FLST_NODE_SIZE)
#define FSEG_ID 0 /*8字节, segment id*/
#define FSEG_NOT_FULL_N_USED 8 /*FSEG_NOT_FULL列表中的page数*/
#define FSEG_FREE 12 /*segment空闲的extent列表*/
#define FSEG_NOT_FULL (12 + FLST_BASE_NODE_SIZE) /*没有完全占满的extent的队列*/
#define FSEG_FULL (12 + 2 * FLST_BASE_NODE_SIZE) /*完全占满的extent的队列*/
#define FSEG_MAGIC_N (12 + 3 * FLST_BASE_NODE_SIZE) /*segment的魔法字,用于校验*/
#define FSEG_FRAG_ARR (16 + 3 * FLST_BASE_NODE_SIZE) /*segment frag addr偏移量*/
#define FSEG_FRAG_ARR_N_SLOTS (FSP_EXTENT_SIZE / 2) /*fragment page(应该是extent头页)的slots,slot中存储的是page no*/
#define FSEG_FRAG_SLOT_SIZE 4
/*segment inode的长度,segment的最后32个page no slot数组*/
#define FSEG_INODE_SIZE (16 + 3 * FLST_BASE_NODE_SIZE + FSEG_FRAG_ARR_N_SLOTS * FSEG_FRAG_SLOT_SIZE)
/*单个页可以存放segment inode对象的个数*/
#define FSP_SEG_INODES_PER_PAGE ((UNIV_PAGE_SIZE - FSEG_ARR_OFFSET - 10) / FSEG_INODE_SIZE)
/*segment 魔法字的值*/
#define FSEG_MAGIC_N_VALUE 97937874
#define FSEG_FILLFACTOR 8 /*segment的页分配因子*/
#define FSEG_FRAG_LIMIT FSEG_FRAG_ARR_N_SLOTS /*segment inode中extent头页的数量上限*/
#define FSEG_FREE_LIST_LIMIT 40 /*segment最大的extent数量*/
#define FSEG_FREE_LIST_MAX_LEN 4
/*区(extent)*/
#define XDES_ID 0 /*extent id,8字节*/
#define XDES_FLST_NODE 8 /*一个定位extent page的ffil_addr_t对,prev和next*/
#define XDES_STATE (FLST_NODE_SIZE + 8) /*extent的状态,XDES_FREE、XDES_FREE_FRAG、XDES_FULL_FRAG、XDES_FSEG*/
#define XDES_BITMAP (FLST_NODE_SIZE + 12) /*页状态位图*/
#define XDES_BITS_PER_PAGE 2
#define XDES_FREE_BIT 0
#define XDES_CLEAN_BIT 1
#define XDES_FREE 1
#define XDES_FREE_FRAG 2
#define XDES_FULL_FRAG 3
#define XDES_FSEG 4
#define XDES_SIZE (XDES_BITMAP + (FSP_EXTENT_SIZE * XDES_BITS_PER_PAGE + 7) / 8) /*40*/
#define XDES_ARR_OFFSET (FSP_HEADER_OFFSET + FSP_HEADER_SIZE) /*150*/
/********************************************************************/
/*释放一个extent到space的free list中*/
static void fsp_free_extent(ulint space, ulint page, mtr_t* mtr);
/*释放segment中的一个extent到space free list中*/
static void fseg_free_extent(fseg_inode_t* seg_inode, ulint space, ulint page, mtr_t* mtr);
/*计算fseg_inode当中所有的page数和已经使用的page数量*/
static ulint fseg_n_reserved_pages_low(fseg_inode_t* header, ulint* used, mtr_t* mtr);
/*标记一个page已经被占用了*/
static void fseg_mark_page_used(fseg_inode_t* seg_inde, ulint space, ulint page, mtr_t* mtr);
/*获得fseg node中第一个extent*/
static xdes_t* fseg_get_first_extent(fseg_inode_t* inode, mtr_t* mtr);
static void fsp_fill_free_list(ulint space, fsp_header_t* header, mtr_t* mtr);
static ulint fseg_alloc_free_page_low(ulint space, fseg_inode_t* seg_inode, ulint hint, byte direction, mtr_t* mtr);
/*********************************************************************/
/*获取space header的缓冲区指针*/
UNIV_INLINE fsp_header_t* fsp_get_space_header(ulint space_id, mtr_t* mtr)
{
fsp_header_t* header;
ut_ad(mtr);
/*获得space header缓冲区*/
header = buf_page_get(space_id, 0, RW_X_LATCH, mtr) + FSP_HEADER_OFFSET;
buf_page_dbg_add_level(header, SYNC_FSP_PAGE);
return header;
}
/*判断xdes指定的bitmap上的值是否是1*/
UNIV_INLINE ibool xdes_get_bit(xdes_t* descr, ulint bit, ulint offset, mtr_t* mtr)
{
ulint index;
ulint byte_index;
ulint bit_index;
/*mtr一定持有X LATCH*/
ut_ad(mtr_memo_contains(mtr, buf_block_align(descr), MTR_MEMO_PAGE_X_FIX));
ut_ad((bit == XDES_FREE_BIT) || (bit == XDES_CLEAN_BIT));
ut_ad(offset < FSP_EXTENT_SIZE);
/*每个页占2个bit,第一个bit表示free状态,第二个bit表示clean状态*/
index = bit + XDES_BITS_PER_PAGE * offset;
byte_index = index / 8;
bit_index = index % 8;
/*读取xdes中对应的bit并判断是否为1*/
return ut_bit_get_nth(mtr_read_ulint(descr + XDES_BITMAP + byte_index, MLOG_1BYTE, mtr), bit_index);
}
UNIV_INLINE void xdes_set_bit(xdes_t* descr, ulint bit, ulint offset, ibool val, mtr_t* mtr)
{
ulint index;
ulint byte_index;
ulint bit_index;
ulint descr_byte;
ut_ad(mtr_memo_contains(mtr, buf_block_align(descr), MTR_MEMO_PAGE_X_FIX));
ut_ad((bit == XDES_FREE_BIT) || (bit == XDES_CLEAN_BIT));
ut_ad(offset < FSP_EXTENT_SIZE);
index = bit + XDES_BITS_PER_PAGE * offset;
byte_index = index / 8;
bit_index = index % 8;
descr_byte = mtr_read_ulint(descr + XDES_BITMAP + byte_index, MLOG_1BYTE, mtr);
descr_byte = ut_bit_set_nth(descr_byte, bit_index, val);
mlog_write_ulint(descr + XDES_BITMAP + byte_index, descr_byte, MLOG_1BYTE, mtr);
}
/*从hint出由低到高进行查找*/
UNIV_INLINE ulint xdes_find_bit(xdes_t* descr, ulint bit,ibool val, ulint hint, mtr_t* mtr)
{
ulint i;
ut_ad(descr && mtr);
ut_ad(val <= TRUE);
ut_ad(hint < FSP_EXTENT_SIZE);
ut_ad(mtr_memo_contains(mtr, buf_block_align(descr), MTR_MEMO_PAGE_X_FIX));
/*hint是bit推断的位置,可能就bit存在的位置*/
for(i = hint; i < FSP_EXTENT_SIZE; i ++){
if(val == xdes_get_bit(descr, bit, i, mtr))
return i;
}
for(i = 0; i < hint; i ++){
if(val == xdes_get_bit(descr, bit, i, mtr))
return i;
}
return ULINT_UNDEFINED;
}
/*从hint出由高到低进行查找*/
UNIV_INLINE ulint xdes_find_bit_downward(xdes_t* descr, ulint bit, ibool val, ulint hint, mtr_t* mtr)
{
ulint i;
ut_ad(descr && mtr);
ut_ad(val <= TRUE);
ut_ad(hint < FSP_EXTENT_SIZE);
ut_ad(mtr_memo_contains(mtr, buf_block_align(descr), MTR_MEMO_PAGE_X_FIX));
for(i = hint + 1; i > 0; i --){
if(val == xdes_get_bit(descr, bit, i - 1, mtr))
return i - 1;
}
for(i = FSP_EXTENT_SIZE - 1; i > hint; i --){
if(val = xdes_get_bit(descr, bit, i, mtr))
return i;
}
return ULINT_UNDEFINED;
}
/*返回已经xdes descr中已使用的page数量,通过free位来判断*/
UNIV_INLINE ulint xdes_get_n_used(xdes_t* descr, mtr_t* mtr)
{
ulint i;
ulint count = 0;
ut_ad(descr && mtr);
ut_ad(mtr_memo_contains(mtr, buf_block_align(descr), MTR_MEMO_PAGE_X_FIX));
for(i = 0; i < FSP_EXTENT_SIZE; i ++){
if(xdes_get_bit(descr, XDES_FREE_BIT, i, mtr) == FALSE) /*FASLSE表示被占用了*/
count ++;
}
return count;
}
/*判断一个区是否空闲*/
UNIV_INLINE ibool xdes_is_free(xdes_t* descr, mtr_t* mtr)
{
if(0 == xdes_get_n_used(descr, mtr))
return TRUE;
else
return FALSE;
}
/*判断一个区是否满了*/
UNIV_INLINE ibool xdes_is_full(xdes_t* descr, mtr_t* mtr)
{
if(xdes_get_n_used(descr, mtr) == FSP_EXTENT_SIZE)
return TRUE;
else
return FALSE;
}
/*设置extent的状态*/
UNIV_INLINE void xdes_set_state(xdes_t* descr, ulint state, mtr_t* mtr)
{
ut_ad(descr && mtr);
ut_ad(state >= XDES_FREE);
ut_ad(state <= XDES_FSEG);
ut_ad(mtr_memo_contains(mtr, buf_block_align(descr), MTR_MEMO_PAGE_X_FIX));
mlog_write_ulint(descr + XDES_STATE, state, MLOG_4BYTES, mtr);
}
UNIV_INLINE ulint xdes_get_state(xdes_t* descr, mtr_t* mtr)
{
ut_ad(descr && mtr);
ut_ad(mtr_memo_contains(mtr, buf_block_align(descr), MTR_MEMO_PAGE_X_FIX));
return mtr_read_ulint(descr + XDES_STATE, MLOG_4BYTES, mtr);
}
UNIV_INLINE void xdes_init(xdes_t* descr, mtr_t* mtr)
{
ulint i;
ut_ad(descr && mtr);
ut_ad(mtr_memo_contains(mtr, buf_block_align(descr), MTR_MEMO_PAGE_X_FIX));
ut_ad((XDES_SIZE - XDES_BITMAP) % 4 == 0);
/*初始化bitmap,全部page bit为1*/
for(i = XDES_BITMAP, i < XDES_SIZE; i += 4){
mlog_write_ulint(descr + i, 0xFFFFFFFF, MLOG_4BYTES, mtr);
}
/*设置extent free状态*/
xdes_set_state(descr, XDES_FREE, mtr);
}
/*offset按page size对齐*/
UNIV_INLINE ulint xdes_calc_descriptor_page(ulint offset)
{
ut_ad(UNIV_PAGE_SIZE > XDES_ARR_OFFSET + (XDES_DESCRIBED_PER_PAGE / FSP_EXTENT_SIZE) * XDES_SIZE);
return ut_2pow_round(offset, XDES_DESCRIBED_PER_PAGE);
}
/*计算offset对应page 的extent描述索引*/
UNIV_INLINE ulint xdes_calc_descriptor_index(ulint offset)
{
ut_2pow_remainder(offset, XDES_DESCRIBED_PER_PAGE) / FSP_EXTENT_SIZE;
}
UNIV_INLINE xdes_t* xdes_get_descriptor_with_space_hdr(fsp_header_t* sp_header, ulint space, ulint offset, mtr_t* mtr)
{
ulint limit;
ulint size;
ulint descr_page_no;
ulint descr_page;
ut_ad(mtr);
ut_ad(mtr_memo_contains(mtr, fil_space_get_latch(space), MTR_MEMO_X_LOCK));
limit = mtr_read_ulint(sp_header + FSP_FREE_LIMIT, MLOG_4BYTES, mtr);
size = mtr_read_ulint(sp_header + FSP_SIZE, MLOG_4BYTES, mtr);
if(offset >= size || offset > limit)
return NULL;
/*已经到了limit上限,进行开辟新的extent*/
if(offset == limit)
fsp_fill_free_list(space, sp_header, mtr);
/*计算对应的xdesc page no*/
descr_page_no = xdes_calc_descriptor_page(offset);
if(descr_page_no == 0)
descr_page = buf_frame_align(sp_header);
else{
descr_page = buf_page_get(space, descr_page_no, RW_X_LATCH, mtr); /*获得descr_page_no对应的指针地址*/
buf_page_dbg_add_level(descr_page, SYNC_FSP_PAGE);
}
/*返回offset对应的xdes descr的指针地址*/
return (descr_page + XDES_ARR_OFFSET + XDES_SIZE * xdes_calc_descriptor_index(offset));
}
/*通过offset获取space对应的extent描述指针*/
static xdes_t* xdes_get_descriptor(ulint space, ulint offset, mtr_t* mtr)
{
fsp_header_t* sp_header;
sp_header = FSP_HEADER_OFFSET + buf_page_get(space, 0, RW_X_LATCH, mtr);
buf_page_dbg_add_level(sp_header, SYNC_FSP_PAGE);
return xdes_get_descriptor_with_space_hdr(sp_header, space, offset, mtr);
}
/*通过fil_addr获得extent指针*/
UNIV_INLINE xdes_t* xdes_lst_get_descriptor(ulint space, fil_addr_t lst_node, mtr_t* mtr)
{
xdes_t* descr;
ut_ad(mtr);
ut_ad(mtr_memo_contains(mtr, fil_space_get_latch(space), MTR_MEMO_X_LOCK));
descr = fut_get_ptr(space, lst_node, RW_X_LATCH, mtr) - XDES_FLST_NODE;
return descr;
}
/*获得descr对应链表中的下一个xdes entry*/
UNIV_INLINE xdes_t* xdes_ls_get_next(xdes_t* descr, mtr_t* mtr)
{
ulint space;
ut_ad(mtr & descr);
space = buf_frame_get_space_id(descr);
return xdes_lst_get_descriptor(space, flst_get_next_addr(descr + XDES_FLST_NODE, mtr), mtr);
}
/*descr对应物理地址的偏移量,与xdes_get_descriptor函数正好相反,*/
UNIV_INLINE ulint xdes_get_offset(xdes_t* descr)
{
ut_ad(descr);
return buf_frame_get_page_no(descr) + ((descr - buf_frame_align(descr) - XDES_ARR_OFFSET) / XDES_SIZE) * FSP_EXTENT_SIZE;
}
/*初始化一个page页*/
static void fsp_init_file_page_low(byte* ptr)
{
page_t* page;
page = buf_frame_align(ptr);
/*初始化page trailer lsn*/
mach_write_to_8(page + UNIV_PAGE_SIZE - FIL_PAGE_END_LSN, ut_dulint_zero);
/*初始化page lsn*/
mach_write_to_8(page + FIL_PAGE_LSN, ut_dulint_zero);
}
void fsp_init_file_page(page_t* page, mtr_t* mtr)
{
/*初始化一个页*/
fsp_init_file_page_low(page);
/*记录一个初始化页的mtr日志*/
mlog_write_initial_log_record(page, MLOG_INIT_FILE_PAGE, mtr);
}
/*解析并执行一个初始化页的redo log,在重做日志时调用*/
byte* fsp_parse_init_file_page(byte* ptr, byte* end_ptr, page_t* page)
{
ut_ad(ptr && end_ptr);
if(page)
fsp_init_file_page_low(page);
return ptr;
}
void fsp_init()
{
}
/*初始化space header*/
void fsp_header_init(ulint space, ulint size, mtr_t* mtr)
{
fsp_header_t* header;
page_t* page;
ut_ad(mtr);
/*获得space mtr的x-latch控制权*/
mtr_x_lock(fil_space_get_latch(space), mtr);
/*创建一个初始化页*/
page = buf_page_create(space, 0, mtr);
buf_page_dbg_add_level(page, SYNC_FSP_PAGE);
/*获得page的X_LATCH*/
buf_page_get(space, 0, RW_X_LATCH, mtr);
buf_page_dbg_add_level(page, SYNC_FSP_PAGE);
/*对页进行初始化*/
fsp_init_file_page(page, mtr);
/*获得space header的指针*/
header = FSP_HEADER_OFFSET + page;
/*对头信息进行初始化*/
mlog_write_ulint(header + FSP_SIZE, size, MLOG_4BYTES, mtr);
mlog_write_ulint(header + FSP_FREE_LIMIT, 0, MLOG_4BYTES, mtr);
mlog_write_ulint(header + FSP_LOWEST_NO_WRITE, 0, MLOG_4BYTES, mtr);
mlog_write_ulint(header + FSP_FRAG_N_USED, 0, MLOG_4BYTES, mtr);
/*初始化各类磁盘列表*/
flst_init(header + FSP_FREE, mtr);
flst_init(header + FSP_FREE_FRAG, mtr);
flst_init(header + FSP_FULL_FRAG, mtr);
flst_init(header + FSP_SEG_INODES_FULL, mtr);
flst_init(header + FSP_SEG_INODES_FREE, mtr);
/*赋值seg_id*/
mlog_write_dulint(header + FSP_SEG_ID, ut_dulint_create(0, 1), MLOG_8BYTES, mtr);
/*开辟新的一些可用的extent*/
fsp_fill_free_list(space, header, mtr);
/*建立一个btree*/
btr_create(DICT_CLUSTERED | DICT_UNIVERSAL | DICT_IBUF, space, ut_dulint_add(DICT_IBUF_ID_MIN, space), mtr);
}
/*增加space的长度*/
void fsp_header_inc_size(ulint space, ulint size_inc, mtr_t* mtr)
{
fsp_header_t* header;
ulint size;
ut_ad(mtr);
mtr_x_lock(fil_space_get_latch(space), mtr);
header = fsp_get_space_header(space, mtr);
size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);
mlog_write_ulint(header + FSP_SIZE, size + size_inc, MLOG_4BYTES, mtr);
}
ulint fsp_header_get_free_limit(ulint space)
{
fsp_header_t* header;
ulint limit;
mtr_t mtr;
ut_a(space == 0);
mtr_start(&mtr);
mtr_x_lock(fil_space_get_latch(space), &mtr);
header = fsp_get_space_header(space, &mtr);
limit = mtr_read_ulint(header + FSP_FREE_LIMIT, MLOG_4BYTES, &mtr);
limit = limit / ((1024 * 1024) / UNIV_PAGE_SIZE);
/*设置log的checkpoint limit*/
log_fsp_current_free_limit_set_and_checkpoint(limit);
mtr_commit(&mtr);
return limit;
}
/*获得表空间的size*/
ulint fsp_header_get_tablespace_size(ulint space)
{
fsp_header_t* header;
ulint size;
mtr_t mtr;
mtr_start(&mtr);
mtr_x_lock(fil_space_get_latch(space), &mtr);
header = fsp_get_space_header(space, mtr);
size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, &mtr);
mtr_commit(&mtr);
return size;
}
/*尝试扩大last data file,加入文件定义的是自动扩大*/
static ibool fsp_try_extend_last_file(ulint* actual_increase, ulint space, fsp_header_t* header, mtr_t* mtr)
{
ulint size;
ulint size_increase;
ibool success;
ut_a(space == 0);
*actual_increase = 0;
/*不是文件自动放大的*/
if(!srv_auto_extend_last_data_file)
return FALSE;
/*确定扩大的大小*/
size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);
if(srv_last_file_size_max != 0){
if(srv_last_file_size_max < srv_data_file_sizes[srv_n_data_files - 1]){
fprintf(stderr, "InnoDB: Error: Last data file size is %lu, max size allowed %lu\n",
srv_data_file_sizes[srv_n_data_files - 1], srv_last_file_size_max);
}
size_increase = srv_last_file_size_max - srv_data_file_sizes[srv_n_data_files - 1];
if(size_increase > SRV_AUTO_EXTEND_INCREMENT)
size_increase = SRV_AUTO_EXTEND_INCREMENT;
}
else
size_increase = SRV_AUTO_EXTEND_INCREMENT;
if(size_increase == 0)
return TRUE;
/*放大最后data file,扩大部分用0填充数据*/
success = fil_extend_last_data_file(actual_increase, size_increase);
if(success) /*重新设置space size*/
mlog_write_ulint(header + FSP_SIZE, size + *actual_increase, MLOG_4BYTES, mtr);
return TRUE;
}
static void fsp_fill_free_list(ulint space, fsp_header_t* header, mtr_t* mtr)
{
ulint limit;
ulint size;
xdes_t* descr;
ulint count = 0;
ulint frag_n_used;
page_t* descr_page;
page_t* ibuf_page;
ulint actual_increase;
ulint i;
mtr_t ibuf_mtr;
ut_ad(header && mtr);
/*获得space size*/
size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);
/*获得space limit*/
limit = mtr_read_ulint(header + FSP_FREE_LIMIT, MLOG_4BYTES, mtr);
/*判断是否要放大空间*/
if(srv_auto_extend_last_data_file && size < limit + FSP_EXTENT_SIZE * FSP_FREE_ADD){
fsp_try_extend_last_file(&actual_increase, space, header, mtr);
size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);
}
i = limit;
/*一个区包含64页,每次分配4个extent entry,4M的空间*/
while((i + FSP_EXTENT_SIZE <= size) && count < FSP_FREE_ADD){
mlog_write_ulint(header + FSP_FREE_LIMIT, i + FSP_EXTENT_SIZE, MLOG_4BYTES, mtr);
log_fsp_current_free_limit_set_and_checkpoint((i + FSP_EXTENT_SIZE)/ ((1024 * 1024) / UNIV_PAGE_SIZE));
if(0 == i % XDES_DESCRIBED_PER_PAGE){ /*初始化一个extent desc page,*/
if(i > 0){
descr_page = buf_page_create(space, i, mtr);
buf_page_dbg_add_level(descr_page,SYNC_FSP_PAGE);
buf_page_get(space, i, RW_X_LATCH, mtr);
buf_page_dbg_add_level(descr_page,SYNC_FSP_PAGE);
fsp_init_file_page(descr_page, mtr);
}
mtr_start(&ibuf_mtr);
/*进行ibuf bitmap page的初始化,每16K page必须有一个ibuf bitmap page*/
ibuf_page = buf_page_create(space, i + FSP_IBUF_BITMAP_OFFSET, &ibuf_mtr);
buf_page_dbg_add_level(ibuf_page, SYNC_IBUF_BITMAP);
buf_page_get(space, i + FSP_IBUF_BITMAP_OFFSET, RW_X_LATCH, &ibuf_mtr);
buf_page_dbg_add_level(ibuf_page, SYNC_FSP_PAGE);
fsp_init_file_page(ibuf_page, &ibuf_mtr);
/*初始化一个bitmap page*/
ibuf_bitmap_page_init(ibuf_page, &ibuf_mtr);
mtr_commit(&ibuf_mtr);
}
/*获得区描述并初始化区*/
descr = xdes_get_descriptor_with_space_hdr(header, space, i, mtr);
xdes_init(descr, mtr);
ut_ad(XDES_DESCRIBED_PER_PAGE % FSP_EXTENT_SIZE == 0);
/*设置extent entry对应bitmap上的状态*/
if(0 == i % XDES_DESCRIBED_PER_PAGE){
xdes_set_bit(descr, XDES_FREE_BIT, 0, FALSE, mtr);
xdes_set_bit(descr, XDES_FREE_BIT, FSP_IBUF_BITMAP_OFFSET, FALSE, mtr); /*设置ibuf bitmap page对应的xdes free占用的标识*/
/*表明这个extent entry已经有一部分page被使用了*/
xdes_set_state(descr, XDES_FREE_FRAG, mtr);
flst_add_last(header + FSP_FREE_FRAG, descr + XDES_FLST_NODE, mtr);
frag_n_used = mtr_read_ulint(header + FSP_FRAG_N_USED, MLOG_4BYTES, mtr);
/*一个xdes page和一个ibuf bitmap page被占用*/
mlog_write_ulint(header + FSP_FRAG_N_USED, frag_n_used + 2, MLOG_4BYTES, mtr);
}
else{ /*增加空闲的extent entry(xdes_t)*/
flst_add_last(header + FSP_FREE, descr + XDES_FLST_NODE, mtr);
count ++;
}
/*继续下一个extent*/
i += FSP_EXTENT_SIZE;
}
}
/*获得一个新的空闲的extent*/
static xdes_t* fsp_alloc_free_extent(ulint space, ulint hint, mtr_t* mtr)
{
fsp_header_t* header;
fil_addr_t first;
xdes_t* descr;
ut_ad(mtr);
/*获得头信息和头信息中可用的extent*/
header = fsp_get_space_header(space, mtr);
/*hint可能是上次申请的位置,只需要申请后面的就行了,hint不大于limit,获得hint对应xdes desc*/
descr = xdes_get_descriptor_with_space_hdr(header, space, hint, mtr);
if(descr && (xdes_get_state(descr, mtr) == XDES_FREE)){
}
else{
first = flst_get_first(header + FSP_FREE, mtr);
if(fil_addr_is_null(first)){ /*free list是空的,增加新可用的extent*/
fsp_fill_free_list(space, header, mtr);
first = flst_get_first(header + FSP_FREE, mtr);
}
if(fil_addr_is_null(first))
return NULL;
/*获得first对应的xdes的描述*/
descr = xdes_lst_get_descriptor(space, first, mtr);
}
/*从可用的队列中删除掉已经分配的node*/
flst_remove(header + FSP_FREE, descr + XDES_FLST_NODE, mtr);
return descr;
}
static ulint fsp_alloc_free_page(ulint space, ulint hint, mtr_t* mtr)
{
fsp_header_t* header;
fil_addr_t first;
xdes_t* descr;
page_t* page;
ulint free;
ulint frag_n_used;
ulint page_no;
ut_ad(mtr);
header = fsp_get_space_header(space, mtr);
descr = xdes_get_descriptor_with_space_hdr(header, space, hint, mtr);
if(descr && (xdes_get_state(descr, mtr) == XDES_FREE_FRAG)){
}
else{
first = flst_get_first(header + FSP_FREE_FRAG, mtr);
if(fil_addr_is_null(first)){ /*第一个extent不是NULL,在FSP_FREE获取一个新的*/
descr = fsp_alloc_free_extent(space, hint, mtr);
if(descr == NULL)
return FIL_NULL;
xdes_set_state(descr, XDES_FREE_FRAG, mtr);
flst_add_last(header + FSP_FREE_FRAG, descr + XDES_FLST_NODE, mtr);
}
else
descr = xdes_lst_get_descriptor(space, first, mtr);
hint = 0;
}
/*查找可以用的页*/
free = xdes_find_bit(descr, XDES_FREE_BIT, TRUE, hint % FSP_EXTENT_SIZE, mtr);
ut_a(free != NULL);
/*设置bitmap占用标志*/
xdes_set_bit(descr, XDES_FREE_BIT, free, FALSE, mtr);
frag_n_used = mtr_read_ulint(header + FSP_FRAG_N_USED, MLOG_4BYTES, mtr);
frag_n_used ++;
/*修改已经使用的页数量*/
mlog_write_ulint(header + FSP_FRAG_N_USED, frag_n_used, MLOG_4BYTES, mtr);
if(xdes_is_full(descr, mtr)){/*entry entry的页被占满了*/
flst_remove(header + FSP_FREE_FRAG, descr + XDES_FLST_NODE, mtr);
xdes_set_state(descr, XDES_FULL_FRAG, mtr);
flst_add_last(header + FSP_FULL_FRAG, descr + XDES_FLST_NODE, mtr);
mlog_write_ulint(header + FSP_FRAG_N_USED, frag_n_used - FSP_EXTENT_SIZE, MLOG_4BYTES, mtr);
}
page_no = xdes_get_offset(descr) + free;
/*建立并初始化一个页*/
buf_page_create(space, page_no, mtr);
page = buf_page_get(space, page_no, RW_X_LATCH, mtr);
buf_page_dbg_add_level(page, SYNC_FSP_PAGE);
fsp_init_file_page(page, mtr);
return page_no;
}
static void fsp_free_page(ulint space, ulint page, mtr_t* mtr)
{
fsp_header_t* header;
xdes_t* descr;
ulint state;
ulint frag_n_used;
char buf[1000];
ut_ad(mtr);
header = fsp_get_space_header(space, mtr);
/*获得page对应的xdes*/
descr = xdes_get_descriptor_with_space_hdr(header, space, page, mtr);
state = xdes_get_state(descr, mtr);
if(state != XDES_FREE_FRAG && state != XDES_FULL_FRAG){ /*xdes desc的状态出错了*/
fprintf(stderr,"InnoDB: Error: File space extent descriptor of page %lu has state %lu\n", page, state);
ut_sprintf_buf(buf, ((byte*)descr) - 50, 200);
fprintf(stderr, "InnoDB: Dump of descriptor: %s\n", buf);
if(state == XDES_FREE)
return;
ut_a(0);
}
/*bitmap中的标志已经是可用状态*/
if(xdes_get_bit(descr, XDES_FREE_BIT, page % FSP_EXTENT_SIZE, mtr)){
fprintf(stderr, "InnoDB: Error: File space extent descriptor of page %lu says it is free\n", page);
ut_sprintf_buf(buf, ((byte*)descr) - 50, 200);
fprintf(stderr, "InnoDB: Dump of descriptor: %s\n", buf);
return;
}
/*修改xdes desc对应的状态*/
xdes_set_bit(descr, XDES_FREE_BIT, page % FSP_EXTENT_SIZE, TRUE, mtr);
xdes_set_bit(descr, XDES_CLEAN_BIT, page % FSP_EXTENT_SIZE, TRUE, mtr);
frag_n_used = mtr_read_ulint(header + FSP_FRAG_N_USED, MLOG_4BYTES, mtr);
if(state == XDES_FULL_FRAG){
flst_remove(header + FSP_FULL_FRAG, descr + XDES_FLST_NODE, mtr);
xdes_set_state(descr, FSP_FREE_FRAG, mtr);
flst_add_last(header + FSP_FREE_FRAG, descr + XDES_FLST_NODE, mtr);
/*修改当前区占用的页个数,如果一个页释放了在一个满的区里面,那么应该是区最大PAGE数量 -1,有一个extent有full变成frag free*/
mlog_write_ulint(header + FSP_FRAG_N_USED, frag_n_used + FSP_EXTENT_SIZE - 1, MLOG_4BYTES, mtr);
}
else{
ut_a(frag_n_used > 0);
mlog_write_ulint(header + FSP_FRAG_N_USED, frag_n_used - 1, MLOG_4BYTES, mtr);
}
if(xdes_is_free(descr, mtr)){ /*区是完全空闲了,从FSP_FREE_FRAG中删除,放入FSP_FREE当中*/
flst_remove(header + FSP_FREE_FRAG, descr + XDES_FLST_NODE, mtr);
/*释放掉extent,其实就是放入FSP_FREE*/
fsp_free_extent(space, page, mtr);
}
}
static void fsp_free_extent(ulint space, ulint page, mtr_t* mtr)
{
fsp_header_t* header;
xdes_t* descr;
ut_ad(mtr);
header = fsp_get_space_header(space, mtr);
descr = xdes_get_descriptor_with_space_hdr(header, space, page, mtr);
ut_a(xdes_get_state(descr, mtr) != XDES_FREE);
/*清空xdes*/
xdes_init(descr, mtr);
flst_add_last(header + FSP_FREE, descr + XDES_FLST_NODE, mtr);
}
UNIV_INLINE fseg_inode_t* fsp_seg_inode_page_get_nth_inode(page_t* page, ulint i, mtr_t* mtr)
{
ut_ad(i < FSP_SEG_INODES_PER_PAGE);
ut_ad(mtr_memo_contains(mtr, buf_block_align(page), MTR_MEMO_PAGE_X_FIX));
return(page + FSEG_ARR_OFFSET + FSEG_INODE_SIZE * i);
}
static ulint fsp_seg_inode_page_find_used(page_t* page, mtr_t* mtr)
{
ulint i;
fseg_inode_t* inode;
for(i = 0; i < FSP_SEG_INODES_PER_PAGE; i ++){
inode = fsp_seg_inode_page_get_nth_inode(page, i, mtr);
if(ut_dulint_cmp(mach_read_from_8(inode + FSEG_ID), ut_dulint_zero) != 0)
return i;
}
return ULINT_UNDEFINED;
}
static ulint fsp_seg_inode_page_find_free(page_t* page, ulint j, mtr_t* mtr)
{
ulint i;
fseg_inode_t* inode;
for(i = j; i < FSP_SEG_INODES_PER_PAGE; i ++){
inode = fsp_seg_inode_page_get_nth_inode(page, i, mtr);
if(ut_dulint_cmp(mach_read_from_8(inode + FSEG_ID), ut_dulint_zero) == 0)
return i;
}
return ULINT_UNDEFINED;
}
/*分配一个空闲的inode页*/
static bool fsp_alloc_seg_inode_page(fsp_header_t* space_header, mtr_t* mtr)
{
fseg_inode_t* inode;
page_t* page;
ulint page_no;
ulint space;
ulint i;
space = buf_frame_get_sapce_id(space_header);
page_no = fsp_alloc_free_page(space, 0, mtr); /*获得一个空闲的page*/
if(page_no == FIL_NULL)
return FALSE;
page = buf_get_get(space, page_no, RW_X_LATCH, mtr);
buf_page_dbg_add_level(page, SYNC_FSP_PAGE);
/*初始化页中所有inode, FSP_SEG_INODES_PER_PAGE = 85,一个inodes page最多包含85个 seg inode对象*/
for(i = 0; i < FSP_SEG_INODES_PER_PAGE; i ++){
inode = fsp_seg_inode_page_get_nth_inode(page, i, mtr);
mlog_write_dulint(inode + FSEG_ID, ut_dulint_zero, MLOG_8BYTES, mtr);
}
/*将页插入到FSP_SEG_INODES_FREE的最后*/
flst_add_last(space_header + FSP_SEG_INODES_FREE, page + FSEG_INODE_PAGE_NODE, mtr);
return TRUE;
}
/*获得一个空闲的segment inode*/
static fseg_inode_t* fsp_alloc_seg_inode(fsp_header_t* space_header, mtr_t* mtr)
{
ulint page_no;
page_t* page;
fseg_inode_t* inode;
ibool success;
ulint n;
/*没有空闲的inode页*/
if(flst_get_len(space_header + FSP_SEG_INODES_FREE, mtr) == 0){
if(fsp_alloc_seg_inode_page(space_header, mtr) == FALSE)
return NULL;
}
page_no = flst_get_first(space_header + FSP_SEG_INODES_FREE, mtr).page;
page = buf_page_get(buf_frame_get_space_id(space_header), page, RW_X_LATCH, mtr);
buf_page_dbg_add_level(page, SYNC_FSP_PAGE);
n = fsp_seg_inode_page_find_free(page, 0, mtr);
ut_a(n != ULINT_UNDEFINED);
/*从可以用的inode page中拿去inode*/
inode = fsp_seg_inode_page_get_nth_inode(page, n, mtr);
if(ULINT32_UNDEFINED == fsp_seg_inode_page_find_free(page, n + 1, mtr)){ /*inode页已经满了*/
flst_remove(space_header + FSP_SEG_INODES_FREE, page + FSEG_INODE_PAGE_NODE, mtr);
flst_add_last(space_header + FSP_SEG_INODES_FULL, page + FSEG_INODE_PAGE_NODE, mtr);
}
return inode;
}
/*释放一个segment inode*/
static void fsp_free_seg_inode(ulint space, fseg_inode_t* inode, mtr_t* mtr)
{
page_t* page;
fsp_header_t* space_header;
page = buf_frame_align(inode);
space_header = fsp_get_space_header(space, mtr);
page = buf_frame_align(inode);
/*魔法字校验*/
ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N) == FSEG_MAGIC_N_VALUE);
if(ULINT_UNDEFINED == fsp_seg_inode_page_find_free(page, 0, mtr)){ /*这个page存在空闲的inode*/
flst_remove(space_header + FSP_SEG_INODES_FULL, page + FSEG_INODE_PAGE_NODE, mtr);
flst_add_last(space_header + FSP_SEG_INODES_FREE, page + FSEG_INODE_PAGE_NODE, mtr);
}
/*重置seg_id和魔法字*/
mlog_write_dulint(inode + FSEG_ID, ut_dulint_zero, MLOG_8BYTES, mtr);
mlog_write_ulint(inode + FSEG_MAGIC_N, 0, MLOG_4BYTES, mtr);
/*这个页中的inode全部空闲,释放对页的占用*/
if(ULINT_UNDEFINED == fsp_seg_inode_page_find_used(page, mtr)){
flst_remove(space_header + FSP_SEG_INODES_FREE, page + FSEG_INODE_PAGE_NODE, mtr);
fsp_free_page(space, buf_frame_get_page_no(page), mtr);
}
}
static fseg_inode_t* fseg_inode_get(fseg_header_t* header, mtr_t* mtr)
{
fil_addr_t inode_addr;
fseg_inode_t* inode;
/*获得当前segment inode的链表fil_addr_t地址*/
inode_addr.page = mach_read_from_4(header + FSEG_HDR_PAGE_NO);
inode_addr.boffset = mach_read_from_2(header + FSEG_HDR_OFFSET);
/*获得对应inode的指针地址*/
inode = fut_get_ptr(mach_read_from_4(header + FSEG_HDR_SPACE), inode_addr, RW_X_LATCH, mtr);
/*魔法字校验*/
ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N) == FSEG_MAGIC_N_VALUE);
return inode;
}
/*获得inode指定位置的page no*/
UNIV_INLINE ulint fseg_get_nth_frag_page_no(fseg_inode_t* inode, ulint n, mtr_t* mtr)
{
ut_ad(inode && mtr);
ut_ad(n < FSEG_FRAG_ARR_N_SLOTS);
ut_ad(mtr_memo_contains(mtr, buf_block_align(inode), MTR_MEMO_PAGE_X_FIX));
return(mach_read_from_4(inode + FSEG_FRAG_ARR + n * FSEG_FRAG_SLOT_SIZE));
}
UNIV_INLINE void fseg_set_nth_frag_page_no(fseg_inode_t* inode, ulint n, ulint page_no, mtr_t* mtr)
{
ut_ad(inode && mtr);
ut_ad(n < FSEG_FRAG_ARR_N_SLOTS);
ut_ad(mtr_memo_contains(mtr, buf_block_align(inode), MTR_MEMO_PAGE_X_FIX));
/*将page no写入到对应的slot位置上*/
mlog_write_ulint(inode + FSEG_FRAG_ARR + n * FSEG_FRAG_SLOT_SIZE, page_no, MLOG_4BYTES, mtr);
}
/*在segment的inode当中获得一个空闲的page的slot*/
static ulint fseg_find_free_frag_page_slot(fseg_inode_t* inode, mtr_t* mtr)
{
ulint i;
ulint page_no;
ut_ad(inode & mtr);
for(i = 0; i < FSEG_FRAG_ARR_N_SLOTS; i ++){
page_no = fseg_get_nth_frag_page_no(inode, i, mtr); /*获得*/
if(page_no == FIL_NULL)
return i;
}
return ULINT_UNDEFINED;
}
/*在inode当中查找最后一个被使用的页的slot*/
static ulint fseg_find_last_used_frag_page_slot(fseg_inode_t* inode, mtr_t* mtr)
{
ulint i;
ulint page_no;
ut_ad(inode && mtr);
/*从后面查找到前面,应该为的是效率问题*/
for(i = 0; i < FSEG_FRAG_ARR_N_SLOTS; i ++){
page_no = fseg_get_nth_frag_page_no(inode, FSEG_FRAG_ARR_N_SLOTS - i -1, mtr);
if(page_no != FIL_NULL)
return FSEG_FRAG_ARR_N_SLOTS - i - 1;
}
return ULINT_UNDEFINED;
}
/*获取inode中已经使用的page数*/