Skip to content

Commit

Permalink
Process calls Environment for Cpu usage for current process.
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh committed Jul 20, 2024
1 parent ed34eae commit c73dfed
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public TimeSpan TotalProcessorTime
{
get
{
if (IsCurrentProcess)
{
return Environment.CpuUsage.TotalTime;
}

EnsureState(State.HaveNonExitedId);
Interop.Process.proc_stats stat = Interop.Process.GetThreadInfo(_processId, 0);
return Process.TicksToTimeSpan(stat.userTime + stat.systemTime);
Expand All @@ -51,6 +56,10 @@ public TimeSpan UserProcessorTime
{
get
{
if (IsCurrentProcess)
{
return Environment.CpuUsage.UserTime;
}
EnsureState(State.HaveNonExitedId);

Interop.Process.proc_stats stat = Interop.Process.GetThreadInfo(_processId, 0);
Expand All @@ -66,6 +75,11 @@ public TimeSpan PrivilegedProcessorTime
{
get
{
if (IsCurrentProcess)
{
return Environment.CpuUsage.PrivilegedTime;
}

EnsureState(State.HaveNonExitedId);

Interop.Process.proc_stats stat = Interop.Process.GetThreadInfo(_processId, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ public static Process[] GetProcessesByName(string? processName, string machineNa
[SupportedOSPlatform("maccatalyst")]
public TimeSpan PrivilegedProcessorTime
{
get
{
return TicksToTimeSpan(GetStat().stime);
}
get => IsCurrentProcess ? Environment.CpuUsage.PrivilegedTime : TicksToTimeSpan(GetStat().stime);
}

/// <summary>Gets the time the associated process was started.</summary>
Expand Down Expand Up @@ -132,6 +129,11 @@ public TimeSpan TotalProcessorTime
{
get
{
if (IsCurrentProcess)
{
Environment.CpuUsage.TotalTime;
}

Interop.procfs.ParsedStat stat = GetStat();
return TicksToTimeSpan(stat.utime + stat.stime);
}
Expand All @@ -146,10 +148,7 @@ public TimeSpan TotalProcessorTime
[SupportedOSPlatform("maccatalyst")]
public TimeSpan UserProcessorTime
{
get
{
return TicksToTimeSpan(GetStat().utime);
}
get => IsCurrentProcess ? Environment.CpuUsage.UserTime : TicksToTimeSpan(GetStat().utime);
}

partial void EnsureHandleCountPopulated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public TimeSpan PrivilegedProcessorTime
{
get
{
if (IsCurrentProcess)
{
return Environment.CpuUsage.PrivilegedTime;
}

EnsureState(State.HaveNonExitedId);
Interop.libproc.rusage_info_v3 info = Interop.libproc.proc_pid_rusage(_processId);
return MapTime(info.ri_system_time);
Expand Down Expand Up @@ -64,6 +69,11 @@ public TimeSpan TotalProcessorTime
{
get
{
if (IsCurrentProcess)
{
return Environment.CpuUsage.TotalTime;
}

EnsureState(State.HaveNonExitedId);
Interop.libproc.rusage_info_v3 info = Interop.libproc.proc_pid_rusage(_processId);
return MapTime(info.ri_system_time + info.ri_user_time);
Expand All @@ -81,6 +91,11 @@ public TimeSpan UserProcessorTime
{
get
{
if (IsCurrentProcess)
{
return Environment.CpuUsage.UserTime;
}

EnsureState(State.HaveNonExitedId);
Interop.libproc.rusage_info_v3 info = Interop.libproc.proc_pid_rusage(_processId);
return MapTime(info.ri_user_time);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private DateTime ExitTimeCore
[SupportedOSPlatform("maccatalyst")]
public TimeSpan PrivilegedProcessorTime
{
get { return GetProcessTimes().PrivilegedProcessorTime; }
get => IsCurrentProcess ? Environment.CpuUsage.PrivilegedTime : GetProcessTimes().TotalProcessorTime;
}

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

/// <summary>
Expand All @@ -263,7 +263,7 @@ public TimeSpan TotalProcessorTime
[SupportedOSPlatform("maccatalyst")]
public TimeSpan UserProcessorTime
{
get { return GetProcessTimes().UserProcessorTime; }
get => IsCurrentProcess ? Environment.CpuUsage.UserTime : GetProcessTimes().UserProcessorTime;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,8 @@ public static Process[] GetProcesses(string machineName)
return processes;
}

private bool IsCurrentProcess => _processId == Environment.ProcessId;

/// <devdoc>
/// <para>
/// Returns a new <see cref='System.Diagnostics.Process'/>
Expand Down

0 comments on commit c73dfed

Please sign in to comment.