diff --git a/Cargo.lock b/Cargo.lock index 00554e72..154930f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -465,6 +465,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +[[package]] +name = "bytesize" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e368af43e418a04d52505cf3dbc23dda4e3407ae2fa99fd0e4f308ce546acc" + [[package]] name = "cc" version = "1.0.86" @@ -1198,6 +1204,7 @@ dependencies = [ "libsecp256k1", "num-bigint", "num-traits", + "num_cpus", "parking_lot", "pea2pea", "prost 0.11.9", @@ -1209,6 +1216,7 @@ dependencies = [ "sha3", "snow", "sqlx", + "systemstat", "thiserror", "tokio", "tokio-stream", @@ -3662,6 +3670,20 @@ dependencies = [ "libc", ] +[[package]] +name = "systemstat" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a24aec24a9312c83999a28e3ef9db7e2afd5c64bf47725b758cdc1cafd5b0bd2" +dependencies = [ + "bytesize", + "lazy_static", + "libc", + "nom", + "time", + "winapi", +] + [[package]] name = "tap" version = "1.0.1" diff --git a/crates/node/Cargo.toml b/crates/node/Cargo.toml index ddf79254..e0f30767 100644 --- a/crates/node/Cargo.toml +++ b/crates/node/Cargo.toml @@ -35,12 +35,14 @@ home = { version = "0.5", optional = true} http-body-util = { version = "0.1", optional = true } hyper = { version = "1", features = ["full"], optional = true } hyper-util = { version = "0.1", features = ["full"], optional = true } +num_cpus = { version = "1.4.0", optional = true } num-traits = { version = "0.2", optional = true } parking_lot = { version = "0.12", optional = true } pea2pea = { version = "0.48", optional = true } prost = { version = "0.11", optional = true } qapi = { version = "0.14", features = [ "qmp", "async-tokio-net" ], optional = true } snow = { version = "0.9", optional = true } +systemstat = { version = "0.2.3", optional = true } tokio-stream = { version = "0.1", optional = true } tokio-util = { version = "0.7", optional = true } tokio-vsock = { version = "0.4.0", features = ["tonic-conn"], optional = true } @@ -62,12 +64,14 @@ node-binary = [ "http-body-util", "hyper", "hyper-util", + "num_cpus", "num-traits", "parking_lot", "pea2pea", "prost", "qapi", "snow", + "systemstat", "tokio-stream", "tokio-util", "tokio-vsock", diff --git a/crates/node/src/cli.rs b/crates/node/src/cli.rs index dda104fa..b05a1aba 100644 --- a/crates/node/src/cli.rs +++ b/crates/node/src/cli.rs @@ -101,21 +101,15 @@ pub struct Config { )] pub vsock_listen_port: u32, - #[arg( - long, - long_help = "Number of CPUs available", - env = "GEVULOT_CPUS", - default_value_t = 8 - )] - pub num_cpus: u64, + #[arg(long, long_help = "Number of CPUs available", env = "GEVULOT_CPUS")] + pub num_cpus: Option, #[arg( long, long_help = "Amount of memory available (in GBs)", - env = "GEVULOT_MEM_GB", - default_value_t = 8 + env = "GEVULOT_MEM_GB" )] - pub mem_gb: u64, + pub mem_gb: Option, #[arg(long, long_help = "GPU PCI devices", env = "GEVULOT_GPU_DEVICES")] pub gpu_devices: Option, diff --git a/crates/node/src/main.rs b/crates/node/src/main.rs index 50b5d8f4..f7b84d2d 100644 --- a/crates/node/src/main.rs +++ b/crates/node/src/main.rs @@ -22,6 +22,7 @@ use std::{ thread::sleep, time::Duration, }; +use systemstat::{ByteSize, Platform, System}; use tokio::sync::mpsc; use tokio::sync::{Mutex as TMutex, RwLock}; use tokio_stream::wrappers::UnboundedReceiverStream; @@ -234,11 +235,30 @@ async fn run(config: Arc) -> Result<()> { let p2p_listen_addr = p2p.node().start_listening().await?; tracing::info!("listening for p2p at {}", p2p_listen_addr); - // TODO(tuommaki): read total available resources from config / acquire system stats. + let sys = System::new(); let num_gpus = if config.gpu_devices.is_some() { 1 } else { 0 }; + let num_cpus = match config.num_cpus { + Some(cpus) => cpus, + None => num_cpus::get() as u64, + }; + let available_mem = match config.mem_gb { + Some(mem_gb) => mem_gb * 1024 * 1024 * 1024, + None => { + let mem = sys.memory()?; + mem.total.as_u64() + } + }; + + tracing::info!( + "node configured with {} CPUs, {} MEM and {} GPUs", + num_cpus, + ByteSize(available_mem).to_string_as(true), + num_gpus + ); + let resource_manager = Arc::new(Mutex::new(scheduler::ResourceManager::new( - config.mem_gb * 1024 * 1024 * 1024, - config.num_cpus, + available_mem, + num_cpus, num_gpus, ))); diff --git a/crates/node/src/rpc_server/mod.rs b/crates/node/src/rpc_server/mod.rs index da58dc26..2372ce66 100644 --- a/crates/node/src/rpc_server/mod.rs +++ b/crates/node/src/rpc_server/mod.rs @@ -442,8 +442,8 @@ mod tests { p2p_advertised_listen_addr: None, provider: "qemu".to_string(), vsock_listen_port: 8080, - num_cpus: 8, - mem_gb: 8, + num_cpus: None, + mem_gb: None, gpu_devices: None, http_download_port: 0, });