Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ internal struct ProcessCpuInformation
}

[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetCpuUtilization")]
internal static partial int GetCpuUtilization(ref ProcessCpuInformation previousCpuInfo);
internal static partial double GetCpuUtilization(ref ProcessCpuInformation previousCpuInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected override void OnEventCommand(EventCommandEventArgs command)
// overhead by at all times even when counters aren't enabled.

// On disable, PollingCounters will stop polling for values so it should be fine to leave them around.
_cpuTimeCounter ??= new PollingCounter("cpu-usage", this, () => RuntimeEventSourceHelper.GetCpuUsage()) { DisplayName = "CPU Usage", DisplayUnits = "%" };
_cpuTimeCounter ??= new PollingCounter("cpu-usage", this, RuntimeEventSourceHelper.GetCpuUsage) { DisplayName = "CPU Usage", DisplayUnits = "%" };
_workingSetCounter ??= new PollingCounter("working-set", this, () => ((double)Environment.WorkingSet / 1_000_000)) { DisplayName = "Working Set", DisplayUnits = "MB" };
_gcHeapSizeCounter ??= new PollingCounter("gc-heap-size", this, () => ((double)GC.GetTotalMemory(false) / 1_000_000)) { DisplayName = "GC Heap Size", DisplayUnits = "MB" };
_gen0GCCounter ??= new IncrementingPollingCounter("gen-0-gc-count", this, () => GC.CollectionCount(0)) { DisplayName = "Gen 0 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static class RuntimeEventSourceHelper
{
private static Interop.Sys.ProcessCpuInformation s_cpuInfo;

internal static int GetCpuUsage() =>
internal static double GetCpuUsage() =>
Interop.Sys.GetCpuUtilization(ref s_cpuInfo) / Environment.ProcessorCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ internal static class RuntimeEventSourceHelper
private static long s_prevSystemUserTime;
private static long s_prevSystemKernelTime;

internal static int GetCpuUsage()
internal static double GetCpuUsage()
{
// Returns the current process' CPU usage as a percentage

int cpuUsage = 0;
double cpuUsage = 0.0;

if (Interop.Kernel32.GetProcessTimes(Interop.Kernel32.GetCurrentProcess(), out _, out _, out long procKernelTime, out long procUserTime) &&
Interop.Kernel32.GetSystemTimes(out _, out long systemUserTime, out long systemKernelTime))
Expand All @@ -25,7 +25,7 @@ internal static int GetCpuUsage()
if (s_prevSystemUserTime != 0 && s_prevSystemKernelTime != 0 && // These may be 0 when we report CPU usage for the first time, in which case we should just return 0.
totalSystemTime != 0)
{
cpuUsage = (int)(totalProcTime * 100 / totalSystemTime);
cpuUsage = (totalProcTime * 100.0 / totalSystemTime);
}

s_prevProcUserTime = procUserTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private struct CpuUtilizationReader
{
private Interop.Sys.ProcessCpuInformation _cpuInfo;

public int CurrentUtilization =>
public double CurrentUtilization =>
Interop.Sys.GetCpuUtilization(ref _cpuInfo) / Environment.ProcessorCount;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static void GateThreadStart()
(uint)threadPoolInstance.GetAndResetHighWatermarkCountOfThreadsProcessingUserCallbacks());
}

int cpuUtilization = cpuUtilizationReader.CurrentUtilization;
int cpuUtilization = (int)cpuUtilizationReader.CurrentUtilization;
threadPoolInstance._cpuUtilization = cpuUtilization;

bool needGateThreadForRuntime = ThreadPool.PerformRuntimeSpecificGateActivities(cpuUtilization);
Expand Down
8 changes: 4 additions & 4 deletions src/native/libs/System.Native/pal_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ uint64_t SystemNative_GetTimestamp()
#endif
}

int32_t SystemNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo)
double SystemNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo)
{
uint64_t kernelTime = 0;
uint64_t userTime = 0;
Expand All @@ -109,7 +109,7 @@ int32_t SystemNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo)
else
{
kernelTime =
((uint64_t)(resUsage.ru_stime.tv_sec) * SecondsToNanoSeconds) +
((uint64_t)(resUsage.ru_stime.tv_sec) * SecondsToNanoSeconds) +
((uint64_t)(resUsage.ru_stime.tv_usec) * MicroSecondsToNanoSeconds);
userTime =
((uint64_t)(resUsage.ru_utime.tv_sec) * SecondsToNanoSeconds) +
Expand All @@ -134,10 +134,10 @@ int32_t SystemNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo)
cpuBusyTime = (userTime - lastRecordedUserTime) + (kernelTime - lastRecordedKernelTime);
}

int32_t cpuUtilization = 0;
double cpuUtilization = 0.0;
if (cpuTotalTime > 0 && cpuBusyTime > 0)
{
cpuUtilization = (int32_t)(cpuBusyTime * 100 / cpuTotalTime);
cpuUtilization = ((double)cpuBusyTime * 100.0 / (double)cpuTotalTime);
}

previousCpuInfo->lastRecordedCurrentTime = currentTime;
Expand Down
2 changes: 1 addition & 1 deletion src/native/libs/System.Native/pal_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ PALEXPORT uint64_t SystemNative_GetTimestamp(void);
* returned is sum of utilization across all processors, e.g. this function will
* return 200 when two cores are running at 100%.
*/
PALEXPORT int32_t SystemNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo);
PALEXPORT double SystemNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo);