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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @constructor
* @param {!ProfilerAgent.CPUProfile} profile
*/
WebInspector.CPUProfileDataModel = function(profile)
{
this.profileHead = profile.head;
this.samples = profile.samples;
this.timestamps = profile.timestamps;
this.profileStartTime = profile.startTime * 1000;
this.profileEndTime = profile.endTime * 1000;
this._assignParentsInProfile();
if (this.samples) {
this._normalizeTimestamps();
this._buildIdToNodeMap();
this._fixMissingSamples();
}
this._calculateTimes(profile);
}
WebInspector.CPUProfileDataModel.prototype = {
/**
* @param {!ProfilerAgent.CPUProfile} profile
*/
_calculateTimes: function(profile)
{
function totalHitCount(node) {
var result = node.hitCount;
for (var i = 0; i < node.children.length; i++)
result += totalHitCount(node.children[i]);
return result;
}
profile.totalHitCount = totalHitCount(profile.head);
var duration = this.profileEndTime - this.profileStartTime;
var samplingInterval = duration / profile.totalHitCount;
this.samplingInterval = samplingInterval;
function calculateTimesForNode(node) {
node.selfTime = node.hitCount * samplingInterval;
var totalHitCount = node.hitCount;
for (var i = 0; i < node.children.length; i++)
totalHitCount += calculateTimesForNode(node.children[i]);
node.totalTime = totalHitCount * samplingInterval;
return totalHitCount;
}
calculateTimesForNode(profile.head);
},
_assignParentsInProfile: function()
{
var head = this.profileHead;
head.parent = null;
head.depth = -1;
this.maxDepth = 0;
var nodesToTraverse = [ head ];
while (nodesToTraverse.length) {
var parent = nodesToTraverse.pop();
var depth = parent.depth + 1;
if (depth > this.maxDepth)
this.maxDepth = depth;
var children = parent.children;
var length = children.length;
for (var i = 0; i < length; ++i) {
var child = children[i];
child.parent = parent;
child.depth = depth;
if (child.children.length)
nodesToTraverse.push(child);
}
}
},
_normalizeTimestamps: function()
{
var timestamps = this.timestamps;
if (!timestamps) {
// Support loading old CPU profiles that are missing timestamps.
// Derive timestamps from profile start and stop times.
var profileStartTime = this.profileStartTime;
var interval = (this.profileEndTime - profileStartTime) / this.samples.length;
timestamps = new Float64Array(this.samples.length + 1);
for (var i = 0; i < timestamps.length; ++i)
timestamps[i] = profileStartTime + i * interval;
this.timestamps = timestamps;
return;
}
// Convert samples from usec to msec
for (var i = 0; i < timestamps.length; ++i)
timestamps[i] /= 1000;
var averageSample = (timestamps.peekLast() - timestamps[0]) / (timestamps.length - 1);
// Add an extra timestamp used to calculate the last sample duration.
this.timestamps.push(timestamps.peekLast() + averageSample);
this.profileStartTime = timestamps[0];
this.profileEndTime = timestamps.peekLast();
},
_buildIdToNodeMap: function()
{
/** @type {!Object.<number, !ProfilerAgent.CPUProfileNode>} */
this._idToNode = {};
var idToNode = this._idToNode;
var stack = [this.profileHead];
while (stack.length) {
var node = stack.pop();
idToNode[node.id] = node;
for (var i = 0; i < node.children.length; i++)
stack.push(node.children[i]);
}
var topLevelNodes = this.profileHead.children;
for (var i = 0; i < topLevelNodes.length && !(this.gcNode && this.programNode && this.idleNode); i++) {
var node = topLevelNodes[i];
if (node.functionName === "(garbage collector)")
this.gcNode = node;
else if (node.functionName === "(program)")
this.programNode = node;
else if (node.functionName === "(idle)")
this.idleNode = node;
}
},
_fixMissingSamples: function()
{
// Sometimes sampler is not able to parse the JS stack and returns
// a (program) sample instead. The issue leads to call frames belong
// to the same function invocation being split apart.
// Here's a workaround for that. When there's a single (program) sample
// between two call stacks sharing the same bottom node, it is replaced
// with the preceeding sample.
var samples = this.samples;
var samplesCount = samples.length;
if (!this.programNode || samplesCount < 3)
return;
var idToNode = this._idToNode;
var programNodeId = this.programNode.id;
var gcNodeId = this.gcNode ? this.gcNode.id : -1;
var idleNodeId = this.idleNode ? this.idleNode.id : -1;
var prevNodeId = samples[0];
var nodeId = samples[1];
for (var sampleIndex = 1; sampleIndex < samplesCount - 1; sampleIndex++) {
var nextNodeId = samples[sampleIndex + 1];
if (nodeId === programNodeId && !isSystemNode(prevNodeId) && !isSystemNode(nextNodeId)
&& bottomNode(idToNode[prevNodeId]) === bottomNode(idToNode[nextNodeId])) {
samples[sampleIndex] = prevNodeId;
}
prevNodeId = nodeId;
nodeId = nextNodeId;
}
/**
* @param {!ProfilerAgent.CPUProfileNode} node
* @return {!ProfilerAgent.CPUProfileNode}
*/
function bottomNode(node)
{
while (node.parent)
node = node.parent;
return node;
}
/**
* @param {number} nodeId
* @return {boolean}
*/
function isSystemNode(nodeId)
{
return nodeId === programNodeId || nodeId === gcNodeId || nodeId === idleNodeId;
}
},
/**
* @param {function(number, !ProfilerAgent.CPUProfileNode, number)} openFrameCallback
* @param {function(number, !ProfilerAgent.CPUProfileNode, number, number, number)} closeFrameCallback
* @param {number=} startTime
* @param {number=} stopTime
*/
forEachFrame: function(openFrameCallback, closeFrameCallback, startTime, stopTime)
{
if (!this.profileHead)
return;
startTime = startTime || 0;
stopTime = stopTime || Infinity;
var samples = this.samples;
var timestamps = this.timestamps;
var idToNode = this._idToNode;
var gcNode = this.gcNode;
var samplesCount = samples.length;
var startIndex = timestamps.lowerBound(startTime);
var stackTop = 0;
var stackNodes = [];
var prevId = this.profileHead.id;
var sampleTime = timestamps[samplesCount];
var gcParentNode = null;
if (!this._stackStartTimes)
this._stackStartTimes = new Float64Array(this.maxDepth + 2);
var stackStartTimes = this._stackStartTimes;
if (!this._stackChildrenDuration)
this._stackChildrenDuration = new Float64Array(this.maxDepth + 2);
var stackChildrenDuration = this._stackChildrenDuration;
for (var sampleIndex = startIndex; sampleIndex < samplesCount; sampleIndex++) {
sampleTime = timestamps[sampleIndex];
if (sampleTime >= stopTime)
break;
var id = samples[sampleIndex];
if (id === prevId)
continue;
var node = idToNode[id];
var prevNode = idToNode[prevId];
if (node === gcNode) {
// GC samples have no stack, so we just put GC node on top of the last recorded sample.
gcParentNode = prevNode;
openFrameCallback(gcParentNode.depth + 1, gcNode, sampleTime);
stackStartTimes[++stackTop] = sampleTime;
stackChildrenDuration[stackTop] = 0;
prevId = id;
continue;
}
if (prevNode === gcNode) {
// end of GC frame
var start = stackStartTimes[stackTop];
var duration = sampleTime - start;
stackChildrenDuration[stackTop - 1] += duration;
closeFrameCallback(gcParentNode.depth + 1, gcNode, start, duration, duration - stackChildrenDuration[stackTop]);
--stackTop;
prevNode = gcParentNode;
prevId = prevNode.id;
gcParentNode = null;
}
while (node.depth > prevNode.depth) {
stackNodes.push(node);
node = node.parent;
}
// Go down to the LCA and close current intervals.
while (prevNode !== node) {
var start = stackStartTimes[stackTop];
var duration = sampleTime - start;
stackChildrenDuration[stackTop - 1] += duration;
closeFrameCallback(prevNode.depth, prevNode, start, duration, duration - stackChildrenDuration[stackTop]);
--stackTop;
if (node.depth === prevNode.depth) {
stackNodes.push(node);
node = node.parent;
}
prevNode = prevNode.parent;
}
// Go up the nodes stack and open new intervals.
while (stackNodes.length) {
node = stackNodes.pop();
openFrameCallback(node.depth, node, sampleTime);
stackStartTimes[++stackTop] = sampleTime;
stackChildrenDuration[stackTop] = 0;
}
prevId = id;
}
if (idToNode[prevId] === gcNode) {
var start = stackStartTimes[stackTop];
var duration = sampleTime - start;
stackChildrenDuration[stackTop - 1] += duration;
closeFrameCallback(gcParentNode.depth + 1, node, start, duration, duration - stackChildrenDuration[stackTop]);
--stackTop;
}
for (var node = idToNode[prevId]; node.parent; node = node.parent) {
var start = stackStartTimes[stackTop];
var duration = sampleTime - start;
stackChildrenDuration[stackTop - 1] += duration;
closeFrameCallback(node.depth, node, start, duration, duration - stackChildrenDuration[stackTop]);
--stackTop;
}
},
/**
* @param {number} index
* @return {!ProfilerAgent.CPUProfileNode}
*/
nodeByIndex: function(index)
{
return this._idToNode[this.samples[index]];
}
}
|