Defer Initialization of FrameworkEventSource During EventPipeController Initializatio...
authorBrian Robbins <brianrob@microsoft.com>
Fri, 30 Nov 2018 00:39:41 +0000 (16:39 -0800)
committerGitHub <noreply@github.com>
Fri, 30 Nov 2018 00:39:41 +0000 (16:39 -0800)
src/System.Private.CoreLib/src/System/Diagnostics/Eventing/EventPipeController.cs
src/System.Private.CoreLib/src/System/Threading/Timer.cs

index ef5f331..7e7fec7 100644 (file)
@@ -60,6 +60,9 @@ namespace System.Diagnostics.Tracing
         // Singleton controller instance.
         private static EventPipeController s_controllerInstance = null;
 
+        // Initialization flag used to avoid initializing FrameworkEventSource on the startup path.
+        private static bool s_initializing = false;
+
         // Controller object state.
         private Timer m_timer;
         private string m_configFilePath;
@@ -67,11 +70,18 @@ namespace System.Diagnostics.Tracing
         private string m_traceFilePath = null;
         private bool m_configFileExists = false;
 
+        internal static bool Initializing
+        {
+            get { return s_initializing; }
+        }
+
         internal static void Initialize()
         {
             // Don't allow failures to propagate upstream.  Ensure program correctness without tracing.
             try
             {
+                s_initializing = true;
+
                 if (s_controllerInstance == null)
                 {
                     if (Config_EnableEventPipe > 0)
@@ -88,6 +98,10 @@ namespace System.Diagnostics.Tracing
                 }
             }
             catch { }
+            finally
+            {
+                s_initializing = false;
+            }
         }
 
         private EventPipeController()
index 0bc235d..eeacf5b 100644 (file)
@@ -567,7 +567,8 @@ namespace System.Threading
                 }
                 else
                 {
-                    if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.ThreadTransfer))
+                    // Don't emit this event during EventPipeController.  This avoids initializing FrameworkEventSource during start-up which is expensive relative to the rest of start-up.
+                    if (!EventPipeController.Initializing && FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.ThreadTransfer))
                         FrameworkEventSource.Log.ThreadTransferSendObj(this, 1, string.Empty, true, (int)dueTime, (int)period);
 
                     success = m_associatedTimerQueue.UpdateTimer(this, dueTime, period);