From 102e10fc397dbf223c876c510bc02899a044fcff Mon Sep 17 00:00:00 2001 From: Yutaka Juba Date: Wed, 16 Jun 2021 22:29:41 +0900 Subject: [PATCH 1/2] Add support for cpuacct in cgroup v1. --- src/cgroups/v1/controller_type.rs | 3 ++ src/cgroups/v1/cpuacct.rs | 57 +++++++++++++++++++++++++++++++ src/cgroups/v1/manager.rs | 5 +-- src/cgroups/v1/mod.rs | 1 + src/cgroups/v1/util.rs | 6 +++- 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/cgroups/v1/cpuacct.rs diff --git a/src/cgroups/v1/controller_type.rs b/src/cgroups/v1/controller_type.rs index 722d1b545..68cb25572 100644 --- a/src/cgroups/v1/controller_type.rs +++ b/src/cgroups/v1/controller_type.rs @@ -2,6 +2,7 @@ use std::fmt::Display; pub enum ControllerType { Cpu, + CpuAcct, CpuSet, Devices, HugeTlb, @@ -16,6 +17,7 @@ impl Display for ControllerType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let print = match *self { Self::Cpu => "cpu", + Self::CpuAcct => "cpuacct", Self::CpuSet => "cpuset", Self::Devices => "devices", Self::HugeTlb => "hugetlb", @@ -32,6 +34,7 @@ impl Display for ControllerType { pub const CONTROLLERS: &[ControllerType] = &[ ControllerType::Cpu, + ControllerType::CpuAcct, ControllerType::CpuSet, ControllerType::Devices, ControllerType::HugeTlb, diff --git a/src/cgroups/v1/cpuacct.rs b/src/cgroups/v1/cpuacct.rs new file mode 100644 index 000000000..e15093e47 --- /dev/null +++ b/src/cgroups/v1/cpuacct.rs @@ -0,0 +1,57 @@ +use std::{fs, path::Path}; + +use anyhow::Result; +use nix::unistd::Pid; +use oci_spec::LinuxResources; + +use crate::cgroups::common::{self, CGROUP_PROCS}; + +use super::Controller; + +const CGROUP_CPUACCT_TASKS: &str = "tasks"; + +pub struct CpuAcct {} + +impl Controller for CpuAcct { + fn apply(_linux_resources: &LinuxResources, cgroup_path: &Path, pid: Pid) -> Result<()> { + log::debug!("Apply cpuacct cgroup config"); + fs::create_dir_all(cgroup_path)?; + + Self::apply(cgroup_path, pid)?; + + common::write_cgroup_file(cgroup_path.join(CGROUP_PROCS), pid)?; + Ok(()) + } +} + +impl CpuAcct { + fn apply(root_path: &Path, pid: Pid) -> Result<()> { + common::write_cgroup_file_str(root_path.join(CGROUP_CPUACCT_TASKS), &pid.to_string())?; + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use std::fs; + + use super::*; + use crate::cgroups::test::setup; + use nix::unistd::Pid; + + #[test] + fn test_set_cpuacct() { + // arrange + let (tmp, cpuacct) = setup("test_set_cpuacct", CGROUP_CPUACCT_TASKS); + let pid = Pid::from_raw(1000); + + // act + CpuAcct::apply(&tmp, pid).expect("apply cpuacct"); + + // assert + let content = fs::read_to_string(&cpuacct) + .unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPUACCT_TASKS)); + assert_eq!(content, "1000"); + } +} diff --git a/src/cgroups/v1/manager.rs b/src/cgroups/v1/manager.rs index 886b065d0..fd5d7a2ea 100644 --- a/src/cgroups/v1/manager.rs +++ b/src/cgroups/v1/manager.rs @@ -8,8 +8,8 @@ use nix::unistd::Pid; use procfs::process::Process; use super::{ - blkio::Blkio, controller_type::CONTROLLERS, cpu::Cpu, cpuset::CpuSet, devices::Devices, - hugetlb::Hugetlb, memory::Memory, network_classifier::NetworkClassifier, + blkio::Blkio, controller_type::CONTROLLERS, cpu::Cpu, cpuacct::CpuAcct, cpuset::CpuSet, + devices::Devices, hugetlb::Hugetlb, memory::Memory, network_classifier::NetworkClassifier, network_priority::NetworkPriority, pids::Pids, util, Controller, }; @@ -61,6 +61,7 @@ impl CgroupManager for Manager { for subsys in &self.subsystems { match subsys.0.as_str() { "cpu" => Cpu::apply(linux_resources, &subsys.1, pid)?, + "cpuacct" => CpuAcct::apply(linux_resources, &subsys.1, pid)?, "cpuset" => CpuSet::apply(linux_resources, &subsys.1, pid)?, "devices" => Devices::apply(linux_resources, &subsys.1, pid)?, "hugetlb" => Hugetlb::apply(linux_resources, &subsys.1, pid)?, diff --git a/src/cgroups/v1/mod.rs b/src/cgroups/v1/mod.rs index 7d9b75b0f..9816dc9f5 100644 --- a/src/cgroups/v1/mod.rs +++ b/src/cgroups/v1/mod.rs @@ -2,6 +2,7 @@ mod blkio; mod controller; mod controller_type; mod cpu; +mod cpuacct; mod cpuset; mod devices; mod hugetlb; diff --git a/src/cgroups/v1/util.rs b/src/cgroups/v1/util.rs index 76236c69f..7a31e28bf 100644 --- a/src/cgroups/v1/util.rs +++ b/src/cgroups/v1/util.rs @@ -40,9 +40,13 @@ pub fn get_subsystem_mount_points(subsystem: &str) -> Result { return m.mount_point.ends_with("cpu,cpuacct") || m.mount_point.ends_with("cpu"); } + if subsystem == "cpuacct" { + return m.mount_point.ends_with("cpu,cpuacct") + || m.mount_point.ends_with("cpuacct"); + } } m.mount_point.ends_with(&subsystem) }) .map(|m| m.mount_point) .ok_or_else(|| anyhow!("could not find mountpoint for {}", subsystem)) -} \ No newline at end of file +} From b3b7d95bd48ac4eabe4211b8144e511406d110dd Mon Sep 17 00:00:00 2001 From: Yutaka Juba Date: Thu, 17 Jun 2021 18:26:04 +0900 Subject: [PATCH 2/2] Remove unnecessary processing. --- src/cgroups/v1/cpuacct.rs | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/src/cgroups/v1/cpuacct.rs b/src/cgroups/v1/cpuacct.rs index e15093e47..3060aebcf 100644 --- a/src/cgroups/v1/cpuacct.rs +++ b/src/cgroups/v1/cpuacct.rs @@ -8,8 +8,6 @@ use crate::cgroups::common::{self, CGROUP_PROCS}; use super::Controller; -const CGROUP_CPUACCT_TASKS: &str = "tasks"; - pub struct CpuAcct {} impl Controller for CpuAcct { @@ -17,41 +15,7 @@ impl Controller for CpuAcct { log::debug!("Apply cpuacct cgroup config"); fs::create_dir_all(cgroup_path)?; - Self::apply(cgroup_path, pid)?; - common::write_cgroup_file(cgroup_path.join(CGROUP_PROCS), pid)?; Ok(()) } } - -impl CpuAcct { - fn apply(root_path: &Path, pid: Pid) -> Result<()> { - common::write_cgroup_file_str(root_path.join(CGROUP_CPUACCT_TASKS), &pid.to_string())?; - - Ok(()) - } -} - -#[cfg(test)] -mod tests { - use std::fs; - - use super::*; - use crate::cgroups::test::setup; - use nix::unistd::Pid; - - #[test] - fn test_set_cpuacct() { - // arrange - let (tmp, cpuacct) = setup("test_set_cpuacct", CGROUP_CPUACCT_TASKS); - let pid = Pid::from_raw(1000); - - // act - CpuAcct::apply(&tmp, pid).expect("apply cpuacct"); - - // assert - let content = fs::read_to_string(&cpuacct) - .unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPUACCT_TASKS)); - assert_eq!(content, "1000"); - } -}