Skip to content

Commit

Permalink
Sleep thread to account for tiered jit in older Core runtimes.
Browse files Browse the repository at this point in the history
  • Loading branch information
timcassell committed Apr 22, 2024
1 parent 02f1381 commit 3518d50
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/BenchmarkDotNet/Engines/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using BenchmarkDotNet.Characteristics;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Portability;
Expand Down Expand Up @@ -214,15 +215,24 @@ private ClockSpan Measure(Action<long> action, long invokeCount)

private (GcStats, ThreadingStats, double) GetExtraStats(IterationData data)
{
// Warm up the GetAllocatedBytes function before starting the actual measurement.
// Warm up the measurement functions before starting the actual measurement.
DeadCodeEliminationHelper.KeepAliveWithoutBoxing(GcStats.ReadInitial());
DeadCodeEliminationHelper.KeepAliveWithoutBoxing(GcStats.ReadFinal());

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
var exceptionsStats = new ExceptionsStats(); // allocates
exceptionsStats.StartListening(); // this method might allocate

if (RuntimeInformation.IsNetCore && Environment.Version.Major < 7)
{
// We put the current thread to sleep so tiered jit can kick in, compile it's stuff
// and NOT allocate anything on the background thread when we are measuring allocations.
// This is only an issue on net6 and older.
Thread.Sleep(TimeSpan.FromMilliseconds(500));
}

// GC collect before measuring allocations, as we do not collect during the measurement.
ForceGcCollect();
var gcStats = MeasureWithGc(data.InvokeCount / data.UnrollFactor);
Expand Down

0 comments on commit 3518d50

Please sign in to comment.