Skip to content
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

Copy GC settings from host process #1765

Merged
merged 4 commits into from Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/BenchmarkDotNet/Extensions/ProcessExtensions.cs
Expand Up @@ -124,7 +124,7 @@ internal static void SetEnvironmentVariables(this ProcessStartInfo start, Benchm
// we have to set "COMPlus_GC*" environment variables as documented in
// https://docs.microsoft.com/en-us/dotnet/core/run-time-config/garbage-collector
if (benchmarkCase.Job.Infrastructure.Toolchain is CoreRunToolchain _)
start.SetCoreRunEnvironmentVariables(benchmarkCase);
start.SetCoreRunEnvironmentVariables(benchmarkCase, resolver);

if (!benchmarkCase.Job.HasValue(EnvironmentMode.EnvironmentVariablesCharacteristic))
return;
Expand Down Expand Up @@ -229,19 +229,21 @@ private static int RunProcessAndIgnoreOutput(string fileName, string arguments,
}
}

private static void SetCoreRunEnvironmentVariables(this ProcessStartInfo start, BenchmarkCase benchmarkCase)
private static void SetCoreRunEnvironmentVariables(this ProcessStartInfo start, BenchmarkCase benchmarkCase, IResolver resolver)
{
var gcMode = benchmarkCase.Job.Environment.Gc;
if (!gcMode.HasChanges)
return; // do nothing for the default settings

start.EnvironmentVariables["COMPlus_gcServer"] = gcMode.Server ? "1" : "0";
start.EnvironmentVariables["COMPlus_gcConcurrent"] = gcMode.Concurrent ? "1" : "0";
start.EnvironmentVariables["COMPlus_GCCpuGroup"] = gcMode.CpuGroups ? "1" : "0";
start.EnvironmentVariables["COMPlus_gcAllowVeryLargeObjects"] = gcMode.AllowVeryLargeObjects ? "1" : "0";
start.EnvironmentVariables["COMPlus_GCRetainVM"] = gcMode.RetainVm ? "1" : "0";
start.EnvironmentVariables["COMPlus_GCNoAffinitize"] = gcMode.NoAffinitize ? "1" : "0";

start.EnvironmentVariables["COMPlus_gcServer"] = gcMode.ResolveValue(GcMode.ServerCharacteristic, resolver) ? "1" : "0";
start.EnvironmentVariables["COMPlus_gcConcurrent"] = gcMode.ResolveValue(GcMode.ConcurrentCharacteristic, resolver) ? "1" : "0";

if (gcMode.HasValue(GcMode.CpuGroupsCharacteristic))
start.EnvironmentVariables["COMPlus_GCCpuGroup"] = gcMode.ResolveValue(GcMode.CpuGroupsCharacteristic, resolver) ? "1" : "0";
if (gcMode.HasValue(GcMode.AllowVeryLargeObjectsCharacteristic))
start.EnvironmentVariables["COMPlus_gcAllowVeryLargeObjects"] = gcMode.ResolveValue(GcMode.AllowVeryLargeObjectsCharacteristic, resolver) ? "1" : "0";
if (gcMode.HasValue(GcMode.RetainVmCharacteristic))
start.EnvironmentVariables["COMPlus_GCRetainVM"] = gcMode.ResolveValue(GcMode.RetainVmCharacteristic, resolver) ? "1" : "0";
if (gcMode.HasValue(GcMode.NoAffinitizeCharacteristic))
start.EnvironmentVariables["COMPlus_GCNoAffinitize"] = gcMode.ResolveValue(GcMode.NoAffinitizeCharacteristic, resolver) ? "1" : "0";
if (gcMode.HasValue(GcMode.HeapAffinitizeMaskCharacteristic))
start.EnvironmentVariables["COMPlus_GCHeapAffinitizeMask"] = gcMode.HeapAffinitizeMask.ToString("X");
if (gcMode.HasValue(GcMode.HeapCountCharacteristic))
Expand Down
12 changes: 6 additions & 6 deletions src/BenchmarkDotNet/Toolchains/AppConfigGenerator.cs
Expand Up @@ -100,15 +100,15 @@ private static void GenerateJitSettings(XmlDocument xmlDocument, XmlNode runtime

private static void GenerateGCSettings(XmlDocument xmlDocument, XmlNode runtimeElement, GcMode gcMode, IResolver resolver)
{
if (!gcMode.HasChanges)
return;

CreateNodeWithAttribute(xmlDocument, runtimeElement, "gcConcurrent", "enabled", gcMode.ResolveValue(GcMode.ConcurrentCharacteristic, resolver).ToLowerCase());
CreateNodeWithAttribute(xmlDocument, runtimeElement, "gcServer", "enabled", gcMode.ResolveValue(GcMode.ServerCharacteristic, resolver).ToLowerCase());
CreateNodeWithAttribute(xmlDocument, runtimeElement, "GCCpuGroup", "enabled", gcMode.ResolveValue(GcMode.CpuGroupsCharacteristic, resolver).ToLowerCase());
CreateNodeWithAttribute(xmlDocument, runtimeElement, "gcAllowVeryLargeObjects", "enabled", gcMode.ResolveValue(GcMode.AllowVeryLargeObjectsCharacteristic, resolver).ToLowerCase());
CreateNodeWithAttribute(xmlDocument, runtimeElement, "GCNoAffinitize", "enabled", gcMode.ResolveValue(GcMode.NoAffinitizeCharacteristic, resolver).ToLowerCase());

if (gcMode.HasValue(GcMode.CpuGroupsCharacteristic))
CreateNodeWithAttribute(xmlDocument, runtimeElement, "GCCpuGroup", "enabled", gcMode.ResolveValue(GcMode.CpuGroupsCharacteristic, resolver).ToLowerCase());
if (gcMode.HasValue(GcMode.AllowVeryLargeObjectsCharacteristic))
CreateNodeWithAttribute(xmlDocument, runtimeElement, "gcAllowVeryLargeObjects", "enabled", gcMode.ResolveValue(GcMode.AllowVeryLargeObjectsCharacteristic, resolver).ToLowerCase());
if (gcMode.HasValue(GcMode.NoAffinitizeCharacteristic))
CreateNodeWithAttribute(xmlDocument, runtimeElement, "GCNoAffinitize", "enabled", gcMode.ResolveValue(GcMode.NoAffinitizeCharacteristic, resolver).ToLowerCase());
if (gcMode.HasValue(GcMode.HeapAffinitizeMaskCharacteristic))
CreateNodeWithAttribute(xmlDocument, runtimeElement, "GCHeapAffinitizeMask", "enabled", gcMode.ResolveValue(GcMode.HeapAffinitizeMaskCharacteristic, resolver).ToString());
if (gcMode.HasValue(GcMode.HeapCountCharacteristic))
Expand Down
17 changes: 8 additions & 9 deletions src/BenchmarkDotNet/Toolchains/CsProj/CsProjGenerator.cs
Expand Up @@ -83,16 +83,15 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts
[PublicAPI]
protected virtual string GetRuntimeSettings(GcMode gcMode, IResolver resolver)
{
if (!gcMode.HasChanges)
return string.Empty;

return new StringBuilder(80)
var builder = new StringBuilder(80)
.AppendLine("<PropertyGroup>")
.AppendLine($"<ServerGarbageCollection>{gcMode.ResolveValue(GcMode.ServerCharacteristic, resolver).ToLowerCase()}</ServerGarbageCollection>")
.AppendLine($"<ConcurrentGarbageCollection>{gcMode.ResolveValue(GcMode.ConcurrentCharacteristic, resolver).ToLowerCase()}</ConcurrentGarbageCollection>")
.AppendLine($"<RetainVMGarbageCollection>{gcMode.ResolveValue(GcMode.RetainVmCharacteristic, resolver).ToLowerCase()}</RetainVMGarbageCollection>")
.AppendLine("</PropertyGroup>")
.ToString();
.AppendLine($"<ServerGarbageCollection>{gcMode.ResolveValue(GcMode.ServerCharacteristic, resolver).ToLowerCase()}</ServerGarbageCollection>")
.AppendLine($"<ConcurrentGarbageCollection>{gcMode.ResolveValue(GcMode.ConcurrentCharacteristic, resolver).ToLowerCase()}</ConcurrentGarbageCollection>");

if (gcMode.HasValue(GcMode.RetainVmCharacteristic))
builder.AppendLine($"<RetainVMGarbageCollection>{gcMode.ResolveValue(GcMode.RetainVmCharacteristic, resolver).ToLowerCase()}</RetainVMGarbageCollection>");

return builder.AppendLine("</PropertyGroup>").ToString();
}

// the host project or one of the .props file that it imports might contain some custom settings that needs to be copied, sth like
Expand Down
Expand Up @@ -12,6 +12,7 @@
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
<NoWarn>$(NoWarn);CA2007</NoWarn>
<ServerGarbageCollection>true</ServerGarbageCollection>
</PropertyGroup>
<ItemGroup>
<Content Include="xunit.runner.json">
Expand Down
8 changes: 6 additions & 2 deletions tests/BenchmarkDotNet.IntegrationTests/GcModeTests.cs
Expand Up @@ -18,10 +18,14 @@ public class GcModeTests : BenchmarkTestExecutor
private IConfig CreateConfig(GcMode gc) => ManualConfig.CreateEmpty().AddJob(new Job(Job.Dry, gc));

[Fact]
public void CanHostGcMode()
public void HostProcessSettingsAreCopiedByDefault()
{
var config = CreateConfig(GcMode.Default);
CanExecute<WorkstationGcOnly>(config);

if (GCSettings.IsServerGC)
CanExecute<ServerModeEnabled>(config);
else
CanExecute<WorkstationGcOnly>(config);
}

[Fact]
Expand Down