Skip to content

Commit 8efb670

Browse files
committed
Stabilize TieredJitShouldNotInterfereAllocationResults test.
1 parent 9f71a07 commit 8efb670

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/BenchmarkDotNet/Engines/Engine.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private unsafe ClockSpan MeasureWithRandomStack(Action<long> action, long invoke
185185
return clockSpan;
186186
}
187187

188-
[MethodImpl(MethodImplOptions.NoInlining)]
188+
[MethodImpl(MethodImplOptions.NoInlining | CodeGenHelper.AggressiveOptimizationOption)]
189189
private unsafe void Consume(byte* _) { }
190190

191191
[MethodImpl(MethodImplOptions.NoInlining | CodeGenHelper.AggressiveOptimizationOption)]
@@ -196,6 +196,7 @@ private ClockSpan Measure(Action<long> action, long invokeCount)
196196
return clock.GetElapsed();
197197
}
198198

199+
[MethodImpl(CodeGenHelper.AggressiveOptimizationOption)]
199200
private (GcStats, ThreadingStats, double) GetExtraStats(IterationData data)
200201
{
201202
// Warm up the measurement functions before starting the actual measurement.
@@ -208,18 +209,13 @@ private ClockSpan Measure(Action<long> action, long invokeCount)
208209
var exceptionsStats = new ExceptionsStats(); // allocates
209210
exceptionsStats.StartListening(); // this method might allocate
210211

211-
#if !NET7_0_OR_GREATER
212-
if (JitInfo.IsTiered && CoreRuntime.TryGetVersion(out var version) && version.Major is >= 3 and <= 6)
212+
if (JitInfo.IsTiered)
213213
{
214214
// #1542
215215
// We put the current thread to sleep so tiered jit can kick in, compile its stuff,
216216
// and NOT allocate anything on the background thread when we are measuring allocations.
217-
// This is only an issue on netcoreapp3.0 to net6.0. Tiered jit allocations were "fixed" in net7.0
218-
// (maybe not completely eliminated forever, but at least reduced to a point where measurements are much more stable),
219-
// and netcoreapp2.X uses only GetAllocatedBytesForCurrentThread which doesn't capture the tiered jit allocations.
220-
Thread.Sleep(TimeSpan.FromMilliseconds(500));
217+
Thread.Sleep(JitInfo.BackgroundCompilationDelay);
221218
}
222-
#endif
223219

224220
// GC collect before measuring allocations.
225221
ForceGcCollect();
@@ -250,6 +246,7 @@ private GcStats MeasureWithGc(Action<long> action, long invokeCount)
250246
return finalGcStats - initialGcStats;
251247
}
252248

249+
[MethodImpl(CodeGenHelper.AggressiveOptimizationOption)]
253250
private void RandomizeManagedHeapMemory()
254251
{
255252
// invoke global cleanup before global setup
@@ -268,6 +265,7 @@ private void RandomizeManagedHeapMemory()
268265
// we don't enforce GC.Collects here as engine does it later anyway
269266
}
270267

268+
[MethodImpl(CodeGenHelper.AggressiveOptimizationOption)]
271269
private void GcCollect()
272270
{
273271
if (!ForceGcCleanups)
@@ -276,6 +274,7 @@ private void GcCollect()
276274
ForceGcCollect();
277275
}
278276

277+
[MethodImpl(CodeGenHelper.AggressiveOptimizationOption)]
279278
internal static void ForceGcCollect()
280279
{
281280
GC.Collect();
@@ -327,6 +326,7 @@ private sealed class Impl
327326
private readonly object hangLock = new();
328327
private readonly ManualResetEventSlim enteredFinalizerEvent = new(false);
329328

329+
[MethodImpl(CodeGenHelper.AggressiveOptimizationOption)]
330330
~Impl()
331331
{
332332
lock (hangLock)
@@ -336,14 +336,15 @@ private sealed class Impl
336336
}
337337
}
338338

339-
[MethodImpl(MethodImplOptions.NoInlining)]
339+
[MethodImpl(MethodImplOptions.NoInlining | CodeGenHelper.AggressiveOptimizationOption)]
340340
internal static (object hangLock, ManualResetEventSlim enteredFinalizerEvent) CreateWeakly()
341341
{
342342
var impl = new Impl();
343343
return (impl.hangLock, impl.enteredFinalizerEvent);
344344
}
345345
}
346346

347+
[MethodImpl(CodeGenHelper.AggressiveOptimizationOption)]
347348
internal static FinalizerBlocker MaybeStart()
348349
{
349350
if (Environment.GetEnvironmentVariable(UnitTestBlockFinalizerEnvKey) != UnitTestBlockFinalizerEnvValue)
@@ -360,6 +361,7 @@ internal static FinalizerBlocker MaybeStart()
360361
return new FinalizerBlocker(hangLock);
361362
}
362363

364+
[MethodImpl(CodeGenHelper.AggressiveOptimizationOption)]
363365
public void Dispose()
364366
{
365367
if (hangLock is not null)

0 commit comments

Comments
 (0)