From 40268dcd8dd57638a33ab44340ae130525e0a956 Mon Sep 17 00:00:00 2001 From: Felix Prasanna <91577249+fprasx@users.noreply.github.com> Date: Fri, 25 Aug 2023 13:29:10 -0400 Subject: [PATCH] monitor: fix filecache calculations (#5112) ## Problem An underflow bug in the filecache calculations. ## Summary of changes Fixed the bug, cleaned up calculations in general. --- libs/vm_monitor/src/cgroup.rs | 8 +++++--- libs/vm_monitor/src/filecache.rs | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/libs/vm_monitor/src/cgroup.rs b/libs/vm_monitor/src/cgroup.rs index 84a1a07b9d..4f529d16fd 100644 --- a/libs/vm_monitor/src/cgroup.rs +++ b/libs/vm_monitor/src/cgroup.rs @@ -634,7 +634,7 @@ impl CgroupWatcher { .context("failed to get memory subsystem")? .set_mem(cgroups_rs::memory::SetMemory { low: None, - high: Some(MaxValue::Value(bytes.min(i64::MAX as u64) as i64)), + high: Some(MaxValue::Value(u64::min(bytes, i64::MAX as u64) as i64)), min: None, max: None, }) @@ -654,8 +654,10 @@ impl CgroupWatcher { .set_mem(cgroups_rs::memory::SetMemory { min: None, low: None, - high: Some(MaxValue::Value(limits.high.min(i64::MAX as u64) as i64)), - max: Some(MaxValue::Value(limits.max.min(i64::MAX as u64) as i64)), + high: Some(MaxValue::Value( + u64::min(limits.high, i64::MAX as u64) as i64 + )), + max: Some(MaxValue::Value(u64::min(limits.max, i64::MAX as u64) as i64)), }) .context("failed to set memory limits") } diff --git a/libs/vm_monitor/src/filecache.rs b/libs/vm_monitor/src/filecache.rs index 33c76de35e..8dd369c9f1 100644 --- a/libs/vm_monitor/src/filecache.rs +++ b/libs/vm_monitor/src/filecache.rs @@ -132,11 +132,11 @@ impl FileCacheConfig { // Conversions to ensure we don't overflow from floating-point ops let size_from_spread = - 0_i64.max((available as f64 / (1.0 + self.spread_factor)) as i64) as u64; + i64::max(0, (available as f64 / (1.0 + self.spread_factor)) as i64) as u64; let size_from_normal = (total as f64 * self.resource_multiplier) as u64; - let byte_size = size_from_spread.min(size_from_normal); + let byte_size = u64::min(size_from_spread, size_from_normal); // The file cache operates in units of mebibytes, so the sizes we produce should // be rounded to a mebibyte. We round down to be conservative. @@ -268,7 +268,7 @@ impl FileCacheState { .context("failed to extract max file cache size from query result")?; let max_mb = max_bytes / MiB; - let num_mb = (num_bytes / MiB).max(max_mb); + let num_mb = u64::min(num_bytes, max_bytes) / MiB; let capped = if num_bytes > max_bytes { " (capped by maximum size)"