Skip to content

Commit 608fd7e

Browse files
authored
fix(timing): more self-contained timing/log data (#16378)
### What does this PR try to resolve? Factored out of <#16377>. This makes data more self-contained. ### How to test and review this PR?
2 parents 9de93cd + 9627f4d commit 608fd7e

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

src/cargo/core/compiler/timings/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,14 @@ impl<'gctx> Timings<'gctx> {
437437
.iter()
438438
.map(|kind| build_runner.bcx.target_data.short_name(kind))
439439
.collect::<Vec<_>>();
440+
let num_cpus = std::thread::available_parallelism()
441+
.ok()
442+
.map(|x| x.get() as u64);
440443

441444
let unit_data = report::to_unit_data(&self.unit_times, &self.unit_to_index);
442445
let concurrency = report::compute_concurrency(&unit_data);
443446

444447
let ctx = report::RenderContext {
445-
start: self.start,
446448
start_str: &self.start_str,
447449
root_units: &self.root_targets,
448450
profile: &self.profile,
@@ -455,6 +457,7 @@ impl<'gctx> Timings<'gctx> {
455457
host: &build_runner.bcx.rustc().host,
456458
requested_targets,
457459
jobs: build_runner.bcx.jobs(),
460+
num_cpus,
458461
error,
459462
};
460463
report::write_html(ctx, &mut f)?;

src/cargo/core/compiler/timings/report.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::borrow::Cow;
44
use std::collections::HashMap;
55
use std::collections::HashSet;
66
use std::io::Write;
7-
use std::time::Instant;
87

98
use itertools::Itertools as _;
109

@@ -96,8 +95,6 @@ pub struct Concurrency {
9695
}
9796

9897
pub struct RenderContext<'a> {
99-
/// When Cargo started.
100-
pub start: Instant,
10198
/// A rendered string of when compilation started.
10299
pub start_str: &'a str,
103100
/// A summary of the root units.
@@ -127,13 +124,16 @@ pub struct RenderContext<'a> {
127124
pub requested_targets: &'a [&'a str],
128125
/// The number of jobs specified for this build.
129126
pub jobs: u32,
127+
/// Available parallelism of the compilation environment.
128+
pub num_cpus: Option<u64>,
130129
/// Fatal error during the build.
131130
pub error: &'a Option<anyhow::Error>,
132131
}
133132

134133
/// Writes an HTML report.
135134
pub(super) fn write_html(ctx: RenderContext<'_>, f: &mut impl Write) -> CargoResult<()> {
136-
let duration = ctx.start.elapsed().as_secs_f64();
135+
// The last concurrency record should equal to the last unit finished time.
136+
let duration = ctx.concurrency.last().map(|c| c.t).unwrap_or(0.0);
137137
let roots: Vec<&str> = ctx
138138
.root_units
139139
.iter()
@@ -186,10 +186,11 @@ fn write_summary_table(
186186
};
187187
let total_time = format!("{:.1}s{}", duration, time_human);
188188

189-
let max_concurrency = ctx.concurrency.iter().map(|c| c.active).max().unwrap();
190-
let num_cpus = std::thread::available_parallelism()
191-
.map(|x| x.get().to_string())
192-
.unwrap_or_else(|_| "n/a".into());
189+
let max_concurrency = ctx.concurrency.iter().map(|c| c.active).max().unwrap_or(0);
190+
let num_cpus = ctx
191+
.num_cpus
192+
.map(|x| x.to_string())
193+
.unwrap_or_else(|| "n/a".into());
193194

194195
let requested_targets = ctx.requested_targets.join(", ");
195196

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,14 @@ pub fn compile_ws<'a>(
162162

163163
if let Some(ref logger) = logger {
164164
let rustc = ws.gctx().load_global_rustc(Some(ws))?;
165+
let num_cpus = std::thread::available_parallelism()
166+
.ok()
167+
.map(|x| x.get() as u64);
165168
logger.log(LogMessage::BuildStarted {
166169
cwd: ws.gctx().cwd().to_path_buf(),
167170
host: rustc.host.to_string(),
168171
jobs: options.build_config.jobs,
172+
num_cpus,
169173
profile: options.build_config.requested_profile.to_string(),
170174
rustc_version: rustc.version.to_string(),
171175
rustc_version_verbose: rustc.verbose_version.clone(),

src/cargo/util/log_message.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub enum LogMessage {
2424
host: String,
2525
/// Number of parallel jobs.
2626
jobs: u32,
27+
/// Available parallelism of the compilation environment.
28+
num_cpus: Option<u64>,
2729
/// Build profile name (e.g., "dev", "release").
2830
profile: String,
2931
/// The rustc version (`1.23.4-beta.2`).

tests/testsuite/build_analysis.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ fn log_msg_build_started() {
8080
"cwd": "[ROOT]/foo",
8181
"host": "[HOST_TARGET]",
8282
"jobs": "{...}",
83+
"num_cpus": "{...}",
8384
"profile": "dev",
8485
"reason": "build-started",
8586
"run_id": "[..]T[..]Z-[..]",

0 commit comments

Comments
 (0)