-
-
Notifications
You must be signed in to change notification settings - Fork 964
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CpuInfo detection, fix #2577
- Loading branch information
1 parent
d2f73e8
commit 64b3d85
Showing
33 changed files
with
854 additions
and
815 deletions.
There are no files selected for viewing
This file contains 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
18 changes: 18 additions & 0 deletions
18
src/BenchmarkDotNet/Portability/Cpu/CompositeCpuInfoDetector.cs
This file contains 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,18 @@ | ||
using System.Linq; | ||
using BenchmarkDotNet.Extensions; | ||
using BenchmarkDotNet.Portability.Cpu.Linux; | ||
using BenchmarkDotNet.Portability.Cpu.macOS; | ||
using BenchmarkDotNet.Portability.Cpu.Windows; | ||
|
||
namespace BenchmarkDotNet.Portability.Cpu; | ||
|
||
internal class CompositeCpuInfoDetector(params ICpuInfoDetector[] detectors) : ICpuInfoDetector | ||
{ | ||
public bool IsApplicable() => detectors.Any(loader => loader.IsApplicable()); | ||
|
||
public CpuInfo? Detect() => detectors | ||
.Where(loader => loader.IsApplicable()) | ||
.Select(loader => loader.Detect()) | ||
.WhereNotNull() | ||
.FirstOrDefault(); | ||
} |
This file contains 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,37 +1,33 @@ | ||
using Perfolizer.Horology; | ||
using BenchmarkDotNet.Portability.Cpu.Linux; | ||
using BenchmarkDotNet.Portability.Cpu.macOS; | ||
using BenchmarkDotNet.Portability.Cpu.Windows; | ||
using Perfolizer.Horology; | ||
|
||
namespace BenchmarkDotNet.Portability.Cpu | ||
namespace BenchmarkDotNet.Portability.Cpu; | ||
|
||
public class CpuInfo( | ||
string? processorName, | ||
int? physicalProcessorCount, | ||
int? physicalCoreCount, | ||
int? logicalCoreCount, | ||
Frequency? nominalFrequency, | ||
Frequency? maxFrequency = null) | ||
{ | ||
public class CpuInfo | ||
{ | ||
public string ProcessorName { get; } | ||
public int? PhysicalProcessorCount { get; } | ||
public int? PhysicalCoreCount { get; } | ||
public int? LogicalCoreCount { get; } | ||
public Frequency? NominalFrequency { get; } | ||
public Frequency? MinFrequency { get; } | ||
public Frequency? MaxFrequency { get; } | ||
public static readonly CpuInfo Empty = new (null, null, null, null, null, null); | ||
public static CpuInfo FromName(string processorName) => new (processorName, null, null, null, null); | ||
public static CpuInfo FromNameAndFrequency(string processorName, Frequency nominalFrequency) => new (processorName, null, null, null, nominalFrequency); | ||
|
||
private static readonly CompositeCpuInfoDetector Detector = new ( | ||
new WindowsCpuInfoDetector(), | ||
new LinuxCpuInfoDetector(), | ||
new MacOsCpuInfoDetector()); | ||
|
||
internal CpuInfo(string processorName, Frequency? nominalFrequency) | ||
: this(processorName, null, null, null, nominalFrequency, null, null) | ||
{ | ||
} | ||
public static CpuInfo? DetectCurrent() => Detector.Detect(); | ||
|
||
public CpuInfo(string processorName, | ||
int? physicalProcessorCount, | ||
int? physicalCoreCount, | ||
int? logicalCoreCount, | ||
Frequency? nominalFrequency, | ||
Frequency? minFrequency, | ||
Frequency? maxFrequency) | ||
{ | ||
ProcessorName = processorName; | ||
PhysicalProcessorCount = physicalProcessorCount; | ||
PhysicalCoreCount = physicalCoreCount; | ||
LogicalCoreCount = logicalCoreCount; | ||
NominalFrequency = nominalFrequency; | ||
MinFrequency = minFrequency; | ||
MaxFrequency = maxFrequency; | ||
} | ||
} | ||
public string? ProcessorName { get; } = processorName; | ||
public int? PhysicalProcessorCount { get; } = physicalProcessorCount; | ||
public int? PhysicalCoreCount { get; } = physicalCoreCount; | ||
public int? LogicalCoreCount { get; } = logicalCoreCount; | ||
public Frequency? NominalFrequency { get; } = nominalFrequency ?? maxFrequency; | ||
public Frequency? MaxFrequency { get; } = maxFrequency ?? nominalFrequency; | ||
} |
Oops, something went wrong.