Skip to content

Commit 472f9a9

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Cleanup profiling code
Reviewed By: milend Differential Revision: D38680952 fbshipit-source-id: e7bbbb45ca3158780ce0dde48579d26c6b02d10c
1 parent 339f24c commit 472f9a9

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

starlark/src/eval/runtime/evaluator.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,10 @@ pub(crate) enum EvaluatorError {
8080
InstrumentationEnabled,
8181
#[error("Profile data already collected")]
8282
ProfileDataAlreadyCollected,
83-
#[error("Can't call `write_heap_profile` unless you first call `enable_heap_profile`.")]
84-
HeapProfilingNotEnabled,
85-
#[error("Can't call `write_stmt_profile` unless you first call `enable_stmt_profile`.")]
86-
StmtProfilingNotEnabled,
87-
#[error("Can't call `write_flame_profile` unless you first call `enable_flame_profile`.")]
88-
FlameProfilingNotEnabled,
8983
#[error("Retained memory profiling can be only obtained from `FrozenModule`")]
9084
RetainedMemoryProfilingCannotBeObtainedFromEvaluator,
9185
#[error("Can't call `write_bc_profile` unless you first call `enable_bc_profile`.")]
9286
BcProfilingNotEnabled,
93-
#[error("Typecheck profiling not enabled")]
94-
TypecheckProfilingNotEnabled,
9587
#[error("Profile or instrumentation already enabled")]
9688
ProfileOrInstrumentationAlreadyEnabled,
9789
#[error("Top frame is not def (internal error)")]
@@ -324,31 +316,20 @@ impl<'v, 'a> Evaluator<'v, 'a> {
324316
match mode {
325317
ProfileMode::HeapSummaryAllocated => self
326318
.heap_profile
327-
.gen(self.heap(), HeapProfileFormat::Summary)
328-
.ok_or_else(|| EvaluatorError::HeapProfilingNotEnabled.into()),
319+
.gen(self.heap(), HeapProfileFormat::Summary),
329320
ProfileMode::HeapFlameAllocated => self
330321
.heap_profile
331-
.gen(self.heap(), HeapProfileFormat::FlameGraph)
332-
.ok_or_else(|| EvaluatorError::HeapProfilingNotEnabled.into()),
322+
.gen(self.heap(), HeapProfileFormat::FlameGraph),
333323
ProfileMode::HeapSummaryRetained | ProfileMode::HeapFlameRetained => {
334324
Err(EvaluatorError::RetainedMemoryProfilingCannotBeObtainedFromEvaluator.into())
335325
}
336-
ProfileMode::Statement => self
337-
.stmt_profile
338-
.gen()
339-
.ok_or_else(|| EvaluatorError::StmtProfilingNotEnabled.into()),
326+
ProfileMode::Statement => self.stmt_profile.gen(),
340327
ProfileMode::Bytecode | ProfileMode::BytecodePairs => self
341328
.bc_profile
342329
.gen_csv()
343330
.map(|csv| ProfileData::new(mode, csv)),
344-
ProfileMode::TimeFlame => self
345-
.flame_profile
346-
.gen()
347-
.ok_or_else(|| EvaluatorError::FlameProfilingNotEnabled.into()),
348-
ProfileMode::Typecheck => self
349-
.typecheck_profile
350-
.gen()
351-
.ok_or_else(|| EvaluatorError::TypecheckProfilingNotEnabled.into()),
331+
ProfileMode::TimeFlame => self.flame_profile.gen(),
332+
ProfileMode::Typecheck => self.typecheck_profile.gen(),
352333
}
353334
}
354335

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ use crate::values::Trace;
3232
use crate::values::Tracer;
3333
use crate::values::Value;
3434

35+
#[derive(Debug, thiserror::Error)]
36+
enum FlameProfileError {
37+
#[error("Flame profile not enabled")]
38+
NotEnabled,
39+
}
40+
3541
/// Index into FlameData.values
3642
#[derive(Hash, PartialEq, Eq, Clone, Copy, Dupe)]
3743
struct ValueIndex(usize);
@@ -169,8 +175,11 @@ impl<'v> FlameProfile<'v> {
169175
}
170176

171177
// We could expose profile on the Heap, but it's an implementation detail that it works here.
172-
pub(crate) fn gen(&self) -> Option<ProfileData> {
173-
self.0.as_ref().map(|box x| Self::gen_profile(x))
178+
pub(crate) fn gen(&self) -> anyhow::Result<ProfileData> {
179+
match &self.0 {
180+
None => Err(FlameProfileError::NotEnabled.into()),
181+
Some(x) => Ok(Self::gen_profile(x)),
182+
}
174183
}
175184

176185
fn gen_profile(x: &FlameData) -> ProfileData {

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ use crate::values::layout::heap::profile::aggregated::AggregateHeapProfileInfo;
2525
use crate::values::Heap;
2626
use crate::values::Value;
2727

28+
#[derive(Debug, thiserror::Error)]
29+
enum HeapProfileError {
30+
#[error("heap profile not enabled")]
31+
NotEnabled,
32+
}
33+
2834
#[derive(Copy, Clone, Dupe, Debug)]
2935
pub(crate) enum HeapProfileFormat {
3036
Summary,
@@ -61,12 +67,15 @@ impl HeapProfile {
6167
}
6268

6369
// We could expose profile on the Heap, but it's an implementation detail that it works here.
64-
pub(crate) fn gen(&self, heap: &Heap, format: HeapProfileFormat) -> Option<ProfileData> {
70+
pub(crate) fn gen(
71+
&self,
72+
heap: &Heap,
73+
format: HeapProfileFormat,
74+
) -> anyhow::Result<ProfileData> {
6575
if !self.enabled {
66-
None
67-
} else {
68-
Some(Self::gen_enabled(heap, format))
76+
return Err(HeapProfileError::NotEnabled.into());
6977
}
78+
Ok(Self::gen_enabled(heap, format))
7079
}
7180

7281
pub(crate) fn gen_enabled(heap: &Heap, format: HeapProfileFormat) -> ProfileData {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,15 @@ impl StmtProfile {
183183
}
184184

185185
// None = not applicable because not enabled
186-
pub(crate) fn gen(&self) -> Option<ProfileData> {
186+
pub(crate) fn gen(&self) -> anyhow::Result<ProfileData> {
187187
let now = Instant::now();
188-
self.0
189-
.as_ref()
190-
.map(|data| ProfileData::new(ProfileMode::Statement, data.write_to_string(now)))
188+
match &self.0 {
189+
Some(data) => Ok(ProfileData::new(
190+
ProfileMode::Statement,
191+
data.write_to_string(now),
192+
)),
193+
None => Err(StmtProfileError::NotEnabled.into()),
194+
}
191195
}
192196

193197
pub(crate) fn coverage(&self) -> anyhow::Result<HashSet<ResolvedFileSpan>> {

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ use crate::eval::runtime::small_duration::SmallDuration;
2626
use crate::eval::ProfileMode;
2727
use crate::values::FrozenStringValue;
2828

29+
#[derive(Debug, thiserror::Error)]
30+
enum TypecheckProfileErorr {
31+
#[error("Typecheck profile not enabled")]
32+
NotEnabled,
33+
}
34+
2935
#[derive(Default, Debug)]
3036
pub(crate) struct TypecheckProfile {
3137
pub(crate) enabled: bool,
@@ -62,11 +68,11 @@ impl TypecheckProfile {
6268
w.finish()
6369
}
6470

65-
pub(crate) fn gen(&self) -> Option<ProfileData> {
71+
pub(crate) fn gen(&self) -> anyhow::Result<ProfileData> {
6672
if !self.enabled {
67-
return None;
73+
return Err(TypecheckProfileErorr::NotEnabled.into());
6874
}
69-
Some(ProfileData::new(ProfileMode::Typecheck, self.gen_csv()))
75+
Ok(ProfileData::new(ProfileMode::Typecheck, self.gen_csv()))
7076
}
7177
}
7278

0 commit comments

Comments
 (0)