Skip to content

Commit

Permalink
ARM support (#979)
Browse files Browse the repository at this point in the history
* add ARM platform support

* fix NRE on ARM

* remove empty new line

* proper fix for the NRE
  • Loading branch information
adamsitnik committed Nov 29, 2018
1 parent 847c270 commit 6fb8309
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion samples/BenchmarkDotNet.Samples/IntroInProcessWrongEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private class Config : ManualConfig
{
public Config()
{
var wrongPlatform = RuntimeInformation.GetCurrentPlatform() == Platform.X86
var wrongPlatform = RuntimeInformation.Is64BitPlatform()
? Platform.X64
: Platform.X86;

Expand Down
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Diagnosers/WindowsDisassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static bool Is64Bit(Process process)
return !isWow64;
}

return RuntimeInformation.GetCurrentPlatform() == Platform.X64; // todo: find the way to cover all scenarios for .NET Core
return RuntimeInformation.Is64BitPlatform(); // todo: find the way to cover all scenarios for .NET Core
}

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
Expand Down
12 changes: 11 additions & 1 deletion src/BenchmarkDotNet/Environments/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public enum Platform
/// <summary>
/// x64
/// </summary>
X64
X64,

/// <summary>
/// ARM
/// </summary>
Arm,

/// <summary>
/// ARM64
/// </summary>
Arm64
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public static class ProcessorBrandStringHelper
[NotNull]
public static string Prettify(CpuInfo cpuInfo, bool includeMaxFrequency = false)
{
if (cpuInfo == null)
if (cpuInfo == null || string.IsNullOrEmpty(cpuInfo.ProcessorName))
{
return "";
return "Unknown processor";
}

// Remove parts which don't provide any useful information for user
var processorName = cpuInfo.ProcessorName.Replace("@", "").Replace("(R)", "").Replace("(TM)", "");

// If we have found physical core(s), we can safely assume we can drop extra info from brand
if (cpuInfo.PhysicalCoreCount > 0)
if (cpuInfo.PhysicalCoreCount.HasValue && cpuInfo.PhysicalCoreCount.Value > 0)
processorName = Regex.Replace(processorName, @"(\w+?-Core Processor)", "").Trim();

string frequencyString = GetBrandStyledActualFrequency(cpuInfo.NominalFrequency);
Expand Down
4 changes: 4 additions & 0 deletions src/BenchmarkDotNet/Extensions/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public static string ToConfig(this Platform platform)
return "x86";
case Platform.X64:
return "x64";
case Platform.Arm:
return "ARM";
case Platform.Arm64:
return "ARM64";
default:
return "AnyCPU";
}
Expand Down
4 changes: 2 additions & 2 deletions src/BenchmarkDotNet/Extensions/ProcessExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void EnsureHighPriority(this Process process, ILogger logger)
}

internal static string ToPresentation(this IntPtr processorAffinity, int processorCount)
=> (RuntimeInformation.GetCurrentPlatform() == Platform.X64
=> (RuntimeInformation.Is64BitPlatform()
? Convert.ToString(processorAffinity.ToInt64(), 2)
: Convert.ToString(processorAffinity.ToInt32(), 2))
.PadLeft(processorCount, '0');
Expand All @@ -43,7 +43,7 @@ private static IntPtr FixAffinity(IntPtr processorAffinity)
{
int cpuMask = (1 << Environment.ProcessorCount) - 1;

return RuntimeInformation.GetCurrentPlatform() == Platform.X64
return RuntimeInformation.Is64BitPlatform()
? new IntPtr(processorAffinity.ToInt64() & cpuMask)
: new IntPtr(processorAffinity.ToInt32() & cpuMask);
}
Expand Down
4 changes: 1 addition & 3 deletions src/BenchmarkDotNet/Portability/Cpu/CpuInfoFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public static string Format(CpuInfo cpuInfo)

var parts = new List<string>
{
!string.IsNullOrWhiteSpace(cpuInfo.ProcessorName)
? ProcessorBrandStringHelper.Prettify(cpuInfo, includeMaxFrequency: true)
: "Unknown processor"
ProcessorBrandStringHelper.Prettify(cpuInfo, includeMaxFrequency: true)
};

if (cpuInfo.PhysicalProcessorCount > 0)
Expand Down
25 changes: 21 additions & 4 deletions src/BenchmarkDotNet/Portability/RuntimeInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal static class RuntimeInformation

internal static string ScriptFileExtension => IsWindows() ? ".bat" : ".sh";

internal static string GetArchitecture() => GetCurrentPlatform() == Platform.X86 ? "32bit" : "64bit";
internal static string GetArchitecture() => Is64BitPlatform() ? "64bit" : "32bit";

private static string DockerSdkVersion => Environment.GetEnvironmentVariable("DOTNET_VERSION");
private static string DockerAspnetSdkVersion => Environment.GetEnvironmentVariable("ASPNETCORE_VERSION");
Expand Down Expand Up @@ -238,7 +238,24 @@ internal static Runtime GetCurrentRuntime()
throw new NotSupportedException("Unknown .NET Framework"); // todo: adam sitnik fix it
}

public static Platform GetCurrentPlatform() => IntPtr.Size == 4 ? Platform.X86 : Platform.X64;
public static Platform GetCurrentPlatform()
{
switch (System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture)
{
case Architecture.Arm:
return Platform.Arm;
case Architecture.Arm64:
return Platform.Arm64;
case Architecture.X64:
return Platform.X64;
case Architecture.X86:
return Platform.X86;
default:
throw new ArgumentOutOfRangeException();
}
}

public static bool Is64BitPlatform() => IntPtr.Size == 8;

private static IEnumerable<JitModule> GetJitModules()
{
Expand All @@ -258,7 +275,7 @@ internal static bool HasRyuJit()
if (IsNetCore)
return true;

return GetCurrentPlatform() == Platform.X64
return Is64BitPlatform()
&& GetConfiguration() != DebugConfigurationName
&& !new JitHelper().IsMsX64();
}
Expand Down Expand Up @@ -420,4 +437,4 @@ internal static VirtualMachineHypervisor GetVirtualMachineHypervisor()
return null;
}
}
}
}
4 changes: 4 additions & 0 deletions src/BenchmarkDotNet/Toolchains/Roslyn/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ private Platform GetPlatform(OurPlatform platform)
return Platform.X86;
case OurPlatform.X64:
return Platform.X64;
case OurPlatform.Arm:
return Platform.Arm;
case OurPlatform.Arm64:
return Platform.Arm64;
default:
throw new ArgumentOutOfRangeException(nameof(platform), platform, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public BuildTimeoutTests(ITestOutputHelper outputHelper) : base(outputHelper) {
[Fact]
public void WhenBuildTakesMoreTimeThanTheTimeoutTheBuildIsCancelled()
{
if (RuntimeInformation.GetCurrentPlatform() == Platform.X86) // CoreRT does not support 32bit yet
if (!RuntimeInformation.Is64BitPlatform()) // CoreRT does not support 32bit yet
return;

// we use CoreRT on purpose because it takes a LOT of time to build it
Expand Down
2 changes: 1 addition & 1 deletion tests/BenchmarkDotNet.IntegrationTests/CoreRtTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public CoreRtTests(ITestOutputHelper outputHelper) : base(outputHelper) { }
[Fact]
public void CoreRtIsSupported()
{
if (RuntimeInformation.GetCurrentPlatform() == Platform.X86) // CoreRT does not support 32bit yet
if (!RuntimeInformation.Is64BitPlatform()) // CoreRT does not support 32bit yet
return;

var config = ManualConfig.CreateEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,15 @@ public void AmdIsPrettifiedWithDiffFrequencies(string originalName, string prett

Assert.Equal(prettifiedName, ProcessorBrandStringHelper.Prettify(cpuInfo, includeMaxFrequency: true));
}

[Theory]
[InlineData("", "Unknown processor")]
[InlineData(null, "Unknown processor")]
public void UnknownProcessorDoesNotThrow(string originalName, string prettifiedName)
{
var cpuInfo = new CpuInfo(originalName, nominalFrequency: null);

Assert.Equal(prettifiedName, ProcessorBrandStringHelper.Prettify(cpuInfo, includeMaxFrequency: true));
}
}
}

0 comments on commit 6fb8309

Please sign in to comment.