Skip to content

Commit be769f8

Browse files
Enable supported GcMode characteristics with corerun (#1552)
Co-authored-by: Adam Sitnik <[email protected]>
1 parent c6cd54b commit be769f8

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/BenchmarkDotNet/Extensions/ProcessExtensions.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.Diagnostics;
@@ -9,6 +9,7 @@
99
using BenchmarkDotNet.Loggers;
1010
using BenchmarkDotNet.Portability;
1111
using BenchmarkDotNet.Running;
12+
using BenchmarkDotNet.Toolchains.CoreRun;
1213
using JetBrains.Annotations;
1314

1415
namespace BenchmarkDotNet.Extensions
@@ -119,6 +120,12 @@ internal static void SetEnvironmentVariables(this ProcessStartInfo start, Benchm
119120
if (benchmarkCase.Job.Environment.Runtime is MonoRuntime monoRuntime && !string.IsNullOrEmpty(monoRuntime.MonoBclPath))
120121
start.EnvironmentVariables["MONO_PATH"] = monoRuntime.MonoBclPath;
121122

123+
// corerun does not understand runtimeconfig.json files;
124+
// we have to set "COMPlus_GC*" environment variables as documented in
125+
// https://docs.microsoft.com/en-us/dotnet/core/run-time-config/garbage-collector
126+
if (benchmarkCase.Job.Infrastructure.Toolchain is CoreRunToolchain _)
127+
start.SetCoreRunEnvironmentVariables(benchmarkCase);
128+
122129
if (!benchmarkCase.Job.HasValue(EnvironmentMode.EnvironmentVariablesCharacteristic))
123130
return;
124131

@@ -221,5 +228,24 @@ private static int RunProcessAndIgnoreOutput(string fileName, string arguments,
221228
return process.ExitCode;
222229
}
223230
}
231+
232+
private static void SetCoreRunEnvironmentVariables(this ProcessStartInfo start, BenchmarkCase benchmarkCase)
233+
{
234+
var gcMode = benchmarkCase.Job.Environment.Gc;
235+
if (!gcMode.HasChanges)
236+
return; // do nothing for the default settings
237+
238+
start.EnvironmentVariables["COMPlus_gcServer"] = gcMode.Server ? "1" : "0";
239+
start.EnvironmentVariables["COMPlus_gcConcurrent"] = gcMode.Concurrent ? "1" : "0";
240+
start.EnvironmentVariables["COMPlus_GCCpuGroup"] = gcMode.CpuGroups ? "1" : "0";
241+
start.EnvironmentVariables["COMPlus_gcAllowVeryLargeObjects"] = gcMode.AllowVeryLargeObjects ? "1" : "0";
242+
start.EnvironmentVariables["COMPlus_GCRetainVM"] = gcMode.RetainVm ? "1" : "0";
243+
start.EnvironmentVariables["COMPlus_GCNoAffinitize"] = gcMode.NoAffinitize ? "1" : "0";
244+
245+
if (gcMode.HasValue(GcMode.HeapAffinitizeMaskCharacteristic))
246+
start.EnvironmentVariables["COMPlus_GCHeapAffinitizeMask"] = gcMode.HeapAffinitizeMask.ToString("X");
247+
if (gcMode.HasValue(GcMode.HeapCountCharacteristic))
248+
start.EnvironmentVariables["COMPlus_GCHeapCount"] = gcMode.HeapCount.ToString("X");
249+
}
224250
}
225-
}
251+
}

0 commit comments

Comments
 (0)