Skip to content

Commit

Permalink
Fix CPU detection on Windows when wmic is not available via PATH, fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed Jul 4, 2023
1 parent a6edfe6 commit 144f5c5
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/BenchmarkDotNet/Portability/Cpu/WmicCpuInfoProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.IO;
using BenchmarkDotNet.Helpers;
using JetBrains.Annotations;

namespace BenchmarkDotNet.Portability.Cpu
{
Expand All @@ -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;
}
}
}
}

0 comments on commit 144f5c5

Please sign in to comment.