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

Implement apply for blkio cgroup

This commit is contained in:
Furisto 2021-05-23 17:58:24 +02:00
parent 03c1915403
commit 92fa6bbda7
3 changed files with 69 additions and 4 deletions

64
src/cgroups/blkio.rs Normal file

@ -0,0 +1,64 @@
use std::{fs::{self, OpenOptions}, io::Write, path::Path};
use crate::{cgroups::Controller, spec::{ LinuxBlockIo, LinuxResources}};
const CGROUP_BLKIO_THROTTLE_READ_DPS: &str = "blkio.throttle.read_bps_device";
const CGROUP_BLKIO_THROTTLE_WRITE_DPS: &str = "blkio.throttle.write_bps_device";
const CGROUP_BLKIO_THROTTLE_READ_IOPS: &str = "blkio.throttle.read_iops_device";
const CGROUP_BLKIO_THROTTLE_WRITE_IOPS: &str = "blkio.throttle.write_iops_device";
pub struct Blkio {}
impl Controller for Blkio {
fn apply(linux_resources: &LinuxResources, 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)?;
}
}
OpenOptions::new()
.create(false)
.write(true)
.truncate(false)
.open(cgroup_root.join("cgroup.procs"))?
.write_all(pid.to_string().as_bytes())?;
Ok(())
}
}
impl Blkio {
fn apply(root_path: &Path, blkio: &LinuxBlockIo) -> anyhow::Result<()> {
for trbd in &blkio.blkio_throttle_read_bps_device {
Self::write_file(&root_path.join(CGROUP_BLKIO_THROTTLE_READ_DPS), &format!("{}:{} {}", trbd.major, trbd.minor, trbd.rate))?;
}
for twbd in &blkio.blkio_throttle_write_bps_device {
Self::write_file(&root_path.join(CGROUP_BLKIO_THROTTLE_WRITE_DPS), &format!("{}:{} {}", twbd.major, twbd.minor, twbd.rate))?;
}
for trid in &blkio.blkio_throttle_read_iops_device {
Self::write_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(&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)
.append(true)
.truncate(false)
.open(file_path)?
.write_all(data.as_bytes())?;
Ok(())
}
}

@ -2,6 +2,7 @@ mod controller;
mod controller_type;
mod devices;
mod hugetlb;
mod blkio;
mod manager;
mod memory;
mod pids;

@ -382,13 +382,13 @@ pub struct LinuxBlockIo {
pub blkio_leaf_weight: Option<u16>,
#[serde(default)]
pub blkio_weight_device: Vec<LinuxWeightDevice>,
#[serde(default)]
#[serde(default, rename = "throttleReadBpsDevice")]
pub blkio_throttle_read_bps_device: Vec<LinuxThrottleDevice>,
#[serde(default)]
#[serde(default, rename = "throttleWriteBpsDevice")]
pub blkio_throttle_write_bps_device: Vec<LinuxThrottleDevice>,
#[serde(default, rename = "blkioThrottleReadIOPSDevice")]
#[serde(default, rename = "throttleReadIOPSDevice")]
pub blkio_throttle_read_iops_device: Vec<LinuxThrottleDevice>,
#[serde(default, rename = "blkioThrottleWriteIOPSDevice")]
#[serde(default, rename = "throttleWriteIOPSDevice")]
pub blkio_throttle_write_iops_device: Vec<LinuxThrottleDevice>,
}