1
0
mirror of https://github.com/containers/youki synced 2024-11-23 17:32:15 +01:00

Move dbus code to libcgroups create

This commit is contained in:
Furisto 2021-10-25 20:16:56 +02:00
parent a3ce77064d
commit d018a24814
11 changed files with 70 additions and 55 deletions

@ -189,7 +189,7 @@ pub fn create_cgroup_manager<P: Into<PathBuf>>(
bail!("systemd cgroup flag passed, but systemd support for managing cgroups is not available"); bail!("systemd cgroup flag passed, but systemd support for managing cgroups is not available");
} }
log::info!("systemd cgroup manager will be used"); log::info!("systemd cgroup manager will be used");
return Ok(Box::new(v2::SystemDCGroupManager::new( return Ok(Box::new(crate::systemd::manager::Manager::new(
DEFAULT_CGROUP_ROOT.into(), DEFAULT_CGROUP_ROOT.into(),
cgroup_path.into(), cgroup_path.into(),
)?)); )?));

@ -8,6 +8,7 @@ mod test;
pub mod common; pub mod common;
pub mod stats; pub mod stats;
pub mod systemd;
pub mod test_manager; pub mod test_manager;
pub mod v1; pub mod v1;
pub mod v2; pub mod v2;

@ -0,0 +1,39 @@
use std::fmt::Display;
pub enum ControllerType {
Cpu,
Io,
Memory,
Tasks,
}
impl Display for ControllerType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let print = match self {
ControllerType::Cpu => "cpu",
ControllerType::Io => "io",
ControllerType::Memory => "memory",
ControllerType::Tasks => "tasks",
};
write!(f, "{}", print)
}
}
impl AsRef<str> for ControllerType {
fn as_ref(&self) -> &str {
match self {
ControllerType::Cpu => "cpu",
ControllerType::Io => "io",
ControllerType::Memory => "memory",
ControllerType::Tasks => "tasks",
}
}
}
pub const CONTROLLER_TYPES: &[ControllerType] = &[
ControllerType::Cpu,
ControllerType::Io,
ControllerType::Memory,
ControllerType::Tasks,
];

@ -1,6 +1,6 @@
use crate::dbus::systemd_manager::OrgFreedesktopSystemd1Manager; use crate::systemd::dbus::systemd_api::OrgFreedesktopSystemd1Manager;
use anyhow::Result; use anyhow::Result;
use dbus::arg::{RefArg, Variant, PropMap}; use dbus::arg::{PropMap, Variant};
use dbus::blocking::Connection; use dbus::blocking::Connection;
use std::time::Duration; use std::time::Duration;
@ -62,7 +62,7 @@ impl Client {
let mut properties = vec![]; let mut properties = vec![];
for (name, variant) in &mut properties_map.iter() { for (name, variant) in &mut properties_map.iter() {
let s : &str = &*name; let s: &str = &*name;
properties.push((s, variant.to_owned())); properties.push((s, variant.to_owned()));
} }
//proxy.start_transient_unit(unit_name, "replace", properties, vec![])?; //proxy.start_transient_unit(unit_name, "replace", properties, vec![])?;

@ -0,0 +1,2 @@
pub(crate) mod client;
pub(crate) mod systemd_api;

@ -8,12 +8,9 @@ use anyhow::{anyhow, bail, Result};
use nix::unistd::Pid; use nix::unistd::Pid;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use super::controller_type::{ControllerType, CONTROLLER_TYPES};
#[cfg(feature = "cgroupsv2_devices")] #[cfg(feature = "cgroupsv2_devices")]
use super::devices::Devices; use super::devices::Devices;
use super::{
controller::Controller, controller_type::ControllerType, cpu::Cpu, cpuset::CpuSet,
freezer::Freezer, hugetlb::HugeTlb, io::Io, memory::Memory, pids::Pids,
};
use crate::common::{self, CgroupManager, ControllerOpt, FreezerState, PathBufExt}; use crate::common::{self, CgroupManager, ControllerOpt, FreezerState, PathBufExt};
use crate::stats::Stats; use crate::stats::Stats;
@ -21,16 +18,8 @@ const CGROUP_PROCS: &str = "cgroup.procs";
const CGROUP_CONTROLLERS: &str = "cgroup.controllers"; const CGROUP_CONTROLLERS: &str = "cgroup.controllers";
const CGROUP_SUBTREE_CONTROL: &str = "cgroup.subtree_control"; const CGROUP_SUBTREE_CONTROL: &str = "cgroup.subtree_control";
// v2 systemd only supports cpu, io, memory and pids.
const CONTROLLER_TYPES: &[ControllerType] = &[
ControllerType::Cpu,
ControllerType::Io,
ControllerType::Memory,
ControllerType::Pids,
];
/// SystemDCGroupManager is a driver for managing cgroups via systemd. /// SystemDCGroupManager is a driver for managing cgroups via systemd.
pub struct SystemDCGroupManager { pub struct Manager {
root_path: PathBuf, root_path: PathBuf,
cgroups_path: PathBuf, cgroups_path: PathBuf,
full_path: PathBuf, full_path: PathBuf,
@ -46,14 +35,14 @@ struct CgroupsPath {
name: String, name: String,
} }
impl SystemDCGroupManager { impl Manager {
pub fn new(root_path: PathBuf, cgroups_path: PathBuf) -> Result<Self> { pub fn new(root_path: PathBuf, cgroups_path: PathBuf) -> Result<Self> {
// TODO: create the systemd unit using a dbus client. // TODO: create the systemd unit using a dbus client.
let destructured_path = Self::destructure_cgroups_path(cgroups_path)?; let destructured_path = Self::destructure_cgroups_path(cgroups_path)?;
let cgroups_path = Self::construct_cgroups_path(destructured_path)?; let cgroups_path = Self::construct_cgroups_path(destructured_path)?;
let full_path = root_path.join_safely(&cgroups_path)?; let full_path = root_path.join_safely(&cgroups_path)?;
Ok(SystemDCGroupManager { Ok(Manager {
root_path, root_path,
cgroups_path, cgroups_path,
full_path, full_path,
@ -203,7 +192,7 @@ impl SystemDCGroupManager {
"cpu" => controllers.push(ControllerType::Cpu), "cpu" => controllers.push(ControllerType::Cpu),
"io" => controllers.push(ControllerType::Io), "io" => controllers.push(ControllerType::Io),
"memory" => controllers.push(ControllerType::Memory), "memory" => controllers.push(ControllerType::Memory),
"pids" => controllers.push(ControllerType::Pids), "pids" => controllers.push(ControllerType::Tasks),
_ => continue, _ => continue,
} }
} }
@ -220,7 +209,7 @@ impl SystemDCGroupManager {
} }
} }
impl CgroupManager for SystemDCGroupManager { impl CgroupManager for Manager {
fn add_task(&self, pid: Pid) -> Result<()> { fn add_task(&self, pid: Pid) -> Result<()> {
// Dont attach any pid to the cgroup if -1 is specified as a pid // Dont attach any pid to the cgroup if -1 is specified as a pid
if pid.as_raw() == -1 { if pid.as_raw() == -1 {
@ -234,12 +223,7 @@ impl CgroupManager for SystemDCGroupManager {
fn apply(&self, controller_opt: &ControllerOpt) -> Result<()> { fn apply(&self, controller_opt: &ControllerOpt) -> Result<()> {
for controller in CONTROLLER_TYPES { for controller in CONTROLLER_TYPES {
match controller { match controller {
ControllerType::Cpu => Cpu::apply(controller_opt, &self.full_path)?, _ => {}
ControllerType::CpuSet => CpuSet::apply(controller_opt, &self.full_path)?,
ControllerType::HugeTlb => HugeTlb::apply(controller_opt, &self.full_path)?,
ControllerType::Io => Io::apply(controller_opt, &self.full_path)?,
ControllerType::Memory => Memory::apply(controller_opt, &self.full_path)?,
ControllerType::Pids => Pids::apply(controller_opt, &self.full_path)?,
} }
} }
@ -253,13 +237,7 @@ impl CgroupManager for SystemDCGroupManager {
} }
fn freeze(&self, state: FreezerState) -> Result<()> { fn freeze(&self, state: FreezerState) -> Result<()> {
let controller_opt = ControllerOpt { todo!();
resources: &Default::default(),
freezer_state: Some(state),
oom_score_adj: None,
disable_oom_killer: false,
};
Freezer::apply(&controller_opt, &self.full_path)
} }
fn stats(&self) -> Result<Stats> { fn stats(&self) -> Result<Stats> {
@ -278,7 +256,7 @@ mod tests {
#[test] #[test]
fn expand_slice_works() -> Result<()> { fn expand_slice_works() -> Result<()> {
assert_eq!( assert_eq!(
SystemDCGroupManager::expand_slice("test-a-b.slice")?, Manager::expand_slice("test-a-b.slice")?,
PathBuf::from("/test.slice/test-a.slice/test-a-b.slice"), PathBuf::from("/test.slice/test-a.slice/test-a-b.slice"),
); );
@ -287,13 +265,12 @@ mod tests {
#[test] #[test]
fn get_cgroups_path_works_with_a_complex_slice() -> Result<()> { fn get_cgroups_path_works_with_a_complex_slice() -> Result<()> {
let cgroups_path = SystemDCGroupManager::destructure_cgroups_path(PathBuf::from( let cgroups_path =
"test-a-b.slice:docker:foo", Manager::destructure_cgroups_path(PathBuf::from("test-a-b.slice:docker:foo"))
)) .expect("");
.expect("");
assert_eq!( assert_eq!(
SystemDCGroupManager::construct_cgroups_path(cgroups_path)?, Manager::construct_cgroups_path(cgroups_path)?,
PathBuf::from("/test.slice/test-a.slice/test-a-b.slice/docker-foo.scope"), PathBuf::from("/test.slice/test-a.slice/test-a-b.slice/docker-foo.scope"),
); );
@ -302,13 +279,11 @@ mod tests {
#[test] #[test]
fn get_cgroups_path_works_with_a_simple_slice() -> Result<()> { fn get_cgroups_path_works_with_a_simple_slice() -> Result<()> {
let cgroups_path = SystemDCGroupManager::destructure_cgroups_path(PathBuf::from( let cgroups_path =
"machine.slice:libpod:foo", Manager::destructure_cgroups_path(PathBuf::from("machine.slice:libpod:foo")).expect("");
))
.expect("");
assert_eq!( assert_eq!(
SystemDCGroupManager::construct_cgroups_path(cgroups_path)?, Manager::construct_cgroups_path(cgroups_path)?,
PathBuf::from("/machine.slice/libpod-foo.scope"), PathBuf::from("/machine.slice/libpod-foo.scope"),
); );
@ -318,10 +293,10 @@ mod tests {
#[test] #[test]
fn get_cgroups_path_works_with_scope() -> Result<()> { fn get_cgroups_path_works_with_scope() -> Result<()> {
let cgroups_path = let cgroups_path =
SystemDCGroupManager::destructure_cgroups_path(PathBuf::from(":docker:foo")).expect(""); Manager::destructure_cgroups_path(PathBuf::from(":docker:foo")).expect("");
assert_eq!( assert_eq!(
SystemDCGroupManager::construct_cgroups_path(cgroups_path)?, Manager::construct_cgroups_path(cgroups_path)?,
PathBuf::from("/machine.slice/docker-foo.scope"), PathBuf::from("/machine.slice/docker-foo.scope"),
); );

@ -0,0 +1,3 @@
pub mod controller_type;
mod dbus;
pub mod manager;

@ -2,15 +2,13 @@ mod controller;
pub mod controller_type; pub mod controller_type;
mod cpu; mod cpu;
mod cpuset; mod cpuset;
#[cfg(feature = "cgroupsv2_devices")]
pub mod devices;
mod freezer; mod freezer;
mod hugetlb; mod hugetlb;
mod io; mod io;
pub mod manager; pub mod manager;
mod memory; mod memory;
mod pids; mod pids;
pub mod systemd_manager;
mod unified; mod unified;
pub mod util; pub mod util;
pub use systemd_manager::SystemDCGroupManager;
#[cfg(feature = "cgroupsv2_devices")]
pub mod devices;

@ -1,2 +0,0 @@
pub mod client;
mod systemd_manager;

@ -1,7 +1,6 @@
pub mod apparmor; pub mod apparmor;
pub mod capabilities; pub mod capabilities;
pub mod container; pub mod container;
pub mod dbus;
pub mod hooks; pub mod hooks;
pub mod namespaces; pub mod namespaces;
pub mod notify_socket; pub mod notify_socket;