1+ use bytesize:: ByteSize ;
12use futures:: StreamExt ;
23use iroh:: protocol:: ProtocolHandler ;
3- use nvml_wrapper:: Nvml ;
44use serde:: { Deserialize , Serialize } ;
55use sysinfo:: { ProcessRefreshKind , ProcessesToUpdate , System , get_current_pid} ;
66use tarpc:: {
@@ -16,7 +16,7 @@ pub struct ResourceUsage {
1616 pub cpu : f32 ,
1717 pub rss : u64 ,
1818 pub vss : u64 ,
19- pub gpu : Option < u64 > ,
19+ pub gpu : Option < Vec < ( u64 , u64 ) > > ,
2020}
2121
2222#[ tarpc:: service]
@@ -43,25 +43,30 @@ impl ComanRPC for RpcServer {
4343 let Some ( process) = sys. process ( pid) else {
4444 return ResourceUsage :: default ( ) ;
4545 } ;
46- let gpu_usage = match Nvml :: init ( ) {
47- Ok ( nvml) => match nvml. device_by_index ( 0 ) {
48- Ok ( device) => match device. memory_info ( ) {
49- Ok ( memory_info) => Some ( memory_info. used ) ,
50- Err ( e) => {
51- println ! ( "Couldn't get GPU memory info: {e:?}" ) ;
52- None
53- }
54- } ,
55- Err ( e) => {
56- println ! ( "couldn't load nvidia device 0: {e:?}" ) ;
57- None
58- }
59- } ,
60- Err ( e) => {
61- println ! ( "Nvidia Device Info not available: {e:?}" ) ;
62- None
63- }
46+ let gpu_usage = if let Ok ( output) = std:: process:: Command :: new ( "nvidia-smi" )
47+ . args ( vec ! [
48+ "--query-gpu=memory.total,memory.used" ,
49+ "--format=csv,noheader,nounits" ,
50+ ] )
51+ . output ( )
52+ {
53+ let output = String :: from_utf8_lossy ( & output. stdout ) ;
54+ let usage = output
55+ . lines ( )
56+ . map ( |l| l. split_once ( "," ) . unwrap ( ) )
57+ . map ( |( total, used) | {
58+ (
59+ ByteSize :: mib ( total. trim ( ) . parse :: < u64 > ( ) . unwrap ( ) ) . as_u64 ( ) ,
60+ ByteSize :: mib ( used. trim ( ) . parse :: < u64 > ( ) . unwrap ( ) ) . as_u64 ( ) ,
61+ )
62+ } )
63+ . collect ( ) ;
64+ Some ( usage)
65+ } else {
66+ println ! ( "Failed to execute nvidia-smi, maybe it's not installed" ) ;
67+ None
6468 } ;
69+
6570 ResourceUsage {
6671 cpu : process. cpu_usage ( ) / sys. cpus ( ) . len ( ) as f32 ,
6772 rss : process. memory ( ) ,
0 commit comments