Skip to content

Commit a8fa75a

Browse files
authored
fix memory limits reading on fly.io/firecracker (#5114)
1 parent 59bcce7 commit a8fa75a

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

backend/windmill-common/src/worker.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,16 +512,43 @@ pub fn get_vcpus() -> Option<i64> {
512512
}
513513
}
514514

515+
fn get_memory_from_meminfo() -> Option<i64> {
516+
let memory_info = parse_file::<String>("/proc/meminfo")?;
517+
if memory_info.contains("MemTotal") {
518+
let memory_total = memory_info
519+
.split("MemTotal:")
520+
.nth(1)?
521+
.split("kB")
522+
.next()?
523+
.trim()
524+
.parse::<i64>()
525+
.ok()?;
526+
return Some(memory_total * 1024);
527+
}
528+
None
529+
}
530+
515531
pub fn get_memory() -> Option<i64> {
516-
if Path::new("/sys/fs/cgroup/memory/memory.limit_in_bytes").exists() {
517-
// cgroup v1
518-
parse_file("/sys/fs/cgroup/memory/memory.limit_in_bytes")
519-
} else {
520-
// cgroup v2
521-
let cgroup_path = get_cgroupv2_path()?;
522-
let memory_max_path = format!("{cgroup_path}/memory.max");
532+
let memory_limit: Option<i64> =
533+
if Path::new("/sys/fs/cgroup/memory/memory.limit_in_bytes").exists() {
534+
// cgroup v1
535+
parse_file("/sys/fs/cgroup/memory/memory.limit_in_bytes")
536+
} else {
537+
// cgroup v2
538+
let cgroup_path = get_cgroupv2_path()?;
539+
let memory_max_path = format!("{cgroup_path}/memory.max");
540+
541+
parse_file(&memory_max_path)
542+
};
523543

524-
parse_file(&memory_max_path)
544+
// if memory_max is super high, read machine total memory (meminfo)
545+
if memory_limit
546+
.as_ref()
547+
.is_some_and(|x| x > &(1024 * 1024 * 1024 * 1024 * 1024))
548+
{
549+
get_memory_from_meminfo()
550+
} else {
551+
memory_limit
525552
}
526553
}
527554

0 commit comments

Comments
 (0)