forked from node-inspector/v8-profiler
-
Notifications
You must be signed in to change notification settings - Fork 5
/
v8-profiler.js
245 lines (214 loc) · 5.73 KB
/
v8-profiler.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
var binding = require("./profiler");
function Snapshot() {}
Snapshot.prototype.compare = function (other) {
var my_objects = this.nodeCounts(),
their_objects = other.nodeCounts(),
diff = {}, i, k, my_val, their_val;
all_keys = Object.keys(my_objects).concat(Object.keys(their_objects)); //has dupes, oh well
for (i = 0; i < all_keys.length; i++) {
k = all_keys[i];
my_val = my_objects[k] || 0;
their_val = their_objects[k] || 0;
diff[k] = their_val - my_val;
}
return diff;
};
Snapshot.prototype.hotPath = function() {
var path = [], node = this.root, c, i = 0;
c = this.children(node);
while (c.length > 0 && i < 1000) {
node = c[0].to;
c = this.children(node);
path.push(node);
i++;
}
return path;
};
Snapshot.prototype.children = function(node) {
var i, children = [];
for(i = 0; i < node.childrenCount; i++) {
children[i] = node.getChild(i);
}
children.sort(function (a, b){
return b.to.retainedSize() - a.to.retainedSize();
});
return children;
};
Snapshot.prototype.topDominatorIds = function() {
var doms = {}, arr;
this.allNodes().forEach(function(node){
var dom = node.dominatorNode || { id: "none"};
if (doms[dom.id]) {
doms[dom.id] += 1;
}
else {
doms[dom.id] = 1;
}
});
arr = Object.keys(doms).map(function(d){
return {id: d, count: doms[d]};
});
arr.sort(function(a, b) {
return b.count - a.count;
});
return arr;
};
Snapshot.prototype.topDominators = function() {
var self = this;
return this.topDominatorIds().map(function(d){
return self.getNodeById(+d.id);
});
};
Snapshot.prototype.allNodes = function() {
var n = this.nodesCount, i, nodes = [];
for (i = 0; i < n; i++) {
nodes[i] = this.getNode(i);
}
return nodes;
};
Snapshot.prototype.nodeCounts = function() {
var objects = {};
this.allNodes().forEach(function(n){
if(n.type === "Object") {
if (objects[n.name]) {
objects[n.name] += 1;
}
else {
objects[n.name] = 1;
}
}
else {
if (objects[n.type]) {
objects[n.type] += 1;
}
else {
objects[n.type] = 1;
}
}
});
return objects;
};
//adapted from WebCore/bindings/v8/ScriptHeapSnapshot.cpp
Snapshot.prototype.stringify = function() {
var root = this.root, i, j, count_i, count_j, node,
lowLevels = {}, entries = {}, entry,
children = {}, child, edge, result = {};
for (i = 0, count_i = root.childrenCount; i < count_i; i++) {
node = root.getChild(i).to;
if (node.type === 'Hidden') {
lowLevels[node.name] = {
count: node.instancesCount,
size: node.size,
type: node.name
};
}
else if (node.instancesCount > 0) {
entries[node.name] = {
constructorName: node.name,
count: node.instancesCount,
size: node.size
};
}
// FIXME: the children portion is too slow and bloats the results
//*
else {
entry = {
constructorName: node.name
};
for(j = 0, count_j = node.childrenCount; j < count_j; j++) {
edge = node.getChild(j);
child = edge.to;
entry[child.ptr.toString()] = {
constructorName: child.name,
count: parseInt(edge.name, 10)
};
}
children[node.ptr.toString()] = entry;
}//*/
}
result.lowlevels = lowLevels;
result.entries = entries;
result.children = children;
return JSON.stringify(result);
};
function CpuProfile() {}
function inspectorObjectFor(node) {
var i, count, child,
result = {
functionName: node.functionName,
url: node.scriptName,
lineNumber: node.lineNumber,
totalTime: node.totalTime,
selfTime: node.selfTime,
numberOfCalls: 0,
visible: true,
callUID: node.callUid,
children: []
};
for(i = 0, count = node.childrenCount; i < count; i++) {
child = node.getChild(i);
result.children.push(inspectorObjectFor(child));
}
return result;
}
CpuProfile.prototype.getTopDownRoot = function() {
return inspectorObjectFor(this.topRoot);
};
CpuProfile.prototype.getBottomUpRoot = function() {
return inspectorObjectFor(this.bottomRoot);
};
var heapCache = [];
exports.takeSnapshot = function(name, control) {
if (typeof name == 'function') {
control = name;
name = '';
}
if (!name || !name.length) {
name = 'org.nodejs.profiles.heap.user-initiated.' + (heapCache.length + 1);
}
var snapshot = binding.heapProfiler.takeSnapshot(name, control);
snapshot.__proto__ = Snapshot.prototype;
heapCache.push(snapshot);
return snapshot;
};
exports.getSnapshot = function(index) {
return heapCache[index];
};
exports.findSnapshot = function(uid) {
return heapCache.filter(function(s) {return s.uid === uid;})[0];
};
exports.snapshotCount = function() {
return heapCache.length;
};
exports.deleteAllSnapshots = function () {
heapCache = [];
binding.heapProfiler.deleteAllSnapshots();
};
var cpuCache = [];
exports.startProfiling = function(name) {
if (!name || !name.length) {
name = 'org.nodejs.profiles.cpu.user-initiated.' + (cpuCache.length + 1);
}
binding.cpuProfiler.startProfiling(name);
};
exports.stopProfiling = function(name) {
name = name ? name : '';
var profile = binding.cpuProfiler.stopProfiling(name);
profile.__proto__ = CpuProfile.prototype;
cpuCache.push(profile);
return profile;
};
exports.getProfile = function(index) {
return cpuCache[index];
};
exports.findProfile = function(uid) {
return cpuCache.filter(function(s) {return s.uid === uid;})[0];
};
exports.profileCount = function() {
return cpuCache.length;
};
exports.deleteAllProfiles = function() {
cpuCache = [];
binding.cpuProfiler.deleteAllProfiles();
};
process.profiler = exports;