oomtop-core 0.2.2

oomtop core: pure data model, units, attribution, headroom, forecast, throttle, query and ranking logic (no I/O)
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
//! OOM forecast (SPEC §8.3): which killer applies, its thresholds, a linear-trend ETA, the likely victim and
//! recent kills — all pure functions over history, config text and snapshots.
//!
//! Forecast rules:
//! - Linear least-squares fit of the last 2–5 min of available memory, swap used and PSI (every window of
//!   5, 4, 3 and 2 min is tried; the earliest qualifying ETA wins) against the nearest applicable threshold.
//!   A window is the shortest run of newest samples that spans **at least** its length (the sample just
//!   before the cutoff is included when it is within [`WINDOW_SLACK_MS`]), so jittered wall-clock timestamps
//!   still give the 2-min window its full 2 min.
//! - The memory-pressure series is PSI `some avg10` (SPEC §21).
//! - The killer-configuration parsers and [`likely_victim`] in this module are the pure reference
//!   implementations used by the core tests and goldens. The shipping binary builds `Oom` thresholds in
//!   `oomtop-collect` (`linux/oom.rs`) and picks the victim in `oomtop-cli` (`engine.rs`); keep them in step.
//! - Shown only when the trend is consistent: R² ≥ 0.6, ≥ 8 points over ≥ 2 min, both halves of the window
//!   trend the same way, and no single step accounts for more than half of the change — so a single spike
//!   (or a one-off step such as a model load) never raises an alarm. ETA must be < 30 min.
//! - Killer semantics: the kernel, systemd-oomd and jetsam act when **any** of their rules is met (oomd
//!   pressure adds its sustain duration); earlyoom acts only when **all** of its thresholds are met
//!   (available memory *and* free swap), so its ETA is the latest of its per-metric ETAs. The systemd-oomd
//!   swap rule is itself an AND: memory used *and* swap used both above `SwapUsedLimit` (systemd
//!   `oomd_mem_available_below && oomd_swap_free_below`); the memory half is checked when RAM size is known.
//! - A series whose newest value is older than [`MAX_STALE_S`] is not forecast; a slightly stale one has its
//!   ETA measured from the newest history point, not from its own last value.
//! - macOS swap is dynamic (swap files are added on demand), so "swap used → swap total" is not a limit
//!   when jetsam is the killer; the ceiling there is `swap_limit` (swap used + free space on the swap
//!   volume — jetsam kills when swap can't grow). With ample disk that ETA is far beyond 30 min: "stable".

use crate::history::{History, HistoryPoint};
use crate::model::{
    Forecast, ForecastTarget, GroupKind, Oom, OomKill, OomKiller, OomThreshold, OsKind, ProcId, Snapshot,
    ThresholdMetric, Victim,
};
use crate::units::{parse_duration_s, parse_percent};
use serde::{Deserialize, Serialize};

pub const MIN_R2: f64 = 0.6;
pub const MAX_ETA_S: f64 = 30.0 * 60.0;
/// Fit window (5 min) and minimum span of data required (2 min).
pub const WINDOW_MS: u64 = 5 * 60 * 1000;
pub const MIN_SPAN_S: f64 = 120.0;
/// Minimum number of points for a fit.
pub const MIN_POINTS: usize = 8;
/// Windows tried, longest first (SPEC §8.3 "last 2–5 min").
pub const WINDOWS_MS: [u64; 4] = [5 * 60_000, 4 * 60_000, 3 * 60_000, 2 * 60_000];
/// How far before a window's cutoff the sample that completes the window may lie (one slow sample
/// interval). Without it a window only reaches its nominal span when a sample lands exactly on the cutoff
/// millisecond, and the 2-min window could never satisfy [`MIN_SPAN_S`] on live (jittered) timestamps.
pub const WINDOW_SLACK_MS: u64 = 10_000;
/// A single step may account for at most this share of the fitted change over the window.
pub const MAX_STEP_SHARE: f64 = 0.5;
/// A metric whose newest sample is older than this (relative to the newest history point) is not forecast.
pub const MAX_STALE_S: f64 = 30.0;

/// systemd-oomd defaults (oomd.conf(5)).
pub const OOMD_DEFAULT_SWAP_USED_LIMIT_PCT: f64 = 90.0;
pub const OOMD_DEFAULT_MEM_PRESSURE_LIMIT_PCT: f64 = 60.0;
pub const OOMD_DEFAULT_MEM_PRESSURE_DURATION_S: u32 = 30;
/// earlyoom defaults (`-m 10 -s 10`; SIGKILL at half).
pub const EARLYOOM_DEFAULT_MEM_PCT: f64 = 10.0;
pub const EARLYOOM_DEFAULT_SWAP_PCT: f64 = 10.0;

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
pub struct LinearFit {
    /// Units per second.
    pub slope: f64,
    pub intercept: f64,
    pub r2: f64,
    /// Time span covered, seconds.
    pub span_s: f64,
    pub n: usize,
}

impl LinearFit {
    /// Fitted value at time `t`.
    pub fn at(&self, t: f64) -> f64 {
        self.intercept + self.slope * t
    }
}

/// Least-squares fit of `(t_s, y)` points.
pub fn linear_fit(points: &[(f64, f64)]) -> Option<LinearFit> {
    let n = points.len();
    if n < 2 {
        return None;
    }
    if points.iter().any(|(t, y)| !t.is_finite() || !y.is_finite()) {
        return None;
    }
    let nf = n as f64;
    let mean_t = points.iter().map(|p| p.0).sum::<f64>() / nf;
    let mean_y = points.iter().map(|p| p.1).sum::<f64>() / nf;
    let mut sxx = 0.0;
    let mut sxy = 0.0;
    let mut syy = 0.0;
    for (t, y) in points {
        sxx += (t - mean_t).powi(2);
        sxy += (t - mean_t) * (y - mean_y);
        syy += (y - mean_y).powi(2);
    }
    if sxx == 0.0 {
        return None;
    }
    let slope = sxy / sxx;
    let intercept = mean_y - slope * mean_t;
    let r2 = if syy == 0.0 {
        0.0
    } else {
        (sxy * sxy) / (sxx * syy)
    };
    let span_s = points.last()?.0 - points.first()?.0;
    Some(LinearFit {
        slope,
        intercept,
        r2,
        span_s,
        n,
    })
}

/// Spike/step guard: both halves of the window trend in the fit's direction, no single step between
/// consecutive points — **in either direction** — accounts for more than [`MAX_STEP_SHARE`] of the fitted
/// change, and the newest value is still on the fitted line. A drop against the trend (an OOM kill, a stopped
/// hog, a reclaim) therefore clears the forecast instead of leaving a stale ETA.
pub fn trend_is_consistent(points: &[(f64, f64)], fit: &LinearFit) -> bool {
    if points.len() < MIN_POINTS || fit.slope == 0.0 {
        return false;
    }
    let total_change = (fit.slope * fit.span_s).abs();
    if total_change <= 0.0 {
        return false;
    }
    let max_step = points
        .windows(2)
        .map(|w| (w[1].1 - w[0].1).abs())
        .fold(0.0, f64::max);
    if max_step > MAX_STEP_SHARE * total_change {
        return false;
    }
    // The newest actual value must sit near the line (a recent reversal the fit hasn't caught up with).
    if let Some((t_last, y_last)) = points.last() {
        if (y_last - fit.at(*t_last)).abs() > MAX_STEP_SHARE * total_change {
            return false;
        }
    }
    let mid = points.len() / 2;
    let halves = [&points[..mid], &points[mid..]];
    halves.iter().all(|h| match linear_fit(h) {
        Some(f) => f.slope.signum() == fit.slope.signum() && f.slope != 0.0,
        None => false,
    })
}

/// ETA (seconds from the last point) for a series to reach `threshold`, rising (`rising = true`) or falling.
/// Returns `None` unless the trend is consistent, heading towards the threshold, and ETA < 30 min. The ETA
/// is measured from the newest **actual** value (not the fitted one) at the fitted rate.
pub fn eta_to_threshold(points: &[(f64, f64)], threshold: f64, rising: bool) -> Option<(f64, LinearFit)> {
    if points.len() < MIN_POINTS || !threshold.is_finite() {
        return None;
    }
    let fit = linear_fit(points)?;
    if fit.span_s < MIN_SPAN_S || fit.r2 < MIN_R2 {
        return None;
    }
    if !trend_is_consistent(points, &fit) {
        return None;
    }
    let (_, y_now) = *points.last()?;
    let towards = if rising {
        fit.slope > 0.0 && y_now < threshold
    } else {
        fit.slope < 0.0 && y_now > threshold
    };
    if !towards {
        return None;
    }
    let eta = (threshold - y_now) / fit.slope;
    (eta.is_finite() && eta > 0.0 && eta < MAX_ETA_S).then_some((eta, fit))
}

/// Tries every window in [`WINDOWS_MS`] and returns the earliest qualifying ETA (conservative: a trend that
/// only started recently is not diluted by the flat minutes before it).
pub fn eta_in_history(
    history: &History,
    f: impl Fn(&HistoryPoint) -> Option<f64>,
    threshold: f64,
    rising: bool,
) -> Option<(f64, LinearFit)> {
    let now_ms = history.last()?.t_ms;
    WINDOWS_MS
        .iter()
        .filter_map(|w| {
            let w = &window_reaching(history, *w);
            // `series` is relative to the window's first point; points where the metric is missing are
            // dropped, so the series may end before "now".
            let t0 = history.window(*w).first()?.t_ms;
            let points = history.series(*w, &f);
            let stale_s = now_ms.saturating_sub(t0) as f64 / 1000.0 - points.last()?.0;
            if stale_s > MAX_STALE_S {
                return None;
            }
            let (eta, fit) = eta_to_threshold(&points, threshold, rising)?;
            let eta = eta - stale_s.max(0.0);
            (eta > 0.0).then_some((eta, fit))
        })
        .min_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal))
}

/// The window length (ms, back from the newest point) that makes a nominal `window_ms` window span at least
/// `window_ms`: extended to the newest sample at or before the cutoff when that sample is within
/// [`WINDOW_SLACK_MS`] of it; `window_ms` itself otherwise (history shorter than the window, or a gap).
pub fn window_reaching(history: &History, window_ms: u64) -> u64 {
    let Some(now_ms) = history.last().map(|p| p.t_ms) else {
        return window_ms;
    };
    let cutoff = now_ms.saturating_sub(window_ms);
    history
        .iter()
        .map(|p| p.t_ms)
        .take_while(|t| *t <= cutoff)
        .last()
        .filter(|t| cutoff - *t <= WINDOW_SLACK_MS)
        .map(|t| now_ms - t)
        .unwrap_or(window_ms)
}

fn metric_value(p: &HistoryPoint, metric: ThresholdMetric, mem_total: Option<u64>) -> Option<f64> {
    match metric {
        ThresholdMetric::SwapUsedPct => {
            let t = p.swap_total.filter(|t| *t > 0)? as f64;
            Some(p.swap_used? as f64 / t * 100.0)
        }
        ThresholdMetric::MemPressurePct => p.psi_some_avg10,
        ThresholdMetric::AvailableBytes => p.available.map(|v| v as f64),
        ThresholdMetric::AvailablePct => {
            let t = mem_total.filter(|t| *t > 0)? as f64;
            Some(p.available? as f64 / t * 100.0)
        }
    }
}

/// Swap/available metrics fall towards their threshold or rise towards it.
fn metric_rising(metric: ThresholdMetric) -> bool {
    matches!(
        metric,
        ThresholdMetric::SwapUsedPct | ThresholdMetric::MemPressurePct
    )
}

/// Converts a fitted slope (units/s) into the forecast rate (bytes/min; 0 for PSI, which has no byte rate).
fn rate_per_min(metric: ThresholdMetric, slope: f64, swap_total: Option<u64>, mem_total: Option<u64>) -> f64 {
    let per_min = slope.abs() * 60.0;
    match metric {
        ThresholdMetric::AvailableBytes => per_min,
        ThresholdMetric::SwapUsedPct => swap_total.map(|t| per_min * t as f64 / 100.0).unwrap_or(0.0),
        ThresholdMetric::AvailablePct => mem_total.map(|t| per_min * t as f64 / 100.0).unwrap_or(0.0),
        ThresholdMetric::MemPressurePct => 0.0,
    }
}

/// Whether the threshold is already met at the latest point.
fn already_met(p: &HistoryPoint, th: &OomThreshold, mem_total: Option<u64>) -> Option<bool> {
    let v = metric_value(p, th.metric, mem_total)?;
    Some(if metric_rising(th.metric) {
        v >= th.value
    } else {
        v <= th.value
    })
}

/// ETA for one metric to reach `value`: `(0, None)` when already met at the newest point, `(eta, fit)` when
/// trending towards it, `None` otherwise.
fn metric_eta(
    history: &History,
    last: &HistoryPoint,
    metric: ThresholdMetric,
    value: f64,
    mem_total: Option<u64>,
) -> Option<(f64, Option<LinearFit>)> {
    let th = OomThreshold {
        metric,
        value,
        ..Default::default()
    };
    if already_met(last, &th, mem_total) == Some(true) {
        return Some((0.0, None));
    }
    let (eta, fit) = eta_in_history(
        history,
        |p| metric_value(p, metric, mem_total),
        value,
        metric_rising(metric),
    )?;
    Some((eta, Some(fit)))
}

/// Forecasts the nearest OOM target from history and the host's killer thresholds.
pub fn forecast_oom(history: &History, oom: &Oom) -> Option<Forecast> {
    forecast_oom_with(history, oom, None)
}

/// [`forecast_oom`] with the RAM size, which enables `AvailablePct` thresholds (earlyoom `-m`).
pub fn forecast_oom_with(history: &History, oom: &Oom, mem_total: Option<u64>) -> Option<Forecast> {
    let last = history.last()?;
    // Latest known swap size (one sample without swap data must not hide the swap forecast).
    let swap_total = history.iter().filter_map(|p| p.swap_total).last();
    let killers: Vec<OomKiller> = if oom.killers.is_empty() {
        vec![oom.killer]
    } else {
        oom.killers.clone()
    };
    let jetsam = killers.contains(&OomKiller::Jetsam);
    let base_killer = if killers.contains(&OomKiller::Kernel) {
        OomKiller::Kernel
    } else {
        oom.killer
    };
    let mut best: Option<Forecast> = None;
    let mut consider = |f: Forecast| {
        let better = match &best {
            None => true,
            Some(b) => f.eta_s < b.eta_s || (f.eta_s == b.eta_s && f.confidence > b.confidence),
        };
        if better {
            best = Some(f);
        }
    };

    // Swap exhaustion: swap_used → the swap ceiling. Fixed-size swap: swap_total. Jetsam hosts (macOS) add
    // swap files on demand, so the ceiling is `swap_limit` (swap used + free space on the swap volume).
    let ceiling = if jetsam {
        history.iter().filter_map(|p| p.swap_limit).last()
    } else {
        swap_total
    };
    if let Some(total) = ceiling.filter(|t| *t > 0) {
        if let Some((eta, fit)) =
            eta_in_history(history, |p| p.swap_used.map(|v| v as f64), total as f64, true)
        {
            consider(Forecast {
                target: ForecastTarget::SwapExhaustion,
                killer: Some(base_killer),
                eta_s: eta as u64,
                confidence: fit.r2,
                rate_per_min: fit.slope.abs() * 60.0,
                window_s: fit.span_s as u64,
            });
        }
    }
    // Available memory → 0.
    if let Some((eta, fit)) = eta_in_history(history, |p| p.available.map(|v| v as f64), 0.0, false) {
        consider(Forecast {
            target: ForecastTarget::AvailableExhaustion,
            killer: Some(base_killer),
            eta_s: eta as u64,
            confidence: fit.r2,
            rate_per_min: fit.slope.abs() * 60.0,
            window_s: fit.span_s as u64,
        });
    }

    // Killer thresholds.
    for killer in &killers {
        let ths: Vec<&OomThreshold> = oom.thresholds.iter().filter(|t| t.killer == *killer).collect();
        if ths.is_empty() {
            continue;
        }
        // Per threshold: (eta, fit of the metric that decides it, that metric).
        let per: Vec<Option<(f64, Option<LinearFit>, ThresholdMetric)>> = ths
            .iter()
            .map(|th| {
                // earlyoom without swap: "free swap below X %" always holds.
                let no_swap = th.metric == ThresholdMetric::SwapUsedPct && swap_total == Some(0);
                if no_swap && *killer == OomKiller::Earlyoom {
                    return Some((0.0, None, th.metric));
                }
                let (eta, fit) = metric_eta(history, last, th.metric, th.value, mem_total)?;
                let mut out = (eta, fit, th.metric);
                if *killer == OomKiller::SystemdOomd && th.metric == ThresholdMetric::SwapUsedPct {
                    // Swap rule = swap used ≥ limit AND memory used ≥ limit (available ≤ 100 − limit).
                    // Without the RAM size only the swap half can be checked (earlier = conservative).
                    if mem_total.is_some() {
                        let avail_pct = (100.0 - th.value).clamp(0.0, 100.0);
                        let (m_eta, m_fit) =
                            metric_eta(history, last, ThresholdMetric::AvailablePct, avail_pct, mem_total)?;
                        // The later half decides ("already met" is ETA 0). Both met → (0, None): oomd is
                        // acting now, which is not a trend forecast.
                        if m_eta > out.0 {
                            out = (m_eta, m_fit, ThresholdMetric::AvailablePct);
                        }
                    }
                }
                Some((out.0 + th.duration_s.unwrap_or(0) as f64, out.1, out.2))
            })
            .collect();
        let as_forecast = |eta: f64, fit: &LinearFit, metric: ThresholdMetric| Forecast {
            target: ForecastTarget::KillerThreshold,
            killer: Some(*killer),
            eta_s: eta as u64,
            confidence: fit.r2,
            rate_per_min: rate_per_min(metric, fit.slope, swap_total, mem_total),
            window_s: fit.span_s as u64,
        };
        if *killer == OomKiller::Earlyoom {
            // AND: every threshold must be met or forecast; ETA = latest; needs at least one real trend.
            if per.iter().any(|p| p.is_none()) {
                continue;
            }
            let all: Vec<(f64, Option<LinearFit>, ThresholdMetric)> = per.into_iter().flatten().collect();
            let latest = all
                .iter()
                .filter(|(_, fit, _)| fit.is_some())
                .max_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
            if let Some((eta, Some(fit), metric)) = latest {
                if *eta < MAX_ETA_S {
                    let mut f = as_forecast(*eta, fit, *metric);
                    f.confidence = all
                        .iter()
                        .filter_map(|(_, fit, _)| fit.map(|x| x.r2))
                        .fold(1.0, f64::min);
                    consider(f);
                }
            }
        } else {
            // OR: the earliest trending threshold.
            for (eta, fit, metric) in per.into_iter().flatten() {
                if let Some(fit) = fit {
                    if eta < MAX_ETA_S {
                        consider(as_forecast(eta, &fit, metric));
                    }
                }
            }
        }
    }
    best
}

// -----------------------------------------------------------------------------------------------------
// Killer configuration (pure parsers; the collector reads the files / argv)
// -----------------------------------------------------------------------------------------------------

/// systemd-oomd global settings (`/etc/systemd/oomd.conf` + drop-ins, `[OOM]` section).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct OomdConfig {
    pub swap_used_limit_pct: f64,
    pub mem_pressure_limit_pct: f64,
    pub mem_pressure_duration_s: u32,
    /// "default" or the last file that set a value.
    pub source: String,
}

impl Default for OomdConfig {
    fn default() -> Self {
        OomdConfig {
            swap_used_limit_pct: OOMD_DEFAULT_SWAP_USED_LIMIT_PCT,
            mem_pressure_limit_pct: OOMD_DEFAULT_MEM_PRESSURE_LIMIT_PCT,
            mem_pressure_duration_s: OOMD_DEFAULT_MEM_PRESSURE_DURATION_S,
            source: "systemd-oomd default".into(),
        }
    }
}

/// Iterates `key=value` pairs of an INI-style systemd file, yielding `(section, key, value)`.
fn systemd_pairs(text: &str) -> Vec<(String, String, String)> {
    let mut section = String::new();
    let mut out = Vec::new();
    for raw in text.lines() {
        let line = raw.trim();
        if line.is_empty() || line.starts_with('#') || line.starts_with(';') {
            continue;
        }
        if line.starts_with('[') && line.ends_with(']') {
            section = line[1..line.len() - 1].trim().to_string();
            continue;
        }
        if let Some((k, v)) = line.split_once('=') {
            out.push((section.clone(), k.trim().to_string(), v.trim().to_string()));
        }
    }
    out
}

/// Parses oomd.conf layers in precedence order (main file first, then drop-ins alphabetically). Each layer
/// is `(origin, text)`. Invalid values are ignored (the previous value stays).
pub fn parse_oomd_conf(layers: &[(&str, &str)]) -> OomdConfig {
    let mut c = OomdConfig::default();
    for (origin, text) in layers {
        for (section, k, v) in systemd_pairs(text) {
            if section != "OOM" {
                continue;
            }
            match k.as_str() {
                "SwapUsedLimit" => {
                    if let Ok(p) = parse_percent(&v) {
                        c.swap_used_limit_pct = p;
                        c.source = origin.to_string();
                    }
                }
                "DefaultMemoryPressureLimit" => {
                    if let Ok(p) = parse_percent(&v) {
                        c.mem_pressure_limit_pct = p;
                        c.source = origin.to_string();
                    }
                }
                "DefaultMemoryPressureDurationSec" => {
                    if let Ok(s) = parse_duration_s(&v) {
                        c.mem_pressure_duration_s = s.round().clamp(0.0, u32::MAX as f64) as u32;
                        c.source = origin.to_string();
                    }
                }
                _ => {}
            }
        }
    }
    c
}

/// A unit's `ManagedOOM*` settings (slice/service/scope files or `systemctl show` output).
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct ManagedOom {
    pub unit: String,
    pub swap_kill: bool,
    pub pressure_kill: bool,
    pub pressure_limit_pct: Option<f64>,
    pub pressure_duration_s: Option<u32>,
}

/// Parses `ManagedOOMSwap=`, `ManagedOOMMemoryPressure=`, `ManagedOOMMemoryPressureLimit=` and
/// `ManagedOOMMemoryPressureDurationSec=` from a unit file or `systemctl show -p …` output.
pub fn parse_managed_oom(unit: &str, text: &str) -> ManagedOom {
    let mut m = ManagedOom {
        unit: unit.to_string(),
        ..Default::default()
    };
    for (_, k, v) in systemd_pairs(text) {
        match k.as_str() {
            "ManagedOOMSwap" => m.swap_kill = v == "kill",
            "ManagedOOMMemoryPressure" => m.pressure_kill = v == "kill",
            "ManagedOOMMemoryPressureLimit" => {
                // "0" / "0%" means "use the default".
                m.pressure_limit_pct = parse_percent(&v).ok().filter(|p| *p > 0.0);
            }
            "ManagedOOMMemoryPressureDurationSec" => {
                m.pressure_duration_s = parse_duration_s(&v)
                    .ok()
                    .filter(|s| *s > 0.0)
                    .map(|s| s.round().clamp(0.0, u32::MAX as f64) as u32);
            }
            _ => {}
        }
    }
    m
}

/// systemd-oomd thresholds. Without unit information both kinds are assumed to be managed (conservative);
/// with units, only managed kinds apply and the tightest pressure limit wins.
pub fn oomd_thresholds(cfg: &OomdConfig, units: &[ManagedOom]) -> Vec<OomThreshold> {
    let mut out = Vec::new();
    let swap = units.is_empty() || units.iter().any(|u| u.swap_kill);
    if swap {
        out.push(OomThreshold {
            killer: OomKiller::SystemdOomd,
            metric: ThresholdMetric::SwapUsedPct,
            value: cfg.swap_used_limit_pct,
            duration_s: None,
            source: cfg.source.clone(),
        });
    }
    let pressure_units: Vec<&ManagedOom> = units.iter().filter(|u| u.pressure_kill).collect();
    if units.is_empty() {
        out.push(OomThreshold {
            killer: OomKiller::SystemdOomd,
            metric: ThresholdMetric::MemPressurePct,
            value: cfg.mem_pressure_limit_pct,
            duration_s: Some(cfg.mem_pressure_duration_s),
            source: cfg.source.clone(),
        });
    } else if let Some(u) = pressure_units.iter().min_by(|a, b| {
        let la = a.pressure_limit_pct.unwrap_or(cfg.mem_pressure_limit_pct);
        let lb = b.pressure_limit_pct.unwrap_or(cfg.mem_pressure_limit_pct);
        la.partial_cmp(&lb).unwrap_or(std::cmp::Ordering::Equal)
    }) {
        out.push(OomThreshold {
            killer: OomKiller::SystemdOomd,
            metric: ThresholdMetric::MemPressurePct,
            value: u.pressure_limit_pct.unwrap_or(cfg.mem_pressure_limit_pct),
            duration_s: Some(u.pressure_duration_s.unwrap_or(cfg.mem_pressure_duration_s)),
            source: if u.pressure_limit_pct.is_some() {
                format!("{} ManagedOOMMemoryPressureLimit", u.unit)
            } else {
                cfg.source.clone()
            },
        });
    }
    out
}

/// earlyoom settings from its argv (`-m PERCENT[,KILL]`, `-s PERCENT[,KILL]`, `-M KiB[,KILL]`,
/// `-S KiB[,KILL]`). SIGTERM thresholds are the ones that matter for a forecast.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct EarlyoomConfig {
    pub mem_term_pct: f64,
    pub mem_kill_pct: f64,
    pub swap_term_pct: f64,
    pub swap_kill_pct: f64,
    pub mem_term_kib: Option<u64>,
    pub mem_kill_kib: Option<u64>,
    pub swap_term_kib: Option<u64>,
    pub swap_kill_kib: Option<u64>,
}

impl Default for EarlyoomConfig {
    fn default() -> Self {
        EarlyoomConfig {
            mem_term_pct: EARLYOOM_DEFAULT_MEM_PCT,
            mem_kill_pct: EARLYOOM_DEFAULT_MEM_PCT / 2.0,
            swap_term_pct: EARLYOOM_DEFAULT_SWAP_PCT,
            swap_kill_pct: EARLYOOM_DEFAULT_SWAP_PCT / 2.0,
            mem_term_kib: None,
            mem_kill_kib: None,
            swap_term_kib: None,
            swap_kill_kib: None,
        }
    }
}

fn split_pair(v: &str) -> Result<(f64, Option<f64>), String> {
    let mut it = v.splitn(2, ',');
    let a: f64 = it
        .next()
        .unwrap_or("")
        .trim()
        .parse()
        .map_err(|_| format!("invalid value {v:?}"))?;
    let b = match it.next() {
        Some(s) => Some(
            s.trim()
                .parse::<f64>()
                .map_err(|_| format!("invalid value {v:?}"))?,
        ),
        None => None,
    };
    if !a.is_finite() || a < 0.0 || b.map(|x| !x.is_finite() || x < 0.0).unwrap_or(false) {
        return Err(format!("invalid value {v:?}"));
    }
    Ok((a, b))
}

/// Parses an earlyoom command line (argv[0] may be included). Unknown options are ignored.
/// Reference implementation (see the module docs): the collector parses the live argv itself.
pub fn parse_earlyoom_argv(argv: &[String]) -> Result<EarlyoomConfig, String> {
    // Short options that take an argument (so their value is not mistaken for an option).
    const WITH_ARG: &[&str] = &["-r", "-N", "--prefer", "--avoid", "--ignore"];
    let mut c = EarlyoomConfig::default();
    let mut i = 0;
    let args: Vec<&str> = argv.iter().map(String::as_str).collect();
    while i < args.len() {
        let a = args[i];
        // `-m10` style: split after the option letter. argv is another process's command line, so the
        // letter may be any character: only split on a char boundary ("-é" must not panic).
        let (flag, attached) =
            if a.len() > 2 && a.starts_with('-') && !a.starts_with("--") && a.is_char_boundary(2) {
                (&a[..2], Some(&a[2..]))
            } else {
                (a, None)
            };
        let value = |i: &mut usize| -> Result<String, String> {
            if let Some(v) = attached {
                return Ok(v.to_string());
            }
            *i += 1;
            args.get(*i)
                .map(|s| s.to_string())
                .ok_or_else(|| format!("{flag} needs a value"))
        };
        match flag {
            "-m" => {
                let (t, k) = split_pair(&value(&mut i)?)?;
                if t > 100.0 {
                    return Err(format!("-m {t} out of range"));
                }
                c.mem_term_pct = t;
                c.mem_kill_pct = k.unwrap_or(t / 2.0);
            }
            "-s" => {
                let (t, k) = split_pair(&value(&mut i)?)?;
                if t > 100.0 {
                    return Err(format!("-s {t} out of range"));
                }
                c.swap_term_pct = t;
                c.swap_kill_pct = k.unwrap_or(t / 2.0);
            }
            "-M" => {
                let (t, k) = split_pair(&value(&mut i)?)?;
                c.mem_term_kib = Some(t as u64);
                c.mem_kill_kib = Some(k.unwrap_or(t / 2.0) as u64);
            }
            "-S" => {
                let (t, k) = split_pair(&value(&mut i)?)?;
                c.swap_term_kib = Some(t as u64);
                c.swap_kill_kib = Some(k.unwrap_or(t / 2.0) as u64);
            }
            f if WITH_ARG.contains(&f) && attached.is_none() => {
                i += 1;
            }
            _ => {}
        }
        i += 1;
    }
    Ok(c)
}

/// earlyoom SIGTERM thresholds. `-m`/`-M` and `-s`/`-S`: the lower of the two applies. With known totals
/// the thresholds are resolved to bytes / swap-used %; without swap (`swap_total == Some(0)`) the swap
/// condition is always met and no swap threshold is emitted.
pub fn earlyoom_thresholds(
    c: &EarlyoomConfig,
    mem_total: Option<u64>,
    swap_total: Option<u64>,
) -> Vec<OomThreshold> {
    let mut out = Vec::new();
    let source = "earlyoom argv".to_string();
    match mem_total.filter(|t| *t > 0) {
        Some(total) => {
            let pct_bytes = (total as f64 * c.mem_term_pct / 100.0) as u64;
            let bytes = match c.mem_term_kib {
                Some(k) => pct_bytes.min(k.saturating_mul(1024)),
                None => pct_bytes,
            };
            out.push(OomThreshold {
                killer: OomKiller::Earlyoom,
                metric: ThresholdMetric::AvailableBytes,
                value: bytes as f64,
                duration_s: None,
                source: source.clone(),
            });
        }
        None => out.push(OomThreshold {
            killer: OomKiller::Earlyoom,
            metric: ThresholdMetric::AvailablePct,
            value: c.mem_term_pct,
            duration_s: None,
            source: source.clone(),
        }),
    }
    if swap_total != Some(0) {
        let mut free_pct = c.swap_term_pct;
        if let (Some(k), Some(t)) = (c.swap_term_kib, swap_total.filter(|t| *t > 0)) {
            free_pct = free_pct.min(k as f64 * 1024.0 / t as f64 * 100.0);
        }
        out.push(OomThreshold {
            killer: OomKiller::Earlyoom,
            metric: ThresholdMetric::SwapUsedPct,
            value: (100.0 - free_pct).clamp(0.0, 100.0),
            duration_s: None,
            source,
        });
    }
    out
}

/// What the collector found about user-space OOM killers.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct OomInputs {
    pub os: OsKind,
    /// systemd-oomd is active: its config and the managed units (empty = unknown).
    pub oomd: Option<(OomdConfig, Vec<ManagedOom>)>,
    /// earlyoom is running with this argv.
    pub earlyoom_argv: Option<Vec<String>>,
    pub mem_total: Option<u64>,
    pub swap_total: Option<u64>,
}

/// Reference implementation for tests and goldens: the Linux collector builds the live `Oom` itself
/// (`oomtop-collect`, `linux/decode.rs` `decode_oom`).
///
/// Builds `Oom { killer, killers, thresholds }` (forecast/victim/kills are filled later). The nearest
/// killer is the most proactive one present: earlyoom, then systemd-oomd, then the kernel; jetsam on macOS.
/// An unparsable earlyoom argv falls back to earlyoom's defaults.
pub fn oom_setup(inputs: &OomInputs) -> Oom {
    let mut oom = Oom::default();
    match inputs.os {
        OsKind::Macos => {
            oom.killer = OomKiller::Jetsam;
            oom.killers = vec![OomKiller::Jetsam];
        }
        _ => {
            oom.killers.push(OomKiller::Kernel);
            oom.killer = OomKiller::Kernel;
            if let Some((cfg, units)) = &inputs.oomd {
                oom.killers.push(OomKiller::SystemdOomd);
                oom.thresholds.extend(oomd_thresholds(cfg, units));
                oom.killer = OomKiller::SystemdOomd;
            }
            if let Some(argv) = &inputs.earlyoom_argv {
                oom.killers.push(OomKiller::Earlyoom);
                let cfg = parse_earlyoom_argv(argv).unwrap_or_default();
                oom.thresholds
                    .extend(earlyoom_thresholds(&cfg, inputs.mem_total, inputs.swap_total));
                oom.killer = OomKiller::Earlyoom;
            }
        }
    }
    oom
}

// -----------------------------------------------------------------------------------------------------
// Likely victim & recent kills
// -----------------------------------------------------------------------------------------------------

/// Processes jetsam never picks (kernel, launchd) or that sit in the highest bands.
const JETSAM_EXEMPT: &[&str] = &["kernel_task", "launchd", "WindowServer", "loginwindow"];

/// Who the killer would likely pick. Linux: highest `oom_score` (readable unprivileged). macOS: jetsam bands
/// need root, so the largest-footprint non-system group (its root process), labeled as a heuristic.
///
/// This is the rule the binary uses: `oomtop-cli`'s engine calls it for every snapshot.
pub fn likely_victim(s: &Snapshot) -> Option<Victim> {
    let footprint = |p: &crate::model::Process| p.mem.footprint_or_pss.usable().unwrap_or(0);
    let scored = s
        .processes
        .iter()
        .filter(|p| p.oom_score.unwrap_or(0) > 0 && p.id.pid > 1)
        .max_by(|a, b| {
            a.oom_score
                .cmp(&b.oom_score)
                .then_with(|| footprint(a).cmp(&footprint(b)))
                .then_with(|| b.id.pid.cmp(&a.id.pid))
        });
    if s.host.os != OsKind::Macos {
        if let Some(p) = scored {
            return Some(Victim {
                id: p.id,
                name: p.name.clone(),
                group_id: s.group_of(p.id).map(|g| g.id.clone()),
                reason: format!("highest oom_score ({})", p.oom_score.unwrap_or(0)),
                heuristic: false,
            });
        }
        if s.host.os == OsKind::Linux {
            return None;
        }
    }
    // macOS (or no oom_score data): largest non-system group by footprint.
    let group = s
        .groups
        .iter()
        .filter(|g| g.kind != GroupKind::System && !g.is_self && !g.protected)
        .filter(|g| g.totals.footprint.usable().unwrap_or(0) > 0)
        .max_by_key(|g| {
            (
                g.totals.footprint.usable().unwrap_or(0),
                std::cmp::Reverse(g.id.clone()),
            )
        });
    let pick = |id: ProcId| {
        s.process(id)
            .filter(|p| !JETSAM_EXEMPT.contains(&p.name.as_str()) && p.id.pid > 1)
    };
    if let Some(g) = group {
        let proc_ = g.root.and_then(pick).or_else(|| {
            g.members
                .iter()
                .filter_map(|m| pick(m.id))
                .max_by_key(|p| footprint(p))
        });
        if let Some(p) = proc_ {
            return Some(Victim {
                id: p.id,
                name: if g.label.is_empty() {
                    p.name.clone()
                } else {
                    g.label.clone()
                },
                group_id: Some(g.id.clone()),
                reason: "largest footprint among apps (heuristic: jetsam bands need root)".into(),
                heuristic: true,
            });
        }
    }
    let p = s
        .processes
        .iter()
        .filter(|p| p.id.pid > 1 && !JETSAM_EXEMPT.contains(&p.name.as_str()) && Some(p.id.pid) != s.self_pid)
        .max_by_key(|p| footprint(p))
        .filter(|p| footprint(p) > 0)?;
    Some(Victim {
        id: p.id,
        name: p.name.clone(),
        group_id: s.group_of(p.id).map(|g| g.id.clone()),
        reason: "largest footprint (heuristic: jetsam bands need root)".into(),
        heuristic: true,
    })
}

/// cgroup v2 `memory.events` counters.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct MemoryEvents {
    pub low: u64,
    pub high: u64,
    pub max: u64,
    pub oom: u64,
    pub oom_kill: u64,
    pub oom_group_kill: u64,
}

/// Parses `memory.events` ("oom_kill 3" lines; unknown keys ignored).
pub fn parse_memory_events(text: &str) -> MemoryEvents {
    let mut e = MemoryEvents::default();
    for line in text.lines() {
        let mut it = line.split_whitespace();
        let (Some(k), Some(v)) = (it.next(), it.next()) else {
            continue;
        };
        let Ok(v) = v.parse::<u64>() else { continue };
        match k {
            "low" => e.low = v,
            "high" => e.high = v,
            "max" => e.max = v,
            "oom" => e.oom = v,
            "oom_kill" => e.oom_kill = v,
            "oom_group_kill" => e.oom_group_kill = v,
            _ => {}
        }
    }
    e
}

/// New OOM kills between two reads (counter resets → 0).
pub fn oom_kills_since(prev: &MemoryEvents, cur: &MemoryEvents) -> u64 {
    cur.oom_kill.saturating_sub(prev.oom_kill)
}

/// Days since 1970-01-01 for a civil date (proleptic Gregorian).
fn days_from_civil(y: i64, m: u32, d: u32) -> i64 {
    let y = if m <= 2 { y - 1 } else { y };
    let era = if y >= 0 { y } else { y - 399 } / 400;
    let yoe = y - era * 400;
    let mp = (m as i64 + 9) % 12;
    let doy = (153 * mp + 2) / 5 + d as i64 - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    era * 146_097 + doe - 719_468
}

/// Parses an `.ips` timestamp like `"2026-09-29 10:10:10.00 +0100"` to ms since the epoch.
pub fn parse_ips_timestamp_ms(s: &str) -> Option<u64> {
    let mut parts = s.split_whitespace();
    let date = parts.next()?;
    let time = parts.next()?;
    let tz = parts.next().unwrap_or("+0000");
    let mut d = date.split('-');
    let y: i64 = d.next()?.parse().ok()?;
    let mo: u32 = d.next()?.parse().ok()?;
    let da: u32 = d.next()?.parse().ok()?;
    if !(1..=12).contains(&mo) || !(1..=31).contains(&da) {
        return None;
    }
    let mut t = time.split(':');
    let h: i64 = t.next()?.parse().ok()?;
    let mi: i64 = t.next()?.parse().ok()?;
    let sec: f64 = t.next()?.parse().ok()?;
    if !(0..24).contains(&h) || !(0..60).contains(&mi) || !(0.0..61.0).contains(&sec) {
        return None;
    }
    let (sign, digits) = match tz.as_bytes().first()? {
        b'+' => (1, &tz[1..]),
        b'-' => (-1, &tz[1..]),
        _ => return None,
    };
    if digits.len() != 4 || !digits.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    let tzh: i64 = digits[..2].parse().ok()?;
    let tzm: i64 = digits[2..].parse().ok()?;
    let offset_s = sign * (tzh * 3600 + tzm * 60);
    let secs = days_from_civil(y, mo, da) * 86_400 + h * 3600 + mi * 60 - offset_s;
    let ms = secs as f64 * 1000.0 + (sec * 1000.0).round();
    (ms >= 0.0).then_some(ms as u64)
}

/// Parses a macOS `JetsamEvent-*.ips` report into kills (processes carrying a `reason`). The first line is
/// a JSON header, the rest the JSON body; older single-document reports are accepted too.
pub fn parse_jetsam_event(text: &str, source: &str) -> Vec<OomKill> {
    let (header, body) = match text.split_once('\n') {
        Some((h, b)) if !b.trim().is_empty() => (
            serde_json::from_str::<serde_json::Value>(h).ok(),
            serde_json::from_str::<serde_json::Value>(b).ok(),
        ),
        _ => (None, serde_json::from_str::<serde_json::Value>(text).ok()),
    };
    let Some(body) = body else {
        return Vec::new();
    };
    let at_ms = body
        .get("date")
        .and_then(|d| d.as_str())
        .and_then(parse_ips_timestamp_ms)
        .or_else(|| {
            header
                .as_ref()?
                .get("timestamp")?
                .as_str()
                .and_then(parse_ips_timestamp_ms)
        });
    body.get("processes")
        .and_then(|p| p.as_array())
        .map(|procs| {
            procs
                .iter()
                .filter(|p| p.get("reason").and_then(|r| r.as_str()).is_some())
                .map(|p| OomKill {
                    at_ms,
                    killer: OomKiller::Jetsam,
                    victim_name: p.get("name").and_then(|n| n.as_str()).map(str::to_string),
                    victim_pid: p
                        .get("pid")
                        .and_then(|n| n.as_u64())
                        .and_then(|n| u32::try_from(n).ok()),
                    source: format!(
                        "{source} ({})",
                        p.get("reason").and_then(|r| r.as_str()).unwrap_or("?")
                    ),
                })
                .collect()
        })
        .unwrap_or_default()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::units::{GIB, MIB};

    fn hist(f: impl Fn(u64) -> u64) -> History {
        let mut h = History::default();
        for i in 0..=60u64 {
            h.push(HistoryPoint {
                t_ms: i * 5_000,
                swap_used: Some(f(i)),
                swap_total: Some(7 * GIB),
                available: Some(4 * GIB),
                ..Default::default()
            });
        }
        h
    }

    #[test]
    fn steady_growth_forecasts() {
        // +400 MiB/min: 5 s steps → 400/12 MiB per step, starting at 4 GiB.
        let h = hist(|i| 4 * GIB + i * 400 * MIB / 12);
        let f = forecast_oom(&h, &Oom::default()).expect("forecast");
        assert_eq!(f.target, ForecastTarget::SwapExhaustion);
        // remaining at t=300s: 7G - (4G + 2000M) = 1072 MiB → ~160 s
        assert!((150..=170).contains(&f.eta_s), "eta {}", f.eta_s);
        assert!(f.confidence > 0.99);
        assert!((f.rate_per_min - 400.0 * MIB as f64).abs() < MIB as f64);
    }

    #[test]
    fn two_minute_window_qualifies_with_jittered_timestamps() {
        // Live samples are wall-clock: ~2 s apart with millisecond jitter, never exactly on a window cutoff.
        // Swap falls for 3 min, then grows 5 MiB/s for 126 s: only the 2-min window is all growth (the
        // longer ones fail the halves check), and it must still span ≥ 2 min.
        let mut h = History::default();
        let mut last_t = 0;
        for i in 0..=153u64 {
            let t_ms = i * 2_000 + (i * 37) % 23 + 7;
            let t = t_ms as f64 / 1000.0;
            let used = if t <= 180.0 {
                5.0 * GIB as f64 - 5.0 * MIB as f64 * t
            } else {
                5.0 * GIB as f64 - 5.0 * MIB as f64 * 180.0 + 5.0 * MIB as f64 * (t - 180.0)
            };
            h.push(HistoryPoint {
                t_ms,
                swap_used: Some(used as u64),
                swap_total: Some(7 * GIB),
                available: Some(4 * GIB),
                ..Default::default()
            });
            last_t = t_ms;
        }
        // No sample sits on the 2-min cutoff: the plain window spans less than MIN_SPAN_S.
        let plain = h.series(120_000, |p| p.swap_used.map(|v| v as f64));
        assert!(plain.last().unwrap().0 - plain[0].0 < MIN_SPAN_S);
        let w = window_reaching(&h, 120_000);
        assert!((120_000..=124_000).contains(&w), "window {w} ms");
        let f = forecast_oom(&h, &Oom::default()).expect("forecast from the 2-min window");
        assert_eq!(f.target, ForecastTarget::SwapExhaustion);
        assert!((120..=124).contains(&f.window_s), "window {}", f.window_s);
        // 7 GiB − (5 GiB − 900 MiB + 630 MiB) = 2318 MiB at 5 MiB/s ≈ 464 s.
        assert!((440..=490).contains(&f.eta_s), "eta {}", f.eta_s);
        assert!((f.rate_per_min - 300.0 * MIB as f64).abs() < 5.0 * MIB as f64);

        // A history shorter than the window is not stretched, and a gap wider than the slack is not bridged.
        let mut short = History::default();
        for i in 0..=50u64 {
            short.push(HistoryPoint {
                t_ms: last_t + i * 2_003,
                ..Default::default()
            });
        }
        assert_eq!(window_reaching(&short, 120_000), 120_000);
        let mut gap = History::default();
        for t_ms in [0, 2_000, 40_000, 62_000, 181_000] {
            gap.push(HistoryPoint {
                t_ms,
                ..Default::default()
            });
        }
        assert_eq!(window_reaching(&gap, 120_000), 120_000);
        assert_eq!(window_reaching(&History::default(), 120_000), 120_000);
    }

    #[test]
    fn earlyoom_argv_with_a_multibyte_option_letter_does_not_panic() {
        let argv: Vec<String> = ["earlyoom", "-é", "-é5", "–m", "-m", "7", "-s3"]
            .iter()
            .map(|s| s.to_string())
            .collect();
        let c = parse_earlyoom_argv(&argv).unwrap();
        assert_eq!(c.mem_term_pct, 7.0);
        assert_eq!(c.swap_term_pct, 3.0);
    }

    #[test]
    fn single_spike_does_not_forecast() {
        let h = hist(|i| if i == 60 { 6 * GIB } else { 4 * GIB });
        assert!(forecast_oom(&h, &Oom::default()).is_none());
    }

    #[test]
    fn step_does_not_forecast() {
        // A model load: swap jumps once by 2 GiB halfway and stays (R² of a line through it is ~0.75).
        let h = hist(|i| if i >= 30 { 6 * GIB } else { 4 * GIB });
        let s = h.series(WINDOW_MS, |p| p.swap_used.map(|v| v as f64));
        assert!(linear_fit(&s).unwrap().r2 > MIN_R2);
        assert!(forecast_oom(&h, &Oom::default()).is_none());
    }

    #[test]
    fn flat_is_stable() {
        let h = hist(|_| 4 * GIB);
        assert!(forecast_oom(&h, &Oom::default()).is_none());
    }

    #[test]
    fn too_little_data() {
        let mut h = History::default();
        for i in 0..10u64 {
            h.push(HistoryPoint {
                t_ms: i * 5_000, // 45 s < 2 min
                swap_used: Some(GIB + i * 100 * MIB),
                swap_total: Some(2 * GIB),
                ..Default::default()
            });
        }
        assert!(forecast_oom(&h, &Oom::default()).is_none());
        assert!(forecast_oom(&History::default(), &Oom::default()).is_none());
    }

    #[test]
    fn recent_trend_uses_shorter_window() {
        // Flat for 3 min, then +300 MiB/min for 2.5 min: the 5-min window fails the halves check, 2 min qualifies.
        let h = hist(|i| {
            if i <= 36 {
                4 * GIB
            } else {
                4 * GIB + (i - 36) * 25 * MIB
            }
        });
        let f = forecast_oom(&h, &Oom::default()).expect("forecast from recent trend");
        assert_eq!(f.target, ForecastTarget::SwapExhaustion);
        assert!(f.window_s <= 180, "window {}", f.window_s);
    }

    #[test]
    fn jetsam_ignores_dynamic_swap_total() {
        let h = hist(|i| 4 * GIB + i * 400 * MIB / 12);
        let oom = Oom {
            killer: OomKiller::Jetsam,
            killers: vec![OomKiller::Jetsam],
            ..Default::default()
        };
        assert!(forecast_oom(&h, &oom).is_none());
    }

    #[test]
    fn jetsam_forecasts_against_the_swap_ceiling() {
        // macOS: swap grows 400 MiB/min; the swap volume leaves room for 7 GiB of swap in total.
        let mut h = History::default();
        for i in 0..=60u64 {
            h.push(HistoryPoint {
                t_ms: i * 5_000,
                swap_used: Some(4 * GIB + i * 400 * MIB / 12),
                swap_total: Some(5 * GIB + i * 400 * MIB / 12), // grows with use: not a limit
                swap_limit: Some(7 * GIB),
                available: Some(4 * GIB),
                ..Default::default()
            });
        }
        let oom = oom_setup(&OomInputs {
            os: OsKind::Macos,
            ..Default::default()
        });
        let f = forecast_oom(&h, &oom).expect("forecast against the swap ceiling");
        assert_eq!(f.target, ForecastTarget::SwapExhaustion);
        assert_eq!(f.killer, Some(OomKiller::Jetsam));
        assert!((150..=170).contains(&f.eta_s), "eta {}", f.eta_s);
        // Plenty of disk: the ceiling is far away → stable.
        let mut roomy = History::default();
        for p in h.iter() {
            roomy.push(HistoryPoint {
                swap_limit: Some(700 * GIB),
                ..p.clone()
            });
        }
        assert!(forecast_oom(&roomy, &oom).is_none());
    }

    #[test]
    fn drop_after_growth_clears_the_forecast() {
        // Swap grows steadily for 4 min, then an OOM kill (or a stopped hog) frees most of it at once.
        let grow = |i: u64| 4 * GIB + i * 400 * MIB / 12;
        let h = hist(|i| if i < 50 { grow(i) } else { 2 * GIB });
        assert!(
            forecast_oom(&h, &Oom::default()).is_none(),
            "a drop against the trend must not leave an ETA"
        );
        // The same growth without the drop does forecast.
        assert!(forecast_oom(&hist(grow), &Oom::default()).is_some());
    }

    #[test]
    fn eta_is_measured_from_the_actual_last_value() {
        // A perfect line except the last point, which sits a little above it (within the guard).
        let mut pts: Vec<(f64, f64)> = (0..=36).map(|i| (i as f64 * 5.0, 100.0 + i as f64)).collect();
        pts.last_mut().unwrap().1 += 3.0; // actual 139 vs fitted ~136.x
        let (eta, fit) = eta_to_threshold(&pts, 200.0, true).unwrap();
        let from_actual = (200.0 - 139.0) / fit.slope;
        assert!((eta - from_actual).abs() < 1e-9, "eta {eta} vs {from_actual}");
    }

    #[test]
    fn available_falling_forecasts() {
        let mut h = History::default();
        for i in 0..=36u64 {
            h.push(HistoryPoint {
                t_ms: i * 5_000,
                available: Some(4 * GIB - i * 50 * MIB),
                ..Default::default()
            });
        }
        let f = forecast_oom(&h, &Oom::default()).unwrap();
        assert_eq!(f.target, ForecastTarget::AvailableExhaustion);
        assert_eq!(f.killer, Some(OomKiller::Kernel));
        // 4096 − 1800 = 2296 MiB left at 600 MiB/min → ≈ 230 s
        assert!((225..=235).contains(&f.eta_s), "eta {}", f.eta_s);
    }

    #[test]
    fn oomd_pressure_threshold_adds_duration() {
        let mut h = History::default();
        for i in 0..=36u64 {
            h.push(HistoryPoint {
                t_ms: i * 5_000,
                psi_some_avg10: Some(10.0 + i as f64),
                ..Default::default()
            });
        }
        let oom = oom_setup(&OomInputs {
            os: OsKind::Linux,
            oomd: Some((OomdConfig::default(), vec![])),
            ..Default::default()
        });
        let f = forecast_oom(&h, &oom).unwrap();
        assert_eq!(f.target, ForecastTarget::KillerThreshold);
        assert_eq!(f.killer, Some(OomKiller::SystemdOomd));
        // PSI at 46 → 60 at +1/5 s = 70 s, + 30 s sustain = 100 s.
        assert!((98..=102).contains(&f.eta_s), "eta {}", f.eta_s);
    }

    #[test]
    fn earlyoom_needs_both_conditions() {
        let total = 16 * GIB;
        let mut h = History::default();
        for i in 0..=36u64 {
            h.push(HistoryPoint {
                t_ms: i * 5_000,
                // available falls 3 GiB → 1.2 GiB …
                available: Some(3 * GIB - i * 50 * MIB),
                // … swap has plenty free and is flat
                swap_used: Some(GIB),
                swap_total: Some(8 * GIB),
                ..Default::default()
            });
        }
        let oom = oom_setup(&OomInputs {
            os: OsKind::Linux,
            earlyoom_argv: Some(vec!["earlyoom".into(), "-m".into(), "5".into()]),
            mem_total: Some(total),
            swap_total: Some(8 * GIB),
            ..Default::default()
        });
        assert_eq!(oom.killer, OomKiller::Earlyoom);
        // Swap never reaches 90 % used → earlyoom does not act; the kernel forecast (available → 0) remains.
        let f = forecast_oom(&h, &oom).unwrap();
        assert_eq!(f.killer, Some(OomKiller::Kernel));
        assert_eq!(f.target, ForecastTarget::AvailableExhaustion);

        // Without swap the memory condition alone decides (also when the collector still emits a swap
        // threshold for a swapless host).
        let mut with_swap_rule = oom_setup(&OomInputs {
            os: OsKind::Linux,
            earlyoom_argv: Some(vec!["-m".into(), "5".into()]),
            mem_total: Some(total),
            swap_total: None,
            ..Default::default()
        });
        assert_eq!(with_swap_rule.thresholds.len(), 2);
        with_swap_rule.killer = OomKiller::Earlyoom;
        let mut swapless = History::default();
        for p in h.iter() {
            swapless.push(HistoryPoint {
                swap_used: Some(0),
                swap_total: Some(0),
                ..p.clone()
            });
        }
        let f = forecast_oom(&swapless, &with_swap_rule).unwrap();
        assert_eq!(f.killer, Some(OomKiller::Earlyoom));
        let oom = oom_setup(&OomInputs {
            os: OsKind::Linux,
            earlyoom_argv: Some(vec!["-m".into(), "5".into()]),
            mem_total: Some(total),
            swap_total: Some(0),
            ..Default::default()
        });
        let f = forecast_oom(&h, &oom).unwrap();
        assert_eq!(f.killer, Some(OomKiller::Earlyoom));
        assert_eq!(f.target, ForecastTarget::KillerThreshold);
        // 1272 MiB now → 819.2 MiB (5 % of 16 GiB) at 600 MiB/min ≈ 45 s
        assert!((43..=47).contains(&f.eta_s), "eta {}", f.eta_s);
    }

    #[test]
    fn oomd_swap_rule_needs_memory_used_too() {
        // Swap grows 300 MiB/min from 6 GiB of 8 GiB; oomd SwapUsedLimit 90 % (7.2 GiB) in ≈ 4 min.
        let total = 16 * GIB;
        let build = |avail: &dyn Fn(u64) -> u64| {
            let mut h = History::default();
            for i in 0..=36u64 {
                h.push(HistoryPoint {
                    t_ms: i * 5_000,
                    swap_used: Some(5 * GIB + i * 25 * MIB),
                    swap_total: Some(8 * GIB),
                    available: Some(avail(i)),
                    psi_some_avg10: Some(1.0),
                    ..Default::default()
                });
            }
            h
        };
        let oom = oom_setup(&OomInputs {
            os: OsKind::Linux,
            oomd: Some((OomdConfig::default(), vec![])),
            ..Default::default()
        });
        // Plenty of RAM available (50 %): memory used is far below 90 % → oomd's swap rule cannot fire.
        let roomy = build(&|_| 8 * GIB);
        let f = forecast_oom_with(&roomy, &oom, Some(total)).expect("kernel swap exhaustion");
        assert_eq!(f.killer, Some(OomKiller::Kernel));
        assert_eq!(f.target, ForecastTarget::SwapExhaustion);
        // Without the RAM size only the swap half is checked (conservative: oomd forecast).
        let f = forecast_oom(&roomy, &oom).unwrap();
        assert_eq!(f.killer, Some(OomKiller::SystemdOomd));
        // Memory already ≥ 90 % used (available 1 GiB < 1.6 GiB): the swap half decides.
        let tight = build(&|_| GIB);
        let f = forecast_oom_with(&tight, &oom, Some(total)).unwrap();
        assert_eq!(f.killer, Some(OomKiller::SystemdOomd));
        assert_eq!(f.target, ForecastTarget::KillerThreshold);
        // remaining 7.2 GiB − 5.88 GiB ≈ 1352 MiB at 300 MiB/min ≈ 270 s
        assert!((260..=280).contains(&f.eta_s), "eta {}", f.eta_s);
        // Swap near the limit already, memory falling towards 10 % available: the memory half decides.
        let late = {
            let mut h = History::default();
            for i in 0..=36u64 {
                h.push(HistoryPoint {
                    t_ms: i * 5_000,
                    swap_used: Some(7 * GIB + 512 * MIB),
                    swap_total: Some(8 * GIB),
                    available: Some(4 * GIB - i * 50 * MIB),
                    ..Default::default()
                });
            }
            h
        };
        let f = forecast_oom_with(&late, &oom, Some(total)).unwrap();
        assert_eq!(f.killer, Some(OomKiller::SystemdOomd));
        // 2296 MiB now → 1638.4 MiB (10 % of 16 GiB) at 600 MiB/min ≈ 66 s
        assert!((60..=72).contains(&f.eta_s), "eta {}", f.eta_s);
        assert!((f.rate_per_min - 600.0 * MIB as f64).abs() < 2.0 * MIB as f64);
    }

    #[test]
    fn stale_series_is_not_forecast_from_its_own_end() {
        // Swap data stops 20 s before the newest point: the ETA is shortened by those 20 s.
        let mut h = hist(|i| 4 * GIB + i * 400 * MIB / 12);
        let fresh = forecast_oom(&h, &Oom::default()).unwrap().eta_s;
        let mut g = History::default();
        for p in h.iter() {
            g.push(p.clone());
        }
        for k in 1..=4u64 {
            g.push(HistoryPoint {
                t_ms: 300_000 + k * 5_000,
                available: Some(4 * GIB),
                ..Default::default()
            });
        }
        let f = forecast_oom(&g, &Oom::default()).unwrap();
        assert_eq!(f.target, ForecastTarget::SwapExhaustion);
        assert!(
            (fresh - 22..=fresh - 18).contains(&f.eta_s),
            "{} vs {fresh}",
            f.eta_s
        );
        // Stale for more than 30 s → no forecast from that metric.
        for k in 5..=12u64 {
            h.push(HistoryPoint {
                t_ms: 300_000 + k * 5_000,
                available: Some(4 * GIB),
                ..Default::default()
            });
        }
        let mut stale = History::default();
        for p in g.iter().chain(h.iter().filter(|p| p.t_ms > 320_000)) {
            stale.push(p.clone());
        }
        assert!(forecast_oom(&stale, &Oom::default()).is_none());
    }

    #[test]
    fn ips_timestamp_never_panics_on_odd_timezones() {
        for tz in ["+1é1", "+é12", "+12345", "+ab12", "Z", "", "+"] {
            let s = format!("2026-09-29 12:10:10.00 {tz}");
            let _ = parse_ips_timestamp_ms(&s);
        }
        assert_eq!(parse_ips_timestamp_ms("2026-09-29 12:10:10.00 +1é1"), None);
    }

    #[test]
    fn earlyoom_argv_forms() {
        let v = |s: &[&str]| s.iter().map(|x| x.to_string()).collect::<Vec<_>>();
        let c =
            parse_earlyoom_argv(&v(&["/usr/bin/earlyoom", "-r", "3600", "-m", "4,2", "-s", "20"])).unwrap();
        assert_eq!((c.mem_term_pct, c.mem_kill_pct), (4.0, 2.0));
        assert_eq!((c.swap_term_pct, c.swap_kill_pct), (20.0, 10.0));
        let c = parse_earlyoom_argv(&v(&["-m5", "-M", "1048576", "--prefer", "-m 1", "-n"])).unwrap();
        assert_eq!(c.mem_term_pct, 5.0);
        assert_eq!(c.mem_term_kib, Some(1_048_576));
        let d = parse_earlyoom_argv(&v(&["earlyoom"])).unwrap();
        assert_eq!(d, EarlyoomConfig::default());
        assert!(parse_earlyoom_argv(&v(&["-m"])).is_err());
        assert!(parse_earlyoom_argv(&v(&["-m", "x"])).is_err());
        assert!(parse_earlyoom_argv(&v(&["-s", "150"])).is_err());
        // -M lower than -m wins.
        let th = earlyoom_thresholds(&c, Some(64 * GIB), Some(0));
        assert_eq!(th.len(), 1);
        assert_eq!(th[0].value, GIB as f64);
        let th = earlyoom_thresholds(&EarlyoomConfig::default(), None, None);
        assert_eq!(th[0].metric, ThresholdMetric::AvailablePct);
        assert_eq!(th[1].value, 90.0);
    }

    #[test]
    fn oomd_config_parsing() {
        let main = "[OOM]\n#SwapUsedLimit=90%\nDefaultMemoryPressureLimit=50%\n";
        let dropin = "[OOM]\nSwapUsedLimit=80%\nDefaultMemoryPressureDurationSec=1min\nBogus=1\n";
        let c = parse_oomd_conf(&[
            ("/etc/systemd/oomd.conf", main),
            ("oomd.conf.d/10-x.conf", dropin),
        ]);
        assert_eq!(c.swap_used_limit_pct, 80.0);
        assert_eq!(c.mem_pressure_limit_pct, 50.0);
        assert_eq!(c.mem_pressure_duration_s, 60);
        assert_eq!(c.source, "oomd.conf.d/10-x.conf");
        let d = parse_oomd_conf(&[("x", "[OOM]\nSwapUsedLimit=lots\n")]);
        assert_eq!(d.swap_used_limit_pct, 90.0);

        let slice = parse_managed_oom(
            "user@1000.service",
            "[Service]\nManagedOOMMemoryPressure=kill\nManagedOOMMemoryPressureLimit=40%\n",
        );
        let root = parse_managed_oom("-.slice", "ManagedOOMSwap=kill\n");
        let th = oomd_thresholds(&OomdConfig::default(), &[slice, root]);
        assert_eq!(th.len(), 2);
        assert_eq!(th[0].metric, ThresholdMetric::SwapUsedPct);
        assert_eq!(th[1].value, 40.0);
        assert_eq!(th[1].duration_s, Some(30));
        let defaults = oomd_thresholds(&OomdConfig::default(), &[]);
        assert_eq!(defaults.len(), 2);
        let none = oomd_thresholds(&OomdConfig::default(), &[ManagedOom::default()]);
        assert!(none.is_empty());
    }

    #[test]
    fn setup_per_os() {
        let mac = oom_setup(&OomInputs {
            os: OsKind::Macos,
            ..Default::default()
        });
        assert_eq!(mac.killers, vec![OomKiller::Jetsam]);
        let linux = oom_setup(&OomInputs {
            os: OsKind::Linux,
            ..Default::default()
        });
        assert_eq!(linux.killers, vec![OomKiller::Kernel]);
        assert!(linux.thresholds.is_empty());
    }

    #[test]
    fn memory_events() {
        let e = parse_memory_events(
            "low 0\nhigh 12\nmax 3\noom 2\noom_kill 2\noom_group_kill 0\nnew_key 9\nbad\n",
        );
        assert_eq!(e.oom_kill, 2);
        assert_eq!(e.high, 12);
        let later = MemoryEvents { oom_kill: 5, ..e };
        assert_eq!(oom_kills_since(&e, &later), 3);
        assert_eq!(oom_kills_since(&later, &e), 0);
    }

    #[test]
    fn ips_timestamps_and_jetsam() {
        assert_eq!(parse_ips_timestamp_ms("1970-01-01 00:00:00.00 +0000"), Some(0));
        assert_eq!(parse_ips_timestamp_ms("1970-01-01 01:00:00.50 +0100"), Some(500));
        // 2026-09-29 10:10:10 UTC = 1790676610
        assert_eq!(
            parse_ips_timestamp_ms("2026-09-29 12:10:10.00 +0200"),
            Some(1_790_676_610_000)
        );
        assert_eq!(parse_ips_timestamp_ms("garbage"), None);
        assert_eq!(parse_ips_timestamp_ms("2026-13-01 00:00:00 +0000"), None);
        let ips = r#"{"bug_type":"298","timestamp":"2026-09-29 12:10:10.00 +0200","os_version":"macOS 26.6"}
{"date":"2026-09-29 12:10:09.50 +0200","largestProcess":"java","processes":[{"name":"java","pid":78921,"reason":"vm-pageshortage","rpages":200000},{"name":"Finder","pid":400,"rpages":1000}]}"#;
        let kills = parse_jetsam_event(ips, "JetsamEvent-2026-09-29-121010.ips");
        assert_eq!(kills.len(), 1);
        assert_eq!(kills[0].victim_name.as_deref(), Some("java"));
        assert_eq!(kills[0].victim_pid, Some(78921));
        assert_eq!(kills[0].at_ms, Some(1_790_676_609_500));
        assert!(kills[0].source.contains("vm-pageshortage"));
        assert!(parse_jetsam_event("not json", "x").is_empty());
    }
}