aft-base 0.67.1

Shared foundation for Agent Foundational Toolkit
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
use std::collections::BTreeMap;
use std::path::Path;

use serde::Serialize;
use serde_json::Value;

/// A cold-path estimate of memory AFT can attribute without allocator hooks.
///
/// `estimated_bytes` is `None` when a subsystem is busy or its resident bytes
/// are not cheaply observable. Counts remain available in those cases so the
/// status response never substitutes a fabricated byte estimate.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct MemoryEstimate {
    pub status: &'static str,
    pub bytes_status: &'static str,
    pub estimated_bytes: Option<u64>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub not_estimated: Vec<String>,
    #[serde(flatten)]
    pub counts: BTreeMap<String, u64>,
}

impl MemoryEstimate {
    pub fn estimated(bytes: u64) -> Self {
        Self {
            status: "ready",
            bytes_status: "estimated",
            estimated_bytes: Some(bytes),
            not_estimated: Vec::new(),
            counts: BTreeMap::new(),
        }
    }

    pub fn partial(bytes: u64) -> Self {
        Self {
            status: "ready",
            bytes_status: "partial",
            estimated_bytes: Some(bytes),
            not_estimated: Vec::new(),
            counts: BTreeMap::new(),
        }
    }

    pub fn not_estimated() -> Self {
        Self {
            status: "ready",
            bytes_status: "not_estimated",
            estimated_bytes: None,
            not_estimated: Vec::new(),
            counts: BTreeMap::new(),
        }
    }

    pub fn busy() -> Self {
        Self {
            status: "busy",
            bytes_status: "not_estimated",
            estimated_bytes: None,
            not_estimated: Vec::new(),
            counts: BTreeMap::new(),
        }
    }

    pub fn count(mut self, name: impl Into<String>, value: usize) -> Self {
        self.counts.insert(name.into(), usize_to_u64(value));
        self
    }

    pub fn count_u64(mut self, name: impl Into<String>, value: u64) -> Self {
        self.counts.insert(name.into(), value);
        self
    }

    pub fn gap(mut self, name: impl Into<String>) -> Self {
        self.not_estimated.push(name.into());
        self
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct RootMemorySnapshot {
    pub status: &'static str,
    pub attributed_bytes: u64,
    pub semantic: MemoryEstimate,
    pub trigram: MemoryEstimate,
    pub symbols: MemoryEstimate,
    pub callgraph: MemoryEstimate,
    pub callgraph_projection: MemoryEstimate,
    pub inspect: MemoryEstimate,
    pub bash: MemoryEstimate,
    pub lsp: MemoryEstimate,
    pub parser_pool: MemoryEstimate,
    /// Finished artifact loads handed off by their loader but not yet
    /// installed. Zero almost always; it is a label of its own so a load that
    /// waits is never invisible.
    pub pending_install: MemoryEstimate,
    pub exact_memo: MemoryEstimate,
    pub compression_filters: MemoryEstimate,
    /// What the root's checkout in the data layer holds that its planes
    /// share: the checkout state lists, its open segments and its snapshot.
    /// The bytes each plane holds alone are in `trigram` (text), `callgraph`
    /// and `semantic` (meaning search), each as `data_layer_bytes`; the
    /// block cache every checkout shares is counted once per process.
    /// Present only while the data layer runs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_layer: Option<MemoryEstimate>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct RootMemoryRollup {
    pub status: &'static str,
    pub attributed_bytes: u64,
    pub busy_subsystems: usize,
    pub not_estimated_subsystems: usize,
    /// Present only for roots declared as standing. Keeping the marker on the
    /// existing row uses the same top-eight selection and one omitted-roots
    /// rollup instead of creating a separate standing-memory table.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub standing: Option<bool>,
    /// How long ago the index walks behind this row were made, when the
    /// health rollup reused an earlier walk (0 for a walk made now). Unset
    /// outside the health rollup.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub measured_age_ms: Option<u64>,
}

impl RootMemoryRollup {
    pub fn from_estimates(estimates: &[&MemoryEstimate]) -> Self {
        let attributed_bytes = estimates
            .iter()
            .filter_map(|estimate| estimate.estimated_bytes)
            .fold(0u64, u64::saturating_add);
        let busy_subsystems = estimates
            .iter()
            .filter(|estimate| estimate.status == "busy")
            .count();
        let not_estimated_subsystems = estimates
            .iter()
            .filter(|estimate| estimate.estimated_bytes.is_none())
            .count();
        Self {
            status: if busy_subsystems > 0 { "busy" } else { "ready" },
            attributed_bytes,
            busy_subsystems,
            not_estimated_subsystems,
            standing: None,
            measured_age_ms: None,
        }
    }

    /// Record how old the index walks behind this row are.
    pub fn with_measured_age_ms(mut self, age_ms: u64) -> Self {
        self.measured_age_ms = Some(age_ms);
        self
    }

    /// Attribute this existing per-root rollup to a configured standing entry.
    pub fn with_standing(mut self) -> Self {
        self.standing = Some(true);
        self
    }
}

impl RootMemorySnapshot {
    pub fn new(
        semantic: MemoryEstimate,
        trigram: MemoryEstimate,
        symbols: MemoryEstimate,
        callgraph: MemoryEstimate,
        callgraph_projection: MemoryEstimate,
        inspect: MemoryEstimate,
        bash: MemoryEstimate,
        lsp: MemoryEstimate,
        parser_pool: MemoryEstimate,
    ) -> Self {
        let rollup = RootMemoryRollup::from_estimates(&[
            &semantic,
            &trigram,
            &symbols,
            &callgraph,
            &callgraph_projection,
            &inspect,
            &bash,
            &lsp,
            &parser_pool,
        ]);
        Self {
            status: rollup.status,
            attributed_bytes: rollup.attributed_bytes,
            semantic,
            trigram,
            symbols,
            callgraph,
            callgraph_projection,
            inspect,
            bash,
            lsp,
            parser_pool,
            pending_install: MemoryEstimate::estimated(0),
            exact_memo: MemoryEstimate::estimated(0),
            compression_filters: MemoryEstimate::estimated(0),
            data_layer: None,
        }
    }

    /// Attach the data layer's estimate for this root, when it runs, and
    /// fold it into the root's status and attributed total.
    pub fn with_data_layer(mut self, data_layer: Option<MemoryEstimate>) -> Self {
        self.data_layer = data_layer;
        let rollup = self.rollup();
        self.status = rollup.status;
        self.attributed_bytes = rollup.attributed_bytes;
        self
    }

    /// Attach the finished-but-uninstalled load estimate and fold it into the
    /// root's status and attributed total.
    pub fn with_pending_install(mut self, pending_install: MemoryEstimate) -> Self {
        self.pending_install = pending_install;
        let rollup = self.rollup();
        self.status = rollup.status;
        self.attributed_bytes = rollup.attributed_bytes;
        self
    }

    pub fn with_query_caches(
        mut self,
        exact_memo: MemoryEstimate,
        compression_filters: MemoryEstimate,
    ) -> Self {
        self.exact_memo = exact_memo;
        self.compression_filters = compression_filters;
        let rollup = self.rollup();
        self.status = rollup.status;
        self.attributed_bytes = rollup.attributed_bytes;
        self
    }

    pub fn rollup(&self) -> RootMemoryRollup {
        let mut estimates = vec![
            &self.semantic,
            &self.trigram,
            &self.symbols,
            &self.callgraph,
            &self.callgraph_projection,
            &self.inspect,
            &self.bash,
            &self.lsp,
            &self.parser_pool,
            &self.pending_install,
            &self.exact_memo,
            &self.compression_filters,
        ];
        estimates.extend(self.data_layer.as_ref());
        RootMemoryRollup::from_estimates(&estimates)
    }

    pub fn busy_subsystem_count(&self) -> usize {
        self.rollup().busy_subsystems
    }

    pub fn not_estimated_subsystem_count(&self) -> usize {
        self.rollup().not_estimated_subsystems
    }

    /// Bytes released when the root goes idle: retained index handles,
    /// symbol data, inspect caches, and the data layer's checkout, which the
    /// idle reaper's release drops (its planes' bytes are in `trigram`,
    /// `semantic` and `callgraph`, what they share in `data_layer`). This
    /// deliberately shares the already-collected estimates instead of taking
    /// a second estimate on the reply path.
    pub fn evictable_bytes(&self) -> u64 {
        [
            &self.trigram,
            &self.semantic,
            &self.symbols,
            &self.callgraph,
            &self.inspect,
            &self.exact_memo,
        ]
        .into_iter()
        .chain(self.data_layer.as_ref())
        .filter_map(|estimate| estimate.estimated_bytes)
        .fold(0, u64::saturating_add)
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SqliteMemorySnapshot {
    pub status: &'static str,
    pub memory_used_bytes: u64,
    pub memory_highwater_bytes: u64,
}

impl SqliteMemorySnapshot {
    pub fn measure() -> Self {
        // SQLite's allocator counters are process-wide and internally synchronized.
        // They intentionally replace per-connection guesses in root estimates.
        let memory_used = unsafe { rusqlite::ffi::sqlite3_memory_used() };
        let memory_highwater = unsafe { rusqlite::ffi::sqlite3_memory_highwater(0) };
        Self {
            status: "measured",
            memory_used_bytes: nonnegative_i64_to_u64(memory_used),
            memory_highwater_bytes: nonnegative_i64_to_u64(memory_highwater),
        }
    }
}

/// What `retained_slack_bytes` / `allocator_slack_bytes` measure, shown next to
/// the number wherever it is reported so nobody reads it as resident memory.
pub const ALLOCATOR_SLACK_LABEL: &str = "address-space slack: allocator-mapped bytes minus in-use bytes; includes free pages already returned to the OS, so it is not resident memory";

#[cfg(target_os = "linux")]
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct JemallocMemorySnapshot {
    pub accounting_source: &'static str,
    pub allocated: u64,
    pub active: u64,
    pub resident: u64,
    pub retained: u64,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct AllocatorMemorySnapshot {
    pub status: &'static str,
    pub bytes_in_use: Option<u64>,
    pub size_allocated: Option<u64>,
    /// Address-space slack: `size_allocated - bytes_in_use`. This is NOT
    /// resident memory. Allocators can return free pages without releasing the
    /// address space. Slack can therefore exceed RSS.
    pub retained_slack_bytes: Option<u64>,
    /// Always `ALLOCATOR_SLACK_LABEL`; serialized so status readers see what
    /// `retained_slack_bytes` means.
    pub retained_slack_label: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub not_estimated: Option<&'static str>,
    #[cfg(target_os = "linux")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub jemalloc: Option<JemallocMemorySnapshot>,
}

impl AllocatorMemorySnapshot {
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    fn measured(bytes_in_use: u64, size_allocated: u64) -> Self {
        Self {
            status: "measured",
            bytes_in_use: Some(bytes_in_use),
            size_allocated: Some(size_allocated),
            retained_slack_bytes: Some(size_allocated.saturating_sub(bytes_in_use)),
            retained_slack_label: ALLOCATOR_SLACK_LABEL,
            not_estimated: None,
            #[cfg(target_os = "linux")]
            jemalloc: None,
        }
    }

    // A supported allocator can also fail to provide a measurement at runtime.
    #[cfg_attr(target_os = "macos", allow(dead_code))]
    fn not_estimated(reason: &'static str) -> Self {
        Self {
            status: "not_estimated_on_this_platform",
            bytes_in_use: None,
            size_allocated: None,
            retained_slack_bytes: None,
            retained_slack_label: ALLOCATOR_SLACK_LABEL,
            not_estimated: Some(reason),
            #[cfg(target_os = "linux")]
            jemalloc: None,
        }
    }

    fn unavailable(reason: &'static str) -> Self {
        Self {
            status: "unavailable",
            ..Self::not_estimated(reason)
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AllocatorSource {
    /// Use the process-wide observation published by a sampler or health refresh.
    Cached,
    /// Walk allocator statistics now and publish the result for future cached reads.
    Measure,
}

/// One allocator reading shared by all memory surfaces in a health refresh.
#[derive(Clone)]
pub struct AllocatorObservation {
    pub snapshot: AllocatorMemorySnapshot,
    pub age_ms: Option<u64>,
}

impl AllocatorObservation {
    pub fn from_source(source: AllocatorSource) -> Self {
        let (snapshot, age_ms) = allocator_observation(source);
        Self { snapshot, age_ms }
    }

    pub fn measure() -> Self {
        Self::from_source(AllocatorSource::Measure)
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ProcessMemorySnapshot {
    pub rss_status: &'static str,
    pub rss_bytes: Option<u64>,
    /// Kernel physical footprint (macOS `phys_footprint`): dirty + compressed +
    /// IOKit pages, excluding clean/reclaimable ones. This is the number
    /// Activity Monitor's "Real Memory" and the OOM killer use. RSS counts
    /// MADV_FREE pages the allocator has already surrendered (the kernel
    /// reclaims them lazily), so RSS can read gigabytes above what the process
    /// actually holds — observed 5.1 GB RSS over a 610 MB footprint. None on
    /// non-macOS platforms (Linux RSS does not have this skew; MADV_FREE'd
    /// pages leave Linux RSS on reclaim, not on advice).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub phys_footprint_bytes: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rss_not_estimated: Option<&'static str>,
    pub sqlite: SqliteMemorySnapshot,
    /// Allocator bytes overlap the attributed subsystem totals and are an
    /// allocation envelope, not another amount to subtract from RSS.
    pub allocator: AllocatorMemorySnapshot,
    /// Whether allocator slack came from an available allocator measurement.
    pub allocator_slack_measured: bool,
    /// Age of the cached allocator observation. A direct measurement is age zero;
    /// a request-path read is null until the first process-wide observation exists.
    pub allocator_observation_age_ms: Option<u64>,
    pub total_attributed_bytes: u64,
    /// Live local ONNX embedding sessions, shared across roots and query/build workers.
    pub embedding_sessions: u64,
    /// Serialized weight bytes for those sessions, counted once per session.
    /// This estimates model storage, not ORT's transient tensors or allocator slack.
    pub embedding_model_bytes: u64,
    /// Bytes resident in the data layer's block cache, shared by every
    /// checkout and counted once here. Included in `total_attributed_bytes`.
    /// Present only once a checkout of this process has bound.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_layer_block_cache_bytes: Option<u64>,
    pub unattributed_bytes: Option<i64>,
    pub root_count: usize,
    pub busy_subsystems: usize,
    pub not_estimated_subsystems: usize,
}

/// Source label for `AllocatorPressureRelief::allocator_accounting_bytes` on
/// Linux: the drop in jemalloc's `stats.resident`, not the kernel's RSS.
pub const RELIEF_ACCOUNTING_SOURCE_JEMALLOC: &str = "jemalloc_stats_resident_drop";
/// Source label for `AllocatorPressureRelief::allocator_accounting_bytes` on
/// macOS: the byte count `malloc_zone_pressure_relief` returns, i.e. what
/// libmalloc says it handed back. It is the allocator's own claim, not a
/// measurement of resident memory.
pub const RELIEF_ACCOUNTING_SOURCE_MACOS: &str = "malloc_zone_pressure_relief_return";

/// Resident-memory readings taken on each side of a relief pass.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ResidentReading {
    pub rss_bytes: Option<u64>,
    /// macOS only; `None` elsewhere.
    pub phys_footprint_bytes: Option<u64>,
}

impl ResidentReading {
    #[cfg_attr(not(any(target_os = "macos", target_os = "linux")), allow(dead_code))]
    fn read() -> Self {
        Self {
            rss_bytes: process_rss_bytes(),
            phys_footprint_bytes: process_phys_footprint_bytes(),
        }
    }
}

/// What one allocator relief pass observed. There is deliberately no single
/// "released" number: the allocator's accounting and the process's resident
/// memory measure different things and disagree in practice.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AllocatorPressureRelief {
    /// Bytes the allocator's own accounting says the pass returned. What it
    /// measures depends on the platform; see `allocator_accounting_source`.
    pub allocator_accounting_bytes: u64,
    /// Names the allocator counter used, independently of the kernel readings.
    pub allocator_accounting_source: &'static str,
    /// Observed RSS drop across the pass (`rss_before - rss_after`, floored at
    /// zero). `None` when either reading is unavailable. Other threads can
    /// allocate or free during the pass, so this is an observation of the
    /// process, not an exact attribution to the allocator.
    pub rss_drop_bytes: Option<u64>,
    /// Observed physical-footprint drop across the pass (macOS only). macOS
    /// relief marks pages reusable, which lowers the footprint right away but
    /// can leave them counted in RSS until the kernel reclaims them, so on
    /// macOS this is the better observable than `rss_drop_bytes`.
    pub phys_footprint_drop_bytes: Option<u64>,
    pub rss_before_bytes: Option<u64>,
    pub rss_after_bytes: Option<u64>,
    pub phys_footprint_before_bytes: Option<u64>,
    pub phys_footprint_after_bytes: Option<u64>,
    pub allocator_before: AllocatorMemorySnapshot,
    pub allocator_after: AllocatorMemorySnapshot,
}

impl AllocatorPressureRelief {
    /// Assemble a relief record from the readings around the pass. The
    /// resident drops come only from the resident readings; the allocator
    /// figure comes only from the allocator. Keeping them apart is the point.
    #[cfg_attr(not(any(target_os = "macos", target_os = "linux")), allow(dead_code))]
    fn from_observations(
        allocator_accounting_bytes: u64,
        allocator_accounting_source: &'static str,
        resident_before: ResidentReading,
        resident_after: ResidentReading,
        allocator_before: AllocatorMemorySnapshot,
        allocator_after: AllocatorMemorySnapshot,
    ) -> Self {
        let drop = |before: Option<u64>, after: Option<u64>| {
            before
                .zip(after)
                .map(|(before, after)| before.saturating_sub(after))
        };
        Self {
            allocator_accounting_bytes,
            allocator_accounting_source,
            rss_drop_bytes: drop(resident_before.rss_bytes, resident_after.rss_bytes),
            phys_footprint_drop_bytes: drop(
                resident_before.phys_footprint_bytes,
                resident_after.phys_footprint_bytes,
            ),
            rss_before_bytes: resident_before.rss_bytes,
            rss_after_bytes: resident_after.rss_bytes,
            phys_footprint_before_bytes: resident_before.phys_footprint_bytes,
            phys_footprint_after_bytes: resident_after.phys_footprint_bytes,
            allocator_before,
            allocator_after,
        }
    }

    /// Linux form: the allocator figure is the drop in `stats.resident`.
    #[cfg(target_os = "linux")]
    fn from_jemalloc_resident_drop(
        resident_before: ResidentReading,
        resident_after: ResidentReading,
        allocator_before: AllocatorMemorySnapshot,
        allocator_after: AllocatorMemorySnapshot,
    ) -> Self {
        let accounting = allocator_before
            .jemalloc
            .as_ref()
            .map(|stats| stats.resident)
            .zip(
                allocator_after
                    .jemalloc
                    .as_ref()
                    .map(|stats| stats.resident),
            )
            .map(|(before, after)| before.saturating_sub(after))
            .unwrap_or(0);
        Self::from_observations(
            accounting,
            RELIEF_ACCOUNTING_SOURCE_JEMALLOC,
            resident_before,
            resident_after,
            allocator_before,
            allocator_after,
        )
    }
}

/// Cap on per-root detail entries in serialized snapshots. Process totals
/// always cover every root; only the per-root breakdown is capped so a
/// many-root daemon process cannot balloon the status payload past
/// downstream consumers' size limits (the daemon metrics cache truncates
/// around 27 KB, and JSON keys serialize alphabetically, so an oversized
/// `memory.roots` map pushes later sections past the cut).
pub const MEMORY_SNAPSHOT_ROOT_DETAIL_CAP: usize = 8;

/// Operations the census names by their allocation high-water mark.
pub const CENSUS_ALLOCATION_LABELS: usize = 5;

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct MemorySnapshot {
    pub roots_status: &'static str,
    /// Top roots by attributed bytes, capped at
    /// [`MEMORY_SNAPSHOT_ROOT_DETAIL_CAP`]; the remainder is summarized by
    /// `roots_omitted` / `roots_omitted_bytes`.
    pub roots: BTreeMap<String, RootMemorySnapshot>,
    /// Total roots attributed (including omitted ones).
    pub roots_total: usize,
    /// Roots summarized out of the detail map.
    pub roots_omitted: usize,
    /// Attributed bytes carried by the omitted roots (already included in
    /// `process.total_attributed_bytes`).
    pub roots_omitted_bytes: u64,
    /// Immutable shared semantic bases, attributed once process-wide: the
    /// snapshots borrowed loads share and the indexes owner roots froze when
    /// they installed them. Root rows count zero for a view of such a base.
    pub shared_semantic_bases: MemoryEstimate,
    /// The data layer's block cache, shared by every checkout and attributed
    /// once process-wide (`process.data_layer_block_cache_bytes`). Present
    /// only once a checkout of this process has bound.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_layer_block_cache: Option<MemoryEstimate>,
    pub process: ProcessMemorySnapshot,
    /// The operations whose live heap bytes peaked highest since the
    /// previous census, highest first, as the `aft` binary's allocation
    /// tracer counts them. Only the uncapped census carries them, and only
    /// when an operation allocated since the last one.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub allocation_labels: Vec<crate::alloc_trace::LabelPeak>,
}

/// Memory attributed once for the whole process rather than to any root:
/// shared semantic bases and the data layer's block cache.
#[derive(Debug, Clone)]
pub struct SharedProcessMemory {
    pub shared_semantic_bases: MemoryEstimate,
    pub data_layer_block_cache: Option<MemoryEstimate>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct MemoryRollupSnapshot {
    pub roots_status: &'static str,
    pub roots: BTreeMap<String, RootMemoryRollup>,
    pub roots_total: usize,
    pub roots_omitted: usize,
    pub roots_omitted_bytes: u64,
    pub process: ProcessMemorySnapshot,
}

fn cap_roots<T>(
    roots: BTreeMap<String, T>,
    cap: usize,
    attributed_bytes: impl Fn(&T) -> u64,
) -> (BTreeMap<String, T>, usize, u64) {
    if roots.len() <= cap {
        return (roots, 0, 0);
    }
    let mut entries: Vec<_> = roots.into_iter().collect();
    entries.sort_by(|a, b| {
        attributed_bytes(&b.1)
            .cmp(&attributed_bytes(&a.1))
            .then_with(|| a.0.cmp(&b.0))
    });
    let omitted = entries.split_off(cap);
    let omitted_bytes = omitted
        .iter()
        .map(|(_, snapshot)| attributed_bytes(snapshot))
        .fold(0u64, u64::saturating_add);
    (entries.into_iter().collect(), omitted.len(), omitted_bytes)
}

pub fn cap_root_rollups(
    roots: BTreeMap<String, RootMemoryRollup>,
    cap: usize,
) -> (BTreeMap<String, RootMemoryRollup>, usize, u64) {
    cap_roots(roots, cap, |snapshot| snapshot.attributed_bytes)
}

/// Keep the `cap` roots with the highest attributed bytes; report the rest
/// as an omitted-count + omitted-bytes rollup.
pub fn cap_root_detail(
    roots: BTreeMap<String, RootMemorySnapshot>,
    cap: usize,
) -> (BTreeMap<String, RootMemorySnapshot>, usize, u64) {
    cap_roots(roots, cap, |snapshot| snapshot.attributed_bytes)
}

pub fn path_bytes(path: &Path) -> u64 {
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStrExt;
        usize_to_u64(path.as_os_str().as_bytes().len())
    }
    #[cfg(windows)]
    {
        use std::os::windows::ffi::OsStrExt;
        usize_to_u64(path.as_os_str().encode_wide().count())
            .saturating_mul(std::mem::size_of::<u16>() as u64)
    }
    #[cfg(not(any(unix, windows)))]
    {
        usize_to_u64(path.to_string_lossy().len())
    }
}

pub fn usize_to_u64(value: usize) -> u64 {
    u64::try_from(value).unwrap_or(u64::MAX)
}

pub fn estimated_json_bytes(value: &Value) -> u64 {
    match value {
        Value::Null => 0,
        Value::Bool(_) => std::mem::size_of::<bool>() as u64,
        Value::Number(_) => std::mem::size_of::<serde_json::Number>() as u64,
        Value::String(value) => usize_to_u64(value.len()),
        Value::Array(values) => values
            .iter()
            .map(estimated_json_bytes)
            .fold(0u64, u64::saturating_add),
        Value::Object(values) => values.iter().fold(0u64, |bytes, (key, value)| {
            bytes
                .saturating_add(usize_to_u64(key.len()))
                .saturating_add(estimated_json_bytes(value))
        }),
    }
}

pub fn signed_difference(lhs: u64, rhs: u64) -> i64 {
    let difference = i128::from(lhs) - i128::from(rhs);
    difference.clamp(i128::from(i64::MIN), i128::from(i64::MAX)) as i64
}

fn nonnegative_i64_to_u64(value: i64) -> u64 {
    u64::try_from(value).unwrap_or(0)
}

#[cfg(target_os = "macos")]
fn allocator_memory_snapshot_impl() -> AllocatorMemorySnapshot {
    let mut statistics = std::mem::MaybeUninit::<libc::malloc_statistics_t>::zeroed();
    unsafe {
        libc::malloc_zone_statistics(libc::malloc_default_zone(), statistics.as_mut_ptr());
    }
    let statistics = unsafe { statistics.assume_init() };
    AllocatorMemorySnapshot::measured(
        usize_to_u64(statistics.size_in_use),
        usize_to_u64(statistics.size_allocated),
    )
}

#[cfg(target_os = "linux")]
fn allocator_memory_snapshot_impl() -> AllocatorMemorySnapshot {
    let read = || -> Result<_, tikv_jemalloc_ctl::Error> {
        tikv_jemalloc_ctl::epoch::advance()?;
        Ok((
            tikv_jemalloc_ctl::stats::allocated::read()? as u64,
            tikv_jemalloc_ctl::stats::active::read()? as u64,
            tikv_jemalloc_ctl::stats::resident::read()? as u64,
            tikv_jemalloc_ctl::stats::retained::read()? as u64,
            tikv_jemalloc_ctl::stats::mapped::read()? as u64,
        ))
    };
    let Ok((allocated, active, resident, retained, mapped)) = read() else {
        return AllocatorMemorySnapshot::unavailable("jemalloc_statistics_unavailable");
    };
    // Preserve the existing envelope: bytes_in_use is stats.allocated;
    // size_allocated is mapped + retained virtual address space, not residency;
    // retained_slack_bytes is that envelope minus allocated, not stats.retained.
    let mut snapshot =
        AllocatorMemorySnapshot::measured(allocated, mapped.saturating_add(retained));
    snapshot.jemalloc = Some(JemallocMemorySnapshot {
        accounting_source: "jemalloc",
        allocated,
        active,
        resident,
        retained,
    });
    snapshot
}

/// Return unused Linux allocator pages without measuring counters or RSS.
/// Call-graph build phases use the same purge as the measured idle-relief path.
#[cfg(target_os = "linux")]
pub fn purge_allocator() -> std::io::Result<()> {
    // MALLCTL_ARENAS_ALL is 4096 in jemalloc 5.3.0. This command takes no value.
    let result = unsafe {
        tikv_jemalloc_sys::mallctl(
            c"arena.4096.purge".as_ptr(),
            std::ptr::null_mut(),
            std::ptr::null_mut(),
            std::ptr::null_mut(),
            0,
        )
    };
    if result == 0 {
        Ok(())
    } else {
        Err(std::io::Error::from_raw_os_error(result))
    }
}

#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn allocator_memory_snapshot_impl() -> AllocatorMemorySnapshot {
    AllocatorMemorySnapshot::not_estimated("platform_allocator_statistics_unavailable")
}

/// Read allocator-wide statistics. Callers on the dispatch path must use the
/// cached slack scheduler below instead: macOS walks allocator zones under
/// their locks while producing this snapshot.
fn allocator_memory_snapshot() -> AllocatorMemorySnapshot {
    #[cfg(any(test, feature = "test-support"))]
    {
        THREAD_ALLOCATOR_SNAPSHOT_CALLS.with(|calls| calls.set(calls.get() + 1));
        let name = std::thread::current()
            .name()
            .unwrap_or("unnamed")
            .to_string();
        ALLOCATOR_SNAPSHOT_THREADS
            .lock()
            .expect("allocator snapshot test thread log")
            .push(name);
    }
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    let started = std::time::Instant::now();
    let snapshot = allocator_memory_snapshot_impl();
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    LAST_ALLOCATOR_WALK_MS.store(
        started.elapsed().as_millis().min(u128::from(u64::MAX)) as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    snapshot
}

fn allocator_observation(source: AllocatorSource) -> (AllocatorMemorySnapshot, Option<u64>) {
    match source {
        AllocatorSource::Cached => cached_allocator_observation()
            .map(|(snapshot, age_ms)| (snapshot, Some(age_ms)))
            .unwrap_or_else(|| {
                // A cold cache is not evidence that the allocator lacks counters.
                // Keep the request path cheap while the first sample is pending.
                let snapshot = if cfg!(any(target_os = "macos", target_os = "linux")) {
                    AllocatorMemorySnapshot::unavailable("allocator_observation_unavailable")
                } else {
                    AllocatorMemorySnapshot::not_estimated("allocator_observation_unavailable")
                };
                (snapshot, None)
            }),
        AllocatorSource::Measure => measure_allocator_observation(),
    }
}

fn measure_allocator_observation() -> (AllocatorMemorySnapshot, Option<u64>) {
    // Only the platforms that publish an observation stamp its start; the
    // Windows deny-warnings lane rejects the unused binding otherwise.
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    let sampled_at = std::time::Instant::now();
    let snapshot = allocator_memory_snapshot();
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    {
        let sampled_at_ms = allocator_slack_elapsed_ms(sampled_at);
        publish_allocator_slack(&snapshot, sampled_at_ms);
        let age_ms =
            allocator_slack_elapsed_ms(std::time::Instant::now()).saturating_sub(sampled_at_ms);
        (snapshot, Some(age_ms))
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        (snapshot, None)
    }
}

#[cfg(target_os = "macos")]
unsafe extern "C" {
    fn malloc_zone_pressure_relief(zone: *mut libc::malloc_zone_t, goal: usize) -> usize;
}

/// Address-space allocator slack above which opportunistic pressure relief is
/// worth the zone-lock contention it briefly causes. Slack includes free pages
/// the allocator already returned to the OS (it cannot tell which free pages
/// are resident), so it stays above this line after a successful pass and the
/// pass repeats at `ALLOCATOR_SLACK_RELIEF_MIN_INTERVAL`. That is intended: a
/// repeat pass is cheap and returns whatever has been freed since the last one.
pub const ALLOCATOR_SLACK_RELIEF_THRESHOLD_BYTES: u64 = 1024 * 1024 * 1024;

/// Minimum spacing between opportunistic relief passes so a workload that
/// legitimately cycles through large allocations does not thrash the allocator.
pub const ALLOCATOR_SLACK_RELIEF_MIN_INTERVAL: std::time::Duration =
    std::time::Duration::from_secs(300);

/// Periodic allocator readers wait at least this many times the last walk's
/// own duration before walking again, so walks take at most 0.5% of the time.
/// Allocator walks can contend with allocations, so their measured cost bounds
/// sampling frequency even when a fragmented heap makes a walk expensive.
const ALLOCATOR_WALK_COST_FACTOR: u64 = 200;

/// The longest an expensive walk can stretch a periodic reader's reuse window,
/// so a slow allocator still gets a fresh reading every ten minutes.
const ALLOCATOR_WALK_MAX_REUSE_MS: u64 = 10 * 60 * 1000;

/// How long a periodic reader may reuse an allocator observation whose walk
/// took `walk_ms`: `floor_ms` for a cheap walk, stretched to
/// `ALLOCATOR_WALK_COST_FACTOR` times the walk's duration for an expensive
/// one, and never past `ALLOCATOR_WALK_MAX_REUSE_MS` (or `floor_ms`, if that
/// is larger).
pub fn allocator_reuse_window_ms(floor_ms: u64, walk_ms: u64) -> u64 {
    walk_ms
        .saturating_mul(ALLOCATOR_WALK_COST_FACTOR)
        .clamp(floor_ms, ALLOCATOR_WALK_MAX_REUSE_MS.max(floor_ms))
}

#[cfg(any(target_os = "linux", all(test, target_os = "macos")))]
/// The Linux module loop ticks every 250ms. Reuse its process-wide jemalloc
/// observation for at least 60s to bound telemetry work, longer when walks
/// are expensive (see `allocator_reuse_window_ms`).
/// This bounds allocator statistics walks to one per idle minute, or two for a
/// relief pass that uses its before/after snapshots.
const ALLOCATOR_SLACK_SAMPLE_INTERVAL: std::time::Duration = std::time::Duration::from_secs(60);
/// Wall-clock duration of the most recent allocator statistics walk, on any
/// thread, in milliseconds.
#[cfg(any(target_os = "macos", target_os = "linux"))]
static LAST_ALLOCATOR_WALK_MS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
#[cfg(any(target_os = "macos", target_os = "linux"))]
const UNKNOWN_ALLOCATOR_SLACK: u64 = u64::MAX;
#[cfg(any(target_os = "macos", target_os = "linux"))]
const UNSAMPLED_AT_MS: u64 = u64::MAX;
#[cfg(any(target_os = "macos", target_os = "linux"))]
const ALLOCATOR_OBSERVATION_UNSAMPLED: u8 = 0;
#[cfg(any(target_os = "macos", target_os = "linux"))]
const ALLOCATOR_OBSERVATION_MEASURED: u8 = 1;
#[cfg(any(target_os = "macos", target_os = "linux"))]
const ALLOCATOR_OBSERVATION_NOT_ESTIMATED: u8 = 2;
#[cfg(any(target_os = "macos", target_os = "linux"))]
static ALLOCATOR_SLACK_CACHE_ORIGIN: std::sync::OnceLock<std::time::Instant> =
    std::sync::OnceLock::new();
#[cfg(any(target_os = "macos", target_os = "linux"))]
static LAST_OBSERVED_ALLOCATOR_SLACK_BYTES: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(UNKNOWN_ALLOCATOR_SLACK);
#[cfg(any(target_os = "macos", target_os = "linux"))]
static LAST_OBSERVED_ALLOCATOR_BYTES_IN_USE: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(UNKNOWN_ALLOCATOR_SLACK);
#[cfg(any(target_os = "macos", target_os = "linux"))]
static LAST_OBSERVED_ALLOCATOR_SIZE_ALLOCATED: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(UNKNOWN_ALLOCATOR_SLACK);
#[cfg(target_os = "linux")]
static LAST_OBSERVED_JEMALLOC: [std::sync::atomic::AtomicU64; 3] =
    [const { std::sync::atomic::AtomicU64::new(UNKNOWN_ALLOCATOR_SLACK) }; 3];
#[cfg(any(target_os = "macos", target_os = "linux"))]
static LAST_ALLOCATOR_OBSERVATION_STATUS: std::sync::atomic::AtomicU8 =
    std::sync::atomic::AtomicU8::new(ALLOCATOR_OBSERVATION_UNSAMPLED);
#[cfg(any(target_os = "macos", target_os = "linux"))]
static LAST_ALLOCATOR_OBSERVATION_AT_MS: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(UNSAMPLED_AT_MS);
#[cfg(any(target_os = "macos", target_os = "linux"))]
static ALLOCATOR_OBSERVATION_SEQUENCE: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(0);
#[cfg(any(target_os = "macos", target_os = "linux"))]
static ALLOCATOR_OBSERVATION_PUBLISH_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[cfg(any(target_os = "macos", target_os = "linux"))]
static LAST_ALLOCATOR_SLACK_SAMPLE_AT_MS: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(UNSAMPLED_AT_MS);
#[cfg(any(target_os = "macos", target_os = "linux"))]
static LAST_ALLOCATOR_SLACK_RELIEF_AT_MS: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(UNSAMPLED_AT_MS);
#[cfg(any(target_os = "macos", target_os = "linux"))]
static LAST_ALLOCATOR_SLACK_RELIEF_ACCOUNTING_BYTES: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(0);
#[cfg(any(target_os = "macos", target_os = "linux"))]
static LAST_ALLOCATOR_SLACK_RELIEF_RSS_DROP_BYTES: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(UNKNOWN_ALLOCATOR_SLACK);
#[cfg(any(target_os = "macos", target_os = "linux"))]
static LAST_ALLOCATOR_SLACK_RELIEF_FOOTPRINT_DROP_BYTES: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(UNKNOWN_ALLOCATOR_SLACK);

#[cfg(any(test, feature = "test-support"))]
pub static ALLOCATOR_SNAPSHOT_THREADS: std::sync::Mutex<Vec<String>> =
    std::sync::Mutex::new(Vec::new());

#[cfg(any(test, feature = "test-support"))]
thread_local! {
    /// Per-thread count of allocator statistics walks. Tests that assert a
    /// synchronous render path took no walk read this: the walk, if it
    /// happened, ran on the calling thread. A process-global counter is
    /// wrong for that assertion - sibling tests and the health thread walk
    /// concurrently, and a parallel run read a delta of 1 for a render that
    /// walked nothing. Which-thread questions use the thread-name log.
    static THREAD_ALLOCATOR_SNAPSHOT_CALLS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}

#[cfg(any(test, feature = "test-support"))]
pub fn allocator_snapshot_calls_for_test() -> u64 {
    THREAD_ALLOCATOR_SNAPSHOT_CALLS.with(|calls| calls.get())
}

#[cfg(any(test, feature = "test-support"))]
fn allocator_observation_test_mutex() -> &'static std::sync::Mutex<()> {
    static LOCK: std::sync::OnceLock<std::sync::Mutex<()>> = std::sync::OnceLock::new();
    LOCK.get_or_init(|| std::sync::Mutex::new(()))
}

#[cfg(any(test, feature = "test-support"))]
pub fn allocator_observation_test_lock() -> std::sync::MutexGuard<'static, ()> {
    allocator_observation_test_mutex()
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

#[cfg(any(test, feature = "test-support"))]
pub fn reset_allocator_observation_for_test() {
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    {
        ALLOCATOR_OBSERVATION_SEQUENCE.store(0, std::sync::atomic::Ordering::Release);
        LAST_ALLOCATOR_OBSERVATION_STATUS.store(
            ALLOCATOR_OBSERVATION_UNSAMPLED,
            std::sync::atomic::Ordering::Release,
        );
        LAST_OBSERVED_ALLOCATOR_BYTES_IN_USE.store(
            UNKNOWN_ALLOCATOR_SLACK,
            std::sync::atomic::Ordering::Release,
        );
        LAST_OBSERVED_ALLOCATOR_SIZE_ALLOCATED.store(
            UNKNOWN_ALLOCATOR_SLACK,
            std::sync::atomic::Ordering::Release,
        );
        LAST_OBSERVED_ALLOCATOR_SLACK_BYTES.store(
            UNKNOWN_ALLOCATOR_SLACK,
            std::sync::atomic::Ordering::Release,
        );
        LAST_ALLOCATOR_OBSERVATION_AT_MS
            .store(UNSAMPLED_AT_MS, std::sync::atomic::Ordering::Release);
        LAST_ALLOCATOR_SLACK_SAMPLE_AT_MS
            .store(UNSAMPLED_AT_MS, std::sync::atomic::Ordering::Release);
    }
}

#[cfg(any(target_os = "macos", target_os = "linux"))]
fn allocator_slack_elapsed_ms(now: std::time::Instant) -> u64 {
    let origin = ALLOCATOR_SLACK_CACHE_ORIGIN.get_or_init(|| now);
    now.duration_since(*origin)
        .as_millis()
        .min(u128::from(u64::MAX)) as u64
}

#[cfg(target_os = "linux")]
fn cached_allocator_slack() -> Option<u64> {
    let slack = LAST_OBSERVED_ALLOCATOR_SLACK_BYTES.load(std::sync::atomic::Ordering::Acquire);
    (slack != UNKNOWN_ALLOCATOR_SLACK).then_some(slack)
}

#[cfg(any(target_os = "macos", target_os = "linux"))]
fn cached_allocator_not_estimated_reason() -> &'static str {
    #[cfg(target_os = "linux")]
    {
        "jemalloc_statistics_unavailable"
    }
    #[cfg(not(target_os = "linux"))]
    {
        "platform_allocator_statistics_unavailable"
    }
}

/// Return the last process-wide allocator observation without walking allocator
/// zones. The observation timestamp is separate from the sampler reservation so
/// a status request never mistakes an in-progress sample for a fresh reading.
pub fn cached_allocator_observation() -> Option<(AllocatorMemorySnapshot, u64)> {
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    {
        loop {
            let sequence =
                ALLOCATOR_OBSERVATION_SEQUENCE.load(std::sync::atomic::Ordering::Acquire);
            if sequence % 2 != 0 {
                std::hint::spin_loop();
                continue;
            }
            let sampled_at_ms =
                LAST_ALLOCATOR_OBSERVATION_AT_MS.load(std::sync::atomic::Ordering::Acquire);
            if sampled_at_ms == UNSAMPLED_AT_MS {
                return None;
            }
            let status =
                LAST_ALLOCATOR_OBSERVATION_STATUS.load(std::sync::atomic::Ordering::Acquire);
            let bytes_in_use =
                LAST_OBSERVED_ALLOCATOR_BYTES_IN_USE.load(std::sync::atomic::Ordering::Acquire);
            let size_allocated =
                LAST_OBSERVED_ALLOCATOR_SIZE_ALLOCATED.load(std::sync::atomic::Ordering::Acquire);
            let retained_slack =
                LAST_OBSERVED_ALLOCATOR_SLACK_BYTES.load(std::sync::atomic::Ordering::Acquire);
            #[cfg(target_os = "linux")]
            let jemalloc_counters = LAST_OBSERVED_JEMALLOC
                .each_ref()
                .map(|value| value.load(std::sync::atomic::Ordering::Acquire));
            let sequence_after =
                ALLOCATOR_OBSERVATION_SEQUENCE.load(std::sync::atomic::Ordering::Acquire);
            if sequence != sequence_after || sequence_after % 2 != 0 {
                continue;
            }
            let snapshot = match status {
                ALLOCATOR_OBSERVATION_MEASURED
                    if bytes_in_use != UNKNOWN_ALLOCATOR_SLACK
                        && size_allocated != UNKNOWN_ALLOCATOR_SLACK
                        && retained_slack != UNKNOWN_ALLOCATOR_SLACK =>
                {
                    AllocatorMemorySnapshot {
                        status: "measured",
                        bytes_in_use: Some(bytes_in_use),
                        size_allocated: Some(size_allocated),
                        retained_slack_bytes: Some(retained_slack),
                        retained_slack_label: ALLOCATOR_SLACK_LABEL,
                        not_estimated: None,
                        #[cfg(target_os = "linux")]
                        jemalloc: (!jemalloc_counters.contains(&UNKNOWN_ALLOCATOR_SLACK))
                            .then_some(JemallocMemorySnapshot {
                                accounting_source: "jemalloc",
                                allocated: bytes_in_use,
                                active: jemalloc_counters[0],
                                resident: jemalloc_counters[1],
                                retained: jemalloc_counters[2],
                            }),
                    }
                }
                ALLOCATOR_OBSERVATION_NOT_ESTIMATED => {
                    AllocatorMemorySnapshot::not_estimated(cached_allocator_not_estimated_reason())
                }
                _ => return None,
            };
            let age_ms =
                allocator_slack_elapsed_ms(std::time::Instant::now()).saturating_sub(sampled_at_ms);
            return Some((snapshot, age_ms));
        }
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        None
    }
}

#[cfg(any(target_os = "macos", target_os = "linux"))]
fn publish_allocator_slack(snapshot: &AllocatorMemorySnapshot, sampled_at_ms: u64) {
    let _publish_guard = ALLOCATOR_OBSERVATION_PUBLISH_LOCK
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    ALLOCATOR_OBSERVATION_SEQUENCE.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
    LAST_ALLOCATOR_OBSERVATION_STATUS.store(
        if snapshot.status == "measured" {
            ALLOCATOR_OBSERVATION_MEASURED
        } else {
            ALLOCATOR_OBSERVATION_NOT_ESTIMATED
        },
        std::sync::atomic::Ordering::Relaxed,
    );
    LAST_OBSERVED_ALLOCATOR_BYTES_IN_USE.store(
        snapshot.bytes_in_use.unwrap_or(UNKNOWN_ALLOCATOR_SLACK),
        std::sync::atomic::Ordering::Relaxed,
    );
    LAST_OBSERVED_ALLOCATOR_SIZE_ALLOCATED.store(
        snapshot.size_allocated.unwrap_or(UNKNOWN_ALLOCATOR_SLACK),
        std::sync::atomic::Ordering::Relaxed,
    );
    LAST_OBSERVED_ALLOCATOR_SLACK_BYTES.store(
        snapshot
            .retained_slack_bytes
            .unwrap_or(UNKNOWN_ALLOCATOR_SLACK),
        std::sync::atomic::Ordering::Relaxed,
    );
    LAST_ALLOCATOR_OBSERVATION_AT_MS.store(sampled_at_ms, std::sync::atomic::Ordering::Relaxed);
    #[cfg(target_os = "linux")]
    for (counter, value) in LAST_OBSERVED_JEMALLOC.iter().zip(
        snapshot
            .jemalloc
            .as_ref()
            .map_or([UNKNOWN_ALLOCATOR_SLACK; 3], |stats| {
                [stats.active, stats.resident, stats.retained]
            }),
    ) {
        counter.store(value, std::sync::atomic::Ordering::Relaxed);
    }
    ALLOCATOR_OBSERVATION_SEQUENCE.fetch_add(1, std::sync::atomic::Ordering::Release);
    LAST_ALLOCATOR_SLACK_SAMPLE_AT_MS.store(sampled_at_ms, std::sync::atomic::Ordering::Release);
}

#[cfg(target_os = "linux")]
fn sample_is_stale(now_ms: u64) -> bool {
    sample_is_stale_after(
        LAST_ALLOCATOR_SLACK_SAMPLE_AT_MS.load(std::sync::atomic::Ordering::Acquire),
        now_ms,
        LAST_ALLOCATOR_WALK_MS.load(std::sync::atomic::Ordering::Relaxed),
    )
}

/// Whether a sample taken at `sampled_at` is due for replacement at `now_ms`
/// when the last walk took `walk_ms`.
#[cfg(any(target_os = "linux", all(test, target_os = "macos")))]
fn sample_is_stale_after(sampled_at: u64, now_ms: u64, walk_ms: u64) -> bool {
    sampled_at == UNSAMPLED_AT_MS
        || now_ms.saturating_sub(sampled_at)
            >= allocator_reuse_window_ms(
                ALLOCATOR_SLACK_SAMPLE_INTERVAL.as_millis() as u64,
                walk_ms,
            )
}

#[cfg(target_os = "linux")]
fn reserve_sample(now_ms: u64) -> Option<u64> {
    let previous = LAST_ALLOCATOR_SLACK_SAMPLE_AT_MS.load(std::sync::atomic::Ordering::Acquire);
    if !sample_is_stale(now_ms) {
        return None;
    }
    LAST_ALLOCATOR_SLACK_SAMPLE_AT_MS
        .compare_exchange(
            previous,
            now_ms,
            std::sync::atomic::Ordering::AcqRel,
            std::sync::atomic::Ordering::Acquire,
        )
        .ok()
}

#[cfg(target_os = "linux")]
fn reserve_relief(now_ms: u64) -> Option<u64> {
    let previous = LAST_ALLOCATOR_SLACK_RELIEF_AT_MS.load(std::sync::atomic::Ordering::Acquire);
    let due = cached_allocator_slack().is_some_and(|slack| {
        slack >= ALLOCATOR_SLACK_RELIEF_THRESHOLD_BYTES
            && (previous == UNSAMPLED_AT_MS
                || now_ms.saturating_sub(previous)
                    >= ALLOCATOR_SLACK_RELIEF_MIN_INTERVAL.as_millis() as u64)
    });
    due.then(|| {
        LAST_ALLOCATOR_SLACK_RELIEF_AT_MS
            .compare_exchange(
                previous,
                now_ms,
                std::sync::atomic::Ordering::AcqRel,
                std::sync::atomic::Ordering::Acquire,
            )
            .ok()
    })?
}

#[cfg(target_os = "linux")]
fn log_allocator_relief(relief: AllocatorPressureRelief) {
    log::info!(
        "allocator slack relief: allocator_accounting_bytes={} allocator_accounting_source={} rss_drop_bytes={:?} phys_footprint_drop_bytes={:?} rss_bytes_before={:?} rss_bytes_after={:?} address_space_slack_bytes_before={:?} address_space_slack_bytes_after={:?}",
        relief.allocator_accounting_bytes,
        relief.allocator_accounting_source,
        relief.rss_drop_bytes,
        relief.phys_footprint_drop_bytes,
        relief.rss_before_bytes,
        relief.rss_after_bytes,
        relief.allocator_before.retained_slack_bytes,
        relief.allocator_after.retained_slack_bytes,
    );
}

/// Record the latest periodic relief pass for the memory census.
#[cfg(target_os = "linux")]
fn record_last_allocator_relief(relief: &AllocatorPressureRelief) {
    let ordering = std::sync::atomic::Ordering::Release;
    LAST_ALLOCATOR_SLACK_RELIEF_ACCOUNTING_BYTES.store(relief.allocator_accounting_bytes, ordering);
    LAST_ALLOCATOR_SLACK_RELIEF_RSS_DROP_BYTES.store(
        relief.rss_drop_bytes.unwrap_or(UNKNOWN_ALLOCATOR_SLACK),
        ordering,
    );
    LAST_ALLOCATOR_SLACK_RELIEF_FOOTPRINT_DROP_BYTES.store(
        relief
            .phys_footprint_drop_bytes
            .unwrap_or(UNKNOWN_ALLOCATOR_SLACK),
        ordering,
    );
}

pub fn last_allocator_relief_at_ms() -> Option<u64> {
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    {
        let value = LAST_ALLOCATOR_SLACK_RELIEF_AT_MS.load(std::sync::atomic::Ordering::Acquire);
        return (value != UNSAMPLED_AT_MS).then_some(value);
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        None
    }
}

/// Allocator-accounting figure from the latest periodic relief pass; see
/// `allocator_relief_accounting_source` for what it measures on this platform.
pub fn last_allocator_relief_accounting_bytes() -> u64 {
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    {
        return LAST_ALLOCATOR_SLACK_RELIEF_ACCOUNTING_BYTES
            .load(std::sync::atomic::Ordering::Acquire);
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        0
    }
}

/// Observed RSS drop across the latest periodic relief pass, if one ran and
/// both RSS readings were available.
pub fn last_allocator_relief_rss_drop_bytes() -> Option<u64> {
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    {
        let value =
            LAST_ALLOCATOR_SLACK_RELIEF_RSS_DROP_BYTES.load(std::sync::atomic::Ordering::Acquire);
        return (value != UNKNOWN_ALLOCATOR_SLACK).then_some(value);
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        None
    }
}

/// Observed physical-footprint drop across the latest periodic relief pass
/// (macOS only).
pub fn last_allocator_relief_phys_footprint_drop_bytes() -> Option<u64> {
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    {
        let value = LAST_ALLOCATOR_SLACK_RELIEF_FOOTPRINT_DROP_BYTES
            .load(std::sync::atomic::Ordering::Acquire);
        return (value != UNKNOWN_ALLOCATOR_SLACK).then_some(value);
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        None
    }
}

/// What `last_allocator_relief_accounting_bytes` measures on this platform,
/// or `None` where no relief pass exists.
pub fn allocator_relief_accounting_source() -> Option<&'static str> {
    #[cfg(target_os = "macos")]
    {
        Some(RELIEF_ACCOUNTING_SOURCE_MACOS)
    }
    #[cfg(target_os = "linux")]
    {
        Some(RELIEF_ACCOUNTING_SOURCE_JEMALLOC)
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        None
    }
}

/// Decide whether an opportunistic allocator relief pass is due.
#[cfg(any(target_os = "macos", target_os = "linux"))]
pub fn allocator_slack_relief_due(
    retained_slack_bytes: Option<u64>,
    last_relief: Option<std::time::Instant>,
    now: std::time::Instant,
) -> bool {
    let Some(slack) = retained_slack_bytes else {
        return false;
    };
    if slack < ALLOCATOR_SLACK_RELIEF_THRESHOLD_BYTES {
        return false;
    }
    match last_relief {
        None => true,
        Some(at) => now.duration_since(at) >= ALLOCATOR_SLACK_RELIEF_MIN_INTERVAL,
    }
}

/// On Linux, opportunistically return unused allocator pages without reading allocator
/// statistics on the module loop. The loop only reads cached atomics. When the
/// cached observation is stale, one sampler takes an initial snapshot; if it
/// finds enough slack, the page-return operation reuses it and takes only one
/// additional after snapshot, limiting the pass to two statistics walks.
///
/// `phys_footprint_bytes` cannot make this decision: it includes file mappings
/// and other resident memory, so it cannot identify mapped-but-unused allocator
/// arenas without allocator-zone statistics.
#[cfg(target_os = "linux")]
pub fn spawn_allocator_slack_relief_if_due(now: std::time::Instant) -> bool {
    let now_ms = allocator_slack_elapsed_ms(now);
    if sample_is_stale(now_ms) {
        let Some(previous_sample) = reserve_sample(now_ms) else {
            return false;
        };
        let spawned = std::thread::Builder::new()
            .name("aft-mem-slack-sample".to_string())
            .spawn(move || {
                let before = allocator_memory_snapshot();
                publish_allocator_slack(&before, now_ms);
                if reserve_relief(now_ms).is_some() {
                    let relief = relieve_allocator_pressure_from_snapshot(before);
                    publish_allocator_slack(
                        &relief.allocator_after,
                        allocator_slack_elapsed_ms(std::time::Instant::now()),
                    );
                    record_last_allocator_relief(&relief);
                    log_allocator_relief(relief);
                }
            })
            .is_ok();
        if !spawned {
            let _ = LAST_ALLOCATOR_SLACK_SAMPLE_AT_MS.compare_exchange(
                now_ms,
                previous_sample,
                std::sync::atomic::Ordering::AcqRel,
                std::sync::atomic::Ordering::Acquire,
            );
        }
        return false;
    }

    let Some(previous_relief) = reserve_relief(now_ms) else {
        return false;
    };
    let spawned = std::thread::Builder::new()
        .name("aft-mem-relief".to_string())
        .spawn(move || {
            let relief = relieve_allocator_pressure();
            publish_allocator_slack(
                &relief.allocator_after,
                allocator_slack_elapsed_ms(std::time::Instant::now()),
            );
            record_last_allocator_relief(&relief);
            log_allocator_relief(relief);
        })
        .is_ok();
    if !spawned {
        let _ = LAST_ALLOCATOR_SLACK_RELIEF_AT_MS.compare_exchange(
            now_ms,
            previous_relief,
            std::sync::atomic::Ordering::AcqRel,
            std::sync::atomic::Ordering::Acquire,
        );
    }
    spawned
}

/// macOS statistics take every allocator-zone lock; sampling and relief there
/// are explicit-only. Other non-Linux targets have no automatic sampler.
#[cfg(not(target_os = "linux"))]
pub fn spawn_allocator_slack_relief_if_due(_now: std::time::Instant) -> bool {
    false
}

/// Ask the platform allocator to return unused pages after a process-wide idle
/// gate. Callers own that gate because allocator pressure relief can add
/// latency. Linux purges all jemalloc arenas; macOS asks libmalloc for relief.
#[cfg(target_os = "macos")]
fn relieve_allocator_pressure_from_snapshot(
    allocator_before: AllocatorMemorySnapshot,
) -> AllocatorPressureRelief {
    let resident_before = ResidentReading::read();
    // The return value is libmalloc's own count of bytes it handed back. It is
    // reported as the allocator's claim; the footprint and RSS drops recorded
    // alongside it are what the process was actually seen to give up.
    let reported = unsafe { malloc_zone_pressure_relief(std::ptr::null_mut(), 0) };
    let allocator_after = allocator_memory_snapshot();
    let resident_after = ResidentReading::read();
    AllocatorPressureRelief::from_observations(
        usize_to_u64(reported),
        RELIEF_ACCOUNTING_SOURCE_MACOS,
        resident_before,
        resident_after,
        allocator_before,
        allocator_after,
    )
}

#[cfg(target_os = "macos")]
pub fn relieve_allocator_pressure() -> AllocatorPressureRelief {
    relieve_allocator_pressure_from_snapshot(allocator_memory_snapshot())
}

/// Hands the allocator's free pages back to the system without measuring
/// anything, for callers that would throw the measurement away. On macOS the
/// measured `relieve_allocator_pressure` reads zone statistics before and after
/// the relief, which takes every allocator-zone lock and stalls every thread
/// that allocates meanwhile; a call-graph build calls this between stages.
#[cfg(target_os = "macos")]
pub fn return_free_pages() {
    unsafe { malloc_zone_pressure_relief(std::ptr::null_mut(), 0) };
}

#[cfg(target_os = "linux")]
pub fn return_free_pages() {
    let _ = purge_allocator();
}

#[cfg(target_os = "linux")]
fn relieve_allocator_pressure_from_snapshot(
    allocator_before: AllocatorMemorySnapshot,
) -> AllocatorPressureRelief {
    let resident_before = ResidentReading::read();
    let _ = purge_allocator();
    let allocator_after = allocator_memory_snapshot();
    let resident_after = ResidentReading::read();
    AllocatorPressureRelief::from_jemalloc_resident_drop(
        resident_before,
        resident_after,
        allocator_before,
        allocator_after,
    )
}

#[cfg(target_os = "linux")]
pub fn relieve_allocator_pressure() -> AllocatorPressureRelief {
    relieve_allocator_pressure_from_snapshot(allocator_memory_snapshot())
}

#[cfg(target_os = "macos")]
pub fn process_rss_bytes() -> Option<u64> {
    let mut info = std::mem::MaybeUninit::<libc::proc_taskinfo>::zeroed();
    let size = std::mem::size_of::<libc::proc_taskinfo>();
    let written = unsafe {
        libc::proc_pidinfo(
            libc::getpid(),
            libc::PROC_PIDTASKINFO,
            0,
            info.as_mut_ptr().cast(),
            i32::try_from(size).ok()?,
        )
    };
    if written != i32::try_from(size).ok()? {
        return None;
    }
    Some(unsafe { info.assume_init() }.pti_resident_size)
}

/// Kernel physical footprint via `proc_pid_rusage` (`ri_phys_footprint`).
/// See `phys_footprint_bytes` for why this, not RSS, is the headline number.
#[cfg(target_os = "macos")]
pub fn process_phys_footprint_bytes() -> Option<u64> {
    let mut usage = std::mem::MaybeUninit::<libc::rusage_info_v4>::zeroed();
    let rc = unsafe {
        libc::proc_pid_rusage(
            libc::getpid(),
            libc::RUSAGE_INFO_V4,
            usage.as_mut_ptr().cast(),
        )
    };
    if rc != 0 {
        return None;
    }
    Some(unsafe { usage.assume_init() }.ri_phys_footprint)
}

#[cfg(not(target_os = "macos"))]
pub fn process_phys_footprint_bytes() -> Option<u64> {
    None
}

#[cfg(target_os = "linux")]
pub fn process_rss_bytes() -> Option<u64> {
    let statm = std::fs::read_to_string("/proc/self/statm").ok()?;
    let resident_pages = statm.split_whitespace().nth(1)?.parse::<u64>().ok()?;
    let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
    if page_size <= 0 {
        return None;
    }
    resident_pages.checked_mul(page_size as u64)
}

#[cfg(not(any(target_os = "macos", target_os = "linux")))]
pub fn process_rss_bytes() -> Option<u64> {
    None
}

pub fn rss_bytes() -> Option<u64> {
    process_rss_bytes()
}

#[cfg(any(test, feature = "test-support"))]
#[cfg_attr(not(test), allow(dead_code, unused_imports))]
pub mod snapshot_cap_tests {
    use super::*;

    pub fn root_with_bytes(bytes: u64) -> RootMemorySnapshot {
        let estimate = MemoryEstimate::estimated;
        RootMemorySnapshot::new(
            estimate(bytes),
            estimate(0),
            estimate(0),
            estimate(0),
            estimate(0),
            estimate(0),
            estimate(0),
            estimate(0),
            estimate(0),
        )
    }
}

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

    #[test]
    fn process_snapshot_preserves_negative_residuals() {
        assert_eq!(signed_difference(5, 8), -3);
    }

    #[test]
    fn allocator_reuse_window_stretches_with_walk_cost() {
        // A cheap walk keeps the floor.
        assert_eq!(allocator_reuse_window_ms(30_000, 0), 30_000);
        assert_eq!(allocator_reuse_window_ms(30_000, 150), 30_000);
        // An expensive walk waits 200 times its own duration...
        assert_eq!(allocator_reuse_window_ms(30_000, 1_000), 200_000);
        // ...but never more than ten minutes.
        assert_eq!(allocator_reuse_window_ms(30_000, 6_500), 600_000);
        assert_eq!(allocator_reuse_window_ms(30_000, u64::MAX), 600_000);
        // A floor above the cap still wins.
        assert_eq!(allocator_reuse_window_ms(900_000, 6_500), 900_000);
    }

    #[cfg(any(target_os = "macos", target_os = "linux"))]
    #[test]
    fn slack_sample_waits_longer_after_an_expensive_walk() {
        assert!(sample_is_stale_after(UNSAMPLED_AT_MS, 0, 0));
        // A cheap walk: due once a minute.
        assert!(!sample_is_stale_after(0, 59_999, 20));
        assert!(sample_is_stale_after(0, 60_000, 20));
        // A one-second walk: due after 200 seconds, not after one minute.
        assert!(!sample_is_stale_after(0, 60_000, 1_000));
        assert!(!sample_is_stale_after(0, 199_999, 1_000));
        assert!(sample_is_stale_after(0, 200_000, 1_000));
        // A multi-second glibc walk: capped at ten minutes.
        assert!(!sample_is_stale_after(0, 599_999, 6_500));
        assert!(sample_is_stale_after(0, 600_000, 6_500));
    }

    #[cfg(any(target_os = "macos", all(target_os = "linux", target_env = "gnu")))]
    #[test]
    fn cached_allocator_before_first_publication_is_unavailable_not_unsupported() {
        let _test_guard = allocator_observation_test_lock();
        let publish_guard = ALLOCATOR_OBSERVATION_PUBLISH_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        reset_allocator_observation_for_test();
        let calls_before = allocator_snapshot_calls_for_test();

        // Hold publication at startup: a request can arrive before the sampler
        // publishes its first reading, even on a measurable allocator.
        let (allocator, age_ms) = allocator_observation(AllocatorSource::Cached);
        assert_eq!(allocator.status, "unavailable");
        assert_eq!(
            allocator.not_estimated,
            Some("allocator_observation_unavailable")
        );
        assert_eq!(allocator.bytes_in_use, None);
        assert_eq!(allocator.size_allocated, None);
        assert_eq!(allocator.retained_slack_bytes, None);
        assert_eq!(age_ms, None);
        assert_eq!(allocator_snapshot_calls_for_test(), calls_before);

        drop(publish_guard);
        publish_allocator_slack(&measured_allocator(100, 140), 0);
        let (allocator, age_ms) = allocator_observation(AllocatorSource::Cached);
        assert_eq!(allocator.status, "measured");
        assert_eq!(allocator.not_estimated, None);
        assert_eq!(allocator.bytes_in_use, Some(100));
        assert_eq!(allocator.retained_slack_bytes, Some(40));
        assert!(age_ms.is_some());
        assert_eq!(allocator_snapshot_calls_for_test(), calls_before);
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn loop_slack_decision_never_reads_allocator_statistics() {
        LAST_OBSERVED_ALLOCATOR_SLACK_BYTES.store(
            UNKNOWN_ALLOCATOR_SLACK,
            std::sync::atomic::Ordering::Release,
        );
        LAST_ALLOCATOR_SLACK_SAMPLE_AT_MS
            .store(UNSAMPLED_AT_MS, std::sync::atomic::Ordering::Release);
        LAST_ALLOCATOR_SLACK_RELIEF_AT_MS
            .store(UNSAMPLED_AT_MS, std::sync::atomic::Ordering::Release);
        ALLOCATOR_SNAPSHOT_THREADS
            .lock()
            .expect("clear allocator snapshot thread log")
            .clear();

        for _ in 0..1_000 {
            assert!(!spawn_allocator_slack_relief_if_due(
                std::time::Instant::now()
            ));
        }
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
        let sampler_ran = || {
            ALLOCATOR_SNAPSHOT_THREADS
                .lock()
                .expect("read allocator snapshot thread log")
                .iter()
                .any(|name| name == "aft-mem-slack-sample")
        };
        while !sampler_ran() {
            assert!(std::time::Instant::now() < deadline, "sampler did not run");
            std::thread::yield_now();
        }
        // The thread log is process-global and other tests in the parallel suite
        // read allocator statistics on their own threads; the property under
        // test is only that THIS thread (the loop stand-in) never did.
        let loop_thread = std::thread::current()
            .name()
            .unwrap_or("unnamed")
            .to_string();
        let threads = ALLOCATOR_SNAPSHOT_THREADS
            .lock()
            .expect("read allocator snapshot thread log")
            .clone();
        assert!(
            !threads.iter().any(|name| *name == loop_thread),
            "the loop-side decision must never read allocator statistics: {threads:?}"
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn macos_automatic_slack_sampling_and_relief_are_explicit_only() {
        let before = allocator_snapshot_calls_for_test();
        for seconds in [0, 30, 300, 3_600] {
            assert!(!spawn_allocator_slack_relief_if_due(
                std::time::Instant::now() + std::time::Duration::from_secs(seconds)
            ));
        }
        assert_eq!(allocator_snapshot_calls_for_test(), before);
    }

    #[cfg(any(target_os = "macos", target_os = "linux"))]
    #[test]
    fn slack_relief_fires_on_large_slack_and_respects_spacing() {
        use std::time::{Duration, Instant};
        let now = Instant::now();
        let big = Some(ALLOCATOR_SLACK_RELIEF_THRESHOLD_BYTES);
        // Unknown slack (allocator stats unavailable) never fires.
        assert!(!allocator_slack_relief_due(None, None, now));
        // Below threshold never fires.
        assert!(!allocator_slack_relief_due(
            Some(ALLOCATOR_SLACK_RELIEF_THRESHOLD_BYTES - 1),
            None,
            now
        ));
        // At threshold with no prior pass fires.
        assert!(allocator_slack_relief_due(big, None, now));
        // A recent pass suppresses the next one...
        let recent = now - Duration::from_secs(10);
        assert!(!allocator_slack_relief_due(big, Some(recent), now));
        // ...until the minimum spacing has elapsed.
        let stale = now - ALLOCATOR_SLACK_RELIEF_MIN_INTERVAL;
        assert!(allocator_slack_relief_due(big, Some(stale), now));
    }

    #[test]
    fn json_estimator_scales_with_payload_content() {
        let empty = estimated_json_bytes(&serde_json::json!({}));
        let populated = estimated_json_bytes(&serde_json::json!({"message": "hello"}));
        assert_eq!(empty, 0);
        assert!(populated >= 12);
    }

    #[cfg(any(target_os = "macos", target_os = "linux"))]
    #[test]
    fn allocator_snapshot_reports_measured_slack() {
        let allocator = allocator_memory_snapshot();
        if allocator.status == "measured" {
            let in_use = allocator.bytes_in_use.expect("allocator bytes in use");
            let allocated = allocator.size_allocated.expect("allocator size allocated");
            assert_eq!(
                allocator.retained_slack_bytes,
                Some(allocated.saturating_sub(in_use))
            );
        } else {
            assert_eq!(allocator.status, "not_estimated_on_this_platform");
            assert_eq!(allocator.bytes_in_use, None);
            assert_eq!(allocator.size_allocated, None);
            assert_eq!(allocator.retained_slack_bytes, None);
            assert_eq!(
                allocator.not_estimated,
                Some("jemalloc_statistics_unavailable")
            );
        }
    }

    fn measured_allocator(bytes_in_use: u64, size_allocated: u64) -> AllocatorMemorySnapshot {
        AllocatorMemorySnapshot {
            status: "measured",
            bytes_in_use: Some(bytes_in_use),
            size_allocated: Some(size_allocated),
            retained_slack_bytes: Some(size_allocated.saturating_sub(bytes_in_use)),
            retained_slack_label: ALLOCATOR_SLACK_LABEL,
            not_estimated: None,
            #[cfg(target_os = "linux")]
            jemalloc: Some(JemallocMemorySnapshot {
                accounting_source: "jemalloc",
                allocated: bytes_in_use,
                active: bytes_in_use,
                resident: size_allocated,
                retained: 0,
            }),
        }
    }

    #[cfg(target_os = "linux")]
    const GIB: u64 = 1024 * 1024 * 1024;
    const MIB: u64 = 1024 * 1024;

    /// Kernel RSS can fall independently of the allocator's resident estimate.
    /// The record must carry both figures, each labelled.
    #[cfg(target_os = "linux")]
    #[test]
    fn relief_reports_rss_drop_when_allocator_accounting_is_unchanged() {
        let in_use = 2 * GIB;
        let size_allocated = in_use + 7560 * MIB;
        let before = ResidentReading {
            rss_bytes: Some(6440 * MIB),
            phys_footprint_bytes: None,
        };
        let after = ResidentReading {
            rss_bytes: Some(4910 * MIB),
            phys_footprint_bytes: None,
        };
        let relief = AllocatorPressureRelief::from_jemalloc_resident_drop(
            before,
            after,
            measured_allocator(in_use, size_allocated),
            measured_allocator(in_use, size_allocated),
        );
        assert_eq!(relief.allocator_accounting_bytes, 0);
        assert_eq!(
            relief.allocator_accounting_source,
            RELIEF_ACCOUNTING_SOURCE_JEMALLOC
        );
        assert_eq!(relief.rss_drop_bytes, Some(1530 * MIB));
        assert_eq!(relief.rss_before_bytes, Some(6440 * MIB));
        assert_eq!(relief.rss_after_bytes, Some(4910 * MIB));
        assert_eq!(relief.phys_footprint_drop_bytes, None);
        // Slack is unchanged even though memory went back to the OS: it is
        // address space, not residency.
        assert_eq!(
            relief.allocator_before.retained_slack_bytes,
            relief.allocator_after.retained_slack_bytes
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn relief_keeps_allocator_accounting_and_resident_drops_independent() {
        // jemalloc's resident estimate fell by 64 MiB while RSS fell by 300 MiB.
        let relief = AllocatorPressureRelief::from_jemalloc_resident_drop(
            ResidentReading {
                rss_bytes: Some(1000 * MIB),
                phys_footprint_bytes: None,
            },
            ResidentReading {
                rss_bytes: Some(700 * MIB),
                phys_footprint_bytes: None,
            },
            measured_allocator(100 * MIB, 500 * MIB),
            measured_allocator(100 * MIB, 436 * MIB),
        );
        assert_eq!(relief.allocator_accounting_bytes, 64 * MIB);
        assert_eq!(relief.rss_drop_bytes, Some(300 * MIB));

        // RSS growth during the pass floors the drop at zero; a missing
        // reading yields no drop rather than a guessed one.
        let grew = AllocatorPressureRelief::from_jemalloc_resident_drop(
            ResidentReading {
                rss_bytes: Some(700 * MIB),
                phys_footprint_bytes: Some(500 * MIB),
            },
            ResidentReading {
                rss_bytes: Some(900 * MIB),
                phys_footprint_bytes: None,
            },
            measured_allocator(100 * MIB, 500 * MIB),
            measured_allocator(100 * MIB, 500 * MIB),
        );
        assert_eq!(grew.rss_drop_bytes, Some(0));
        assert_eq!(grew.phys_footprint_drop_bytes, None);
    }

    #[test]
    fn macos_style_relief_reports_allocator_return_and_footprint_drop() {
        let relief = AllocatorPressureRelief::from_observations(
            40 * MIB,
            RELIEF_ACCOUNTING_SOURCE_MACOS,
            ResidentReading {
                rss_bytes: Some(900 * MIB),
                phys_footprint_bytes: Some(600 * MIB),
            },
            ResidentReading {
                rss_bytes: Some(900 * MIB),
                phys_footprint_bytes: Some(520 * MIB),
            },
            measured_allocator(100 * MIB, 500 * MIB),
            measured_allocator(100 * MIB, 500 * MIB),
        );
        assert_eq!(relief.allocator_accounting_bytes, 40 * MIB);
        assert_eq!(
            relief.allocator_accounting_source,
            RELIEF_ACCOUNTING_SOURCE_MACOS
        );
        assert_eq!(relief.rss_drop_bytes, Some(0));
        assert_eq!(relief.phys_footprint_drop_bytes, Some(80 * MIB));
    }

    #[test]
    fn allocator_snapshot_labels_slack_as_address_space() {
        let value = serde_json::to_value(measured_allocator(1, 3)).unwrap();
        assert_eq!(value["retained_slack_bytes"], 2);
        assert_eq!(value["retained_slack_label"], ALLOCATOR_SLACK_LABEL);
        assert!(ALLOCATOR_SLACK_LABEL.starts_with("address-space slack"));
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn linux_allocator_pressure_relief_smoke() {
        let mut allocation = vec![0u8; 32 * 1024 * 1024];
        for byte in allocation.iter_mut().step_by(4096) {
            *byte = 1;
        }
        std::hint::black_box(&allocation);
        drop(allocation);

        let relief = relieve_allocator_pressure();
        assert_eq!(
            relief.allocator_accounting_source,
            RELIEF_ACCOUNTING_SOURCE_JEMALLOC
        );
        assert!(relief.allocator_before.jemalloc.is_some());
        assert!(relief.allocator_after.jemalloc.is_some());
        purge_allocator().expect("jemalloc all-arena purge must succeed");
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    #[test]
    fn allocator_snapshot_is_honest_when_platform_counters_are_unavailable() {
        let allocator = allocator_memory_snapshot();
        assert_eq!(allocator.status, "not_estimated_on_this_platform");
        assert_eq!(allocator.bytes_in_use, None);
        assert_eq!(allocator.size_allocated, None);
        assert_eq!(allocator.retained_slack_bytes, None);
        assert_eq!(
            allocator.not_estimated,
            Some("platform_allocator_statistics_unavailable")
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    #[ignore = "bounded live RSS experiment; run explicitly after allocator changes"]
    fn allocator_pressure_relief_warm_then_idle_measurement() {
        let warm_pages = (0..16 * 1024)
            .map(|seed| {
                let mut page = Box::new([0u8; 4096]);
                page[0] = seed as u8;
                page
            })
            .collect::<Vec<_>>();
        std::hint::black_box(&warm_pages);
        drop(warm_pages);

        let relief = relieve_allocator_pressure();
        let sqlite = SqliteMemorySnapshot::measure();
        eprintln!(
            "warm-then-idle pressure relief: rss_before={:?} rss_after={:?} footprint_before={:?} footprint_after={:?} allocator_in_use_before={:?} allocator_in_use_after={:?} allocator_allocated_before={:?} allocator_allocated_after={:?} address_space_slack_before={:?} address_space_slack_after={:?} allocator_accounting={} ({}) sqlite_used={} sqlite_highwater={}",
            relief.rss_before_bytes,
            relief.rss_after_bytes,
            relief.phys_footprint_before_bytes,
            relief.phys_footprint_after_bytes,
            relief.allocator_before.bytes_in_use,
            relief.allocator_after.bytes_in_use,
            relief.allocator_before.size_allocated,
            relief.allocator_after.size_allocated,
            relief.allocator_before.retained_slack_bytes,
            relief.allocator_after.retained_slack_bytes,
            relief.allocator_accounting_bytes,
            relief.allocator_accounting_source,
            sqlite.memory_used_bytes,
            sqlite.memory_highwater_bytes,
        );
        assert_eq!(relief.allocator_before.status, "measured");
        assert_eq!(relief.allocator_after.status, "measured");
    }
}