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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "vm/HelperThreads.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Maybe.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Unused.h"
#include "mozilla/Utf8.h" // mozilla::Utf8Unit
#include <algorithm>
#include "frontend/BytecodeCompilation.h"
#include "frontend/CompilationInfo.h" // frontend::CompilationStencilSet, frontend::CompilationGCOutput
#include "frontend/ParserAtom.h" // frontend::ParserAtomsTable
#include "gc/GC.h" // gc::MergeRealms
#include "jit/IonCompileTask.h"
#include "jit/JitRuntime.h"
#include "js/ContextOptions.h" // JS::ContextOptions
#include "js/friend/StackLimits.h" // js::ReportOverRecursed
#include "js/OffThreadScriptCompilation.h" // JS::OffThreadToken, JS::OffThreadCompileCallback
#include "js/SourceText.h"
#include "js/UniquePtr.h"
#include "js/Utility.h"
#include "threading/CpuCount.h"
#include "util/NativeStack.h"
#include "vm/ErrorReporting.h"
#include "vm/HelperThreadState.h"
#include "vm/MutexIDs.h"
#include "vm/SharedImmutableStringsCache.h"
#include "vm/Time.h"
#include "vm/TraceLogging.h"
#include "vm/Xdr.h"
#include "wasm/WasmGenerator.h"
#include "debugger/DebugAPI-inl.h"
#include "gc/ArenaList-inl.h"
#include "vm/JSContext-inl.h"
#include "vm/JSObject-inl.h"
#include "vm/JSScript-inl.h"
#include "vm/NativeObject-inl.h"
#include "vm/Realm-inl.h"
using namespace js;
using mozilla::Maybe;
using mozilla::TimeDuration;
using mozilla::TimeStamp;
using mozilla::Unused;
using mozilla::Utf8Unit;
using JS::CompileOptions;
using JS::ReadOnlyCompileOptions;
namespace js {
Mutex gHelperThreadLock(mutexid::GlobalHelperThreadState);
GlobalHelperThreadState* gHelperThreadState = nullptr;
} // namespace js
// These macros are identical in function to the same-named ones in
// GeckoProfiler.h, but they are defined separately because SpiderMonkey can't
// use GeckoProfiler.h.
#define PROFILER_RAII_PASTE(id, line) id##line
#define PROFILER_RAII_EXPAND(id, line) PROFILER_RAII_PASTE(id, line)
#define PROFILER_RAII PROFILER_RAII_EXPAND(raiiObject, __LINE__)
#define AUTO_PROFILER_LABEL(label, categoryPair) \
HelperThread::AutoProfilerLabel PROFILER_RAII( \
this, label, JS::ProfilingCategoryPair::categoryPair)
bool js::CreateHelperThreadsState() {
MOZ_ASSERT(!gHelperThreadState);
UniquePtr<GlobalHelperThreadState> helperThreadState =
MakeUnique<GlobalHelperThreadState>();
if (!helperThreadState) {
return false;
}
gHelperThreadState = helperThreadState.release();
if (!gHelperThreadState->ensureContextList(gHelperThreadState->threadCount)) {
js_delete(gHelperThreadState);
gHelperThreadState = nullptr;
return false;
}
return true;
}
void js::DestroyHelperThreadsState() {
if (!gHelperThreadState) {
return;
}
gHelperThreadState->finish();
js_delete(gHelperThreadState);
gHelperThreadState = nullptr;
}
bool js::EnsureHelperThreadsInitialized() {
MOZ_ASSERT(gHelperThreadState);
return gHelperThreadState->ensureInitialized();
}
static size_t ClampDefaultCPUCount(size_t cpuCount) {
// It's extremely rare for SpiderMonkey to have more than a few cores worth
// of work. At higher core counts, performance can even decrease due to NUMA
// (and SpiderMonkey's lack of NUMA-awareness), contention, and general lack
// of optimization for high core counts. So to avoid wasting thread stack
// resources (and cluttering gdb and core dumps), clamp to 8 cores for now.
return std::min<size_t>(cpuCount, 8);
}
static size_t ThreadCountForCPUCount(size_t cpuCount) {
// We need at least two threads for tier-2 wasm compilations, because
// there's a master task that holds a thread while other threads do the
// compilation.
return std::max<size_t>(cpuCount, 2);
}
bool js::SetFakeCPUCount(size_t count) {
// This must be called before the threads have been initialized.
AutoLockHelperThreadState lock;
MOZ_ASSERT(HelperThreadState().threads(lock).empty());
HelperThreadState().cpuCount = count;
HelperThreadState().threadCount = ThreadCountForCPUCount(count);
return true;
}
void JS::SetProfilingThreadCallbacks(
JS::RegisterThreadCallback registerThread,
JS::UnregisterThreadCallback unregisterThread) {
HelperThreadState().registerThread = registerThread;
HelperThreadState().unregisterThread = unregisterThread;
}
bool js::StartOffThreadWasmCompile(wasm::CompileTask* task,
wasm::CompileMode mode) {
return HelperThreadState().submitTask(task, mode);
}
bool GlobalHelperThreadState::submitTask(wasm::CompileTask* task,
wasm::CompileMode mode) {
AutoLockHelperThreadState lock;
if (!wasmWorklist(lock, mode).pushBack(task)) {
return false;
}
dispatch(lock);
return true;
}
size_t js::RemovePendingWasmCompileTasks(
const wasm::CompileTaskState& taskState, wasm::CompileMode mode,
const AutoLockHelperThreadState& lock) {
wasm::CompileTaskPtrFifo& worklist =
HelperThreadState().wasmWorklist(lock, mode);
return worklist.eraseIf([&taskState](wasm::CompileTask* task) {
return &task->state == &taskState;
});
}
void js::StartOffThreadWasmTier2Generator(wasm::UniqueTier2GeneratorTask task) {
Unused << HelperThreadState().submitTask(std::move(task));
}
bool GlobalHelperThreadState::submitTask(wasm::UniqueTier2GeneratorTask task) {
MOZ_ASSERT(CanUseExtraThreads());
AutoLockHelperThreadState lock;
if (!wasmTier2GeneratorWorklist(lock).append(task.get())) {
return false;
}
Unused << task.release();
dispatch(lock);
return true;
}
static void CancelOffThreadWasmTier2GeneratorLocked(
AutoLockHelperThreadState& lock) {
if (HelperThreadState().threads(lock).empty()) {
return;
}
// Remove pending tasks from the tier2 generator worklist and cancel and
// delete them.
{
wasm::Tier2GeneratorTaskPtrVector& worklist =
HelperThreadState().wasmTier2GeneratorWorklist(lock);
for (size_t i = 0; i < worklist.length(); i++) {
wasm::Tier2GeneratorTask* task = worklist[i];
HelperThreadState().remove(worklist, &i);
js_delete(task);
}
}
// There is at most one running Tier2Generator task and we assume that
// below.
static_assert(GlobalHelperThreadState::MaxTier2GeneratorTasks == 1,
"code must be generalized");
// If there is a running Tier2 generator task, shut it down in a predictable
// way. The task will be deleted by the normal deletion logic.
for (auto* helper : HelperThreadState().helperTasks(lock)) {
if (helper->is<wasm::Tier2GeneratorTask>()) {
// Set a flag that causes compilation to shortcut itself.
helper->as<wasm::Tier2GeneratorTask>()->cancel();
// Wait for the generator task to finish. This avoids a shutdown race
// where the shutdown code is trying to shut down helper threads and the
// ongoing tier2 compilation is trying to finish, which requires it to
// have access to helper threads.
uint32_t oldFinishedCount =
HelperThreadState().wasmTier2GeneratorsFinished(lock);
while (HelperThreadState().wasmTier2GeneratorsFinished(lock) ==
oldFinishedCount) {
HelperThreadState().wait(lock, GlobalHelperThreadState::CONSUMER);
}
// At most one of these tasks.
break;
}
}
}
void js::CancelOffThreadWasmTier2Generator() {
AutoLockHelperThreadState lock;
CancelOffThreadWasmTier2GeneratorLocked(lock);
}
bool js::StartOffThreadIonCompile(jit::IonCompileTask* task,
const AutoLockHelperThreadState& lock) {
return HelperThreadState().submitTask(task, lock);
}
bool GlobalHelperThreadState::submitTask(
jit::IonCompileTask* task, const AutoLockHelperThreadState& locked) {
MOZ_ASSERT(CanUseExtraThreads());
if (!ionWorklist(locked).append(task)) {
return false;
}
// The build is moving off-thread. Freeze the LifoAlloc to prevent any
// unwanted mutations.
task->alloc().lifoAlloc()->setReadOnly();
dispatch(locked);
return true;
}
bool js::StartOffThreadIonFree(jit::IonCompileTask* task,
const AutoLockHelperThreadState& lock) {
js::UniquePtr<jit::IonFreeTask> freeTask =
js::MakeUnique<jit::IonFreeTask>(task);
if (!freeTask) {
return false;
}
return HelperThreadState().submitTask(std::move(freeTask), lock);
}
bool GlobalHelperThreadState::submitTask(
UniquePtr<jit::IonFreeTask> task, const AutoLockHelperThreadState& locked) {
MOZ_ASSERT(CanUseExtraThreads());
if (!ionFreeList(locked).append(std::move(task))) {
return false;
}
dispatch(locked);
return true;
}
/*
* Move an IonCompilationTask for which compilation has either finished, failed,
* or been cancelled into the global finished compilation list. All off thread
* compilations which are started must eventually be finished.
*/
void js::FinishOffThreadIonCompile(jit::IonCompileTask* task,
const AutoLockHelperThreadState& lock) {
AutoEnterOOMUnsafeRegion oomUnsafe;
if (!HelperThreadState().ionFinishedList(lock).append(task)) {
oomUnsafe.crash("FinishOffThreadIonCompile");
}
task->script()
->runtimeFromAnyThread()
->jitRuntime()
->numFinishedOffThreadTasksRef(lock)++;
}
static JSRuntime* GetSelectorRuntime(const CompilationSelector& selector) {
struct Matcher {
JSRuntime* operator()(JSScript* script) {
return script->runtimeFromMainThread();
}
JSRuntime* operator()(Realm* realm) {
return realm->runtimeFromMainThread();
}
JSRuntime* operator()(Zone* zone) { return zone->runtimeFromMainThread(); }
JSRuntime* operator()(ZonesInState zbs) { return zbs.runtime; }
JSRuntime* operator()(JSRuntime* runtime) { return runtime; }
};
return selector.match(Matcher());
}
static bool JitDataStructuresExist(const CompilationSelector& selector) {
struct Matcher {
bool operator()(JSScript* script) { return !!script->realm()->jitRealm(); }
bool operator()(Realm* realm) { return !!realm->jitRealm(); }
bool operator()(Zone* zone) { return !!zone->jitZone(); }
bool operator()(ZonesInState zbs) { return zbs.runtime->hasJitRuntime(); }
bool operator()(JSRuntime* runtime) { return runtime->hasJitRuntime(); }
};
return selector.match(Matcher());
}
static bool IonCompileTaskMatches(const CompilationSelector& selector,
jit::IonCompileTask* task) {
struct TaskMatches {
jit::IonCompileTask* task_;
bool operator()(JSScript* script) { return script == task_->script(); }
bool operator()(Realm* realm) { return realm == task_->script()->realm(); }
bool operator()(Zone* zone) {
return zone == task_->script()->zoneFromAnyThread();
}
bool operator()(JSRuntime* runtime) {
return runtime == task_->script()->runtimeFromAnyThread();
}
bool operator()(ZonesInState zbs) {
return zbs.runtime == task_->script()->runtimeFromAnyThread() &&
zbs.state == task_->script()->zoneFromAnyThread()->gcState();
}
};
return selector.match(TaskMatches{task});
}
static void CancelOffThreadIonCompileLocked(const CompilationSelector& selector,
AutoLockHelperThreadState& lock) {
if (HelperThreadState().threads(lock).empty()) {
return;
}
/* Cancel any pending entries for which processing hasn't started. */
GlobalHelperThreadState::IonCompileTaskVector& worklist =
HelperThreadState().ionWorklist(lock);
for (size_t i = 0; i < worklist.length(); i++) {
jit::IonCompileTask* task = worklist[i];
if (IonCompileTaskMatches(selector, task)) {
// Once finished, tasks are added to a Linked list which is
// allocated with the IonCompileTask class. The IonCompileTask is
// allocated in the LifoAlloc so we need the LifoAlloc to be mutable.
worklist[i]->alloc().lifoAlloc()->setReadWrite();
FinishOffThreadIonCompile(task, lock);
HelperThreadState().remove(worklist, &i);
}
}
/* Wait for in progress entries to finish up. */
bool cancelled;
do {
cancelled = false;
for (auto* helper : HelperThreadState().helperTasks(lock)) {
if (!helper->is<jit::IonCompileTask>()) {
continue;
}
jit::IonCompileTask* ionCompileTask = helper->as<jit::IonCompileTask>();
if (IonCompileTaskMatches(selector, ionCompileTask)) {
ionCompileTask->mirGen().cancel();
cancelled = true;
}
}
if (cancelled) {
HelperThreadState().wait(lock, GlobalHelperThreadState::CONSUMER);
}
} while (cancelled);
/* Cancel code generation for any completed entries. */
GlobalHelperThreadState::IonCompileTaskVector& finished =
HelperThreadState().ionFinishedList(lock);
for (size_t i = 0; i < finished.length(); i++) {
jit::IonCompileTask* task = finished[i];
if (IonCompileTaskMatches(selector, task)) {
JSRuntime* rt = task->script()->runtimeFromAnyThread();
rt->jitRuntime()->numFinishedOffThreadTasksRef(lock)--;
jit::FinishOffThreadTask(rt, task, lock);
HelperThreadState().remove(finished, &i);
}
}
/* Cancel lazy linking for pending tasks (attached to the ionScript). */
JSRuntime* runtime = GetSelectorRuntime(selector);
jit::IonCompileTask* task =
runtime->jitRuntime()->ionLazyLinkList(runtime).getFirst();
while (task) {
jit::IonCompileTask* next = task->getNext();
if (IonCompileTaskMatches(selector, task)) {
jit::FinishOffThreadTask(runtime, task, lock);
}
task = next;
}
}
void js::CancelOffThreadIonCompile(const CompilationSelector& selector) {
if (!JitDataStructuresExist(selector)) {
return;
}
AutoLockHelperThreadState lock;
CancelOffThreadIonCompileLocked(selector, lock);
}
#ifdef DEBUG
bool js::HasOffThreadIonCompile(Realm* realm) {
AutoLockHelperThreadState lock;
if (HelperThreadState().threads(lock).empty()) {
return false;
}
GlobalHelperThreadState::IonCompileTaskVector& worklist =
HelperThreadState().ionWorklist(lock);
for (size_t i = 0; i < worklist.length(); i++) {
jit::IonCompileTask* task = worklist[i];
if (task->script()->realm() == realm) {
return true;
}
}
for (auto* helper : HelperThreadState().helperTasks(lock)) {
if (helper->is<jit::IonCompileTask>() &&
helper->as<jit::IonCompileTask>()->script()->realm() == realm) {
return true;
}
}
GlobalHelperThreadState::IonCompileTaskVector& finished =
HelperThreadState().ionFinishedList(lock);
for (size_t i = 0; i < finished.length(); i++) {
jit::IonCompileTask* task = finished[i];
if (task->script()->realm() == realm) {
return true;
}
}
JSRuntime* rt = realm->runtimeFromMainThread();
jit::IonCompileTask* task = rt->jitRuntime()->ionLazyLinkList(rt).getFirst();
while (task) {
if (task->script()->realm() == realm) {
return true;
}
task = task->getNext();
}
return false;
}
#endif
struct MOZ_RAII AutoSetContextParse {
explicit AutoSetContextParse(ParseTask* task) {
TlsContext.get()->setParseTask(task);
}
~AutoSetContextParse() { TlsContext.get()->setParseTask(nullptr); }
};
// We want our default stack size limit to be approximately 2MB, to be safe, but
// expect most threads to use much less. On Linux, however, requesting a stack
// of 2MB or larger risks the kernel allocating an entire 2MB huge page for it
// on first access, which we do not want. To avoid this possibility, we subtract
// 2 standard VM page sizes from our default.
static const uint32_t kDefaultHelperStackSize = 2048 * 1024 - 2 * 4096;
static const uint32_t kDefaultHelperStackQuota = 1800 * 1024;
// TSan enforces a minimum stack size that's just slightly larger than our
// default helper stack size. It does this to store blobs of TSan-specific
// data on each thread's stack. Unfortunately, that means that even though
// we'll actually receive a larger stack than we requested, the effective
// usable space of that stack is significantly less than what we expect.
// To offset TSan stealing our stack space from underneath us, double the
// default.
//
// Note that we don't need this for ASan/MOZ_ASAN because ASan doesn't
// require all the thread-specific state that TSan does.
#if defined(MOZ_TSAN)
static const uint32_t HELPER_STACK_SIZE = 2 * kDefaultHelperStackSize;
static const uint32_t HELPER_STACK_QUOTA = 2 * kDefaultHelperStackQuota;
#else
static const uint32_t HELPER_STACK_SIZE = kDefaultHelperStackSize;
static const uint32_t HELPER_STACK_QUOTA = kDefaultHelperStackQuota;
#endif
AutoSetHelperThreadContext::AutoSetHelperThreadContext(
AutoLockHelperThreadState& lock)
: lock(lock) {
cx = HelperThreadState().getFirstUnusedContext(lock);
MOZ_ASSERT(cx);
cx->setHelperThread(lock);
cx->nativeStackBase = GetNativeStackBase();
// When we set the JSContext, we need to reset the computed stack limits for
// the current thread, so we also set the native stack quota.
JS_SetNativeStackQuota(cx, HELPER_STACK_QUOTA);
}
AutoSetHelperThreadContext::~AutoSetHelperThreadContext() {
cx->tempLifoAlloc().releaseAll();
if (cx->shouldFreeUnusedMemory()) {
cx->tempLifoAlloc().freeAll();
cx->setFreeUnusedMemory(false);
}
cx->clearHelperThread(lock);
cx = nullptr;
}
static const JSClass parseTaskGlobalClass = {"internal-parse-task-global",
JSCLASS_GLOBAL_FLAGS,
&JS::DefaultGlobalClassOps};
ParseTask::ParseTask(ParseTaskKind kind, JSContext* cx,
JS::OffThreadCompileCallback callback, void* callbackData)
: kind(kind),
options(cx),
parseGlobal(nullptr),
callback(callback),
callbackData(callbackData),
overRecursed(false),
outOfMemory(false) {
// Note that |cx| is the main thread context here but the parse task will
// run with a different, helper thread, context.
MOZ_ASSERT(!cx->isHelperThreadContext());
MOZ_ALWAYS_TRUE(scripts.reserve(scripts.capacity()));
MOZ_ALWAYS_TRUE(sourceObjects.reserve(sourceObjects.capacity()));
}
bool ParseTask::init(JSContext* cx, const ReadOnlyCompileOptions& options,
JSObject* global) {
MOZ_ASSERT(!cx->isHelperThreadContext());
if (!this->options.copy(cx, options)) {
return false;
}
runtime = cx->runtime();
parseGlobal = global;
return true;
}
void ParseTask::activate(JSRuntime* rt) {
rt->addParseTaskRef();
if (parseGlobal) {
rt->setUsedByHelperThread(parseGlobal->zone());
}
}
ParseTask::~ParseTask() = default;
void ParseTask::trace(JSTracer* trc) {
if (runtime != trc->runtime()) {
return;
}
if (parseGlobal) {
Zone* zone = MaybeForwarded(parseGlobal)->zoneFromAnyThread();
if (zone->usedByHelperThread()) {
MOZ_ASSERT(!zone->isCollecting());
return;
}
}
TraceNullableRoot(trc, &parseGlobal, "ParseTask::parseGlobal");
scripts.trace(trc);
sourceObjects.trace(trc);
if (stencil_) {
stencil_->trace(trc);
}
if (stencilSet_) {
stencilSet_->trace(trc);
}
gcOutput_.trace(trc);
gcOutputForDelazification_.trace(trc);
}
size_t ParseTask::sizeOfExcludingThis(
mozilla::MallocSizeOf mallocSizeOf) const {
return options.sizeOfExcludingThis(mallocSizeOf) +
errors.sizeOfExcludingThis(mallocSizeOf);
}
void ParseTask::runHelperThreadTask(AutoLockHelperThreadState& locked) {
#ifdef DEBUG
if (parseGlobal) {
runtime->incOffThreadParsesRunning();
}
#endif
runTask(locked);
// The callback is invoked while we are still off thread.
callback(this, callbackData);
// FinishOffThreadScript will need to be called on the script to
// migrate it into the correct compartment.
HelperThreadState().parseFinishedList(locked).insertBack(this);
#ifdef DEBUG
if (parseGlobal) {
runtime->decOffThreadParsesRunning();
}
#endif
}
void ParseTask::runTask(AutoLockHelperThreadState& lock) {
AutoSetHelperThreadContext usesContext(lock);
AutoUnlockHelperThreadState unlock(lock);
JSContext* cx = TlsContext.get();
AutoSetContextRuntime ascr(runtime);
AutoSetContextParse parsetask(this);
gc::AutoSuppressNurseryCellAlloc noNurseryAlloc(cx);
Zone* zone = nullptr;
if (parseGlobal) {
zone = parseGlobal->zoneFromAnyThread();
zone->setHelperThreadOwnerContext(cx);
}
auto resetOwnerContext = mozilla::MakeScopeExit([&] {
if (zone) {
zone->setHelperThreadOwnerContext(nullptr);
}
});
Maybe<AutoRealm> ar;
if (parseGlobal) {
ar.emplace(cx, parseGlobal);
}
parse(cx);
MOZ_ASSERT(cx->tempLifoAlloc().isEmpty());
cx->tempLifoAlloc().freeAll();
cx->frontendCollectionPool().purge();
cx->atomsZoneFreeLists().clear();
}
template <typename Unit>
struct ScriptParseTask : public ParseTask {
JS::SourceText<Unit> data;
ScriptParseTask(JSContext* cx, JS::SourceText<Unit>& srcBuf,
JS::OffThreadCompileCallback callback, void* callbackData);
void parse(JSContext* cx) override;
};
template <typename Unit>
ScriptParseTask<Unit>::ScriptParseTask(JSContext* cx,
JS::SourceText<Unit>& srcBuf,
JS::OffThreadCompileCallback callback,
void* callbackData)
: ParseTask(ParseTaskKind::Script, cx, callback, callbackData),
data(std::move(srcBuf)) {}
template <typename Unit>
void ScriptParseTask<Unit>::parse(JSContext* cx) {
MOZ_ASSERT(cx->isHelperThreadContext());
ScopeKind scopeKind =
options.nonSyntacticScope ? ScopeKind::NonSyntactic : ScopeKind::Global;
stencil_ =
frontend::CompileGlobalScriptToStencil(cx, options, data, scopeKind);
if (stencil_) {
if (!frontend::PrepareForInstantiate(cx, *stencil_, gcOutput_)) {
stencil_ = nullptr;
}
}
if (options.useOffThreadParseGlobal) {
Unused << instantiateStencils(cx);
}
}
bool ParseTask::instantiateStencils(JSContext* cx) {
if (!stencil_ && !stencilSet_) {
return false;
}
bool result;
if (stencil_) {
result = frontend::InstantiateStencils(cx, *stencil_, gcOutput_);
} else {
result = frontend::InstantiateStencils(cx, *stencilSet_, gcOutput_,
gcOutputForDelazification_);
}
// Whatever happens to the top-level script compilation (even if it fails),
// we must finish initializing the SSO. This is because there may be valid
// inner scripts observable by the debugger which reference the partially-
// initialized SSO.
if (gcOutput_.sourceObject) {
sourceObjects.infallibleAppend(gcOutput_.sourceObject);
}
if (result) {
MOZ_ASSERT(gcOutput_.script);
MOZ_ASSERT_IF(gcOutput_.module,
gcOutput_.module->script() == gcOutput_.script);
scripts.infallibleAppend(gcOutput_.script);
}
return result;
}
template <typename Unit>
struct ModuleParseTask : public ParseTask {
JS::SourceText<Unit> data;
ModuleParseTask(JSContext* cx, JS::SourceText<Unit>& srcBuf,
JS::OffThreadCompileCallback callback, void* callbackData);
void parse(JSContext* cx) override;
};
template <typename Unit>
ModuleParseTask<Unit>::ModuleParseTask(JSContext* cx,
JS::SourceText<Unit>& srcBuf,
JS::OffThreadCompileCallback callback,
void* callbackData)
: ParseTask(ParseTaskKind::Module, cx, callback, callbackData),
data(std::move(srcBuf)) {}
template <typename Unit>
void ModuleParseTask<Unit>::parse(JSContext* cx) {
MOZ_ASSERT(cx->isHelperThreadContext());
options.setModule();
stencil_ = frontend::ParseModuleToStencil(cx, options, data);
if (stencil_) {
if (!frontend::PrepareForInstantiate(cx, *stencil_, gcOutput_)) {
stencil_ = nullptr;
}
}
if (options.useOffThreadParseGlobal) {
Unused << instantiateStencils(cx);
}
}
ScriptDecodeTask::ScriptDecodeTask(JSContext* cx,
const JS::TranscodeRange& range,
JS::OffThreadCompileCallback callback,
void* callbackData)
: ParseTask(ParseTaskKind::ScriptDecode, cx, callback, callbackData),
range(range) {
MOZ_ASSERT(JS::IsTranscodingBytecodeAligned(range.begin().get()));
}
void ScriptDecodeTask::parse(JSContext* cx) {
MOZ_ASSERT(cx->isHelperThreadContext());
RootedScript resultScript(cx);
Rooted<ScriptSourceObject*> sourceObject(cx);
if (options.useStencilXDR) {
// The buffer contains stencil.
Rooted<UniquePtr<frontend::CompilationStencilSet>> stencilSet(
cx, js_new<frontend::CompilationStencilSet>(cx, options));
if (!stencilSet) {
ReportOutOfMemory(cx);
return;
}
XDRStencilDecoder decoder(cx, &stencilSet.get()->input.options, range);
if (!stencilSet.get()->input.initForGlobal(cx)) {
return;
}
XDRResult res = decoder.codeStencils(*stencilSet);
if (!res.isOk()) {
return;
}
stencilSet_ = std::move(stencilSet.get());
if (stencilSet_) {
if (!frontend::PrepareForInstantiate(cx, *stencilSet_, gcOutput_,
gcOutputForDelazification_)) {
stencilSet_ = nullptr;
}
}
if (options.useOffThreadParseGlobal) {
Unused << instantiateStencils(cx);
}
return;
}
// The buffer contains JSScript.
Rooted<UniquePtr<XDROffThreadDecoder>> decoder(
cx, js::MakeUnique<XDROffThreadDecoder>(
cx, &options, XDROffThreadDecoder::Type::Single,
/* sourceObjectOut = */ &sourceObject.get(), range));
if (!decoder) {
ReportOutOfMemory(cx);
return;
}
mozilla::DebugOnly<XDRResult> res = decoder->codeScript(&resultScript);
MOZ_ASSERT(bool(resultScript) == static_cast<const XDRResult&>(res).isOk());
if (sourceObject) {
sourceObjects.infallibleAppend(sourceObject);
}
if (resultScript) {
scripts.infallibleAppend(resultScript);
}
}
MultiScriptsDecodeTask::MultiScriptsDecodeTask(
JSContext* cx, JS::TranscodeSources& sources,
JS::OffThreadCompileCallback callback, void* callbackData)
: ParseTask(ParseTaskKind::MultiScriptsDecode, cx, callback, callbackData),
sources(&sources) {}
void MultiScriptsDecodeTask::parse(JSContext* cx) {
MOZ_ASSERT(cx->isHelperThreadContext());
if (!scripts.reserve(sources->length()) ||
!sourceObjects.reserve(sources->length())) {
ReportOutOfMemory(cx); // This sets |outOfMemory|.
return;
}
for (auto& source : *sources) {
CompileOptions opts(cx, options);
opts.setFileAndLine(source.filename, source.lineno);
RootedScript resultScript(cx);
Rooted<ScriptSourceObject*> sourceObject(cx);
Rooted<UniquePtr<XDROffThreadDecoder>> decoder(
cx, js::MakeUnique<XDROffThreadDecoder>(
cx, &opts, XDROffThreadDecoder::Type::Multi,
&sourceObject.get(), source.range));
if (!decoder) {
ReportOutOfMemory(cx);
return;
}
mozilla::DebugOnly<XDRResult> res = decoder->codeScript(&resultScript);
MOZ_ASSERT(bool(resultScript) == static_cast<const XDRResult&>(res).isOk());
if (sourceObject) {
sourceObjects.infallibleAppend(sourceObject);
}
if (resultScript) {
scripts.infallibleAppend(resultScript);
} else {
// If any decodes fail, don't process the rest. We likely are hitting OOM.
break;
}
}
}
static void WaitForOffThreadParses(JSRuntime* rt,
AutoLockHelperThreadState& lock) {
if (HelperThreadState().threads(lock).empty()) {
return;
}
GlobalHelperThreadState::ParseTaskVector& worklist =
HelperThreadState().parseWorklist(lock);
while (true) {
bool pending = false;
for (const auto& task : worklist) {
if (task->runtimeMatches(rt)) {
pending = true;
break;
}
}
if (!pending) {
bool inProgress = false;
for (auto* helper : HelperThreadState().helperTasks(lock)) {
if (helper->is<ParseTask>() &&
helper->as<ParseTask>()->runtimeMatches(rt)) {
inProgress = true;
break;
}
}
if (!inProgress) {
break;
}
}
HelperThreadState().wait(lock, GlobalHelperThreadState::CONSUMER);
}
#ifdef DEBUG
for (const auto& task : worklist) {
MOZ_ASSERT(!task->runtimeMatches(rt));
}
for (auto* helper : HelperThreadState().helperTasks(lock)) {
MOZ_ASSERT_IF(helper->is<ParseTask>(),
!helper->as<ParseTask>()->runtimeMatches(rt));
}
#endif
}
void js::WaitForOffThreadParses(JSRuntime* rt) {
AutoLockHelperThreadState lock;
WaitForOffThreadParses(rt, lock);
}
void js::CancelOffThreadParses(JSRuntime* rt) {
AutoLockHelperThreadState lock;
#ifdef DEBUG
for (const auto& task : HelperThreadState().parseWaitingOnGC(lock)) {
MOZ_ASSERT(!task->runtimeMatches(rt));
}
#endif
// Instead of forcibly canceling pending parse tasks, just wait for all
// scheduled and in progress ones to complete. Otherwise the final GC may not
// collect everything due to zones being used off thread.
WaitForOffThreadParses(rt, lock);
// Clean up any parse tasks which haven't been finished by the main thread.
auto& finished = HelperThreadState().parseFinishedList(lock);
while (true) {
bool found = false;
ParseTask* next;
ParseTask* task = finished.getFirst();
while (task) {
next = task->getNext();
if (task->runtimeMatches(rt)) {
found = true;
task->remove();
HelperThreadState().destroyParseTask(rt, task);
}
task = next;
}
if (!found) {
break;
}
}
#ifdef DEBUG
for (ParseTask* task : finished) {
MOZ_ASSERT(!task->runtimeMatches(rt));
}
#endif
}
bool js::OffThreadParsingMustWaitForGC(JSRuntime* rt) {
// Off thread parsing can't occur during incremental collections on the
// atoms zone, to avoid triggering barriers. (Outside the atoms zone, the
// compilation will use a new zone that is never collected.) If an
// atoms-zone GC is in progress, hold off on executing the parse task until
// the atoms-zone GC completes (see EnqueuePendingParseTasksAfterGC).
return rt->activeGCInAtomsZone();
}
static bool EnsureConstructor(JSContext* cx, Handle<GlobalObject*> global,
JSProtoKey key) {
if (!GlobalObject::ensureConstructor(cx, global, key)) {
return false;
}
MOZ_ASSERT(global->getPrototype(key).toObject().isDelegate(),
"standard class prototype wasn't a delegate from birth");
return true;
}
// Initialize all classes potentially created during parsing for use in parser
// data structures, template objects, &c.
static bool EnsureParserCreatedClasses(JSContext* cx, ParseTaskKind kind) {
Handle<GlobalObject*> global = cx->global();
if (!EnsureConstructor(cx, global, JSProto_Function)) {
return false; // needed by functions, also adds object literals' proto
}
if (!EnsureConstructor(cx, global, JSProto_Array)) {
return false; // needed by array literals
}
if (!EnsureConstructor(cx, global, JSProto_RegExp)) {
return false; // needed by regular expression literals
}
if (!EnsureConstructor(cx, global, JSProto_GeneratorFunction)) {
return false; // needed by function*() {}
}
if (!EnsureConstructor(cx, global, JSProto_AsyncFunction)) {
return false; // needed by async function() {}
}
if (!EnsureConstructor(cx, global, JSProto_AsyncGeneratorFunction)) {
return false; // needed by async function*() {}
}
if (kind == ParseTaskKind::Module &&
!GlobalObject::ensureModulePrototypesCreated(cx, global)) {
return false;
}
return true;
}
class MOZ_RAII AutoSetCreatedForHelperThread {
Zone* zone;
public:
explicit AutoSetCreatedForHelperThread(JSObject* global)
: zone(global ? global->zone() : nullptr) {
if (zone) {
zone->setCreatedForHelperThread();
}
}
void forget() { zone = nullptr; }
~AutoSetCreatedForHelperThread() {
if (zone) {
zone->clearUsedByHelperThread();
}
}
};
static JSObject* CreateGlobalForOffThreadParse(JSContext* cx,
const gc::AutoSuppressGC& nogc) {
JS::Realm* currentRealm = cx->realm();
JS::RealmOptions realmOptions(currentRealm->creationOptions(),
currentRealm->behaviors());
auto& creationOptions = realmOptions.creationOptions();
creationOptions.setInvisibleToDebugger(true)
.setMergeable(true)
.setNewCompartmentAndZone();
// Don't falsely inherit the host's global trace hook.
creationOptions.setTrace(nullptr);
return JS_NewGlobalObject(cx, &parseTaskGlobalClass,
currentRealm->principals(),
JS::DontFireOnNewGlobalHook, realmOptions);
}
static bool QueueOffThreadParseTask(JSContext* cx, UniquePtr<ParseTask> task) {
AutoLockHelperThreadState lock;
bool mustWait = task->options.useOffThreadParseGlobal &&
OffThreadParsingMustWaitForGC(cx->runtime());
bool result;
if (mustWait) {
result = HelperThreadState().parseWaitingOnGC(lock).append(std::move(task));
} else {
result =
HelperThreadState().submitTask(cx->runtime(), std::move(task), lock);
}
if (!result) {
ReportOutOfMemory(cx);
}
return result;
}
bool GlobalHelperThreadState::submitTask(
JSRuntime* rt, UniquePtr<ParseTask> task,
const AutoLockHelperThreadState& locked) {
if (!parseWorklist(locked).append(std::move(task))) {
return false;
}
parseWorklist(locked).back()->activate(rt);
dispatch(locked);
return true;
}
static JS::OffThreadToken* StartOffThreadParseTask(
JSContext* cx, UniquePtr<ParseTask> task,
const ReadOnlyCompileOptions& options) {
// Suppress GC so that calls below do not trigger a new incremental GC
// which could require barriers on the atoms zone.
gc::AutoSuppressGC nogc(cx);
gc::AutoSuppressNurseryCellAlloc noNurseryAlloc(cx);
AutoSuppressAllocationMetadataBuilder suppressMetadata(cx);
JSObject* global = nullptr;
if (options.useOffThreadParseGlobal) {
global = CreateGlobalForOffThreadParse(cx, nogc);
if (!global) {
return nullptr;
}
}
// Mark the global's zone as created for a helper thread. This prevents it
// from being collected until clearUsedByHelperThread() is called after
// parsing is complete. If this function exits due to error this state is
// cleared automatically.
AutoSetCreatedForHelperThread createdForHelper(global);
if (!task->init(cx, options, global)) {
return nullptr;
}
JS::OffThreadToken* token = task.get();
if (!QueueOffThreadParseTask(cx, std::move(task))) {
return nullptr;
}
createdForHelper.forget();
// Return an opaque pointer to caller so that it may query/cancel the task
// before the callback is fired.
return token;
}
template <typename Unit>
static JS::OffThreadToken* StartOffThreadParseScriptInternal(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<Unit>& srcBuf, JS::OffThreadCompileCallback callback,
void* callbackData) {
auto task = cx->make_unique<ScriptParseTask<Unit>>(cx, srcBuf, callback,
callbackData);
if (!task) {
return nullptr;
}
return StartOffThreadParseTask(cx, std::move(task), options);
}
JS::OffThreadToken* js::StartOffThreadParseScript(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<char16_t>& srcBuf, JS::OffThreadCompileCallback callback,
void* callbackData) {
return StartOffThreadParseScriptInternal(cx, options, srcBuf, callback,
callbackData);
}
JS::OffThreadToken* js::StartOffThreadParseScript(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<Utf8Unit>& srcBuf, JS::OffThreadCompileCallback callback,
void* callbackData) {
return StartOffThreadParseScriptInternal(cx, options, srcBuf, callback,
callbackData);
}
template <typename Unit>
static JS::OffThreadToken* StartOffThreadParseModuleInternal(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<Unit>& srcBuf, JS::OffThreadCompileCallback callback,
void* callbackData) {
auto task = cx->make_unique<ModuleParseTask<Unit>>(cx, srcBuf, callback,
callbackData);
if (!task) {
return nullptr;
}
return StartOffThreadParseTask(cx, std::move(task), options);
}
JS::OffThreadToken* js::StartOffThreadParseModule(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<char16_t>& srcBuf, JS::OffThreadCompileCallback callback,
void* callbackData) {
return StartOffThreadParseModuleInternal(cx, options, srcBuf, callback,
callbackData);
}
JS::OffThreadToken* js::StartOffThreadParseModule(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<Utf8Unit>& srcBuf, JS::OffThreadCompileCallback callback,
void* callbackData) {
return StartOffThreadParseModuleInternal(cx, options, srcBuf, callback,
callbackData);
}
JS::OffThreadToken* js::StartOffThreadDecodeScript(
JSContext* cx, const ReadOnlyCompileOptions& options,
const JS::TranscodeRange& range, JS::OffThreadCompileCallback callback,
void* callbackData) {
// XDR data must be Stencil format, or a parse-global must be available.
MOZ_RELEASE_ASSERT(options.useStencilXDR || options.useOffThreadParseGlobal);
auto task =
cx->make_unique<ScriptDecodeTask>(cx, range, callback, callbackData);
if (!task) {
return nullptr;
}
return StartOffThreadParseTask(cx, std::move(task), options);
}
JS::OffThreadToken* js::StartOffThreadDecodeMultiScripts(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::TranscodeSources& sources, JS::OffThreadCompileCallback callback,
void* callbackData) {
auto task = cx->make_unique<MultiScriptsDecodeTask>(cx, sources, callback,
callbackData);
if (!task) {
return nullptr;
}
// NOTE: All uses of DecodeMulti are currently generated by non-incremental
// XDR and therefore do not support the stencil format. As a result,
// they must continue to use the off-thread-parse-global in order to
// decode.
CompileOptions optionsCopy(cx, options);
optionsCopy.useStencilXDR = false;
optionsCopy.useOffThreadParseGlobal = true;
return StartOffThreadParseTask(cx, std::move(task), optionsCopy);
}
void js::EnqueuePendingParseTasksAfterGC(JSRuntime* rt) {
MOZ_ASSERT(!OffThreadParsingMustWaitForGC(rt));
AutoLockHelperThreadState lock;
GlobalHelperThreadState::ParseTaskVector& waiting =
HelperThreadState().parseWaitingOnGC(lock);
for (size_t i = 0; i < waiting.length(); i++) {
if (!waiting[i]->runtimeMatches(rt)) {
continue;
}
{
AutoEnterOOMUnsafeRegion oomUnsafe;
if (!HelperThreadState().submitTask(rt, std::move(waiting[i]), lock)) {
oomUnsafe.crash("EnqueuePendingParseTasksAfterGC");
}
}
HelperThreadState().remove(waiting, &i);
}
}
#ifdef DEBUG
bool js::CurrentThreadIsParseThread() {
JSContext* cx = TlsContext.get();
return cx->isHelperThreadContext() && cx->parseTask();
}
#endif
bool GlobalHelperThreadState::ensureInitialized() {
MOZ_ASSERT(CanUseExtraThreads());
MOZ_ASSERT(this == &HelperThreadState());
return ensureThreadCount(threadCount);
}
bool GlobalHelperThreadState::ensureThreadCount(size_t count) {
if (!ensureContextList(count)) {
return false;
}
AutoLockHelperThreadState lock;
if (threads(lock).length() >= count) {
return true;
}
if (!threads(lock).reserve(count)) {
return false;
}
if (!helperTasks_.reserve(count)) {
return false;
}
for (size_t& i : runningTaskCount) {
i = 0;
}
// Update threadCount on exit so this stays consistent with how many threads
// there are.
auto updateThreadCount =
mozilla::MakeScopeExit([&] { threadCount = threads(lock).length(); });
while (threads(lock).length() < count) {
auto thread = js::MakeUnique<HelperThread>();
if (!thread || !thread->init()) {
return false;
}
threads(lock).infallibleEmplaceBack(std::move(thread));
}
return true;
}
GlobalHelperThreadState::GlobalHelperThreadState()
: cpuCount(0),
threadCount(0),
totalCountRunningTasks(0),
registerThread(nullptr),
unregisterThread(nullptr),
wasmTier2GeneratorsFinished_(0) {
cpuCount = ClampDefaultCPUCount(GetCPUCount());
threadCount = ThreadCountForCPUCount(cpuCount);
gcParallelThreadCount = threadCount;
MOZ_ASSERT(cpuCount > 0, "GetCPUCount() seems broken");
}
void GlobalHelperThreadState::finish() {
finishThreads();
// Make sure there are no Ion free tasks left. We check this here because,
// unlike the other tasks, we don't explicitly block on this when
// destroying a runtime.
AutoLockHelperThreadState lock;
auto& freeList = ionFreeList(lock);
while (!freeList.empty()) {
UniquePtr<jit::IonFreeTask> task = std::move(freeList.back());
freeList.popBack();
jit::FreeIonCompileTask(task->compileTask());
}
destroyHelperContexts(lock);
}
void GlobalHelperThreadState::finishThreads() {
HelperThreadVector oldThreads;
{
AutoLockHelperThreadState lock;
if (threads(lock).empty()) {
return;
}
MOZ_ASSERT(CanUseExtraThreads());
waitForAllThreadsLocked(lock);
for (auto& thread : threads(lock)) {
thread->setTerminate(lock);
}
notifyAll(GlobalHelperThreadState::PRODUCER, lock);
std::swap(threads_, oldThreads);
}
for (auto& thread : oldThreads) {
thread->join();
}
}
bool GlobalHelperThreadState::ensureContextList(size_t count) {
AutoLockHelperThreadState lock;
if (helperContexts_.length() >= count) {
return true;
}
while (helperContexts_.length() < count) {
auto cx = js::MakeUnique<JSContext>(nullptr, JS::ContextOptions());
if (!cx || !cx->init(ContextKind::HelperThread) ||
!helperContexts_.append(cx.release())) {
return false;
}
}
return true;
}
JSContext* GlobalHelperThreadState::getFirstUnusedContext(
AutoLockHelperThreadState& locked) {
for (auto& cx : helperContexts_) {
if (cx->contextAvailable(locked)) {
return cx;
}
}
MOZ_CRASH("Expected available JSContext");
}
void GlobalHelperThreadState::destroyHelperContexts(
AutoLockHelperThreadState& lock) {
while (helperContexts_.length() > 0) {
js_delete(helperContexts_.popCopy());
}
}
#ifdef DEBUG
void GlobalHelperThreadState::assertIsLockedByCurrentThread() const {
gHelperThreadLock.assertOwnedByCurrentThread();
}
#endif // DEBUG
void GlobalHelperThreadState::dispatch(
const AutoLockHelperThreadState& locked) {
notifyOne(PRODUCER, locked);
}
void GlobalHelperThreadState::wait(
AutoLockHelperThreadState& locked, CondVar which,
TimeDuration timeout /* = TimeDuration::Forever() */) {
whichWakeup(which).wait_for(locked, timeout);
}
void GlobalHelperThreadState::notifyAll(CondVar which,
const AutoLockHelperThreadState&) {
whichWakeup(which).notify_all();
}
void GlobalHelperThreadState::notifyOne(CondVar which,
const AutoLockHelperThreadState&) {
whichWakeup(which).notify_one();
}
bool GlobalHelperThreadState::hasActiveThreads(
const AutoLockHelperThreadState& lock) {
return !helperTasks(lock).empty();
}
void js::WaitForAllHelperThreads() { HelperThreadState().waitForAllThreads(); }
void js::WaitForAllHelperThreads(AutoLockHelperThreadState& lock) {
HelperThreadState().waitForAllThreadsLocked(lock);
}
void GlobalHelperThreadState::waitForAllThreads() {
AutoLockHelperThreadState lock;
waitForAllThreadsLocked(lock);
}
void GlobalHelperThreadState::waitForAllThreadsLocked(
AutoLockHelperThreadState& lock) {
CancelOffThreadWasmTier2GeneratorLocked(lock);
while (hasActiveThreads(lock) || hasQueuedTasks(lock)) {
wait(lock, CONSUMER);
}
MOZ_ASSERT(!hasActiveThreads(lock));
MOZ_ASSERT(!hasQueuedTasks(lock));
}
// A task can be a "master" task, ie, it will block waiting for other worker
// threads that perform work on its behalf. If so it must not take the last
// available thread; there must always be at least one worker thread able to do
// the actual work. (Or the system may deadlock.)
//
// If a task is a master task it *must* pass isMaster=true here, or perform a
// similar calculation to avoid deadlock from starvation.
//
// isMaster should only be true if the thread calling checkTaskThreadLimit() is
// a helper thread.
//
// NOTE: Calling checkTaskThreadLimit() from a helper thread in the dynamic
// region after currentTask.emplace() and before currentTask.reset() may cause
// it to return a different result than if it is called outside that dynamic
// region, as the predicate inspects the values of the threads' currentTask
// members.
bool GlobalHelperThreadState::checkTaskThreadLimit(
ThreadType threadType, size_t maxThreads, bool isMaster,
const AutoLockHelperThreadState& lock) const {
MOZ_ASSERT(maxThreads > 0);
if (!isMaster && maxThreads >= threadCount) {
return true;
}
size_t count = runningTaskCount[threadType];
if (count >= maxThreads) {
return false;
}
MOZ_ASSERT(threadCount >= totalCountRunningTasks);
size_t idle = threadCount - totalCountRunningTasks;
// It is possible for the number of idle threads to be zero here, because
// checkTaskThreadLimit() can be called from non-helper threads. Notably,
// the compression task scheduler invokes it, and runs off a helper thread.
if (idle == 0) {
return false;
}
// A master thread that's the last available thread must not be allowed to
// run.
if (isMaster && idle == 1) {
return false;
}
return true;
}
void GlobalHelperThreadState::triggerFreeUnusedMemory() {
if (!CanUseExtraThreads()) {
return;
}
AutoLockHelperThreadState lock;
for (auto& context : helperContexts_) {
if (context->shouldFreeUnusedMemory() && context->contextAvailable(lock)) {
// This context hasn't been used since the last time freeUnusedMemory
// was set. Free the temp LifoAlloc from the main thread.
context->tempLifoAllocNoCheck().freeAll();
context->setFreeUnusedMemory(false);
} else {
context->setFreeUnusedMemory(true);
}
}
}
static inline bool IsHelperThreadSimulatingOOM(js::ThreadType threadType) {
#if defined(DEBUG) || defined(JS_OOM_BREAKPOINT)
return js::oom::simulator.targetThread() == threadType;
#else
return false;
#endif
}
void GlobalHelperThreadState::addSizeOfIncludingThis(
JS::GlobalStats* stats, AutoLockHelperThreadState& lock) const {
#ifdef DEBUG
assertIsLockedByCurrentThread();
#endif
mozilla::MallocSizeOf mallocSizeOf = stats->mallocSizeOf_;
JS::HelperThreadStats& htStats = stats->helperThread;
htStats.stateData += mallocSizeOf(this);
htStats.stateData += threads(lock).sizeOfExcludingThis(mallocSizeOf);
// Report memory used by various containers
htStats.stateData +=
ionWorklist_.sizeOfExcludingThis(mallocSizeOf) +
ionFinishedList_.sizeOfExcludingThis(mallocSizeOf) +
ionFreeList_.sizeOfExcludingThis(mallocSizeOf) +
wasmWorklist_tier1_.sizeOfExcludingThis(mallocSizeOf) +
wasmWorklist_tier2_.sizeOfExcludingThis(mallocSizeOf) +
wasmTier2GeneratorWorklist_.sizeOfExcludingThis(mallocSizeOf) +
promiseHelperTasks_.sizeOfExcludingThis(mallocSizeOf) +
parseWorklist_.sizeOfExcludingThis(mallocSizeOf) +
parseFinishedList_.sizeOfExcludingThis(mallocSizeOf) +
parseWaitingOnGC_.sizeOfExcludingThis(mallocSizeOf) +
compressionPendingList_.sizeOfExcludingThis(mallocSizeOf) +
compressionWorklist_.sizeOfExcludingThis(mallocSizeOf) +
compressionFinishedList_.sizeOfExcludingThis(mallocSizeOf) +
gcParallelWorklist_.sizeOfExcludingThis(mallocSizeOf) +
helperContexts_.sizeOfExcludingThis(mallocSizeOf) +
helperTasks_.sizeOfExcludingThis(mallocSizeOf);
// Report ParseTasks on wait lists
for (const auto& task : parseWorklist_) {
htStats.parseTask += task->sizeOfIncludingThis(mallocSizeOf);
}
for (auto task : parseFinishedList_) {
htStats.parseTask += task->sizeOfIncludingThis(mallocSizeOf);
}
for (const auto& task : parseWaitingOnGC_) {
htStats.parseTask += task->sizeOfIncludingThis(mallocSizeOf);
}
// Report IonCompileTasks on wait lists
for (auto task : ionWorklist_) {
htStats.ionCompileTask += task->sizeOfExcludingThis(mallocSizeOf);
}
for (auto task : ionFinishedList_) {
htStats.ionCompileTask += task->sizeOfExcludingThis(mallocSizeOf);
}
for (const auto& task : ionFreeList_) {
htStats.ionCompileTask +=
task->compileTask()->sizeOfExcludingThis(mallocSizeOf);
}
// Report wasm::CompileTasks on wait lists
for (auto task : wasmWorklist_tier1_) {
htStats.wasmCompile += task->sizeOfExcludingThis(mallocSizeOf);
}
for (auto task : wasmWorklist_tier2_) {
htStats.wasmCompile += task->sizeOfExcludingThis(mallocSizeOf);
}
{
// Report memory used by the JSContexts.
// We're holding the helper state lock, and the JSContext memory reporter
// won't do anything more substantial than traversing data structures and
// getting their size, so disable ProtectedData checks.
AutoNoteSingleThreadedRegion anstr;
for (auto* cx : helperContexts_) {
htStats.contexts += cx->sizeOfIncludingThis(mallocSizeOf);
}
}
// Report number of helper threads.
MOZ_ASSERT(htStats.idleThreadCount == 0);
MOZ_ASSERT(threadCount >= totalCountRunningTasks);
htStats.activeThreadCount = totalCountRunningTasks;
htStats.idleThreadCount = threadCount - totalCountRunningTasks;
}
size_t GlobalHelperThreadState::maxIonCompilationThreads() const {
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_ION)) {
return 1;
}
return threadCount;
}
size_t GlobalHelperThreadState::maxWasmCompilationThreads() const {
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_WASM)) {
return 1;
}
return cpuCount;
}
size_t GlobalHelperThreadState::maxWasmTier2GeneratorThreads() const {
return MaxTier2GeneratorTasks;
}
size_t GlobalHelperThreadState::maxPromiseHelperThreads() const {
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_WASM)) {
return 1;
}
return cpuCount;
}
size_t GlobalHelperThreadState::maxParseThreads() const {
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_PARSE)) {
return 1;
}
return cpuCount;
}
size_t GlobalHelperThreadState::maxCompressionThreads() const {
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_COMPRESS)) {
return 1;
}
// Compression is triggered on major GCs to compress ScriptSources. It is
// considered low priority work.
return 1;
}
size_t GlobalHelperThreadState::maxGCParallelThreads(
const AutoLockHelperThreadState& lock) const {
if (IsHelperThreadSimulatingOOM(js::THREAD_TYPE_GCPARALLEL)) {
return 1;
}
return gcParallelThreadCount;
}
HelperThreadTask* GlobalHelperThreadState::maybeGetWasmTier1CompileTask(
const AutoLockHelperThreadState& lock) {
return maybeGetWasmCompile(lock, wasm::CompileMode::Tier1);
}
HelperThreadTask* GlobalHelperThreadState::maybeGetWasmTier2CompileTask(
const AutoLockHelperThreadState& lock) {
return maybeGetWasmCompile(lock, wasm::CompileMode::Tier2);
}
HelperThreadTask* GlobalHelperThreadState::maybeGetWasmCompile(
const AutoLockHelperThreadState& lock, wasm::CompileMode mode) {
if (wasmWorklist(lock, mode).empty()) {
return nullptr;
}
// Parallel compilation and background compilation should be disabled on
// unicore systems.
MOZ_RELEASE_ASSERT(cpuCount > 1);
// If Tier2 is very backlogged we must give priority to it, since the Tier2
// queue holds onto Tier1 tasks. Indeed if Tier2 is backlogged we will
// devote more resources to Tier2 and not start any Tier1 work at all.
bool tier2oversubscribed = wasmTier2GeneratorWorklist(lock).length() > 20;
// For Tier1 and Once compilation, honor the maximum allowed threads to
// compile wasm jobs at once, to avoid oversaturating the machine.
//
// For Tier2 compilation we need to allow other things to happen too, so we
// do not allow all logical cores to be used for background work; instead we
// wish to use a fraction of the physical cores. We can't directly compute
// the physical cores from the logical cores, but 1/3 of the logical cores
// is a safe estimate for the number of physical cores available for
// background work.
size_t physCoresAvailable = size_t(ceil(cpuCount / 3.0));
size_t threads;
if (mode == wasm::CompileMode::Tier2) {
if (tier2oversubscribed) {
threads = maxWasmCompilationThreads();
} else {
threads = physCoresAvailable;
}
} else {
if (tier2oversubscribed) {
threads = 0;
} else {
threads = maxWasmCompilationThreads();
}
}
if (!threads || !checkTaskThreadLimit(THREAD_TYPE_WASM, threads, lock)) {
return nullptr;
}
return wasmWorklist(lock, mode).popCopyFront();
}
HelperThreadTask* GlobalHelperThreadState::maybeGetWasmTier2GeneratorTask(
const AutoLockHelperThreadState& lock) {
if (wasmTier2GeneratorWorklist(lock).empty() ||
!checkTaskThreadLimit(THREAD_TYPE_WASM_TIER2,
maxWasmTier2GeneratorThreads(),
/*isMaster=*/true, lock)) {
return nullptr;
}
return wasmTier2GeneratorWorklist(lock).popCopy();
}
HelperThreadTask* GlobalHelperThreadState::maybeGetPromiseHelperTask(
const AutoLockHelperThreadState& lock) {
// PromiseHelperTasks can be wasm compilation tasks that in turn block on
// wasm compilation so set isMaster = true.
if (promiseHelperTasks(lock).empty() ||
!checkTaskThreadLimit(THREAD_TYPE_PROMISE_TASK, maxPromiseHelperThreads(),
/*isMaster=*/true, lock)) {
return nullptr;
}
return promiseHelperTasks(lock).popCopy();
}
static bool IonCompileTaskHasHigherPriority(jit::IonCompileTask* first,
jit::IonCompileTask* second) {
// Return true if priority(first) > priority(second).
//
// This method can return whatever it wants, though it really ought to be a
// total order. The ordering is allowed to race (change on the fly), however.
// A higher warm-up counter indicates a higher priority.
jit::JitScript* firstJitScript = first->script()->jitScript();
jit::JitScript* secondJitScript = second->script()->jitScript();
return firstJitScript->warmUpCount() / first->script()->length() >
secondJitScript->warmUpCount() / second->script()->length();
}
HelperThreadTask* GlobalHelperThreadState::maybeGetIonCompileTask(
const AutoLockHelperThreadState& lock) {
if (ionWorklist(lock).empty() ||
!checkTaskThreadLimit(THREAD_TYPE_ION, maxIonCompilationThreads(),
lock)) {
return nullptr;
}
return highestPriorityPendingIonCompile(lock);
}
HelperThreadTask* GlobalHelperThreadState::maybeGetIonFreeTask(
const AutoLockHelperThreadState& lock) {
if (ionFreeList(lock).empty()) {
return nullptr;
}
UniquePtr<jit::IonFreeTask> task = std::move(ionFreeList(lock).back());
ionFreeList(lock).popBack();
return task.release();
}
jit::IonCompileTask* GlobalHelperThreadState::highestPriorityPendingIonCompile(
const AutoLockHelperThreadState& lock) {
auto& worklist = ionWorklist(lock);
MOZ_ASSERT(!worklist.empty());
// Get the highest priority IonCompileTask which has not started compilation
// yet.
size_t index = 0;
for (size_t i = 1; i < worklist.length(); i++) {
if (IonCompileTaskHasHigherPriority(worklist[i], worklist[index])) {
index = i;
}
}
jit::IonCompileTask* task = worklist[index];
worklist.erase(&worklist[index]);
return task;
}
HelperThreadTask* GlobalHelperThreadState::maybeGetParseTask(
const AutoLockHelperThreadState& lock) {
// Parse tasks that end up compiling asm.js in turn may use Wasm compilation
// threads to generate machine code. We have no way (at present) to know
// ahead of time whether a parse task is going to parse asm.js content or
// not, so we just assume that all parse tasks are master tasks.
if (parseWorklist(lock).empty() ||
!checkTaskThreadLimit(THREAD_TYPE_PARSE, maxParseThreads(),
/*isMaster=*/true, lock)) {
return nullptr;
}
auto& worklist = parseWorklist(lock);
UniquePtr<ParseTask> task = std::move(worklist.back());
worklist.popBack();
return task.release();
}
HelperThreadTask* GlobalHelperThreadState::maybeGetCompressionTask(
const AutoLockHelperThreadState& lock) {
if (compressionWorklist(lock).empty() ||
!checkTaskThreadLimit(THREAD_TYPE_COMPRESS, maxCompressionThreads(),
lock)) {
return nullptr;
}
auto& worklist = compressionWorklist(lock);
UniquePtr<SourceCompressionTask> task = std::move(worklist.back());
worklist.popBack();
return task.release();
}
void GlobalHelperThreadState::startHandlingCompressionTasks(
ScheduleCompressionTask schedule, JSRuntime* maybeRuntime,
const AutoLockHelperThreadState& lock) {
MOZ_ASSERT((schedule == ScheduleCompressionTask::GC) ==
(maybeRuntime != nullptr));
auto& pending = compressionPendingList(lock);
for (size_t i = 0; i < pending.length(); i++) {
UniquePtr<SourceCompressionTask>& task = pending[i];
if (schedule == ScheduleCompressionTask::API ||
(task->runtimeMatches(maybeRuntime) && task->shouldStart())) {
// OOMing during appending results in the task not being scheduled
// and deleted.
Unused << submitTask(std::move(task), lock);
remove(pending, &i);
}
}
}
bool GlobalHelperThreadState::submitTask(
UniquePtr<SourceCompressionTask> task,
const AutoLockHelperThreadState& locked) {
if (!compressionWorklist(locked).append(std::move(task))) {
return false;
}
dispatch(locked);
return true;
}
bool GlobalHelperThreadState::submitTask(
GCParallelTask* task, const AutoLockHelperThreadState& locked) {
gcParallelWorklist(locked).insertBack(task);
dispatch(locked);
return true;
}
HelperThreadTask* GlobalHelperThreadState::maybeGetGCParallelTask(
const AutoLockHelperThreadState& lock) {
if (gcParallelWorklist(lock).isEmpty() ||
!checkTaskThreadLimit(THREAD_TYPE_GCPARALLEL, maxGCParallelThreads(lock),
lock)) {
return nullptr;
}
return gcParallelWorklist(lock).popFirst();
}
static void LeaveParseTaskZone(JSRuntime* rt, ParseTask* task) {
// Mark the zone as no longer in use by a helper thread, and available
// to be collected by the GC.
if (task->parseGlobal) {
rt->clearUsedByHelperThread(task->parseGlobal->zoneFromAnyThread());
}
rt->decParseTaskRef();
}
ParseTask* GlobalHelperThreadState::removeFinishedParseTask(
JSContext* cx, ParseTaskKind kind, JS::OffThreadToken* token) {
// The token is really a ParseTask* which should be in the finished list.
auto task = static_cast<ParseTask*>(token);
// The token was passed in from the browser. Check that the pointer is likely
// a valid parse task of the expected kind.
MOZ_RELEASE_ASSERT(task->runtime == cx->runtime());
MOZ_RELEASE_ASSERT(task->kind == kind);
// Remove the task from the finished list.
AutoLockHelperThreadState lock;
MOZ_ASSERT(parseFinishedList(lock).contains(task));
task->remove();
return task;
}
UniquePtr<ParseTask> GlobalHelperThreadState::finishParseTaskCommon(
JSContext* cx, ParseTaskKind kind, JS::OffThreadToken* token) {
MOZ_ASSERT(!cx->isHelperThreadContext());
MOZ_ASSERT(cx->realm());
Rooted<UniquePtr<ParseTask>> parseTask(
cx, removeFinishedParseTask(cx, kind, token));
if (parseTask->options.useOffThreadParseGlobal) {
// Make sure we have all the constructors we need for the prototype
// remapping below, since we can't GC while that's happening.
if (!EnsureParserCreatedClasses(cx, kind)) {
LeaveParseTaskZone(cx->runtime(), parseTask.get().get());
return nullptr;
}
mergeParseTaskRealm(cx, parseTask.get().get(), cx->realm());
for (auto& script : parseTask->scripts) {
cx->releaseCheck(script);
}
if (kind == ParseTaskKind::Module) {
if (parseTask->scripts.length() > 0) {
MOZ_ASSERT(parseTask->scripts[0]->isModule());
parseTask->scripts[0]->module()->fixEnvironmentsAfterRealmMerge();
}
}
// Finish initializing ScriptSourceObject now that we are back on
// main-thread and in the correct realm.
for (auto& sourceObject : parseTask->sourceObjects) {
RootedScriptSourceObject sso(cx, sourceObject);
if (!ScriptSourceObject::initFromOptions(cx, sso, parseTask->options)) {
return nullptr;
}
if (!sso->source()->tryCompressOffThread(cx)) {
return nullptr;
}
}
} else {
// GC things should be allocated in finishSingleParseTask, after
// calling finishParseTaskCommon.
MOZ_ASSERT(parseTask->scripts.length() == 0);
MOZ_ASSERT(parseTask->sourceObjects.length() == 0);
}
// Report out of memory errors eagerly, or errors could be malformed.
if (parseTask->outOfMemory) {
ReportOutOfMemory(cx);
return nullptr;
}
// Report any error or warnings generated during the parse.
for (size_t i = 0; i < parseTask->errors.length(); i++) {
parseTask->errors[i]->throwError(cx);
}
if (parseTask->overRecursed) {
ReportOverRecursed(cx);
}
if (cx->isExceptionPending()) {
return nullptr;
}
if (parseTask->options.useOffThreadParseGlobal) {
if (coverage::IsLCovEnabled()) {
if (!generateLCovSources(cx, parseTask.get().get())) {
return nullptr;
}
}
}
return std::move(parseTask.get());
}
// Generate initial LCovSources for generated inner functions.
bool GlobalHelperThreadState::generateLCovSources(JSContext* cx,
ParseTask* parseTask) {
Rooted<GCVector<JSScript*>> workList(cx, GCVector<JSScript*>(cx));
if (!workList.appendAll(parseTask->scripts)) {
return false;
}
RootedScript elem(cx);
while (!workList.empty()) {
elem = workList.popCopy();
// Initialize LCov data for the script.
if (!coverage::InitScriptCoverage(cx, elem)) {
return false;
}
// Add inner-function scripts to the work-list.
for (JS::GCCellPtr gcThing : elem->gcthings()) {
if (!gcThing.is<JSObject>()) {
continue;
}
JSObject* obj = &gcThing.as<JSObject>();
if (!obj->is<JSFunction>()) {
continue;
}
JSFunction* fun = &obj->as<JSFunction>();
// Ignore asm.js functions
if (!fun->isInterpreted()) {
continue;
}
MOZ_ASSERT(fun->hasBytecode(),
"No lazy scripts exist when collecting coverage");
if (!workList.append(fun->nonLazyScript())) {
return false;
}
}
}
return true;
}
JSScript* GlobalHelperThreadState::finishSingleParseTask(
JSContext* cx, ParseTaskKind kind, JS::OffThreadToken* token,
StartEncoding startEncoding /* = StartEncoding::No */) {
Rooted<UniquePtr<ParseTask>> parseTask(
cx, finishParseTaskCommon(cx, kind, token));
if (!parseTask) {
return nullptr;
}
JS::RootedScript script(cx);
// Finish main-thread initialization of scripts.
if (parseTask->options.useOffThreadParseGlobal) {
if (parseTask->scripts.length() > 0) {
script = parseTask->scripts[0];
}
if (!script) {
// No error was reported, but no script produced. Assume we hit out of
// memory.
MOZ_ASSERT(false, "Expected script");
ReportOutOfMemory(cx);
return nullptr;
}
if (kind == ParseTaskKind::Module) {
// See: InstantiateTopLevel in frontend/Stencil.cpp.
MOZ_ASSERT(script->isModule());
RootedModuleObject module(cx, script->module());
if (!ModuleObject::Freeze(cx, module)) {
return nullptr;
}
}
// The Debugger only needs to be told about the topmost script that was
// compiled.
if (!parseTask->options.hideScriptFromDebugger) {
DebugAPI::onNewScript(cx, script);
}
} else {
MOZ_ASSERT(parseTask->stencil_.get() || parseTask->stencilSet_.get());
if (!parseTask->instantiateStencils(cx)) {
return nullptr;
}
MOZ_RELEASE_ASSERT(parseTask->scripts.length() == 1);
script = parseTask->scripts[0];
}
// Start the incremental-XDR encoder.
if (startEncoding == StartEncoding::Yes) {
if (parseTask->options.useStencilXDR) {
UniquePtr<XDRIncrementalEncoderBase> xdrEncoder;
if (parseTask->stencil_.get()) {
auto* stencil = parseTask->stencil_.get();
if (!stencil->input.source()->xdrEncodeInitialStencil(cx, *stencil,
xdrEncoder)) {
return nullptr;
}
} else {
auto* stencilSet = parseTask->stencilSet_.get();
if (!stencilSet->input.source()->xdrEncodeStencils(cx, *stencilSet,
xdrEncoder)) {
return nullptr;
}
}
script->scriptSource()->setIncrementalEncoder(xdrEncoder.release());
} else {
if (!script->scriptSource()->xdrEncodeTopLevel(cx, script)) {
return nullptr;
}
}
}
return script;
}
bool GlobalHelperThreadState::finishMultiParseTask(
JSContext* cx, ParseTaskKind kind, JS::OffThreadToken* token,
MutableHandle<ScriptVector> scripts) {
Rooted<UniquePtr<ParseTask>> parseTask(
cx, finishParseTaskCommon(cx, kind, token));
if (!parseTask) {
return false;
}
MOZ_ASSERT(parseTask->kind == ParseTaskKind::MultiScriptsDecode);
auto task = static_cast<MultiScriptsDecodeTask*>(parseTask.get().get());
size_t expectedLength = task->sources->length();
if (!scripts.reserve(parseTask->scripts.length())) {
ReportOutOfMemory(cx);
return false;
}
for (auto& script : parseTask->scripts) {
scripts.infallibleAppend(script);
}
if (scripts.length() != expectedLength) {
// No error was reported, but fewer scripts produced than expected.
// Assume we hit out of memory.
MOZ_ASSERT(false, "Expected more scripts");
ReportOutOfMemory(cx);
return false;
}
// The Debugger only needs to be told about the topmost scripts that were
// compiled.
if (!parseTask->options.hideScriptFromDebugger) {
JS::RootedScript rooted(cx);
for (auto& script : scripts) {
MOZ_ASSERT(script->isGlobalCode());
rooted = script;
DebugAPI::onNewScript(cx, rooted);
}
}
return true;
}
JSScript* GlobalHelperThreadState::finishScriptParseTask(
JSContext* cx, JS::OffThreadToken* token,
StartEncoding startEncoding /* = StartEncoding::No */) {
JSScript* script =
finishSingleParseTask(cx, ParseTaskKind::Script, token, startEncoding);
MOZ_ASSERT_IF(script, script->isGlobalCode());
return script;
}
JSScript* GlobalHelperThreadState::finishScriptDecodeTask(
JSContext* cx, JS::OffThreadToken* token) {
JSScript* script =
finishSingleParseTask(cx, ParseTaskKind::ScriptDecode, token);
MOZ_ASSERT_IF(script, script->isGlobalCode());
return script;
}
bool GlobalHelperThreadState::finishMultiScriptsDecodeTask(
JSContext* cx, JS::OffThreadToken* token,
MutableHandle<ScriptVector> scripts) {
return finishMultiParseTask(cx, ParseTaskKind::MultiScriptsDecode, token,
scripts);
}
JSObject* GlobalHelperThreadState::finishModuleParseTask(
JSContext* cx, JS::OffThreadToken* token) {
JSScript* script = finishSingleParseTask(cx, ParseTaskKind::Module, token);
if (!script) {
return nullptr;
}
return script->module();
}
void GlobalHelperThreadState::cancelParseTask(JSRuntime* rt, ParseTaskKind kind,
JS::OffThreadToken* token) {
AutoLockHelperThreadState lock;
MOZ_ASSERT(token);
ParseTask* task = static_cast<ParseTask*>(token);
// Check pending queues to see if we can simply remove the task.
GlobalHelperThreadState::ParseTaskVector& waitingOnGC =
HelperThreadState().parseWaitingOnGC(lock);
for (size_t i = 0; i < waitingOnGC.length(); i++) {
if (task == waitingOnGC[i]) {
MOZ_ASSERT(task->kind == kind);
MOZ_ASSERT(task->runtimeMatches(rt));
task->parseGlobal->zoneFromAnyThread()->clearUsedByHelperThread();
HelperThreadState().remove(waitingOnGC, &i);
return;
}
}
GlobalHelperThreadState::ParseTaskVector& worklist =
HelperThreadState().parseWorklist(lock);
for (size_t i = 0; i < worklist.length(); i++) {
if (task == worklist[i]) {
MOZ_ASSERT(task->kind == kind);
MOZ_ASSERT(task->runtimeMatches(rt));
LeaveParseTaskZone(rt, task);
HelperThreadState().remove(worklist, &i);
return;
}
}
// If task is currently running, wait for it to complete.
while (true) {
bool foundTask = false;
for (auto* helper : HelperThreadState().helperTasks(lock)) {
if (helper->is<ParseTask>() && helper->as<ParseTask>() == task) {
MOZ_ASSERT(helper->as<ParseTask>()->kind == kind);
MOZ_ASSERT(helper->as<ParseTask>()->runtimeMatches(rt));
foundTask = true;
break;
}
}
if (!foundTask) {
break;
}
HelperThreadState().wait(lock, GlobalHelperThreadState::CONSUMER);
}
auto& finished = HelperThreadState().parseFinishedList(lock);
for (auto* t : finished) {
if (task == t) {
MOZ_ASSERT(task->kind == kind);
MOZ_ASSERT(task->runtimeMatches(rt));
task->remove();
HelperThreadState().destroyParseTask(rt, task);
return;
}
}
}
void GlobalHelperThreadState::destroyParseTask(JSRuntime* rt,
ParseTask* parseTask) {
MOZ_ASSERT(!parseTask->isInList());
LeaveParseTaskZone(rt, parseTask);
js_delete(parseTask);
}
void GlobalHelperThreadState::mergeParseTaskRealm(JSContext* cx,
ParseTask* parseTask,
Realm* dest) {
MOZ_ASSERT(parseTask->parseGlobal);
// After we call LeaveParseTaskZone() it's not safe to GC until we have
// finished merging the contents of the parse task's realm into the
// destination realm.
JS::AutoAssertNoGC nogc(cx);
LeaveParseTaskZone(cx->runtime(), parseTask);
// Move the parsed script and all its contents into the desired realm.
gc::MergeRealms(parseTask->parseGlobal->as<GlobalObject>().realm(), dest);
}
HelperThread::HelperThread()
: thread(Thread::Options().setStackSize(HELPER_STACK_SIZE)) {}
bool HelperThread::init() {
return thread.init(HelperThread::ThreadMain, this);
}
void HelperThread::setTerminate(const AutoLockHelperThreadState& lock) {
terminate = true;
}
void HelperThread::join() { thread.join(); }
void HelperThread::ensureRegisteredWithProfiler() {
if (profilingStack) {
return;
}
// Note: To avoid dead locks, we should not hold on the helper thread lock
// while calling this function. This is safe because the registerThread field
// is a WriteOnceData<> type stored on the global helper tread state.
JS::RegisterThreadCallback callback = HelperThreadState().registerThread;
if (callback) {
profilingStack =
callback("JS Helper", reinterpret_cast<void*>(GetNativeStackBase()));
}
}
void HelperThread::unregisterWithProfilerIfNeeded() {
if (!profilingStack) {
return;
}
// Note: To avoid dead locks, we should not hold on the helper thread lock
// while calling this function. This is safe because the unregisterThread
// field is a WriteOnceData<> type stored on the global helper tread state.
JS::UnregisterThreadCallback callback = HelperThreadState().unregisterThread;
if (callback) {
callback();
profilingStack = nullptr;
}
}
/* static */
void HelperThread::ThreadMain(void* arg) {
ThisThread::SetName("JS Helper");
auto helper = static_cast<HelperThread*>(arg);
helper->ensureRegisteredWithProfiler();
helper->threadLoop();
helper->unregisterWithProfilerIfNeeded();
}
bool JSContext::addPendingCompileError(js::CompileError** error) {
auto errorPtr = make_unique<js::CompileError>();
if (!errorPtr) {
return false;
}
if (!parseTask_->errors.append(std::move(errorPtr))) {
ReportOutOfMemory(this);
return false;
}
*error = parseTask_->errors.back().get();
return true;
}
bool JSContext::isCompileErrorPending() const {
return parseTask_->errors.length() > 0;
}
void JSContext::addPendingOverRecursed() {
if (parseTask_) {
parseTask_->overRecursed = true;
}
}
void JSContext::addPendingOutOfMemory() {
// Keep in sync with recoverFromOutOfMemory.
if (parseTask_) {
parseTask_->outOfMemory = true;
}
}
bool js::EnqueueOffThreadCompression(JSContext* cx,
UniquePtr<SourceCompressionTask> task) {
AutoLockHelperThreadState lock;
auto& pending = HelperThreadState().compressionPendingList(lock);
if (!pending.append(std::move(task))) {
if (!cx->isHelperThreadContext()) {
ReportOutOfMemory(cx);
}
return false;
}
return true;
}
void js::StartHandlingCompressionsOnGC(JSRuntime* runtime) {
AutoLockHelperThreadState lock;
HelperThreadState().startHandlingCompressionTasks(
GlobalHelperThreadState::ScheduleCompressionTask::GC, runtime, lock);
}
template <typename T>
static void ClearCompressionTaskList(T& list, JSRuntime* runtime) {
for (size_t i = 0; i < list.length(); i++) {
if (list[i]->runtimeMatches(runtime)) {
HelperThreadState().remove(list, &i);
}
}
}
void js::CancelOffThreadCompressions(JSRuntime* runtime) {
AutoLockHelperThreadState lock;
if (HelperThreadState().threads(lock).empty()) {
return;
}
// Cancel all pending compression tasks.
ClearCompressionTaskList(HelperThreadState().compressionPendingList(lock),
runtime);
ClearCompressionTaskList(HelperThreadState().compressionWorklist(lock),
runtime);
// Cancel all in-process compression tasks and wait for them to join so we
// clean up the finished tasks.
while (true) {
bool inProgress = false;
for (auto* helper : HelperThreadState().helperTasks(lock)) {
if (!helper->is<SourceCompressionTask>()) {
continue;
}
if (helper->as<SourceCompressionTask>()->runtimeMatches(runtime)) {
inProgress = true;
}
}
if (!inProgress) {
break;
}
HelperThreadState().wait(lock, GlobalHelperThreadState::CONSUMER);
}
// Clean up finished tasks.
ClearCompressionTaskList(HelperThreadState().compressionFinishedList(lock),
runtime);
}
void js::AttachFinishedCompressions(JSRuntime* runtime,
AutoLockHelperThreadState& lock) {
auto& finished = HelperThreadState().compressionFinishedList(lock);
for (size_t i = 0; i < finished.length(); i++) {
if (finished[i]->runtimeMatches(runtime)) {
UniquePtr<SourceCompressionTask> compressionTask(std::move(finished[i]));
HelperThreadState().remove(finished, &i);
compressionTask->complete();
}
}
}
void js::SweepPendingCompressions(AutoLockHelperThreadState& lock) {
auto& pending = HelperThreadState().compressionPendingList(lock);
for (size_t i = 0; i < pending.length(); i++) {
if (pending[i]->shouldCancel()) {
HelperThreadState().remove(pending, &i);
}
}
}
void js::RunPendingSourceCompressions(JSRuntime* runtime) {
AutoLockHelperThreadState lock;
if (HelperThreadState().threads(lock).empty()) {
return;
}
HelperThreadState().startHandlingCompressionTasks(
GlobalHelperThreadState::ScheduleCompressionTask::API, nullptr, lock);
// Wait until all tasks have started compression.
while (!HelperThreadState().compressionWorklist(lock).empty()) {
HelperThreadState().wait(lock, GlobalHelperThreadState::CONSUMER);
}
// Wait for all in-process compression tasks to complete.
HelperThreadState().waitForAllThreadsLocked(lock);
AttachFinishedCompressions(runtime, lock);
}
void PromiseHelperTask::executeAndResolveAndDestroy(JSContext* cx) {
execute();
run(cx, JS::Dispatchable::NotShuttingDown);
}
void PromiseHelperTask::runHelperThreadTask(AutoLockHelperThreadState& lock) {
{
AutoUnlockHelperThreadState unlock(lock);
execute();
}
// Don't release the lock between dispatching the resolve and destroy
// operation (which may start immediately on another thread) and returning
// from this method.
dispatchResolveAndDestroy(lock);
}
bool js::StartOffThreadPromiseHelperTask(JSContext* cx,
UniquePtr<PromiseHelperTask> task) {
// Execute synchronously if there are no helper threads.
if (!CanUseExtraThreads()) {
task.release()->executeAndResolveAndDestroy(cx);
return true;
}
if (!HelperThreadState().submitTask(task.get())) {
ReportOutOfMemory(cx);
return false;
}
Unused << task.release();
return true;
}
bool js::StartOffThreadPromiseHelperTask(PromiseHelperTask* task) {
MOZ_ASSERT(CanUseExtraThreads());
return HelperThreadState().submitTask(task);
}
bool GlobalHelperThreadState::submitTask(PromiseHelperTask* task) {
AutoLockHelperThreadState lock;
if (!promiseHelperTasks(lock).append(task)) {
return false;
}
dispatch(lock);
return true;
}
void GlobalHelperThreadState::trace(JSTracer* trc) {
AutoLockHelperThreadState lock;
#ifdef DEBUG
// Since we hold the helper thread lock here we must disable GCMarker's
// checking of the atom marking bitmap since that also relies on taking the
// lock.
GCMarker* marker = nullptr;
if (trc->isMarkingTracer()) {
marker = GCMarker::fromTracer(trc);
marker->setCheckAtomMarking(false);
}
auto reenableAtomMarkingCheck = mozilla::MakeScopeExit([marker] {
if (marker) {
marker->setCheckAtomMarking(true);
}
});
#endif
for (auto task : ionWorklist(lock)) {
task->alloc().lifoAlloc()->setReadWrite();
task->trace(trc);
task->alloc().lifoAlloc()->setReadOnly();
}
for (auto task : ionFinishedList(lock)) {
task->trace(trc);
}
for (auto* helper : HelperThreadState().helperTasks(lock)) {
if (helper->is<jit::IonCompileTask>()) {
helper->as<jit::IonCompileTask>()->trace(trc);
}
}
JSRuntime* rt = trc->runtime();
if (auto* jitRuntime = rt->jitRuntime()) {
jit::IonCompileTask* task = jitRuntime->ionLazyLinkList(rt).getFirst();
while (task) {
task->trace(trc);
task = task->getNext();
}
}
for (auto& parseTask : parseWorklist_) {
parseTask->trace(trc);
}
for (auto parseTask : parseFinishedList_) {
parseTask->trace(trc);
}
for (auto& parseTask : parseWaitingOnGC_) {
parseTask->trace(trc);
}
}
// Definition of helper thread tasks.
//
// Priority is determined by the order they're listed here.
const HelperThread::Selector HelperThread::selectors[] = {
&GlobalHelperThreadState::maybeGetGCParallelTask,
&GlobalHelperThreadState::maybeGetIonCompileTask,
&GlobalHelperThreadState::maybeGetWasmTier1CompileTask,
&GlobalHelperThreadState::maybeGetPromiseHelperTask,
&GlobalHelperThreadState::maybeGetParseTask,
&GlobalHelperThreadState::maybeGetCompressionTask,
&GlobalHelperThreadState::maybeGetIonFreeTask,
&GlobalHelperThreadState::maybeGetWasmTier2CompileTask,
&GlobalHelperThreadState::maybeGetWasmTier2GeneratorTask};
bool GlobalHelperThreadState::hasQueuedTasks(
const AutoLockHelperThreadState& lock) {
return !gcParallelWorklist(lock).isEmpty() || !ionWorklist(lock).empty() ||
!wasmWorklist(lock, wasm::CompileMode::Tier1).empty() ||
!promiseHelperTasks(lock).empty() || !parseWorklist(lock).empty() ||
!compressionWorklist(lock).empty() || !ionFreeList(lock).empty() ||
!wasmWorklist(lock, wasm::CompileMode::Tier2).empty() ||
!wasmTier2GeneratorWorklist(lock).empty();
}
HelperThread::AutoProfilerLabel::AutoProfilerLabel(
HelperThread* helperThread, const char* label,
JS::ProfilingCategoryPair categoryPair)
: profilingStack(helperThread->profilingStack) {
if (profilingStack) {
profilingStack->pushLabelFrame(label, nullptr, this, categoryPair);
}
}
HelperThread::AutoProfilerLabel::~AutoProfilerLabel() {
if (profilingStack) {
profilingStack->pop();
}
}
void HelperThread::threadLoop() {
MOZ_ASSERT(CanUseExtraThreads());
AutoLockHelperThreadState lock;
while (!terminate) {
// The selectors may depend on the HelperThreadState not changing
// between task selection and task execution, in particular, on new
// tasks not being added (because of the lifo structure of the work
// lists). Unlocking the HelperThreadState between task selection and
// execution is not well-defined.
HelperThreadTask* task = findHighestPriorityTask(lock);
if (!task) {
AUTO_PROFILER_LABEL("HelperThread::threadLoop::wait", IDLE);
HelperThreadState().wait(lock, GlobalHelperThreadState::PRODUCER);
continue;
}
HelperThreadState().runTaskLocked(task, lock);
}
}
HelperThreadTask* HelperThread::findHighestPriorityTask(
const AutoLockHelperThreadState& locked) {
// Return the highest priority task that is ready to start, or nullptr.
for (const auto& selector : selectors) {
if (auto* task = (HelperThreadState().*(selector))(locked)) {
return task;
}
}
return nullptr;
}
void GlobalHelperThreadState::runTaskLocked(HelperThreadTask* task,
AutoLockHelperThreadState& locked) {
JS::AutoSuppressGCAnalysis nogc;
HelperThreadState().helperTasks(locked).infallibleEmplaceBack(task);
ThreadType threadType = task->threadType();
js::oom::SetThreadType(threadType);
runningTaskCount[threadType]++;
totalCountRunningTasks++;
task->runHelperThreadTask(locked);
// Delete task from helperTasks.
HelperThreadState().helperTasks(locked).eraseIfEqual(task);
totalCountRunningTasks--;
runningTaskCount[threadType]--;
js::oom::SetThreadType(js::THREAD_TYPE_NONE);
HelperThreadState().notifyAll(GlobalHelperThreadState::CONSUMER, locked);
}
|