Skip to content

Commit 3660ac2

Browse files
authored
Merge pull request RustPython#4290 from DimitrisJim/flame_it
Fix issues with flame-it.
2 parents f885db8 + d075478 commit 3660ac2

File tree

2 files changed

+70
-58
lines changed

2 files changed

+70
-58
lines changed

src/lib.rs

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ extern crate env_logger;
4343
#[macro_use]
4444
extern crate log;
4545

46+
#[cfg(feature = "flame-it")]
47+
use vm::Settings;
48+
4649
mod interpreter;
4750
mod settings;
4851
mod shell;
@@ -80,70 +83,13 @@ pub fn run(init: impl FnOnce(&mut VirtualMachine) + 'static) -> ExitCode {
8083
config = config.init_stdlib();
8184
}
8285
config = config.init_hook(Box::new(init));
83-
let interp = config.interpreter();
84-
85-
#[cfg(feature = "flame-it")]
86-
let main_guard = flame::start_guard("RustPython main");
8786

87+
let interp = config.interpreter();
8888
let exitcode = interp.run(move |vm| run_rustpython(vm, run_mode));
8989

90-
#[cfg(feature = "flame-it")]
91-
{
92-
main_guard.end();
93-
if let Err(e) = write_profile(&matches) {
94-
error!("Error writing profile information: {}", e);
95-
}
96-
}
9790
ExitCode::from(exitcode)
9891
}
9992

100-
#[cfg(feature = "flame-it")]
101-
fn write_profile(matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
102-
use std::{fs, io};
103-
104-
enum ProfileFormat {
105-
Html,
106-
Text,
107-
Speedscope,
108-
}
109-
110-
let profile_output = matches.value_of_os("profile_output");
111-
112-
let profile_format = match matches.value_of("profile_format") {
113-
Some("html") => ProfileFormat::Html,
114-
Some("text") => ProfileFormat::Text,
115-
None if profile_output == Some("-".as_ref()) => ProfileFormat::Text,
116-
Some("speedscope") | None => ProfileFormat::Speedscope,
117-
Some(other) => {
118-
error!("Unknown profile format {}", other);
119-
// TODO: Need to change to ExitCode or Termination
120-
std::process::exit(1);
121-
}
122-
};
123-
124-
let profile_output = profile_output.unwrap_or_else(|| match profile_format {
125-
ProfileFormat::Html => "flame-graph.html".as_ref(),
126-
ProfileFormat::Text => "flame.txt".as_ref(),
127-
ProfileFormat::Speedscope => "flamescope.json".as_ref(),
128-
});
129-
130-
let profile_output: Box<dyn io::Write> = if profile_output == "-" {
131-
Box::new(io::stdout())
132-
} else {
133-
Box::new(fs::File::create(profile_output)?)
134-
};
135-
136-
let profile_output = io::BufWriter::new(profile_output);
137-
138-
match profile_format {
139-
ProfileFormat::Html => flame::dump_html(profile_output)?,
140-
ProfileFormat::Text => flame::dump_text_to_writer(profile_output)?,
141-
ProfileFormat::Speedscope => flamescope::dump(profile_output)?,
142-
}
143-
144-
Ok(())
145-
}
146-
14793
fn setup_main_module(vm: &VirtualMachine) -> PyResult<Scope> {
14894
let scope = vm.new_scope_with_builtins();
14995
let main_module = vm.new_module("__main__", scope.globals.clone(), None);
@@ -206,6 +152,9 @@ fn install_pip(_installer: &str, _scope: Scope, vm: &VirtualMachine) -> PyResult
206152
}
207153

208154
fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
155+
#[cfg(feature = "flame-it")]
156+
let main_guard = flame::start_guard("RustPython main");
157+
209158
let scope = setup_main_module(vm)?;
210159

211160
let site_result = vm.import("site", None, 0);
@@ -244,6 +193,57 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
244193
}
245194
}
246195
}
196+
#[cfg(feature = "flame-it")]
197+
{
198+
main_guard.end();
199+
if let Err(e) = write_profile(&vm.state.as_ref().settings) {
200+
error!("Error writing profile information: {}", e);
201+
}
202+
}
203+
Ok(())
204+
}
205+
206+
#[cfg(feature = "flame-it")]
207+
fn write_profile(settings: &Settings) -> Result<(), Box<dyn std::error::Error>> {
208+
use std::{fs, io};
209+
210+
enum ProfileFormat {
211+
Html,
212+
Text,
213+
Speedscope,
214+
}
215+
let profile_output = settings.profile_output.as_deref();
216+
let profile_format = match settings.profile_format.as_deref() {
217+
Some("html") => ProfileFormat::Html,
218+
Some("text") => ProfileFormat::Text,
219+
None if profile_output == Some("-".as_ref()) => ProfileFormat::Text,
220+
Some("speedscope") | None => ProfileFormat::Speedscope,
221+
Some(other) => {
222+
error!("Unknown profile format {}", other);
223+
// TODO: Need to change to ExitCode or Termination
224+
std::process::exit(1);
225+
}
226+
};
227+
228+
let profile_output = profile_output.unwrap_or_else(|| match profile_format {
229+
ProfileFormat::Html => "flame-graph.html".as_ref(),
230+
ProfileFormat::Text => "flame.txt".as_ref(),
231+
ProfileFormat::Speedscope => "flamescope.json".as_ref(),
232+
});
233+
234+
let profile_output: Box<dyn io::Write> = if profile_output == "-" {
235+
Box::new(io::stdout())
236+
} else {
237+
Box::new(fs::File::create(profile_output)?)
238+
};
239+
240+
let profile_output = io::BufWriter::new(profile_output);
241+
242+
match profile_format {
243+
ProfileFormat::Html => flame::dump_html(profile_output)?,
244+
ProfileFormat::Text => flame::dump_text_to_writer(profile_output)?,
245+
ProfileFormat::Speedscope => flamescope::dump(profile_output)?,
246+
}
247247

248248
Ok(())
249249
}

vm/src/vm/setting.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(feature = "flame-it")]
2+
use std::ffi::OsString;
3+
14
/// Struct containing all kind of settings for the python vm.
25
#[non_exhaustive]
36
pub struct Settings {
@@ -67,6 +70,11 @@ pub struct Settings {
6770

6871
/// false for wasm. Not a command-line option
6972
pub allow_external_library: bool,
73+
74+
#[cfg(feature = "flame-it")]
75+
pub profile_output: Option<OsString>,
76+
#[cfg(feature = "flame-it")]
77+
pub profile_format: Option<String>,
7078
}
7179

7280
/// Sensible default settings.
@@ -95,6 +103,10 @@ impl Default for Settings {
95103
stdio_unbuffered: false,
96104
check_hash_based_pycs: "default".to_owned(),
97105
allow_external_library: cfg!(feature = "importlib"),
106+
#[cfg(feature = "flame-it")]
107+
profile_output: None,
108+
#[cfg(feature = "flame-it")]
109+
profile_format: None,
98110
}
99111
}
100112
}

0 commit comments

Comments
 (0)