gam-runtime 0.3.157

Resource policy, caching, and warm-start runtime foundation for the gam penalized-likelihood engine
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
//! Typed Linux cgroup memory observations.
//!
//! `sysinfo::CGroupLimits` is intentionally not used here. Its aggregate loses
//! the distinction between the literal cgroup-v2 `memory.max = max` token and a
//! finite limit, derives "free" as `max - memory.current` even though
//! `memory.current` includes reclaimable file cache, and probes a fixed cgroup
//! path rather than the current process' hierarchy. Memory admission needs the
//! kernel contract before any of that information is erased.

use std::fmt;

/// The exact syntax of one cgroup-v2 `memory.max` value.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CgroupMemoryLimit {
    /// The literal `max` token: this level imposes no hard memory ceiling.
    Unlimited,
    /// A finite hard ceiling in bytes. Zero is valid and authoritative.
    Finite(u64),
}

/// Why a live cgroup memory hierarchy could not be observed safely.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CgroupMemoryProbeFailureKind {
    MalformedMembership,
    MissingUnifiedMount,
    MalformedMountInfo,
    Io,
    InvalidLimit,
    InvalidCounter,
    MissingCounter,
    InconsistentCounters,
}

impl fmt::Display for CgroupMemoryProbeFailureKind {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            Self::MalformedMembership => "malformed-membership",
            Self::MissingUnifiedMount => "missing-unified-mount",
            Self::MalformedMountInfo => "malformed-mountinfo",
            Self::Io => "io",
            Self::InvalidLimit => "invalid-limit",
            Self::InvalidCounter => "invalid-counter",
            Self::MissingCounter => "missing-counter",
            Self::InconsistentCounters => "inconsistent-counters",
        };
        formatter.write_str(name)
    }
}

/// Fail-closed evidence from an active cgroup controller probe.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CgroupMemoryProbeFailure {
    kind: CgroupMemoryProbeFailureKind,
    path: Box<str>,
    detail: Box<str>,
}

impl CgroupMemoryProbeFailure {
    pub const fn kind(&self) -> CgroupMemoryProbeFailureKind {
        self.kind
    }

    pub fn path(&self) -> &str {
        &self.path
    }

    pub fn detail(&self) -> &str {
        &self.detail
    }
}

impl fmt::Display for CgroupMemoryProbeFailure {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{} at {}: {}", self.kind, self.path, self.detail)
    }
}

/// Reclaim-aware headroom under the binding finite cgroup ancestor.
///
/// `working_set_bytes = memory.current - inactive_file`. Only inactive file
/// cache is credited as reclaimable; active file cache and reclaimable slab are
/// deliberately left in the working set. The governor's separate 1/4 headroom
/// remains available for reclaim latency, allocator slack, and untracked work.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CgroupMemoryAvailability {
    binding_path: Box<str>,
    limit_bytes: u64,
    current_bytes: u64,
    inactive_file_bytes: u64,
    working_set_bytes: u64,
    available_bytes: u64,
    inspected_levels: usize,
}

impl CgroupMemoryAvailability {
    /// Assemble one observation from counters that were read together.
    ///
    /// `None` when `inactive_file > current`, which is a torn read of the two
    /// counters rather than a legal state: the working set would be negative.
    /// This is the single construction site — the live probe, the replay used
    /// by [`crate::test_support::simulated_cgroup_memory_environment`], and the
    /// unit fixtures all funnel through it, so the derived `working_set` and
    /// `available` can never disagree between a measured and a replayed
    /// environment.
    pub(crate) fn from_consistent_counters(
        binding_path: impl Into<Box<str>>,
        limit_bytes: u64,
        current_bytes: u64,
        inactive_file_bytes: u64,
        inspected_levels: usize,
    ) -> Option<Self> {
        let working_set_bytes = current_bytes.checked_sub(inactive_file_bytes)?;
        Some(Self {
            binding_path: binding_path.into(),
            limit_bytes,
            current_bytes,
            inactive_file_bytes,
            working_set_bytes,
            available_bytes: limit_bytes.saturating_sub(working_set_bytes),
            inspected_levels,
        })
    }

    pub const fn limit_bytes(&self) -> u64 {
        self.limit_bytes
    }

    pub const fn current_bytes(&self) -> u64 {
        self.current_bytes
    }

    pub const fn available_bytes(&self) -> u64 {
        self.available_bytes
    }

}

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

    impl CgroupMemoryProbeFailure {
    }

    impl CgroupMemoryAvailability {
    }
}

impl fmt::Display for CgroupMemoryAvailability {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "finite cgroup ceiling at {} (limit={}, current={}, inactive_file={}, working_set={}, available={}, visible_levels={})",
            self.binding_path,
            self.limit_bytes,
            self.current_bytes,
            self.inactive_file_bytes,
            self.working_set_bytes,
            self.available_bytes,
            self.inspected_levels,
        )
    }
}

/// The process' typed cgroup memory provenance.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CgroupMemoryObservation {
    /// No active memory controller applies on this platform/hierarchy.
    NotPresent,
    /// A cgroup-v2 hierarchy was found, but every visible hard limit was the
    /// literal `max` token (or the process is at the unconstrained root).
    V2Unbounded {
        cgroup_path: Box<str>,
        inspected_levels: usize,
    },
    /// At least one finite hard ceiling applies. This carries the ancestor with
    /// the least reclaim-aware headroom.
    V2Limited(CgroupMemoryAvailability),
    /// A cgroup-v1 memory hierarchy is active. V1 exposes its unlimited state
    /// as an architecture-dependent numeric sentinel rather than a token, so
    /// every visible numeric ceiling participates in the minimum; an enormous
    /// sentinel naturally loses to host availability.
    V1Limited(CgroupMemoryAvailability),
    /// A memory controller appears active but its semantics could not be read
    /// exactly. Admission must fail closed rather than inherit host memory.
    ProbeFailed(CgroupMemoryProbeFailure),
}

impl fmt::Display for CgroupMemoryObservation {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotPresent => formatter.write_str("no active cgroup memory controller"),
            Self::V2Unbounded {
                cgroup_path,
                inspected_levels,
            } => write!(
                formatter,
                "unbounded cgroup-v2 hierarchy at {cgroup_path} ({inspected_levels} memory.max levels)"
            ),
            Self::V2Limited(observation) => write!(formatter, "cgroup-v2 {observation}"),
            Self::V1Limited(observation) => write!(formatter, "cgroup-v1 {observation}"),
            Self::ProbeFailed(failure) => write!(formatter, "cgroup probe failed: {failure}"),
        }
    }
}

#[cfg(not(target_os = "linux"))]
pub(crate) fn detect_cgroup_memory() -> CgroupMemoryObservation {
    CgroupMemoryObservation::NotPresent
}

#[cfg(target_os = "linux")]
mod linux {
    use super::*;
    use std::ffi::OsString;
    use std::fs;
    use std::io;
    use std::os::unix::ffi::OsStringExt;
    use std::path::{Component, Path, PathBuf};

    impl CgroupMemoryProbeFailure {
        fn new(
            kind: CgroupMemoryProbeFailureKind,
            path: impl Into<Box<str>>,
            detail: impl Into<Box<str>>,
        ) -> Self {
            Self {
                kind,
                path: path.into(),
                detail: detail.into(),
            }
        }
    }

    const PROC_SELF_CGROUP: &str = "/proc/self/cgroup";
    const PROC_SELF_MOUNTINFO: &str = "/proc/self/mountinfo";

    #[derive(Debug)]
    struct ControllerMount {
        root: PathBuf,
        mount_point: PathBuf,
    }

    #[derive(Debug)]
    struct MemoryMembership {
        unified: Option<PathBuf>,
        legacy: Option<PathBuf>,
    }

    pub(super) fn detect() -> CgroupMemoryObservation {
        detect_from_proc_files(Path::new(PROC_SELF_CGROUP), Path::new(PROC_SELF_MOUNTINFO))
            .unwrap_or_else(CgroupMemoryObservation::ProbeFailed)
    }

    fn detect_from_proc_files(
        cgroup_file: &Path,
        mountinfo_file: &Path,
    ) -> Result<CgroupMemoryObservation, CgroupMemoryProbeFailure> {
        let membership_text = read_required(cgroup_file)?;
        let membership = parse_memory_membership(&membership_text, cgroup_file)?;
        let mountinfo_text = read_required(mountinfo_file)?;
        if let Some(legacy) = membership.legacy {
            let mount = select_controller_mount(
                &mountinfo_text,
                &legacy,
                mountinfo_file,
                "cgroup",
                Some("memory"),
            )?;
            let leaf = resolve_membership_leaf(&legacy, &mount, mountinfo_file)?;
            return inspect_visible_v1_hierarchy(&leaf, &mount.mount_point);
        }
        if let Some(unified) = membership.unified {
            let mount = select_controller_mount(
                &mountinfo_text,
                &unified,
                mountinfo_file,
                "cgroup2",
                None,
            )?;
            let leaf = resolve_membership_leaf(&unified, &mount, mountinfo_file)?;
            return inspect_visible_v2_hierarchy(&leaf, &mount.mount_point);
        }
        Ok(CgroupMemoryObservation::NotPresent)
    }

    fn parse_memory_membership(
        text: &str,
        source: &Path,
    ) -> Result<MemoryMembership, CgroupMemoryProbeFailure> {
        let mut unified = None;
        let mut legacy = None;
        for line in text.lines().filter(|line| !line.is_empty()) {
            let mut fields = line.splitn(3, ':');
            let hierarchy = fields.next();
            let controllers = fields.next();
            let path = fields.next();
            let (Some(hierarchy), Some(controllers), Some(path)) = (hierarchy, controllers, path)
            else {
                return Err(failure(
                    CgroupMemoryProbeFailureKind::MalformedMembership,
                    source,
                    format!("invalid /proc/self/cgroup record {line:?}"),
                ));
            };
            if controllers.split(',').any(|name| name == "memory") {
                if legacy.is_some() {
                    return Err(failure(
                        CgroupMemoryProbeFailureKind::MalformedMembership,
                        source,
                        "multiple cgroup-v1 memory memberships",
                    ));
                }
                let path = PathBuf::from(path);
                validate_absolute_cgroup_path(&path, source)?;
                legacy = Some(path);
            }
            if hierarchy == "0" && controllers.is_empty() {
                if unified.is_some() {
                    return Err(failure(
                        CgroupMemoryProbeFailureKind::MalformedMembership,
                        source,
                        "multiple unified cgroup-v2 memberships",
                    ));
                }
                let path = PathBuf::from(path);
                validate_absolute_cgroup_path(&path, source)?;
                unified = Some(path);
            }
        }
        Ok(MemoryMembership { unified, legacy })
    }

    fn validate_absolute_cgroup_path(
        path: &Path,
        source: &Path,
    ) -> Result<(), CgroupMemoryProbeFailure> {
        if !path.is_absolute()
            || path
                .components()
                .any(|component| matches!(component, Component::ParentDir))
        {
            return Err(failure(
                CgroupMemoryProbeFailureKind::MalformedMembership,
                source,
                format!("invalid unified cgroup path {}", path.display()),
            ));
        }
        Ok(())
    }

    fn select_controller_mount(
        text: &str,
        membership: &Path,
        source: &Path,
        filesystem: &str,
        required_super_option: Option<&str>,
    ) -> Result<ControllerMount, CgroupMemoryProbeFailure> {
        let mut selected: Option<ControllerMount> = None;
        for line in text.lines().filter(|line| !line.is_empty()) {
            let Some((before_separator, after_separator)) = line.split_once(" - ") else {
                return Err(failure(
                    CgroupMemoryProbeFailureKind::MalformedMountInfo,
                    source,
                    format!("mountinfo record has no separator: {line:?}"),
                ));
            };
            let after_fields = after_separator.split_whitespace().collect::<Vec<_>>();
            if after_fields.first().copied() != Some(filesystem) {
                continue;
            }
            if let Some(required) = required_super_option
                && !after_fields
                    .get(2)
                    .is_some_and(|options| options.split(',').any(|option| option == required))
            {
                continue;
            }
            let before_fields = before_separator.split_whitespace().collect::<Vec<_>>();
            if before_fields.len() < 6 {
                return Err(failure(
                    CgroupMemoryProbeFailureKind::MalformedMountInfo,
                    source,
                    format!("short {filesystem} mountinfo record: {line:?}"),
                ));
            }
            let root = decode_mountinfo_path(before_fields[3], source)?;
            let mount_point = decode_mountinfo_path(before_fields[4], source)?;
            if !root.is_absolute() || !mount_point.is_absolute() {
                return Err(failure(
                    CgroupMemoryProbeFailureKind::MalformedMountInfo,
                    source,
                    format!(
                        "{filesystem} mount paths must be absolute (root={}, mount_point={})",
                        root.display(),
                        mount_point.display()
                    ),
                ));
            }
            if membership.strip_prefix(&root).is_err() {
                continue;
            }
            let candidate = ControllerMount { root, mount_point };
            let candidate_depth = candidate.root.components().count();
            let selected_depth = selected
                .as_ref()
                .map_or(0, |mount| mount.root.components().count());
            if selected.is_none() || candidate_depth > selected_depth {
                selected = Some(candidate);
            }
        }
        selected.ok_or_else(|| {
            failure(
                CgroupMemoryProbeFailureKind::MissingUnifiedMount,
                source,
                format!(
                    "no {filesystem} memory mount covers process membership {}",
                    membership.display()
                ),
            )
        })
    }

    fn resolve_membership_leaf(
        membership: &Path,
        mount: &ControllerMount,
        source: &Path,
    ) -> Result<PathBuf, CgroupMemoryProbeFailure> {
        let relative = membership.strip_prefix(&mount.root).map_err(|_| {
            failure(
                CgroupMemoryProbeFailureKind::MissingUnifiedMount,
                source,
                format!(
                    "cgroup path {} is outside selected mount root {}",
                    membership.display(),
                    mount.root.display()
                ),
            )
        })?;
        let relative = relative.strip_prefix(Path::new("/")).unwrap_or(relative);
        Ok(mount.mount_point.join(relative))
    }

    fn decode_mountinfo_path(
        raw: &str,
        source: &Path,
    ) -> Result<PathBuf, CgroupMemoryProbeFailure> {
        let bytes = raw.as_bytes();
        let mut decoded = Vec::with_capacity(bytes.len());
        let mut index = 0;
        while index < bytes.len() {
            if bytes[index] != b'\\' {
                decoded.push(bytes[index]);
                index += 1;
                continue;
            }
            let octal = bytes
                .get(index + 1..index + 4)
                .filter(|digits| digits.iter().all(|digit| (b'0'..=b'7').contains(digit)));
            let Some(octal) = octal else {
                return Err(failure(
                    CgroupMemoryProbeFailureKind::MalformedMountInfo,
                    source,
                    format!("invalid mountinfo path escape in {raw:?}"),
                ));
            };
            let value = u16::from(octal[0] - b'0') * 64
                + u16::from(octal[1] - b'0') * 8
                + u16::from(octal[2] - b'0');
            let value = u8::try_from(value).map_err(|_| {
                failure(
                    CgroupMemoryProbeFailureKind::MalformedMountInfo,
                    source,
                    format!("mountinfo path escape exceeds one byte in {raw:?}"),
                )
            })?;
            decoded.push(value);
            index += 4;
        }
        Ok(PathBuf::from(OsString::from_vec(decoded)))
    }

    fn inspect_visible_v2_hierarchy(
        leaf: &Path,
        mount_point: &Path,
    ) -> Result<CgroupMemoryObservation, CgroupMemoryProbeFailure> {
        if !leaf.starts_with(mount_point) {
            return Err(failure(
                CgroupMemoryProbeFailureKind::MissingUnifiedMount,
                leaf,
                format!("leaf is outside mount point {}", mount_point.display()),
            ));
        }
        let leaf_metadata = fs::metadata(leaf).map_err(|error| io_failure(leaf, error))?;
        if !leaf_metadata.is_dir() {
            return Err(failure(
                CgroupMemoryProbeFailureKind::MissingUnifiedMount,
                leaf,
                "resolved process cgroup is not a directory",
            ));
        }
        let mut directory = leaf.to_path_buf();
        let mut inspected_levels = 0usize;
        let mut binding: Option<CgroupMemoryAvailability> = None;
        loop {
            let max_path = directory.join("memory.max");
            let current_path = directory.join("memory.current");
            let stat_path = directory.join("memory.stat");
            match read_optional(&max_path)? {
                Some(raw_limit) => {
                    inspected_levels = inspected_levels.saturating_add(1);
                    match parse_limit(&raw_limit, &max_path)? {
                        CgroupMemoryLimit::Unlimited => {}
                        CgroupMemoryLimit::Finite(limit_bytes) => {
                            let current_before_raw =
                                read_optional(&current_path)?.ok_or_else(|| {
                                    failure(
                                        CgroupMemoryProbeFailureKind::MissingCounter,
                                        &current_path,
                                        "finite memory.max requires memory.current",
                                    )
                                })?;
                            let current_before = parse_counter(&current_before_raw, &current_path)?;
                            let stat_raw = read_optional(&stat_path)?.ok_or_else(|| {
                                failure(
                                    CgroupMemoryProbeFailureKind::MissingCounter,
                                    &stat_path,
                                    "finite memory.max requires memory.stat",
                                )
                            })?;
                            let inactive_file_bytes =
                                parse_stat_counter(&stat_raw, "inactive_file", &stat_path)?;
                            // `memory.current` and `memory.stat` are live files,
                            // not an atomic snapshot. Bracket the stat read and
                            // use the larger current value: this is conservative
                            // for admission and prevents an ordinary concurrent
                            // charge/uncharge from looking like malformed data.
                            let current_after_raw =
                                read_optional(&current_path)?.ok_or_else(|| {
                                    failure(
                                        CgroupMemoryProbeFailureKind::MissingCounter,
                                        &current_path,
                                        "memory.current disappeared during cgroup probe",
                                    )
                                })?;
                            let current_after = parse_counter(&current_after_raw, &current_path)?;
                            let current_bytes = current_before.max(current_after);
                            if inactive_file_bytes > current_bytes {
                                return Err(failure(
                                    CgroupMemoryProbeFailureKind::InconsistentCounters,
                                    &stat_path,
                                    format!(
                                        "inactive_file={inactive_file_bytes} exceeds memory.current={current_bytes}"
                                    ),
                                ));
                            }
                            let candidate = CgroupMemoryAvailability::from_consistent_counters(
                                directory.display().to_string().into_boxed_str(),
                                limit_bytes,
                                current_bytes,
                                inactive_file_bytes,
                                0,
                            )
                            .ok_or_else(|| {
                                failure(
                                    CgroupMemoryProbeFailureKind::InconsistentCounters,
                                    &stat_path,
                                    "memory counters became inconsistent during construction",
                                )
                            })?;
                            if binding.as_ref().map_or(true, |current| {
                                candidate.available_bytes() < current.available_bytes()
                            }) {
                                binding = Some(candidate);
                            }
                        }
                    }
                }
                None => {
                    let has_current = read_optional(&current_path)?.is_some();
                    let has_stat = read_optional(&stat_path)?.is_some();
                    // The cgroup-v2 root is exempt from resource control and
                    // therefore has accounting files but no `memory.max`.
                    // Every non-root level with controller accounting must
                    // expose its hard-limit file; otherwise the observation is
                    // incomplete and cannot safely inherit host capacity.
                    if directory != mount_point && (has_current || has_stat) {
                        return Err(failure(
                            CgroupMemoryProbeFailureKind::MissingCounter,
                            &max_path,
                            "memory controller files are present but memory.max is missing",
                        ));
                    }
                }
            }
            if directory == mount_point {
                break;
            }
            let Some(parent) = directory.parent() else {
                return Err(failure(
                    CgroupMemoryProbeFailureKind::MissingUnifiedMount,
                    &directory,
                    "cgroup hierarchy ended before its mount point",
                ));
            };
            directory = parent.to_path_buf();
        }
        if let Some(mut binding) = binding {
            binding.inspected_levels = inspected_levels;
            Ok(CgroupMemoryObservation::V2Limited(binding))
        } else {
            Ok(CgroupMemoryObservation::V2Unbounded {
                cgroup_path: leaf.display().to_string().into_boxed_str(),
                inspected_levels,
            })
        }
    }

    fn inspect_visible_v1_hierarchy(
        leaf: &Path,
        mount_point: &Path,
    ) -> Result<CgroupMemoryObservation, CgroupMemoryProbeFailure> {
        if !leaf.starts_with(mount_point) {
            return Err(failure(
                CgroupMemoryProbeFailureKind::MissingUnifiedMount,
                leaf,
                format!("leaf is outside mount point {}", mount_point.display()),
            ));
        }
        let leaf_metadata = fs::metadata(leaf).map_err(|error| io_failure(leaf, error))?;
        if !leaf_metadata.is_dir() {
            return Err(failure(
                CgroupMemoryProbeFailureKind::MissingUnifiedMount,
                leaf,
                "resolved process cgroup is not a directory",
            ));
        }

        let hierarchy_path = leaf.join("memory.use_hierarchy");
        let hierarchy_raw = read_required(&hierarchy_path)?;
        let hierarchical = match parse_counter(&hierarchy_raw, &hierarchy_path)? {
            0 => false,
            1 => true,
            value => {
                return Err(failure(
                    CgroupMemoryProbeFailureKind::InvalidCounter,
                    &hierarchy_path,
                    format!("memory.use_hierarchy must be 0 or 1, got {value}"),
                ));
            }
        };

        let mut directory = leaf.to_path_buf();
        let mut inspected_levels = 0usize;
        let mut binding: Option<CgroupMemoryAvailability> = None;
        loop {
            let limit_path = directory.join("memory.limit_in_bytes");
            let usage_path = directory.join("memory.usage_in_bytes");
            let stat_path = directory.join("memory.stat");
            let limit_bytes = parse_counter(&read_required(&limit_path)?, &limit_path)?;
            let usage_before = parse_counter(&read_required(&usage_path)?, &usage_path)?;
            let stat_raw = read_required(&stat_path)?;
            let inactive_key = if hierarchical {
                "total_inactive_file"
            } else {
                "inactive_file"
            };
            let inactive_file_bytes = parse_stat_counter(&stat_raw, inactive_key, &stat_path)?;
            let usage_after = parse_counter(&read_required(&usage_path)?, &usage_path)?;
            let current_bytes = usage_before.max(usage_after);
            if inactive_file_bytes > current_bytes {
                return Err(failure(
                    CgroupMemoryProbeFailureKind::InconsistentCounters,
                    &stat_path,
                    format!(
                        "{inactive_key}={inactive_file_bytes} exceeds memory.usage_in_bytes={current_bytes}"
                    ),
                ));
            }
            inspected_levels = inspected_levels.saturating_add(1);
            let candidate = CgroupMemoryAvailability::from_consistent_counters(
                directory.display().to_string().into_boxed_str(),
                limit_bytes,
                current_bytes,
                inactive_file_bytes,
                0,
            )
            .ok_or_else(|| {
                failure(
                    CgroupMemoryProbeFailureKind::InconsistentCounters,
                    &stat_path,
                    "memory counters became inconsistent during construction",
                )
            })?;
            if binding
                .as_ref()
                .is_none_or(|current| candidate.available_bytes() < current.available_bytes())
            {
                binding = Some(candidate);
            }

            if !hierarchical || directory == mount_point {
                break;
            }
            let Some(parent) = directory.parent() else {
                return Err(failure(
                    CgroupMemoryProbeFailureKind::MissingUnifiedMount,
                    &directory,
                    "cgroup hierarchy ended before its mount point",
                ));
            };
            directory = parent.to_path_buf();
        }

        let mut binding = binding.ok_or_else(|| {
            failure(
                CgroupMemoryProbeFailureKind::MissingCounter,
                leaf,
                "cgroup-v1 memory hierarchy exposed no accounting level",
            )
        })?;
        binding.inspected_levels = inspected_levels;
        Ok(CgroupMemoryObservation::V1Limited(binding))
    }

    fn parse_limit(
        raw: &str,
        source: &Path,
    ) -> Result<CgroupMemoryLimit, CgroupMemoryProbeFailure> {
        let value = raw.trim();
        if value == "max" {
            return Ok(CgroupMemoryLimit::Unlimited);
        }
        let bytes = value.parse::<u64>().map_err(|_| {
            failure(
                CgroupMemoryProbeFailureKind::InvalidLimit,
                source,
                format!("expected literal max or an unsigned byte count, got {value:?}"),
            )
        })?;
        Ok(CgroupMemoryLimit::Finite(bytes))
    }

    fn parse_counter(raw: &str, source: &Path) -> Result<u64, CgroupMemoryProbeFailure> {
        let value = raw.trim();
        value.parse::<u64>().map_err(|_| {
            failure(
                CgroupMemoryProbeFailureKind::InvalidCounter,
                source,
                format!("expected an unsigned byte count, got {value:?}"),
            )
        })
    }

    fn parse_stat_counter(
        raw: &str,
        key: &str,
        source: &Path,
    ) -> Result<u64, CgroupMemoryProbeFailure> {
        for line in raw.lines() {
            let mut fields = line.split_whitespace();
            let Some(name) = fields.next() else {
                continue;
            };
            if name != key {
                continue;
            }
            let Some(value) = fields.next() else {
                return Err(failure(
                    CgroupMemoryProbeFailureKind::InvalidCounter,
                    source,
                    format!("memory.stat key {key:?} has no value"),
                ));
            };
            if fields.next().is_some() {
                return Err(failure(
                    CgroupMemoryProbeFailureKind::InvalidCounter,
                    source,
                    format!("memory.stat key {key:?} has trailing fields"),
                ));
            }
            return value.parse::<u64>().map_err(|_| {
                failure(
                    CgroupMemoryProbeFailureKind::InvalidCounter,
                    source,
                    format!("memory.stat key {key:?} is not an unsigned byte count"),
                )
            });
        }
        Err(failure(
            CgroupMemoryProbeFailureKind::MissingCounter,
            source,
            format!("memory.stat is missing required key {key:?}"),
        ))
    }

    fn read_required(path: &Path) -> Result<String, CgroupMemoryProbeFailure> {
        fs::read_to_string(path).map_err(|error| io_failure(path, error))
    }

    fn read_optional(path: &Path) -> Result<Option<String>, CgroupMemoryProbeFailure> {
        match fs::read_to_string(path) {
            Ok(value) => Ok(Some(value)),
            Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(None),
            Err(error) => Err(io_failure(path, error)),
        }
    }

    fn io_failure(path: &Path, error: io::Error) -> CgroupMemoryProbeFailure {
        failure(CgroupMemoryProbeFailureKind::Io, path, error.to_string())
    }

    fn failure(
        kind: CgroupMemoryProbeFailureKind,
        path: &Path,
        detail: impl Into<Box<str>>,
    ) -> CgroupMemoryProbeFailure {
        CgroupMemoryProbeFailure::new(kind, path.display().to_string().into_boxed_str(), detail)
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use std::fs;
        use tempfile::TempDir;

        struct Fixture {
            _temp: TempDir,
            mount: PathBuf,
            cgroup_file: PathBuf,
            mountinfo_file: PathBuf,
        }

        impl Fixture {
            fn new(membership: &str) -> Self {
                let temp = TempDir::new().expect("fixture tempdir");
                let mount = temp.path().join("cgroup2");
                fs::create_dir_all(&mount).expect("fixture mount");
                let cgroup_file = temp.path().join("self.cgroup");
                fs::write(&cgroup_file, format!("0::{membership}\n")).expect("fixture membership");
                let mountinfo_file = temp.path().join("self.mountinfo");
                fs::write(
                    &mountinfo_file,
                    format!(
                        "29 23 0:26 / {} rw,nosuid,nodev,noexec,relatime - cgroup2 cgroup rw\n",
                        mount.display()
                    ),
                )
                .expect("fixture mountinfo");
                Self {
                    _temp: temp,
                    mount,
                    cgroup_file,
                    mountinfo_file,
                }
            }

            fn level(&self, relative: &str, limit: &str, current: u64, inactive: u64) {
                let directory = self.mount.join(relative.trim_start_matches('/'));
                fs::create_dir_all(&directory).expect("fixture level");
                fs::write(directory.join("memory.max"), format!("{limit}\n"))
                    .expect("fixture memory.max");
                fs::write(directory.join("memory.current"), format!("{current}\n"))
                    .expect("fixture memory.current");
                fs::write(
                    directory.join("memory.stat"),
                    format!("anon 1\ninactive_file {inactive}\nactive_file 2\n"),
                )
                .expect("fixture memory.stat");
            }

            fn observe(&self) -> CgroupMemoryObservation {
                detect_from_proc_files(&self.cgroup_file, &self.mountinfo_file)
                    .unwrap_or_else(CgroupMemoryObservation::ProbeFailed)
            }
        }

        #[test]
        fn literal_max_is_typed_unbounded_even_when_current_is_large() {
            let fixture = Fixture::new("/tenant/leaf");
            fixture.level("tenant/leaf", "max", u64::MAX - 1, 0);
            assert!(matches!(
                fixture.observe(),
                CgroupMemoryObservation::V2Unbounded {
                    inspected_levels: 1,
                    ..
                }
            ));
        }

        #[test]
        fn cgroup_v2_root_accounting_without_memory_max_is_unbounded() {
            let fixture = Fixture::new("/");
            fs::write(fixture.mount.join("memory.current"), "1000\n").expect("root current");
            fs::write(fixture.mount.join("memory.stat"), "inactive_file 700\n").expect("root stat");
            assert!(matches!(
                fixture.observe(),
                CgroupMemoryObservation::V2Unbounded {
                    inspected_levels: 0,
                    ..
                }
            ));
        }

        #[test]
        fn finite_zero_remains_authoritative() {
            let fixture = Fixture::new("/tenant/leaf");
            fixture.level("tenant/leaf", "0", 0, 0);
            let CgroupMemoryObservation::V2Limited(observation) = fixture.observe() else {
                panic!("finite zero must be a real cgroup ceiling");
            };
            assert_eq!(observation.limit_bytes(), 0);
            assert_eq!(observation.available_bytes(), 0);
        }

        #[test]
        fn malformed_active_controller_fails_closed() {
            let fixture = Fixture::new("/tenant/leaf");
            fixture.level("tenant/leaf", "1000", 500, 100);
            fs::write(
                fixture.mount.join("tenant/leaf/memory.current"),
                "not-a-counter\n",
            )
            .expect("corrupt current");
            let CgroupMemoryObservation::ProbeFailed(failure) = fixture.observe() else {
                panic!("malformed active controller must fail closed");
            };
            assert_eq!(failure.kind(), CgroupMemoryProbeFailureKind::InvalidCounter);
        }

        #[test]
        fn inconsistent_cache_counter_fails_closed() {
            let fixture = Fixture::new("/tenant/leaf");
            fixture.level("tenant/leaf", "1000", 100, 101);
            let CgroupMemoryObservation::ProbeFailed(failure) = fixture.observe() else {
                panic!("inconsistent controller counters must fail closed");
            };
            assert_eq!(
                failure.kind(),
                CgroupMemoryProbeFailureKind::InconsistentCounters
            );
        }

        #[test]
        fn hybrid_hierarchy_uses_the_active_v1_memory_controller() {
            let fixture = Fixture::new("/tenant/leaf");
            let v1_mount = fixture.mount.parent().unwrap().join("cgroup-memory");
            let v1_leaf = v1_mount.join("legacy");
            fs::create_dir_all(&v1_leaf).expect("v1 leaf");
            fs::write(v1_leaf.join("memory.use_hierarchy"), "0\n").expect("hierarchy");
            fs::write(v1_leaf.join("memory.limit_in_bytes"), "2048\n").expect("limit");
            fs::write(v1_leaf.join("memory.usage_in_bytes"), "1024\n").expect("usage");
            fs::write(
                v1_leaf.join("memory.stat"),
                "inactive_file 256\ntotal_inactive_file 256\n",
            )
            .expect("stat");
            fs::write(&fixture.cgroup_file, "0::/tenant/leaf\n4:memory:/legacy\n")
                .expect("hybrid membership");
            let original = fs::read_to_string(&fixture.mountinfo_file).expect("mountinfo");
            fs::write(
                &fixture.mountinfo_file,
                format!(
                    "{original}31 23 0:30 / {} rw - cgroup cgroup rw,memory\n",
                    v1_mount.display()
                ),
            )
            .expect("hybrid mountinfo");
            let CgroupMemoryObservation::V1Limited(observation) = fixture.observe() else {
                panic!("hybrid memory accounting must follow v1");
            };
            assert_eq!(observation.available_bytes(), 1280);
        }
    }
}

#[cfg(target_os = "linux")]
pub(crate) fn detect_cgroup_memory() -> CgroupMemoryObservation {
    linux::detect()
}

#[cfg(all(test, not(target_os = "linux")))]
mod non_linux_tests {
    use super::*;

    #[test]
    fn non_linux_platform_has_no_cgroup_controller() {
        assert_eq!(detect_cgroup_memory(), CgroupMemoryObservation::NotPresent);
    }
}