Cleanup more EventSource calls in SPC to work better with illinker (#40539)
authorMarek Safar <marek.safar@gmail.com>
Fri, 9 Oct 2020 17:00:27 +0000 (19:00 +0200)
committerGitHub <noreply@github.com>
Fri, 9 Oct 2020 17:00:27 +0000 (19:00 +0200)
Co-authored-by: Koundinya Veluri <kouvel@users.noreply.github.com>
src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs
src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TaskContinuation.cs
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPool.cs

index b82412d..3e94bea 100644 (file)
@@ -839,10 +839,13 @@ namespace System.Threading.Tasks
             {
                 m_stateFlags |= Task.TASK_STATE_TASKSCHEDULED_WAS_FIRED;
 
-                Task? currentTask = Task.InternalCurrent;
-                Task? parentTask = m_contingentProperties?.m_parent;
-                TplEventSource.Log.TaskScheduled(ts.Id, currentTask == null ? 0 : currentTask.Id,
-                                     this.Id, parentTask == null ? 0 : parentTask.Id, (int)this.Options);
+                if (TplEventSource.Log.IsEnabled())
+                {
+                    Task? currentTask = Task.InternalCurrent;
+                    Task? parentTask = m_contingentProperties?.m_parent;
+                    TplEventSource.Log.TaskScheduled(ts.Id, currentTask == null ? 0 : currentTask.Id,
+                                        this.Id, parentTask == null ? 0 : parentTask.Id, (int)this.Options);
+                }
             }
         }
 
@@ -2288,11 +2291,9 @@ namespace System.Threading.Tasks
                     log.TaskStarted(previousTask.m_taskScheduler!.Id, previousTask.Id, this.Id);
                 else
                     log.TaskStarted(TaskScheduler.Current.Id, 0, this.Id);
-            }
 
-            bool loggingOn = TplEventSource.Log.IsEnabled();
-            if (loggingOn)
-                TplEventSource.Log.TraceSynchronousWorkBegin(this.Id, CausalitySynchronousWork.Execution);
+                log.TraceSynchronousWorkBegin(this.Id, CausalitySynchronousWork.Execution);
+            }
 
             try
             {
@@ -2327,8 +2328,8 @@ namespace System.Threading.Tasks
                     HandleException(exn);
                 }
 
-                if (loggingOn)
-                    TplEventSource.Log.TraceSynchronousWorkEnd(CausalitySynchronousWork.Execution);
+                if (etwIsEnabled)
+                    log.TraceSynchronousWorkEnd(CausalitySynchronousWork.Execution);
 
                 Finish(true);
             }
@@ -3199,14 +3200,10 @@ namespace System.Threading.Tasks
         {
             Debug.Assert(continuationObject != null);
 
-            TplEventSource? log = TplEventSource.Log;
-            if (!log.IsEnabled())
-            {
-                log = null;
-            }
-
-            if (TplEventSource.Log.IsEnabled())
-                TplEventSource.Log.TraceSynchronousWorkBegin(this.Id, CausalitySynchronousWork.CompletionNotification);
+            TplEventSource log = TplEventSource.Log;
+            bool etwIsEnabled = log.IsEnabled();
+            if (etwIsEnabled)
+                log.TraceSynchronousWorkBegin(this.Id, CausalitySynchronousWork.CompletionNotification);
 
             bool canInlineContinuations =
                 (m_stateFlags & (int)TaskCreationOptions.RunContinuationsAsynchronously) == 0 &&
@@ -3279,7 +3276,8 @@ namespace System.Threading.Tasks
                         if ((stc.m_options & TaskContinuationOptions.ExecuteSynchronously) == 0)
                         {
                             continuations[i] = null; // so that we can skip this later
-                            log?.RunningContinuationList(Id, i, stc);
+                            if (etwIsEnabled)
+                                log.RunningContinuationList(Id, i, stc);
                             stc.Run(this, canInlineContinuationTask: false);
                         }
                     }
@@ -3288,7 +3286,8 @@ namespace System.Threading.Tasks
                         if (forceContinuationsAsync)
                         {
                             continuations[i] = null;
-                            log?.RunningContinuationList(Id, i, currentContinuation);
+                            if (etwIsEnabled)
+                                log.RunningContinuationList(Id, i, currentContinuation);
                             switch (currentContinuation)
                             {
                                 case IAsyncStateMachineBox stateMachineBox:
@@ -3319,7 +3318,8 @@ namespace System.Threading.Tasks
                     continue;
                 }
                 continuations[i] = null; // to enable free'ing up memory earlier
-                log?.RunningContinuationList(Id, i, currentContinuation);
+                if (etwIsEnabled)
+                   log.RunningContinuationList(Id, i, currentContinuation);
 
                 switch (currentContinuation)
                 {
index f6a1627..18feb9a 100644 (file)
@@ -311,7 +311,7 @@ namespace System.Threading.Tasks
                 // If the task was cancel before running (e.g a ContinueWhenAll with a cancelled caancelation token)
                 // we will still flow it to ScheduleAndStart() were it will check the status before running
                 // We check here to avoid faulty logs that contain a join event to an operation that was already set as completed.
-                if (!continuationTask.IsCanceled && TplEventSource.Log.IsEnabled())
+                if (TplEventSource.Log.IsEnabled() && !continuationTask.IsCanceled)
                 {
                     // Log now that we are sure that this continuation is being ran
                     TplEventSource.Log.TraceOperationRelation(continuationTask.Id, CausalityRelation.AssignDelegate);
@@ -419,7 +419,7 @@ namespace System.Threading.Tasks
             var c = (SynchronizationContextAwaitTaskContinuation)state;
 
             TplEventSource log = TplEventSource.Log;
-            if (log.TasksSetActivityIds && c.m_continuationId != 0)
+            if (log.IsEnabled() && log.TasksSetActivityIds && c.m_continuationId != 0)
             {
                 c.m_syncContext.Post(s_postCallback, GetActionLogDelegate(c.m_continuationId, c.m_action));
             }
@@ -629,7 +629,7 @@ namespace System.Threading.Tasks
             }
 
             Guid savedActivityId = default;
-            if (log.TasksSetActivityIds && m_continuationId != 0)
+            if (log.IsEnabled() && log.TasksSetActivityIds && m_continuationId != 0)
             {
                 Guid activityId = TplEventSource.CreateGuidForTaskID(m_continuationId);
                 System.Diagnostics.Tracing.EventSource.SetCurrentThreadActivityId(activityId, out savedActivityId);
@@ -656,7 +656,7 @@ namespace System.Threading.Tasks
             }
             finally
             {
-                if (log.TasksSetActivityIds && m_continuationId != 0)
+                if (log.IsEnabled() && log.TasksSetActivityIds && m_continuationId != 0)
                 {
                     System.Diagnostics.Tracing.EventSource.SetCurrentThreadActivityId(savedActivityId);
                 }
index 2093db5..4488480 100644 (file)
@@ -450,7 +450,7 @@ namespace System.Threading
         {
             Debug.Assert((callback is IThreadPoolWorkItem) ^ (callback is Task));
 
-            if (loggingEnabled)
+            if (loggingEnabled && FrameworkEventSource.Log.IsEnabled())
                 System.Diagnostics.Tracing.FrameworkEventSource.Log.ThreadPoolEnqueueWorkObject(callback);
 
             ThreadPoolWorkQueueThreadLocals? tl = null;
@@ -595,7 +595,7 @@ namespace System.Threading
                         return true;
                     }
 
-                    if (workQueue.loggingEnabled)
+                    if (workQueue.loggingEnabled && FrameworkEventSource.Log.IsEnabled())
                         System.Diagnostics.Tracing.FrameworkEventSource.Log.ThreadPoolDequeueWorkObject(workItem);
 
                     //