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

Support for cgroup v1 cpu and cpuset subsystem #63

Merged
merged 8 commits into from
Jun 5, 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
2 changes: 1 addition & 1 deletion integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
root=$(pwd)
cd integration_test/src/github.com/opencontainers/runtime-tools
GOPATH=$root/integration_test make runtimetest validation-executables
test_cases=("default/default.t" "linux_cgroups_devices/linux_cgroups_devices.t" "linux_cgroups_hugetlb/linux_cgroups_hugetlb.t" "linux_cgroups_pids/linux_cgroups_pids.t" "linux_cgroups_memory/linux_cgroups_memory.t" "linux_cgroups_network/linux_cgroups_network.t")
test_cases=("default/default.t" "linux_cgroups_devices/linux_cgroups_devices.t" "linux_cgroups_hugetlb/linux_cgroups_hugetlb.t" "linux_cgroups_pids/linux_cgroups_pids.t" "linux_cgroups_memory/linux_cgroups_memory.t" "linux_cgroups_network/linux_cgroups_network.t" "linux_cgroups_cpus/linux_cgroups_cpus.t")
for case in "${test_cases[@]}"; do
echo "Running $case"
if [ 0 -ne $(sudo RUST_BACKTRACE=1 YOUKI_LOG_LEVEL=debug RUNTIME=$root/target/x86_64-unknown-linux-gnu/debug/youki $root/integration_test/src/github.com/opencontainers/runtime-tools/validation/$case | grep "not ok" | wc -l) ]; then
Expand Down
33 changes: 29 additions & 4 deletions src/cgroups/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use std::{
path::{Path, PathBuf},
};

use anyhow::{bail, Result};
use anyhow::{anyhow, bail, Result};
use nix::unistd::Pid;
use oci_spec::LinuxResources;
use procfs::process::Process;

use crate::cgroups::v1;
use crate::cgroups::v2;

pub const CGROUP_PROCS: &str = "cgroup.procs";
pub const DEFAULT_CGROUP_ROOT: &str = "/sys/fs/cgroup";

pub trait CgroupManager {
Expand All @@ -39,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)
Expand All @@ -50,6 +51,27 @@ pub fn write_cgroup_file<P: AsRef<Path>>(path: P, data: &str) -> Result<()> {
Ok(())
}

#[inline]
pub fn write_cgroup_file<P: AsRef<Path>, T: ToString>(path: P, data: T) -> Result<()> {
fs::OpenOptions::new()
.create(false)
.write(true)
.truncate(false)
.open(path)?
.write_all(data.to_string().as_bytes())?;

Ok(())
}

pub fn get_cgroupv1_mount_path(subsystem: &str) -> Result<PathBuf> {
Process::myself()?
.mountinfo()?
.into_iter()
.find(|m| m.fs_type == "cgroup" && m.mount_point.ends_with(subsystem))
.map(|m| m.mount_point)
.ok_or_else(|| anyhow!("could not find mountpoint for {}", subsystem))
}

pub fn create_cgroup_manager<P: Into<PathBuf>>(cgroup_path: P) -> Result<Box<dyn CgroupManager>> {
let cgroup_mount = Process::myself()?
.mountinfo()?
Expand Down Expand Up @@ -83,8 +105,11 @@ 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"),
}
Expand Down
15 changes: 9 additions & 6 deletions src/cgroups/v1/blkio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -26,36 +29,36 @@ 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(())
}
}

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),
)?;
Expand Down
4 changes: 4 additions & 0 deletions src/cgroups/v1/controller_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::string::ToString;

pub enum ControllerType {
Cpu,
CpuSet,
Devices,
HugeTlb,
Pids,
Expand All @@ -13,6 +15,8 @@ pub enum ControllerType {
impl ToString for ControllerType {
fn to_string(&self) -> String {
match self {
Self::Cpu => "cpu".into(),
tsturzl marked this conversation as resolved.
Show resolved Hide resolved
Self::CpuSet => "cpuset".into(),
Self::Devices => "devices".into(),
Self::HugeTlb => "hugetlb".into(),
Self::Pids => "pids".into(),
Expand Down
156 changes: 156 additions & 0 deletions src/cgroups/v1/cpu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
use std::{fs, path::Path};

use anyhow::Result;
use nix::unistd::Pid;
use oci_spec::{LinuxCpu, LinuxResources};

use crate::cgroups::common::{self, CGROUP_PROCS};

use super::Controller;

const CGROUP_CPU_SHARES: &str = "cpu.shares";
const CGROUP_CPU_QUOTA: &str = "cpu.cfs_quota_us";
const CGROUP_CPU_PERIOD: &str = "cpu.cfs_period_us";
const CGROUP_CPU_RT_RUNTIME: &str = "cpu.rt_runtime_us";
const CGROUP_CPU_RT_PERIOD: &str = "cpu.rt_period_us";

pub struct Cpu {}

impl Controller for Cpu {
fn apply(linux_resources: &LinuxResources, cgroup_root: &Path, pid: Pid) -> Result<()> {
log::debug!("Apply Cpu cgroup config");
fs::create_dir_all(cgroup_root)?;
if let Some(cpu) = &linux_resources.cpu {
Self::apply(cgroup_root, cpu)?;
}

common::write_cgroup_file(cgroup_root.join(CGROUP_PROCS), pid)?;
Ok(())
}
}

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)?;
}
}

if let Some(cpu_period) = cpu.period {
if cpu_period != 0 {
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)?;
}
}

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)?;
}
}

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)?;
}
}

Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::cgroups::test::{set_fixture, setup, LinuxCpuBuilder};
use std::fs;

#[test]
fn test_set_shares() {
// arrange
let (tmp, shares) = setup("test_set_shares", CGROUP_CPU_SHARES);
let _ = set_fixture(&tmp, CGROUP_CPU_SHARES, "")
.unwrap_or_else(|_| panic!("set test fixture for {}", CGROUP_CPU_SHARES));
let cpu = LinuxCpuBuilder::new().with_shares(2048).build();

// act
Cpu::apply(&tmp, &cpu).expect("apply cpu");

// assert
let content = fs::read_to_string(shares)
.unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_SHARES));
assert_eq!(content, 2048.to_string());
}

#[test]
fn test_set_quota() {
// arrange
const QUOTA: i64 = 200000;
let (tmp, max) = setup("test_set_quota", CGROUP_CPU_QUOTA);
let cpu = LinuxCpuBuilder::new().with_quota(QUOTA).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_QUOTA));
assert_eq!(content, QUOTA.to_string());
}

#[test]
fn test_set_period() {
// arrange
const PERIOD: u64 = 100000;
let (tmp, max) = setup("test_set_period", CGROUP_CPU_PERIOD);
let cpu = LinuxCpuBuilder::new().with_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_PERIOD));
assert_eq!(content, PERIOD.to_string());
}

#[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());
}

#[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());
}
}
Loading