|
| 1 | +// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use rustc::util::common::{ProfQDumpParams, ProfileQueriesMsg, profq_msg, profq_set_chan}; |
| 12 | +use std::sync::mpsc::{Receiver}; |
| 13 | +use std::io::{Write}; |
| 14 | + |
| 15 | +pub mod trace; |
| 16 | + |
| 17 | +/// begin a profile thread, if not already running |
| 18 | +pub fn begin() { |
| 19 | + use std::thread; |
| 20 | + use std::sync::mpsc::{channel}; |
| 21 | + let (tx, rx) = channel(); |
| 22 | + if profq_set_chan(tx) { |
| 23 | + thread::spawn(move||profile_queries_thread(rx)); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// dump files with profiling information to the given base path, and |
| 28 | +/// wait for this dump to complete. |
| 29 | +/// |
| 30 | +/// wraps the RPC (send/recv channel logic) of requesting a dump. |
| 31 | +pub fn dump(path:String) { |
| 32 | + use std::sync::mpsc::{channel}; |
| 33 | + let (tx, rx) = channel(); |
| 34 | + let params = ProfQDumpParams{ |
| 35 | + path, ack:tx, |
| 36 | + // FIXME: Add another compiler flag to toggle whether this log |
| 37 | + // is written; false for now |
| 38 | + dump_profq_msg_log:false, |
| 39 | + }; |
| 40 | + profq_msg(ProfileQueriesMsg::Dump(params)); |
| 41 | + let _ = rx.recv().unwrap(); |
| 42 | +} |
| 43 | + |
| 44 | +// profiling thread; retains state (in local variables) and dump traces, upon request. |
| 45 | +fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) { |
| 46 | + use self::trace::*; |
| 47 | + use std::fs::File; |
| 48 | + use std::time::{Instant}; |
| 49 | + |
| 50 | + let mut profq_msgs : Vec<ProfileQueriesMsg> = vec![]; |
| 51 | + let mut frame : StackFrame = StackFrame{ parse_st:ParseState::NoQuery, traces:vec![] }; |
| 52 | + let mut stack : Vec<StackFrame> = vec![]; |
| 53 | + loop { |
| 54 | + let msg = r.recv(); |
| 55 | + if let Err(_recv_err) = msg { |
| 56 | + // FIXME: Perhaps do something smarter than simply quitting? |
| 57 | + break |
| 58 | + }; |
| 59 | + let msg = msg.unwrap(); |
| 60 | + debug!("profile_queries_thread: {:?}", msg); |
| 61 | + |
| 62 | + // Meta-level versus _actual_ queries messages |
| 63 | + match msg { |
| 64 | + ProfileQueriesMsg::Halt => return, |
| 65 | + ProfileQueriesMsg::Dump(params) => { |
| 66 | + assert!(stack.len() == 0); |
| 67 | + assert!(frame.parse_st == trace::ParseState::NoQuery); |
| 68 | + { |
| 69 | + // write log of all messages |
| 70 | + if params.dump_profq_msg_log { |
| 71 | + let mut log_file = |
| 72 | + File::create(format!("{}.log.txt", params.path)).unwrap(); |
| 73 | + for m in profq_msgs.iter() { |
| 74 | + writeln!(&mut log_file, "{:?}", m).unwrap() |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + // write HTML file, and counts file |
| 79 | + let html_path = format!("{}.html", params.path); |
| 80 | + let mut html_file = File::create(&html_path).unwrap(); |
| 81 | + |
| 82 | + let counts_path = format!("{}.counts.txt", params.path); |
| 83 | + let mut counts_file = File::create(&counts_path).unwrap(); |
| 84 | + |
| 85 | + write!(html_file, "<html>\n").unwrap(); |
| 86 | + write!(html_file, |
| 87 | + "<head>\n<link rel=\"stylesheet\" type=\"text/css\" href=\"{}\">\n", |
| 88 | + "profile_queries.css").unwrap(); |
| 89 | + write!(html_file, "<style>\n").unwrap(); |
| 90 | + trace::write_style(&mut html_file); |
| 91 | + write!(html_file, "</style>\n").unwrap(); |
| 92 | + write!(html_file, "</head>\n").unwrap(); |
| 93 | + write!(html_file, "<body>\n").unwrap(); |
| 94 | + trace::write_traces(&mut html_file, &mut counts_file, &frame.traces); |
| 95 | + write!(html_file, "</body>\n</html>\n").unwrap(); |
| 96 | + |
| 97 | + // Tell main thread that we are done, e.g., so it can exit |
| 98 | + params.ack.send(()).unwrap(); |
| 99 | + } |
| 100 | + continue |
| 101 | + } |
| 102 | + // Actual query message: |
| 103 | + msg => { |
| 104 | + // Record msg in our log |
| 105 | + profq_msgs.push(msg.clone()); |
| 106 | + // Respond to the message, knowing that we've already handled Halt and Dump, above. |
| 107 | + match (frame.parse_st.clone(), msg) { |
| 108 | + (_,ProfileQueriesMsg::Halt) => unreachable!(), |
| 109 | + (_,ProfileQueriesMsg::Dump(_)) => unreachable!(), |
| 110 | + |
| 111 | + // Parse State: NoQuery |
| 112 | + (ParseState::NoQuery, |
| 113 | + ProfileQueriesMsg::QueryBegin(span,querymsg)) => { |
| 114 | + let start = Instant::now(); |
| 115 | + frame.parse_st = ParseState::HaveQuery |
| 116 | + (Query{span:span, msg:querymsg}, start) |
| 117 | + }, |
| 118 | + (ParseState::NoQuery, |
| 119 | + ProfileQueriesMsg::CacheHit) => { |
| 120 | + panic!("parse error: unexpected CacheHit; expected QueryBegin") |
| 121 | + }, |
| 122 | + (ParseState::NoQuery, |
| 123 | + ProfileQueriesMsg::ProviderBegin) => { |
| 124 | + panic!("parse error: expected QueryBegin before beginning a provider") |
| 125 | + }, |
| 126 | + (ParseState::NoQuery, |
| 127 | + ProfileQueriesMsg::ProviderEnd) => { |
| 128 | + let provider_extent = frame.traces; |
| 129 | + match stack.pop() { |
| 130 | + None => |
| 131 | + panic!("parse error: expected a stack frame; found an empty stack"), |
| 132 | + Some(old_frame) => { |
| 133 | + match old_frame.parse_st { |
| 134 | + ParseState::NoQuery => |
| 135 | + panic!("parse error: expected a stack frame for a query"), |
| 136 | + ParseState::HaveQuery(q,start) => { |
| 137 | + let duration = start.elapsed(); |
| 138 | + frame = StackFrame{ |
| 139 | + parse_st:ParseState::NoQuery, |
| 140 | + traces:old_frame.traces |
| 141 | + }; |
| 142 | + let trace = Rec { |
| 143 | + effect: Effect::QueryBegin(q, CacheCase::Miss), |
| 144 | + extent: Box::new(provider_extent), |
| 145 | + start: start, |
| 146 | + duration: duration, |
| 147 | + }; |
| 148 | + frame.traces.push( trace ); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // Parse State: HaveQuery |
| 156 | + (ParseState::HaveQuery(q,start), |
| 157 | + ProfileQueriesMsg::CacheHit) => { |
| 158 | + let duration = start.elapsed(); |
| 159 | + let trace : Rec = Rec{ |
| 160 | + effect: Effect::QueryBegin(q, CacheCase::Hit), |
| 161 | + extent: Box::new(vec![]), |
| 162 | + start: start, |
| 163 | + duration: duration, |
| 164 | + }; |
| 165 | + frame.traces.push( trace ); |
| 166 | + frame.parse_st = ParseState::NoQuery; |
| 167 | + }, |
| 168 | + (ParseState::HaveQuery(_,_), |
| 169 | + ProfileQueriesMsg::ProviderBegin) => { |
| 170 | + stack.push(frame); |
| 171 | + frame = StackFrame{parse_st:ParseState::NoQuery, traces:vec![]}; |
| 172 | + }, |
| 173 | + (ParseState::HaveQuery(q,_), |
| 174 | + ProfileQueriesMsg::ProviderEnd) => { |
| 175 | + panic!("parse error: unexpected ProviderEnd; \ |
| 176 | + expected something else to follow BeginQuery for {:?}", q) |
| 177 | + }, |
| 178 | + (ParseState::HaveQuery(q1,_), |
| 179 | + ProfileQueriesMsg::QueryBegin(span2,querymsg2)) => { |
| 180 | + panic!("parse error: unexpected QueryBegin; \ |
| 181 | + earlier query is unfinished: {:?} and now {:?}", |
| 182 | + q1, Query{span:span2, msg:querymsg2}) |
| 183 | + }, |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments