Skip to content

Commit a51fe70

Browse files
committed
Added memory diagnoser option to measure survived memory from the first benchmark run.
1 parent 36f9e73 commit a51fe70

File tree

19 files changed

+342
-32
lines changed

19 files changed

+342
-32
lines changed

src/BenchmarkDotNet/Attributes/MemoryDiagnoserAttribute.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ public class MemoryDiagnoserAttribute : Attribute, IConfigSource
1010
public IConfig Config { get; }
1111

1212
/// <param name="displayGenColumns">Display Garbage Collections per Generation columns (Gen 0, Gen 1, Gen 2). True by default.</param>
13-
public MemoryDiagnoserAttribute(bool displayGenColumns = true)
13+
/// <param name="includeSurvived">If true, monitoring will be enabled and survived memory will be measured on the first benchmark run.</param>
14+
public MemoryDiagnoserAttribute(bool displayGenColumns = true, bool includeSurvived = false)
1415
{
15-
Config = ManualConfig.CreateEmpty().AddDiagnoser(new MemoryDiagnoser(new MemoryDiagnoserConfig(displayGenColumns)));
16+
Config = ManualConfig.CreateEmpty().AddDiagnoser(new MemoryDiagnoser(new MemoryDiagnoserConfig(displayGenColumns, includeSurvived)));
1617
}
1718
}
1819
}

src/BenchmarkDotNet/Code/CodeGenerator.cs

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ internal static string Generate(BuildPartition buildPartition)
6363
.Replace("$PassArguments$", passArguments)
6464
.Replace("$EngineFactoryType$", GetEngineFactoryTypeName(benchmark))
6565
.Replace("$MeasureExtraStats$", buildInfo.Config.HasExtraStatsDiagnoser() ? "true" : "false")
66+
.Replace("$MeasureSurvivedMemory$", buildInfo.Config.HasSurvivedMemoryDiagnoser() ? "true" : "false")
6667
.Replace("$DisassemblerEntryMethodName$", DisassemblerConstants.DisassemblerEntryMethodName)
6768
.Replace("$WorkloadMethodCall$", provider.GetWorkloadMethodCall(passArguments))
6869
.RemoveRedundantIfDefines(compilationId);

src/BenchmarkDotNet/Configs/ImmutableConfig.cs

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ internal ImmutableConfig(
104104

105105
public bool HasMemoryDiagnoser() => diagnosers.OfType<MemoryDiagnoser>().Any();
106106

107+
public bool HasSurvivedMemoryDiagnoser() => diagnosers.Any(diagnoser => diagnoser is MemoryDiagnoser md && md.Config.IncludeSurvived);
108+
107109
public bool HasThreadingDiagnoser() => diagnosers.Contains(ThreadingDiagnoser.Default);
108110

109111
public bool HasExceptionDiagnoser() => diagnosers.Contains(ExceptionDiagnoser.Default);

src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public bool UseDisassemblyDiagnoser
6262
[Option('a', "artifacts", Required = false, HelpText = "Valid path to accessible directory")]
6363
public DirectoryInfo ArtifactsDirectory { get; set; }
6464

65+
[Option("memorySurvived", Required = false, Default = false, HelpText = "Measures survived memory.")]
66+
public bool UseSurvivedMemoryDiagnoser { get; set; }
67+
6568
[Option("outliers", Required = false, Default = OutlierMode.RemoveUpper, HelpText = "DontRemove/RemoveUpper/RemoveLower/RemoveAll")]
6669
public OutlierMode Outliers { get; set; }
6770

src/BenchmarkDotNet/ConsoleArguments/ConfigParser.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,11 @@ private static IConfig CreateConfig(CommandLineOptions options, IConfig globalCo
210210
.Select(counterName => (HardwareCounter)Enum.Parse(typeof(HardwareCounter), counterName, ignoreCase: true))
211211
.ToArray());
212212

213-
if (options.UseMemoryDiagnoser)
213+
if (options.UseSurvivedMemoryDiagnoser)
214+
config.AddDiagnoser(new MemoryDiagnoser(new MemoryDiagnoserConfig(includeSurvived: true)));
215+
else if (options.UseMemoryDiagnoser)
214216
config.AddDiagnoser(MemoryDiagnoser.Default);
217+
215218
if (options.UseThreadingDiagnoser)
216219
config.AddDiagnoser(ThreadingDiagnoser.Default);
217220
if (options.UseExceptionDiagnoser)

src/BenchmarkDotNet/Diagnosers/MemoryDiagnoser.cs

+19
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ public IEnumerable<Metric> ProcessResults(DiagnoserResults diagnoserResults)
4242
yield return new Metric(GarbageCollectionsMetricDescriptor.Gen2, diagnoserResults.GcStats.Gen2Collections / (double)diagnoserResults.GcStats.TotalOperations * 1000);
4343

4444
yield return new Metric(AllocatedMemoryMetricDescriptor.Instance, diagnoserResults.GcStats.GetBytesAllocatedPerOperation(diagnoserResults.BenchmarkCase));
45+
46+
if (Config.IncludeSurvived)
47+
{
48+
yield return new Metric(SurvivedMemoryMetricDescriptor.Instance, diagnoserResults.GcStats.SurvivedBytes);
49+
}
50+
}
51+
52+
private class SurvivedMemoryMetricDescriptor : IMetricDescriptor
53+
{
54+
internal static readonly IMetricDescriptor Instance = new SurvivedMemoryMetricDescriptor();
55+
56+
public string Id => "Survived Memory";
57+
public string DisplayName => "Survived";
58+
public string Legend => "Memory survived after the first operation (managed only, inclusive, 1KB = 1024B)";
59+
public string NumberFormat => "N0";
60+
public UnitType UnitType => UnitType.Size;
61+
public string Unit => SizeUnit.B.Name;
62+
public bool TheGreaterTheBetter => false;
63+
public int PriorityInCategory { get; } = AllocatedMemoryMetricDescriptor.Instance.PriorityInCategory + 1;
4564
}
4665

4766
private class GarbageCollectionsMetricDescriptor : IMetricDescriptor

src/BenchmarkDotNet/Diagnosers/MemoryDiagnoserConfig.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ namespace BenchmarkDotNet.Diagnosers
55
public class MemoryDiagnoserConfig
66
{
77
/// <param name="displayGenColumns">Display Garbage Collections per Generation columns (Gen 0, Gen 1, Gen 2). True by default.</param>
8+
/// <param name="includeSurvived">If true, monitoring will be enabled and survived memory will be measured on the first benchmark run.</param>
89
[PublicAPI]
9-
public MemoryDiagnoserConfig(bool displayGenColumns = true)
10+
public MemoryDiagnoserConfig(bool displayGenColumns = true, bool includeSurvived = false)
1011
{
1112
DisplayGenColumns = displayGenColumns;
13+
IncludeSurvived = includeSurvived;
1214
}
1315

1416
public bool DisplayGenColumns { get; }
17+
public bool IncludeSurvived { get; }
1518
}
1619
}

src/BenchmarkDotNet/Engines/Consumer.cs

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ private static readonly HashSet<Type> SupportedTypes
3535
private IntPtr ptrHolder;
3636
private UIntPtr uptrHolder;
3737

38+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39+
[PublicAPI]
40+
public void Clear()
41+
{
42+
Volatile.Write(ref stringHolder, null);
43+
Volatile.Write(ref objectHolder, null);
44+
}
45+
3846
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3947
[PublicAPI]
4048
public void Consume(byte byteValue) => byteHolder = byteValue;

src/BenchmarkDotNet/Engines/Engine.cs

+102-10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class Engine : IEngine
2020

2121
[PublicAPI] public IHost Host { get; }
2222
[PublicAPI] public Action<long> WorkloadAction { get; }
23+
[PublicAPI] public Action<long> WorkloadActionNoUnroll { get; }
2324
[PublicAPI] public Action Dummy1Action { get; }
2425
[PublicAPI] public Action Dummy2Action { get; }
2526
[PublicAPI] public Action Dummy3Action { get; }
@@ -45,19 +46,23 @@ public class Engine : IEngine
4546
private readonly EnginePilotStage pilotStage;
4647
private readonly EngineWarmupStage warmupStage;
4748
private readonly EngineActualStage actualStage;
48-
private readonly bool includeExtraStats;
4949
private readonly Random random;
50+
private readonly bool includeExtraStats, includeSurvivedMemory;
51+
52+
private long survivedBytes;
53+
private bool survivedBytesMeasured;
54+
private static Func<long> GetTotalBytes { get; set; }
5055

5156
internal Engine(
5257
IHost host,
5358
IResolver resolver,
54-
Action dummy1Action, Action dummy2Action, Action dummy3Action, Action<long> overheadAction, Action<long> workloadAction, Job targetJob,
59+
Action dummy1Action, Action dummy2Action, Action dummy3Action, Action<long> overheadAction, Action<long> workloadAction, Action<long> workloadActionNoUnroll, Job targetJob,
5560
Action globalSetupAction, Action globalCleanupAction, Action iterationSetupAction, Action iterationCleanupAction, long operationsPerInvoke,
56-
bool includeExtraStats, string benchmarkName)
61+
bool includeExtraStats, bool includeSurvivedMemory, string benchmarkName)
5762
{
58-
5963
Host = host;
6064
OverheadAction = overheadAction;
65+
WorkloadActionNoUnroll = workloadActionNoUnroll;
6166
Dummy1Action = dummy1Action;
6267
Dummy2Action = dummy2Action;
6368
Dummy3Action = dummy3Action;
@@ -70,6 +75,7 @@ internal Engine(
7075
OperationsPerInvoke = operationsPerInvoke;
7176
this.includeExtraStats = includeExtraStats;
7277
BenchmarkName = benchmarkName;
78+
this.includeSurvivedMemory = includeSurvivedMemory;
7379

7480
Resolver = resolver;
7581

@@ -85,6 +91,55 @@ internal Engine(
8591
actualStage = new EngineActualStage(this);
8692

8793
random = new Random(12345); // we are using constant seed to try to get repeatable results
94+
95+
if (includeSurvivedMemory && GetTotalBytes is null)
96+
{
97+
// CreateGetTotalBytesFunc enables monitoring, so we only call it if we need to measure survived memory.
98+
GetTotalBytes = CreateGetTotalBytesFunc();
99+
100+
// Necessary for CORE runtimes.
101+
// Measure bytes to allow GC monitor to make its allocations.
102+
GetTotalBytes();
103+
// Run the clock once to allow it to make its allocations.
104+
MeasureAction(_ => { }, 0);
105+
GetTotalBytes();
106+
}
107+
}
108+
109+
private static Func<long> CreateGetTotalBytesFunc()
110+
{
111+
// Don't try to measure in Mono, Monitoring is not available, and GC.GetTotalMemory is very inaccurate.
112+
if (RuntimeInformation.IsMono)
113+
return () => 0;
114+
try
115+
{
116+
// Docs say this should be available in .NET Core 2.1, but it throws an exception.
117+
// Just try this on all non-Mono runtimes, fallback to GC.GetTotalMemory.
118+
AppDomain.MonitoringIsEnabled = true;
119+
return () =>
120+
{
121+
// Enforce GC.Collect here to make sure we get accurate results.
122+
ForceGcCollect();
123+
return AppDomain.CurrentDomain.MonitoringSurvivedMemorySize;
124+
};
125+
}
126+
catch
127+
{
128+
return () =>
129+
{
130+
// Enforce GC.Collect here to make sure we get accurate results.
131+
ForceGcCollect();
132+
return GC.GetTotalMemory(true);
133+
};
134+
}
135+
}
136+
137+
internal Engine WithInitialData(Engine other)
138+
{
139+
// Copy the survived bytes from the other engine so we only measure it once.
140+
survivedBytes = other.survivedBytes;
141+
survivedBytesMeasured = other.survivedBytesMeasured;
142+
return this;
88143
}
89144

90145
public void Dispose()
@@ -160,7 +215,9 @@ public Measurement RunIteration(IterationData data)
160215
var action = isOverhead ? OverheadAction : WorkloadAction;
161216

162217
if (!isOverhead)
218+
{
163219
IterationSetupAction();
220+
}
164221

165222
GcCollect();
166223

@@ -169,10 +226,36 @@ public Measurement RunIteration(IterationData data)
169226

170227
Span<byte> stackMemory = randomizeMemory ? stackalloc byte[random.Next(32)] : Span<byte>.Empty;
171228

172-
// Measure
173-
var clock = Clock.Start();
174-
action(invokeCount / unrollFactor);
175-
var clockSpan = clock.GetElapsed();
229+
bool needsSurvivedMeasurement = includeSurvivedMemory && !isOverhead && !survivedBytesMeasured;
230+
double nanoseconds;
231+
if (needsSurvivedMeasurement)
232+
{
233+
// Measure survived bytes for only the first invocation.
234+
survivedBytesMeasured = true;
235+
if (totalOperations == 1)
236+
{
237+
// Measure normal invocation for both survived memory and time.
238+
long beforeBytes = GetTotalBytes();
239+
nanoseconds = MeasureAction(action, invokeCount / unrollFactor);
240+
long afterBytes = GetTotalBytes();
241+
survivedBytes = afterBytes - beforeBytes;
242+
}
243+
else
244+
{
245+
// Measure a single invocation for survived memory, plus normal invocations for time.
246+
++totalOperations;
247+
long beforeBytes = GetTotalBytes();
248+
nanoseconds = MeasureAction(WorkloadActionNoUnroll, 1);
249+
long afterBytes = GetTotalBytes();
250+
survivedBytes = afterBytes - beforeBytes;
251+
nanoseconds += MeasureAction(action, invokeCount / unrollFactor);
252+
}
253+
}
254+
else
255+
{
256+
// Measure time normally.
257+
nanoseconds = MeasureAction(action, invokeCount / unrollFactor);
258+
}
176259

177260
if (EngineEventSource.Log.IsEnabled())
178261
EngineEventSource.Log.IterationStop(data.IterationMode, data.IterationStage, totalOperations);
@@ -186,7 +269,7 @@ public Measurement RunIteration(IterationData data)
186269
GcCollect();
187270

188271
// Results
189-
var measurement = new Measurement(0, data.IterationMode, data.IterationStage, data.Index, totalOperations, clockSpan.GetNanoseconds());
272+
var measurement = new Measurement(0, data.IterationMode, data.IterationStage, data.Index, totalOperations, nanoseconds);
190273
WriteLine(measurement.ToString());
191274
if (measurement.IterationStage == IterationStage.Jitting)
192275
jittingMeasurements.Add(measurement);
@@ -196,6 +279,15 @@ public Measurement RunIteration(IterationData data)
196279
return measurement;
197280
}
198281

282+
// This is necessary for the CORE runtime to clean up the memory from the clock.
283+
[MethodImpl(MethodImplOptions.NoInlining)]
284+
private double MeasureAction(Action<long> action, long arg)
285+
{
286+
var clock = Clock.Start();
287+
action(arg);
288+
return clock.GetElapsed().GetNanoseconds();
289+
}
290+
199291
private (GcStats, ThreadingStats, double) GetExtraStats(IterationData data)
200292
{
201293
// we enable monitoring after main target run, for this single iteration which is executed at the end
@@ -219,7 +311,7 @@ public Measurement RunIteration(IterationData data)
219311
IterationCleanupAction(); // we run iteration cleanup after collecting GC stats
220312

221313
var totalOperationsCount = data.InvokeCount * OperationsPerInvoke;
222-
GcStats gcStats = (finalGcStats - initialGcStats).WithTotalOperations(totalOperationsCount);
314+
GcStats gcStats = (finalGcStats - initialGcStats).WithTotalOperationsAndSurvivedBytes(data.InvokeCount * OperationsPerInvoke, survivedBytes);
223315
ThreadingStats threadingStats = (finalThreadingStats - initialThreadingStats).WithTotalOperations(data.InvokeCount * OperationsPerInvoke);
224316

225317
return (gcStats, threadingStats, exceptionsStats.ExceptionsCount / (double)totalOperationsCount);

src/BenchmarkDotNet/Engines/EngineFactory.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ public IEngine CreateReadyToRun(EngineParameters engineParameters)
6969
.WithMinInvokeCount(2) // the minimum is 2 (not the default 4 which can be too much and not 1 which we already know is not enough)
7070
.WithEvaluateOverhead(false); // it's something very time consuming, it overhead is too small compared to total time
7171

72-
return CreateEngine(engineParameters, needsPilot, engineParameters.OverheadActionNoUnroll, engineParameters.WorkloadActionNoUnroll);
72+
return CreateEngine(engineParameters, needsPilot, engineParameters.OverheadActionNoUnroll, engineParameters.WorkloadActionNoUnroll)
73+
.WithInitialData(singleActionEngine);
7374
}
7475

75-
var multiActionEngine = CreateMultiActionEngine(engineParameters);
76+
var multiActionEngine = CreateMultiActionEngine(engineParameters)
77+
.WithInitialData(singleActionEngine);
7678

7779
DeadCodeEliminationHelper.KeepAliveWithoutBoxing(Jit(multiActionEngine, ++jitIndex, invokeCount: defaultUnrollFactor, unrollFactor: defaultUnrollFactor));
7880

@@ -118,13 +120,15 @@ private static Engine CreateEngine(EngineParameters engineParameters, Job job, A
118120
engineParameters.Dummy3Action,
119121
idle,
120122
main,
123+
engineParameters.WorkloadActionNoUnroll,
121124
job,
122125
engineParameters.GlobalSetupAction,
123126
engineParameters.GlobalCleanupAction,
124127
engineParameters.IterationSetupAction,
125128
engineParameters.IterationCleanupAction,
126129
engineParameters.OperationsPerInvoke,
127130
engineParameters.MeasureExtraStats,
131+
engineParameters.MeasureSurvivedMemory,
128132
engineParameters.BenchmarkName);
129133
}
130134
}

src/BenchmarkDotNet/Engines/EngineParameters.cs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class EngineParameters
2727
public Action IterationCleanupAction { get; set; }
2828
public bool MeasureExtraStats { get; set; }
2929

30+
public bool MeasureSurvivedMemory { get; set; }
31+
3032
[PublicAPI] public string BenchmarkName { get; set; }
3133

3234
public bool NeedsJitting => TargetJob.ResolveValue(RunMode.RunStrategyCharacteristic, DefaultResolver).NeedsJitting();

0 commit comments

Comments
 (0)