forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV8CpuProfile.cs
More file actions
394 lines (337 loc) · 14.3 KB
/
Copy pathV8CpuProfile.cs
File metadata and controls
394 lines (337 loc) · 14.3 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript.V8
{
/// <summary>
/// Represents a V8 CPU profile.
/// </summary>
public class V8CpuProfile
{
internal V8CpuProfile()
{
// the help file builder (SHFB) insists on an empty constructor here
}
/// <summary>
/// Gets the profile's name.
/// </summary>
public string Name { get; internal set; }
/// <summary>
/// Gets the profile's starting timestamp in microseconds.
/// </summary>
/// <remarks>
/// The timestamp specifies an offset relative to an unspecified moment in the past. All
/// timestamps within the profile are relative to the same moment.
/// </remarks>
public ulong StartTimestamp { get; internal set; }
/// <summary>
/// Gets the profile's ending timestamp in microseconds.
/// </summary>
/// <remarks>
/// The timestamp specifies an offset relative to an unspecified moment in the past. All
/// timestamps within the profile are relative to the same moment.
/// </remarks>
public ulong EndTimestamp { get; internal set; }
/// <summary>
/// Gets the root node of the profile's call tree.
/// </summary>
public Node RootNode { get; internal set; }
/// <summary>
/// Gets the profile's sample collection.
/// </summary>
/// <remarks>
/// This property returns <c>null</c> if the profile contains no samples.
/// </remarks>
public IReadOnlyList<Sample> Samples { get; internal set; }
/// <summary>
/// Returns a JSON representation of the profile.
/// </summary>
/// <remarks>
/// See the
/// <see href="https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/inspector/js_protocol-1.3.json">V8 Inspector JSON Protocol</see>
/// for schema details.
/// </remarks>
/// <returns>A JSON representation of the profile in V8 Inspector format.</returns>
public string ToJson()
{
using (var writer = new StringWriter())
{
WriteJson(writer);
return writer.ToString();
}
}
/// <summary>
/// Writes a JSON representation of the profile to the given text writer.
/// </summary>
/// <param name="writer">The text writer to which to write the profile.</param>
/// <remarks>
/// See the
/// <see href="https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/inspector/js_protocol-1.3.json">V8 Inspector JSON Protocol</see>
/// for schema details.
/// </remarks>
public void WriteJson(TextWriter writer)
{
// V8 Inspector JSON Protocol 1.3: https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/inspector/js_protocol-1.3.json
writer.Write('{');
{
WriteNodesJson(writer);
writer.Write(",\"startTime\":" + StartTimestamp);
writer.Write(",\"endTime\":" + EndTimestamp);
WriteSamplesJson(writer);
WriteTimeDeltasJson(writer);
}
writer.Write('}');
}
internal Node FindNode(ulong nodeId)
{
return RootNode?.FindNode(nodeId);
}
private void WriteNodesJson(TextWriter writer)
{
// V8 Inspector JSON Protocol 1.3: https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/inspector/js_protocol-1.3.json
writer.Write("\"nodes\":[");
{
if (RootNode is not null)
{
var queue = new Queue<Node>();
RootNode.WriteJson(writer, queue);
while (queue.Count > 0)
{
writer.Write(',');
queue.Dequeue().WriteJson(writer, queue);
}
}
}
writer.Write(']');
}
private void WriteSamplesJson(TextWriter writer)
{
// V8 Inspector JSON Protocol 1.3: https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/inspector/js_protocol-1.3.json
if ((Samples is not null) && (Samples.Count > 0))
{
writer.Write(",\"samples\":[{0}]", string.Join(",", Samples.Select(sample => sample.Node.NodeId)));
}
}
private void WriteTimeDeltasJson(TextWriter writer)
{
// V8 Inspector JSON Protocol 1.3: https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/inspector/js_protocol-1.3.json
if ((Samples is not null) && (Samples.Count > 0))
{
const ulong maxSafeInteger = 9007199254740991UL; // 2^53 - 1
writer.Write(",\"timeDeltas\":[");
{
writer.Write(Samples[0].Timestamp - StartTimestamp);
for (var index = 1; index < Samples.Count; index++)
{
var current = Samples[index].Timestamp;
var previous = Samples[index - 1].Timestamp;
var delta = (current > previous) ? (current - previous) : 0;
delta = Math.Min(delta, maxSafeInteger);
writer.Write(',');
writer.Write(delta);
}
}
writer.Write(']');
}
}
#region Nested type: Node
/// <summary>
/// Represents a node in a V8 CPU profile's call tree.
/// </summary>
public sealed class Node
{
internal Node()
{
// the help file builder (SHFB) insists on an empty constructor here
}
/// <summary>
/// Gets the node's numeric identifier.
/// </summary>
/// <remarks>
/// This value is unique within the profile.
/// </remarks>
public ulong NodeId { get; set; }
/// <summary>
/// Gets the numeric identifier of the document containing the node's script function.
/// </summary>
public long ScriptId { get; set; }
/// <summary>
/// Gets the name or URL of the document containing the node's script function.
/// </summary>
public string ScriptName { get; internal set; }
/// <summary>
/// Gets the name of the node's script function.
/// </summary>
public string FunctionName { get; internal set; }
/// <summary>
/// Gets the 1-based line number of the start of the node's script function.
/// </summary>
/// <remarks>
/// A value of zero indicates that no line number is available.
/// </remarks>
/// <c><seealso cref="V8CpuProfileFlags"/></c>
public long LineNumber { get; internal set; }
/// <summary>
/// Gets the 1-based column number of the start of the node's script function.
/// </summary>
/// <remarks>
/// A value of zero indicates that no column number is available.
/// </remarks>
/// <c><seealso cref="V8CpuProfileFlags"/></c>
public long ColumnNumber { get; internal set; }
/// <summary>
/// Gets the node's hit count.
/// </summary>
/// <remarks>
/// This value represents the number of times the CPU profiler observed the node's
/// script function at the top of the call stack.
/// </remarks>
public ulong HitCount { get; internal set; }
/// <summary>
/// Gets an optional string describing the reason why the node's script function was not optimized.
/// </summary>
public string BailoutReason { get; internal set; }
/// <summary>
/// Gets the node's hit line collection.
/// </summary>
/// <remarks>
/// This property returns <c>null</c> if the node contains no hit lines.
/// </remarks>
public IReadOnlyList<HitLine> HitLines { get; internal set; }
/// <summary>
/// Gets the node's child node collection.
/// </summary>
/// <remarks>
/// This property returns <c>null</c> if the node has no child nodes.
/// </remarks>
public IReadOnlyList<Node> ChildNodes { get; internal set; }
internal Node FindNode(ulong nodeId)
{
if (NodeId == nodeId)
{
return this;
}
if (ChildNodes is not null)
{
foreach (var childNode in ChildNodes)
{
var node = childNode.FindNode(nodeId);
if (node is not null)
{
return node;
}
}
}
return null;
}
internal void WriteJson(TextWriter writer, Queue<Node> queue)
{
// V8 Inspector JSON Protocol 1.3: https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/inspector/js_protocol-1.3.json
writer.Write('{');
{
writer.Write("\"id\":" + NodeId);
WriteCallFrameJson(writer);
writer.Write(",\"hitCount\":" + HitCount);
WriteChildrenJson(writer, queue);
if (!string.IsNullOrEmpty(BailoutReason))
{
writer.Write(",\"deoptReason\":" + BailoutReason.ToQuotedJson());
}
WritePositionTicksJson(writer);
}
writer.Write('}');
}
private void WriteCallFrameJson(TextWriter writer)
{
// V8 Inspector JSON Protocol 1.3: https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/inspector/js_protocol-1.3.json
writer.Write(",\"callFrame\":{");
{
writer.Write("\"functionName\":" + (FunctionName ?? string.Empty).ToQuotedJson());
writer.Write(",\"scriptId\":" + ScriptId);
writer.Write(",\"url\":" + (ScriptName ?? string.Empty).ToQuotedJson());
writer.Write(",\"lineNumber\":" + (LineNumber - 1));
writer.Write(",\"columnNumber\":" + (ColumnNumber - 1));
}
writer.Write('}');
}
private void WriteChildrenJson(TextWriter writer, Queue<Node> queue)
{
// V8 Inspector JSON Protocol 1.3: https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/inspector/js_protocol-1.3.json
if ((ChildNodes is not null) && (ChildNodes.Count > 0))
{
writer.Write(",\"children\":[{0}]", string.Join(",", ChildNodes.Select(node => node.NodeId)));
ChildNodes.ForEach(queue.Enqueue);
}
}
private void WritePositionTicksJson(TextWriter writer)
{
// V8 Inspector JSON Protocol 1.3: https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/inspector/js_protocol-1.3.json
if ((HitLines is not null) && (HitLines.Count > 0))
{
writer.Write(",\"positionTicks\":[");
{
HitLines[0].WriteJson(writer);
for (var index = 1; index < HitLines.Count; index++)
{
writer.Write(',');
HitLines[index].WriteJson(writer);
}
}
writer.Write(']');
}
}
#region Nested type: HitLine
/// <summary>
/// Represents a script line observed by the V8 CPU profiler.
/// </summary>
public struct HitLine
{
/// <summary>
/// Gets the 1-based line number.
/// </summary>
public long LineNumber;
/// <summary>
/// Gets the hit count for the script line.
/// </summary>
/// <remarks>
/// This value represents the number of times the CPU profiler observed the current
/// script line at the top of the call stack.
/// </remarks>
public ulong HitCount;
internal void WriteJson(TextWriter writer)
{
// V8 Inspector JSON Protocol 1.3: https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/inspector/js_protocol-1.3.json
writer.Write("{{\"line\":{0},\"ticks\":{1}}}", LineNumber, HitCount);
}
}
#endregion
}
#endregion
#region Nested type: Sample
/// <summary>
/// Represents a V8 CPU profile sample.
/// </summary>
public sealed class Sample
{
internal Sample()
{
// the help file builder (SHFB) insists on an empty constructor here
}
/// <summary>
/// Gets the sample's node within the profile's call tree.
/// </summary>
public Node Node { get; internal set; }
/// <summary>
/// Gets the sample's timestamp in microseconds.
/// </summary>
/// <remarks>
/// The timestamp specifies an offset relative to an unspecified moment in the past. All
/// timestamps within the profile are relative to the same moment.
/// </remarks>
public ulong Timestamp { get; internal set; }
}
#endregion
}
}