2009-04-17 17:40:52 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
// Initlialize namespaces
|
|
|
|
var devtools = devtools || {};
|
|
|
|
devtools.profiler = devtools.profiler || {};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a profile object for processing profiling-related events
|
|
|
|
* and calculating function execution times.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile = function() {
|
|
|
|
this.codeMap_ = new devtools.profiler.CodeMap();
|
|
|
|
this.topDownTree_ = new devtools.profiler.CallTree();
|
|
|
|
this.bottomUpTree_ = new devtools.profiler.CallTree();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether a function with the specified name must be skipped.
|
|
|
|
* Should be overriden by subclasses.
|
|
|
|
*
|
|
|
|
* @param {string} name Function name.
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.prototype.skipThisFunction = function(name) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-04-24 11:37:38 +00:00
|
|
|
/**
|
|
|
|
* Enum for profiler operations that involve looking up existing
|
|
|
|
* code entries.
|
|
|
|
*
|
|
|
|
* @enum {number}
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.Operation = {
|
|
|
|
MOVE: 0,
|
|
|
|
DELETE: 1,
|
|
|
|
TICK: 2
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-04-17 17:40:52 +00:00
|
|
|
/**
|
|
|
|
* Called whenever the specified operation has failed finding a function
|
|
|
|
* containing the specified address. Should be overriden by subclasses.
|
2009-04-24 11:37:38 +00:00
|
|
|
* See the devtools.profiler.Profile.Operation enum for the list of
|
|
|
|
* possible operations.
|
2009-04-17 17:40:52 +00:00
|
|
|
*
|
2009-04-24 11:37:38 +00:00
|
|
|
* @param {number} operation Operation.
|
2009-04-17 17:40:52 +00:00
|
|
|
* @param {number} addr Address of the unknown code.
|
2009-04-24 11:37:38 +00:00
|
|
|
* @param {number} opt_stackPos If an unknown address is encountered
|
|
|
|
* during stack strace processing, specifies a position of the frame
|
|
|
|
* containing the address.
|
2009-04-17 17:40:52 +00:00
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.prototype.handleUnknownCode = function(
|
2009-04-24 11:37:38 +00:00
|
|
|
operation, addr, opt_stackPos) {
|
2009-04-17 17:40:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers static (library) code entry.
|
|
|
|
*
|
|
|
|
* @param {string} name Code entry name.
|
|
|
|
* @param {number} startAddr Starting address.
|
|
|
|
* @param {number} endAddr Ending address.
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.prototype.addStaticCode = function(
|
|
|
|
name, startAddr, endAddr) {
|
2009-04-24 11:37:38 +00:00
|
|
|
var entry = new devtools.profiler.CodeMap.CodeEntry(
|
|
|
|
endAddr - startAddr, name);
|
|
|
|
this.codeMap_.addStaticCode(startAddr, entry);
|
|
|
|
return entry;
|
2009-04-17 17:40:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers dynamic (JIT-compiled) code entry.
|
|
|
|
*
|
|
|
|
* @param {string} type Code entry type.
|
|
|
|
* @param {string} name Code entry name.
|
|
|
|
* @param {number} start Starting address.
|
|
|
|
* @param {number} size Code entry size.
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.prototype.addCode = function(
|
|
|
|
type, name, start, size) {
|
2009-04-24 11:37:38 +00:00
|
|
|
var entry = new devtools.profiler.Profile.DynamicCodeEntry(size, type, name);
|
|
|
|
this.codeMap_.addCode(start, entry);
|
|
|
|
return entry;
|
2009-04-17 17:40:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reports about moving of a dynamic code entry.
|
|
|
|
*
|
|
|
|
* @param {number} from Current code entry address.
|
|
|
|
* @param {number} to New code entry address.
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.prototype.moveCode = function(from, to) {
|
|
|
|
try {
|
|
|
|
this.codeMap_.moveCode(from, to);
|
|
|
|
} catch (e) {
|
2009-04-24 11:37:38 +00:00
|
|
|
this.handleUnknownCode(devtools.profiler.Profile.Operation.MOVE, from);
|
2009-04-17 17:40:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reports about deletion of a dynamic code entry.
|
|
|
|
*
|
|
|
|
* @param {number} start Starting address.
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.prototype.deleteCode = function(start) {
|
|
|
|
try {
|
|
|
|
this.codeMap_.deleteCode(start);
|
|
|
|
} catch (e) {
|
2009-04-24 11:37:38 +00:00
|
|
|
this.handleUnknownCode(devtools.profiler.Profile.Operation.DELETE, start);
|
2009-04-17 17:40:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Records a tick event. Stack must contain a sequence of
|
|
|
|
* addresses starting with the program counter value.
|
|
|
|
*
|
|
|
|
* @param {Array<number>} stack Stack sample.
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.prototype.recordTick = function(stack) {
|
|
|
|
var processedStack = this.resolveAndFilterFuncs_(stack);
|
|
|
|
this.bottomUpTree_.addPath(processedStack);
|
|
|
|
processedStack.reverse();
|
|
|
|
this.topDownTree_.addPath(processedStack);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates addresses into function names and filters unneeded
|
|
|
|
* functions.
|
|
|
|
*
|
|
|
|
* @param {Array<number>} stack Stack sample.
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.prototype.resolveAndFilterFuncs_ = function(stack) {
|
|
|
|
var result = [];
|
|
|
|
for (var i = 0; i < stack.length; ++i) {
|
|
|
|
var entry = this.codeMap_.findEntry(stack[i]);
|
|
|
|
if (entry) {
|
|
|
|
var name = entry.getName();
|
|
|
|
if (!this.skipThisFunction(name)) {
|
|
|
|
result.push(name);
|
|
|
|
}
|
|
|
|
} else {
|
2009-04-24 11:37:38 +00:00
|
|
|
this.handleUnknownCode(
|
|
|
|
devtools.profiler.Profile.Operation.TICK, stack[i], i);
|
2009-04-17 17:40:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-04-30 08:10:27 +00:00
|
|
|
* Performs a BF traversal of the top down call graph.
|
2009-04-17 17:40:52 +00:00
|
|
|
*
|
|
|
|
* @param {function(devtools.profiler.CallTree.Node)} f Visitor function.
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.prototype.traverseTopDownTree = function(f) {
|
|
|
|
this.topDownTree_.traverse(f);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-04-30 08:10:27 +00:00
|
|
|
* Performs a BF traversal of the bottom up call graph.
|
2009-04-17 17:40:52 +00:00
|
|
|
*
|
|
|
|
* @param {function(devtools.profiler.CallTree.Node)} f Visitor function.
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.prototype.traverseBottomUpTree = function(f) {
|
|
|
|
this.bottomUpTree_.traverse(f);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-04-24 11:37:38 +00:00
|
|
|
/**
|
2009-04-30 08:10:27 +00:00
|
|
|
* Calculates a top down profile for a node with the specified label.
|
|
|
|
* If no name specified, returns the whole top down calls tree.
|
2009-04-24 11:37:38 +00:00
|
|
|
*
|
2009-04-30 08:10:27 +00:00
|
|
|
* @param {string} opt_label Node label.
|
2009-04-24 11:37:38 +00:00
|
|
|
*/
|
2009-04-30 08:10:27 +00:00
|
|
|
devtools.profiler.Profile.prototype.getTopDownProfile = function(opt_label) {
|
|
|
|
return this.getTreeProfile_(this.topDownTree_, opt_label);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates a bottom up profile for a node with the specified label.
|
|
|
|
* If no name specified, returns the whole bottom up calls tree.
|
|
|
|
*
|
|
|
|
* @param {string} opt_label Node label.
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.prototype.getBottomUpProfile = function(opt_label) {
|
|
|
|
return this.getTreeProfile_(this.bottomUpTree_, opt_label);
|
2009-04-24 11:37:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-04-30 08:10:27 +00:00
|
|
|
* Helper function for calculating a tree profile.
|
2009-04-24 11:37:38 +00:00
|
|
|
*
|
2009-04-30 08:10:27 +00:00
|
|
|
* @param {devtools.profiler.Profile.CallTree} tree Call tree.
|
|
|
|
* @param {string} opt_label Node label.
|
2009-04-24 11:37:38 +00:00
|
|
|
*/
|
2009-04-30 08:10:27 +00:00
|
|
|
devtools.profiler.Profile.prototype.getTreeProfile_ = function(tree, opt_label) {
|
|
|
|
if (!opt_label) {
|
|
|
|
tree.computeTotalWeights();
|
|
|
|
return tree;
|
2009-04-24 11:37:38 +00:00
|
|
|
} else {
|
2009-04-30 08:10:27 +00:00
|
|
|
var subTree = tree.cloneSubtree(opt_label);
|
|
|
|
subTree.computeTotalWeights();
|
|
|
|
return subTree;
|
2009-04-24 11:37:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-04-17 17:40:52 +00:00
|
|
|
/**
|
2009-04-30 08:10:27 +00:00
|
|
|
* Calculates a flat profile of callees starting from a node with
|
|
|
|
* the specified label. If no name specified, starts from the root.
|
2009-04-17 17:40:52 +00:00
|
|
|
*
|
2009-04-30 08:10:27 +00:00
|
|
|
* @param {string} opt_label Starting node label.
|
2009-04-17 17:40:52 +00:00
|
|
|
*/
|
2009-04-30 08:10:27 +00:00
|
|
|
devtools.profiler.Profile.prototype.getFlatProfile = function(opt_label) {
|
2009-04-24 11:37:38 +00:00
|
|
|
var counters = new devtools.profiler.CallTree();
|
2009-04-30 08:10:27 +00:00
|
|
|
var rootLabel = opt_label || devtools.profiler.CallTree.ROOT_NODE_LABEL;
|
2009-04-17 17:40:52 +00:00
|
|
|
var precs = {};
|
2009-04-30 08:10:27 +00:00
|
|
|
precs[rootLabel] = 0;
|
|
|
|
var root = counters.findOrAddChild(rootLabel);
|
|
|
|
|
2009-04-17 17:40:52 +00:00
|
|
|
this.topDownTree_.computeTotalWeights();
|
|
|
|
this.topDownTree_.traverseInDepth(
|
|
|
|
function onEnter(node) {
|
|
|
|
if (!(node.label in precs)) {
|
|
|
|
precs[node.label] = 0;
|
|
|
|
}
|
2009-04-30 08:10:27 +00:00
|
|
|
var nodeLabelIsRootLabel = node.label == rootLabel;
|
|
|
|
if (nodeLabelIsRootLabel || precs[rootLabel] > 0) {
|
|
|
|
if (precs[rootLabel] == 0) {
|
|
|
|
root.selfWeight += node.selfWeight;
|
|
|
|
root.totalWeight += node.totalWeight;
|
|
|
|
} else {
|
|
|
|
var rec = root.findOrAddChild(node.label);
|
|
|
|
rec.selfWeight += node.selfWeight;
|
|
|
|
if (nodeLabelIsRootLabel || precs[node.label] == 0) {
|
|
|
|
rec.totalWeight += node.totalWeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
precs[node.label]++;
|
2009-04-17 17:40:52 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
function onExit(node) {
|
2009-04-30 08:10:27 +00:00
|
|
|
if (node.label == rootLabel || precs[rootLabel] > 0) {
|
|
|
|
precs[node.label]--;
|
|
|
|
}
|
2009-04-17 17:40:52 +00:00
|
|
|
},
|
2009-04-30 08:10:27 +00:00
|
|
|
null);
|
|
|
|
|
|
|
|
if (!opt_label) {
|
|
|
|
// If we have created a flat profile for the whole program, we don't
|
|
|
|
// need an explicit root in it. Thus, replace the counters tree
|
|
|
|
// root with the node corresponding to the whole program.
|
|
|
|
counters.root_ = root;
|
|
|
|
} else {
|
|
|
|
// Propagate weights so percents can be calculated correctly.
|
|
|
|
counters.getRoot().selfWeight = root.selfWeight;
|
|
|
|
counters.getRoot().totalWeight = root.totalWeight;
|
|
|
|
}
|
2009-04-24 11:37:38 +00:00
|
|
|
return counters;
|
2009-04-17 17:40:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a dynamic code entry.
|
|
|
|
*
|
|
|
|
* @param {number} size Code size.
|
|
|
|
* @param {string} type Code type.
|
|
|
|
* @param {string} name Function name.
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.DynamicCodeEntry = function(size, type, name) {
|
|
|
|
devtools.profiler.CodeMap.CodeEntry.call(this, size, name);
|
|
|
|
this.type = type;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns node name.
|
|
|
|
*/
|
|
|
|
devtools.profiler.Profile.DynamicCodeEntry.prototype.getName = function() {
|
|
|
|
var name = this.name;
|
|
|
|
if (name.length == 0) {
|
|
|
|
name = '<anonymous>';
|
|
|
|
} else if (name.charAt(0) == ' ') {
|
|
|
|
// An anonymous function with location: " aaa.js:10".
|
|
|
|
name = '<anonymous>' + name;
|
|
|
|
}
|
|
|
|
return this.type + ': ' + name;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a call graph.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree = function() {
|
2009-04-30 08:10:27 +00:00
|
|
|
this.root_ = new devtools.profiler.CallTree.Node(
|
|
|
|
devtools.profiler.CallTree.ROOT_NODE_LABEL);
|
2009-04-17 17:40:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-04-30 08:10:27 +00:00
|
|
|
/**
|
|
|
|
* The label of the root node.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.ROOT_NODE_LABEL = '';
|
|
|
|
|
|
|
|
|
2009-04-17 17:40:52 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.prototype.totalsComputed_ = false;
|
|
|
|
|
|
|
|
|
2009-04-24 11:37:38 +00:00
|
|
|
/**
|
|
|
|
* Returns the tree root.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.prototype.getRoot = function() {
|
|
|
|
return this.root_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-04-17 17:40:52 +00:00
|
|
|
/**
|
|
|
|
* Adds the specified call path, constructing nodes as necessary.
|
|
|
|
*
|
|
|
|
* @param {Array<string>} path Call path.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.prototype.addPath = function(path) {
|
|
|
|
if (path.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var curr = this.root_;
|
|
|
|
for (var i = 0; i < path.length; ++i) {
|
|
|
|
curr = curr.findOrAddChild(path[i]);
|
|
|
|
}
|
|
|
|
curr.selfWeight++;
|
|
|
|
this.totalsComputed_ = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-04-24 11:37:38 +00:00
|
|
|
/**
|
|
|
|
* Finds an immediate child of the specified parent with the specified
|
|
|
|
* label, creates a child node if necessary. If a parent node isn't
|
|
|
|
* specified, uses tree root.
|
|
|
|
*
|
|
|
|
* @param {string} label Child node label.
|
|
|
|
*/
|
2009-04-30 08:10:27 +00:00
|
|
|
devtools.profiler.CallTree.prototype.findOrAddChild = function(label) {
|
|
|
|
return this.root_.findOrAddChild(label);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a subtree by cloning and merging all subtrees rooted at nodes
|
|
|
|
* with a given label. E.g. cloning the following call tree on label 'A'
|
|
|
|
* will give the following result:
|
|
|
|
*
|
|
|
|
* <A>--<B> <B>
|
|
|
|
* / /
|
|
|
|
* <root> == clone on 'A' ==> <root>--<A>
|
|
|
|
* \ \
|
|
|
|
* <C>--<A>--<D> <D>
|
|
|
|
*
|
|
|
|
* And <A>'s selfWeight will be the sum of selfWeights of <A>'s from the
|
|
|
|
* source call tree.
|
|
|
|
*
|
|
|
|
* @param {string} label The label of the new root node.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.prototype.cloneSubtree = function(label) {
|
|
|
|
var subTree = new devtools.profiler.CallTree();
|
|
|
|
this.traverse(function(node, parent) {
|
|
|
|
if (!parent && node.label != label) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var child = (parent ? parent : subTree).findOrAddChild(node.label);
|
|
|
|
child.selfWeight += node.selfWeight;
|
|
|
|
return child;
|
|
|
|
});
|
|
|
|
return subTree;
|
2009-04-24 11:37:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-04-17 17:40:52 +00:00
|
|
|
/**
|
|
|
|
* Computes total weights in the call graph.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.prototype.computeTotalWeights = function() {
|
|
|
|
if (this.totalsComputed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.root_.computeTotalWeight();
|
|
|
|
this.totalsComputed_ = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-04-24 11:37:38 +00:00
|
|
|
* Traverses the call graph in preorder. This function can be used for
|
|
|
|
* building optionally modified tree clones. This is the boilerplate code
|
|
|
|
* for this scenario:
|
2009-04-17 17:40:52 +00:00
|
|
|
*
|
2009-04-24 11:37:38 +00:00
|
|
|
* callTree.traverse(function(node, parentClone) {
|
|
|
|
* var nodeClone = cloneNode(node);
|
|
|
|
* if (parentClone)
|
|
|
|
* parentClone.addChild(nodeClone);
|
|
|
|
* return nodeClone;
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* @param {function(devtools.profiler.CallTree.Node, *)} f Visitor function.
|
|
|
|
* The second parameter is the result of calling 'f' on the parent node.
|
2009-04-17 17:40:52 +00:00
|
|
|
*/
|
2009-04-30 08:10:27 +00:00
|
|
|
devtools.profiler.CallTree.prototype.traverse = function(f) {
|
2009-04-27 13:50:42 +00:00
|
|
|
var pairsToProcess = new ConsArray();
|
2009-04-30 08:10:27 +00:00
|
|
|
pairsToProcess.concat([{node: this.root_, param: null}]);
|
2009-04-27 13:50:42 +00:00
|
|
|
while (!pairsToProcess.atEnd()) {
|
|
|
|
var pair = pairsToProcess.next();
|
2009-04-24 11:37:38 +00:00
|
|
|
var node = pair.node;
|
|
|
|
var newParam = f(node, pair.param);
|
2009-04-27 13:50:42 +00:00
|
|
|
var morePairsToProcess = [];
|
|
|
|
node.forEachChild(function (child) {
|
|
|
|
morePairsToProcess.push({node: child, param: newParam}); });
|
|
|
|
pairsToProcess.concat(morePairsToProcess);
|
2009-04-17 17:40:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs an indepth call graph traversal.
|
|
|
|
*
|
|
|
|
* @param {function(devtools.profiler.CallTree.Node)} enter A function called
|
|
|
|
* prior to visiting node's children.
|
|
|
|
* @param {function(devtools.profiler.CallTree.Node)} exit A function called
|
|
|
|
* after visiting node's children.
|
|
|
|
*/
|
2009-04-30 08:10:27 +00:00
|
|
|
devtools.profiler.CallTree.prototype.traverseInDepth = function(enter, exit) {
|
2009-04-17 17:40:52 +00:00
|
|
|
function traverse(node) {
|
|
|
|
enter(node);
|
|
|
|
node.forEachChild(traverse);
|
|
|
|
exit(node);
|
|
|
|
}
|
2009-04-30 08:10:27 +00:00
|
|
|
traverse(this.root_);
|
2009-04-17 17:40:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a call graph node.
|
|
|
|
*
|
|
|
|
* @param {string} label Node label.
|
|
|
|
* @param {devtools.profiler.CallTree.Node} opt_parent Node parent.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.Node = function(label, opt_parent) {
|
|
|
|
this.label = label;
|
|
|
|
this.parent = opt_parent;
|
|
|
|
this.children = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Node self weight (how many times this node was the last node in
|
|
|
|
* a call path).
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.Node.prototype.selfWeight = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Node total weight (includes weights of all children).
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.Node.prototype.totalWeight = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a child node.
|
|
|
|
*
|
|
|
|
* @param {string} label Child node label.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.Node.prototype.addChild = function(label) {
|
|
|
|
var child = new devtools.profiler.CallTree.Node(label, this);
|
|
|
|
this.children[label] = child;
|
|
|
|
return child;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes node's total weight.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.Node.prototype.computeTotalWeight =
|
|
|
|
function() {
|
|
|
|
var totalWeight = this.selfWeight;
|
|
|
|
this.forEachChild(function(child) {
|
|
|
|
totalWeight += child.computeTotalWeight(); });
|
|
|
|
return this.totalWeight = totalWeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all node's children as an array.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.Node.prototype.exportChildren = function() {
|
|
|
|
var result = [];
|
|
|
|
this.forEachChild(function (node) { result.push(node); });
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds an immediate child with the specified label.
|
|
|
|
*
|
|
|
|
* @param {string} label Child node label.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.Node.prototype.findChild = function(label) {
|
|
|
|
return this.children[label] || null;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds an immediate child with the specified label, creates a child
|
|
|
|
* node if necessary.
|
|
|
|
*
|
|
|
|
* @param {string} label Child node label.
|
|
|
|
*/
|
2009-04-30 08:10:27 +00:00
|
|
|
devtools.profiler.CallTree.Node.prototype.findOrAddChild = function(label) {
|
2009-04-17 17:40:52 +00:00
|
|
|
return this.findChild(label) || this.addChild(label);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls the specified function for every child.
|
|
|
|
*
|
|
|
|
* @param {function(devtools.profiler.CallTree.Node)} f Visitor function.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.Node.prototype.forEachChild = function(f) {
|
|
|
|
for (var c in this.children) {
|
|
|
|
f(this.children[c]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walks up from the current node up to the call tree root.
|
|
|
|
*
|
|
|
|
* @param {function(devtools.profiler.CallTree.Node)} f Visitor function.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.Node.prototype.walkUpToRoot = function(f) {
|
|
|
|
for (var curr = this; curr != null; curr = curr.parent) {
|
|
|
|
f(curr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to find a node with the specified path.
|
|
|
|
*
|
|
|
|
* @param {Array<string>} labels The path.
|
|
|
|
* @param {function(devtools.profiler.CallTree.Node)} opt_f Visitor function.
|
|
|
|
*/
|
|
|
|
devtools.profiler.CallTree.Node.prototype.descendToChild = function(
|
|
|
|
labels, opt_f) {
|
|
|
|
for (var pos = 0, curr = this; pos < labels.length && curr != null; pos++) {
|
|
|
|
var child = curr.findChild(labels[pos]);
|
|
|
|
if (opt_f) {
|
|
|
|
opt_f(child, pos);
|
|
|
|
}
|
|
|
|
curr = child;
|
|
|
|
}
|
|
|
|
return curr;
|
|
|
|
};
|