Skip to content

Commit 309aaa4

Browse files
authored
Merge pull request #1227 from nnethercote/some-cleanups
Some cleanups
2 parents 42c1f98 + dd4a120 commit 309aaa4

File tree

1 file changed

+54
-73
lines changed

1 file changed

+54
-73
lines changed

collector/src/rustc-fake.rs

Lines changed: 54 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ fn determinism_env(cmd: &mut Command) {
1818
cmd.env("RUSTC_FORCE_RUSTC_VERSION", "rustc-perf");
1919
}
2020

21+
fn run_with_determinism_env(mut cmd: Command) {
22+
determinism_env(&mut cmd);
23+
let status = cmd.status().expect("failed to spawn");
24+
assert!(
25+
status.success(),
26+
"command did not complete successfully: {:?}",
27+
cmd
28+
);
29+
}
30+
2131
fn main() {
2232
let mut args_os = env::args_os();
2333
let name = args_os.next().unwrap().into_string().unwrap();
@@ -64,7 +74,6 @@ fn main() {
6474
match wrapper {
6575
"PerfStat" | "PerfStatSelfProfile" => {
6676
let mut cmd = Command::new("perf");
67-
determinism_env(&mut cmd);
6877
let has_perf = cmd.output().is_ok();
6978
assert!(has_perf);
7079
cmd.arg("stat")
@@ -94,9 +103,8 @@ fn main() {
94103
}
95104

96105
let start = Instant::now();
97-
let status = cmd.status().expect("failed to spawn");
106+
run_with_determinism_env(cmd);
98107
let dur = start.elapsed();
99-
assert!(status.success());
100108
print_memory();
101109
print_time(dur);
102110
if wrapper == "PerfStatSelfProfile" {
@@ -147,7 +155,6 @@ fn main() {
147155
assert!(status.success(), "tracelog did not complete successfully");
148156

149157
let mut tool = Command::new(tool);
150-
determinism_env(&mut tool);
151158
tool.args(&args);
152159

153160
let prof_out_dir = std::env::current_dir().unwrap().join("self-profile-output");
@@ -161,18 +168,14 @@ fn main() {
161168
}
162169

163170
let start = Instant::now();
164-
let status = tool.status().expect("tool failed to start");
171+
run_with_determinism_env(tool);
165172
let dur = start.elapsed();
166-
assert!(status.success(), "tool did not run successfully");
167173
println!("!wall-time:{}.{:09}", dur.as_secs(), dur.subsec_nanos());
168174

169175
let xperf = |args: &[&str]| {
170176
let mut cmd = Command::new(&xperf);
171177
cmd.args(args);
172-
assert!(
173-
cmd.status().expect("failed to spawn xperf").success(),
174-
"xperf did not complete successfully"
175-
);
178+
assert!(cmd.status().expect("failed to spawn xperf").success());
176179
};
177180

178181
xperf(&["-stop", "rustc-perf-counters"]);
@@ -189,27 +192,26 @@ fn main() {
189192

190193
"SelfProfile" => {
191194
let mut cmd = Command::new(&tool);
192-
determinism_env(&mut cmd);
193-
cmd.arg("-Zself-profile-events=all");
194-
cmd.arg("-Zself-profile=Zsp").args(&args);
195+
cmd.arg("-Zself-profile-events=all")
196+
.arg("-Zself-profile=Zsp")
197+
.args(&args);
195198

196-
assert!(cmd.status().expect("failed to spawn").success());
199+
run_with_determinism_env(cmd);
197200
}
198201

199202
"TimePasses" => {
200-
args.insert(0, "-Ztime-passes".into());
201-
202203
let mut cmd = Command::new(&tool);
203-
determinism_env(&mut cmd);
204-
cmd.args(args).stderr(std::process::Stdio::from(
205-
std::fs::File::create("Ztp").unwrap(),
206-
));
207-
assert!(cmd.status().expect("failed to spawn").success());
204+
cmd.arg("-Ztime-passes")
205+
.args(args)
206+
.stderr(std::process::Stdio::from(
207+
std::fs::File::create("Ztp").unwrap(),
208+
));
209+
210+
run_with_determinism_env(cmd);
208211
}
209212

210213
"PerfRecord" => {
211214
let mut cmd = Command::new("perf");
212-
determinism_env(&mut cmd);
213215
let has_perf = cmd.output().is_ok();
214216
assert!(has_perf);
215217
cmd.arg("record")
@@ -220,23 +222,21 @@ fn main() {
220222
.arg(&tool)
221223
.args(&args);
222224

223-
assert!(cmd.status().expect("failed to spawn").success());
225+
run_with_determinism_env(cmd);
224226
}
225227

226228
"Oprofile" => {
227229
let mut cmd = Command::new("operf");
228-
determinism_env(&mut cmd);
229230
let has_oprofile = cmd.output().is_ok();
230231
assert!(has_oprofile);
231232
// Other possibly useful args: --callgraph, --separate-thread
232233
cmd.arg("operf").arg(&tool).args(&args);
233234

234-
assert!(cmd.status().expect("failed to spawn").success());
235+
run_with_determinism_env(cmd);
235236
}
236237

237238
"Cachegrind" => {
238239
let mut cmd = Command::new("valgrind");
239-
determinism_env(&mut cmd);
240240
let has_valgrind = cmd.output().is_ok();
241241
assert!(has_valgrind);
242242

@@ -256,12 +256,11 @@ fn main() {
256256
.arg(&tool)
257257
.args(&args);
258258

259-
assert!(cmd.status().expect("failed to spawn").success());
259+
run_with_determinism_env(cmd);
260260
}
261261

262262
"Callgrind" => {
263263
let mut cmd = Command::new("valgrind");
264-
determinism_env(&mut cmd);
265264
let has_valgrind = cmd.output().is_ok();
266265
assert!(has_valgrind);
267266

@@ -274,12 +273,11 @@ fn main() {
274273
.arg(&tool)
275274
.args(&args);
276275

277-
assert!(cmd.status().expect("failed to spawn").success());
276+
run_with_determinism_env(cmd);
278277
}
279278

280279
"Dhat" => {
281280
let mut cmd = Command::new("valgrind");
282-
determinism_env(&mut cmd);
283281
let has_valgrind = cmd.output().is_ok();
284282
assert!(has_valgrind);
285283
cmd.arg("--tool=dhat")
@@ -288,12 +286,11 @@ fn main() {
288286
.arg(&tool)
289287
.args(&args);
290288

291-
assert!(cmd.status().expect("failed to spawn").success());
289+
run_with_determinism_env(cmd);
292290
}
293291

294292
"DhatCopy" => {
295293
let mut cmd = Command::new("valgrind");
296-
determinism_env(&mut cmd);
297294
let has_valgrind = cmd.output().is_ok();
298295
assert!(has_valgrind);
299296
cmd.arg("--tool=dhat")
@@ -303,12 +300,11 @@ fn main() {
303300
.arg(&tool)
304301
.args(&args);
305302

306-
assert!(cmd.status().expect("failed to spawn").success());
303+
run_with_determinism_env(cmd);
307304
}
308305

309306
"Massif" => {
310307
let mut cmd = Command::new("valgrind");
311-
determinism_env(&mut cmd);
312308
let has_valgrind = cmd.output().is_ok();
313309
assert!(has_valgrind);
314310
cmd.arg("--tool=massif")
@@ -320,14 +316,16 @@ fn main() {
320316
.arg(&tool)
321317
.args(&args);
322318

323-
assert!(cmd.status().expect("failed to spawn").success());
319+
run_with_determinism_env(cmd);
324320
}
325321

326322
"Eprintln" => {
327-
let mut cmd = bash_command(tool, args, "2> eprintln");
328-
determinism_env(&mut cmd);
323+
let mut cmd = Command::new(tool);
324+
cmd.args(args).stderr(std::process::Stdio::from(
325+
std::fs::File::create("eprintln").unwrap(),
326+
));
329327

330-
assert!(cmd.status().expect("failed to spawn").success());
328+
run_with_determinism_env(cmd);
331329
}
332330

333331
"LlvmLines" => {
@@ -339,40 +337,41 @@ fn main() {
339337
// `rustc` (which this file wraps) doesn't produce the output,
340338
// this file can't redirect that output.
341339
let mut cmd = Command::new(&tool);
342-
determinism_env(&mut cmd);
343340
cmd.args(&args);
344341

345-
assert!(cmd.status().expect("failed to spawn").success());
342+
run_with_determinism_env(cmd);
346343
}
347344

348345
"LlvmIr" => {
349-
args.push("--emit=llvm-ir=./llvm-ir".into());
350-
args.push("-Cno-prepopulate-passes".into());
351-
args.push("-Cpasses=name-anon-globals".into());
352346
let mut cmd = Command::new(tool);
353-
cmd.args(args);
354-
determinism_env(&mut cmd);
355-
assert!(cmd.status().expect("failed to spawn").success());
347+
cmd.arg("--emit=llvm-ir=./llvm-ir")
348+
.arg("-Cno-prepopulate-passes")
349+
.arg("-Cpasses=name-anon-globals")
350+
.args(args);
351+
352+
run_with_determinism_env(cmd);
356353
}
357354

358355
"MonoItems" => {
359356
// Lazy item collection is the default (i.e., without this
360357
// option)
361-
args.push("-Zprint-mono-items=lazy".into());
362-
let mut cmd = bash_command(tool, args, "1> mono-items");
363-
determinism_env(&mut cmd);
358+
let mut cmd = Command::new(tool);
359+
cmd.arg("-Zprint-mono-items=lazy")
360+
.args(args)
361+
.stdout(std::process::Stdio::from(
362+
std::fs::File::create("mono-items").unwrap(),
363+
));
364364

365-
assert!(cmd.status().expect("failed to spawn").success());
365+
run_with_determinism_env(cmd);
366366
}
367367

368368
"DepGraph" => {
369-
args.push("-Zdump-dep-graph".into());
370-
args.push("-Zquery-dep-graph".into());
371369
let mut cmd = Command::new(tool);
372-
determinism_env(&mut cmd);
373-
cmd.args(&args);
370+
cmd.arg("-Zdump-dep-graph")
371+
.arg("-Zquery-dep-graph")
372+
.args(&args);
374373

375-
assert!(cmd.status().expect("failed to spawn").success());
374+
run_with_determinism_env(cmd);
376375
}
377376

378377
_ => {
@@ -459,24 +458,6 @@ fn process_self_profile_output(prof_out_dir: PathBuf, args: &[OsString]) {
459458
}
460459
}
461460

462-
/// Run a command via bash, in order to redirect its output to a file.
463-
/// `redirect` should be something like "> out" or "2> out".
464-
fn bash_command(tool: OsString, args: Vec<OsString>, redirect: &str) -> Command {
465-
let mut bash_cmd = String::new();
466-
bash_cmd.push_str(&format!("{} ", tool.to_str().unwrap()));
467-
for arg in args {
468-
// Args with double quotes (e.g. `--cfg feature="foo"`)
469-
// will be munged by bash if we don't protect them. So we
470-
// wrap every arg in single quotes.
471-
bash_cmd.push_str(&format!("'{}' ", arg.to_str().unwrap()));
472-
}
473-
bash_cmd.push_str(redirect);
474-
475-
let mut cmd = Command::new("bash");
476-
cmd.args(&["-c", &bash_cmd]);
477-
cmd
478-
}
479-
480461
#[cfg(windows)]
481462
fn exec(cmd: &mut Command) {
482463
let cmd_d = format!("{:?}", cmd);

0 commit comments

Comments
 (0)