|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using System;
|
| 5 | +#if !NET9_0_OR_GREATER |
5 | 6 | using System.Diagnostics;
|
| 7 | +#endif |
6 | 8 | using System.Diagnostics.CodeAnalysis;
|
7 | 9 | using System.Diagnostics.Metrics;
|
8 | 10 | using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Interop;
|
@@ -95,18 +97,31 @@ internal WindowsSnapshotProvider(
|
95 | 97 |
|
96 | 98 | public Snapshot GetSnapshot()
|
97 | 99 | {
|
| 100 | +#if NET9_0_OR_GREATER |
| 101 | + var cpuUsage = Environment.CpuUsage; |
| 102 | + return new Snapshot( |
| 103 | + totalTimeSinceStart: TimeSpan.FromTicks(_timeProvider.GetUtcNow().Ticks), |
| 104 | + kernelTimeSinceStart: cpuUsage.PrivilegedTime, |
| 105 | + userTimeSinceStart: cpuUsage.UserTime, |
| 106 | + memoryUsageInBytes: (ulong)Environment.WorkingSet); |
| 107 | +#else |
98 | 108 | using var process = Process.GetCurrentProcess();
|
99 |
| - |
100 |
| - return new Snapshot(totalTimeSinceStart: TimeSpan.FromTicks(_timeProvider.GetUtcNow().Ticks), |
| 109 | + return new Snapshot( |
| 110 | + totalTimeSinceStart: TimeSpan.FromTicks(_timeProvider.GetUtcNow().Ticks), |
101 | 111 | kernelTimeSinceStart: process.PrivilegedProcessorTime,
|
102 | 112 | userTimeSinceStart: process.UserProcessorTime,
|
103 | 113 | memoryUsageInBytes: (ulong)Environment.WorkingSet);
|
| 114 | +#endif |
104 | 115 | }
|
105 | 116 |
|
106 | 117 | internal static long GetCpuTicks()
|
107 | 118 | {
|
| 119 | +#if NET9_0_OR_GREATER |
| 120 | + return Environment.CpuUsage.TotalTime.Ticks; |
| 121 | +#else |
108 | 122 | using var process = Process.GetCurrentProcess();
|
109 | 123 | return process.TotalProcessorTime.Ticks;
|
| 124 | +#endif |
110 | 125 | }
|
111 | 126 |
|
112 | 127 | internal static int GetCpuUnits() => Environment.ProcessorCount;
|
|
0 commit comments