Skip to content

Commit c61dc72

Browse files
authored
Fix metrics size regression (#106415)
When adding the startup init for runtime metrics recently I only used a dynamic check for the System.Diagnostics.Metrics.Meter.IsSupported AppContext flag. This had the right runtime behavior but couldn't be statically analyzed by ILLink so size was larger than necessary. Using a property the linker can be aware of should allow the code to be trimmed once again.
1 parent 4de58b7 commit c61dc72

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
<method signature="System.Boolean IsEnabled(System.Diagnostics.Tracing.EventLevel,System.Diagnostics.Tracing.EventKeywords)" body="stub" value="false" />
88
<method signature="System.Boolean IsEnabled(System.Diagnostics.Tracing.EventLevel,System.Diagnostics.Tracing.EventKeywords,System.Diagnostics.Tracing.EventChannel)" body="stub" value="false" />
99
</type>
10+
<type fullname="System.Diagnostics.Tracing.EventSource" feature="System.Diagnostics.Metrics.Meter.IsSupported" featurevalue="false">
11+
<method signature="System.Boolean get_IsMeterSupported()" body="stub" value="false" />
12+
</type>
1013
<type fullname="System.Diagnostics.StackTrace" feature="System.Diagnostics.StackTrace.IsSupported" featurevalue="false">
1114
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
1215
</type>

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ public partial class EventSource : IDisposable
230230
private static bool InitializeIsSupported() =>
231231
AppContext.TryGetSwitch("System.Diagnostics.Tracing.EventSource.IsSupported", out bool isSupported) ? isSupported : true;
232232

233+
internal static bool IsMeterSupported { get; } = InitializeIsMeterSupported();
234+
235+
private static bool InitializeIsMeterSupported() =>
236+
AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out bool isSupported) ? isSupported : true;
237+
233238
#if FEATURE_EVENTSOURCE_XPLAT
234239
#pragma warning disable CA1823 // field is used to keep listener alive
235240
private static readonly EventListener? persistent_Xplat_Listener = IsSupported ? XplatEventLogger.InitializePersistentListener() : null;
@@ -3831,7 +3836,7 @@ internal static void InitializeDefaultEventSources()
38313836
// Functionally we could preregister NativeRuntimeEventSource and RuntimeEventSource as well, but it may not provide
38323837
// much benefit. The main benefit for MetricsEventSource is that the app may never use it and it defers
38333838
// pulling the System.Diagnostics.DiagnosticSource assembly into the process until it is needed.
3834-
if (AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out bool isSupported) ? isSupported : true)
3839+
if (IsMeterSupported)
38353840
{
38363841
const string name = "System.Diagnostics.Metrics";
38373842
Guid id = new Guid("20752bc4-c151-50f5-f27b-df92d8af5a61");

0 commit comments

Comments
 (0)