From 2c2fc70d2ec95cd878b124badfd1f5d1d7fccdf1 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 8 Feb 2019 11:57:48 -0500 Subject: [PATCH] Move GCSettings to shared (dotnet/coreclr#22483) Mainly just type definitions and error handling that gets shared. Commit migrated from https://github.com/dotnet/coreclr/commit/13ae47e082c7ca0da9d4d8d99c29a83da052e1c7 --- .../System.Private.CoreLib/Resources/Strings.resx | 3 + .../System.Private.CoreLib.csproj | 2 +- .../src/System.Private.CoreLib/src/System/GC.cs | 25 +------ .../src/System/Runtime/GCSettings.CoreCLR.cs | 29 ++++++++ .../src/System/Runtime/GcSettings.cs | 86 ---------------------- src/coreclr/src/vm/ecalllist.h | 14 ++-- .../src/System.Private.CoreLib.Shared.projitems | 1 + .../src/System/Runtime/GCSettings.cs | 69 +++++++++++++++++ 8 files changed, 113 insertions(+), 116 deletions(-) create mode 100644 src/coreclr/src/System.Private.CoreLib/src/System/Runtime/GCSettings.CoreCLR.cs delete mode 100644 src/coreclr/src/System.Private.CoreLib/src/System/Runtime/GcSettings.cs create mode 100644 src/libraries/System.Private.CoreLib/src/System/Runtime/GCSettings.cs diff --git a/src/coreclr/src/System.Private.CoreLib/Resources/Strings.resx b/src/coreclr/src/System.Private.CoreLib/Resources/Strings.resx index 2af274f..6dc744d 100644 --- a/src/coreclr/src/System.Private.CoreLib/Resources/Strings.resx +++ b/src/coreclr/src/System.Private.CoreLib/Resources/Strings.resx @@ -2617,6 +2617,9 @@ SetData can only be used to set the value of a given name once. + + The NoGCRegion mode is in progress.End it and then set a different mode. + Method body should not exist. diff --git a/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj index 2feaaa2..777f1f0 100644 --- a/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -242,7 +242,7 @@ - + diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/GC.cs b/src/coreclr/src/System.Private.CoreLib/src/System/GC.cs index 838e814..1e0268e 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/GC.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/GC.cs @@ -12,18 +12,10 @@ ** ** ===========================================================*/ -//This class only static members and doesn't require the serializable keyword. -using System; -using System.Reflection; -using System.Security; -using System.Threading; -using System.Runtime; using System.Runtime.CompilerServices; -using System.Runtime.ConstrainedExecution; using System.Globalization; using System.Runtime.InteropServices; -using System.Runtime.Versioning; using System.Diagnostics; namespace System @@ -61,12 +53,6 @@ namespace System public static class GC { [MethodImplAttribute(MethodImplOptions.InternalCall)] - internal static extern int GetGCLatencyMode(); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - internal static extern int SetGCLatencyMode(int newLatencyMode); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] internal static extern void GetMemoryInfo(out uint highMemLoadThreshold, out ulong totalPhysicalMem, out uint lastRecordedMemLoad, @@ -79,13 +65,7 @@ namespace System [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] internal static extern int _EndNoGCRegion(); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - internal static extern int GetLOHCompactionMode(); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - internal static extern void SetLOHCompactionMode(int newLOHCompactionMode); - + [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern int GetGenerationWR(IntPtr handle); @@ -102,9 +82,6 @@ namespace System private static extern int _CollectionCount(int generation, int getSpecialGCCount); [MethodImplAttribute(MethodImplOptions.InternalCall)] - internal static extern bool IsServerGC(); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] internal static extern ulong GetSegmentSize(); [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/GCSettings.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/GCSettings.CoreCLR.cs new file mode 100644 index 0000000..1fbc1dc --- /dev/null +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/GCSettings.CoreCLR.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.CompilerServices; + +namespace System.Runtime +{ + public static partial class GCSettings + { + public static extern bool IsServerGC + { + [MethodImpl(MethodImplOptions.InternalCall)] + get; + } + + [MethodImpl(MethodImplOptions.InternalCall)] + private static extern GCLatencyMode GetGCLatencyMode(); + + [MethodImpl(MethodImplOptions.InternalCall)] + private static extern SetLatencyModeStatus SetGCLatencyMode(GCLatencyMode newLatencyMode); + + [MethodImpl(MethodImplOptions.InternalCall)] + private static extern GCLargeObjectHeapCompactionMode GetLOHCompactionMode(); + + [MethodImpl(MethodImplOptions.InternalCall)] + private static extern void SetLOHCompactionMode(GCLargeObjectHeapCompactionMode newLOHCompactionMode); + } +} diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/GcSettings.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/GcSettings.cs deleted file mode 100644 index 6e08aa3..0000000 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/GcSettings.cs +++ /dev/null @@ -1,86 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - - -using System; -using System.Runtime.CompilerServices; -using System.Runtime.ConstrainedExecution; - -namespace System.Runtime -{ - // These settings are the same format as in clr\src\vm\gcpriv.h - // make sure you change that file if you change this file! - - public enum GCLargeObjectHeapCompactionMode - { - Default = 1, - CompactOnce = 2 - } - - public enum GCLatencyMode - { - Batch = 0, - Interactive = 1, - LowLatency = 2, - SustainedLowLatency = 3, - NoGCRegion = 4 - } - - public static class GCSettings - { - private enum SetLatencyModeStatus - { - Succeeded = 0, - NoGCInProgress = 1 // NoGCRegion is in progress, can't change pause mode. - }; - - public static GCLatencyMode LatencyMode - { - get - { - return (GCLatencyMode)(GC.GetGCLatencyMode()); - } - - // We don't want to allow this API when hosted. - set - { - if ((value < GCLatencyMode.Batch) || (value > GCLatencyMode.SustainedLowLatency)) - { - throw new ArgumentOutOfRangeException(SR.ArgumentOutOfRange_Enum); - } - - if (GC.SetGCLatencyMode((int)value) == (int)SetLatencyModeStatus.NoGCInProgress) - throw new InvalidOperationException("The NoGCRegion mode is in progress. End it and then set a different mode."); - } - } - - public static GCLargeObjectHeapCompactionMode LargeObjectHeapCompactionMode - { - get - { - return (GCLargeObjectHeapCompactionMode)(GC.GetLOHCompactionMode()); - } - - // We don't want to allow this API when hosted. - set - { - if ((value < GCLargeObjectHeapCompactionMode.Default) || - (value > GCLargeObjectHeapCompactionMode.CompactOnce)) - { - throw new ArgumentOutOfRangeException(SR.ArgumentOutOfRange_Enum); - } - - GC.SetLOHCompactionMode((int)value); - } - } - - public static bool IsServerGC - { - get - { - return GC.IsServerGC(); - } - } - } -} diff --git a/src/coreclr/src/vm/ecalllist.h b/src/coreclr/src/vm/ecalllist.h index c604db3..6b96316 100644 --- a/src/coreclr/src/vm/ecalllist.h +++ b/src/coreclr/src/vm/ecalllist.h @@ -776,13 +776,8 @@ FCFuncStart(gGCInterfaceFuncs) FCFuncElement("_WaitForFullGCComplete", GCInterface::WaitForFullGCComplete) FCFuncElement("_CollectionCount", GCInterface::CollectionCount) FCFuncElement("GetMemoryInfo", GCInterface::GetMemoryInfo) - FCFuncElement("GetGCLatencyMode", GCInterface::GetGcLatencyMode) - FCFuncElement("SetGCLatencyMode", GCInterface::SetGcLatencyMode) - FCFuncElement("GetLOHCompactionMode", GCInterface::GetLOHCompactionMode) - FCFuncElement("SetLOHCompactionMode", GCInterface::SetLOHCompactionMode) QCFuncElement("_StartNoGCRegion", GCInterface::StartNoGCRegion) QCFuncElement("_EndNoGCRegion", GCInterface::EndNoGCRegion) - FCFuncElement("IsServerGC", SystemNative::IsServerGC) FCFuncElement("GetSegmentSize", GCInterface::GetSegmentSize) QCFuncElement("_AddMemoryPressure", GCInterface::_AddMemoryPressure) QCFuncElement("_RemoveMemoryPressure", GCInterface::_RemoveMemoryPressure) @@ -798,6 +793,14 @@ FCFuncStart(gGCInterfaceFuncs) FCFuncElement("_GetAllocatedBytesForCurrentThread", GCInterface::GetAllocatedBytesForCurrentThread) FCFuncEnd() +FCFuncStart(gGCSettingsFuncs) + FCFuncElement("get_IsServerGC", SystemNative::IsServerGC) + FCFuncElement("GetGCLatencyMode", GCInterface::GetGcLatencyMode) + FCFuncElement("GetLOHCompactionMode", GCInterface::GetLOHCompactionMode) + FCFuncElement("SetGCLatencyMode", GCInterface::SetGcLatencyMode) + FCFuncElement("SetLOHCompactionMode", GCInterface::SetLOHCompactionMode) +FCFuncEnd() + FCFuncStart(gInteropMarshalFuncs) FCFuncElement("GetLastWin32Error", MarshalNative::GetLastWin32Error) FCFuncElement("SetLastWin32Error", MarshalNative::SetLastWin32Error) @@ -1224,6 +1227,7 @@ FCClassElement("FileLoadException", "System.IO", gFileLoadExceptionFuncs) FCClassElement("FormatterServices", "System.Runtime.Serialization", gSerializationFuncs) FCClassElement("GC", "System", gGCInterfaceFuncs) FCClassElement("GCHandle", "System.Runtime.InteropServices", gGCHandleFuncs) +FCClassElement("GCSettings", "System.Runtime", gGCSettingsFuncs) #ifdef FEATURE_COMINTEROP FCClassElement("IEnumerable", "System.Collections", gStdMngIEnumerableFuncs) FCClassElement("IEnumerator", "System.Collections", gStdMngIEnumeratorFuncs) diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 8063228..8ee292b 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -687,6 +687,7 @@ + diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/GCSettings.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/GCSettings.cs new file mode 100644 index 0000000..86d10f4 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/GCSettings.cs @@ -0,0 +1,69 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; + +namespace System.Runtime +{ + public enum GCLargeObjectHeapCompactionMode + { + Default = 1, + CompactOnce = 2 + } + + // These settings are the same format as in the GC in the runtime. + public enum GCLatencyMode + { + Batch = 0, + Interactive = 1, + LowLatency = 2, + SustainedLowLatency = 3, + NoGCRegion = 4 + } + + public static partial class GCSettings + { + private enum SetLatencyModeStatus + { + Succeeded = 0, + NoGCInProgress = 1 // NoGCRegion is in progress, can't change pause mode. + }; + + public static GCLatencyMode LatencyMode + { + get => GetGCLatencyMode(); + set + { + if ((value < GCLatencyMode.Batch) || + (value > GCLatencyMode.SustainedLowLatency)) + { + throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_Enum); + } + + SetLatencyModeStatus status = SetGCLatencyMode(value); + if (status == SetLatencyModeStatus.NoGCInProgress) + { + throw new InvalidOperationException(SR.InvalidOperation_SetLatencyModeNoGC); + } + + Debug.Assert(status == SetLatencyModeStatus.Succeeded, $"Unexpected return value '{status}' from {nameof(SetGCLatencyMode)}."); + } + } + + public static GCLargeObjectHeapCompactionMode LargeObjectHeapCompactionMode + { + get => GetLOHCompactionMode(); + set + { + if ((value < GCLargeObjectHeapCompactionMode.Default) || + (value > GCLargeObjectHeapCompactionMode.CompactOnce)) + { + throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_Enum); + } + + SetLOHCompactionMode(value); + } + } + } +} -- 2.7.4