Skip to content

Commit

Permalink
[WIP] Introduce Phd
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed Aug 27, 2024
1 parent a58872b commit 57eb566
Show file tree
Hide file tree
Showing 135 changed files with 12,774 additions and 1,384 deletions.
14 changes: 14 additions & 0 deletions BenchmarkDotNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkDotNet.Exporters.P
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkDotNet.Exporters.Plotting.Tests", "tests\BenchmarkDotNet.Exporters.Plotting.Tests\BenchmarkDotNet.Exporters.Plotting.Tests.csproj", "{199AC83E-30BD-40CD-87CE-0C838AC0320D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Perfolizer", "..\perfolizer\src\Perfolizer\Perfolizer\Perfolizer.csproj", "{CC6E253E-04E1-41CB-AA85-290A60E37677}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Perfolizer.Tests", "..\perfolizer\src\Perfolizer\Perfolizer.Tests\Perfolizer.Tests.csproj", "{71F1801D-D0AD-4A33-8253-B72776761D3A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -161,6 +165,14 @@ Global
{199AC83E-30BD-40CD-87CE-0C838AC0320D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{199AC83E-30BD-40CD-87CE-0C838AC0320D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{199AC83E-30BD-40CD-87CE-0C838AC0320D}.Release|Any CPU.Build.0 = Release|Any CPU
{CC6E253E-04E1-41CB-AA85-290A60E37677}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC6E253E-04E1-41CB-AA85-290A60E37677}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC6E253E-04E1-41CB-AA85-290A60E37677}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC6E253E-04E1-41CB-AA85-290A60E37677}.Release|Any CPU.Build.0 = Release|Any CPU
{71F1801D-D0AD-4A33-8253-B72776761D3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71F1801D-D0AD-4A33-8253-B72776761D3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71F1801D-D0AD-4A33-8253-B72776761D3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71F1801D-D0AD-4A33-8253-B72776761D3A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -190,6 +202,8 @@ Global
{2E2283A3-6DA6-4482-8518-99D6D9F689AB} = {D6597E3A-6892-4A68-8E14-042FC941FDA2}
{B92ECCEF-7C27-4012-9E19-679F3C40A6A6} = {D6597E3A-6892-4A68-8E14-042FC941FDA2}
{199AC83E-30BD-40CD-87CE-0C838AC0320D} = {14195214-591A-45B7-851A-19D3BA2413F9}
{CC6E253E-04E1-41CB-AA85-290A60E37677} = {D6597E3A-6892-4A68-8E14-042FC941FDA2}
{71F1801D-D0AD-4A33-8253-B72776761D3A} = {14195214-591A-45B7-851A-19D3BA2413F9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4D9AF12B-1F7F-45A7-9E8C-E4E46ADCBD1F}
Expand Down
3 changes: 2 additions & 1 deletion src/BenchmarkDotNet.Diagnostics.Windows/HardwareCounters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
Expand Down Expand Up @@ -31,7 +32,7 @@ private static readonly Dictionary<HardwareCounter, string> EtwTranslations

public static IEnumerable<ValidationError> Validate(ValidationParameters validationParameters, bool mandatory)
{
if (!RuntimeInformation.IsWindows())
if (!OsDetector.IsWindows())
{
yield return new ValidationError(true, "Hardware Counters and EtwProfiler are supported only on Windows");
yield break;
Expand Down
3 changes: 2 additions & 1 deletion src/BenchmarkDotNet.Diagnostics.Windows/JitDiagnoser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Loggers;
Expand Down Expand Up @@ -33,7 +34,7 @@ public void Handle(HostSignal signal, DiagnoserActionParameters parameters)

public IEnumerable<ValidationError> Validate(ValidationParameters validationParameters)
{
if (!RuntimeInformation.IsWindows())
if (!OsDetector.IsWindows())
{
yield return new ValidationError(true, $"{GetType().Name} is supported only on Windows");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using System.Collections.Immutable;
using System.Linq;
using BenchmarkDotNet.Analysers;
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Validators;
Expand Down Expand Up @@ -124,10 +124,10 @@ internal static bool IsSupported(RuntimeMoniker runtimeMoniker)
case RuntimeMoniker.NetCoreApp20:
case RuntimeMoniker.NetCoreApp21:
case RuntimeMoniker.NetCoreApp22:
return RuntimeInformation.IsWindows();
return OsDetector.IsWindows();
case RuntimeMoniker.NetCoreApp30:
case RuntimeMoniker.NetCoreApp31:
return RuntimeInformation.IsWindows() || RuntimeInformation.IsLinux();
return OsDetector.IsWindows() || OsDetector.IsLinux();
default:
throw new ArgumentOutOfRangeException(nameof(runtimeMoniker), runtimeMoniker, $"Runtime moniker {runtimeMoniker} is not supported");
}
Expand Down
6 changes: 3 additions & 3 deletions src/BenchmarkDotNet.Diagnostics.dotTrace/DotTraceDiagnoser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using System.Collections.Immutable;
using System.Linq;
using BenchmarkDotNet.Analysers;
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains;
Expand Down Expand Up @@ -118,10 +118,10 @@ internal static bool IsSupported(RuntimeMoniker runtimeMoniker)
case RuntimeMoniker.NetCoreApp20:
case RuntimeMoniker.NetCoreApp21:
case RuntimeMoniker.NetCoreApp22:
return RuntimeInformation.IsWindows();
return OsDetector.IsWindows();
case RuntimeMoniker.NetCoreApp30:
case RuntimeMoniker.NetCoreApp31:
return RuntimeInformation.IsWindows() || RuntimeInformation.IsLinux();
return OsDetector.IsWindows() || OsDetector.IsLinux();
default:
throw new ArgumentOutOfRangeException(nameof(runtimeMoniker), runtimeMoniker, $"Runtime moniker {runtimeMoniker} is not supported");
}
Expand Down
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Analysers/ZeroMeasurementAnalyser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private ZeroMeasurementAnalyser() { }

protected override IEnumerable<Conclusion> AnalyseReport(BenchmarkReport report, Summary summary)
{
var currentFrequency = summary.HostEnvironmentInfo.CpuInfo.Value.MaxFrequency;
var currentFrequency = summary.HostEnvironmentInfo.Cpu.Value.MaxFrequency();
if (!currentFrequency.HasValue || currentFrequency <= 0)
currentFrequency = FallbackCpuResolutionValue.ToFrequency();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;
using BenchmarkDotNet.Exporters;

namespace BenchmarkDotNet.Attributes;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
public class PhdExporterAttribute() : ExporterConfigBaseAttribute(new PhdJsonExporter());
4 changes: 2 additions & 2 deletions src/BenchmarkDotNet/BenchmarkDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Templates\*" Exclude="bin\**;obj\**;**\*.xproj;packages\**;@(EmbeddedResource)" />
<EmbeddedResource Include="Environments\microarchitectures.txt" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Gee.External.Capstone" Version="2.3.0" />
<PackageReference Include="Iced" Version="1.17.0" />
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="2.2.332302" />
<PackageReference Include="Perfolizer" Version="[0.3.17]" />
<!-- <PackageReference Include="Perfolizer" Version="[0.3.17]" />-->
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.1.8" PrivateAssets="contentfiles;analyzers" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
<!-- Do not update these packages, or else netcoreapp3.0 and older will no longer work -->
Expand All @@ -47,6 +46,7 @@
<EmbeddedResource Include="Disassemblers\net462\win7-x64\Iced.dll" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\perfolizer\src\Perfolizer\Perfolizer\Perfolizer.csproj" />
<ProjectReference Include="..\BenchmarkDotNet.Annotations\BenchmarkDotNet.Annotations.csproj" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETStandard' ">
Expand Down
3 changes: 2 additions & 1 deletion src/BenchmarkDotNet/Configs/DefaultConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using BenchmarkDotNet.Analysers;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.EventProcessors;
using BenchmarkDotNet.Exporters;
Expand Down Expand Up @@ -90,7 +91,7 @@ public string ArtifactsPath
{
get
{
var root = RuntimeInformation.IsAndroid() ?
var root = OsDetector.IsAndroid() ?
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) :
Directory.GetCurrentDirectory();
return Path.Combine(root, "BenchmarkDotNet.Artifacts");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using BenchmarkDotNet.Environments;
using System.Text;

using BenchmarkDotNet.Environments;
#if NET6_0_OR_GREATER
using System.Runtime.Intrinsics.X86;
using System.Runtime.Intrinsics.Arm;
#endif

namespace BenchmarkDotNet.Portability.Cpu
namespace BenchmarkDotNet.Detectors.Cpu
{
internal static class HardwareIntrinsics
{
Expand Down
12 changes: 12 additions & 0 deletions src/BenchmarkDotNet/Detectors/Cpu/ICpuDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Perfolizer.Phd.Dto;

namespace BenchmarkDotNet.Detectors.Cpu;

/// <summary>
/// Loads the <see cref="PhdCpu"/> for the current hardware
/// </summary>
public interface ICpuDetector
{
bool IsApplicable();
PhdCpu? Detect();
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using System.Collections.Generic;
using BenchmarkDotNet.Helpers;
using BenchmarkDotNet.Portability;
using Perfolizer.Phd.Dto;

namespace BenchmarkDotNet.Portability.Cpu.Linux;
namespace BenchmarkDotNet.Detectors.Cpu.Linux;

/// <summary>
/// CPU information from output of the `cat /proc/cpuinfo` and `lscpu` command.
/// Linux only.
/// </summary>
internal class LinuxCpuInfoDetector : ICpuInfoDetector
internal class LinuxCpuDetector : ICpuDetector
{
public bool IsApplicable() => RuntimeInformation.IsLinux();
public bool IsApplicable() => OsDetector.IsLinux();

public CpuInfo? Detect()
public PhdCpu? Detect()
{
if (!IsApplicable()) return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Text.RegularExpressions;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Helpers;
using BenchmarkDotNet.Portability;
using Perfolizer.Horology;
using Perfolizer.Phd.Dto;

namespace BenchmarkDotNet.Portability.Cpu.Linux;
namespace BenchmarkDotNet.Detectors.Cpu.Linux;

internal static class LinuxCpuInfoParser
{
Expand All @@ -26,7 +28,7 @@ private static class Lscpu

/// <param name="cpuInfo">Output of `cat /proc/cpuinfo`</param>
/// <param name="lscpu">Output of `lscpu`</param>
internal static CpuInfo Parse(string? cpuInfo, string? lscpu)
internal static PhdCpu Parse(string? cpuInfo, string? lscpu)
{
var processorModelNames = new HashSet<string>();
var processorsToPhysicalCoreCount = new Dictionary<string, int>();
Expand Down Expand Up @@ -87,13 +89,15 @@ internal static CpuInfo Parse(string? cpuInfo, string? lscpu)
string processorName = processorModelNames.Count > 0 ? string.Join(", ", processorModelNames) : null;
int? physicalProcessorCount = processorsToPhysicalCoreCount.Count > 0 ? processorsToPhysicalCoreCount.Count : null;
int? physicalCoreCount = processorsToPhysicalCoreCount.Count > 0 ? processorsToPhysicalCoreCount.Values.Sum() : coresPerSocket;
return new CpuInfo(
processorName,
physicalProcessorCount,
physicalCoreCount,
logicalCoreCount > 0 ? logicalCoreCount : null,
nominalFrequency,
maxFrequency);
return new PhdCpu
{
ProcessorName = processorName,
PhysicalProcessorCount = physicalProcessorCount,
PhysicalCoreCount = physicalCoreCount,
LogicalCoreCount = logicalCoreCount > 0 ? logicalCoreCount : null,
NominalFrequencyHz = nominalFrequency?.Hertz.RoundToLong(),
MaxFrequencyHz = maxFrequency?.Hertz.RoundToLong()
};
}

internal static Frequency? ParseFrequencyFromBrandString(string brandString)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Portability;
using Perfolizer.Horology;
using Perfolizer.Phd.Dto;

namespace BenchmarkDotNet.Portability.Cpu.Windows;
namespace BenchmarkDotNet.Detectors.Cpu.Windows;

internal class MosCpuInfoDetector : ICpuInfoDetector
internal class MosCpuDetector : ICpuDetector
{
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public bool IsApplicable() => RuntimeInformation.IsWindows() &&
public bool IsApplicable() => OsDetector.IsWindows() &&
RuntimeInformation.IsFullFramework &&
!RuntimeInformation.IsMono;

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public CpuInfo? Detect()
public PhdCpu? Detect()
{
if (!IsApplicable()) return null;

Expand Down Expand Up @@ -49,11 +51,14 @@ public bool IsApplicable() => RuntimeInformation.IsWindows() &&
? Frequency.FromMHz(sumMaxFrequency * 1.0 / processorsCount)
: null;

return new CpuInfo(
processorName,
GetCount(processorsCount), GetCount(physicalCoreCount), GetCount(logicalCoreCount),
maxFrequency, maxFrequency);

int? GetCount(int count) => count > 0 ? count : null;
return new PhdCpu
{
ProcessorName = processorName,
PhysicalProcessorCount = processorsCount > 0 ? processorsCount : null,
PhysicalCoreCount = physicalCoreCount > 0 ? physicalCoreCount : null,
LogicalCoreCount = logicalCoreCount > 0 ? logicalCoreCount : null,
NominalFrequencyHz = maxFrequency?.Hertz.RoundToLong(),
MaxFrequencyHz = maxFrequency?.Hertz.RoundToLong()
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace BenchmarkDotNet.Detectors.Cpu.Windows;

internal class WindowsCpuDetector() : CpuDetector(new MosCpuDetector(), new WmicCpuDetector());
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using System.IO;
using BenchmarkDotNet.Helpers;
using BenchmarkDotNet.Portability;
using Perfolizer.Phd.Dto;

namespace BenchmarkDotNet.Portability.Cpu.Windows;
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 WmicCpuInfoDetector : ICpuInfoDetector
internal class WmicCpuDetector : ICpuDetector
{
private const string DefaultWmicPath = @"C:\Windows\System32\wbem\WMIC.exe";

public bool IsApplicable() => RuntimeInformation.IsWindows();
public bool IsApplicable() => OsDetector.IsWindows();

public CpuInfo? Detect()
public PhdCpu? Detect()
{
if (!IsApplicable()) return null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BenchmarkDotNet.Portability.Cpu.Windows;
namespace BenchmarkDotNet.Detectors.Cpu.Windows;

internal static class WmicCpuInfoKeyNames
{
Expand Down
Loading

0 comments on commit 57eb566

Please sign in to comment.