Skip to content

Commit

Permalink
Rollup merge of rust-lang#61123 - michaelwoerister:self-profile-dir, …
Browse files Browse the repository at this point in the history
…r=wesleywiser

Allow to specify profiling data output directory as -Zself-profile argument.

The PR also makes `rustc` include the crate-name (if already available) in the output file name.

r? @wesleywiser

At some point we should add some basic tests for `-Zself-profile`.
  • Loading branch information
Centril committed May 28, 2019
2 parents 7da1185 + 64ee32e commit 56b57fb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 28 deletions.
32 changes: 17 additions & 15 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ impl LinkerPluginLto {
}

#[derive(Clone, PartialEq, Hash)]
pub enum PgoGenerate {
pub enum SwitchWithOptPath {
Enabled(Option<PathBuf>),
Disabled,
}

impl PgoGenerate {
impl SwitchWithOptPath {
pub fn enabled(&self) -> bool {
match *self {
PgoGenerate::Enabled(_) => true,
PgoGenerate::Disabled => false,
SwitchWithOptPath::Enabled(_) => true,
SwitchWithOptPath::Disabled => false,
}
}
}
Expand Down Expand Up @@ -834,15 +834,15 @@ macro_rules! options {
pub const parse_linker_plugin_lto: Option<&str> =
Some("either a boolean (`yes`, `no`, `on`, `off`, etc), \
or the path to the linker plugin");
pub const parse_pgo_generate: Option<&str> =
pub const parse_switch_with_opt_path: Option<&str> =
Some("an optional path to the profiling data output directory");
pub const parse_merge_functions: Option<&str> =
Some("one of: `disabled`, `trampolines`, or `aliases`");
}

#[allow(dead_code)]
mod $mod_set {
use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto, PgoGenerate};
use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath};
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -1097,10 +1097,10 @@ macro_rules! options {
true
}

fn parse_pgo_generate(slot: &mut PgoGenerate, v: Option<&str>) -> bool {
fn parse_switch_with_opt_path(slot: &mut SwitchWithOptPath, v: Option<&str>) -> bool {
*slot = match v {
None => PgoGenerate::Enabled(None),
Some(path) => PgoGenerate::Enabled(Some(PathBuf::from(path))),
None => SwitchWithOptPath::Enabled(None),
Some(path) => SwitchWithOptPath::Enabled(Some(PathBuf::from(path))),
};
true
}
Expand Down Expand Up @@ -1379,7 +1379,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"extra arguments to prepend to the linker invocation (space separated)"),
profile: bool = (false, parse_bool, [TRACKED],
"insert profiling code"),
pgo_gen: PgoGenerate = (PgoGenerate::Disabled, parse_pgo_generate, [TRACKED],
pgo_gen: SwitchWithOptPath = (SwitchWithOptPath::Disabled,
parse_switch_with_opt_path, [TRACKED],
"Generate PGO profile data, to a given file, or to the default location if it's empty."),
pgo_use: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
"Use PGO profile data from the given profile file."),
Expand Down Expand Up @@ -1447,7 +1448,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"don't interleave execution of lints; allows benchmarking individual lints"),
crate_attr: Vec<String> = (Vec::new(), parse_string_push, [TRACKED],
"inject the given attribute in the crate"),
self_profile: bool = (false, parse_bool, [UNTRACKED],
self_profile: SwitchWithOptPath = (SwitchWithOptPath::Disabled,
parse_switch_with_opt_path, [UNTRACKED],
"run the self profiler and output the raw event data"),
self_profile_events: Option<Vec<String>> = (None, parse_opt_comma_list, [UNTRACKED],
"specifies which kinds of events get recorded by the self profiler"),
Expand Down Expand Up @@ -2558,7 +2560,7 @@ mod dep_tracking {
use std::path::PathBuf;
use std::collections::hash_map::DefaultHasher;
use super::{CrateType, DebugInfo, ErrorOutputType, OptLevel, OutputTypes,
Passes, Sanitizer, LtoCli, LinkerPluginLto, PgoGenerate};
Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath};
use syntax::feature_gate::UnstableFeatures;
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel, TargetTriple};
use syntax::edition::Edition;
Expand Down Expand Up @@ -2626,7 +2628,7 @@ mod dep_tracking {
impl_dep_tracking_hash_via_hash!(TargetTriple);
impl_dep_tracking_hash_via_hash!(Edition);
impl_dep_tracking_hash_via_hash!(LinkerPluginLto);
impl_dep_tracking_hash_via_hash!(PgoGenerate);
impl_dep_tracking_hash_via_hash!(SwitchWithOptPath);

impl_dep_tracking_hash_for_sortable_vec_of!(String);
impl_dep_tracking_hash_for_sortable_vec_of!(PathBuf);
Expand Down Expand Up @@ -2694,7 +2696,7 @@ mod tests {
build_session_options_and_crate_config,
to_crate_config
};
use crate::session::config::{LtoCli, LinkerPluginLto, PgoGenerate, ExternEntry};
use crate::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry};
use crate::session::build_session;
use crate::session::search_paths::SearchPath;
use std::collections::{BTreeMap, BTreeSet};
Expand Down Expand Up @@ -3207,7 +3209,7 @@ mod tests {
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());

opts = reference.clone();
opts.debugging_opts.pgo_gen = PgoGenerate::Enabled(None);
opts.debugging_opts.pgo_gen = SwitchWithOptPath::Enabled(None);
assert_ne!(reference.dep_tracking_hash(), opts.dep_tracking_hash());

opts = reference.clone();
Expand Down
16 changes: 13 additions & 3 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::lint;
use crate::lint::builtin::BuiltinLintDiagnostics;
use crate::middle::allocator::AllocatorKind;
use crate::middle::dependency_format;
use crate::session::config::OutputType;
use crate::session::config::{OutputType, SwitchWithOptPath};
use crate::session::search_paths::{PathKind, SearchPath};
use crate::util::nodemap::{FxHashMap, FxHashSet};
use crate::util::common::{duration_to_secs_str, ErrorReported};
Expand Down Expand Up @@ -1137,8 +1137,18 @@ fn build_session_(
driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
) -> Session {
let self_profiler =
if sopts.debugging_opts.self_profile {
let profiler = SelfProfiler::new(&sopts.debugging_opts.self_profile_events);
if let SwitchWithOptPath::Enabled(ref d) = sopts.debugging_opts.self_profile {
let directory = if let Some(ref directory) = d {
directory
} else {
std::path::Path::new(".")
};

let profiler = SelfProfiler::new(
directory,
sopts.crate_name.as_ref().map(|s| &s[..]),
&sopts.debugging_opts.self_profile_events
);
match profiler {
Ok(profiler) => {
crate::ty::query::QueryName::register_with_profiler(&profiler);
Expand Down
17 changes: 13 additions & 4 deletions src/librustc/util/profiling.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::borrow::Cow;
use std::error::Error;
use std::fs;
use std::mem::{self, Discriminant};
use std::path::Path;
use std::process;
use std::thread::ThreadId;
use std::u32;
Expand Down Expand Up @@ -71,10 +73,17 @@ pub struct SelfProfiler {
}

impl SelfProfiler {
pub fn new(event_filters: &Option<Vec<String>>) -> Result<SelfProfiler, Box<dyn Error>> {
let filename = format!("pid-{}.rustc_profile", process::id());
let path = std::path::Path::new(&filename);
let profiler = Profiler::new(path)?;
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)?;

let query_event_kind = profiler.alloc_string("Query");
let generic_activity_event_kind = profiler.alloc_string("GenericActivity");
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_codegen_llvm/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::LlvmCodegenBackend;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc_codegen_ssa::back::write::{CodegenContext, ModuleConfig, run_assembler};
use rustc_codegen_ssa::traits::*;
use rustc::session::config::{self, OutputType, Passes, Lto, PgoGenerate};
use rustc::session::config::{self, OutputType, Passes, Lto, SwitchWithOptPath};
use rustc::session::Session;
use rustc::ty::TyCtxt;
use rustc_codegen_ssa::{RLIB_BYTECODE_EXTENSION, ModuleCodegen, CompiledModule};
Expand Down Expand Up @@ -707,7 +707,7 @@ pub unsafe fn with_llvm_pmb(llmod: &llvm::Module,
let inline_threshold = config.inline_threshold;

let pgo_gen_path = match config.pgo_gen {
PgoGenerate::Enabled(ref opt_dir_path) => {
SwitchWithOptPath::Enabled(ref opt_dir_path) => {
let path = if let Some(dir_path) = opt_dir_path {
dir_path.join("default_%m.profraw")
} else {
Expand All @@ -716,7 +716,7 @@ pub unsafe fn with_llvm_pmb(llmod: &llvm::Module,

Some(CString::new(format!("{}", path.display())).unwrap())
}
PgoGenerate::Disabled => {
SwitchWithOptPath::Disabled => {
None
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_codegen_ssa/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc::dep_graph::{WorkProduct, WorkProductId, WorkProductFileKind};
use rustc::dep_graph::cgu_reuse_tracker::CguReuseTracker;
use rustc::middle::cstore::EncodedMetadata;
use rustc::session::config::{self, OutputFilenames, OutputType, Passes, Lto,
Sanitizer, PgoGenerate};
Sanitizer, SwitchWithOptPath};
use rustc::session::Session;
use rustc::util::nodemap::FxHashMap;
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
Expand Down Expand Up @@ -56,7 +56,7 @@ pub struct ModuleConfig {
/// Some(level) to optimize binary size, or None to not affect program size.
pub opt_size: Option<config::OptLevel>,

pub pgo_gen: PgoGenerate,
pub pgo_gen: SwitchWithOptPath,
pub pgo_use: Option<PathBuf>,

// Flags indicating which outputs to produce.
Expand Down Expand Up @@ -94,7 +94,7 @@ impl ModuleConfig {
opt_level: None,
opt_size: None,

pgo_gen: PgoGenerate::Disabled,
pgo_gen: SwitchWithOptPath::Disabled,
pgo_use: None,

emit_no_opt_bc: false,
Expand Down

0 comments on commit 56b57fb

Please sign in to comment.