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 configure cpu.idle by Cgroupfs #908

Merged
merged 3 commits into from
May 11, 2022
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
99 changes: 10 additions & 89 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/libcgroups/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ nix = "0.24.1"
procfs = "0.12.0"
log = "0.4"
anyhow = "1.0"
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "89376af" }
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e" }
oci-spec = "0.5.6"

dbus = { version = "0.9.5", optional = true }
fixedbitset = "0.4.1"
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -35,7 +35,7 @@ errno = { version = "0.2.8", optional = true }
libc = { version = "0.2.125", optional = true }

[dev-dependencies]
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "89376af", features = ["proptests"] }
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e", features = ["proptests"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e", features = ["proptests"] }
oci-spec = { version = "0.5.6", features = ["proptests"] }

quickcheck = "1"
mockall = { version = "0.11.0", features = [] }
clap = "3.1.17"
Expand Down
33 changes: 33 additions & 0 deletions crates/libcgroups/src/v1/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const CGROUP_CPU_BURST: &str = "cpu.cfs_burst_us";
const CGROUP_CPU_RT_RUNTIME: &str = "cpu.rt_runtime_us";
const CGROUP_CPU_RT_PERIOD: &str = "cpu.rt_period_us";
const CGROUP_CPU_STAT: &str = "cpu.stat";
const CGROUP_CPU_IDLE: &str = "cpu.idle";

pub struct Cpu {}

Expand All @@ -40,6 +41,7 @@ impl Controller for Cpu {
|| cpu.quota().is_some()
|| cpu.realtime_period().is_some()
|| cpu.realtime_runtime().is_some()
|| cpu.idle().is_some()
wineway marked this conversation as resolved.
Show resolved Hide resolved
{
return Some(cpu);
}
Expand Down Expand Up @@ -130,6 +132,10 @@ impl Cpu {
}
}

if let Some(idle) = cpu.idle() {
common::write_cgroup_file(root_path.join(CGROUP_CPU_IDLE), idle)?;
}

Ok(())
}
}
Expand Down Expand Up @@ -209,6 +215,33 @@ mod tests {
assert_eq!(content, RUNTIME.to_string());
}

#[test]
fn test_set_cpu_idle() {
// arrange
const IDLE: i64 = 1;
const CPU: &str = "cpu";

if !Path::new(common::DEFAULT_CGROUP_ROOT)
.join(CPU)
.join(CGROUP_CPU_IDLE)
.exists()
{
// skip test_set_cpu_idle due to not found cpu.idle, maybe due to old kernel version
return;
}

let (tmp, max) = setup("test_set_cpu_idle", CGROUP_CPU_IDLE);
let cpu = LinuxCpuBuilder::default().idle(IDLE).build().unwrap();

// 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_IDLE));
assert_eq!(content, IDLE.to_string());
}

#[test]
fn test_set_rt_period() {
// arrange
Expand Down
32 changes: 32 additions & 0 deletions crates/libcgroups/src/v2/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::controller::Controller;
const CGROUP_CPU_WEIGHT: &str = "cpu.weight";
const CGROUP_CPU_MAX: &str = "cpu.max";
const CGROUP_CPU_BURST: &str = "cpu.max.burst";
const CGROUP_CPU_IDLE: &str = "cpu.idle";
const UNRESTRICTED_QUOTA: &str = "max";
const MAX_CPU_WEIGHT: u64 = 10000;

Expand Down Expand Up @@ -96,6 +97,10 @@ impl Cpu {
common::write_cgroup_file(path.join(CGROUP_CPU_BURST), burst)?;
}

if let Some(idle) = cpu.idle() {
common::write_cgroup_file(path.join(CGROUP_CPU_IDLE), idle)?;
wineway marked this conversation as resolved.
Show resolved Hide resolved
}

Ok(())
}

Expand Down Expand Up @@ -153,6 +158,33 @@ mod tests {
assert_eq!(content, 840.to_string());
}

#[test]
fn test_set_cpu_idle() {
// arrange
const IDLE: i64 = 1;
const CPU: &str = "cpu";

if !Path::new(common::DEFAULT_CGROUP_ROOT)
.join(CPU)
.join(CGROUP_CPU_IDLE)
.exists()
{
// skip test_set_cpu_idle due to not found cpu.idle, maybe due to old kernel version
return;
}

let (tmp, max) = setup("test_set_cpu_idle", CGROUP_CPU_IDLE);
let cpu = LinuxCpuBuilder::default().idle(IDLE).build().unwrap();

// 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_IDLE));
assert_eq!(content, format!("{}", IDLE))
}

#[test]
fn test_set_positive_quota() {
// arrange
Expand Down
4 changes: 2 additions & 2 deletions crates/libcontainer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ libc = "0.2.125"
log = "0.4"
mio = { version = "0.8.3", features = ["os-ext", "os-poll"] }
nix = "0.24.1"
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "89376af" }
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e" }
oci-spec = "0.5.6"

path-clean = "0.1.0"
procfs = "0.12.0"
prctl = "1.0.0"
Expand All @@ -43,7 +43,7 @@ wasmer = { version = "2.2.0", optional = true }
wasmer-wasi = { version = "2.2.0", optional = true }

[dev-dependencies]
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "89376af", features = ["proptests"] }
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e", features = ["proptests"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e", features = ["proptests"] }
oci-spec = { version = "0.5.6", features = ["proptests"] }

quickcheck = "1"
serial_test = "0.6.0"
rand = "0.8.5"
2 changes: 1 addition & 1 deletion crates/youki/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ libcontainer = { version = "0.0.3", path = "../libcontainer" }
liboci-cli = { version = "0.0.3", path = "../liboci-cli" }
log = { version = "0.4", features = ["std"]}
nix = "0.24.1"
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "89376af" }
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e" }
oci-spec = "0.5.6"

once_cell = "1.10.0"
pentacle = "1.0.0"
procfs = "0.12.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/rust-integration-tests/integration_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ libcontainer = { path = "../../../crates/libcontainer" }
log = { version = "0.4", features = ["std"] }
nix = "0.24.1"
num_cpus = "1.13"
oci-spec = "0.5.5"
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "6df620e" }
once_cell = "1.10.0"
pnet = "0.29.0"
procfs = "0.12.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn create_cpu_spec(
shares: u64,
quota: i64,
period: u64,
idle_opt: Option<i64>,
cpus: &str,
mems: &str,
realtime_period_opt: Option<u64>,
Expand All @@ -24,6 +25,10 @@ fn create_cpu_spec(
.cpus(cpus)
.mems(mems);

if let Some(idle) = idle_opt {
builder = builder.idle(idle);
}

if let Some(realtime_period) = realtime_period_opt {
builder = builder.realtime_period(realtime_period);
}
Expand Down
Loading