-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for cgroups managed by systemd
- Loading branch information
Showing
16 changed files
with
152 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
use crate::{ | ||
command::Command, | ||
}; | ||
use crate::command::Command; | ||
use caps::*; | ||
|
||
use anyhow::Result; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use crate::cgroups::Manager; | ||
use std::{collections::HashMap, path::PathBuf}; | ||
use std::{fs::remove_dir, path::Path}; | ||
|
||
use anyhow::Result; | ||
use nix::unistd::Pid; | ||
use procfs::process::Process; | ||
|
||
use super::{ | ||
blkio::Blkio, devices::Devices, hugetlb::Hugetlb, memory::Memory, | ||
network_classifier::NetworkClassifier, network_priority::NetworkPriority, pids::Pids, | ||
Controller, | ||
}; | ||
use crate::{cgroups::ControllerType, utils::PathBufExt}; | ||
use oci_spec::LinuxResources; | ||
|
||
const CONTROLLERS: &[ControllerType] = &[ | ||
ControllerType::Devices, | ||
ControllerType::HugeTlb, | ||
ControllerType::Memory, | ||
ControllerType::Pids, | ||
ControllerType::Blkio, | ||
ControllerType::NetworkPriority, | ||
ControllerType::NetworkClassifier, | ||
]; | ||
|
||
pub struct CGroupsFSManager { | ||
subsystems: HashMap<String, PathBuf>, | ||
} | ||
|
||
impl CGroupsFSManager { | ||
pub fn new(cgroup_path: PathBuf) -> Result<Self> { | ||
let mut subsystems = HashMap::<String, PathBuf>::new(); | ||
for subsystem in CONTROLLERS.iter().map(|c| c.to_string()) { | ||
subsystems.insert( | ||
subsystem.to_owned(), | ||
Self::get_subsystem_path(&cgroup_path, &subsystem)?, | ||
); | ||
} | ||
|
||
Ok(CGroupsFSManager { subsystems }) | ||
} | ||
|
||
fn get_subsystem_path(cgroup_path: &Path, subsystem: &str) -> anyhow::Result<PathBuf> { | ||
log::debug!("Get path for subsystem: {}", subsystem); | ||
let mount = Process::myself()? | ||
.mountinfo()? | ||
.into_iter() | ||
.find(|m| { | ||
if m.fs_type == "cgroup" { | ||
// Some systems mount net_prio and net_cls in the same directory | ||
// other systems mount them in their own diretories. This | ||
// should handle both cases. | ||
if subsystem == "net_cls" || subsystem == "net_prio" { | ||
return m.mount_point.ends_with("net_cls,net_prio") | ||
|| m.mount_point.ends_with("net_prio,net_cls"); | ||
} | ||
} | ||
m.mount_point.ends_with(subsystem) | ||
}) | ||
.unwrap(); | ||
|
||
let cgroup = Process::myself()? | ||
.cgroups()? | ||
.into_iter() | ||
.find(|c| c.controllers.contains(&subsystem.to_owned())) | ||
.unwrap(); | ||
|
||
let p = if cgroup_path.to_string_lossy().into_owned().is_empty() { | ||
mount | ||
.mount_point | ||
.join_absolute_path(Path::new(&cgroup.pathname))? | ||
} else { | ||
mount.mount_point.join_absolute_path(&cgroup_path)? | ||
}; | ||
|
||
Ok(p) | ||
} | ||
} | ||
|
||
impl Manager for CGroupsFSManager { | ||
fn apply(&self, linux_resources: &LinuxResources, pid: Pid) -> Result<()> { | ||
for subsys in &self.subsystems { | ||
match subsys.0.as_str() { | ||
"devices" => Devices::apply(linux_resources, &subsys.1, pid)?, | ||
"hugetlb" => Hugetlb::apply(linux_resources, &subsys.1, pid)?, | ||
"memory" => Memory::apply(linux_resources, &subsys.1, pid)?, | ||
"pids" => Pids::apply(linux_resources, &subsys.1, pid)?, | ||
"blkio" => Blkio::apply(linux_resources, &subsys.1, pid)?, | ||
"net_prio" => NetworkPriority::apply(linux_resources, &subsys.1, pid)?, | ||
"net_cls" => NetworkClassifier::apply(linux_resources, &subsys.1, pid)?, | ||
_ => continue, | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn remove(&self) -> Result<()> { | ||
for cgroup_path in &self.subsystems { | ||
if cgroup_path.1.exists() { | ||
log::debug!("remove cgroup {:?}", cgroup_path.1); | ||
remove_dir(&cgroup_path.1)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,11 @@ | ||
use std::{collections::HashMap, path::PathBuf}; | ||
use std::{fs::remove_dir, path::Path}; | ||
|
||
use anyhow::Result; | ||
use nix::unistd::Pid; | ||
use procfs::process::Process; | ||
|
||
use crate::{cgroups::ControllerType, utils::PathBufExt}; | ||
use oci_spec::LinuxResources; | ||
use super::{ | ||
blkio::Blkio, devices::Devices, hugetlb::Hugetlb, memory::Memory, | ||
network_classifier::NetworkClassifier, network_priority::NetworkPriority, pids::Pids, | ||
Controller, | ||
}; | ||
|
||
const CONTROLLERS: &[ControllerType] = &[ | ||
ControllerType::Devices, | ||
ControllerType::HugeTlb, | ||
ControllerType::Memory, | ||
ControllerType::Pids, | ||
ControllerType::Blkio, | ||
ControllerType::NetworkPriority, | ||
ControllerType::NetworkClassifier, | ||
]; | ||
|
||
pub struct Manager { | ||
subsystems: HashMap<String, PathBuf>, | ||
} | ||
|
||
impl Manager { | ||
pub fn new(cgroup_path: PathBuf) -> Result<Self> { | ||
let mut subsystems = HashMap::<String, PathBuf>::new(); | ||
for subsystem in CONTROLLERS.iter().map(|c| c.to_string()) { | ||
subsystems.insert( | ||
subsystem.to_owned(), | ||
Self::get_subsystem_path(&cgroup_path, &subsystem)?, | ||
); | ||
} | ||
|
||
Ok(Manager { subsystems }) | ||
} | ||
|
||
pub fn apply(&self, linux_resources: &LinuxResources, pid: Pid) -> Result<()> { | ||
for subsys in &self.subsystems { | ||
match subsys.0.as_str() { | ||
"devices" => Devices::apply(linux_resources, &subsys.1, pid)?, | ||
"hugetlb" => Hugetlb::apply(linux_resources, &subsys.1, pid)?, | ||
"memory" => Memory::apply(linux_resources, &subsys.1, pid)?, | ||
"pids" => Pids::apply(linux_resources, &subsys.1, pid)?, | ||
"blkio" => Blkio::apply(linux_resources, &subsys.1, pid)?, | ||
"net_prio" => NetworkPriority::apply(linux_resources, &subsys.1, pid)?, | ||
"net_cls" => NetworkClassifier::apply(linux_resources, &subsys.1, pid)?, | ||
_ => continue, | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn remove(&self) -> Result<()> { | ||
for cgroup_path in &self.subsystems { | ||
if cgroup_path.1.exists() { | ||
log::debug!("remove cgroup {:?}", cgroup_path.1); | ||
remove_dir(&cgroup_path.1)?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn get_subsystem_path(cgroup_path: &Path, subsystem: &str) -> anyhow::Result<PathBuf> { | ||
log::debug!("Get path for subsystem: {}", subsystem); | ||
let mount = Process::myself()? | ||
.mountinfo()? | ||
.into_iter() | ||
.find(|m| { | ||
if m.fs_type == "cgroup" { | ||
// Some systems mount net_prio and net_cls in the same directory | ||
// other systems mount them in their own diretories. This | ||
// should handle both cases. | ||
if subsystem == "net_cls" || subsystem == "net_prio" { | ||
return m.mount_point.ends_with("net_cls,net_prio") | ||
|| m.mount_point.ends_with("net_prio,net_cls"); | ||
} | ||
} | ||
m.mount_point.ends_with(subsystem) | ||
}) | ||
.unwrap(); | ||
|
||
let cgroup = Process::myself()? | ||
.cgroups()? | ||
.into_iter() | ||
.find(|c| c.controllers.contains(&subsystem.to_owned())) | ||
.unwrap(); | ||
|
||
let p = if cgroup_path.to_string_lossy().into_owned().is_empty() { | ||
mount | ||
.mount_point | ||
.join_absolute_path(Path::new(&cgroup.pathname))? | ||
} else { | ||
mount.mount_point.join_absolute_path(&cgroup_path)? | ||
}; | ||
|
||
Ok(p) | ||
} | ||
pub trait Manager { | ||
fn apply(&self, linux_resources: &LinuxResources, pid: Pid) -> Result<()>; | ||
fn remove(&self) -> Result<()>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
mod blkio; | ||
mod cgroupsfs_manager; | ||
mod controller; | ||
mod controller_type; | ||
mod devices; | ||
mod hugetlb; | ||
mod blkio; | ||
mod manager; | ||
mod memory; | ||
mod network_classifier; | ||
mod network_priority; | ||
mod pids; | ||
mod systemd_manager; | ||
pub use cgroupsfs_manager::CGroupsFSManager; | ||
pub use controller::Controller; | ||
pub use controller_type::ControllerType; | ||
pub use manager::Manager; | ||
pub use systemd_manager::SystemDCGroupManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::cgroups::Manager; | ||
use anyhow::Result; | ||
use nix::unistd::Pid; | ||
use oci_spec::LinuxResources; | ||
|
||
pub struct SystemDCGroupManager; | ||
|
||
impl Manager for SystemDCGroupManager { | ||
fn apply(&self, linux_resources: &LinuxResources, pid: Pid) -> Result<()> { | ||
Ok(()) | ||
} | ||
|
||
fn remove(&self) -> Result<()> { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.