-
Notifications
You must be signed in to change notification settings - Fork 827
Consider using Environment.CpuUsage in ResourceMonitoring #6696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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()
andGetCpuTicks()
methods to use the new API - Eliminates the overhead of creating Process instances when running on .NET 9.0+
...aries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/WindowsSnapshotProvider.cs
Show resolved
Hide resolved
@dotnet-policy-service agree Microsoft |
@dotnet-policy-service agree company="Microsoft" |
...aries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/WindowsSnapshotProvider.cs
Outdated
Show resolved
Hide resolved
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. The following screenshot shows the same project and package, but with the changes from this PR applied. As you can see, both dashboards report similar values, indicating that the changes are functioning as intended. |
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