-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrace.js
251 lines (219 loc) · 6.75 KB
/
trace.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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/**
* Traces encapsulate a full trace of function calls.
*
* @param {object} client
* The event emitter providing trace data.
* @param {string} name
* The name of the trace.
*/
function Trace(client, name) {
this.name = name;
this.children = []; // List of call trees
this.frames = []; // List of all frames
this.functions = []; // function ID -> aggregated function info
this.maxDepth = 0;
// Used only while collecting trace data
this._stack = [this]; // Frames on current call stack
this._functionIds = {}; // name:location -> function ID
this.onEnteredFrame = this.onEnteredFrame.bind(this);
this.onExitedFrame = this.onExitedFrame.bind(this);
if (client) {
this._client = client;
this._client.addListener("enteredFrame", this.onEnteredFrame);
this._client.addListener("exitedFrame", this.onExitedFrame);
}
EventEmitter.decorate(this);
};
Trace.prototype = {
get _currentFrame() { return this._stack[this._stack.length - 1]; },
get totalTime() { return this.endTime; },
/**
* Removes event listeners.
*/
finish: function() {
this._client.removeListener("enteredFrame", this.onEnteredFrame);
this._client.removeListener("exitedFrame", this.onExitedFrame);
this._stack = null;
this._functionIds = null;
this.finished = true;
this.emit("finished", this);
},
/**
* Returns a JSON representation of this trace.
*
* @return {string}
*/
toJSON: function() {
var jsonObj = Object.create(null);
jsonObj.functions = this.functions;
jsonObj.children = [];
for (var key in this.children) {
var child = this.children[key];
jsonObj.children.push(this._frameToJSONObj(child));
}
return JSON.stringify(jsonObj);
},
/**
* Returns an object representation of a frame without cyclic
* references or redundant information, for use by JSON.stringify.
*
* @param {object} frame
* @return {object}
*/
_frameToJSONObj: function(frame) {
var jsonObj = Object.create(null);
var propsToCopy = [
"fid",
"startTime",
"endTime",
"arguments",
"return",
"throw",
"yield"
];
for (var key in propsToCopy) {
var prop = propsToCopy[key];
if (frame.hasOwnProperty(prop)) {
jsonObj[prop] = frame[prop];
}
}
jsonObj.children = [];
for (var key in frame.children) {
var child = frame.children[key];
jsonObj.children.push(this._frameToJSONObj(child));
}
return jsonObj;
},
/**
* Returns the stack frame in this trace with the given UID.
*
* @param {integer} uid
* The UID of the desired frame.
*/
frameByUid: function(uid) {
return this.frames[uid];
},
/**
* Called when a new stack frame is entered.
*
* @param {string} ev
* The event type.
* @param {object} packet
* The frame entry packet from the trace client.
*/
onEnteredFrame: function(ev, packet) {
var current = this._currentFrame;
var frame = {
uid: this.frames.length,
depth: this._stack.length - 1,
children: []
};
// Add references to parent and siblings
if (current) {
frame.older = current;
if (current.children.length) {
frame.previous = current.children[current.children.length - 1];
frame.previous.next = frame;
}
}
// Add new frame to frames list, parent, and frame stack
this.frames.push(frame);
current.children.push(frame);
this._stack.push(frame);
if (frame.depth > this.maxDepth) {
this.maxDepth = frame.depth;
}
// Add reference to aggregated info, creating it if necessary
var key = locationToString(packet.location, packet.name);
if (!this._functionIds[key]) {
this._functionIds[key] = this.functions.length;
this.functions.push({
count: 0,
name: packet.name,
location: packet.location,
parameterNames: packet.parameterNames
});
}
frame.fid = this._functionIds[key]
var aggregated = this.functions[frame.fid];
aggregated.count++;
frame.aggregated = aggregated;
frame.name = aggregated.name;
frame.location = aggregated.location;
frame.parameterNames = aggregated.parameterNames;
// Add frame start time and update trace timing
frame.startTime = packet.time;
if (typeof this.startTime !== "number") {
this.startTime = frame.startTime;
}
this.endTime = frame.startTime;
// Add remaining properties
frame.callsite = packet.callsite;
frame.arguments = packet.arguments;
this.emit("enteredFrame", frame);
},
/**
* Called when a stack frame is exited.
*
* @param {string} ev
* The event type.
* @param {object} packet
* The frame exit packet from the trace client.
*/
onExitedFrame: function(ev, packet) {
var frame = this._currentFrame;
if (frame === this) {
// We've seen more exits than entries because the trace was
// started while some frames were on the stack.
this.finish();
} else {
this._stack.pop();
// Add frame end time and update trace timing
frame.endTime = packet.time;
frame.totalTime = frame.endTime - frame.startTime;
var selfTime = frame.totalTime;
for (var key in frame.children) {
var child = frame.children[key];
selfTime -= child.totalTime;
}
frame.selfTime = selfTime;
if (typeof frame.aggregated.totalTime === "undefined") {
frame.aggregated.totalTime = 0;
frame.aggregated.selfTime = 0;
}
frame.aggregated.totalTime += frame.totalTime;
frame.aggregated.selfTime += frame.selfTime;
if (!this.endTime || frame.endTime > this.endTime) {
this.endTime = frame.endTime;
}
var types = ["return", "throw", "yield"];
for (var key in types) {
var type = types[key];
if (typeof packet[type] !== "undefined") {
frame[type] = packet[type];
}
}
this.emit("exitedFrame", frame);
}
}
};
/**
* Returns a string representation of the given source
* location.
*
* @param {SourceLocation} loc
* The source location to convert to a string.
* @param {string} name
* The function name, to distinguish this location from other
* functions which claim to be defined at the same source
* location. Workaround to deal with the fact that anonymous
* inner functions report the same location as the outer
* function.
*/
function locationToString(loc, name) {
return (name || "") + JSON.stringify(loc);
}