1
0
Fork 0
mirror of https://github.com/containers/youki synced 2024-06-10 16:56:17 +02:00

Cut down on boilerplate

This commit is contained in:
Furisto 2021-06-04 15:34:07 +02:00
parent 4af3fdf831
commit 7a09809fe8
13 changed files with 98 additions and 77 deletions

View File

@ -40,7 +40,7 @@ impl Display for Cgroup {
}
#[inline]
pub fn write_cgroup_file<P: AsRef<Path>>(path: P, data: &str) -> Result<()> {
pub fn write_cgroup_file_str<P: AsRef<Path>>(path: P, data: &str) -> Result<()> {
fs::OpenOptions::new()
.create(false)
.write(true)
@ -52,7 +52,7 @@ pub fn write_cgroup_file<P: AsRef<Path>>(path: P, data: &str) -> Result<()> {
}
#[inline]
pub fn write_cgroup_file_<P: AsRef<Path>, T: ToString>(path: P, data: T) -> Result<()> {
pub fn write_cgroup_file<P: AsRef<Path>, T: ToString>(path: P, data: T) -> Result<()> {
fs::OpenOptions::new()
.create(false)
.write(true)
@ -96,7 +96,10 @@ pub fn create_cgroup_manager<P: Into<PathBuf>>(cgroup_path: P) -> Result<Box<dyn
cgroup_path.into(),
)?))
}
_ => Ok(Box::new(v1::manager::Manager::new(cgroup_path.into())?)),
_ => {
log::info!("cgroup manager V1 will be used");
Ok(Box::new(v1::manager::Manager::new(cgroup_path.into())?))
}
}
}
_ => bail!("could not find cgroup filesystem"),

View File

@ -3,7 +3,10 @@ use std::{
path::Path,
};
use crate::cgroups::{common, v1::Controller};
use crate::cgroups::{
common::{self, CGROUP_PROCS},
v1::Controller,
};
use oci_spec::{LinuxBlockIo, LinuxResources};
const CGROUP_BLKIO_THROTTLE_READ_BPS: &str = "blkio.throttle.read_bps_device";
@ -26,7 +29,7 @@ impl Controller for Blkio {
Self::apply(cgroup_root, blkio)?;
}
common::write_cgroup_file(&cgroup_root.join("cgroup.procs"), &pid.to_string())?;
common::write_cgroup_file(cgroup_root.join(CGROUP_PROCS), pid)?;
Ok(())
}
}
@ -34,28 +37,28 @@ impl Controller for Blkio {
impl Blkio {
fn apply(root_path: &Path, blkio: &LinuxBlockIo) -> anyhow::Result<()> {
for trbd in &blkio.blkio_throttle_read_bps_device {
common::write_cgroup_file(
common::write_cgroup_file_str(
&root_path.join(CGROUP_BLKIO_THROTTLE_READ_BPS),
&format!("{}:{} {}", trbd.major, trbd.minor, trbd.rate),
)?;
}
for twbd in &blkio.blkio_throttle_write_bps_device {
common::write_cgroup_file(
common::write_cgroup_file_str(
&root_path.join(CGROUP_BLKIO_THROTTLE_WRITE_BPS),
&format!("{}:{} {}", twbd.major, twbd.minor, twbd.rate),
)?;
}
for trid in &blkio.blkio_throttle_read_iops_device {
common::write_cgroup_file(
common::write_cgroup_file_str(
&root_path.join(CGROUP_BLKIO_THROTTLE_READ_IOPS),
&format!("{}:{} {}", trid.major, trid.minor, trid.rate),
)?;
}
for twid in &blkio.blkio_throttle_write_iops_device {
common::write_cgroup_file(
common::write_cgroup_file_str(
&root_path.join(CGROUP_BLKIO_THROTTLE_WRITE_IOPS),
&format!("{}:{} {}", twid.major, twid.minor, twid.rate),
)?;

View File

@ -24,7 +24,7 @@ impl Controller for Cpu {
Self::apply(cgroup_root, cpu)?;
}
common::write_cgroup_file_(cgroup_root.join(CGROUP_PROCS), pid)?;
common::write_cgroup_file(cgroup_root.join(CGROUP_PROCS), pid)?;
Ok(())
}
}
@ -33,31 +33,31 @@ impl Cpu {
fn apply(root_path: &Path, cpu: &LinuxCpu) -> Result<()> {
if let Some(cpu_shares) = cpu.shares {
if cpu_shares != 0 {
common::write_cgroup_file_(root_path.join(CGROUP_CPU_SHARES), cpu_shares)?;
common::write_cgroup_file(root_path.join(CGROUP_CPU_SHARES), cpu_shares)?;
}
}
if let Some(cpu_period) = cpu.period {
if cpu_period != 0 {
common::write_cgroup_file_(root_path.join(CGROUP_CPU_PERIOD), cpu_period)?;
common::write_cgroup_file(root_path.join(CGROUP_CPU_PERIOD), cpu_period)?;
}
}
if let Some(cpu_quota) = cpu.quota {
if cpu_quota != 0 {
common::write_cgroup_file_(root_path.join(CGROUP_CPU_QUOTA), cpu_quota)?;
common::write_cgroup_file(root_path.join(CGROUP_CPU_QUOTA), cpu_quota)?;
}
}
if let Some(rt_runtime) = cpu.realtime_runtime {
if rt_runtime != 0 {
common::write_cgroup_file_(root_path.join(CGROUP_CPU_RT_RUNTIME), rt_runtime)?;
common::write_cgroup_file(root_path.join(CGROUP_CPU_RT_RUNTIME), rt_runtime)?;
}
}
if let Some(rt_period) = cpu.realtime_period {
if rt_period != 0 {
common::write_cgroup_file_(root_path.join(CGROUP_CPU_RT_PERIOD), rt_period)?;
common::write_cgroup_file(root_path.join(CGROUP_CPU_RT_PERIOD), rt_period)?;
}
}
@ -122,34 +122,35 @@ mod tests {
#[test]
fn test_set_rt_runtime() {
// arrange
const RUNTIME: i64 = 100000;
let (tmp, max) = setup("test_set_rt_runtime", CGROUP_CPU_RT_RUNTIME);
let cpu = LinuxCpuBuilder::new().with_realtime_runtime(RUNTIME).build();
// act
Cpu::apply(&tmp, &cpu).expect("apply cpu");
// assert
let content = fs::read_to_string(max)
.unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_RT_RUNTIME));
assert_eq!(content, RUNTIME.to_string());
// arrange
const RUNTIME: i64 = 100000;
let (tmp, max) = setup("test_set_rt_runtime", CGROUP_CPU_RT_RUNTIME);
let cpu = LinuxCpuBuilder::new()
.with_realtime_runtime(RUNTIME)
.build();
// act
Cpu::apply(&tmp, &cpu).expect("apply cpu");
// assert
let content = fs::read_to_string(max)
.unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_RT_RUNTIME));
assert_eq!(content, RUNTIME.to_string());
}
#[test]
fn test_set_rt_period() {
// arrange
const PERIOD: u64 = 100000;
let (tmp, max) = setup("test_set_rt_period", CGROUP_CPU_RT_PERIOD);
let cpu = LinuxCpuBuilder::new().with_realtime_period(PERIOD).build();
// act
Cpu::apply(&tmp, &cpu).expect("apply cpu");
// assert
let content = fs::read_to_string(max)
.unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_RT_PERIOD));
assert_eq!(content, PERIOD.to_string());
}
// arrange
const PERIOD: u64 = 100000;
let (tmp, max) = setup("test_set_rt_period", CGROUP_CPU_RT_PERIOD);
let cpu = LinuxCpuBuilder::new().with_realtime_period(PERIOD).build();
// act
Cpu::apply(&tmp, &cpu).expect("apply cpu");
// assert
let content = fs::read_to_string(max)
.unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_RT_PERIOD));
assert_eq!(content, PERIOD.to_string());
}
}

View File

@ -22,7 +22,7 @@ impl Controller for CpuSet {
Self::apply(cgroup_root, cpuset)?;
}
common::write_cgroup_file_(cgroup_root.join(CGROUP_PROCS), pid)?;
common::write_cgroup_file(cgroup_root.join(CGROUP_PROCS), pid)?;
Ok(())
}
}
@ -30,11 +30,11 @@ impl Controller for CpuSet {
impl CpuSet {
fn apply(root_path: &Path, cpuset: &LinuxCpu) -> Result<()> {
if let Some(cpus) = &cpuset.cpus {
common::write_cgroup_file(root_path.join(CGROUP_CPUSET_CPUS), cpus)?;
common::write_cgroup_file_str(root_path.join(CGROUP_CPUSET_CPUS), cpus)?;
}
if let Some(mems) = &cpuset.mems {
common::write_cgroup_file(root_path.join(CGROUP_CPUSET_MEMS), mems)?;
common::write_cgroup_file_str(root_path.join(CGROUP_CPUSET_MEMS), mems)?;
}
Ok(())

View File

@ -3,7 +3,7 @@ use std::{fs::create_dir_all, path::Path};
use anyhow::Result;
use nix::unistd::Pid;
use crate::cgroups::common;
use crate::cgroups::common::{self, CGROUP_PROCS};
use crate::{cgroups::v1::Controller, rootfs::default_devices};
use oci_spec::{LinuxDeviceCgroup, LinuxDeviceType, LinuxResources};
@ -27,7 +27,7 @@ impl Controller for Devices {
Self::apply_device(&d, &cgroup_root)?;
}
common::write_cgroup_file(cgroup_root.join("cgroup.procs"), &pid.to_string())?;
common::write_cgroup_file(cgroup_root.join(CGROUP_PROCS), pid)?;
Ok(())
}
}
@ -40,7 +40,7 @@ impl Devices {
cgroup_root.join("devices.deny")
};
common::write_cgroup_file(path, &device.to_string())?;
common::write_cgroup_file_str(path, &device.to_string())?;
Ok(())
}

View File

@ -3,7 +3,10 @@ use std::{fs, path::Path};
use anyhow::anyhow;
use regex::Regex;
use crate::cgroups::{common, v1::Controller};
use crate::cgroups::{
common::{self, CGROUP_PROCS},
v1::Controller,
};
use oci_spec::{LinuxHugepageLimit, LinuxResources};
pub struct Hugetlb {}
@ -21,7 +24,7 @@ impl Controller for Hugetlb {
Self::apply(cgroup_root, hugetlb)?
}
common::write_cgroup_file(cgroup_root.join("cgroup.procs"), &pid.to_string())?;
common::write_cgroup_file(cgroup_root.join(CGROUP_PROCS), pid)?;
Ok(())
}
}
@ -42,7 +45,7 @@ impl Hugetlb {
common::write_cgroup_file(
&root_path.join(format!("hugetlb.{}.limit_in_bytes", hugetlb.page_size)),
&hugetlb.limit.to_string(),
&hugetlb.limit,
)?;
Ok(())
}

View File

@ -7,7 +7,7 @@ use std::{
use anyhow::{Result, *};
use nix::{errno::Errno, unistd::Pid};
use crate::cgroups::common;
use crate::cgroups::common::{self, CGROUP_PROCS};
use crate::cgroups::v1::Controller;
use oci_spec::{LinuxMemory, LinuxResources};
@ -35,18 +35,24 @@ impl Controller for Memory {
Self::apply(&memory, cgroup_root)?;
if reservation != 0 {
Self::set(reservation, &cgroup_root.join(CGROUP_MEMORY_RESERVATION))?;
common::write_cgroup_file(
cgroup_root.join(CGROUP_MEMORY_RESERVATION),
reservation,
)?;
}
if linux_resources.disable_oom_killer {
Self::set(0, &cgroup_root.join(CGROUP_MEMORY_OOM_CONTROL))?;
common::write_cgroup_file(cgroup_root.join(CGROUP_MEMORY_OOM_CONTROL), 0)?;
} else {
Self::set(1, &cgroup_root.join(CGROUP_MEMORY_OOM_CONTROL))?;
common::write_cgroup_file(cgroup_root.join(CGROUP_MEMORY_OOM_CONTROL), 1)?;
}
if let Some(swappiness) = memory.swappiness {
if swappiness <= 100 {
Self::set(swappiness, &cgroup_root.join(CGROUP_MEMORY_SWAPPINESS))?;
common::write_cgroup_file(
cgroup_root.join(CGROUP_MEMORY_SWAPPINESS),
swappiness,
)?;
} else {
// invalid swappiness value
return Err(anyhow!(
@ -60,14 +66,17 @@ impl Controller for Memory {
// neither are implemented by runc. Tests pass without this, but
// kept in per the spec.
if let Some(kmem) = memory.kernel {
Self::set(kmem, &cgroup_root.join(CGROUP_KERNEL_MEMORY_LIMIT))?;
common::write_cgroup_file(cgroup_root.join(CGROUP_KERNEL_MEMORY_LIMIT), kmem)?;
}
if let Some(tcp_mem) = memory.kernel_tcp {
Self::set(tcp_mem, &cgroup_root.join(CGROUP_KERNEL_TCP_MEMORY_LIMIT))?;
common::write_cgroup_file(
cgroup_root.join(CGROUP_KERNEL_TCP_MEMORY_LIMIT),
tcp_mem,
)?;
}
}
common::write_cgroup_file(cgroup_root.join("cgroup.procs"), &pid.to_string())?;
common::write_cgroup_file(cgroup_root.join(CGROUP_PROCS), pid)?;
Ok(())
}
}
@ -170,14 +179,12 @@ impl Memory {
}
}
fn set_swap(val: i64, cgroup_root: &Path) -> Result<()> {
if val == 0 {
fn set_swap(swap: i64, cgroup_root: &Path) -> Result<()> {
if swap == 0 {
return Ok(());
}
let path = cgroup_root.join(CGROUP_MEMORY_SWAP_LIMIT);
Self::set(val, &path)?;
common::write_cgroup_file(cgroup_root.join(CGROUP_MEMORY_SWAP_LIMIT), swap)?;
Ok(())
}

View File

@ -4,6 +4,7 @@ use anyhow::Result;
use nix::unistd::Pid;
use crate::cgroups::common;
use crate::cgroups::common::CGROUP_PROCS;
use crate::cgroups::v1::Controller;
use oci_spec::{LinuxNetwork, LinuxResources};
@ -18,7 +19,7 @@ impl Controller for NetworkClassifier {
Self::apply(cgroup_root, network)?;
}
common::write_cgroup_file(cgroup_root.join("cgroup.procs"), &pid.to_string())?;
common::write_cgroup_file(cgroup_root.join(CGROUP_PROCS), pid)?;
Ok(())
}
}
@ -26,7 +27,7 @@ impl Controller for NetworkClassifier {
impl NetworkClassifier {
fn apply(root_path: &Path, network: &LinuxNetwork) -> Result<()> {
if let Some(class_id) = network.class_id {
common::write_cgroup_file(&root_path.join("net_cls.classid"), &class_id.to_string())?;
common::write_cgroup_file(root_path.join("net_cls.classid"), class_id)?;
}
Ok(())

View File

@ -4,6 +4,7 @@ use anyhow::Result;
use nix::unistd::Pid;
use crate::cgroups::common;
use crate::cgroups::common::CGROUP_PROCS;
use crate::cgroups::v1::Controller;
use oci_spec::{LinuxNetwork, LinuxResources};
@ -18,7 +19,7 @@ impl Controller for NetworkPriority {
Self::apply(cgroup_root, network)?;
}
common::write_cgroup_file(cgroup_root.join("cgroup.procs"), &pid.to_string())?;
common::write_cgroup_file(cgroup_root.join(CGROUP_PROCS), pid)?;
Ok(())
}
}
@ -26,7 +27,7 @@ impl Controller for NetworkPriority {
impl NetworkPriority {
fn apply(root_path: &Path, network: &LinuxNetwork) -> Result<()> {
let priorities: String = network.priorities.iter().map(|p| p.to_string()).collect();
common::write_cgroup_file(&root_path.join("net_prio.ifpriomap"), &priorities.trim())?;
common::write_cgroup_file_str(root_path.join("net_prio.ifpriomap"), &priorities.trim())?;
Ok(())
}

View File

@ -5,7 +5,10 @@ use std::{
use anyhow::Result;
use crate::cgroups::{common, v1::Controller};
use crate::cgroups::{
common::{self, CGROUP_PROCS},
v1::Controller,
};
use oci_spec::{LinuxPids, LinuxResources};
pub struct Pids {}
@ -23,7 +26,7 @@ impl Controller for Pids {
Self::apply(cgroup_root, pids)?;
}
common::write_cgroup_file(cgroup_root.join("cgroup.procs"), &pid.to_string())?;
common::write_cgroup_file(cgroup_root.join(CGROUP_PROCS), pid)?;
Ok(())
}
}
@ -36,7 +39,7 @@ impl Pids {
"max".to_string()
};
common::write_cgroup_file(&root_path.join("pids.max"), &limit)?;
common::write_cgroup_file_str(&root_path.join("pids.max"), &limit)?;
Ok(())
}
}

View File

@ -33,7 +33,7 @@ impl Cpu {
shares = Self::convert_shares_to_cgroup2(shares);
if shares != 0 {
// will result in Erno 34 (numerical result out of range) otherwise
common::write_cgroup_file(&path.join(CGROUP_CPU_WEIGHT), &shares.to_string())?;
common::write_cgroup_file(path.join(CGROUP_CPU_WEIGHT), shares)?;
}
}
@ -57,7 +57,7 @@ impl Cpu {
// 250000 250000 -> 1 CPU worth of runtime every 250ms
// 10000 50000 -> 20% of one CPU every 50ms
let max = quota_string + " " + &period_string;
common::write_cgroup_file(&path.join(CGROUP_CPU_MAX), &max)?;
common::write_cgroup_file_str(path.join(CGROUP_CPU_MAX), &max)?;
Ok(())
}

View File

@ -24,11 +24,11 @@ impl Controller for CpuSet {
impl CpuSet {
fn apply(path: &Path, cpuset: &LinuxCpu) -> Result<()> {
if let Some(cpus) = &cpuset.cpus {
common::write_cgroup_file(&path.join(CGROUP_CPUSET_CPUS), cpus)?;
common::write_cgroup_file_str(path.join(CGROUP_CPUSET_CPUS), cpus)?;
}
if let Some(mems) = &cpuset.mems {
common::write_cgroup_file(&path.join(CGROUP_CPUSET_MEMS), mems)?;
common::write_cgroup_file_str(path.join(CGROUP_CPUSET_MEMS), mems)?;
}
Ok(())

View File

@ -13,13 +13,12 @@ use super::{cpu::Cpu, cpuset::CpuSet, hugetlb::HugeTlb, io::Io, memory::Memory,
use crate::{
cgroups::v2::controller::Controller,
cgroups::{
common::{self, CgroupManager},
common::{self, CgroupManager, CGROUP_PROCS},
v2::controller_type::ControllerType,
},
utils::PathBufExt,
};
const CGROUP_PROCS: &str = "cgroup.procs";
const CGROUP_CONTROLLERS: &str = "cgroup.controllers";
const CGROUP_SUBTREE_CONTROL: &str = "cgroup.subtree_control";
@ -66,7 +65,7 @@ impl Manager {
// if this were set, writing to the cgroups.procs file will fail with Erno 16 (device or resource busy)
if components.peek().is_some() {
for controller in &controllers {
common::write_cgroup_file(
common::write_cgroup_file_str(
&current_path.join(CGROUP_SUBTREE_CONTROL),
controller,
)?;
@ -74,7 +73,7 @@ impl Manager {
}
}
common::write_cgroup_file(&full_path.join(CGROUP_PROCS), &pid.to_string())?;
common::write_cgroup_file(&full_path.join(CGROUP_PROCS), pid)?;
Ok(full_path)
}