diff --git a/src/cgroup/cgroup_option.rs b/src/cgroup/cgroup_option.rs new file mode 100644 index 0000000..78956e0 --- /dev/null +++ b/src/cgroup/cgroup_option.rs @@ -0,0 +1,6 @@ +use super::cpu_limit::CpuLimit; + +#[derive(Debug, Clone, Copy, Default)] +pub struct CGroupOption { + pub cpu_max: Option, +} diff --git a/src/cgroup/cpu_limit.rs b/src/cgroup/cpu_limit.rs new file mode 100644 index 0000000..4206a1c --- /dev/null +++ b/src/cgroup/cpu_limit.rs @@ -0,0 +1,49 @@ +use core::fmt; +use std::str::FromStr; + +use super::limit_value::CGroupLimitValue; + +#[derive(Debug, Clone, Copy)] +pub struct CpuLimit { + pub max: CGroupLimitValue, + pub period: u64, +} + +#[derive(Debug, PartialEq, Eq)] +pub struct ParseCpuLimitError; + +impl FromStr for CpuLimit { + type Err = ParseCpuLimitError; + + fn from_str(s: &str) -> Result { + let mut spl = s.trim().split(" "); + + let max = if let Some(max) = spl.next() { + if let Ok(max) = CGroupLimitValue::from_str(max) { + max + } else { + return Err(ParseCpuLimitError); + } + } else { + return Err(ParseCpuLimitError); + }; + + let period = if let Some(period) = spl.next() { + if let Ok(period) = u64::from_str(period) { + period + } else { + return Err(ParseCpuLimitError); + } + } else { + return Err(ParseCpuLimitError); + }; + + Ok(Self { max, period }) + } +} + +impl fmt::Display for CpuLimit { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} {}", self.max, self.period) + } +}