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
//! Exclusive ownership, staging, rollback, and session lifecycle.
use super::{
AddressUnit, Capabilities, DEFAULT_SESSION_LOCK_PATH, DamonAdmin, DamonConfig, Error,
FvaddrSessionBuilder, KdamondCommand, ManagedHierarchy, MonitorBuilder, ObservedConfiguration,
PaddrSessionBuilder, Path, PathBuf, Pid, QuotaGoalConfig, RawSnapshot, Result, RuntimeBatch,
RuntimeReadBatch, SchemeStats, SessionLock, StagedConfiguration, VaddrSessionBuilder,
WorkflowOptions, ensure_hierarchy_stopped, replaceable_configuration_read_error,
restore_after_capability_probe, restore_configuration, retry_busy,
stage_and_verify_configuration, stage_capability_probe, with_rollback,
};
/// Entry point for high-level DAMON monitoring.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Damon {
pub(super) admin: DamonAdmin,
pub(super) lock_path: PathBuf,
}
impl Damon {
/// Opens the conventional `/sys/kernel/mm/damon/admin` hierarchy.
pub fn new() -> Result<Self> {
if !cfg!(target_os = "linux") {
return Err(Error::UnsupportedPlatform);
}
Self::at(crate::sysfs::DEFAULT_ADMIN_PATH)
}
/// Opens a DAMON admin hierarchy at a custom location.
///
/// This is primarily useful for mounted sysfs instances, containers, and
/// deterministic test fixtures.
pub fn at(path: impl AsRef<Path>) -> Result<Self> {
Self::at_with_lock(path, DEFAULT_SESSION_LOCK_PATH)
}
/// Opens a DAMON hierarchy with a custom high-level session lock path.
///
/// Cooperating controllers must use the same lock path. The lock is
/// advisory because the kernel DAMON sysfs ABI has no ownership primitive.
/// The lock file's parent directory should be trusted and non-writable by
/// unprivileged users on a production system. Both paths must be absolute.
pub fn at_with_lock(path: impl AsRef<Path>, lock_path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref();
let lock_path = lock_path.as_ref();
if !path.is_absolute() || !lock_path.is_absolute() {
return Err(Error::InvalidConfiguration {
field: "high-level DAMON paths",
reason: "admin and advisory lock paths must be absolute",
});
}
Ok(Self {
admin: DamonAdmin::open(path)?,
lock_path: lock_path.to_path_buf(),
})
}
/// Returns the advisory lock path used by high-level sessions.
#[must_use]
pub fn lock_path(&self) -> &Path {
&self.lock_path
}
/// Starts building a virtual-address monitor for a process.
#[must_use]
pub fn monitor_pid(&self, pid: Pid) -> MonitorBuilder<'_> {
self.vaddr().pid(pid)
}
/// Starts building a process virtual-address monitoring workflow.
#[must_use]
pub fn vaddr(&self) -> VaddrSessionBuilder<'_> {
VaddrSessionBuilder {
options: WorkflowOptions::new(self),
targets: Vec::new(),
}
}
/// Starts building a fixed virtual-address monitoring workflow.
#[must_use]
pub fn fvaddr(&self) -> FvaddrSessionBuilder<'_> {
FvaddrSessionBuilder {
options: WorkflowOptions::new(self),
targets: Vec::new(),
}
}
/// Starts building a physical-address monitoring workflow.
#[must_use]
pub fn paddr(&self) -> PaddrSessionBuilder<'_> {
PaddrSessionBuilder {
options: WorkflowOptions::new(self),
address_unit: AddressUnit::ONE,
}
}
/// Transactionally stages a complete owned DAMON configuration.
///
/// The complete object graph is validated before the first sysfs access.
/// Staging then holds the advisory session lock, requires every existing
/// kdamond to be stopped, and verifies the values read back from the
/// kernel. If staging fails, the preceding writable hierarchy is restored,
/// including unknown future attributes.
///
/// The kernel does not provide an atomic replacement or ownership
/// primitive. Other controllers must honor [`Self::lock_path`] for these
/// guarantees to hold, and readers can observe intermediate sysfs writes.
pub fn stage_configuration(&self, config: &DamonConfig) -> Result<()> {
config.validate()?;
let session_lock = SessionLock::acquire(&self.lock_path)?;
self.stage_validated_configuration_locked(&session_lock, config)
.map(drop)
}
/// Stages and cooperatively owns a complete runnable DAMON hierarchy.
///
/// Every kdamond is initially stopped. The returned hierarchy retains the
/// advisory lock and restores the preceding stopped configuration when
/// explicitly closed or dropped.
pub fn managed_hierarchy(&self, config: &DamonConfig) -> Result<ManagedHierarchy> {
config.validate_runnable()?;
let session_lock = SessionLock::acquire(&self.lock_path)?;
let staged_configuration =
self.stage_validated_configuration_locked(&session_lock, config)?;
ManagedHierarchy::new(
self.admin.clone(),
staged_configuration,
config,
session_lock,
)
}
/// Reads the known typed hierarchy and every writable configuration value.
///
/// This acquires the cooperative lock for the complete observation but
/// cannot exclude controllers that ignore that lock.
pub fn observed_configuration(&self) -> Result<ObservedConfiguration> {
let _session_lock = SessionLock::acquire(&self.lock_path)?;
retry_busy(|| self.admin.observed_configuration())
}
/// Stages a complete configuration and retains exclusive cooperative
/// ownership until the returned session is closed or dropped.
///
/// Exactly one kdamond must be requested. Any preceding stopped hierarchy
/// is restored by [`ExclusiveSession::close`], or on a best-effort basis
/// when the session is dropped.
pub fn exclusive_session(&self, config: &DamonConfig) -> Result<ExclusiveSession> {
if config.kdamonds.len() != 1 {
return Err(Error::InvalidConfiguration {
field: "exclusive session kdamond count",
reason: "must contain exactly one kdamond",
});
}
Ok(ExclusiveSession {
managed: self.managed_hierarchy(config)?,
})
}
/// Stages a validated configuration while the caller retains ownership of
/// the advisory session lock.
///
/// Keeping this boundary explicit lets a future running session stage,
/// start, and record its ownership fingerprint under one uninterrupted
/// lock acquisition.
fn stage_validated_configuration_locked(
&self,
_session_lock: &SessionLock,
config: &DamonConfig,
) -> Result<StagedConfiguration> {
retry_busy(|| ensure_hierarchy_stopped(&self.admin))?;
let previous = retry_busy(|| self.admin.configuration_snapshot())?;
let observed = match retry_busy(|| self.admin.configuration()) {
Ok(observed) => Some(observed),
Err(error) if replaceable_configuration_read_error(&error) => None,
Err(error) => return Err(error),
};
if !retry_busy(|| previous.values_match_current())? {
return Err(Error::OwnershipLost {
reason: "the DAMON hierarchy changed while it was being captured",
});
}
retry_busy(|| ensure_hierarchy_stopped(&self.admin))?;
if observed
.as_ref()
.is_some_and(|observed| config.equivalent_after_kernel_normalization(observed))
{
return Ok(StagedConfiguration {
current: previous.clone(),
previous,
});
}
let operation = stage_and_verify_configuration(&self.admin, config, observed.as_ref());
match operation {
Ok(current) => Ok(StagedConfiguration { previous, current }),
Err(operation) => Err(with_rollback(
operation,
restore_configuration(&self.admin, &previous),
)),
}
}
/// Exclusively stages representative nodes and discovers this kernel's
/// concrete and semantic DAMON sysfs capabilities.
///
/// Discovery holds the same advisory lock as a monitor and requires all
/// existing kdamonds to be stopped. It never starts a kdamond. The exact
/// preceding writable hierarchy is restored before this method returns.
/// This makes capabilities below indexed children and accepted filter
/// values observable without discarding a stopped configuration.
pub fn capabilities(&self) -> Result<Capabilities> {
let _session_lock = SessionLock::acquire(&self.lock_path)?;
retry_busy(|| ensure_hierarchy_stopped(&self.admin))?;
let previous = retry_busy(|| self.admin.configuration_snapshot())?;
if !retry_busy(|| previous.values_match_current())? {
return Err(Error::OwnershipLost {
reason: "the DAMON hierarchy changed while capability probing captured it",
});
}
let kdamond = self.admin.kdamond(0);
let result = (|| {
retry_busy(|| ensure_hierarchy_stopped(&self.admin))?;
retry_busy(|| self.admin.set_kdamond_count(1))?;
stage_capability_probe(&kdamond)
})();
match result {
Ok((capabilities, fingerprint)) => {
restore_after_capability_probe(&self.admin, &kdamond, &fingerprint, &previous)?;
Ok(capabilities)
}
Err(operation) => Err(with_rollback(
operation,
restore_configuration(&self.admin, &previous),
)),
}
}
}
/// A cooperatively exclusive, transactionally staged DAMON session.
///
/// The session holds the advisory lock for its entire lifetime. It verifies
/// the staged configuration and the running kdamond identity around runtime
/// commands, and restores the preceding stopped hierarchy on [`Self::close`].
/// Kernel-updated sampling and aggregation interval leaves are treated as
/// volatile while interval auto-tuning is enabled.
/// Runtime update and result-materialization methods require mutable access,
/// preventing concurrent command and read sequences within one session.
/// Controllers that ignore the advisory lock can still race this API because
/// the kernel provides no ownership primitive.
#[derive(Debug)]
pub struct ExclusiveSession {
managed: ManagedHierarchy,
}
impl ExclusiveSession {
/// Starts the staged kdamond and records its kernel-thread identity.
pub fn start(&mut self) -> Result<()> {
self.managed.start_all()
}
/// Stops the kdamond while retaining the staged configuration and lock.
pub fn stop(&mut self) -> Result<()> {
self.managed.stop_all()
}
/// Stops the kdamond and restores the hierarchy that preceded the session.
///
/// Unlike [`Drop`], this method reports restoration failures.
pub fn close(mut self) -> Result<()> {
self.managed.close_inner()
}
/// Returns whether this session's identified kdamond is still running.
pub fn is_running(&self) -> Result<bool> {
self.managed.is_running(0)
}
/// Reads the complete staged configuration after verifying ownership.
pub fn configuration(&self) -> Result<DamonConfig> {
self.managed.configuration()
}
/// Reads the known typed hierarchy and all writable values after verification.
pub fn observed_configuration(&self) -> Result<ObservedConfiguration> {
self.managed.observed_configuration()
}
/// Discovers capabilities for a staged context and scheme without mutation.
pub fn capabilities(&self, context_index: usize, scheme_index: usize) -> Result<Capabilities> {
self.verify_owned_state()?;
let kdamond = self.managed.kdamond(0);
let capabilities = retry_busy(|| kdamond.capabilities(context_index, scheme_index))?;
self.verify_owned_state()?;
Ok(capabilities)
}
pub(super) fn capabilities_for_schemes(
&self,
context_index: usize,
scheme_indices: &[usize],
) -> Result<Capabilities> {
self.verify_owned_state()?;
let kdamond = self.managed.kdamond(0);
let capabilities =
retry_busy(|| kdamond.capabilities_for_schemes(context_index, scheme_indices))?;
self.verify_owned_state()?;
Ok(capabilities)
}
pub(super) fn replace_staged_configuration(&mut self, config: &DamonConfig) -> Result<()> {
if config.kdamonds.len() != 1 {
return Err(Error::InvalidConfiguration {
field: "exclusive session kdamond count",
reason: "must contain exactly one kdamond",
});
}
self.managed.replace_staged_configuration(config)
}
/// Applies the currently staged inputs to the running kdamond.
///
/// The session refuses untracked sysfs changes. Use
/// [`Self::update_configuration`] to stage and commit a changed owned
/// configuration while preserving rollback and ownership checks.
pub fn commit(&mut self) -> Result<()> {
self.command_after_ownership_check(&KdamondCommand::Commit)?;
self.verify_running_identity()
}
/// Transactionally stages and commits a changed running configuration.
///
/// The requested hierarchy must still contain this session's one runnable
/// kdamond. Obsolete targets must match the preceding target at the same
/// index. Their one-shot markers are removed from the staged hierarchy
/// after a successful commit, and scheme target-filter indexes refer to the
/// retained post-commit target list. On failure, the method stages and
/// commits the preceding owned configuration before returning the error.
/// Controllers that ignore the advisory lock can still interfere because
/// sysfs has no ownership or atomic-commit primitive.
pub fn update_configuration(&mut self, config: &DamonConfig) -> Result<()> {
if config.kdamonds.len() != 1 {
return Err(Error::InvalidConfiguration {
field: "exclusive session kdamond count",
reason: "must contain exactly one kdamond",
});
}
self.managed.update_configuration(config, &[0])
}
/// 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.managed
.runtime(0)?
.update_scheme_quota_goals(context_index, scheme_index, goals)
}
/// 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.managed.runtime(0)?.pause_context(context_index)
}
/// Resumes one monitoring context and commits the request.
pub fn resume_context(&mut self, context_index: usize) -> Result<()> {
self.managed.runtime(0)?.resume_context(context_index)
}
/// Refreshes and reads one scheme's runtime statistics.
pub fn scheme_stats(
&mut self,
context_index: usize,
scheme_index: usize,
) -> Result<SchemeStats> {
self.managed
.runtime(0)?
.scheme_stats(context_index, scheme_index)
}
/// Reads the last materialized scheme statistics without requesting a
/// synchronous kernel refresh.
pub fn cached_scheme_stats(
&self,
context_index: usize,
scheme_index: usize,
) -> Result<SchemeStats> {
self.verify_running()?;
let stats = self.scheme(context_index, scheme_index)?.stats()?;
self.verify_running_identity()?;
Ok(stats)
}
/// Materializes and reads one scheme's tried regions in raw address units.
pub fn tried_regions(
&mut self,
context_index: usize,
scheme_index: usize,
capacity_hint: usize,
) -> Result<RawSnapshot> {
self.managed
.runtime(0)?
.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.verify_running()?;
let snapshot = self
.scheme(context_index, scheme_index)?
.tried_regions(capacity_hint)?;
self.verify_running_identity()?;
Ok(snapshot)
}
/// Refreshes and reads one scheme's total tried size in core address units.
pub fn tried_bytes_units(&mut self, context_index: usize, scheme_index: usize) -> Result<u64> {
self.managed
.runtime(0)?
.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.verify_running()?;
let units = self
.scheme(context_index, scheme_index)?
.tried_bytes_units()?;
self.verify_running_identity()?;
Ok(units)
}
/// Refreshes and reads one scheme's effective quota in core address units.
pub fn effective_quota_units(
&mut self,
context_index: usize,
scheme_index: usize,
) -> Result<u64> {
self.managed
.runtime(0)?
.effective_quota_units(context_index, scheme_index)
}
/// Reads the last materialized effective quota without requesting a
/// synchronous kernel refresh.
pub fn cached_effective_quota_units(
&self,
context_index: usize,
scheme_index: usize,
) -> Result<u64> {
self.verify_running()?;
let units = self
.scheme(context_index, scheme_index)?
.quotas()
.effective_size_units()?;
self.verify_running_identity()?;
Ok(units)
}
/// Runs multiple runtime reads or refreshes under one pair of complete
/// ownership checks.
///
/// Individual batch operations still verify the kdamond thread identity.
/// The complete writable hierarchy is checked before and after the
/// closure, reducing sysfs reads for polling loops without weakening the
/// boundary checks. If both the closure and the 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.managed.runtime(0)?.runtime_batch(operation)
}
/// 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.managed.read_batch(|hierarchy| {
let mut kdamond = hierarchy.kdamond(0)?;
operation(&mut kdamond)
})
}
/// Refreshes auto-tuned interval values for the running kdamond.
pub fn update_tuned_intervals(&mut self) -> Result<()> {
self.managed.runtime(0)?.update_tuned_intervals()
}
/// Clears all materialized tried-region results.
pub fn clear_tried_regions(&mut self) -> Result<()> {
self.managed.runtime(0)?.clear_tried_regions()
}
fn command_after_ownership_check(&self, command: &KdamondCommand) -> Result<()> {
self.managed.command_after_ownership_check(0, command)
}
pub(super) fn scheme(
&self,
context_index: usize,
scheme_index: usize,
) -> Result<crate::sysfs::Scheme> {
let kdamond = self.managed.kdamond(0);
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))
}
fn verify_owned_state(&self) -> Result<()> {
self.managed.verify_owned_state()
}
fn verify_running(&self) -> Result<()> {
self.managed.verify_running(0)
}
fn verify_running_identity(&self) -> Result<()> {
self.managed.verify_running_identity(0)
}
}