Skip to content

Commit bcd1ec6

Browse files
committed
add getting system metrics to rpc
1 parent 5d5822a commit bcd1ec6

7 files changed

Lines changed: 207 additions & 73 deletions

File tree

.github/workflows/pr-review.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ jobs:
6666
VCS__PIPELINE__PULL_NUMBER: ${{ github.event.number}}
6767
VCS__HTTP_CLIENT__API_URL: "https://api.github.com"
6868
VCS__HTTP_CLIENT__API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
69+
REVIEW__IGNORE_CHANGES: '["Cargo.lock"]'

Cargo.lock

Lines changed: 98 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coman/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ rust_supervisor = "0.2.0"
7979
iroh-ssh = "0.2.7"
8080
whoami = "1.6.1"
8181
base64 = "0.22.1"
82-
dirs = "6.0.0"
8382
iroh = "0.95.1"
8483
rand = "0.9.2"
8584
regex = "1.12.2"
@@ -91,6 +90,9 @@ tarpc = { version = "0.37.0", features = [
9190
"tokio1",
9291
] }
9392
tokio-duplex = "1.0.1"
93+
sysinfo = "0.38.0"
94+
nvml-wrapper = "0.11.0"
95+
bytesize = "2.3.1"
9496

9597
[build-dependencies]
9698
anyhow = "1.0.90"

coman/src/cli/exec.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use iroh::{
77
endpoint::ConnectionError,
88
protocol::{ProtocolHandler, Router},
99
};
10-
use nom::AsBytes;
1110
use pid1::Pid1Settings;
1211
use rust_supervisor::{ChildType, Supervisor, SupervisorConfig};
1312
use tokio::{io::AsyncWriteExt, net::TcpStream};
@@ -127,7 +126,7 @@ async fn port_forward() -> Result<()> {
127126

128127
// add rpc server
129128
let rpc_handler = RpcHandler;
130-
builder = builder.accept(b"/coman/rpc/".as_bytes(), rpc_handler);
129+
builder = builder.accept(b"/coman/rpc", rpc_handler);
131130
let _router = builder.spawn();
132131
println!("port forwarding started");
133132

coman/src/cli/rpc.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use futures::StreamExt;
22
use iroh::protocol::ProtocolHandler;
3+
use nvml_wrapper::Nvml;
4+
use serde::{Deserialize, Serialize};
5+
use sysinfo::System;
36
use tarpc::{
47
serde_transport as transport, server, server::Channel, tokio_serde::formats::Bincode,
58
tokio_util::codec::LengthDelimitedCodec,
@@ -8,9 +11,18 @@ use tokio_duplex::Duplex;
811

912
use crate::cli::app::COMAN_VERSION;
1013

14+
#[derive(Debug, Clone, Serialize, Deserialize)]
15+
pub struct ResourceUsage {
16+
pub cpu: f32,
17+
pub mem_used: u64,
18+
pub mem_total: u64,
19+
pub gpu: Option<u64>,
20+
}
21+
1122
#[tarpc::service]
1223
pub trait ComanRPC {
1324
async fn version() -> String;
25+
async fn resource_usage() -> ResourceUsage;
1426
}
1527
#[derive(Debug, Clone)]
1628
struct RpcServer;
@@ -19,6 +31,41 @@ impl ComanRPC for RpcServer {
1931
async fn version(self, _: tarpc::context::Context) -> String {
2032
COMAN_VERSION.to_string()
2133
}
34+
35+
async fn resource_usage(self, _context: ::tarpc::context::Context) -> ResourceUsage {
36+
let mut sys = System::new_all();
37+
sys.refresh_all();
38+
let mut cpu_usage = 0.0;
39+
for cpu in sys.cpus() {
40+
cpu_usage += cpu.cpu_usage();
41+
}
42+
cpu_usage /= sys.cpus().len() as f32;
43+
let gpu_usage = match Nvml::init() {
44+
Ok(nvml) => match nvml.device_by_index(0) {
45+
Ok(device) => match device.memory_info() {
46+
Ok(memory_info) => Some(memory_info.used),
47+
Err(e) => {
48+
println!("Couldn't get GPU memory info: {e:?}");
49+
None
50+
}
51+
},
52+
Err(e) => {
53+
println!("couldn't load nvidia device 0: {e:?}");
54+
None
55+
}
56+
},
57+
Err(e) => {
58+
println!("Nvidia Device Info not available: {e:?}");
59+
None
60+
}
61+
};
62+
ResourceUsage {
63+
cpu: cpu_usage,
64+
mem_used: sys.used_memory(),
65+
mem_total: sys.total_memory(),
66+
gpu: gpu_usage,
67+
}
68+
}
2269
}
2370

2471
#[derive(Debug, Default)]

0 commit comments

Comments
 (0)