use super::{
ByteSizeRange, DestinationConfig, Duration, Error, FilterConfig, InitialRegionConfig,
MAX_EAGER_READ_CAPACITY, OperationAttributesConfig, Path, PathBuf, ProbeHitsWeightedSumRange,
ProbePreparationAction, ProbePreparationConfig, QuotaConfig, QuotaGoalConfig, QuotaGoalMetric,
QuotaGoalTuner, QuotaWeights, Result, SampleControlConfig, SampleFilterConfig,
SampleFilterType, SamplePrimitivesConfig, SchemeFilterType, WatermarkMetric, WatermarksConfig,
ensure_count, exact_micros, exact_millis, invalid_kernel_value, needs_stage, optional_pair,
optional_read, path_exists, read_bool, read_enum, read_i32, read_indexed, read_sysfs_string,
read_u32, read_u64, read_usize, stage_optional_default, validate_count, validate_sysfs_string,
write_bool, write_bytes, write_enum, write_value,
};
mod filter;
mod quota;
mod watermark;
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct SchemeStats {
pub regions_tried: u64,
pub size_tried_units: u64,
pub regions_applied: u64,
pub size_applied_units: u64,
pub operations_filter_passed_units: Option<u64>,
pub quota_exceeds: u64,
pub application_snapshots: Option<u64>,
pub application_snapshot_limit: Option<u64>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InitialRegion {
pub(super) path: PathBuf,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SchemeQuotas {
pub(super) path: PathBuf,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct QuotaGoal {
pub(super) path: PathBuf,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SchemeWatermarks {
pub(super) path: PathBuf,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SchemeFilter {
pub(super) path: PathBuf,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MigrationDestination {
pub(super) path: PathBuf,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OperationAttributes {
pub(super) path: PathBuf,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProbePreparation {
pub(super) path: PathBuf,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SampleControl {
pub(super) path: PathBuf,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SampleFilter {
pub(super) path: PathBuf,
}
impl InitialRegion {
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
pub fn start(&self) -> Result<u64> {
read_u64(&self.path.join("start"))
}
pub fn set_start(&self, start: u64) -> Result<()> {
write_value(&self.path.join("start"), start)
}
pub fn end(&self) -> Result<u64> {
read_u64(&self.path.join("end"))
}
pub fn set_end(&self, end: u64) -> Result<()> {
write_value(&self.path.join("end"), end)
}
pub fn configuration(&self) -> Result<InitialRegionConfig> {
Ok(InitialRegionConfig {
start: self.start()?,
end: self.end()?,
})
}
pub fn stage_configuration(&self, config: InitialRegionConfig) -> Result<()> {
InitialRegionConfig::new(config.start, config.end)?;
self.set_start(config.start)?;
self.set_end(config.end)
}
}
impl OperationAttributes {
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
pub fn use_reports(&self) -> Result<bool> {
read_bool(&self.path.join("use_reports"))
}
pub fn set_use_reports(&self, value: bool) -> Result<()> {
write_bool(&self.path.join("use_reports"), value)
}
pub fn write_only(&self) -> Result<bool> {
read_bool(&self.path.join("write_only"))
}
pub fn set_write_only(&self, value: bool) -> Result<()> {
write_bool(&self.path.join("write_only"), value)
}
pub fn cpus(&self) -> Result<String> {
read_sysfs_string(&self.path.join("cpus"))
}
pub fn set_cpus(&self, value: &str) -> Result<()> {
validate_sysfs_string("operation CPU list", value)?;
write_bytes(&self.path.join("cpus"), value.as_bytes())
}
pub fn thread_ids(&self) -> Result<String> {
read_sysfs_string(&self.path.join("tids"))
}
pub fn set_thread_ids(&self, value: &str) -> Result<()> {
validate_sysfs_string("operation thread list", value)?;
write_bytes(&self.path.join("tids"), value.as_bytes())
}
pub fn configuration(&self) -> Result<OperationAttributesConfig> {
Ok(OperationAttributesConfig {
use_reports: self.use_reports()?,
write_only: self.write_only()?,
cpus: self.cpus()?,
thread_ids: self.thread_ids()?,
})
}
pub(super) fn stage_configuration(&self, config: &OperationAttributesConfig) -> Result<()> {
self.set_use_reports(config.use_reports)?;
self.set_write_only(config.write_only)?;
self.set_cpus(&config.cpus)?;
self.set_thread_ids(&config.thread_ids)
}
}
impl ProbePreparation {
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
pub fn action(&self) -> Result<ProbePreparationAction> {
read_enum(
&self.path.join("prep_action"),
ProbePreparationAction::parse,
)
}
pub fn set_action(&self, action: &ProbePreparationAction) -> Result<()> {
write_enum(&self.path.join("prep_action"), action)
}
pub fn configuration(&self) -> Result<ProbePreparationConfig> {
Ok(ProbePreparationConfig::new(self.action()?))
}
pub(super) fn stage_configuration(&self, config: &ProbePreparationConfig) -> Result<()> {
self.set_action(&config.action)
}
}
impl SampleControl {
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
pub fn page_table_enabled(&self) -> Result<bool> {
read_bool(&self.path.join("primitives/page_table"))
}
pub fn set_page_table_enabled(&self, value: bool) -> Result<()> {
write_bool(&self.path.join("primitives/page_table"), value)
}
pub fn page_fault_enabled(&self) -> Result<bool> {
read_bool(&self.path.join("primitives/page_fault"))
}
pub fn set_page_fault_enabled(&self, value: bool) -> Result<()> {
write_bool(&self.path.join("primitives/page_fault"), value)
}
pub fn filter_count(&self) -> Result<usize> {
read_usize(&self.path.join("filters/nr_filters"))
}
pub fn set_filter_count(&self, count: usize) -> Result<()> {
validate_count("sample filter count", count)?;
write_value(&self.path.join("filters/nr_filters"), count)
}
#[must_use]
pub fn filter(&self, index: usize) -> SampleFilter {
SampleFilter {
path: self.path.join("filters").join(index.to_string()),
}
}
pub fn configuration(&self) -> Result<SampleControlConfig> {
Ok(SampleControlConfig {
primitives: SamplePrimitivesConfig {
page_table: self.page_table_enabled()?,
page_fault: self.page_fault_enabled()?,
},
filters: read_indexed(self.filter_count()?, |index| {
self.filter(index).configuration()
})?,
})
}
pub(super) fn stage_configuration(&self, config: &SampleControlConfig) -> Result<()> {
self.set_page_table_enabled(config.primitives.page_table)?;
self.set_page_fault_enabled(config.primitives.page_fault)?;
ensure_count(&self.path.join("filters/nr_filters"), config.filters.len())?;
for (index, filter) in config.filters.iter().enumerate() {
self.filter(index).stage_configuration(filter)?;
}
Ok(())
}
}
impl SampleFilter {
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
pub fn filter_type(&self) -> Result<SampleFilterType> {
read_enum(&self.path.join("type"), SampleFilterType::parse)
}
pub fn set_filter_type(&self, value: &SampleFilterType) -> Result<()> {
write_enum(&self.path.join("type"), value)
}
pub fn matching(&self) -> Result<bool> {
read_bool(&self.path.join("matching"))
}
pub fn set_matching(&self, value: bool) -> Result<()> {
write_bool(&self.path.join("matching"), value)
}
pub fn allowed(&self) -> Result<bool> {
read_bool(&self.path.join("allow"))
}
pub fn set_allowed(&self, value: bool) -> Result<()> {
write_bool(&self.path.join("allow"), value)
}
pub fn cpu_mask(&self) -> Result<String> {
read_sysfs_string(&self.path.join("cpumask"))
}
pub fn set_cpu_mask(&self, value: &str) -> Result<()> {
validate_sysfs_string("sample filter CPU mask", value)?;
write_bytes(&self.path.join("cpumask"), value.as_bytes())
}
pub fn thread_ids(&self) -> Result<String> {
read_sysfs_string(&self.path.join("tid_arr"))
}
pub fn set_thread_ids(&self, value: &str) -> Result<()> {
validate_sysfs_string("sample filter thread list", value)?;
write_bytes(&self.path.join("tid_arr"), value.as_bytes())
}
pub fn configuration(&self) -> Result<SampleFilterConfig> {
let filter_type = self.filter_type()?;
let mut config =
SampleFilterConfig::new(filter_type.clone(), self.matching()?, self.allowed()?);
match filter_type {
SampleFilterType::CpuMask => config.cpu_mask = Some(self.cpu_mask()?),
SampleFilterType::Threads => config.thread_ids = Some(self.thread_ids()?),
SampleFilterType::Unknown(_) => {
config.cpu_mask = optional_read(&self.path.join("cpumask"), || self.cpu_mask())?;
config.thread_ids =
optional_read(&self.path.join("tid_arr"), || self.thread_ids())?;
}
SampleFilterType::Write => {}
}
Ok(config)
}
pub(super) fn stage_configuration(&self, config: &SampleFilterConfig) -> Result<()> {
self.set_filter_type(&config.filter_type)?;
self.set_matching(config.matching)?;
self.set_allowed(config.allow)?;
if let Some(value) = &config.cpu_mask {
self.set_cpu_mask(value)?;
}
if let Some(value) = &config.thread_ids {
self.set_thread_ids(value)?;
}
Ok(())
}
}