Skip to content

Commit fc7f50f

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Raw BC profile data in ProfileData
Summary: Store raw bytecode profile data in `ProfileData` instead of `String`. It is merged later in the stack (D38681490). Reviewed By: bobyangyf Differential Revision: D38681131 fbshipit-source-id: 551bfc5efc155a58e53f4b06b390edb954b32541
1 parent 94b1b3b commit fc7f50f

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

starlark/src/eval/runtime/evaluator.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,8 @@ impl<'v, 'a> Evaluator<'v, 'a> {
324324
Err(EvaluatorError::RetainedMemoryProfilingCannotBeObtainedFromEvaluator.into())
325325
}
326326
ProfileMode::Statement => self.stmt_profile.gen(),
327-
ProfileMode::Bytecode | ProfileMode::BytecodePairs => self
328-
.bc_profile
329-
.gen_csv()
330-
.map(|csv| ProfileData::new(mode, csv)),
327+
ProfileMode::Bytecode => self.bc_profile.gen_bc_profile(),
328+
ProfileMode::BytecodePairs => self.bc_profile.gen_bc_pairs_profile(),
331329
ProfileMode::TimeFlame => self.flame_profile.gen(),
332330
ProfileMode::Typecheck => self.typecheck_profile.gen(),
333331
}

starlark/src/eval/runtime/profile/bc.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
2020
use std::collections::HashMap;
2121
use std::iter::Sum;
22+
use std::mem;
2223

2324
use gazebo::prelude::*;
2425

2526
use crate::eval::bc::opcode::BcOpcode;
2627
use crate::eval::runtime::evaluator::EvaluatorError;
2728
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;
2832

29-
#[derive(Default, Clone, Dupe, Copy)]
33+
#[derive(Default, Clone, Dupe, Copy, Debug)]
3034
struct BcInstrStat {
3135
count: u64,
3236
}
@@ -41,19 +45,20 @@ impl<'a> Sum<&'a BcInstrStat> for BcInstrStat {
4145
}
4246
}
4347

44-
#[derive(Default, Clone, Copy, Dupe)]
48+
#[derive(Default, Clone, Copy, Dupe, Debug)]
4549
struct BcInstrPairsStat {
4650
count: u64,
4751
// We are not measuring time here, because even time for single opcode
4852
// is not very accurate or helpful, and time for pairs is even less helpful.
4953
}
5054

51-
struct BcProfileData {
55+
#[derive(Clone, Debug)]
56+
pub(crate) struct BcProfileData {
5257
by_instr: [BcInstrStat; BcOpcode::COUNT],
5358
}
5459

55-
#[derive(Default)]
56-
struct BcPairsProfileData {
60+
#[derive(Default, Clone, Debug)]
61+
pub(crate) struct BcPairsProfileData {
5762
last: Option<BcOpcode>,
5863
by_instr: HashMap<[BcOpcode; 2], BcInstrPairsStat>,
5964
}
@@ -72,7 +77,7 @@ impl BcProfileData {
7277
self.by_instr[opcode as usize].count += 1;
7378
}
7479

75-
fn gen_csv(&self) -> String {
80+
pub(crate) fn gen_csv(&self) -> String {
7681
let mut by_instr: Vec<_> = self
7782
.by_instr
7883
.iter()
@@ -112,7 +117,7 @@ impl BcPairsProfileData {
112117
self.last = Some(opcode);
113118
}
114119

115-
fn gen_csv(&self) -> String {
120+
pub(crate) fn gen_csv(&self) -> String {
116121
let mut by_instr: Vec<_> = self
117122
.by_instr
118123
.iter()
@@ -168,11 +173,23 @@ impl BcProfile {
168173
}
169174
}
170175

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()),
176193
}
177194
}
178195

@@ -209,7 +226,7 @@ mod tests {
209226
&globals,
210227
)
211228
.unwrap();
212-
let csv = eval.bc_profile.gen_csv().unwrap();
229+
let csv = eval.bc_profile.gen_bc_profile().unwrap().gen().unwrap();
213230
assert!(
214231
csv.contains(&format!("\n\"{:?}\",1,", BcOpcode::CallFrozenNativePos)),
215232
"{:?}",
@@ -228,7 +245,12 @@ mod tests {
228245
&globals,
229246
)
230247
.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();
232254
assert!(
233255
csv.contains(&format!(
234256
"\n\"{:?}\",\"{:?}\",1",

starlark/src/eval/runtime/profile/data.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,23 @@ use std::path::Path;
2020

2121
use anyhow::Context;
2222

23+
use crate::eval::runtime::profile::bc::BcPairsProfileData;
24+
use crate::eval::runtime::profile::bc::BcProfileData;
2325
use crate::eval::ProfileMode;
2426

2527
#[derive(Clone, Debug)]
2628
pub(crate) enum ProfileDataImpl {
29+
Bc(Box<BcProfileData>),
30+
BcPairs(BcPairsProfileData),
2731
Other(String),
2832
}
2933

3034
/// Collected profiling data.
3135
#[derive(Clone, Debug)]
3236
pub struct ProfileData {
33-
profile_mode: ProfileMode,
37+
pub(crate) profile_mode: ProfileMode,
3438
/// Serialized to text (e.g. CSV or flamegraph).
35-
profile: ProfileDataImpl,
39+
pub(crate) profile: ProfileDataImpl,
3640
}
3741

3842
impl ProfileData {
@@ -47,6 +51,8 @@ impl ProfileData {
4751
pub fn gen(&self) -> anyhow::Result<String> {
4852
match &self.profile {
4953
ProfileDataImpl::Other(profile) => Ok(profile.clone()),
54+
ProfileDataImpl::Bc(bc) => Ok(bc.gen_csv()),
55+
ProfileDataImpl::BcPairs(bc_pairs) => Ok(bc_pairs.gen_csv()),
5056
}
5157
}
5258

0 commit comments

Comments
 (0)