Skip to content

Commit

Permalink
Rework core frequency monitoring to be less wasteful
Browse files Browse the repository at this point in the history
  • Loading branch information
L3tum committed Oct 19, 2019
1 parent 093a11d commit 1b41d04
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions HardwareInformation/MachineInformationGatherer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private static void GetCoreSpeeds()
/// repeat). Values under 1000ms may produce wrong results.
/// </param>
/// <returns></returns>
public static async Task<MachineInformation.Core> MonitorCoreFrequencies(int coreNumber,
public static Task<MachineInformation.Core> MonitorCoreFrequencies(int coreNumber,
CancellationToken token, int measurementDelay = 1000)
{
if (coreNumber > 64 || coreNumber < 0)
Expand All @@ -336,9 +336,9 @@ private static void GetCoreSpeeds()
throw new PlatformNotSupportedException("This method only works on Windows or Linux!");
}

var core = new MachineInformation.Core();
var thread = Task.Run(() =>
return Task.Run(() =>
{
var core = new MachineInformation.Core();
var highestFrequency = 0u;
var lowestFrequency = 0u;
PerformanceCounter counter = null;
Expand All @@ -349,19 +349,10 @@ private static void GetCoreSpeeds()
counter = new PerformanceCounter("Processor Information", "% Processor Performance",
"0," + coreNumber);
counter.NextValue();
var mos = new ManagementObjectSearcher(
"select MaxClockSpeed from Win32_Processor");

foreach (var managementBaseObject in mos.Get())
{
foreach (var propertyData in managementBaseObject.Properties)
{
if (propertyData.Name == "MaxClockSpeed")
{
normalFrequency = uint.Parse(propertyData.Value.ToString());
}
}
}
ManagementObject Mo = new ManagementObject($"Win32_Processor.DeviceID='CPU{coreNumber}'");

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

while (!token.IsCancellationRequested)
Expand Down Expand Up @@ -396,11 +387,9 @@ private static void GetCoreSpeeds()
core.Number = (uint) coreNumber;
core.MaxClockSpeed = highestFrequency;
core.NormalClockSpeed = lowestFrequency;
});

await thread;

return core;
return core;
});
}
}
}

0 comments on commit 1b41d04

Please sign in to comment.