11use futures:: StreamExt ;
22use iroh:: protocol:: ProtocolHandler ;
3- use tarpc:: {
4- serde_transport as transport, server, server:: Channel , tokio_serde:: formats:: Bincode ,
5- tokio_util:: codec:: LengthDelimitedCodec ,
6- } ;
3+ use nvml_wrapper:: Nvml ;
4+ use serde:: { Deserialize , Serialize } ;
5+ use sysinfo:: System ;
6+ use tarpc:: server:: Channel ;
7+ use tarpc:: tokio_serde:: formats:: Bincode ;
8+ use tarpc:: tokio_util:: codec:: LengthDelimitedCodec ;
9+ use tarpc:: { serde_transport as transport, server} ;
710use tokio_duplex:: Duplex ;
811
912use crate :: cli:: app:: COMAN_VERSION ;
1013
14+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
15+ pub struct ResourceUsage {
16+ pub cpu : f32 ,
17+ pub rss : u64 ,
18+ pub gpu : u64 ,
19+ }
20+
1121#[ tarpc:: service]
1222pub trait ComanRPC {
1323 async fn version ( ) -> String ;
24+ async fn resource_usage ( ) -> ResourceUsage ;
1425}
1526#[ derive( Debug , Clone ) ]
1627struct RpcServer ;
@@ -19,6 +30,24 @@ impl ComanRPC for RpcServer {
1930 async fn version ( self , _: tarpc:: context:: Context ) -> String {
2031 COMAN_VERSION . to_string ( )
2132 }
33+
34+ async fn resource_usage ( self , _context : :: tarpc:: context:: Context ) -> ResourceUsage {
35+ let mut sys = System :: new_all ( ) ;
36+ sys. refresh_all ( ) ;
37+ let mut cpu_usage = 0.0 ;
38+ for cpu in sys. cpus ( ) {
39+ cpu_usage += cpu. cpu_usage ( ) ;
40+ }
41+ cpu_usage /= sys. cpus ( ) . len ( ) as f32 ;
42+ let nvml = Nvml :: init ( ) . unwrap ( ) ;
43+ let device = nvml. device_by_index ( 0 ) . unwrap ( ) ;
44+ let memory_info = device. memory_info ( ) . unwrap ( ) ;
45+ ResourceUsage {
46+ cpu : cpu_usage,
47+ rss : sys. used_memory ( ) ,
48+ gpu : memory_info. used ,
49+ }
50+ }
2251}
2352
2453#[ derive( Debug , Default ) ]
0 commit comments