1- use crate :: MemoryUsage ;
21use std:: {
32 fmt,
43 time:: { Duration , Instant } ,
54} ;
65
6+ use crate :: MemoryUsage ;
7+
78pub struct StopWatch {
89 time : Instant ,
10+ #[ cfg( target_os = "linux" ) ]
911 counter : Option < perf_event:: Counter > ,
1012 memory : Option < MemoryUsage > ,
1113}
@@ -18,12 +20,21 @@ pub struct StopWatchSpan {
1820
1921impl StopWatch {
2022 pub fn start ( ) -> StopWatch {
21- let mut counter = perf_event:: Builder :: new ( ) . build ( ) . ok ( ) ;
22- if let Some ( counter) = & mut counter {
23- let _ = counter. enable ( ) ;
24- }
23+ #[ cfg( target_os = "linux" ) ]
24+ let counter = {
25+ let mut counter = perf_event:: Builder :: new ( ) . build ( ) . ok ( ) ;
26+ if let Some ( counter) = & mut counter {
27+ let _ = counter. enable ( ) ;
28+ }
29+ counter
30+ } ;
2531 let time = Instant :: now ( ) ;
26- StopWatch { time, counter, memory : None }
32+ StopWatch {
33+ time,
34+ #[ cfg( target_os = "linux" ) ]
35+ counter,
36+ memory : None ,
37+ }
2738 }
2839 pub fn memory ( mut self , yes : bool ) -> StopWatch {
2940 if yes {
@@ -33,7 +44,12 @@ impl StopWatch {
3344 }
3445 pub fn elapsed ( & mut self ) -> StopWatchSpan {
3546 let time = self . time . elapsed ( ) ;
47+
48+ #[ cfg( target_os = "linux" ) ]
3649 let instructions = self . counter . as_mut ( ) . and_then ( |it| it. read ( ) . ok ( ) ) ;
50+ #[ cfg( not( target_os = "linux" ) ) ]
51+ let instructions = None ;
52+
3753 let memory = self . memory . map ( |it| MemoryUsage :: current ( ) - it) ;
3854 StopWatchSpan { time, instructions, memory }
3955 }
@@ -65,6 +81,7 @@ impl fmt::Display for StopWatchSpan {
6581// https://github.com/jimblandy/perf-event/issues/8
6682impl Drop for StopWatch {
6783 fn drop ( & mut self ) {
84+ #[ cfg( target_os = "linux" ) ]
6885 if let Some ( mut counter) = self . counter . take ( ) {
6986 let _ = counter. disable ( ) ;
7087 }
0 commit comments