Fix CPU detection on Windows when wmic is not available via PATH, fix #2271

This commit is contained in:
Andrey Akinshin 2023-07-04 20:02:51 +02:00
Родитель a6edfe6c5b
Коммит 144f5c5552
1 изменённых файлов: 9 добавлений и 5 удалений

Просмотреть файл

@ -1,6 +1,6 @@
using System;
using System.IO;
using BenchmarkDotNet.Helpers;
using JetBrains.Annotations;
namespace BenchmarkDotNet.Portability.Cpu
{
@ -10,17 +10,21 @@ namespace BenchmarkDotNet.Portability.Cpu
/// </summary>
internal static class WmicCpuInfoProvider
{
internal static readonly Lazy<CpuInfo> WmicCpuInfo = new Lazy<CpuInfo>(Load);
internal static readonly Lazy<CpuInfo> WmicCpuInfo = new (Load);
private const string DefaultWmicPath = @"C:\Windows\System32\wbem\WMIC.exe";
private static CpuInfo? Load()
{
if (RuntimeInformation.IsWindows())
{
string argList = $"{WmicCpuInfoKeyNames.Name}, {WmicCpuInfoKeyNames.NumberOfCores}, {WmicCpuInfoKeyNames.NumberOfLogicalProcessors}, {WmicCpuInfoKeyNames.MaxClockSpeed}";
string content = ProcessHelper.RunAndReadOutput("wmic", $"cpu get {argList} /Format:List");
const string argList = $"{WmicCpuInfoKeyNames.Name}, {WmicCpuInfoKeyNames.NumberOfCores}, " +
$"{WmicCpuInfoKeyNames.NumberOfLogicalProcessors}, {WmicCpuInfoKeyNames.MaxClockSpeed}";
string wmicPath = File.Exists(DefaultWmicPath) ? DefaultWmicPath : "wmic";
string content = ProcessHelper.RunAndReadOutput(wmicPath, $"cpu get {argList} /Format:List");
return WmicCpuInfoParser.ParseOutput(content);
}
return null;
}
}
}
}