use std::path::{Path, PathBuf};
use std::time::Duration;
use crate::config::Pid;
use crate::{Error, Result};
use super::configuration;
use super::ownership::{
ConfigurationFingerprint, ConfigurationSnapshot, ObservedConfiguration, capture_configuration,
};
use super::sysfs_io::{
duration_millis, invalid_kernel_value, path_exists, read_i32, read_text, read_u32, read_usize,
write_bytes, write_value, write_value_if_present,
};
use super::{Context, DEFAULT_ADMIN_PATH, KdamondCommand, KdamondState};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DamonAdmin {
pub(super) root: PathBuf,
}
impl DamonAdmin {
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
let root = path.as_ref().to_path_buf();
let count_path = root.join("kdamonds/nr_kdamonds");
let exists = path_exists(&count_path)?;
if !exists {
return Err(Error::Unavailable { path: root });
}
let admin = Self { root };
admin.kdamond_count()?;
Ok(admin)
}
pub fn open_default() -> Result<Self> {
Self::open(DEFAULT_ADMIN_PATH)
}
#[must_use]
pub fn path(&self) -> &Path {
&self.root
}
pub fn kdamond_count(&self) -> Result<usize> {
read_usize(&self.root.join("kdamonds/nr_kdamonds"))
}
pub fn set_kdamond_count(&self, count: usize) -> Result<()> {
configuration::validate_count("kdamond count", count)?;
write_value(&self.root.join("kdamonds/nr_kdamonds"), count)
}
#[must_use]
pub fn kdamond(&self, index: usize) -> Kdamond {
Kdamond {
path: self.root.join("kdamonds").join(index.to_string()),
}
}
pub(crate) fn configuration_snapshot(&self) -> Result<ConfigurationSnapshot> {
ConfigurationSnapshot::capture(&self.root)
}
pub fn observed_configuration(&self) -> Result<ObservedConfiguration> {
let snapshot = self.configuration_snapshot()?;
let configuration = self.configuration()?;
if !snapshot.matches_complete_current_except(&[])? {
return Err(Error::OwnershipLost {
reason: "the DAMON hierarchy changed while it was being observed",
});
}
Ok(snapshot.into_observed(configuration))
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Kdamond {
pub(super) path: PathBuf,
}
impl Kdamond {
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
pub fn state(&self) -> Result<KdamondState> {
let path = self.path.join("state");
let value = read_text(&path)?;
value.trim().parse()
}
pub fn command(&self, command: &KdamondCommand) -> Result<()> {
configuration::validate_token("kdamond command", command.kernel_name())?;
write_bytes(&self.path.join("state"), command.kernel_name().as_bytes())
}
pub fn pid(&self) -> Result<Option<Pid>> {
let raw = read_i32(&self.path.join("pid"))?;
if raw < 0 {
return Ok(None);
}
let raw = u32::try_from(raw).map_err(|_| {
invalid_kernel_value(
&self.path.join("pid"),
raw.to_string(),
"a process ID or -1",
)
})?;
Pid::new(raw).map(Some).map_err(|_| {
invalid_kernel_value(
&self.path.join("pid"),
raw.to_string(),
"a process ID or -1",
)
})
}
pub fn refresh_interval(&self) -> Result<Duration> {
let milliseconds = read_u32(&self.path.join("refresh_ms"))?;
Ok(Duration::from_millis(u64::from(milliseconds)))
}
pub fn set_refresh_interval(&self, interval: Duration) -> Result<()> {
let milliseconds = duration_millis(interval)?;
write_value(&self.path.join("refresh_ms"), milliseconds)
}
pub(crate) fn set_default_refresh_interval_if_present(&self) -> Result<()> {
write_value_if_present(&self.path.join("refresh_ms"), 0_u8).map(|_| ())
}
pub fn context_count(&self) -> Result<usize> {
read_usize(&self.path.join("contexts/nr_contexts"))
}
pub fn set_context_count(&self, count: usize) -> Result<()> {
configuration::validate_count("context count", count)?;
write_value(&self.path.join("contexts/nr_contexts"), count)
}
#[must_use]
pub fn context(&self, index: usize) -> Context {
Context {
path: self.path.join("contexts").join(index.to_string()),
}
}
pub(crate) fn configuration_fingerprint(&self) -> Result<ConfigurationFingerprint> {
capture_configuration(&self.path)
}
}