damon 0.1.1

A safe, typed Rust interface to Linux DAMON
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
//! Multi-kdamond ownership and lifecycle management.

use super::{
    Capabilities, ConfigurationSnapshot, DamonAdmin, DamonConfig, Error, HierarchyReadBatch,
    HierarchyRuntimeBatch, Kdamond, KdamondCommand, KdamondState, ManagedKdamond,
    MonitoringIntervals, ObservedConfiguration, PathBuf, Pid, QuotaGoalConfig, Result, SessionLock,
    StagedConfiguration, StagedOwnership, collect_writes, ensure_hierarchy_stopped,
    restore_configuration, retry_busy, running_thread_pid, stage_and_verify_configuration,
    with_rollback,
};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum KdamondSessionState {
    Staged,
    Running(Pid),
    UnidentifiedRunning,
}

pub(super) struct PersistentParts {
    pub(super) configuration: ConfigurationSnapshot,
    pub(super) volatile_paths: Box<[PathBuf]>,
    pub(super) kdamond_count: usize,
    pub(super) identities: Box<[(usize, Pid)]>,
}

/// A cooperatively exclusive, transactionally staged DAMON hierarchy.
///
/// The hierarchy holds the advisory session lock for its lifetime and tracks
/// each running kdamond by its kernel-thread ID. It restores the complete
/// stopped hierarchy that preceded the session when [`Self::close`] succeeds,
/// or on a best-effort basis when dropped.
///
/// Controllers that ignore the advisory lock can still race this API because
/// DAMON sysfs provides no ownership primitive.
#[derive(Debug)]
pub struct ManagedHierarchy {
    pub(super) admin: DamonAdmin,
    previous: Option<ConfigurationSnapshot>,
    pub(super) staged: StagedOwnership,
    states: Box<[KdamondSessionState]>,
    _session_lock: SessionLock,
    owns_hierarchy: bool,
}

impl ManagedHierarchy {
    pub(super) fn new(
        admin: DamonAdmin,
        staged_configuration: StagedConfiguration,
        config: &DamonConfig,
        session_lock: SessionLock,
    ) -> Result<Self> {
        let staged = StagedOwnership::new(staged_configuration.current, &admin, config);
        if let Err(operation) = staged.verify(&admin) {
            return Err(with_rollback(
                operation,
                restore_configuration(&admin, &staged_configuration.previous),
            ));
        }
        Ok(Self {
            admin,
            previous: Some(staged_configuration.previous),
            staged,
            states: vec![KdamondSessionState::Staged; config.kdamonds.len()].into_boxed_slice(),
            _session_lock: session_lock,
            owns_hierarchy: true,
        })
    }

    /// Returns the number of managed kdamonds.
    #[must_use]
    pub const fn kdamond_count(&self) -> usize {
        self.states.len()
    }

    /// Borrows an ownership-safe runtime view of one running kdamond.
    ///
    /// Constructing the view performs no sysfs access. Each operation verifies
    /// the hierarchy and selected kernel-thread identity at its trust
    /// boundaries.
    pub fn runtime(&mut self, kdamond_index: usize) -> Result<ManagedKdamond<'_>> {
        self.expected_running_pid(kdamond_index)?;
        Ok(ManagedKdamond {
            hierarchy: self,
            index: kdamond_index,
        })
    }

    /// Runs operations across managed kdamonds under one pair of complete
    /// hierarchy ownership checks.
    ///
    /// Individual operations still verify the selected kernel-thread identity
    /// at their trust boundaries. If both the closure and final ownership check
    /// fail, the ownership error is returned because the closure's outputs
    /// cannot be trusted.
    pub fn runtime_batch<T>(
        &mut self,
        operation: impl FnOnce(&mut HierarchyRuntimeBatch<'_>) -> Result<T>,
    ) -> Result<T> {
        self.verify_owned_state()?;
        let result = {
            let mut batch = HierarchyRuntimeBatch { hierarchy: self };
            operation(&mut batch)
        };
        self.verify_owned_state()?;
        result
    }

    /// Runs cached reads across kdamonds under one pair of ownership checks.
    ///
    /// Unlike [`Self::runtime_batch`], this requires only shared access and
    /// exposes no kernel commands or configuration mutations.
    pub fn read_batch<T>(
        &self,
        operation: impl FnOnce(&mut HierarchyReadBatch<'_>) -> Result<T>,
    ) -> Result<T> {
        self.verify_owned_state()?;
        let result = {
            let mut batch = HierarchyReadBatch { hierarchy: self };
            operation(&mut batch)
        };
        self.verify_owned_state()?;
        result
    }

    /// Discovers capabilities for one staged scheme.
    ///
    /// This is available before or after starting the hierarchy. The complete
    /// owned state is verified before and after capability discovery.
    pub fn capabilities(
        &self,
        kdamond_index: usize,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<Capabilities> {
        self.validate_kdamond_index(kdamond_index)?;
        self.verify_owned_state()?;
        let capabilities = retry_busy(|| {
            self.kdamond(kdamond_index)
                .capabilities(context_index, scheme_index)
        })?;
        self.verify_owned_state()?;
        Ok(capabilities)
    }

    /// Starts every staged kdamond in index order.
    ///
    /// Each kernel-thread ID is captured immediately after its start command.
    /// If a later start fails, already identified kdamonds are stopped in
    /// reverse order. A kdamond whose start succeeded but whose identity could
    /// not be captured is never stopped without proof of ownership.
    pub fn start_all(&mut self) -> Result<()> {
        if !self.owns_hierarchy {
            return Err(Error::NotRunning);
        }
        for (index, state) in self.states.iter().enumerate() {
            match state {
                KdamondSessionState::Staged => {}
                KdamondSessionState::Running(_) => {
                    return Err(Error::KdamondRunning { index });
                }
                KdamondSessionState::UnidentifiedRunning => {
                    return Err(Error::OwnershipLost {
                        reason: "the kdamond started but its identity was not captured",
                    });
                }
            }
        }

        self.staged.verify(&self.admin)?;
        for index in 0..self.states.len() {
            match retry_busy(|| self.kdamond(index).state())? {
                KdamondState::Off => {}
                KdamondState::On => {
                    return Err(Error::OwnershipLost {
                        reason: "the staged kdamond was started by another controller",
                    });
                }
                KdamondState::Unknown(state) => {
                    return Err(Error::UnexpectedKdamondState { state });
                }
            }
        }

        for index in 0..self.states.len() {
            let ownership = self
                .staged
                .verify_count(&self.admin)
                .and_then(|()| self.staged.verify_kdamond_configuration(&self.admin, index));
            if let Err(operation) = ownership {
                return Err(with_rollback(operation, self.rollback_started()));
            }
            if let Err(operation) = self.start_one(index) {
                return Err(with_rollback(operation, self.rollback_started()));
            }
        }
        if let Err(operation) = self.verify_owned_state() {
            return Err(with_rollback(operation, self.rollback_started()));
        }
        Ok(())
    }

    /// Stops every managed kdamond whose configuration and thread identity are
    /// still owned.
    ///
    /// Ownership loss for one kdamond does not prevent stopping other owned
    /// kdamonds. The first ownership or stop error is returned after all safe
    /// stops have been attempted.
    pub fn stop_all(&mut self) -> Result<()> {
        if !self.owns_hierarchy {
            return Ok(());
        }
        self.staged.verify_count(&self.admin)?;
        let mut first_error = None;
        for index in (0..self.states.len()).rev() {
            if let Err(error) = self.staged.verify_count(&self.admin) {
                if first_error.is_none() {
                    first_error = Some(error);
                }
                break;
            }
            if let Err(error) = self.staged.verify_kdamond_configuration(&self.admin, index) {
                if first_error.is_none() {
                    first_error = Some(error);
                }
                continue;
            }
            if let Err(error) = self.stop_one(index) {
                if first_error.is_none() {
                    first_error = Some(error);
                }
            }
        }
        if first_error.is_none() {
            first_error = self.staged.verify(&self.admin).err();
        }
        match first_error {
            Some(error) => Err(error),
            None => Ok(()),
        }
    }

    /// Returns whether one identified managed kdamond is still running.
    pub fn is_running(&self, kdamond_index: usize) -> Result<bool> {
        self.validate_kdamond_index(kdamond_index)?;
        match self.states[kdamond_index] {
            KdamondSessionState::Staged => {
                self.staged.verify(&self.admin)?;
                match retry_busy(|| self.kdamond(kdamond_index).state())? {
                    KdamondState::Off => Ok(false),
                    KdamondState::On => Err(Error::OwnershipLost {
                        reason: "the staged kdamond was started by another controller",
                    }),
                    KdamondState::Unknown(state) => Err(Error::UnexpectedKdamondState { state }),
                }
            }
            KdamondSessionState::Running(_) => match self.verify_running(kdamond_index) {
                Ok(()) => Ok(true),
                Err(Error::NotRunning) => Ok(false),
                Err(error) => Err(error),
            },
            KdamondSessionState::UnidentifiedRunning => Err(Error::OwnershipLost {
                reason: "the kdamond started but its identity was not captured",
            }),
        }
    }

    /// Reads the complete staged configuration after verifying ownership.
    pub fn configuration(&self) -> Result<DamonConfig> {
        self.verify_owned_state()?;
        let configuration = retry_busy(|| self.admin.configuration())?;
        self.verify_owned_state()?;
        Ok(configuration)
    }

    /// Reads the known typed hierarchy and all writable values after verifying ownership.
    pub fn observed_configuration(&self) -> Result<ObservedConfiguration> {
        self.verify_owned_state()?;
        let observation = retry_busy(|| self.admin.observed_configuration())?;
        self.verify_owned_state()?;
        Ok(observation)
    }

    /// Transactionally updates selected running kdamonds.
    ///
    /// `config` describes the complete staged hierarchy. Unselected kdamonds
    /// must match their currently staged configurations. Selected indexes are
    /// nonempty, distinct, and committed in the supplied order after one
    /// hierarchy verification. Kernel-tuned interval leaves of unselected
    /// kdamonds are preserved. On failure, the preceding staged configuration
    /// is restored and committed to every selected kdamond.
    pub fn update_configuration(
        &mut self,
        config: &DamonConfig,
        kdamond_indices: &[usize],
    ) -> Result<()> {
        config.validate_running_update()?;
        let selected =
            validate_selected_indices(kdamond_indices, self.states.len(), config.kdamonds.len())?;
        if selected.is_empty() {
            return Err(Error::InvalidConfiguration {
                field: "selected kdamond indexes",
                reason: "must contain at least one index",
            });
        }
        for &index in &selected {
            self.expected_running_pid(index)?;
        }

        self.verify_owned_state()?;
        let previous_snapshot = retry_busy(|| self.admin.configuration_snapshot())?;
        let previous = retry_busy(|| self.admin.configuration())?;
        if !retry_busy(|| previous_snapshot.matches_current_except(&self.staged.volatile_paths))? {
            return Err(Error::OwnershipLost {
                reason: "the running DAMON hierarchy changed while its rollback state was captured",
            });
        }
        self.verify_owned_state()?;
        let mut effective = config.clone();
        let mut comparable_previous = previous.clone();
        normalize_running_tuned_intervals(config, Some(&previous), &mut comparable_previous)?;
        for index in 0..self.states.len() {
            if !selected.contains(&index) {
                if !kdamond_configurations_equivalent(
                    &config.kdamonds[index],
                    &comparable_previous.kdamonds[index],
                ) {
                    return Err(Error::InvalidConfiguration {
                        field: "unselected kdamond configuration",
                        reason: "must match the currently staged configuration",
                    });
                }
                effective.kdamonds[index] = previous.kdamonds[index].clone();
            }
        }
        validate_obsolete_target_updates(&previous, &effective)?;

        let (operation, writes) = collect_writes(|| {
            self.stage_and_commit_running(&effective, Some(&previous), &selected)
        });
        match operation {
            Ok(staged) => {
                self.staged = staged;
                Ok(())
            }
            Err(operation) => match self.restore_and_commit_running(
                &previous_snapshot,
                &previous,
                &selected,
                &writes,
            ) {
                Ok(staged) => {
                    self.staged = staged;
                    Err(operation)
                }
                Err(rollback) => Err(Error::Rollback {
                    operation: Box::new(operation),
                    rollback: Box::new(rollback),
                }),
            },
        }
    }

    pub(super) fn update_scheme_quota_goals(
        &mut self,
        kdamond_index: usize,
        context_index: usize,
        scheme_index: usize,
        goals: &[QuotaGoalConfig],
    ) -> Result<()> {
        self.validate_kdamond_index(kdamond_index)?;
        crate::sysfs::SchemeQuotas::validate_goals(goals)?;
        self.expected_running_pid(kdamond_index)?;

        self.verify_owned_state()?;
        let previous_snapshot = retry_busy(|| self.admin.configuration_snapshot())?;
        let quotas = self
            .scheme(kdamond_index, context_index, scheme_index)?
            .quotas();
        let mut quota = retry_busy(|| quotas.configuration())?;
        if !retry_busy(|| previous_snapshot.matches_current_except(&self.staged.volatile_paths))? {
            return Err(Error::OwnershipLost {
                reason: "the running DAMON hierarchy changed while its rollback state was captured",
            });
        }
        self.verify_owned_state()?;
        let previous_goals = std::mem::replace(&mut quota.goals, goals.to_vec());
        quota.validate()?;
        if quota.goals == previous_goals {
            return Ok(());
        }

        let (operation, writes) = collect_writes(|| {
            self.stage_and_commit_quota_goals(
                &quotas,
                &quota.goals,
                Some(&previous_goals),
                kdamond_index,
            )
        });
        match operation {
            Ok(staged) => {
                self.staged = staged;
                Ok(())
            }
            Err(operation) => match self.restore_and_commit_quota_goals(
                &previous_snapshot,
                kdamond_index,
                &writes,
            ) {
                Ok(staged) => {
                    self.staged = staged;
                    Err(operation)
                }
                Err(rollback) => Err(Error::Rollback {
                    operation: Box::new(operation),
                    rollback: Box::new(rollback),
                }),
            },
        }
    }

    /// Stops owned kdamonds and restores the preceding stopped hierarchy.
    ///
    /// Unlike [`Drop`], this method reports stop and restoration failures.
    pub fn close(mut self) -> Result<()> {
        self.close_inner()
    }

    pub(super) fn kdamond(&self, index: usize) -> Kdamond {
        self.admin.kdamond(index)
    }

    pub(super) fn attach_persistent(
        admin: DamonAdmin,
        staged: StagedOwnership,
        identities: &[(usize, Pid)],
        session_lock: SessionLock,
    ) -> Result<Self> {
        staged.verify_complete(&admin)?;
        let mut states = vec![KdamondSessionState::Staged; staged.kdamond_count()];
        let mut previous_index = None;
        for &(index, pid) in identities {
            if index >= states.len() {
                return Err(Error::InvalidReceipt {
                    reason: "a running kdamond identity is outside the hierarchy",
                });
            }
            if previous_index.is_some_and(|previous| previous >= index) {
                return Err(Error::InvalidReceipt {
                    reason: "running kdamond identities are not distinct and ordered",
                });
            }
            states[index] = KdamondSessionState::Running(pid);
            previous_index = Some(index);
        }
        let managed = Self {
            admin,
            previous: None,
            staged,
            states: states.into_boxed_slice(),
            _session_lock: session_lock,
            owns_hierarchy: true,
        };
        managed.verify_owned_state()?;
        Ok(managed)
    }

    pub(super) fn persistent_parts(&self) -> Result<PersistentParts> {
        self.verify_owned_state()?;
        self.staged.verify_complete(&self.admin)?;
        let identities = self
            .states
            .iter()
            .enumerate()
            .filter_map(|(index, state)| match state {
                KdamondSessionState::Running(pid) => Some(Ok((index, *pid))),
                KdamondSessionState::Staged => None,
                KdamondSessionState::UnidentifiedRunning => Some(Err(Error::OwnershipLost {
                    reason: "the kdamond started but its identity was not captured",
                })),
            })
            .collect::<Result<Vec<_>>>()?
            .into_boxed_slice();
        Ok(PersistentParts {
            configuration: self.staged.configuration.clone(),
            volatile_paths: self.staged.volatile_paths.clone(),
            kdamond_count: self.states.len(),
            identities,
        })
    }

    pub(super) fn disarm_cleanup(&mut self) {
        self.previous = None;
        self.owns_hierarchy = false;
    }

    pub(super) fn scheme(
        &self,
        kdamond_index: usize,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<crate::sysfs::Scheme> {
        self.validate_kdamond_index(kdamond_index)?;
        let kdamond = self.kdamond(kdamond_index);
        let context_count = kdamond.context_count()?;
        if context_index >= context_count {
            return Err(Error::IndexOutOfBounds {
                kind: "context",
                index: context_index,
                count: context_count,
            });
        }
        let context = kdamond.context(context_index);
        let scheme_count = context.scheme_count()?;
        if scheme_index >= scheme_count {
            return Err(Error::IndexOutOfBounds {
                kind: "scheme",
                index: scheme_index,
                count: scheme_count,
            });
        }
        Ok(context.scheme(scheme_index))
    }

    pub(super) fn replace_staged_configuration(&mut self, config: &DamonConfig) -> Result<()> {
        config.validate_runnable()?;
        if config.kdamonds.len() != self.states.len() {
            return Err(Error::InvalidConfiguration {
                field: "managed hierarchy kdamond count",
                reason: "cannot change while the hierarchy is owned",
            });
        }
        if let Some(index) = self
            .states
            .iter()
            .position(|state| !matches!(state, KdamondSessionState::Staged))
        {
            return Err(Error::KdamondRunning { index });
        }
        self.staged.verify(&self.admin)?;
        retry_busy(|| ensure_hierarchy_stopped(&self.admin))?;
        let previous = retry_busy(|| self.admin.configuration())?;
        self.staged.verify(&self.admin)?;

        match stage_and_verify_configuration(&self.admin, config, Some(&previous)) {
            Ok(current) => {
                self.staged = StagedOwnership::new(current, &self.admin, config);
                Ok(())
            }
            Err(operation) => match stage_and_verify_configuration(&self.admin, &previous, None) {
                Ok(current) => {
                    self.staged = StagedOwnership::new(current, &self.admin, &previous);
                    Err(operation)
                }
                Err(rollback) => Err(Error::Rollback {
                    operation: Box::new(operation),
                    rollback: Box::new(rollback),
                }),
            },
        }
    }

    pub(super) fn command_after_ownership_check(
        &self,
        kdamond_index: usize,
        command: &KdamondCommand,
    ) -> Result<()> {
        self.verify_running(kdamond_index)?;
        retry_busy(|| self.kdamond(kdamond_index).command(command))
    }

    pub(super) fn verify_owned_state(&self) -> Result<()> {
        if !self.owns_hierarchy {
            return Err(Error::NotRunning);
        }
        self.staged.verify(&self.admin)?;
        for index in 0..self.states.len() {
            match self.states[index] {
                KdamondSessionState::Staged => match retry_busy(|| self.kdamond(index).state())? {
                    KdamondState::Off => {}
                    KdamondState::On => {
                        return Err(Error::OwnershipLost {
                            reason: "the staged kdamond was started by another controller",
                        });
                    }
                    KdamondState::Unknown(state) => {
                        return Err(Error::UnexpectedKdamondState { state });
                    }
                },
                KdamondSessionState::Running(_) => {
                    self.verify_running_state_and_identity_without_count(index)?;
                }
                KdamondSessionState::UnidentifiedRunning => {
                    return Err(Error::OwnershipLost {
                        reason: "the kdamond started but its identity was not captured",
                    });
                }
            }
        }
        Ok(())
    }

    pub(super) fn verify_running(&self, kdamond_index: usize) -> Result<()> {
        self.validate_kdamond_index(kdamond_index)?;
        self.staged.verify(&self.admin)?;
        self.verify_running_state_and_identity_without_count(kdamond_index)
    }

    pub(super) fn verify_running_identity(&self, kdamond_index: usize) -> Result<()> {
        self.staged.verify(&self.admin)?;
        self.verify_running_identity_only(kdamond_index)
    }

    pub(super) fn verify_running_identity_only(&self, kdamond_index: usize) -> Result<()> {
        self.validate_kdamond_index(kdamond_index)?;
        let kdamond = self.kdamond(kdamond_index);
        self.verify_running_identity_with(kdamond_index, &kdamond)
    }

    pub(super) fn verify_running_identity_with(
        &self,
        kdamond_index: usize,
        kdamond: &Kdamond,
    ) -> Result<()> {
        if self.admin.kdamond_count()? != self.states.len() {
            return Err(Error::OwnershipLost {
                reason: "the staged kdamond count changed",
            });
        }
        let expected = self.expected_running_pid(kdamond_index)?;
        let current = retry_busy(|| kdamond.pid())?.ok_or(Error::NotRunning)?;
        if current != expected {
            return Err(Error::OwnershipLost {
                reason: "the kdamond kernel-thread ID changed",
            });
        }
        Ok(())
    }

    fn verify_running_identity_without_count(&self, kdamond_index: usize) -> Result<()> {
        let expected = self.expected_running_pid(kdamond_index)?;
        let current = retry_busy(|| self.kdamond(kdamond_index).pid())?.ok_or(Error::NotRunning)?;
        if current != expected {
            return Err(Error::OwnershipLost {
                reason: "the kdamond kernel-thread ID changed",
            });
        }
        Ok(())
    }

    fn verify_running_state_and_identity_without_count(&self, kdamond_index: usize) -> Result<()> {
        let expected = self.expected_running_pid(kdamond_index)?;
        let current = running_thread_pid(&self.kdamond(kdamond_index))?;
        if current != expected {
            return Err(Error::OwnershipLost {
                reason: "the kdamond kernel-thread ID changed",
            });
        }
        Ok(())
    }

    fn start_one(&mut self, index: usize) -> Result<()> {
        let kdamond = self.kdamond(index);
        retry_busy(|| kdamond.command(&KdamondCommand::On))?;
        self.states[index] = KdamondSessionState::UnidentifiedRunning;
        let pid = running_thread_pid(&kdamond)?;
        self.states[index] = KdamondSessionState::Running(pid);
        self.verify_running_identity_only(index)?;
        self.staged.verify_kdamond_configuration(&self.admin, index)
    }

    fn stop_one(&mut self, index: usize) -> Result<()> {
        let kdamond = self.kdamond(index);
        match self.states[index] {
            KdamondSessionState::Staged => match retry_busy(|| kdamond.state())? {
                KdamondState::Off => Ok(()),
                KdamondState::On => Err(Error::OwnershipLost {
                    reason: "the staged kdamond was started by another controller",
                }),
                KdamondState::Unknown(state) => Err(Error::UnexpectedKdamondState { state }),
            },
            KdamondSessionState::Running(expected) => match retry_busy(|| kdamond.state())? {
                KdamondState::Off => {
                    self.states[index] = KdamondSessionState::Staged;
                    Ok(())
                }
                KdamondState::On => {
                    let current = retry_busy(|| kdamond.pid())?.ok_or(Error::NotRunning)?;
                    if current != expected {
                        return Err(Error::OwnershipLost {
                            reason: "the kdamond kernel-thread ID changed",
                        });
                    }
                    retry_busy(|| kdamond.command(&KdamondCommand::Off))?;
                    match retry_busy(|| kdamond.state())? {
                        KdamondState::Off => {
                            self.states[index] = KdamondSessionState::Staged;
                            Ok(())
                        }
                        KdamondState::On => Err(Error::OwnershipLost {
                            reason: "the kdamond restarted while it was being stopped",
                        }),
                        KdamondState::Unknown(state) => {
                            Err(Error::UnexpectedKdamondState { state })
                        }
                    }
                }
                KdamondState::Unknown(state) => Err(Error::UnexpectedKdamondState { state }),
            },
            KdamondSessionState::UnidentifiedRunning => match retry_busy(|| kdamond.state())? {
                KdamondState::Off => {
                    self.states[index] = KdamondSessionState::Staged;
                    Ok(())
                }
                KdamondState::On => Err(Error::OwnershipLost {
                    reason: "cannot safely stop a kdamond before its kernel-thread ID was captured",
                }),
                KdamondState::Unknown(state) => Err(Error::UnexpectedKdamondState { state }),
            },
        }
    }

    fn rollback_started(&mut self) -> Result<()> {
        if self
            .states
            .iter()
            .all(|state| matches!(state, KdamondSessionState::Staged))
        {
            return Ok(());
        }
        self.staged.verify_count(&self.admin)?;
        let mut first_error = None;
        for index in (0..self.states.len()).rev() {
            if matches!(self.states[index], KdamondSessionState::Staged) {
                continue;
            }
            if let Err(error) = self.staged.verify_kdamond_configuration(&self.admin, index) {
                if first_error.is_none() {
                    first_error = Some(error);
                }
                continue;
            }
            if let Err(error) = self.stop_one(index) {
                if first_error.is_none() {
                    first_error = Some(error);
                }
            }
        }
        match first_error {
            Some(error) => Err(error),
            None => Ok(()),
        }
    }

    fn stage_and_commit_running(
        &self,
        config: &DamonConfig,
        observed: Option<&DamonConfig>,
        selected: &[usize],
    ) -> Result<StagedOwnership> {
        self.verify_owned_identities_only()?;
        retry_busy(|| {
            self.verify_owned_identities_only()?;
            self.admin
                .stage_validated_configuration_from(config, observed)
        })?;
        self.verify_owned_identities_only()?;
        let snapshot = retry_busy(|| self.admin.configuration_snapshot())?;
        let mut staged_config = retry_busy(|| self.admin.configuration())?;
        normalize_running_tuned_intervals(config, observed, &mut staged_config)?;
        if let Some(error) = config.mismatch_error(&staged_config) {
            return Err(error);
        }
        if !retry_busy(|| snapshot.values_match_current())? {
            return Err(Error::OwnershipLost {
                reason: "the running DAMON hierarchy changed during staging",
            });
        }
        self.verify_owned_identities_only()?;
        for &index in selected {
            self.verify_running_identity_only(index)?;
            retry_busy(|| self.kdamond(index).command(&KdamondCommand::Commit))?;
            self.verify_running_identity_only(index)?;
        }

        if contains_obsolete_targets(config, selected) {
            let cleaned = without_obsolete_targets(config, selected);
            retry_busy(|| {
                self.verify_owned_identities_only()?;
                self.admin
                    .stage_validated_configuration_from(&cleaned, Some(config))
            })?;
            self.verify_owned_identities_only()?;
            let cleaned_snapshot = retry_busy(|| self.admin.configuration_snapshot())?;
            let mut cleaned_staged_config = retry_busy(|| self.admin.configuration())?;
            normalize_running_tuned_intervals(&cleaned, Some(config), &mut cleaned_staged_config)?;
            if let Some(error) = cleaned.mismatch_error(&cleaned_staged_config) {
                return Err(error);
            }
            if !retry_busy(|| cleaned_snapshot.values_match_current())? {
                return Err(Error::OwnershipLost {
                    reason: "the running DAMON hierarchy changed during obsolete-target cleanup",
                });
            }
            self.verify_owned_identities_only()?;
            let staged = StagedOwnership::new(cleaned_snapshot, &self.admin, &cleaned);
            staged.verify(&self.admin)?;
            return Ok(staged);
        }

        let staged = StagedOwnership::new(snapshot, &self.admin, config);
        staged.verify(&self.admin)?;
        Ok(staged)
    }

    fn restore_and_commit_running(
        &self,
        snapshot: &ConfigurationSnapshot,
        config: &DamonConfig,
        selected: &[usize],
        writes: &[PathBuf],
    ) -> Result<StagedOwnership> {
        self.verify_owned_identities_only()?;
        if let Err(boundary) = self.verify_rollback_boundary(snapshot, writes) {
            return Err(with_rollback(
                boundary,
                self.restore_written_and_commit_running(snapshot, writes, selected),
            ));
        }
        let affected = snapshot.paths_affected_by_writes(writes);
        retry_busy(|| snapshot.restore_paths_except(&affected, &self.staged.volatile_paths))?;
        self.verify_owned_identities_only()?;
        if !retry_busy(|| snapshot.matches_current_except(&self.staged.volatile_paths))? {
            return Err(Error::OwnershipLost {
                reason: "the running DAMON hierarchy changed during rollback",
            });
        }
        for &index in selected {
            self.verify_running_identity_only(index)?;
            retry_busy(|| self.kdamond(index).command(&KdamondCommand::Commit))?;
            self.verify_running_identity_only(index)?;
        }
        let current = retry_busy(|| self.admin.configuration_snapshot())?;
        if !retry_busy(|| snapshot.matches_current_except(&self.staged.volatile_paths))? {
            return Err(Error::OwnershipLost {
                reason: "the running DAMON hierarchy changed after rollback commit",
            });
        }
        let staged = StagedOwnership::new(current, &self.admin, config);
        staged.verify(&self.admin)?;
        Ok(staged)
    }

    fn stage_and_commit_quota_goals(
        &self,
        quotas: &crate::sysfs::SchemeQuotas,
        goals: &[QuotaGoalConfig],
        observed: Option<&[QuotaGoalConfig]>,
        kdamond_index: usize,
    ) -> Result<StagedOwnership> {
        self.verify_owned_identities_only()?;
        retry_busy(|| {
            self.verify_owned_identities_only()?;
            quotas.stage_goals_from(goals, observed)
        })?;
        self.verify_owned_identities_only()?;
        let goals_root = quotas.path().join("goals");
        let unchanged_matches = if observed.is_some_and(|values| values.len() == goals.len()) {
            let mut ignored = self.staged.volatile_paths.to_vec();
            for (index, goal) in goals.iter().enumerate() {
                if observed.is_some_and(|values| &values[index] == goal) {
                    continue;
                }
                let goal_path = quotas.goal(index).path().to_path_buf();
                ignored.push(goal_path.join("target_metric"));
                ignored.push(goal_path.join("target_value"));
                ignored.push(goal_path.join("current_value"));
                if goal.node_id.is_some() {
                    ignored.push(goal_path.join("nid"));
                }
                if goal.cgroup_path.is_some() {
                    ignored.push(goal_path.join("path"));
                }
            }
            ignored.sort_unstable();
            ignored.dedup();
            self.staged.configuration.matches_current_except(&ignored)?
        } else {
            self.staged
                .configuration
                .matches_current_outside_except(&goals_root, &self.staged.volatile_paths)?
        };
        if !unchanged_matches {
            return Err(Error::OwnershipLost {
                reason: "the running DAMON hierarchy changed during quota-goal staging",
            });
        }
        let snapshot = retry_busy(|| self.admin.configuration_snapshot())?;
        let staged_goals = retry_busy(|| quotas.goal_configurations())?;
        if staged_goals != goals {
            return Err(Error::ConfigurationMismatch {
                path: "quotas/goals".into(),
                expected: format!("{goals:?}").into(),
                observed: format!("{staged_goals:?}").into(),
            });
        }
        if !retry_busy(|| snapshot.values_match_current())? {
            return Err(Error::OwnershipLost {
                reason: "the running DAMON hierarchy changed during quota-goal staging",
            });
        }
        self.verify_owned_identities_only()?;
        self.verify_running_identity_only(kdamond_index)?;
        retry_busy(|| {
            self.kdamond(kdamond_index)
                .command(&KdamondCommand::CommitSchemesQuotaGoals)
        })?;
        self.verify_running_identity_only(kdamond_index)?;

        let staged = self.staged.with_configuration(snapshot);
        staged.verify(&self.admin)?;
        Ok(staged)
    }

    fn restore_and_commit_quota_goals(
        &self,
        snapshot: &ConfigurationSnapshot,
        kdamond_index: usize,
        writes: &[PathBuf],
    ) -> Result<StagedOwnership> {
        self.verify_owned_identities_only()?;
        if let Err(boundary) = self.verify_rollback_boundary(snapshot, writes) {
            return Err(with_rollback(
                boundary,
                self.restore_written_and_commit_quota_goals(snapshot, writes, kdamond_index),
            ));
        }
        let affected = snapshot.paths_affected_by_writes(writes);
        retry_busy(|| snapshot.restore_paths_except(&affected, &self.staged.volatile_paths))?;
        self.verify_running_identity_only(kdamond_index)?;
        retry_busy(|| {
            self.kdamond(kdamond_index)
                .command(&KdamondCommand::CommitSchemesQuotaGoals)
        })?;
        self.verify_running_identity_only(kdamond_index)?;
        if !retry_busy(|| snapshot.matches_current_except(&self.staged.volatile_paths))? {
            return Err(Error::OwnershipLost {
                reason: "the running DAMON hierarchy changed after quota-goal rollback",
            });
        }
        let current = retry_busy(|| self.admin.configuration_snapshot())?;
        let staged = self.staged.with_configuration(current);
        staged.verify(&self.admin)?;
        Ok(staged)
    }

    fn restore_written_and_commit_running(
        &self,
        snapshot: &ConfigurationSnapshot,
        writes: &[PathBuf],
        selected: &[usize],
    ) -> Result<()> {
        let affected = snapshot.paths_affected_by_writes(writes);
        retry_busy(|| snapshot.restore_paths_except(&affected, &self.staged.volatile_paths))?;
        for &index in selected {
            self.verify_running_identity_only(index)?;
            retry_busy(|| self.kdamond(index).command(&KdamondCommand::Commit))?;
            self.verify_running_identity_only(index)?;
        }
        Ok(())
    }

    fn restore_written_and_commit_quota_goals(
        &self,
        snapshot: &ConfigurationSnapshot,
        writes: &[PathBuf],
        kdamond_index: usize,
    ) -> Result<()> {
        let affected = snapshot.paths_affected_by_writes(writes);
        retry_busy(|| snapshot.restore_paths_except(&affected, &self.staged.volatile_paths))?;
        self.verify_running_identity_only(kdamond_index)?;
        retry_busy(|| {
            self.kdamond(kdamond_index)
                .command(&KdamondCommand::CommitSchemesQuotaGoals)
        })?;
        self.verify_running_identity_only(kdamond_index)
    }

    fn verify_rollback_boundary(
        &self,
        snapshot: &ConfigurationSnapshot,
        writes: &[PathBuf],
    ) -> Result<()> {
        let mut allowed = snapshot.paths_affected_by_writes(writes);
        allowed.extend(self.staged.volatile_paths.iter().cloned());
        allowed.sort_unstable();
        allowed.dedup();
        if !retry_busy(|| snapshot.matches_current_except(&allowed))? {
            return Err(Error::OwnershipLost {
                reason: "an unrelated writable value changed during the transaction",
            });
        }
        Ok(())
    }

    fn verify_owned_identities_only(&self) -> Result<()> {
        if self.admin.kdamond_count()? != self.states.len() {
            return Err(Error::OwnershipLost {
                reason: "the staged kdamond count changed",
            });
        }
        for index in 0..self.states.len() {
            match self.states[index] {
                KdamondSessionState::Staged => {
                    if !matches!(
                        retry_busy(|| self.kdamond(index).state())?,
                        KdamondState::Off
                    ) {
                        return Err(Error::OwnershipLost {
                            reason: "a staged kdamond state changed",
                        });
                    }
                }
                KdamondSessionState::Running(_) => {
                    self.verify_running_identity_without_count(index)?;
                }
                KdamondSessionState::UnidentifiedRunning => {
                    return Err(Error::OwnershipLost {
                        reason: "the kdamond started but its identity was not captured",
                    });
                }
            }
        }
        Ok(())
    }

    fn validate_kdamond_index(&self, index: usize) -> Result<()> {
        if index >= self.states.len() {
            return Err(Error::IndexOutOfBounds {
                kind: "kdamond",
                index,
                count: self.states.len(),
            });
        }
        Ok(())
    }

    pub(super) fn expected_running_pid(&self, index: usize) -> Result<Pid> {
        self.validate_kdamond_index(index)?;
        match self.states[index] {
            KdamondSessionState::Running(pid) => Ok(pid),
            KdamondSessionState::Staged => Err(Error::NotRunning),
            KdamondSessionState::UnidentifiedRunning => Err(Error::OwnershipLost {
                reason: "the kdamond started but its identity was not captured",
            }),
        }
    }

    pub(super) fn close_inner(&mut self) -> Result<()> {
        if !self.owns_hierarchy {
            return Ok(());
        }
        self.stop_all()?;
        self.staged.verify(&self.admin)?;
        let previous = self.previous.as_ref().ok_or(Error::OwnershipLost {
            reason: "the managed hierarchy has no restoration snapshot",
        })?;
        if !retry_busy(|| previous.matches_current())? {
            restore_configuration(&self.admin, previous)?;
        }
        self.owns_hierarchy = false;
        Ok(())
    }
}

fn validate_selected_indices(
    requested: &[usize],
    managed_count: usize,
    requested_count: usize,
) -> Result<Vec<usize>> {
    if requested_count != managed_count {
        return Err(Error::InvalidConfiguration {
            field: "managed hierarchy kdamond count",
            reason: "must match the owned hierarchy",
        });
    }
    let mut seen = vec![false; managed_count];
    let mut selected = Vec::with_capacity(requested.len());
    for &index in requested {
        if index >= managed_count {
            return Err(Error::IndexOutOfBounds {
                kind: "kdamond",
                index,
                count: managed_count,
            });
        }
        if seen[index] {
            return Err(Error::InvalidConfiguration {
                field: "selected kdamond indexes",
                reason: "must not contain duplicates",
            });
        }
        seen[index] = true;
        selected.push(index);
    }
    Ok(selected)
}

fn validate_obsolete_target_updates(previous: &DamonConfig, requested: &DamonConfig) -> Result<()> {
    for (previous_kdamond, requested_kdamond) in previous.kdamonds.iter().zip(&requested.kdamonds) {
        for (previous_context, requested_context) in previous_kdamond
            .contexts
            .iter()
            .zip(&requested_kdamond.contexts)
        {
            for (index, requested_target) in requested_context.targets.iter().enumerate() {
                if !requested_target.obsolete {
                    continue;
                }
                let Some(previous_target) = previous_context.targets.get(index) else {
                    return Err(Error::InvalidConfiguration {
                        field: "obsolete target",
                        reason: "must identify an existing target at the same index",
                    });
                };
                let mut retained_target = requested_target.clone();
                retained_target.obsolete = false;
                if previous_target.obsolete || retained_target != *previous_target {
                    return Err(Error::InvalidConfiguration {
                        field: "obsolete target",
                        reason: "must match the existing target at the same index",
                    });
                }
            }
        }
    }
    Ok(())
}

fn kdamond_configurations_equivalent(
    expected: &super::KdamondConfig,
    observed: &super::KdamondConfig,
) -> bool {
    DamonConfig {
        kdamonds: vec![expected.clone()],
    }
    .equivalent_after_kernel_normalization(&DamonConfig {
        kdamonds: vec![observed.clone()],
    })
}

fn contains_obsolete_targets(config: &DamonConfig, selected: &[usize]) -> bool {
    selected.iter().any(|&index| {
        config.kdamonds[index]
            .contexts
            .iter()
            .any(|context| context.targets.iter().any(|target| target.obsolete))
    })
}

fn without_obsolete_targets(config: &DamonConfig, selected: &[usize]) -> DamonConfig {
    let mut cleaned = config.clone();
    for &index in selected {
        for context in &mut cleaned.kdamonds[index].contexts {
            context.targets.retain(|target| !target.obsolete);
        }
    }
    cleaned
}

fn normalize_running_tuned_intervals(
    requested: &DamonConfig,
    previous: Option<&DamonConfig>,
    observed: &mut DamonConfig,
) -> Result<()> {
    for (kdamond_index, (requested_kdamond, observed_kdamond)) in requested
        .kdamonds
        .iter()
        .zip(&mut observed.kdamonds)
        .enumerate()
    {
        for (context_index, (requested_context, observed_context)) in requested_kdamond
            .contexts
            .iter()
            .zip(&mut observed_kdamond.contexts)
            .enumerate()
        {
            let was_tuned = previous
                .and_then(|config| config.kdamonds.get(kdamond_index))
                .and_then(|kdamond| kdamond.contexts.get(context_index))
                .is_some_and(|context| context.intervals_goal.aggregation_intervals != 0);
            if requested_context.intervals_goal.aggregation_intervals == 0 && !was_tuned {
                continue;
            }
            observed_context.intervals = MonitoringIntervals::new(
                requested_context.intervals.sample(),
                requested_context.intervals.aggregation(),
                observed_context.intervals.update(),
            )?;
        }
    }
    Ok(())
}

impl Drop for ManagedHierarchy {
    fn drop(&mut self) {
        if self.previous.is_some() {
            let _ = self.close_inner();
        }
    }
}