Skip to content

Commit d67609f

Browse files
committed
addressing PR feedback, #736
1 parent 773c47f commit d67609f

File tree

9 files changed

+45
-11
lines changed

9 files changed

+45
-11
lines changed

samples/BenchmarkDotNet.Samples/Intro/IntroInProcessWrongEnv.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using BenchmarkDotNet.Environments;
66
using BenchmarkDotNet.Jobs;
77
using BenchmarkDotNet.Order;
8+
using BenchmarkDotNet.Portability;
89
using BenchmarkDotNet.Toolchains.InProcess;
910

1011
namespace BenchmarkDotNet.Samples.Intro
@@ -18,7 +19,7 @@ private class Config : ManualConfig
1819
{
1920
public Config()
2021
{
21-
var wrongPlatform = IntPtr.Size == sizeof(int)
22+
var wrongPlatform = RuntimeInformation.GetCurrentPlatform() == Platform.X86
2223
? Platform.X64
2324
: Platform.X86;
2425

src/BenchmarkDotNet/Diagnosers/WindowsDisassembler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static bool Is64Bit(Process process)
156156
return !isWow64;
157157
}
158158

159-
return IntPtr.Size == 8; // todo: find the way to cover all scenarios for .NET Core
159+
return Portability.RuntimeInformation.GetCurrentPlatform() == Platform.X64; // todo: find the way to cover all scenarios for .NET Core
160160
}
161161

162162
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]

src/BenchmarkDotNet/Engines/EngineFactory.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public IEngine CreateReadyToRun(EngineParameters engineParameters)
4141
}
4242

4343
var needsPilot = !engineParameters.TargetJob.HasValue(RunMode.InvocationCountCharacteristic);
44-
if (needsPilot)
44+
var hasUnrollFactorDefined = engineParameters.TargetJob.HasValue(RunMode.UnrollFactorCharacteristic);
45+
46+
if (needsPilot && !hasUnrollFactorDefined)
4547
{
4648
var singleActionEngine = CreateEngine(engineParameters, resolver, engineParameters.TargetJob, engineParameters.IdleSingleAction, engineParameters.MainSingleAction);
4749

@@ -70,9 +72,13 @@ private static bool ShouldExecuteOncePerIteration(Measurement jit, TimeInterval
7072

7173
private static Measurement Jit(Engine engine, int unrollFactor)
7274
{
73-
DeadCodeEliminationHelper.KeepAliveWithoutBoxing(engine.RunIteration(new IterationData(IterationMode.IdleJit, index: -1, invokeCount: unrollFactor, unrollFactor: unrollFactor))); // don't forget to JIT idle
75+
DeadCodeEliminationHelper.KeepAliveWithoutBoxing(engine.RunIteration(new IterationData(IterationMode.IdleJitting, index: 1, invokeCount: unrollFactor, unrollFactor: unrollFactor))); // don't forget to JIT idle
76+
77+
var result = engine.RunIteration(new IterationData(IterationMode.MainJitting, index: 1, invokeCount: unrollFactor, unrollFactor: unrollFactor));
78+
79+
engine.WriteLine();
7480

75-
return engine.RunIteration(new IterationData(IterationMode.Jit, index: -1, invokeCount: unrollFactor, unrollFactor: unrollFactor));
81+
return result;
7682
}
7783

7884
private static Engine CreateEngine(EngineParameters engineParameters, IResolver resolver, Job job, Action<long> idle, Action<long> main)

src/BenchmarkDotNet/Engines/IterationMode.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ public enum IterationMode
4040
/// <summary>
4141
/// executing benchmark for the purpose of JIT wamup
4242
/// </summary>
43-
Jit, IdleJit
43+
MainJitting, IdleJitting
4444
}
4545
}

src/BenchmarkDotNet/Engines/IterationModeExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
public static class IterationModeExtensions
44
{
55
public static bool IsIdle(this IterationMode mode)
6-
=> mode == IterationMode.IdleWarmup || mode == IterationMode.IdleTarget || mode == IterationMode.IdleJit;
6+
=> mode == IterationMode.IdleWarmup || mode == IterationMode.IdleTarget || mode == IterationMode.IdleJitting;
77
}
88
}

src/BenchmarkDotNet/Extensions/ProcessExtensions.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using BenchmarkDotNet.Environments;
55
using BenchmarkDotNet.Jobs;
66
using BenchmarkDotNet.Loggers;
7+
using BenchmarkDotNet.Portability;
78
using BenchmarkDotNet.Running;
89
using JetBrains.Annotations;
910

@@ -31,7 +32,7 @@ private static IntPtr FixAffinity(IntPtr processorAffinity)
3132
{
3233
int cpuMask = (1 << Environment.ProcessorCount) - 1;
3334

34-
return IntPtr.Size == sizeof(Int64)
35+
return RuntimeInformation.GetCurrentPlatform() == Platform.X64
3536
? new IntPtr(processorAffinity.ToInt64() & cpuMask)
3637
: new IntPtr(processorAffinity.ToInt32() & cpuMask);
3738
}

src/BenchmarkDotNet/Portability/RuntimeInformation.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal static class RuntimeInformation
6161

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

64-
internal static string GetArchitecture() => IntPtr.Size == 4 ? "32bit" : "64bit";
64+
internal static string GetArchitecture() => GetCurrentPlatform() == Platform.X86 ? "32bit" : "64bit";
6565

6666
internal static bool IsWindows()
6767
{
@@ -246,7 +246,7 @@ internal static bool HasRyuJit()
246246
if (IsNetCore)
247247
return true;
248248

249-
return IntPtr.Size == 8
249+
return GetCurrentPlatform() == Platform.X64
250250
&& GetConfiguration() != DebugConfigurationName
251251
&& !new JitHelper().IsMsX64();
252252
}

tests/BenchmarkDotNet.IntegrationTests/CoreRtTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public CoreRtTests(ITestOutputHelper outputHelper) : base(outputHelper) { }
1717
[Fact]
1818
public void CoreRtIsSupported()
1919
{
20-
if (IntPtr.Size == sizeof(int)) // CoreRT does not support 32bit yet
20+
if (RuntimeInformation.GetCurrentPlatform() == Platform.X86) // CoreRT does not support 32bit yet
2121
return;
2222

2323
var config = ManualConfig.CreateEmpty()

tests/BenchmarkDotNet.Tests/Engine/EngineFactoryTests.cs

+26
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ public void ForJobsThatDontRequireJittingOnlyGlobalSetupIsCalled()
7171

7272
Assert.Equal(1, timesGlobalCleanupCalled);
7373
}
74+
75+
[Fact]
76+
public void ForJobsWithExplicitUnrollFactorTheGlobalSetupIsCalledAndMultiActionCodeGetsJitted()
77+
=> AssertGlobalSetupWasCalledAndMultiActionGotJitted(Job.Default.WithUnrollFactor(16));
78+
79+
[Fact]
80+
public void ForJobsThatDontRequirePilotTheGlobalSetupIsCalledAndMultiActionCodeGetsJitted()
81+
=> AssertGlobalSetupWasCalledAndMultiActionGotJitted(Job.Default.WithInvocationCount(100));
82+
83+
public void AssertGlobalSetupWasCalledAndMultiActionGotJitted(Job job)
84+
{
85+
var engineParameters = CreateEngineParameters(mainSingleAction: Throwing, mainMultiAction: Instant16, job: job);
86+
87+
var engine = new EngineFactory().CreateReadyToRun(engineParameters);
88+
89+
Assert.Equal(1, timesGlobalSetupCalled);
90+
Assert.Equal(2, timesIterationSetupCalled);
91+
Assert.Equal(16, timesBenchmarkCalled);
92+
Assert.Equal(16, timesIdleCalled);
93+
Assert.Equal(2, timesIterationCleanupCalled);
94+
Assert.Equal(0, timesGlobalCleanupCalled);
95+
96+
engine.Dispose();
97+
98+
Assert.Equal(1, timesGlobalCleanupCalled);
99+
}
74100

75101
[Fact]
76102
public void NonVeryTimeConsumingBenchmarksAreExecutedMoreThanOncePerIterationWithUnrollFactorForDefaultSettings()

0 commit comments

Comments
 (0)