Skip to content

Commit

Permalink
Remove frequency monitoring because it's way too expensive
Browse files Browse the repository at this point in the history
  • Loading branch information
L3tum committed Oct 19, 2019
1 parent 1b41d04 commit 97c74fc
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 90 deletions.
84 changes: 0 additions & 84 deletions HardwareInformation/MachineInformationGatherer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,89 +307,5 @@ private static void GetCoreSpeeds()
information.Cpu.MaxClockSpeed =
information.Cpu.Cores.Count > 0 ? information.Cpu.Cores.Max(c => c.MaxClockSpeed) : 0;
}

/// <summary>
/// Monitors the frequency of the specified core until the token is cancelled. Reports maximum frequency in
/// Core.MaxClockSpeed and minimum frequency in Core.NormalClockSpeed.
/// </summary>
/// <param name="coreNumber">
/// 0-indexed number of the core to monitor. Includes hyperthreaded cores (e.g. 0 and 1 are the
/// same physical core)
/// </param>
/// <param name="token">Token to stop the monitoring.</param>
/// <param name="measurementDelay">
/// Delay in ms for taking the measurement (e.g. wait $measurementDelay, take measurement,
/// repeat). Values under 1000ms may produce wrong results.
/// </param>
/// <returns></returns>
public static Task<MachineInformation.Core> MonitorCoreFrequencies(int coreNumber,
CancellationToken token, int measurementDelay = 1000)
{
if (coreNumber > 64 || coreNumber < 0)
{
throw new ArgumentException("Core Number must be below 65 and above 0!");
}

if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
throw new PlatformNotSupportedException("This method only works on Windows or Linux!");
}

return Task.Run(() =>
{
var core = new MachineInformation.Core();
var highestFrequency = 0u;
var lowestFrequency = 0u;
PerformanceCounter counter = null;
var normalFrequency = 0u;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
counter = new PerformanceCounter("Processor Information", "% Processor Performance",
"0," + coreNumber);
counter.NextValue();

ManagementObject Mo = new ManagementObject($"Win32_Processor.DeviceID='CPU{coreNumber}'");

normalFrequency = (uint) Mo["MaxClockSpeed"];
}

while (!token.IsCancellationRequested)
{
Thread.Sleep(measurementDelay);
var measuredValue = 0u;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
measuredValue = (uint) (counter.NextValue() / 100.0f * normalFrequency);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
// KHz
var freq = ulong.Parse(
File.ReadAllText($"/sys/devices/system/cpu/cpu{coreNumber}/cpufreq/scaling_cur_freq"));

measuredValue = (uint) (freq / 1000);
}

if (measuredValue > highestFrequency)
{
highestFrequency = measuredValue;
}

if (measuredValue < lowestFrequency)
{
lowestFrequency = measuredValue;
}
}

core.Number = (uint) coreNumber;
core.MaxClockSpeed = highestFrequency;
core.NormalClockSpeed = lowestFrequency;

return core;
});
}
}
}
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ HRF = Human Readable Format. Normal capacity/size is in bytes, while this is a s

**\*\*\*\* Only available on AMD platforms.**

One additional feature is drive-by core frequency monitoring. This is available on Windows and Linux by calling the following function. See the doc blocks for more information.

``
MachineInformationGatherer.MonitorCoreFrequencies(int coreNumber, CancellationToken token, int measurementDelay = 1000) : Task<MachineInformation.Core>
``

## Goal

The immediate goal is somewhat feature-parity with CPU-Z/CPUID.
Expand Down

0 comments on commit 97c74fc

Please sign in to comment.