@@ -20,31 +20,17 @@ use crate::metrics::ConnectorType;
2020use iggy_common:: { IggyTimestamp , SemanticVersion } ;
2121use iggy_connector_sdk:: api:: { ConnectorRuntimeStats , ConnectorStats } ;
2222use std:: str:: FromStr ;
23- use std:: sync:: Arc ;
23+ use std:: sync:: { Arc , Mutex , OnceLock , PoisonError } ;
2424use sysinfo:: System ;
25+ use system_stats:: SystemProbe ;
2526
2627const VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
2728const SEMANTIC_VERSION : SemanticVersion = SemanticVersion :: parse_const ( VERSION ) ;
2829
29- pub async fn get_runtime_stats ( context : & Arc < RuntimeContext > ) -> ConnectorRuntimeStats {
30- let pid = std:: process:: id ( ) ;
31-
32- let mut system = System :: new_all ( ) ;
33- system. refresh_cpu_all ( ) ;
34- system. refresh_memory ( ) ;
35- system. refresh_processes (
36- sysinfo:: ProcessesToUpdate :: Some ( & [ sysinfo:: Pid :: from_u32 ( pid) ] ) ,
37- true ,
38- ) ;
39-
40- let total_cpu_usage = system. global_cpu_usage ( ) ;
41- let total_memory = system. total_memory ( ) ;
42- let available_memory = system. available_memory ( ) ;
30+ static SYSINFO : OnceLock < Mutex < System > > = OnceLock :: new ( ) ;
4331
44- let ( cpu_usage, memory_usage) = system
45- . process ( sysinfo:: Pid :: from_u32 ( pid) )
46- . map ( |p| ( p. cpu_usage ( ) , p. memory ( ) ) )
47- . unwrap_or ( ( 0.0 , 0 ) ) ;
32+ pub async fn get_runtime_stats ( context : & Arc < RuntimeContext > ) -> ConnectorRuntimeStats {
33+ let system = probe_system ( ) ;
4834
4935 let sources = context. sources . get_all ( ) . await ;
5036 let sinks = context. sinks . get_all ( ) . await ;
@@ -113,12 +99,12 @@ pub async fn get_runtime_stats(context: &Arc<RuntimeContext>) -> ConnectorRuntim
11399 ConnectorRuntimeStats {
114100 connectors_runtime_version : VERSION . to_owned ( ) ,
115101 connectors_runtime_version_semver : SEMANTIC_VERSION . get_numeric_version ( ) . ok ( ) ,
116- process_id : pid ,
117- cpu_usage,
118- total_cpu_usage,
119- memory_usage,
120- total_memory,
121- available_memory,
102+ process_id : system . process_id ,
103+ cpu_usage : system . cpu_usage ,
104+ total_cpu_usage : system . total_cpu_usage ,
105+ memory_usage : system . memory_usage ,
106+ total_memory : system . total_memory ,
107+ available_memory : system . available_memory ,
122108 run_time,
123109 start_time : start,
124110 sources_total,
@@ -128,3 +114,11 @@ pub async fn get_runtime_stats(context: &Arc<RuntimeContext>) -> ConnectorRuntim
128114 connectors,
129115 }
130116}
117+
118+ fn probe_system ( ) -> SystemProbe {
119+ let mut system = SYSINFO
120+ . get_or_init ( || Mutex :: new ( System :: new ( ) ) )
121+ . lock ( )
122+ . unwrap_or_else ( PoisonError :: into_inner) ;
123+ SystemProbe :: capture ( & mut system)
124+ }
0 commit comments