-
Notifications
You must be signed in to change notification settings - Fork 9
/
create-profile.js
167 lines (146 loc) · 5.39 KB
/
create-profile.js
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
/* jshint node:true, esnext: true */
'use strict';
module.exports = createProfile;
/**
* Create a Chrome debugging CPUProfile object from a trace object.
* https://chromedevtools.github.io/debugger-protocol-viewer/Profiler/#type-CPUProfile
* @param {!TraceInfo} traceInfo
* @return {!CPUProfile}
*/
function createProfile(traceInfo) {
/**
* Creates an individual Chrome debugging CPUProfileNode.
* https://chromedevtools.github.io/debugger-protocol-viewer/Profiler/#type-CPUProfileNode
* @param {string} fileName
* @param {string} fnName
* @param {number} callUID
* @param {number} id
* @return {!CPUProfileNode}
*/
function createCPUProfileNode(fileName, fnName, callUID, id) {
var fnDesc = /^(.+)_(\d+)_(\d+)$/.exec(fnName);
return {
functionName: fnDesc[1],
// TODO(bckenny): assumes a single file
scriptId: '0',
url: fileName,
lineNumber: parseInt(fnDesc[2], 10),
columnNumber: parseInt(fnDesc[3], 10),
hitCount: 0,
callUID: callUID,
children: [],
deoptReason: '',
id: id,
positionTicks: []
};
}
/**
* Follows all function entrances and exits in trace from current cursor until
* current node is exited, recording them as profile samples and adding adding
* called functions to node's children.
* @param {!CPUProfileNode} node
* @param {!Object} walkState
*/
function walkTraceNode(node, walkState) {
// Walk children until told otherwise. If head node, exit at end of trace. If
// non-head node, exit when trace exits this node (negative callUID in trace).
while (!(node.id === 1 && walkState.traceCursor >= walkState.trace.length) &&
walkState.trace[walkState.traceCursor] !== -node.callUID) {
if (walkState.traceCursor >= walkState.trace.length) {
throw new Error('trace ended before exiting all entered functions');
}
var childCallUid = walkState.trace[walkState.traceCursor];
if (childCallUid < 0) {
throw new Error('invalid trace: exit of function not currently inside.');
}
// Grab the child node for the next call.
var childNode = null;
// Linear search of children array (in practice very few children per node).
for (var i = 0; i < node.children.length; i++) {
if (node.children[i].callUID === childCallUid) {
childNode = node.children[i];
break;
}
}
// No existing child node, so create a new one.
if (!childNode) {
childNode = createCPUProfileNode(walkState.fileName,
walkState.functionMap[childCallUid], childCallUid,
walkState.currentNodeId++);
node.children.push(childNode);
}
// Register a sample hit for child entry.
childNode.hitCount++;
walkState.profileSamples.push(childNode.id);
var childEnterμs;
if (walkState.traceTimestamps) {
childEnterμs = walkState.traceTimestamps[walkState.traceCursor] * 1000;
} else {
childEnterμs = walkState.timestampCounter++ * 1000;
}
walkState.profileTimestamps.push(Math.floor(childEnterμs));
// Enter child in trace.
walkState.traceCursor++;
// Walk down children's children's great-great-grandchildren or whatever.
walkTraceNode(childNode, walkState);
// If timestamps are available, register a sample hit for child exit and
// then another for self node back on top.
// TODO(bckenny): is this necessary? Or does it assume child goes until
// parent is sampled again?
if (walkState.traceTimestamps) {
childNode.hitCount++;
walkState.profileSamples.push(childNode.id);
var childExitμs = walkState.traceTimestamps[walkState.traceCursor] * 1000;
walkState.profileTimestamps.push(Math.floor(childExitμs));
// Exit child in trace.
walkState.traceCursor++;
// Register a sample 1 μs after exit of child for parent node.
node.hitCount++;
walkState.profileSamples.push(node.id);
var selfReentryμs = childExitμs + 1;
walkState.profileTimestamps.push(Math.floor(selfReentryμs));
} else {
// Exit child in trace.
walkState.traceCursor++;
}
}
}
// Head uses unique `callUID` greater than all existing unique trace ids.
var head = createCPUProfileNode('', '(root)_0_0', traceInfo.fns.length, 1);
var walkState = {
traceCursor: 0,
// TODO(bckenny): assumes a single file
fileName: traceInfo.file,
trace: traceInfo.t,
functionMap: traceInfo.fns,
// Start with `id` 2 since head already uses `id` 1.
currentNodeId: 2,
// Used if trace included timestamps.
traceTimestamps: traceInfo.d ? traceInfo.d: null,
// Used if no timestamps, using call counts as a replacement.
timestampCounter: 0,
profileSamples: [],
profileTimestamps: []
};
walkTraceNode(head, walkState);
var startTime;
var endTime;
if (traceInfo.d) {
// Use actual timestamps.
startTime = traceInfo.d[0];
endTime = traceInfo.d[traceInfo.d.length - 1];
} else {
startTime = 0;
endTime = walkState.timestampCounter;
}
// Chrome debugging CPUProfile object
return {
head: head,
// startTime and endTime are in seconds
startTime: startTime / 1000,
endTime: endTime / 1000,
samples: walkState.profileSamples,
// timestamps are in microseconds
timestamps: walkState.profileTimestamps
};
}