flynnel 0.6.0

K-aware NUMA-aware work-stealing scheduler with extended-Flynn-taxonomy dispatch (MIMD / SIMC / MIMC / MISD / SIMT / MIMT): a rayon-shaped pool reshaped around per-call execution-class plans with first-class CPU+GPU hybrid (join_hybrid, hybrid_pipeline) and per-call dispatch profiles.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
//! Per-call-site adaptive state, keyed by the caller's source
//! location.
//!
//! Every dispatch entry is `#[track_caller]` and maps
//! `std::panic::Location::caller()` to a `&'static CallSiteState`
//! via [`site_for_location`]: an insert-once open-addressed table of
//! leaked nodes fronted by a per-thread one-slot cache, states
//! `Box::leak`ed for process lifetime. A `static` in a generic fn cannot provide this
//! identity (statics never monomorphize; every caller would share
//! one pool). `#[track_caller]` chains through wrapper entries, so
//! delegating helpers resolve to the outermost user call site.
//! Driver loops that funnel many workloads through one textual site
//! share one state; callers can pin their own via
//! [`crate::sched::JobPlan::with_site`]. A caller outside this binary
//! has no source location here, so it names its work by a key instead:
//! [`site_for_key`] resolves a caller-chosen u64 to a site of its own,
//! in a table apart from the locations'.
//!
//! A site holds: a learned
//! [`crate::sched::adaptive_profile::WorkloadClass`] (delta-window
//! classifier, hysteresis 2 adjacent, fast-adapt at bucket
//! distance 2+ with 64+ samples), leaf-time statistics for
//! [`crate::sched::JobPlan::effective_use_smt`], policy-arm A/B
//! EWMAs for the heartbeat-vs-SLAW gate, and placement EWMAs
//! (per log2-size-bucket CPU vs backend wall times) for
//! [`crate::sched::hybrid::hybrid_auto`].
//!
//! Leaves feed both the process-global counters (cold-start prior
//! for site-less plans) and the site's own statistics. Serial-span
//! samples (heartbeat / token-bucket fillers) are site-only:
//! whole-span wall times would poison the global per-item-ns
//! boundaries.

#![allow(clippy::missing_errors_doc)]

use core::sync::atomic::{AtomicBool, AtomicPtr, AtomicU8, AtomicU32, AtomicU64, Ordering};

use crate::sched::adaptive_profile::{
    WorkloadClass, class_tag_decode, class_tag_encode, classify_window,
};

/// Sentinel tag meaning "this site has not been classified yet".
const TAG_UNINIT: u8 = 0xFF;

/// Leaves between site classifier ticks. Matches the process-global
/// [`crate::sched::split_observer::AUTO_CLASSIFY_QUANTUM`] cadence so
/// per-site convergence speed is the same as the global observer's.
const SITE_CLASSIFY_QUANTUM: u64 = 16;

/// Consecutive agreeing ticks required before an adjacent-bucket
/// migration fires (same value as the global observer's hysteresis).
const SITE_MIGRATION_HYSTERESIS: u32 = 2;

/// cv^2 per mille of per-item cost, from the recorder's summed per-item
/// squares (each leaf's `ns^2 / (items << 16)`), the integer mean per
/// item, and the item count those sums cover.
///
/// Formed in 128 bits from the scaled sums directly. An integer variance
/// in between would move in steps of `1000 / (mean^2 >> 16)` per mille -
/// 26 at 1.6 us per item, 333 at 500 ns - against class edges at 50 and
/// 500. The integer mean is the one rounding left, and it biases the
/// result by at most `2 / mean` per mille: under 4 at 500 ns per item,
/// the lowest cost the cv^2 edges apply to.
///
/// `None` when the integer mean is zero, an item costing under one unit
/// of whatever clock the sums are in: there is no spread about a mean
/// that rounded away, and zero would read as uniform items.
fn per_item_cv2(sumsq_per_item: u64, mean: u64, items: u64) -> Option<u64> {
    let mean_sq = (mean as u128).saturating_mul(mean as u128);
    let expected = mean_sq.saturating_mul(items as u128);
    if expected == 0 {
        return None;
    }
    let total = (sumsq_per_item as u128) << 16;
    let spread = total.saturating_sub(expected);
    Some((spread.saturating_mul(1000) / expected) as u64)
}

/// Lowers `slot` to `value` where `value` is below it, from a plain load
/// and a plain store that lands only when the extreme moves.
fn publish_min(slot: &AtomicU64, value: u64) {
    if value < slot.load(Ordering::Relaxed) {
        slot.store(value, Ordering::Relaxed);
    }
}

/// Raises `slot` to `value` where `value` is above it, the same way as
/// [`publish_min`].
fn publish_max(slot: &AtomicU64, value: u64) {
    if value > slot.load(Ordering::Relaxed) {
        slot.store(value, Ordering::Relaxed);
    }
}

/// Adds one to `slot` with a plain load and store.
fn publish_count(slot: &AtomicU64) {
    slot.store(
        slot.load(Ordering::Relaxed).wrapping_add(1),
        Ordering::Relaxed,
    );
}

/// The weight of a batch that spent its whole interval on a core.
const FULL_WEIGHT: u64 = 1000;

/// A batch's weight in parts per mille: the share of its interval the
/// worker spent on a core, from the tick pair the recorder reads at both
/// ends of the batch.
///
/// Formed from the two sums directly in 128 bits. The occupancy reported
/// beside a dispatch is in hundredths, and taking the weight from that
/// would move it in steps of ten per mille, which is the whole spread
/// between a batch that got 99 percent of its cores and one that got
/// 100.
///
/// Capped at [`FULL_WEIGHT`]. The two counts come from different clocks
/// on Windows - executed cycles against a fixed-rate timestamp counter -
/// so a boosted core reads over one, and a batch cannot count for more
/// than one batch because its host was generous.
///
/// `None` where the pair describes no interval: a batch whose ends did
/// not both carry an on-core count, or one whose elapsed count is zero.
/// A caller decides what an unweighted batch means to it, at the point
/// where it knows. Zero is not that answer: it is the weight of a batch
/// that never got a core, which is a reading rather than the absence of
/// one.
pub(crate) fn batch_weight_per_mille(on_core_ticks: u64, wall_ticks: u64) -> Option<u64> {
    if wall_ticks == 0 {
        return None;
    }
    let share = (on_core_ticks as u128).saturating_mul(FULL_WEIGHT as u128)
        / (wall_ticks as u128);
    Some((share as u64).min(FULL_WEIGHT))
}

/// Policy-arm trial cadence: every Nth arm selection returns the
/// non-preferred arm so its EWMA stays fresh enough to detect drift.
const ARM_TRIAL_CADENCE: u32 = 16;

/// Minimum samples per arm before the EWMA comparison is trusted.
const ARM_MIN_SAMPLES: u32 = 3;

/// Placement re-probe cadence: every Nth call in a warm size bucket
/// runs both sides again so the model tracks drift (thermal
/// throttling, contention) instead of freezing on stale data.
const PLACEMENT_REPROBE_CADENCE: u32 = 32;

/// Number of log2-size buckets for the placement EWMAs. Bucket i
/// covers batch sizes in `[2^i, 2^(i+1))`; 40 buckets cover every
/// `u32` batch size and then some.
pub const PLACEMENT_BUCKETS: usize = 40;

/// An averaged cell packs its sample count, saturating at
/// [`EWMA_WARM_SAMPLES`], into the top byte; the low 56 bits hold
/// the average in nanoseconds.
const EWMA_COUNT_SHIFT: u32 = 56;
const EWMA_VALUE_MASK: u64 = (1u64 << EWMA_COUNT_SHIFT) - 1;

/// Smoothing rate of the exponential phase, as the denominator of
/// alpha: each update keeps `1 - 1/EWMA_ALPHA_RECIP` of the old
/// average. The value is the reciprocal because the update is a
/// shift, not a multiply.
const EWMA_ALPHA_RECIP: u64 = 8;

/// Samples averaged with equal weight before the update turns
/// exponential. It is the smoothing rate's denominator, not a
/// separate choice: the equal-weight phase then lasts exactly as
/// long as the exponential's own memory, so the handoff neither
/// leaves a sample over-weighted nor discards one.
///
/// The phase exists because a site's first sample is a cold one
/// (pool start, page faults, a cold device). Seeded straight into
/// the exponential it keeps seven eighths of its weight in the
/// second average and half in the sixth, and a tandem split reading
/// the ratio of two such averages spent six rounds of the gemm
/// parity test short of the balance the eighth sample reaches.
const EWMA_WARM_SAMPLES: u64 = EWMA_ALPHA_RECIP;

/// Averaged-cell update: the running mean while fewer than
/// [`EWMA_WARM_SAMPLES`] samples are in, then exponential at
/// [`EWMA_ALPHA_RECIP`]. Zero is the "empty" sentinel, so the first
/// sample seeds directly. Load/store (not CAS) is deliberate:
/// concurrent updates may drop a sample, which is acceptable for a
/// smoothed statistic and keeps the hot path at two relaxed atomics.
#[inline]
fn ewma_update(cell: &AtomicU64, sample_ns: u64) {
    let packed = cell.load(Ordering::Relaxed);
    let count = packed >> EWMA_COUNT_SHIFT;
    let old = packed & EWMA_VALUE_MASK;
    let sample = sample_ns.max(1) & EWMA_VALUE_MASK;
    let (new, count) = if count == 0 {
        (sample, 1)
    } else if count < EWMA_WARM_SAMPLES {
        ((old * count + sample) / (count + 1), count + 1)
    } else {
        (
            (old - old / EWMA_ALPHA_RECIP).saturating_add(sample / EWMA_ALPHA_RECIP),
            count,
        )
    };
    cell.store((count << EWMA_COUNT_SHIFT) | (new.max(1) & EWMA_VALUE_MASK), Ordering::Relaxed);
}

/// The average held by an averaged cell, zero while empty.
#[inline]
fn ewma_value(cell: &AtomicU64) -> u64 {
    cell.load(Ordering::Relaxed) & EWMA_VALUE_MASK
}

/// Which execution policy a site's A/B state currently prefers.
/// Arm meanings are defined by the consuming dispatch site (for the
/// heartbeat gate: arm 0 = SLAW bisect, arm 1 = heartbeat).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PolicyArm {
    /// The default execution policy for the consuming site.
    Default,
    /// The alternative execution policy for the consuming site.
    Alternative,
}

impl PolicyArm {
    #[inline]
    fn idx(self) -> usize {
        match self {
            PolicyArm::Default => 0,
            PolicyArm::Alternative => 1,
        }
    }
}

/// Placement decision produced by the hybrid-dispatch model.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Placement {
    /// Run only the CPU-side implementation.
    Cpu,
    /// Run only the backend-side implementation.
    Backend,
    /// Run both implementations concurrently and time each: the
    /// exploration/calibration mode (cold bucket or scheduled
    /// re-probe).
    Race,
}

/// Per-call-site adaptive state. Resolved automatically per caller
/// source location via [`caller_site`] / [`site_for_location`], or
/// declared as a caller-owned `static` and attached via
/// [`crate::sched::JobPlan::with_site`].
///
/// All fields are atomics with a `const fn new`, so the type is
/// directly usable in statics with zero lazy-init cost.
pub struct CallSiteState {
    // Classifier: learned class tag + adjacent-bucket hysteresis.
    active_tag: AtomicU8,
    pending_tag: AtomicU8,
    pending_run: AtomicU32,
    // Cumulative leaf statistics for this site, and beside them the
    // items those leaves covered with the summed per-item squared time.
    // The per-item pair is what a class can be decided from without the
    // split moving it: leaf times change when the scheduler splits
    // differently, and how it splits follows from the class.
    leaf_count: AtomicU64,
    leaf_sum_ns: AtomicU64,
    leaf_sumsq_scaled: AtomicU64,
    leaf_items: AtomicU64,
    leaf_sumsq_per_item: AtomicU64,
    // Summed weight of the batches those sums came from, in parts per
    // mille of a batch. The sums above carry the same factor, so this is
    // the count they divide by; `leaf_count` counts leaves and is what
    // the sample guards and the tick quantum read.
    leaf_weight_sum: AtomicU64,
    // The same leaves timed on the thread's own clock, which advances
    // only while the thread is on a core. Kept in raw counter ticks and
    // never converted: on Windows the thread clock counts executed
    // cycles against a fixed-rate elapsed counter, so a nanosecond
    // conversion would carry the achieved-to-base clock ratio into every
    // figure. A cv^2 is a ratio and a common factor cancels out of it,
    // so these feed the SPREAD only and the mean stays on wall time,
    // where the classifier's nanosecond boundaries are stated.
    //
    // Populated on the sampled path, where the cost of two extra clock
    // reads is divided by the stride. A site whose leaves are all
    // recorded off that path carries zero here and says so by its item
    // count rather than by a spread of zero.
    leaf_oncore_items: AtomicU64,
    leaf_oncore_sum: AtomicU64,
    leaf_oncore_sumsq_per_item: AtomicU64,
    // Snapshot of the cumulative counters at the previous classifier
    // tick; each tick classifies the delta window since then.
    last_count: AtomicU64,
    last_sum_ns: AtomicU64,
    last_sumsq: AtomicU64,
    last_items: AtomicU64,
    last_sumsq_per_item: AtomicU64,
    last_weight_sum: AtomicU64,
    last_oncore_items: AtomicU64,
    last_oncore_sum: AtomicU64,
    last_oncore_sumsq_per_item: AtomicU64,
    // Mean leaf time in nanoseconds of the delta window the latest tick
    // classified, and how many windows have been classified; cv^2 per
    // mille of the latest window whose spread could be read, and how
    // many could. A window whose mean is under the unit its spread is
    // kept in has a mean and no spread, and leaves `window_cv2` and its
    // extremes where they were.
    //
    // The tick writes these and the fields below with plain loads and
    // stores, as it keeps the snapshot above, and takes no locked
    // read-modify-write. Two ticks that overlap can each store over the
    // other, so under overlap a count can miss a window and an extreme
    // can miss the window that set it.
    window_mean_ns: AtomicU64,
    window_cv2: AtomicU64,
    window_ticks: AtomicU64,
    window_spread_ticks: AtomicU64,
    // Extremes of the per-window cv^2 across every tick, so a reader
    // gets the range the classifier acted over rather than whichever
    // tick happened to be last. A single tick's figure spans 0 to 527
    // across identical runs, so one reading of it cannot say which
    // regime a run was in.
    window_cv2_min: AtomicU64,
    window_cv2_max: AtomicU64,
    // The same window's spread on each clock apart, with the extremes of
    // each. `window_cv2` is whichever of the two the classifier used;
    // these let a reader set the one it used beside the one it did not.
    // The wall figures are written only for a window whose wall spread
    // could be read, which `window_wall_ticks` counts, and the on-core
    // figures only for a window whose spread the classifier took from
    // the on-core clock, which `window_oncore_ticks` counts.
    window_wall_cv2: AtomicU64,
    window_wall_cv2_min: AtomicU64,
    window_wall_cv2_max: AtomicU64,
    window_wall_ticks: AtomicU64,
    window_oncore_cv2: AtomicU64,
    window_oncore_cv2_min: AtomicU64,
    window_oncore_cv2_max: AtomicU64,
    window_oncore_ticks: AtomicU64,
    // Execution-policy A/B arms: per-arm EWMA wall time + sample
    // counts + a call counter driving the trial cadence.
    arm_ewma_ns: [AtomicU64; 2],
    arm_samples: [AtomicU32; 2],
    arm_calls: AtomicU32,
    // The same shape for the routing decision, kept apart from the
    // execution-policy arms because the two consumers would otherwise
    // average each other's dispatches into one EWMA and neither could
    // read its own effect. Default is the plan the learned class
    // re-derives; Alternative is the plan as the caller built it.
    routing_ewma_ns: [AtomicU64; 2],
    routing_samples: [AtomicU32; 2],
    routing_calls: AtomicU32,
    // Hybrid-placement model: per-log2-size-bucket end-to-end EWMAs
    // for the CPU side and the backend side, plus a per-bucket call
    // counter driving the re-probe cadence.
    place_cpu_ns: [AtomicU64; PLACEMENT_BUCKETS],
    place_backend_ns: [AtomicU64; PLACEMENT_BUCKETS],
    place_calls: [AtomicU32; PLACEMENT_BUCKETS],
    // Split-throughput model: learned per-item cost on each side for
    // proportional slice splitting, site-wide and per log2-size
    // bucket (a batch's per-item cost changes with its size on both
    // sides, so the share is learned per bucket and falls back to
    // the site-wide value while a bucket is cold).
    split_cpu_ns_per_item: AtomicU64,
    split_backend_ns_per_item: AtomicU64,
    split_cpu_ns_per_item_by_size: [AtomicU64; PLACEMENT_BUCKETS],
    split_backend_ns_per_item_by_size: [AtomicU64; PLACEMENT_BUCKETS],
    /// Reduce-merge cost observer for
    /// [`crate::sched::par_iter::reduce_chunks`]: TSC-cycle sum and
    /// sample count of timed `reduce(a, b)` calls at THIS call
    /// site, so a cheap element-wise merge and a heavy
    /// collection-merge reducer each converge on their own average.
    reduce_cost_sum_cycles: AtomicU64,
    reduce_cost_samples: AtomicU32,
    /// True once a body this site ran on the calling thread took
    /// longer than the collapse threshold that admitted it. While
    /// set, the site's calls dispatch regardless of the caller's
    /// per-item estimate.
    collapse_overran: AtomicBool,
    /// Seed-depth hysteresis: the depth in force, the depth a recent
    /// call asked for instead, and how many consecutive calls have
    /// asked for it.
    active_depth: AtomicU32,
    pending_depth: AtomicU32,
    depth_run: AtomicU32,
    /// What fraction of its interval the most recent dispatch at this
    /// site spent on a core, in hundredths, or [`OCCUPANCY_UNREPORTED`]
    /// before any dispatch has said.
    ///
    /// A site with no dispatches and a site whose pool held its cores
    /// throughout are different findings, so they do not share a value.
    recent_occupancy_pct: AtomicU32,
    /// On-core ticks and elapsed ticks summed across every worker that
    /// has run a leaf for this site, in the same unit.
    ///
    /// Monotonic, so a dispatch takes the difference across itself: a
    /// caller's own window covers its join wait, during which it is
    /// deliberately not running, and reads low exactly when the work
    /// spread well. These describe the threads that ran the leaves.
    pool_thread_ticks: AtomicU64,
    pool_wall_ticks: AtomicU64,
    /// The seed depth this site last dispatched with, and how many
    /// times a dispatch has used a different one from the dispatch
    /// before it.
    ///
    /// This is the quantity the seed-depth stabilizers exist to reduce:
    /// the same workload seeding a different leaf count from one call
    /// to the next. Throughput does not express it, because a flip
    /// between two adjacent depths costs little either way.
    last_seed_depth: AtomicU32,
    seed_depth_flips: AtomicU32,
}

/// No seed depth is in force for a site yet.
const DEPTH_UNSET: u32 = u32::MAX;

/// No dispatch at a site has reported its pool's occupancy yet.
///
/// Distinct from every occupancy a dispatch can report, which are
/// hundredths and so at most 100. Not public: a caller reads
/// [`CallSiteState::recent_occupancy`], whose `None` says the same
/// thing without the caller having to know the encoding.
const OCCUPANCY_UNREPORTED: u32 = u32::MAX;

/// Consecutive calls that must agree on a different seed depth before
/// it takes effect.
///
/// The same value and the same reason as [`SITE_MIGRATION_HYSTERESIS`]:
/// the seeded leaf count is a bucketed decision driven by a measured
/// quantity, and one reading that lands the far side of a boundary
/// must not move it alone.
pub const SEED_DEPTH_HYSTERESIS: u32 = 2;

impl CallSiteState {
    /// Fresh, unclassified site. Usable in `static` position.
    #[allow(clippy::new_without_default)]
    pub const fn new() -> Self {
        // Inline-const array elements: atomics are not Copy, so the
        // repeat expressions need per-element const evaluation.
        Self {
            active_tag: AtomicU8::new(TAG_UNINIT),
            pending_tag: AtomicU8::new(TAG_UNINIT),
            pending_run: AtomicU32::new(0),
            leaf_count: AtomicU64::new(0),
            leaf_sum_ns: AtomicU64::new(0),
            leaf_sumsq_scaled: AtomicU64::new(0),
            leaf_items: AtomicU64::new(0),
            leaf_sumsq_per_item: AtomicU64::new(0),
            leaf_weight_sum: AtomicU64::new(0),
            leaf_oncore_items: AtomicU64::new(0),
            leaf_oncore_sum: AtomicU64::new(0),
            leaf_oncore_sumsq_per_item: AtomicU64::new(0),
            last_oncore_items: AtomicU64::new(0),
            last_oncore_sum: AtomicU64::new(0),
            last_oncore_sumsq_per_item: AtomicU64::new(0),
            last_count: AtomicU64::new(0),
            last_sum_ns: AtomicU64::new(0),
            last_sumsq: AtomicU64::new(0),
            last_items: AtomicU64::new(0),
            last_sumsq_per_item: AtomicU64::new(0),
            last_weight_sum: AtomicU64::new(0),
            window_mean_ns: AtomicU64::new(0),
            window_cv2: AtomicU64::new(0),
            window_ticks: AtomicU64::new(0),
            window_spread_ticks: AtomicU64::new(0),
            window_cv2_min: AtomicU64::new(u64::MAX),
            window_cv2_max: AtomicU64::new(0),
            window_wall_cv2: AtomicU64::new(0),
            window_wall_cv2_min: AtomicU64::new(u64::MAX),
            window_wall_cv2_max: AtomicU64::new(0),
            window_wall_ticks: AtomicU64::new(0),
            window_oncore_cv2: AtomicU64::new(0),
            window_oncore_cv2_min: AtomicU64::new(u64::MAX),
            window_oncore_cv2_max: AtomicU64::new(0),
            window_oncore_ticks: AtomicU64::new(0),
            arm_ewma_ns: [const { AtomicU64::new(0) }; 2],
            arm_samples: [const { AtomicU32::new(0) }; 2],
            arm_calls: AtomicU32::new(0),
            routing_ewma_ns: [const { AtomicU64::new(0) }; 2],
            routing_samples: [const { AtomicU32::new(0) }; 2],
            routing_calls: AtomicU32::new(0),
            place_cpu_ns: [const { AtomicU64::new(0) }; PLACEMENT_BUCKETS],
            place_backend_ns: [const { AtomicU64::new(0) }; PLACEMENT_BUCKETS],
            place_calls: [const { AtomicU32::new(0) }; PLACEMENT_BUCKETS],
            split_cpu_ns_per_item: AtomicU64::new(0),
            split_backend_ns_per_item: AtomicU64::new(0),
            split_cpu_ns_per_item_by_size: [const { AtomicU64::new(0) }; PLACEMENT_BUCKETS],
            split_backend_ns_per_item_by_size: [const { AtomicU64::new(0) }; PLACEMENT_BUCKETS],
            reduce_cost_sum_cycles: AtomicU64::new(0),
            reduce_cost_samples: AtomicU32::new(0),
            collapse_overran: AtomicBool::new(false),
            active_depth: AtomicU32::new(DEPTH_UNSET),
            pending_depth: AtomicU32::new(DEPTH_UNSET),
            depth_run: AtomicU32::new(0),
            recent_occupancy_pct: AtomicU32::new(OCCUPANCY_UNREPORTED),
            pool_thread_ticks: AtomicU64::new(0),
            pool_wall_ticks: AtomicU64::new(0),
            last_seed_depth: AtomicU32::new(DEPTH_UNSET),
            seed_depth_flips: AtomicU32::new(0),
        }
    }

    /// Returns the site to the state [`CallSiteState::new`] gives, so a
    /// second arm in the same process does not inherit what the first
    /// one taught the classifier.
    ///
    /// Site state is leaked for the life of the process, which is what
    /// makes a `SiteRef` `'static`. Without this, two arms measured in
    /// one process share a learned class, an EWMA per policy arm and a
    /// seed depth, and the second arm's numbers describe both. Measuring
    /// them in separate processes instead carries whatever else differed
    /// between those processes, which for a decision driven by a
    /// measured estimate is the thing being measured.
    ///
    /// Not atomic as a whole. A dispatch running at this site while the
    /// reset lands sees some counters cleared and some not, which skews
    /// that one window; call it between arms, not during one.
    pub fn reset(&self) {
        // Destructured rather than assigned field by field, so a field
        // added to the struct and not cleared here fails to compile. A
        // reset that silently skips a counter is the defect it exists to
        // prevent, and it would show up as the second arm agreeing
        // suspiciously with the first.
        let Self {
            active_tag,
            pending_tag,
            pending_run,
            leaf_count,
            leaf_sum_ns,
            leaf_sumsq_scaled,
            leaf_items,
            leaf_sumsq_per_item,
            leaf_weight_sum,
            leaf_oncore_items,
            leaf_oncore_sum,
            leaf_oncore_sumsq_per_item,
            last_count,
            last_sum_ns,
            last_sumsq,
            last_items,
            last_sumsq_per_item,
            last_weight_sum,
            last_oncore_items,
            last_oncore_sum,
            last_oncore_sumsq_per_item,
            window_mean_ns,
            window_cv2,
            window_ticks,
            window_spread_ticks,
            window_cv2_min,
            window_cv2_max,
            window_wall_cv2,
            window_wall_cv2_min,
            window_wall_cv2_max,
            window_wall_ticks,
            window_oncore_cv2,
            window_oncore_cv2_min,
            window_oncore_cv2_max,
            window_oncore_ticks,
            arm_ewma_ns,
            arm_samples,
            arm_calls,
            routing_ewma_ns,
            routing_samples,
            routing_calls,
            place_cpu_ns,
            place_backend_ns,
            place_calls,
            split_cpu_ns_per_item,
            split_backend_ns_per_item,
            split_cpu_ns_per_item_by_size,
            split_backend_ns_per_item_by_size,
            reduce_cost_sum_cycles,
            reduce_cost_samples,
            collapse_overran,
            active_depth,
            pending_depth,
            depth_run,
            recent_occupancy_pct,
            pool_thread_ticks,
            pool_wall_ticks,
            last_seed_depth,
            seed_depth_flips,
        } = self;

        for tag in [active_tag, pending_tag] {
            tag.store(TAG_UNINIT, Ordering::Relaxed);
        }
        for counter in [
            leaf_count,
            leaf_sum_ns,
            leaf_sumsq_scaled,
            leaf_items,
            leaf_sumsq_per_item,
            leaf_weight_sum,
            leaf_oncore_items,
            leaf_oncore_sum,
            leaf_oncore_sumsq_per_item,
            last_count,
            last_sum_ns,
            last_sumsq,
            last_items,
            last_sumsq_per_item,
            last_weight_sum,
            last_oncore_items,
            last_oncore_sum,
            last_oncore_sumsq_per_item,
            window_mean_ns,
            window_cv2,
            window_ticks,
            window_spread_ticks,
            window_cv2_max,
            window_wall_cv2,
            window_wall_cv2_max,
            window_wall_ticks,
            window_oncore_cv2,
            window_oncore_cv2_max,
            window_oncore_ticks,
            split_cpu_ns_per_item,
            split_backend_ns_per_item,
            reduce_cost_sum_cycles,
            pool_thread_ticks,
            pool_wall_ticks,
        ] {
            counter.store(0, Ordering::Relaxed);
        }
        for counter in [
            pending_run,
            arm_calls,
            routing_calls,
            reduce_cost_samples,
            depth_run,
            seed_depth_flips,
        ] {
            counter.store(0, Ordering::Relaxed);
        }
        for depth in [active_depth, pending_depth, last_seed_depth] {
            depth.store(DEPTH_UNSET, Ordering::Relaxed);
        }
        for bank in [
            &arm_ewma_ns[..],
            &routing_ewma_ns[..],
            &place_cpu_ns[..],
            &place_backend_ns[..],
            &split_cpu_ns_per_item_by_size[..],
            &split_backend_ns_per_item_by_size[..],
        ] {
            for cell in bank {
                cell.store(0, Ordering::Relaxed);
            }
        }
        for bank in [&arm_samples[..], &routing_samples[..], &place_calls[..]] {
            for cell in bank {
                cell.store(0, Ordering::Relaxed);
            }
        }
        // The extremes start at the identity for a min, so the first
        // window after a reset sets both rather than being weighed
        // against a range the previous arm established.
        for min in [window_cv2_min, window_wall_cv2_min, window_oncore_cv2_min] {
            min.store(u64::MAX, Ordering::Relaxed);
        }
        recent_occupancy_pct.store(OCCUPANCY_UNREPORTED, Ordering::Relaxed);
        collapse_overran.store(false, Ordering::Relaxed);
    }

    /// Note the seed depth a dispatch at this site is about to use, and
    /// count it when it differs from the one before.
    ///
    /// The first call at a site establishes the depth and counts
    /// nothing: there is no previous dispatch for it to differ from.
    pub fn record_seed_depth(&self, depth: usize) {
        let d = depth as u32;
        let previous = self.last_seed_depth.swap(d, Ordering::Relaxed);
        if previous != DEPTH_UNSET && previous != d {
            self.seed_depth_flips.fetch_add(1, Ordering::Relaxed);
        }
    }

    /// Dispatches at this site that seeded a different leaf count from
    /// the dispatch before them.
    ///
    /// The figure the seed-depth stabilizers are measured against: a
    /// mechanism that costs throughput and does not lower this is
    /// paying for nothing.
    ///
    /// Counts only dispatches that reach the adaptive seed depth, which
    /// a plan carrying an explicit `bisect_variant` does not - such a
    /// dispatch names its own shape and has no depth to flip. So zero
    /// at a site whose callers all name a variant means the question
    /// was never asked there, not that the answer was steady.
    pub fn seed_depth_flips(&self) -> u32 {
        self.seed_depth_flips.load(Ordering::Relaxed)
    }

    /// Add one worker's on-core and elapsed ticks for leaves it ran at
    /// this site.
    ///
    /// Called from the worker that ran them, so the sum is over the
    /// threads that did the work rather than over the thread that
    /// waited for it.
    pub(crate) fn add_pool_ticks(&self, thread_ticks: u64, wall_ticks: u64) {
        self.pool_thread_ticks
            .fetch_add(thread_ticks, Ordering::Relaxed);
        self.pool_wall_ticks.fetch_add(wall_ticks, Ordering::Relaxed);
    }

    /// This site's running pool totals, for a caller taking a
    /// difference across a dispatch.
    pub(crate) fn pool_ticks(&self) -> (u64, u64) {
        (
            self.pool_thread_ticks.load(Ordering::Relaxed),
            self.pool_wall_ticks.load(Ordering::Relaxed),
        )
    }

    /// Report what fraction of a dispatch's leaf time its workers spent
    /// on a core.
    ///
    /// A dispatch that ran no leaves through the buffered path reports
    /// nothing, leaving the previous figure rather than overwriting it
    /// with a zero that would read as total contention.
    pub fn record_occupancy(&self, percent: u32) {
        self.recent_occupancy_pct
            .store(percent.min(100), Ordering::Relaxed);
    }

    /// The occupancy of the most recent dispatch at this site, or
    /// `None` if no dispatch has reported one.
    pub fn recent_occupancy(&self) -> Option<u32> {
        match self.recent_occupancy_pct.load(Ordering::Relaxed) {
            OCCUPANCY_UNREPORTED => None,
            pct => Some(pct),
        }
    }

    /// The seed depth to dispatch with, given the depth this call's
    /// estimate asks for.
    ///
    /// The first call takes what it is given. After that a different
    /// depth must be asked for by [`SEED_DEPTH_HYSTERESIS`] consecutive
    /// calls before it takes effect, so one estimate landing the far
    /// side of a power-of-two boundary does not halve or double the
    /// leaf count on its own.
    pub fn stabilize_seed_depth(&self, observed: usize) -> usize {
        let obs = observed as u32;
        let active = self.active_depth.load(Ordering::Relaxed);
        if active == DEPTH_UNSET {
            self.active_depth.store(obs, Ordering::Relaxed);
            return observed;
        }
        if obs == active {
            // The site is asking for what it already has; any run
            // toward a change is broken.
            self.depth_run.store(0, Ordering::Relaxed);
            return observed;
        }
        let previous_pending = self.pending_depth.swap(obs, Ordering::Relaxed);
        let run = if previous_pending == obs {
            self.depth_run
                .fetch_add(1, Ordering::Relaxed)
                .saturating_add(1)
        } else {
            self.depth_run.store(1, Ordering::Relaxed);
            1
        };
        if run >= SEED_DEPTH_HYSTERESIS {
            self.active_depth.store(obs, Ordering::Relaxed);
            self.depth_run.store(0, Ordering::Relaxed);
            return observed;
        }
        active as usize
    }

    /// The seed depth in force, or `None` before this site's first
    /// dispatch. Diagnostics and tests.
    pub fn seeded_depth(&self) -> Option<u32> {
        match self.active_depth.load(Ordering::Relaxed) {
            DEPTH_UNSET => None,
            d => Some(d),
        }
    }

    /// True once this site ran a collapsed body slower than the
    /// collapse threshold, which means its estimate reads low enough
    /// to have made the wrong call.
    #[inline]
    pub fn collapse_overran(&self) -> bool {
        self.collapse_overran.load(Ordering::Relaxed)
    }

    /// Record that a collapsed body took `elapsed_ns` against a
    /// threshold of `threshold_ns`. Latches when it ran over.
    #[inline]
    pub fn note_collapsed_body(&self, elapsed_ns: u64, threshold_ns: u64) {
        if elapsed_ns > threshold_ns {
            self.collapse_overran.store(true, Ordering::Relaxed);
        }
    }

    // -----------------------------------------------------------------
    // Classifier surface
    // -----------------------------------------------------------------

    /// The class this site has learned, or `None` while the site has
    /// not accumulated enough evidence to classify (fresh site, or
    /// fewer than 4 leaves in every delta window so far).
    #[inline]
    pub fn learned_class(&self) -> Option<WorkloadClass> {
        class_tag_decode(self.active_tag.load(Ordering::Acquire))
    }

    /// Record a batch of leaf-time samples for this site. Dual-writes
    /// to the process-global counters (keeping the global prior and
    /// the split-multiplier observer fed) and ticks the site's own
    /// classifier when the batch crosses the site quantum.
    pub fn record_batch(
        &'static self,
        sum_ns: u64,
        sumsq_scaled: u64,
        count: u64,
        items: u64,
        sumsq_per_item: u64,
    ) {
        // Global dual-write first: preserves every existing consumer
        // of the process-wide stats (split multiplier, global
        // auto-classify, effective_use_smt fallback, tests).
        crate::sched::split_observer::record_leaf_batch(
            sum_ns,
            sumsq_scaled,
            count,
            items,
            sumsq_per_item,
        );
        self.record_batch_site_only(sum_ns, sumsq_scaled, count, items, sumsq_per_item);
    }

    /// [`Self::record_batch`] without the process-global dual-write.
    /// For samples that are meaningful to THIS site's statistics but
    /// would poison the global classifier: heartbeat serial-span
    /// durations are whole-span wall times (tens of microseconds and
    /// up), not per-leaf costs, and feeding them to the global
    /// per-item-ns boundaries would migrate the process profile off
    /// unrelated workloads.
    pub(crate) fn record_batch_site_only(
        &'static self,
        sum_ns: u64,
        sumsq_scaled: u64,
        count: u64,
        items: u64,
        sumsq_per_item: u64,
    ) {
        self.record_batch_weighted(sum_ns, sumsq_scaled, count, items, sumsq_per_item, None)
    }

    /// [`Self::record_batch_site_only`] with the share of the batch's
    /// interval its worker spent on a core, from
    /// [`batch_weight_per_mille`].
    ///
    /// Every sum carries the weight as a factor and so does the count
    /// they are divided by, so each statistic is a ratio of weighted
    /// totals: a batch that held its cores counts for a whole batch and
    /// one that got a third of them counts for a third. The means are
    /// unchanged by the weighting and only the influence moves, which is
    /// what a sample of unknown extra spread is worth.
    ///
    /// `None` records the batch at full weight. That is what a platform
    /// with no thread clock, and a batch whose ends did not both carry a
    /// reading, must contribute: an unweighted sample rather than a
    /// suppressed one, because no reading is not evidence of contention.
    ///
    /// The sums scale by up to [`FULL_WEIGHT`] and are not divided back
    /// down, which keeps the ratios exact rather than truncating a small
    /// batch's items to zero. That costs ten bits of headroom on
    /// counters that already saturate, and `leaf_sumsq_scaled` is the
    /// one close enough to its ceiling for that to be reachable.
    ///
    /// `leaf_count` is left unweighted, because it is a count of leaves
    /// rather than a quantity the statistics divide: it drives the
    /// sample guards and the classifier quantum, and weighting it would
    /// put both out by whatever the host was doing.
    pub(crate) fn record_batch_weighted(
        &'static self,
        sum_ns: u64,
        sumsq_scaled: u64,
        count: u64,
        items: u64,
        sumsq_per_item: u64,
        weight_per_mille: Option<u64>,
    ) {
        let w = weight_per_mille.unwrap_or(FULL_WEIGHT).min(FULL_WEIGHT);
        let scale = |v: u64| v.saturating_mul(w);
        self.leaf_sum_ns.fetch_add(scale(sum_ns), Ordering::Relaxed);
        self.leaf_sumsq_scaled
            .fetch_add(scale(sumsq_scaled), Ordering::Relaxed);
        self.leaf_items.fetch_add(scale(items), Ordering::Relaxed);
        self.leaf_sumsq_per_item
            .fetch_add(scale(sumsq_per_item), Ordering::Relaxed);
        self.leaf_weight_sum
            .fetch_add(scale(count), Ordering::Relaxed);
        let prior = self.leaf_count.fetch_add(count, Ordering::Relaxed);
        let new_total = prior.wrapping_add(count);
        if (prior / SITE_CLASSIFY_QUANTUM) != (new_total / SITE_CLASSIFY_QUANTUM) {
            self.tick();
        }
    }

    /// Record a batch of leaves timed on the thread's own clock.
    ///
    /// Separate from [`Self::record_batch_weighted`] because these are
    /// the same leaves measured against a different clock, not more
    /// leaves: adding them to the leaf count would double it. They carry
    /// their own item count, so a site that reaches this path for some
    /// of its leaves and not others divides each figure by the items
    /// that figure actually covers.
    ///
    /// Raw counter ticks, unconverted. See the field comment: only a
    /// ratio is ever taken of these, and a ratio is what survives the
    /// unit being different from the elapsed clock's.
    pub(crate) fn record_oncore_batch(
        &'static self,
        oncore_sum: u64,
        oncore_sumsq_per_item: u64,
        items: u64,
    ) {
        if items == 0 {
            return;
        }
        self.leaf_oncore_sum.fetch_add(oncore_sum, Ordering::Relaxed);
        self.leaf_oncore_sumsq_per_item
            .fetch_add(oncore_sumsq_per_item, Ordering::Relaxed);
        self.leaf_oncore_items.fetch_add(items, Ordering::Relaxed);
    }

    /// cv^2 per mille of per-item cost measured on the thread's own
    /// clock, or `None` where no leaf carried an on-core reading.
    ///
    /// This is the figure a neighbor cannot move. Wall time rises both
    /// because the work is irregular and because the thread lost its
    /// core, and preemption lands on some leaves and not others, so it
    /// reaches a wall-time spread as variance that is indistinguishable
    /// from the work's own. A thread's own clock does not advance while
    /// the thread is off a core, so a preempted leaf reports what it
    /// cost rather than what it waited.
    ///
    /// `None` rather than zero: zero is the spread of perfectly uniform
    /// work, which is a reading, and a site whose leaves never reached
    /// the sampled path has no reading at all. `None` too where an item
    /// cost under one counter tick, as [`per_item_cv2`] says.
    pub fn per_item_oncore_cv2_per_mille(&self) -> Option<u64> {
        let items = self.leaf_oncore_items.load(Ordering::Relaxed);
        if items == 0 {
            return None;
        }
        let mean = self.leaf_oncore_sum.load(Ordering::Relaxed) / items;
        let sumsq = self.leaf_oncore_sumsq_per_item.load(Ordering::Relaxed);
        per_item_cv2(sumsq, mean, items)
    }

    /// Leaves' worth of items that carried an on-core reading.
    ///
    /// Beside [`Self::per_item_oncore_cv2_per_mille`] so a reader can
    /// weigh how much the spread rests on, and distinct from
    /// [`Self::leaf_count`], which counts every leaf however it was
    /// timed.
    pub fn oncore_items(&self) -> u64 {
        self.leaf_oncore_items.load(Ordering::Relaxed)
    }

    /// Coefficient-of-variation squared (parts-per-1000) over this
    /// site's cumulative leaf history, or `None` below 4 samples.
    /// Same fixed-point convention as the global
    /// [`crate::sched::split_observer::leaf_cv_squared_per_mille`], and
    /// `None` on the same further terms: a mean leaf under the 256 ns
    /// unit the squares are kept in has no spread that can be read.
    pub fn cv2_per_mille(&self) -> Option<u64> {
        let n = self.leaf_count.load(Ordering::Relaxed);
        if n < 4 {
            return None;
        }
        let sum = self.leaf_sum_ns.load(Ordering::Relaxed);
        let sumsq = self.leaf_sumsq_scaled.load(Ordering::Relaxed);
        // The sums carry each batch's weight as a factor, so the count
        // they divide by has to carry it too. `n` above is leaves, which
        // is what the four-sample guard is about.
        let n = self.leaf_weight_sum.load(Ordering::Relaxed).max(1);
        let mean_scaled = (sum >> 8) / n;
        if mean_scaled == 0 {
            return None;
        }
        let sumsq_per_n = sumsq / n;
        let mean_sq = mean_scaled.saturating_mul(mean_scaled);
        let var = sumsq_per_n.saturating_sub(mean_sq);
        Some(var.saturating_mul(1000) / mean_sq)
    }

    /// Whether the site has timed its 4 leaves and their mean is under
    /// the 256 ns unit [`Self::cv2_per_mille`] squares in, so that it has
    /// no spread to give for a measured reason rather than for want of
    /// leaves. A router can then decide from the mean it did measure.
    pub(crate) fn leaves_under_spread_unit(&self) -> bool {
        if self.leaf_count.load(Ordering::Relaxed) < 4 {
            return false;
        }
        let n = self.leaf_weight_sum.load(Ordering::Relaxed).max(1);
        (self.leaf_sum_ns.load(Ordering::Relaxed) >> 8) / n == 0
    }

    /// Total leaves recorded against this site.
    #[inline]
    pub fn leaf_count(&self) -> u64 {
        self.leaf_count.load(Ordering::Relaxed)
    }

    /// Summed wall time of the leaves recorded against this site, in
    /// nanoseconds, over its cumulative history: the batch-weighted
    /// mean leaf time times the leaf count, so a batch measured at
    /// partial occupancy counts for less. Zero while no batch has
    /// carried weight.
    pub fn leaf_sum_ns(&self) -> u64 {
        let weight = self.leaf_weight_sum.load(Ordering::Relaxed);
        if weight == 0 {
            return 0;
        }
        let mean = self.leaf_sum_ns.load(Ordering::Relaxed) / weight;
        mean.saturating_mul(self.leaf_count.load(Ordering::Relaxed))
    }

    /// Mean cost of one item at this site, in nanoseconds, over its
    /// cumulative history. `None` below 4 leaves, and `None` while no
    /// recorded leaf carried an item count.
    ///
    /// This is the figure a class can be decided from without the split
    /// moving it. The mean leaf time doubles when the scheduler runs
    /// leaves twice the size, and how finely it splits follows from the
    /// class the leaf times produced.
    pub fn per_item_ns(&self) -> Option<u64> {
        if self.leaf_count.load(Ordering::Relaxed) < 4 {
            return None;
        }
        let items = self.leaf_items.load(Ordering::Relaxed);
        if items == 0 {
            return None;
        }
        Some(self.leaf_sum_ns.load(Ordering::Relaxed) / items)
    }

    /// cv^2 per mille of per-item cost at this site, weighted by the
    /// items each leaf covered. `None` on the same terms as
    /// [`Self::per_item_ns`], and where the mean item cost under a
    /// nanosecond, which leaves no spread to read.
    ///
    /// The recorder keeps each leaf's squared time over its item count,
    /// so the sum of those less the mean squared times the items is the
    /// item-weighted sum of squared deviations, and the variance is that
    /// over the items rather than over the leaves. Leaves of mixed sizes
    /// running identical items read near zero here and read high in
    /// [`Self::cv2_per_mille`].
    pub fn per_item_cv2_per_mille(&self) -> Option<u64> {
        let leaves = self.leaf_count.load(Ordering::Relaxed);
        if leaves < 4 {
            return None;
        }
        let items = self.leaf_items.load(Ordering::Relaxed);
        if items == 0 {
            return None;
        }
        // The mean squares in 128 bits and scales once, matching how the
        // recorder forms each leaf's term: scaling the mean first would
        // subtract a smaller square than the terms carry and report the
        // difference as spread.
        let mean_ns = self.leaf_sum_ns.load(Ordering::Relaxed) / items;
        let sumsq_per_item = self.leaf_sumsq_per_item.load(Ordering::Relaxed);
        per_item_cv2(sumsq_per_item, mean_ns, items)
    }

    /// Mean cost of one item, in nanoseconds, over the delta window the
    /// latest classifier tick classified: the mean
    /// [`Self::learned_class`] was decided from. A window whose samples
    /// carried no item count reports its mean leaf time instead.
    /// `None` until a tick has classified a window. While ticks run it
    /// may come from a different tick than
    /// [`Self::window_cv2_per_mille`].
    pub fn window_mean_ns(&self) -> Option<u64> {
        if self.window_ticks.load(Ordering::Relaxed) == 0 {
            None
        } else {
            Some(self.window_mean_ns.load(Ordering::Relaxed))
        }
    }

    /// cv^2 per mille of per-item cost over the delta window the latest
    /// classifier tick classified, the variance [`Self::learned_class`]
    /// was decided from, as opposed to [`Self::cv2_per_mille`] over the
    /// site's whole life and over leaf times rather than items. It is the
    /// on-core clock's spread where the window carried one and wall
    /// time's otherwise; [`Self::window_wall_cv2_per_mille`] and
    /// [`Self::window_oncore_cv2_per_mille`] give the two apart. A window
    /// whose samples carried no item count reports the spread of its
    /// leaf times instead.
    ///
    /// It is the latest window whose spread could be read. A window whose
    /// mean is under the unit its spread is kept in has none and does not
    /// replace it, so this can be older than [`Self::window_mean_ns`].
    /// `None` until a window with a readable spread has been classified;
    /// [`Self::window_spread_ticks`] counts them.
    pub fn window_cv2_per_mille(&self) -> Option<u64> {
        if self.window_spread_ticks.load(Ordering::Relaxed) == 0 {
            None
        } else {
            Some(self.window_cv2.load(Ordering::Relaxed))
        }
    }

    /// Delta windows the site's classifier has classified.
    pub fn window_ticks(&self) -> u64 {
        self.window_ticks.load(Ordering::Relaxed)
    }

    /// Delta windows whose spread could be read, out of
    /// [`Self::window_ticks`]: the ones [`Self::window_cv2_per_mille`] and
    /// its range are taken over.
    pub fn window_spread_ticks(&self) -> u64 {
        self.window_spread_ticks.load(Ordering::Relaxed)
    }

    /// Smallest and largest per-window cv^2 the classifier has seen,
    /// over every tick rather than the latest one.
    ///
    /// [`Self::window_cv2_per_mille`] reports one tick of what is often
    /// thousands, and that figure spans 0 to 527 across identical runs,
    /// so it cannot say which regimes a run passed through. The range
    /// can: a maximum below the uniform edge says a spread-driven
    /// mechanism was never consulted in its own regime, whatever the
    /// last tick happened to hold. `None` until a window with a readable
    /// spread has been classified.
    pub fn window_cv2_range_per_mille(&self) -> Option<(u64, u64)> {
        if self.window_spread_ticks.load(Ordering::Relaxed) == 0 {
            None
        } else {
            Some((
                self.window_cv2_min.load(Ordering::Relaxed),
                self.window_cv2_max.load(Ordering::Relaxed),
            ))
        }
    }

    /// cv^2 per mille of per-item cost on wall time over the delta window
    /// the latest tick classified, whichever clock the classifier took its
    /// spread from; a window whose samples carried no item count reports
    /// its leaf times' spread. Where [`Self::window_oncore_cv2_per_mille`]
    /// answers for the same tick, the two are one window on two clocks.
    /// The latest window whose wall spread could be read, and `None`
    /// until one has been classified; [`Self::window_wall_ticks`] counts
    /// them.
    pub fn window_wall_cv2_per_mille(&self) -> Option<u64> {
        if self.window_wall_ticks.load(Ordering::Relaxed) == 0 {
            None
        } else {
            Some(self.window_wall_cv2.load(Ordering::Relaxed))
        }
    }

    /// Smallest and largest per-window wall cv^2 over every window whose
    /// wall spread could be read. `None` until one has been classified.
    pub fn window_wall_cv2_range_per_mille(&self) -> Option<(u64, u64)> {
        if self.window_wall_ticks.load(Ordering::Relaxed) == 0 {
            None
        } else {
            Some((
                self.window_wall_cv2_min.load(Ordering::Relaxed),
                self.window_wall_cv2_max.load(Ordering::Relaxed),
            ))
        }
    }

    /// Delta windows whose wall spread could be read, out of
    /// [`Self::window_ticks`].
    pub fn window_wall_ticks(&self) -> u64 {
        self.window_wall_ticks.load(Ordering::Relaxed)
    }

    /// cv^2 per mille of per-item cost on the thread's own clock over the
    /// latest delta window whose spread the classifier took from that
    /// clock: [`crate::sched::levers::oncore_spread`] on, and the window's
    /// leaves timed on the sampled path. `None` until such a window has
    /// been classified, so a run that took no on-core timing reads as
    /// nothing rather than as a spread of zero.
    pub fn window_oncore_cv2_per_mille(&self) -> Option<u64> {
        if self.window_oncore_ticks.load(Ordering::Relaxed) == 0 {
            None
        } else {
            Some(self.window_oncore_cv2.load(Ordering::Relaxed))
        }
    }

    /// Smallest and largest on-core cv^2 over the windows whose spread the
    /// classifier took from the on-core clock. `None` until such a window
    /// has been classified.
    pub fn window_oncore_cv2_range_per_mille(&self) -> Option<(u64, u64)> {
        if self.window_oncore_ticks.load(Ordering::Relaxed) == 0 {
            None
        } else {
            Some((
                self.window_oncore_cv2_min.load(Ordering::Relaxed),
                self.window_oncore_cv2_max.load(Ordering::Relaxed),
            ))
        }
    }

    /// Delta windows whose spread the classifier took from the on-core
    /// clock, out of [`Self::window_ticks`].
    pub fn window_oncore_ticks(&self) -> u64 {
        self.window_oncore_ticks.load(Ordering::Relaxed)
    }

    /// One classifier tick over the delta window since the previous
    /// tick. Same algorithm as the process-global
    /// `tick_auto_classify`: hysteresis [`SITE_MIGRATION_HYSTERESIS`]
    /// for adjacent-bucket moves, immediate migration when the
    /// observation sits at bucket distance >= 2 with >= 64 samples.
    fn tick(&self) {
        let count = self.leaf_count.load(Ordering::Relaxed);
        let sum = self.leaf_sum_ns.load(Ordering::Relaxed);
        let sumsq = self.leaf_sumsq_scaled.load(Ordering::Relaxed);

        let prev_count = self.last_count.load(Ordering::Relaxed);
        let dcount = count.saturating_sub(prev_count);
        if dcount < 4 {
            return;
        }
        let items = self.leaf_items.load(Ordering::Relaxed);
        let sumsq_per_item = self.leaf_sumsq_per_item.load(Ordering::Relaxed);
        let dsum = sum.saturating_sub(self.last_sum_ns.load(Ordering::Relaxed));
        let dsumsq = sumsq.saturating_sub(self.last_sumsq.load(Ordering::Relaxed));
        let ditems = items.saturating_sub(self.last_items.load(Ordering::Relaxed));
        let dsumsq_per_item =
            sumsq_per_item.saturating_sub(self.last_sumsq_per_item.load(Ordering::Relaxed));
        let weight_sum = self.leaf_weight_sum.load(Ordering::Relaxed);
        let dweight = weight_sum
            .saturating_sub(self.last_weight_sum.load(Ordering::Relaxed))
            .max(1);
        self.last_weight_sum.store(weight_sum, Ordering::Relaxed);
        self.last_count.store(count, Ordering::Relaxed);
        self.last_sum_ns.store(sum, Ordering::Relaxed);
        self.last_sumsq.store(sumsq, Ordering::Relaxed);
        self.last_items.store(items, Ordering::Relaxed);
        self.last_sumsq_per_item.store(sumsq_per_item, Ordering::Relaxed);

        // Per item, not per leaf. A leaf's time scales with the items in
        // it, and how many that is comes from the split, which follows
        // from the class this decides: classifying leaf times lets the
        // class hold itself in place. Dividing by the items the window
        // covered leaves a figure the split cannot move.
        //
        // A window whose samples carried no item count - the heartbeat's
        // serial spans - is classified on its leaf times, which is all
        // such a sample can say.
        // The window's on-core deltas, taken over the same leaves on the
        // thread's own clock. A spread computed from these is the work's
        // own irregularity; the same spread computed from wall time also
        // carries every leaf that lost its core, because preemption
        // lands on some leaves and not others and so arrives as variance
        // rather than as a level shift.
        let oncore_items = self.leaf_oncore_items.load(Ordering::Relaxed);
        let oncore_sum = self.leaf_oncore_sum.load(Ordering::Relaxed);
        let oncore_sumsq = self.leaf_oncore_sumsq_per_item.load(Ordering::Relaxed);
        let d_oncore_items =
            oncore_items.saturating_sub(self.last_oncore_items.load(Ordering::Relaxed));
        let d_oncore_sum =
            oncore_sum.saturating_sub(self.last_oncore_sum.load(Ordering::Relaxed));
        let d_oncore_sumsq = oncore_sumsq
            .saturating_sub(self.last_oncore_sumsq_per_item.load(Ordering::Relaxed));
        self.last_oncore_items.store(oncore_items, Ordering::Relaxed);
        self.last_oncore_sum.store(oncore_sum, Ordering::Relaxed);
        self.last_oncore_sumsq_per_item
            .store(oncore_sumsq, Ordering::Relaxed);

        // Ticks, not nanoseconds, and only ever a ratio is taken of
        // them, so the clock's unit never reaches a threshold.
        let oncore_cv2 = d_oncore_sum
            .checked_div(d_oncore_items)
            .and_then(|mean| per_item_cv2(d_oncore_sumsq, mean, d_oncore_items));

        let per_item = dsum.checked_div(ditems);
        // The mean stays on wall time, where the classifier's nanosecond
        // boundaries are stated, and so does the first spread here. The
        // classifier then takes its spread from the on-core clock where
        // the window carried one, and keeps the wall spread where it did
        // not, which is what a platform with no thread clock has always
        // had.
        let (mean_ns, wall_cv2) = if let Some(mean) = per_item {
            (mean, per_item_cv2(dsumsq_per_item, mean, ditems))
        } else {
            // Divided by the window's summed weight rather than its leaf
            // count, because the sums carry each batch's weight as a
            // factor and a leaf count does not.
            let mean = dsum / dweight;
            let scaled_mean = (dsum >> 8) / dweight;
            // A mean leaf under the 256 ns unit the squares are kept in
            // leaves no spread to read.
            let spread = (scaled_mean > 0).then(|| {
                let sumsq_per_n = dsumsq / dweight;
                let mean_sq = scaled_mean.saturating_mul(scaled_mean);
                let var = sumsq_per_n.saturating_sub(mean_sq);
                var.saturating_mul(1000) / mean_sq
            });
            (mean, spread)
        };
        let oncore_used = oncore_cv2.filter(|_| per_item.is_some());
        let cv2 = oncore_used.or(wall_cv2);
        self.window_mean_ns.store(mean_ns, Ordering::Relaxed);
        publish_count(&self.window_ticks);
        if let Some(cv2) = cv2 {
            self.window_cv2.store(cv2, Ordering::Relaxed);
            publish_min(&self.window_cv2_min, cv2);
            publish_max(&self.window_cv2_max, cv2);
            publish_count(&self.window_spread_ticks);
        }
        if let Some(wall) = wall_cv2 {
            self.window_wall_cv2.store(wall, Ordering::Relaxed);
            publish_min(&self.window_wall_cv2_min, wall);
            publish_max(&self.window_wall_cv2_max, wall);
            publish_count(&self.window_wall_ticks);
        }
        if let Some(oncore) = oncore_used {
            self.window_oncore_cv2.store(oncore, Ordering::Relaxed);
            publish_min(&self.window_oncore_cv2_min, oncore);
            publish_max(&self.window_oncore_cv2_max, oncore);
            publish_count(&self.window_oncore_ticks);
        }
        // A window whose class turns on a spread it could not show makes
        // no decision, and the active class and any pending run stand.
        let Some(observed) = classify_window(mean_ns, cv2) else {
            return;
        };
        let observed_tag = class_tag_encode(observed);

        let active = class_tag_decode(self.active_tag.load(Ordering::Relaxed));
        match active {
            None => {
                // First classification of a fresh site: adopt
                // immediately; the static classifier's plan-level
                // guess governed calls up to this point.
                self.active_tag.store(observed_tag, Ordering::Release);
                self.pending_tag.store(observed_tag, Ordering::Relaxed);
                self.pending_run.store(0, Ordering::Relaxed);
            }
            Some(active_class) if active_class != observed => {
                if crate::sched::adaptive_profile::class_bucket_distance(
                    active_class, observed,
                ) >= 2
                    && dcount >= 64
                {
                    self.active_tag.store(observed_tag, Ordering::Release);
                    self.pending_tag.store(observed_tag, Ordering::Relaxed);
                    self.pending_run.store(0, Ordering::Relaxed);
                    return;
                }
                let pending = self.pending_tag.load(Ordering::Relaxed);
                if pending == observed_tag {
                    let run = self
                        .pending_run
                        .fetch_add(1, Ordering::Relaxed)
                        .saturating_add(1);
                    if run >= SITE_MIGRATION_HYSTERESIS {
                        self.active_tag.store(observed_tag, Ordering::Release);
                    }
                } else {
                    self.pending_tag.store(observed_tag, Ordering::Relaxed);
                    self.pending_run.store(1, Ordering::Relaxed);
                }
            }
            Some(_) => {
                // Observation agrees with the active class; reset any
                // stale pending streak toward a different class.
                self.pending_tag.store(observed_tag, Ordering::Relaxed);
                self.pending_run.store(0, Ordering::Relaxed);
            }
        }
    }

    // -----------------------------------------------------------------
    // Policy-arm A/B surface
    // -----------------------------------------------------------------

    /// Pick a policy arm for this dispatch. `alternative_allowed`
    /// gates the alternative arm behind the consuming site's
    /// precondition (e.g. the heartbeat gate requires high cv^2);
    /// when it is false the default arm is returned unconditionally
    /// and no trial fires.
    ///
    /// Selection order when the alternative is allowed:
    /// 1. Either arm below [`ARM_MIN_SAMPLES`]: pick the
    ///    lesser-sampled arm (bounded exploration).
    /// 2. Every [`ARM_TRIAL_CADENCE`]th call: pick the arm the EWMA
    ///    comparison does not prefer (drift detection).
    /// 3. Otherwise: the arm with the lower EWMA wall time.
    pub fn choose_arm(&self, alternative_allowed: bool) -> PolicyArm {
        if !alternative_allowed {
            return PolicyArm::Default;
        }
        let calls = self.arm_calls.fetch_add(1, Ordering::Relaxed);
        let s0 = self.arm_samples[0].load(Ordering::Relaxed);
        let s1 = self.arm_samples[1].load(Ordering::Relaxed);
        if s0 < ARM_MIN_SAMPLES || s1 < ARM_MIN_SAMPLES {
            return if s1 < s0 {
                PolicyArm::Alternative
            } else {
                PolicyArm::Default
            };
        }
        let e0 = ewma_value(&self.arm_ewma_ns[0]);
        let e1 = ewma_value(&self.arm_ewma_ns[1]);
        let best = if e1 < e0 {
            PolicyArm::Alternative
        } else {
            PolicyArm::Default
        };
        if calls % ARM_TRIAL_CADENCE == ARM_TRIAL_CADENCE - 1 {
            // Trial tick: run the non-preferred arm.
            return match best {
                PolicyArm::Default => PolicyArm::Alternative,
                PolicyArm::Alternative => PolicyArm::Default,
            };
        }
        best
    }

    /// Pick whether this dispatch runs the routing the learned class
    /// re-derives ([`PolicyArm::Default`]) or the plan as the caller
    /// built it ([`PolicyArm::Alternative`]).
    ///
    /// Same selection as [`Self::choose_arm`] and for the same reason,
    /// on its own counters: explore until both arms have
    /// [`ARM_MIN_SAMPLES`], then take the lower EWMA, and every
    /// [`ARM_TRIAL_CADENCE`]th call run the other one so a routing that
    /// stopped being the better choice is found rather than assumed.
    ///
    /// A class is a description of the work and this is the check on
    /// what that description costs. Where the two disagree the
    /// measurement wins, which is what keeps a class that has gone
    /// wrong from being expensive as well as wrong.
    pub fn choose_routing_arm(&self) -> PolicyArm {
        let calls = self.routing_calls.fetch_add(1, Ordering::Relaxed);
        let s0 = self.routing_samples[0].load(Ordering::Relaxed);
        let s1 = self.routing_samples[1].load(Ordering::Relaxed);
        if s0 < ARM_MIN_SAMPLES || s1 < ARM_MIN_SAMPLES {
            return if s1 < s0 {
                PolicyArm::Alternative
            } else {
                PolicyArm::Default
            };
        }
        let e0 = ewma_value(&self.routing_ewma_ns[0]);
        let e1 = ewma_value(&self.routing_ewma_ns[1]);
        let best = if e1 < e0 {
            PolicyArm::Alternative
        } else {
            PolicyArm::Default
        };
        if calls % ARM_TRIAL_CADENCE == ARM_TRIAL_CADENCE - 1 {
            return match best {
                PolicyArm::Default => PolicyArm::Alternative,
                PolicyArm::Alternative => PolicyArm::Default,
            };
        }
        best
    }

    /// Record one dispatch's wall time under the routing arm it ran.
    pub fn record_routing_arm(&self, arm: PolicyArm, wall_ns: u64) {
        let i = arm.idx();
        ewma_update(&self.routing_ewma_ns[i], wall_ns);
        self.routing_samples[i].fetch_add(1, Ordering::Relaxed);
    }

    /// Current per-arm EWMA wall times for the routing decision,
    /// `(class_derived_ns, caller_plan_ns)`; zero means no samples yet.
    /// Diagnostics and tests.
    pub fn routing_ewmas(&self) -> (u64, u64) {
        (
            ewma_value(&self.routing_ewma_ns[0]),
            ewma_value(&self.routing_ewma_ns[1]),
        )
    }

    /// Record one dispatch's wall time under `arm`.
    pub fn record_arm(&self, arm: PolicyArm, wall_ns: u64) {
        let i = arm.idx();
        ewma_update(&self.arm_ewma_ns[i], wall_ns);
        self.arm_samples[i].fetch_add(1, Ordering::Relaxed);
    }

    /// Current per-arm EWMA wall times `(default_ns, alternative_ns)`;
    /// zero means "no samples yet". Diagnostics + tests.
    pub fn arm_ewmas(&self) -> (u64, u64) {
        (
            ewma_value(&self.arm_ewma_ns[0]),
            ewma_value(&self.arm_ewma_ns[1]),
        )
    }

    // -----------------------------------------------------------------
    // Hybrid placement surface
    // -----------------------------------------------------------------

    /// Log2 size bucket for a batch size.
    #[inline]
    fn bucket(batch: u32) -> usize {
        (63 - (batch.max(1) as u64).leading_zeros() as usize).min(PLACEMENT_BUCKETS - 1)
    }

    /// Placement decision for a dispatch of `batch` items: `Race`
    /// while the bucket is cold (either side unmeasured) and on every
    /// [`PLACEMENT_REPROBE_CADENCE`]th call, otherwise whichever side
    /// has the lower end-to-end EWMA.
    pub fn choose_placement(&self, batch: u32) -> Placement {
        let b = Self::bucket(batch);
        let calls = self.place_calls[b].fetch_add(1, Ordering::Relaxed);
        let cpu = ewma_value(&self.place_cpu_ns[b]);
        let dev = ewma_value(&self.place_backend_ns[b]);
        if cpu == 0 || dev == 0 {
            return Placement::Race;
        }
        if calls % PLACEMENT_REPROBE_CADENCE == PLACEMENT_REPROBE_CADENCE - 1 {
            return Placement::Race;
        }
        if cpu <= dev { Placement::Cpu } else { Placement::Backend }
    }

    /// Record measured wall times for a dispatch of `batch` items.
    /// Either side may be `None` when only one side ran.
    pub fn record_placement(
        &self,
        batch: u32,
        cpu_ns: Option<u64>,
        backend_ns: Option<u64>,
    ) {
        let b = Self::bucket(batch);
        if let Some(ns) = cpu_ns {
            ewma_update(&self.place_cpu_ns[b], ns);
        }
        if let Some(ns) = backend_ns {
            ewma_update(&self.place_backend_ns[b], ns);
        }
    }

    /// Current placement EWMAs `(cpu_ns, backend_ns)` for the bucket
    /// covering `batch`; zero means unmeasured. Diagnostics + tests.
    pub fn placement_ewmas(&self, batch: u32) -> (u64, u64) {
        let b = Self::bucket(batch);
        (
            ewma_value(&self.place_cpu_ns[b]),
            ewma_value(&self.place_backend_ns[b]),
        )
    }

    /// CPU share of a divisible workload per the learned per-item
    /// throughputs, in parts-per-1000. 500 (an even split) until both
    /// sides have measurements. CPU share = backend_ns_per_item /
    /// (cpu_ns_per_item + backend_ns_per_item): the faster side gets
    /// the larger share.
    pub fn split_cpu_share_per_mille(&self) -> u32 {
        let c = ewma_value(&self.split_cpu_ns_per_item);
        let g = ewma_value(&self.split_backend_ns_per_item);
        if c == 0 || g == 0 {
            return 500;
        }
        let total = c.saturating_add(g).max(1);
        ((g.saturating_mul(1000)) / total).clamp(50, 950) as u32
    }

    /// Record per-item throughput observations from a split dispatch.
    pub fn record_split(
        &self,
        cpu_items: usize,
        cpu_ns: u64,
        backend_items: usize,
        backend_ns: u64,
    ) {
        if cpu_items > 0 {
            ewma_update(
                &self.split_cpu_ns_per_item,
                (cpu_ns / cpu_items as u64).max(1),
            );
        }
        if backend_items > 0 {
            ewma_update(
                &self.split_backend_ns_per_item,
                (backend_ns / backend_items as u64).max(1),
            );
        }
    }

    /// [`Self::split_cpu_share_per_mille`] for a dispatch of `n`
    /// items: the bucket covering `n` when it has measurements on
    /// both sides, the site-wide model otherwise.
    pub fn split_cpu_share_per_mille_for(&self, n: u32) -> u32 {
        let b = Self::bucket(n);
        let c = ewma_value(&self.split_cpu_ns_per_item_by_size[b]);
        let g = ewma_value(&self.split_backend_ns_per_item_by_size[b]);
        if c == 0 || g == 0 {
            return self.split_cpu_share_per_mille();
        }
        let total = c.saturating_add(g).max(1);
        ((g.saturating_mul(1000)) / total).clamp(50, 950) as u32
    }

    /// [`Self::record_split`] for a dispatch of `n` items: updates the
    /// bucket covering `n` and the site-wide model.
    pub fn record_split_for(
        &self,
        n: u32,
        cpu_items: usize,
        cpu_ns: u64,
        backend_items: usize,
        backend_ns: u64,
    ) {
        let b = Self::bucket(n);
        if cpu_items > 0 {
            ewma_update(
                &self.split_cpu_ns_per_item_by_size[b],
                (cpu_ns / cpu_items as u64).max(1),
            );
        }
        if backend_items > 0 {
            ewma_update(
                &self.split_backend_ns_per_item_by_size[b],
                (backend_ns / backend_items as u64).max(1),
            );
        }
        self.record_split(cpu_items, cpu_ns, backend_items, backend_ns);
    }

    /// Whether the reduce-cost observer still wants a calibration
    /// sample (bounded at 16; after convergence the caller skips
    /// the timed merge entirely).
    pub fn reduce_cost_wants_sample(&self) -> bool {
        self.reduce_cost_samples.load(Ordering::Relaxed) < REDUCE_COST_MAX_SAMPLES
    }

    /// Record one timed `reduce(a, b)` merge in TSC cycles.
    pub fn record_reduce_cost_sample(&self, cycles: u64) {
        self.reduce_cost_sum_cycles.fetch_add(cycles, Ordering::Relaxed);
        self.reduce_cost_samples.fetch_add(1, Ordering::Relaxed);
    }

    /// Average observed reduce-merge cost in TSC cycles, or `None`
    /// below 4 samples (cold: the caller defaults to the
    /// always-correct bisect path).
    pub fn reduce_cost_avg_cycles(&self) -> Option<u64> {
        let n = self.reduce_cost_samples.load(Ordering::Relaxed);
        if n < REDUCE_COST_MIN_SAMPLES {
            return None;
        }
        Some(self.reduce_cost_sum_cycles.load(Ordering::Relaxed) / n as u64)
    }
}

/// Reduce-cost observer bounds: sample until 16 merges are timed,
/// trust the average from 4.
const REDUCE_COST_MAX_SAMPLES: u32 = 16;
const REDUCE_COST_MIN_SAMPLES: u32 = 4;

impl core::fmt::Debug for CallSiteState {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("CallSiteState")
            .field("learned_class", &self.learned_class())
            .field("leaf_count", &self.leaf_count())
            .field("cv2_per_mille", &self.cv2_per_mille())
            .finish()
    }
}

/// Copyable handle to a `'static` [`CallSiteState`], with
/// pointer-identity equality/hash so [`crate::sched::JobPlan`] keeps
/// its `PartialEq / Eq / Hash` derives (the state's atomics have no
/// value equality; two sites are "equal" only when they are the same
/// static).
#[derive(Clone, Copy)]
pub struct SiteRef(&'static CallSiteState);

impl SiteRef {
    /// Wrap a `'static` site.
    #[inline]
    pub const fn new(site: &'static CallSiteState) -> Self {
        Self(site)
    }

    /// Access the underlying state.
    #[inline]
    pub fn get(self) -> &'static CallSiteState {
        self.0
    }
}

impl PartialEq for SiteRef {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        core::ptr::eq(self.0, other.0)
    }
}
impl Eq for SiteRef {}
impl core::hash::Hash for SiteRef {
    #[inline]
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        (self.0 as *const CallSiteState as usize).hash(state);
    }
}
impl core::fmt::Debug for SiteRef {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "SiteRef({:p})", self.0 as *const CallSiteState)
    }
}

/// Location-keyed site registry. Value-keyed on (file, line,
/// column) rather than the `&'static Location` address: the same
/// textual call site inside a generic caller can surface as
/// distinct `Location` constants per instantiation, and the value
/// key merges those back into one site.
/// The location is kept beside the state so the registry can say where
/// each site is. The key is a hash and cannot be turned back into a
/// file and line, and `CallSiteState` holds no location of its own, so
/// without this a walk of the registry answers a list of anonymous
/// counters. One pointer per site, stored once when the location is
/// first seen; the dispatch path reads through a thread-local one-slot
/// cache and does not touch this map at all.
struct SiteNode {
    key: u64,
    location: &'static std::panic::Location<'static>,
    state: &'static CallSiteState,
}

/// Slot count of the site table. Call sites are source locations in
/// the binary, so the population is fixed at compile time and small;
/// 2048 slots is 16 KiB of zeroed bss and leaves the table under an
/// eighth full for a crate with a couple of hundred dispatch sites.
/// Linear probing wants the headroom: a table near capacity walks a
/// long run of occupied slots on every miss.
const SITE_SLOTS: usize = 2048;

/// Open-addressed, insert-once, never-removed table of leaked nodes.
///
/// A slot is empty exactly when its pointer is null, and a node is
/// fully built before the pointer that publishes it, so a non-null
/// slot is always a complete entry and a reader needs one atomic load
/// per probe and no lock. Nothing is ever removed or rehashed, so a
/// pointer a reader holds stays valid for the life of the process.
static SITE_TABLE: [AtomicPtr<SiteNode>; SITE_SLOTS] =
    [const { AtomicPtr::new(core::ptr::null_mut()) }; SITE_SLOTS];

/// The number of sites the table failed to hold. Non-zero means
/// [`SITE_SLOTS`] was reached and later sites are running on
/// unshared one-off state, which reads as every call being a first
/// call. Silence here would make that look like ordinary behavior.
static SITE_TABLE_OVERFLOW: AtomicU64 = AtomicU64::new(0);

/// A node in one of the site tables: the key it was inserted under and
/// the state it holds.
trait TableNode: 'static {
    fn key(&self) -> u64;
    fn state(&self) -> &'static CallSiteState;
}

impl TableNode for SiteNode {
    fn key(&self) -> u64 {
        self.key
    }

    fn state(&self) -> &'static CallSiteState {
        self.state
    }
}

/// The node for `key` in `table`, probing from `start`, or `None` with
/// the probe stopping at the first empty slot, which is where an insert
/// for this key would go. `table`'s length is a power of two.
fn table_lookup<N: TableNode>(
    table: &'static [AtomicPtr<N>],
    start: usize,
    key: u64,
) -> Option<&'static N> {
    let mask = table.len() - 1;
    let mut idx = start & mask;
    for _ in 0..table.len() {
        let p = table[idx].load(Ordering::Acquire);
        if p.is_null() {
            return None;
        }
        // SAFETY: a non-null slot holds a leaked node that is never
        // freed, moved or rehashed.
        let node = unsafe { &*p };
        if node.key() == key {
            return Some(node);
        }
        idx = (idx + 1) & mask;
    }
    None
}

/// The state for `key` in `table`, probing from `start` and inserting
/// the node `build` makes if the key is not yet present, or `None` when
/// every slot is taken by another key. Racing inserters of the same key
/// all return the one node that won its slot.
fn table_insert<N: TableNode>(
    table: &'static [AtomicPtr<N>],
    start: usize,
    key: u64,
    build: impl Fn() -> N,
) -> Option<&'static CallSiteState> {
    // Built at most once and only on reaching an empty slot, so a
    // caller that finds the key already present allocates nothing. A
    // racer that loses its CAS and then finds its key further along
    // abandons the node it prepared; that is bounded by the number of
    // threads meeting one new key at the same moment.
    let mut prepared: Option<&'static N> = None;
    let mask = table.len() - 1;
    let mut idx = start & mask;
    for _ in 0..table.len() {
        let p = table[idx].load(Ordering::Acquire);
        if p.is_null() {
            let node = *prepared.get_or_insert_with(|| &*Box::leak(Box::new(build())));
            let fresh = node as *const N as *mut N;
            match table[idx].compare_exchange(
                core::ptr::null_mut(),
                fresh,
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => return Some(node.state()),
                Err(taken) => {
                    // SAFETY: as in table_lookup.
                    let other = unsafe { &*taken };
                    if other.key() == key {
                        return Some(other.state());
                    }
                }
            }
        } else {
            // SAFETY: as in table_lookup.
            let node = unsafe { &*p };
            if node.key() == key {
                return Some(node.state());
            }
        }
        idx = (idx + 1) & mask;
    }
    None
}

/// The node for a location's `key`, or `None` with the probe stopping
/// at the first empty slot.
fn site_lookup(key: u64) -> Option<&'static SiteNode> {
    table_lookup(&SITE_TABLE, key as usize, key)
}

/// The state for a location's `key`, inserting one that records `loc`
/// if the key is not yet present.
fn site_insert(key: u64, loc: &'static std::panic::Location<'static>) -> &'static CallSiteState {
    let inserted = table_insert(&SITE_TABLE, key as usize, key, || SiteNode {
        key,
        location: loc,
        state: Box::leak(Box::new(CallSiteState::new())),
    });
    match inserted {
        Some(state) => state,
        None => {
            SITE_TABLE_OVERFLOW.fetch_add(1, Ordering::Relaxed);
            Box::leak(Box::new(CallSiteState::new()))
        }
    }
}

/// A site that a caller outside this binary named by a key of its own.
struct KeyNode {
    key: u64,
    state: &'static CallSiteState,
}

impl TableNode for KeyNode {
    fn key(&self) -> u64 {
        self.key
    }

    fn state(&self) -> &'static CallSiteState {
        self.state
    }
}

/// Slot count of the key table: 32 KiB of zeroed bss, room for a
/// caller that keys by kernel, phase and size class at once.
const KEY_SLOTS: usize = 4096;

/// Caller-keyed sites, in a table of their own rather than the
/// location table. The locations are fixed when the binary is built,
/// so the code bounds that table's population; a caller's keys are
/// bounded by nothing here, and a caller that mints many must not be
/// able to fill the table the scheduler's own call sites live in.
static KEY_TABLE: [AtomicPtr<KeyNode>; KEY_SLOTS] =
    [const { AtomicPtr::new(core::ptr::null_mut()) }; KEY_SLOTS];

/// The number of keys the key table failed to hold.
static KEY_TABLE_OVERFLOW: AtomicU64 = AtomicU64::new(0);

/// The one site every key past the key table's capacity shares.
///
/// Shared rather than one per key, because a caller's keys are
/// unbounded and a state per key past capacity would leak without
/// limit. Those keys then learn together, as jobs with no key would,
/// and the overflow count says so.
static KEY_OVERFLOW_SITE: CallSiteState = CallSiteState::new();

/// Where a key's probe starts. A caller packs whatever it likes into
/// 64 bits, often small counters or fields in the high bits, so the
/// key is spread by a multiplicative hash; a location's key is a hash
/// already and is used as it is.
fn key_slot(key: u64) -> usize {
    (key.wrapping_mul(0x9E37_79B9_7F4A_7C15) >> 32) as usize
}

/// The site for a key chosen by a caller outside this binary,
/// allocating the state on first sight of the key.
///
/// For work whose dispatch has no source location of its own: every job
/// another native library hands to a chunk runner reaches the pool
/// through that runner's one dispatch call, so without a key they would
/// all learn at that call. Jobs passed the same key learn together and
/// jobs passed different keys learn apart. A key never names a
/// location's site, whatever its value, since the two live in separate
/// tables.
pub fn site_for_key(key: u64) -> SiteRef {
    let slot = key_slot(key);
    if let Some(node) = table_lookup(&KEY_TABLE, slot, key) {
        return SiteRef::new(node.state);
    }
    let inserted = table_insert(&KEY_TABLE, slot, key, || KeyNode {
        key,
        state: Box::leak(Box::new(CallSiteState::new())),
    });
    match inserted {
        Some(state) => SiteRef::new(state),
        None => {
            KEY_TABLE_OVERFLOW.fetch_add(1, Ordering::Relaxed);
            SiteRef::new(&KEY_OVERFLOW_SITE)
        }
    }
}

/// How many keys the key table could not hold, each of which learned at
/// the one shared overflow site. Zero until [`KEY_SLOTS`] distinct keys
/// have been seen.
pub fn keyed_site_table_overflow() -> u64 {
    KEY_TABLE_OVERFLOW.load(Ordering::Relaxed)
}

/// How many sites the table could not hold. Zero on every table that
/// has not reached [`SITE_SLOTS`] distinct call sites.
pub fn site_table_overflow() -> u64 {
    SITE_TABLE_OVERFLOW.load(Ordering::Relaxed)
}

fn location_key(loc: &'static std::panic::Location<'static>) -> u64 {
    use core::hash::{Hash, Hasher};
    let mut h = std::hash::DefaultHasher::new();
    loc.file().hash(&mut h);
    loc.line().hash(&mut h);
    loc.column().hash(&mut h);
    h.finish()
}

/// The site handle for the CALLER's source location. Every
/// dispatch entry uses this (via `#[track_caller]` chaining) to
/// attach automatic per-call-site identity; user code normally
/// never calls it directly, but it is the way to read back what a
/// specific call site learned without switching that site to an
/// explicit [`crate::sched::JobPlan::with_site`] attachment.
#[track_caller]
#[inline]
pub fn caller_site() -> SiteRef {
    site_for_location(std::panic::Location::caller())
}

/// Resolve a source location to its `'static` site, allocating the
/// state on first sight of the location.
///
/// Fast path: a per-thread one-slot cache keyed on the `Location`
/// address (two thread-local loads). Miss path: value-hash of
/// (file, line, column) probed in the site table, one atomic load per
/// probe. A location seen for the first time process-wide is
/// published into an empty slot by a single compare-exchange; every
/// other caller only reads.
pub fn site_for_location(loc: &'static std::panic::Location<'static>) -> SiteRef {
    thread_local! {
        static LAST: core::cell::Cell<(usize, usize)> = const { core::cell::Cell::new((0, 0)) };
    }
    let loc_addr = loc as *const _ as usize;
    let cached = LAST.with(|c| c.get());
    if cached.0 == loc_addr && cached.1 != 0 {
        // SAFETY: the cache only ever stores pointers to
        // `Box::leak`ed `CallSiteState` values inserted below, so
        // the referent is 'static and valid.
        return SiteRef::new(unsafe { &*(cached.1 as *const CallSiteState) });
    }
    let key = location_key(loc);
    let site: &'static CallSiteState = match site_lookup(key) {
        Some(node) => node.state,
        None => site_insert(key, loc),
    };
    LAST.with(|c| c.set((loc_addr, site as *const CallSiteState as usize)));
    SiteRef::new(site)
}

/// Number of distinct call sites the registry has materialized.
#[cfg(test)]
pub(crate) fn registry_len() -> usize {
    site_nodes().count()
}

/// Every node the table holds, in slot order.
///
/// Slot order is the hash's and carries no meaning. A scan can see an
/// insert that lands in an earlier slot after it has passed that slot,
/// so a walk answers the sites present at some point during it rather
/// than an instant's snapshot. Nothing is ever removed, so a site the
/// scan does report was really there.
fn site_nodes() -> impl Iterator<Item = &'static SiteNode> {
    table_nodes(&SITE_TABLE)
}

/// Every node `table` holds, in slot order, on the terms of
/// [`site_nodes`].
fn table_nodes<N: TableNode>(table: &'static [AtomicPtr<N>]) -> impl Iterator<Item = &'static N> {
    table.iter().filter_map(|slot| {
        let p = slot.load(Ordering::Acquire);
        if p.is_null() {
            None
        } else {
            // SAFETY: as in table_lookup.
            Some(unsafe { &*p })
        }
    })
}

/// One call site the registry has materialized, and where it is.
#[derive(Copy, Clone, Debug)]
pub struct RegisteredSite {
    /// The source location the site was first seen at.
    ///
    /// The same textual site inside a generic caller can surface as
    /// distinct `Location` constants per instantiation, and the
    /// registry merges those onto one entry, so this is whichever of
    /// them arrived first. They agree on file, line and column, which
    /// is what the entry is keyed on.
    pub location: &'static std::panic::Location<'static>,
    /// What that site has learned.
    pub site: SiteRef,
}

/// Every call site the registry holds, each with where it is.
///
/// Order is the table's and carries no meaning; sort on the location.
pub fn registered_sites() -> Vec<RegisteredSite> {
    site_nodes()
        .map(|node| RegisteredSite {
            location: node.location,
            site: SiteRef::new(node.state),
        })
        .collect()
}

/// One site a caller outside this binary named by a key, and the key.
#[derive(Copy, Clone, Debug)]
pub struct RegisteredKeyedSite {
    /// The key its caller passed.
    pub key: u64,
    /// What that site has learned.
    pub site: SiteRef,
}

/// Every keyed site the registry holds, each with its key.
///
/// Order is the table's and carries no meaning; sort on the key. Keys
/// past the table's capacity share one site that has no key and is not
/// listed here; [`keyed_site_table_overflow`] counts them.
pub fn registered_keyed_sites() -> Vec<RegisteredKeyedSite> {
    table_nodes(&KEY_TABLE)
        .map(|node| RegisteredKeyedSite {
            key: node.key,
            site: SiteRef::new(node.state),
        })
        .collect()
}

/// Resets every site in the registry, the located and the keyed, and
/// the keyed overflow site once a key has reached it, answering how
/// many.
///
/// For measuring two arms in one process: see [`CallSiteState::reset`]
/// for why the alternative, a process each, measures something else.
///
/// The sweep blocks nothing and is a few tens of atomic stores per
/// site. It is not an instant: a site registered while the walk is
/// past its slot keeps the counters it had, so reset before starting
/// the arm rather than alongside it.
pub fn reset_all_sites() -> usize {
    let mut swept = 0;
    for node in site_nodes() {
        node.state.reset();
        swept += 1;
    }
    for node in table_nodes(&KEY_TABLE) {
        node.state.reset();
        swept += 1;
    }
    if keyed_site_table_overflow() > 0 {
        KEY_OVERFLOW_SITE.reset();
        swept += 1;
    }
    swept
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_batch_that_held_its_cores_weighs_a_whole_batch() {
        assert_eq!(batch_weight_per_mille(8_000, 8_000), Some(FULL_WEIGHT));
        assert_eq!(batch_weight_per_mille(4_800, 8_000), Some(600));
    }

    #[test]
    fn a_weight_resolves_a_single_part_per_mille() {
        // The occupancy printed beside a dispatch is in hundredths, so a
        // weight taken from it would move in steps of ten per mille and
        // read both of these as zero. One percent of a batch is the
        // difference between a pool that held its cores and one that did
        // not, which is the distinction the weight exists to carry.
        assert_eq!(batch_weight_per_mille(1, 1_000), Some(1));
        assert_eq!(batch_weight_per_mille(999, 1_000), Some(999));
    }

    #[test]
    fn a_boosted_clock_pair_weighs_one_batch_and_not_more() {
        // On Windows the two counts come from different clocks: executed
        // cycles against a fixed-rate timestamp counter, so a core above
        // its base frequency reads over one. A generous host must not
        // make a batch count for more than a batch.
        assert_eq!(batch_weight_per_mille(12_000, 8_000), Some(FULL_WEIGHT));
    }

    #[test]
    fn an_interval_that_never_elapsed_has_no_weight_rather_than_none_of_one() {
        // Zero is the weight of a batch that got no core at all. Handing
        // it back for a pair that measured nothing would make a platform
        // without the clock read as permanently contended, which is the
        // defect the ThreadTicks type exists to prevent.
        assert_eq!(batch_weight_per_mille(0, 0), None);
        assert_eq!(batch_weight_per_mille(5_000, 0), None);
        assert_eq!(batch_weight_per_mille(0, 8_000), Some(0));
    }

    #[test]
    fn the_range_keeps_a_tick_the_latest_reading_has_already_lost() {
        // Two windows of different spread, in order. window_cv2 holds
        // the second and nothing recoverable from it says the first
        // happened, which is how a run through a high-variance regime
        // reports as uniform. The range holds both.
        static S: CallSiteState = CallSiteState::new();
        const ITEMS: u64 = 1024;
        let sq = |ns: u64| (ns >> 8).saturating_mul(ns >> 8);
        let per_item_sq =
            |ns: u64| ((ns as u128).saturating_mul(ns as u128) / ((ITEMS as u128) << 16)) as u64;

        // Spread: half the leaves at 1280 ns an item, half at 1920.
        let (fast, slow) = (1280u64 * ITEMS, 1920u64 * ITEMS);
        S.record_batch_site_only(
            8 * fast + 8 * slow,
            8 * sq(fast) + 8 * sq(slow),
            16,
            16 * ITEMS,
            8 * per_item_sq(fast) + 8 * per_item_sq(slow),
        );
        let spread_tick = S.window_cv2_per_mille().expect("a window was classified");
        assert!(spread_tick > 0, "the first window carries a spread");

        // Flat: every leaf the same, so this window's cv^2 is zero.
        let flat = 1600u64 * ITEMS;
        S.record_batch_site_only(
            16 * flat,
            16 * sq(flat),
            16,
            16 * ITEMS,
            16 * per_item_sq(flat),
        );
        assert_eq!(
            S.window_cv2_per_mille(),
            Some(0),
            "the latest reading is the flat window and says nothing of the first"
        );

        let (lo, hi) = S.window_cv2_range_per_mille().expect("two windows were classified");
        assert_eq!(lo, 0, "the flat window is the smallest seen");
        assert_eq!(hi, spread_tick, "and the spread one survives in the range");
    }

    #[test]
    fn a_per_item_spread_is_read_at_its_own_value() {
        // Sixteen leaves of 1024 items: eight at 1280 ns per item, eight
        // at 1920. Mean 1600, standard deviation 320, cv^2 exactly 40
        // per mille. The recorder's per-leaf term, ns^2 / (items << 16),
        // is 25600 and 57600 for the two leaf kinds, both exact. The
        // batch crosses the classify quantum, so it ticks once.
        static S: CallSiteState = CallSiteState::new();
        const ITEMS: u64 = 1024;
        let (fast, slow) = (1280u64 * ITEMS, 1920u64 * ITEMS);
        let sq = |ns: u64| (ns >> 8).saturating_mul(ns >> 8);
        let per_item_sq = |ns: u64| {
            ((ns as u128).saturating_mul(ns as u128) / ((ITEMS as u128) << 16)) as u64
        };
        S.record_batch_site_only(
            8 * fast + 8 * slow,
            8 * sq(fast) + 8 * sq(slow),
            16,
            16 * ITEMS,
            8 * per_item_sq(fast) + 8 * per_item_sq(slow),
        );
        assert_eq!(S.window_mean_ns(), Some(1600));
        assert_eq!(S.per_item_ns(), Some(1600));
        // An integer variance in between reads 25 here: one unit of
        // 1000 / (1600^2 >> 16), which is the wrong side of the 50 edge
        // from the value the leaves carry.
        assert_eq!(S.window_cv2_per_mille(), Some(40));
        assert_eq!(S.per_item_cv2_per_mille(), Some(40));
        let low = crate::sched::adaptive_profile::class_thresholds()
            .cv2_low_per_mille
            .load(Ordering::Relaxed);
        if 40 < low {
            assert_eq!(S.learned_class(), Some(WorkloadClass::Streaming));
        }
    }

    #[test]
    fn a_window_reports_its_spread_on_each_clock() {
        // The leaves of the test above on wall time, cv^2 40 per mille,
        // and the same leaves flat on the thread's own clock. The
        // classifier takes the on-core spread and the wall one stays
        // readable beside it.
        static S: CallSiteState = CallSiteState::new();
        const ITEMS: u64 = 1024;
        let (fast, slow) = (1280u64 * ITEMS, 1920u64 * ITEMS);
        let flat = 1600u64 * ITEMS;
        let sq = |ns: u64| (ns >> 8).saturating_mul(ns >> 8);
        let per_item_sq =
            |ns: u64| ((ns as u128).saturating_mul(ns as u128) / ((ITEMS as u128) << 16)) as u64;
        S.record_oncore_batch(16 * flat, 16 * per_item_sq(flat), 16 * ITEMS);
        S.record_batch_site_only(
            8 * fast + 8 * slow,
            8 * sq(fast) + 8 * sq(slow),
            16,
            16 * ITEMS,
            8 * per_item_sq(fast) + 8 * per_item_sq(slow),
        );
        assert_eq!(S.window_wall_cv2_per_mille(), Some(40));
        assert_eq!(S.window_oncore_cv2_per_mille(), Some(0));
        assert_eq!(
            S.window_cv2_per_mille(),
            Some(0),
            "the classifier used the on-core spread"
        );
        assert_eq!(S.window_oncore_ticks(), 1);
        assert_eq!(S.window_wall_cv2_range_per_mille(), Some((40, 40)));
        assert_eq!(S.window_oncore_cv2_range_per_mille(), Some((0, 0)));

        S.reset();
        assert_eq!(S.window_wall_cv2_per_mille(), None);
        assert_eq!(S.window_oncore_cv2_per_mille(), None);
        assert_eq!(S.window_oncore_ticks(), 0);
        // A flat window after the reset sets both ends of the wall range
        // rather than being weighed against the 40 before it.
        S.record_batch_site_only(
            16 * flat,
            16 * sq(flat),
            16,
            16 * ITEMS,
            16 * per_item_sq(flat),
        );
        assert_eq!(S.window_wall_cv2_range_per_mille(), Some((0, 0)));
    }

    #[test]
    fn a_window_with_no_oncore_timing_reads_none_on_that_clock() {
        static S: CallSiteState = CallSiteState::new();
        const ITEMS: u64 = 1024;
        let (fast, slow) = (1280u64 * ITEMS, 1920u64 * ITEMS);
        let sq = |ns: u64| (ns >> 8).saturating_mul(ns >> 8);
        let per_item_sq =
            |ns: u64| ((ns as u128).saturating_mul(ns as u128) / ((ITEMS as u128) << 16)) as u64;
        S.record_batch_site_only(
            8 * fast + 8 * slow,
            8 * sq(fast) + 8 * sq(slow),
            16,
            16 * ITEMS,
            8 * per_item_sq(fast) + 8 * per_item_sq(slow),
        );
        assert_eq!(S.window_wall_cv2_per_mille(), Some(40));
        assert_eq!(
            S.window_cv2_per_mille(),
            Some(40),
            "wall time is all the window had"
        );
        assert_eq!(S.window_oncore_cv2_per_mille(), None);
        assert_eq!(S.window_oncore_cv2_range_per_mille(), None);
        assert_eq!(S.window_oncore_ticks(), 0);
    }

    #[test]
    fn a_window_of_leaves_under_the_unit_has_a_mean_and_no_spread() {
        // Sixteen leaves of 200 ns with no item count, the heartbeat's
        // kind of sample. Each scales to nothing in 256 ns units, so the
        // window has a measured mean and no spread, and zero is not
        // reported for it.
        static S: CallSiteState = CallSiteState::new();
        let leaf = 200u64;
        S.record_batch_site_only(16 * leaf, 0, 16, 0, 0);
        assert_eq!(S.window_ticks(), 1);
        assert_eq!(S.window_mean_ns(), Some(leaf));
        assert_eq!(S.window_spread_ticks(), 0);
        assert_eq!(S.window_cv2_per_mille(), None);
        assert_eq!(S.window_cv2_range_per_mille(), None);
        assert_eq!(S.window_wall_ticks(), 0);
        assert_eq!(S.window_wall_cv2_per_mille(), None);
        assert_eq!(S.window_wall_cv2_range_per_mille(), None);
        assert_eq!(S.cv2_per_mille(), None);
        assert!(S.leaves_under_spread_unit());
        // A mean this light places the window by itself.
        assert!(S.learned_class().is_some());
    }

    #[test]
    fn items_under_a_nanosecond_have_no_per_item_spread_while_their_leaves_do() {
        // Sixteen leaves of 1024 items at half a nanosecond each: the
        // integer mean per item is zero, while the 512 ns leaves are
        // long enough to read, and flat.
        static S: CallSiteState = CallSiteState::new();
        const ITEMS: u64 = 1024;
        let leaf = 512u64;
        let sq = |ns: u64| (ns >> 8).saturating_mul(ns >> 8);
        S.record_batch_site_only(16 * leaf, 16 * sq(leaf), 16, 16 * ITEMS, 0);
        assert_eq!(S.per_item_ns(), Some(0));
        assert_eq!(S.per_item_cv2_per_mille(), None);
        assert_eq!(S.window_cv2_per_mille(), None);
        assert_eq!(S.window_spread_ticks(), 0);
        assert_eq!(S.cv2_per_mille(), Some(0));
        assert!(!S.leaves_under_spread_unit());
    }

    #[test]
    fn a_window_with_no_spread_keeps_the_last_one_that_had_one() {
        // The flat-and-spread window of the tests above, cv^2 40, then a
        // window of 200 ns leaves with no items. The second classifies
        // and has no spread, so the reading and its range stay on the
        // first, while the mean moves to the second.
        static S: CallSiteState = CallSiteState::new();
        const ITEMS: u64 = 1024;
        let (fast, slow) = (1280u64 * ITEMS, 1920u64 * ITEMS);
        let sq = |ns: u64| (ns >> 8).saturating_mul(ns >> 8);
        let per_item_sq =
            |ns: u64| ((ns as u128).saturating_mul(ns as u128) / ((ITEMS as u128) << 16)) as u64;
        S.record_batch_site_only(
            8 * fast + 8 * slow,
            8 * sq(fast) + 8 * sq(slow),
            16,
            16 * ITEMS,
            8 * per_item_sq(fast) + 8 * per_item_sq(slow),
        );
        S.record_batch_site_only(16 * 200, 0, 16, 0, 0);
        assert_eq!(S.window_ticks(), 2);
        assert_eq!(S.window_spread_ticks(), 1);
        assert_eq!(S.window_wall_ticks(), 1);
        assert_eq!(S.window_mean_ns(), Some(200));
        assert_eq!(S.window_cv2_per_mille(), Some(40));
        assert_eq!(S.window_cv2_range_per_mille(), Some((40, 40)));
        assert_eq!(S.window_wall_cv2_per_mille(), Some(40));
    }

    #[test]
    fn caller_site_is_distinct_per_call_site_and_stable_per_site() {
        // Two textually distinct calls resolve to two different
        // states; repeating a call site resolves to the same state.
        // This is the regression guard for the identity mechanism:
        // a static inside a generic fn is SHARED across
        // monomorphizations, so identity must come from
        // track_caller locations, never from entry-local statics.
        let a = caller_site();
        let b = caller_site();
        assert_ne!(a, b, "distinct call sites must get distinct states");
        let mut repeats = Vec::new();
        for _ in 0..3 {
            repeats.push(caller_site());
        }
        assert_eq!(repeats[0], repeats[1]);
        assert_eq!(repeats[1], repeats[2]);
        assert_ne!(repeats[0], a);
        assert_ne!(repeats[0], b);
        assert!(registry_len() >= 3, "registry materializes one state per site");
    }

    #[test]
    fn fresh_site_is_unclassified() {
        static S: CallSiteState = CallSiteState::new();
        assert_eq!(S.learned_class(), None);
        assert_eq!(S.cv2_per_mille(), None);
        assert_eq!(S.leaf_count(), 0);
    }

    #[test]
    fn a_reset_site_reads_as_a_fresh_one() {
        static S: CallSiteState = CallSiteState::new();
        // Teach it a depth flip, which is a counter with a reader, and
        // a depth, whose fresh value is not zero. A reset that wrote
        // zeros everywhere would pass on the counter and fail here.
        S.record_seed_depth(3);
        S.record_seed_depth(5);
        assert_eq!(S.seed_depth_flips(), 1, "the site must have learned something first");

        S.reset();

        assert_eq!(S.seed_depth_flips(), 0);
        assert_eq!(S.learned_class(), None);
        assert_eq!(S.cv2_per_mille(), None);
        assert_eq!(S.leaf_count(), 0);
        // The depth is unset again rather than zero, so the next
        // dispatch establishes one and counts no flip against the arm
        // before it.
        S.record_seed_depth(9);
        assert_eq!(S.seed_depth_flips(), 0, "the first depth after a reset flips nothing");
    }

    #[test]
    fn the_registry_says_where_each_site_is() {
        let mine = caller_site();
        let sites = registered_sites();
        let found = sites
            .iter()
            .find(|r| r.site == mine)
            .expect("a site resolved through caller_site is in the registry");
        assert!(
            found.location.file().ends_with("call_site.rs"),
            "the location is this file, not the binding's: {}",
            found.location.file()
        );
        assert!(found.location.line() > 0);
    }

    #[test]
    fn resetting_every_site_covers_the_ones_just_made() {
        // Safe to run beside the other tests in this module even though
        // the sweep is process-wide: every one of them that asserts on
        // counters owns its state as a `static CallSiteState`, which is
        // never in the registry, and the ones that use caller_site or
        // site_for_key assert identity and listing, neither of which a
        // reset changes. This is the one test that resets, so the keyed
        // site's counters are checked here rather than in a test of
        // their own that this sweep could land inside.
        let mine = caller_site();
        mine.get().record_seed_depth(2);
        mine.get().record_seed_depth(7);
        assert_eq!(mine.get().seed_depth_flips(), 1);
        let keyed = site_for_key(0x5EED_0004);
        keyed.get().record_seed_depth(2);
        keyed.get().record_seed_depth(7);
        assert_eq!(keyed.get().seed_depth_flips(), 1);

        let swept = reset_all_sites();
        assert!(swept >= 2, "the sweep counts the located and keyed sites it reset");
        assert_eq!(mine.get().seed_depth_flips(), 0);
        assert_eq!(keyed.get().seed_depth_flips(), 0);
    }

    #[test]
    fn one_key_is_one_site_and_two_keys_are_two() {
        let a = site_for_key(0x5EED_0001);
        assert_eq!(site_for_key(0x5EED_0001), a, "a key resolves to the site it made");
        assert_ne!(site_for_key(0x5EED_0002), a, "two keys learn apart");
    }

    #[test]
    fn a_key_never_names_a_location_site() {
        let here = caller_site();
        let found = registered_sites()
            .into_iter()
            .find(|r| r.site == here)
            .expect("a site resolved through caller_site is in the registry");
        // The location table's own key for that site, passed as a
        // caller's key, still names a keyed site.
        let keyed = site_for_key(location_key(found.location));
        assert_ne!(keyed, here, "a key equal to a location's hash is a site of its own");
    }

    #[test]
    fn the_registry_lists_each_keyed_site_by_its_key() {
        let key = 0x5EED_0003;
        let site = site_for_key(key);
        let listed = registered_keyed_sites()
            .into_iter()
            .find(|r| r.key == key)
            .expect("a keyed site is listed with its key");
        assert_eq!(listed.site, site);
        assert!(
            registered_sites().iter().all(|r| r.site != site),
            "a keyed site is not listed among the located ones"
        );
    }

    #[test]
    fn site_classifies_uniform_streaming_leaves() {
        static S: CallSiteState = CallSiteState::new();
        // 64 uniform heavy leaves (1ms each, zero variance) crosses
        // several quanta; mean >= 500ns + cv2 < low gives Streaming
        // per classify_observed.
        for _ in 0..4 {
            let per = 1_000_000u64;
            let scaled = per >> 8;
            // site_only: synthetic test samples must not leak into
            // the process-global stats other suite tests observe.
            // No item count: these samples assert on leaf-time
            // behavior, which the classifier keeps for windows that
            // carry none.
            S.record_batch_site_only(per * 16, scaled * scaled * 16, 16, 0, 0);
        }
        assert_eq!(S.learned_class(), Some(WorkloadClass::Streaming));
    }

    #[test]
    fn two_sites_classify_independently() {
        static LIGHT: CallSiteState = CallSiteState::new();
        static HEAVY: CallSiteState = CallSiteState::new();
        // Interleave the two shapes; each site must converge to its
        // own class with zero cross-talk between the sites.
        for _ in 0..4 {
            let l = 20u64; // 20ns leaves: FineGrain
            LIGHT.record_batch_site_only(l * 16, 0, 16, 0, 0);
            let h = 1_000_000u64; // 1ms uniform: Streaming
            let hs = h >> 8;
            HEAVY.record_batch_site_only(h * 16, hs * hs * 16, 16, 0, 0);
        }
        assert_eq!(LIGHT.learned_class(), Some(WorkloadClass::FineGrain));
        assert_eq!(HEAVY.learned_class(), Some(WorkloadClass::Streaming));
    }

    #[test]
    fn cv2_reflects_site_spread() {
        static UNIFORM: CallSiteState = CallSiteState::new();
        static SPREAD: CallSiteState = CallSiteState::new();
        let per = 10_000u64;
        let scaled = per >> 8;
        UNIFORM.record_batch_site_only(per * 8, scaled * scaled * 8, 8, 0, 0);
        // Spread: half 1us, half 100us.
        let a = 1_000u64;
        let b = 100_000u64;
        let asc = a >> 8;
        let bsc = b >> 8;
        SPREAD.record_batch_site_only(a * 4 + b * 4, asc * asc * 4 + bsc * bsc * 4, 8, 0, 0);
        assert!(UNIFORM.cv2_per_mille().unwrap() < 20);
        assert!(SPREAD.cv2_per_mille().unwrap() >= 500);
    }

    #[test]
    fn a_seed_depth_change_needs_corroboration() {
        static S: CallSiteState = CallSiteState::new();
        assert_eq!(S.seeded_depth(), None, "a fresh site has seeded nothing");
        assert_eq!(
            S.stabilize_seed_depth(5),
            5,
            "the first dispatch takes the depth its estimate asks for"
        );
        assert_eq!(
            S.stabilize_seed_depth(6),
            5,
            "one estimate the far side of a boundary does not move the depth"
        );
        assert_eq!(
            S.stabilize_seed_depth(6),
            6,
            "a second consecutive call asking the same thing does"
        );
        assert_eq!(S.seeded_depth(), Some(6));
    }

    #[test]
    fn a_run_toward_a_new_depth_restarts_when_a_third_value_intervenes() {
        static S: CallSiteState = CallSiteState::new();
        assert_eq!(S.stabilize_seed_depth(5), 5);
        assert_eq!(S.stabilize_seed_depth(6), 5);
        assert_eq!(
            S.stabilize_seed_depth(7),
            5,
            "a different candidate breaks the run toward 6"
        );
        assert_eq!(
            S.stabilize_seed_depth(6),
            5,
            "so 6 starts its run again rather than arriving already seconded"
        );
        assert_eq!(S.stabilize_seed_depth(6), 6);
    }

    #[test]
    fn a_depth_the_site_already_holds_clears_a_pending_run() {
        static S: CallSiteState = CallSiteState::new();
        assert_eq!(S.stabilize_seed_depth(5), 5);
        assert_eq!(S.stabilize_seed_depth(6), 5);
        assert_eq!(
            S.stabilize_seed_depth(5),
            5,
            "asking for what is already in force is not a change"
        );
        assert_eq!(
            S.stabilize_seed_depth(6),
            5,
            "and it broke the run, so 6 needs two agreeing calls again"
        );
        assert_eq!(S.stabilize_seed_depth(6), 6);
    }

    #[test]
    fn a_class_survives_the_same_work_split_into_different_leaf_sizes() {
        // One site, identical per-item cost throughout: 1 us an item.
        // The first windows run 1024-item leaves, the next run leaves
        // from 1 item to 512, which is what a site gets once its class
        // turns SMT on and the recursion floor drops. Leaf times then
        // span three orders of magnitude while the work has not changed
        // at all, and a classifier reading leaf times migrates on that
        // alone.
        static S: CallSiteState = CallSiteState::new();
        const PER_ITEM_NS: u64 = 1_000;

        let record = |items: u64, leaves: u64| {
            let leaf_ns = PER_ITEM_NS * items;
            let scaled = leaf_ns >> 8;
            let sq = scaled.saturating_mul(scaled);
            S.record_batch_site_only(
                leaf_ns * leaves,
                sq * leaves,
                leaves,
                items * leaves,
                (sq / items) * leaves,
            );
        };

        for _ in 0..8 {
            record(1_024, 16);
        }
        let uniform = S.learned_class();
        assert!(uniform.is_some(), "a site fed 128 leaves has classified");

        for _ in 0..8 {
            record(1, 8);
            record(64, 4);
            record(512, 4);
        }

        assert_eq!(
            S.learned_class(),
            uniform,
            "the same work in leaves of 1 to 512 items must hold the class the 1024-item \
             leaves produced; per-item cost never changed"
        );
        assert_eq!(
            S.per_item_ns(),
            Some(PER_ITEM_NS),
            "per-item cost reads the same whatever the leaves came out at"
        );
        // The scale that decides a class, not a comparison against
        // another statistic: classify_observed calls a site uniform
        // below cv2_low_per_mille and high-variance at or above
        // cv2_high_per_mille, so per-item work that never varied has to
        // read under the first of those.
        let per_item_cv2 = S.per_item_cv2_per_mille().expect("items were recorded");
        let low = crate::sched::adaptive_profile::class_thresholds()
            .cv2_low_per_mille
            .load(Ordering::Relaxed);
        assert!(
            per_item_cv2 < low,
            "per-item spread {per_item_cv2} must read as uniform, under {low}, on work whose \
             per-item cost never changed"
        );
    }

    #[test]
    fn routing_arm_adopts_the_caller_plan_when_the_class_routing_is_slower() {
        // The case the measurement exists for: a site whose class has
        // stopped describing its work, so the routing that class
        // re-derives costs more than the plan the caller built. The
        // class is not consulted here at all - only what each arm's
        // dispatches took.
        static S: CallSiteState = CallSiteState::new();
        for _ in 0..4 {
            S.record_routing_arm(PolicyArm::Default, 900_000);
            S.record_routing_arm(PolicyArm::Alternative, 300_000);
        }

        // Two cadences of calls, taken from the constant rather than a
        // number chosen here: the trial fires on every
        // ARM_TRIAL_CADENCE-th selection, so a shorter run can miss it
        // entirely and say nothing about whether it fires at all.
        let calls = 2 * ARM_TRIAL_CADENCE;
        let mut caller_plan = 0;
        let mut class_routing = 0;
        for _ in 0..calls {
            match S.choose_routing_arm() {
                PolicyArm::Alternative => caller_plan += 1,
                PolicyArm::Default => class_routing += 1,
            }
        }
        assert!(
            caller_plan > class_routing,
            "the faster arm must win the routing: caller plan {caller_plan}, class routing \
             {class_routing} over {calls} calls"
        );
        assert_eq!(
            class_routing, 2,
            "the slower arm runs once per cadence of {ARM_TRIAL_CADENCE} and no more often, \
             so a routing that becomes the better one again is found without paying for it \
             every dispatch"
        );
        let (default_ns, alternative_ns) = S.routing_ewmas();
        assert!(
            alternative_ns < default_ns,
            "the arms report what they cost: class routing {default_ns} ns, caller plan \
             {alternative_ns} ns"
        );
    }

    #[test]
    fn policy_arm_explores_then_adopts_faster_arm() {
        static S: CallSiteState = CallSiteState::new();
        // Feed samples: default arm slow (1ms), alternative fast
        // (100us). After both cross ARM_MIN_SAMPLES the chooser must
        // prefer Alternative on non-trial calls.
        for _ in 0..4 {
            S.record_arm(PolicyArm::Default, 1_000_000);
            S.record_arm(PolicyArm::Alternative, 100_000);
        }
        let mut alt = 0;
        for _ in 0..12 {
            if S.choose_arm(true) == PolicyArm::Alternative {
                alt += 1;
            }
        }
        assert!(alt >= 10, "faster arm must dominate; got {alt}/12");
        // Precondition gate: alternative disallowed forces Default.
        assert_eq!(S.choose_arm(false), PolicyArm::Default);
    }

    #[test]
    fn placement_races_cold_then_picks_faster_side() {
        static S: CallSiteState = CallSiteState::new();
        let batch = 4096u32;
        assert_eq!(S.choose_placement(batch), Placement::Race);
        S.record_placement(batch, Some(50_000), Some(5_000_000));
        // Warm bucket, CPU faster: exploit CPU (allowing the
        // scheduled re-probe tick).
        let mut cpu = 0;
        for _ in 0..8 {
            if S.choose_placement(batch) == Placement::Cpu {
                cpu += 1;
            }
        }
        assert!(cpu >= 7, "CPU side must dominate; got {cpu}/8");
        // A different bucket stays cold independently.
        assert_eq!(S.choose_placement(2), Placement::Race);
    }

    #[test]
    fn split_share_tracks_throughput_ratio() {
        static S: CallSiteState = CallSiteState::new();
        assert_eq!(S.split_cpu_share_per_mille(), 500);
        // CPU 10ns/item, backend 30ns/item: CPU should take ~750.
        S.record_split(1000, 10_000, 1000, 30_000);
        let share = S.split_cpu_share_per_mille();
        assert!((700..=800).contains(&share), "share {share}");
    }

    #[test]
    fn split_share_outgrows_a_cold_first_sample_within_eight() {
        static S: CallSiteState = CallSiteState::new();
        // A cold first round: CPU 32 us/item, backend 18 us/item.
        S.record_split(512, 512 * 32_000, 512, 512 * 18_000);
        assert!(S.split_cpu_share_per_mille() < 400, "cold share {}", S.split_cpu_share_per_mille());
        // Seven warm rounds: CPU 10 us/item, backend 25 us/item.
        for _ in 0..7 {
            S.record_split(500, 500 * 10_000, 500, 500 * 25_000);
        }
        // The eighth average weights every sample equally: CPU 12.75,
        // backend 24.1, a share of 654; an exponential average from
        // the first sample would still read about 500 here.
        let share = S.split_cpu_share_per_mille();
        assert!((620..=700).contains(&share), "share {share}");
        // Later samples move exponentially toward the warm ratio.
        for _ in 0..8 {
            S.record_split(500, 500 * 10_000, 500, 500 * 25_000);
        }
        assert!(S.split_cpu_share_per_mille() > share, "share {}", S.split_cpu_share_per_mille());
    }

    #[test]
    fn site_ref_identity_semantics() {
        static A: CallSiteState = CallSiteState::new();
        static B: CallSiteState = CallSiteState::new();
        assert_eq!(SiteRef::new(&A), SiteRef::new(&A));
        assert_ne!(SiteRef::new(&A), SiteRef::new(&B));
    }
}