Skip to content

Conversation

ykumashev
Copy link
Contributor

@ykumashev ykumashev commented Aug 7, 2025

This PR updates the WindowsSnapshotProvider to use the new Environment.CpuUsage property introduced in .NET 9.0+ for obtaining CPU usage information, providing improved performance compared to the previous approach using Process.GetCurrentProcess().

The Environment.CpuUsage property was introduced in .NET 9.0 and provides direct access to the current process's CPU usage information without the overhead of the Process class. This change is conditionally compiled using [#if NET9_0_OR_GREATER] to ensure compatibility with earlier .NET versions.

Microsoft Reviewers: Open in CodeFlow

Resolves #5893

@Copilot Copilot AI review requested due to automatic review settings August 7, 2025 07:07
@ykumashev ykumashev requested a review from a team as a code owner August 7, 2025 07:07
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR modernizes the WindowsSnapshotProvider to leverage the new Environment.CpuUsage property available in .NET 9.0+ for improved CPU usage monitoring performance. The change replaces the previous Process.GetCurrentProcess() approach with direct access to CPU usage information.

  • Implements conditional compilation using #if NET9_0_OR_GREATER to maintain backward compatibility
  • Updates both GetSnapshot() and GetCpuTicks() methods to use the new API
  • Eliminates the overhead of creating Process instances when running on .NET 9.0+

@ykumashev ykumashev enabled auto-merge (squash) August 7, 2025 15:02
@ykumashev ykumashev disabled auto-merge August 7, 2025 15:03
@ykumashev
Copy link
Contributor Author

@dotnet-policy-service agree Microsoft

@ykumashev
Copy link
Contributor Author

@dotnet-policy-service agree company="Microsoft"

@evgenyfedorov2
Copy link
Member

LGTM. However, could you please attach test results and screenshots here?
Here a few good examples: #6338, #6374

@ykumashev
Copy link
Contributor Author

LGTM. However, could you please attach test results and screenshots here? Here a few good examples: #6338, #6374

Sounds like a good idea.

Below is screenshot from a sample project using .NET 9, where I’ve integrated the Microsoft.Extensions.Diagnostics.ResourceMonitoring package from the main branch. The dashboard displays the "process.cpu.utilization" metric, which is directly affected by the changes introduced in this PR.
cpu1

The following screenshot shows the same project and package, but with the changes from this PR applied.
cpu2

As you can see, both dashboards report similar values, indicating that the changes are functioning as intended.

This was referenced Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider using Environment.CpuUsage in ResourceMonitoring
2 participants