-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathThreadTimeProviderType.cs
More file actions
84 lines (76 loc) · 3.23 KB
/
ThreadTimeProviderType.cs
File metadata and controls
84 lines (76 loc) · 3.23 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using Microsoft.Diagnostics.Symbols;
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Etlx;
using Microsoft.Diagnostics.Tracing.Stacks;
namespace Microsoft.Diagnostics.Tools.Trace
{
internal enum ThreadTimeProviderType
{
SampleProfiler,
UniversalEvents,
Unknown
}
internal static class ThreadTimeStackSourceHelper
{
public static MutableTraceEventStackSource GenerateStackSourceFromTrace(string traceFile, bool includeEventSourceEvents = false, bool continueOnError = false)
{
string etlxFilePath = TraceLog.CreateFromEventPipeDataFile(traceFile, null, new TraceLogOptions() { ContinueOnError = continueOnError });
using SymbolReader symbolReader = new(TextWriter.Null) { SymbolPath = SymbolPath.MicrosoftSymbolServerPath };
using TraceLog eventLog = new(etlxFilePath);
MutableTraceEventStackSource stackSource = new(eventLog);
ThreadTimeProviderType providerType = DetectProviderType(eventLog);
switch (providerType)
{
case ThreadTimeProviderType.SampleProfiler:
{
stackSource.OnlyManagedCodeStacks = true;
SampleProfilerThreadTimeComputer computer = new(eventLog, symbolReader)
{
IncludeEventSourceEvents = includeEventSourceEvents,
};
computer.GenerateThreadTimeStacks(stackSource);
break;
}
case ThreadTimeProviderType.UniversalEvents:
{
stackSource.OnlyManagedCodeStacks = false;
#pragma warning disable 618
ThreadTimeStackComputer computer = new(eventLog, symbolReader)
{
IncludeEventSourceEvents = false,
};
computer.GenerateThreadTimeStacks(stackSource);
#pragma warning restore 618
break;
}
case ThreadTimeProviderType.Unknown:
default:
throw new DiagnosticToolException("The trace does not contain SampleProfiler or Universal.Events data required for thread-time analysis.");
}
if (File.Exists(etlxFilePath))
{
File.Delete(etlxFilePath);
}
return stackSource;
}
private static ThreadTimeProviderType DetectProviderType(TraceLog eventLog)
{
foreach (TraceEvent evt in eventLog.Events)
{
if (string.Equals(evt.ProviderName, "Microsoft-DotNETCore-SampleProfiler", StringComparison.Ordinal))
{
return ThreadTimeProviderType.SampleProfiler;
}
if (string.Equals(evt.ProviderName, "Universal.Events", StringComparison.Ordinal))
{
return ThreadTimeProviderType.UniversalEvents;
}
}
return ThreadTimeProviderType.Unknown;
}
}
}