diff --git a/.gitignore b/.gitignore index d032273e6..2713058a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ youki /tutorial +.idea/ **/target .vagrant/ diff --git a/src/cgroups/v1/controller_type.rs b/src/cgroups/v1/controller_type.rs index 6fbf0f37f..f85a39169 100644 --- a/src/cgroups/v1/controller_type.rs +++ b/src/cgroups/v1/controller_type.rs @@ -8,6 +8,7 @@ pub enum ControllerType { Devices, HugeTlb, Pids, + PerfEvent, Memory, Blkio, NetworkPriority, @@ -24,6 +25,7 @@ impl Display for ControllerType { Self::Devices => "devices", Self::HugeTlb => "hugetlb", Self::Pids => "pids", + Self::PerfEvent => "perf_event", Self::Memory => "memory", Self::Blkio => "blkio", Self::NetworkPriority => "net_prio", @@ -43,6 +45,7 @@ pub const CONTROLLERS: &[ControllerType] = &[ ControllerType::HugeTlb, ControllerType::Memory, ControllerType::Pids, + ControllerType::PerfEvent, ControllerType::Blkio, ControllerType::NetworkPriority, ControllerType::NetworkClassifier, diff --git a/src/cgroups/v1/manager.rs b/src/cgroups/v1/manager.rs index 13e9cc9ad..464de53ff 100644 --- a/src/cgroups/v1/manager.rs +++ b/src/cgroups/v1/manager.rs @@ -12,8 +12,8 @@ use super::ControllerType as CtrlType; use super::{ blkio::Blkio, controller_type::CONTROLLERS, cpu::Cpu, cpuacct::CpuAcct, cpuset::CpuSet, devices::Devices, freezer::Freezer, hugetlb::Hugetlb, memory::Memory, - network_classifier::NetworkClassifier, network_priority::NetworkPriority, pids::Pids, util, - Controller, + network_classifier::NetworkClassifier, network_priority::NetworkPriority, + perf_event::PerfEvent, pids::Pids, util, Controller, }; use crate::cgroups::common::CGROUP_PROCS; @@ -75,6 +75,7 @@ impl Manager { CtrlType::HugeTlb => Hugetlb::needs_to_handle(linux_resources).is_some(), CtrlType::Memory => Memory::needs_to_handle(linux_resources).is_some(), CtrlType::Pids => Pids::needs_to_handle(linux_resources).is_some(), + CtrlType::PerfEvent => PerfEvent::needs_to_handle(linux_resources).is_some(), CtrlType::Blkio => Blkio::needs_to_handle(linux_resources).is_some(), CtrlType::NetworkPriority => { NetworkPriority::needs_to_handle(linux_resources).is_some() @@ -109,6 +110,7 @@ impl CgroupManager for Manager { CtrlType::HugeTlb => Hugetlb::add_task(pid, subsys.1)?, CtrlType::Memory => Memory::add_task(pid, subsys.1)?, CtrlType::Pids => Pids::add_task(pid, subsys.1)?, + CtrlType::PerfEvent => PerfEvent::add_task(pid, subsys.1)?, CtrlType::Blkio => Blkio::add_task(pid, subsys.1)?, CtrlType::NetworkPriority => NetworkPriority::add_task(pid, subsys.1)?, CtrlType::NetworkClassifier => NetworkClassifier::add_task(pid, subsys.1)?, @@ -129,6 +131,7 @@ impl CgroupManager for Manager { CtrlType::HugeTlb => Hugetlb::apply(linux_resources, &subsys.1)?, CtrlType::Memory => Memory::apply(linux_resources, &subsys.1)?, CtrlType::Pids => Pids::apply(linux_resources, &subsys.1)?, + CtrlType::PerfEvent => PerfEvent::apply(linux_resources, &subsys.1)?, CtrlType::Blkio => Blkio::apply(linux_resources, &subsys.1)?, CtrlType::NetworkPriority => NetworkPriority::apply(linux_resources, &subsys.1)?, CtrlType::NetworkClassifier => { diff --git a/src/cgroups/v1/mod.rs b/src/cgroups/v1/mod.rs index ff1855143..e80e9a01e 100644 --- a/src/cgroups/v1/mod.rs +++ b/src/cgroups/v1/mod.rs @@ -11,6 +11,7 @@ pub mod manager; mod memory; mod network_classifier; mod network_priority; +pub mod perf_event; mod pids; pub mod util; pub use controller::Controller; diff --git a/src/cgroups/v1/perf_event.rs b/src/cgroups/v1/perf_event.rs new file mode 100644 index 000000000..b48484be3 --- /dev/null +++ b/src/cgroups/v1/perf_event.rs @@ -0,0 +1,40 @@ +use crate::cgroups::v1::Controller; +use anyhow::Result; +use oci_spec::LinuxResources; +use std::path::Path; + +pub struct PerfEvent {} + +impl Controller for PerfEvent { + type Resource = (); + + fn apply(_linux_resources: &LinuxResources, _cgroup_root: &Path) -> Result<()> { + Ok(()) + } + //no need to handle any case + fn needs_to_handle(_linux_resources: &LinuxResources) -> Option<&Self::Resource> { + None + } +} + +#[cfg(test)] +mod tests { + use std::fs; + + use nix::unistd::Pid; + + use super::*; + use crate::cgroups::{common::CGROUP_PROCS, test::setup}; + + #[test] + fn test_add_task() { + let (tmp, procs) = setup("test_perf_event_add_task", CGROUP_PROCS); + let pid = Pid::from_raw(1000); + + PerfEvent::add_task(pid, &tmp).expect("apply perf_event"); + + let content = fs::read_to_string(&procs) + .unwrap_or_else(|_| panic!("read {} file content", CGROUP_PROCS)); + assert_eq!(content, "1000"); + } +}