Skip to content

Commit 51cc594

Browse files
committed
First pass at json output
1 parent e50dfe6 commit 51cc594

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/librustc/session/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
13691369
"inject the given attribute in the crate"),
13701370
self_profile: bool = (false, parse_bool, [UNTRACKED],
13711371
"run the self profiler"),
1372+
profile_json: bool = (false, parse_bool, [UNTRACKED],
1373+
"output a json file with profiler results"),
13721374
}
13731375

13741376
pub fn default_lib_output() -> CrateType {

src/librustc/session/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,11 @@ impl Session {
839839
profiler.print_results(&self.opts);
840840
}
841841

842+
pub fn save_json_results(&self) {
843+
let profiler = self.self_profiling.borrow();
844+
profiler.save_results();
845+
}
846+
842847
pub fn print_perf_stats(&self) {
843848
println!(
844849
"Total time spent computing symbol hashes: {}",

src/librustc/util/profiling.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use session::config::Options;
1212

13+
use std::fs;
1314
use std::io::{self, StdoutLock, Write};
1415
use std::time::Instant;
1516

@@ -119,6 +120,46 @@ impl CategoryData {
119120
p!("Linking", linking);
120121
p!("Other", other);
121122
}
123+
124+
fn json(&self) -> String {
125+
format!("[
126+
{{
127+
\"category\": \"Parsing\",
128+
\"time_ms\": {}
129+
}},
130+
{{
131+
\"category\": \"Expansion\",
132+
\"time_ms\": {}
133+
}},
134+
{{
135+
\"category\": \"TypeChecking\",
136+
\"time_ms\": {}
137+
}},
138+
{{
139+
\"category\": \"BorrowChecking\",
140+
\"time_ms\": {}
141+
}},
142+
{{
143+
\"category\": \"Codegen\",
144+
\"time_ms\": {}
145+
}},
146+
{{
147+
\"category\": \"Linking\",
148+
\"time_ms\": {}
149+
}},
150+
{{
151+
\"category\": \"Other\",
152+
\"time_ms\": {}
153+
}}
154+
]",
155+
self.times.parsing / 1_000_000,
156+
self.times.expansion / 1_000_000,
157+
self.times.type_checking / 1_000_000,
158+
self.times.borrow_checking / 1_000_000,
159+
self.times.codegen / 1_000_000,
160+
self.times.linking / 1_000_000,
161+
self.times.other / 1_000_000)
162+
}
122163
}
123164

124165
pub struct SelfProfiler {
@@ -235,6 +276,10 @@ impl SelfProfiler {
235276
writeln!(lock, "Incremental: {}", incremental).unwrap();
236277
}
237278

279+
pub fn save_results(&self) {
280+
fs::write("self_profiler_results.json", self.data.json()).unwrap();
281+
}
282+
238283
pub fn record_activity<'a>(&'a mut self, category: ProfileCategory) -> ProfilerActivity<'a> {
239284
self.start_activity(category);
240285

src/librustc_driver/driver.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ pub fn compile_input(
355355

356356
if sess.opts.debugging_opts.self_profile {
357357
sess.print_profiler_results();
358+
359+
if sess.opts.debugging_opts.profile_json {
360+
sess.save_json_results();
361+
}
358362
}
359363

360364
controller_entry_point!(

0 commit comments

Comments
 (0)