use super::{
AddressUnit, Pid, ProbeFilterType, ProbePreparationAction, Result, SampleFilterType, invalid,
invalid_const, validate_count, validate_required_path, validate_sysfs_string, validate_token,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct InitialRegionConfig {
pub start: u64,
pub end: u64,
}
impl InitialRegionConfig {
pub const fn new(start: u64, end: u64) -> Result<Self> {
if start >= end {
return invalid_const(
"initial region",
"start must be less than the exclusive end",
);
}
Ok(Self { start, end })
}
pub fn from_byte_range_exact(
start_bytes: u64,
end_bytes: u64,
unit: AddressUnit,
) -> Result<Self> {
Self::new(
unit.units_from_bytes_exact(start_bytes)?,
unit.units_from_bytes_exact(end_bytes)?,
)
}
pub fn from_byte_range_covering(
start_bytes: u64,
end_bytes: u64,
unit: AddressUnit,
) -> Result<Self> {
if start_bytes >= end_bytes {
return invalid_const(
"initial byte region",
"start must be less than the exclusive end",
);
}
Self::new(unit.floor_units(start_bytes), unit.ceil_units(end_bytes))
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct ProbeFilterConfig {
pub filter_type: ProbeFilterType,
pub matching: bool,
pub allow: bool,
pub cgroup_path: Option<String>,
}
impl ProbeFilterConfig {
#[must_use]
pub fn new(filter_type: ProbeFilterType, matching: bool, allow: bool) -> Self {
Self {
filter_type,
matching,
allow,
cgroup_path: None,
}
}
#[must_use]
pub fn memory_control_group(path: impl Into<String>, matching: bool, allow: bool) -> Self {
Self {
filter_type: ProbeFilterType::MemoryControlGroup,
matching,
allow,
cgroup_path: Some(path.into()),
}
}
pub fn validate(&self) -> Result<()> {
validate_token("probe filter type", self.filter_type.kernel_name())?;
if matches!(self.filter_type, ProbeFilterType::MemoryControlGroup) {
validate_required_path("probe filter cgroup path", self.cgroup_path.as_deref())?;
} else if let Some(path) = &self.cgroup_path {
if !matches!(self.filter_type, ProbeFilterType::Unknown(_)) {
return invalid(
"probe filter payload",
"a cgroup path belongs only to a memcg or unknown filter",
);
}
validate_sysfs_string("probe filter cgroup path", path)?;
}
Ok(())
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct ProbeConfig {
pub filters: Vec<ProbeFilterConfig>,
pub weight: u32,
pub preparations: Vec<ProbePreparationConfig>,
}
impl ProbeConfig {
pub fn validate(&self) -> Result<()> {
validate_count("probe filter count", self.filters.len())?;
validate_count("probe preparation count", self.preparations.len())?;
for filter in &self.filters {
filter.validate()?;
}
for preparation in &self.preparations {
preparation.validate()?;
}
Ok(())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct ProbePreparationConfig {
pub action: ProbePreparationAction,
}
impl ProbePreparationConfig {
#[must_use]
pub const fn new(action: ProbePreparationAction) -> Self {
Self { action }
}
pub fn validate(&self) -> Result<()> {
validate_token("probe preparation action", self.action.kernel_name())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct OperationAttributesConfig {
pub use_reports: bool,
pub write_only: bool,
pub cpus: String,
pub thread_ids: String,
}
impl Default for OperationAttributesConfig {
fn default() -> Self {
Self {
use_reports: false,
write_only: false,
cpus: "all".to_owned(),
thread_ids: String::new(),
}
}
}
impl OperationAttributesConfig {
pub fn validate(&self) -> Result<()> {
validate_sysfs_string("operation CPU list", &self.cpus)?;
validate_sysfs_string("operation thread list", &self.thread_ids)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct SampleFilterConfig {
pub filter_type: SampleFilterType,
pub matching: bool,
pub allow: bool,
pub cpu_mask: Option<String>,
pub thread_ids: Option<String>,
}
impl SampleFilterConfig {
#[must_use]
pub fn new(filter_type: SampleFilterType, matching: bool, allow: bool) -> Self {
Self {
filter_type,
matching,
allow,
cpu_mask: None,
thread_ids: None,
}
}
#[must_use]
pub fn cpu_mask(value: impl Into<String>, matching: bool, allow: bool) -> Self {
Self {
cpu_mask: Some(value.into()),
..Self::new(SampleFilterType::CpuMask, matching, allow)
}
}
#[must_use]
pub fn threads(value: impl Into<String>, matching: bool, allow: bool) -> Self {
Self {
thread_ids: Some(value.into()),
..Self::new(SampleFilterType::Threads, matching, allow)
}
}
#[must_use]
pub fn write(matching: bool, allow: bool) -> Self {
Self::new(SampleFilterType::Write, matching, allow)
}
pub fn validate(&self) -> Result<()> {
validate_token("sample filter type", self.filter_type.kernel_name())?;
match self.filter_type {
SampleFilterType::CpuMask => {
validate_required_path("sample filter CPU mask", self.cpu_mask.as_deref())?;
if self.thread_ids.is_some() {
return invalid(
"sample filter payload",
"a thread list does not belong to a cpumask filter",
);
}
}
SampleFilterType::Threads => {
validate_required_path("sample filter thread list", self.thread_ids.as_deref())?;
if self.cpu_mask.is_some() {
return invalid(
"sample filter payload",
"a CPU mask does not belong to a threads filter",
);
}
}
SampleFilterType::Write if self.cpu_mask.is_some() || self.thread_ids.is_some() => {
return invalid(
"sample filter payload",
"write filters do not accept a CPU mask or thread list",
);
}
_ => {}
}
if let Some(value) = &self.cpu_mask {
validate_sysfs_string("sample filter CPU mask", value)?;
}
if let Some(value) = &self.thread_ids {
validate_sysfs_string("sample filter thread list", value)?;
}
Ok(())
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct SamplePrimitivesConfig {
pub page_table: bool,
pub page_fault: bool,
}
impl Default for SamplePrimitivesConfig {
fn default() -> Self {
Self {
page_table: true,
page_fault: false,
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct SampleControlConfig {
pub primitives: SamplePrimitivesConfig,
pub filters: Vec<SampleFilterConfig>,
}
impl SampleControlConfig {
pub fn validate(&self) -> Result<()> {
validate_count("sample filter count", self.filters.len())?;
for filter in &self.filters {
filter.validate()?;
}
Ok(())
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct TargetConfig {
pub pid: Option<Pid>,
pub obsolete: bool,
pub initial_regions: Vec<InitialRegionConfig>,
}
impl TargetConfig {
#[must_use]
pub fn for_pid(pid: Pid) -> Self {
Self {
pid: Some(pid),
obsolete: false,
initial_regions: Vec::new(),
}
}
#[must_use]
pub const fn address_space() -> Self {
Self {
pid: None,
obsolete: false,
initial_regions: Vec::new(),
}
}
pub fn validate(&self) -> Result<()> {
validate_count("initial region count", self.initial_regions.len())?;
let mut previous_end = None;
for region in &self.initial_regions {
if region.start >= region.end {
return invalid(
"initial region",
"start must be less than the exclusive end",
);
}
if previous_end.is_some_and(|end| end > region.start) {
return invalid(
"initial regions",
"regions must be sorted and must not overlap",
);
}
previous_end = Some(region.end);
}
Ok(())
}
}