Skip to content

Commit 9a238b9

Browse files
committed
fix(log): fix unit_to_index mapping from bcx
This also drops unit metadata fields in unit-started event. The metdata is moved to unit-registered event instead
1 parent e668300 commit 9a238b9

File tree

6 files changed

+975
-3723
lines changed

6 files changed

+975
-3723
lines changed

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use crate::util::{CargoResult, GlobalContext};
1717

1818
use cargo_util::paths;
1919
use indexmap::IndexMap;
20-
use itertools::Itertools as _;
2120
use std::collections::HashMap;
2221
use std::io::BufWriter;
2322
use std::time::{Duration, Instant};
@@ -53,9 +52,6 @@ pub struct Timings<'gctx> {
5352
/// Total number of dirty units.
5453
total_dirty: u32,
5554
/// A map from unit to index.
56-
///
57-
/// This for saving log size.
58-
/// Only the unit-started event needs to hold the entire unit information.
5955
unit_to_index: HashMap<Unit, u64>,
6056
/// Time tracking for each individual unit.
6157
unit_times: Vec<UnitTime>,
@@ -174,13 +170,6 @@ impl<'gctx> Timings<'gctx> {
174170
None
175171
}
176172
};
177-
let unit_to_index = bcx
178-
.unit_graph
179-
.keys()
180-
.sorted()
181-
.enumerate()
182-
.map(|(i, unit)| (unit.clone(), i as u64))
183-
.collect();
184173

185174
Timings {
186175
gctx: bcx.gctx,
@@ -193,7 +182,7 @@ impl<'gctx> Timings<'gctx> {
193182
profile,
194183
total_fresh: 0,
195184
total_dirty: 0,
196-
unit_to_index,
185+
unit_to_index: bcx.unit_to_index.clone(),
197186
unit_times: Vec::new(),
198187
active: HashMap::new(),
199188
last_cpu_state,
@@ -237,9 +226,6 @@ impl<'gctx> Timings<'gctx> {
237226
};
238227
if let Some(logger) = build_runner.bcx.logger {
239228
logger.log(LogMessage::UnitStarted {
240-
package_id: unit_time.unit.pkg.package_id().to_spec(),
241-
target: (&unit_time.unit.target).into(),
242-
mode: unit_time.unit.mode,
243229
index: self.unit_to_index[&unit_time.unit],
244230
elapsed: start,
245231
});

src/cargo/ops/cargo_report/timings.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! The `cargo report timings` command.
22
3+
use std::collections::HashMap;
34
use std::ffi::OsStr;
45
use std::fs::File;
56
use std::io::BufReader;
@@ -178,6 +179,8 @@ fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'sta
178179
};
179180
let mut units: IndexMap<_, UnitEntry> = IndexMap::new();
180181

182+
let mut unit_by_index: HashMap<u64, _> = HashMap::new();
183+
181184
for (log_index, result) in serde_json::Deserializer::from_reader(reader)
182185
.into_iter::<LogMessage>()
183186
.enumerate()
@@ -208,13 +211,17 @@ fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'sta
208211
ctx.profile = profile;
209212
ctx.rustc_version = rustc_version;
210213
}
211-
LogMessage::UnitStarted {
214+
LogMessage::UnitRegistered {
212215
package_id,
213216
target,
214217
mode,
215218
index,
216-
elapsed,
217219
} => {
220+
unit_by_index.insert(index, (package_id, target, mode));
221+
}
222+
LogMessage::UnitStarted { index, elapsed } => {
223+
let (package_id, target, mode) = unit_by_index.get(&index).unwrap();
224+
218225
let version = package_id
219226
.version()
220227
.map(|v| v.to_string())
@@ -223,7 +230,7 @@ fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'sta
223230
// This is pretty similar to how the current `core::compiler::timings`
224231
// renders `core::manifest::Target`. However, our target is
225232
// a simplified type so we cannot reuse the same logic here.
226-
let mut target_str = if target.kind == "lib" && mode == CompileMode::Build {
233+
let mut target_str = if target.kind == "lib" && mode == &CompileMode::Build {
227234
// Special case for brevity, since most dependencies hit this path.
228235
"".to_string()
229236
} else if target.kind == "build-script" {

src/cargo/util/log_message.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,14 @@ pub enum LogMessage {
7474
},
7575
/// Emitted when a compilation unit starts.
7676
UnitStarted {
77-
/// Package ID specification.
78-
package_id: PackageIdSpec,
79-
/// Cargo target (lib, bin, example, etc.).
80-
target: Target,
81-
/// The compilation action this unit is for (check, build, test, etc.).
82-
mode: CompileMode,
83-
/// Unit index for compact reference in subsequent events.
77+
/// Unit index from the associated unit-registered event.
8478
index: u64,
8579
/// Seconds elapsed from build start.
8680
elapsed: f64,
8781
},
8882
/// Emitted when a section (e.g., rmeta, link) of the compilation unit finishes.
8983
UnitRmetaFinished {
90-
/// Unit index from the associated unit-started event.
84+
/// Unit index from the associated unit-registered event.
9185
index: u64,
9286
/// Seconds elapsed from build start.
9387
elapsed: f64,
@@ -99,7 +93,7 @@ pub enum LogMessage {
9993
///
10094
/// Requires `-Zsection-timings` to be enabled.
10195
UnitSectionStarted {
102-
/// Unit index from the associated unit-started event.
96+
/// Unit index from the associated unit-registered event.
10397
index: u64,
10498
/// Seconds elapsed from build start.
10599
elapsed: f64,
@@ -110,7 +104,7 @@ pub enum LogMessage {
110104
///
111105
/// Requires `-Zsection-timings` to be enabled.
112106
UnitSectionFinished {
113-
/// Unit index from the associated unit-started event.
107+
/// Unit index from the associated unit-registered event.
114108
index: u64,
115109
/// Seconds elapsed from build start.
116110
elapsed: f64,
@@ -119,7 +113,7 @@ pub enum LogMessage {
119113
},
120114
/// Emitted when a compilation unit finishes.
121115
UnitFinished {
122-
/// Unit index from the associated unit-started event.
116+
/// Unit index from the associated unit-registered event.
123117
index: u64,
124118
/// Seconds elapsed from build start.
125119
elapsed: f64,

tests/testsuite/build_analysis.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,8 @@ fn log_msg_timing_info() {
131131
{
132132
"elapsed": "{...}",
133133
"index": 0,
134-
"mode": "check",
135-
"package_id": "path+[ROOTURL]/foo/bar#0.0.0",
136134
"reason": "unit-started",
137135
"run_id": "[..]T[..]Z-[..]",
138-
"target": {
139-
"kind": "lib",
140-
"name": "bar"
141-
},
142136
"timestamp": "[..]T[..]Z"
143137
},
144138
{
@@ -161,14 +155,8 @@ fn log_msg_timing_info() {
161155
{
162156
"elapsed": "{...}",
163157
"index": 1,
164-
"mode": "check",
165-
"package_id": "path+[ROOTURL]/foo#0.0.0",
166158
"reason": "unit-started",
167159
"run_id": "[..]T[..]Z-[..]",
168-
"target": {
169-
"kind": "lib",
170-
"name": "foo"
171-
},
172160
"timestamp": "[..]T[..]Z"
173161
},
174162
{
@@ -225,14 +213,8 @@ fn log_msg_timing_info_section_timings() {
225213
{
226214
"elapsed": "{...}",
227215
"index": 0,
228-
"mode": "check",
229-
"package_id": "path+[ROOTURL]/foo/bar#0.0.0",
230216
"reason": "unit-started",
231217
"run_id": "[..]T[..]Z-[..]",
232-
"target": {
233-
"kind": "lib",
234-
"name": "bar"
235-
},
236218
"timestamp": "[..]T[..]Z"
237219
},
238220
{
@@ -287,14 +269,8 @@ fn log_msg_timing_info_section_timings() {
287269
{
288270
"elapsed": "{...}",
289271
"index": 1,
290-
"mode": "check",
291-
"package_id": "path+[ROOTURL]/foo#0.0.0",
292272
"reason": "unit-started",
293273
"run_id": "[..]T[..]Z-[..]",
294-
"target": {
295-
"kind": "bin",
296-
"name": "foo"
297-
},
298274
"timestamp": "[..]T[..]Z"
299275
},
300276
{

0 commit comments

Comments
 (0)