-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathListProfilesCommandHandler.cs
More file actions
188 lines (173 loc) · 9.21 KB
/
Copy pathListProfilesCommandHandler.cs
File metadata and controls
188 lines (173 loc) · 9.21 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
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
// 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.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Diagnostics.Tracing;
using System.Threading.Tasks;
using Microsoft.Diagnostics.NETCore.Client;
using Microsoft.Diagnostics.Tracing.Parsers;
namespace Microsoft.Diagnostics.Tools.Trace
{
internal sealed class ListProfilesCommandHandler
{
private static long defaultKeyword = 0x1 | // GC
0x4 | // AssemblyLoader
0x8 | // Loader
0x10 | // JIT
0x8000 | // Exceptions
0x10000 | // Threading
0x20000 | // JittedMethodILToNativeMap
0x1000000000; // Compilation
private static string dotnetCommonDescription = """
Lightweight .NET runtime diagnostics designed to stay low overhead.
Includes GC, AssemblyLoader, Loader, JIT, Exceptions, Threading, JittedMethodILToNativeMap, and Compilation events
Equivalent to --providers "Microsoft-Windows-DotNETRuntime:0x100003801D:4".
""";
public static int GetProfiles()
{
try
{
Console.Out.WriteLine("dotnet-trace profiles:");
int profileNameWidth = ProfileNamesMaxWidth(TraceProfiles);
foreach (Profile profile in TraceProfiles)
{
PrintProfile(profile, profileNameWidth);
}
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine($"[ERROR] {ex}");
return 1;
}
}
public static Command ListProfilesCommand()
{
Command listProfilesCommand = new(
name: "list-profiles",
description: "Lists pre-built tracing profiles with a description of what providers and filters are in each profile");
listProfilesCommand.SetAction((parseResult, ct) => Task.FromResult(GetProfiles()));
return listProfilesCommand;
}
internal static IEnumerable<Profile> TraceProfiles { get; } = new[] {
new Profile(
"dotnet-common",
new EventPipeProvider[] {
new("Microsoft-Windows-DotNETRuntime", EventLevel.Informational, defaultKeyword)
},
dotnetCommonDescription),
new Profile(
"dotnet-sampled-thread-time",
new EventPipeProvider[] {
new("Microsoft-DotNETCore-SampleProfiler", EventLevel.Informational),
},
"Samples .NET thread stacks (~100 Hz) to estimate how much wall clock time code is using.") { VerbExclusivity = "collect" },
new Profile(
"gc-verbose",
new EventPipeProvider[] {
new(
name: "Microsoft-Windows-DotNETRuntime",
eventLevel: EventLevel.Verbose,
keywords: (long)ClrTraceEventParser.Keywords.GC |
(long)ClrTraceEventParser.Keywords.GCHandle |
(long)ClrTraceEventParser.Keywords.Exception
)
},
"Tracks GC collections and samples object allocations."),
new Profile(
"gc-collect",
new EventPipeProvider[] {
new(
name: "Microsoft-Windows-DotNETRuntime",
eventLevel: EventLevel.Informational,
keywords: (long)ClrTraceEventParser.Keywords.GC
),
new(
name: "Microsoft-Windows-DotNETRuntimePrivate",
eventLevel: EventLevel.Informational,
keywords: (long)ClrTraceEventParser.Keywords.GC
)
},
"Tracks GC collections only at very low overhead.") { RundownKeyword = (long)ClrTraceEventParser.Keywords.GC, RetryStrategy = RetryStrategy.DropKeywordDropRundown },
new Profile(
"database",
new EventPipeProvider[] {
new(
name: "System.Threading.Tasks.TplEventSource",
eventLevel: EventLevel.Informational,
keywords: (long)TplEtwProviderTraceEventParser.Keywords.TasksFlowActivityIds
),
new(
name: "Microsoft-Diagnostics-DiagnosticSource",
eventLevel: EventLevel.Verbose,
keywords: (long)DiagnosticSourceKeywords.Messages |
(long)DiagnosticSourceKeywords.Events,
arguments: new Dictionary<string, string> {
{
"FilterAndPayloadSpecs",
"SqlClientDiagnosticListener/System.Data.SqlClient.WriteCommandBefore@Activity1Start:-Command;Command.CommandText;ConnectionId;Operation;Command.Connection.ServerVersion;Command.CommandTimeout;Command.CommandType;Command.Connection.ConnectionString;Command.Connection.Database;Command.Connection.DataSource;Command.Connection.PacketSize\r\n" +
"SqlClientDiagnosticListener/System.Data.SqlClient.WriteCommandAfter@Activity1Stop:\r\n" +
"Microsoft.EntityFrameworkCore/Microsoft.EntityFrameworkCore.Database.Command.CommandExecuting@Activity2Start:-Command.CommandText;Command;ConnectionId;IsAsync;Command.Connection.ClientConnectionId;Command.Connection.ServerVersion;Command.CommandTimeout;Command.CommandType;Command.Connection.ConnectionString;Command.Connection.Database;Command.Connection.DataSource;Command.Connection.PacketSize\r\n" +
"Microsoft.EntityFrameworkCore/Microsoft.EntityFrameworkCore.Database.Command.CommandExecuted@Activity2Stop:"
}
}
)
},
"Captures ADO.NET and Entity Framework database commands"),
new Profile(
"cpu-sampling",
providers: Array.Empty<EventPipeProvider>(),
description: "Kernel CPU sampling events for measuring CPU usage.") { VerbExclusivity = "collect-linux", CollectLinuxArgs = "--on-cpu" },
new Profile(
"thread-time",
providers: Array.Empty<EventPipeProvider>(),
description: "Kernel thread context switch events for measuring CPU usage and wall clock time") { VerbExclusivity = "collect-linux", CollectLinuxArgs = "--off-cpu" },
};
private static int ProfileNamesMaxWidth(IEnumerable<Profile> profiles)
{
int maxWidth = 0;
foreach (Profile profile in profiles)
{
int profileNameWidth = profile.Name.Length;
if (!string.IsNullOrEmpty(profile.VerbExclusivity))
{
profileNameWidth = $"{profile.Name} ({profile.VerbExclusivity})".Length;
}
if (profileNameWidth > maxWidth)
{
maxWidth = profileNameWidth;
}
}
return maxWidth;
}
private static void PrintProfile(Profile profile, int nameColumnWidth)
{
string[] descriptionLines = profile.Description.Replace("\r\n", "\n").Split('\n');
string profileColumn = $"{profile.Name}";
if (!string.IsNullOrEmpty(profile.VerbExclusivity))
{
profileColumn = $"{profile.Name} ({profile.VerbExclusivity})";
}
Console.Out.WriteLine($"\t{profileColumn.PadRight(nameColumnWidth)} - {descriptionLines[0]}");
string continuationPrefix = $"\t{new string(' ', nameColumnWidth)} ";
for (int i = 1; i < descriptionLines.Length; i++)
{
Console.Out.WriteLine(continuationPrefix + descriptionLines[i]);
}
}
/// <summary>
/// Keywords for DiagnosticSourceEventSource provider
/// </summary>
/// <remarks>See https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs</remarks>
private enum DiagnosticSourceKeywords : long
{
Messages = 0x1,
Events = 0x2,
IgnoreShortCutKeywords = 0x0800,
AspNetCoreHosting = 0x1000,
EntityFrameworkCoreCommands = 0x2000
}
}
}