Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory diagnoser fix for Tiered Compilation #1543

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/BenchmarkDotNet/Engines/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ public Measurement RunIteration(IterationData data)
// it does not matter, because we have already obtained the results!
EnableMonitoring();

if (RuntimeInformation.IsNetCore)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this problem is relevant only for .NET Core 3.x, could we add an additional check?

{
// we put the current thread to sleep so Tiered Compiler can kick in, compile it's stuff
// and NOT allocate anything on the background thread when we are measuring allocations
System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(250));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make it 500 (just to be extra sure).

}

IterationSetupAction(); // we run iteration setup first, so even if it allocates, it is not included in the results

var initialThreadingStats = ThreadingStats.ReadInitial(); // this method might allocate
Expand Down
6 changes: 3 additions & 3 deletions src/BenchmarkDotNet/Portability/RuntimeInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ internal static class RuntimeInformation

public static bool IsMono { get; } = Type.GetType("Mono.Runtime") != null; // it allocates a lot of memory, we need to check it once in order to keep Engine non-allocating!

public static bool IsFullFramework => FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase);
public static bool IsFullFramework { get; } = FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase);

[PublicAPI]
public static bool IsNetNative => FrameworkDescription.StartsWith(".NET Native", StringComparison.OrdinalIgnoreCase);

public static bool IsNetCore
=> ((Environment.Version.Major >= 5) || FrameworkDescription.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase))
public static bool IsNetCore { get; }
= ((Environment.Version.Major >= 5) || FrameworkDescription.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase))
&& !string.IsNullOrEmpty(typeof(object).Assembly.Location);

/// <summary>
Expand Down
31 changes: 27 additions & 4 deletions tests/BenchmarkDotNet.IntegrationTests/MemoryDiagnoserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ public void MemoryDiagnoserDoesNotIncludeAllocationsFromSetupAndCleanup(IToolcha
public class NoAllocationsAtAll
{
[Benchmark] public void EmptyMethod() { }

[Benchmark] public ulong TimeConsuming()
{
var r = 1ul;
for (var i = 0; i < 50_000_000; i++)
{
r /= 1;
}
return r;
}
}

[Theory, MemberData(nameof(GetToolchains))]
Expand All @@ -117,6 +127,19 @@ public void EngineShouldNotInterfereAllocationResults(IToolchain toolchain)
});
}

[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void TieredJitShouldNotInterfereAllocationResults(IToolchain toolchain)
{
// see https://github.com/dotnet/BenchmarkDotNet/issues/1542 for details

AssertAllocations(toolchain, typeof(NoAllocationsAtAll), new Dictionary<string, long>
{
{ nameof(NoAllocationsAtAll.TimeConsuming), 0 }
},
iterationCount: 10); // 1 iteration is not enough to repro the problem
}

public class NoBoxing
{
[Benchmark] public ValueTuple<int> ReturnsValueType() => new ValueTuple<int>(0);
Expand Down Expand Up @@ -255,9 +278,9 @@ public void MemoryDiagnoserIsAccurateForMultiThreadedBenchmarks(IToolchain toolc
});
}

private void AssertAllocations(IToolchain toolchain, Type benchmarkType, Dictionary<string, long> benchmarksAllocationsValidators)
private void AssertAllocations(IToolchain toolchain, Type benchmarkType, Dictionary<string, long> benchmarksAllocationsValidators, int iterationCount = 1)
{
var config = CreateConfig(toolchain);
var config = CreateConfig(toolchain, iterationCount);
var benchmarks = BenchmarkConverter.TypeToBenchmarks(benchmarkType, config);

var summary = BenchmarkRunner.Run(benchmarks);
Expand Down Expand Up @@ -285,12 +308,12 @@ private void AssertAllocations(IToolchain toolchain, Type benchmarkType, Diction
}
}

private IConfig CreateConfig(IToolchain toolchain)
private IConfig CreateConfig(IToolchain toolchain, int iterationCount = 1) // single iteration is enough for most of the tests
=> ManualConfig.CreateEmpty()
.AddJob(Job.ShortRun
.WithEvaluateOverhead(false) // no need to run idle for this test
.WithWarmupCount(0) // don't run warmup to save some time for our CI runs
.WithIterationCount(1) // single iteration is enough for us
.WithIterationCount(iterationCount)
.WithGcForce(false)
.WithToolchain(toolchain))
.AddColumnProvider(DefaultColumnProviders.Instance)
Expand Down