Skip to content

Commit 5b83f80

Browse files
committed
feat(build-analysis): emit timing-info log
Log timing information. The schema is currently the same as the `--timings` JSON output.
1 parent 0cb115f commit 5b83f80

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

src/cargo/core/compiler/timings.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::core::PackageId;
77
use crate::core::compiler::job_queue::JobId;
88
use crate::core::compiler::{BuildContext, BuildRunner, TimingOutput};
99
use crate::util::cpu::State;
10+
use crate::util::log_message::LogMessage;
1011
use crate::util::machine_message::{self, Message};
1112
use crate::util::style;
1213
use crate::util::{CargoResult, GlobalContext};
@@ -208,7 +209,7 @@ impl<'gctx> Timings<'gctx> {
208209
let has_report = |what| bcx.build_config.timing_outputs.contains(&what);
209210
let report_html = has_report(TimingOutput::Html);
210211
let report_json = has_report(TimingOutput::Json);
211-
let enabled = report_html | report_json;
212+
let enabled = report_html | report_json | bcx.logger.is_some();
212213

213214
let mut root_map: HashMap<PackageId, Vec<String>> = HashMap::new();
214215
for unit in root_units {
@@ -316,7 +317,7 @@ impl<'gctx> Timings<'gctx> {
316317
/// Mark that a unit has finished running.
317318
pub fn unit_finished(
318319
&mut self,
319-
_build_runner: &BuildRunner<'_, '_>,
320+
build_runner: &BuildRunner<'_, '_>,
320321
id: JobId,
321322
unlocked: Vec<&Unit>,
322323
) {
@@ -345,6 +346,16 @@ impl<'gctx> Timings<'gctx> {
345346
.to_json_string();
346347
crate::drop_println!(self.gctx, "{}", msg);
347348
}
349+
if let Some(logger) = build_runner.bcx.logger {
350+
logger.log(LogMessage::TimingInfo {
351+
package_id: unit_time.unit.pkg.package_id().to_spec(),
352+
target: unit_time.unit.target.clone(),
353+
mode: unit_time.unit.mode,
354+
duration: unit_time.duration,
355+
rmeta_time: unit_time.rmeta_time,
356+
sections: unit_time.sections.clone().into_iter().collect(),
357+
});
358+
}
348359
self.unit_times.push(unit_time);
349360
}
350361

src/cargo/util/log_message.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
use std::io::Write;
44
use std::path::PathBuf;
55

6+
use cargo_util_schemas::core::PackageIdSpec;
67
use jiff::Timestamp;
78
use serde::Serialize;
89

10+
use crate::core::Target;
11+
use crate::core::compiler::CompilationSection;
12+
use crate::core::compiler::CompileMode;
13+
914
/// A log message.
1015
///
1116
/// Each variant represents a different type of event.
@@ -23,6 +28,17 @@ pub enum LogMessage {
2328
target_dir: PathBuf,
2429
workspace_root: PathBuf,
2530
},
31+
/// Emitted when a compilation unit finishes.
32+
TimingInfo {
33+
package_id: PackageIdSpec,
34+
target: Target,
35+
mode: CompileMode,
36+
duration: f64,
37+
#[serde(skip_serializing_if = "Option::is_none")]
38+
rmeta_time: Option<f64>,
39+
#[serde(skip_serializing_if = "Vec::is_empty")]
40+
sections: Vec<(String, CompilationSection)>,
41+
},
2642
}
2743

2844
impl LogMessage {

tests/testsuite/build_analysis.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ fn log_msg_build_started() {
116116
"target_dir": "[ROOT]/foo/target",
117117
"timestamp": "[..]T[..]Z",
118118
"workspace_root": "[ROOT]/foo"
119-
}
119+
},
120+
"{...}"
120121
]
121122
"#]]
122123
.is_json()
@@ -161,14 +162,33 @@ fn log_msg_timing_info() {
161162

162163
let content = std::fs::read_to_string(log_file.path()).unwrap();
163164

164-
// Current behavior: only build-started, no timing-info logs exist yet
165165
assert_e2e().eq(
166166
&content,
167167
str![[r#"
168168
[
169169
{
170170
"reason": "build-started",
171171
"...": "{...}"
172+
},
173+
{
174+
"duration": "{...}",
175+
"mode": "check",
176+
"package_id": "path+[ROOTURL]/foo/bar#0.0.0",
177+
"reason": "timing-info",
178+
"rmeta_time": "{...}",
179+
"run_id": "[..]T[..]Z-[..]",
180+
"target": "{...}",
181+
"timestamp": "[..]T[..]Z"
182+
},
183+
{
184+
"duration": "{...}",
185+
"mode": "check",
186+
"package_id": "path+[ROOTURL]/foo#0.0.0",
187+
"reason": "timing-info",
188+
"rmeta_time": "{...}",
189+
"run_id": "[..]T[..]Z-[..]",
190+
"target": "{...}",
191+
"timestamp": "[..]T[..]Z"
172192
}
173193
]
174194
"#]]

0 commit comments

Comments
 (0)