Skip to content

Commit

Permalink
fix memory limits reading on fly.io/firecracker (#5114)
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoCasa authored Jan 22, 2025
1 parent 59bcce7 commit a8fa75a
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions backend/windmill-common/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,16 +512,43 @@ pub fn get_vcpus() -> Option<i64> {
}
}

fn get_memory_from_meminfo() -> Option<i64> {
let memory_info = parse_file::<String>("/proc/meminfo")?;
if memory_info.contains("MemTotal") {
let memory_total = memory_info
.split("MemTotal:")
.nth(1)?
.split("kB")
.next()?
.trim()
.parse::<i64>()
.ok()?;
return Some(memory_total * 1024);
}
None
}

pub fn get_memory() -> Option<i64> {
if Path::new("/sys/fs/cgroup/memory/memory.limit_in_bytes").exists() {
// cgroup v1
parse_file("/sys/fs/cgroup/memory/memory.limit_in_bytes")
} else {
// cgroup v2
let cgroup_path = get_cgroupv2_path()?;
let memory_max_path = format!("{cgroup_path}/memory.max");
let memory_limit: Option<i64> =
if Path::new("/sys/fs/cgroup/memory/memory.limit_in_bytes").exists() {
// cgroup v1
parse_file("/sys/fs/cgroup/memory/memory.limit_in_bytes")
} else {
// cgroup v2
let cgroup_path = get_cgroupv2_path()?;
let memory_max_path = format!("{cgroup_path}/memory.max");

parse_file(&memory_max_path)
};

parse_file(&memory_max_path)
// if memory_max is super high, read machine total memory (meminfo)
if memory_limit
.as_ref()
.is_some_and(|x| x > &(1024 * 1024 * 1024 * 1024 * 1024))
{
get_memory_from_meminfo()
} else {
memory_limit
}
}

Expand Down

0 comments on commit a8fa75a

Please sign in to comment.