use std::fmt;
use std::str::FromStr;
use crate::{AddressUnit, Error, Result};
use super::configuration::validate_token;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum Operation {
VirtualAddress,
FixedVirtualAddress,
PhysicalAddress,
Unknown(Box<str>),
}
impl Operation {
#[must_use]
pub fn kernel_name(&self) -> &str {
match self {
Self::VirtualAddress => "vaddr",
Self::FixedVirtualAddress => "fvaddr",
Self::PhysicalAddress => "paddr",
Self::Unknown(name) => name,
}
}
pub(super) fn parse(value: &str) -> Self {
match value {
"vaddr" => Self::VirtualAddress,
"fvaddr" => Self::FixedVirtualAddress,
"paddr" => Self::PhysicalAddress,
other => Self::Unknown(other.into()),
}
}
}
impl fmt::Display for Operation {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.kernel_name())
}
}
impl FromStr for Operation {
type Err = Error;
fn from_str(value: &str) -> Result<Self> {
validate_token("monitoring operation", value)?;
Ok(Self::parse(value))
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum KdamondCommand {
On,
Off,
Commit,
CommitSchemesQuotaGoals,
UpdateSchemesStats,
UpdateSchemesTriedBytes,
UpdateSchemesTriedRegions,
ClearSchemesTriedRegions,
UpdateSchemesEffectiveQuotas,
UpdateTunedIntervals,
Unknown(Box<str>),
}
impl KdamondCommand {
#[must_use]
pub fn kernel_name(&self) -> &str {
match self {
Self::On => "on",
Self::Off => "off",
Self::Commit => "commit",
Self::CommitSchemesQuotaGoals => "commit_schemes_quota_goals",
Self::UpdateSchemesStats => "update_schemes_stats",
Self::UpdateSchemesTriedBytes => "update_schemes_tried_bytes",
Self::UpdateSchemesTriedRegions => "update_schemes_tried_regions",
Self::ClearSchemesTriedRegions => "clear_schemes_tried_regions",
Self::UpdateSchemesEffectiveQuotas => "update_schemes_effective_quotas",
Self::UpdateTunedIntervals => "update_tuned_intervals",
Self::Unknown(command) => command,
}
}
}
impl FromStr for KdamondCommand {
type Err = Error;
fn from_str(value: &str) -> Result<Self> {
validate_token("kdamond command", value)?;
Ok(match value {
"on" => Self::On,
"off" => Self::Off,
"commit" => Self::Commit,
"commit_schemes_quota_goals" => Self::CommitSchemesQuotaGoals,
"update_schemes_stats" => Self::UpdateSchemesStats,
"update_schemes_tried_bytes" => Self::UpdateSchemesTriedBytes,
"update_schemes_tried_regions" => Self::UpdateSchemesTriedRegions,
"clear_schemes_tried_regions" => Self::ClearSchemesTriedRegions,
"update_schemes_effective_quotas" => Self::UpdateSchemesEffectiveQuotas,
"update_tuned_intervals" => Self::UpdateTunedIntervals,
other => Self::Unknown(other.into()),
})
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum KdamondState {
On,
Off,
Unknown(Box<str>),
}
impl KdamondState {
#[must_use]
pub fn kernel_name(&self) -> &str {
match self {
Self::On => "on",
Self::Off => "off",
Self::Unknown(state) => state,
}
}
}
impl fmt::Display for KdamondState {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.kernel_name())
}
}
impl FromStr for KdamondState {
type Err = Error;
fn from_str(value: &str) -> Result<Self> {
validate_token("kdamond state", value)?;
Ok(match value {
"on" => Self::On,
"off" => Self::Off,
other => Self::Unknown(other.into()),
})
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum Action {
WillNeed,
Cold,
PageOut,
HugePage,
NoHugePage,
Collapse,
LruPrioritize,
LruDeprioritize,
MigrateHot,
MigrateCold,
DamosAllocate,
DamosFree,
Stat,
Unknown(Box<str>),
}
impl Action {
#[must_use]
pub fn kernel_name(&self) -> &str {
match self {
Self::WillNeed => "willneed",
Self::Cold => "cold",
Self::PageOut => "pageout",
Self::HugePage => "hugepage",
Self::NoHugePage => "nohugepage",
Self::Collapse => "collapse",
Self::LruPrioritize => "lru_prio",
Self::LruDeprioritize => "lru_deprio",
Self::MigrateHot => "migrate_hot",
Self::MigrateCold => "migrate_cold",
Self::DamosAllocate => "damos_alloc",
Self::DamosFree => "damos_free",
Self::Stat => "stat",
Self::Unknown(name) => name,
}
}
pub(super) fn parse(value: &str) -> Self {
match value {
"willneed" => Self::WillNeed,
"cold" => Self::Cold,
"pageout" => Self::PageOut,
"hugepage" => Self::HugePage,
"nohugepage" => Self::NoHugePage,
"collapse" => Self::Collapse,
"lru_prio" => Self::LruPrioritize,
"lru_deprio" => Self::LruDeprioritize,
"migrate_hot" => Self::MigrateHot,
"migrate_cold" => Self::MigrateCold,
"damos_alloc" => Self::DamosAllocate,
"damos_free" => Self::DamosFree,
"stat" => Self::Stat,
other => Self::Unknown(other.into()),
}
}
}
impl fmt::Display for Action {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.kernel_name())
}
}
impl FromStr for Action {
type Err = Error;
fn from_str(value: &str) -> Result<Self> {
validate_token("DAMOS action", value)?;
Ok(Self::parse(value))
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RegionSizeRange {
min: u64,
max: u64,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ByteSizeRange {
min: u64,
max: u64,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ProbeHitsWeightedSumRange {
min: u64,
max: u64,
}
impl ProbeHitsWeightedSumRange {
pub const fn new(min: u64, max: u64) -> Result<Self> {
if min > max {
return Err(Error::InvalidConfiguration {
field: "probe hits weighted sum range",
reason: "minimum must not exceed maximum",
});
}
Ok(Self { min, max })
}
#[must_use]
pub const fn min(self) -> u64 {
self.min
}
#[must_use]
pub const fn max(self) -> u64 {
self.max
}
}
impl ByteSizeRange {
pub const fn new(min: u64, max: u64) -> Result<Self> {
if min > max {
return Err(Error::InvalidConfiguration {
field: "byte size range",
reason: "minimum must not exceed maximum",
});
}
Ok(Self { min, max })
}
#[must_use]
pub const fn min(self) -> u64 {
self.min
}
#[must_use]
pub const fn max(self) -> u64 {
self.max
}
}
impl RegionSizeRange {
pub const fn new(min: u64, max: u64) -> Result<Self> {
if min > max {
return Err(Error::InvalidConfiguration {
field: "region size range",
reason: "minimum must not exceed maximum",
});
}
Ok(Self { min, max })
}
#[must_use]
pub const fn min(self) -> u64 {
self.min
}
#[must_use]
pub const fn max(self) -> u64 {
self.max
}
pub const fn min_bytes(self, address_unit: AddressUnit) -> Result<u64> {
address_unit.to_bytes(self.min)
}
pub const fn max_bytes(self, address_unit: AddressUnit) -> Result<u64> {
address_unit.to_bytes(self.max)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AccessCountRange {
min: u32,
max: u32,
}
impl AccessCountRange {
pub const fn new(min: u32, max: u32) -> Result<Self> {
if min > max {
return Err(Error::InvalidConfiguration {
field: "access count range",
reason: "minimum must not exceed maximum",
});
}
Ok(Self { min, max })
}
#[must_use]
pub const fn min(self) -> u32 {
self.min
}
#[must_use]
pub const fn max(self) -> u32 {
self.max
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AgeRange {
min: u32,
max: u32,
}
impl AgeRange {
pub const fn new(min: u32, max: u32) -> Result<Self> {
if min > max {
return Err(Error::InvalidConfiguration {
field: "age range",
reason: "minimum must not exceed maximum",
});
}
Ok(Self { min, max })
}
#[must_use]
pub const fn min(self) -> u32 {
self.min
}
#[must_use]
pub const fn max(self) -> u32 {
self.max
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AccessPattern {
size: RegionSizeRange,
accesses: AccessCountRange,
age: AgeRange,
}
impl AccessPattern {
#[must_use]
pub const fn new(size: RegionSizeRange, accesses: AccessCountRange, age: AgeRange) -> Self {
Self {
size,
accesses,
age,
}
}
#[must_use]
pub const fn size(self) -> RegionSizeRange {
self.size
}
#[must_use]
pub const fn accesses(self) -> AccessCountRange {
self.accesses
}
#[must_use]
pub const fn age(self) -> AgeRange {
self.age
}
pub(super) fn equivalent_after_kernel_normalization(self, observed: Self) -> bool {
if self == observed {
return true;
}
self.size.min == observed.size.min
&& self.size.max == u64::MAX
&& observed.size.max == u64::from(u32::MAX)
&& self.accesses == observed.accesses
&& self.age == observed.age
}
pub(super) fn normalize_kernel_width(&mut self, observed: Self) {
if self.equivalent_after_kernel_normalization(observed) {
self.size = observed.size;
}
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum ProbeFilterType {
Anonymous,
MemoryControlGroup,
PageIdleUnset,
PageIdleSet,
Unknown(Box<str>),
}
impl ProbeFilterType {
#[must_use]
pub fn kernel_name(&self) -> &str {
match self {
Self::Anonymous => "anon",
Self::MemoryControlGroup => "memcg",
Self::PageIdleUnset => "pgidle_unset",
Self::PageIdleSet => "pgidle_set",
Self::Unknown(name) => name,
}
}
pub(super) fn parse(value: &str) -> Self {
match value {
"anon" => Self::Anonymous,
"memcg" => Self::MemoryControlGroup,
"pgidle_unset" => Self::PageIdleUnset,
"pgidle_set" => Self::PageIdleSet,
other => Self::Unknown(other.into()),
}
}
}
impl fmt::Display for ProbeFilterType {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.kernel_name())
}
}
impl FromStr for ProbeFilterType {
type Err = Error;
fn from_str(value: &str) -> Result<Self> {
validate_token("monitoring probe filter type", value)?;
Ok(Self::parse(value))
}
}