Skip to content

Commit 037cf46

Browse files
committed
Add section timing data to JSON --timings output
1 parent dbd298a commit 037cf46

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ use cargo_util_schemas::manifest::TomlDebugInfo;
105105
use cargo_util_schemas::manifest::TomlTrimPaths;
106106
use cargo_util_schemas::manifest::TomlTrimPathsValue;
107107
use rustfix::diagnostics::Applicability;
108+
pub(crate) use timings::CompilationSection;
108109

109110
const RUSTDOC_CRATE_VERSION_FLAG: &str = "--crate-version";
110111

src/cargo/core/compiler/timings.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::util::style;
1212
use crate::util::{CargoResult, GlobalContext};
1313
use anyhow::Context as _;
1414
use cargo_util::paths;
15+
use indexmap::IndexMap;
1516
use std::collections::HashMap;
1617
use std::io::{BufWriter, Write};
1718
use std::thread::available_parallelism;
@@ -64,8 +65,9 @@ pub struct Timings<'gctx> {
6465
cpu_usage: Vec<(f64, f64)>,
6566
}
6667

67-
/// Section of compilation.
68-
struct TimingSection {
68+
/// Section of compilation (e.g. frontend, backend, linking).
69+
#[derive(Copy, Clone, serde::Serialize)]
70+
pub struct CompilationSection {
6971
/// Start of the section, as an offset in seconds from `UnitTime::start`.
7072
start: f64,
7173
/// End of the section, as an offset in seconds from `UnitTime::start`.
@@ -89,7 +91,10 @@ struct UnitTime {
8991
/// Same as `unlocked_units`, but unlocked by rmeta.
9092
unlocked_rmeta_units: Vec<Unit>,
9193
/// Individual compilation section durations, gathered from `--json=timings`.
92-
sections: HashMap<String, TimingSection>,
94+
///
95+
/// IndexMap is used to keep original insertion order, we want to be able to tell which
96+
/// sections were started in which order.
97+
sections: IndexMap<String, CompilationSection>,
9398
}
9499

95100
/// Periodic concurrency tracking information.
@@ -238,6 +243,7 @@ impl<'gctx> Timings<'gctx> {
238243
mode: unit_time.unit.mode,
239244
duration: unit_time.duration,
240245
rmeta_time: unit_time.rmeta_time,
246+
sections: unit_time.sections.clone().into_iter().collect(),
241247
}
242248
.to_json_string();
243249
crate::drop_println!(self.gctx, "{}", msg);
@@ -616,20 +622,20 @@ impl UnitTime {
616622
.sections
617623
.insert(
618624
name.to_string(),
619-
TimingSection {
625+
CompilationSection {
620626
start: now - self.start,
621627
end: None,
622628
},
623629
)
624630
.is_some()
625631
{
626-
warn!("Compilation section {name} started more than once");
632+
warn!("compilation section {name} started more than once");
627633
}
628634
}
629635

630636
fn end_section(&mut self, name: &str, now: f64) {
631637
let Some(section) = self.sections.get_mut(name) else {
632-
warn!("Compilation section {name} ended, but it has no start recorded");
638+
warn!("compilation section {name} ended, but it has no start recorded");
633639
return;
634640
};
635641
section.end = Some(now - self.start);

src/cargo/util/machine_message.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::ser;
66
use serde_json::{json, value::RawValue};
77

88
use crate::core::Target;
9-
use crate::core::compiler::CompileMode;
9+
use crate::core::compiler::{CompilationSection, CompileMode};
1010

1111
pub trait Message: ser::Serialize {
1212
fn reason(&self) -> &str;
@@ -95,6 +95,8 @@ pub struct TimingInfo<'a> {
9595
pub duration: f64,
9696
#[serde(skip_serializing_if = "Option::is_none")]
9797
pub rmeta_time: Option<f64>,
98+
#[serde(skip_serializing_if = "Vec::is_empty")]
99+
pub sections: Vec<(String, CompilationSection)>,
98100
}
99101

100102
impl<'a> Message for TimingInfo<'a> {

0 commit comments

Comments
 (0)