99//! On non-Linux targets this is a no-op stub so local development on macOS
1010//! still compiles and runs.
1111
12+ #[ cfg( target_os = "linux" ) ]
13+ use std:: path:: Path ;
1214use std:: { path:: PathBuf , sync:: Arc , time:: Duration } ;
1315
1416use parking_lot:: Mutex ;
1517use serde:: Serialize ;
16- use tokio:: task:: JoinHandle ;
17- use tracing:: warn;
18-
19- #[ cfg( target_os = "linux" ) ]
20- use std:: path:: Path ;
2118#[ cfg( target_os = "linux" ) ]
2219use time:: OffsetDateTime ;
20+ use tokio:: task:: JoinHandle ;
21+ use tracing:: warn;
2322
2423/// Handle to a running sampler. Holding it does nothing; drop it to leave
2524/// the sampler running, or call [`CpuSampler::stop`] to flush + join.
@@ -138,11 +137,7 @@ async fn run_sampler(inner: Arc<Inner>, tick: Duration) {
138137 // ---- per-thread /proc/self/task/<tid>/stat ----
139138 if let Ok ( rd) = std:: fs:: read_dir ( "/proc/self/task" ) {
140139 for ent in rd. flatten ( ) {
141- let tid: i64 = match ent
142- . file_name ( )
143- . to_str ( )
144- . and_then ( |s| s. parse ( ) . ok ( ) )
145- {
140+ let tid: i64 = match ent. file_name ( ) . to_str ( ) . and_then ( |s| s. parse ( ) . ok ( ) ) {
146141 Some ( v) => v,
147142 None => continue ,
148143 } ;
@@ -175,8 +170,8 @@ async fn run_sampler(inner: Arc<Inner>, tick: Duration) {
175170 if dt > 0 {
176171 let user = ( ticks. user . saturating_sub ( p. user ) ) as f64 / dt as f64 ;
177172 let sys_ = ( ticks. system . saturating_sub ( p. system ) ) as f64 / dt as f64 ;
178- let iow = ( ticks. iowait . saturating_sub ( p. iowait ) ) as f64 / dt as f64 ;
179- let idl = ( ticks. idle . saturating_sub ( p. idle ) ) as f64 / dt as f64 ;
173+ let iow = ( ticks. iowait . saturating_sub ( p. iowait ) ) as f64 / dt as f64 ;
174+ let idl = ( ticks. idle . saturating_sub ( p. idle ) ) as f64 / dt as f64 ;
180175 inner. core_rows . lock ( ) . push ( CoreRow {
181176 t_ns,
182177 cpu_id,
@@ -226,17 +221,21 @@ fn flush(inner: &Inner) -> std::io::Result<()> {
226221 let path = inner. out_dir . join ( format ! ( "cpu_node{}.csv" , inner. node_id) ) ;
227222 let mut wtr = csv:: Writer :: from_path ( & path) ?;
228223 for r in cpu_rows {
229- wtr. serialize ( r) . map_err ( |e| std:: io:: Error :: other ( e. to_string ( ) ) ) ?;
224+ wtr. serialize ( r)
225+ . map_err ( |e| std:: io:: Error :: other ( e. to_string ( ) ) ) ?;
230226 }
231227 wtr. flush ( ) ?;
232228 }
233229
234230 let core_rows = std:: mem:: take ( & mut * inner. core_rows . lock ( ) ) ;
235231 if !core_rows. is_empty ( ) {
236- let path = inner. out_dir . join ( format ! ( "core_node{}.csv" , inner. node_id) ) ;
232+ let path = inner
233+ . out_dir
234+ . join ( format ! ( "core_node{}.csv" , inner. node_id) ) ;
237235 let mut wtr = csv:: Writer :: from_path ( & path) ?;
238236 for r in core_rows {
239- wtr. serialize ( r) . map_err ( |e| std:: io:: Error :: other ( e. to_string ( ) ) ) ?;
237+ wtr. serialize ( r)
238+ . map_err ( |e| std:: io:: Error :: other ( e. to_string ( ) ) ) ?;
240239 }
241240 wtr. flush ( ) ?;
242241 }
@@ -246,7 +245,8 @@ fn flush(inner: &Inner) -> std::io::Result<()> {
246245 let path = inner. out_dir . join ( format ! ( "net_node{}.csv" , inner. node_id) ) ;
247246 let mut wtr = csv:: Writer :: from_path ( & path) ?;
248247 for r in net_rows {
249- wtr. serialize ( r) . map_err ( |e| std:: io:: Error :: other ( e. to_string ( ) ) ) ?;
248+ wtr. serialize ( r)
249+ . map_err ( |e| std:: io:: Error :: other ( e. to_string ( ) ) ) ?;
250250 }
251251 wtr. flush ( ) ?;
252252 }
@@ -269,8 +269,14 @@ struct CoreTicks {
269269#[ cfg( target_os = "linux" ) ]
270270impl CoreTicks {
271271 fn total ( & self ) -> u64 {
272- self . user + self . nice + self . system + self . idle
273- + self . iowait + self . irq + self . softirq + self . steal
272+ self . user
273+ + self . nice
274+ + self . system
275+ + self . idle
276+ + self . iowait
277+ + self . irq
278+ + self . softirq
279+ + self . steal
274280 }
275281}
276282
@@ -309,16 +315,19 @@ fn read_proc_stat_cores() -> Option<Vec<(u32, CoreTicks)>> {
309315 if nums. len ( ) < 8 {
310316 continue ;
311317 }
312- out. push ( ( cpu_id, CoreTicks {
313- user : nums[ 0 ] ,
314- nice : nums[ 1 ] ,
315- system : nums[ 2 ] ,
316- idle : nums[ 3 ] ,
317- iowait : nums[ 4 ] ,
318- irq : nums[ 5 ] ,
319- softirq : nums[ 6 ] ,
320- steal : nums[ 7 ] ,
321- } ) ) ;
318+ out. push ( (
319+ cpu_id,
320+ CoreTicks {
321+ user : nums[ 0 ] ,
322+ nice : nums[ 1 ] ,
323+ system : nums[ 2 ] ,
324+ idle : nums[ 3 ] ,
325+ iowait : nums[ 4 ] ,
326+ irq : nums[ 5 ] ,
327+ softirq : nums[ 6 ] ,
328+ steal : nums[ 7 ] ,
329+ } ,
330+ ) ) ;
322331 }
323332 Some ( out)
324333}
@@ -342,7 +351,10 @@ fn read_proc_net_dev() -> Option<Vec<(String, u64, u64)>> {
342351 if iface == "lo" || iface. is_empty ( ) {
343352 continue ;
344353 }
345- let nums: Vec < u64 > = rest. split_whitespace ( ) . filter_map ( |f| f. parse ( ) . ok ( ) ) . collect ( ) ;
354+ let nums: Vec < u64 > = rest
355+ . split_whitespace ( )
356+ . filter_map ( |f| f. parse ( ) . ok ( ) )
357+ . collect ( ) ;
346358 if nums. len ( ) < 9 {
347359 continue ;
348360 }
@@ -384,8 +396,8 @@ mod tests {
384396 // Real-world example: comm can contain spaces, parens, etc.
385397 // Fields after comm: state ppid pgrp session tty_nr tpgid flags
386398 // minflt cminflt majflt cmajflt UTIME STIME ...
387- let line = "1234 ((my proc (foo))) S 1 1 1 0 -1 4194304 \
388- 100 200 0 0 1500 750 0 0 20 0 8 0 1234 0 0";
399+ let line = "1234 ((my proc (foo))) S 1 1 1 0 -1 4194304 100 200 0 0 1500 750 0 0 20 0 8 0 \
400+ 1234 0 0";
389401 let ( u, s) = parse_stat ( line) . expect ( "parse" ) ;
390402 assert_eq ! ( u, 1500 ) ;
391403 assert_eq ! ( s, 750 ) ;
0 commit comments