Skip to content

Commit 251ca03

Browse files
authored
Rollup merge of #61123 - michaelwoerister:self-profile-dir, 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`.
2 parents 7da1185 + 64ee32e commit 251ca03

File tree

5 files changed

+49
-28
lines changed

5 files changed

+49
-28
lines changed

src/librustc/session/config.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@ impl LinkerPluginLto {
117117
}
118118

119119
#[derive(Clone, PartialEq, Hash)]
120-
pub enum PgoGenerate {
120+
pub enum SwitchWithOptPath {
121121
Enabled(Option<PathBuf>),
122122
Disabled,
123123
}
124124

125-
impl PgoGenerate {
125+
impl SwitchWithOptPath {
126126
pub fn enabled(&self) -> bool {
127127
match *self {
128-
PgoGenerate::Enabled(_) => true,
129-
PgoGenerate::Disabled => false,
128+
SwitchWithOptPath::Enabled(_) => true,
129+
SwitchWithOptPath::Disabled => false,
130130
}
131131
}
132132
}
@@ -834,15 +834,15 @@ macro_rules! options {
834834
pub const parse_linker_plugin_lto: Option<&str> =
835835
Some("either a boolean (`yes`, `no`, `on`, `off`, etc), \
836836
or the path to the linker plugin");
837-
pub const parse_pgo_generate: Option<&str> =
837+
pub const parse_switch_with_opt_path: Option<&str> =
838838
Some("an optional path to the profiling data output directory");
839839
pub const parse_merge_functions: Option<&str> =
840840
Some("one of: `disabled`, `trampolines`, or `aliases`");
841841
}
842842

843843
#[allow(dead_code)]
844844
mod $mod_set {
845-
use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto, PgoGenerate};
845+
use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath};
846846
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
847847
use std::path::PathBuf;
848848
use std::str::FromStr;
@@ -1097,10 +1097,10 @@ macro_rules! options {
10971097
true
10981098
}
10991099

1100-
fn parse_pgo_generate(slot: &mut PgoGenerate, v: Option<&str>) -> bool {
1100+
fn parse_switch_with_opt_path(slot: &mut SwitchWithOptPath, v: Option<&str>) -> bool {
11011101
*slot = match v {
1102-
None => PgoGenerate::Enabled(None),
1103-
Some(path) => PgoGenerate::Enabled(Some(PathBuf::from(path))),
1102+
None => SwitchWithOptPath::Enabled(None),
1103+
Some(path) => SwitchWithOptPath::Enabled(Some(PathBuf::from(path))),
11041104
};
11051105
true
11061106
}
@@ -1379,7 +1379,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
13791379
"extra arguments to prepend to the linker invocation (space separated)"),
13801380
profile: bool = (false, parse_bool, [TRACKED],
13811381
"insert profiling code"),
1382-
pgo_gen: PgoGenerate = (PgoGenerate::Disabled, parse_pgo_generate, [TRACKED],
1382+
pgo_gen: SwitchWithOptPath = (SwitchWithOptPath::Disabled,
1383+
parse_switch_with_opt_path, [TRACKED],
13831384
"Generate PGO profile data, to a given file, or to the default location if it's empty."),
13841385
pgo_use: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
13851386
"Use PGO profile data from the given profile file."),
@@ -1447,7 +1448,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14471448
"don't interleave execution of lints; allows benchmarking individual lints"),
14481449
crate_attr: Vec<String> = (Vec::new(), parse_string_push, [TRACKED],
14491450
"inject the given attribute in the crate"),
1450-
self_profile: bool = (false, parse_bool, [UNTRACKED],
1451+
self_profile: SwitchWithOptPath = (SwitchWithOptPath::Disabled,
1452+
parse_switch_with_opt_path, [UNTRACKED],
14511453
"run the self profiler and output the raw event data"),
14521454
self_profile_events: Option<Vec<String>> = (None, parse_opt_comma_list, [UNTRACKED],
14531455
"specifies which kinds of events get recorded by the self profiler"),
@@ -2558,7 +2560,7 @@ mod dep_tracking {
25582560
use std::path::PathBuf;
25592561
use std::collections::hash_map::DefaultHasher;
25602562
use super::{CrateType, DebugInfo, ErrorOutputType, OptLevel, OutputTypes,
2561-
Passes, Sanitizer, LtoCli, LinkerPluginLto, PgoGenerate};
2563+
Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath};
25622564
use syntax::feature_gate::UnstableFeatures;
25632565
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel, TargetTriple};
25642566
use syntax::edition::Edition;
@@ -2626,7 +2628,7 @@ mod dep_tracking {
26262628
impl_dep_tracking_hash_via_hash!(TargetTriple);
26272629
impl_dep_tracking_hash_via_hash!(Edition);
26282630
impl_dep_tracking_hash_via_hash!(LinkerPluginLto);
2629-
impl_dep_tracking_hash_via_hash!(PgoGenerate);
2631+
impl_dep_tracking_hash_via_hash!(SwitchWithOptPath);
26302632

26312633
impl_dep_tracking_hash_for_sortable_vec_of!(String);
26322634
impl_dep_tracking_hash_for_sortable_vec_of!(PathBuf);
@@ -2694,7 +2696,7 @@ mod tests {
26942696
build_session_options_and_crate_config,
26952697
to_crate_config
26962698
};
2697-
use crate::session::config::{LtoCli, LinkerPluginLto, PgoGenerate, ExternEntry};
2699+
use crate::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry};
26982700
use crate::session::build_session;
26992701
use crate::session::search_paths::SearchPath;
27002702
use std::collections::{BTreeMap, BTreeSet};
@@ -3207,7 +3209,7 @@ mod tests {
32073209
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
32083210

32093211
opts = reference.clone();
3210-
opts.debugging_opts.pgo_gen = PgoGenerate::Enabled(None);
3212+
opts.debugging_opts.pgo_gen = SwitchWithOptPath::Enabled(None);
32113213
assert_ne!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
32123214

32133215
opts = reference.clone();

src/librustc/session/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::lint;
99
use crate::lint::builtin::BuiltinLintDiagnostics;
1010
use crate::middle::allocator::AllocatorKind;
1111
use crate::middle::dependency_format;
12-
use crate::session::config::OutputType;
12+
use crate::session::config::{OutputType, SwitchWithOptPath};
1313
use crate::session::search_paths::{PathKind, SearchPath};
1414
use crate::util::nodemap::{FxHashMap, FxHashSet};
1515
use crate::util::common::{duration_to_secs_str, ErrorReported};
@@ -1137,8 +1137,18 @@ fn build_session_(
11371137
driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
11381138
) -> Session {
11391139
let self_profiler =
1140-
if sopts.debugging_opts.self_profile {
1141-
let profiler = SelfProfiler::new(&sopts.debugging_opts.self_profile_events);
1140+
if let SwitchWithOptPath::Enabled(ref d) = sopts.debugging_opts.self_profile {
1141+
let directory = if let Some(ref directory) = d {
1142+
directory
1143+
} else {
1144+
std::path::Path::new(".")
1145+
};
1146+
1147+
let profiler = SelfProfiler::new(
1148+
directory,
1149+
sopts.crate_name.as_ref().map(|s| &s[..]),
1150+
&sopts.debugging_opts.self_profile_events
1151+
);
11421152
match profiler {
11431153
Ok(profiler) => {
11441154
crate::ty::query::QueryName::register_with_profiler(&profiler);

src/librustc/util/profiling.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::borrow::Cow;
22
use std::error::Error;
3+
use std::fs;
34
use std::mem::{self, Discriminant};
5+
use std::path::Path;
46
use std::process;
57
use std::thread::ThreadId;
68
use std::u32;
@@ -71,10 +73,17 @@ pub struct SelfProfiler {
7173
}
7274

7375
impl SelfProfiler {
74-
pub fn new(event_filters: &Option<Vec<String>>) -> Result<SelfProfiler, Box<dyn Error>> {
75-
let filename = format!("pid-{}.rustc_profile", process::id());
76-
let path = std::path::Path::new(&filename);
77-
let profiler = Profiler::new(path)?;
76+
pub fn new(
77+
output_directory: &Path,
78+
crate_name: Option<&str>,
79+
event_filters: &Option<Vec<String>>
80+
) -> Result<SelfProfiler, Box<dyn Error>> {
81+
fs::create_dir_all(output_directory)?;
82+
83+
let crate_name = crate_name.unwrap_or("unknown-crate");
84+
let filename = format!("{}-{}.rustc_profile", crate_name, process::id());
85+
let path = output_directory.join(&filename);
86+
let profiler = Profiler::new(&path)?;
7887

7988
let query_event_kind = profiler.alloc_string("Query");
8089
let generic_activity_event_kind = profiler.alloc_string("GenericActivity");

src/librustc_codegen_llvm/back/write.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::LlvmCodegenBackend;
1313
use rustc::hir::def_id::LOCAL_CRATE;
1414
use rustc_codegen_ssa::back::write::{CodegenContext, ModuleConfig, run_assembler};
1515
use rustc_codegen_ssa::traits::*;
16-
use rustc::session::config::{self, OutputType, Passes, Lto, PgoGenerate};
16+
use rustc::session::config::{self, OutputType, Passes, Lto, SwitchWithOptPath};
1717
use rustc::session::Session;
1818
use rustc::ty::TyCtxt;
1919
use rustc_codegen_ssa::{RLIB_BYTECODE_EXTENSION, ModuleCodegen, CompiledModule};
@@ -707,7 +707,7 @@ pub unsafe fn with_llvm_pmb(llmod: &llvm::Module,
707707
let inline_threshold = config.inline_threshold;
708708

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

717717
Some(CString::new(format!("{}", path.display())).unwrap())
718718
}
719-
PgoGenerate::Disabled => {
719+
SwitchWithOptPath::Disabled => {
720720
None
721721
}
722722
};

src/librustc_codegen_ssa/back/write.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc::dep_graph::{WorkProduct, WorkProductId, WorkProductFileKind};
1313
use rustc::dep_graph::cgu_reuse_tracker::CguReuseTracker;
1414
use rustc::middle::cstore::EncodedMetadata;
1515
use rustc::session::config::{self, OutputFilenames, OutputType, Passes, Lto,
16-
Sanitizer, PgoGenerate};
16+
Sanitizer, SwitchWithOptPath};
1717
use rustc::session::Session;
1818
use rustc::util::nodemap::FxHashMap;
1919
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
@@ -56,7 +56,7 @@ pub struct ModuleConfig {
5656
/// Some(level) to optimize binary size, or None to not affect program size.
5757
pub opt_size: Option<config::OptLevel>,
5858

59-
pub pgo_gen: PgoGenerate,
59+
pub pgo_gen: SwitchWithOptPath,
6060
pub pgo_use: Option<PathBuf>,
6161

6262
// Flags indicating which outputs to produce.
@@ -94,7 +94,7 @@ impl ModuleConfig {
9494
opt_level: None,
9595
opt_size: None,
9696

97-
pgo_gen: PgoGenerate::Disabled,
97+
pgo_gen: SwitchWithOptPath::Disabled,
9898
pgo_use: None,
9999

100100
emit_no_opt_bc: false,

0 commit comments

Comments
 (0)