Skip to content

Commit

Permalink
use benchmark process runtime, not host process runtime when deciding…
Browse files Browse the repository at this point in the history
… whether allocation quantum side effects should be excluded
  • Loading branch information
adamsitnik authored and AndreyAkinshin committed Aug 4, 2021
1 parent 37ec19f commit 4bd433d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Diagnosers/MemoryDiagnoser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public IEnumerable<Metric> ProcessResults(DiagnoserResults diagnoserResults)
yield return new Metric(GarbageCollectionsMetricDescriptor.Gen0, diagnoserResults.GcStats.Gen0Collections / (double)diagnoserResults.GcStats.TotalOperations * 1000);
yield return new Metric(GarbageCollectionsMetricDescriptor.Gen1, diagnoserResults.GcStats.Gen1Collections / (double)diagnoserResults.GcStats.TotalOperations * 1000);
yield return new Metric(GarbageCollectionsMetricDescriptor.Gen2, diagnoserResults.GcStats.Gen2Collections / (double)diagnoserResults.GcStats.TotalOperations * 1000);
yield return new Metric(AllocatedMemoryMetricDescriptor.Instance, diagnoserResults.GcStats.BytesAllocatedPerOperation);
yield return new Metric(AllocatedMemoryMetricDescriptor.Instance, diagnoserResults.GcStats.GetBytesAllocatedPerOperation(diagnoserResults.BenchmarkCase));
}

private class AllocatedMemoryMetricDescriptor : IMetricDescriptor
Expand Down
21 changes: 9 additions & 12 deletions src/BenchmarkDotNet/Engines/GcStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Reflection;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Running;
using JetBrains.Annotations;

namespace BenchmarkDotNet.Engines
Expand Down Expand Up @@ -38,19 +39,15 @@ private GcStats(int gen0Collections, int gen1Collections, int gen2Collections, l

public long TotalOperations { get; }

public long BytesAllocatedPerOperation
public long GetBytesAllocatedPerOperation(BenchmarkCase benchmarkCase)
{
get
{
bool excludeAllocationQuantumSideEffects = !RuntimeInformation.IsNetCore
|| RuntimeInformation.GetCurrentRuntime().RuntimeMoniker == RuntimeMoniker.NetCoreApp20; // the issue got fixed for .NET Core 2.0+ https://github.com/dotnet/coreclr/issues/10207

return GetTotalAllocatedBytes(excludeAllocationQuantumSideEffects) == 0
? 0
: (long) Math.Round( // let's round it to reduce the side effects of Allocation quantum
(double) GetTotalAllocatedBytes(excludeAllocationQuantumSideEffects) / TotalOperations,
MidpointRounding.ToEven);
}
bool excludeAllocationQuantumSideEffects = benchmarkCase.GetRuntime().RuntimeMoniker <= RuntimeMoniker.NetCoreApp20; // the issue got fixed for .NET Core 2.0+ https://github.com/dotnet/coreclr/issues/10207

return GetTotalAllocatedBytes(excludeAllocationQuantumSideEffects) == 0
? 0
: (long) Math.Round( // let's round it to reduce the side effects of Allocation quantum
(double) GetTotalAllocatedBytes(excludeAllocationQuantumSideEffects) / TotalOperations,
MidpointRounding.ToEven);
}

public static GcStats operator +(GcStats left, GcStats right)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static MeasurementColumn[] GetColumns(Summary summary)
new MeasurementColumn("Gen_0", (_, report, __) => report.GcStats.Gen0Collections.ToString(summary.GetCultureInfo())),
new MeasurementColumn("Gen_1", (_, report, __) => report.GcStats.Gen1Collections.ToString(summary.GetCultureInfo())),
new MeasurementColumn("Gen_2", (_, report, __) => report.GcStats.Gen2Collections.ToString(summary.GetCultureInfo())),
new MeasurementColumn("Allocated_Bytes", (_, report, __) => report.GcStats.BytesAllocatedPerOperation.ToString(summary.GetCultureInfo()))
new MeasurementColumn("Allocated_Bytes", (_, report, __) => report.GcStats.GetBytesAllocatedPerOperation(report.BenchmarkCase).ToString(summary.GetCultureInfo()))
};

return columns.ToArray();
Expand Down
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Reports/SummaryTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal SummaryTable(Summary summary, SummaryStyle style = null)

if (style.SizeUnit == null)
{
style = style.WithSizeUnit(SizeUnit.GetBestSizeUnit(summary.Reports.Select(r => r.GcStats.BytesAllocatedPerOperation).ToArray()));
style = style.WithSizeUnit(SizeUnit.GetBestSizeUnit(summary.Reports.Select(r => r.GcStats.GetBytesAllocatedPerOperation(r.BenchmarkCase)).ToArray()));
}

var columns = summary.GetColumns();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private void AssertAllocations(IToolchain toolchain, Type benchmarkType, Diction
{
var benchmarkReport = summary.Reports.Single(report => report.BenchmarkCase == benchmark);

Assert.Equal(benchmarkAllocationsValidator.Value, benchmarkReport.GcStats.BytesAllocatedPerOperation);
Assert.Equal(benchmarkAllocationsValidator.Value, benchmarkReport.GcStats.GetBytesAllocatedPerOperation(benchmark));

if (benchmarkAllocationsValidator.Value == 0)
{
Expand Down

0 comments on commit 4bd433d

Please sign in to comment.