Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/cargo/core/compiler/timings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,12 @@ impl<'gctx> Timings<'gctx> {
.lines()
.next()
.expect("rustc version");
let requested_targets = &build_runner
let requested_targets = build_runner
.bcx
.build_config
.requested_kinds
.iter()
.map(|kind| build_runner.bcx.target_data.short_name(kind))
.map(|kind| build_runner.bcx.target_data.short_name(kind).to_owned())
.collect::<Vec<_>>();
let num_cpus = std::thread::available_parallelism()
.ok()
Expand All @@ -432,7 +432,7 @@ impl<'gctx> Timings<'gctx> {

let ctx = report::RenderContext {
start_str: self.start_str.clone(),
root_units: &self.root_targets,
root_units: self.root_targets.clone(),
profile: self.profile.clone(),
total_fresh: self.total_fresh,
total_dirty: self.total_dirty,
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/timings/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub struct RenderContext<'a> {
/// A summary of the root units.
///
/// Tuples of `(package_description, target_descriptions)`.
pub root_units: &'a [(String, Vec<String>)],
pub root_units: Vec<(String, Vec<String>)>,
/// The build profile.
pub profile: String,
/// Total number of fresh units.
Expand All @@ -115,7 +115,7 @@ pub struct RenderContext<'a> {
/// The host triple (arch-platform-OS).
pub host: String,
/// The requested target platforms of compilation for this build.
pub requested_targets: &'a [&'a str],
pub requested_targets: Vec<String>,
/// The number of jobs specified for this build.
pub jobs: u32,
/// Available parallelism of the compilation environment.
Expand Down
21 changes: 16 additions & 5 deletions src/cargo/ops/cargo_compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,9 @@ pub fn create_bcx<'a, 'gctx>(
// Rebuild the unit graph, replacing the explicit host targets with
// CompileKind::Host, removing `artifact_target_for_features` and merging any dependencies
// shared with build and artifact dependencies.
(root_units, scrape_units, unit_graph) = rebuild_unit_graph_shared(
//
// NOTE: after this point, all units and the unit graph must be immutable.
let (root_units, scrape_units, unit_graph) = rebuild_unit_graph_shared(
interner,
unit_graph,
&root_units,
Expand All @@ -521,21 +523,30 @@ pub fn create_bcx<'a, 'gctx>(
build_config.compile_time_deps_only,
);

// unit_graph must be immutable after this point.
let unit_graph = unit_graph;
let units: Vec<_> = unit_graph.keys().sorted().collect();
let unit_to_index: HashMap<_, _> = units
.iter()
.enumerate()
.map(|(i, &unit)| (unit.clone(), i as u64))
.collect();
if let Some(logger) = logger {
for (i, unit) in units.into_iter().enumerate() {
let root_unit_indexes: HashSet<_> =
root_units.iter().map(|unit| unit_to_index[&unit]).collect();

for (index, unit) in units.into_iter().enumerate() {
let index = index as u64;
logger.log(LogMessage::UnitRegistered {
package_id: unit.pkg.package_id().to_spec(),
target: (&unit.target).into(),
mode: unit.mode,
index: i as u64,
platform: target_data.short_name(&unit.kind).to_owned(),
index,
features: unit
.features
.iter()
.map(|s| s.as_str().to_owned())
.collect(),
requested: root_unit_indexes.contains(&index),
});
}
let elapsed = ws.gctx().creation_time().elapsed().as_secs_f64();
Expand Down
74 changes: 64 additions & 10 deletions src/cargo/ops/cargo_report/timings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The `cargo report timings` command.

use std::collections::HashMap;
use std::collections::HashSet;
use std::ffi::OsStr;
use std::fs::File;
use std::io::BufReader;
Expand Down Expand Up @@ -29,7 +29,9 @@ use crate::core::compiler::timings::report::round_to_centisecond;
use crate::core::compiler::timings::report::write_html;
use crate::util::BuildLogger;
use crate::util::important_paths::find_root_manifest_for_wd;
use crate::util::log_message::FingerprintStatus;
use crate::util::log_message::LogMessage;
use crate::util::log_message::Target;
use crate::util::logger::RunId;
use crate::util::style;

Expand All @@ -41,6 +43,7 @@ pub struct ReportTimingsOptions<'gctx> {

/// Collects sections data for later post-processing through [`aggregate_sections`].
struct UnitEntry {
target: Target,
data: UnitData,
sections: IndexMap<String, CompilationSection>,
rmeta_time: Option<f64>,
Expand Down Expand Up @@ -179,7 +182,9 @@ fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'sta
};
let mut units: IndexMap<_, UnitEntry> = IndexMap::new();

let mut unit_by_index: HashMap<u64, _> = HashMap::new();
let mut platform_targets = HashSet::new();

let mut requested_units = HashSet::new();

for (log_index, result) in serde_json::Deserializer::from_reader(reader)
.into_iter::<LogMessage>()
Expand All @@ -201,10 +206,15 @@ fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'sta
num_cpus,
profile,
rustc_version,
rustc_version_verbose: _,
rustc_version_verbose,
target_dir: _,
workspace_root: _,
} => {
let rustc_version = rustc_version_verbose
.lines()
.next()
.map(ToOwned::to_owned)
.unwrap_or(rustc_version);
ctx.host = host;
ctx.jobs = jobs;
ctx.num_cpus = num_cpus;
Expand All @@ -215,12 +225,15 @@ fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'sta
package_id,
target,
mode,
platform,
index,
features,
requested,
} => {
unit_by_index.insert(index, (package_id, target, mode));
}
LogMessage::UnitStarted { index, elapsed } => {
let (package_id, target, mode) = unit_by_index.get(&index).unwrap();
if requested {
requested_units.insert(index);
}
platform_targets.insert(platform);

let version = package_id
.version()
Expand All @@ -230,7 +243,7 @@ fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'sta
// This is pretty similar to how the current `core::compiler::timings`
// renders `core::manifest::Target`. However, our target is
// a simplified type so we cannot reuse the same logic here.
let mut target_str = if target.kind == "lib" && mode == &CompileMode::Build {
let mut target_str = if target.kind == "lib" && mode == CompileMode::Build {
// Special case for brevity, since most dependencies hit this path.
"".to_string()
} else if target.kind == "build-script" {
Expand Down Expand Up @@ -262,8 +275,8 @@ fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'sta
version,
mode: mode_str.to_owned(),
target: target_str,
features: Vec::new(),
start: elapsed,
features,
start: 0.0,
duration: 0.0,
unblocked_units: Vec::new(),
unblocked_rmeta_units: Vec::new(),
Expand All @@ -273,12 +286,26 @@ fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'sta
units.insert(
index,
UnitEntry {
target,
data,
sections: IndexMap::new(),
rmeta_time: None,
},
);
}
LogMessage::UnitFingerprint { status, .. } => match status {
FingerprintStatus::New => ctx.total_dirty += 1,
FingerprintStatus::Dirty => ctx.total_dirty += 1,
FingerprintStatus::Fresh => ctx.total_fresh += 1,
},
LogMessage::UnitStarted { index, elapsed } => {
units
.entry(index)
.and_modify(|unit| unit.data.start = elapsed)
.or_insert_with(|| {
unreachable!("unit {index} must have been registered first")
});
}
LogMessage::UnitRmetaFinished {
index,
elapsed,
Expand Down Expand Up @@ -364,10 +391,36 @@ fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'sta
}
}

ctx.root_units = {
let mut root_map: IndexMap<_, Vec<_>> = IndexMap::new();
for index in requested_units {
let unit = &units[&index];
// Pretty much like `core::Target::description_named`
let target_desc = if unit.target.kind == "lib" {
"lib".to_owned()
} else if unit.target.kind == "build-script" {
"build script".to_owned()
} else {
format!(r#" {} "{}""#, unit.target.name, unit.target.kind)
};
root_map.entry(index).or_default().push(target_desc);
}
root_map
.into_iter()
.sorted_by_key(|(i, _)| *i)
.map(|(index, targets)| {
let unit = &units[&index];
let pkg_desc = format!("{} {}", unit.data.name, unit.data.version);
(pkg_desc, targets)
})
.collect()
};

let unit_data: Vec<_> = units
.into_values()
.map(
|UnitEntry {
target: _,
mut data,
sections,
rmeta_time,
Expand All @@ -388,6 +441,7 @@ fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'sta

ctx.unit_data = unit_data;
ctx.concurrency = compute_concurrency(&ctx.unit_data);
ctx.requested_targets = platform_targets.into_iter().sorted_unstable().collect();

Ok(ctx)
}
15 changes: 15 additions & 0 deletions src/cargo/util/log_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,23 @@ pub enum LogMessage {
target: Target,
/// The compilation action this unit is for (check, build, test, etc.).
mode: CompileMode,
/// The target platform this unit builds for.
///
/// It is either a [target triple] the compiler accepts,
/// or a file name with the `json` extension for a [custom target].
///
/// [target triple]: https://doc.rust-lang.org/nightly/rustc/platform-support.html
/// [custom target]: https://doc.rust-lang.org/nightly/rustc/targets/custom.html
platform: String,
/// Unit index for compact reference in subsequent events.
index: u64,
/// Enabled features.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
features: Vec<String>,
/// Whether this is requested to build by user directly,
/// like via the `-p` flag or the default workspace members.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
requested: bool,
},
/// Emitted when a compilation unit starts.
UnitStarted {
Expand Down
6 changes: 6 additions & 0 deletions tests/testsuite/build_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ fn log_msg_unit_graph() {
"index": 0,
"mode": "check",
"package_id": "path+[ROOTURL]/foo/bar#0.0.0",
"platform": "[HOST_TARGET]",
"reason": "unit-registered",
"run_id": "[..]T[..]Z-[..]",
"target": {
Expand All @@ -595,6 +596,7 @@ fn log_msg_unit_graph() {
"index": 1,
"mode": "doc",
"package_id": "path+[ROOTURL]/foo/bar#0.0.0",
"platform": "[HOST_TARGET]",
"reason": "unit-registered",
"run_id": "[..]T[..]Z-[..]",
"target": {
Expand All @@ -607,7 +609,9 @@ fn log_msg_unit_graph() {
"index": 2,
"mode": "doc",
"package_id": "path+[ROOTURL]/foo#0.0.0",
"platform": "[HOST_TARGET]",
"reason": "unit-registered",
"requested": true,
"run_id": "[..]T[..]Z-[..]",
"target": {
"kind": "lib",
Expand All @@ -619,6 +623,7 @@ fn log_msg_unit_graph() {
"index": 3,
"mode": "build",
"package_id": "path+[ROOTURL]/foo#0.0.0",
"platform": "[HOST_TARGET]",
"reason": "unit-registered",
"run_id": "[..]T[..]Z-[..]",
"target": {
Expand All @@ -631,6 +636,7 @@ fn log_msg_unit_graph() {
"index": 4,
"mode": "run-custom-build",
"package_id": "path+[ROOTURL]/foo#0.0.0",
"platform": "[HOST_TARGET]",
"reason": "unit-registered",
"run_id": "[..]T[..]Z-[..]",
"target": {
Expand Down
Loading