From ce0537d693ec1c1372b3b046bf12c6adceefc8b6 Mon Sep 17 00:00:00 2001 From: Eliel <46634774+elielnfinic@users.noreply.github.com> Date: Mon, 22 Apr 2024 21:50:15 +0200 Subject: [PATCH 1/7] Implementing get_cpu_temps --- Cargo.lock | 17 ++++++++++++++++- Cargo.toml | 1 + src/rpc_server.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 62d6e58f..0b7293af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1608,6 +1608,7 @@ dependencies = [ "serde_json", "sha3", "strum 0.25.0", + "systemstat", "tarpc", "tasm-lib", "test-strategy", @@ -2628,6 +2629,20 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[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 = "tarpc" version = "0.34.0" @@ -2673,7 +2688,7 @@ dependencies = [ "const_format", "derive_tasm_object", "hex", - "itertools 0.10.5", + "itertools 0.12.1", "ndarray", "num", "num-traits", diff --git a/Cargo.toml b/Cargo.toml index c2316609..8154c3c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,6 +66,7 @@ leveldb-sys = "2.0.9" async-trait = "0.1.77" async-stream = "0.3.5" sha3 = "0.10.8" +systemstat = "0.2.3" [dev-dependencies] test-strategy = "0.3" diff --git a/src/rpc_server.rs b/src/rpc_server.rs index 03a3db83..4e5b171a 100644 --- a/src/rpc_server.rs +++ b/src/rpc_server.rs @@ -10,6 +10,7 @@ use std::collections::HashMap; use std::net::IpAddr; use std::net::SocketAddr; use std::str::FromStr; +use systemstat::{Platform, System}; use tarpc::context; use tokio::sync::mpsc::error::SendError; use tracing::{error, info}; @@ -196,6 +197,9 @@ pub trait RPC { /// Gracious shutdown. async fn shutdown() -> bool; + + /// Get CPU temperature. + async fn get_cpu_temps() -> Option; } #[derive(Clone)] @@ -718,6 +722,14 @@ impl RPC for NeptuneRPCServer { .get_all_own_coins_with_possible_timelocks() .await } + + async fn get_cpu_temps(self, _context: tarpc::context::Context) -> Option { + let current_system = System::new(); + match current_system.cpu_temp() { + Ok(temp) => Some(temp.into()), + Err(_) => None, + } + } } #[cfg(test)] @@ -1094,4 +1106,19 @@ mod rpc_server_tests { Ok(()) } + + #[allow(clippy::shadow_unrelated)] + #[traced_test] + #[tokio::test] + async fn test_can_get_server_temperature() { + let (rpc_server, state_lock) = + test_rpc_server(Network::Alpha, WalletSecret::new_random(), 2).await; + let current_server_temperature = rpc_server.get_cpu_temps(context::current()).await; + // Silicon Macs do not provide CPU temperature using systemstat + if std::env::consts::OS == "macos" { + assert!(current_server_temperature.is_none()); + } else { + assert!(current_server_temperature.is_some()); + } + } } From 3ce1b094d450d7153570455e9c8a43cef47ece45 Mon Sep 17 00:00:00 2001 From: Eliel <46634774+elielnfinic@users.noreply.github.com> Date: Tue, 23 Apr 2024 07:14:41 +0200 Subject: [PATCH 2/7] Fixing unused variable : --- src/rpc_server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc_server.rs b/src/rpc_server.rs index 4e5b171a..a67de4d1 100644 --- a/src/rpc_server.rs +++ b/src/rpc_server.rs @@ -1111,7 +1111,7 @@ mod rpc_server_tests { #[traced_test] #[tokio::test] async fn test_can_get_server_temperature() { - let (rpc_server, state_lock) = + let (rpc_server, _state_lock) = test_rpc_server(Network::Alpha, WalletSecret::new_random(), 2).await; let current_server_temperature = rpc_server.get_cpu_temps(context::current()).await; // Silicon Macs do not provide CPU temperature using systemstat From 5d671e4d8394370288cc740e43f9685e4f9119c3 Mon Sep 17 00:00:00 2001 From: Eliel <46634774+elielnfinic@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:29:36 +0200 Subject: [PATCH 3/7] Updating get_cpu_temps to cpu_temp --- src/rpc_server.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rpc_server.rs b/src/rpc_server.rs index a67de4d1..7c67d528 100644 --- a/src/rpc_server.rs +++ b/src/rpc_server.rs @@ -199,7 +199,7 @@ pub trait RPC { async fn shutdown() -> bool; /// Get CPU temperature. - async fn get_cpu_temps() -> Option; + async fn cpu_temp() -> Option; } #[derive(Clone)] @@ -723,7 +723,7 @@ impl RPC for NeptuneRPCServer { .await } - async fn get_cpu_temps(self, _context: tarpc::context::Context) -> Option { + async fn cpu_temp(self, _context: tarpc::context::Context) -> Option { let current_system = System::new(); match current_system.cpu_temp() { Ok(temp) => Some(temp.into()), @@ -1113,7 +1113,7 @@ mod rpc_server_tests { async fn test_can_get_server_temperature() { let (rpc_server, _state_lock) = test_rpc_server(Network::Alpha, WalletSecret::new_random(), 2).await; - let current_server_temperature = rpc_server.get_cpu_temps(context::current()).await; + let current_server_temperature = rpc_server.cpu_temp(context::current()).await; // Silicon Macs do not provide CPU temperature using systemstat if std::env::consts::OS == "macos" { assert!(current_server_temperature.is_none()); From e7d7a717ead04b1cb3e0ad8cb9fcddaf617f131a Mon Sep 17 00:00:00 2001 From: Eliel <46634774+elielnfinic@users.noreply.github.com> Date: Thu, 25 Apr 2024 17:47:10 +0200 Subject: [PATCH 4/7] Resolving conflict and refactoring --- src/rpc_server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc_server.rs b/src/rpc_server.rs index a452eba1..0126c67b 100644 --- a/src/rpc_server.rs +++ b/src/rpc_server.rs @@ -1148,7 +1148,7 @@ mod rpc_server_tests { } else { assert!(current_server_temperature.is_some()); } - } + } #[traced_test] #[tokio::test] From bc79466b6bf0ee2ba6230aa7d32e71a8fc0c0ff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eliel=20=EC=88=98=ED=95=99?= <46634774+elielnfinic@users.noreply.github.com> Date: Thu, 2 May 2024 07:15:21 +0200 Subject: [PATCH 5/7] Update src/rpc_server.rs Co-authored-by: dan-da --- src/rpc_server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc_server.rs b/src/rpc_server.rs index 0126c67b..110563ca 100644 --- a/src/rpc_server.rs +++ b/src/rpc_server.rs @@ -749,7 +749,7 @@ impl RPC for NeptuneRPCServer { .await } - async fn cpu_temp(self, _context: tarpc::context::Context) -> Option { + async fn cpu_temp(self, _context: Context) -> Option { let current_system = System::new(); match current_system.cpu_temp() { Ok(temp) => Some(temp.into()), From 9fa8d82a08815dda949ca7e39633de40d09be3ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eliel=20=EC=88=98=ED=95=99?= <46634774+elielnfinic@users.noreply.github.com> Date: Thu, 2 May 2024 05:33:09 +0000 Subject: [PATCH 6/7] Fixed latest nits by @dan-da --- src/rpc_server.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rpc_server.rs b/src/rpc_server.rs index 1543cd3d..1d5784a0 100644 --- a/src/rpc_server.rs +++ b/src/rpc_server.rs @@ -1138,7 +1138,6 @@ mod rpc_server_tests { Ok(()) } - #[allow(clippy::shadow_unrelated)] #[traced_test] #[tokio::test] async fn test_can_get_server_temperature() { From 6cd89c4be1505350163a7e5a45ad4348d8ce1e78 Mon Sep 17 00:00:00 2001 From: Eliel <46634774+elielnfinic@users.noreply.github.com> Date: Sun, 19 May 2024 11:59:07 +0200 Subject: [PATCH 7/7] Fixing Context object import on cpu_temp --- src/rpc_server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc_server.rs b/src/rpc_server.rs index 1d5784a0..a7b0aed0 100644 --- a/src/rpc_server.rs +++ b/src/rpc_server.rs @@ -752,7 +752,7 @@ impl RPC for NeptuneRPCServer { .await } - async fn cpu_temp(self, _context: Context) -> Option { + async fn cpu_temp(self, _context: tarpc::context::Context) -> Option { let current_system = System::new(); match current_system.cpu_temp() { Ok(temp) => Some(temp.into()),