Skip to content

Commit 7b61237

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Wrap String in ProfileData
Summary: Types makes code easier to read. Reviewed By: bobyangyf Differential Revision: D38551784 fbshipit-source-id: 2b973ef99ec3041554ff8859f4572677d991400a
1 parent cf5c7a2 commit 7b61237

File tree

8 files changed

+73
-21
lines changed

8 files changed

+73
-21
lines changed

starlark/src/eval/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub use runtime::file_loader::ReturnFileLoader;
3535
pub use runtime::params::ParametersParser;
3636
pub use runtime::params::ParametersSpec;
3737
pub use runtime::params::ParametersSpecBuilder;
38+
pub use runtime::profile::data::ProfileData;
3839
pub use runtime::profile::ProfileMode;
3940

4041
use crate::collections::symbol_map::Symbol;

starlark/src/eval/runtime/evaluator.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
use std::cell::Cell;
1919
use std::collections::HashSet;
20-
use std::fs;
2120
use std::mem;
2221
use std::mem::MaybeUninit;
2322
use std::path::Path;
2423

25-
use anyhow::Context;
2624
use gazebo::any::AnyLifetime;
2725
use gazebo::cast;
2826
use gazebo::dupe::Dupe;
@@ -47,6 +45,7 @@ use crate::eval::runtime::call_stack::CheapCallStack;
4745
use crate::eval::runtime::call_stack::FrozenFileSpan;
4846
use crate::eval::runtime::inlined_frame::InlinedFrames;
4947
use crate::eval::runtime::profile::bc::BcProfile;
48+
use crate::eval::runtime::profile::data::ProfileData;
5049
use crate::eval::runtime::profile::flame::FlameProfile;
5150
use crate::eval::runtime::profile::heap::HeapProfile;
5251
use crate::eval::runtime::profile::heap::HeapProfileFormat;
@@ -301,15 +300,12 @@ impl<'v, 'a> Evaluator<'v, 'a> {
301300
/// Write a profile to a file.
302301
/// Only valid if corresponding profiler was enabled.
303302
pub fn write_profile<P: AsRef<Path>>(&self, filename: P) -> anyhow::Result<()> {
304-
let profile = self.gen_profile()?;
305-
fs::write(filename.as_ref(), profile)
306-
.with_context(|| format!("writing profile to `{}`", filename.as_ref().display()))?;
307-
Ok(())
303+
self.gen_profile()?.write(filename.as_ref())
308304
}
309305

310306
/// Generate profile for a given mode.
311307
/// Only valid if corresponding profiler was enabled.
312-
pub fn gen_profile(&self) -> anyhow::Result<String> {
308+
pub fn gen_profile(&self) -> anyhow::Result<ProfileData> {
313309
let mode = match &self.profile_or_instrumentation_mode {
314310
ProfileOrInstrumentationMode::None => {
315311
return Err(EvaluatorError::ProfilingNotEnabled.into());
@@ -335,7 +331,9 @@ impl<'v, 'a> Evaluator<'v, 'a> {
335331
.stmt_profile
336332
.gen()
337333
.ok_or_else(|| EvaluatorError::StmtProfilingNotEnabled.into()),
338-
ProfileMode::Bytecode | ProfileMode::BytecodePairs => self.bc_profile.gen_csv(),
334+
ProfileMode::Bytecode | ProfileMode::BytecodePairs => {
335+
self.bc_profile.gen_csv().map(ProfileData::new)
336+
}
339337
ProfileMode::TimeFlame => self
340338
.flame_profile
341339
.gen()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2019 The Starlark in Rust Authors.
3+
* Copyright (c) Facebook, Inc. and its affiliates.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use std::fs;
19+
use std::path::Path;
20+
21+
use anyhow::Context;
22+
23+
/// Collected profiling data.
24+
#[derive(Clone, Debug)]
25+
pub struct ProfileData {
26+
/// Serialized to text (e.g. CSV or flamegraph).
27+
profile: String,
28+
}
29+
30+
impl ProfileData {
31+
pub(crate) fn new(profile: String) -> ProfileData {
32+
ProfileData { profile }
33+
}
34+
35+
/// Generate a string with profile data (e.g. CSV or flamegraph, depending on profile type).
36+
pub fn gen(&self) -> String {
37+
self.profile.clone()
38+
}
39+
40+
/// Write to a file.
41+
pub fn write(&self, path: &Path) -> anyhow::Result<()> {
42+
fs::write(path, &self.profile)
43+
.with_context(|| format!("write profile data to `{}`", path.display()))?;
44+
Ok(())
45+
}
46+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::time::Instant;
2323
use gazebo::prelude::*;
2424

2525
use crate as starlark;
26+
use crate::eval::runtime::profile::data::ProfileData;
2627
use crate::eval::runtime::profile::flamegraph::FlameGraphWriter;
2728
use crate::eval::runtime::small_duration::SmallDuration;
2829
use crate::values::layout::pointer::RawPointer;
@@ -167,15 +168,15 @@ impl<'v> FlameProfile<'v> {
167168
}
168169

169170
// We could expose profile on the Heap, but it's an implementation detail that it works here.
170-
pub(crate) fn gen(&self) -> Option<String> {
171+
pub(crate) fn gen(&self) -> Option<ProfileData> {
171172
self.0.as_ref().map(|box x| Self::gen_profile(x))
172173
}
173174

174-
fn gen_profile(x: &FlameData) -> String {
175+
fn gen_profile(x: &FlameData) -> ProfileData {
175176
// Need to write out lines which look like:
176177
// root;calls1;calls2 1
177178
// All the numbers at the end must be whole numbers (we use milliseconds)
178179
let names = x.values.map(|x| x.to_repr());
179-
Stacks::new(&names, &x.frames).render()
180+
ProfileData::new(Stacks::new(&names, &x.frames).render())
180181
}
181182
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::fmt::Debug;
1919

2020
use gazebo::dupe::Dupe;
2121

22+
use crate::eval::runtime::profile::data::ProfileData;
2223
use crate::values::layout::heap::profile::aggregated::AggregateHeapProfileInfo;
2324
use crate::values::Heap;
2425
use crate::values::Value;
@@ -59,29 +60,29 @@ impl HeapProfile {
5960
}
6061

6162
// We could expose profile on the Heap, but it's an implementation detail that it works here.
62-
pub(crate) fn gen(&self, heap: &Heap, format: HeapProfileFormat) -> Option<String> {
63+
pub(crate) fn gen(&self, heap: &Heap, format: HeapProfileFormat) -> Option<ProfileData> {
6364
if !self.enabled {
6465
None
6566
} else {
6667
Some(Self::gen_enabled(heap, format))
6768
}
6869
}
6970

70-
pub(crate) fn gen_enabled(heap: &Heap, format: HeapProfileFormat) -> String {
71+
pub(crate) fn gen_enabled(heap: &Heap, format: HeapProfileFormat) -> ProfileData {
7172
match format {
7273
HeapProfileFormat::Summary => Self::write_summarized_heap_profile(heap),
7374
HeapProfileFormat::FlameGraph => Self::write_flame_heap_profile(heap),
7475
}
7576
}
7677

77-
fn write_flame_heap_profile(heap: &Heap) -> String {
78+
fn write_flame_heap_profile(heap: &Heap) -> ProfileData {
7879
let stacks = AggregateHeapProfileInfo::collect(heap, None);
79-
stacks.gen_flame_graph()
80+
ProfileData::new(stacks.gen_flame_graph())
8081
}
8182

82-
fn write_summarized_heap_profile(heap: &Heap) -> String {
83+
fn write_summarized_heap_profile(heap: &Heap) -> ProfileData {
8384
let stacks = AggregateHeapProfileInfo::collect(heap, None);
84-
stacks.gen_summary_csv()
85+
ProfileData::new(stacks.gen_summary_csv())
8586
}
8687
}
8788

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use gazebo::dupe::Dupe;
2222

2323
pub(crate) mod bc;
2424
pub(crate) mod csv;
25+
pub(crate) mod data;
2526
pub(crate) mod flame;
2627
pub(crate) mod flamegraph;
2728
pub(crate) mod heap;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::codemap::FileSpanRef;
3030
use crate::codemap::ResolvedFileSpan;
3131
use crate::codemap::Span;
3232
use crate::eval::runtime::profile::csv::CsvWriter;
33+
use crate::eval::runtime::profile::data::ProfileData;
3334
use crate::eval::runtime::small_duration::SmallDuration;
3435

3536
#[derive(Debug, thiserror::Error)]
@@ -181,9 +182,11 @@ impl StmtProfile {
181182
}
182183

183184
// None = not applicable because not enabled
184-
pub(crate) fn gen(&self) -> Option<String> {
185+
pub(crate) fn gen(&self) -> Option<ProfileData> {
185186
let now = Instant::now();
186-
self.0.as_ref().map(|data| data.write_to_string(now))
187+
self.0
188+
.as_ref()
189+
.map(|data| ProfileData::new(data.write_to_string(now)))
187190
}
188191

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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::time::Duration;
2121

2222
use crate::collections::SmallMap;
2323
use crate::eval::runtime::profile::csv::CsvWriter;
24+
use crate::eval::runtime::profile::data::ProfileData;
2425
use crate::eval::runtime::small_duration::SmallDuration;
2526
use crate::values::FrozenStringValue;
2627

@@ -60,11 +61,11 @@ impl TypecheckProfile {
6061
w.finish()
6162
}
6263

63-
pub(crate) fn gen(&self) -> Option<String> {
64+
pub(crate) fn gen(&self) -> Option<ProfileData> {
6465
if !self.enabled {
6566
return None;
6667
}
67-
Some(self.gen_csv())
68+
Some(ProfileData::new(self.gen_csv()))
6869
}
6970
}
7071

0 commit comments

Comments
 (0)