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
//! Ownership-safe runtime access to managed kdamonds.

use super::{
    Capabilities, Error, KdamondCommand, ManagedHierarchy, QuotaGoalConfig, RawSnapshot, Result,
    SchemeStats, retry_busy, with_rollback,
};

/// A borrowed ownership-safe runtime view of one managed kdamond.
///
/// Construct this through [`ManagedHierarchy::runtime`]. Every operation
/// verifies the complete staged hierarchy and the selected kernel thread at
/// its trust boundaries.
#[derive(Debug)]
pub struct ManagedKdamond<'a> {
    pub(super) hierarchy: &'a mut ManagedHierarchy,
    pub(super) index: usize,
}

impl ManagedKdamond<'_> {
    /// Returns this kdamond's index in the managed hierarchy.
    #[must_use]
    pub const fn index(&self) -> usize {
        self.index
    }

    /// Discovers capabilities for one staged context and scheme.
    pub fn capabilities(&self, context_index: usize, scheme_index: usize) -> Result<Capabilities> {
        self.hierarchy
            .capabilities(self.index, context_index, scheme_index)
    }

    /// Returns whether the selected owned kdamond is still running.
    pub fn is_running(&self) -> Result<bool> {
        self.hierarchy.is_running(self.index)
    }

    /// Pauses the first monitoring context and commits the request.
    pub fn pause(&mut self) -> Result<()> {
        self.pause_context(0)
    }

    /// Resumes the first monitoring context and commits the request.
    pub fn resume(&mut self) -> Result<()> {
        self.resume_context(0)
    }

    /// Pauses one monitoring context and commits the request.
    pub fn pause_context(&mut self, context_index: usize) -> Result<()> {
        self.set_context_paused(context_index, true)
    }

    /// Resumes one monitoring context and commits the request.
    pub fn resume_context(&mut self, context_index: usize) -> Result<()> {
        self.set_context_paused(context_index, false)
    }

    /// Refreshes and reads one scheme's runtime statistics.
    pub fn scheme_stats(
        &mut self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<SchemeStats> {
        self.runtime_batch(|batch| batch.scheme_stats(context_index, scheme_index))
    }

    /// Reads one scheme's last materialized runtime statistics.
    pub fn cached_scheme_stats(
        &self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<SchemeStats> {
        self.hierarchy.verify_running(self.index)?;
        let stats = self
            .hierarchy
            .scheme(self.index, context_index, scheme_index)?
            .stats()?;
        self.hierarchy.verify_running_identity(self.index)?;
        Ok(stats)
    }

    /// Materializes and reads one scheme's tried regions in raw address units.
    ///
    /// Linux may block the materialization command until every scheme reaches
    /// its next apply interval. The returned regions are scheme scoped because
    /// ordinary sysfs tried-region output contains no target identifier.
    pub fn tried_regions(
        &mut self,
        context_index: usize,
        scheme_index: usize,
        capacity_hint: usize,
    ) -> Result<RawSnapshot> {
        self.runtime_batch(|batch| batch.tried_regions(context_index, scheme_index, capacity_hint))
    }

    /// Reads one scheme's already materialized tried regions.
    pub fn cached_tried_regions(
        &self,
        context_index: usize,
        scheme_index: usize,
        capacity_hint: usize,
    ) -> Result<RawSnapshot> {
        self.hierarchy.verify_running(self.index)?;
        let snapshot = self
            .hierarchy
            .scheme(self.index, context_index, scheme_index)?
            .tried_regions(capacity_hint)?;
        self.hierarchy.verify_running_identity(self.index)?;
        Ok(snapshot)
    }

    /// Synchronously refreshes and reads total tried units.
    pub fn tried_bytes_units(&mut self, context_index: usize, scheme_index: usize) -> Result<u64> {
        self.runtime_batch(|batch| batch.tried_bytes_units(context_index, scheme_index))
    }

    /// Reads one scheme's already materialized total tried units.
    pub fn cached_tried_bytes_units(
        &self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<u64> {
        self.hierarchy.verify_running(self.index)?;
        let units = self
            .hierarchy
            .scheme(self.index, context_index, scheme_index)?
            .tried_bytes_units()?;
        self.hierarchy.verify_running_identity(self.index)?;
        Ok(units)
    }

    /// Synchronously refreshes and reads one scheme's effective quota units.
    pub fn effective_quota_units(
        &mut self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<u64> {
        self.runtime_batch(|batch| batch.effective_quota_units(context_index, scheme_index))
    }

    /// Reads one scheme's last materialized effective quota units.
    pub fn cached_effective_quota_units(
        &self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<u64> {
        self.hierarchy.verify_running(self.index)?;
        let units = self
            .hierarchy
            .scheme(self.index, context_index, scheme_index)?
            .quotas()
            .effective_size_units()?;
        self.hierarchy.verify_running_identity(self.index)?;
        Ok(units)
    }

    /// Transactionally replaces one scheme's quota goals and commits only
    /// quota-goal inputs to the running kdamond.
    pub fn update_scheme_quota_goals(
        &mut self,
        context_index: usize,
        scheme_index: usize,
        goals: &[QuotaGoalConfig],
    ) -> Result<()> {
        self.hierarchy
            .update_scheme_quota_goals(self.index, context_index, scheme_index, goals)
    }

    /// Runs multiple runtime reads or refreshes under one pair of complete
    /// ownership checks.
    ///
    /// Individual batch operations still verify the selected kernel thread.
    /// 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 RuntimeBatch<'_>) -> Result<T>,
    ) -> Result<T> {
        self.hierarchy.verify_running(self.index)?;
        let result = {
            let kdamond = self.hierarchy.kdamond(self.index);
            let mut batch = RuntimeBatch {
                hierarchy: self.hierarchy,
                kdamond_index: self.index,
                kdamond,
                context_count: None,
                cached_context: None,
            };
            operation(&mut batch)
        };
        self.hierarchy.verify_running_identity(self.index)?;
        result
    }

    /// Runs cached reads under one pair of complete ownership checks.
    pub fn read_batch<T>(
        &self,
        operation: impl FnOnce(&mut RuntimeReadBatch<'_>) -> Result<T>,
    ) -> Result<T> {
        self.hierarchy.verify_running(self.index)?;
        let result = {
            let kdamond = self.hierarchy.kdamond(self.index);
            let mut batch = RuntimeReadBatch {
                hierarchy: self.hierarchy,
                kdamond_index: self.index,
                kdamond,
                context_count: None,
                cached_context: None,
            };
            operation(&mut batch)
        };
        self.hierarchy.verify_running_identity(self.index)?;
        result
    }

    /// Synchronously refreshes auto-tuned monitoring intervals.
    #[allow(clippy::redundant_closure_for_method_calls)]
    pub fn update_tuned_intervals(&mut self) -> Result<()> {
        self.runtime_batch(|batch| batch.update_tuned_intervals())
    }

    /// Clears materialized tried-region results.
    #[allow(clippy::redundant_closure_for_method_calls)]
    pub fn clear_tried_regions(&mut self) -> Result<()> {
        self.runtime_batch(|batch| batch.clear_tried_regions())
    }

    fn set_context_paused(&mut self, context_index: usize, paused: bool) -> Result<()> {
        self.hierarchy.verify_running(self.index)?;
        let kdamond = self.hierarchy.kdamond(self.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);
        if !context.pause_control_available()? {
            return Err(Error::UnsupportedFeature {
                feature: "DAMON context pause",
            });
        }
        let previous = context.is_paused()?;
        if previous == paused {
            self.hierarchy.verify_running_identity(self.index)?;
            return Ok(());
        }
        let previous_fingerprint = self.hierarchy.staged.configuration.clone();
        let pause_path = context.path().join("pause");
        context.set_paused(paused)?;
        let operation = (|| {
            retry_busy(|| kdamond.command(&KdamondCommand::Commit))?;
            self.hierarchy.verify_running_identity_only(self.index)?;
            let observed = context.is_paused()?;
            if observed != paused {
                return Err(Error::ConfigurationMismatch {
                    path: format!("contexts/{context_index}/pause").into(),
                    expected: paused.to_string().into(),
                    observed: observed.to_string().into(),
                });
            }
            let refreshed = self.hierarchy.staged.configuration.refreshed_paths_except(
                std::slice::from_ref(&pause_path),
                &self.hierarchy.staged.volatile_paths,
            )?;
            self.hierarchy.verify_running_identity_only(self.index)?;
            self.hierarchy.staged.configuration = refreshed;
            Ok(())
        })();
        if let Err(operation) = operation {
            let rollback = (|| {
                context.set_paused(previous)?;
                retry_busy(|| kdamond.command(&KdamondCommand::Commit))?;
                let restored = previous_fingerprint.refreshed_paths_except(
                    std::slice::from_ref(&pause_path),
                    &self.hierarchy.staged.volatile_paths,
                )?;
                self.hierarchy.verify_running_identity_only(self.index)?;
                self.hierarchy.staged.configuration = restored;
                Ok(())
            })();
            return Err(with_rollback(operation, rollback));
        }
        Ok(())
    }
}

/// Runtime operations across kdamonds batched between complete ownership checks.
///
/// Construct this through [`ManagedHierarchy::runtime_batch`]. Selecting a
/// kdamond performs no sysfs scan. Each operation on the returned
/// [`RuntimeBatch`] still verifies that kdamond's kernel-thread identity.
#[derive(Debug)]
pub struct HierarchyRuntimeBatch<'a> {
    pub(super) hierarchy: &'a mut ManagedHierarchy,
}

impl HierarchyRuntimeBatch<'_> {
    /// Borrows batched runtime access to one running kdamond.
    pub fn kdamond(&mut self, kdamond_index: usize) -> Result<RuntimeBatch<'_>> {
        self.hierarchy.expected_running_pid(kdamond_index)?;
        let kdamond = self.hierarchy.kdamond(kdamond_index);
        Ok(RuntimeBatch {
            hierarchy: self.hierarchy,
            kdamond_index,
            kdamond,
            context_count: None,
            cached_context: None,
        })
    }
}

/// Cached reads across kdamonds between complete ownership checks.
#[derive(Debug)]
pub struct HierarchyReadBatch<'a> {
    pub(super) hierarchy: &'a ManagedHierarchy,
}

impl HierarchyReadBatch<'_> {
    /// Borrows batched cached access to one running kdamond.
    pub fn kdamond(&mut self, kdamond_index: usize) -> Result<RuntimeReadBatch<'_>> {
        self.hierarchy.expected_running_pid(kdamond_index)?;
        let kdamond = self.hierarchy.kdamond(kdamond_index);
        Ok(RuntimeReadBatch {
            hierarchy: self.hierarchy,
            kdamond_index,
            kdamond,
            context_count: None,
            cached_context: None,
        })
    }
}

/// Runtime operations batched between complete ownership checks.
///
/// Construct this through [`ManagedKdamond::runtime_batch`] or
/// [`crate::ExclusiveSession::runtime_batch`].
#[derive(Debug)]
pub struct RuntimeBatch<'a> {
    pub(super) hierarchy: &'a mut ManagedHierarchy,
    pub(super) kdamond_index: usize,
    kdamond: crate::sysfs::Kdamond,
    context_count: Option<usize>,
    cached_context: Option<(usize, crate::sysfs::Context, usize)>,
}

impl RuntimeBatch<'_> {
    /// Returns the selected kdamond index.
    #[must_use]
    pub const fn kdamond_index(&self) -> usize {
        self.kdamond_index
    }

    /// Synchronously refreshes and reads one scheme's statistics.
    pub fn scheme_stats(
        &mut self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<SchemeStats> {
        let scheme = self.scheme(context_index, scheme_index)?;
        self.issue_command(&KdamondCommand::UpdateSchemesStats)?;
        let stats = scheme.stats()?;
        self.verify_identity()?;
        Ok(stats)
    }

    /// Reads the last materialized scheme statistics.
    pub fn cached_scheme_stats(
        &mut self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<SchemeStats> {
        let stats = self.scheme(context_index, scheme_index)?.stats()?;
        self.verify_identity()?;
        Ok(stats)
    }

    /// Materializes and reads one scheme's tried regions.
    pub fn tried_regions(
        &mut self,
        context_index: usize,
        scheme_index: usize,
        capacity_hint: usize,
    ) -> Result<RawSnapshot> {
        let scheme = self.scheme(context_index, scheme_index)?;
        self.issue_command(&KdamondCommand::UpdateSchemesTriedRegions)?;
        let snapshot = scheme.tried_regions(capacity_hint)?;
        self.verify_identity()?;
        Ok(snapshot)
    }

    /// Reads one scheme's already materialized tried regions.
    pub fn cached_tried_regions(
        &mut self,
        context_index: usize,
        scheme_index: usize,
        capacity_hint: usize,
    ) -> Result<RawSnapshot> {
        let snapshot = self
            .scheme(context_index, scheme_index)?
            .tried_regions(capacity_hint)?;
        self.verify_identity()?;
        Ok(snapshot)
    }

    /// Synchronously refreshes and reads total tried units.
    pub fn tried_bytes_units(&mut self, context_index: usize, scheme_index: usize) -> Result<u64> {
        let scheme = self.scheme(context_index, scheme_index)?;
        self.issue_command(&KdamondCommand::UpdateSchemesTriedBytes)?;
        let units = scheme.tried_bytes_units()?;
        self.verify_identity()?;
        Ok(units)
    }

    /// Reads one scheme's already materialized total tried units.
    pub fn cached_tried_bytes_units(
        &mut self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<u64> {
        let units = self
            .scheme(context_index, scheme_index)?
            .tried_bytes_units()?;
        self.verify_identity()?;
        Ok(units)
    }

    /// Synchronously refreshes and reads effective quota units.
    pub fn effective_quota_units(
        &mut self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<u64> {
        let scheme = self.scheme(context_index, scheme_index)?;
        self.issue_command(&KdamondCommand::UpdateSchemesEffectiveQuotas)?;
        let units = scheme.quotas().effective_size_units()?;
        self.verify_identity()?;
        Ok(units)
    }

    /// Reads the last materialized effective quota units.
    pub fn cached_effective_quota_units(
        &mut self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<u64> {
        let units = self
            .scheme(context_index, scheme_index)?
            .quotas()
            .effective_size_units()?;
        self.verify_identity()?;
        Ok(units)
    }

    /// Synchronously refreshes auto-tuned monitoring intervals.
    pub fn update_tuned_intervals(&mut self) -> Result<()> {
        self.command(&KdamondCommand::UpdateTunedIntervals)
    }

    /// Clears materialized tried-region results.
    pub fn clear_tried_regions(&mut self) -> Result<()> {
        self.command(&KdamondCommand::ClearSchemesTriedRegions)
    }

    fn scheme(
        &mut self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<crate::sysfs::Scheme> {
        cached_scheme(
            &self.kdamond,
            context_index,
            scheme_index,
            &mut self.context_count,
            &mut self.cached_context,
        )
    }

    fn command(&self, command: &KdamondCommand) -> Result<()> {
        self.issue_command(command)?;
        self.verify_identity()
    }

    fn issue_command(&self, command: &KdamondCommand) -> Result<()> {
        self.verify_identity()?;
        retry_busy(|| self.kdamond.command(command))
    }

    fn verify_identity(&self) -> Result<()> {
        self.hierarchy
            .verify_running_identity_with(self.kdamond_index, &self.kdamond)
    }
}

/// Read-only runtime operations batched between complete ownership checks.
#[derive(Debug)]
pub struct RuntimeReadBatch<'a> {
    hierarchy: &'a ManagedHierarchy,
    kdamond_index: usize,
    kdamond: crate::sysfs::Kdamond,
    context_count: Option<usize>,
    cached_context: Option<(usize, crate::sysfs::Context, usize)>,
}

impl RuntimeReadBatch<'_> {
    /// Returns the selected kdamond index.
    #[must_use]
    pub const fn kdamond_index(&self) -> usize {
        self.kdamond_index
    }

    /// Reads one scheme's last materialized statistics.
    pub fn scheme_stats(
        &mut self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<SchemeStats> {
        let stats = self.scheme(context_index, scheme_index)?.stats()?;
        self.verify_identity()?;
        Ok(stats)
    }

    /// Reads one scheme's already materialized tried regions.
    pub fn tried_regions(
        &mut self,
        context_index: usize,
        scheme_index: usize,
        capacity_hint: usize,
    ) -> Result<RawSnapshot> {
        let snapshot = self
            .scheme(context_index, scheme_index)?
            .tried_regions(capacity_hint)?;
        self.verify_identity()?;
        Ok(snapshot)
    }

    /// Reads one scheme's already materialized tried total in core units.
    pub fn tried_bytes_units(&mut self, context_index: usize, scheme_index: usize) -> Result<u64> {
        let units = self
            .scheme(context_index, scheme_index)?
            .tried_bytes_units()?;
        self.verify_identity()?;
        Ok(units)
    }

    /// Reads one scheme's last materialized effective quota in core units.
    pub fn effective_quota_units(
        &mut self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<u64> {
        let units = self
            .scheme(context_index, scheme_index)?
            .quotas()
            .effective_size_units()?;
        self.verify_identity()?;
        Ok(units)
    }

    fn scheme(
        &mut self,
        context_index: usize,
        scheme_index: usize,
    ) -> Result<crate::sysfs::Scheme> {
        cached_scheme(
            &self.kdamond,
            context_index,
            scheme_index,
            &mut self.context_count,
            &mut self.cached_context,
        )
    }

    fn verify_identity(&self) -> Result<()> {
        self.hierarchy
            .verify_running_identity_with(self.kdamond_index, &self.kdamond)
    }
}

fn cached_scheme(
    kdamond: &crate::sysfs::Kdamond,
    context_index: usize,
    scheme_index: usize,
    context_count: &mut Option<usize>,
    cached_context: &mut Option<(usize, crate::sysfs::Context, usize)>,
) -> Result<crate::sysfs::Scheme> {
    let count = if let Some(count) = *context_count {
        count
    } else {
        let count = kdamond.context_count()?;
        *context_count = Some(count);
        count
    };
    if context_index >= count {
        return Err(Error::IndexOutOfBounds {
            kind: "context",
            index: context_index,
            count,
        });
    }
    if let Some((cached_index, context, scheme_count)) = cached_context.as_ref() {
        if *cached_index == context_index {
            if scheme_index >= *scheme_count {
                return Err(Error::IndexOutOfBounds {
                    kind: "scheme",
                    index: scheme_index,
                    count: *scheme_count,
                });
            }
            return Ok(context.scheme(scheme_index));
        }
    }

    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,
        });
    }
    let scheme = context.scheme(scheme_index);
    *cached_context = Some((context_index, context, scheme_count));
    Ok(scheme)
}