19
19
20
20
use std:: collections:: HashMap ;
21
21
use std:: iter:: Sum ;
22
+ use std:: mem;
22
23
23
24
use gazebo:: prelude:: * ;
24
25
25
26
use crate :: eval:: bc:: opcode:: BcOpcode ;
26
27
use crate :: eval:: runtime:: evaluator:: EvaluatorError ;
27
28
use crate :: eval:: runtime:: profile:: csv:: CsvWriter ;
29
+ use crate :: eval:: runtime:: profile:: data:: ProfileDataImpl ;
30
+ use crate :: eval:: ProfileData ;
31
+ use crate :: eval:: ProfileMode ;
28
32
29
- #[ derive( Default , Clone , Dupe , Copy ) ]
33
+ #[ derive( Default , Clone , Dupe , Copy , Debug ) ]
30
34
struct BcInstrStat {
31
35
count : u64 ,
32
36
}
@@ -41,19 +45,20 @@ impl<'a> Sum<&'a BcInstrStat> for BcInstrStat {
41
45
}
42
46
}
43
47
44
- #[ derive( Default , Clone , Copy , Dupe ) ]
48
+ #[ derive( Default , Clone , Copy , Dupe , Debug ) ]
45
49
struct BcInstrPairsStat {
46
50
count : u64 ,
47
51
// We are not measuring time here, because even time for single opcode
48
52
// is not very accurate or helpful, and time for pairs is even less helpful.
49
53
}
50
54
51
- struct BcProfileData {
55
+ #[ derive( Clone , Debug ) ]
56
+ pub ( crate ) struct BcProfileData {
52
57
by_instr : [ BcInstrStat ; BcOpcode :: COUNT ] ,
53
58
}
54
59
55
- #[ derive( Default ) ]
56
- struct BcPairsProfileData {
60
+ #[ derive( Default , Clone , Debug ) ]
61
+ pub ( crate ) struct BcPairsProfileData {
57
62
last : Option < BcOpcode > ,
58
63
by_instr : HashMap < [ BcOpcode ; 2 ] , BcInstrPairsStat > ,
59
64
}
@@ -72,7 +77,7 @@ impl BcProfileData {
72
77
self . by_instr [ opcode as usize ] . count += 1 ;
73
78
}
74
79
75
- fn gen_csv ( & self ) -> String {
80
+ pub ( crate ) fn gen_csv ( & self ) -> String {
76
81
let mut by_instr: Vec < _ > = self
77
82
. by_instr
78
83
. iter ( )
@@ -112,7 +117,7 @@ impl BcPairsProfileData {
112
117
self . last = Some ( opcode) ;
113
118
}
114
119
115
- fn gen_csv ( & self ) -> String {
120
+ pub ( crate ) fn gen_csv ( & self ) -> String {
116
121
let mut by_instr: Vec < _ > = self
117
122
. by_instr
118
123
. iter ( )
@@ -168,11 +173,23 @@ impl BcProfile {
168
173
}
169
174
}
170
175
171
- pub ( crate ) fn gen_csv ( & self ) -> anyhow:: Result < String > {
172
- match & self . data {
173
- BcProfileDataMode :: Bc ( data) => Ok ( data. gen_csv ( ) ) ,
174
- BcProfileDataMode :: BcPairs ( data) => Ok ( data. gen_csv ( ) ) ,
175
- BcProfileDataMode :: Disabled => Err ( EvaluatorError :: BcProfilingNotEnabled . into ( ) ) ,
176
+ pub ( crate ) fn gen_bc_profile ( & mut self ) -> anyhow:: Result < ProfileData > {
177
+ match mem:: replace ( & mut self . data , BcProfileDataMode :: Disabled ) {
178
+ BcProfileDataMode :: Bc ( bc) => Ok ( ProfileData {
179
+ profile_mode : ProfileMode :: Bytecode ,
180
+ profile : ProfileDataImpl :: Bc ( bc) ,
181
+ } ) ,
182
+ _ => Err ( EvaluatorError :: BcProfilingNotEnabled . into ( ) ) ,
183
+ }
184
+ }
185
+
186
+ pub ( crate ) fn gen_bc_pairs_profile ( & mut self ) -> anyhow:: Result < ProfileData > {
187
+ match mem:: replace ( & mut self . data , BcProfileDataMode :: Disabled ) {
188
+ BcProfileDataMode :: BcPairs ( bc_pairs) => Ok ( ProfileData {
189
+ profile_mode : ProfileMode :: BytecodePairs ,
190
+ profile : ProfileDataImpl :: BcPairs ( * bc_pairs) ,
191
+ } ) ,
192
+ _ => Err ( EvaluatorError :: BcProfilingNotEnabled . into ( ) ) ,
176
193
}
177
194
}
178
195
@@ -209,7 +226,7 @@ mod tests {
209
226
& globals,
210
227
)
211
228
. unwrap ( ) ;
212
- let csv = eval. bc_profile . gen_csv ( ) . unwrap ( ) ;
229
+ let csv = eval. bc_profile . gen_bc_profile ( ) . unwrap ( ) . gen ( ) . unwrap ( ) ;
213
230
assert ! (
214
231
csv. contains( & format!( "\n \" {:?}\" ,1," , BcOpcode :: CallFrozenNativePos ) ) ,
215
232
"{:?}" ,
@@ -228,7 +245,12 @@ mod tests {
228
245
& globals,
229
246
)
230
247
. unwrap ( ) ;
231
- let csv = eval. bc_profile . gen_csv ( ) . unwrap ( ) ;
248
+ let csv = eval
249
+ . bc_profile
250
+ . gen_bc_pairs_profile ( )
251
+ . unwrap ( )
252
+ . gen ( )
253
+ . unwrap ( ) ;
232
254
assert ! (
233
255
csv. contains( & format!(
234
256
"\n \" {:?}\" ,\" {:?}\" ,1" ,
0 commit comments