Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align cgroup controller implementations #54

Merged
merged 1 commit into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/cgroups/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,8 @@ impl Display for Cgroup {
}
}

pub fn write_cgroup_file_truncate(path: &Path, data: &str) -> Result<()> {
fs::OpenOptions::new()
.create(false)
.write(true)
.truncate(true)
.open(path)?
.write_all(data.as_bytes())?;

Ok(())
}

pub fn write_cgroup_file(path: &Path, data: &str) -> Result<()> {
#[inline]
pub fn write_cgroup_file<P: AsRef<Path>>(path: P, data: &str) -> Result<()> {
fs::OpenOptions::new()
.create(false)
.write(true)
Expand Down Expand Up @@ -94,7 +84,7 @@ pub fn create_cgroup_manager<P: Into<PathBuf>>(cgroup_path: P) -> Result<Box<dyn
)?))
}
_ => Ok(Box::new(v1::manager::Manager::new(cgroup_path.into())?)),
}
}
}
_ => bail!("could not find cgroup filesystem"),
}
Expand Down
45 changes: 13 additions & 32 deletions src/cgroups/v1/blkio.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::{
fs::{self, OpenOptions},
io::Write,
fs::{self},
path::Path,
};

use crate::cgroups::v1::Controller;
use crate::cgroups::{common, v1::Controller};
use oci_spec::{LinuxBlockIo, LinuxResources};

const CGROUP_BLKIO_THROTTLE_READ_BPS: &str = "blkio.throttle.read_bps_device";
Expand All @@ -20,73 +19,55 @@ impl Controller for Blkio {
cgroup_root: &Path,
pid: nix::unistd::Pid,
) -> anyhow::Result<()> {
match &linux_resources.block_io {
None => return Ok(()),
Some(block_io) => {
fs::create_dir_all(cgroup_root)?;
Self::apply(cgroup_root, block_io)?;
}
}
log::debug!("Apply blkio cgroup config");
fs::create_dir_all(cgroup_root)?;

OpenOptions::new()
.create(false)
.write(true)
.truncate(false)
.open(cgroup_root.join("cgroup.procs"))?
.write_all(pid.to_string().as_bytes())?;
if let Some(blkio) = &linux_resources.block_io {
Self::apply(cgroup_root, blkio)?;
}

common::write_cgroup_file(&cgroup_root.join("cgroup.procs"), &pid.to_string())?;
Ok(())
}
}

impl Blkio {
fn apply(root_path: &Path, blkio: &LinuxBlockIo) -> anyhow::Result<()> {
for trbd in &blkio.blkio_throttle_read_bps_device {
Self::write_file(
common::write_cgroup_file(
&root_path.join(CGROUP_BLKIO_THROTTLE_READ_BPS),
&format!("{}:{} {}", trbd.major, trbd.minor, trbd.rate),
)?;
}

for twbd in &blkio.blkio_throttle_write_bps_device {
Self::write_file(
common::write_cgroup_file(
&root_path.join(CGROUP_BLKIO_THROTTLE_WRITE_BPS),
&format!("{}:{} {}", twbd.major, twbd.minor, twbd.rate),
)?;
}

for trid in &blkio.blkio_throttle_read_iops_device {
Self::write_file(
common::write_cgroup_file(
&root_path.join(CGROUP_BLKIO_THROTTLE_READ_IOPS),
&format!("{}:{} {}", trid.major, trid.minor, trid.rate),
)?;
}

for twid in &blkio.blkio_throttle_write_iops_device {
Self::write_file(
common::write_cgroup_file(
&root_path.join(CGROUP_BLKIO_THROTTLE_WRITE_IOPS),
&format!("{}:{} {}", twid.major, twid.minor, twid.rate),
)?;
}

Ok(())
}

fn write_file(file_path: &Path, data: &str) -> anyhow::Result<()> {
fs::OpenOptions::new()
.create(false)
.write(true)
.truncate(false)
.open(file_path)?
.write_all(data.as_bytes())?;

Ok(())
}
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;
use std::{io::Write, path::PathBuf};

use super::*;
use oci_spec::{LinuxBlockIo, LinuxThrottleDevice};
Expand Down
21 changes: 4 additions & 17 deletions src/cgroups/v1/devices.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::io::Write;
use std::{
fs::{create_dir_all, OpenOptions},
path::Path,
};
use std::{fs::create_dir_all, path::Path};

use anyhow::Result;
use nix::unistd::Pid;

use crate::cgroups::common;
use crate::{cgroups::v1::Controller, rootfs::default_devices};
use oci_spec::{LinuxDeviceCgroup, LinuxDeviceType, LinuxResources};

Expand All @@ -30,12 +27,7 @@ impl Controller for Devices {
Self::apply_device(&d, &cgroup_root)?;
}

OpenOptions::new()
.create(false)
.write(true)
.truncate(false)
.open(cgroup_root.join("cgroup.procs"))?
.write_all(pid.to_string().as_bytes())?;
common::write_cgroup_file(cgroup_root.join("cgroup.procs"), &pid.to_string())?;
Ok(())
}
}
Expand All @@ -48,12 +40,7 @@ impl Devices {
cgroup_root.join("devices.deny")
};

OpenOptions::new()
.create(false)
.write(true)
.truncate(false)
.open(path)?
.write_all(device.to_string().as_bytes())?;
common::write_cgroup_file(path, &device.to_string())?;
Ok(())
}

Expand Down
30 changes: 5 additions & 25 deletions src/cgroups/v1/hugetlb.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use std::{
fs::{self, OpenOptions},
io::Write,
path::Path,
};
use std::{fs, path::Path};

use anyhow::anyhow;
use regex::Regex;

use crate::cgroups::v1::Controller;
use crate::cgroups::{common, v1::Controller};
use oci_spec::{LinuxHugepageLimit, LinuxResources};

pub struct Hugetlb {}
Expand All @@ -25,12 +21,7 @@ impl Controller for Hugetlb {
Self::apply(cgroup_root, hugetlb)?
}

OpenOptions::new()
.create(false)
.write(true)
.truncate(false)
.open(cgroup_root.join("cgroup.procs"))?
.write_all(pid.to_string().as_bytes())?;
common::write_cgroup_file(cgroup_root.join("cgroup.procs"), &pid.to_string())?;
Ok(())
}
}
Expand All @@ -49,32 +40,21 @@ impl Hugetlb {
}
}

Self::write_file(
common::write_cgroup_file(
&root_path.join(format!("hugetlb.{}.limit_in_bytes", hugetlb.page_size)),
&hugetlb.limit.to_string(),
)?;
Ok(())
}

fn write_file(file_path: &Path, data: &str) -> anyhow::Result<()> {
fs::OpenOptions::new()
.create(false)
.write(true)
.truncate(true)
.open(file_path)?
.write_all(data.as_bytes())?;

Ok(())
}

fn is_power_of_two(number: u64) -> bool {
(number != 0) && (number & (number - 1)) == 0
}
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;
use std::{io::Write, path::PathBuf};

use super::*;
use oci_spec::LinuxHugepageLimit;
Expand Down
2 changes: 1 addition & 1 deletion src/cgroups/v1/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{
Controller,
};

use crate::{cgroups::v1::ControllerType, cgroups::common::CgroupManager, utils::PathBufExt};
use crate::{cgroups::common::CgroupManager, cgroups::v1::ControllerType, utils::PathBufExt};
use oci_spec::LinuxResources;

const CONTROLLERS: &[ControllerType] = &[
Expand Down
11 changes: 3 additions & 8 deletions src/cgroups/v1/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
use anyhow::{Result, *};
use nix::{errno::Errno, unistd::Pid};

use crate::cgroups::common;
use crate::cgroups::v1::Controller;
use oci_spec::{LinuxMemory, LinuxResources};

Expand Down Expand Up @@ -64,14 +65,9 @@ impl Controller for Memory {
if let Some(tcp_mem) = memory.kernel_tcp {
Self::set(tcp_mem, &cgroup_root.join(CGROUP_KERNEL_TCP_MEMORY_LIMIT))?;
}

OpenOptions::new()
.create(false)
.write(true)
.truncate(false)
.open(cgroup_root.join("cgroup.procs"))?
.write_all(pid.to_string().as_bytes())?;
}

common::write_cgroup_file(cgroup_root.join("cgroup.procs"), &pid.to_string())?;
Ok(())
}
}
Expand Down Expand Up @@ -180,7 +176,6 @@ impl Memory {
}

let path = cgroup_root.join(CGROUP_MEMORY_SWAP_LIMIT);

Self::set(val, &path)?;

Ok(())
Expand Down
30 changes: 5 additions & 25 deletions src/cgroups/v1/network_classifier.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::io::Write;
use std::{
fs::{create_dir_all, OpenOptions},
path::Path,
};
use std::{fs::create_dir_all, path::Path};

use anyhow::Result;
use nix::unistd::Pid;

use crate::cgroups::common;
use crate::cgroups::v1::Controller;
use oci_spec::{LinuxNetwork, LinuxResources};

Expand All @@ -19,43 +16,26 @@ impl Controller for NetworkClassifier {

if let Some(network) = linux_resources.network.as_ref() {
Self::apply(cgroup_root, network)?;

OpenOptions::new()
.create(false)
.write(true)
.truncate(true)
.open(cgroup_root.join("cgroup.procs"))?
.write_all(pid.to_string().as_bytes())?;
}

common::write_cgroup_file(cgroup_root.join("cgroup.procs"), &pid.to_string())?;
Ok(())
}
}

impl NetworkClassifier {
fn apply(root_path: &Path, network: &LinuxNetwork) -> Result<()> {
if let Some(class_id) = network.class_id {
Self::write_file(&root_path.join("net_cls.classid"), &class_id.to_string())?;
common::write_cgroup_file(&root_path.join("net_cls.classid"), &class_id.to_string())?;
}

Ok(())
}

fn write_file(file_path: &Path, data: &str) -> Result<()> {
OpenOptions::new()
.create(false)
.write(true)
.truncate(true)
.open(file_path)?
.write_all(data.as_bytes())?;

Ok(())
}
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;
use std::{io::Write, path::PathBuf};

use super::*;

Expand Down
Loading