diff --git a/zeusd/Cargo.toml b/zeusd/Cargo.toml index c5d66cce..cfa294d6 100644 --- a/zeusd/Cargo.toml +++ b/zeusd/Cargo.toml @@ -20,7 +20,7 @@ name = "zeusd" [dependencies] nvml-wrapper = "0.10" actix-web = "4" -tokio = { version = "1", features = ["macros", "rt-multi-thread"] } +tokio = { version = "1", features = ["macros", "rt-multi-thread", "fs"] } thiserror = "1" clap = { version = "4.5.4", features = ["derive"] } serde = { version = "1", features = ["derive"] } diff --git a/zeusd/src/devices/cpu/mod.rs b/zeusd/src/devices/cpu/mod.rs new file mode 100644 index 00000000..5d71a3e8 --- /dev/null +++ b/zeusd/src/devices/cpu/mod.rs @@ -0,0 +1,47 @@ +//! CPU power measurement with RAPL. Only supported on Linux. + +use std::io::Read; + +use tokio::io::AsyncReadExt; + +pub struct FieldInfo { + pub name: String, + pub max_energy_uj: f64, +} + +pub trait CpuManager { + fn get_available_fields(&self) -> Vec; + fn get_field_energy(&self, field_name: &str) -> f64; +} + +static RAPL_DIR: &'static str = "/sys/class/powercap/intel-rapl"; + +pub struct RAPLCpuManager { + fields: Vec, +} + +impl RAPLCpuManager { + pub fn new() -> Self { + let mut fields = Vec::with_capacity(2); + + // The package domain is always present + + // Look for subdomains and add to fields + + Self { fields } + } +} + +fn read_u64(path: &std::path::Path) -> f64 { + let mut file = std::fs::File::open(path).unwrap(); + let mut buf = String::new(); + file.read_to_string(&mut buf).unwrap(); + buf.trim().parse().unwrap() +} + +async fn read_u64_async(path: &std::path::Path) -> f64 { + let mut file = tokio::fs::File::open(path).await.unwrap(); + let mut buf = String::new(); + file.read_to_string(&mut buf).await.unwrap(); + buf.trim().parse().unwrap() +} diff --git a/zeusd/src/devices/mod.rs b/zeusd/src/devices/mod.rs index eaeb673a..d1010fa9 100644 --- a/zeusd/src/devices/mod.rs +++ b/zeusd/src/devices/mod.rs @@ -1,3 +1,4 @@ //! Interfaces for interacting with devices pub mod gpu; +pub mod cpu; diff --git a/zeusd/src/routes/gpu.rs b/zeusd/src/routes/gpu.rs index b0d5bf75..ec0ce50b 100644 --- a/zeusd/src/routes/gpu.rs +++ b/zeusd/src/routes/gpu.rs @@ -13,7 +13,7 @@ use crate::error::ZeusdError; /// This macro takes /// - the API name (set_power_limit, set_persistence_mode, etc.), /// - the method and path for the request handler, -/// - and a list of `field name ` pairs of the corresponding `GpuCommand` variant. +/// - and a list of `field name: type` pairs of the corresponding `GpuCommand` variant. /// /// Gien this, the macro generates /// - a request payload struct named API name (e.g., SetPowerLimit) and all the