DragonFlyBSD Kernel Audit
sys/kern/usched_dfly.c
← back
   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
/*
 * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>.  All rights reserved.
 * Copyright (c) 2012-2020 The DragonFly Project.  All rights reserved.
 *
 * This code is derived from software contributed to The DragonFly Project
 * by Matthew Dillon <dillon@backplane.com>,
 * by Mihai Carabas <mihai.carabas@gmail.com>
 * and many others.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name of The DragonFly Project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific, prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/queue.h>
#include <sys/proc.h>
#include <sys/rtprio.h>
#include <sys/uio.h>
#include <sys/sysctl.h>
#include <sys/resourcevar.h>
#include <sys/spinlock.h>
#include <sys/cpu_topology.h>
#include <sys/thread2.h>
#include <sys/spinlock2.h>

#include <sys/ktr.h>

#include <machine/cpu.h>
#include <machine/smp.h>

#include <sys/usched_dfly.h>

/*static void dfly_acquire_curproc(struct lwp *lp); see sys/usched.h */
static void dfly_release_curproc(struct lwp *lp);
static void dfly_select_curproc(globaldata_t gd);
static void dfly_setrunqueue(struct lwp *lp);
static void dfly_setrunqueue_dd(dfly_pcpu_t rdd, struct lwp *lp);
static void dfly_schedulerclock(struct lwp *lp, sysclock_t period,
				sysclock_t cpstamp);
static void dfly_recalculate_estcpu(struct lwp *lp);
static void dfly_resetpriority(struct lwp *lp);
static void dfly_forking(struct lwp *plp, struct lwp *lp);
static void dfly_exiting(struct lwp *lp, struct proc *);
static void dfly_uload_update(struct lwp *lp);
static void dfly_yield(struct lwp *lp);
static void dfly_changeqcpu_locked(struct lwp *lp,
				dfly_pcpu_t dd, dfly_pcpu_t rdd);
static dfly_pcpu_t dfly_choose_best_queue(struct lwp *lp);
static dfly_pcpu_t dfly_choose_worst_queue(dfly_pcpu_t dd, int forceit);
static dfly_pcpu_t dfly_choose_queue_simple(dfly_pcpu_t dd, struct lwp *lp);
static void dfly_need_user_resched_remote(void *dummy);
static struct lwp *dfly_chooseproc_locked(dfly_pcpu_t rdd, dfly_pcpu_t dd,
					  struct lwp *chklp, int worst);
static void dfly_remrunqueue_locked(dfly_pcpu_t dd, struct lwp *lp);
static void dfly_setrunqueue_locked(dfly_pcpu_t dd, struct lwp *lp);
static void dfly_changedcpu(struct lwp *lp);

struct usched usched_dfly = {
	{ NULL },
	"dfly", "Original DragonFly Scheduler",
	NULL,			/* default registration */
	NULL,			/* default deregistration */
	dfly_acquire_curproc,
	dfly_release_curproc,
	dfly_setrunqueue,
	dfly_schedulerclock,
	dfly_recalculate_estcpu,
	dfly_resetpriority,
	dfly_forking,
	dfly_exiting,
	dfly_uload_update,
	NULL,			/* setcpumask not supported */
	dfly_yield,
	dfly_changedcpu
};

/*
 * We have NQS (32) run queues per scheduling class.  For the normal
 * class, there are 128 priorities scaled onto these 32 queues.  New
 * processes are added to the last entry in each queue, and processes
 * are selected for running by taking them from the head and maintaining
 * a simple FIFO arrangement.  Realtime and Idle priority processes have
 * and explicit 0-31 priority which maps directly onto their class queue
 * index.  When a queue has something in it, the corresponding bit is
 * set in the queuebits variable, allowing a single read to determine
 * the state of all 32 queues and then a ffs() to find the first busy
 * queue.
 *
 * curprocmask is used to publish cpus with assigned curprocs to the rest
 * of the cpus.  In certain situations curprocmask may leave a bit set
 * (e.g. a yield or a token-based yield) even though dd->uschedcp is
 * NULL'd out temporarily).
 */
					/* currently running a user process */
static cpumask_t dfly_curprocmask = CPUMASK_INITIALIZER_ALLONES;
static cpumask_t dfly_rdyprocmask;	/* ready to accept a user process */
static struct usched_dfly_pcpu dfly_pcpu[MAXCPU];
static struct sysctl_ctx_list usched_dfly_sysctl_ctx;
static struct sysctl_oid *usched_dfly_sysctl_tree;
static struct lock usched_dfly_config_lk = LOCK_INITIALIZER("usdfs", 0, 0);

/* Debug info exposed through debug.* sysctl */

static int usched_dfly_debug = -1;
SYSCTL_INT(_debug, OID_AUTO, dfly_scdebug, CTLFLAG_RW,
	   &usched_dfly_debug, 0,
	   "Print debug information for this pid");

static int usched_dfly_pid_debug = -1;
SYSCTL_INT(_debug, OID_AUTO, dfly_pid_debug, CTLFLAG_RW,
	   &usched_dfly_pid_debug, 0,
	   "Print KTR debug information for this pid");

static int usched_dfly_chooser = 0;
SYSCTL_INT(_debug, OID_AUTO, dfly_chooser, CTLFLAG_RW,
	   &usched_dfly_chooser, 0,
	   "Print KTR debug information for this pid");

/*
 * WARNING!
 *
 * The fork bias can have a large effect on the system in the face of a
 * make -j N or other high-forking applications.
 *
 * Larger values are much less invasive vs other things that
 * might be running in the system, but can cause exec chains
 * such as those typically generated by make to have higher
 * latencies in the face of modest load.
 *
 * Lower values are more invasive but have reduced latencies
 * for such exec chains.
 *
 *	make -j 10 buildkernel example, build times:
 *
 *	     +0	3:04
 *	     +1 3:14	-5.2%	<-- default
 *	     +2 3:22	-8.9%
 *
 * This issue occurs due to the way the scheduler affinity heuristics work.
 * There is no way to really 'fix' the affinity heuristics because when it
 * comes right down to it trying to instantly schedule a process on an
 * available cpu (even if it will become unavailable a microsecond later)
 * tends to cause processes to shift around between cpus and sockets too much
 * and breaks the affinity.
 *
 * NOTE: Heavily concurrent builds typically have enough things on the pan
 *	 that they remain time-efficient even with a higher bias.
 */
static int usched_dfly_forkbias = 1;
SYSCTL_INT(_debug, OID_AUTO, dfly_forkbias, CTLFLAG_RW,
	   &usched_dfly_forkbias, 0,
	   "Fork bias for estcpu in whole queues");

/*
 * Tunning usched_dfly - configurable through kern.usched_dfly.
 *
 * weight1 - Tries to keep threads on their current cpu.  If you
 *	     make this value too large the scheduler will not be
 *	     able to load-balance large loads.
 *
 *	     Generally set to a fairly low value, but high enough
 *	     such that estcpu jitter doesn't move threads around.
 *
 * weight2 - If non-zero, detects thread pairs undergoing synchronous
 *	     communications and tries to move them closer together.
 *	     The weight advantages the same package and socket and
 *	     disadvantages the same core and same cpu.
 *
 *	     WARNING!  Weight2 is a ridiculously sensitive parameter,
 *	     particularly against weight4.  change the default at your
 *	     peril.
 *
 * weight3 - Weighting based on the number of recently runnable threads
 *	     on the userland scheduling queue (ignoring their loads).
 *
 *	     A nominal value here prevents high-priority (low-load)
 *	     threads from accumulating on one cpu core when other
 *	     cores are available.
 *
 *	     This value should be left fairly small because low-load
 *	     high priority threads can still be mostly idle and too
 *	     high a value will kick cpu-bound processes off the cpu
 *	     unnecessarily.
 *
 * weight4 - Weighting based on availability of other logical cpus running
 *	     less important threads (by upri) than the thread we are trying
 *	     to schedule.
 *
 *	     This allows a thread to migrate to another nearby cpu if it
 *	     is unable to run on the current cpu based on the other cpu
 *	     being idle or running a less important (higher lwp_priority)
 *	     thread.  This value should be large enough to override weight1,
 *	     but not so large as to override weight2.
 *
 *	     This parameter generally ensures fairness at the cost of some
 *	     performance (if set to too high).  It should generally be just
 *	     a tad lower than weight2.
 *
 * weight5 - Weighting based on the relative amount of ram connected
 *	     to the node a cpu resides on.
 *
 *	     This value should remain fairly low to allow assymetric
 *	     NUMA nodes to get threads scheduled to them.  Setting a very
 *	     high level will prevent scheduling on assymetric NUMA nodes
 *	     with low amounts of directly-attached memory.
 *
 *	     Note that when testing e.g. N threads on a machine with N
 *	     cpu cores with assymtric NUMA nodes, a non-zero value will
 *	     cause some cpu threads on the low-priority NUMA nodes to remain
 *	     idle even when a few process threads are doubled-up on other
 *	     cpus.  But this is typically more ideal because it deschedules
 *	     low-priority NUMA nodes at lighter nodes.
 *
 *	     Values between 50 and 200 are recommended.  Default is 50.
 *
 * weight6 - rdd transfer weight hysteresis for regular pair rebalancing
 *	     (feature 0x04).
 *
 *	     Defaults to 0, can be increased to improve stabillity at the
 *	     cost of more mis-schedules.
 *
 * weight7 - rdd transfer weight hysteresis for idle cpu 'pull' (feature 0x01).
 *
 *	     Defaults to -100 to strongly promote a transfer.
 *
 * ipc_smt - If enabled, advantage IPC pairing to sibling cpu threads.
 *	     If -1, automatic when load >= 1/2 ncpus (default).
 *
 * ipc_same- If enabled, advantage IPC pairing to the same logical cpu.
 *	     If -1, automatic when load >= ncpus (default).
 *
 * features - These flags can be set or cleared to enable or disable various
 *	      features.
 *
 *	      0x01	Enable idle-cpu pulling			(default)
 *	      0x02	Enable proactive pushing		(default)
 *	      0x04	Enable rebalancing rover		(default)
 *	      0x08	Enable more proactive pushing		(default)
 *	      0x10	(unassigned)
 *	      0x20	choose best cpu for forked process	(default)
 *	      0x40	choose current cpu for forked process
 *	      0x80	choose random cpu for forked process
 *
 *	     NOTE - The idea behind forking mechanic 0x20 is that most
 *		    fork()ing is either followed by an exec in the child,
 *		    or the parent wait*()s.  If the child is short-lived,
 *		    there is effectively an IPC dependency (td_wakefromcpu
 *		    is also set in kern_fork.c) and we want to implement
 *		    the weight2 behavior to reduce IPIs and to reduce CPU
 *		    cache ping-ponging.
 */
__read_mostly static int usched_dfly_smt = 0;
__read_mostly static int usched_dfly_cache_coherent = 0;
__read_mostly static int usched_dfly_weight1 = 30;  /* keep thread on cpu */
__read_mostly static int usched_dfly_weight2 = 180; /* IPC locality */
__read_mostly static int usched_dfly_weight3 = 10;  /* threads on queue */
__read_mostly static int usched_dfly_weight4 = 120; /* availability of cores */
__read_mostly static int usched_dfly_weight5 = 50;  /* node attached memory */
__read_mostly static int usched_dfly_weight6 = 0;   /* 0x04 transfer weight */
__read_mostly static int usched_dfly_weight7 = -100;/* 0x01 transfer weight */
__read_mostly static int usched_dfly_features = 0x2f;	     /* allow pulls */
__read_mostly static int usched_dfly_fast_resched = PPQ / 2; /* delta pri */
__read_mostly static int usched_dfly_swmask = ~PPQMASK;	     /* allow pulls */
__read_mostly static int usched_dfly_rrinterval = (ESTCPUFREQ + 9) / 10;
__read_mostly static int usched_dfly_decay = 8;
__read_mostly static int usched_dfly_ipc_smt = -1;  /* IPC auto smt pair */
__read_mostly static int usched_dfly_ipc_same = -1; /* IPC auto same log cpu */
__read_mostly static int usched_dfly_poll_ticks = 1; /* helper polling ticks */
__read_mostly static long usched_dfly_node_mem;

/* KTR debug printings */

KTR_INFO_MASTER(usched);

#if !defined(KTR_USCHED_DFLY)
#define	KTR_USCHED_DFLY	KTR_ALL
#endif

KTR_INFO(KTR_USCHED_DFLY, usched, chooseproc, 0,
    "USCHED_DFLY(chooseproc: pid %d, old_cpuid %d, curr_cpuid %d)",
    pid_t pid, int old_cpuid, int curr);

/*
 * This function is called when the kernel intends to return to userland.
 * It is responsible for making the thread the current designated userland
 * thread for this cpu, blocking if necessary.
 *
 * The kernel will not depress our LWKT priority until after we return,
 * in case we have to shove over to another cpu.
 *
 * We must determine our thread's disposition before we switch away.  This
 * is very sensitive code.
 *
 * WARNING! THIS FUNCTION IS ALLOWED TO CAUSE THE CURRENT THREAD TO MIGRATE
 * TO ANOTHER CPU!  Because most of the kernel assumes that no migration will
 * occur, this function is called only under very controlled circumstances.
 */
void
dfly_acquire_curproc(struct lwp *lp)
{
	globaldata_t gd;
	dfly_pcpu_t dd;
	dfly_pcpu_t rdd;
	thread_t td;
	int force_resched;

	td = lp->lwp_thread;
	gd = mycpu;
	dd = &dfly_pcpu[gd->gd_cpuid];

	/*
	 * Quickly return if possible.
	 */
	if (__predict_true((td->td_flags & TDF_TSLEEPQ) == 0 &&
			   !sched_action_wanted_gd(gd) &&
			   dd->uschedcp == lp)) {
		return;
	}

	/*
	 * Make sure we aren't sitting on a tsleep queue.
	 */
	crit_enter_quick(td);
	if (td->td_flags & TDF_TSLEEPQ)
		tsleep_remove(td);
	dfly_recalculate_estcpu(lp);

	/*
	 * Process any pending interrupts/ipi's, then handle reschedule
	 * requests.  dfly_release_curproc() will try to assign a new
	 * uschedcp that isn't us and otherwise NULL it out.
	 */
	force_resched = 0;
	if (user_resched_wanted()) {
		if (dd->uschedcp == lp)
			force_resched = 1;
		clear_user_resched();
		dfly_release_curproc(lp);
	}

	/*
	 * Loop until we are the current user thread.
	 *
	 * NOTE: dd spinlock not held at top of loop.
	 */
	if (dd->uschedcp == lp)
		lwkt_yield_quick();

	while (dd->uschedcp != lp) {
		/*
		 * Do not do a lwkt_yield_quick() here as it will prevent
		 * the lwp from being placed on the dfly_bsd runqueue for
		 * one cycle (possibly an entire round-robin), preventing
		 * it from being scheduled to another cpu.
		 */
		/* lwkt_yield_quick(); */

		if (usched_dfly_debug == lp->lwp_proc->p_pid)
			kprintf(" pid %d acquire curcpu %d (force %d) ",
				lp->lwp_proc->p_pid, gd->gd_cpuid,
				force_resched);


		spin_lock(&dd->spin);

		/* This lwp is an outcast; force reschedule. */
		if (__predict_false(
		    CPUMASK_TESTBIT(lp->lwp_cpumask, gd->gd_cpuid) == 0) &&
		    (rdd = dfly_choose_best_queue(lp)) != dd) {
			dfly_changeqcpu_locked(lp, dd, rdd);
			spin_unlock(&dd->spin);
			lwkt_deschedule(lp->lwp_thread);
			dfly_setrunqueue_dd(rdd, lp);
			lwkt_switch();
			gd = mycpu;
			dd = &dfly_pcpu[gd->gd_cpuid];
			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("SEL-A cpu %d\n", gd->gd_cpuid);
			continue;
		}

		/*
		 * We are not or are no longer the current lwp and a forced
		 * reschedule was requested.  Figure out the best cpu to
		 * run on (our current cpu will be given significant weight).
		 *
		 * Doing this on many cpus simultaneously leads to
		 * instability so pace the operation.
		 *
		 * (if a reschedule was not requested we want to move this
		 * step after the uschedcp tests).
		 */
		if (force_resched &&
		   (usched_dfly_features & 0x08) &&
		   (u_int)sched_ticks / 8 % ncpus == gd->gd_cpuid) {
			if ((rdd = dfly_choose_best_queue(lp)) != dd) {
				dfly_changeqcpu_locked(lp, dd, rdd);
				spin_unlock(&dd->spin);
				lwkt_deschedule(lp->lwp_thread);
				dfly_setrunqueue_dd(rdd, lp);
				lwkt_switch();
				gd = mycpu;
				dd = &dfly_pcpu[gd->gd_cpuid];
				if (usched_dfly_debug == lp->lwp_proc->p_pid)
					kprintf("SEL-B cpu %d\n", gd->gd_cpuid);
				continue;
			}
			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("(SEL-B same cpu) ");
		}

		/*
		 * Either no reschedule was requested or the best queue was
		 * dd, and no current process has been selected.  We can
		 * trivially become the current lwp on the current cpu.
		 */
		if (dd->uschedcp == NULL) {
			atomic_clear_int(&lp->lwp_thread->td_mpflags,
					 TDF_MP_DIDYIELD);
			if ((dd->flags & DFLY_PCPU_CURMASK) == 0) {
				ATOMIC_CPUMASK_ORBIT(dfly_curprocmask,
						     gd->gd_cpuid);
				dd->flags |= DFLY_PCPU_CURMASK;
			}
			dd->uschedcp = lp;
			dd->upri = lp->lwp_priority;
			KKASSERT(lp->lwp_qcpu == dd->cpuid);
			spin_unlock(&dd->spin);
			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("SEL-C cpu %d (same cpu)\n",
					gd->gd_cpuid);
			break;
		}

		/*
		 * Can we steal the current designated user thread?
		 *
		 * If we do the other thread will stall when it tries to
		 * return to userland, possibly rescheduling elsewhere.
		 * Set need_user_resched() to get the thread to cycle soonest.
		 *
		 * It is important to do a masked test to avoid the edge
		 * case where two near-equal-priority threads are constantly
		 * interrupting each other.
		 *
		 * In the exact match case another thread has already gained
		 * uschedcp and lowered its priority, if we steal it the
		 * other thread will stay stuck on the LWKT runq and not
		 * push to another cpu.  So don't steal on equal-priority even
		 * though it might appear to be more beneficial due to not
		 * having to switch back to the other thread's context.
		 *
		 * usched_dfly_fast_resched requires that two threads be
		 * significantly far apart in priority in order to interrupt.
		 *
		 * If better but not sufficiently far apart, the current
		 * uschedcp will be interrupted at the next scheduler clock.
		 */
		if (dd->uschedcp &&
		   (dd->upri & ~PPQMASK) >
		   (lp->lwp_priority & ~PPQMASK) + usched_dfly_fast_resched) {
			dd->uschedcp = lp;
			dd->upri = lp->lwp_priority;
			KKASSERT(lp->lwp_qcpu == dd->cpuid);
			need_user_resched();
			spin_unlock(&dd->spin);
			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("SEL-D cpu %d (same cpu)\n",
					gd->gd_cpuid);
			break;
		}

		/*
		 * Requeue us at lwp_priority, which recalculate_estcpu()
		 * set for us.  Reset the rrcount to force placement
		 * at the end of the queue.
		 *
		 * We used to move ourselves to the worst queue, but
		 * this creates a fairly serious priority inversion
		 * problem.
		 */
		if (lp->lwp_thread->td_mpflags & TDF_MP_DIDYIELD) {
			spin_unlock(&dd->spin);
			lp->lwp_rrcount = usched_dfly_rrinterval;
			lp->lwp_rqindex = (lp->lwp_priority & PRIMASK) / PPQ;

			lwkt_deschedule(lp->lwp_thread);
			dfly_setrunqueue_dd(dd, lp);
			atomic_clear_int(&lp->lwp_thread->td_mpflags,
					 TDF_MP_DIDYIELD);
			lwkt_switch();
			gd = mycpu;
			dd = &dfly_pcpu[gd->gd_cpuid];
			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("SEL-E cpu %d (requeue)\n",
					gd->gd_cpuid);
			continue;
		}

		/*
		 * We are not the current lwp, figure out the best cpu
		 * to run on (our current cpu will be given significant
		 * weight).  Loop on cpu change.
		 */
		if ((usched_dfly_features & 0x02) &&
		    force_resched == 0 &&
		    (rdd = dfly_choose_best_queue(lp)) != dd) {
			dfly_changeqcpu_locked(lp, dd, rdd);
			spin_unlock(&dd->spin);
			lwkt_deschedule(lp->lwp_thread);
			dfly_setrunqueue_dd(rdd, lp);
			lwkt_switch();
			gd = mycpu;
			dd = &dfly_pcpu[gd->gd_cpuid];
			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("SEL-F cpu %d (requeue new cpu)\n",
					gd->gd_cpuid);
			continue;
		}

		/*
		 * We cannot become the current lwp, place the lp on the
		 * run-queue of this or another cpu and deschedule ourselves.
		 *
		 * When we are reactivated we will have another chance.
		 *
		 * Reload after a switch or setrunqueue/switch possibly
		 * moved us to another cpu.
		 */
		spin_unlock(&dd->spin);
		lwkt_deschedule(lp->lwp_thread);
		dfly_setrunqueue_dd(dd, lp);
		lwkt_switch();
		gd = mycpu;
		dd = &dfly_pcpu[gd->gd_cpuid];
		if (usched_dfly_debug == lp->lwp_proc->p_pid)
			kprintf("SEL-G cpu %d (fallback setrunq)\n",
				gd->gd_cpuid);
	}
	if (usched_dfly_debug == lp->lwp_proc->p_pid)
		kprintf(" pid %d acquire DONE cpu %d\n",
			lp->lwp_proc->p_pid, gd->gd_cpuid);

	/*
	 * Make sure upri is synchronized, then yield to LWKT threads as
	 * needed before returning.  This could result in another reschedule.
	 * XXX
	 */
	crit_exit_quick(td);

	KKASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) == 0);
}

/*
 * DFLY_RELEASE_CURPROC
 *
 * This routine detaches the current thread from the userland scheduler,
 * usually because the thread needs to run or block in the kernel (at
 * kernel priority) for a while.
 *
 * This routine is also responsible for selecting a new thread to
 * make the current thread.
 *
 * NOTE: This implementation differs from the dummy example in that
 * dfly_select_curproc() is able to select the current process, whereas
 * dummy_select_curproc() is not able to select the current process.
 * This means we have to NULL out uschedcp.
 *
 * Additionally, note that we may already be on a run queue if releasing
 * via the lwkt_switch() in dfly_setrunqueue().
 */
static void
dfly_release_curproc(struct lwp *lp)
{
	globaldata_t gd = mycpu;
	dfly_pcpu_t dd = &dfly_pcpu[gd->gd_cpuid];

	/*
	 * Make sure td_wakefromcpu is defaulted.  This will be overwritten
	 * by wakeup().
	 */
	if (dd->uschedcp == lp) {
		KKASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) == 0);
		spin_lock(&dd->spin);
		if (dd->uschedcp == lp) {
			dd->uschedcp = NULL;	/* don't let lp be selected */
			dd->upri = PRIBASE_NULL;

			/*
			 * We're just going to set it again, avoid the global
			 * cache line ping-pong.
			 */
			if ((lp->lwp_thread->td_mpflags & TDF_MP_DIDYIELD) == 0) {
				if (dd->flags & DFLY_PCPU_CURMASK) {
					ATOMIC_CPUMASK_NANDBIT(dfly_curprocmask,
							       gd->gd_cpuid);
					dd->flags &= ~DFLY_PCPU_CURMASK;
				}
			}
			spin_unlock(&dd->spin);
			dfly_select_curproc(gd);
		} else {
			spin_unlock(&dd->spin);
		}
	}
}

/*
 * DFLY_SELECT_CURPROC
 *
 * Select a new current process for this cpu and clear any pending user
 * reschedule request.  The cpu currently has no current process.
 *
 * This routine is also responsible for equal-priority round-robining,
 * typically triggered from dfly_schedulerclock().  In our dummy example
 * all the 'user' threads are LWKT scheduled all at once and we just
 * call lwkt_switch().
 *
 * The calling process is not on the queue and cannot be selected.
 */
static
void
dfly_select_curproc(globaldata_t gd)
{
	dfly_pcpu_t dd = &dfly_pcpu[gd->gd_cpuid];
	struct lwp *nlp;
	int cpuid = gd->gd_cpuid;

	crit_enter_gd(gd);

	spin_lock(&dd->spin);
	nlp = dfly_chooseproc_locked(dd, dd, dd->uschedcp, 0);

	if (nlp) {
		if ((dd->flags & DFLY_PCPU_CURMASK) == 0) {
			ATOMIC_CPUMASK_ORBIT(dfly_curprocmask, cpuid);
			dd->flags |= DFLY_PCPU_CURMASK;
		}
		dd->upri = nlp->lwp_priority;
		dd->uschedcp = nlp;
#if 0
		dd->rrcount = 0;		/* reset round robin */
#endif
		spin_unlock(&dd->spin);
		lwkt_acquire(nlp->lwp_thread);
		lwkt_schedule(nlp->lwp_thread);
	} else {
		spin_unlock(&dd->spin);
	}
	crit_exit_gd(gd);
}

/*
 * Place the specified lwp on the user scheduler's run queue.  This routine
 * must be called with the thread descheduled.  The lwp must be runnable.
 * It must not be possible for anyone else to explicitly schedule this thread.
 *
 * The thread may be the current thread as a special case.
 */
static void
dfly_setrunqueue(struct lwp *lp)
{
	dfly_pcpu_t dd;
	dfly_pcpu_t rdd;

	/*
	 * First validate the process LWKT state.
	 */
	KASSERT(lp->lwp_stat == LSRUN, ("setrunqueue: lwp not LSRUN"));
	KASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) == 0,
	    ("lwp %d/%d already on runq! flag %08x/%08x", lp->lwp_proc->p_pid,
	     lp->lwp_tid, lp->lwp_proc->p_flags, lp->lwp_flags));
	KKASSERT((lp->lwp_thread->td_flags & TDF_RUNQ) == 0);

	/*
	 * NOTE: dd/rdd do not necessarily represent the current cpu.
	 *	 Instead they may represent the cpu the thread was last
	 *	 scheduled on or inherited by its parent.
	 */
	dd = &dfly_pcpu[lp->lwp_qcpu];
	rdd = dd;

	/*
	 * This process is not supposed to be scheduled anywhere or assigned
	 * as the current process anywhere.  Assert the condition.
	 */
	KKASSERT(rdd->uschedcp != lp);

	/*
	 * Ok, we have to setrunqueue some target cpu and request a reschedule
	 * if necessary.
	 *
	 * We have to choose the best target cpu.  It might not be the current
	 * target even if the current cpu has no running user thread (for
	 * example, because the current cpu might be a hyperthread and its
	 * sibling has a thread assigned).
	 *
	 * If we just forked it is most optimal to run the child on the same
	 * cpu just in case the parent decides to wait for it (thus getting
	 * off that cpu).  As long as there is nothing else runnable on the
	 * cpu, that is.  If we did this unconditionally a parent forking
	 * multiple children before waiting (e.g. make -j N) leaves other
	 * cpus idle that could be working.
	 */
	if (lp->lwp_forked) {
		lp->lwp_forked = 0;
		if (usched_dfly_features & 0x20)
			rdd = dfly_choose_best_queue(lp);
		else if (usched_dfly_features & 0x40)
			rdd = &dfly_pcpu[lp->lwp_qcpu];
		else if (usched_dfly_features & 0x80)
			rdd = dfly_choose_queue_simple(rdd, lp);
		else if (dfly_pcpu[lp->lwp_qcpu].runqcount)
			rdd = dfly_choose_best_queue(lp);
		else
			rdd = &dfly_pcpu[lp->lwp_qcpu];
	} else {
		rdd = dfly_choose_best_queue(lp);
		/* rdd = &dfly_pcpu[lp->lwp_qcpu]; */
	}
	if (lp->lwp_qcpu != rdd->cpuid) {
		spin_lock(&dd->spin);
		dfly_changeqcpu_locked(lp, dd, rdd);
		spin_unlock(&dd->spin);
	}
	dfly_setrunqueue_dd(rdd, lp);
}

/*
 * Change qcpu to rdd->cpuid.  The dd the lp is CURRENTLY on must be
 * spin-locked on-call.  rdd does not have to be.
 */
static void
dfly_changeqcpu_locked(struct lwp *lp, dfly_pcpu_t dd, dfly_pcpu_t rdd)
{
	if (lp->lwp_qcpu != rdd->cpuid) {
		spin_lock(&lp->lwp_spin);
		if (lp->lwp_mpflags & LWP_MP_ULOAD) {
			atomic_clear_int(&lp->lwp_mpflags, LWP_MP_ULOAD);
			atomic_add_long(&dd->uload, -lp->lwp_uload);
			atomic_add_int(&dd->ucount, -1);
		}
		lp->lwp_qcpu = rdd->cpuid;
		spin_unlock(&lp->lwp_spin);
	}
}

/*
 * Place lp on rdd's runqueue.  Nothing is locked on call.  This function
 * also performs all necessary ancillary notification actions.
 */
static void
dfly_setrunqueue_dd(dfly_pcpu_t rdd, struct lwp *lp)
{
	globaldata_t rgd;

	/*
	 * We might be moving the lp to another cpu's run queue, and once
	 * on the runqueue (even if it is our cpu's), another cpu can rip
	 * it away from us.
	 *
	 * TDF_MIGRATING might already be set if this is part of a
	 * remrunqueue+setrunqueue sequence.
	 */
	if ((lp->lwp_thread->td_flags & TDF_MIGRATING) == 0)
		lwkt_giveaway(lp->lwp_thread);

	rgd = rdd->gd;

	/*
	 * We lose control of the lp the moment we release the spinlock
	 * after having placed it on the queue.  i.e. another cpu could pick
	 * it up, or it could exit, or its priority could be further
	 * adjusted, or something like that.
	 *
	 * WARNING! rdd can point to a foreign cpu!
	 */
	spin_lock(&rdd->spin);
	dfly_setrunqueue_locked(rdd, lp);

	/*
	 * Potentially interrupt the currently-running thread
	 */
	if ((rdd->upri & ~PPQMASK) <= (lp->lwp_priority & ~PPQMASK)) {
		/*
		 * Currently running thread is better or same, do not
		 * interrupt.
		 */
		spin_unlock(&rdd->spin);
	} else if ((rdd->upri & ~PPQMASK) <= (lp->lwp_priority & ~PPQMASK) +
		   usched_dfly_fast_resched) {
		/*
		 * Currently running thread is not better, but not so bad
		 * that we need to interrupt it.  Let it run for one more
		 * scheduler tick.
		 */
		if (rdd->uschedcp &&
		    rdd->uschedcp->lwp_rrcount < usched_dfly_rrinterval) {
			rdd->uschedcp->lwp_rrcount = usched_dfly_rrinterval - 1;
		}
		spin_unlock(&rdd->spin);
	} else if (rgd == mycpu) {
		/*
		 * We should interrupt the currently running thread, which
		 * is on the current cpu.  However, if DIDYIELD is set we
		 * round-robin unconditionally and do not interrupt it.
		 */
		spin_unlock(&rdd->spin);
		if (rdd->uschedcp == NULL)
			wakeup_mycpu(rdd->helper_thread); /* XXX */
		if ((lp->lwp_thread->td_mpflags & TDF_MP_DIDYIELD) == 0)
			need_user_resched();
	} else {
		/*
		 * We should interrupt the currently running thread, which
		 * is on a different cpu.
		 */
		spin_unlock(&rdd->spin);
		lwkt_send_ipiq(rgd, dfly_need_user_resched_remote, NULL);
	}
}

/*
 * This routine is called from a systimer IPI.  It MUST be MP-safe and
 * the BGL IS NOT HELD ON ENTRY.  This routine is called at ESTCPUFREQ on
 * each cpu.
 */
static
void
dfly_schedulerclock(struct lwp *lp, sysclock_t period, sysclock_t cpstamp)
{
	globaldata_t gd = mycpu;
	dfly_pcpu_t dd = &dfly_pcpu[gd->gd_cpuid];

	/*
	 * Spinlocks also hold a critical section so there should not be
	 * any active.
	 */
	KKASSERT(gd->gd_spinlocks == 0 || dumping);

	/*
	 * If lp is NULL we might be contended and lwkt_switch() may have
	 * cycled into the idle thread.  Apply the tick to the current
	 * process on this cpu if it is contended.
	 */
	if (gd->gd_curthread == &gd->gd_idlethread) {
		lp = dd->uschedcp;
		if (lp && (lp->lwp_thread == NULL ||
			   lp->lwp_thread->td_contended == 0)) {
			lp = NULL;
		}
	}

	/*
	 * Dock thread for tick
	 */
	if (lp) {
		/*
		 * Do we need to round-robin?  We round-robin 10 times a
		 * second.  This should only occur for cpu-bound batch
		 * processes.
		 */
		if (++lp->lwp_rrcount >= usched_dfly_rrinterval)
			need_user_resched();
		if ((lp->lwp_thread->td_mpflags & TDF_MP_BATCH_DEMARC) &&
		    lp->lwp_rrcount >= usched_dfly_rrinterval / 2) {
			need_user_resched();
		}

		/*
		 * Adjust estcpu upward using a real time equivalent
		 * calculation, and recalculate lp's priority.  Estcpu
		 * is increased such that it will cap-out over a period
		 * of one second.
		 */
		lp->lwp_estcpu = ESTCPULIM(lp->lwp_estcpu +
					   ESTCPUMAX / ESTCPUFREQ + 1);
		dfly_resetpriority(lp);
	}

	/*
	 * Rebalance two cpus every 8 ticks, pulling the worst thread
	 * from the worst cpu's queue into a rotating cpu number.
	 * Also require that the moving of the highest-load thread
	 * from rdd to dd does not cause the uload to cross over.
	 *
	 * This mechanic is needed because the push algorithms can
	 * steady-state in an non-optimal configuration.  We need to mix it
	 * up a little, even if it means breaking up a paired thread, so
	 * the push algorithms can rebalance the degenerate conditions.
	 * This portion of the algorithm exists to ensure stability at the
	 * selected weightings.
	 *
	 * Because we might be breaking up optimal conditions we do not want
	 * to execute this too quickly, hence we only rebalance approximately
	 * ~7-8 times per second.  The push's, on the otherhand, are capable
	 * moving threads to other cpus at a much higher rate.
	 *
	 * We choose the most heavily loaded thread from the worst queue
	 * in order to ensure that multiple heavy-weight threads on the same
	 * queue get broken up, and also because these threads are the most
	 * likely to be able to remain in place.  Hopefully then any pairings,
	 * if applicable, migrate to where these threads are.
	 */
	if ((usched_dfly_features & 0x04) &&
	    ((u_int)sched_ticks & 7) == 0 &&
	    (u_int)sched_ticks / 8 % ncpus == gd->gd_cpuid) {
		/*
		 * Our cpu is up.
		 */
		struct lwp *nlp;
		dfly_pcpu_t rdd;

		rdd = dfly_choose_worst_queue(dd, 1);
		if (rdd && dd->uload + usched_dfly_weight6 / 2 < rdd->uload) {
			spin_lock(&dd->spin);
			if (spin_trylock(&rdd->spin)) {
				nlp = dfly_chooseproc_locked(rdd, dd, NULL, 1);
				spin_unlock(&rdd->spin);
				if (nlp == NULL)
					spin_unlock(&dd->spin);
			} else {
				spin_unlock(&dd->spin);
				nlp = NULL;
			}
		} else {
			nlp = NULL;
		}
		/* dd->spin held if nlp != NULL */

		/*
		 * Either schedule it or add it to our queue.
		 */
		if (nlp &&
		    (nlp->lwp_priority & ~PPQMASK) < (dd->upri & ~PPQMASK)) {
			if ((dd->flags & DFLY_PCPU_CURMASK) == 0) {
				ATOMIC_CPUMASK_ORMASK(dfly_curprocmask,
						      dd->cpumask);
				dd->flags |= DFLY_PCPU_CURMASK;
			}
			dd->upri = nlp->lwp_priority;
			dd->uschedcp = nlp;
#if 0
			dd->rrcount = 0;	/* reset round robin */
#endif
			spin_unlock(&dd->spin);
			lwkt_acquire(nlp->lwp_thread);
			lwkt_schedule(nlp->lwp_thread);
		} else if (nlp) {
			dfly_setrunqueue_locked(dd, nlp);
			spin_unlock(&dd->spin);
		}
	}
}

/*
 * Called from acquire and from kern_synch's one-second timer (one of the
 * callout helper threads) with a critical section held.
 *
 * Adjust p_estcpu based on our single-cpu load, p_nice, and compensate for
 * overall system load.
 *
 * Note that no recalculation occurs for a process which sleeps and wakes
 * up in the same tick.  That is, a system doing thousands of context
 * switches per second will still only do serious estcpu calculations
 * ESTCPUFREQ times per second.
 */
static
void
dfly_recalculate_estcpu(struct lwp *lp)
{
	globaldata_t gd = mycpu;
	sysclock_t cpbase;
	sysclock_t ttlticks;
	int estcpu;
	int decay_factor;
	int ucount;

	/*
	 * We have to subtract periodic to get the last schedclock
	 * timeout time, otherwise we would get the upcoming timeout.
	 * Keep in mind that a process can migrate between cpus and
	 * while the scheduler clock should be very close, boundary
	 * conditions could lead to a small negative delta.
	 */
	cpbase = gd->gd_schedclock.time - gd->gd_schedclock.periodic;

	if (lp->lwp_slptime > 1) {
		/*
		 * Too much time has passed, do a coarse correction.
		 */
		lp->lwp_estcpu = lp->lwp_estcpu >> 1;
		dfly_resetpriority(lp);
		lp->lwp_cpbase = cpbase;
		lp->lwp_cpticks = 0;
		lp->lwp_estfast = 0;
	} else if (lp->lwp_cpbase != cpbase) {
		/*
		 * Adjust estcpu if we are in a different tick.  Don't waste
		 * time if we are in the same tick.
		 *
		 * First calculate the number of ticks in the measurement
		 * interval.  The ttlticks calculation can wind up 0 due to
		 * a bug in the handling of lwp_slptime  (as yet not found),
		 * so make sure we do not get a divide by 0 panic.
		 */
		ttlticks = (cpbase - lp->lwp_cpbase) /
			   gd->gd_schedclock.periodic;
		if ((ssysclock_t)ttlticks < 0) {
			ttlticks = 0;
			lp->lwp_cpbase = cpbase;
		}
		if (ttlticks < 4)
			return;
		updatepcpu(lp, lp->lwp_cpticks, ttlticks);

		/*
		 * Calculate instant estcpu based percentage of (one) cpu
		 * used and exponentially average it into the current
		 * lwp_estcpu.
		 */
		ucount = dfly_pcpu[lp->lwp_qcpu].ucount;
		estcpu = lp->lwp_cpticks * ESTCPUMAX / ttlticks;

		/*
		 * The higher ttlticks gets, the more meaning the calculation
		 * has and the smaller our decay_factor in the exponential
		 * average.
		 *
		 * The uload calculation has been removed because it actually
		 * makes things worse, causing processes which use less cpu
		 * (such as a browser) to be pumped up and treated the same
		 * as a cpu-bound process (such as a make).  The same effect
		 * can occur with sufficient load without the uload
		 * calculation, but occurs less quickly and takes more load.
		 * In addition, the less cpu a process uses the smaller the
		 * effect of the overload.
		 */
		if (ttlticks >= hz)
			decay_factor = 1;
		else
			decay_factor = hz - ttlticks;

		lp->lwp_estcpu = ESTCPULIM(
				(lp->lwp_estcpu * ttlticks + estcpu) /
				(ttlticks + 1));
		dfly_resetpriority(lp);
		lp->lwp_cpbase += ttlticks * gd->gd_schedclock.periodic;
		lp->lwp_cpticks = 0;
	}
}

/*
 * Compute the priority of a process when running in user mode.
 * Arrange to reschedule if the resulting priority is better
 * than that of the current process.
 *
 * This routine may be called with any process.
 *
 * This routine is called by fork1() for initial setup with the process of
 * the run queue, and also may be called normally with the process on or
 * off the run queue.
 */
static void
dfly_resetpriority(struct lwp *lp)
{
	dfly_pcpu_t rdd;
	int newpriority;
	u_short newrqtype;
	int rcpu;
	int checkpri;
	int estcpu;
	int delta_uload;

	crit_enter();

	/*
	 * Lock the scheduler (lp) belongs to.  This can be on a different
	 * cpu.  Handle races.  This loop breaks out with the appropriate
	 * rdd locked.
	 */
	for (;;) {
		rcpu = lp->lwp_qcpu;
		cpu_ccfence();
		rdd = &dfly_pcpu[rcpu];
		spin_lock(&rdd->spin);
		if (rcpu == lp->lwp_qcpu)
			break;
		spin_unlock(&rdd->spin);
	}

	/*
	 * Calculate the new priority and queue type
	 */
	newrqtype = lp->lwp_rtprio.type;

	switch(newrqtype) {
	case RTP_PRIO_REALTIME:
	case RTP_PRIO_FIFO:
		newpriority = PRIBASE_REALTIME +
			     (lp->lwp_rtprio.prio & PRIMASK);
		break;
	case RTP_PRIO_NORMAL:
		/*
		 * Calculate the new priority.
		 *
		 * nice contributes up to NICE_QS queues (typ 32 - full range)
		 * estcpu contributes up to EST_QS queues (typ 24)
		 *
		 * A nice +20 process receives 1/10 cpu vs nice+0.  Niced
		 * process more than 20 apart may receive no cpu, so cpu
		 * bound nice -20 can prevent a nice +5 from getting any
		 * cpu.  A nice+0, being in the middle, always gets some cpu
		 * no matter what.
		 */
		estcpu = lp->lwp_estcpu;
		newpriority = (lp->lwp_proc->p_nice - PRIO_MIN) *
			      (NICE_QS * PPQ) / PRIO_RANGE;
		newpriority += estcpu * PPQ / ESTCPUPPQ;
		if (newpriority < 0)
			newpriority = 0;
		if (newpriority >= MAXPRI)
			newpriority = MAXPRI - 1;
		newpriority += PRIBASE_NORMAL;
		break;
	case RTP_PRIO_IDLE:
		newpriority = PRIBASE_IDLE + (lp->lwp_rtprio.prio & PRIMASK);
		break;
	case RTP_PRIO_THREAD:
		newpriority = PRIBASE_THREAD + (lp->lwp_rtprio.prio & PRIMASK);
		break;
	default:
		panic("Bad RTP_PRIO %d", newrqtype);
		/* NOT REACHED */
	}

	/*
	 * The LWKT scheduler doesn't dive usched structures, give it a hint
	 * on the relative priority of user threads running in the kernel.
	 * The LWKT scheduler will always ensure that a user thread running
	 * in the kernel will get cpu some time, regardless of its upri,
	 * but can decide not to instantly switch from one kernel or user
	 * mode user thread to a kernel-mode user thread when it has a less
	 * desireable user priority.
	 *
	 * td_upri has normal sense (higher values are more desireable), so
	 * negate it (this is a different field lp->lwp_priority)
	 */
	lp->lwp_thread->td_upri = -(newpriority & usched_dfly_swmask);

	/*
	 * The newpriority incorporates the queue type so do a simple masked
	 * check to determine if the process has moved to another queue.  If
	 * it has, and it is currently on a run queue, then move it.
	 *
	 * Since uload is ~PPQMASK masked, no modifications are necessary if
	 * we end up in the same run queue.
	 *
	 * Reset rrcount if moving to a higher-priority queue, otherwise
	 * retain rrcount.
	 */
	if ((lp->lwp_priority ^ newpriority) & ~PPQMASK) {
		if (lp->lwp_priority < newpriority)
			lp->lwp_rrcount = 0;
		if (lp->lwp_mpflags & LWP_MP_ONRUNQ) {
			dfly_remrunqueue_locked(rdd, lp);
			lp->lwp_priority = newpriority;
			lp->lwp_rqtype = newrqtype;
			lp->lwp_rqindex = (newpriority & PRIMASK) / PPQ;
			dfly_setrunqueue_locked(rdd, lp);
			checkpri = 1;
		} else {
			lp->lwp_priority = newpriority;
			lp->lwp_rqtype = newrqtype;
			lp->lwp_rqindex = (newpriority & PRIMASK) / PPQ;
			checkpri = 0;
		}
	} else {
		/*
		 * In the same PPQ, uload cannot change.
		 */
		lp->lwp_priority = newpriority;
		checkpri = 1;
		rcpu = -1;
	}

	/*
	 * Adjust effective load.
	 *
	 * Calculate load then scale up or down geometrically based on p_nice.
	 * Processes niced up (positive) are less important, and processes
	 * niced downard (negative) are more important.  The higher the uload,
	 * the more important the thread.
	 */
	/* 0-511, 0-100% cpu */
	spin_lock(&lp->lwp_spin);
	delta_uload = lptouload(lp);
	delta_uload -= lp->lwp_uload;
	if (lp->lwp_uload + delta_uload < -32767) {
		delta_uload = -32768 - lp->lwp_uload;
	} else if (lp->lwp_uload + delta_uload > 32767) {
		delta_uload = 32767 - lp->lwp_uload;
	}
	lp->lwp_uload += delta_uload;
	if (lp->lwp_mpflags & LWP_MP_ULOAD)
		atomic_add_long(&dfly_pcpu[lp->lwp_qcpu].uload, delta_uload);
	spin_unlock(&lp->lwp_spin);

	/*
	 * Determine if we need to reschedule the target cpu.  This only
	 * occurs if the LWP is already on a scheduler queue, which means
	 * that idle cpu notification has already occured.  At most we
	 * need only issue a need_user_resched() on the appropriate cpu.
	 *
	 * The LWP may be owned by a CPU different from the current one,
	 * in which case dd->uschedcp may be modified without an MP lock
	 * or a spinlock held.  The worst that happens is that the code
	 * below causes a spurious need_user_resched() on the target CPU
	 * and dd->pri to be wrong for a short period of time, both of
	 * which are harmless.
	 *
	 * If checkpri is 0 we are adjusting the priority of the current
	 * process, possibly higher (less desireable), so ignore the upri
	 * check which will fail in that case.
	 */
	if (rcpu >= 0) {
		if (CPUMASK_TESTBIT(dfly_rdyprocmask, rcpu) &&
		    (checkpri == 0 ||
		     (rdd->upri & ~PRIMASK) >
		     (lp->lwp_priority & ~PRIMASK))) {
			if (rcpu == mycpu->gd_cpuid) {
				spin_unlock(&rdd->spin);
				need_user_resched();
			} else {
				spin_unlock(&rdd->spin);
				lwkt_send_ipiq(globaldata_find(rcpu),
					       dfly_need_user_resched_remote,
					       NULL);
			}
		} else {
			spin_unlock(&rdd->spin);
		}
	} else {
		spin_unlock(&rdd->spin);
	}
	crit_exit();
}

static
void
dfly_yield(struct lwp *lp)
{
	if (lp->lwp_qcpu != mycpu->gd_cpuid)
		return;
	KKASSERT(lp == curthread->td_lwp);

	/*
	 * Don't set need_user_resched() or mess with rrcount or anything.
	 * the TDF flag will override everything as long as we release.
	 */
	atomic_set_int(&lp->lwp_thread->td_mpflags, TDF_MP_DIDYIELD);
	dfly_release_curproc(lp);
}

/*
 * Thread was forcefully migrated to another cpu.  Normally forced migrations
 * are used for iterations and the kernel returns to the original cpu before
 * returning and this is not needed.  However, if the kernel migrates a
 * thread to another cpu and wants to leave it there, it has to call this
 * scheduler helper.
 *
 * Note that the lwkt_migratecpu() function also released the thread, so
 * we don't have to worry about that.
 */
static
void
dfly_changedcpu(struct lwp *lp)
{
	dfly_pcpu_t dd = &dfly_pcpu[lp->lwp_qcpu];
	dfly_pcpu_t rdd = &dfly_pcpu[mycpu->gd_cpuid];

	if (dd != rdd) {
		spin_lock(&dd->spin);
		dfly_changeqcpu_locked(lp, dd, rdd);
		spin_unlock(&dd->spin);
	}
}

/*
 * Called from fork1() when a new child process is being created.
 *
 * Give the child process an initial estcpu that is more batch then
 * its parent and dock the parent for the fork (but do not
 * reschedule the parent).
 *
 * fast
 *
 * XXX lwp should be "spawning" instead of "forking"
 */
static void
dfly_forking(struct lwp *plp, struct lwp *lp)
{
	int estcpu;

	/*
	 * Put the child 4 queue slots (out of 32) higher than the parent
	 * (less desireable than the parent).
	 */
	lp->lwp_estcpu = ESTCPULIM(plp->lwp_estcpu +
				   ESTCPUPPQ * usched_dfly_forkbias);
	lp->lwp_forked = 1;
	lp->lwp_estfast = 0;

	/*
	 * Even though the lp will be scheduled specially the first time
	 * due to lp->lwp_forked, it is important to initialize lwp_qcpu
	 * to avoid favoring a fixed cpu. XXX
	 */
#if 0
	static uint16_t save_cpu;
	lp->lwp_qcpu = ++save_cpu % ncpus;
#else
	lp->lwp_qcpu = plp->lwp_qcpu;
	if (CPUMASK_TESTBIT(lp->lwp_cpumask, lp->lwp_qcpu) == 0)
		lp->lwp_qcpu = BSFCPUMASK(lp->lwp_cpumask);
#endif

	/*
	 * Dock the parent a cost for the fork, protecting us from fork
	 * bombs.  If the parent is forking quickly this makes both the
	 * parent and child more batchy.
	 */
	estcpu = plp->lwp_estcpu + ESTCPUPPQ / 16;
	plp->lwp_estcpu = ESTCPULIM(estcpu);
}

/*
 * Called when a lwp is being removed from this scheduler, typically
 * during lwp_exit().  We have to clean out any ULOAD accounting before
 * we can let the lp go.
 *
 * Scheduler dequeueing has already occurred, no further action in that
 * regard is needed.
 */
static void
dfly_exiting(struct lwp *lp, struct proc *child_proc)
{
	dfly_pcpu_t dd;

	spin_lock(&lp->lwp_spin);
	dd = &dfly_pcpu[lp->lwp_qcpu];
	if (lp->lwp_mpflags & LWP_MP_ULOAD) {
		atomic_clear_int(&lp->lwp_mpflags, LWP_MP_ULOAD);
		atomic_add_long(&dd->uload, -lp->lwp_uload);
		atomic_add_int(&dd->ucount, -1);
	}
	spin_unlock(&lp->lwp_spin);
}

/*
 * This function cannot block in any way, but spinlocks are ok.
 *
 * Update the uload based on the state of the thread (whether it is going
 * to sleep or running again).  The uload is meant to be a longer-term
 * load and not an instantanious load.
 */
static void
dfly_uload_update(struct lwp *lp)
{
	dfly_pcpu_t dd;

	if (lp->lwp_thread->td_flags & TDF_RUNQ) {
		if ((lp->lwp_mpflags & LWP_MP_ULOAD) == 0) {
			spin_lock(&lp->lwp_spin);
			dd = &dfly_pcpu[lp->lwp_qcpu];
			if ((lp->lwp_mpflags & LWP_MP_ULOAD) == 0) {
				atomic_set_int(&lp->lwp_mpflags,
					       LWP_MP_ULOAD);
				atomic_add_long(&dd->uload, lp->lwp_uload);
				atomic_add_int(&dd->ucount, 1);
			}
			spin_unlock(&lp->lwp_spin);
		}
	} else if (lp->lwp_slptime > 0) {
		if (lp->lwp_mpflags & LWP_MP_ULOAD) {
			spin_lock(&lp->lwp_spin);
			dd = &dfly_pcpu[lp->lwp_qcpu];
			if (lp->lwp_mpflags & LWP_MP_ULOAD) {
				atomic_clear_int(&lp->lwp_mpflags,
						 LWP_MP_ULOAD);
				atomic_add_long(&dd->uload, -lp->lwp_uload);
				atomic_add_int(&dd->ucount, -1);
			}
			spin_unlock(&lp->lwp_spin);
		}
	}
}

/*
 * chooseproc() is called when a cpu needs a user process to LWKT schedule,
 * it selects a user process and returns it.  If chklp is non-NULL and chklp
 * has a better or equal priority then the process that would otherwise be
 * chosen, NULL is returned.
 *
 * Until we fix the RUNQ code the chklp test has to be strict or we may
 * bounce between processes trying to acquire the current process designation.
 *
 * Must be called with rdd->spin locked.  The spinlock is left intact through
 * the entire routine.  dd->spin does not have to be locked.
 *
 * If worst is non-zero this function finds the worst thread instead of the
 * best thread (used by the schedulerclock-based rover).
 */
static
struct lwp *
dfly_chooseproc_locked(dfly_pcpu_t rdd, dfly_pcpu_t dd,
		       struct lwp *chklp, int worst)
{
	struct lwp *lp;
	struct rq *q;
	u_int32_t *which;
	u_int32_t pri;
	u_int32_t rtqbits;
	u_int32_t tsqbits;
	u_int32_t idqbits;

	/*
	 * Select best or worst process.  Once selected, clear the bit
	 * in our local variable (idqbits, tsqbits, or rtqbits) just
	 * in case we have to loop.
	 */
	rtqbits = rdd->rtqueuebits;
	tsqbits = rdd->queuebits;
	idqbits = rdd->idqueuebits;

loopfar:
	if (worst) {
		if (idqbits) {
			pri = bsrl(idqbits);
			idqbits &= ~(1U << pri);
			q = &rdd->idqueues[pri];
			which = &rdd->idqueuebits;
		} else if (tsqbits) {
			pri = bsrl(tsqbits);
			tsqbits &= ~(1U << pri);
			q = &rdd->queues[pri];
			which = &rdd->queuebits;
		} else if (rtqbits) {
			pri = bsrl(rtqbits);
			rtqbits &= ~(1U << pri);
			q = &rdd->rtqueues[pri];
			which = &rdd->rtqueuebits;
		} else {
			return (NULL);
		}
		lp = TAILQ_LAST(q, rq);
	} else {
		if (rtqbits) {
			pri = bsfl(rtqbits);
			rtqbits &= ~(1U << pri);
			q = &rdd->rtqueues[pri];
			which = &rdd->rtqueuebits;
		} else if (tsqbits) {
			pri = bsfl(tsqbits);
			tsqbits &= ~(1U << pri);
			q = &rdd->queues[pri];
			which = &rdd->queuebits;
		} else if (idqbits) {
			pri = bsfl(idqbits);
			idqbits &= ~(1U << pri);
			q = &rdd->idqueues[pri];
			which = &rdd->idqueuebits;
		} else {
			return (NULL);
		}
		lp = TAILQ_FIRST(q);
	}
	KASSERT(lp, ("chooseproc: no lwp on busy queue"));

loopnear:
	/*
	 * If the passed lwp <chklp> is reasonably close to the selected
	 * lwp <lp>, return NULL (indicating that <chklp> should be kept).
	 *
	 * Note that we must error on the side of <chklp> to avoid bouncing
	 * between threads in the acquire code.
	 */
	if (chklp) {
		if (chklp->lwp_priority < lp->lwp_priority + PPQ)
			return(NULL);
	}

	/*
	 * When rdd != dd, we have to make sure that the process we
	 * are pulling is allow to run on our cpu.  This alternative
	 * path is a bit more expensive but its not considered to be
	 * in the critical path.
	 */
	if (rdd != dd && CPUMASK_TESTBIT(lp->lwp_cpumask, dd->cpuid) == 0) {
		if (worst)
			lp = TAILQ_PREV(lp, rq, lwp_procq);
		else
			lp = TAILQ_NEXT(lp, lwp_procq);
		if (lp)
			goto loopnear;
		goto loopfar;
	}

	KTR_COND_LOG(usched_chooseproc,
	    lp->lwp_proc->p_pid == usched_dfly_pid_debug,
	    lp->lwp_proc->p_pid,
	    lp->lwp_thread->td_gd->gd_cpuid,
	    mycpu->gd_cpuid);

	KASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) != 0, ("not on runq6!"));
	atomic_clear_int(&lp->lwp_mpflags, LWP_MP_ONRUNQ);
	TAILQ_REMOVE(q, lp, lwp_procq);
	--rdd->runqcount;
	if (TAILQ_EMPTY(q))
		*which &= ~(1 << pri);

	/*
	 * If we are choosing a process from rdd with the intent to
	 * move it to dd, lwp_qcpu must be adjusted while rdd's spinlock
	 * is still held.
	 */
	if (rdd != dd) {
		spin_lock(&lp->lwp_spin);
		if (lp->lwp_mpflags & LWP_MP_ULOAD) {
			atomic_add_long(&rdd->uload, -lp->lwp_uload);
			atomic_add_int(&rdd->ucount, -1);
		}
		lp->lwp_qcpu = dd->cpuid;
		atomic_add_long(&dd->uload, lp->lwp_uload);
		atomic_add_int(&dd->ucount, 1);
		atomic_set_int(&lp->lwp_mpflags, LWP_MP_ULOAD);
		spin_unlock(&lp->lwp_spin);
	}
	return lp;
}

/*
 * USED TO PUSH RUNNABLE LWPS TO THE LEAST LOADED CPU.
 *
 * Choose a cpu node to schedule lp on, hopefully nearby its current
 * node.
 *
 * We give the current node a modest advantage for obvious reasons.
 *
 * We also give the node the thread was woken up FROM a slight advantage
 * in order to try to schedule paired threads which synchronize/block waiting
 * for each other fairly close to each other.  Similarly in a network setting
 * this feature will also attempt to place a user process near the kernel
 * protocol thread that is feeding it data.  THIS IS A CRITICAL PART of the
 * algorithm as it heuristically groups synchronizing processes for locality
 * of reference in multi-socket systems.
 *
 * We check against running processes and give a big advantage if there
 * are none running.
 *
 * The caller will normally dfly_setrunqueue() lp on the returned queue.
 *
 * When the topology is known choose a cpu whos group has, in aggregate,
 * has the lowest weighted load.
 */
static
dfly_pcpu_t
dfly_choose_best_queue(struct lwp *lp)
{
	cpumask_t wakemask;
	cpumask_t mask;
	cpu_node_t *cpup;
	cpu_node_t *cpun;
	cpu_node_t *cpub;
	dfly_pcpu_t dd = &dfly_pcpu[lp->lwp_qcpu];
	dfly_pcpu_t rdd;
	int wakecpu;
	int cpuid;
	int n;
	int loadav;
	long load;
	long lowest_load;

	/*
	 * When the topology is unknown choose a random cpu that is hopefully
	 * idle.
	 */
	if (dd->cpunode == NULL)
		return (dfly_choose_queue_simple(dd, lp));

	loadav = (averunnable.ldavg[0] + FSCALE / 2) >> FSHIFT;

	/*
	 * Pairing mask
	 */
	if ((wakecpu = lp->lwp_thread->td_wakefromcpu) >= 0)
		wakemask = dfly_pcpu[wakecpu].cpumask;
	else
		CPUMASK_ASSZERO(wakemask);

	if (usched_dfly_debug == lp->lwp_proc->p_pid)
		kprintf("choosebest wakefromcpu %d:\n",
			lp->lwp_thread->td_wakefromcpu);

	/*
	 * When the topology is known choose a cpu whos group has, in
	 * aggregate, has the lowest weighted load.
	 */
	cpup = root_cpu_node;
	rdd = dd;

	while (cpup) {
		/*
		 * Degenerate case super-root
		 */
		if (cpup->child_no == 1) {
			cpup = cpup->child_node[0];
			continue;
		}

		/*
		 * Terminal cpunode
		 */
		if (cpup->child_no == 0) {
			rdd = &dfly_pcpu[BSFCPUMASK(cpup->members)];
			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("  last cpu %d\n", rdd->cpuid);
			break;
		}

		cpub = NULL;
		lowest_load = 0x7FFFFFFFFFFFFFFFLL;
		if (usched_dfly_debug == lp->lwp_proc->p_pid)
			kprintf("  reset lowest_load for scan\n");

		for (n = 0; n < cpup->child_no; ++n) {
			/*
			 * Accumulate load information for all cpus
			 * which are members of this node.
			 */
			int count;

			cpun = cpup->child_node[n];
			mask = cpun->members;
			CPUMASK_ANDMASK(mask, usched_global_cpumask);
			CPUMASK_ANDMASK(mask, smp_active_mask);
			CPUMASK_ANDMASK(mask, lp->lwp_cpumask);
			if (CPUMASK_TESTZERO(mask))
				continue;

			load = 0;
			count = 0;

			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("  mask:");
			while (CPUMASK_TESTNZERO(mask)) {
				cpuid = BSFCPUMASK(mask);
				rdd = &dfly_pcpu[cpuid];

				if (usched_dfly_debug == lp->lwp_proc->p_pid)
					kprintf(" %d", cpuid);

				/*
				 * Cumulative load for members.  Note that
				 * if (lp) is part of the group, lp's
				 * contribution will be backed out later.
				 */
				load += rdd->uload;
				load += rdd->ucount *
					usched_dfly_weight3;

				/*
				 * If the node is running a less important
				 * thread than our thread, give it an
				 * advantage.  Witha high-enough weighting
				 * this can override most other considerations
				 * to provide ultimate priority fairness at
				 * the cost of localization.
				 */
				if ((rdd->upri & ~PPQMASK) >
				    (lp->lwp_priority & ~PPQMASK)) {
					load -= usched_dfly_weight4;
				}

#if 0
				if (rdd->uschedcp == NULL &&
				    rdd->runqcount == 0 &&
				    rdd->gd->gd_tdrunqcount == 0
				) {
					load += rdd->uload / 2;
					load += rdd->ucount *
						usched_dfly_weight3 / 2;
				} else {
					load += rdd->uload;
					load += rdd->ucount *
						usched_dfly_weight3;
				}
#endif
				CPUMASK_NANDBIT(mask, cpuid);
				++count;
			}

			/*
			 * Compensate if the lp is already accounted for in
			 * the aggregate uload for this mask set.  We want
			 * to calculate the loads as if lp were not present,
			 * otherwise the calculation is bogus.
			 */
			if ((lp->lwp_mpflags & LWP_MP_ULOAD) &&
			    CPUMASK_TESTMASK(dd->cpumask, cpun->members)) {
				load -= lp->lwp_uload;
				load -= usched_dfly_weight3;	/* ucount */
			}

			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("\n  accum_start c=%d ld=%ld "
					"cpu=%d ld/cnt=%ld ",
					count, load, rdd->cpuid,
					load / count);

			/*
			 * load is the aggregate load of count CPUs in the
			 * group.  For the weightings to work as intended,
			 * we want an average per-cpu load.
			 */
			load = load / count;

			/*
			 * Advantage the cpu group (lp) is already on.
			 */
			if (CPUMASK_TESTMASK(cpun->members, dd->cpumask))
				load -= usched_dfly_weight1;

			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("B:%ld ", load);

			/*
			 * Advantage nodes with more memory
			 */
			if (usched_dfly_node_mem) {
				load -= cpun->phys_mem * usched_dfly_weight5 /
					usched_dfly_node_mem;
			}

			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("C:%ld ", load);

			/*
			 * Advantage the cpu group we desire to pair (lp)
			 * to, but Disadvantage hyperthreads on the same
			 * core, or the same thread as the ipc peer.
			 *
			 * Under very heavy loads it is usually beneficial
			 * to set kern.usched_dfly.ipc_smt to 1, and under
			 * extreme loads it might be beneficial to also set
			 * kern.usched_dfly.ipc_same to 1.
			 *
			 * load+    disadvantage
			 * load-    advantage
			 */
			if (CPUMASK_TESTMASK(cpun->members, wakemask)) {
				if (cpun->child_no) {
					if (cpun->type == CORE_LEVEL &&
					    usched_dfly_ipc_smt < 0 &&
					    loadav >= (ncpus >> 1)) {
						/*
						 * Advantage at higher levels
						 * of the topology.
						 */
						load -= usched_dfly_weight2;
					} else if (cpun->type == CORE_LEVEL &&
						   usched_dfly_ipc_smt == 0) {
						/*
						 * Disadvantage the same core
						 * when there are hyperthreads.
						 */
						load += usched_dfly_weight2;
					} else {
						/*
						 * Advantage at higher levels
						 * of the topology.
						 */
						load -= usched_dfly_weight2;
					}
				} else {
					/*
					 * Disadvantage the last level (core
					 * or hyperthread).  Try to schedule
					 * the ipc
					 */
					if (usched_dfly_ipc_same < 0 &&
					    loadav >= ncpus) {
						load -= usched_dfly_weight2;
					} else if (usched_dfly_ipc_same) {
						load -= usched_dfly_weight2;
					} else {
						load += usched_dfly_weight2;
					}
				}
#if 0
				if (cpun->child_no != 0) {
					/* advantage */
					load -= usched_dfly_weight2;
				} else {
					/*
					 * 0x10 (disadvantage)
					 * 0x00 (advantage)   - default
					 */
					if (usched_dfly_features & 0x10)
						load += usched_dfly_weight2;
					else
						load -= usched_dfly_weight2;
				}
#endif
			}

			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("D:%ld ", load);

			/*
			 * Calculate the best load
			 */
			if (cpub == NULL || lowest_load > load ||
			    (lowest_load == load &&
			     CPUMASK_TESTMASK(cpun->members, dd->cpumask))
			) {
				lowest_load = load;
				cpub = cpun;
			}

			if (usched_dfly_debug == lp->lwp_proc->p_pid)
				kprintf("low=%ld]\n", lowest_load);
		}
		cpup = cpub;
	}
	/* Dispatch this outcast to a proper CPU. */
	if (__predict_false(CPUMASK_TESTBIT(lp->lwp_cpumask, rdd->cpuid) == 0))
		rdd = &dfly_pcpu[BSFCPUMASK(lp->lwp_cpumask)];
	if (usched_dfly_chooser > 0) {
		--usched_dfly_chooser;		/* only N lines */
		kprintf("lp %02d->%02d %s\n",
			lp->lwp_qcpu, rdd->cpuid, lp->lwp_proc->p_comm);
	}
	if (usched_dfly_debug == lp->lwp_proc->p_pid)
		kprintf("final cpu %d\n", rdd->cpuid);
	return (rdd);
}

/*
 * USED TO PULL RUNNABLE LWPS FROM THE MOST LOADED CPU.
 *
 * Choose the worst queue close to dd's cpu node with a non-empty runq
 * that is NOT dd.
 *
 * This is used by the thread chooser when the current cpu's queues are
 * empty to steal a thread from another cpu's queue.  We want to offload
 * the most heavily-loaded queue.
 *
 * However, we do not want to steal from far-away nodes who themselves
 * have idle cpu's that are more suitable to distribute the far-away
 * thread to.
 */
static
dfly_pcpu_t
dfly_choose_worst_queue(dfly_pcpu_t dd, int forceit)
{
	cpumask_t mask;
	cpu_node_t *cpup;
	cpu_node_t *cpun;
	cpu_node_t *cpub;
	dfly_pcpu_t rdd;
	int cpuid;
	int n;
	int highest_runqcount;
	long load;
	long highest_load;
#if 0
	int pri;
	int hpri;
#endif

	/*
	 * When the topology is unknown choose a random cpu that is hopefully
	 * idle.
	 */
	if (dd->cpunode == NULL) {
		return (NULL);
	}

	/*
	 * When the topology is known choose a cpu whos group has, in
	 * aggregate, has the highest weighted load.
	 */
	cpup = root_cpu_node;
	rdd = dd;
	while (cpup) {
		/*
		 * Degenerate case super-root
		 */
		if (cpup->child_no == 1) {
			cpup = cpup->child_node[0];
			continue;
		}

		/*
		 * Terminal cpunode
		 */
		if (cpup->child_no == 0) {
			rdd = &dfly_pcpu[BSFCPUMASK(cpup->members)];
			break;
		}

		cpub = NULL;
		highest_load = -0x7FFFFFFFFFFFFFFFLL;

		for (n = 0; n < cpup->child_no; ++n) {
			/*
			 * Accumulate load information for all cpus
			 * which are members of this node.
			 */
			int count;
			int runqcount;

			cpun = cpup->child_node[n];
			mask = cpun->members;
			CPUMASK_ANDMASK(mask, usched_global_cpumask);
			CPUMASK_ANDMASK(mask, smp_active_mask);
			if (CPUMASK_TESTZERO(mask))
				continue;

			load = 0;
			count = 0;
			runqcount = 0;

			while (CPUMASK_TESTNZERO(mask)) {
				cpuid = BSFCPUMASK(mask);
				rdd = &dfly_pcpu[cpuid];

				load += rdd->uload;
				load += rdd->ucount * usched_dfly_weight3;

#if 0
				if (rdd->uschedcp == NULL &&
				    rdd->runqcount == 0 &&
				    rdd->gd->gd_tdrunqcount == 0
				) {
					load += rdd->uload / 2;
					load += rdd->ucount *
						usched_dfly_weight3 / 2;
				} else {
					load += rdd->uload;
					load += rdd->ucount *
						usched_dfly_weight3;
				}
#endif
				CPUMASK_NANDBIT(mask, cpuid);
				++count;
				runqcount += rdd->runqcount;
			}
			load /= count;

			/*
			 * Advantage the cpu group (dd) is already on.
			 *
			 * When choosing the worst queue we reverse the
			 * sign, but only count half the weight.
			 *
			 * weight1 needs to be high enough to be stable,
			 * but this can also cause it to be too sticky,
			 * so the iterator which rebalances the load sets
			 * forceit to ignore it.
			 */
			if (forceit == 0 &&
			    CPUMASK_TESTMASK(dd->cpumask, cpun->members)) {
				load += usched_dfly_weight1 / 2;
			}

			/*
			 * Disadvantage nodes with more memory (same sign).
			 */
			if (usched_dfly_node_mem) {
				load -= cpun->phys_mem * usched_dfly_weight5 /
					usched_dfly_node_mem;
			}


			/*
			 * The best candidate is the one with the worst
			 * (highest) load, as long as it also has processes
			 * on the run queue (verses running one and nothing
			 * on the run queue).
			 */
			if (cpub == NULL ||
			    (runqcount && (highest_load < load ||
					 (highest_load == load &&
					  CPUMASK_TESTMASK(cpun->members,
							   dd->cpumask)))) ||
			    (runqcount && highest_runqcount < runqcount + 1)) {
				highest_load = load;
				highest_runqcount = runqcount;
				cpub = cpun;
			}
		}
		cpup = cpub;
	}

	/*
	 * We never return our own node (dd), and only return a remote
	 * node if it's load is significantly worse than ours (i.e. where
	 * stealing a thread would be considered reasonable).
	 *
	 * This also helps us avoid breaking paired threads apart which
	 * can have disastrous effects on performance.
	 */
	if (rdd == dd)
		return(NULL);

#if 0
	hpri = 0;
	if (rdd->rtqueuebits && hpri < (pri = bsrl(rdd->rtqueuebits)))
		hpri = pri;
	if (rdd->queuebits && hpri < (pri = bsrl(rdd->queuebits)))
		hpri = pri;
	if (rdd->idqueuebits && hpri < (pri = bsrl(rdd->idqueuebits)))
		hpri = pri;
	hpri *= PPQ;
	if (rdd->uload - hpri < dd->uload + hpri)
		return(NULL);
#endif
	return (rdd);
}

static
dfly_pcpu_t
dfly_choose_queue_simple(dfly_pcpu_t dd, struct lwp *lp)
{
	dfly_pcpu_t rdd;
	cpumask_t tmpmask;
	cpumask_t mask;
	int cpubase;
	int cpuid;

	/*
	 * Fallback to the original heuristic, select random cpu,
	 * first checking the cpus not currently running a user thread.
	 *
	 * Use cpuid as the base cpu in our scan, first checking
	 * cpuid...(ncpus-1), then 0...(cpuid-1).  This avoid favoring
	 * lower-numbered cpus.
	 */
	++dd->scancpu;		/* SMP race ok */
	mask = dfly_rdyprocmask;
	CPUMASK_NANDMASK(mask, dfly_curprocmask);
	CPUMASK_ANDMASK(mask, lp->lwp_cpumask);
	CPUMASK_ANDMASK(mask, smp_active_mask);
	CPUMASK_ANDMASK(mask, usched_global_cpumask);

	cpubase = (int)(dd->scancpu % ncpus);
	CPUMASK_ASSBMASK(tmpmask, cpubase);
	CPUMASK_INVMASK(tmpmask);
	CPUMASK_ANDMASK(tmpmask, mask);
	while (CPUMASK_TESTNZERO(tmpmask)) {
		cpuid = BSFCPUMASK(tmpmask);
		rdd = &dfly_pcpu[cpuid];

		if ((rdd->upri & ~PPQMASK) >= (lp->lwp_priority & ~PPQMASK))
			goto found;
		CPUMASK_NANDBIT(tmpmask, cpuid);
	}

	CPUMASK_ASSBMASK(tmpmask, cpubase);
	CPUMASK_ANDMASK(tmpmask, mask);
	while (CPUMASK_TESTNZERO(tmpmask)) {
		cpuid = BSFCPUMASK(tmpmask);
		rdd = &dfly_pcpu[cpuid];

		if ((rdd->upri & ~PPQMASK) >= (lp->lwp_priority & ~PPQMASK))
			goto found;
		CPUMASK_NANDBIT(tmpmask, cpuid);
	}

	/*
	 * Then cpus which might have a currently running lp
	 */
	mask = dfly_rdyprocmask;
	CPUMASK_ANDMASK(mask, dfly_curprocmask);
	CPUMASK_ANDMASK(mask, lp->lwp_cpumask);
	CPUMASK_ANDMASK(mask, smp_active_mask);
	CPUMASK_ANDMASK(mask, usched_global_cpumask);

	CPUMASK_ASSBMASK(tmpmask, cpubase);
	CPUMASK_INVMASK(tmpmask);
	CPUMASK_ANDMASK(tmpmask, mask);
	while (CPUMASK_TESTNZERO(tmpmask)) {
		cpuid = BSFCPUMASK(tmpmask);
		rdd = &dfly_pcpu[cpuid];

		if ((rdd->upri & ~PPQMASK) > (lp->lwp_priority & ~PPQMASK))
			goto found;
		CPUMASK_NANDBIT(tmpmask, cpuid);
	}

	CPUMASK_ASSBMASK(tmpmask, cpubase);
	CPUMASK_ANDMASK(tmpmask, mask);
	while (CPUMASK_TESTNZERO(tmpmask)) {
		cpuid = BSFCPUMASK(tmpmask);
		rdd = &dfly_pcpu[cpuid];

		if ((rdd->upri & ~PPQMASK) > (lp->lwp_priority & ~PPQMASK))
			goto found;
		CPUMASK_NANDBIT(tmpmask, cpuid);
	}

	/*
	 * If we cannot find a suitable cpu we round-robin using scancpu.
	 * Other cpus will pickup as they release their current lwps or
	 * become ready.
	 *
	 * Avoid a degenerate system lockup case if usched_global_cpumask
	 * is set to 0 or otherwise does not cover lwp_cpumask.
	 *
	 * We only kick the target helper thread in this case, we do not
	 * set the user resched flag because
	 */
	cpuid = cpubase;
	if (CPUMASK_TESTBIT(lp->lwp_cpumask, cpuid) == 0)
		cpuid = BSFCPUMASK(lp->lwp_cpumask);
	else if (CPUMASK_TESTBIT(usched_global_cpumask, cpuid) == 0)
		cpuid = 0;
	rdd = &dfly_pcpu[cpuid];
found:
	return (rdd);
}

static
void
dfly_need_user_resched_remote(void *dummy)
{
	globaldata_t gd = mycpu;
	dfly_pcpu_t  dd = &dfly_pcpu[gd->gd_cpuid];

	/*
	 * Flag reschedule needed
	 */
	need_user_resched();

	/*
	 * If no user thread is currently running we need to kick the helper
	 * on our cpu to recover.  Otherwise the cpu will never schedule
	 * anything again.
	 *
	 * We cannot schedule the process ourselves because this is an
	 * IPI callback and we cannot acquire spinlocks in an IPI callback.
	 *
	 * Call wakeup_mycpu to avoid sending IPIs to other CPUs
	 */
	if (dd->uschedcp == NULL && (dd->flags & DFLY_PCPU_RDYMASK)) {
		ATOMIC_CPUMASK_NANDBIT(dfly_rdyprocmask, gd->gd_cpuid);
		dd->flags &= ~DFLY_PCPU_RDYMASK;
		wakeup_mycpu(dd->helper_thread);
	}
}

/*
 * dfly_remrunqueue_locked() removes a given process from the run queue
 * that it is on, clearing the queue busy bit if it becomes empty.
 *
 * Note that user process scheduler is different from the LWKT schedule.
 * The user process scheduler only manages user processes but it uses LWKT
 * underneath, and a user process operating in the kernel will often be
 * 'released' from our management.
 *
 * uload is NOT adjusted here.  It is only adjusted if the lwkt_thread goes
 * to sleep or the lwp is moved to a different runq.
 */
static void
dfly_remrunqueue_locked(dfly_pcpu_t rdd, struct lwp *lp)
{
	struct rq *q;
	u_int32_t *which;
	u_int8_t pri;

	KKASSERT(rdd->runqcount >= 0);

	pri = lp->lwp_rqindex;

	switch(lp->lwp_rqtype) {
	case RTP_PRIO_NORMAL:
		q = &rdd->queues[pri];
		which = &rdd->queuebits;
		break;
	case RTP_PRIO_REALTIME:
	case RTP_PRIO_FIFO:
		q = &rdd->rtqueues[pri];
		which = &rdd->rtqueuebits;
		break;
	case RTP_PRIO_IDLE:
		q = &rdd->idqueues[pri];
		which = &rdd->idqueuebits;
		break;
	default:
		panic("remrunqueue: invalid rtprio type");
		/* NOT REACHED */
	}
	KKASSERT(lp->lwp_mpflags & LWP_MP_ONRUNQ);
	atomic_clear_int(&lp->lwp_mpflags, LWP_MP_ONRUNQ);
	TAILQ_REMOVE(q, lp, lwp_procq);
	--rdd->runqcount;
	if (TAILQ_EMPTY(q)) {
		KASSERT((*which & (1 << pri)) != 0,
			("remrunqueue: remove from empty queue"));
		*which &= ~(1 << pri);
	}
}

/*
 * dfly_setrunqueue_locked()
 *
 * Add a process whos rqtype and rqindex had previously been calculated
 * onto the appropriate run queue.   Determine if the addition requires
 * a reschedule on a cpu and return the cpuid or -1.
 *
 * NOTE: 	  Lower priorities are better priorities.
 *
 * NOTE ON ULOAD: This variable specifies the aggregate load on a cpu, the
 *		  sum of the rough lwp_priority for all running and runnable
 *		  processes.  Lower priority processes (higher lwp_priority
 *		  values) actually DO count as more load, not less, because
 *		  these are the programs which require the most care with
 *		  regards to cpu selection.
 */
static void
dfly_setrunqueue_locked(dfly_pcpu_t rdd, struct lwp *lp)
{
	u_int32_t *which;
	struct rq *q;
	int pri;

	KKASSERT(lp->lwp_qcpu == rdd->cpuid);

	spin_lock(&lp->lwp_spin);
	if ((lp->lwp_mpflags & LWP_MP_ULOAD) == 0) {
		atomic_set_int(&lp->lwp_mpflags, LWP_MP_ULOAD);
		atomic_add_long(&rdd->uload, lp->lwp_uload);
		atomic_add_int(&rdd->ucount, 1);
	}
	spin_unlock(&lp->lwp_spin);

	pri = lp->lwp_rqindex;

	switch(lp->lwp_rqtype) {
	case RTP_PRIO_NORMAL:
		q = &rdd->queues[pri];
		which = &rdd->queuebits;
		break;
	case RTP_PRIO_REALTIME:
	case RTP_PRIO_FIFO:
		q = &rdd->rtqueues[pri];
		which = &rdd->rtqueuebits;
		break;
	case RTP_PRIO_IDLE:
		q = &rdd->idqueues[pri];
		which = &rdd->idqueuebits;
		break;
	default:
		panic("remrunqueue: invalid rtprio type");
		/* NOT REACHED */
	}

	/*
	 * Place us on the selected queue.  Determine if we should be
	 * placed at the head of the queue or at the end.
	 *
	 * We are placed at the tail if our round-robin count has expired,
	 * or is about to expire and the system thinks its a good place to
	 * round-robin, or there is already a next thread on the queue
	 * (it might be trying to pick up where it left off and we don't
	 * want to interfere).
	 */
	KKASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) == 0);
	atomic_set_int(&lp->lwp_mpflags, LWP_MP_ONRUNQ);
	++rdd->runqcount;

	if (lp->lwp_rrcount >= usched_dfly_rrinterval ||
	    (lp->lwp_rrcount >= usched_dfly_rrinterval / 2 &&
	     (lp->lwp_thread->td_mpflags & TDF_MP_BATCH_DEMARC))
	) {
		/*
		 * Place on tail
		 */
		atomic_clear_int(&lp->lwp_thread->td_mpflags,
				 TDF_MP_BATCH_DEMARC);
		lp->lwp_rrcount = 0;
		TAILQ_INSERT_TAIL(q, lp, lwp_procq);
	} else {
		/*
		 * Retain rrcount and place on head.  Count is retained
		 * even if the queue is empty.
		 */
		TAILQ_INSERT_HEAD(q, lp, lwp_procq);
	}
	*which |= 1 << pri;
}

/*
 * For SMP systems a user scheduler helper thread is created for each
 * cpu and is used to allow one cpu to wakeup another for the purposes of
 * scheduling userland threads from setrunqueue().
 *
 * UP systems do not need the helper since there is only one cpu.
 *
 * We can't use the idle thread for this because we might block.
 * Additionally, doing things this way allows us to HLT idle cpus
 * on MP systems.
 */
static void
dfly_helper_thread(void *dummy)
{
    globaldata_t gd;
    dfly_pcpu_t dd;
    dfly_pcpu_t rdd;
    struct lwp *nlp;
    cpumask_t mask;
    int sleepok;
    int cpuid;

    gd = mycpu;
    cpuid = gd->gd_cpuid;	/* doesn't change */
    mask = gd->gd_cpumask;	/* doesn't change */
    dd = &dfly_pcpu[cpuid];

    /*
     * Initial interlock, make sure all dfly_pcpu[] structures have
     * been initialized before proceeding.
     */
    lockmgr(&usched_dfly_config_lk, LK_SHARED);
    lockmgr(&usched_dfly_config_lk, LK_RELEASE);

    /*
     * Since we only want to be woken up only when no user processes
     * are scheduled on a cpu, run at an ultra low priority.
     */
    lwkt_setpri_self(TDPRI_USER_SCHEDULER);

    for (;;) {
	/*
	 * We use the LWKT deschedule-interlock trick to avoid racing
	 * dfly_rdyprocmask.  This means we cannot block through to the
	 * manual lwkt_switch() call we make below.
	 */
	sleepok = 1;
	crit_enter_gd(gd);
	tsleep_interlock(dd->helper_thread, 0);

	spin_lock(&dd->spin);
	if ((dd->flags & DFLY_PCPU_RDYMASK) == 0) {
		ATOMIC_CPUMASK_ORMASK(dfly_rdyprocmask, mask);
		dd->flags |= DFLY_PCPU_RDYMASK;
	}
	clear_user_resched();	/* This satisfied the reschedule request */
#if 0
	dd->rrcount = 0;	/* Reset the round-robin counter */
#endif

	if (dd->runqcount || dd->uschedcp != NULL) {
		/*
		 * Threads are available.  A thread may or may not be
		 * currently scheduled.  Get the best thread already queued
		 * to this cpu.
		 */
		nlp = dfly_chooseproc_locked(dd, dd, dd->uschedcp, 0);
		if (nlp) {
			if ((dd->flags & DFLY_PCPU_CURMASK) == 0) {
				ATOMIC_CPUMASK_ORMASK(dfly_curprocmask, mask);
				dd->flags |= DFLY_PCPU_CURMASK;
			}
			dd->upri = nlp->lwp_priority;
			dd->uschedcp = nlp;
#if 0
			dd->rrcount = 0;	/* reset round robin */
#endif
			spin_unlock(&dd->spin);
			lwkt_acquire(nlp->lwp_thread);
			lwkt_schedule(nlp->lwp_thread);
		} else {
			/*
			 * This situation should not occur because we had
			 * at least one thread available.
			 */
			spin_unlock(&dd->spin);
		}
	} else if (usched_dfly_features & 0x01) {
		/*
		 * This cpu is devoid of runnable threads, steal a thread
		 * from another nearby cpu that is both running something
		 * and has runnable threads queued.  Since we're stealing,
		 * we might as well load balance at the same time.
		 *
		 * We choose the worst thread from the worst queue.  This
		 * can be a bit problematic if the worst queue intends to
		 * run the thread we choose,
		 *
		 * NOTE! This function only returns a non-NULL rdd when
		 *	 another cpu's queue is obviously overloaded.  We
		 *	 do not want to perform the type of rebalancing
		 *	 the schedclock does here because it would result
		 *	 in insane process pulling when 'steady' state is
		 *	 partially unbalanced (e.g. 6 runnables and only
		 *	 4 cores).
		 */
		rdd = dfly_choose_worst_queue(dd, 0);
		if (rdd && dd->uload + usched_dfly_weight7 < rdd->uload) {
			if (rdd->uschedcp && spin_trylock(&rdd->spin)) {
				nlp = dfly_chooseproc_locked(rdd, dd, NULL, 1);
				spin_unlock(&rdd->spin);
			} else {
				nlp = NULL;
			}
		} else {
			nlp = NULL;
		}
		if (nlp) {
			if ((dd->flags & DFLY_PCPU_CURMASK) == 0) {
				ATOMIC_CPUMASK_ORMASK(dfly_curprocmask, mask);
				dd->flags |= DFLY_PCPU_CURMASK;
			}
			dd->upri = nlp->lwp_priority;
			dd->uschedcp = nlp;
#if 0
			dd->rrcount = 0;	/* reset round robin */
#endif
			spin_unlock(&dd->spin);
			lwkt_acquire(nlp->lwp_thread);
			lwkt_schedule(nlp->lwp_thread);
		} else {
			/*
			 * Leave the thread on our run queue.  Another
			 * scheduler will try to pull it later.
			 */
			spin_unlock(&dd->spin);
		}
	} else {
		/*
		 * devoid of runnable threads and not allowed to steal
		 * any.
		 */
		spin_unlock(&dd->spin);
	}

	/*
	 * We're descheduled unless someone scheduled us.  Switch away.
	 * Exiting the critical section will cause splz() to be called
	 * for us if interrupts and such are pending.
	 */
	crit_exit_gd(gd);
	if (sleepok) {
		tsleep(dd->helper_thread, PINTERLOCKED, "schslp",
		       usched_dfly_poll_ticks);
	}
    }
}

#if 0
static int
sysctl_usched_dfly_stick_to_level(SYSCTL_HANDLER_ARGS)
{
	int error, new_val;

	new_val = usched_dfly_stick_to_level;

	error = sysctl_handle_int(oidp, &new_val, 0, req);
        if (error != 0 || req->newptr == NULL)
		return (error);
	if (new_val > cpu_topology_levels_number - 1 || new_val < 0)
		return (EINVAL);
	usched_dfly_stick_to_level = new_val;
	return (0);
}
#endif

/*
 * Setup the queues and scheduler helpers (scheduler helpers are SMP only).
 * Note that curprocmask bit 0 has already been cleared by rqinit() and
 * we should not mess with it further.
 */
static void
usched_dfly_cpu_init(void)
{
	int i;
	int j;
	int smt_not_supported = 0;
	int cache_coherent_not_supported = 0;

	if (bootverbose)
		kprintf("Start usched_dfly helpers on cpus:\n");

	sysctl_ctx_init(&usched_dfly_sysctl_ctx);
	usched_dfly_sysctl_tree =
		SYSCTL_ADD_NODE(&usched_dfly_sysctl_ctx,
				SYSCTL_STATIC_CHILDREN(_kern), OID_AUTO,
				"usched_dfly", CTLFLAG_RD, 0, "");

	usched_dfly_node_mem = get_highest_node_memory();

	lockmgr(&usched_dfly_config_lk, LK_EXCLUSIVE);

	for (i = 0; i < ncpus; ++i) {
		dfly_pcpu_t dd = &dfly_pcpu[i];
		cpumask_t mask;

		CPUMASK_ASSBIT(mask, i);
		if (CPUMASK_TESTMASK(mask, smp_active_mask) == 0)
		    continue;

		spin_init(&dd->spin, "uschedcpuinit");
		dd->cpunode = get_cpu_node_by_cpuid(i);
		dd->cpuid = i;
		dd->gd = globaldata_find(i);
		CPUMASK_ASSBIT(dd->cpumask, i);
		for (j = 0; j < NQS; j++) {
			TAILQ_INIT(&dd->queues[j]);
			TAILQ_INIT(&dd->rtqueues[j]);
			TAILQ_INIT(&dd->idqueues[j]);
		}
		ATOMIC_CPUMASK_NANDBIT(dfly_curprocmask, 0);
		if (i == 0)
			dd->flags &= ~DFLY_PCPU_CURMASK;

		if (dd->cpunode == NULL) {
			smt_not_supported = 1;
			cache_coherent_not_supported = 1;
			if (bootverbose)
				kprintf ("    cpu%d - WARNING: No CPU NODE "
					 "found for cpu\n", i);
		} else {
			switch (dd->cpunode->type) {
			case THREAD_LEVEL:
				if (bootverbose)
					kprintf ("    cpu%d - HyperThreading "
						 "available. Core siblings: ",
						 i);
				break;
			case CORE_LEVEL:
				smt_not_supported = 1;

				if (bootverbose)
					kprintf ("    cpu%d - No HT available, "
						 "multi-core/physical "
						 "cpu. Physical siblings: ",
						 i);
				break;
			case CHIP_LEVEL:
				smt_not_supported = 1;

				if (bootverbose)
					kprintf ("    cpu%d - No HT available, "
						 "single-core/physical cpu. "
						 "Package siblings: ",
						 i);
				break;
			default:
				/* Let's go for safe defaults here */
				smt_not_supported = 1;
				cache_coherent_not_supported = 1;
				if (bootverbose)
					kprintf ("    cpu%d - Unknown cpunode->"
						 "type=%u. siblings: ",
						 i,
						 (u_int)dd->cpunode->type);
				break;
			}

			if (bootverbose) {
				if (dd->cpunode->parent_node != NULL) {
					kprint_cpuset(&dd->cpunode->
							parent_node->members);
					kprintf("\n");
				} else {
					kprintf(" no siblings\n");
				}
			}
		}

		lwkt_create(dfly_helper_thread, NULL, &dd->helper_thread, NULL,
			    0, i, "usched %d", i);

		/*
		 * Allow user scheduling on the target cpu.  cpu #0 has already
		 * been enabled in rqinit().
		 */
		if (i) {
			ATOMIC_CPUMASK_NANDMASK(dfly_curprocmask, mask);
			dd->flags &= ~DFLY_PCPU_CURMASK;
		}
		if ((dd->flags & DFLY_PCPU_RDYMASK) == 0) {
			ATOMIC_CPUMASK_ORMASK(dfly_rdyprocmask, mask);
			dd->flags |= DFLY_PCPU_RDYMASK;
		}
		dd->upri = PRIBASE_NULL;

	}

	/* usched_dfly sysctl configurable parameters */

	SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
		       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
		       OID_AUTO, "rrinterval", CTLFLAG_RW,
		       &usched_dfly_rrinterval, 0, "");
	SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
		       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
		       OID_AUTO, "decay", CTLFLAG_RW,
		       &usched_dfly_decay, 0, "Extra decay when not running");
	SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
		       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
		       OID_AUTO, "ipc_smt", CTLFLAG_RW,
		       &usched_dfly_ipc_smt, 0, "Pair IPC on hyper-threads");
	SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
		       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
		       OID_AUTO, "ipc_same", CTLFLAG_RW,
		       &usched_dfly_ipc_same, 0, "Pair IPC on same thread");
	SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
		       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
		       OID_AUTO, "poll_ticks", CTLFLAG_RW,
		       &usched_dfly_poll_ticks, 0, "Poll for work (0 ok)");

	/* Add enable/disable option for SMT scheduling if supported */
	if (smt_not_supported) {
		usched_dfly_smt = 0;
		SYSCTL_ADD_STRING(&usched_dfly_sysctl_ctx,
				  SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
				  OID_AUTO, "smt", CTLFLAG_RD,
				  "NOT SUPPORTED", 0, "SMT NOT SUPPORTED");
	} else {
		usched_dfly_smt = 1;
		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "smt", CTLFLAG_RW,
			       &usched_dfly_smt, 0, "Enable SMT scheduling");
	}

	/*
	 * Add enable/disable option for cache coherent scheduling
	 * if supported
	 */
	if (cache_coherent_not_supported) {
		usched_dfly_cache_coherent = 0;
		SYSCTL_ADD_STRING(&usched_dfly_sysctl_ctx,
				  SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
				  OID_AUTO, "cache_coherent", CTLFLAG_RD,
				  "NOT SUPPORTED", 0,
				  "Cache coherence NOT SUPPORTED");
	} else {
		usched_dfly_cache_coherent = 1;
		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "cache_coherent", CTLFLAG_RW,
			       &usched_dfly_cache_coherent, 0,
			       "Enable/Disable cache coherent scheduling");

		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "weight1", CTLFLAG_RW,
			       &usched_dfly_weight1, 200,
			       "Weight selection for current cpu");

		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "weight2", CTLFLAG_RW,
			       &usched_dfly_weight2, 180,
			       "Weight selection for wakefrom cpu");

		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "weight3", CTLFLAG_RW,
			       &usched_dfly_weight3, 40,
			       "Weight selection for num threads on queue");

		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "weight4", CTLFLAG_RW,
			       &usched_dfly_weight4, 160,
			       "Availability of other idle cpus");

		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "weight5", CTLFLAG_RW,
			       &usched_dfly_weight5, 50,
			       "Memory attached to node");

		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "weight6", CTLFLAG_RW,
			       &usched_dfly_weight6, 150,
			       "Transfer weight Feat 0x04");

		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "weight7", CTLFLAG_RW,
			       &usched_dfly_weight7, -100,
			       "Transfer weight Feat 0x01");

		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "fast_resched", CTLFLAG_RW,
			       &usched_dfly_fast_resched, 0,
			       "Availability of other idle cpus");

		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "features", CTLFLAG_RW,
			       &usched_dfly_features, 0x8F,
			       "Allow pulls into empty queues");

		SYSCTL_ADD_INT(&usched_dfly_sysctl_ctx,
			       SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
			       OID_AUTO, "swmask", CTLFLAG_RW,
			       &usched_dfly_swmask, ~PPQMASK,
			       "Queue mask to force thread switch");

#if 0
		SYSCTL_ADD_PROC(&usched_dfly_sysctl_ctx,
				SYSCTL_CHILDREN(usched_dfly_sysctl_tree),
				OID_AUTO, "stick_to_level",
				CTLTYPE_INT | CTLFLAG_RW,
				NULL, sizeof usched_dfly_stick_to_level,
				sysctl_usched_dfly_stick_to_level, "I",
				"Stick a process to this level. See sysctl"
				"paremter hw.cpu_topology.level_description");
#endif
	}
	lockmgr(&usched_dfly_config_lk, LK_RELEASE);
}

SYSINIT(uschedtd, SI_BOOT2_USCHED, SI_ORDER_SECOND,
	usched_dfly_cpu_init, NULL);