-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix "Unknown Processor" on Windows if WMIC is not present #2749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
timcassell
merged 70 commits into
dotnet:master
from
alastairlundy:fix-unknown-processor-windows
Jun 16, 2025
Merged
Changes from all commits
Commits
Show all changes
70 commits
Select commit
Hold shift + click to select a range
495855a
allow MosCpuDetector to run on .NET 5+
alastairlundy 3a75c1e
Revert "allow MosCpuDetector to run on .NET 5+"
alastairlundy 3fbf726
add WmiLightCpu Detector
alastairlundy 5972f77
Fix WmicCpuDetector being chosen if not available
alastairlundy 7dc92b0
enable NativeAOT
alastairlundy fd5f096
simplify IsApplicable
alastairlundy 9b9a5eb
Update spacing of SupportedOsPlatform attribute
alastairlundy 8762345
Merge branch 'master' into fix-unknown-processor-windows
alastairlundy 589f23a
Revert "Fix WmicCpuDetector being chosen if not available"
alastairlundy 2bfbd44
Merge branch 'fix-unknown-processor-windows' of https://github.com/al…
alastairlundy a62fe32
add WMIC deprecation remarks
alastairlundy 56aa5ca
remove WmiLight code
alastairlundy e7ad945
update WmiCpuInfoParser to return null if Processor Name isn't detected
alastairlundy 29e2f96
remove WmiLightCpuDetector reference from WindowsCpuDetector
alastairlundy f9a5c34
Update WmicCpuInfoParser.cs
alastairlundy c181db9
check if wmicOutput is null or empty instead
alastairlundy 18f44b8
add PowershellWmiCpuDetector (parser still not complete)
alastairlundy fa77462
return null if there's no version of powershell installed
alastairlundy 0f3dd87
fix Powershell 7+ check
alastairlundy b6d9fb4
rework search statement given that regex isn't supported
alastairlundy 8d0fbb9
add parser code
alastairlundy aca21d0
add PowershellWmiCpuDetector to WindowsCpuDetector
alastairlundy 74c1b6c
rename variable to lower case
alastairlundy 52155fa
improve checking of latest powershell 7+ version
alastairlundy d974b1a
use explicit typing
alastairlundy c7aa4e2
fix frequency addition issue
alastairlundy fa4ea57
revert to how WMIC parser handles processor frequency
alastairlundy 3a8d884
add string is null or empty check to WmiCpuDetector
alastairlundy b4ceb96
invoke Powershell as "PowerShell" if the file isn't found
alastairlundy bc3b8e7
add nominal Frequency detection and improve max frequency detection
alastairlundy 06e82da
fix issue with detecting latest Powershell
alastairlundy 32f0d57
update comment
alastairlundy ef2385b
Update PowershellWmiCpuDetector.cs
alastairlundy fb87c98
refactor Powershell locating code to PowershellLocator
alastairlundy 59b36be
simplify frequency checks
alastairlundy be3fbfe
Create PowershellWmiParserTests.cs
alastairlundy 677bd1b
fix null being returned when object is expected.
alastairlundy c856ab0
rename test
alastairlundy 8fbccd5
Merge branch 'dotnet:master' into fix-unknown-processor-windows
alastairlundy c54ee51
simplify max frequency check
alastairlundy 1d86ff2
use """ for string in parser test
alastairlundy a13283b
Merge branch 'fix-unknown-processor-windows' of https://github.com/al…
alastairlundy 589566f
Update PowershellWmiCpuInfoParserTests.cs
alastairlundy 3e3d67b
reduce indentation with """
alastairlundy 106cac1
remove unnecessary test info
alastairlundy 2413529
move string null check to caller
alastairlundy 476cad5
add nominal frequency support for MosCpuDetector
alastairlundy caf07ad
Update src/BenchmarkDotNet/Detectors/Cpu/Windows/PowershellWmiCpuDete…
alastairlundy 2e0867a
remove nullability of parser
alastairlundy 30768de
check if tempMaxFrequency > 0 before assignment
alastairlundy afb3220
remove nullability of WmicCpuInfoParser
alastairlundy ffc2b75
Merge branch 'fix-unknown-processor-windows' of https://github.com/al…
alastairlundy b60ad9f
use file scoped namespace
alastairlundy f300e4e
use double instead of int
alastairlundy 85d6f1b
add null check to LinuxCpuDetector
alastairlundy f6d6eaf
change nullability of LinuxCpuParser
alastairlundy a5f7153
update LinuxCpuInfoParser nominal and max frequency detection
alastairlundy 65c45b9
Update LinuxCpuInfoParser.cs
alastairlundy d2741e1
fix nullability check
alastairlundy d1172a3
simplify nominal frequency comparison
alastairlundy 8307016
add null check to macOS
alastairlundy f7a6d37
Merge branch 'fix-unknown-processor-windows' of https://github.com/al…
alastairlundy cca1e5b
Merge branch 'dotnet:master' into fix-unknown-processor-windows
alastairlundy d5eb7e4
don't accept null string from detector
alastairlundy bbd6a8a
Merge branch 'fix-unknown-processor-windows' of https://github.com/al…
alastairlundy b85d12d
fix LinuxCpuInfo parser test issues and improve robustness of Linux C…
alastairlundy 9609f83
fix Powershell Wmi Parser parsing issues
alastairlundy 8059686
fix .net framework cpu parsing issue
alastairlundy 183f353
do implicit conversion to double from uint
alastairlundy 5f1328f
Merge branch 'master' into fix-unknown-processor-windows
alastairlundy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/BenchmarkDotNet/Detectors/Cpu/Windows/PowershellWmiCpuDetector.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.Versioning; | ||
using System.Text.RegularExpressions; | ||
using BenchmarkDotNet.Helpers; | ||
using Perfolizer.Models; | ||
|
||
namespace BenchmarkDotNet.Detectors.Cpu.Windows; | ||
|
||
/// <summary> | ||
/// CPU information from output of the `wmic cpu get Name, NumberOfCores, NumberOfLogicalProcessors /Format:List` command. | ||
/// Windows only. | ||
/// </summary> | ||
internal class PowershellWmiCpuDetector : ICpuDetector | ||
{ | ||
private readonly string windowsPowershellPath = | ||
$"{Environment.SystemDirectory}{Path.DirectorySeparatorChar}WindowsPowerShell{Path.DirectorySeparatorChar}" + | ||
$"v1.0{Path.DirectorySeparatorChar}powershell.exe"; | ||
|
||
public bool IsApplicable() => OsDetector.IsWindows(); | ||
|
||
#if NET6_0_OR_GREATER | ||
[SupportedOSPlatform("windows")] | ||
#endif | ||
public CpuInfo? Detect() | ||
{ | ||
if (!IsApplicable()) return null; | ||
|
||
const string argList = $"{WmicCpuInfoKeyNames.Name}, " + | ||
$"{WmicCpuInfoKeyNames.NumberOfCores}, " + | ||
$"{WmicCpuInfoKeyNames.NumberOfLogicalProcessors}, " + | ||
$"{WmicCpuInfoKeyNames.MaxClockSpeed}"; | ||
|
||
string output = ProcessHelper.RunAndReadOutput(PowerShellLocator.LocateOnWindows() ?? "PowerShell", | ||
"Get-CimInstance Win32_Processor -Property " + argList); | ||
|
||
if (string.IsNullOrEmpty(output)) | ||
return null; | ||
|
||
return PowershellWmiCpuInfoParser.Parse(output); | ||
timcassell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/BenchmarkDotNet/Detectors/Cpu/Windows/PowershellWmiCpuInfoParser.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using BenchmarkDotNet.Extensions; | ||
using BenchmarkDotNet.Helpers; | ||
using Perfolizer.Horology; | ||
using Perfolizer.Models; | ||
|
||
namespace BenchmarkDotNet.Detectors.Cpu.Windows; | ||
|
||
internal static class PowershellWmiCpuInfoParser | ||
alastairlundy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
internal static CpuInfo Parse(string powershellWmiOutput) | ||
{ | ||
HashSet<string> processorModelNames = new HashSet<string>(); | ||
|
||
int physicalCoreCount = 0; | ||
int logicalCoreCount = 0; | ||
int processorCount = 0; | ||
double maxFrequency = 0.0; | ||
double nominalFrequency = 0.0; | ||
|
||
List<Dictionary<string, string>> processors = SectionsHelper.ParseSectionsForPowershellWmi(powershellWmiOutput, ':'); | ||
foreach (Dictionary<string, string> processor in processors) | ||
{ | ||
if (processor.TryGetValue(WmicCpuInfoKeyNames.NumberOfCores, out string numberOfCoresValue) && | ||
int.TryParse(numberOfCoresValue, out int numberOfCores) && | ||
numberOfCores > 0) | ||
physicalCoreCount += numberOfCores; | ||
|
||
if (processor.TryGetValue(WmicCpuInfoKeyNames.NumberOfLogicalProcessors, out string numberOfLogicalValue) && | ||
int.TryParse(numberOfLogicalValue, out int numberOfLogical) && | ||
numberOfLogical > 0) | ||
logicalCoreCount += numberOfLogical; | ||
|
||
if (processor.TryGetValue(WmicCpuInfoKeyNames.Name, out string name)) | ||
{ | ||
processorModelNames.Add(name); | ||
processorCount++; | ||
} | ||
|
||
if (processor.TryGetValue(WmicCpuInfoKeyNames.MaxClockSpeed, out string frequencyValue) | ||
&& double.TryParse(frequencyValue, out double frequency) | ||
&& frequency > 0) | ||
{ | ||
nominalFrequency = nominalFrequency == 0 ? frequency : Math.Min(nominalFrequency, frequency); | ||
maxFrequency = Math.Max(maxFrequency, frequency); | ||
} | ||
} | ||
|
||
string? processorName = processorModelNames.Count > 0 ? string.Join(", ", processorModelNames) : null; | ||
Frequency? maxFrequencyActual = maxFrequency > 0 && processorCount > 0 | ||
? Frequency.FromMHz(maxFrequency) : null; | ||
|
||
Frequency? nominalFrequencyActual = nominalFrequency > 0 && processorCount > 0 ? | ||
Frequency.FromMHz(nominalFrequency) : null; | ||
|
||
return new CpuInfo | ||
{ | ||
ProcessorName = processorName, | ||
PhysicalProcessorCount = processorCount > 0 ? processorCount : null, | ||
PhysicalCoreCount = physicalCoreCount > 0 ? physicalCoreCount : null, | ||
LogicalCoreCount = logicalCoreCount > 0 ? logicalCoreCount : null, | ||
NominalFrequencyHz = nominalFrequencyActual?.Hertz.RoundToLong(), | ||
MaxFrequencyHz = maxFrequencyActual?.Hertz.RoundToLong() | ||
}; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
src/BenchmarkDotNet/Detectors/Cpu/Windows/WindowsCpuDetector.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
namespace BenchmarkDotNet.Detectors.Cpu.Windows; | ||
|
||
internal class WindowsCpuDetector() : CpuDetector(new MosCpuDetector(), new WmicCpuDetector()); | ||
internal class WindowsCpuDetector() : CpuDetector(new MosCpuDetector(), new PowershellWmiCpuDetector(), | ||
new WmicCpuDetector()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.