forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.rs
223 lines (190 loc) · 7.24 KB
/
training.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use crate::environment::Environment;
use crate::exec::{cmd, CmdBuilder};
use crate::utils::io::{count_files, delete_directory};
use crate::utils::with_log_group;
use anyhow::Context;
use camino::{Utf8Path, Utf8PathBuf};
use humansize::BINARY;
const LLVM_PGO_CRATES: &[&str] = &[
"syn-1.0.89",
"cargo-0.60.0",
"serde-1.0.136",
"ripgrep-13.0.0",
"regex-1.5.5",
"clap-3.1.6",
"hyper-0.14.18",
];
const RUSTC_PGO_CRATES: &[&str] = &[
"externs",
"ctfe-stress-5",
"cargo-0.60.0",
"token-stream-stress",
"match-stress",
"tuple-stress",
"diesel-1.4.8",
"bitmaps-3.1.0",
];
const LLVM_BOLT_CRATES: &[&str] = LLVM_PGO_CRATES;
fn init_compiler_benchmarks(
env: &dyn Environment,
profiles: &[&str],
scenarios: &[&str],
crates: &[&str],
) -> CmdBuilder {
// Run rustc-perf benchmarks
// Benchmark using profile_local with eprintln, which essentially just means
// don't actually benchmark -- just make sure we run rustc a bunch of times.
cmd(&[
env.cargo_stage_0().as_str(),
"run",
"-p",
"collector",
"--bin",
"collector",
"--",
"profile_local",
"eprintln",
env.rustc_stage_2().as_str(),
"--id",
"Test",
"--cargo",
env.cargo_stage_0().as_str(),
"--profiles",
profiles.join(",").as_str(),
"--scenarios",
scenarios.join(",").as_str(),
"--include",
crates.join(",").as_str(),
])
.env("RUST_LOG", "collector=debug")
.env("RUSTC", env.rustc_stage_0().as_str())
.env("RUSTC_BOOTSTRAP", "1")
.workdir(&env.rustc_perf_dir())
}
/// Describes which `llvm-profdata` binary should be used for merging PGO profiles.
enum LlvmProfdata {
/// Use llvm-profdata from the host toolchain (i.e. from LLVM provided externally).
Host,
/// Use llvm-profdata from the target toolchain (i.e. from LLVM built from `src/llvm-project`).
Target,
}
fn merge_llvm_profiles(
env: &dyn Environment,
merged_path: &Utf8Path,
profile_dir: &Utf8Path,
profdata: LlvmProfdata,
) -> anyhow::Result<()> {
let llvm_profdata = match profdata {
LlvmProfdata::Host => env.downloaded_llvm_dir().join("bin/llvm-profdata"),
LlvmProfdata::Target => env
.build_artifacts()
.join("llvm")
.join("build")
.join(format!("bin/llvm-profdata{}", env.executable_extension())),
};
cmd(&[llvm_profdata.as_str(), "merge", "-o", merged_path.as_str(), profile_dir.as_str()])
.run()
.context("Cannot merge LLVM profiles")?;
Ok(())
}
fn log_profile_stats(
name: &str,
merged_profile: &Utf8Path,
profile_root: &Utf8Path,
) -> anyhow::Result<()> {
log::info!("{name} PGO statistics");
log::info!(
"{merged_profile}: {}",
humansize::format_size(std::fs::metadata(merged_profile.as_std_path())?.len(), BINARY)
);
log::info!(
"{profile_root}: {}",
humansize::format_size(fs_extra::dir::get_size(profile_root.as_std_path())?, BINARY)
);
log::info!("Profile file count: {}", count_files(profile_root)?);
Ok(())
}
pub struct LlvmPGOProfile(pub Utf8PathBuf);
pub fn gather_llvm_profiles(
env: &dyn Environment,
profile_root: &Utf8Path,
) -> anyhow::Result<LlvmPGOProfile> {
log::info!("Running benchmarks with PGO instrumented LLVM");
with_log_group("Running benchmarks", || {
init_compiler_benchmarks(env, &["Debug", "Opt"], &["Full"], LLVM_PGO_CRATES)
.run()
.context("Cannot gather LLVM PGO profiles")
})?;
let merged_profile = env.opt_artifacts().join("llvm-pgo.profdata");
log::info!("Merging LLVM PGO profiles to {merged_profile}");
merge_llvm_profiles(env, &merged_profile, profile_root, LlvmProfdata::Host)?;
log_profile_stats("LLVM", &merged_profile, profile_root)?;
// We don't need the individual .profraw files now that they have been merged
// into a final .profdata
delete_directory(profile_root)?;
Ok(LlvmPGOProfile(merged_profile))
}
pub struct RustcPGOProfile(pub Utf8PathBuf);
pub fn gather_rustc_profiles(
env: &dyn Environment,
profile_root: &Utf8Path,
) -> anyhow::Result<RustcPGOProfile> {
log::info!("Running benchmarks with PGO instrumented rustc");
// The profile data is written into a single filepath that is being repeatedly merged when each
// rustc invocation ends. Empirically, this can result in some profiling data being lost. That's
// why we override the profile path to include the PID. This will produce many more profiling
// files, but the resulting profile will produce a slightly faster rustc binary.
let profile_template = profile_root.join("default_%m_%p.profraw");
// Here we're profiling the `rustc` frontend, so we also include `Check`.
// The benchmark set includes various stress tests that put the frontend under pressure.
with_log_group("Running benchmarks", || {
init_compiler_benchmarks(env, &["Check", "Debug", "Opt"], &["All"], RUSTC_PGO_CRATES)
.env("LLVM_PROFILE_FILE", profile_template.as_str())
.run()
.context("Cannot gather rustc PGO profiles")
})?;
let merged_profile = env.opt_artifacts().join("rustc-pgo.profdata");
log::info!("Merging Rustc PGO profiles to {merged_profile}");
merge_llvm_profiles(env, &merged_profile, profile_root, LlvmProfdata::Target)?;
log_profile_stats("Rustc", &merged_profile, profile_root)?;
// We don't need the individual .profraw files now that they have been merged
// into a final .profdata
delete_directory(profile_root)?;
Ok(RustcPGOProfile(merged_profile))
}
pub struct LlvmBoltProfile(pub Utf8PathBuf);
pub fn gather_llvm_bolt_profiles(env: &dyn Environment) -> anyhow::Result<LlvmBoltProfile> {
log::info!("Running benchmarks with BOLT instrumented LLVM");
with_log_group("Running benchmarks", || {
init_compiler_benchmarks(env, &["Check", "Debug", "Opt"], &["Full"], LLVM_BOLT_CRATES)
.run()
.context("Cannot gather LLVM BOLT profiles")
})?;
let merged_profile = env.opt_artifacts().join("llvm-bolt.profdata");
let profile_root = Utf8PathBuf::from("/tmp/prof.fdata");
log::info!("Merging LLVM BOLT profiles to {merged_profile}");
let profiles: Vec<_> =
glob::glob(&format!("{profile_root}*"))?.into_iter().collect::<Result<Vec<_>, _>>()?;
let mut merge_args = vec!["merge-fdata"];
merge_args.extend(profiles.iter().map(|p| p.to_str().unwrap()));
with_log_group("Merging BOLT profiles", || {
cmd(&merge_args)
.redirect_output(merged_profile.clone())
.run()
.context("Cannot merge BOLT profiles")
})?;
log::info!("LLVM BOLT statistics");
log::info!(
"{merged_profile}: {}",
humansize::format_size(std::fs::metadata(merged_profile.as_std_path())?.len(), BINARY)
);
let size = profiles
.iter()
.map(|p| std::fs::metadata(p).map(|metadata| metadata.len()))
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.sum::<u64>();
log::info!("{profile_root}: {}", humansize::format_size(size, BINARY));
log::info!("Profile file count: {}", profiles.len());
Ok(LlvmBoltProfile(merged_profile))
}