Loading...
   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
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
/*
 * Copyright (c) 2006-2018 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 *
 */

#include <kern/sched_prim.h>
#include <kern/kalloc.h>
#include <kern/assert.h>
#include <kern/debug.h>
#include <kern/locks.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/host.h>
#include <kern/policy_internal.h>
#include <kern/thread_call.h>
#include <kern/thread_group.h>

#include <libkern/libkern.h>
#include <mach/coalition.h>
#include <mach/mach_time.h>
#include <mach/task.h>
#include <mach/host_priv.h>
#include <mach/mach_host.h>
#include <os/log.h>
#include <pexpert/pexpert.h>
#include <sys/coalition.h>
#include <sys/kern_event.h>
#include <sys/kdebug.h>
#include <sys/kdebug_kernel.h>
#include <sys/proc.h>
#include <sys/proc_info.h>
#include <sys/reason.h>
#include <sys/signal.h>
#include <sys/signalvar.h>
#include <sys/sysctl.h>
#include <sys/sysproto.h>
#include <sys/ubc.h> /* mach_to_bsd_errno */
#include <sys/wait.h>
#include <sys/tree.h>
#include <sys/priv.h>
#include <vm/vm_pageout.h>
#include <vm/vm_protos.h>
#include <vm/vm_page.h>
#include <vm/vm_compressor_xnu.h>
#include <vm/vm_compressor_backing_store_xnu.h>
#include <mach/machine/sdt.h>
#include <libkern/coreanalytics/coreanalytics.h>
#include <libkern/section_keywords.h>
#include <stdatomic.h>

#include <IOKit/IOBSD.h>

#if CONFIG_FREEZE
#include <vm/vm_map_xnu.h>
#endif /* CONFIG_FREEZE */

#include <kern/kern_memorystatus_internal.h>
#include <sys/kern_memorystatus.h>
#include <sys/kern_memorystatus_freeze.h>
#include <sys/kern_memorystatus_notify.h>
#include <sys/ubc.h>

unsigned int memorystatus_frozen_count = 0;
unsigned int memorystatus_frozen_count_webcontent = 0;
unsigned int memorystatus_frozen_count_xpc_service = 0;

#if CONFIG_FREEZE

static LCK_GRP_DECLARE(freezer_lck_grp, "freezer");
static LCK_MTX_DECLARE(freezer_mutex, &freezer_lck_grp);

/* Thresholds */
unsigned int memorystatus_freeze_threshold = 0;
unsigned int memorystatus_freeze_pages_min = 0;
unsigned int memorystatus_freeze_pages_max = 0;
unsigned int memorystatus_freeze_suspended_threshold = FREEZE_SUSPENDED_THRESHOLD_DEFAULT;
unsigned int memorystatus_freeze_daily_mb_max = FREEZE_DAILY_MB_MAX_DEFAULT;
uint64_t     memorystatus_freeze_budget_pages_remaining = 0; /* Remaining # of pages that can be frozen to disk */
uint64_t     memorystatus_freeze_budget_multiplier = 100; /* Multiplies the daily budget by 100/multiplier */
boolean_t memorystatus_freeze_degradation = FALSE; /* Protected by the freezer mutex. Signals we are in a degraded freeze mode. */
unsigned int memorystatus_freeze_max_candidate_band = FREEZE_MAX_CANDIDATE_BAND;

unsigned int memorystatus_max_frozen_demotions_daily = 0;
unsigned int memorystatus_thaw_count_demotion_threshold = 0;
unsigned int memorystatus_min_thaw_refreeze_threshold;

#if XNU_TARGET_OS_WATCH
#define FREEZE_DYNAMIC_THREAD_DELAY_ENABLED_DEFAULT true
#else
#define FREEZE_DYNAMIC_THREAD_DELAY_ENABLED_DEFAULT false
#endif
boolean_t memorystatus_freeze_dynamic_thread_delay_enabled = FREEZE_DYNAMIC_THREAD_DELAY_ENABLED_DEFAULT;
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_dynamic_thread_delay_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_dynamic_thread_delay_enabled, 0, "");

#define FREEZE_APPS_IDLE_DELAY_MULTIPLIER_FAST 1
#define FREEZE_APPS_IDLE_DELAY_MULTIPLIER_SLOW 30
#define FREEZE_APPS_IDLE_DELAY_MULTIPLIER_DEFAULT FREEZE_APPS_IDLE_DELAY_MULTIPLIER_FAST
unsigned int memorystatus_freeze_apps_idle_delay_multiplier = FREEZE_APPS_IDLE_DELAY_MULTIPLIER_DEFAULT;

#if (XNU_TARGET_OS_IOS && !XNU_TARGET_OS_XR) || XNU_TARGET_OS_WATCH
#define FREEZE_ENABLED_DEFAULT TRUE
#else
#define FREEZE_ENABLED_DEFAULT FALSE
#endif
boolean_t memorystatus_freeze_enabled = FREEZE_ENABLED_DEFAULT;

int memorystatus_freeze_wakeup = 0;

#define MAX_XPC_SERVICE_PIDS 10 /* Max. # of XPC services per coalition we'll consider freezing. */

unsigned int memorystatus_frozen_processes_max = 0;
unsigned int memorystatus_frozen_shared_mb = 0;
unsigned int memorystatus_frozen_shared_mb_max = 0;
unsigned int memorystatus_freeze_shared_mb_per_process_max = 0; /* Max. MB allowed per process to be freezer-eligible. */
#if XNU_TARGET_OS_WATCH
unsigned int memorystatus_freeze_private_shared_pages_ratio = 1; /* Ratio of private:shared pages for a process to be freezer-eligible. */
#else
unsigned int memorystatus_freeze_private_shared_pages_ratio = 2; /* Ratio of private:shared pages for a process to be freezer-eligible. */
#endif
unsigned int memorystatus_thaw_count = 0; /* # of thaws in the current freezer interval */
uint64_t memorystatus_thaw_count_since_boot = 0; /* The number of thaws since boot */
unsigned int memorystatus_refreeze_eligible_count = 0; /* # of processes currently thawed i.e. have state on disk & in-memory */

struct memorystatus_freezer_stats_t memorystatus_freezer_stats = {0};

static inline boolean_t memorystatus_can_freeze_processes(void);
static boolean_t memorystatus_can_freeze(boolean_t *memorystatus_freeze_swap_low);
static void memorystatus_freeze_thread(void *param __unused, wait_result_t wr __unused);
static uint32_t memorystatus_freeze_calculate_new_budget(
	unsigned int time_since_last_interval_expired_sec,
	unsigned int burst_multiple,
	unsigned int interval_duration_min,
	uint32_t rollover);
static void memorystatus_freeze_start_normal_throttle_interval(uint32_t new_budget, mach_timespec_t start_ts);

static void memorystatus_set_freeze_is_enabled(bool enabled);
static void memorystatus_disable_freeze(void);
static bool kill_all_frozen_processes(uint64_t max_band, bool suspended_only, os_reason_t jetsam_reason, uint64_t *memory_reclaimed_out);

/* Stats */
static uint64_t memorystatus_freeze_pageouts = 0;

/* Throttling */
#define DEGRADED_WINDOW_MINS    (30)
#define NORMAL_WINDOW_MINS      (24 * 60)

/* Protected by the freezer_mutex */
static throttle_interval_t throttle_intervals[] = {
	{ DEGRADED_WINDOW_MINS, 1, 0, 0, { 0, 0 }},
	{ NORMAL_WINDOW_MINS, 1, 0, 0, { 0, 0 }},
};
throttle_interval_t *degraded_throttle_window = &throttle_intervals[0];
throttle_interval_t *normal_throttle_window = &throttle_intervals[1];
uint32_t memorystatus_freeze_current_interval = 0;
static thread_call_t freeze_interval_reset_thread_call;
static uint32_t memorystatus_freeze_calculate_new_budget(
	unsigned int time_since_last_interval_expired_sec,
	unsigned int burst_multiple,
	unsigned int interval_duration_min,
	uint32_t rollover);

struct memorystatus_freezer_candidate_list memorystatus_global_freeze_list = {NULL, 0};
struct memorystatus_freezer_candidate_list memorystatus_global_demote_list = {NULL, 0};
/*
 * When enabled, freeze candidates are chosen from the memorystatus_global_freeze_list
 * in order (as opposed to using the older LRU approach).
 */
#if XNU_TARGET_OS_WATCH
#define FREEZER_USE_ORDERED_LIST_DEFAULT 1
#else
#define FREEZER_USE_ORDERED_LIST_DEFAULT 0
#endif
int memorystatus_freezer_use_ordered_list = FREEZER_USE_ORDERED_LIST_DEFAULT;
EXPERIMENT_FACTOR_UINT(_kern, memorystatus_freezer_use_ordered_list, &memorystatus_freezer_use_ordered_list, 0, 1, "");
/*
 * When enabled, demotion candidates are chosen from memorystatus_global_demotion_list
 */
int memorystatus_freezer_use_demotion_list = 0;
EXPERIMENT_FACTOR_UINT(_kern, memorystatus_freezer_use_demotion_list, &memorystatus_freezer_use_demotion_list, 0, 1, "");

extern boolean_t vm_swap_max_budget(uint64_t *);

static void memorystatus_freeze_update_throttle(uint64_t *budget_pages_allowed);
static void memorystatus_demote_frozen_processes(bool urgent_mode);

static void memorystatus_freeze_handle_error(proc_t p, const freezer_error_code_t freezer_error_code, bool was_refreeze, pid_t pid, const coalition_t coalition, const char* log_prefix);
static void memorystatus_freeze_out_of_slots(void);
uint64_t memorystatus_freezer_thread_next_run_ts = 0;

/* Sysctls needed for aggd stats */

SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_frozen_count, 0, "");
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_count_webcontent, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_frozen_count_webcontent, 0, "");
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_count_xpc_service, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_frozen_count_xpc_service, 0, "");
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_thaw_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_thaw_count, 0, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_thaw_count_since_boot, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_thaw_count_since_boot, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freeze_pageouts, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freeze_pageouts, "");
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_interval, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freeze_current_interval, 0, "");

/*
 * Force a new interval with the given budget (no rollover).
 */
static void
memorystatus_freeze_force_new_interval(uint64_t new_budget)
{
	LCK_MTX_ASSERT(&freezer_mutex, LCK_MTX_ASSERT_OWNED);
	mach_timespec_t now_ts;
	clock_sec_t sec;
	clock_nsec_t nsec;

	clock_get_system_nanotime(&sec, &nsec);
	now_ts.tv_sec = (unsigned int)(MIN(sec, UINT32_MAX));
	now_ts.tv_nsec = nsec;
	memorystatus_freeze_start_normal_throttle_interval((uint32_t) MIN(new_budget, UINT32_MAX), now_ts);
	/* Don't carry over any excess pageouts since we're forcing a new budget */
	normal_throttle_window->pageouts = 0;
	memorystatus_freeze_budget_pages_remaining = normal_throttle_window->max_pageouts;
}
#if DEVELOPMENT || DEBUG
static int sysctl_memorystatus_freeze_budget_pages_remaining SYSCTL_HANDLER_ARGS
{
	#pragma unused(arg1, arg2, oidp)
	int error, changed;
	uint64_t new_budget = memorystatus_freeze_budget_pages_remaining;

	lck_mtx_lock(&freezer_mutex);

	error = sysctl_io_number(req, memorystatus_freeze_budget_pages_remaining, sizeof(uint64_t), &new_budget, &changed);
	if (changed) {
		if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
			lck_mtx_unlock(&freezer_mutex);
			return ENOTSUP;
		}
		memorystatus_freeze_force_new_interval(new_budget);
	}

	lck_mtx_unlock(&freezer_mutex);
	return error;
}

SYSCTL_PROC(_kern, OID_AUTO, memorystatus_freeze_budget_pages_remaining, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, &sysctl_memorystatus_freeze_budget_pages_remaining, "Q", "");
#else /* DEVELOPMENT || DEBUG */
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freeze_budget_pages_remaining, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freeze_budget_pages_remaining, "");
#endif /* DEVELOPMENT || DEBUG */
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_error_excess_shared_memory_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_error_excess_shared_memory_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_error_low_private_shared_ratio_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_error_low_private_shared_ratio_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_error_no_compressor_space_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_error_no_compressor_space_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_error_no_swap_space_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_error_no_swap_space_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_error_below_min_pages_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_error_below_min_pages_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_error_low_probability_of_use_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_error_low_probability_of_use_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_error_elevated_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_error_elevated_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_error_other_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_error_other_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_process_considered_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_process_considered_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_below_threshold_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_below_threshold_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_skipped_full_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_skipped_full_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_skipped_shared_mb_high_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_skipped_shared_mb_high_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_shared_pages_skipped, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_shared_pages_skipped, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_bytes_refrozen, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_bytes_refrozen, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_refreeze_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_refreeze_count, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_freeze_pid_mismatches, CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_freeze_pid_mismatches, "");
SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freezer_demote_pid_mismatches, CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freezer_stats.mfs_demote_pid_mismatches, "");

static_assert(_kMemorystatusFreezeSkipReasonMax <= UINT8_MAX);

/*
 * Calculates the hit rate for the freezer.
 * The hit rate is defined as the percentage of procs that are currently in the
 * freezer which we have thawed.
 * A low hit rate means we're freezing bad candidates since they're not re-used.
 */
static int
calculate_thaw_percentage(uint64_t frozen_count, uint64_t thaw_count)
{
	int thaw_percentage = 100;

	if (frozen_count > 0) {
		if (thaw_count > frozen_count) {
			/*
			 * Both counts are using relaxed atomics & could be out of sync
			 * causing us to see thaw_percentage > 100.
			 */
			thaw_percentage = 100;
		} else {
			thaw_percentage = (int)(100 * thaw_count / frozen_count);
		}
	}
	return thaw_percentage;
}

static int
get_thaw_percentage()
{
	uint64_t processes_frozen, processes_thawed;
	processes_frozen = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_frozen, relaxed);
	processes_thawed = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_thawed, relaxed);
	return calculate_thaw_percentage(processes_frozen, processes_thawed);
}

static int
sysctl_memorystatus_freezer_thaw_percentage SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int thaw_percentage = get_thaw_percentage();
	return sysctl_handle_int(oidp, &thaw_percentage, 0, req);
}
SYSCTL_PROC(_kern, OID_AUTO, memorystatus_freezer_thaw_percentage, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, &sysctl_memorystatus_freezer_thaw_percentage, "I", "");

static int
get_thaw_percentage_fg()
{
	uint64_t processes_frozen, processes_thawed_fg;
	processes_frozen = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_frozen, relaxed);
	processes_thawed_fg = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_thawed_fg, relaxed);
	return calculate_thaw_percentage(processes_frozen, processes_thawed_fg);
}

static int sysctl_memorystatus_freezer_thaw_percentage_fg SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int thaw_percentage = get_thaw_percentage_fg();
	return sysctl_handle_int(oidp, &thaw_percentage, 0, req);
}
SYSCTL_PROC(_kern, OID_AUTO, memorystatus_freezer_thaw_percentage_fg, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, &sysctl_memorystatus_freezer_thaw_percentage_fg, "I", "");

static int
get_thaw_percentage_webcontent()
{
	uint64_t processes_frozen_webcontent, processes_thawed_webcontent;
	processes_frozen_webcontent = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_frozen_webcontent, relaxed);
	processes_thawed_webcontent = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_thawed_webcontent, relaxed);
	return calculate_thaw_percentage(processes_frozen_webcontent, processes_thawed_webcontent);
}

static int sysctl_memorystatus_freezer_thaw_percentage_webcontent SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int thaw_percentage = get_thaw_percentage_webcontent();
	return sysctl_handle_int(oidp, &thaw_percentage, 0, req);
}
SYSCTL_PROC(_kern, OID_AUTO, memorystatus_freezer_thaw_percentage_webcontent, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, &sysctl_memorystatus_freezer_thaw_percentage_webcontent, "I", "");


static int
get_thaw_percentage_bg()
{
	uint64_t processes_frozen, processes_thawed_fg, processes_thawed;
	processes_frozen = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_frozen, relaxed);
	processes_thawed = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_thawed, relaxed);
	processes_thawed_fg = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_thawed_fg, relaxed);
	return calculate_thaw_percentage(processes_frozen, processes_thawed - processes_thawed_fg);
}

static int sysctl_memorystatus_freezer_thaw_percentage_bg SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int thaw_percentage = get_thaw_percentage_bg();
	return sysctl_handle_int(oidp, &thaw_percentage, 0, req);
}
SYSCTL_PROC(_kern, OID_AUTO, memorystatus_freezer_thaw_percentage_bg, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, &sysctl_memorystatus_freezer_thaw_percentage_bg, "I", "");

static int
get_thaw_percentage_fg_non_xpc_service()
{
	uint64_t processes_frozen, processes_frozen_xpc_service, processes_thawed_fg, processes_thawed_fg_xpc_service;
	processes_frozen = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_frozen, relaxed);
	processes_frozen_xpc_service = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_frozen_xpc_service, relaxed);
	processes_thawed_fg = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_thawed_fg, relaxed);
	processes_thawed_fg_xpc_service = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_thawed_fg_xpc_service, relaxed);
	/*
	 * Since these are all relaxed loads, it's possible (although unlikely) to read a value for
	 * frozen/thawed xpc services that's > the value for processes frozen / thawed.
	 * Clamp just in case.
	 */
	processes_frozen_xpc_service = MIN(processes_frozen_xpc_service, processes_frozen);
	processes_thawed_fg_xpc_service = MIN(processes_thawed_fg_xpc_service, processes_thawed_fg);
	return calculate_thaw_percentage(processes_frozen - processes_frozen_xpc_service, processes_thawed_fg - processes_thawed_fg_xpc_service);
}

static int sysctl_memorystatus_freezer_thaw_percentage_fg_non_xpc_service SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int thaw_percentage = get_thaw_percentage_fg_non_xpc_service();
	return sysctl_handle_int(oidp, &thaw_percentage, 0, req);
}

SYSCTL_PROC(_kern, OID_AUTO, memorystatus_freezer_thaw_percentage_fg_non_xpc_service, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, &sysctl_memorystatus_freezer_thaw_percentage_fg_non_xpc_service, "I", "");

#define FREEZER_ERROR_STRING_LENGTH 128

EXPERIMENT_FACTOR_UINT(_kern, memorystatus_freeze_pages_min, &memorystatus_freeze_pages_min, 0, UINT32_MAX, "");
EXPERIMENT_FACTOR_UINT(_kern, memorystatus_freeze_pages_max, &memorystatus_freeze_pages_max, 0, UINT32_MAX, "");
EXPERIMENT_FACTOR_UINT(_kern, memorystatus_freeze_processes_max, &memorystatus_frozen_processes_max, 0, UINT32_MAX, "");
EXPERIMENT_FACTOR_UINT(_kern, memorystatus_freeze_jetsam_band, &memorystatus_freeze_jetsam_band, JETSAM_PRIORITY_BACKGROUND, JETSAM_PRIORITY_FOREGROUND, "");
EXPERIMENT_FACTOR_UINT(_kern, memorystatus_freeze_private_shared_pages_ratio, &memorystatus_freeze_private_shared_pages_ratio, 0, UINT32_MAX, "");
EXPERIMENT_FACTOR_UINT(_kern, memorystatus_freeze_min_processes, &memorystatus_freeze_suspended_threshold, 0, UINT32_MAX, "");
EXPERIMENT_FACTOR_UINT(_kern, memorystatus_freeze_max_candidate_band, &memorystatus_freeze_max_candidate_band, JETSAM_PRIORITY_IDLE, JETSAM_PRIORITY_FOREGROUND, "");
static int
sysctl_memorystatus_freeze_budget_multiplier SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2, oidp, req)
	int error = 0, changed = 0;
	uint64_t val = memorystatus_freeze_budget_multiplier;
	unsigned int new_budget;
	clock_sec_t sec;
	clock_nsec_t nsec;
	mach_timespec_t now_ts;

	error = sysctl_io_number(req, memorystatus_freeze_budget_multiplier, sizeof(val), &val, &changed);
	if (error) {
		return error;
	}
	if (changed) {
		if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
			return ENOTSUP;
		}
#if !(DEVELOPMENT || DEBUG)
		if (val > 100) {
			/* Can not increase budget on release. */
			return EINVAL;
		}
#endif
		lck_mtx_lock(&freezer_mutex);

		memorystatus_freeze_budget_multiplier = val;
		/* Start a new throttle interval with this budget multiplier */
		new_budget = memorystatus_freeze_calculate_new_budget(0, 1, NORMAL_WINDOW_MINS, 0);
		clock_get_system_nanotime(&sec, &nsec);
		now_ts.tv_sec = (unsigned int)(MIN(sec, UINT32_MAX));
		now_ts.tv_nsec = nsec;
		memorystatus_freeze_start_normal_throttle_interval(new_budget, now_ts);
		memorystatus_freeze_budget_pages_remaining = normal_throttle_window->max_pageouts;

		lck_mtx_unlock(&freezer_mutex);
	}
	return 0;
}
EXPERIMENT_FACTOR_PROC(_kern, memorystatus_freeze_budget_multiplier, CTLTYPE_QUAD | CTLFLAG_RW, 0, 0, &sysctl_memorystatus_freeze_budget_multiplier, "Q", "");
/*
 * max. # of frozen process demotions we will allow in our daily cycle.
 */
EXPERIMENT_FACTOR_UINT(_kern, memorystatus_max_freeze_demotions_daily, &memorystatus_max_frozen_demotions_daily, 0, UINT32_MAX, "");

/*
 * min # of thaws needed by a process to protect it from getting demoted into the IDLE band.
 */
EXPERIMENT_FACTOR_UINT(_kern, memorystatus_thaw_count_demotion_threshold, &memorystatus_thaw_count_demotion_threshold, 0, UINT32_MAX, "");

/*
 * min # of global thaws needed for us to consider refreezing these processes.
 */
EXPERIMENT_FACTOR_UINT(_kern, memorystatus_min_thaw_refreeze_threshold, &memorystatus_min_thaw_refreeze_threshold, 0, UINT32_MAX, "");

#if DEVELOPMENT || DEBUG

SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_daily_mb_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_daily_mb_max, 0, "");
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_degraded_mode, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freeze_degradation, 0, "");
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_threshold, 0, "");
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_refreeze_eligible_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_refreeze_eligible_count, 0, "");

/*
 * Max. shared-anonymous memory in MB that can be held by frozen processes in the high jetsam band.
 * "0" means no limit.
 * Default is 10% of system-wide task limit.
 */

SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_shared_mb_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_frozen_shared_mb_max, 0, "");
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_shared_mb, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_frozen_shared_mb, 0, "");

SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_shared_mb_per_process_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_shared_mb_per_process_max, 0, "");

boolean_t memorystatus_freeze_throttle_enabled = TRUE;
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_throttle_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_throttle_enabled, 0, "");

/*
 * When set to true, this keeps frozen processes in the compressor pool in memory, instead of swapping them out to disk.
 * Exposed via the sysctl kern.memorystatus_freeze_to_memory.
 */
boolean_t memorystatus_freeze_to_memory = FALSE;
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_to_memory, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_to_memory, 0, "");

#define VM_PAGES_FOR_ALL_PROCS    (2)

/*
 * Manual trigger of freeze and thaw for dev / debug kernels only.
 */
static int
sysctl_memorystatus_freeze SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int error, pid = 0;
	proc_t p;
	freezer_error_code_t freezer_error_code = 0;
	pid_t pid_list[MAX_XPC_SERVICE_PIDS];
	int ntasks = 0;
	coalition_t coal = COALITION_NULL;

	error = sysctl_handle_int(oidp, &pid, 0, req);
	if (error || !req->newptr) {
		return error;
	}

	if (pid == VM_PAGES_FOR_ALL_PROCS) {
		error = mach_to_bsd_errno(vm_pageout_anonymous_pages());
		return error;
	}

	lck_mtx_lock(&freezer_mutex);
	if (memorystatus_freeze_enabled == false) {
		lck_mtx_unlock(&freezer_mutex);
		memorystatus_log("sysctl_freeze: Freeze is DISABLED\n");
		return ENOTSUP;
	}

again:
	p = proc_find(pid);
	if (p != NULL) {
		memorystatus_freezer_stats.mfs_process_considered_count++;
		uint32_t purgeable, wired, clean, dirty, shared;
		uint32_t max_pages = 0, state = 0;

		if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
			/*
			 * Freezer backed by the compressor and swap file(s)
			 * will hold compressed data.
			 *
			 * Set the sysctl kern.memorystatus_freeze_to_memory to true to keep compressed data from
			 * being swapped out to disk. Note that this disables freezer swap support globally,
			 * not just for the process being frozen.
			 *
			 *
			 * We don't care about the global freezer budget or the process's (min/max) budget here.
			 * The freeze sysctl is meant to force-freeze a process.
			 *
			 * We also don't update any global or process stats on this path, so that the jetsam/ freeze
			 * logic remains unaffected. The tasks we're performing here are: freeze the process, set the
			 * P_MEMSTAT_FROZEN bit, and elevate the process to a higher band (if the freezer is active).
			 */
			max_pages = memorystatus_freeze_pages_max;
		} else {
			/*
			 * We only have the compressor without any swap.
			 */
			max_pages = UINT32_MAX - 1;
		}

		proc_list_lock();
		state = p->p_memstat_state;
		proc_list_unlock();

		/*
		 * The jetsam path also verifies that the process is a suspended App. We don't care about that here.
		 * We simply ensure that jetsam is not already working on the process and that the process has not
		 * explicitly disabled freezing.
		 */
		if (state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FREEZE_DISABLED)) {
			memorystatus_log_error("sysctl_freeze: p_memstat_state check failed, process is%s%s%s\n",
			    (state & P_MEMSTAT_TERMINATED) ? " terminated" : "",
			    (state & P_MEMSTAT_LOCKED) ? " locked" : "",
			    (state & P_MEMSTAT_FREEZE_DISABLED) ? " unfreezable" : "");

			proc_rele(p);
			lck_mtx_unlock(&freezer_mutex);
			return EPERM;
		}

		KDBG(MEMSTAT_CODE(BSD_MEMSTAT_FREEZE) | DBG_FUNC_START, memorystatus_available_pages, pid, max_pages);
		error = task_freeze(proc_task(p), &purgeable, &wired, &clean, &dirty, max_pages, &shared, &freezer_error_code, FALSE /* eval only */);
		if (!error || freezer_error_code == FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO) {
			memorystatus_freezer_stats.mfs_shared_pages_skipped += shared;
		}
		KDBG(MEMSTAT_CODE(BSD_MEMSTAT_FREEZE) | DBG_FUNC_END, purgeable, wired, clean, dirty);

		if (error) {
			memorystatus_freeze_handle_error(p, freezer_error_code, state & P_MEMSTAT_FROZEN, pid, coal, "sysctl_freeze");
			if (error == KERN_NO_SPACE) {
				/* Make it easy to distinguish between failures due to low compressor/ swap space and other failures. */
				error = ENOSPC;
			} else {
				error = EIO;
			}
		} else {
			proc_list_lock();
			if (!_memstat_proc_is_frozen(p)) {
				p->p_memstat_state |= P_MEMSTAT_FROZEN;
				p->p_memstat_freeze_skip_reason = kMemorystatusFreezeSkipReasonNone;
				memorystatus_frozen_count++;
				os_atomic_inc(&memorystatus_freezer_stats.mfs_processes_frozen, relaxed);
				if (strcmp(p->p_name, "com.apple.WebKit.WebContent") == 0) {
					memorystatus_frozen_count_webcontent++;
					os_atomic_inc(&(memorystatus_freezer_stats.mfs_processes_frozen_webcontent), relaxed);
				}
				if (memorystatus_frozen_count == memorystatus_frozen_processes_max) {
					memorystatus_freeze_out_of_slots();
				}
			} else {
				// This was a re-freeze
				if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
					memorystatus_freezer_stats.mfs_bytes_refrozen += dirty * PAGE_SIZE;
					memorystatus_freezer_stats.mfs_refreeze_count++;
				}
			}
			p->p_memstat_frozen_count++;

			if (coal != NULL) {
				/* We just froze an xpc service. Mark it as such for telemetry */
				p->p_memstat_state |= P_MEMSTAT_FROZEN_XPC_SERVICE;
				memorystatus_frozen_count_xpc_service++;
				os_atomic_inc(&(memorystatus_freezer_stats.mfs_processes_frozen_xpc_service), relaxed);
			}


			proc_list_unlock();

			if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
				/*
				 * We elevate only if we are going to swap out the data.
				 */
				error = memorystatus_update_inactive_jetsam_priority_band(pid, MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE,
				    memorystatus_freeze_jetsam_band, TRUE);

				if (error) {
					memorystatus_log_error("sysctl_freeze: Elevating frozen process to higher jetsam band failed with %d\n", error);
				}
			}
		}

		if ((error == 0) && (coal == NULL)) {
			/*
			 * We froze a process and so we check to see if it was
			 * a coalition leader and if it has XPC services that
			 * might need freezing.
			 * Only one leader can be frozen at a time and so we shouldn't
			 * enter this block more than once per call. Hence the
			 * check that 'coal' has to be NULL. We should make this an
			 * assert() or panic() once we have a much more concrete way
			 * to detect an app vs a daemon.
			 */

			task_t          curr_task = NULL;

			curr_task = proc_task(p);
			coal = task_get_coalition(curr_task, COALITION_TYPE_JETSAM);
			if (coalition_is_leader(curr_task, coal)) {
				ntasks = coalition_get_pid_list(coal, COALITION_ROLEMASK_XPC,
				    COALITION_SORT_DEFAULT, pid_list, MAX_XPC_SERVICE_PIDS);

				if (ntasks > MAX_XPC_SERVICE_PIDS) {
					ntasks = MAX_XPC_SERVICE_PIDS;
				}
			}
		}

		proc_rele(p);

		while (ntasks) {
			pid = pid_list[--ntasks];
			goto again;
		}

		lck_mtx_unlock(&freezer_mutex);
		return error;
	} else {
		memorystatus_log_error("sysctl_freeze: Invalid process\n");
	}


	lck_mtx_unlock(&freezer_mutex);
	return EINVAL;
}

SYSCTL_PROC(_kern, OID_AUTO, memorystatus_freeze, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED,
    0, 0, &sysctl_memorystatus_freeze, "I", "");

/*
 * Manual trigger of agressive frozen demotion for dev / debug kernels only.
 */
static int
sysctl_memorystatus_demote_frozen_process SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int error, val;
	/*
	 * Only demote on write to prevent demoting during `sysctl -a`.
	 * The actual value written doesn't matter.
	 */
	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error || !req->newptr) {
		return error;
	}
	if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		return ENOTSUP;
	}
	lck_mtx_lock(&freezer_mutex);
	memorystatus_demote_frozen_processes(false);
	lck_mtx_unlock(&freezer_mutex);
	return 0;
}

SYSCTL_PROC(_kern, OID_AUTO, memorystatus_demote_frozen_processes, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, &sysctl_memorystatus_demote_frozen_process, "I", "");

static int
sysctl_memorystatus_available_pages_thaw SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)

	int error, pid = 0;
	proc_t p;

	if (memorystatus_freeze_enabled == false) {
		return ENOTSUP;
	}

	error = sysctl_handle_int(oidp, &pid, 0, req);
	if (error || !req->newptr) {
		return error;
	}

	if (pid == VM_PAGES_FOR_ALL_PROCS) {
		do_fastwake_warmup_all();
		return 0;
	} else {
		p = proc_find(pid);
		if (p != NULL) {
			error = task_thaw(proc_task(p));

			if (error) {
				error = EIO;
			} else {
				/*
				 * task_thaw() succeeded.
				 *
				 * We increment memorystatus_frozen_count on the sysctl freeze path.
				 * And so we need the P_MEMSTAT_FROZEN to decrement the frozen count
				 * when this process exits.
				 *
				 * proc_list_lock();
				 * p->p_memstat_state &= ~P_MEMSTAT_FROZEN;
				 * proc_list_unlock();
				 */
			}
			proc_rele(p);
			return error;
		}
	}

	return EINVAL;
}

SYSCTL_PROC(_kern, OID_AUTO, memorystatus_thaw, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED,
    0, 0, &sysctl_memorystatus_available_pages_thaw, "I", "");


typedef struct _global_freezable_status {
	boolean_t       freeze_pages_threshold_crossed;
	boolean_t       freeze_eligible_procs_available;
	boolean_t       freeze_scheduled_in_future;
}global_freezable_status_t;

typedef struct _proc_freezable_status {
	boolean_t    freeze_has_memstat_state;
	boolean_t    freeze_has_pages_min;
	int        freeze_has_probability;
	int        freeze_leader_eligible;
	boolean_t    freeze_attempted;
	uint32_t    p_memstat_state;
	uint32_t    p_pages;
	int        p_freeze_error_code;
	int        p_pid;
	int        p_leader_pid;
	char        p_name[MAXCOMLEN + 1];
}proc_freezable_status_t;

#define MAX_FREEZABLE_PROCESSES 200 /* Total # of processes in band 0 that we evaluate for freezability */

/*
 * For coalition based freezing evaluations, we proceed as follows:
 *  - detect that the process is a coalition member and a XPC service
 *  - mark its 'freeze_leader_eligible' field with FREEZE_PROC_LEADER_FREEZABLE_UNKNOWN
 *  - continue its freezability evaluation assuming its leader will be freezable too
 *
 * Once we are done evaluating all processes, we do a quick run thru all
 * processes and for a coalition member XPC service we look up the 'freezable'
 * status of its leader and iff:
 *  - the xpc service is freezable i.e. its individual freeze evaluation worked
 *  - and, its leader is also marked freezable
 * we update its 'freeze_leader_eligible' to FREEZE_PROC_LEADER_FREEZABLE_SUCCESS.
 */

#define FREEZE_PROC_LEADER_FREEZABLE_UNKNOWN   (-1)
#define FREEZE_PROC_LEADER_FREEZABLE_SUCCESS    (1)
#define FREEZE_PROC_LEADER_FREEZABLE_FAILURE    (2)

static int
memorystatus_freezer_get_status(user_addr_t buffer, size_t buffer_size, int32_t *retval)
{
	uint32_t            proc_count = 0, freeze_eligible_proc_considered = 0, band = 0, xpc_index = 0, leader_index = 0;
	global_freezable_status_t    *list_head;
	proc_freezable_status_t     *list_entry, *list_entry_start;
	size_t                list_size = 0, entry_count = 0;
	proc_t                p, leader_proc;
	memstat_bucket_t        *bucket;
	uint32_t            state = 0, pages = 0;
	boolean_t            try_freeze = TRUE, xpc_skip_size_probability_check = FALSE;
	int                error = 0, probability_of_use = 0;
	pid_t              leader_pid = 0;
	struct memorystatus_freeze_list_iterator iterator;

	if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE == FALSE) {
		return ENOTSUP;
	}

	bzero(&iterator, sizeof(struct memorystatus_freeze_list_iterator));

	list_size = sizeof(global_freezable_status_t) + (sizeof(proc_freezable_status_t) * MAX_FREEZABLE_PROCESSES);

	if (buffer_size < list_size) {
		return EINVAL;
	}

	list_head = (global_freezable_status_t *)kalloc_data(list_size, Z_WAITOK | Z_ZERO);
	if (list_head == NULL) {
		return ENOMEM;
	}

	list_size = sizeof(global_freezable_status_t);

	lck_mtx_lock(&freezer_mutex);
	proc_list_lock();

	uint64_t curr_time = mach_absolute_time();

	list_head->freeze_pages_threshold_crossed = (memorystatus_available_pages < memorystatus_freeze_threshold);
	if (memorystatus_freezer_use_ordered_list) {
		list_head->freeze_eligible_procs_available = memorystatus_frozen_count < memorystatus_global_freeze_list.mfcl_length;
	} else {
		list_head->freeze_eligible_procs_available = ((memorystatus_suspended_count - memorystatus_frozen_count) > memorystatus_freeze_suspended_threshold);
	}
	list_head->freeze_scheduled_in_future = (curr_time < memorystatus_freezer_thread_next_run_ts);

	list_entry_start = (proc_freezable_status_t*) ((uintptr_t)list_head + sizeof(global_freezable_status_t));
	list_entry = list_entry_start;

	bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE];

	entry_count = (memorystatus_global_probabilities_size / sizeof(memorystatus_internal_probabilities_t));

	if (memorystatus_freezer_use_ordered_list) {
		while (iterator.global_freeze_list_index < memorystatus_global_freeze_list.mfcl_length) {
			p = memorystatus_freezer_candidate_list_get_proc(
				&memorystatus_global_freeze_list,
				(iterator.global_freeze_list_index)++,
				NULL);
			if (p != PROC_NULL) {
				break;
			}
		}
	} else {
		p = memorystatus_get_first_proc_locked(&band, FALSE);
	}

	proc_count++;

	while ((proc_count <= MAX_FREEZABLE_PROCESSES) &&
	    (p) &&
	    (list_size < buffer_size)) {
		if (isSysProc(p)) {
			/*
			 * Daemon:- We will consider freezing it iff:
			 * - it belongs to a coalition and the leader is freeze-eligible (delayed evaluation)
			 * - its role in the coalition is XPC service.
			 *
			 * We skip memory size requirements in this case.
			 */

			coalition_t     coal = COALITION_NULL;
			task_t          leader_task = NULL, curr_task = NULL;
			int             task_role_in_coalition = 0;

			curr_task = proc_task(p);
			coal = task_get_coalition(curr_task, COALITION_TYPE_JETSAM);

			if (coal == COALITION_NULL || coalition_is_leader(curr_task, coal)) {
				/*
				 * By default, XPC services without an app
				 * will be the leader of their own single-member
				 * coalition.
				 */
				goto skip_ineligible_xpc;
			}

			leader_task = coalition_get_leader(coal);
			if (leader_task == TASK_NULL) {
				/*
				 * This jetsam coalition is currently leader-less.
				 * This could happen if the app died, but XPC services
				 * have not yet exited.
				 */
				goto skip_ineligible_xpc;
			}

			leader_proc = (proc_t)get_bsdtask_info(leader_task);
			task_deallocate(leader_task);

			if (leader_proc == PROC_NULL) {
				/* leader task is exiting */
				goto skip_ineligible_xpc;
			}

			task_role_in_coalition = task_coalition_role_for_type(curr_task, COALITION_TYPE_JETSAM);

			if (task_role_in_coalition == COALITION_TASKROLE_XPC) {
				xpc_skip_size_probability_check = TRUE;
				leader_pid = proc_getpid(leader_proc);
				goto continue_eval;
			}

skip_ineligible_xpc:
			p = memorystatus_get_next_proc_locked(&band, p, FALSE);
			proc_count++;
			continue;
		}

continue_eval:
		strlcpy(list_entry->p_name, p->p_name, MAXCOMLEN + 1);

		list_entry->p_pid = proc_getpid(p);

		state = p->p_memstat_state;

		if ((state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FREEZE_DISABLED | P_MEMSTAT_FREEZE_IGNORE)) ||
		    !(state & P_MEMSTAT_SUSPENDED)) {
			try_freeze = list_entry->freeze_has_memstat_state = FALSE;
		} else {
			try_freeze = list_entry->freeze_has_memstat_state = TRUE;
		}

		list_entry->p_memstat_state = state;

		if (xpc_skip_size_probability_check == TRUE) {
			/*
			 * Assuming the coalition leader is freezable
			 * we don't care re. minimum pages and probability
			 * as long as the process isn't marked P_MEMSTAT_FREEZE_DISABLED.
			 * XPC services have to be explicity opted-out of the disabled
			 * state. And we checked that state above.
			 */
			list_entry->freeze_has_pages_min = TRUE;
			list_entry->p_pages = -1;
			list_entry->freeze_has_probability = -1;

			list_entry->freeze_leader_eligible = FREEZE_PROC_LEADER_FREEZABLE_UNKNOWN;
			list_entry->p_leader_pid = leader_pid;

			xpc_skip_size_probability_check = FALSE;
		} else {
			list_entry->freeze_leader_eligible = FREEZE_PROC_LEADER_FREEZABLE_SUCCESS; /* Apps are freeze eligible and their own leaders. */
			list_entry->p_leader_pid = 0; /* Setting this to 0 signifies this isn't a coalition driven freeze. */

			memorystatus_get_task_page_counts(proc_task(p), &pages, NULL, NULL);
			if (pages < memorystatus_freeze_pages_min) {
				try_freeze = list_entry->freeze_has_pages_min = FALSE;
			} else {
				list_entry->freeze_has_pages_min = TRUE;
			}

			list_entry->p_pages = pages;

			if (entry_count) {
				uint32_t j = 0;
				for (j = 0; j < entry_count; j++) {
					if (strncmp(memorystatus_global_probabilities_table[j].proc_name,
					    p->p_name,
					    MAXCOMLEN) == 0) {
						probability_of_use = memorystatus_global_probabilities_table[j].use_probability;
						break;
					}
				}

				list_entry->freeze_has_probability = probability_of_use;

				try_freeze = ((probability_of_use > 0) && try_freeze);
			} else {
				list_entry->freeze_has_probability = -1;
			}
		}

		if (try_freeze) {
			uint32_t purgeable, wired, clean, dirty, shared;
			uint32_t max_pages = 0;
			int freezer_error_code = 0;

			error = task_freeze(proc_task(p), &purgeable, &wired, &clean, &dirty, max_pages, &shared, &freezer_error_code, TRUE /* eval only */);

			if (error) {
				list_entry->p_freeze_error_code = freezer_error_code;
			}

			list_entry->freeze_attempted = TRUE;
		}

		list_entry++;
		freeze_eligible_proc_considered++;

		list_size += sizeof(proc_freezable_status_t);

		if (memorystatus_freezer_use_ordered_list) {
			p = PROC_NULL;
			while (iterator.global_freeze_list_index < memorystatus_global_freeze_list.mfcl_length) {
				p = memorystatus_freezer_candidate_list_get_proc(
					&memorystatus_global_freeze_list,
					(iterator.global_freeze_list_index)++,
					NULL);
				if (p != PROC_NULL) {
					break;
				}
			}
		} else {
			p = memorystatus_get_next_proc_locked(&band, p, FALSE);
		}

		proc_count++;
	}

	proc_list_unlock();
	lck_mtx_unlock(&freezer_mutex);

	list_entry = list_entry_start;

	for (xpc_index = 0; xpc_index < freeze_eligible_proc_considered; xpc_index++) {
		if (list_entry[xpc_index].freeze_leader_eligible == FREEZE_PROC_LEADER_FREEZABLE_UNKNOWN) {
			leader_pid = list_entry[xpc_index].p_leader_pid;

			leader_proc = proc_find(leader_pid);

			if (leader_proc) {
				if (_memstat_proc_is_frozen(leader_proc)) {
					/*
					 * Leader has already been frozen.
					 */
					list_entry[xpc_index].freeze_leader_eligible = FREEZE_PROC_LEADER_FREEZABLE_SUCCESS;
					proc_rele(leader_proc);
					continue;
				}
				proc_rele(leader_proc);
			}

			for (leader_index = 0; leader_index < freeze_eligible_proc_considered; leader_index++) {
				if (list_entry[leader_index].p_pid == leader_pid) {
					if (list_entry[leader_index].freeze_attempted && list_entry[leader_index].p_freeze_error_code == 0) {
						list_entry[xpc_index].freeze_leader_eligible = FREEZE_PROC_LEADER_FREEZABLE_SUCCESS;
					} else {
						list_entry[xpc_index].freeze_leader_eligible = FREEZE_PROC_LEADER_FREEZABLE_FAILURE;
						list_entry[xpc_index].p_freeze_error_code = FREEZER_ERROR_GENERIC;
					}
					break;
				}
			}

			/*
			 * Didn't find the leader entry. This might be likely because
			 * the leader never made it down to band 0.
			 */
			if (leader_index == freeze_eligible_proc_considered) {
				list_entry[xpc_index].freeze_leader_eligible = FREEZE_PROC_LEADER_FREEZABLE_FAILURE;
				list_entry[xpc_index].p_freeze_error_code = FREEZER_ERROR_GENERIC;
			}
		}
	}

	buffer_size = MIN(list_size, INT32_MAX);

	error = copyout(list_head, buffer, buffer_size);
	if (error == 0) {
		*retval = (int32_t) buffer_size;
	} else {
		*retval = 0;
	}

	list_size = sizeof(global_freezable_status_t) + (sizeof(proc_freezable_status_t) * MAX_FREEZABLE_PROCESSES);
	kfree_data(list_head, list_size);

	memorystatus_log_debug("memorystatus_freezer_get_status: returning %d (%lu - size)\n", error, (unsigned long)list_size);

	return error;
}

#endif /* DEVELOPMENT || DEBUG */

/*
 * Get a list of all processes in the freezer band which are currently frozen.
 * Used by powerlog to collect analytics on frozen process.
 */
static int
memorystatus_freezer_get_procs(user_addr_t buffer, size_t buffer_size, int32_t *retval)
{
	global_frozen_procs_t *frozen_procs = NULL;
	uint32_t band = memorystatus_freeze_jetsam_band;
	proc_t p;
	int error;
	if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE == FALSE) {
		return ENOTSUP;
	}
	if (buffer_size < sizeof(global_frozen_procs_t)) {
		return EINVAL;
	}
	frozen_procs = (global_frozen_procs_t *)kalloc_data(sizeof(global_frozen_procs_t), Z_WAITOK | Z_ZERO);
	if (frozen_procs == NULL) {
		return ENOMEM;
	}

	proc_list_lock();
	p = memorystatus_get_first_proc_locked(&band, FALSE);
	while (p && frozen_procs->gfp_num_frozen < FREEZER_CONTROL_GET_PROCS_MAX_COUNT) {
		if (_memstat_proc_is_frozen(p)) {
			frozen_procs->gfp_procs[frozen_procs->gfp_num_frozen].fp_pid = proc_getpid(p);
			strlcpy(frozen_procs->gfp_procs[frozen_procs->gfp_num_frozen].fp_name,
			    p->p_name, sizeof(proc_name_t));
			frozen_procs->gfp_num_frozen++;
		}
		p = memorystatus_get_next_proc_locked(&band, p, FALSE);
	}
	proc_list_unlock();

	buffer_size = MIN(buffer_size, sizeof(global_frozen_procs_t));
	error = copyout(frozen_procs, buffer, buffer_size);
	if (error == 0) {
		*retval = (int32_t) buffer_size;
	} else {
		*retval = 0;
	}
	kfree_data(frozen_procs, sizeof(global_frozen_procs_t));

	return error;
}

/*
 * If dasd is running an experiment that impacts their freezer candidate selection,
 * we record that in our telemetry.
 */
static memorystatus_freezer_trial_identifiers_v1 dasd_trial_identifiers;

static int
memorystatus_freezer_set_dasd_trial_identifiers(user_addr_t buffer, size_t buffer_size, int32_t *retval)
{
	memorystatus_freezer_trial_identifiers_v1 identifiers;
	int error = 0;

	if (buffer_size != sizeof(identifiers)) {
		return EINVAL;
	}
	error = copyin(buffer, &identifiers, sizeof(identifiers));
	if (error != 0) {
		return error;
	}
	if (identifiers.version != 1) {
		return EINVAL;
	}
	dasd_trial_identifiers = identifiers;
	*retval = 0;
	return error;
}

/*
 * Reset the freezer state by wiping out all suspended frozen apps, clearing
 * per-process freezer state, and starting a fresh interval.
 */
static int
memorystatus_freezer_reset_state(int32_t *retval)
{
	uint32_t band = JETSAM_PRIORITY_IDLE;
	/* Don't kill above the frozen band */
	uint32_t kMaxBand = memorystatus_freeze_jetsam_band;
	proc_t next_p = PROC_NULL;
	uint64_t new_budget;

	if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		return ENOTSUP;
	}

	os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_GENERIC);
	if (jetsam_reason == OS_REASON_NULL) {
		memorystatus_log_error("memorystatus_freezer_reset_state -- sync: failed to allocate jetsam reason\n");
	}
	lck_mtx_lock(&freezer_mutex);
	kill_all_frozen_processes(kMaxBand, true, jetsam_reason, NULL);
	proc_list_lock();

	/*
	 * Clear the considered and skip reason flags on all processes
	 * so we're starting fresh with the new policy.
	 */
	next_p = memorystatus_get_first_proc_locked(&band, TRUE);
	while (next_p) {
		proc_t p = next_p;
		uint32_t state = p->p_memstat_state;
		next_p = memorystatus_get_next_proc_locked(&band, p, TRUE);

		if (p->p_memstat_effectivepriority > kMaxBand) {
			break;
		}
		if (state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED)) {
			continue;
		}

		p->p_memstat_state &= ~(P_MEMSTAT_FREEZE_CONSIDERED);
		p->p_memstat_freeze_skip_reason = kMemorystatusFreezeSkipReasonNone;
	}

	proc_list_unlock();

	new_budget = memorystatus_freeze_calculate_new_budget(0, normal_throttle_window->burst_multiple, normal_throttle_window->mins, 0);
	memorystatus_freeze_force_new_interval(new_budget);

	lck_mtx_unlock(&freezer_mutex);
	*retval = 0;
	return 0;
}

int
memorystatus_freezer_control(int32_t flags, user_addr_t buffer, size_t buffer_size, int32_t *retval)
{
	int err = ENOTSUP;

#if DEVELOPMENT || DEBUG
	if (flags == FREEZER_CONTROL_GET_STATUS) {
		err = memorystatus_freezer_get_status(buffer, buffer_size, retval);
	}
#endif /* DEVELOPMENT || DEBUG */
	if (flags == FREEZER_CONTROL_GET_PROCS) {
		err = memorystatus_freezer_get_procs(buffer, buffer_size, retval);
	} else if (flags == FREEZER_CONTROL_SET_DASD_TRIAL_IDENTIFIERS) {
		err = memorystatus_freezer_set_dasd_trial_identifiers(buffer, buffer_size, retval);
	} else if (flags == FREEZER_CONTROL_RESET_STATE) {
		err = memorystatus_freezer_reset_state(retval);
	}

	return err;
}

static bool
kill_all_frozen_processes(uint64_t max_band, bool suspended_only, os_reason_t jetsam_reason, uint64_t *memory_reclaimed_out)
{
	LCK_MTX_ASSERT(&freezer_mutex, LCK_MTX_ASSERT_OWNED);
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_NOTOWNED);

	unsigned int band = 0;
	proc_t p = PROC_NULL, next_p = PROC_NULL;
	pid_t pid = 0;
	bool retval = false, killed = false;
	uint32_t state;
	uint64_t memory_reclaimed = 0, footprint = 0, skips = 0;
	proc_list_lock();

	band = JETSAM_PRIORITY_IDLE;
	p = PROC_NULL;
	next_p = PROC_NULL;

	next_p = memorystatus_get_first_proc_locked(&band, TRUE);
	while (next_p) {
		p = next_p;
		next_p = memorystatus_get_next_proc_locked(&band, p, TRUE);
		state = p->p_memstat_state;

		if (p->p_memstat_effectivepriority > max_band) {
			break;
		}

		if (!(state & P_MEMSTAT_FROZEN)) {
			continue;
		}

		if (suspended_only && !(state & P_MEMSTAT_SUSPENDED)) {
			continue;
		}

		if (state & P_MEMSTAT_ERROR) {
			p->p_memstat_state &= ~P_MEMSTAT_ERROR;
		}

		if (state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED)) {
			memorystatus_log("memorystatus: Skipping kill of frozen process %s (%d) because it's already exiting.\n", p->p_name, proc_getpid(p));
			skips++;
			continue;
		}

		footprint = get_task_phys_footprint(proc_task(p));
		pid = proc_getpid(p);
		proc_list_unlock();

		/* memorystatus_kill_with_jetsam_reason_sync drops a reference. */
		os_reason_ref(jetsam_reason);
		retval = memorystatus_kill_with_jetsam_reason_sync(pid, jetsam_reason);
		if (retval) {
			killed = true;
			memory_reclaimed += footprint;
		}
		proc_list_lock();
		/*
		 * The bands might have changed when we dropped the proc list lock.
		 * So start from the beginning.
		 * Since we're preventing any further freezing by holding the freezer mutex,
		 * and we skip anything we've already tried to kill this is guaranteed to terminate.
		 */
		band = 0;
		skips = 0;
		next_p = memorystatus_get_first_proc_locked(&band, TRUE);
	}

	assert(skips <= memorystatus_frozen_count);
#if MACH_ASSERT
	if (!suspended_only && max_band == JETSAM_PRIORITY_MAX) {
		/*
		 * Check that we've killed all frozen processes.
		 * Note that they may still be exiting (represented by skips).
		 */
		if (memorystatus_frozen_count - skips > 0) {
			assert(memorystatus_freeze_enabled == false);

			panic("memorystatus_disable_freeze: Failed to kill all frozen processes, memorystatus_frozen_count = %d",
			    memorystatus_frozen_count);
		}
	}
#endif /* MACH_ASSERT */
	if (memory_reclaimed_out) {
		*memory_reclaimed_out = memory_reclaimed;
	}
	proc_list_unlock();
	return killed;
}

/*
 * Disables the freezer, jetsams all frozen processes,
 * and reclaims the swap space immediately.
 */

void
memorystatus_disable_freeze(void)
{
	uint64_t memory_reclaimed = 0;
	bool killed = false;
	LCK_MTX_ASSERT(&freezer_mutex, LCK_MTX_ASSERT_OWNED);
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_NOTOWNED);


	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_FREEZE_DISABLE) | DBG_FUNC_START,
	    memorystatus_available_pages);
	memorystatus_log("memorystatus: Disabling freezer. Will kill all frozen processes\n");

	/*
	 * We hold the freezer_mutex (preventing anything from being frozen in parallel)
	 * and all frozen processes will be killed
	 * by the time we release it. Setting memorystatus_freeze_enabled to false,
	 * ensures that no new processes will be frozen once we release the mutex.
	 *
	 */
	memorystatus_freeze_enabled = false;

	/*
	 * Move dirty pages out from the throttle to the active queue since we're not freezing anymore.
	 */
	vm_page_reactivate_all_throttled();
	os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE);
	if (jetsam_reason == OS_REASON_NULL) {
		memorystatus_log_error("memorystatus_disable_freeze -- sync: failed to allocate jetsam reason\n");
	}

	killed = kill_all_frozen_processes(JETSAM_PRIORITY_MAX, false, jetsam_reason, &memory_reclaimed);

	if (killed) {
		memorystatus_log_info("memorystatus: Killed all frozen processes.\n");
		vm_swap_consider_defragmenting(VM_SWAP_FLAGS_FORCE_DEFRAG | VM_SWAP_FLAGS_FORCE_RECLAIM);

		memorystatus_post_snapshot();
	} else {
		memorystatus_log_info("memorystatus: No frozen processes to kill.\n");
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_FREEZE_DISABLE) | DBG_FUNC_END,
	    memorystatus_available_pages, memory_reclaimed);

	return;
}

static void
memorystatus_set_freeze_is_enabled(bool enabled)
{
	lck_mtx_lock(&freezer_mutex);
	if (enabled != memorystatus_freeze_enabled) {
		if (enabled) {
			memorystatus_freeze_enabled = true;
		} else {
			memorystatus_disable_freeze();
		}
	}
	lck_mtx_unlock(&freezer_mutex);
}


static int
sysctl_freeze_enabled SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int error, val = memorystatus_freeze_enabled ? 1 : 0;

	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error || !req->newptr) {
		return error;
	}

	if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		memorystatus_log_error("memorystatus: Failed attempt to set vm.freeze_enabled sysctl\n");
		return EINVAL;
	}

	memorystatus_set_freeze_is_enabled(val);

	return 0;
}

EXPERIMENT_FACTOR_PROC(_vm, freeze_enabled, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY, NULL, 0, sysctl_freeze_enabled, "I", "");

static void
schedule_interval_reset(thread_call_t reset_thread_call, throttle_interval_t *interval)
{
	uint64_t interval_expiration_ns = interval->ts.tv_sec * NSEC_PER_SEC + interval->ts.tv_nsec;
	uint64_t interval_expiration_absolutetime;
	nanoseconds_to_absolutetime(interval_expiration_ns, &interval_expiration_absolutetime);
	memorystatus_log_info("memorystatus: scheduling new freezer interval at %llu absolute time\n", interval_expiration_absolutetime);

	thread_call_enter_delayed(reset_thread_call, interval_expiration_absolutetime);
}

extern uuid_string_t trial_treatment_id;
extern uuid_string_t trial_experiment_id;
extern int trial_deployment_id;

CA_EVENT(freezer_interval,
    CA_INT, budget_remaining,
    CA_INT, error_below_min_pages,
    CA_INT, error_excess_shared_memory,
    CA_INT, error_low_private_shared_ratio,
    CA_INT, error_no_compressor_space,
    CA_INT, error_no_swap_space,
    CA_INT, error_low_probability_of_use,
    CA_INT, error_elevated,
    CA_INT, error_other,
    CA_INT, frozen_count,
    CA_INT, pageouts,
    CA_INT, refreeze_average,
    CA_INT, skipped_full,
    CA_INT, skipped_shared_mb_high,
    CA_INT, swapusage,
    CA_INT, thaw_count,
    CA_INT, thaw_percentage,
    CA_INT, thaws_per_gb,
    CA_INT, trial_deployment_id,
    CA_INT, dasd_trial_deployment_id,
    CA_INT, budget_exhaustion_duration_remaining,
    CA_INT, thaw_percentage_webcontent,
    CA_INT, thaw_percentage_fg,
    CA_INT, thaw_percentage_bg,
    CA_INT, thaw_percentage_fg_non_xpc_service,
    CA_INT, fg_resume_count,
    CA_INT, unique_freeze_count,
    CA_INT, unique_thaw_count,
    CA_STATIC_STRING(CA_UUID_LEN), trial_treatment_id,
    CA_STATIC_STRING(CA_UUID_LEN), trial_experiment_id,
    CA_STATIC_STRING(CA_UUID_LEN), dasd_trial_treatment_id,
    CA_STATIC_STRING(CA_UUID_LEN), dasd_trial_experiment_id);


/*
 * Record statistics from the expiring interval
 * via core analytics.
 */
static void
memorystatus_freeze_record_interval_analytics(void)
{
	ca_event_t event = CA_EVENT_ALLOCATE(freezer_interval);
	CA_EVENT_TYPE(freezer_interval) * e = event->data;
	e->budget_remaining = memorystatus_freeze_budget_pages_remaining * PAGE_SIZE / (1UL << 20);
	uint64_t process_considered_count, refrozen_count, below_threshold_count;
	memory_object_size_t swap_size;
	process_considered_count = memorystatus_freezer_stats.mfs_process_considered_count;
	if (process_considered_count != 0) {
		e->error_below_min_pages = memorystatus_freezer_stats.mfs_error_below_min_pages_count * 100 / process_considered_count;
		e->error_excess_shared_memory = memorystatus_freezer_stats.mfs_error_excess_shared_memory_count * 100 / process_considered_count;
		e->error_low_private_shared_ratio = memorystatus_freezer_stats.mfs_error_low_private_shared_ratio_count * 100 / process_considered_count;
		e->error_no_compressor_space = memorystatus_freezer_stats.mfs_error_no_compressor_space_count * 100 / process_considered_count;
		e->error_no_swap_space = memorystatus_freezer_stats.mfs_error_no_swap_space_count * 100 / process_considered_count;
		e->error_low_probability_of_use = memorystatus_freezer_stats.mfs_error_low_probability_of_use_count * 100 / process_considered_count;
		e->error_elevated = memorystatus_freezer_stats.mfs_error_elevated_count * 100 / process_considered_count;
		e->error_other = memorystatus_freezer_stats.mfs_error_other_count * 100 / process_considered_count;
	}
	e->frozen_count = memorystatus_frozen_count;
	e->pageouts = normal_throttle_window->pageouts * PAGE_SIZE / (1UL << 20);
	refrozen_count = memorystatus_freezer_stats.mfs_refreeze_count;
	if (refrozen_count != 0) {
		e->refreeze_average = (memorystatus_freezer_stats.mfs_bytes_refrozen / (1UL << 20)) / refrozen_count;
	}
	below_threshold_count = memorystatus_freezer_stats.mfs_below_threshold_count;
	if (below_threshold_count != 0) {
		e->skipped_full = memorystatus_freezer_stats.mfs_skipped_full_count * 100 / below_threshold_count;
		e->skipped_shared_mb_high = memorystatus_freezer_stats.mfs_skipped_shared_mb_high_count * 100 / below_threshold_count;
	}
	if (VM_CONFIG_SWAP_IS_PRESENT) {
		swap_size = vm_swap_get_total_space();
		if (swap_size) {
			e->swapusage = vm_swap_get_free_space() * 100 / swap_size;
		}
	}
	e->thaw_count = memorystatus_thaw_count;
	e->thaw_percentage = get_thaw_percentage();
	e->thaw_percentage_webcontent = get_thaw_percentage_webcontent();
	e->thaw_percentage_fg = get_thaw_percentage_fg();
	e->thaw_percentage_bg = get_thaw_percentage_bg();
	e->thaw_percentage_fg_non_xpc_service = get_thaw_percentage_fg_non_xpc_service();

	if (e->pageouts / (1UL << 10) != 0) {
		e->thaws_per_gb = memorystatus_thaw_count / (e->pageouts / (1UL << 10));
	}
	e->budget_exhaustion_duration_remaining = memorystatus_freezer_stats.mfs_budget_exhaustion_duration_remaining;
	e->fg_resume_count = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_thawed_fg, relaxed);
	e->unique_freeze_count = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_frozen, relaxed);
	e->unique_thaw_count = os_atomic_load(&memorystatus_freezer_stats.mfs_processes_thawed, relaxed);

	/*
	 * Record any xnu or dasd experiment information
	 */
	strlcpy(e->trial_treatment_id, trial_treatment_id, CA_UUID_LEN);
	strlcpy(e->trial_experiment_id, trial_experiment_id, CA_UUID_LEN);
	e->trial_deployment_id = trial_deployment_id;
	strlcpy(e->dasd_trial_treatment_id, dasd_trial_identifiers.treatment_id, CA_UUID_LEN);
	strlcpy(e->dasd_trial_experiment_id, dasd_trial_identifiers.experiment_id, CA_UUID_LEN);
	e->dasd_trial_deployment_id = dasd_trial_identifiers.deployment_id;

	CA_EVENT_SEND(event);
}

static void
memorystatus_freeze_reset_interval(void *arg0, void *arg1)
{
#pragma unused(arg0, arg1)
	struct throttle_interval_t *interval = NULL;
	clock_sec_t sec;
	clock_nsec_t nsec;
	mach_timespec_t now_ts;
	uint32_t budget_rollover = 0;

	clock_get_system_nanotime(&sec, &nsec);
	now_ts.tv_sec = (unsigned int)(MIN(sec, UINT32_MAX));
	now_ts.tv_nsec = nsec;
	interval = normal_throttle_window;

	/* Record analytics from the old interval before resetting. */
	memorystatus_freeze_record_interval_analytics();

	lck_mtx_lock(&freezer_mutex);
	/* How long has it been since the previous interval expired? */
	mach_timespec_t expiration_period_ts = now_ts;
	SUB_MACH_TIMESPEC(&expiration_period_ts, &interval->ts);
	/* Get unused budget. Clamp to 0. We'll adjust for overused budget in the next interval. */
	budget_rollover = interval->pageouts > interval->max_pageouts ?
	    0 : interval->max_pageouts - interval->pageouts;

	memorystatus_freeze_start_normal_throttle_interval(memorystatus_freeze_calculate_new_budget(
		    expiration_period_ts.tv_sec, interval->burst_multiple,
		    interval->mins, budget_rollover),
	    now_ts);
	memorystatus_freeze_budget_pages_remaining = interval->max_pageouts;

	if (!memorystatus_freezer_use_demotion_list) {
		memorystatus_demote_frozen_processes(false); /* normal mode...don't force a demotion */
	}
	lck_mtx_unlock(&freezer_mutex);
}


proc_t
memorystatus_get_coalition_leader_and_role(proc_t p, int *role_in_coalition)
{
	coalition_t     coal = COALITION_NULL;
	task_t          leader_task = NULL, curr_task = NULL;
	proc_t          leader_proc = PROC_NULL;

	curr_task = proc_task(p);
	coal = task_get_coalition(curr_task, COALITION_TYPE_JETSAM);

	if (coal == NULL || coalition_is_leader(curr_task, coal)) {
		return p;
	}

	leader_task = coalition_get_leader(coal);
	if (leader_task == TASK_NULL) {
		/*
		 * This jetsam coalition is currently leader-less.
		 * This could happen if the app died, but XPC services
		 * have not yet exited.
		 */
		return PROC_NULL;
	}

	leader_proc = (proc_t)get_bsdtask_info(leader_task);
	task_deallocate(leader_task);

	if (leader_proc == PROC_NULL) {
		/* leader task is exiting */
		return PROC_NULL;
	}

	*role_in_coalition = task_coalition_role_for_type(curr_task, COALITION_TYPE_JETSAM);

	return leader_proc;
}

bool
memorystatus_freeze_process_is_recommended(const proc_t p)
{
	assert(!memorystatus_freezer_use_ordered_list);
	int probability_of_use = 0;

	size_t entry_count = 0, i = 0;
	entry_count = (memorystatus_global_probabilities_size / sizeof(memorystatus_internal_probabilities_t));
	if (entry_count == 0) {
		/*
		 * If dasd hasn't supplied a table yet, we default to every app being eligible
		 * for the freezer.
		 */
		return true;
	}
	for (i = 0; i < entry_count; i++) {
		/*
		 * NB: memorystatus_internal_probabilities.proc_name is MAXCOMLEN + 1 bytes
		 * proc_t.p_name is 2*MAXCOMLEN + 1 bytes. So we only compare the first
		 * MAXCOMLEN bytes here since the name in the probabilities table could
		 * be truncated from the proc_t's p_name.
		 */
		if (strncmp(memorystatus_global_probabilities_table[i].proc_name,
		    p->p_name,
		    MAXCOMLEN) == 0) {
			probability_of_use = memorystatus_global_probabilities_table[i].use_probability;
			break;
		}
	}
	return probability_of_use > 0;
}

__private_extern__ void
memorystatus_freeze_init(void)
{
	kern_return_t result;
	thread_t thread;

	if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		int32_t memorystatus_freezer_use_ordered_list_bootarg = 0;
		if (PE_parse_boot_argn("memorystatus_freezer_use_ordered_list", &memorystatus_freezer_use_ordered_list_bootarg, sizeof(memorystatus_freezer_use_ordered_list_bootarg))) {
			memorystatus_freezer_use_ordered_list = (memorystatus_freezer_use_ordered_list_bootarg != 0);
		}

		int32_t memorystatus_freeze_max_candidate_band_bootarg = 0;
		if (PE_parse_boot_argn("memorystatus_freeze_max_candidate_band", &memorystatus_freeze_max_candidate_band_bootarg, sizeof(memorystatus_freeze_max_candidate_band_bootarg))) {
			if (memorystatus_freeze_max_candidate_band_bootarg >= 0 && memorystatus_freeze_max_candidate_band_bootarg <= 1000) {
				memorystatus_freeze_max_candidate_band = memorystatus_freeze_max_candidate_band_bootarg;
			}
		}

		/*
		 * This is just the default value if the underlying
		 * storage device doesn't have any specific budget.
		 * We check with the storage layer in memorystatus_freeze_update_throttle()
		 * before we start our freezing the first time.
		 */
		memorystatus_freeze_budget_pages_remaining = (memorystatus_freeze_daily_mb_max * 1024 * 1024) / PAGE_SIZE;

		result = kernel_thread_start(memorystatus_freeze_thread, NULL, &thread);
		if (result == KERN_SUCCESS) {
			proc_set_thread_policy(thread, TASK_POLICY_INTERNAL, TASK_POLICY_IO, THROTTLE_LEVEL_COMPRESSOR_TIER2);
			proc_set_thread_policy(thread, TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
			thread_set_thread_name(thread, "VM_freezer");

			thread_deallocate(thread);
		} else {
			panic("Could not create memorystatus_freeze_thread");
		}

		freeze_interval_reset_thread_call = thread_call_allocate_with_options(memorystatus_freeze_reset_interval, NULL, THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
		/* Start a new interval */

		lck_mtx_lock(&freezer_mutex);
		uint32_t budget;
		budget = memorystatus_freeze_calculate_new_budget(0, normal_throttle_window->burst_multiple, normal_throttle_window->mins, 0);
		memorystatus_freeze_force_new_interval(budget);
		lck_mtx_unlock(&freezer_mutex);
	} else {
		memorystatus_freeze_budget_pages_remaining = 0;
	}
}

void
memorystatus_freeze_configure_for_swap()
{
	if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		return;
	}

	assert(memorystatus_swap_all_apps);

	/*
	 * We expect both a larger working set and larger individual apps
	 * in this mode, so tune up the freezer accordingly.
	 */
	memorystatus_frozen_processes_max = FREEZE_PROCESSES_MAX_SWAP_ENABLED_DEFAULT;
	memorystatus_max_frozen_demotions_daily = MAX_FROZEN_PROCESS_DEMOTIONS_SWAP_ENABLED_DEFAULT;
	memorystatus_freeze_pages_max = FREEZE_PAGES_MAX_SWAP_ENABLED_DEFAULT;

	/*
	 * We don't have a budget when running with full app swap.
	 * Force a new interval. memorystatus_freeze_calculate_new_budget should give us an
	 * unlimited budget.
	 */
	lck_mtx_lock(&freezer_mutex);
	uint32_t budget;
	budget = memorystatus_freeze_calculate_new_budget(0, normal_throttle_window->burst_multiple, normal_throttle_window->mins, 0);
	memorystatus_freeze_force_new_interval(budget);
	lck_mtx_unlock(&freezer_mutex);
}

void
memorystatus_freeze_disable_swap()
{
	if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		return;
	}

	assert(!memorystatus_swap_all_apps);

	memorystatus_frozen_processes_max = FREEZE_PROCESSES_MAX_DEFAULT;
	memorystatus_max_frozen_demotions_daily = MAX_FROZEN_PROCESS_DEMOTIONS_DEFAULT;
	memorystatus_freeze_pages_max = FREEZE_PAGES_MAX_DEFAULT;

	/*
	 * Calculate a new budget now that we're constrained by our daily write budget again.
	 */
	lck_mtx_lock(&freezer_mutex);
	uint32_t budget;
	budget = memorystatus_freeze_calculate_new_budget(0, normal_throttle_window->burst_multiple, normal_throttle_window->mins, 0);
	memorystatus_freeze_force_new_interval(budget);
	lck_mtx_unlock(&freezer_mutex);
}

/*
 * Called with both the freezer_mutex and proc_list_lock held & both will be held on return.
 */
static int
memorystatus_freeze_process(
	proc_t p,
	coalition_t *coal, /* IN / OUT */
	pid_t *coalition_list, /* OUT */
	unsigned int *coalition_list_length /* OUT */)
{
	LCK_MTX_ASSERT(&freezer_mutex, LCK_MTX_ASSERT_OWNED);
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	kern_return_t kr;
	uint32_t purgeable, wired, clean, dirty, shared;
	uint64_t max_pages = 0;
	freezer_error_code_t freezer_error_code = 0;
	bool is_refreeze = false;
	task_t curr_task = TASK_NULL;

	pid_t aPid = proc_getpid(p);

	is_refreeze = _memstat_proc_is_frozen(p);

	/* Ensure the process is eligible for (re-)freezing */
	if (is_refreeze && !memorystatus_freeze_proc_is_refreeze_eligible(p)) {
		/* Process is already frozen & hasn't been thawed. Nothing to do here. */
		return EINVAL;
	}
	if (is_refreeze) {
		/*
		 * Not currently being looked at for something.
		 */
		if (p->p_memstat_state & P_MEMSTAT_LOCKED) {
			return EBUSY;
		}

		/*
		 * We are going to try and refreeze and so re-evaluate
		 * the process. We don't want to double count the shared
		 * memory. So deduct the old snapshot here.
		 */
		memorystatus_frozen_shared_mb -= p->p_memstat_freeze_sharedanon_pages;
		p->p_memstat_freeze_sharedanon_pages = 0;

		p->p_memstat_state &= ~P_MEMSTAT_REFREEZE_ELIGIBLE;
		memorystatus_refreeze_eligible_count--;
	} else {
		if (!memorystatus_is_process_eligible_for_freeze(p)) {
			return EINVAL;
		}
		if (memorystatus_frozen_count >= memorystatus_frozen_processes_max) {
			memorystatus_freeze_handle_error(p, FREEZER_ERROR_NO_SLOTS, is_refreeze, aPid, (coal ? *coal : NULL), "memorystatus_freeze_process");
			return ENOSPC;
		}
	}

	if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		/*
		 * Freezer backed by the compressor and swap file(s)
		 * will hold compressed data.
		 */

		max_pages = MIN(memorystatus_freeze_pages_max, memorystatus_freeze_budget_pages_remaining);
	} else {
		/*
		 * We only have the compressor pool.
		 */
		max_pages = UINT32_MAX - 1;
	}

	/* Mark as locked temporarily to avoid kill */
	p->p_memstat_state |= P_MEMSTAT_LOCKED;

	p = proc_ref(p, true);
	if (!p) {
		memorystatus_freezer_stats.mfs_error_other_count++;
		return EBUSY;
	}

	proc_list_unlock();

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_FREEZE) | DBG_FUNC_START, memorystatus_available_pages, aPid, max_pages);

	max_pages = MIN(max_pages, UINT32_MAX);
	kr = task_freeze(proc_task(p), &purgeable, &wired, &clean, &dirty, (uint32_t) max_pages, &shared, &freezer_error_code, FALSE /* eval only */);
	if (kr == KERN_SUCCESS || freezer_error_code == FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO) {
		memorystatus_freezer_stats.mfs_shared_pages_skipped += shared;
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_FREEZE) | DBG_FUNC_END, purgeable, wired, clean, dirty);

	memorystatus_log_debug("memorystatus_freeze_top_process: task_freeze %s for pid %d [%s] - "
	    "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, max_pages %llu, shared %d",
	    (kr == KERN_SUCCESS) ? "SUCCEEDED" : "FAILED", aPid, (*p->p_name ? p->p_name : "(unknown)"),
	    memorystatus_available_pages, purgeable, wired, clean, dirty, max_pages, shared);

	proc_list_lock();

	/* Success? */
	if (KERN_SUCCESS == kr) {
		memorystatus_freeze_entry_t data = { aPid, TRUE, dirty };

		p->p_memstat_freeze_sharedanon_pages += shared;

		memorystatus_frozen_shared_mb += shared;

		if (!is_refreeze) {
			p->p_memstat_state |= P_MEMSTAT_FROZEN;
			p->p_memstat_freeze_skip_reason = kMemorystatusFreezeSkipReasonNone;
			memorystatus_frozen_count++;
			os_atomic_inc(&memorystatus_freezer_stats.mfs_processes_frozen, relaxed);
			if (strcmp(p->p_name, "com.apple.WebKit.WebContent") == 0) {
				memorystatus_frozen_count_webcontent++;
				os_atomic_inc(&(memorystatus_freezer_stats.mfs_processes_frozen_webcontent), relaxed);
			}
			if (memorystatus_frozen_count == memorystatus_frozen_processes_max) {
				memorystatus_freeze_out_of_slots();
			}
		} else {
			// This was a re-freeze
			if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
				memorystatus_freezer_stats.mfs_bytes_refrozen += dirty * PAGE_SIZE;
				memorystatus_freezer_stats.mfs_refreeze_count++;
			}
		}

		p->p_memstat_frozen_count++;

		/*
		 * Still keeping the P_MEMSTAT_LOCKED bit till we are actually done elevating this frozen process
		 * to its higher jetsam band.
		 */
		proc_list_unlock();

		memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data));

		if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
#if FREEZE_USE_ELEVATED_INACTIVE_BAND
			int ret;
			ret = memorystatus_update_inactive_jetsam_priority_band(proc_getpid(p), MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE, memorystatus_freeze_jetsam_band, TRUE);

			if (ret) {
				memorystatus_log_error("Elevating the frozen process failed with %d\n", ret);
				/* not fatal */
			}
#endif

			/* Update stats */
			for (unsigned int i = 0; i < sizeof(throttle_intervals) / sizeof(struct throttle_interval_t); i++) {
				throttle_intervals[i].pageouts += dirty;
			}
		}
		memorystatus_freeze_update_throttle(&memorystatus_freeze_budget_pages_remaining);
		memorystatus_log("memorystatus: %sfreezing (%s) pid %d [%s] done, memorystatus_freeze_budget_pages_remaining %llu %sfroze %u pages\n",
		    is_refreeze ? "re" : "", ((!coal || !*coal) ? "general" : "coalition-driven"), aPid, ((p && *p->p_name) ? p->p_name : "unknown"),
		    memorystatus_freeze_budget_pages_remaining, is_refreeze ? "Re" : "", dirty);

		proc_list_lock();

		memorystatus_freeze_pageouts += dirty;

		if (memorystatus_frozen_count == (memorystatus_frozen_processes_max - 1)) {
			/*
			 * Add some eviction logic here? At some point should we
			 * jetsam a process to get back its swap space so that we
			 * can freeze a more eligible process at this moment in time?
			 */
		}

		/* Check if we just froze a coalition leader. If so, return the list of XPC services to freeze next. */
		if (coal != NULL && *coal == NULL) {
			curr_task = proc_task(p);
			*coal = task_get_coalition(curr_task, COALITION_TYPE_JETSAM);
			if (coalition_is_leader(curr_task, *coal)) {
				*coalition_list_length = coalition_get_pid_list(*coal, COALITION_ROLEMASK_XPC,
				    COALITION_SORT_DEFAULT, coalition_list, MAX_XPC_SERVICE_PIDS);

				if (*coalition_list_length > MAX_XPC_SERVICE_PIDS) {
					*coalition_list_length = MAX_XPC_SERVICE_PIDS;
				}
			}
		} else {
			/* We just froze an xpc service. Mark it as such for telemetry */
			p->p_memstat_state |= P_MEMSTAT_FROZEN_XPC_SERVICE;
			memorystatus_frozen_count_xpc_service++;
			os_atomic_inc(&(memorystatus_freezer_stats.mfs_processes_frozen_xpc_service), relaxed);
		}

		p->p_memstat_state &= ~P_MEMSTAT_LOCKED;
		wakeup(&p->p_memstat_state);
		proc_rele(p);
		return 0;
	} else {
		if (is_refreeze) {
			if ((freezer_error_code == FREEZER_ERROR_EXCESS_SHARED_MEMORY) ||
			    (freezer_error_code == FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO)) {
				/*
				 * Keeping this prior-frozen process in this high band when
				 * we failed to re-freeze it due to bad shared memory usage
				 * could cause excessive pressure on the lower bands.
				 * We need to demote it for now. It'll get re-evaluated next
				 * time because we don't set the P_MEMSTAT_FREEZE_IGNORE
				 * bit.
				 */

				p->p_memstat_state &= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND;
				memstat_update_priority_locked(p, JETSAM_PRIORITY_IDLE,
				    MEMSTAT_PRIORITY_INSERT_HEAD | MEMSTAT_PRIORITY_NO_AGING);
			}
		} else {
			p->p_memstat_state |= P_MEMSTAT_FREEZE_IGNORE;
		}
		memorystatus_freeze_handle_error(p, freezer_error_code, p->p_memstat_state & P_MEMSTAT_FROZEN, aPid, (coal != NULL) ? *coal : NULL, "memorystatus_freeze_process");

		p->p_memstat_state &= ~P_MEMSTAT_LOCKED;
		wakeup(&p->p_memstat_state);
		proc_rele(p);

		return EINVAL;
	}
}

/*
 * Synchronously freeze the passed proc. Called with a reference to the proc held.
 *
 * Doesn't deal with:
 * - re-freezing because this is called on a specific process and
 *   not by the freezer thread. If that changes, we'll have to teach it about
 *   refreezing a frozen process.
 *
 * - grouped/coalition freezing because we are hoping to deprecate this
 *   interface as it was used by user-space to freeze particular processes. But
 *   we have moved away from that approach to having the kernel choose the optimal
 *   candidates to be frozen.
 *
 * Returns ENOTSUP if the freezer isn't supported on this device. Otherwise
 * returns EINVAL or the value returned by task_freeze().
 */
int
memorystatus_freeze_process_sync(proc_t p)
{
	int ret = EINVAL;
	boolean_t memorystatus_freeze_swap_low = FALSE;

	if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		return ENOTSUP;
	}

	lck_mtx_lock(&freezer_mutex);

	if (p == NULL) {
		memorystatus_log_error("memorystatus_freeze_process_sync: Invalid process\n");
		goto exit;
	}

	if (memorystatus_freeze_enabled == false) {
		memorystatus_log_error("memorystatus_freeze_process_sync: Freezing is DISABLED\n");
		goto exit;
	}

	if (!memorystatus_can_freeze(&memorystatus_freeze_swap_low)) {
		memorystatus_log_info("memorystatus_freeze_process_sync: Low compressor and/or low swap space...skipping freeze\n");
		goto exit;
	}

	memorystatus_freeze_update_throttle(&memorystatus_freeze_budget_pages_remaining);
	if (!memorystatus_freeze_budget_pages_remaining) {
		memorystatus_log_info("memorystatus_freeze_process_sync: exit with NO available budget\n");
		goto exit;
	}

	proc_list_lock();

	ret = memorystatus_freeze_process(p, NULL, NULL, NULL);

exit:
	lck_mtx_unlock(&freezer_mutex);

	return ret;
}

proc_t
memorystatus_freezer_candidate_list_get_proc(
	struct memorystatus_freezer_candidate_list *list,
	size_t index,
	uint64_t *pid_mismatch_counter)
{
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);
	if (list->mfcl_list == NULL || list->mfcl_length <= index) {
		return NULL;
	}
	memorystatus_properties_freeze_entry_v1 *entry = &list->mfcl_list[index];
	if (entry->pid == NO_PID) {
		/* Entry has been removed. */
		return NULL;
	}

	proc_t p = proc_find_locked(entry->pid);
	if (p && strncmp(entry->proc_name, p->p_name, sizeof(proc_name_t)) == 0) {
		/*
		 * We grab a reference when we are about to freeze the process. So drop
		 * the reference that proc_find_locked() grabbed for us.
		 * We also have the proc_list_lock so this process is stable.
		 */
		proc_rele(p);
		return p;
	} else {
		if (p) {
			/* pid rollover. */
			proc_rele(p);
		}
		/*
		 * The proc has exited since we received this list.
		 * It may have re-launched with a new pid, so we go looking for it.
		 */
		unsigned int band = JETSAM_PRIORITY_IDLE;
		p = memorystatus_get_first_proc_locked(&band, TRUE);
		while (p != NULL && band <= memorystatus_freeze_max_candidate_band) {
			if (strncmp(entry->proc_name, p->p_name, sizeof(proc_name_t)) == 0) {
				if (pid_mismatch_counter != NULL) {
					(*pid_mismatch_counter)++;
				}
				/* Stash the pid for faster lookup next time. */
				entry->pid = proc_getpid(p);
				return p;
			}
			p = memorystatus_get_next_proc_locked(&band, p, TRUE);
		}
		/* No match. */
		return NULL;
	}
}

static size_t
memorystatus_freeze_pid_list(pid_t *pid_list, unsigned int num_pids)
{
	int ret = 0;
	size_t num_frozen = 0;
	while (num_pids > 0 &&
	    memorystatus_frozen_count < memorystatus_frozen_processes_max) {
		pid_t pid = pid_list[--num_pids];
		proc_t p = proc_find_locked(pid);
		if (p) {
			proc_rele(p);
			ret = memorystatus_freeze_process(p, NULL, NULL, NULL);
			if (ret != 0) {
				break;
			}
			num_frozen++;
		}
	}
	return num_frozen;
}

/*
 * Attempt to freeze the best candidate process.
 * Keep trying until we freeze something or run out of candidates.
 * Returns the number of processes frozen (including coalition members).
 */
static size_t
memorystatus_freeze_top_process(void)
{
	int freeze_ret;
	size_t num_frozen = 0;
	coalition_t coal = COALITION_NULL;
	pid_t pid_list[MAX_XPC_SERVICE_PIDS];
	unsigned int ntasks = 0;
	struct memorystatus_freeze_list_iterator iterator;
	LCK_MTX_ASSERT(&freezer_mutex, LCK_MTX_ASSERT_OWNED);

	bzero(&iterator, sizeof(struct memorystatus_freeze_list_iterator));
	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_FREEZE_SCAN) | DBG_FUNC_START, memorystatus_available_pages);

	proc_list_lock();
	while (true) {
		proc_t p = memorystatus_freeze_pick_process(&iterator);
		if (p == PROC_NULL) {
			/* Nothing left to freeze */
			break;
		}
		freeze_ret = memorystatus_freeze_process(p, &coal, pid_list, &ntasks);
		if (freeze_ret == 0) {
			num_frozen = 1;
			/*
			 * We froze a process successfully.
			 * If it's a coalition head, freeze the coalition.
			 * Then we're done for now.
			 */
			if (coal != NULL) {
				num_frozen += memorystatus_freeze_pid_list(pid_list, ntasks);
			}
			break;
		} else {
			if (vm_compressor_low_on_space() || vm_swap_low_on_space()) {
				break;
			}
			/*
			 * Freeze failed but we're not out of space.
			 * Keep trying to find a good candidate,
			 * memorystatus_freeze_pick_process will not return this proc again until
			 * we reset the iterator.
			 */
		}
	}
	proc_list_unlock();

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_FREEZE_SCAN) | DBG_FUNC_END, memorystatus_available_pages);

	return num_frozen;
}

#if DEVELOPMENT || DEBUG
/* For testing memorystatus_freeze_top_process */
static int
sysctl_memorystatus_freeze_top_process SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int error, val, ret = 0;
	size_t num_frozen;
	/*
	 * Only freeze on write to prevent freezing during `sysctl -a`.
	 * The actual value written doesn't matter.
	 */
	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error || !req->newptr) {
		return error;
	}

	if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		return ENOTSUP;
	}

	lck_mtx_lock(&freezer_mutex);
	num_frozen = memorystatus_freeze_top_process();
	lck_mtx_unlock(&freezer_mutex);

	if (num_frozen == 0) {
		ret = ESRCH;
	}
	return ret;
}
SYSCTL_PROC(_vm, OID_AUTO, memorystatus_freeze_top_process, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MASKED,
    0, 0, &sysctl_memorystatus_freeze_top_process, "I", "");
#endif /* DEVELOPMENT || DEBUG */

static inline boolean_t
memorystatus_can_freeze_processes(void)
{
	boolean_t ret;

	proc_list_lock();

	if (memorystatus_suspended_count) {
		memorystatus_freeze_suspended_threshold = MIN(memorystatus_freeze_suspended_threshold, FREEZE_SUSPENDED_THRESHOLD_DEFAULT);

		if ((memorystatus_suspended_count - memorystatus_frozen_count) > memorystatus_freeze_suspended_threshold) {
			ret = TRUE;
		} else {
			ret = FALSE;
		}
	} else {
		ret = FALSE;
	}

	proc_list_unlock();

	return ret;
}

static boolean_t
memorystatus_can_freeze(boolean_t *memorystatus_freeze_swap_low)
{
	boolean_t can_freeze = TRUE;

	/* Only freeze if we're sufficiently low on memory; this holds off freeze right
	*  after boot,  and is generally is a no-op once we've reached steady state. */
	if (memorystatus_available_pages > memorystatus_freeze_threshold) {
		return FALSE;
	}

	/* Check minimum suspended process threshold. */
	if (!memorystatus_can_freeze_processes()) {
		return FALSE;
	}
	assert(VM_CONFIG_COMPRESSOR_IS_PRESENT);

	if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		/*
		 * In-core compressor used for freezing WITHOUT on-disk swap support.
		 */
		if (vm_compressor_low_on_space()) {
			if (*memorystatus_freeze_swap_low) {
				*memorystatus_freeze_swap_low = TRUE;
			}

			can_freeze = FALSE;
		} else {
			if (*memorystatus_freeze_swap_low) {
				*memorystatus_freeze_swap_low = FALSE;
			}

			can_freeze = TRUE;
		}
	} else {
		/*
		 * Freezing WITH on-disk swap support.
		 *
		 * In-core compressor fronts the swap.
		 */
		if (vm_swap_low_on_space()) {
			if (*memorystatus_freeze_swap_low) {
				*memorystatus_freeze_swap_low = TRUE;
			}

			can_freeze = FALSE;
		}
	}

	return can_freeze;
}

/*
 * Demote the given frozen process.
 * Caller must hold the proc_list_lock & it will be held on return.
 */
static void
memorystatus_demote_frozen_process(proc_t p, bool urgent_mode __unused)
{
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	/* We demote to IDLE unless someone has asserted a higher priority on this process. */
	int priority = JETSAM_PRIORITY_IDLE;
	p->p_memstat_state &= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND;
	if (_memstat_proc_has_priority_assertion(p)) {
		priority = MAX(p->p_memstat_assertionpriority, priority);
	}
	if (_memstat_proc_is_tracked(p) && _memstat_proc_is_dirty(p)) {
		priority = MAX(p->p_memstat_requestedpriority, priority);
	}
	memstat_update_priority_locked(p, priority, MEMSTAT_PRIORITY_NO_AGING);
#if DEVELOPMENT || DEBUG
	memorystatus_log("memorystatus_demote_frozen_process(%s) pid %d [%s]\n",
	    (urgent_mode ? "urgent" : "normal"), (p ? proc_getpid(p) : -1), ((p && *p->p_name) ? p->p_name : "unknown"));
#endif /* DEVELOPMENT || DEBUG */

	/*
	 * The freezer thread will consider this a normal app to be frozen
	 * because it is in the IDLE band. So we don't need the
	 * P_MEMSTAT_REFREEZE_ELIGIBLE state here. Also, if it gets resumed
	 * we'll correctly count it as eligible for re-freeze again.
	 *
	 * We don't drop the frozen count because this process still has
	 * state on disk. So there's a chance it gets resumed and then it
	 * should land in the higher jetsam band. For that it needs to
	 * remain marked frozen.
	 */
	if (memorystatus_freeze_proc_is_refreeze_eligible(p)) {
		p->p_memstat_state &= ~P_MEMSTAT_REFREEZE_ELIGIBLE;
		memorystatus_refreeze_eligible_count--;
	}
}

static unsigned int
memorystatus_demote_frozen_processes_using_thaw_count(bool urgent_mode)
{
	unsigned int band = (unsigned int) memorystatus_freeze_jetsam_band;
	unsigned int demoted_proc_count = 0;
	proc_t p = PROC_NULL, next_p = PROC_NULL;
	proc_list_lock();

	next_p = memorystatus_get_first_proc_locked(&band, FALSE);
	while (next_p) {
		p = next_p;
		next_p = memorystatus_get_next_proc_locked(&band, p, FALSE);

		if (!_memstat_proc_is_frozen(p)) {
			continue;
		}

		if (p->p_memstat_state & P_MEMSTAT_LOCKED) {
			continue;
		}

		if (urgent_mode) {
			if (!memorystatus_freeze_proc_is_refreeze_eligible(p)) {
				/*
				 * This process hasn't been thawed recently and so most of
				 * its state sits on NAND and so we skip it -- jetsamming it
				 * won't help with memory pressure.
				 */
				continue;
			}
		} else {
			if (p->p_memstat_thaw_count >= memorystatus_thaw_count_demotion_threshold) {
				/*
				 * This process has met / exceeded our thaw count demotion threshold
				 * and so we let it live in the higher bands.
				 */
				continue;
			}
		}

		memorystatus_demote_frozen_process(p, urgent_mode);
		demoted_proc_count++;
		if ((urgent_mode) || (demoted_proc_count == memorystatus_max_frozen_demotions_daily)) {
			break;
		}
	}

	proc_list_unlock();
	return demoted_proc_count;
}

static unsigned int
memorystatus_demote_frozen_processes_using_demote_list(bool urgent_mode)
{
	LCK_MTX_ASSERT(&freezer_mutex, LCK_MTX_ASSERT_OWNED);
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_NOTOWNED);
	assert(memorystatus_freezer_use_demotion_list);
	unsigned int demoted_proc_count = 0;

	proc_list_lock();
	for (size_t i = 0; i < memorystatus_global_demote_list.mfcl_length; i++) {
		proc_t p = memorystatus_freezer_candidate_list_get_proc(
			&memorystatus_global_demote_list,
			i,
			&memorystatus_freezer_stats.mfs_demote_pid_mismatches);
		if (p != NULL && memorystatus_freeze_proc_is_refreeze_eligible(p)) {
			memorystatus_demote_frozen_process(p, urgent_mode);
			/* Remove this entry now that it's been demoted. */
			memorystatus_global_demote_list.mfcl_list[i].pid = NO_PID;
			demoted_proc_count++;
			/*
			 * We only demote one proc at a time in this mode.
			 * This gives jetsam a chance to kill the recently demoted processes.
			 */
			break;
		}
	}

	proc_list_unlock();
	return demoted_proc_count;
}

/*
 * This function evaluates if the currently frozen processes deserve
 * to stay in the higher jetsam band. There are 2 modes:
 * - 'force one == TRUE': (urgent mode)
 *	We are out of budget and can't refreeze a process. The process's
 * state, if it was resumed, will stay in compressed memory. If we let it
 * remain up in the higher frozen jetsam band, it'll put a lot of pressure on
 * the lower bands. So we force-demote the least-recently-used-and-thawed
 * process.
 *
 * - 'force_one == FALSE': (normal mode)
 *      If the # of thaws of a process is below our threshold, then we
 * will demote that process into the IDLE band.
 * We don't immediately kill the process here because it  already has
 * state on disk and so it might be worth giving it another shot at
 * getting thawed/resumed and used.
 */
static void
memorystatus_demote_frozen_processes(bool urgent_mode)
{
	unsigned int demoted_proc_count = 0;

	if (memorystatus_freeze_enabled == false) {
		/*
		 * Freeze has been disabled likely to
		 * reclaim swap space. So don't change
		 * any state on the frozen processes.
		 */
		return;
	}

	/*
	 * We have two demotion policies which can be toggled by userspace.
	 * In non-urgent mode, the ordered list policy will
	 * choose a demotion candidate using the list provided by dasd.
	 * The thaw count policy will demote the oldest process that hasn't been
	 * thawed more than memorystatus_thaw_count_demotion_threshold times.
	 *
	 * If urgent_mode is set, both policies will only consider demoting
	 * processes that are re-freeze eligible. But the ordering is different.
	 * The ordered list policy will scan in the order given by dasd.
	 * The thaw count policy will scan through the frozen band.
	 */
	if (memorystatus_freezer_use_demotion_list) {
		demoted_proc_count += memorystatus_demote_frozen_processes_using_demote_list(urgent_mode);

		if (demoted_proc_count == 0 && urgent_mode) {
			/*
			 * We're out of budget and the demotion list doesn't contain any valid
			 * candidates. We still need to demote something. Fall back to scanning
			 * the frozen band.
			 */
			memorystatus_demote_frozen_processes_using_thaw_count(true);
		}
	} else {
		demoted_proc_count += memorystatus_demote_frozen_processes_using_thaw_count(urgent_mode);
	}
}

/*
 * Calculate a new freezer budget.
 * @param time_since_last_interval_expired_sec How long has it been (in seconds) since the previous interval expired.
 * @param burst_multiple The burst_multiple for the new period
 * @param interval_duration_min How many minutes will the new interval be?
 * @param rollover The amount to rollover from the previous budget.
 *
 * @return A budget for the new interval.
 */
static uint32_t
memorystatus_freeze_calculate_new_budget(
	unsigned int time_since_last_interval_expired_sec,
	unsigned int burst_multiple,
	unsigned int interval_duration_min,
	uint32_t rollover)
{
	uint64_t freeze_daily_budget = 0, freeze_daily_budget_mb = 0, daily_budget_pageouts = 0, budget_missed = 0, freeze_daily_pageouts_max = 0, new_budget = 0;
	const static unsigned int kNumSecondsInDay = 60 * 60 * 24;
	/* Precision factor for days_missed. 2 decimal points. */
	const static unsigned int kFixedPointFactor = 100;
	unsigned int days_missed;

	if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		return 0;
	}
	if (memorystatus_swap_all_apps) {
		/*
		 * We effectively have an unlimited budget when app swap is enabled.
		 */
		memorystatus_freeze_daily_mb_max = UINT32_MAX;
		return UINT32_MAX;
	}

	/* Get the daily budget from the storage layer */
	if (vm_swap_max_budget(&freeze_daily_budget)) {
		freeze_daily_budget_mb = freeze_daily_budget / (1024 * 1024);
		assert(freeze_daily_budget_mb <= UINT32_MAX);
		memorystatus_freeze_daily_mb_max = (unsigned int) freeze_daily_budget_mb;
		memorystatus_log_info("memorystatus: memorystatus_freeze_daily_mb_max set to %dMB\n", memorystatus_freeze_daily_mb_max);
	}
	/* Calculate the daily pageout budget */
	freeze_daily_pageouts_max = memorystatus_freeze_daily_mb_max * (1024 * 1024 / PAGE_SIZE);
	/* Multiply by memorystatus_freeze_budget_multiplier */
	freeze_daily_pageouts_max = ((kFixedPointFactor * memorystatus_freeze_budget_multiplier / 100) * freeze_daily_pageouts_max) / kFixedPointFactor;

	daily_budget_pageouts = (burst_multiple * (((uint64_t) interval_duration_min * freeze_daily_pageouts_max) / (kNumSecondsInDay / 60)));

	/*
	 * Add additional budget for time since the interval expired.
	 * For example, if the interval expired n days ago, we should get an additional n days
	 * of budget since we didn't use any budget during those n days.
	 */
	days_missed = time_since_last_interval_expired_sec * kFixedPointFactor / kNumSecondsInDay;
	budget_missed = days_missed * freeze_daily_pageouts_max / kFixedPointFactor;
	new_budget = rollover + daily_budget_pageouts + budget_missed;
	return (uint32_t) MIN(new_budget, UINT32_MAX);
}

/*
 * Mark all non frozen, freezer-eligible processes as skipped for the given reason.
 * Used when we hit some system freeze limit and know that we won't be considering remaining processes.
 * If you're using this for a new reason, make sure to add it to memorystatus_freeze_init_proc so that
 * it gets set for new processes.
 * NB: These processes will retain this skip reason until they are reconsidered by memorystatus_is_process_eligible_for_freeze.
 */
static void
memorystatus_freeze_mark_eligible_processes_with_skip_reason(memorystatus_freeze_skip_reason_t reason, bool locked)
{
	LCK_MTX_ASSERT(&freezer_mutex, LCK_MTX_ASSERT_OWNED);
	LCK_MTX_ASSERT(&proc_list_mlock, locked ? LCK_MTX_ASSERT_OWNED : LCK_MTX_ASSERT_NOTOWNED);
	unsigned int band = JETSAM_PRIORITY_IDLE;
	proc_t p;

	if (!locked) {
		proc_list_lock();
	}
	p = memorystatus_get_first_proc_locked(&band, FALSE);
	while (p) {
		assert(p->p_memstat_effectivepriority == (int32_t) band);
		if (!_memstat_proc_is_frozen(p) &&
		    memorystatus_is_process_eligible_for_freeze(p)) {
			assert(p->p_memstat_freeze_skip_reason == kMemorystatusFreezeSkipReasonNone);
			p->p_memstat_freeze_skip_reason = (uint8_t) reason;
		}
		p = memorystatus_get_next_proc_locked(&band, p, FALSE);
	}
	if (!locked) {
		proc_list_unlock();
	}
}

/*
 * Called after we fail to freeze a process.
 * Logs the failure, marks the process with the failure reason, and updates freezer stats.
 */
static void
memorystatus_freeze_handle_error(
	proc_t p,
	const freezer_error_code_t freezer_error_code,
	bool was_refreeze,
	pid_t pid,
	const coalition_t coalition,
	const char* log_prefix)
{
	const char *reason;
	memorystatus_freeze_skip_reason_t skip_reason;

	switch (freezer_error_code) {
	case FREEZER_ERROR_EXCESS_SHARED_MEMORY:
		memorystatus_freezer_stats.mfs_error_excess_shared_memory_count++;
		reason = "too much shared memory";
		skip_reason = kMemorystatusFreezeSkipReasonExcessSharedMemory;
		break;
	case FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO:
		memorystatus_freezer_stats.mfs_error_low_private_shared_ratio_count++;
		reason = "private-shared pages ratio";
		skip_reason = kMemorystatusFreezeSkipReasonLowPrivateSharedRatio;
		break;
	case FREEZER_ERROR_NO_COMPRESSOR_SPACE:
		memorystatus_freezer_stats.mfs_error_no_compressor_space_count++;
		reason = "no compressor space";
		skip_reason = kMemorystatusFreezeSkipReasonNoCompressorSpace;
		break;
	case FREEZER_ERROR_NO_SWAP_SPACE:
		memorystatus_freezer_stats.mfs_error_no_swap_space_count++;
		reason = "no swap space";
		skip_reason = kMemorystatusFreezeSkipReasonNoSwapSpace;
		break;
	case FREEZER_ERROR_NO_SLOTS:
		memorystatus_freezer_stats.mfs_skipped_full_count++;
		reason = "no slots";
		skip_reason = kMemorystatusFreezeSkipReasonOutOfSlots;
		break;
	default:
		reason = "unknown error";
		skip_reason = kMemorystatusFreezeSkipReasonOther;
	}

	p->p_memstat_freeze_skip_reason = (uint8_t) skip_reason;

	memorystatus_log("%s: %sfreezing (%s) pid %d [%s]...skipped (%s)\n",
	    log_prefix, was_refreeze ? "re" : "",
	    (coalition == NULL ? "general" : "coalition-driven"), pid,
	    ((p && *p->p_name) ? p->p_name : "unknown"), reason);
}

/*
 * Start a new normal throttle interval with the given budget.
 * Caller must hold the freezer mutex
 */
static void
memorystatus_freeze_start_normal_throttle_interval(uint32_t new_budget, mach_timespec_t start_ts)
{
	unsigned int band;
	proc_t p, next_p;
	LCK_MTX_ASSERT(&freezer_mutex, LCK_MTX_ASSERT_OWNED);
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_NOTOWNED);

	normal_throttle_window->max_pageouts = new_budget;
	normal_throttle_window->ts.tv_sec = normal_throttle_window->mins * 60;
	normal_throttle_window->ts.tv_nsec = 0;
	ADD_MACH_TIMESPEC(&normal_throttle_window->ts, &start_ts);
	/* Since we update the throttle stats pre-freeze, adjust for overshoot here */
	if (normal_throttle_window->pageouts > normal_throttle_window->max_pageouts) {
		normal_throttle_window->pageouts -= normal_throttle_window->max_pageouts;
	} else {
		normal_throttle_window->pageouts = 0;
	}
	/* Ensure the normal window is now active. */
	memorystatus_freeze_degradation = FALSE;

	/*
	 * Reset interval statistics.
	 */
	memorystatus_freezer_stats.mfs_shared_pages_skipped = 0;
	memorystatus_freezer_stats.mfs_process_considered_count = 0;
	memorystatus_freezer_stats.mfs_error_below_min_pages_count = 0;
	memorystatus_freezer_stats.mfs_error_excess_shared_memory_count = 0;
	memorystatus_freezer_stats.mfs_error_low_private_shared_ratio_count = 0;
	memorystatus_freezer_stats.mfs_error_no_compressor_space_count = 0;
	memorystatus_freezer_stats.mfs_error_no_swap_space_count = 0;
	memorystatus_freezer_stats.mfs_error_low_probability_of_use_count = 0;
	memorystatus_freezer_stats.mfs_error_elevated_count = 0;
	memorystatus_freezer_stats.mfs_error_other_count = 0;
	memorystatus_freezer_stats.mfs_refreeze_count = 0;
	memorystatus_freezer_stats.mfs_bytes_refrozen = 0;
	memorystatus_freezer_stats.mfs_below_threshold_count = 0;
	memorystatus_freezer_stats.mfs_skipped_full_count = 0;
	memorystatus_freezer_stats.mfs_skipped_shared_mb_high_count = 0;
	memorystatus_freezer_stats.mfs_budget_exhaustion_duration_remaining = 0;
	memorystatus_thaw_count = 0;
	os_atomic_store(&memorystatus_freezer_stats.mfs_processes_thawed, 0, release);
	os_atomic_store(&memorystatus_freezer_stats.mfs_processes_thawed_webcontent, 0, release);
	os_atomic_store(&memorystatus_freezer_stats.mfs_processes_thawed_fg, 0, release);
	os_atomic_store(&memorystatus_freezer_stats.mfs_processes_thawed_fg_xpc_service, 0, release);
	os_atomic_store(&memorystatus_freezer_stats.mfs_processes_frozen, memorystatus_frozen_count, release);
	os_atomic_store(&memorystatus_freezer_stats.mfs_processes_frozen_webcontent, memorystatus_frozen_count_webcontent, release);
	os_atomic_store(&memorystatus_freezer_stats.mfs_processes_frozen_xpc_service, memorystatus_frozen_count_xpc_service, release);
	os_atomic_store(&memorystatus_freezer_stats.mfs_processes_fg_resumed, 0, release);
	os_atomic_inc(&memorystatus_freeze_current_interval, release);

	/* Clear the focal thaw bit */
	proc_list_lock();
	band = JETSAM_PRIORITY_IDLE;
	p = PROC_NULL;
	next_p = PROC_NULL;

	next_p = memorystatus_get_first_proc_locked(&band, TRUE);
	while (next_p) {
		p = next_p;
		next_p = memorystatus_get_next_proc_locked(&band, p, TRUE);

		if (p->p_memstat_effectivepriority > JETSAM_PRIORITY_FOREGROUND) {
			break;
		}
		p->p_memstat_state &= ~P_MEMSTAT_FROZEN_FOCAL_THAW;
	}
	proc_list_unlock();

	schedule_interval_reset(freeze_interval_reset_thread_call, normal_throttle_window);
}

#if DEVELOPMENT || DEBUG

static int
sysctl_memorystatus_freeze_calculate_new_budget SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int error = 0;
	unsigned int time_since_last_interval_expired_sec = 0;
	unsigned int new_budget;

	error = sysctl_handle_int(oidp, &time_since_last_interval_expired_sec, 0, req);
	if (error || !req->newptr) {
		return error;
	}

	if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		return ENOTSUP;
	}
	new_budget = memorystatus_freeze_calculate_new_budget(time_since_last_interval_expired_sec, 1, NORMAL_WINDOW_MINS, 0);
	return copyout(&new_budget, req->oldptr, MIN(sizeof(req->oldlen), sizeof(new_budget)));
}

SYSCTL_PROC(_vm, OID_AUTO, memorystatus_freeze_calculate_new_budget, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MASKED,
    0, 0, &sysctl_memorystatus_freeze_calculate_new_budget, "I", "");

#endif /* DEVELOPMENT || DEBUG */

/*
 * Called when we first run out of budget in an interval.
 * Marks idle processes as not frozen due to lack of budget.
 * NB: It might be worth having a CA event here.
 */
static void
memorystatus_freeze_out_of_budget(const struct throttle_interval_t *interval)
{
	LCK_MTX_ASSERT(&freezer_mutex, LCK_MTX_ASSERT_OWNED);
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_NOTOWNED);

	mach_timespec_t time_left = {0, 0};
	mach_timespec_t now_ts;
	clock_sec_t sec;
	clock_nsec_t nsec;

	time_left.tv_sec = interval->ts.tv_sec;
	time_left.tv_nsec = 0;
	clock_get_system_nanotime(&sec, &nsec);
	now_ts.tv_sec = (unsigned int)(MIN(sec, UINT32_MAX));
	now_ts.tv_nsec = nsec;

	SUB_MACH_TIMESPEC(&time_left, &now_ts);
	memorystatus_freezer_stats.mfs_budget_exhaustion_duration_remaining = time_left.tv_sec;
	memorystatus_log(
		"memorystatus_freeze: Out of NAND write budget with %u minutes left in the current freezer interval. %u procs are frozen.\n",
		time_left.tv_sec / 60, memorystatus_frozen_count);

	memorystatus_freeze_mark_eligible_processes_with_skip_reason(kMemorystatusFreezeSkipReasonOutOfBudget, false);
}

/*
 * Called when we cross over the threshold of maximum frozen processes allowed.
 * Marks remaining idle processes as not frozen due to lack of slots.
 */
static void
memorystatus_freeze_out_of_slots(void)
{
	LCK_MTX_ASSERT(&freezer_mutex, LCK_MTX_ASSERT_OWNED);
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);
	assert(memorystatus_frozen_count == memorystatus_frozen_processes_max);

	memorystatus_log(
		"memorystatus_freeze: Out of slots in the freezer. %u procs are frozen.\n",
		memorystatus_frozen_count);

	memorystatus_freeze_mark_eligible_processes_with_skip_reason(kMemorystatusFreezeSkipReasonOutOfSlots, true);
}

/*
 * This function will do 4 things:
 *
 * 1) check to see if we are currently in a degraded freezer mode, and if so:
 *    - check to see if our window has expired and we should exit this mode, OR,
 *    - return a budget based on the degraded throttle window's max. pageouts vs current pageouts.
 *
 * 2) check to see if we are in a NEW normal window and update the normal throttle window's params.
 *
 * 3) check what the current normal window allows for a budget.
 *
 * 4) calculate the current rate of pageouts for DEGRADED_WINDOW_MINS duration. If that rate is below
 *    what we would normally expect, then we are running low on our daily budget and need to enter
 *    degraded perf. mode.
 *
 *    Caller must hold the freezer mutex
 *    Caller must not hold the proc_list lock
 */

static void
memorystatus_freeze_update_throttle(uint64_t *budget_pages_allowed)
{
	clock_sec_t sec;
	clock_nsec_t nsec;
	mach_timespec_t now_ts;
	LCK_MTX_ASSERT(&freezer_mutex, LCK_MTX_ASSERT_OWNED);
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_NOTOWNED);

	unsigned int freeze_daily_pageouts_max = 0;
	bool started_with_budget = (*budget_pages_allowed > 0);

#if DEVELOPMENT || DEBUG
	if (!memorystatus_freeze_throttle_enabled) {
		/*
		 * No throttling...we can use the full budget everytime.
		 */
		*budget_pages_allowed = UINT64_MAX;
		return;
	}
#endif

	clock_get_system_nanotime(&sec, &nsec);
	now_ts.tv_sec = (unsigned int)(MIN(sec, UINT32_MAX));
	now_ts.tv_nsec = nsec;

	struct throttle_interval_t *interval = NULL;

	if (memorystatus_freeze_degradation == TRUE) {
		interval = degraded_throttle_window;

		if (CMP_MACH_TIMESPEC(&now_ts, &interval->ts) >= 0) {
			interval->pageouts = 0;
			interval->max_pageouts = 0;
		} else {
			*budget_pages_allowed = interval->max_pageouts - interval->pageouts;
		}
	}

	interval = normal_throttle_window;

	/*
	 * Current throttle window.
	 * Deny freezing if we have no budget left.
	 * Try graceful degradation if we are within 25% of:
	 * - the daily budget, and
	 * - the current budget left is below our normal budget expectations.
	 */

	if (memorystatus_freeze_degradation == FALSE) {
		if (interval->pageouts >= interval->max_pageouts) {
			*budget_pages_allowed = 0;
			if (started_with_budget) {
				memorystatus_freeze_out_of_budget(interval);
			}
		} else {
			int budget_left = interval->max_pageouts - interval->pageouts;
			int budget_threshold = (freeze_daily_pageouts_max * FREEZE_DEGRADATION_BUDGET_THRESHOLD) / 100;

			mach_timespec_t time_left = {0, 0};

			time_left.tv_sec = interval->ts.tv_sec;
			time_left.tv_nsec = 0;

			SUB_MACH_TIMESPEC(&time_left, &now_ts);

			if (budget_left <= budget_threshold) {
				/*
				 * For the current normal window, calculate how much we would pageout in a DEGRADED_WINDOW_MINS duration.
				 * And also calculate what we would pageout for the same DEGRADED_WINDOW_MINS duration if we had the full
				 * daily pageout budget.
				 */

				unsigned int current_budget_rate_allowed = ((budget_left / time_left.tv_sec) / 60) * DEGRADED_WINDOW_MINS;
				unsigned int normal_budget_rate_allowed = (freeze_daily_pageouts_max / NORMAL_WINDOW_MINS) * DEGRADED_WINDOW_MINS;

				/*
				 * The current rate of pageouts is below what we would expect for
				 * the normal rate i.e. we have below normal budget left and so...
				 */

				if (current_budget_rate_allowed < normal_budget_rate_allowed) {
					memorystatus_freeze_degradation = TRUE;
					degraded_throttle_window->max_pageouts = current_budget_rate_allowed;
					degraded_throttle_window->pageouts = 0;

					/*
					 * Switch over to the degraded throttle window so the budget
					 * doled out is based on that window.
					 */
					interval = degraded_throttle_window;
				}
			}

			*budget_pages_allowed = interval->max_pageouts - interval->pageouts;
		}
	}

	memorystatus_log_debug(
		"memorystatus_freeze_update_throttle_interval: throttle updated - %d frozen (%d max) within %dm; %dm remaining\n",
		interval->pageouts, interval->max_pageouts, interval->mins, (interval->ts.tv_sec - now_ts.tv_sec) / 60);
}

SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_apps_idle_delay_multiplier, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_apps_idle_delay_multiplier, 0, "");

bool memorystatus_freeze_thread_init = false;
static void
memorystatus_freeze_thread(void *param __unused, wait_result_t wr __unused)
{
	static boolean_t memorystatus_freeze_swap_low = FALSE;
	size_t max_to_freeze = 0, num_frozen = 0, num_frozen_this_iteration = 0;

	if (!memorystatus_freeze_thread_init) {
#if CONFIG_THREAD_GROUPS
		thread_group_vm_add();
#endif
		memorystatus_freeze_thread_init = true;
	}

	max_to_freeze = memorystatus_pick_freeze_count_for_wakeup();

	lck_mtx_lock(&freezer_mutex);
	if (memorystatus_freeze_enabled) {
		if (memorystatus_freezer_use_demotion_list && memorystatus_refreeze_eligible_count > 0) {
			memorystatus_demote_frozen_processes(false); /* Normal mode. Consider demoting thawed processes. */
		}
		while (num_frozen < max_to_freeze &&
		    memorystatus_can_freeze(&memorystatus_freeze_swap_low) &&
		    ((memorystatus_frozen_count < memorystatus_frozen_processes_max) ||
		    (memorystatus_refreeze_eligible_count >= memorystatus_min_thaw_refreeze_threshold))) {
			/* Only freeze if we've not exceeded our pageout budgets.*/
			memorystatus_freeze_update_throttle(&memorystatus_freeze_budget_pages_remaining);

			if (memorystatus_freeze_budget_pages_remaining) {
				num_frozen_this_iteration = memorystatus_freeze_top_process();
				if (num_frozen_this_iteration == 0) {
					/* Nothing left to freeze. */
					break;
				}
				num_frozen += num_frozen_this_iteration;
			} else {
				memorystatus_demote_frozen_processes(true); /* urgent mode..force one demotion */
				break;
			}
		}
	}

	/*
	 * Give applications currently in the aging band a chance to age out into the idle band before
	 * running the freezer again.
	 */
	if (memorystatus_freeze_dynamic_thread_delay_enabled) {
		if ((num_frozen > 0) || (memorystatus_frozen_count == 0)) {
			memorystatus_freeze_apps_idle_delay_multiplier = FREEZE_APPS_IDLE_DELAY_MULTIPLIER_FAST;
		} else {
			memorystatus_freeze_apps_idle_delay_multiplier = FREEZE_APPS_IDLE_DELAY_MULTIPLIER_SLOW;
		}
	}
	memorystatus_freezer_thread_next_run_ts = mach_absolute_time() + (memorystatus_apps_idle_delay_time * memorystatus_freeze_apps_idle_delay_multiplier);

	assert_wait((event_t) &memorystatus_freeze_wakeup, THREAD_UNINT);
	lck_mtx_unlock(&freezer_mutex);

	thread_block((thread_continue_t) memorystatus_freeze_thread);
}

int
memorystatus_get_process_is_freezable(pid_t pid, int *is_freezable)
{
	proc_t p = PROC_NULL;

	if (pid == 0) {
		return EINVAL;
	}

	p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}

	/*
	 * Only allow this on the current proc for now.
	 * We can check for privileges and allow targeting another process in the future.
	 */
	if (p != current_proc()) {
		proc_rele(p);
		return EPERM;
	}

	proc_list_lock();
	*is_freezable = ((p->p_memstat_state & P_MEMSTAT_FREEZE_DISABLED) ? 0 : 1);
	proc_rele(p);
	proc_list_unlock();

	return 0;
}

errno_t
memorystatus_get_process_is_frozen(pid_t pid, int *is_frozen)
{
	proc_t p = PROC_NULL;

	if (pid == 0) {
		return EINVAL;
	}

	/*
	 * Only allow this on the current proc for now.
	 * We can check for privileges and allow targeting another process in the future.
	 */
	p = current_proc();
	if (proc_getpid(p) != pid) {
		return EPERM;
	}

	proc_list_lock();
	*is_frozen = _memstat_proc_is_frozen(p);
	proc_list_unlock();

	return 0;
}

int
memorystatus_set_process_is_freezable(pid_t pid, boolean_t is_freezable)
{
	proc_t p = PROC_NULL;

	if (pid == 0) {
		return EINVAL;
	}

	/*
	 * To enable freezable status, you need to be root or an entitlement.
	 */
	if (is_freezable &&
	    !kauth_cred_issuser(kauth_cred_get()) &&
	    !IOCurrentTaskHasEntitlement(MEMORYSTATUS_ENTITLEMENT)) {
		return EPERM;
	}

	p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}

	/*
	 * A process can change its own status. A coalition leader can
	 * change the status of coalition members.
	 * An entitled process (or root) can change anyone's status.
	 */
	if (p != current_proc() &&
	    !kauth_cred_issuser(kauth_cred_get()) &&
	    !IOCurrentTaskHasEntitlement(MEMORYSTATUS_ENTITLEMENT)) {
		coalition_t coal = task_get_coalition(proc_task(p), COALITION_TYPE_JETSAM);
		if (!coalition_is_leader(proc_task(current_proc()), coal)) {
			proc_rele(p);
			return EPERM;
		}
	}

	proc_list_lock();
	if (is_freezable == FALSE) {
		/* Freeze preference set to FALSE. Set the P_MEMSTAT_FREEZE_DISABLED bit. */
		p->p_memstat_state |= P_MEMSTAT_FREEZE_DISABLED;
		memorystatus_log_info("memorystatus_set_process_is_freezable: disabling freeze for pid %d [%s]\n",
		    proc_getpid(p), (*p->p_name ? p->p_name : "unknown"));
	} else {
		p->p_memstat_state &= ~P_MEMSTAT_FREEZE_DISABLED;
		memorystatus_log_info("memorystatus_set_process_is_freezable: enabling freeze for pid %d [%s]\n",
		    proc_getpid(p), (*p->p_name ? p->p_name : "unknown"));
	}
	proc_rele(p);
	proc_list_unlock();

	return 0;
}

/*
 * Called when process is created before it is added to a memorystatus bucket.
 */
void
memorystatus_freeze_init_proc(proc_t p)
{
	/* NB: Process is not on the memorystatus lists yet so it's safe to modify the skip reason without the freezer mutex. */
	if (memorystatus_freeze_budget_pages_remaining == 0) {
		p->p_memstat_freeze_skip_reason = kMemorystatusFreezeSkipReasonOutOfBudget;
	} else if ((memorystatus_frozen_count >= memorystatus_frozen_processes_max)) {
		p->p_memstat_freeze_skip_reason = kMemorystatusFreezeSkipReasonOutOfSlots;
	} else {
		p->p_memstat_freeze_skip_reason = kMemorystatusFreezeSkipReasonNone;
	}
}


static int
sysctl_memorystatus_do_fastwake_warmup_all  SYSCTL_HANDLER_ARGS
{
#pragma unused(oidp, arg1, arg2)

	if (!req->newptr) {
		return EINVAL;
	}

	/* Need to be root or have entitlement */
	if (!kauth_cred_issuser(kauth_cred_get()) && !IOCurrentTaskHasEntitlement( MEMORYSTATUS_ENTITLEMENT)) {
		return EPERM;
	}

	if (memorystatus_freeze_enabled == false) {
		return ENOTSUP;
	}

	if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
		return ENOTSUP;
	}

	do_fastwake_warmup_all();

	return 0;
}

SYSCTL_PROC(_kern, OID_AUTO, memorystatus_do_fastwake_warmup_all, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED,
    0, 0, &sysctl_memorystatus_do_fastwake_warmup_all, "I", "");

/*
 * Takes in a candidate list from the user_addr, validates it, and copies it into the list pointer.
 * Takes ownership over the original value of list.
 * Assumes that list is protected by the freezer_mutex.
 * The caller should not hold any locks.
 */
static errno_t
set_freezer_candidate_list(user_addr_t buffer, size_t buffer_size, struct memorystatus_freezer_candidate_list *list)
{
	errno_t error = 0;
	memorystatus_properties_freeze_entry_v1 *entries = NULL, *tmp_entries = NULL;
	size_t entry_count = 0, entries_size = 0, tmp_size = 0;

	/* Validate the user provided list. */
	if ((buffer == USER_ADDR_NULL) || (buffer_size == 0)) {
		memorystatus_log_error("memorystatus_cmd_grp_set_freeze_priority: NULL or empty list\n");
		return EINVAL;
	}

	if (buffer_size % sizeof(memorystatus_properties_freeze_entry_v1) != 0) {
		memorystatus_log_error(
			"memorystatus_cmd_grp_set_freeze_priority: Invalid list length (caller might have comiled agsinst invalid headers.)\n");
		return EINVAL;
	}

	entry_count = buffer_size / sizeof(memorystatus_properties_freeze_entry_v1);
	entries_size = buffer_size;
	entries = kalloc_data(buffer_size, Z_WAITOK | Z_ZERO);
	if (entries == NULL) {
		return ENOMEM;
	}

	error = copyin(buffer, entries, buffer_size);
	if (error != 0) {
		goto out;
	}

#if MACH_ASSERT
	for (size_t i = 0; i < entry_count; i++) {
		memorystatus_properties_freeze_entry_v1 *entry = &entries[i];
		if (entry->version != 1) {
			memorystatus_log_error("memorystatus_cmd_grp_set_freeze_priority: Invalid entry version number.");
			error = EINVAL;
			goto out;
		}
		if (i > 0 && entry->priority >= entries[i - 1].priority) {
			memorystatus_log_error("memorystatus_cmd_grp_set_freeze_priority: Entry list is not in descending order.");
			error = EINVAL;
			goto out;
		}
	}
#endif /* MACH_ASSERT */

	lck_mtx_lock(&freezer_mutex);

	tmp_entries = list->mfcl_list;
	tmp_size = list->mfcl_length * sizeof(memorystatus_properties_freeze_entry_v1);
	list->mfcl_list = entries;
	list->mfcl_length = entry_count;

	lck_mtx_unlock(&freezer_mutex);

	entries = tmp_entries;
	entries_size = tmp_size;

out:
	kfree_data(entries, entries_size);
	return error;
}

errno_t
memorystatus_cmd_grp_set_freeze_list(user_addr_t buffer, size_t buffer_size)
{
	return set_freezer_candidate_list(buffer, buffer_size, &memorystatus_global_freeze_list);
}

errno_t
memorystatus_cmd_grp_set_demote_list(user_addr_t buffer, size_t buffer_size)
{
	return set_freezer_candidate_list(buffer, buffer_size, &memorystatus_global_demote_list);
}

void
memorystatus_freezer_mark_ui_transition(proc_t p)
{
	bool frozen = false, previous_focal_thaw = false, xpc_service = false, suspended = false;
	proc_list_lock();

	if (isSysProc(p)) {
		goto out;
	}

	frozen = _memstat_proc_is_frozen(p);
	previous_focal_thaw = (p->p_memstat_state & P_MEMSTAT_FROZEN_FOCAL_THAW) != 0;
	xpc_service = (p->p_memstat_state & P_MEMSTAT_FROZEN_XPC_SERVICE) != 0;
	suspended = (p->p_memstat_state & P_MEMSTAT_SUSPENDED) != 0;
	if (!suspended) {
		if (frozen) {
			if (!previous_focal_thaw) {
				p->p_memstat_state |= P_MEMSTAT_FROZEN_FOCAL_THAW;
				os_atomic_inc(&(memorystatus_freezer_stats.mfs_processes_thawed_fg), relaxed);
				if (xpc_service) {
					os_atomic_inc(&(memorystatus_freezer_stats.mfs_processes_thawed_fg_xpc_service), relaxed);
				}
			}
		}
		os_atomic_inc(&(memorystatus_freezer_stats.mfs_processes_fg_resumed), relaxed);
	}

out:
	proc_list_unlock();
}

#endif /* CONFIG_FREEZE */