Skip to content

Commit c73dfed

Browse files
committed
Process calls Environment for Cpu usage for current process.
1 parent ed34eae commit c73dfed

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.FreeBSD.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public TimeSpan TotalProcessorTime
3434
{
3535
get
3636
{
37+
if (IsCurrentProcess)
38+
{
39+
return Environment.CpuUsage.TotalTime;
40+
}
41+
3742
EnsureState(State.HaveNonExitedId);
3843
Interop.Process.proc_stats stat = Interop.Process.GetThreadInfo(_processId, 0);
3944
return Process.TicksToTimeSpan(stat.userTime + stat.systemTime);
@@ -51,6 +56,10 @@ public TimeSpan UserProcessorTime
5156
{
5257
get
5358
{
59+
if (IsCurrentProcess)
60+
{
61+
return Environment.CpuUsage.UserTime;
62+
}
5463
EnsureState(State.HaveNonExitedId);
5564

5665
Interop.Process.proc_stats stat = Interop.Process.GetThreadInfo(_processId, 0);
@@ -66,6 +75,11 @@ public TimeSpan PrivilegedProcessorTime
6675
{
6776
get
6877
{
78+
if (IsCurrentProcess)
79+
{
80+
return Environment.CpuUsage.PrivilegedTime;
81+
}
82+
6983
EnsureState(State.HaveNonExitedId);
7084

7185
Interop.Process.proc_stats stat = Interop.Process.GetThreadInfo(_processId, 0);

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Linux.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ public static Process[] GetProcessesByName(string? processName, string machineNa
5353
[SupportedOSPlatform("maccatalyst")]
5454
public TimeSpan PrivilegedProcessorTime
5555
{
56-
get
57-
{
58-
return TicksToTimeSpan(GetStat().stime);
59-
}
56+
get => IsCurrentProcess ? Environment.CpuUsage.PrivilegedTime : TicksToTimeSpan(GetStat().stime);
6057
}
6158

6259
/// <summary>Gets the time the associated process was started.</summary>
@@ -132,6 +129,11 @@ public TimeSpan TotalProcessorTime
132129
{
133130
get
134131
{
132+
if (IsCurrentProcess)
133+
{
134+
Environment.CpuUsage.TotalTime;
135+
}
136+
135137
Interop.procfs.ParsedStat stat = GetStat();
136138
return TicksToTimeSpan(stat.utime + stat.stime);
137139
}
@@ -146,10 +148,7 @@ public TimeSpan TotalProcessorTime
146148
[SupportedOSPlatform("maccatalyst")]
147149
public TimeSpan UserProcessorTime
148150
{
149-
get
150-
{
151-
return TicksToTimeSpan(GetStat().utime);
152-
}
151+
get => IsCurrentProcess ? Environment.CpuUsage.UserTime : TicksToTimeSpan(GetStat().utime);
153152
}
154153

155154
partial void EnsureHandleCountPopulated()

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.OSX.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public TimeSpan PrivilegedProcessorTime
2222
{
2323
get
2424
{
25+
if (IsCurrentProcess)
26+
{
27+
return Environment.CpuUsage.PrivilegedTime;
28+
}
29+
2530
EnsureState(State.HaveNonExitedId);
2631
Interop.libproc.rusage_info_v3 info = Interop.libproc.proc_pid_rusage(_processId);
2732
return MapTime(info.ri_system_time);
@@ -64,6 +69,11 @@ public TimeSpan TotalProcessorTime
6469
{
6570
get
6671
{
72+
if (IsCurrentProcess)
73+
{
74+
return Environment.CpuUsage.TotalTime;
75+
}
76+
6777
EnsureState(State.HaveNonExitedId);
6878
Interop.libproc.rusage_info_v3 info = Interop.libproc.proc_pid_rusage(_processId);
6979
return MapTime(info.ri_system_time + info.ri_user_time);
@@ -81,6 +91,11 @@ public TimeSpan UserProcessorTime
8191
{
8292
get
8393
{
94+
if (IsCurrentProcess)
95+
{
96+
return Environment.CpuUsage.UserTime;
97+
}
98+
8499
EnsureState(State.HaveNonExitedId);
85100
Interop.libproc.rusage_info_v3 info = Interop.libproc.proc_pid_rusage(_processId);
86101
return MapTime(info.ri_user_time);

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Windows.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private DateTime ExitTimeCore
232232
[SupportedOSPlatform("maccatalyst")]
233233
public TimeSpan PrivilegedProcessorTime
234234
{
235-
get { return GetProcessTimes().PrivilegedProcessorTime; }
235+
get => IsCurrentProcess ? Environment.CpuUsage.PrivilegedTime : GetProcessTimes().TotalProcessorTime;
236236
}
237237

238238
/// <summary>Gets the time the associated process was started.</summary>
@@ -251,7 +251,7 @@ internal DateTime StartTimeCore
251251
[SupportedOSPlatform("maccatalyst")]
252252
public TimeSpan TotalProcessorTime
253253
{
254-
get { return GetProcessTimes().TotalProcessorTime; }
254+
get => IsCurrentProcess ? Environment.CpuUsage.TotalTime : GetProcessTimes().TotalProcessorTime;
255255
}
256256

257257
/// <summary>
@@ -263,7 +263,7 @@ public TimeSpan TotalProcessorTime
263263
[SupportedOSPlatform("maccatalyst")]
264264
public TimeSpan UserProcessorTime
265265
{
266-
get { return GetProcessTimes().UserProcessorTime; }
266+
get => IsCurrentProcess ? Environment.CpuUsage.UserTime : GetProcessTimes().UserProcessorTime;
267267
}
268268

269269
/// <summary>

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,8 @@ public static Process[] GetProcesses(string machineName)
11061106
return processes;
11071107
}
11081108

1109+
private bool IsCurrentProcess => _processId == Environment.ProcessId;
1110+
11091111
/// <devdoc>
11101112
/// <para>
11111113
/// Returns a new <see cref='System.Diagnostics.Process'/>

0 commit comments

Comments
 (0)