Skip to content

Commit

Permalink
fix: fix parsing cpu limit for cgroup v1 (#16408)
Browse files Browse the repository at this point in the history
Signed-off-by: arkbriar <[email protected]>
  • Loading branch information
arkbriar authored Apr 22, 2024
1 parent a7e72c3 commit 031ced6
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/utils/resource_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,16 @@ pub mod cpu {
max_value: f32,
) -> Result<f32, std::io::Error> {
let content = std::fs::read_to_string(quota_path)?;
if content.trim() == super::DEFAULT_CGROUP_MAX_INDICATOR {
return Ok(max_value);
}
let cpu_quota = content
.trim()
.parse::<usize>()
.parse::<i64>()
.map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidData, "not a number"))?;
// According to the kernel documentation, if the value is negative, it means no limit.
// https://docs.kernel.org/scheduler/sched-bwc.html#management
if cpu_quota < 0 {
return Ok(max_value);
}

let cpu_period = super::util::read_usize(period_path)?;

Ok((cpu_quota as f32) / (cpu_period as f32))
Expand Down Expand Up @@ -593,6 +596,15 @@ mod util {
}

let test_cases = HashMap::from([
(
"default-values",
TestCase {
file_exists: true,
value_in_quota_file: String::from("-1"),
value_in_period_file: String::from("10000"),
expected: Ok(thread::available_parallelism().unwrap().get() as f32),
},
),
(
"valid-values-in-file",
TestCase {
Expand Down Expand Up @@ -627,10 +639,10 @@ mod util {
},
),
(
"max-value-in-file",
"negative-value-in-file",
TestCase {
file_exists: true,
value_in_quota_file: String::from("max"),
value_in_quota_file: String::from("-2"),
value_in_period_file: String::from("20000"),
expected: Ok(thread::available_parallelism().unwrap().get() as f32),
},
Expand Down

0 comments on commit 031ced6

Please sign in to comment.