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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
|
/*****************************************************************************
Copyright (c) 1995, 2017, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2008, 2009 Google Inc.
Copyright (c) 2009, Percona Inc.
Copyright (c) 2013, 2017, MariaDB Corporation.
Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
briefly in the InnoDB documentation. The contributions by Google are
incorporated with their permission, and subject to the conditions contained in
the file COPYING.Google.
Portions of this file contain modifications contributed and copyrighted
by Percona Inc.. Those modifications are
gratefully acknowledged and are described briefly in the InnoDB
documentation. The contributions by Percona Inc. are incorporated with
their permission, and subject to the conditions contained in the file
COPYING.Percona.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
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 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 srv/srv0srv.cc
The database server main program
Created 10/8/1995 Heikki Tuuri
*******************************************************/
#include "my_global.h"
// JAN: TODO: MySQL 5.7 missing header
//#include "my_thread.h"
//
// #include "mysql/psi/mysql_stage.h"
// #include "mysql/psi/psi.h"
#include "ha_prototypes.h"
#include "btr0sea.h"
#include "buf0flu.h"
#include "buf0lru.h"
#include "dict0boot.h"
#include "dict0load.h"
#include "fsp0sysspace.h"
#include "ibuf0ibuf.h"
#include "lock0lock.h"
#include "log0recv.h"
#include "mem0mem.h"
#include "os0proc.h"
#include "pars0pars.h"
#include "que0que.h"
#include "row0mysql.h"
#include "row0trunc.h"
#include "row0log.h"
#include "srv0mon.h"
#include "srv0srv.h"
#include "srv0start.h"
#include "sync0sync.h"
#include "trx0i_s.h"
#include "trx0purge.h"
#include "usr0sess.h"
#include "ut0crc32.h"
#include "btr0defragment.h"
#include "ut0mem.h"
#include "fil0fil.h"
#include "fil0crypt.h"
#include "fil0pagecompress.h"
#include "btr0scrub.h"
#ifdef WITH_WSREP
extern int wsrep_debug;
extern int wsrep_trx_is_aborting(void *thd_ptr);
#endif
/* The following is the maximum allowed duration of a lock wait. */
UNIV_INTERN ulong srv_fatal_semaphore_wait_threshold = DEFAULT_SRV_FATAL_SEMAPHORE_TIMEOUT;
/* How much data manipulation language (DML) statements need to be delayed,
in microseconds, in order to reduce the lagging of the purge thread. */
ulint srv_dml_needed_delay;
bool srv_monitor_active;
bool srv_error_monitor_active;
bool srv_buf_dump_thread_active;
bool srv_dict_stats_thread_active;
bool srv_buf_resize_thread_active;
my_bool srv_scrub_log;
const char* srv_main_thread_op_info = "";
/** Prefix used by MySQL to indicate pre-5.1 table name encoding */
const char srv_mysql50_table_name_prefix[10] = "#mysql50#";
/* Server parameters which are read from the initfile */
/* The following three are dir paths which are catenated before file
names, where the file name itself may also contain a path */
char* srv_data_home;
/** Rollback files directory, can be absolute. */
char* srv_undo_dir;
/** The number of tablespaces to use for rollback segments. */
ulong srv_undo_tablespaces;
/** The number of UNDO tablespaces that are open and ready to use. */
ulint srv_undo_tablespaces_open;
/** The number of UNDO tablespaces that are active (hosting some rollback
segment). It is quite possible that some of the tablespaces doesn't host
any of the rollback-segment based on configuration used. */
ulint srv_undo_tablespaces_active;
/* The number of rollback segments to use */
ulong srv_undo_logs;
/** Rate at which UNDO records should be purged. */
ulong srv_purge_rseg_truncate_frequency;
/** Enable or Disable Truncate of UNDO tablespace.
Note: If enabled then UNDO tablespace will be selected for truncate.
While Server waits for undo-tablespace to truncate if user disables
it, truncate action is completed but no new tablespace is marked
for truncate (action is never aborted). */
my_bool srv_undo_log_truncate;
/** Maximum size of undo tablespace. */
unsigned long long srv_max_undo_log_size;
/** Default undo tablespace size in UNIV_PAGEs count (10MB). */
const ulint SRV_UNDO_TABLESPACE_SIZE_IN_PAGES =
((1024 * 1024) * 10) / UNIV_PAGE_SIZE_DEF;
/** Set if InnoDB must operate in read-only mode. We don't do any
recovery and open all tables in RO mode instead of RW mode. We don't
sync the max trx id to disk either. */
my_bool srv_read_only_mode;
/** store to its own file each table created by an user; data
dictionary tables are in the system tablespace 0 */
my_bool srv_file_per_table;
/** Set if InnoDB operates in read-only mode or innodb-force-recovery
is greater than SRV_FORCE_NO_TRX_UNDO. */
my_bool high_level_read_only;
/** Place locks to records only i.e. do not use next-key locking except
on duplicate key checking and foreign key checking */
ibool srv_locks_unsafe_for_binlog;
/** Sort buffer size in index creation */
ulong srv_sort_buf_size;
/** Maximum modification log file size for online index creation */
unsigned long long srv_online_max_size;
/* If this flag is TRUE, then we will use the native aio of the
OS (provided we compiled Innobase with it in), otherwise we will
use simulated aio we build below with threads.
Currently we support native aio on windows and linux */
my_bool srv_use_native_aio;
my_bool srv_numa_interleave;
/** copy of innodb_use_atomic_writes; @see innobase_init() */
my_bool srv_use_atomic_writes;
/** innodb_compression_algorithm; used with page compression */
ulong innodb_compression_algorithm;
#ifdef UNIV_DEBUG
/** Used by SET GLOBAL innodb_master_thread_disabled_debug = X. */
my_bool srv_master_thread_disabled_debug;
/** Event used to inform that master thread is disabled. */
static os_event_t srv_master_thread_disabled_event;
#endif /* UNIV_DEBUG */
/*------------------------- LOG FILES ------------------------ */
char* srv_log_group_home_dir;
ulong srv_n_log_files;
/** The InnoDB redo log file size, or 0 when changing the redo log format
at startup (while disallowing writes to the redo log). */
ulonglong srv_log_file_size;
/** copy of innodb_log_buffer_size, but in database pages */
ulint srv_log_buffer_size;
/** innodb_flush_log_at_trx_commit */
ulong srv_flush_log_at_trx_commit;
/** innodb_flush_log_at_timeout */
uint srv_flush_log_at_timeout;
/** innodb_page_size */
ulong srv_page_size;
/** log2 of innodb_page_size; @see innobase_init() */
ulong srv_page_size_shift;
/** innodb_log_write_ahead_size */
ulong srv_log_write_ahead_size;
page_size_t univ_page_size(0, 0, false);
/** innodb_adaptive_flushing; try to flush dirty pages so as to avoid
IO bursts at the checkpoints. */
my_bool srv_adaptive_flushing;
/** innodb_flush_sync; whether to ignore io_capacity at log checkpoints */
my_bool srv_flush_sync;
/** Maximum number of times allowed to conditionally acquire
mutex before switching to blocking wait on the mutex */
#define MAX_MUTEX_NOWAIT 20
/** Check whether the number of failed nonblocking mutex
acquisition attempts exceeds maximum allowed value. If so,
srv_printf_innodb_monitor() will request mutex acquisition
with mutex_enter(), which will wait until it gets the mutex. */
#define MUTEX_NOWAIT(mutex_skipped) ((mutex_skipped) < MAX_MUTEX_NOWAIT)
#ifdef WITH_INNODB_DISALLOW_WRITES
UNIV_INTERN os_event_t srv_allow_writes_event;
#endif /* WITH_INNODB_DISALLOW_WRITES */
/** copy of innodb_buffer_pool_size */
ulint srv_buf_pool_size;
const ulint srv_buf_pool_min_size = 5 * 1024 * 1024;
/** Default pool size in bytes */
const ulint srv_buf_pool_def_size = 128 * 1024 * 1024;
/** Requested buffer pool chunk size. Each buffer pool instance consists
of one or more chunks. */
ulong srv_buf_pool_chunk_unit;
/** innodb_buffer_pool_instances (0 is interpreted as 1) */
ulong srv_buf_pool_instances;
/** Default value of innodb_buffer_pool_instances */
const ulong srv_buf_pool_instances_default = 0;
/** innodb_page_hash_locks (a debug-only parameter);
number of locks to protect buf_pool->page_hash */
ulong srv_n_page_hash_locks = 16;
/** innodb_lru_scan_depth; number of blocks scanned in LRU flush batch */
ulong srv_LRU_scan_depth;
/** innodb_flush_neighbors; whether or not to flush neighbors of a block */
ulong srv_flush_neighbors;
/** Previously requested size */
ulint srv_buf_pool_old_size;
/** Current size as scaling factor for the other components */
ulint srv_buf_pool_base_size;
/** Current size in bytes */
ulint srv_buf_pool_curr_size;
/** Dump this % of each buffer pool during BP dump */
ulong srv_buf_pool_dump_pct;
/** Lock table size in bytes */
ulint srv_lock_table_size = ULINT_MAX;
/** innodb_idle_flush_pct */
ulong srv_idle_flush_pct;
/** copy of innodb_read_io_threads */
ulint srv_n_read_io_threads;
/** copy of innodb_write_io_threads */
ulint srv_n_write_io_threads;
/** innodb_random_read_ahead */
my_bool srv_random_read_ahead;
/** innodb_read_ahead_threshold; the number of pages that must be present
in the buffer cache and accessed sequentially for InnoDB to trigger a
readahead request. */
ulong srv_read_ahead_threshold;
/** innodb_change_buffer_max_size; maximum on-disk size of change
buffer in terms of percentage of the buffer pool. */
uint srv_change_buffer_max_size;
char* srv_file_flush_method_str;
enum srv_flush_t srv_file_flush_method = IF_WIN(SRV_ALL_O_DIRECT_FSYNC,SRV_FSYNC);
/** copy of innodb_open_files, initialized by innobase_init() */
ulint srv_max_n_open_files;
/** innodb_io_capacity */
ulong srv_io_capacity;
/** innodb_io_capacity_max */
ulong srv_max_io_capacity;
/** innodb_page_cleaners; the number of page cleaner threads */
ulong srv_n_page_cleaners;
/* The InnoDB main thread tries to keep the ratio of modified pages
in the buffer pool to all database pages in the buffer pool smaller than
the following number. But it is not guaranteed that the value stays below
that during a time of heavy update/insert activity. */
/** innodb_max_dirty_pages_pct */
double srv_max_buf_pool_modified_pct;
/** innodb_max_dirty_pages_pct_lwm */
double srv_max_dirty_pages_pct_lwm;
/** innodb_adaptive_flushing_lwm; the percentage of log capacity at
which adaptive flushing, if enabled, will kick in. */
double srv_adaptive_flushing_lwm;
/** innodb_flushing_avg_loops; number of iterations over which
adaptive flushing is averaged */
ulong srv_flushing_avg_loops;
/** innodb_purge_threads; the number of purge threads to use */
ulong srv_n_purge_threads;
/** innodb_purge_batch_size, in pages */
ulong srv_purge_batch_size;
/** innodb_stats_method decides how InnoDB treats
NULL value when collecting statistics. By default, it is set to
SRV_STATS_NULLS_EQUAL(0), ie. all NULL value are treated equal */
ulong srv_innodb_stats_method;
srv_stats_t srv_stats;
/* structure to pass status variables to MySQL */
export_var_t export_vars;
/** Normally 0. When nonzero, skip some phases of crash recovery,
starting from SRV_FORCE_IGNORE_CORRUPT, so that data can be recovered
by SELECT or mysqldump. When this is nonzero, we do not allow any user
modifications to the data. */
ulong srv_force_recovery;
/** innodb_print_all_deadlocks; whether to print all user-level
transactions deadlocks to the error log */
my_bool srv_print_all_deadlocks;
/** innodb_cmp_per_index_enabled; enable
INFORMATION_SCHEMA.innodb_cmp_per_index */
my_bool srv_cmp_per_index_enabled;
/** innodb_fast_shutdown; if 1 then we do not run purge and insert buffer
merge to completion before shutdown. If it is set to 2, do not even flush the
buffer pool to data files at the shutdown: we effectively 'crash'
InnoDB (but lose no committed transactions). */
uint srv_fast_shutdown;
/** copy of innodb_status_file; generate a innodb_status.<pid> file */
ibool srv_innodb_status;
/** innodb_prefix_index_cluster_optimization; whether to optimize
prefix index queries to skip cluster index lookup when possible */
my_bool srv_prefix_index_cluster_optimization;
/** innodb_stats_transient_sample_pages;
When estimating number of different key values in an index, sample
this many index pages, there are 2 ways to calculate statistics:
* persistent stats that are calculated by ANALYZE TABLE and saved
in the innodb database.
* quick transient stats, that are used if persistent stats for the given
table/index are not found in the innodb database */
unsigned long long srv_stats_transient_sample_pages;
/** innodb_stats_persistent */
my_bool srv_stats_persistent;
/** innodb_stats_include_delete_marked */
my_bool srv_stats_include_delete_marked;
/** innodb_stats_persistent_sample_pages */
unsigned long long srv_stats_persistent_sample_pages;
/** innodb_stats_auto_recalc */
my_bool srv_stats_auto_recalc;
/** innodb_stats_modified_counter; The number of rows modified before
we calculate new statistics (default 0 = current limits) */
unsigned long long srv_stats_modified_counter;
/** innodb_stats_traditional; enable traditional statistic calculation
based on number of configured pages */
my_bool srv_stats_sample_traditional;
/** copy of innodb_doublewrite */
ibool srv_use_doublewrite_buf;
/** innodb_doublewrite_batch_size (a debug parameter) specifies the
number of pages to use in LRU and flush_list batch flushing.
The rest of the doublewrite buffer is used for single-page flushing. */
ulong srv_doublewrite_batch_size = 120;
/** innodb_replication_delay */
ulong srv_replication_delay;
/** innodb_sync_spin_loops */
ulong srv_n_spin_wait_rounds;
/** innodb_spin_wait_delay */
uint srv_spin_wait_delay;
static ulint srv_n_rows_inserted_old;
static ulint srv_n_rows_updated_old;
static ulint srv_n_rows_deleted_old;
static ulint srv_n_rows_read_old;
static ulint srv_n_system_rows_inserted_old;
static ulint srv_n_system_rows_updated_old;
static ulint srv_n_system_rows_deleted_old;
static ulint srv_n_system_rows_read_old;
ulint srv_truncated_status_writes;
/** Number of initialized rollback segments for persistent undo log */
ulong srv_available_undo_logs;
UNIV_INTERN ib_uint64_t srv_page_compression_saved;
UNIV_INTERN ib_uint64_t srv_page_compression_trim_sect512;
UNIV_INTERN ib_uint64_t srv_page_compression_trim_sect4096;
UNIV_INTERN ib_uint64_t srv_index_pages_written;
UNIV_INTERN ib_uint64_t srv_non_index_pages_written;
UNIV_INTERN ib_uint64_t srv_pages_page_compressed;
UNIV_INTERN ib_uint64_t srv_page_compressed_trim_op;
UNIV_INTERN ib_uint64_t srv_page_compressed_trim_op_saved;
UNIV_INTERN ib_uint64_t srv_index_page_decompressed;
/* Defragmentation */
UNIV_INTERN my_bool srv_defragment;
/** innodb_defragment_n_pages */
UNIV_INTERN uint srv_defragment_n_pages;
UNIV_INTERN uint srv_defragment_stats_accuracy;
/** innodb_defragment_fill_factor_n_recs */
UNIV_INTERN uint srv_defragment_fill_factor_n_recs;
/** innodb_defragment_fill_factor */
UNIV_INTERN double srv_defragment_fill_factor;
/** innodb_defragment_frequency */
UNIV_INTERN uint srv_defragment_frequency;
/** derived from innodb_defragment_frequency;
@see innodb_defragment_frequency_update() */
UNIV_INTERN ulonglong srv_defragment_interval;
/** Current mode of operation */
UNIV_INTERN enum srv_operation_mode srv_operation;
/* Set the following to 0 if you want InnoDB to write messages on
stderr on startup/shutdown. Not enabled on the embedded server. */
ibool srv_print_verbose_log;
my_bool srv_print_innodb_monitor;
my_bool srv_print_innodb_lock_monitor;
my_bool srv_print_innodb_tablespace_monitor;
my_bool srv_print_innodb_table_monitor;
/** innodb_force_primary_key; whether to disallow CREATE TABLE without
PRIMARY KEY */
my_bool srv_force_primary_key;
/* Array of English strings describing the current state of an
i/o handler thread */
const char* srv_io_thread_op_info[SRV_MAX_N_IO_THREADS];
const char* srv_io_thread_function[SRV_MAX_N_IO_THREADS];
static time_t srv_last_monitor_time;
static ib_mutex_t srv_innodb_monitor_mutex;
/** Mutex protecting page_zip_stat_per_index */
ib_mutex_t page_zip_stat_per_index_mutex;
/* Mutex for locking srv_monitor_file. Not created if srv_read_only_mode */
ib_mutex_t srv_monitor_file_mutex;
/** Temporary file for innodb monitor output */
FILE* srv_monitor_file;
/** Mutex for locking srv_misc_tmpfile. Not created if srv_read_only_mode.
This mutex has a very low rank; threads reserving it should not
acquire any further latches or sleep before releasing this one. */
ib_mutex_t srv_misc_tmpfile_mutex;
/** Temporary file for miscellanous diagnostic output */
FILE* srv_misc_tmpfile;
static ulint srv_main_thread_process_no;
static ulint srv_main_thread_id;
/* The following counts are used by the srv_master_thread. */
/** Iterations of the loop bounded by 'srv_active' label. */
static ulint srv_main_active_loops;
/** Iterations of the loop bounded by the 'srv_idle' label. */
static ulint srv_main_idle_loops;
/** Iterations of the loop bounded by the 'srv_shutdown' label. */
static ulint srv_main_shutdown_loops;
/** Log writes involving flush. */
static ulint srv_log_writes_and_flush;
/* This is only ever touched by the master thread. It records the
time when the last flush of log file has happened. The master
thread ensures that we flush the log files at least once per
second. */
static time_t srv_last_log_flush_time;
/* Interval in seconds at which various tasks are performed by the
master thread when server is active. In order to balance the workload,
we should try to keep intervals such that they are not multiple of
each other. For example, if we have intervals for various tasks
defined as 5, 10, 15, 60 then all tasks will be performed when
current_time % 60 == 0 and no tasks will be performed when
current_time % 5 != 0. */
# define SRV_MASTER_CHECKPOINT_INTERVAL (7)
# define SRV_MASTER_PURGE_INTERVAL (10)
# define SRV_MASTER_DICT_LRU_INTERVAL (47)
/** Simulate compression failures. */
UNIV_INTERN uint srv_simulate_comp_failures;
/** Buffer pool dump status frequence in percentages */
UNIV_INTERN ulong srv_buf_dump_status_frequency;
/** Acquire the system_mutex. */
#define srv_sys_mutex_enter() do { \
mutex_enter(&srv_sys.mutex); \
} while (0)
/** Test if the system mutex is owned. */
#define srv_sys_mutex_own() (mutex_own(&srv_sys.mutex) \
&& !srv_read_only_mode)
/** Release the system mutex. */
#define srv_sys_mutex_exit() do { \
mutex_exit(&srv_sys.mutex); \
} while (0)
/*
IMPLEMENTATION OF THE SERVER MAIN PROGRAM
=========================================
There is the following analogue between this database
server and an operating system kernel:
DB concept equivalent OS concept
---------- ---------------------
transaction -- process;
query thread -- thread;
lock -- semaphore;
kernel -- kernel;
query thread execution:
(a) without lock mutex
reserved -- process executing in user mode;
(b) with lock mutex reserved
-- process executing in kernel mode;
The server has several backgroind threads all running at the same
priority as user threads. It periodically checks if here is anything
happening in the server which requires intervention of the master
thread. Such situations may be, for example, when flushing of dirty
blocks is needed in the buffer pool or old version of database rows
have to be cleaned away (purged). The user can configure a separate
dedicated purge thread(s) too, in which case the master thread does not
do any purging.
The threads which we call user threads serve the queries of the MySQL
server. They run at normal priority.
When there is no activity in the system, also the master thread
suspends itself to wait for an event making the server totally silent.
There is still one complication in our server design. If a
background utility thread obtains a resource (e.g., mutex) needed by a user
thread, and there is also some other user activity in the system,
the user thread may have to wait indefinitely long for the
resource, as the OS does not schedule a background thread if
there is some other runnable user thread. This problem is called
priority inversion in real-time programming.
One solution to the priority inversion problem would be to keep record
of which thread owns which resource and in the above case boost the
priority of the background thread so that it will be scheduled and it
can release the resource. This solution is called priority inheritance
in real-time programming. A drawback of this solution is that the overhead
of acquiring a mutex increases slightly, maybe 0.2 microseconds on a 100
MHz Pentium, because the thread has to call os_thread_get_curr_id. This may
be compared to 0.5 microsecond overhead for a mutex lock-unlock pair. Note
that the thread cannot store the information in the resource , say mutex,
itself, because competing threads could wipe out the information if it is
stored before acquiring the mutex, and if it stored afterwards, the
information is outdated for the time of one machine instruction, at least.
(To be precise, the information could be stored to lock_word in mutex if
the machine supports atomic swap.)
The above solution with priority inheritance may become actual in the
future, currently we do not implement any priority twiddling solution.
Our general aim is to reduce the contention of all mutexes by making
them more fine grained.
The thread table contains information of the current status of each
thread existing in the system, and also the event semaphores used in
suspending the master thread and utility threads when they have nothing
to do. The thread table can be seen as an analogue to the process table
in a traditional Unix implementation. */
/** The server system struct */
struct srv_sys_t{
ib_mutex_t tasks_mutex; /*!< variable protecting the
tasks queue */
UT_LIST_BASE_NODE_T(que_thr_t)
tasks; /*!< task queue */
ib_mutex_t mutex; /*!< variable protecting the
fields below. */
ulint n_sys_threads; /*!< size of the sys_threads
array */
srv_slot_t sys_threads[32 + 1]; /*!< server thread table;
os_event_set() and
os_event_reset() on
sys_threads[]->event are
covered by srv_sys_t::mutex */
ulint n_threads_active[SRV_MASTER + 1];
/*!< number of threads active
in a thread class; protected
by both my_atomic_addlint()
and mutex */
srv_stats_t::ulint_ctr_1_t
activity_count; /*!< For tracking server
activity */
};
static srv_sys_t srv_sys;
/** Event to signal srv_monitor_thread. Not protected by a mutex.
Set after setting srv_print_innodb_monitor. */
os_event_t srv_monitor_event;
/** Event to signal the shutdown of srv_error_monitor_thread.
Not protected by a mutex. */
os_event_t srv_error_event;
/** Event for waking up buf_dump_thread. Not protected by a mutex.
Set on shutdown or by buf_dump_start() or buf_load_start(). */
os_event_t srv_buf_dump_event;
/** Event to signal the buffer pool resize thread */
os_event_t srv_buf_resize_event;
/** The buffer pool dump/load file name */
char* srv_buf_dump_filename;
/** Boolean config knobs that tell InnoDB to dump the buffer pool at shutdown
and/or load it during startup. */
char srv_buffer_pool_dump_at_shutdown = TRUE;
char srv_buffer_pool_load_at_startup = TRUE;
/** Slot index in the srv_sys.sys_threads array for the purge thread. */
static const ulint SRV_PURGE_SLOT = 1;
/** Slot index in the srv_sys.sys_threads array for the master thread. */
static const ulint SRV_MASTER_SLOT = 0;
#ifdef HAVE_PSI_STAGE_INTERFACE
/** Performance schema stage event for monitoring ALTER TABLE progress
everything after flush log_make_checkpoint_at(). */
PSI_stage_info srv_stage_alter_table_end
= {0, "alter table (end)", PSI_FLAG_STAGE_PROGRESS};
/** Performance schema stage event for monitoring ALTER TABLE progress
log_make_checkpoint_at(). */
PSI_stage_info srv_stage_alter_table_flush
= {0, "alter table (flush)", PSI_FLAG_STAGE_PROGRESS};
/** Performance schema stage event for monitoring ALTER TABLE progress
row_merge_insert_index_tuples(). */
PSI_stage_info srv_stage_alter_table_insert
= {0, "alter table (insert)", PSI_FLAG_STAGE_PROGRESS};
/** Performance schema stage event for monitoring ALTER TABLE progress
row_log_apply(). */
PSI_stage_info srv_stage_alter_table_log_index
= {0, "alter table (log apply index)", PSI_FLAG_STAGE_PROGRESS};
/** Performance schema stage event for monitoring ALTER TABLE progress
row_log_table_apply(). */
PSI_stage_info srv_stage_alter_table_log_table
= {0, "alter table (log apply table)", PSI_FLAG_STAGE_PROGRESS};
/** Performance schema stage event for monitoring ALTER TABLE progress
row_merge_sort(). */
PSI_stage_info srv_stage_alter_table_merge_sort
= {0, "alter table (merge sort)", PSI_FLAG_STAGE_PROGRESS};
/** Performance schema stage event for monitoring ALTER TABLE progress
row_merge_read_clustered_index(). */
PSI_stage_info srv_stage_alter_table_read_pk_internal_sort
= {0, "alter table (read PK and internal sort)", PSI_FLAG_STAGE_PROGRESS};
/** Performance schema stage event for monitoring buffer pool load progress. */
PSI_stage_info srv_stage_buffer_pool_load
= {0, "buffer pool load", PSI_FLAG_STAGE_PROGRESS};
#endif /* HAVE_PSI_STAGE_INTERFACE */
/*********************************************************************//**
Prints counters for work done by srv_master_thread. */
static
void
srv_print_master_thread_info(
/*=========================*/
FILE *file) /* in: output stream */
{
fprintf(file, "srv_master_thread loops: " ULINTPF " srv_active, "
ULINTPF " srv_shutdown, " ULINTPF " srv_idle\n"
"srv_master_thread log flush and writes: " ULINTPF "\n",
srv_main_active_loops,
srv_main_shutdown_loops,
srv_main_idle_loops,
srv_log_writes_and_flush);
}
/*********************************************************************//**
Sets the info describing an i/o thread current state. */
void
srv_set_io_thread_op_info(
/*======================*/
ulint i, /*!< in: the 'segment' of the i/o thread */
const char* str) /*!< in: constant char string describing the
state */
{
ut_a(i < SRV_MAX_N_IO_THREADS);
srv_io_thread_op_info[i] = str;
}
/*********************************************************************//**
Resets the info describing an i/o thread current state. */
void
srv_reset_io_thread_op_info()
/*=========================*/
{
for (ulint i = 0; i < UT_ARR_SIZE(srv_io_thread_op_info); ++i) {
srv_io_thread_op_info[i] = "not started yet";
}
}
#ifdef UNIV_DEBUG
/*********************************************************************//**
Validates the type of a thread table slot.
@return TRUE if ok */
static
ibool
srv_thread_type_validate(
/*=====================*/
srv_thread_type type) /*!< in: thread type */
{
switch (type) {
case SRV_NONE:
break;
case SRV_WORKER:
case SRV_PURGE:
case SRV_MASTER:
return(TRUE);
}
ut_error;
return(FALSE);
}
#endif /* UNIV_DEBUG */
/*********************************************************************//**
Gets the type of a thread table slot.
@return thread type */
static
srv_thread_type
srv_slot_get_type(
/*==============*/
const srv_slot_t* slot) /*!< in: thread slot */
{
srv_thread_type type = slot->type;
ut_ad(srv_thread_type_validate(type));
return(type);
}
/*********************************************************************//**
Reserves a slot in the thread table for the current thread.
@return reserved slot */
static
srv_slot_t*
srv_reserve_slot(
/*=============*/
srv_thread_type type) /*!< in: type of the thread */
{
srv_slot_t* slot = 0;
srv_sys_mutex_enter();
ut_ad(srv_thread_type_validate(type));
switch (type) {
case SRV_MASTER:
slot = &srv_sys.sys_threads[SRV_MASTER_SLOT];
break;
case SRV_PURGE:
slot = &srv_sys.sys_threads[SRV_PURGE_SLOT];
break;
case SRV_WORKER:
/* Find an empty slot, skip the master and purge slots. */
for (slot = &srv_sys.sys_threads[2];
slot->in_use;
++slot) {
ut_a(slot < &srv_sys.sys_threads[
srv_sys.n_sys_threads]);
}
break;
case SRV_NONE:
ut_error;
}
ut_a(!slot->in_use);
slot->in_use = TRUE;
slot->suspended = FALSE;
slot->type = type;
ut_ad(srv_slot_get_type(slot) == type);
my_atomic_addlint(&srv_sys.n_threads_active[type], 1);
srv_sys_mutex_exit();
return(slot);
}
/*********************************************************************//**
Suspends the calling thread to wait for the event in its thread slot.
@return the current signal count of the event. */
static
int64_t
srv_suspend_thread_low(
/*===================*/
srv_slot_t* slot) /*!< in/out: thread slot */
{
ut_ad(!srv_read_only_mode);
ut_ad(srv_sys_mutex_own());
ut_ad(slot->in_use);
srv_thread_type type = srv_slot_get_type(slot);
switch (type) {
case SRV_NONE:
ut_error;
case SRV_MASTER:
/* We have only one master thread and it
should be the first entry always. */
ut_a(srv_sys.n_threads_active[type] == 1);
break;
case SRV_PURGE:
/* We have only one purge coordinator thread
and it should be the second entry always. */
ut_a(srv_sys.n_threads_active[type] == 1);
break;
case SRV_WORKER:
ut_a(srv_n_purge_threads > 1);
break;
}
ut_a(!slot->suspended);
slot->suspended = TRUE;
if (my_atomic_addlint(&srv_sys.n_threads_active[type], -1) < 0) {
ut_error;
}
return(os_event_reset(slot->event));
}
/*********************************************************************//**
Suspends the calling thread to wait for the event in its thread slot.
@return the current signal count of the event. */
static
int64_t
srv_suspend_thread(
/*===============*/
srv_slot_t* slot) /*!< in/out: thread slot */
{
srv_sys_mutex_enter();
int64_t sig_count = srv_suspend_thread_low(slot);
srv_sys_mutex_exit();
return(sig_count);
}
/** Resume the calling thread.
@param[in,out] slot thread slot
@param[in] sig_count signal count (if wait)
@param[in] wait whether to wait for the event
@param[in] timeout_usec timeout in microseconds (0=infinite)
@return whether the wait timed out */
static
bool
srv_resume_thread(srv_slot_t* slot, int64_t sig_count = 0, bool wait = true,
ulint timeout_usec = 0)
{
bool timeout;
ut_ad(!srv_read_only_mode);
ut_ad(slot->in_use);
ut_ad(slot->suspended);
if (!wait) {
timeout = false;
} else if (timeout_usec) {
timeout = OS_SYNC_TIME_EXCEEDED == os_event_wait_time_low(
slot->event, timeout_usec, sig_count);
} else {
timeout = false;
os_event_wait_low(slot->event, sig_count);
}
srv_sys_mutex_enter();
ut_ad(slot->in_use);
ut_ad(slot->suspended);
slot->suspended = FALSE;
my_atomic_addlint(&srv_sys.n_threads_active[slot->type], 1);
srv_sys_mutex_exit();
return(timeout);
}
/** Ensure that a given number of threads of the type given are running
(or are already terminated).
@param[in] type thread type
@param[in] n number of threads that have to run */
void
srv_release_threads(enum srv_thread_type type, ulint n)
{
ulint running;
ut_ad(srv_thread_type_validate(type));
ut_ad(n > 0);
do {
running = 0;
srv_sys_mutex_enter();
for (ulint i = 0; i < srv_sys.n_sys_threads; i++) {
srv_slot_t* slot = &srv_sys.sys_threads[i];
if (!slot->in_use || srv_slot_get_type(slot) != type) {
continue;
} else if (!slot->suspended) {
if (++running >= n) {
break;
}
continue;
}
switch (type) {
case SRV_NONE:
ut_error;
case SRV_MASTER:
/* We have only one master thread and it
should be the first entry always. */
ut_a(n == 1);
ut_a(i == SRV_MASTER_SLOT);
ut_a(srv_sys.n_threads_active[type] == 0);
break;
case SRV_PURGE:
/* We have only one purge coordinator thread
and it should be the second entry always. */
ut_a(n == 1);
ut_a(i == SRV_PURGE_SLOT);
ut_a(srv_n_purge_threads > 0);
ut_a(srv_sys.n_threads_active[type] == 0);
break;
case SRV_WORKER:
ut_a(srv_n_purge_threads > 1);
ut_a(srv_sys.n_threads_active[type]
< srv_n_purge_threads - 1);
break;
}
os_event_set(slot->event);
}
srv_sys_mutex_exit();
} while (running && running < n);
}
/*********************************************************************//**
Release a thread's slot. */
static
void
srv_free_slot(
/*==========*/
srv_slot_t* slot) /*!< in/out: thread slot */
{
srv_sys_mutex_enter();
/* Mark the thread as inactive. */
srv_suspend_thread_low(slot);
/* Free the slot for reuse. */
ut_ad(slot->in_use);
slot->in_use = FALSE;
srv_sys_mutex_exit();
}
/** Initialize the server. */
static
void
srv_init()
{
mutex_create(LATCH_ID_SRV_INNODB_MONITOR, &srv_innodb_monitor_mutex);
srv_sys.n_sys_threads = srv_read_only_mode
? 0
: srv_n_purge_threads + 1/* purge coordinator */;
if (!srv_read_only_mode) {
mutex_create(LATCH_ID_SRV_SYS, &srv_sys.mutex);
mutex_create(LATCH_ID_SRV_SYS_TASKS, &srv_sys.tasks_mutex);
for (ulint i = 0; i < srv_sys.n_sys_threads; ++i) {
srv_slot_t* slot = &srv_sys.sys_threads[i];
slot->event = os_event_create(0);
ut_a(slot->event);
}
srv_error_event = os_event_create(0);
srv_monitor_event = os_event_create(0);
srv_buf_dump_event = os_event_create(0);
buf_flush_event = os_event_create("buf_flush_event");
UT_LIST_INIT(srv_sys.tasks, &que_thr_t::queue);
}
srv_buf_resize_event = os_event_create(0);
ut_d(srv_master_thread_disabled_event = os_event_create(0));
/* page_zip_stat_per_index_mutex is acquired from:
1. page_zip_compress() (after SYNC_FSP)
2. page_zip_decompress()
3. i_s_cmp_per_index_fill_low() (where SYNC_DICT is acquired)
4. innodb_cmp_per_index_update(), no other latches
since we do not acquire any other latches while holding this mutex,
it can have very low level. We pick SYNC_ANY_LATCH for it. */
mutex_create(LATCH_ID_PAGE_ZIP_STAT_PER_INDEX,
&page_zip_stat_per_index_mutex);
/* Create dummy indexes for infimum and supremum records */
dict_ind_init();
#ifdef WITH_INNODB_DISALLOW_WRITES
/* Writes have to be enabled on init or else we hang. Thus, we
always set the event here regardless of innobase_disallow_writes.
That flag will always be 0 at this point because it isn't settable
via my.cnf or command line arg. */
srv_allow_writes_event = os_event_create(0);
os_event_set(srv_allow_writes_event);
#endif /* WITH_INNODB_DISALLOW_WRITES */
/* Initialize some INFORMATION SCHEMA internal structures */
trx_i_s_cache_init(trx_i_s_cache);
ut_crc32_init();
dict_mem_init();
}
/*********************************************************************//**
Frees the data structures created in srv_init(). */
void
srv_free(void)
/*==========*/
{
if (!srv_buf_resize_event) {
return;
}
mutex_free(&srv_innodb_monitor_mutex);
mutex_free(&page_zip_stat_per_index_mutex);
if (!srv_read_only_mode) {
mutex_free(&srv_sys.mutex);
mutex_free(&srv_sys.tasks_mutex);
for (ulint i = 0; i < srv_sys.n_sys_threads; ++i) {
os_event_destroy(srv_sys.sys_threads[i].event);
}
os_event_destroy(srv_error_event);
os_event_destroy(srv_monitor_event);
os_event_destroy(srv_buf_dump_event);
os_event_destroy(buf_flush_event);
}
os_event_destroy(srv_buf_resize_event);
ut_d(os_event_destroy(srv_master_thread_disabled_event));
dict_ind_free();
trx_i_s_cache_free(trx_i_s_cache);
}
/*********************************************************************//**
Normalizes init parameter values to use units we use inside InnoDB. */
static
void
srv_normalize_init_values(void)
/*===========================*/
{
srv_sys_space.normalize();
srv_tmp_space.normalize();
srv_log_buffer_size /= UNIV_PAGE_SIZE;
srv_lock_table_size = 5 * (srv_buf_pool_size / UNIV_PAGE_SIZE);
}
/*********************************************************************//**
Boots the InnoDB server. */
void
srv_boot(void)
/*==========*/
{
/* Transform the init parameter values given by MySQL to
use units we use inside InnoDB: */
srv_normalize_init_values();
sync_check_init();
/* Reset the system variables in the recovery module. */
recv_sys_var_init();
trx_pool_init();
row_mysql_init();
/* Initialize this module */
srv_init();
}
/******************************************************************//**
Refreshes the values used to calculate per-second averages. */
static
void
srv_refresh_innodb_monitor_stats(void)
/*==================================*/
{
mutex_enter(&srv_innodb_monitor_mutex);
srv_last_monitor_time = time(NULL);
os_aio_refresh_stats();
#ifdef BTR_CUR_HASH_ADAPT
btr_cur_n_sea_old = btr_cur_n_sea;
#endif /* BTR_CUR_HASH_ADAPT */
btr_cur_n_non_sea_old = btr_cur_n_non_sea;
log_refresh_stats();
buf_refresh_io_stats_all();
srv_n_rows_inserted_old = srv_stats.n_rows_inserted;
srv_n_rows_updated_old = srv_stats.n_rows_updated;
srv_n_rows_deleted_old = srv_stats.n_rows_deleted;
srv_n_rows_read_old = srv_stats.n_rows_read;
srv_n_system_rows_inserted_old = srv_stats.n_system_rows_inserted;
srv_n_system_rows_updated_old = srv_stats.n_system_rows_updated;
srv_n_system_rows_deleted_old = srv_stats.n_system_rows_deleted;
srv_n_system_rows_read_old = srv_stats.n_system_rows_read;
mutex_exit(&srv_innodb_monitor_mutex);
}
/******************************************************************//**
Outputs to a file the output of the InnoDB Monitor.
@return FALSE if not all information printed
due to failure to obtain necessary mutex */
ibool
srv_printf_innodb_monitor(
/*======================*/
FILE* file, /*!< in: output stream */
ibool nowait, /*!< in: whether to wait for the
lock_sys_t:: mutex */
ulint* trx_start_pos, /*!< out: file position of the start of
the list of active transactions */
ulint* trx_end) /*!< out: file position of the end of
the list of active transactions */
{
double time_elapsed;
time_t current_time;
ulint n_reserved;
ibool ret;
mutex_enter(&srv_innodb_monitor_mutex);
current_time = time(NULL);
/* We add 0.001 seconds to time_elapsed to prevent division
by zero if two users happen to call SHOW ENGINE INNODB STATUS at the
same time */
time_elapsed = difftime(current_time, srv_last_monitor_time)
+ 0.001;
srv_last_monitor_time = time(NULL);
fputs("\n=====================================\n", file);
ut_print_timestamp(file);
fprintf(file,
" INNODB MONITOR OUTPUT\n"
"=====================================\n"
"Per second averages calculated from the last %lu seconds\n",
(ulong) time_elapsed);
fputs("-----------------\n"
"BACKGROUND THREAD\n"
"-----------------\n", file);
srv_print_master_thread_info(file);
fputs("----------\n"
"SEMAPHORES\n"
"----------\n", file);
sync_print(file);
/* Conceptually, srv_innodb_monitor_mutex has a very high latching
order level in sync0sync.h, while dict_foreign_err_mutex has a very
low level 135. Therefore we can reserve the latter mutex here without
a danger of a deadlock of threads. */
mutex_enter(&dict_foreign_err_mutex);
if (!srv_read_only_mode && ftell(dict_foreign_err_file) != 0L) {
fputs("------------------------\n"
"LATEST FOREIGN KEY ERROR\n"
"------------------------\n", file);
ut_copy_file(file, dict_foreign_err_file);
}
mutex_exit(&dict_foreign_err_mutex);
/* Only if lock_print_info_summary proceeds correctly,
before we call the lock_print_info_all_transactions
to print all the lock information. IMPORTANT NOTE: This
function acquires the lock mutex on success. */
ret = lock_print_info_summary(file, nowait);
if (ret) {
if (trx_start_pos) {
long t = ftell(file);
if (t < 0) {
*trx_start_pos = ULINT_UNDEFINED;
} else {
*trx_start_pos = (ulint) t;
}
}
/* NOTE: If we get here then we have the lock mutex. This
function will release the lock mutex that we acquired when
we called the lock_print_info_summary() function earlier. */
lock_print_info_all_transactions(file);
if (trx_end) {
long t = ftell(file);
if (t < 0) {
*trx_end = ULINT_UNDEFINED;
} else {
*trx_end = (ulint) t;
}
}
}
fputs("--------\n"
"FILE I/O\n"
"--------\n", file);
os_aio_print(file);
fputs("-------------------------------------\n"
"INSERT BUFFER AND ADAPTIVE HASH INDEX\n"
"-------------------------------------\n", file);
ibuf_print(file);
#ifdef BTR_CUR_HASH_ADAPT
for (ulint i = 0; i < btr_ahi_parts; ++i) {
rw_lock_s_lock(btr_search_latches[i]);
ha_print_info(file, btr_search_sys->hash_tables[i]);
rw_lock_s_unlock(btr_search_latches[i]);
}
fprintf(file,
"%.2f hash searches/s, %.2f non-hash searches/s\n",
(btr_cur_n_sea - btr_cur_n_sea_old)
/ time_elapsed,
(btr_cur_n_non_sea - btr_cur_n_non_sea_old)
/ time_elapsed);
btr_cur_n_sea_old = btr_cur_n_sea;
#else /* BTR_CUR_HASH_ADAPT */
fprintf(file,
"%.2f non-hash searches/s\n",
(btr_cur_n_non_sea - btr_cur_n_non_sea_old)
/ time_elapsed);
#endif /* BTR_CUR_HASH_ADAPT */
btr_cur_n_non_sea_old = btr_cur_n_non_sea;
fputs("---\n"
"LOG\n"
"---\n", file);
log_print(file);
fputs("----------------------\n"
"BUFFER POOL AND MEMORY\n"
"----------------------\n", file);
fprintf(file,
"Total large memory allocated " ULINTPF "\n"
"Dictionary memory allocated " ULINTPF "\n",
os_total_large_mem_allocated,
dict_sys_get_size());
buf_print_io(file);
fputs("--------------\n"
"ROW OPERATIONS\n"
"--------------\n", file);
fprintf(file,
ULINTPF " queries inside InnoDB, "
ULINTPF " queries in queue\n",
srv_conc_get_active_threads(),
srv_conc_get_waiting_threads());
/* This is a dirty read, without holding trx_sys->mutex. */
fprintf(file, ULINTPF " read views open inside InnoDB\n",
trx_sys->mvcc->size());
n_reserved = fil_space_get_n_reserved_extents(0);
if (n_reserved > 0) {
fprintf(file,
ULINTPF " tablespace extents now reserved for"
" B-tree split operations\n",
n_reserved);
}
fprintf(file,
"Process ID=" ULINTPF
", Main thread ID=" ULINTPF
", state: %s\n",
srv_main_thread_process_no,
srv_main_thread_id,
srv_main_thread_op_info);
fprintf(file,
"Number of rows inserted " ULINTPF
", updated " ULINTPF
", deleted " ULINTPF
", read " ULINTPF "\n",
(ulint) srv_stats.n_rows_inserted,
(ulint) srv_stats.n_rows_updated,
(ulint) srv_stats.n_rows_deleted,
(ulint) srv_stats.n_rows_read);
fprintf(file,
"%.2f inserts/s, %.2f updates/s,"
" %.2f deletes/s, %.2f reads/s\n",
((ulint) srv_stats.n_rows_inserted - srv_n_rows_inserted_old)
/ time_elapsed,
((ulint) srv_stats.n_rows_updated - srv_n_rows_updated_old)
/ time_elapsed,
((ulint) srv_stats.n_rows_deleted - srv_n_rows_deleted_old)
/ time_elapsed,
((ulint) srv_stats.n_rows_read - srv_n_rows_read_old)
/ time_elapsed);
fprintf(file,
"Number of system rows inserted " ULINTPF
", updated " ULINTPF ", deleted " ULINTPF
", read " ULINTPF "\n",
(ulint) srv_stats.n_system_rows_inserted,
(ulint) srv_stats.n_system_rows_updated,
(ulint) srv_stats.n_system_rows_deleted,
(ulint) srv_stats.n_system_rows_read);
fprintf(file,
"%.2f inserts/s, %.2f updates/s,"
" %.2f deletes/s, %.2f reads/s\n",
((ulint) srv_stats.n_system_rows_inserted
- srv_n_system_rows_inserted_old) / time_elapsed,
((ulint) srv_stats.n_system_rows_updated
- srv_n_system_rows_updated_old) / time_elapsed,
((ulint) srv_stats.n_system_rows_deleted
- srv_n_system_rows_deleted_old) / time_elapsed,
((ulint) srv_stats.n_system_rows_read
- srv_n_system_rows_read_old) / time_elapsed);
srv_n_rows_inserted_old = srv_stats.n_rows_inserted;
srv_n_rows_updated_old = srv_stats.n_rows_updated;
srv_n_rows_deleted_old = srv_stats.n_rows_deleted;
srv_n_rows_read_old = srv_stats.n_rows_read;
srv_n_system_rows_inserted_old = srv_stats.n_system_rows_inserted;
srv_n_system_rows_updated_old = srv_stats.n_system_rows_updated;
srv_n_system_rows_deleted_old = srv_stats.n_system_rows_deleted;
srv_n_system_rows_read_old = srv_stats.n_system_rows_read;
fputs("----------------------------\n"
"END OF INNODB MONITOR OUTPUT\n"
"============================\n", file);
mutex_exit(&srv_innodb_monitor_mutex);
fflush(file);
return(ret);
}
/******************************************************************//**
Function to pass InnoDB status variables to MySQL */
void
srv_export_innodb_status(void)
/*==========================*/
{
buf_pool_stat_t stat;
buf_pools_list_size_t buf_pools_list_size;
ulint LRU_len;
ulint free_len;
ulint flush_list_len;
fil_crypt_stat_t crypt_stat;
btr_scrub_stat_t scrub_stat;
buf_get_total_stat(&stat);
buf_get_total_list_len(&LRU_len, &free_len, &flush_list_len);
buf_get_total_list_size_in_bytes(&buf_pools_list_size);
if (!srv_read_only_mode) {
fil_crypt_total_stat(&crypt_stat);
btr_scrub_total_stat(&scrub_stat);
}
mutex_enter(&srv_innodb_monitor_mutex);
export_vars.innodb_data_pending_reads =
ulint(MONITOR_VALUE(MONITOR_OS_PENDING_READS));
export_vars.innodb_data_pending_writes =
ulint(MONITOR_VALUE(MONITOR_OS_PENDING_WRITES));
export_vars.innodb_data_pending_fsyncs =
fil_n_pending_log_flushes
+ fil_n_pending_tablespace_flushes;
export_vars.innodb_data_fsyncs = os_n_fsyncs;
export_vars.innodb_data_read = srv_stats.data_read;
export_vars.innodb_data_reads = os_n_file_reads;
export_vars.innodb_data_writes = os_n_file_writes;
export_vars.innodb_data_written = srv_stats.data_written;
export_vars.innodb_buffer_pool_read_requests = stat.n_page_gets;
export_vars.innodb_buffer_pool_write_requests =
srv_stats.buf_pool_write_requests;
export_vars.innodb_buffer_pool_wait_free =
srv_stats.buf_pool_wait_free;
export_vars.innodb_buffer_pool_pages_flushed =
srv_stats.buf_pool_flushed;
export_vars.innodb_buffer_pool_reads = srv_stats.buf_pool_reads;
export_vars.innodb_buffer_pool_read_ahead_rnd =
stat.n_ra_pages_read_rnd;
export_vars.innodb_buffer_pool_read_ahead =
stat.n_ra_pages_read;
export_vars.innodb_buffer_pool_read_ahead_evicted =
stat.n_ra_pages_evicted;
export_vars.innodb_buffer_pool_pages_data = LRU_len;
export_vars.innodb_buffer_pool_bytes_data =
buf_pools_list_size.LRU_bytes
+ buf_pools_list_size.unzip_LRU_bytes;
export_vars.innodb_buffer_pool_pages_dirty = flush_list_len;
export_vars.innodb_buffer_pool_bytes_dirty =
buf_pools_list_size.flush_list_bytes;
export_vars.innodb_buffer_pool_pages_free = free_len;
#ifdef UNIV_DEBUG
export_vars.innodb_buffer_pool_pages_latched =
buf_get_latched_pages_number();
#endif /* UNIV_DEBUG */
export_vars.innodb_buffer_pool_pages_total = buf_pool_get_n_pages();
export_vars.innodb_buffer_pool_pages_misc =
buf_pool_get_n_pages() - LRU_len - free_len;
#ifdef HAVE_ATOMIC_BUILTINS
export_vars.innodb_have_atomic_builtins = 1;
#else
export_vars.innodb_have_atomic_builtins = 0;
#endif
export_vars.innodb_page_size = UNIV_PAGE_SIZE;
export_vars.innodb_log_waits = srv_stats.log_waits;
export_vars.innodb_os_log_written = srv_stats.os_log_written;
export_vars.innodb_os_log_fsyncs = fil_n_log_flushes;
export_vars.innodb_os_log_pending_fsyncs = fil_n_pending_log_flushes;
export_vars.innodb_os_log_pending_writes =
srv_stats.os_log_pending_writes;
export_vars.innodb_log_write_requests = srv_stats.log_write_requests;
export_vars.innodb_log_writes = srv_stats.log_writes;
export_vars.innodb_dblwr_pages_written =
srv_stats.dblwr_pages_written;
export_vars.innodb_dblwr_writes = srv_stats.dblwr_writes;
export_vars.innodb_pages_created = stat.n_pages_created;
export_vars.innodb_pages_read = stat.n_pages_read;
export_vars.innodb_page0_read = srv_stats.page0_read;
export_vars.innodb_pages_written = stat.n_pages_written;
export_vars.innodb_row_lock_waits = srv_stats.n_lock_wait_count;
export_vars.innodb_row_lock_current_waits =
srv_stats.n_lock_wait_current_count;
export_vars.innodb_row_lock_time = srv_stats.n_lock_wait_time / 1000;
if (srv_stats.n_lock_wait_count > 0) {
export_vars.innodb_row_lock_time_avg = (ulint)
(srv_stats.n_lock_wait_time
/ 1000 / srv_stats.n_lock_wait_count);
} else {
export_vars.innodb_row_lock_time_avg = 0;
}
export_vars.innodb_row_lock_time_max =
lock_sys->n_lock_max_wait_time / 1000;
export_vars.innodb_rows_read = srv_stats.n_rows_read;
export_vars.innodb_rows_inserted = srv_stats.n_rows_inserted;
export_vars.innodb_rows_updated = srv_stats.n_rows_updated;
export_vars.innodb_rows_deleted = srv_stats.n_rows_deleted;
export_vars.innodb_system_rows_read = srv_stats.n_system_rows_read;
export_vars.innodb_system_rows_inserted =
srv_stats.n_system_rows_inserted;
export_vars.innodb_system_rows_updated =
srv_stats.n_system_rows_updated;
export_vars.innodb_system_rows_deleted =
srv_stats.n_system_rows_deleted;
export_vars.innodb_num_open_files = fil_n_file_opened;
export_vars.innodb_truncated_status_writes =
srv_truncated_status_writes;
export_vars.innodb_available_undo_logs = srv_available_undo_logs;
export_vars.innodb_page_compression_saved = srv_stats.page_compression_saved;
export_vars.innodb_index_pages_written = srv_stats.index_pages_written;
export_vars.innodb_non_index_pages_written = srv_stats.non_index_pages_written;
export_vars.innodb_pages_page_compressed = srv_stats.pages_page_compressed;
export_vars.innodb_page_compressed_trim_op = srv_stats.page_compressed_trim_op;
export_vars.innodb_pages_page_decompressed = srv_stats.pages_page_decompressed;
export_vars.innodb_pages_page_compression_error = srv_stats.pages_page_compression_error;
export_vars.innodb_pages_decrypted = srv_stats.pages_decrypted;
export_vars.innodb_pages_encrypted = srv_stats.pages_encrypted;
export_vars.innodb_n_merge_blocks_encrypted = srv_stats.n_merge_blocks_encrypted;
export_vars.innodb_n_merge_blocks_decrypted = srv_stats.n_merge_blocks_decrypted;
export_vars.innodb_n_rowlog_blocks_encrypted = srv_stats.n_rowlog_blocks_encrypted;
export_vars.innodb_n_rowlog_blocks_decrypted = srv_stats.n_rowlog_blocks_decrypted;
export_vars.innodb_defragment_compression_failures =
btr_defragment_compression_failures;
export_vars.innodb_defragment_failures = btr_defragment_failures;
export_vars.innodb_defragment_count = btr_defragment_count;
export_vars.innodb_onlineddl_rowlog_rows = onlineddl_rowlog_rows;
export_vars.innodb_onlineddl_rowlog_pct_used = onlineddl_rowlog_pct_used;
export_vars.innodb_onlineddl_pct_progress = onlineddl_pct_progress;
#ifdef UNIV_DEBUG
rw_lock_s_lock(&purge_sys->latch);
trx_id_t up_limit_id = purge_sys->view.up_limit_id();;
trx_id_t done_trx_no = purge_sys->done.trx_no;
rw_lock_s_unlock(&purge_sys->latch);
mutex_enter(&trx_sys->mutex);
trx_id_t max_trx_id = trx_sys->rw_max_trx_id;
mutex_exit(&trx_sys->mutex);
if (!done_trx_no || max_trx_id < done_trx_no - 1) {
export_vars.innodb_purge_trx_id_age = 0;
} else {
export_vars.innodb_purge_trx_id_age =
(ulint) (max_trx_id - done_trx_no + 1);
}
if (!up_limit_id
|| max_trx_id < up_limit_id) {
export_vars.innodb_purge_view_trx_id_age = 0;
} else {
export_vars.innodb_purge_view_trx_id_age =
(ulint) (max_trx_id - up_limit_id);
}
#endif /* UNIV_DEBUG */
export_vars.innodb_sec_rec_cluster_reads =
srv_stats.n_sec_rec_cluster_reads;
export_vars.innodb_sec_rec_cluster_reads_avoided =
srv_stats.n_sec_rec_cluster_reads_avoided;
if (!srv_read_only_mode) {
export_vars.innodb_encryption_rotation_pages_read_from_cache =
crypt_stat.pages_read_from_cache;
export_vars.innodb_encryption_rotation_pages_read_from_disk =
crypt_stat.pages_read_from_disk;
export_vars.innodb_encryption_rotation_pages_modified =
crypt_stat.pages_modified;
export_vars.innodb_encryption_rotation_pages_flushed =
crypt_stat.pages_flushed;
export_vars.innodb_encryption_rotation_estimated_iops =
crypt_stat.estimated_iops;
export_vars.innodb_encryption_key_requests =
srv_stats.n_key_requests;
export_vars.innodb_key_rotation_list_length =
srv_stats.key_rotation_list_length;
export_vars.innodb_scrub_page_reorganizations =
scrub_stat.page_reorganizations;
export_vars.innodb_scrub_page_splits =
scrub_stat.page_splits;
export_vars.innodb_scrub_page_split_failures_underflow =
scrub_stat.page_split_failures_underflow;
export_vars.innodb_scrub_page_split_failures_out_of_filespace =
scrub_stat.page_split_failures_out_of_filespace;
export_vars.innodb_scrub_page_split_failures_missing_index =
scrub_stat.page_split_failures_missing_index;
export_vars.innodb_scrub_page_split_failures_unknown =
scrub_stat.page_split_failures_unknown;
export_vars.innodb_scrub_log = srv_stats.n_log_scrubs;
}
mutex_exit(&srv_innodb_monitor_mutex);
}
/*********************************************************************//**
A thread which prints the info output by various InnoDB monitors.
@return a dummy parameter */
extern "C"
os_thread_ret_t
DECLARE_THREAD(srv_monitor_thread)(void*)
{
int64_t sig_count;
double time_elapsed;
time_t current_time;
time_t last_monitor_time;
time_t last_table_monitor_time;
time_t last_tablespace_monitor_time;
ulint mutex_skipped;
ibool last_srv_print_monitor;
ut_ad(!srv_read_only_mode);
#ifdef UNIV_DEBUG_THREAD_CREATION
ib::info() << "Lock timeout thread starts, id "
<< os_thread_pf(os_thread_get_curr_id());
#endif /* UNIV_DEBUG_THREAD_CREATION */
#ifdef UNIV_PFS_THREAD
pfs_register_thread(srv_monitor_thread_key);
#endif /* UNIV_PFS_THREAD */
srv_last_monitor_time = ut_time();
last_table_monitor_time = ut_time();
last_tablespace_monitor_time = ut_time();
last_monitor_time = ut_time();
mutex_skipped = 0;
last_srv_print_monitor = srv_print_innodb_monitor;
loop:
/* Wake up every 5 seconds to see if we need to print
monitor information or if signalled at shutdown. */
sig_count = os_event_reset(srv_monitor_event);
os_event_wait_time_low(srv_monitor_event, 5000000, sig_count);
current_time = ut_time();
time_elapsed = difftime(current_time, last_monitor_time);
if (time_elapsed > 15) {
last_monitor_time = ut_time();
if (srv_print_innodb_monitor) {
/* Reset mutex_skipped counter everytime
srv_print_innodb_monitor changes. This is to
ensure we will not be blocked by lock_sys->mutex
for short duration information printing,
such as requested by sync_array_print_long_waits() */
if (!last_srv_print_monitor) {
mutex_skipped = 0;
last_srv_print_monitor = TRUE;
}
if (!srv_printf_innodb_monitor(stderr,
MUTEX_NOWAIT(mutex_skipped),
NULL, NULL)) {
mutex_skipped++;
} else {
/* Reset the counter */
mutex_skipped = 0;
}
} else {
last_srv_print_monitor = FALSE;
}
/* We don't create the temp files or associated
mutexes in read-only-mode */
if (!srv_read_only_mode && srv_innodb_status) {
mutex_enter(&srv_monitor_file_mutex);
rewind(srv_monitor_file);
if (!srv_printf_innodb_monitor(srv_monitor_file,
MUTEX_NOWAIT(mutex_skipped),
NULL, NULL)) {
mutex_skipped++;
} else {
mutex_skipped = 0;
}
os_file_set_eof(srv_monitor_file);
mutex_exit(&srv_monitor_file_mutex);
}
if (srv_print_innodb_tablespace_monitor
&& difftime(current_time,
last_tablespace_monitor_time) > 60) {
last_tablespace_monitor_time = ut_time();
fputs("========================"
"========================\n",
stderr);
ut_print_timestamp(stderr);
fputs(" INNODB TABLESPACE MONITOR OUTPUT\n"
"========================"
"========================\n",
stderr);
// JAN: TODO: MySQL 5.7
//fsp_print(0);
//fputs("Validating tablespace\n", stderr);
//fsp_validate(0);
fputs("Validation ok\n"
"---------------------------------------\n"
"END OF INNODB TABLESPACE MONITOR OUTPUT\n"
"=======================================\n",
stderr);
}
if (srv_print_innodb_table_monitor
&& difftime(current_time, last_table_monitor_time) > 60) {
last_table_monitor_time = ut_time();
// fprintf(stderr, "Warning: %s\n",
// DEPRECATED_MSG_INNODB_TABLE_MONITOR);
fputs("===========================================\n",
stderr);
ut_print_timestamp(stderr);
fputs(" INNODB TABLE MONITOR OUTPUT\n"
"===========================================\n",
stderr);
// dict_print();
fputs("-----------------------------------\n"
"END OF INNODB TABLE MONITOR OUTPUT\n"
"==================================\n",
stderr);
//fprintf(stderr, "Warning: %s\n",
// DEPRECATED_MSG_INNODB_TABLE_MONITOR);
}
}
if (srv_shutdown_state != SRV_SHUTDOWN_NONE) {
goto exit_func;
}
if (srv_print_innodb_monitor
|| srv_print_innodb_lock_monitor
|| srv_print_innodb_tablespace_monitor
|| srv_print_innodb_table_monitor) {
goto loop;
}
goto loop;
exit_func:
srv_monitor_active = false;
/* We count the number of threads in os_thread_exit(). A created
thread should always use that to exit and not use return() to exit. */
os_thread_exit();
OS_THREAD_DUMMY_RETURN;
}
/*********************************************************************//**
A thread which prints warnings about semaphore waits which have lasted
too long. These can be used to track bugs which cause hangs.
@return a dummy parameter */
extern "C"
os_thread_ret_t
DECLARE_THREAD(srv_error_monitor_thread)(void*)
{
/* number of successive fatal timeouts observed */
ulint fatal_cnt = 0;
lsn_t old_lsn;
lsn_t new_lsn;
int64_t sig_count;
/* longest waiting thread for a semaphore */
os_thread_id_t waiter = os_thread_get_curr_id();
os_thread_id_t old_waiter = waiter;
/* the semaphore that is being waited for */
const void* sema = NULL;
const void* old_sema = NULL;
ut_ad(!srv_read_only_mode);
old_lsn = srv_start_lsn;
#ifdef UNIV_DEBUG_THREAD_CREATION
ib::info() << "Error monitor thread starts, id "
<< os_thread_pf(os_thread_get_curr_id());
#endif /* UNIV_DEBUG_THREAD_CREATION */
#ifdef UNIV_PFS_THREAD
pfs_register_thread(srv_error_monitor_thread_key);
#endif /* UNIV_PFS_THREAD */
loop:
/* Try to track a strange bug reported by Harald Fuchs and others,
where the lsn seems to decrease at times */
if (log_peek_lsn(&new_lsn)) {
if (new_lsn < old_lsn) {
ib::error() << "Old log sequence number " << old_lsn << " was"
<< " greater than the new log sequence number "
<< new_lsn << ". Please submit a bug report to"
" https://jira.mariadb.org/";
ut_ad(0);
}
old_lsn = new_lsn;
}
if (difftime(time(NULL), srv_last_monitor_time) > 60) {
/* We referesh InnoDB Monitor values so that averages are
printed from at most 60 last seconds */
srv_refresh_innodb_monitor_stats();
}
/* Update the statistics collected for deciding LRU
eviction policy. */
buf_LRU_stat_update();
if (sync_array_print_long_waits(&waiter, &sema)
&& sema == old_sema && os_thread_eq(waiter, old_waiter)) {
#if defined(WITH_WSREP) && defined(WITH_INNODB_DISALLOW_WRITES)
if (os_event_is_set(srv_allow_writes_event)) {
#endif /* WITH_WSREP */
fatal_cnt++;
#if defined(WITH_WSREP) && defined(WITH_INNODB_DISALLOW_WRITES)
} else {
fprintf(stderr,
"WSREP: avoiding InnoDB self crash due to long "
"semaphore wait of > %lu seconds\n"
"Server is processing SST donor operation, "
"fatal_cnt now: " ULINTPF,
srv_fatal_semaphore_wait_threshold, fatal_cnt);
}
#endif /* WITH_WSREP */
if (fatal_cnt > 10) {
ib::fatal() << "Semaphore wait has lasted > "
<< srv_fatal_semaphore_wait_threshold
<< " seconds. We intentionally crash the"
" server because it appears to be hung.";
}
} else {
fatal_cnt = 0;
old_waiter = waiter;
old_sema = sema;
}
/* Flush stderr so that a database user gets the output
to possible MySQL error file */
fflush(stderr);
sig_count = os_event_reset(srv_error_event);
os_event_wait_time_low(srv_error_event, 1000000, sig_count);
if (srv_shutdown_state == SRV_SHUTDOWN_NONE) {
goto loop;
}
srv_error_monitor_active = false;
/* We count the number of threads in os_thread_exit(). A created
thread should always use that to exit and not use return() to exit. */
os_thread_exit();
OS_THREAD_DUMMY_RETURN;
}
/******************************************************************//**
Increment the server activity count. */
void
srv_inc_activity_count(void)
/*========================*/
{
srv_sys.activity_count.inc();
}
/**********************************************************************//**
Check whether any background thread is active. If so return the thread
type.
@return SRV_NONE if all are suspended or have exited, thread
type if any are still active. */
srv_thread_type
srv_get_active_thread_type(void)
/*============================*/
{
srv_thread_type ret = SRV_NONE;
if (srv_read_only_mode) {
return(SRV_NONE);
}
srv_sys_mutex_enter();
for (ulint i = SRV_WORKER; i <= SRV_MASTER; ++i) {
if (srv_sys.n_threads_active[i] != 0) {
ret = static_cast<srv_thread_type>(i);
break;
}
}
srv_sys_mutex_exit();
if (ret == SRV_NONE && srv_shutdown_state != SRV_SHUTDOWN_NONE
&& purge_sys != NULL) {
/* Check only on shutdown. */
switch (trx_purge_state()) {
case PURGE_STATE_RUN:
case PURGE_STATE_STOP:
ret = SRV_PURGE;
break;
case PURGE_STATE_INIT:
case PURGE_STATE_DISABLED:
case PURGE_STATE_EXIT:
break;
}
}
return(ret);
}
/** Wake up the InnoDB master thread if it was suspended (not sleeping). */
void
srv_active_wake_master_thread_low()
{
ut_ad(!srv_read_only_mode);
ut_ad(!srv_sys_mutex_own());
srv_inc_activity_count();
if (my_atomic_loadlint(&srv_sys.n_threads_active[SRV_MASTER]) == 0) {
srv_slot_t* slot;
srv_sys_mutex_enter();
slot = &srv_sys.sys_threads[SRV_MASTER_SLOT];
/* Only if the master thread has been started. */
if (slot->in_use) {
ut_a(srv_slot_get_type(slot) == SRV_MASTER);
os_event_set(slot->event);
}
srv_sys_mutex_exit();
}
}
/** Wake up the purge threads if there is work to do. */
void
srv_wake_purge_thread_if_not_active()
{
ut_ad(!srv_sys_mutex_own());
if (purge_sys->state == PURGE_STATE_RUN
&& !my_atomic_loadlint(&srv_sys.n_threads_active[SRV_PURGE])
&& my_atomic_loadlint(&trx_sys->rseg_history_len)) {
srv_release_threads(SRV_PURGE, 1);
}
}
/** Wake up the master thread if it is suspended or being suspended. */
void
srv_wake_master_thread()
{
srv_inc_activity_count();
srv_release_threads(SRV_MASTER, 1);
}
/*******************************************************************//**
Get current server activity count. We don't hold srv_sys::mutex while
reading this value as it is only used in heuristics.
@return activity count. */
ulint
srv_get_activity_count(void)
/*========================*/
{
return(srv_sys.activity_count);
}
/*******************************************************************//**
Check if there has been any activity.
@return FALSE if no change in activity counter. */
ibool
srv_check_activity(
/*===============*/
ulint old_activity_count) /*!< in: old activity count */
{
return(srv_sys.activity_count != old_activity_count);
}
/********************************************************************//**
The master thread is tasked to ensure that flush of log file happens
once every second in the background. This is to ensure that not more
than one second of trxs are lost in case of crash when
innodb_flush_logs_at_trx_commit != 1 */
static
void
srv_sync_log_buffer_in_background(void)
/*===================================*/
{
time_t current_time = time(NULL);
srv_main_thread_op_info = "flushing log";
if (difftime(current_time, srv_last_log_flush_time)
>= srv_flush_log_at_timeout) {
log_buffer_sync_in_background(true);
srv_last_log_flush_time = current_time;
srv_log_writes_and_flush++;
}
}
/********************************************************************//**
Make room in the table cache by evicting an unused table.
@return number of tables evicted. */
static
ulint
srv_master_evict_from_table_cache(
/*==============================*/
ulint pct_check) /*!< in: max percent to check */
{
ulint n_tables_evicted = 0;
rw_lock_x_lock(dict_operation_lock);
dict_mutex_enter_for_mysql();
n_tables_evicted = dict_make_room_in_cache(
innobase_get_table_cache_size(), pct_check);
dict_mutex_exit_for_mysql();
rw_lock_x_unlock(dict_operation_lock);
return(n_tables_evicted);
}
/*********************************************************************//**
This function prints progress message every 60 seconds during server
shutdown, for any activities that master thread is pending on. */
static
void
srv_shutdown_print_master_pending(
/*==============================*/
ib_time_t* last_print_time, /*!< last time the function
print the message */
ulint n_tables_to_drop, /*!< number of tables to
be dropped */
ulint n_bytes_merged) /*!< number of change buffer
just merged */
{
ib_time_t current_time;
double time_elapsed;
current_time = ut_time();
time_elapsed = ut_difftime(current_time, *last_print_time);
if (time_elapsed > 60) {
*last_print_time = current_time;
if (n_tables_to_drop) {
ib::info() << "Waiting for " << n_tables_to_drop
<< " table(s) to be dropped";
}
/* Check change buffer merge, we only wait for change buffer
merge if it is a slow shutdown */
if (!srv_fast_shutdown && n_bytes_merged) {
ib::info() << "Waiting for change buffer merge to"
" complete number of bytes of change buffer"
" just merged: " << n_bytes_merged;
}
}
}
#ifdef UNIV_DEBUG
/** Waits in loop as long as master thread is disabled (debug) */
static
void
srv_master_do_disabled_loop(void)
{
if (!srv_master_thread_disabled_debug) {
/* We return here to avoid changing op_info. */
return;
}
srv_main_thread_op_info = "disabled";
while (srv_master_thread_disabled_debug) {
os_event_set(srv_master_thread_disabled_event);
if (srv_shutdown_state != SRV_SHUTDOWN_NONE) {
break;
}
os_thread_sleep(100000);
}
srv_main_thread_op_info = "";
}
/** Disables master thread. It's used by:
SET GLOBAL innodb_master_thread_disabled_debug = 1 (0).
@param[in] thd thread handle
@param[in] var pointer to system variable
@param[out] var_ptr where the formal string goes
@param[in] save immediate result from check function */
void
srv_master_thread_disabled_debug_update(
THD* thd,
struct st_mysql_sys_var* var,
void* var_ptr,
const void* save)
{
/* This method is protected by mutex, as every SET GLOBAL .. */
ut_ad(srv_master_thread_disabled_event != NULL);
const bool disable = *static_cast<const my_bool*>(save);
const int64_t sig_count = os_event_reset(
srv_master_thread_disabled_event);
srv_master_thread_disabled_debug = disable;
if (disable) {
os_event_wait_low(
srv_master_thread_disabled_event, sig_count);
}
}
#endif /* UNIV_DEBUG */
/*********************************************************************//**
Perform the tasks that the master thread is supposed to do when the
server is active. There are two types of tasks. The first category is
of such tasks which are performed at each inovcation of this function.
We assume that this function is called roughly every second when the
server is active. The second category is of such tasks which are
performed at some interval e.g.: purge, dict_LRU cleanup etc. */
static
void
srv_master_do_active_tasks(void)
/*============================*/
{
ib_time_t cur_time = ut_time();
uintmax_t counter_time = ut_time_us(NULL);
/* First do the tasks that we are suppose to do at each
invocation of this function. */
++srv_main_active_loops;
MONITOR_INC(MONITOR_MASTER_ACTIVE_LOOPS);
/* ALTER TABLE in MySQL requires on Unix that the table handler
can drop tables lazily after there no longer are SELECT
queries to them. */
srv_main_thread_op_info = "doing background drop tables";
row_drop_tables_for_mysql_in_background();
MONITOR_INC_TIME_IN_MICRO_SECS(
MONITOR_SRV_BACKGROUND_DROP_TABLE_MICROSECOND, counter_time);
ut_d(srv_master_do_disabled_loop());
if (srv_shutdown_state != SRV_SHUTDOWN_NONE) {
return;
}
/* make sure that there is enough reusable space in the redo
log files */
srv_main_thread_op_info = "checking free log space";
log_free_check();
/* Do an ibuf merge */
srv_main_thread_op_info = "doing insert buffer merge";
counter_time = ut_time_us(NULL);
ibuf_merge_in_background(false);
MONITOR_INC_TIME_IN_MICRO_SECS(
MONITOR_SRV_IBUF_MERGE_MICROSECOND, counter_time);
/* Flush logs if needed */
srv_main_thread_op_info = "flushing log";
srv_sync_log_buffer_in_background();
MONITOR_INC_TIME_IN_MICRO_SECS(
MONITOR_SRV_LOG_FLUSH_MICROSECOND, counter_time);
/* Now see if various tasks that are performed at defined
intervals need to be performed. */
if (srv_shutdown_state != SRV_SHUTDOWN_NONE) {
return;
}
if (cur_time % SRV_MASTER_DICT_LRU_INTERVAL == 0) {
srv_main_thread_op_info = "enforcing dict cache limit";
ulint n_evicted = srv_master_evict_from_table_cache(50);
if (n_evicted != 0) {
MONITOR_INC_VALUE(
MONITOR_SRV_DICT_LRU_EVICT_COUNT_ACTIVE, n_evicted);
}
MONITOR_INC_TIME_IN_MICRO_SECS(
MONITOR_SRV_DICT_LRU_MICROSECOND, counter_time);
}
if (srv_shutdown_state != SRV_SHUTDOWN_NONE) {
return;
}
/* Make a new checkpoint */
if (cur_time % SRV_MASTER_CHECKPOINT_INTERVAL == 0) {
srv_main_thread_op_info = "making checkpoint";
log_checkpoint(TRUE, FALSE);
MONITOR_INC_TIME_IN_MICRO_SECS(
MONITOR_SRV_CHECKPOINT_MICROSECOND, counter_time);
}
}
/*********************************************************************//**
Perform the tasks that the master thread is supposed to do whenever the
server is idle. We do check for the server state during this function
and if the server has entered the shutdown phase we may return from
the function without completing the required tasks.
Note that the server can move to active state when we are executing this
function but we don't check for that as we are suppose to perform more
or less same tasks when server is active. */
static
void
srv_master_do_idle_tasks(void)
/*==========================*/
{
uintmax_t counter_time;
++srv_main_idle_loops;
MONITOR_INC(MONITOR_MASTER_IDLE_LOOPS);
/* ALTER TABLE in MySQL requires on Unix that the table handler
can drop tables lazily after there no longer are SELECT
queries to them. */
counter_time = ut_time_us(NULL);
srv_main_thread_op_info = "doing background drop tables";
row_drop_tables_for_mysql_in_background();
MONITOR_INC_TIME_IN_MICRO_SECS(
MONITOR_SRV_BACKGROUND_DROP_TABLE_MICROSECOND,
counter_time);
ut_d(srv_master_do_disabled_loop());
if (srv_shutdown_state != SRV_SHUTDOWN_NONE) {
return;
}
/* make sure that there is enough reusable space in the redo
log files */
srv_main_thread_op_info = "checking free log space";
log_free_check();
/* Do an ibuf merge */
counter_time = ut_time_us(NULL);
srv_main_thread_op_info = "doing insert buffer merge";
ibuf_merge_in_background(true);
MONITOR_INC_TIME_IN_MICRO_SECS(
MONITOR_SRV_IBUF_MERGE_MICROSECOND, counter_time);
if (srv_shutdown_state != SRV_SHUTDOWN_NONE) {
return;
}
srv_main_thread_op_info = "enforcing dict cache limit";
ulint n_evicted = srv_master_evict_from_table_cache(100);
if (n_evicted != 0) {
MONITOR_INC_VALUE(
MONITOR_SRV_DICT_LRU_EVICT_COUNT_IDLE, n_evicted);
}
MONITOR_INC_TIME_IN_MICRO_SECS(
MONITOR_SRV_DICT_LRU_MICROSECOND, counter_time);
/* Flush logs if needed */
srv_sync_log_buffer_in_background();
MONITOR_INC_TIME_IN_MICRO_SECS(
MONITOR_SRV_LOG_FLUSH_MICROSECOND, counter_time);
if (srv_shutdown_state != SRV_SHUTDOWN_NONE) {
return;
}
/* Make a new checkpoint */
srv_main_thread_op_info = "making checkpoint";
log_checkpoint(TRUE, FALSE);
MONITOR_INC_TIME_IN_MICRO_SECS(MONITOR_SRV_CHECKPOINT_MICROSECOND,
counter_time);
}
/** Perform shutdown tasks.
@param[in] ibuf_merge whether to complete the change buffer merge */
static
void
srv_shutdown(bool ibuf_merge)
{
ulint n_bytes_merged = 0;
ulint n_tables_to_drop;
ib_time_t now = ut_time();
do {
ut_ad(!srv_read_only_mode);
ut_ad(srv_shutdown_state == SRV_SHUTDOWN_CLEANUP);
++srv_main_shutdown_loops;
/* FIXME: Remove the background DROP TABLE queue; it is not
crash-safe and breaks ACID. */
srv_main_thread_op_info = "doing background drop tables";
n_tables_to_drop = row_drop_tables_for_mysql_in_background();
if (ibuf_merge) {
srv_main_thread_op_info = "checking free log space";
log_free_check();
srv_main_thread_op_info = "doing insert buffer merge";
n_bytes_merged = ibuf_merge_in_background(true);
/* Flush logs if needed */
srv_sync_log_buffer_in_background();
}
/* Print progress message every 60 seconds during shutdown */
if (srv_print_verbose_log) {
srv_shutdown_print_master_pending(
&now, n_tables_to_drop, n_bytes_merged);
}
} while (n_bytes_merged || n_tables_to_drop);
}
/*********************************************************************//**
Puts master thread to sleep. At this point we are using polling to
service various activities. Master thread sleeps for one second before
checking the state of the server again */
static
void
srv_master_sleep(void)
/*==================*/
{
srv_main_thread_op_info = "sleeping";
os_thread_sleep(1000000);
srv_main_thread_op_info = "";
}
/*********************************************************************//**
The master thread controlling the server.
@return a dummy parameter */
extern "C"
os_thread_ret_t
DECLARE_THREAD(srv_master_thread)(
/*==============================*/
void* arg MY_ATTRIBUTE((unused)))
/*!< in: a dummy parameter required by
os_thread_create */
{
my_thread_init();
DBUG_ENTER("srv_master_thread");
srv_slot_t* slot;
ulint old_activity_count = srv_get_activity_count();
ut_ad(!srv_read_only_mode);
#ifdef UNIV_DEBUG_THREAD_CREATION
ib::info() << "Master thread starts, id "
<< os_thread_pf(os_thread_get_curr_id());
#endif /* UNIV_DEBUG_THREAD_CREATION */
#ifdef UNIV_PFS_THREAD
pfs_register_thread(srv_master_thread_key);
#endif /* UNIV_PFS_THREAD */
srv_main_thread_process_no = os_proc_get_number();
srv_main_thread_id = os_thread_pf(os_thread_get_curr_id());
slot = srv_reserve_slot(SRV_MASTER);
ut_a(slot == srv_sys.sys_threads);
loop:
if (srv_force_recovery >= SRV_FORCE_NO_BACKGROUND) {
goto suspend_thread;
}
while (srv_shutdown_state == SRV_SHUTDOWN_NONE) {
srv_master_sleep();
MONITOR_INC(MONITOR_MASTER_THREAD_SLEEP);
if (srv_check_activity(old_activity_count)) {
old_activity_count = srv_get_activity_count();
srv_master_do_active_tasks();
} else {
srv_master_do_idle_tasks();
}
}
suspend_thread:
switch (srv_shutdown_state) {
case SRV_SHUTDOWN_NONE:
break;
case SRV_SHUTDOWN_FLUSH_PHASE:
case SRV_SHUTDOWN_LAST_PHASE:
ut_ad(0);
/* fall through */
case SRV_SHUTDOWN_EXIT_THREADS:
/* srv_init_abort() must have been invoked */
case SRV_SHUTDOWN_CLEANUP:
if (srv_shutdown_state == SRV_SHUTDOWN_CLEANUP
&& srv_fast_shutdown < 2) {
srv_shutdown(srv_fast_shutdown == 0);
}
srv_suspend_thread(slot);
my_thread_end();
os_thread_exit();
}
srv_main_thread_op_info = "suspending";
srv_suspend_thread(slot);
/* DO NOT CHANGE THIS STRING. innobase_start_or_create_for_mysql()
waits for database activity to die down when converting < 4.1.x
databases, and relies on this string being exactly as it is. InnoDB
manual also mentions this string in several places. */
srv_main_thread_op_info = "waiting for server activity";
srv_resume_thread(slot);
goto loop;
}
/** Check if purge should stop.
@param[in] n_purged pages purged in the last batch
@return whether purge should exit */
static
bool
srv_purge_should_exit(ulint n_purged)
{
ut_ad(srv_shutdown_state == SRV_SHUTDOWN_NONE
|| srv_shutdown_state == SRV_SHUTDOWN_CLEANUP);
if (srv_undo_sources) {
return(false);
}
if (srv_fast_shutdown) {
return(true);
}
/* Slow shutdown was requested. */
if (n_purged) {
/* The previous round still did some work. */
return(false);
}
/* Exit if there are no active transactions to roll back. */
return(trx_sys_any_active_transactions() == 0);
}
/*********************************************************************//**
Fetch and execute a task from the work queue.
@return true if a task was executed */
static
bool
srv_task_execute(void)
/*==================*/
{
que_thr_t* thr = NULL;
ut_ad(!srv_read_only_mode);
ut_a(srv_force_recovery < SRV_FORCE_NO_BACKGROUND);
mutex_enter(&srv_sys.tasks_mutex);
if (UT_LIST_GET_LEN(srv_sys.tasks) > 0) {
thr = UT_LIST_GET_FIRST(srv_sys.tasks);
ut_a(que_node_get_type(thr->child) == QUE_NODE_PURGE);
UT_LIST_REMOVE(srv_sys.tasks, thr);
}
mutex_exit(&srv_sys.tasks_mutex);
if (thr != NULL) {
que_run_threads(thr);
my_atomic_addlint(
&purge_sys->n_completed, 1);
}
return(thr != NULL);
}
/*********************************************************************//**
Worker thread that reads tasks from the work queue and executes them.
@return a dummy parameter */
extern "C"
os_thread_ret_t
DECLARE_THREAD(srv_worker_thread)(
/*==============================*/
void* arg MY_ATTRIBUTE((unused))) /*!< in: a dummy parameter
required by os_thread_create */
{
my_thread_init();
srv_slot_t* slot;
ut_ad(!srv_read_only_mode);
ut_a(srv_force_recovery < SRV_FORCE_NO_BACKGROUND);
my_thread_init();
THD* thd = innobase_create_background_thd("InnoDB purge worker");
#ifdef UNIV_DEBUG_THREAD_CREATION
ib::info() << "Worker thread starting, id "
<< os_thread_pf(os_thread_get_curr_id());
#endif /* UNIV_DEBUG_THREAD_CREATION */
slot = srv_reserve_slot(SRV_WORKER);
ut_a(srv_n_purge_threads > 1);
ut_a(my_atomic_loadlint(&srv_sys.n_threads_active[SRV_WORKER])
< static_cast<lint>(srv_n_purge_threads));
/* We need to ensure that the worker threads exit after the
purge coordinator thread. Otherwise the purge coordinator can
end up waiting forever in trx_purge_wait_for_workers_to_complete() */
do {
srv_suspend_thread(slot);
srv_resume_thread(slot);
if (srv_task_execute()) {
/* If there are tasks in the queue, wakeup
the purge coordinator thread. */
srv_wake_purge_thread_if_not_active();
}
/* Note: we are checking the state without holding the
purge_sys->latch here. */
} while (purge_sys->state != PURGE_STATE_EXIT);
srv_free_slot(slot);
rw_lock_x_lock(&purge_sys->latch);
ut_a(!purge_sys->running);
ut_a(purge_sys->state == PURGE_STATE_EXIT);
rw_lock_x_unlock(&purge_sys->latch);
#ifdef UNIV_DEBUG_THREAD_CREATION
ib::info() << "Purge worker thread exiting, id "
<< os_thread_pf(os_thread_get_curr_id());
#endif /* UNIV_DEBUG_THREAD_CREATION */
innobase_destroy_background_thd(thd);
my_thread_end();
/* We count the number of threads in os_thread_exit(). A created
thread should always use that to exit and not use return() to exit. */
os_thread_exit();
OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
}
/** Do the actual purge operation.
@param[in,out] n_total_purged total number of purged pages
@return length of history list before the last purge batch. */
static
ulint
srv_do_purge(ulint* n_total_purged)
{
ulint n_pages_purged;
static ulint count = 0;
static ulint n_use_threads = 0;
static ulint rseg_history_len = 0;
ulint old_activity_count = srv_get_activity_count();
const ulint n_threads = srv_n_purge_threads;
ut_a(n_threads > 0);
ut_ad(!srv_read_only_mode);
/* Purge until there are no more records to purge and there is
no change in configuration or server state. If the user has
configured more than one purge thread then we treat that as a
pool of threads and only use the extra threads if purge can't
keep up with updates. */
if (n_use_threads == 0) {
n_use_threads = n_threads;
}
do {
if (trx_sys->rseg_history_len > rseg_history_len
|| (srv_max_purge_lag > 0
&& rseg_history_len > srv_max_purge_lag)) {
/* History length is now longer than what it was
when we took the last snapshot. Use more threads. */
if (n_use_threads < n_threads) {
++n_use_threads;
}
} else if (srv_check_activity(old_activity_count)
&& n_use_threads > 1) {
/* History length same or smaller since last snapshot,
use fewer threads. */
--n_use_threads;
old_activity_count = srv_get_activity_count();
}
/* Ensure that the purge threads are less than what
was configured. */
ut_a(n_use_threads > 0);
ut_a(n_use_threads <= n_threads);
/* Take a snapshot of the history list before purge. */
if ((rseg_history_len = trx_sys->rseg_history_len) == 0) {
break;
}
ulint undo_trunc_freq =
purge_sys->undo_trunc.get_rseg_truncate_frequency();
ulint rseg_truncate_frequency = ut_min(
static_cast<ulint>(srv_purge_rseg_truncate_frequency),
undo_trunc_freq);
n_pages_purged = trx_purge(
n_use_threads, srv_purge_batch_size,
(++count % rseg_truncate_frequency) == 0);
*n_total_purged += n_pages_purged;
} while (!srv_purge_should_exit(n_pages_purged)
&& n_pages_purged > 0
&& purge_sys->state == PURGE_STATE_RUN);
return(rseg_history_len);
}
/*********************************************************************//**
Suspend the purge coordinator thread. */
static
void
srv_purge_coordinator_suspend(
/*==========================*/
srv_slot_t* slot, /*!< in/out: Purge coordinator
thread slot */
ulint rseg_history_len) /*!< in: history list length
before last purge */
{
ut_ad(!srv_read_only_mode);
ut_a(slot->type == SRV_PURGE);
bool stop = false;
/** Maximum wait time on the purge event, in micro-seconds. */
static const ulint SRV_PURGE_MAX_TIMEOUT = 10000;
int64_t sig_count = srv_suspend_thread(slot);
do {
rw_lock_x_lock(&purge_sys->latch);
purge_sys->running = false;
rw_lock_x_unlock(&purge_sys->latch);
/* We don't wait right away on the the non-timed wait because
we want to signal the thread that wants to suspend purge. */
const bool wait = stop
|| rseg_history_len <= trx_sys->rseg_history_len;
const bool timeout = srv_resume_thread(
slot, sig_count, wait,
stop ? 0 : SRV_PURGE_MAX_TIMEOUT);
sig_count = srv_suspend_thread(slot);
rw_lock_x_lock(&purge_sys->latch);
stop = (srv_shutdown_state == SRV_SHUTDOWN_NONE
&& purge_sys->state == PURGE_STATE_STOP);
if (!stop) {
ut_a(purge_sys->n_stop == 0);
purge_sys->running = true;
if (timeout
&& rseg_history_len == trx_sys->rseg_history_len
&& trx_sys->rseg_history_len < 5000) {
/* No new records were added since the
wait started. Simply wait for new
records. The magic number 5000 is an
approximation for the case where we
have cached UNDO log records which
prevent truncate of the UNDO
segments. */
stop = true;
}
} else {
ut_a(purge_sys->n_stop > 0);
/* Signal that we are suspended. */
os_event_set(purge_sys->event);
}
rw_lock_x_unlock(&purge_sys->latch);
} while (stop && srv_undo_sources);
srv_resume_thread(slot, 0, false);
}
/*********************************************************************//**
Purge coordinator thread that schedules the purge tasks.
@return a dummy parameter */
extern "C"
os_thread_ret_t
DECLARE_THREAD(srv_purge_coordinator_thread)(
/*=========================================*/
void* arg MY_ATTRIBUTE((unused))) /*!< in: a dummy parameter
required by os_thread_create */
{
my_thread_init();
THD* thd = innobase_create_background_thd("InnoDB purge coordinator");
srv_slot_t* slot;
ulint n_total_purged = ULINT_UNDEFINED;
ut_ad(!srv_read_only_mode);
ut_a(srv_n_purge_threads >= 1);
ut_a(trx_purge_state() == PURGE_STATE_INIT);
ut_a(srv_force_recovery < SRV_FORCE_NO_BACKGROUND);
rw_lock_x_lock(&purge_sys->latch);
purge_sys->running = true;
purge_sys->state = PURGE_STATE_RUN;
rw_lock_x_unlock(&purge_sys->latch);
#ifdef UNIV_PFS_THREAD
pfs_register_thread(srv_purge_thread_key);
#endif /* UNIV_PFS_THREAD */
#ifdef UNIV_DEBUG_THREAD_CREATION
ib::info() << "Purge coordinator thread created, id "
<< os_thread_pf(os_thread_get_curr_id());
#endif /* UNIV_DEBUG_THREAD_CREATION */
slot = srv_reserve_slot(SRV_PURGE);
ulint rseg_history_len = trx_sys->rseg_history_len;
do {
/* If there are no records to purge or the last
purge didn't purge any records then wait for activity. */
if (srv_shutdown_state == SRV_SHUTDOWN_NONE
&& srv_undo_sources
&& (purge_sys->state == PURGE_STATE_STOP
|| n_total_purged == 0)) {
srv_purge_coordinator_suspend(slot, rseg_history_len);
}
ut_ad(!slot->suspended);
if (srv_purge_should_exit(n_total_purged)) {
break;
}
n_total_purged = 0;
rseg_history_len = srv_do_purge(&n_total_purged);
} while (!srv_purge_should_exit(n_total_purged));
/* The task queue should always be empty, independent of fast
shutdown state. */
ut_a(srv_get_task_queue_length() == 0);
srv_free_slot(slot);
/* Note that we are shutting down. */
rw_lock_x_lock(&purge_sys->latch);
purge_sys->state = PURGE_STATE_EXIT;
/* If there are any pending undo-tablespace truncate then clear
it off as we plan to shutdown the purge thread. */
purge_sys->undo_trunc.clear();
purge_sys->running = false;
rw_lock_x_unlock(&purge_sys->latch);
#ifdef UNIV_DEBUG_THREAD_CREATION
ib::info() << "Purge coordinator exiting, id "
<< os_thread_pf(os_thread_get_curr_id());
#endif /* UNIV_DEBUG_THREAD_CREATION */
/* Ensure that all the worker threads quit. */
if (srv_n_purge_threads > 1) {
srv_release_threads(SRV_WORKER, srv_n_purge_threads - 1);
}
innobase_destroy_background_thd(thd);
my_thread_end();
/* We count the number of threads in os_thread_exit(). A created
thread should always use that to exit and not use return() to exit. */
os_thread_exit();
OS_THREAD_DUMMY_RETURN; /* Not reached, avoid compiler warning */
}
/**********************************************************************//**
Enqueues a task to server task queue and releases a worker thread, if there
is a suspended one. */
void
srv_que_task_enqueue_low(
/*=====================*/
que_thr_t* thr) /*!< in: query thread */
{
ut_ad(!srv_read_only_mode);
mutex_enter(&srv_sys.tasks_mutex);
UT_LIST_ADD_LAST(srv_sys.tasks, thr);
mutex_exit(&srv_sys.tasks_mutex);
srv_release_threads(SRV_WORKER, 1);
}
/**********************************************************************//**
Get count of tasks in the queue.
@return number of tasks in queue */
ulint
srv_get_task_queue_length(void)
/*===========================*/
{
ulint n_tasks;
ut_ad(!srv_read_only_mode);
mutex_enter(&srv_sys.tasks_mutex);
n_tasks = UT_LIST_GET_LEN(srv_sys.tasks);
mutex_exit(&srv_sys.tasks_mutex);
return(n_tasks);
}
/** Wake up the purge threads. */
void
srv_purge_wakeup()
{
ut_ad(!srv_read_only_mode);
if (srv_force_recovery >= SRV_FORCE_NO_BACKGROUND) {
return;
}
do {
srv_release_threads(SRV_PURGE, 1);
if (srv_n_purge_threads > 1) {
ulint n_workers = srv_n_purge_threads - 1;
srv_release_threads(SRV_WORKER, n_workers);
}
} while (!srv_running
&& (srv_sys.n_threads_active[SRV_WORKER]
|| srv_sys.n_threads_active[SRV_PURGE]));
}
/** Check if tablespace is being truncated.
(Ignore system-tablespace as we don't re-create the tablespace
and so some of the action that are suppressed by this function
for independent tablespace are not applicable to system-tablespace).
@param space_id space_id to check for truncate action
@return true if being truncated, false if not being
truncated or tablespace is system-tablespace. */
bool
srv_is_tablespace_truncated(ulint space_id)
{
if (is_system_tablespace(space_id)) {
return(false);
}
return(truncate_t::is_tablespace_truncated(space_id)
|| undo::Truncate::is_tablespace_truncated(space_id));
}
/** Check if tablespace was truncated.
@param[in] space space object to check for truncate action
@return true if tablespace was truncated and we still have an active
MLOG_TRUNCATE REDO log record. */
bool
srv_was_tablespace_truncated(const fil_space_t* space)
{
if (space == NULL) {
ut_ad(0);
return(false);
}
return (!is_system_tablespace(space->id)
&& truncate_t::was_tablespace_truncated(space->id));
}
|