2019-02-13 07:13:30 -06:00
|
|
|
use std::borrow::Cow;
|
2019-04-04 18:41:49 -05:00
|
|
|
use std::error::Error;
|
2019-05-24 09:36:44 -05:00
|
|
|
use std::fs;
|
2019-04-04 18:41:49 -05:00
|
|
|
use std::mem::{self, Discriminant};
|
2019-05-24 09:36:44 -05:00
|
|
|
use std::path::Path;
|
2019-02-11 17:11:43 -06:00
|
|
|
use std::process;
|
2019-01-27 12:52:17 -06:00
|
|
|
use std::thread::ThreadId;
|
2019-04-04 18:41:49 -05:00
|
|
|
use std::u32;
|
2018-05-19 12:50:58 -05:00
|
|
|
|
2019-04-04 18:41:49 -05:00
|
|
|
use crate::ty::query::QueryName;
|
2019-01-27 12:52:17 -06:00
|
|
|
|
2019-04-04 18:41:49 -05:00
|
|
|
use measureme::{StringId, TimestampKind};
|
|
|
|
|
|
|
|
/// MmapSerializatioSink is faster on macOS and Linux
|
|
|
|
/// but FileSerializationSink is faster on Windows
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
type Profiler = measureme::Profiler<measureme::MmapSerializationSink>;
|
|
|
|
#[cfg(windows)]
|
|
|
|
type Profiler = measureme::Profiler<measureme::FileSerializationSink>;
|
2019-02-25 17:46:53 -06:00
|
|
|
|
2019-01-27 12:52:17 -06:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)]
|
|
|
|
pub enum ProfileCategory {
|
|
|
|
Parsing,
|
|
|
|
Expansion,
|
|
|
|
TypeChecking,
|
|
|
|
BorrowChecking,
|
|
|
|
Codegen,
|
|
|
|
Linking,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
2019-04-12 07:48:41 -05:00
|
|
|
bitflags! {
|
|
|
|
struct EventFilter: u32 {
|
|
|
|
const GENERIC_ACTIVITIES = 1 << 0;
|
|
|
|
const QUERY_PROVIDERS = 1 << 1;
|
|
|
|
const QUERY_CACHE_HITS = 1 << 2;
|
|
|
|
const QUERY_BLOCKED = 1 << 3;
|
|
|
|
const INCR_CACHE_LOADS = 1 << 4;
|
|
|
|
|
|
|
|
const DEFAULT = Self::GENERIC_ACTIVITIES.bits |
|
|
|
|
Self::QUERY_PROVIDERS.bits |
|
|
|
|
Self::QUERY_BLOCKED.bits |
|
|
|
|
Self::INCR_CACHE_LOADS.bits;
|
|
|
|
|
|
|
|
// empty() and none() aren't const-fns unfortunately
|
|
|
|
const NONE = 0;
|
|
|
|
const ALL = !Self::NONE.bits;
|
|
|
|
}
|
2019-01-27 12:52:17 -06:00
|
|
|
}
|
|
|
|
|
2019-04-12 07:48:41 -05:00
|
|
|
const EVENT_FILTERS_BY_NAME: &[(&str, EventFilter)] = &[
|
|
|
|
("none", EventFilter::NONE),
|
|
|
|
("all", EventFilter::ALL),
|
|
|
|
("generic-activity", EventFilter::GENERIC_ACTIVITIES),
|
|
|
|
("query-provider", EventFilter::QUERY_PROVIDERS),
|
|
|
|
("query-cache-hit", EventFilter::QUERY_CACHE_HITS),
|
|
|
|
("query-blocked" , EventFilter::QUERY_BLOCKED),
|
|
|
|
("incr-cache-load", EventFilter::INCR_CACHE_LOADS),
|
|
|
|
];
|
|
|
|
|
2019-02-11 17:11:43 -06:00
|
|
|
fn thread_id_to_u64(tid: ThreadId) -> u64 {
|
|
|
|
unsafe { mem::transmute::<ThreadId, u64>(tid) }
|
2018-05-19 12:50:58 -05:00
|
|
|
}
|
|
|
|
|
2019-02-11 17:11:43 -06:00
|
|
|
pub struct SelfProfiler {
|
2019-04-04 18:41:49 -05:00
|
|
|
profiler: Profiler,
|
2019-04-12 07:48:41 -05:00
|
|
|
event_filter_mask: EventFilter,
|
2019-04-04 18:41:49 -05:00
|
|
|
query_event_kind: StringId,
|
|
|
|
generic_activity_event_kind: StringId,
|
|
|
|
incremental_load_result_event_kind: StringId,
|
|
|
|
query_blocked_event_kind: StringId,
|
|
|
|
query_cache_hit_event_kind: StringId,
|
2018-05-19 12:50:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SelfProfiler {
|
2019-05-24 09:36:44 -05:00
|
|
|
pub fn new(
|
|
|
|
output_directory: &Path,
|
|
|
|
crate_name: Option<&str>,
|
|
|
|
event_filters: &Option<Vec<String>>
|
|
|
|
) -> Result<SelfProfiler, Box<dyn Error>> {
|
|
|
|
fs::create_dir_all(output_directory)?;
|
|
|
|
|
|
|
|
let crate_name = crate_name.unwrap_or("unknown-crate");
|
|
|
|
let filename = format!("{}-{}.rustc_profile", crate_name, process::id());
|
|
|
|
let path = output_directory.join(&filename);
|
|
|
|
let profiler = Profiler::new(&path)?;
|
2019-04-04 18:41:49 -05:00
|
|
|
|
|
|
|
let query_event_kind = profiler.alloc_string("Query");
|
|
|
|
let generic_activity_event_kind = profiler.alloc_string("GenericActivity");
|
|
|
|
let incremental_load_result_event_kind = profiler.alloc_string("IncrementalLoadResult");
|
|
|
|
let query_blocked_event_kind = profiler.alloc_string("QueryBlocked");
|
|
|
|
let query_cache_hit_event_kind = profiler.alloc_string("QueryCacheHit");
|
|
|
|
|
2019-04-12 07:48:41 -05:00
|
|
|
let mut event_filter_mask = EventFilter::empty();
|
|
|
|
|
|
|
|
if let Some(ref event_filters) = *event_filters {
|
|
|
|
let mut unknown_events = vec![];
|
|
|
|
for item in event_filters {
|
|
|
|
if let Some(&(_, mask)) = EVENT_FILTERS_BY_NAME.iter()
|
|
|
|
.find(|&(name, _)| name == item) {
|
|
|
|
event_filter_mask |= mask;
|
|
|
|
} else {
|
|
|
|
unknown_events.push(item.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn about any unknown event names
|
|
|
|
if unknown_events.len() > 0 {
|
|
|
|
unknown_events.sort();
|
|
|
|
unknown_events.dedup();
|
|
|
|
|
|
|
|
warn!("Unknown self-profiler events specified: {}. Available options are: {}.",
|
|
|
|
unknown_events.join(", "),
|
|
|
|
EVENT_FILTERS_BY_NAME.iter()
|
|
|
|
.map(|&(name, _)| name.to_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event_filter_mask = EventFilter::DEFAULT;
|
|
|
|
}
|
|
|
|
|
2019-04-04 18:41:49 -05:00
|
|
|
Ok(SelfProfiler {
|
|
|
|
profiler,
|
2019-04-12 07:48:41 -05:00
|
|
|
event_filter_mask,
|
2019-04-04 18:41:49 -05:00
|
|
|
query_event_kind,
|
|
|
|
generic_activity_event_kind,
|
|
|
|
incremental_load_result_event_kind,
|
|
|
|
query_blocked_event_kind,
|
|
|
|
query_cache_hit_event_kind,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_query_name_string_id(query_name: QueryName) -> StringId {
|
|
|
|
let discriminant = unsafe {
|
|
|
|
mem::transmute::<Discriminant<QueryName>, u64>(mem::discriminant(&query_name))
|
2018-05-19 12:50:58 -05:00
|
|
|
};
|
|
|
|
|
2019-04-04 18:41:49 -05:00
|
|
|
StringId::reserved(discriminant as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register_query_name(&self, query_name: QueryName) {
|
|
|
|
let id = SelfProfiler::get_query_name_string_id(query_name);
|
|
|
|
self.profiler.alloc_string_with_reserved_id(id, query_name.as_str());
|
2018-05-19 12:50:58 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 10:59:41 -06:00
|
|
|
#[inline]
|
2019-02-13 07:13:30 -06:00
|
|
|
pub fn start_activity(
|
2019-04-04 18:41:49 -05:00
|
|
|
&self,
|
2019-02-13 07:13:30 -06:00
|
|
|
label: impl Into<Cow<'static, str>>,
|
|
|
|
) {
|
2019-04-12 07:48:41 -05:00
|
|
|
if self.event_filter_mask.contains(EventFilter::GENERIC_ACTIVITIES) {
|
|
|
|
self.record(&label.into(), self.generic_activity_event_kind, TimestampKind::Start);
|
|
|
|
}
|
2019-01-27 12:52:17 -06:00
|
|
|
}
|
2018-05-19 12:50:58 -05:00
|
|
|
|
2019-02-05 10:59:41 -06:00
|
|
|
#[inline]
|
2019-02-13 07:13:30 -06:00
|
|
|
pub fn end_activity(
|
2019-04-04 18:41:49 -05:00
|
|
|
&self,
|
2019-02-13 07:13:30 -06:00
|
|
|
label: impl Into<Cow<'static, str>>,
|
|
|
|
) {
|
2019-04-12 07:48:41 -05:00
|
|
|
if self.event_filter_mask.contains(EventFilter::GENERIC_ACTIVITIES) {
|
|
|
|
self.record(&label.into(), self.generic_activity_event_kind, TimestampKind::End);
|
|
|
|
}
|
2019-01-27 12:52:17 -06:00
|
|
|
}
|
2018-05-19 12:50:58 -05:00
|
|
|
|
2019-02-05 10:59:41 -06:00
|
|
|
#[inline]
|
2019-04-04 18:41:49 -05:00
|
|
|
pub fn record_query_hit(&self, query_name: QueryName) {
|
2019-04-12 07:48:41 -05:00
|
|
|
if self.event_filter_mask.contains(EventFilter::QUERY_CACHE_HITS) {
|
|
|
|
self.record_query(query_name, self.query_cache_hit_event_kind, TimestampKind::Instant);
|
|
|
|
}
|
2018-05-19 12:50:58 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 10:59:41 -06:00
|
|
|
#[inline]
|
2019-04-04 18:41:49 -05:00
|
|
|
pub fn start_query(&self, query_name: QueryName) {
|
2019-04-12 07:48:41 -05:00
|
|
|
if self.event_filter_mask.contains(EventFilter::QUERY_PROVIDERS) {
|
|
|
|
self.record_query(query_name, self.query_event_kind, TimestampKind::Start);
|
|
|
|
}
|
2018-05-29 22:12:49 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 10:59:41 -06:00
|
|
|
#[inline]
|
2019-04-04 18:41:49 -05:00
|
|
|
pub fn end_query(&self, query_name: QueryName) {
|
2019-04-12 07:48:41 -05:00
|
|
|
if self.event_filter_mask.contains(EventFilter::QUERY_PROVIDERS) {
|
|
|
|
self.record_query(query_name, self.query_event_kind, TimestampKind::End);
|
|
|
|
}
|
2018-05-29 22:12:49 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 10:59:41 -06:00
|
|
|
#[inline]
|
2019-04-04 18:41:49 -05:00
|
|
|
pub fn incremental_load_result_start(&self, query_name: QueryName) {
|
2019-04-12 07:48:41 -05:00
|
|
|
if self.event_filter_mask.contains(EventFilter::INCR_CACHE_LOADS) {
|
|
|
|
self.record_query(
|
|
|
|
query_name,
|
|
|
|
self.incremental_load_result_event_kind,
|
|
|
|
TimestampKind::Start
|
|
|
|
);
|
|
|
|
}
|
2019-01-27 12:52:17 -06:00
|
|
|
}
|
|
|
|
|
2019-02-07 02:58:14 -06:00
|
|
|
#[inline]
|
2019-04-04 18:41:49 -05:00
|
|
|
pub fn incremental_load_result_end(&self, query_name: QueryName) {
|
2019-04-12 07:48:41 -05:00
|
|
|
if self.event_filter_mask.contains(EventFilter::INCR_CACHE_LOADS) {
|
|
|
|
self.record_query(
|
|
|
|
query_name,
|
|
|
|
self.incremental_load_result_event_kind,
|
|
|
|
TimestampKind::End
|
|
|
|
);
|
|
|
|
}
|
2019-02-07 02:58:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-04 18:41:49 -05:00
|
|
|
pub fn query_blocked_start(&self, query_name: QueryName) {
|
2019-04-12 07:48:41 -05:00
|
|
|
if self.event_filter_mask.contains(EventFilter::QUERY_BLOCKED) {
|
|
|
|
self.record_query(query_name, self.query_blocked_event_kind, TimestampKind::Start);
|
|
|
|
}
|
2019-02-08 10:17:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-04 18:41:49 -05:00
|
|
|
pub fn query_blocked_end(&self, query_name: QueryName) {
|
2019-04-12 07:48:41 -05:00
|
|
|
if self.event_filter_mask.contains(EventFilter::QUERY_BLOCKED) {
|
|
|
|
self.record_query(query_name, self.query_blocked_event_kind, TimestampKind::End);
|
|
|
|
}
|
2019-02-08 10:17:58 -06:00
|
|
|
}
|
|
|
|
|
2019-02-05 10:59:41 -06:00
|
|
|
#[inline]
|
2019-04-04 18:41:49 -05:00
|
|
|
fn record(&self, event_id: &str, event_kind: StringId, timestamp_kind: TimestampKind) {
|
|
|
|
let thread_id = thread_id_to_u64(std::thread::current().id());
|
2019-01-27 12:52:17 -06:00
|
|
|
|
2019-04-04 18:41:49 -05:00
|
|
|
let event_id = self.profiler.alloc_string(event_id);
|
|
|
|
self.profiler.record_event(event_kind, event_id, thread_id, timestamp_kind);
|
2019-01-27 12:52:17 -06:00
|
|
|
}
|
|
|
|
|
2019-02-13 04:33:51 -06:00
|
|
|
#[inline]
|
2019-04-04 18:41:49 -05:00
|
|
|
fn record_query(
|
|
|
|
&self,
|
|
|
|
query_name: QueryName,
|
|
|
|
event_kind: StringId,
|
|
|
|
timestamp_kind: TimestampKind,
|
|
|
|
) {
|
|
|
|
let dep_node_name = SelfProfiler::get_query_name_string_id(query_name);
|
2019-02-11 17:11:43 -06:00
|
|
|
|
2019-04-04 18:41:49 -05:00
|
|
|
let thread_id = thread_id_to_u64(std::thread::current().id());
|
2018-05-19 12:50:58 -05:00
|
|
|
|
2019-04-04 18:41:49 -05:00
|
|
|
self.profiler.record_event(event_kind, dep_node_name, thread_id, timestamp_kind);
|
2018-06-05 22:05:30 -05:00
|
|
|
}
|
2018-06-20 06:57:22 -05:00
|
|
|
}
|