|
3 | 3 | using System.Globalization;
|
4 | 4 | using System.Linq;
|
5 | 5 | using System.Runtime.CompilerServices;
|
| 6 | +using System.Threading; |
6 | 7 | using BenchmarkDotNet.Characteristics;
|
7 | 8 | using BenchmarkDotNet.Jobs;
|
8 | 9 | using BenchmarkDotNet.Portability;
|
@@ -214,15 +215,26 @@ private ClockSpan Measure(Action<long> action, long invokeCount)
|
214 | 215 |
|
215 | 216 | private (GcStats, ThreadingStats, double) GetExtraStats(IterationData data)
|
216 | 217 | {
|
217 |
| - // Warm up the GetAllocatedBytes function before starting the actual measurement. |
| 218 | + // Warm up the measurement functions before starting the actual measurement. |
218 | 219 | DeadCodeEliminationHelper.KeepAliveWithoutBoxing(GcStats.ReadInitial());
|
| 220 | + DeadCodeEliminationHelper.KeepAliveWithoutBoxing(GcStats.ReadFinal()); |
219 | 221 |
|
220 | 222 | IterationSetupAction(); // we run iteration setup first, so even if it allocates, it is not included in the results
|
221 | 223 |
|
222 | 224 | var initialThreadingStats = ThreadingStats.ReadInitial(); // this method might allocate
|
223 | 225 | var exceptionsStats = new ExceptionsStats(); // allocates
|
224 | 226 | exceptionsStats.StartListening(); // this method might allocate
|
225 | 227 |
|
| 228 | + if (RuntimeInformation.IsNetCore && Environment.Version.Major is >= 3 and <= 6 && Environment.GetEnvironmentVariable("COMPlus_TieredCompilation") != "0") |
| 229 | + { |
| 230 | + // #1542 |
| 231 | + // We put the current thread to sleep so tiered jit can kick in, compile it's stuff |
| 232 | + // and NOT allocate anything on the background thread when we are measuring allocations. |
| 233 | + // This is only an issue on netcoreapp3.0 to net6.0. Tiered jit allocations were fixed in net7.0, |
| 234 | + // and netcoreapp2.X uses only GetAllocatedBytesForCurrentThread which doesn't capture the tiered jit allocations. |
| 235 | + Thread.Sleep(TimeSpan.FromMilliseconds(500)); |
| 236 | + } |
| 237 | + |
226 | 238 | // GC collect before measuring allocations, as we do not collect during the measurement.
|
227 | 239 | ForceGcCollect();
|
228 | 240 | var gcStats = MeasureWithGc(data.InvokeCount / data.UnrollFactor);
|
|
0 commit comments