Skip to content

Commit 4251032

Browse files
author
Matthew Hammer
committed
-Z profile-queries includes dep_graph.with_task uses in output
1 parent 3c24fea commit 4251032

File tree

4 files changed

+99
-13
lines changed

4 files changed

+99
-13
lines changed

src/librustc/dep_graph/graph.rs

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_data_structures::fx::FxHashMap;
1212
use session::config::OutputType;
1313
use std::cell::{Ref, RefCell};
1414
use std::rc::Rc;
15+
use util::common::{ProfileQueriesMsg, profq_msg};
1516

1617
use super::dep_node::{DepNode, DepKind, WorkProductId};
1718
use super::query::DepGraphQuery;
@@ -118,7 +119,13 @@ impl DepGraph {
118119
{
119120
if let Some(ref data) = self.data {
120121
data.edges.borrow_mut().push_task(key);
122+
if cfg!(debug_assertions) {
123+
profq_msg(ProfileQueriesMsg::TaskBegin(key.clone()))
124+
};
121125
let result = task(cx, arg);
126+
if cfg!(debug_assertions) {
127+
profq_msg(ProfileQueriesMsg::TaskEnd)
128+
};
122129
let dep_node_index = data.edges.borrow_mut().pop_task(key);
123130
(result, dep_node_index)
124131
} else {

src/librustc/util/common.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::time::{Duration, Instant};
2222
use std::sync::mpsc::{Sender};
2323
use syntax_pos::{Span};
2424
use ty::maps::{QueryMsg};
25+
use dep_graph::{DepNode};
2526

2627
// The name of the associated type for `Fn` return types
2728
pub const FN_OUTPUT_NAME: &'static str = "Output";
@@ -53,8 +54,12 @@ pub struct ProfQDumpParams {
5354
pub enum ProfileQueriesMsg {
5455
/// begin a timed pass
5556
TimeBegin(String),
56-
// end a timed pass
57+
/// end a timed pass
5758
TimeEnd,
59+
/// begin a task (see dep_graph::graph::with_task)
60+
TaskBegin(DepNode),
61+
/// end a task
62+
TaskEnd,
5863
/// begin a new query
5964
QueryBegin(Span, QueryMsg),
6065
/// query is satisfied by using an already-known value for the given key

src/librustc_driver/profile/mod.rs

+53-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::time::{Instant};
1212
use rustc::util::common::{ProfQDumpParams, ProfileQueriesMsg, profq_msg, profq_set_chan};
1313
use std::sync::mpsc::{Receiver};
1414
use std::io::{Write};
15+
use rustc::dep_graph::{DepNode};
1516

1617
pub mod trace;
1718

@@ -52,6 +53,8 @@ enum ParseState {
5253
HaveQuery(trace::Query, Instant),
5354
// Have "time-begin" information from the last message (doit flag, and message)
5455
HaveTimeBegin(String, Instant),
56+
// Have "task-begin" information from the last message
57+
HaveTaskBegin(DepNode, Instant),
5558
}
5659
struct StackFrame {
5760
pub parse_st: ParseState,
@@ -170,7 +173,7 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
170173
}
171174
}
172175
}
173-
},
176+
},
174177

175178

176179
(ParseState::Clear,
@@ -212,19 +215,44 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
212215
},
213216
(_, ProfileQueriesMsg::TimeEnd) => { panic!("parse error") }
214217

218+
(ParseState::Clear,
219+
ProfileQueriesMsg::TaskBegin(key)) => {
220+
let start = Instant::now();
221+
frame.parse_st = ParseState::HaveTaskBegin(key, start);
222+
stack.push(frame);
223+
frame = StackFrame{parse_st:ParseState::Clear, traces:vec![]};
224+
},
225+
(_, ProfileQueriesMsg::TaskBegin(_)) =>
226+
panic!("parse error; did not expect time begin here"),
215227

216-
// Parse State: HaveTimeBegin -- for timing old
217-
// passes in driver (outside of query model, but
218-
// still in use)
219-
(ParseState::HaveTimeBegin(_, _),
220-
ProfileQueriesMsg::ProviderBegin) => {
228+
(ParseState::Clear,
229+
ProfileQueriesMsg::TaskEnd) => {
230+
let provider_extent = frame.traces;
231+
match stack.pop() {
232+
None =>
233+
panic!("parse error: expected a stack frame; found an empty stack"),
234+
Some(old_frame) => {
235+
match old_frame.parse_st {
236+
ParseState::HaveTaskBegin(key, start) => {
237+
let duration = start.elapsed();
238+
frame = StackFrame{
239+
parse_st:ParseState::Clear,
240+
traces:old_frame.traces
241+
};
242+
let trace = Rec {
243+
effect: Effect::TaskBegin(key),
244+
extent: Box::new(provider_extent),
245+
start: start,
246+
duration: duration,
247+
};
248+
frame.traces.push( trace );
249+
},
250+
_ => panic!("internal parse error: malformed parse stack")
251+
}
252+
}
253+
}
221254
},
222-
(ParseState::HaveTimeBegin(_, _),
223-
ProfileQueriesMsg::CacheHit) => { unreachable!() },
224-
(ParseState::HaveTimeBegin(_, _),
225-
ProfileQueriesMsg::QueryBegin(_, _)) => { unreachable!() },
226-
(ParseState::HaveTimeBegin(_, _),
227-
ProfileQueriesMsg::ProviderEnd) => { unreachable!() },
255+
(_, ProfileQueriesMsg::TaskEnd) => { panic!("parse error") }
228256

229257
// Parse State: HaveQuery
230258
(ParseState::HaveQuery(q,start),
@@ -244,6 +272,11 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
244272
stack.push(frame);
245273
frame = StackFrame{parse_st:ParseState::Clear, traces:vec![]};
246274
},
275+
276+
//
277+
//
278+
// Parse errors:
279+
247280
(ParseState::HaveQuery(q,_),
248281
ProfileQueriesMsg::ProviderEnd) => {
249282
panic!("parse error: unexpected ProviderEnd; \
@@ -255,7 +288,15 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
255288
earlier query is unfinished: {:?} and now {:?}",
256289
q1, Query{span:span2, msg:querymsg2})
257290
},
291+
292+
(ParseState::HaveTimeBegin(_, _), _) => {
293+
unreachable!()
294+
},
295+
(ParseState::HaveTaskBegin(_, _), _) => {
296+
unreachable!()
297+
},
258298
}
299+
259300
}
260301
}
261302
}

src/librustc_driver/profile/trace.rs

+33
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc::ty::maps::QueryMsg;
1414
use std::fs::File;
1515
use std::time::{Duration, Instant};
1616
use std::collections::hash_map::HashMap;
17+
use rustc::dep_graph::{DepNode};
1718

1819
#[derive(Debug, Clone, Eq, PartialEq)]
1920
pub struct Query {
@@ -23,6 +24,7 @@ pub struct Query {
2324
pub enum Effect {
2425
QueryBegin(Query, CacheCase),
2526
TimeBegin(String),
27+
TaskBegin(DepNode),
2628
}
2729
pub enum CacheCase {
2830
Hit, Miss
@@ -46,13 +48,24 @@ pub fn cons_of_query_msg(q: &trace::Query) -> String {
4648
cons[0].to_string()
4749
}
4850

51+
pub fn cons_of_key(k: &DepNode) -> String {
52+
let s = format!("{:?}", k);
53+
let cons: Vec<&str> = s.split(|d| d == '(' || d == '{').collect();
54+
assert!(cons.len() > 0 && cons[0] != "");
55+
cons[0].to_string()
56+
}
57+
4958
// First return value is text; second return value is a CSS class
5059
pub fn html_of_effect(eff: &Effect) -> (String, String) {
5160
match *eff {
5261
Effect::TimeBegin(ref msg) => {
5362
(msg.clone(),
5463
format!("time-begin"))
5564
},
65+
Effect::TaskBegin(ref key) => {
66+
let cons = cons_of_key(key);
67+
(cons.clone(), format!("{} task-begin", cons))
68+
},
5669
Effect::QueryBegin(ref qmsg, ref cc) => {
5770
let cons = cons_of_query_msg(qmsg);
5871
(cons.clone(),
@@ -146,6 +159,20 @@ fn compute_counts_rec(counts: &mut HashMap<String,QueryMetric>, traces: &Vec<Rec
146159
}};
147160
counts.insert(msg.clone(), qm);
148161
},
162+
Effect::TaskBegin(ref key) => {
163+
let cons = cons_of_key(key);
164+
let qm = match counts.get(&cons) {
165+
Some(qm) =>
166+
QueryMetric{
167+
count: qm.count + 1,
168+
duration: qm.duration + t.duration
169+
},
170+
None => QueryMetric{
171+
count: 1,
172+
duration: t.duration
173+
}};
174+
counts.insert(cons, qm);
175+
},
149176
Effect::QueryBegin(ref qmsg, ref _cc) => {
150177
let qcons = cons_of_query_msg(qmsg);
151178
let qm = match counts.get(&qcons) {
@@ -209,6 +236,12 @@ body {
209236
margin: 1px;
210237
font-size: 0px;
211238
}
239+
.task-begin {
240+
border-width: 1px;
241+
color: white;
242+
border-color: #ff8;
243+
font-size: 0px;
244+
}
212245
.miss {
213246
border-color: red;
214247
border-width: 1px;

0 commit comments

Comments
 (0)