1
0
Fork 0
mirror of https://github.com/containers/youki synced 2024-06-03 13:26:13 +02:00
youki/cgroups/src/v2/cpuset.rs

75 lines
1.9 KiB
Rust
Raw Normal View History

2021-05-29 17:15:16 +02:00
use anyhow::Result;
use std::path::Path;
2021-05-25 17:06:13 +02:00
2021-08-01 22:49:31 +02:00
use crate::common;
2021-05-25 17:06:13 +02:00
use oci_spec::{LinuxCpu, LinuxResources};
2021-05-29 17:15:16 +02:00
use super::controller::Controller;
2021-05-25 17:06:13 +02:00
const CGROUP_CPUSET_CPUS: &str = "cpuset.cpus";
const CGROUP_CPUSET_MEMS: &str = "cpuset.mems";
pub struct CpuSet {}
impl Controller for CpuSet {
fn apply(linux_resources: &LinuxResources, cgroup_path: &Path) -> Result<()> {
2021-05-27 18:54:20 +02:00
if let Some(cpuset) = &linux_resources.cpu {
Self::apply(cgroup_path, cpuset)?;
2021-05-25 17:06:13 +02:00
}
Ok(())
}
}
impl CpuSet {
fn apply(path: &Path, cpuset: &LinuxCpu) -> Result<()> {
2021-05-26 21:42:07 +02:00
if let Some(cpus) = &cpuset.cpus {
2021-06-04 15:34:07 +02:00
common::write_cgroup_file_str(path.join(CGROUP_CPUSET_CPUS), cpus)?;
2021-05-25 17:06:13 +02:00
}
2021-05-26 21:42:07 +02:00
if let Some(mems) = &cpuset.mems {
2021-06-04 15:34:07 +02:00
common::write_cgroup_file_str(path.join(CGROUP_CPUSET_MEMS), mems)?;
2021-05-25 17:06:13 +02:00
}
Ok(())
}
}
2021-05-28 22:27:45 +02:00
#[cfg(test)]
mod tests {
2021-06-02 12:17:54 +02:00
use std::fs;
2021-05-28 22:27:45 +02:00
use super::*;
2021-08-01 22:49:31 +02:00
use crate::test::{setup, LinuxCpuBuilder};
2021-05-28 22:27:45 +02:00
#[test]
fn test_set_cpus() {
// arrange
let (tmp, cpus) = setup("test_set_cpus", CGROUP_CPUSET_CPUS);
let cpuset = LinuxCpuBuilder::new().with_cpus("1-3".to_owned()).build();
// act
CpuSet::apply(&tmp, &cpuset).expect("apply cpuset");
// assert
2021-05-31 15:58:05 +02:00
let content = fs::read_to_string(&cpus)
.unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPUSET_CPUS));
2021-05-28 22:27:45 +02:00
assert_eq!(content, "1-3");
}
#[test]
fn test_set_mems() {
2021-05-29 17:15:16 +02:00
// arrange
let (tmp, mems) = setup("test_set_mems", CGROUP_CPUSET_MEMS);
let cpuset = LinuxCpuBuilder::new().with_mems("1-3".to_owned()).build();
// act
CpuSet::apply(&tmp, &cpuset).expect("apply cpuset");
// assert
2021-05-31 15:58:05 +02:00
let content = fs::read_to_string(&mems)
.unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPUSET_MEMS));
2021-05-29 17:15:16 +02:00
assert_eq!(content, "1-3");
2021-05-28 22:27:45 +02:00
}
}