Skip to content

Commit 03debdc

Browse files
authored
Merge pull request #2185 from Kobzol/rustc-log
Stream bootstrap log when running the rustc benchmark
2 parents f83a503 + 06cbf71 commit 03debdc

File tree

6 files changed

+45
-43
lines changed

6 files changed

+45
-43
lines changed

collector/src/compile/execute/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,6 @@ impl<'a> CargoProcess<'a> {
447447
client.configure(&mut cmd);
448448
}
449449

450-
log::debug!("{:?}", cmd);
451-
452450
let cmd = tokio::process::Command::from(cmd);
453451
let output = async_command_output(cmd).await?;
454452

collector/src/compile/execute/rustc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async fn record(
105105
.context("configuring")?;
106106
assert!(status.success(), "configure successful");
107107

108-
let output = crate::command_output(
108+
let output = crate::command_output_stream(
109109
Command::new("python3")
110110
.arg(
111111
checkout

collector/src/lib.rs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,12 @@ pub fn run_command(cmd: &mut Command) -> anyhow::Result<()> {
169169
Ok(())
170170
}
171171

172-
fn run_command_with_output(cmd: &mut Command) -> anyhow::Result<process::Output> {
172+
/// If `stream_output` is true, stdout/stderr of `cmd` should be streamed to stdout/stderr of the
173+
/// current process, in addition to being captured.
174+
fn run_command_with_output(
175+
cmd: &mut Command,
176+
stream_output: bool,
177+
) -> anyhow::Result<process::Output> {
173178
use anyhow::Context;
174179
use utils::read2;
175180
let mut child = cmd
@@ -178,27 +183,28 @@ fn run_command_with_output(cmd: &mut Command) -> anyhow::Result<process::Output>
178183
.spawn()
179184
.with_context(|| format!("failed to spawn process for cmd: {cmd:?}"))?;
180185

181-
let mut stdout = Vec::new();
182-
let mut stderr = Vec::new();
183-
let mut stdout_writer = std::io::LineWriter::new(std::io::stdout());
184-
let mut stderr_writer = std::io::LineWriter::new(std::io::stderr());
185-
read2::read2(
186+
let mut stdout_writer = std::io::LineWriter::new(std::io::stdout().lock());
187+
let mut stderr_writer = std::io::LineWriter::new(std::io::stderr().lock());
188+
189+
let mut stdout_written = 0;
190+
let mut stderr_written = 0;
191+
let (stdout, stderr) = read2::read2(
186192
child.stdout.take().unwrap(),
187193
child.stderr.take().unwrap(),
188194
&mut |is_stdout, buffer, _is_done| {
189195
// Send output if trace logging is enabled
190-
if log::log_enabled!(target: "raw_cargo_messages", log::Level::Trace) {
196+
if stream_output || log::log_enabled!(target: "raw_cargo_messages", log::Level::Trace) {
191197
use std::io::Write;
192198
if is_stdout {
193-
stdout_writer.write_all(&buffer[stdout.len()..]).unwrap();
199+
stdout_writer.write_all(&buffer[stdout_written..]).unwrap();
194200
} else {
195-
stderr_writer.write_all(&buffer[stderr.len()..]).unwrap();
201+
stderr_writer.write_all(&buffer[stderr_written..]).unwrap();
196202
}
197203
}
198204
if is_stdout {
199-
stdout = buffer.clone();
205+
stdout_written = buffer.len();
200206
} else {
201-
stderr = buffer.clone();
207+
stderr_written = buffer.len();
202208
}
203209
},
204210
)?;
@@ -215,42 +221,47 @@ fn run_command_with_output(cmd: &mut Command) -> anyhow::Result<process::Output>
215221
}
216222

217223
pub fn command_output(cmd: &mut Command) -> anyhow::Result<process::Output> {
218-
let output = run_command_with_output(cmd)?;
224+
let output = run_command_with_output(cmd, false)?;
225+
check_command_output(&output)?;
226+
Ok(output)
227+
}
219228

229+
pub fn command_output_stream(cmd: &mut Command) -> anyhow::Result<process::Output> {
230+
let output = run_command_with_output(cmd, true)?;
231+
check_command_output(&output)?;
232+
Ok(output)
233+
}
234+
235+
fn check_command_output(output: &process::Output) -> anyhow::Result<()> {
220236
if !output.status.success() {
221-
return Err(anyhow::anyhow!(
237+
Err(anyhow::anyhow!(
222238
"expected success, got {}\n\nstderr={}\n\n stdout={}\n",
223239
output.status,
224240
String::from_utf8_lossy(&output.stderr),
225241
String::from_utf8_lossy(&output.stdout)
226-
));
242+
))
243+
} else {
244+
Ok(())
227245
}
228-
229-
Ok(output)
230246
}
231247

232248
pub async fn async_command_output(
233249
mut cmd: tokio::process::Command,
234250
) -> anyhow::Result<process::Output> {
235251
use anyhow::Context;
236252

253+
log::debug!("Executing {:?}", cmd);
254+
237255
let start = Instant::now();
238256
let child = cmd
239257
.stdout(Stdio::piped())
240258
.stderr(Stdio::piped())
241259
.spawn()
242260
.with_context(|| format!("failed to spawn process for cmd: {cmd:?}"))?;
243261
let output = child.wait_with_output().await?;
244-
log::trace!("command {cmd:?} took {} ms", start.elapsed().as_millis());
262+
log::trace!("Command took {} ms", start.elapsed().as_millis());
245263

246-
if !output.status.success() {
247-
return Err(anyhow::anyhow!(
248-
"expected success, got {}\n\nstderr={}\n\n stdout={}\n",
249-
output.status,
250-
String::from_utf8_lossy(&output.stderr),
251-
String::from_utf8_lossy(&output.stdout)
252-
));
253-
}
264+
check_command_output(&output)?;
254265

255266
Ok(output)
256267
}

collector/src/runtime/mod.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub use benchmark::{
1515
use database::{ArtifactIdNumber, CollectionId, Connection};
1616

1717
use crate::utils::git::get_rustc_perf_commit;
18-
use crate::{run_command_with_output, CollectorCtx};
18+
use crate::{command_output, CollectorCtx};
1919

2020
mod benchmark;
2121
mod profile;
@@ -214,15 +214,7 @@ fn execute_runtime_benchmark_binary(
214214
command.args(["--include", &filter.include.join(",")]);
215215
}
216216

217-
let output = run_command_with_output(&mut command)?;
218-
if !output.status.success() {
219-
return Err(anyhow::anyhow!(
220-
"Process finished with exit code {}\n{}",
221-
output.status.code().unwrap_or(-1),
222-
String::from_utf8_lossy(&output.stderr)
223-
));
224-
}
225-
217+
let output = command_output(&mut command)?;
226218
let reader = BufReader::new(Cursor::new(output.stdout));
227219
Ok(reader.lines().map(|line| {
228220
Ok(line.and_then(|line| Ok(serde_json::from_str::<BenchmarkMessage>(&line)?))?)

collector/src/utils/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub fn robocopy(
129129
cmd.arg(arg.as_ref());
130130
}
131131

132-
let output = run_command_with_output(&mut cmd)?;
132+
let output = run_command_with_output(&mut cmd, false)?;
133133

134134
if output.status.code() >= Some(8) {
135135
// robocopy returns 0-7 on success

collector/src/utils/read2.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ mod imp {
3131
use std::os::unix::prelude::*;
3232
use std::process::{ChildStderr, ChildStdout};
3333

34+
/// Returns (stdout, stderr).
3435
pub fn read2(
3536
mut out_pipe: ChildStdout,
3637
mut err_pipe: ChildStderr,
3738
data: &mut dyn FnMut(bool, &mut Vec<u8>, bool),
38-
) -> io::Result<()> {
39+
) -> io::Result<(Vec<u8>, Vec<u8>)> {
3940
unsafe {
4041
libc::fcntl(out_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
4142
libc::fcntl(err_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
@@ -93,7 +94,7 @@ mod imp {
9394
}
9495
data(true, &mut out, out_done);
9596
}
96-
Ok(())
97+
Ok((out, err))
9798
}
9899
}
99100

@@ -120,7 +121,7 @@ mod imp {
120121
out_pipe: ChildStdout,
121122
err_pipe: ChildStderr,
122123
data: &mut dyn FnMut(bool, &mut Vec<u8>, bool),
123-
) -> io::Result<()> {
124+
) -> io::Result<(Vec<u8>, Vec<u8>)> {
124125
let mut out = Vec::new();
125126
let mut err = Vec::new();
126127

@@ -151,7 +152,7 @@ mod imp {
151152
}
152153
}
153154

154-
Ok(())
155+
Ok((out, err))
155156
}
156157
}
157158

0 commit comments

Comments
 (0)