From f8cc3baacbf39bcf6e4f31c262d2b0f5816c1fe3 Mon Sep 17 00:00:00 2001 From: HugoCasa Date: Thu, 17 Jul 2025 14:39:25 +0200 Subject: [PATCH] feat: windows memory and vcpu reading (#6212) --- backend/Cargo.lock | 1 + backend/Cargo.toml | 1 + backend/windmill-common/Cargo.toml | 3 +++ backend/windmill-common/src/worker.rs | 28 +++++++++++++++++++++++++++ 4 files changed, 33 insertions(+) diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 8c81028a53..bd4a7009d7 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -14934,6 +14934,7 @@ dependencies = [ "sqlx", "strum 0.27.1", "strum_macros 0.27.1", + "sysinfo", "systemstat", "tar", "tempfile", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 742ad1374c..2937431719 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -267,6 +267,7 @@ google-cloud-googleapis = {version = "0.16.1", features = ["pubsub"]} # TODO: remove once deno fixes the issue on their end # https://github.com/denoland/deno/issues/28557 winapi = { version = "0.3.9", features = ["sysinfoapi"] } +sysinfo = { version = "0.32.1" } swc_common = "=0.37.5" swc_ecma_parser = "=0.149.1" diff --git a/backend/windmill-common/Cargo.toml b/backend/windmill-common/Cargo.toml index 980ec87d64..67c4466b17 100644 --- a/backend/windmill-common/Cargo.toml +++ b/backend/windmill-common/Cargo.toml @@ -102,3 +102,6 @@ tonic = { workspace = true, optional = true } [target.'cfg(not(target_env = "msvc"))'.dependencies] tikv-jemalloc-ctl = { optional = true, workspace = true } + +[target.'cfg(windows)'.dependencies] +sysinfo.workspace = true diff --git a/backend/windmill-common/src/worker.rs b/backend/windmill-common/src/worker.rs index 1f09176512..58afcc18e6 100644 --- a/backend/windmill-common/src/worker.rs +++ b/backend/windmill-common/src/worker.rs @@ -19,6 +19,8 @@ use std::{ str::FromStr, sync::{atomic::AtomicBool, Arc}, }; +#[cfg(windows)] +use sysinfo::System; use tokio::sync::RwLock; use uuid::Uuid; use windmill_macros::annotations; @@ -759,6 +761,7 @@ fn write_binary_file(main_path: &str, byts: &mut bytes::Bytes) -> error::Result< Ok(()) } +#[cfg(not(windows))] fn get_cgroupv2_path() -> Option { let cgroup_path: String = parse_file("/proc/self/cgroup")?; @@ -767,6 +770,7 @@ fn get_cgroupv2_path() -> Option { .map(|x| format!("/sys/fs/cgroup{}", x.get(1).unwrap().as_str())) } +#[cfg(not(windows))] pub fn get_vcpus() -> Option { if Path::new("/sys/fs/cgroup/cpu/cpu.cfs_quota_us").exists() { // cgroup v1 @@ -792,6 +796,14 @@ pub fn get_vcpus() -> Option { } } +#[cfg(windows)] +pub fn get_vcpus() -> Option { + let mut sys = System::new(); + sys.refresh_cpu_all(); + (sys.cpus().len() * 100000).try_into().ok() +} + +#[cfg(not(windows))] fn get_memory_from_meminfo() -> Option { let memory_info = parse_file::("/proc/meminfo")?; if memory_info.contains("MemTotal") { @@ -808,6 +820,7 @@ fn get_memory_from_meminfo() -> Option { None } +#[cfg(not(windows))] pub fn get_memory() -> Option { let memory_limit: Option = if Path::new("/sys/fs/cgroup/memory/memory.limit_in_bytes").exists() { @@ -832,6 +845,14 @@ pub fn get_memory() -> Option { } } +#[cfg(windows)] +pub fn get_memory() -> Option { + let mut sys = System::new(); + sys.refresh_memory(); + Some(sys.total_memory() as i64) +} + +#[cfg(not(windows))] pub fn get_worker_memory_usage() -> Option { if Path::new("/sys/fs/cgroup/memory/memory.usage_in_bytes").exists() { // cgroup v1 @@ -869,6 +890,13 @@ pub fn get_worker_memory_usage() -> Option { } } +#[cfg(windows)] +pub fn get_worker_memory_usage() -> Option { + let mut sys = System::new(); + sys.refresh_memory(); + Some(sys.used_memory() as i64) +} + pub fn get_windmill_memory_usage() -> Option { #[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))] {