forked from bluethinking/InnoSQL
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathinnodb_flash_cache.patch
More file actions
5164 lines (5045 loc) · 161 KB
/
innodb_flash_cache.patch
File metadata and controls
5164 lines (5045 loc) · 161 KB
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
Index: storage/innobase/os/os0file.c
===================================================================
--- storage/innobase/os/os0file.c (revision 207)
+++ storage/innobase/os/os0file.c (revision 392)
@@ -39,6 +39,7 @@
#endif
#include "ut0mem.h"
+#include "fc0fc.h"
#include "srv0srv.h"
#include "srv0start.h"
#include "fil0fil.h"
@@ -268,6 +269,8 @@
static os_aio_array_t* os_aio_ibuf_array = NULL; /*!< Insert buffer */
static os_aio_array_t* os_aio_log_array = NULL; /*!< Redo log */
static os_aio_array_t* os_aio_sync_array = NULL; /*!< Synchronous I/O */
+static os_aio_array_t* os_aio_fc_read_array = NULL; /*!< Flash cache read thread */
+static os_aio_array_t* os_aio_fc_write_array = NULL; /*!< Flash cache write thread */
/* @} */
/** Number of asynchronous I/O segments. Set by os_aio_init(). */
@@ -3021,8 +3024,7 @@
The function os_file_dirname returns a directory component of a
null-terminated pathname string. In the usual case, dirname returns
the string up to, but not including, the final '/', and basename
-is the component following the final '/'. Trailing '/' charac
-ters are not counted as part of the pathname.
+is the component following the final '/'. Trailing '/' charac?ters are not counted as part of the pathname.
If path does not contain a slash, dirname returns the string ".".
@@ -3372,8 +3374,15 @@
array */
{
ulint i;
- ulint n_segments = 2 + n_read_segs + n_write_segs;
+ ulint n_segments;
+ if (fc_is_enabled()){
+ n_segments = 4 + n_read_segs + n_write_segs;
+ }
+ else{
+ n_segments = 2 + n_read_segs + n_write_segs;
+ }
+
ut_ad(n_segments >= 4);
os_io_init_simple();
@@ -3416,17 +3425,28 @@
goto err_exit;
}
- for (i = 2 + n_read_segs; i < n_segments; i++) {
- ut_a(i < SRV_MAX_N_IO_THREADS);
- srv_io_thread_function[i] = "write thread";
+ if (fc_is_enabled()){
+ for (i = 2 + n_read_segs; i < n_segments - 2; i++) {
+ ut_a(i < SRV_MAX_N_IO_THREADS);
+ srv_io_thread_function[i] = "write thread";
+ }
+ srv_io_thread_function[i] = "flash cache read thread";
+ srv_io_thread_function[i+1] = "flash cache write thread";
+ os_aio_fc_read_array = os_aio_array_create(n_per_seg,1);
+ os_aio_fc_write_array = os_aio_array_create(n_per_seg,1);
}
+ else{
+ for (i = 2 + n_read_segs; i < n_segments; i++) {
+ ut_a(i < SRV_MAX_N_IO_THREADS);
+ srv_io_thread_function[i] = "write thread";
+ }
+ }
os_aio_sync_array = os_aio_array_create(n_slots_sync, 1);
if (os_aio_sync_array == NULL) {
goto err_exit;
}
-
os_aio_n_segments = n_segments;
os_aio_validate();
@@ -3466,6 +3486,13 @@
os_aio_array_free(os_aio_sync_array);
os_aio_sync_array = NULL;
+ if (fc_is_enabled()){
+ os_aio_array_free(os_aio_fc_read_array);
+ os_aio_fc_read_array = NULL;
+ os_aio_array_free(os_aio_fc_write_array);
+ os_aio_fc_write_array = NULL;
+ }
+
for (i = 0; i < os_aio_n_segments; i++) {
os_event_free(os_aio_segment_wait_events[i]);
}
@@ -3510,7 +3537,10 @@
os_aio_array_wake_win_aio_at_shutdown(os_aio_write_array);
os_aio_array_wake_win_aio_at_shutdown(os_aio_ibuf_array);
os_aio_array_wake_win_aio_at_shutdown(os_aio_log_array);
-
+ if (fc_is_enabled()){
+ os_aio_array_wake_win_aio_at_shutdown(os_aio_fc_read_array);
+ os_aio_array_wake_win_aio_at_shutdown(os_aio_fc_write_array);
+ }
#elif defined(LINUX_NATIVE_AIO)
/* When using native AIO interface the io helper threads
@@ -3543,6 +3573,29 @@
os_event_wait(os_aio_write_array->is_empty);
}
+
+/************************************************************************//**
+Waits until there are no pending writes in os_aio_write_array. There can
+be other, synchronous, pending writes. */
+UNIV_INTERN
+void
+os_aio_wait_until_no_pending_reads(void)
+/*=====================================*/
+{
+ os_event_wait(os_aio_read_array->is_empty);
+}
+
+/************************************************************************//**
+Waits until there are no pending writes in os_aio_fc_write_array. There can
+be other, synchronous, pending writes. */
+UNIV_INTERN
+void
+os_aio_wait_until_no_pending_fc_writes(void)
+/*=====================================*/
+{
+ os_event_wait(os_aio_fc_write_array->is_empty);
+}
+
/**********************************************************************//**
Calculates segment number for a slot.
@return segment number (which is the number used by, for example,
@@ -3568,13 +3621,17 @@
/ os_aio_read_array->n_segments;
segment = 2 + slot->pos / seg_len;
- } else {
+ } else if (array == os_aio_fc_read_array){
+ segment = os_aio_read_array->n_segments + 2 + os_aio_write_array->n_segments;
+ } else if (array == os_aio_fc_write_array){
+ segment = os_aio_read_array->n_segments + 3 + os_aio_write_array->n_segments;;
+ } else{
ut_a(array == os_aio_write_array);
seg_len = os_aio_write_array->n_slots
/ os_aio_write_array->n_segments;
-
- segment = os_aio_read_array->n_segments + 2
- + slot->pos / seg_len;
+
+ segment = os_aio_read_array->n_segments + 2
+ + slot->pos / seg_len;
}
return(segment);
@@ -3606,6 +3663,12 @@
*array = os_aio_read_array;
segment = global_segment - 2;
+ } else if (global_segment == 2 + os_aio_read_array->n_segments + os_aio_write_array->n_segments){
+ *array = os_aio_fc_read_array;
+ segment = 0;
+ } else if (global_segment == 3 + os_aio_read_array->n_segments + os_aio_write_array->n_segments){
+ *array = os_aio_fc_write_array;
+ segment = 0;
} else {
*array = os_aio_write_array;
@@ -4097,6 +4160,9 @@
ut_a(!srv_use_native_aio);
#endif /* LINUX_NATIVE_AIO */
break;
+ case OS_AIO_FLASH_CACHE_WRITE:
+ array = os_aio_fc_write_array;
+ break;
default:
ut_error;
array = NULL; /* Eliminate compiler warning */
@@ -5138,13 +5204,30 @@
goto loop;
}
- if (array == os_aio_write_array) {
- fputs(",\n ibuf aio reads:", file);
- array = os_aio_ibuf_array;
+ if (fc_is_enabled()){
+ if (array == os_aio_write_array) {
+ fputs(", fc write aio i/o's:", file);
+ array = os_aio_fc_write_array;
- goto loop;
+ goto loop;
+ }
+
+ if ( array == os_aio_fc_write_array ){
+ fputs(",\n ibuf aio reads:", file);
+ array = os_aio_ibuf_array;
+
+ goto loop;
+ }
}
+ else{
+ if (array == os_aio_write_array) {
+ fputs(",\n ibuf aio reads:", file);
+ array = os_aio_ibuf_array;
+ goto loop;
+ }
+ }
+
if (array == os_aio_ibuf_array) {
fputs(", log i/o's:", file);
array = os_aio_log_array;
@@ -5164,10 +5247,11 @@
time_elapsed = 0.001 + difftime(current_time, os_last_printout);
fprintf(file,
- "Pending flushes (fsync) log: %lu; buffer pool: %lu\n"
+ "Pending flushes (fsync) log: %lu; buffer pool: %lu; flash cache %lu\n"
"%lu OS file reads, %lu OS file writes, %lu OS fsyncs\n",
(ulong) fil_n_pending_log_flushes,
(ulong) fil_n_pending_tablespace_flushes,
+ (ulong) fil_n_pending_flash_cache_flushes,
(ulong) os_n_file_reads, (ulong) os_n_file_writes,
(ulong) os_n_fsyncs);
Index: storage/innobase/fc/fc0dump.c
===================================================================
--- storage/innobase/fc/fc0dump.c (revision 0)
+++ storage/innobase/fc/fc0dump.c (revision 392)
@@ -0,0 +1,709 @@
+/**************************************************//**
+@file fc/fc0dump.c
+Flash Cache dump and load
+
+Created 24/4/2012 David Jiang ([email protected])
+*******************************************************/
+
+#include "fc0dump.h"
+#include "srv0srv.h"
+#include "srv0start.h"
+#include "os0file.h"
+#include "fil0fil.h"
+#include "fc0log.h"
+
+/******************************************************************//**
+Dump blocks from flash cache to file*/
+UNIV_INTERN
+void
+fc_dump(
+/*==================*/
+)
+{
+
+ char full_filename[OS_FILE_MAX_PATH];
+ char tmp_filename[OS_FILE_MAX_PATH];
+ FILE* f;
+ ulint i;
+ int ret;
+ fc_block_t* b;
+
+ ut_snprintf(full_filename, sizeof(full_filename),
+ "%s/%s", srv_data_home, "flash_cache.dump");
+ srv_normalize_path_for_win(full_filename);
+
+ ut_snprintf(tmp_filename, sizeof(tmp_filename),
+ "%s.incomplete", full_filename);
+ srv_normalize_path_for_win(tmp_filename);
+
+ f = fopen(tmp_filename, "w");
+ if (f == NULL) {
+ fprintf(stderr," InnoDB: Cannot open '%s' for writing: %s.\n",tmp_filename, strerror(errno));
+ return;
+ }
+ /* dump flash cache block info */
+ for ( i = 0; i < fc_get_size(); i++ ){
+ b = &fc->block[i];
+ if ( b->state != BLOCK_NOT_USED ){
+ /* only dump ~BLOCK_NOT_USED */
+
+ ret = fprintf(f, "%lu,%lu,%lu,%lu\n",
+ (unsigned long)b->space,
+ (unsigned long)b->offset,
+ (unsigned long)b->fil_offset,
+ (unsigned long)b->state);
+ if (ret < 0) {
+ fclose(f);
+ fprintf(stderr,
+ " InnoDB: Cannot write to '%s': %s.\n",
+ tmp_filename, strerror(errno));
+ /* leave tmp_filename to exist */
+ return;
+ }
+ }
+ }
+ ret = fclose(f);
+ if (ret != 0) {
+ fprintf(stderr,
+ " InnoDB: Cannot close '%s': %s.\n",
+ tmp_filename, strerror(errno));
+ return;
+ }
+ ret = unlink(full_filename);
+ if (ret != 0 && errno != ENOENT) {
+ fprintf(stderr,
+ " InnoDB: Cannot delete '%s': %s.\n",
+ full_filename, strerror(errno));
+ /* leave tmp_filename to exist */
+ return;
+ }
+ ret = rename(tmp_filename, full_filename);
+ if (ret != 0) {
+ fprintf(stderr,
+ " InnoDB: Cannot rename '%s' to '%s': %s.\n",
+ tmp_filename, full_filename,
+ strerror(errno));
+ /* leave tmp_filename to exist */
+ return;
+ }
+
+ ut_print_timestamp(stderr);
+
+ fprintf(stderr,
+ " InnoDB: flash cache dump completed.\n");
+}
+
+/******************************************************************//**
+Load flash cache from dump file */
+UNIV_INTERN
+void
+fc_load(
+/*==================*/
+)
+{
+
+ char full_filename[OS_FILE_MAX_PATH];
+ ulint i;
+ FILE* f;
+ ulint dump_n;
+ int fscanf_ret;
+ fc_block_t* b;
+ ulint space_id = 0;
+ ulint page_no = 0;
+ ulint fil_offset = 0;
+ ulint state = 0;
+
+ ut_snprintf(full_filename, sizeof(full_filename),
+ "%s/%s", srv_data_home, "flash_cache.dump");
+ srv_normalize_path_for_win(full_filename);
+
+ f = fopen(full_filename, "r");
+ if (f == NULL) {
+ ut_print_timestamp(stderr);
+ fprintf(stderr,
+ " InnoDB: Cannot open '%s' for reading: %s.\n",
+ full_filename, strerror(errno));
+ fprintf(stderr, "InnoDB: flash cache did not shutdown correctly,"
+ "scan flash cache file %s to recover.\n",
+ srv_flash_cache_file);
+ return;
+ }
+
+ dump_n = 0;
+ while (fscanf(f, "%lu,%lu,%lu,%lu", &space_id, &page_no, &fil_offset, &state) == 4 ) {
+ dump_n++;
+ }
+
+ if ( !feof(f) ) {
+ /* fscanf() returned != 4 */
+ const char* what;
+ if (ferror(f)) {
+ what = "reading";
+ } else {
+ what = "parsing";
+ }
+ fclose(f);
+ fprintf(stderr, " InnoDB: Error %s '%s', "
+ "unable to load buffer pool (stage 1).\n",
+ what, full_filename);
+ return;
+ }
+
+ rewind(f);
+
+ for (i = 0; i < dump_n; i++) {
+ fscanf_ret = fscanf(f, "%lu,%lu,%lu,%lu",&space_id, &page_no, &fil_offset, &state);
+ if (fscanf_ret != 4) {
+ if (feof(f)) {
+ break;
+ }
+ fclose(f);
+ fprintf(stderr,
+ " InnoDB: Error parsing '%s', unable "
+ "to load buffer pool (stage 2).\n",
+ full_filename);
+ return;
+ }
+
+ if (space_id > ULINT32_MASK || page_no > ULINT32_MASK) {
+ fclose(f);
+ /* error found, we should not continue */
+ ut_error;
+ }
+
+ ut_ad ( state != BLOCK_NOT_USED );
+ b = &fc->block[fil_offset];
+ b->offset = page_no;
+ b->space = space_id;
+ b->state = state;
+ flash_cache_hash_mutex_enter(space_id,page_no);
+
+ /* insert to hash table */
+ HASH_INSERT(fc_block_t,hash,fc->hash_table,
+ buf_page_address_fold(b->space, b->offset),
+ b);
+ flash_cache_hash_mutex_exit(space_id,page_no);
+
+ srv_flash_cache_used++;
+ }
+
+ fclose(f);
+
+ ut_a(os_file_delete(full_filename));
+
+ ut_print_timestamp(stderr);
+
+ fprintf(stderr,
+ " InnoDB: flash cache load completed.\n");
+
+ srv_flash_cache_load_from_dump_file = TRUE;
+
+}
+
+/******************************************************************//**
+Dump blocks from flash cache to file*/
+static
+void
+fc_load_blocks(
+/*==================*/
+ FILE* f, /*<! flash cache file */
+ char* full_filename,
+ ulint count,
+ byte* page
+){
+ ulint space_id;
+ ulint page_no;
+ int fscanf_ret;
+ ulint ret;
+ fc_block_t* b;
+ int i;
+ ulint start_offset;
+
+ ut_a(fc->write_off == fc->flush_off);
+
+ start_offset = fc->write_off;
+
+ os_aio_simulated_put_read_threads_to_sleep();
+
+ for( i = 0; i < count; i++){
+
+ fscanf_ret = fscanf(f, "%lu,%lu",&space_id, &page_no);
+ if (fscanf_ret != 2) {
+ if (feof(f)) {
+ break;
+ }
+ fclose(f);
+ fprintf(stderr,
+ " InnoDB: Error parsing '%s', unable "
+ "to load buffer pool (stage 2).\n",
+ full_filename);
+ return;
+ }
+
+ if (space_id > ULINT32_MASK || page_no > ULINT32_MASK) {
+ fclose(f);
+ /* error found, we should not continue */
+ ut_error;
+ }
+
+ ret = fil_io(OS_FILE_READ | OS_AIO_SIMULATED_WAKE_LATER,FALSE,space_id,0,page_no,0,UNIV_PAGE_SIZE,&page[i*UNIV_PAGE_SIZE],NULL);
+ if ( ret != DB_SUCCESS ){
+ ut_print_timestamp(stderr);
+ fprintf(stderr," InnoDB: can read from disk.\n");
+ ut_error;
+ }
+
+ b = &fc->block[fc->write_off];
+ b->offset = page_no;
+ b->space = space_id;
+ b->state = BLOCK_READ_CACHE;
+ flash_cache_hash_mutex_enter(space_id,page_no);
+
+ /* insert to hash table */
+ HASH_INSERT(fc_block_t,hash,fc->hash_table,
+ buf_page_address_fold(b->space, b->offset),
+ b);
+ flash_cache_hash_mutex_exit(space_id,page_no);
+
+ srv_flash_cache_used++;
+ fc->write_off = ( fc->write_off + 1 ) % fc_get_size();
+ fc->flush_off = ( fc->flush_off + 1 ) % fc_get_size();
+ }
+
+ os_aio_simulated_wake_handler_threads();
+ os_aio_wait_until_no_pending_reads();
+
+#ifdef UNIV_DEBUG
+ for ( i = 0; i < count; i++ ){
+
+ ulint space;
+ ulint offset;
+ fc_block_t* b3;
+
+ offset = mach_read_from_4(page + i*UNIV_PAGE_SIZE + FIL_PAGE_OFFSET);
+ space = mach_read_from_4(page + i*UNIV_PAGE_SIZE + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
+
+ b3 = &fc->block[start_offset + i];
+
+ ut_ad(b3->space == space);
+ ut_ad(b3->offset == offset);
+ }
+#endif
+ ret = fil_io(OS_FILE_WRITE ,TRUE,FLASH_CACHE_SPACE,0,start_offset,0,UNIV_PAGE_SIZE*count,page,NULL);
+ if ( ret != DB_SUCCESS ){
+ ut_print_timestamp(stderr);
+ fprintf(stderr," InnoDB: Error in write page to flash cache.\n");
+ ut_error;
+ }
+
+}
+
+/******************************************************************//**
+Load flash cache from warmup file */
+UNIV_INTERN
+void
+fc_load_warmup_file(
+/*==================*/
+)
+{
+
+ char full_filename[OS_FILE_MAX_PATH];
+ FILE* f;
+ ulint dump_n;
+ ulint space_id;
+ ulint page_no;
+ byte* page_unalign;
+ byte* page;
+ ulint len;
+
+ ut_snprintf(full_filename, sizeof(full_filename),
+ "%s/%s", srv_data_home, "flash_cache.warmup");
+ srv_normalize_path_for_win(full_filename);
+
+ f = fopen(full_filename, "r");
+ if (f == NULL) {
+ ut_print_timestamp(stderr);
+ fprintf(stderr,
+ " InnoDB: Cannot open '%s' for warmup: %s.\n",
+ full_filename, strerror(errno));
+ return;
+ }
+
+ dump_n = 0;
+ while (fscanf(f, "%lu,%lu", &space_id, &page_no) == 2 ) {
+ dump_n++;
+ }
+
+ if ( !feof(f) ) {
+ /* fscanf() returned != 4 */
+ const char* what;
+ if (ferror(f)) {
+ what = "reading";
+ } else {
+ what = "parsing";
+ }
+ fclose(f);
+ fprintf(stderr, " InnoDB: Error %s '%s', "
+ "unable to load buffer pool (stage 1).\n",
+ what, full_filename);
+ return;
+ }
+
+ rewind(f);
+
+ len = dump_n;
+ page_unalign = (byte*)ut_malloc((srv_flash_cache_pages_per_read+1)*UNIV_PAGE_SIZE);
+ page = (byte*)ut_align(page_unalign,UNIV_PAGE_SIZE);
+
+ ut_print_timestamp(stderr);
+ fprintf(stderr," InnoDB: warmup flash cache from flash_cache.warmup ");
+ while ( len > srv_flash_cache_pages_per_read ){
+ fc_load_blocks(f,full_filename,srv_flash_cache_pages_per_read,page);
+ len = len - srv_flash_cache_pages_per_read;
+ fprintf(stderr,"%lu ",(unsigned long)100*(dump_n-len)/dump_n);
+ }
+
+ if ( len > 0 ){
+ fc_load_blocks(f,full_filename,len,page);
+ fprintf(stderr,"100.\n");
+ }
+
+ fclose(f);
+
+ rename("flash_cache.warmup","flash_cache.warmup.old");
+
+ ut_print_timestamp(stderr);
+ fprintf(stderr,
+ " InnoDB: flash cache load completed.\n");
+
+ srv_flash_cache_load_from_dump_file = TRUE;
+
+ if ( page_unalign ){
+ ut_free(page_unalign);
+ }
+
+}
+
+/********************************************************************//**
+Warm up tablespace to flash cache block. */
+static
+ibool
+fc_warmup_tablespace(
+/*=============================*/
+ os_file_t file, /*!< in: file of tablespace */
+ const char* dbname, /*!< in: database name */
+ const char* tablename, /*!< in: tablespace to load tpcc.*:test.mysql */
+ ulint space_id) /*!< in: tablespace space id */
+{
+
+ byte* buf_unaligned;
+ byte* buf;
+ byte* page;
+ ibool success = FALSE;
+ char* token;
+ char name[128];
+ char name2[128];
+ char str[128];
+ ulint n_pages;
+ ulint write_off;
+ ulint i = 0;
+ ulint zip_size;
+ ulint size;
+ ulint size_high;
+ ulint foffset;
+ ulint foffset_high;
+ ulint j;
+ fc_block_t* b;
+ ulint offset;
+
+ sprintf(name,"%s.%s",dbname,tablename);
+ sprintf(name2,"%s.*",dbname);
+ ut_strcpy(str,srv_flash_cache_warmup_table);
+ token = strtok(str,":");
+ while( token != NULL && !success ){
+ if ( ut_strcmp(token,name) == 0 || ut_strcmp(token,name2) == 0 )
+ success = TRUE;
+ token = strtok(NULL,":");
+ }
+
+ if ( !success ){
+ return TRUE;
+ }
+
+
+ if ( fc->write_round == fc->flush_round ){
+ n_pages = fc_get_size() - ( fc->write_off - fc->flush_off ) ;
+ }
+ else{
+ ut_a(fc->write_round = fc->flush_round+1);
+ n_pages = fc->flush_off - fc->write_off;
+ }
+
+ /* get zip size */
+ zip_size = fil_space_get_zip_size(space_id);
+ if (zip_size == 0){
+ zip_size = UNIV_PAGE_SIZE;
+ }
+
+ /* start write offset */
+ write_off = fc->write_off;
+ /* get file size */
+ os_file_get_size(file,&size,&size_high);
+ /* malloc memory for page to read */
+ buf_unaligned = (byte*)ut_malloc((srv_flash_cache_pages_per_read+1)*UNIV_PAGE_SIZE);
+ buf = (byte*)ut_align(buf_unaligned,zip_size);
+
+ ut_print_timestamp(stderr);
+ fprintf(stderr," InnoDB: start to warm up tablespace %s.%s to flash cache.\n",dbname,tablename);
+
+ while( i + srv_flash_cache_pages_per_read < n_pages ){
+ foffset = ((ulint)(i*zip_size)) & 0xFFFFFFFFUL;
+ foffset_high = (ib_uint64_t)(i*zip_size) >> 32;
+ success = os_file_read_no_error_handling(file, buf, foffset, foffset_high, zip_size*srv_flash_cache_pages_per_read);
+ if ( !success ){
+ ut_free(buf_unaligned);
+ return (TRUE);
+ }
+ for( j=0; j<srv_flash_cache_pages_per_read; j++ ){
+ page = buf + j*zip_size;
+ if ( fil_page_get_type(page) != FIL_PAGE_INDEX
+ && fil_page_get_type(page) != FIL_PAGE_INODE
+ ){
+ continue;
+ }
+ offset = mach_read_from_4(page+FIL_PAGE_OFFSET);
+ ut_ad( mach_read_from_4(page+FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID) == space_id );
+ flash_cache_hash_mutex_enter(space_id,offset);
+ HASH_SEARCH(hash,fc->hash_table,
+ buf_page_address_fold(space_id,offset),
+ fc_block_t*,b,
+ ut_ad(1),
+ space_id == b->space && offset == b->offset);
+ if ( b ){
+#ifdef UNIV_FLASH_DEBUG
+ /* if found in hash table, remove it first */
+ ulint ret3;
+ ulint space3;
+ ulint offset3;
+ ib_uint64_t lsn3;
+ ib_uint64_t lsn;
+ byte read_buf[UNIV_PAGE_SIZE];
+ lsn = mach_read_from_8(page+FIL_PAGE_LSN);
+ /* lsn in hash table should smaller than this */
+ ret3 = fil_io(OS_FILE_READ,TRUE,FLASH_CACHE_SPACE,0,b->fil_offset,0,UNIV_PAGE_SIZE,&read_buf,NULL);
+ ut_ad(ret3);
+ lsn3 = mach_read_from_8(read_buf+FIL_PAGE_LSN);
+ space3 = mach_read_from_4(read_buf+FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
+ offset3 = mach_read_from_4(read_buf+FIL_PAGE_OFFSET);
+ ut_ad( space3 == space_id );
+ ut_ad( offset3 == offset );
+ ut_ad(lsn3>=lsn);
+#endif
+ /* page in flash cache should always newer than page in disk */
+ flash_cache_hash_mutex_exit(space_id,offset);
+ continue;
+ }
+ else{
+ b = &fc->block[(fc->write_off)%fc_get_size()];
+ ut_a( b->state == BLOCK_NOT_USED );
+ b->space = space_id;
+ b->offset = offset;
+ b->state = BLOCK_READ_CACHE;
+#ifdef UNIV_FLASH_DEBUG
+ fprintf(stderr," warmup space: %lu, page no: %lu.\n",space_id,offset);
+#endif
+ HASH_INSERT(fc_block_t,hash,fc->hash_table,
+ buf_page_address_fold(b->space, b->offset),
+ b);
+ success = fil_io(OS_FILE_WRITE | OS_AIO_SIMULATED_WAKE_LATER,FALSE,FLASH_CACHE_SPACE,0,b->fil_offset,0,UNIV_PAGE_SIZE,page,NULL);
+ if ( success != DB_SUCCESS ){
+ ut_print_timestamp(stderr);
+ fprintf(stderr," InnoDB [Error]: Can not recover tablespace %lu, offset is %lu.\n",
+ space_id,offset);
+ ut_error;
+ }
+ srv_flash_cache_used = srv_flash_cache_used + 1;
+ }
+ flash_cache_hash_mutex_exit(space_id,offset);
+ fc->write_off = (fc->write_off + 1) % fc_get_size();
+ fc->flush_off = (fc->flush_off + 1) % fc_get_size();
+ if ( fc->write_off == 0 ){
+ os_aio_simulated_wake_handler_threads();
+ os_aio_wait_until_no_pending_fc_writes();
+ fil_flush_file_spaces(FIL_FLASH_CACHE);
+ ut_free(buf_unaligned);
+ ut_print_timestamp(stderr);
+ fprintf(stderr," InnoDB: warm up table %s.%s to space: %lu offset %lu.(100%%)\n",dbname,tablename,space_id,i);
+ ut_print_timestamp(stderr);
+ fprintf(stderr," InnoDB: flash cache is full, warm up stop.\n");
+ return FALSE;
+ }
+ }
+ os_aio_simulated_wake_handler_threads();
+ os_aio_wait_until_no_pending_fc_writes();
+ fil_flush_file_spaces(FIL_FLASH_CACHE);
+ i = i + srv_flash_cache_pages_per_read;
+ }
+ ut_free(buf_unaligned);
+ return (TRUE);
+}
+
+/********************************************************************//**
+Warm up tablespaces to flash cache block.,stop if no space left. */
+UNIV_INTERN
+void
+fc_warmup_tablespaces(
+/*=============================*/
+)
+{
+ int ret;
+ char* dbpath = NULL;
+ ulint dbpath_len = 100;
+ os_file_dir_t dir;
+ os_file_dir_t dbdir;
+ os_file_stat_t dbinfo;
+ os_file_stat_t fileinfo;
+ ulint err = DB_SUCCESS;
+
+ if ( srv_flash_cache_size == 0 || fc_log->recovery ){
+ return;
+ }
+
+ /* The datadir of MySQL is always the default directory of mysqld */
+
+ dir = os_file_opendir(fil_path_to_mysql_datadir, TRUE);
+
+ if (dir == NULL) {
+
+ return;
+ }
+
+ dbpath = (char*)mem_alloc(dbpath_len);
+
+ /* Scan all directories under the datadir. They are the database
+ directories of MySQL. */
+
+ ret = fil_file_readdir_next_file(&err, fil_path_to_mysql_datadir, dir,
+ &dbinfo);
+ while (ret == 0) {
+ ulint len;
+ /* printf("Looking at %s in datadir\n", dbinfo.name); */
+
+ if (dbinfo.type == OS_FILE_TYPE_FILE
+ || dbinfo.type == OS_FILE_TYPE_UNKNOWN) {
+
+ goto next_datadir_item;
+ }
+
+ /* We found a symlink or a directory; try opening it to see
+ if a symlink is a directory */
+
+ len = strlen(fil_path_to_mysql_datadir)
+ + strlen (dbinfo.name) + 2;
+ if (len > dbpath_len) {
+ dbpath_len = len;
+
+ if (dbpath) {
+ mem_free(dbpath);
+ }
+
+ dbpath = (char*)mem_alloc(dbpath_len);
+ }
+ sprintf(dbpath, "%s/%s", fil_path_to_mysql_datadir,
+ dbinfo.name);
+ srv_normalize_path_for_win(dbpath);
+
+ dbdir = os_file_opendir(dbpath, FALSE);
+
+ if (dbdir != NULL) {
+ /* printf("Opened dir %s\n", dbinfo.name); */
+
+ /* We found a database directory; loop through it,
+ looking for possible .ibd files in it */
+
+ ret = fil_file_readdir_next_file(&err, dbpath, dbdir,
+ &fileinfo);
+ while (ret == 0) {
+ /* printf(
+ " Looking at file %s\n", fileinfo.name); */
+
+ if (fileinfo.type == OS_FILE_TYPE_DIR) {
+
+ goto next_file_item;
+ }
+
+ /* We found a symlink or a file */
+ if (strlen(fileinfo.name) > 4
+ && 0 == strcmp(fileinfo.name
+ + strlen(fileinfo.name) - 4,
+ ".ibd")) {
+ /* The name ends in .ibd; try opening
+ the file */
+ char* filepath;
+ os_file_t file;
+ ibool success;
+ byte* buf2;
+ byte* page;
+ ulint space_id;
+ /* Initialize file path */
+ filepath = (char*)mem_alloc(strlen(dbinfo.name) + strlen(fileinfo.name)
+ + strlen(fil_path_to_mysql_datadir) + 3);
+ sprintf(filepath, "%s/%s/%s", fil_path_to_mysql_datadir, dbinfo.name,
+ fileinfo.name);
+ srv_normalize_path_for_win(filepath);
+ //dict_casedn_str(filepath);
+
+ /* Get file handler */
+ file = os_file_create_simple_no_error_handling(innodb_file_data_key,
+ filepath, OS_FILE_OPEN, OS_FILE_READ_ONLY, &success);
+
+ /* Get space id */
+ buf2 = (byte*)ut_malloc(2 * UNIV_PAGE_SIZE);
+ /* Align the memory for file i/o if we might have O_DIRECT set */
+ page = (byte*)ut_align(buf2, UNIV_PAGE_SIZE);
+ os_file_read(file, page, 0, 0, UNIV_PAGE_SIZE);
+ /* We have to read the tablespace id from the file */
+ space_id = fsp_header_get_space_id(page);
+
+ /* Preload to flash cache */
+ if (fc_warmup_tablespace(file,dbinfo.name,strtok(fileinfo.name,"."),space_id) == FALSE ){
+ goto finish;
+ }
+
+ os_file_close(file);
+ ut_free(buf2);
+ mem_free(filepath);
+ }
+next_file_item:
+ ret = fil_file_readdir_next_file(&err,
+ dbpath, dbdir,
+ &fileinfo);
+ }
+
+ if (0 != os_file_closedir(dbdir)) {
+ fputs("InnoDB: Warning: could not"
+ " close database directory ", stderr);
+ ut_print_filename(stderr, dbpath);
+ putc('\n', stderr);
+
+ err = DB_ERROR;
+ }
+ }
+
+next_datadir_item:
+ ret = fil_file_readdir_next_file(&err,
+ fil_path_to_mysql_datadir,
+ dir, &dbinfo);
+ }
+finish:
+ flash_cache_mutex_enter();
+ fc_log_commit();
+ flash_cache_mutex_exit();
+ ut_print_timestamp(stderr);
+ fprintf(stderr," InnoDB: flash cache warm up finish.\n");
+#ifdef UNIV_FLASH_DEBUG
+ buf_flush_flash_cache_validate();
+#endif
+
+ mem_free(dbpath);
+}
\ No newline at end of file
Index: storage/innobase/fc/fc0fc.c
===================================================================
--- storage/innobase/fc/fc0fc.c (revision 0)
+++ storage/innobase/fc/fc0fc.c (revision 392)
@@ -0,0 +1,1945 @@
+/**************************************************//**
+@file fc/fc0fc.c
+Flash Cache for InnoDB
+
+Created 24/4/2012 David Jiang ([email protected])
+*******************************************************/
+
+#include "fc0fc.h"
+
+#include "ut0ut.h"
+#include "os0file.h"
+#include "fc0log.h"
+#include "srv0srv.h"
+#include "log0recv.h"
+#include "ibuf0ibuf.h"
+#include "fc0dump.h"
+#include "fc0recv.h"
+#include "srv0start.h"
+#include "fsp0types.h"
+
+
+/* flash cache size */
+UNIV_INTERN ulint srv_flash_cache_size = ULINT_MAX;
+/* flash cache file */
+UNIV_INTERN char* srv_flash_cache_file = NULL;
+/* flash cache warmup table when startup */
+UNIV_INTERN char* srv_flash_cache_warmup_table = NULL;
+/* flash cache warmup from file when startup */
+UNIV_INTERN char* srv_flash_cache_warmup_file = NULL;
+/* read number of page per operation in recovery and warmup */
+UNIV_INTERN ulint srv_flash_cache_pages_per_read = 512;
+/* flash cache write cache percentage */
+UNIV_INTERN ulong srv_flash_cache_write_cache_pct = 10;
+/* flash cache do full IO percentage */
+UNIV_INTERN ulong srv_flash_cache_do_full_io_pct = 30;
+/* flash cache block move limit */
+UNIV_INTERN ulong srv_flash_cache_move_limit = 90;
+/* whether enable flash cache block move */
+UNIV_INTERN my_bool srv_flash_cache_enable_move = TRUE;
+/* whether enable flash cache block migrate */
+UNIV_INTERN my_bool srv_flash_cache_enable_migrate = TRUE;
+/* use flash cache device as raw */
+UNIV_INTERN my_bool srv_flash_cache_is_raw = FALSE;
+/* adaptive flush for flash cache */
+UNIV_INTERN my_bool srv_flash_cache_adaptive_flushing = FALSE;
+/* flash cache io capacity */
+UNIV_INTERN ulong srv_fc_io_capacity = 500;
+/* doublewrite buffer flush to flash cache or behave as default */
+UNIV_INTERN my_bool srv_flash_cache_enable_write = TRUE;
+/* When 'innodb_flash_cache_enable_write' is first set to FALSE and then