-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathSampleProfileBuilder.cs
More file actions
310 lines (265 loc) · 10.5 KB
/
SampleProfileBuilder.cs
File metadata and controls
310 lines (265 loc) · 10.5 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Etlx;
using Microsoft.Diagnostics.Tracing.Stacks;
using Sentry.Extensibility;
using Sentry.Protocol;
namespace Sentry.Profiling;
/// <summary>
/// Build a SampleProfile from TraceEvent data.
/// </summary>
internal class SampleProfileBuilder
{
private readonly SentryOptions _options;
private readonly TraceLog _traceLog;
private readonly ActivityComputer _activityComputer;
// private readonly StartStopActivityComputer _startStopActivityComputer;
private readonly MutableTraceEventStackSource _stackSource;
// Output profile being built.
public readonly SampleProfile Profile = new();
// A sparse array that maps from StackSourceFrameIndex to an index in the output Profile.frames.
private readonly Dictionary<int, int> _frameIndexes = new();
// A dictionary from a CallStackIndex to an index in the output Profile.stacks.
private readonly Dictionary<int, int> _stackIndexes = new();
// A dictionary from a StackSourceCallStackIndex to an index in the output Profile.stacks.
private readonly Dictionary<int, int> _stackSourceStackIndexes = new();
// A sparse array mapping from a ThreadIndex to an index in Profile.Threads.
private readonly Dictionary<int, int> _threadIndexes = new();
// A sparse array mapping from an ActivityIndex to an index in Profile.Threads.
private readonly Dictionary<int, int> _activityIndexes = new();
// TODO make downsampling conditional once this is available: https://github.com/dotnet/runtime/issues/82939
private readonly Downsampler _downsampler = new();
public SampleProfileBuilder(
SentryOptions options,
TraceLog traceLog,
MutableTraceEventStackSource stackSource,
ActivityComputer activityComputer)
{
_options = options;
_traceLog = traceLog;
_activityComputer = activityComputer;
_stackSource = stackSource;
}
internal void AddSample(TraceEvent data, double timestampMs)
{
var thread = data.Thread();
if (thread is null || thread.ThreadIndex == ThreadIndex.Invalid)
{
_options.DiagnosticLogger?.LogDebug("Encountered a Profiler Sample without a correct thread. Skipping.");
return;
}
var activity = _activityComputer.GetCurrentActivity(thread);
if (activity is null)
{
_options.DiagnosticLogger?.LogDebug("Failed to get activity for a profiler sample. Skipping.");
return;
}
int threadIndex;
if (activity.IsThreadActivity)
{
threadIndex = AddThread(thread);
}
else
{
threadIndex = AddActivity(activity);
}
if (threadIndex < 0)
{
_options.DiagnosticLogger?.LogDebug("Profiler Sample threadIndex is invalid. Skipping.");
return;
}
// We need custom sampling because the TraceLog dispatches events from a queue with a delay of about 2 seconds.
if (!_downsampler.ShouldSample(threadIndex, timestampMs))
{
return;
}
int stackIndex;
if (activity.IsThreadActivity)
{
stackIndex = AddThreadStackTrace(data);
}
else
{
stackIndex = AddActivityStackTrace(thread, data);
}
if (stackIndex < 0)
{
_options.DiagnosticLogger?.LogDebug("Encountered a Profiler Sample without an associated call stack. Skipping.");
return;
}
Profile.Samples.Add(new()
{
Timestamp = (ulong)(timestampMs * 1_000_000),
StackId = stackIndex,
ThreadId = threadIndex
});
}
/// <summary>
/// Adds stack trace and frames, if missing.
/// </summary>
/// <returns>The index into the Profile's stacks list</returns>
private int AddThreadStackTrace(TraceEvent data)
{
var callStackIndex = data.CallStackIndex();
if (callStackIndex == CallStackIndex.Invalid)
{
return -1;
}
var key = (int)callStackIndex;
if (!_stackIndexes.TryGetValue(key, out var value))
{
Profile.Stacks.Add(CreateStackTrace(callStackIndex));
value = Profile.Stacks.Count - 1;
_stackIndexes[key] = value;
}
return value;
}
/// <summary>
/// Adds stack trace and frames, if missing.
/// </summary>
/// <returns>The index into the Profile's stacks list</returns>
private int AddActivityStackTrace(TraceThread thread, TraceEvent data)
{
var stackSourceCallStackIndex = StackSourceCallStackIndex.Invalid;
lock (_stackSource)
{
stackSourceCallStackIndex = _activityComputer.GetCallStack(_stackSource, data,
null // TODO topThread => _startStopActivityComputer.GetCurrentStartStopActivityStack(_stackSource, thread, topThread)
);
}
if (stackSourceCallStackIndex == StackSourceCallStackIndex.Invalid)
{
return -1;
}
var key = (int)stackSourceCallStackIndex;
if (!_stackSourceStackIndexes.TryGetValue(key, out var value))
{
Profile.Stacks.Add(CreateStackTrace(stackSourceCallStackIndex));
value = Profile.Stacks.Count - 1;
_stackIndexes[key] = value;
}
return value;
}
private Internal.GrowableArray<int> CreateStackTrace(CallStackIndex callstackIndex)
{
var stackTrace = new Internal.GrowableArray<int>(10);
while (callstackIndex != CallStackIndex.Invalid)
{
var codeAddressIndex = _traceLog.CallStacks.CodeAddressIndex(callstackIndex);
if (codeAddressIndex == CodeAddressIndex.Invalid)
{
// No need to traverse further up the stack when we're on the thread/process.
break;
}
stackTrace.Add(AddStackFrame(codeAddressIndex));
callstackIndex = _traceLog.CallStacks.Caller(callstackIndex);
}
stackTrace.Trim(10);
return stackTrace;
}
private Internal.GrowableArray<int> CreateStackTrace(StackSourceCallStackIndex callstackIndex)
{
var stackTrace = new Internal.GrowableArray<int>(10);
CodeAddressIndex codeAddressIndex;
while (callstackIndex != StackSourceCallStackIndex.Invalid
// GetFrameIndex() throws... seems to be happening on top thread frames for some reason...
&& (callstackIndex - _stackSource.Interner.CallStackStartIndex) < _stackSource.Interner.CallStackCount)
{
lock (_stackSource)
{
var frameIndex = _stackSource.GetFrameIndex(callstackIndex);
codeAddressIndex = _stackSource.GetFrameCodeAddress(frameIndex);
if (codeAddressIndex == CodeAddressIndex.Invalid)
{
// No need to traverse further up the stack when we're on the thread/process.
break;
}
callstackIndex = _stackSource.GetCallerIndex(callstackIndex);
}
stackTrace.Add(AddStackFrame(codeAddressIndex));
}
stackTrace.Trim(10);
return stackTrace;
}
/// <summary>
/// Check if the frame is already stored in the output Profile, or adds it.
/// </summary>
/// <returns>The index to the output Profile frames array.</returns>
private int AddStackFrame(CodeAddressIndex codeAddressIndex)
{
var key = (int)codeAddressIndex;
if (!_frameIndexes.TryGetValue(key, out var value))
{
Profile.Frames.Add(CreateStackFrame(codeAddressIndex));
value = Profile.Frames.Count - 1;
_frameIndexes[key] = value;
}
return value;
}
/// <summary>
/// Ensures the thread is stored in the output Profile.
/// </summary>
/// <returns>The index to the output Profile thread array.</returns>
private int AddThread(TraceThread thread)
{
var key = (int)thread.ThreadIndex;
if (!_threadIndexes.TryGetValue(key, out var value))
{
value = AddSampleProfileThread(thread.ThreadInfo ?? $"Thread {thread.ThreadID}");
_threadIndexes[key] = value;
}
return value;
}
/// <summary>
/// Ensures the activity is stored in the output Profile as a Thread.
/// </summary>
/// <returns>The index to the output Profile thread array.</returns>
private int AddActivity(TraceActivity activity)
{
var key = (int)activity.Index;
if (!_activityIndexes.TryGetValue(key, out var value))
{
// Note: there's also activity.Name but it's rather verbose:
// '<Activity (continuation) Index="8" Thread="Thread (1652)" Create="216.744" Start="216.938" kind="TaskScheduled" RawID="0x2072a40000000004"/>'
value = AddSampleProfileThread($"Activity {activity.Path}");
_activityIndexes[key] = value;
}
return value;
}
private int AddSampleProfileThread(string name)
{
Profile.Threads.Add(new() { Name = name });
var value = Profile.Threads.Count - 1;
_downsampler.NewThreadAdded(value);
return value;
}
private SentryStackFrame CreateStackFrame(CodeAddressIndex codeAddressIndex)
{
var frame = new SentryStackFrame();
var methodIndex = _traceLog.CodeAddresses.MethodIndex(codeAddressIndex);
if (_traceLog.CodeAddresses.Methods[methodIndex] is { } method)
{
frame.Function = method.FullMethodName;
if (method.MethodModuleFile is { } moduleFile)
{
frame.Module = moduleFile.Name;
}
frame.ConfigureAppFrame(_options);
}
else
{
// Fall back if the method info is unknown, see more info on Symbol resolution in
// https://github.com/getsentry/perfview/blob/031250ffb4f9fcadb9263525d6c9f274be19ca51/src/PerfView/SupportFiles/UsersGuide.htm#L7745-L7784
frame.InstructionAddress = (long?)_traceLog.CodeAddresses.Address(codeAddressIndex);
if (_traceLog.CodeAddresses.ModuleFile(codeAddressIndex) is { } moduleFile)
{
frame.Module = moduleFile.Name;
frame.ConfigureAppFrame(_options);
}
else
{
frame.InApp = false;
}
}
return frame;
}
}