Skip to content

Commit 43335ae

Browse files
author
Matthew Hammer
committed
-Z profile-query-and-key, separate from -Z profile-query; query key is string option
1 parent 4251032 commit 43335ae

File tree

6 files changed

+76
-30
lines changed

6 files changed

+76
-30
lines changed

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
997997
"enable queries of the dependency graph for regression testing"),
998998
profile_queries: bool = (false, parse_bool, [UNTRACKED],
999999
"trace and profile the queries of the incremental compilation framework"),
1000+
profile_queries_and_keys: bool = (false, parse_bool, [UNTRACKED],
1001+
"trace and profile the queries and keys of the incremental compilation framework"),
10001002
no_analysis: bool = (false, parse_bool, [UNTRACKED],
10011003
"parse and expand the source, but run no analysis"),
10021004
extra_plugins: Vec<String> = (Vec::new(), parse_list, [TRACKED],

src/librustc/session/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,13 @@ impl Session {
389389
}
390390
pub fn verbose(&self) -> bool { self.opts.debugging_opts.verbose }
391391
pub fn time_passes(&self) -> bool { self.opts.debugging_opts.time_passes }
392-
pub fn profile_queries(&self) -> bool { self.opts.debugging_opts.profile_queries }
392+
pub fn profile_queries(&self) -> bool {
393+
self.opts.debugging_opts.profile_queries ||
394+
self.opts.debugging_opts.profile_queries_and_keys
395+
}
396+
pub fn profile_queries_and_keys(&self) -> bool {
397+
self.opts.debugging_opts.profile_queries_and_keys
398+
}
393399
pub fn count_llvm_insns(&self) -> bool {
394400
self.opts.debugging_opts.count_llvm_insns
395401
}

src/librustc/ty/maps.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -511,19 +511,32 @@ impl<'tcx> QueryDescription for queries::extern_crate<'tcx> {
511511
impl<'tcx> QueryDescription for queries::lint_levels<'tcx> {
512512
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
513513
format!("computing the lint levels for items in this crate")
514+
}
514515
}
515516

516517
// If enabled, send a message to the profile-queries thread
517518
macro_rules! profq_msg {
518519
($tcx:expr, $msg:expr) => {
519520
if cfg!(debug_assertions) {
520-
if $tcx.sess.opts.debugging_opts.profile_queries {
521+
if $tcx.sess.profile_queries() {
521522
profq_msg($msg)
522523
}
523524
}
524525
}
525526
}
526527

528+
// If enabled, format a key using its debug string, which can be
529+
// expensive to compute (in terms of time).
530+
macro_rules! profq_key {
531+
($tcx:expr, $key:expr) => {
532+
if cfg!(debug_assertions) {
533+
if $tcx.sess.profile_queries_and_keys() {
534+
Some(format!("{:?}", $key))
535+
} else { None }
536+
} else { None }
537+
}
538+
}
539+
527540
macro_rules! define_maps {
528541
(<$tcx:tt>
529542
$($(#[$attr:meta])*
@@ -553,7 +566,7 @@ macro_rules! define_maps {
553566
#[allow(bad_style)]
554567
#[derive(Clone, Debug, PartialEq, Eq)]
555568
pub enum QueryMsg {
556-
$($name(String)),*
569+
$($name(Option<String>)),*
557570
}
558571

559572
impl<$tcx> Query<$tcx> {
@@ -599,8 +612,11 @@ macro_rules! define_maps {
599612
span);
600613

601614
profq_msg!(tcx,
602-
ProfileQueriesMsg::QueryBegin(span.clone(),
603-
QueryMsg::$name(format!("{:?}", key))));
615+
ProfileQueriesMsg::QueryBegin(
616+
span.clone(),
617+
QueryMsg::$name(profq_key!(tcx, key))
618+
)
619+
);
604620

605621
if let Some(&(ref result, dep_node_index)) = tcx.maps.$name.borrow().map.get(&key) {
606622
tcx.dep_graph.read_index(dep_node_index);

src/librustc_driver/driver.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn compile_input(sess: &Session,
107107
sess.abort_if_errors();
108108
}
109109

110-
if sess.opts.debugging_opts.profile_queries {
110+
if sess.profile_queries() {
111111
profile::begin();
112112
}
113113

@@ -543,7 +543,7 @@ pub fn phase_1_parse_input<'a>(control: &CompileController,
543543
-> PResult<'a, ast::Crate> {
544544
sess.diagnostic().set_continue_after_error(control.continue_parse_after_error);
545545

546-
if sess.opts.debugging_opts.profile_queries {
546+
if sess.profile_queries() {
547547
profile::begin();
548548
}
549549

@@ -1130,7 +1130,7 @@ pub fn phase_4_translate_to_llvm<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11301130
"translation",
11311131
move || trans::trans_crate(tcx, analysis, incremental_hashes_map, output_filenames));
11321132

1133-
if tcx.sess.opts.debugging_opts.profile_queries {
1133+
if tcx.sess.profile_queries() {
11341134
profile::dump("profile_queries".to_string())
11351135
}
11361136

src/librustc_driver/profile/mod.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::time::{Instant};
1211
use rustc::util::common::{ProfQDumpParams, ProfileQueriesMsg, profq_msg, profq_set_chan};
1312
use std::sync::mpsc::{Receiver};
1413
use std::io::{Write};
1514
use rustc::dep_graph::{DepNode};
15+
use std::time::{Duration, Instant};
1616

1717
pub mod trace;
1818

@@ -37,7 +37,7 @@ pub fn dump(path:String) {
3737
path, ack:tx,
3838
// FIXME: Add another compiler flag to toggle whether this log
3939
// is written; false for now
40-
dump_profq_msg_log:false,
40+
dump_profq_msg_log:true,
4141
};
4242
profq_msg(ProfileQueriesMsg::Dump(params));
4343
let _ = rx.recv().unwrap();
@@ -61,6 +61,12 @@ struct StackFrame {
6161
pub traces: Vec<trace::Rec>,
6262
}
6363

64+
fn total_duration(traces: &Vec<trace::Rec>) -> Duration {
65+
let mut sum : Duration = Duration::new(0,0);
66+
for t in traces.iter() { sum += t.dur_total; }
67+
return sum
68+
}
69+
6470
// profiling thread; retains state (in local variables) and dump traces, upon request.
6571
fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
6672
use self::trace::*;
@@ -161,11 +167,13 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
161167
parse_st:ParseState::Clear,
162168
traces:old_frame.traces
163169
};
170+
let dur_extent = total_duration(&provider_extent);
164171
let trace = Rec {
165172
effect: Effect::QueryBegin(q, CacheCase::Miss),
166173
extent: Box::new(provider_extent),
167174
start: start,
168-
duration: duration,
175+
dur_self: duration - dur_extent,
176+
dur_total: duration,
169177
};
170178
frame.traces.push( trace );
171179
},
@@ -200,11 +208,13 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
200208
parse_st:ParseState::Clear,
201209
traces:old_frame.traces
202210
};
211+
let dur_extent = total_duration(&provider_extent);
203212
let trace = Rec {
204213
effect: Effect::TimeBegin(msg),
205214
extent: Box::new(provider_extent),
206215
start: start,
207-
duration: duration,
216+
dur_total: duration,
217+
dur_self: duration - dur_extent,
208218
};
209219
frame.traces.push( trace );
210220
},
@@ -239,11 +249,13 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
239249
parse_st:ParseState::Clear,
240250
traces:old_frame.traces
241251
};
252+
let dur_extent = total_duration(&provider_extent);
242253
let trace = Rec {
243254
effect: Effect::TaskBegin(key),
244255
extent: Box::new(provider_extent),
245256
start: start,
246-
duration: duration,
257+
dur_total: duration,
258+
dur_self: duration - dur_extent,
247259
};
248260
frame.traces.push( trace );
249261
},
@@ -262,7 +274,8 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
262274
effect: Effect::QueryBegin(q, CacheCase::Hit),
263275
extent: Box::new(vec![]),
264276
start: start,
265-
duration: duration,
277+
dur_self: duration,
278+
dur_total: duration,
266279
};
267280
frame.traces.push( trace );
268281
frame.parse_st = ParseState::Clear;

src/librustc_driver/profile/trace.rs

+25-16
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ pub enum CacheCase {
3333
pub struct Rec {
3434
pub effect: Effect,
3535
pub start: Instant,
36-
pub duration: Duration,
36+
pub dur_self: Duration,
37+
pub dur_total: Duration,
3738
pub extent: Box<Vec<Rec>>,
3839
}
3940
pub struct QueryMetric {
4041
pub count: usize,
41-
pub duration: Duration,
42+
pub dur_self: Duration,
43+
pub dur_total: Duration,
4244
}
4345

4446
pub fn cons_of_query_msg(q: &trace::Query) -> String {
@@ -108,7 +110,7 @@ fn html_of_fraction(frac: f64) -> (String, String) {
108110
fn total_duration(traces: &Vec<Rec>) -> Duration {
109111
let mut sum : Duration = Duration::new(0,0);
110112
for t in traces.iter() {
111-
sum += t.duration;
113+
sum += t.dur_total;
112114
}
113115
return sum
114116
}
@@ -124,8 +126,8 @@ fn duration_div(nom: Duration, den: Duration) -> f64 {
124126
fn write_traces_rec(file: &mut File, traces: &Vec<Rec>, total: Duration, depth: usize) {
125127
for t in traces {
126128
let (eff_text, eff_css_classes) = html_of_effect(&t.effect);
127-
let (dur_text, dur_css_classes) = html_of_duration(&t.start, &t.duration);
128-
let fraction = duration_div(t.duration, total);
129+
let (dur_text, dur_css_classes) = html_of_duration(&t.start, &t.dur_total);
130+
let fraction = duration_div(t.dur_total, total);
129131
let percent = fraction * 100.0;
130132
let (frc_text, frc_css_classes) = html_of_fraction(fraction);
131133
write!(file, "<div class=\"trace depth-{} extent-{}{} {} {} {}\">\n",
@@ -155,7 +157,8 @@ fn compute_counts_rec(counts: &mut HashMap<String,QueryMetric>, traces: &Vec<Rec
155157
Some(_qm) => { panic!("TimeBegin with non-unique, repeat message") }
156158
None => QueryMetric{
157159
count: 1,
158-
duration: t.duration
160+
dur_self: t.dur_self,
161+
dur_total: t.dur_total,
159162
}};
160163
counts.insert(msg.clone(), qm);
161164
},
@@ -165,11 +168,13 @@ fn compute_counts_rec(counts: &mut HashMap<String,QueryMetric>, traces: &Vec<Rec
165168
Some(qm) =>
166169
QueryMetric{
167170
count: qm.count + 1,
168-
duration: qm.duration + t.duration
171+
dur_self: qm.dur_self + t.dur_self,
172+
dur_total: qm.dur_total + t.dur_total,
169173
},
170174
None => QueryMetric{
171175
count: 1,
172-
duration: t.duration
176+
dur_self: t.dur_self,
177+
dur_total: t.dur_total,
173178
}};
174179
counts.insert(cons, qm);
175180
},
@@ -179,11 +184,13 @@ fn compute_counts_rec(counts: &mut HashMap<String,QueryMetric>, traces: &Vec<Rec
179184
Some(qm) =>
180185
QueryMetric{
181186
count: qm.count + 1,
182-
duration: qm.duration + t.duration
187+
dur_total: qm.dur_total + t.dur_total,
188+
dur_self: qm.dur_self + t.dur_self
183189
},
184190
None => QueryMetric{
185191
count: 1,
186-
duration: t.duration
192+
dur_total: t.dur_total,
193+
dur_self: t.dur_self,
187194
}
188195
};
189196
counts.insert(qcons, qm);
@@ -199,13 +206,15 @@ pub fn write_counts(count_file: &mut File, counts: &mut HashMap<String,QueryMetr
199206

200207
let mut data = vec![];
201208
for (ref cons, ref qm) in counts.iter() {
202-
data.push((cons.clone(), qm.count.clone(), qm.duration.clone()));
209+
data.push((cons.clone(), qm.count.clone(), qm.dur_total.clone(), qm.dur_self.clone()));
203210
};
204-
data.sort_by(|&(_,_,d1),&(_,_,d2)|
205-
if d1 > d2 { Ordering::Less } else { Ordering::Greater } );
206-
for (cons, count, duration) in data {
207-
write!(count_file, "{},{},{}\n",
208-
cons, count, duration_to_secs_str(duration)
211+
data.sort_by(|&(_,_,_,self1),&(_,_,_,self2)|
212+
if self1 > self2 { Ordering::Less } else { Ordering::Greater } );
213+
for (cons, count, dur_total, dur_self) in data {
214+
write!(count_file, "{}, {}, {}, {}\n",
215+
cons, count,
216+
duration_to_secs_str(dur_total),
217+
duration_to_secs_str(dur_self)
209218
).unwrap();
210219
}
211220
}

0 commit comments

Comments
 (0)