You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR adds CPU usage tracking to the log files generated by -Z build-analysis, enabling cargo report timings to reconstruct and display CPU usage graphs in the HTML timing report.
Previously, CPU usage data was only available in the live --timings path (collected in-memory by record_cpu() and passed directly to RenderContext). When using cargo report timings to reconstruct a report from log files, the CPU usage graph was always empty — even though the HTML legend still displayed a "CPU Usage" entry.
Added CpuUsage { elapsed: f64, usage: f64 } variant to the LogMessage enum, representing a periodic CPU usage sample.
src/cargo/core/compiler/timings/mod.rs
In finished(), log all accumulated CPU usage samples to the file logger before generating the HTML report.
Moved CPU logging outside the get_logs() guard (which only succeeds for --timings) into the outer if let Some(logger) block, so data is written whenever any logger exists (including the file logger used by -Z build-analysis).
Updated the live path to use Cow::Borrowed for the cpu_usage field.
src/cargo/core/compiler/timings/report.rs
Changed RenderContext::cpu_usage from &'a [(f64, f64)] to Cow<'a, [(f64, f64)]> to support both borrowed data (live --timings path) and owned data (log reconstruction path).
src/cargo/ops/cargo_report/timings.rs
Added handling of LogMessage::CpuUsage in prepare_context() to collect CPU samples from log files and store them as Cow::Owned in the render context.
Updating tests
Added a "{...}" trailing wildcard to match the cpu-usage entries, same pattern used by other tests in the file (e.g., log_rebuild_reason_fresh_build). Also proactively applied the same fix to log_msg_timing_info_section_timings (nightly-only test) for consistency.
Testing
All existing cargo_report_timings tests pass (16 passed, 1 ignored for nightly-only).
All existing timings tests pass.
End-to-end verified: built a project with CARGO_BUILD_ANALYSIS_ENABLED=true cargo check -Z build-analysis, confirmed 120 cpu-usage entries in the log file, and verified cargo report timings produces an HTML report with populated CPU_USAGE JavaScript data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add CPU usage data to
cargo report timingsSummary
This PR adds CPU usage tracking to the log files generated by
-Z build-analysis, enablingcargo report timingsto reconstruct and display CPU usage graphs in the HTML timing report.Previously, CPU usage data was only available in the live
--timingspath (collected in-memory by record_cpu() and passed directly to RenderContext). When usingcargo report timingsto reconstruct a report from log files, the CPU usage graph was always empty — even though the HTML legend still displayed a "CPU Usage" entry.Closes #16651
Changes
src/cargo/util/log_message.rs
CpuUsage { elapsed: f64, usage: f64 }variant to theLogMessageenum, representing a periodic CPU usage sample.src/cargo/core/compiler/timings/mod.rs
--timings) into the outerif let Some(logger)block, so data is written whenever any logger exists (including the file logger used by-Z build-analysis).Cow::Borrowedfor thecpu_usagefield.src/cargo/core/compiler/timings/report.rs
RenderContext::cpu_usagefrom&'a [(f64, f64)]toCow<'a, [(f64, f64)]>to support both borrowed data (live--timingspath) and owned data (log reconstruction path).src/cargo/ops/cargo_report/timings.rs
LogMessage::CpuUsagein prepare_context() to collect CPU samples from log files and store them asCow::Ownedin the render context.Updating tests
Added a "{...}" trailing wildcard to match the cpu-usage entries, same pattern used by other tests in the file (e.g., log_rebuild_reason_fresh_build). Also proactively applied the same fix to log_msg_timing_info_section_timings (nightly-only test) for consistency.
Testing
CARGO_BUILD_ANALYSIS_ENABLED=true cargo check -Z build-analysis, confirmed 120cpu-usageentries in the log file, and verifiedcargo report timingsproduces an HTML report with populatedCPU_USAGEJavaScript data.Sample log entry:
{"reason":"cpu-usage","elapsed":1.255,"usage":76.72}