From 7c6bee64191e91d62a23151b27893a99d2683e3d Mon Sep 17 00:00:00 2001 From: Noah Falk Date: Tue, 13 Aug 2024 10:30:47 -0700 Subject: [PATCH] Fix metrics size regression 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. --- .../src/ILLink/ILLink.Substitutions.Shared.xml | 3 +++ .../src/System/Diagnostics/Tracing/EventSource.cs | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml index 465c0d5f87b37..c6f8d232dc46d 100644 --- a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml +++ b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml @@ -7,6 +7,9 @@ + + + diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 3edb4ad9d54f6..e765a1df9209f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -230,6 +230,11 @@ public partial class EventSource : IDisposable private static bool InitializeIsSupported() => AppContext.TryGetSwitch("System.Diagnostics.Tracing.EventSource.IsSupported", out bool isSupported) ? isSupported : true; + internal static bool IsMeterSupported { get; } = InitializeIsMeterSupported(); + + private static bool InitializeIsMeterSupported() => + AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out bool isSupported) ? isSupported : true; + #if FEATURE_EVENTSOURCE_XPLAT #pragma warning disable CA1823 // field is used to keep listener alive private static readonly EventListener? persistent_Xplat_Listener = IsSupported ? XplatEventLogger.InitializePersistentListener() : null; @@ -3831,7 +3836,7 @@ internal static void InitializeDefaultEventSources() // Functionally we could preregister NativeRuntimeEventSource and RuntimeEventSource as well, but it may not provide // much benefit. The main benefit for MetricsEventSource is that the app may never use it and it defers // pulling the System.Diagnostics.DiagnosticSource assembly into the process until it is needed. - if (AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out bool isSupported) ? isSupported : true) + if (IsMeterSupported) { const string name = "System.Diagnostics.Metrics"; Guid id = new Guid("20752bc4-c151-50f5-f27b-df92d8af5a61");