Skip to content

Comments

Adding cpu usage in cargo log#16655

Draft
Suryansh-Dey wants to merge 2 commits intorust-lang:masterfrom
Suryansh-Dey:cpu-usage
Draft

Adding cpu usage in cargo log#16655
Suryansh-Dey wants to merge 2 commits intorust-lang:masterfrom
Suryansh-Dey:cpu-usage

Conversation

@Suryansh-Dey
Copy link

@Suryansh-Dey Suryansh-Dey commented Feb 19, 2026

Add CPU usage data to cargo report timings

Summary

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.

Closes #16651

Changes

src/cargo/util/log_message.rs

  • 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.

Sample log entry:

{"reason":"cpu-usage","elapsed":1.255,"usage":76.72}

@rustbot rustbot added A-timings Area: timings Command-report S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 19, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 19, 2026

r? @weihanglo

rustbot has assigned @weihanglo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ehuss, @epage, @weihanglo
  • @ehuss, @epage, @weihanglo expanded to ehuss, epage, weihanglo
  • Random selection from ehuss, epage, weihanglo

@epage
Copy link
Contributor

epage commented Feb 19, 2026

Note that #16651 was marked S-needs-design which means it isn't ready yet for implementation and review, see https://doc.crates.io/contrib/issues.html#issue-status-labels

@Suryansh-Dey
Copy link
Author

Suryansh-Dey commented Feb 19, 2026

Note that #16651 was marked S-needs-design which means it isn't ready yet for implementation and review, see https://doc.crates.io/contrib/issues.html#issue-status-labels

I may update or tweak my PR later according to yous discussions. Just a plan proposal because I found it interesting to work upon.

@epage
Copy link
Contributor

epage commented Feb 19, 2026

s. Just a plan proposal because I found it interesting to work upon.

If your intent is just that, then I would either recommend keeping it local or communicating your intent and marking it as a Draft.

@Suryansh-Dey Suryansh-Dey marked this pull request as draft February 20, 2026 04:27
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add CPU usage to cargo report timings

4 participants