From 99651c225f54034e881a224eda5a22ec43a7be2e Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Mon, 26 Apr 2021 17:42:09 +0200 Subject: [PATCH] Respect EventSource::IsSupported setting in more codepaths --- .../EventSourceGenerator.Emitter.cs | 11 ++++++ .../generators/EventSourceGenerator.cs | 4 +++ .../System/Buffers/ArrayPoolEventSource.cs | 6 ---- .../System/Diagnostics/Tracing/EventSource.cs | 36 +++++++++++++------ .../Tracing/FrameworkEventSource.cs | 5 --- .../Tracing/NativeRuntimeEventSource.cs | 5 --- .../Diagnostics/Tracing/RuntimeEventSource.cs | 4 --- .../System/Threading/Tasks/TplEventSource.cs | 9 ----- 8 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/generators/EventSourceGenerator.Emitter.cs b/src/libraries/System.Private.CoreLib/generators/EventSourceGenerator.Emitter.cs index d855d34634b1e0..36f1d02db233a5 100644 --- a/src/libraries/System.Private.CoreLib/generators/EventSourceGenerator.Emitter.cs +++ b/src/libraries/System.Private.CoreLib/generators/EventSourceGenerator.Emitter.cs @@ -52,6 +52,8 @@ partial class {ec.ClassName} {{"); GenerateConstructor(ec); + GenerateLogSingleton(ec.ClassName); + GenerateProviderMetadata(ec.SourceName); _builder.AppendLine($@" @@ -68,6 +70,15 @@ private void GenerateConstructor(EventSourceClass ec) { _builder.AppendLine($@" private {ec.ClassName}() : base(new Guid({ec.Guid.ToString("x").Replace("{", "").Replace("}", "")}), ""{ec.SourceName}"") {{ }}"); + + _builder.AppendLine($@" + private {ec.ClassName}(bool _) : base() {{ }}"); + } + + private void GenerateLogSingleton(string className) + { + _builder.AppendLine($@" + public static readonly {className} Log = IsSupported ? new() : new(false);"); } private void GenerateProviderMetadata(string sourceName) diff --git a/src/libraries/System.Private.CoreLib/generators/EventSourceGenerator.cs b/src/libraries/System.Private.CoreLib/generators/EventSourceGenerator.cs index 1a1339352ba77b..836068a35a7ccc 100644 --- a/src/libraries/System.Private.CoreLib/generators/EventSourceGenerator.cs +++ b/src/libraries/System.Private.CoreLib/generators/EventSourceGenerator.cs @@ -28,6 +28,10 @@ public partial class EventSourceGenerator : ISourceGenerator // { // private RuntimeEventSource() : base(new Guid(0x49592c0f,0x5a05,0x516d,0xaa,0x4b,0xa6,0x4e,0x02,0x02,0x6c,0x89), "System.Runtime") { } // + // private RuntimeEventSource(bool _) : base() { } + // + // public static readonly RuntimeEventSource Log = IsSupported ? new() : new(false); + // // private protected override ReadOnlySpan ProviderMetadata => new byte[] { 0x11, 0x0, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x0, }; // } // } diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/ArrayPoolEventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/ArrayPoolEventSource.cs index 7fb9e54b6bb85e..c67008006fe962 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/ArrayPoolEventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/ArrayPoolEventSource.cs @@ -13,8 +13,6 @@ internal sealed partial class ArrayPoolEventSource : EventSource #if !ES_BUILD_STANDALONE private const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe"; #endif - internal static readonly ArrayPoolEventSource Log = new ArrayPoolEventSource(); - /// Bucket ID used when renting/returning an array that's too large for a pool. internal const int NoBucketId = -1; @@ -38,10 +36,6 @@ internal enum BufferDroppedReason : int OverMaximumSize, } - // Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor - // as you can't make a constructor partial. - private ArrayPoolEventSource(int _) { } - /// /// Event for when a buffer is rented. This is invoked once for every successful call to Rent, /// regardless of whether a buffer is allocated or a buffer is taken from the pool. In a 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 398b1f9a128b27..fa18930617eea5 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 @@ -493,6 +493,9 @@ public static void SendCommand(EventSource eventSource, EventCommand command, ID /// public override string ToString() { + if (!IsSupported) + return string.Empty; + return SR.Format(SR.EventSource_ToString, Name, Guid); } @@ -666,8 +669,15 @@ public static void SetCurrentThreadActivityId(Guid activityId, out Guid oldActiv /// the ETW provider name. /// protected EventSource() - : this(EventSourceSettings.EtwManifestEventFormat) { + if (!IsSupported) + return; + +#if FEATURE_PERFTRACING + m_eventHandleTable = new TraceLoggingEventHandleTable(); +#endif + m_config = EventSourceSettings.EtwManifestEventFormat; + Initialize(null); } /// @@ -701,19 +711,23 @@ protected EventSource(EventSourceSettings settings) : this(settings, null) { } /// A collection of key-value strings (must be an even number). protected EventSource(EventSourceSettings settings, params string[]? traits) { - if (IsSupported) - { + if (!IsSupported) + return; + #if FEATURE_PERFTRACING - m_eventHandleTable = new TraceLoggingEventHandleTable(); + m_eventHandleTable = new TraceLoggingEventHandleTable(); #endif - m_config = ValidateSettings(settings); + m_config = ValidateSettings(settings); + Initialize(traits); + } - Type myType = this.GetType(); - Guid eventSourceGuid = GetGuid(myType); - string eventSourceName = GetName(myType); + private void Initialize(string[]? traits) + { + Type myType = this.GetType(); + Guid eventSourceGuid = GetGuid(myType); + string eventSourceName = GetName(myType); - Initialize(eventSourceGuid, eventSourceName, traits); - } + Initialize(eventSourceGuid, eventSourceName, traits); } #if FEATURE_PERFTRACING @@ -1546,6 +1560,8 @@ internal EventSource(Guid eventSourceGuid, string eventSourceName, EventSourceSe /// private unsafe void Initialize(Guid eventSourceGuid, string eventSourceName, string[]? traits) { + Debug.Assert(IsSupported); + try { m_traits = traits; diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/FrameworkEventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/FrameworkEventSource.cs index 700955e8cad4bd..10362c7b0c44c7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/FrameworkEventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/FrameworkEventSource.cs @@ -14,7 +14,6 @@ internal sealed partial class FrameworkEventSource : EventSource #if !ES_BUILD_STANDALONE private const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe"; #endif - public static readonly FrameworkEventSource Log = new FrameworkEventSource(); // Keyword definitions. These represent logical groups of events that can be turned on and off independently // Often each task has a keyword, but where tasks are determined by subsystem, keywords are determined by @@ -33,10 +32,6 @@ public static class Keywords public const EventTask ThreadTransfer = (EventTask)3; } - // Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor - // as you can't make a constructor partial. - private FrameworkEventSource(int _) { } - // optimized for common signatures (used by the ThreadTransferSend/Receive events) #if !ES_BUILD_STANDALONE [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/NativeRuntimeEventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/NativeRuntimeEventSource.cs index bc5901403874cd..2fb303c37b3346 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/NativeRuntimeEventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/NativeRuntimeEventSource.cs @@ -17,11 +17,6 @@ namespace System.Diagnostics.Tracing internal sealed partial class NativeRuntimeEventSource : EventSource { internal const string EventSourceName = "Microsoft-Windows-DotNETRuntime"; - public static readonly NativeRuntimeEventSource Log = new NativeRuntimeEventSource(); - - // Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor - // as you can't make a constructor partial. - private NativeRuntimeEventSource(int _) { } #if FEATURE_PERFTRACING /// diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs index fe47aae73fde21..48dff92b70a7de 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs @@ -45,10 +45,6 @@ public static void Initialize() s_RuntimeEventSource = new RuntimeEventSource(); } - // Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor - // as you can't make a constructor partial. - private RuntimeEventSource(int _) { } - protected override void OnEventCommand(EventCommandEventArgs command) { if (command.Command == EventCommand.Enable) diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TplEventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TplEventSource.cs index eea410711a3674..67964b8dd46da8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TplEventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TplEventSource.cs @@ -46,15 +46,6 @@ protected override void OnEventCommand(EventCommandEventArgs command) DebugActivityId = IsEnabled(EventLevel.Informational, Keywords.DebugActivityId); } - /// - /// Defines the singleton instance for the TPL ETW provider. - /// - public static readonly TplEventSource Log = new TplEventSource(); - - // Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor - // as you can't make a constructor partial. - private TplEventSource(int _) { } - /// Configured behavior of a task wait operation. public enum TaskWaitBehavior : int {