-
-
Notifications
You must be signed in to change notification settings - Fork 946
/
MemoryDiagnoserTests.cs
executable file
·414 lines (355 loc) · 17 KB
/
MemoryDiagnoserTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.IntegrationTests.Xunit;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Tests.Loggers;
using BenchmarkDotNet.Tests.XUnit;
using BenchmarkDotNet.Toolchains;
using BenchmarkDotNet.Toolchains.NativeAot;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using Xunit;
using Xunit.Abstractions;
using BenchmarkDotNet.Toolchains.Mono;
namespace BenchmarkDotNet.IntegrationTests
{
public class MemoryDiagnoserTests
{
private readonly ITestOutputHelper output;
public MemoryDiagnoserTests(ITestOutputHelper outputHelper) => output = outputHelper;
public static IEnumerable<object[]> GetToolchains()
{
yield return new object[] { Job.Default.GetToolchain() };
// InProcessEmit reports flaky allocations in current .Net 8.
if (!RuntimeInformation.IsNetCore)
{
yield return new object[] { InProcessEmitToolchain.Instance };
}
}
public class AccurateAllocations
{
[Benchmark] public byte[] EightBytesArray() => new byte[8];
[Benchmark] public byte[] SixtyFourBytesArray() => new byte[64];
[Benchmark] public Task<int> AllocateTask() => Task.FromResult<int>(-12345);
}
[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void MemoryDiagnoserIsAccurate(IToolchain toolchain)
{
long objectAllocationOverhead = IntPtr.Size * 2; // pointer to method table + object header word
long arraySizeOverhead = IntPtr.Size; // array length
if (toolchain is MonoToolchain)
{
objectAllocationOverhead += IntPtr.Size;
}
AssertAllocations(toolchain, typeof(AccurateAllocations), new Dictionary<string, long>
{
{ nameof(AccurateAllocations.EightBytesArray), 8 + objectAllocationOverhead + arraySizeOverhead },
{ nameof(AccurateAllocations.SixtyFourBytesArray), 64 + objectAllocationOverhead + arraySizeOverhead },
{ nameof(AccurateAllocations.AllocateTask), CalculateRequiredSpace<Task<int>>() },
});
}
[FactEnvSpecific("We don't want to test NativeAOT twice (for .NET Framework 4.6.2 and .NET 8.0)", EnvRequirement.DotNetCoreOnly)]
public void MemoryDiagnoserSupportsNativeAOT()
{
if (RuntimeInformation.IsMacOS())
return; // currently not supported
MemoryDiagnoserIsAccurate(NativeAotToolchain.Net80);
}
[FactEnvSpecific("We don't want to test MonoVM twice (for .NET Framework 4.6.2 and .NET 8.0)", EnvRequirement.DotNetCoreOnly)]
public void MemoryDiagnoserSupportsModernMono()
{
MemoryDiagnoserIsAccurate(MonoToolchain.Mono80);
}
public class AllocatingGlobalSetupAndCleanup
{
private List<int> list;
[Benchmark] public void AllocateNothing() { }
[IterationSetup]
[GlobalSetup]
public void AllocatingSetUp() => AllocateUntilGcWakesUp();
[IterationCleanup]
[GlobalCleanup]
public void AllocatingCleanUp() => AllocateUntilGcWakesUp();
private void AllocateUntilGcWakesUp()
{
int initialCollectionCount = GC.CollectionCount(0);
while (initialCollectionCount == GC.CollectionCount(0))
list = Enumerable.Range(0, 100).ToList();
}
}
[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void MemoryDiagnoserDoesNotIncludeAllocationsFromSetupAndCleanup(IToolchain toolchain)
{
AssertAllocations(toolchain, typeof(AllocatingGlobalSetupAndCleanup), new Dictionary<string, long>
{
{ nameof(AllocatingGlobalSetupAndCleanup.AllocateNothing), 0 }
});
}
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))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void EngineShouldNotInterfereAllocationResults(IToolchain toolchain)
{
AssertAllocations(toolchain, typeof(NoAllocationsAtAll), new Dictionary<string, long>
{
{ nameof(NoAllocationsAtAll.EmptyMethod), 0 }
});
}
// #1542
[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void TieredJitShouldNotInterfereAllocationResults(IToolchain toolchain)
{
AssertAllocations(toolchain, typeof(NoAllocationsAtAll), new Dictionary<string, long>
{
{ nameof(NoAllocationsAtAll.TimeConsuming), 0 }
},
disableTieredJit: false, iterationCount: 10); // 1 iteration is not enough to repro the problem
}
public class NoBoxing
{
[Benchmark] public ValueTuple<int> ReturnsValueType() => new ValueTuple<int>(0);
}
[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void EngineShouldNotIntroduceBoxing(IToolchain toolchain)
{
AssertAllocations(toolchain, typeof(NoBoxing), new Dictionary<string, long>
{
{ nameof(NoBoxing.ReturnsValueType), 0 }
});
}
public class NonAllocatingAsynchronousBenchmarks
{
private readonly Task<int> completedTaskOfT = Task.FromResult(default(int)); // we store it in the field, because Task<T> is reference type so creating it allocates heap memory
[Benchmark] public Task CompletedTask() => Task.CompletedTask;
[Benchmark] public Task<int> CompletedTaskOfT() => completedTaskOfT;
[Benchmark] public ValueTask<int> CompletedValueTaskOfT() => new ValueTask<int>(default(int));
}
[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void AwaitingTasksShouldNotInterfereAllocationResults(IToolchain toolchain)
{
AssertAllocations(toolchain, typeof(NonAllocatingAsynchronousBenchmarks), new Dictionary<string, long>
{
{ nameof(NonAllocatingAsynchronousBenchmarks.CompletedTask), 0 },
{ nameof(NonAllocatingAsynchronousBenchmarks.CompletedTaskOfT), 0 },
{ nameof(NonAllocatingAsynchronousBenchmarks.CompletedValueTaskOfT), 0 }
});
}
public class WithOperationsPerInvokeBenchmarks
{
[Benchmark(OperationsPerInvoke = 4)]
public void WithOperationsPerInvoke()
{
DoNotInline(new object(), new object());
DoNotInline(new object(), new object());
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void DoNotInline(object left, object right) { }
}
[Theory, MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void AllocatedMemoryShouldBeScaledForOperationsPerInvoke(IToolchain toolchain)
{
long objectAllocationOverhead = IntPtr.Size * 2; // pointer to method table + object header word
AssertAllocations(toolchain, typeof(WithOperationsPerInvokeBenchmarks), new Dictionary<string, long>
{
{ nameof(WithOperationsPerInvokeBenchmarks.WithOperationsPerInvoke), objectAllocationOverhead + IntPtr.Size }
});
}
public class TimeConsuming
{
[Benchmark]
public byte[] SixtyFourBytesArray()
{
// this benchmark should hit allocation quantum problem
// it allocates a little of memory, but it takes a lot of time to execute so we can't run in thousands of times!
Thread.Sleep(TimeSpan.FromSeconds(0.5));
return new byte[64];
}
}
[TheoryEnvSpecific("Full Framework cannot measure precisely enough for low invocation counts.", EnvRequirement.DotNetCoreOnly), MemberData(nameof(GetToolchains),
Skip = "Some random background allocations are occurring that we haven't been able to figure out, causing this test in particular to be flaky." +
" Other tests likely also suffer from it, but their high invocation counts successfully drown it out. #2562")]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void AllocationQuantumIsNotAnIssueForNetCore21Plus(IToolchain toolchain)
{
long objectAllocationOverhead = IntPtr.Size * 2; // pointer to method table + object header word
long arraySizeOverhead = IntPtr.Size; // array length
AssertAllocations(toolchain, typeof(TimeConsuming), new Dictionary<string, long>
{
{ nameof(TimeConsuming.SixtyFourBytesArray), 64 + objectAllocationOverhead + arraySizeOverhead }
});
}
public class MultiThreadedAllocation
{
public const int Size = 1024;
public const int ThreadsCount = 10;
// We cache the threads in GlobalSetup and reuse them for each benchmark invocation
// to avoid measuring the cost of thread start and join, which varies across different runtimes.
private Thread[] threads;
private volatile bool keepRunning = true;
private readonly Barrier barrier = new (ThreadsCount + 1);
private readonly CountdownEvent countdownEvent = new (ThreadsCount);
[GlobalSetup]
public void Setup()
{
threads = Enumerable.Range(0, ThreadsCount)
.Select(_ => new Thread(() =>
{
while (keepRunning)
{
barrier.SignalAndWait();
GC.KeepAlive(new byte[Size]);
countdownEvent.Signal();
}
}))
.ToArray();
foreach (var thread in threads)
{
thread.Start();
}
}
[GlobalCleanup]
public void Cleanup()
{
keepRunning = false;
barrier.SignalAndWait();
foreach (var thread in threads)
{
thread.Join();
}
}
[Benchmark]
public void Allocate()
{
countdownEvent.Reset(ThreadsCount);
barrier.SignalAndWait();
countdownEvent.Wait();
}
}
[TheoryEnvSpecific("Full Framework cannot measure precisely enough", EnvRequirement.DotNetCoreOnly)]
[MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void MemoryDiagnoserIsAccurateForMultiThreadedBenchmarks(IToolchain toolchain)
{
long objectAllocationOverhead = IntPtr.Size * 2; // pointer to method table + object header word
long arraySizeOverhead = IntPtr.Size; // array length
long memoryAllocatedPerArray = (MultiThreadedAllocation.Size + objectAllocationOverhead + arraySizeOverhead);
AssertAllocations(toolchain, typeof(MultiThreadedAllocation), new Dictionary<string, long>
{
{ nameof(MultiThreadedAllocation.Allocate), memoryAllocatedPerArray * MultiThreadedAllocation.ThreadsCount }
});
}
private void AssertAllocations(IToolchain toolchain, Type benchmarkType, Dictionary<string, long> benchmarksAllocationsValidators, bool disableTieredJit = true, int iterationCount = 1)
{
var config = CreateConfig(toolchain, disableTieredJit, iterationCount);
var benchmarks = BenchmarkConverter.TypeToBenchmarks(benchmarkType, config);
var summary = BenchmarkRunner.Run(benchmarks);
try
{
summary.CheckPlatformLinkerIssues();
}
catch (MisconfiguredEnvironmentException e)
{
if (ContinuousIntegration.IsLocalRun())
{
output.WriteLine(e.SkipMessage);
return;
}
throw;
}
foreach (var benchmarkAllocationsValidator in benchmarksAllocationsValidators)
{
var allocatingBenchmarks = benchmarks.BenchmarksCases.Where(benchmark => benchmark.DisplayInfo.Contains(benchmarkAllocationsValidator.Key));
foreach (var benchmark in allocatingBenchmarks)
{
var benchmarkReport = summary.Reports.Single(report => report.BenchmarkCase == benchmark);
Assert.Equal(benchmarkAllocationsValidator.Value, benchmarkReport.GcStats.GetBytesAllocatedPerOperation(benchmark));
if (benchmarkAllocationsValidator.Value == 0)
{
Assert.Equal(0, benchmarkReport.GcStats.GetTotalAllocatedBytes(excludeAllocationQuantumSideEffects: true));
}
}
}
}
private IConfig CreateConfig(IToolchain toolchain,
// Tiered JIT can allocate some memory on a background thread, let's disable it by default to make our tests less flaky (#1542).
// This was mostly fixed in net7.0, but tiered jit thread is not guaranteed to not allocate, so we disable it just in case.
bool disableTieredJit = true,
// Single iteration is enough for most of the tests.
int iterationCount = 1)
{
var job = 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(iterationCount)
.WithGcForce(false)
.WithGcServer(false)
.WithGcConcurrent(false)
// To prevent finalizers allocating out of our control, we hang the finalizer thread.
// https://github.com/dotnet/runtime/issues/101536#issuecomment-2077647417
.WithEnvironmentVariable(Engines.Engine.UnitTestBlockFinalizerEnvKey, Engines.Engine.UnitTestBlockFinalizerEnvValue)
.WithToolchain(toolchain);
return ManualConfig.CreateEmpty()
.AddJob(disableTieredJit
? job.WithEnvironmentVariable("COMPlus_TieredCompilation", "0")
: job)
.AddColumnProvider(DefaultColumnProviders.Instance)
.AddDiagnoser(MemoryDiagnoser.Default)
.AddLogger(toolchain.IsInProcess ? ConsoleLogger.Default : new OutputLogger(output)); // we can't use OutputLogger for the InProcess toolchains because it allocates memory on the same thread
}
// note: don't copy, never use in production systems (it should work but I am not 100% sure)
private int CalculateRequiredSpace<T>()
{
int total = SizeOfAllFields<T>();
if (!typeof(T).GetTypeInfo().IsValueType)
total += IntPtr.Size * 2; // pointer to method table + object header word
if (total % IntPtr.Size != 0) // aligning..
total += IntPtr.Size - (total % IntPtr.Size);
return total;
}
// note: don't copy, never use in production systems (it should work but I am not 100% sure)
private int SizeOfAllFields<T>()
{
int GetSize(Type type)
{
var sizeOf = typeof(Unsafe).GetTypeInfo().GetMethod(nameof(Unsafe.SizeOf));
return (int)sizeOf.MakeGenericMethod(type).Invoke(null, null);
}
return typeof(T)
.GetAllFields()
.Where(field => !field.IsStatic && !field.IsLiteral)
.Distinct()
.Sum(field => GetSize(field.FieldType));
}
}
}