1
- use crate :: MemoryUsage ;
2
1
use std:: {
3
2
fmt,
4
3
time:: { Duration , Instant } ,
5
4
} ;
6
5
6
+ use crate :: MemoryUsage ;
7
+
7
8
pub struct StopWatch {
8
9
time : Instant ,
10
+ #[ cfg( target_os = "linux" ) ]
9
11
counter : Option < perf_event:: Counter > ,
10
12
memory : Option < MemoryUsage > ,
11
13
}
@@ -18,12 +20,21 @@ pub struct StopWatchSpan {
18
20
19
21
impl StopWatch {
20
22
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
+ } ;
25
31
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
+ }
27
38
}
28
39
pub fn memory ( mut self , yes : bool ) -> StopWatch {
29
40
if yes {
@@ -33,7 +44,12 @@ impl StopWatch {
33
44
}
34
45
pub fn elapsed ( & mut self ) -> StopWatchSpan {
35
46
let time = self . time . elapsed ( ) ;
47
+
48
+ #[ cfg( target_os = "linux" ) ]
36
49
let instructions = self . counter . as_mut ( ) . and_then ( |it| it. read ( ) . ok ( ) ) ;
50
+ #[ cfg( not( target_os = "linux" ) ) ]
51
+ let instructions = None ;
52
+
37
53
let memory = self . memory . map ( |it| MemoryUsage :: current ( ) - it) ;
38
54
StopWatchSpan { time, instructions, memory }
39
55
}
@@ -65,6 +81,7 @@ impl fmt::Display for StopWatchSpan {
65
81
// https://github.com/jimblandy/perf-event/issues/8
66
82
impl Drop for StopWatch {
67
83
fn drop ( & mut self ) {
84
+ #[ cfg( target_os = "linux" ) ]
68
85
if let Some ( mut counter) = self . counter . take ( ) {
69
86
let _ = counter. disable ( ) ;
70
87
}
0 commit comments