-
-
Notifications
You must be signed in to change notification settings - Fork 951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
built-in accurate and cross platform Memory Diagnoser #284
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
23f3b29
built-in accurate and cross platform Memory Diagnoser, fixes #186, fi…
adamsitnik 4cabc20
don't try to use AppDomain's Monitoring in Mono since it's not implem…
adamsitnik 99c21e8
scale GC collections count / op, makes MemoryDiagnoser output stable …
adamsitnik e91255e
use per mille to make the Memory Diagnoser output more human-friendly…
adamsitnik a0536d1
Merge branch 'master' into universalMemoryDiagnoser
adamsitnik ade1bea
preallocate results list in more safe, but still ugly way
adamsitnik 1022827
closed the ugly code in separate class
adamsitnik 1e2d381
update to netcoreapp1.1 in order to get universal cross platform memo…
adamsitnik e69e80b
don't show Gen 1 and Gen 2 columns if empty for all benchmarks
adamsitnik 2a529ab
update to .NET Core 1.1, fixes #301
adamsitnik e6ccee6
always show Gen 0 column, display Gen 0/1/2 per 1k op
adamsitnik 3bcc598
Merge branch 'master' into universalMemoryDiagnoser
adamsitnik eae2cd5
added documentation and smarter bytes formatting
adamsitnik 1208c33
Merge branch 'master' into universalMemoryDiagnoser
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...ndows/Configs/MemoryDiagnoserAttribute.cs → ...re/Attributes/MemoryDiagnoserAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
using System; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Diagnosers; | ||
|
||
namespace BenchmarkDotNet.Diagnostics.Windows.Configs | ||
namespace BenchmarkDotNet.Attributes | ||
{ | ||
public class MemoryDiagnoserAttribute : Attribute, IConfigSource | ||
{ | ||
public IConfig Config { get; } | ||
|
||
public MemoryDiagnoserAttribute() | ||
{ | ||
Config = ManualConfig.CreateEmpty().With(new MemoryDiagnoser()); | ||
Config = ManualConfig.CreateEmpty().With(MemoryDiagnoser.Default); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using BenchmarkDotNet.Columns; | ||
using BenchmarkDotNet.Environments; | ||
using BenchmarkDotNet.Loggers; | ||
using BenchmarkDotNet.Reports; | ||
using BenchmarkDotNet.Running; | ||
using System.Linq; | ||
using BenchmarkDotNet.Engines; | ||
|
||
namespace BenchmarkDotNet.Diagnosers | ||
{ | ||
public class MemoryDiagnoser : IDiagnoser | ||
{ | ||
public static readonly MemoryDiagnoser Default = new MemoryDiagnoser(); | ||
|
||
private readonly Dictionary<Benchmark, GcStats> results = new Dictionary<Benchmark, GcStats>(); | ||
|
||
public IColumnProvider GetColumnProvider() => new SimpleColumnProvider( | ||
new GCCollectionColumn(results, 0), | ||
new GCCollectionColumn(results, 1), | ||
new GCCollectionColumn(results, 2), | ||
new AllocationColumn(results)); | ||
|
||
// the methods are left empty on purpose | ||
// the action takes places in other process, and the values are gathered by Engine | ||
public void BeforeAnythingElse(Process process, Benchmark benchmark) { } | ||
public void AfterSetup(Process process, Benchmark benchmark) { } | ||
public void BeforeCleanup() { } | ||
|
||
public void ProcessResults(Benchmark benchmark, BenchmarkReport report) | ||
{ | ||
results.Add(benchmark, report.GcStats); | ||
} | ||
|
||
public void DisplayResults(ILogger logger) { } | ||
|
||
public class AllocationColumn : IColumn | ||
{ | ||
private readonly Dictionary<Benchmark, GcStats> results; | ||
|
||
public AllocationColumn(Dictionary<Benchmark, GcStats> results) | ||
{ | ||
this.results = results; | ||
} | ||
|
||
public string Id => nameof(AllocationColumn); | ||
public string ColumnName => "Bytes Allocated/Op"; | ||
public bool IsDefault(Summary summary, Benchmark benchmark) => false; | ||
public bool IsAvailable(Summary summary) => true; | ||
public bool AlwaysShow => true; | ||
public ColumnCategory Category => ColumnCategory.Diagnoser; | ||
public int PriorityInCategory => 0; | ||
|
||
public string GetValue(Summary summary, Benchmark benchmark) | ||
{ | ||
#if !CORE | ||
if (results.ContainsKey(benchmark)) | ||
{ | ||
var result = results[benchmark]; | ||
// TODO scale this based on the minimum value in the column, i.e. use B/KB/MB as appropriate | ||
return (result.AllocatedBytes / result.TotalOperations).ToString("N2", HostEnvironmentInfo.MainCultureInfo); | ||
} | ||
return "N/A"; | ||
#else | ||
return "?"; | ||
#endif | ||
} | ||
} | ||
|
||
public class GCCollectionColumn : IColumn | ||
{ | ||
private Dictionary<Benchmark, GcStats> results; | ||
private int generation; | ||
// TODO also need to find a sensible way of including this in the column name? | ||
private long opsPerGCCount; | ||
|
||
public GCCollectionColumn(Dictionary<Benchmark, GcStats> results, int generation) | ||
{ | ||
ColumnName = $"Gen {generation}"; | ||
this.results = results; | ||
this.generation = generation; | ||
opsPerGCCount = results.Any() ? results.Min(r => r.Value.TotalOperations) : 1; | ||
} | ||
|
||
public bool IsDefault(Summary summary, Benchmark benchmark) => true; | ||
public string Id => $"{nameof(GCCollectionColumn)}{generation}"; | ||
public string ColumnName { get; } | ||
public bool IsAvailable(Summary summary) => true; | ||
public bool AlwaysShow => true; | ||
public ColumnCategory Category => ColumnCategory.Diagnoser; | ||
public int PriorityInCategory => 0; | ||
|
||
public string GetValue(Summary summary, Benchmark benchmark) | ||
{ | ||
if (results.ContainsKey(benchmark)) | ||
{ | ||
var result = results[benchmark]; | ||
var value = generation == 0 ? result.Gen0Collections : | ||
generation == 1 ? result.Gen1Collections : result.Gen2Collections; | ||
|
||
if (value == 0) | ||
return "-"; // make zero more obvious | ||
return (value / (double)result.TotalOperations * opsPerGCCount).ToString("N2", HostEnvironmentInfo.MainCultureInfo); | ||
} | ||
return "N/A"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
using System; | ||
|
||
namespace BenchmarkDotNet.Engines | ||
{ | ||
public struct GcStats | ||
{ | ||
internal const string ResultsLinePrefix = "GC: "; | ||
|
||
private GcStats(int gen0Collections, int gen1Collections, int gen2Collections, long allocatedBytes, long totalOperations) | ||
{ | ||
Gen0Collections = gen0Collections; | ||
Gen1Collections = gen1Collections; | ||
Gen2Collections = gen2Collections; | ||
AllocatedBytes = allocatedBytes; | ||
TotalOperations = totalOperations; | ||
} | ||
|
||
// did not use array here just to avoid heap allocation | ||
public int Gen0Collections { get; } | ||
public int Gen1Collections { get; } | ||
public int Gen2Collections { get; } | ||
public long AllocatedBytes { get; } | ||
public long TotalOperations { get; } | ||
|
||
public static GcStats operator +(GcStats left, GcStats right) | ||
{ | ||
return new GcStats( | ||
left.Gen0Collections + right.Gen0Collections, | ||
left.Gen1Collections + right.Gen1Collections, | ||
left.Gen2Collections + right.Gen2Collections, | ||
left.AllocatedBytes + right.AllocatedBytes, | ||
left.TotalOperations + right.TotalOperations); | ||
} | ||
|
||
public static GcStats operator -(GcStats left, GcStats right) | ||
{ | ||
return new GcStats( | ||
Math.Max(0, left.Gen0Collections - right.Gen0Collections), | ||
Math.Max(0, left.Gen1Collections - right.Gen1Collections), | ||
Math.Max(0, left.Gen2Collections - right.Gen2Collections), | ||
Math.Max(0, left.AllocatedBytes - right.AllocatedBytes), | ||
Math.Max(0, left.TotalOperations - right.TotalOperations)); | ||
} | ||
|
||
public GcStats WithTotalOperations(long totalOperationsCount) | ||
=> this + new GcStats(0, 0, 0, 0, totalOperationsCount); | ||
|
||
internal static GcStats ReadInitial(bool isDiagnosticsEnabled) | ||
{ | ||
// this might force GC.Collect, so we want to do this before collecting collections counts | ||
long allocatedBytes = GetAllocatedBytes(isDiagnosticsEnabled); | ||
|
||
return new GcStats( | ||
GC.CollectionCount(0), | ||
GC.CollectionCount(1), | ||
GC.CollectionCount(2), | ||
allocatedBytes, | ||
0); | ||
} | ||
|
||
internal static GcStats ReadFinal(bool isDiagnosticsEnabled) | ||
{ | ||
return new GcStats( | ||
GC.CollectionCount(0), | ||
GC.CollectionCount(1), | ||
GC.CollectionCount(2), | ||
|
||
// this might force GC.Collect, so we want to do this after collecting collections counts | ||
// to exclude this single full forced collection from results | ||
GetAllocatedBytes(isDiagnosticsEnabled), | ||
0); | ||
} | ||
|
||
public static GcStats FromForced(int forcedFullGarbageCollections) | ||
=> new GcStats(forcedFullGarbageCollections, forcedFullGarbageCollections, forcedFullGarbageCollections, 0, 0); | ||
|
||
private static long GetAllocatedBytes(bool isDiagnosticsEnabled) | ||
{ | ||
#if NETCOREAPP11 // when MS releases new version of .NET Runtime to nuget.org | ||
return GC.GetAllocatedBytesForCurrentThread(); // https://github.com/dotnet/corefx/pull/12489 | ||
#elif CLASSIC | ||
if (!isDiagnosticsEnabled) | ||
return 0; | ||
|
||
// "This instance Int64 property returns the number of bytes that have been allocated by a specific | ||
// AppDomain. The number is accurate as of the last garbage collection." - CLR via C# | ||
// so we enforce GC.Collect here just to make sure we get accurate results | ||
GC.Collect(); | ||
return AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize; | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
#else | ||
return 0; // currently for .NET Core | ||
#endif | ||
} | ||
|
||
internal string ToOutputLine() | ||
=> $"{ResultsLinePrefix} {Gen0Collections} {Gen1Collections} {Gen2Collections} {AllocatedBytes} {TotalOperations}"; | ||
|
||
internal static GcStats Parse(string line) | ||
{ | ||
if(!line.StartsWith(ResultsLinePrefix)) | ||
throw new NotSupportedException($"Line must start with {ResultsLinePrefix}"); | ||
|
||
int gen0, gen1, gen2; | ||
long allocatedBytes, totalOperationsCount; | ||
var measurementSplit = line.Remove(0, ResultsLinePrefix.Length).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); | ||
if (!int.TryParse(measurementSplit[0], out gen0) | ||
|| !int.TryParse(measurementSplit[1], out gen1) | ||
|| !int.TryParse(measurementSplit[2], out gen2) | ||
|| !long.TryParse(measurementSplit[3], out allocatedBytes) | ||
|| !long.TryParse(measurementSplit[4], out totalOperationsCount)) | ||
{ | ||
throw new NotSupportedException("Invalid string"); | ||
} | ||
|
||
return new GcStats(gen0, gen1, gen2, allocatedBytes, totalOperationsCount); | ||
} | ||
|
||
public override string ToString() => ToOutputLine(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.