summaryrefslogtreecommitdiff
path: root/src/System.Private.CoreLib/src/System/Threading/Tasks/AsyncCausalityTracer.cs
blob: cc8b34fbec9f36f23bfd61a105ccfd03fbb76522 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Security;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using System.Runtime.InteropServices.WindowsRuntime;

using WFD = Windows.Foundation.Diagnostics;


namespace System.Threading.Tasks
{
    internal static class AsyncCausalityTracer
    {
        internal static void EnableToETW(bool enabled)
        {
            if (enabled)
                f_LoggingOn |= Loggers.ETW;
            else
                f_LoggingOn &= ~Loggers.ETW;
        }

        internal static bool LoggingOn
        {
            get
            {
                return f_LoggingOn != 0;
            }
        }

        //s_PlatformId = {4B0171A6-F3D0-41A0-9B33-02550652B995}
        private static readonly Guid s_PlatformId = new Guid(0x4B0171A6, 0xF3D0, 0x41A0, 0x9B, 0x33, 0x02, 0x55, 0x06, 0x52, 0xB9, 0x95);

        //Indicates this information comes from the BCL Library
        private const WFD.CausalitySource s_CausalitySource = WFD.CausalitySource.Library;

        //Lazy initialize the actual factory
        private static WFD.IAsyncCausalityTracerStatics s_TracerFactory;

        // The loggers that this Tracer knows about. 
        [Flags]
        private enum Loggers : byte
        {
            CausalityTracer = 1,
            ETW = 2
        }


        //We receive the actual value for these as a callback
        private static Loggers f_LoggingOn; //assumes false by default

        // The precise static constructor will run first time somebody attempts to access this class
        static AsyncCausalityTracer()
        {
            if (!Environment.IsWinRTSupported) return;

            //COM Class Id
            string ClassId = "Windows.Foundation.Diagnostics.AsyncCausalityTracer";

            //COM Interface GUID  {50850B26-267E-451B-A890-AB6A370245EE}
            Guid guid = new Guid(0x50850B26, 0x267E, 0x451B, 0xA8, 0x90, 0XAB, 0x6A, 0x37, 0x02, 0x45, 0xEE);

            object factory = null;

            try
            {
                int hresult = Microsoft.Win32.UnsafeNativeMethods.RoGetActivationFactory(ClassId, ref guid, out factory);

                if (hresult < 0 || factory == null) return; //This prevents having an exception thrown in case IAsyncCausalityTracerStatics isn't registered.

                s_TracerFactory = (WFD.IAsyncCausalityTracerStatics)factory;

                EventRegistrationToken token = s_TracerFactory.add_TracingStatusChanged(new EventHandler<WFD.TracingStatusChangedEventArgs>(TracingStatusChangedHandler));
                Debug.Assert(token != default, "EventRegistrationToken is null");
            }
            catch (Exception ex)
            {
                // Although catching generic Exception is not recommended, this file is one exception
                // since we don't want to propagate any kind of exception to the user since all we are
                // doing here depends on internal state.
                LogAndDisable(ex);
            }
        }

        private static void TracingStatusChangedHandler(object sender, WFD.TracingStatusChangedEventArgs args)
        {
            if (args.Enabled)
                f_LoggingOn |= Loggers.CausalityTracer;
            else
                f_LoggingOn &= ~Loggers.CausalityTracer;
        }

        //
        // The TraceXXX methods should be called only if LoggingOn property returned true
        //
        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Tracking is slow path. Disable inlining for it.
        internal static void TraceOperationCreation(Task task, string operationName)
        {
            try
            {
                int taskId = task.Id;
                if ((f_LoggingOn & Loggers.ETW) != 0)
                    TplEtwProvider.Log.TraceOperationBegin(taskId, operationName, RelatedContext: 0);
                if ((f_LoggingOn & Loggers.CausalityTracer) != 0)
                    s_TracerFactory.TraceOperationCreation(WFD.CausalityTraceLevel.Required, s_CausalitySource, s_PlatformId, GetOperationId((uint)taskId), operationName, relatedContext: 0);
            }
            catch (Exception ex)
            {
                //view function comment
                LogAndDisable(ex);
            }
        }

        [MethodImplAttribute(MethodImplOptions.NoInlining)]
        internal static void TraceOperationCompletion(Task task, AsyncCausalityStatus status)
        {
            try
            {
                int taskId = task.Id;
                if ((f_LoggingOn & Loggers.ETW) != 0)
                    TplEtwProvider.Log.TraceOperationEnd(taskId, status);
                if ((f_LoggingOn & Loggers.CausalityTracer) != 0)
                    s_TracerFactory.TraceOperationCompletion(WFD.CausalityTraceLevel.Required, s_CausalitySource, s_PlatformId, GetOperationId((uint)taskId), (WFD.AsyncCausalityStatus)status);
            }
            catch (Exception ex)
            {
                //view function comment
                LogAndDisable(ex);
            }
        }

        [MethodImplAttribute(MethodImplOptions.NoInlining)]
        internal static void TraceOperationRelation(Task task, CausalityRelation relation)
        {
            try
            {
                int taskId = task.Id;
                if ((f_LoggingOn & Loggers.ETW) != 0)
                    TplEtwProvider.Log.TraceOperationRelation(taskId, relation);
                if ((f_LoggingOn & Loggers.CausalityTracer) != 0)
                    s_TracerFactory.TraceOperationRelation(WFD.CausalityTraceLevel.Important, s_CausalitySource, s_PlatformId, GetOperationId((uint)taskId), (WFD.CausalityRelation)relation);
            }
            catch (Exception ex)
            {
                //view function comment
                LogAndDisable(ex);
            }
        }

        [MethodImplAttribute(MethodImplOptions.NoInlining)]
        internal static void TraceSynchronousWorkStart(Task task, CausalitySynchronousWork work)
        {
            try
            {
                int taskId = task.Id;
                if ((f_LoggingOn & Loggers.ETW) != 0)
                    TplEtwProvider.Log.TraceSynchronousWorkBegin(taskId, work);
                if ((f_LoggingOn & Loggers.CausalityTracer) != 0)
                    s_TracerFactory.TraceSynchronousWorkStart(WFD.CausalityTraceLevel.Required, s_CausalitySource, s_PlatformId, GetOperationId((uint)taskId), (WFD.CausalitySynchronousWork)work);
            }
            catch (Exception ex)
            {
                //view function comment
                LogAndDisable(ex);
            }
        }

        [MethodImplAttribute(MethodImplOptions.NoInlining)]
        internal static void TraceSynchronousWorkCompletion(CausalitySynchronousWork work)
        {
            try
            {
                if ((f_LoggingOn & Loggers.ETW) != 0)
                    TplEtwProvider.Log.TraceSynchronousWorkEnd(work);
                if ((f_LoggingOn & Loggers.CausalityTracer) != 0)
                    s_TracerFactory.TraceSynchronousWorkCompletion(WFD.CausalityTraceLevel.Required, s_CausalitySource, (WFD.CausalitySynchronousWork)work);
            }
            catch (Exception ex)
            {
                //view function comment
                LogAndDisable(ex);
            }
        }

        //fix for 796185: leaking internal exceptions to customers,
        //we should catch and log exceptions but never propagate them.
        private static void LogAndDisable(Exception ex)
        {
            f_LoggingOn = 0;
            Debugger.Log(0, "AsyncCausalityTracer", ex.ToString());
        }

        private static ulong GetOperationId(uint taskId)
        {
            return (((ulong)Thread.GetDomainID()) << 32) + taskId;
        }
    }
}