Skip to content

Commit cda2cd1

Browse files
committed
Add section timing data to JSON --timings output
1 parent b113819 commit cda2cd1

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ pub struct Timings<'gctx> {
6464
cpu_usage: Vec<(f64, f64)>,
6565
}
6666

67-
/// Section of compilation.
68-
struct TimingSection {
67+
/// Section of compilation (e.g. frontend, backend, linking).
68+
#[derive(Copy, Clone, serde::Serialize)]
69+
pub struct CompilationSection {
6970
/// Start of the section, as an offset in seconds from `UnitTime::start`.
7071
start: f64,
7172
/// End of the section, as an offset in seconds from `UnitTime::start`.
@@ -89,7 +90,7 @@ struct UnitTime {
8990
/// Same as `unlocked_units`, but unlocked by rmeta.
9091
unlocked_rmeta_units: Vec<Unit>,
9192
/// Individual compilation section durations, gathered from `--json=timings`.
92-
sections: HashMap<String, TimingSection>,
93+
sections: HashMap<String, CompilationSection>,
9394
}
9495

9596
impl UnitTime {
@@ -315,6 +316,7 @@ impl<'gctx> Timings<'gctx> {
315316
mode: unit_time.unit.mode,
316317
duration: unit_time.duration,
317318
rmeta_time: unit_time.rmeta_time,
319+
sections: unit_time.sections.clone(),
318320
}
319321
.to_json_string();
320322
crate::drop_println!(self.gctx, "{}", msg);
@@ -723,20 +725,20 @@ impl UnitTime {
723725
.sections
724726
.insert(
725727
name.to_string(),
726-
TimingSection {
728+
CompilationSection {
727729
start: now - self.start,
728730
end: None,
729731
},
730732
)
731733
.is_some()
732734
{
733-
warn!("Compilation section {name} started more than once");
735+
warn!("compilation section {name} started more than once");
734736
}
735737
}
736738

737739
fn end_section(&mut self, name: &str, now: f64) {
738740
let Some(section) = self.sections.get_mut(name) else {
739-
warn!("Compilation section {name} ended, but it has no start recorded");
741+
warn!("compilation section {name} ended, but it has no start recorded");
740742
return;
741743
};
742744
section.end = Some(now - self.start);

src/cargo/util/machine_message.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::path::{Path, PathBuf};
23

34
use cargo_util_schemas::core::PackageIdSpec;
@@ -6,7 +7,7 @@ use serde::ser;
67
use serde_json::{json, value::RawValue};
78

89
use crate::core::Target;
9-
use crate::core::compiler::CompileMode;
10+
use crate::core::compiler::{CompilationSection, CompileMode};
1011

1112
pub trait Message: ser::Serialize {
1213
fn reason(&self) -> &str;
@@ -87,6 +88,12 @@ impl<'a> Message for BuildScript<'a> {
8788
}
8889
}
8990

91+
#[derive(Serialize)]
92+
pub struct TimingInfoCompilationSection {
93+
start: f64,
94+
end: f64,
95+
}
96+
9097
#[derive(Serialize)]
9198
pub struct TimingInfo<'a> {
9299
pub package_id: PackageIdSpec,
@@ -95,6 +102,7 @@ pub struct TimingInfo<'a> {
95102
pub duration: f64,
96103
#[serde(skip_serializing_if = "Option::is_none")]
97104
pub rmeta_time: Option<f64>,
105+
pub sections: HashMap<String, CompilationSection>,
98106
}
99107

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

0 commit comments

Comments
 (0)