blob: d1545acd99699adc8672afad809a969105ab0f74 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
Steve Blocka7e24c12009-10-30 11:49:00 +000029/**
30 * Creates a Profile View builder object.
31 *
32 * @param {number} samplingRate Number of ms between profiler ticks.
33 * @constructor
34 */
Steve Block1e0659c2011-05-24 12:43:12 +010035function ViewBuilder(samplingRate) {
Steve Blocka7e24c12009-10-30 11:49:00 +000036 this.samplingRate = samplingRate;
37};
38
39
40/**
41 * Builds a profile view for the specified call tree.
42 *
Steve Block1e0659c2011-05-24 12:43:12 +010043 * @param {CallTree} callTree A call tree.
Steve Blocka7e24c12009-10-30 11:49:00 +000044 * @param {boolean} opt_bottomUpViewWeights Whether remapping
45 * of self weights for a bottom up view is needed.
46 */
Steve Block1e0659c2011-05-24 12:43:12 +010047ViewBuilder.prototype.buildView = function(
Steve Blocka7e24c12009-10-30 11:49:00 +000048 callTree, opt_bottomUpViewWeights) {
49 var head;
50 var samplingRate = this.samplingRate;
51 var createViewNode = this.createViewNode;
52 callTree.traverse(function(node, viewParent) {
53 var totalWeight = node.totalWeight * samplingRate;
54 var selfWeight = node.selfWeight * samplingRate;
55 if (opt_bottomUpViewWeights === true) {
56 if (viewParent === head) {
57 selfWeight = totalWeight;
58 } else {
59 selfWeight = 0;
60 }
61 }
62 var viewNode = createViewNode(node.label, totalWeight, selfWeight, head);
63 if (viewParent) {
64 viewParent.addChild(viewNode);
65 } else {
66 head = viewNode;
67 }
68 return viewNode;
69 });
70 var view = this.createView(head);
71 return view;
72};
73
74
75/**
76 * Factory method for a profile view.
77 *
Steve Block1e0659c2011-05-24 12:43:12 +010078 * @param {ProfileView.Node} head View head node.
79 * @return {ProfileView} Profile view.
Steve Blocka7e24c12009-10-30 11:49:00 +000080 */
Steve Block1e0659c2011-05-24 12:43:12 +010081ViewBuilder.prototype.createView = function(head) {
82 return new ProfileView(head);
Steve Blocka7e24c12009-10-30 11:49:00 +000083};
84
85
86/**
87 * Factory method for a profile view node.
88 *
89 * @param {string} internalFuncName A fully qualified function name.
90 * @param {number} totalTime Amount of time that application spent in the
91 * corresponding function and its descendants (not that depending on
92 * profile they can be either callees or callers.)
93 * @param {number} selfTime Amount of time that application spent in the
94 * corresponding function only.
Steve Block1e0659c2011-05-24 12:43:12 +010095 * @param {ProfileView.Node} head Profile view head.
96 * @return {ProfileView.Node} Profile view node.
Steve Blocka7e24c12009-10-30 11:49:00 +000097 */
Steve Block1e0659c2011-05-24 12:43:12 +010098ViewBuilder.prototype.createViewNode = function(
Steve Blocka7e24c12009-10-30 11:49:00 +000099 funcName, totalTime, selfTime, head) {
Steve Block1e0659c2011-05-24 12:43:12 +0100100 return new ProfileView.Node(
Steve Blocka7e24c12009-10-30 11:49:00 +0000101 funcName, totalTime, selfTime, head);
102};
103
104
105/**
106 * Creates a Profile View object. It allows to perform sorting
107 * and filtering actions on the profile.
108 *
Steve Block1e0659c2011-05-24 12:43:12 +0100109 * @param {ProfileView.Node} head Head (root) node.
Steve Blocka7e24c12009-10-30 11:49:00 +0000110 * @constructor
111 */
Steve Block1e0659c2011-05-24 12:43:12 +0100112function ProfileView(head) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 this.head = head;
114};
115
116
117/**
118 * Sorts the profile view using the specified sort function.
119 *
Steve Block1e0659c2011-05-24 12:43:12 +0100120 * @param {function(ProfileView.Node,
121 * ProfileView.Node):number} sortFunc A sorting
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 * functions. Must comply with Array.sort sorting function requirements.
123 */
Steve Block1e0659c2011-05-24 12:43:12 +0100124ProfileView.prototype.sort = function(sortFunc) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000125 this.traverse(function (node) {
126 node.sortChildren(sortFunc);
127 });
128};
129
130
131/**
132 * Traverses profile view nodes in preorder.
133 *
Steve Block1e0659c2011-05-24 12:43:12 +0100134 * @param {function(ProfileView.Node)} f Visitor function.
Steve Blocka7e24c12009-10-30 11:49:00 +0000135 */
Steve Block1e0659c2011-05-24 12:43:12 +0100136ProfileView.prototype.traverse = function(f) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 var nodesToTraverse = new ConsArray();
138 nodesToTraverse.concat([this.head]);
139 while (!nodesToTraverse.atEnd()) {
140 var node = nodesToTraverse.next();
141 f(node);
142 nodesToTraverse.concat(node.children);
143 }
144};
145
146
147/**
148 * Constructs a Profile View node object. Each node object corresponds to
149 * a function call.
150 *
151 * @param {string} internalFuncName A fully qualified function name.
152 * @param {number} totalTime Amount of time that application spent in the
153 * corresponding function and its descendants (not that depending on
154 * profile they can be either callees or callers.)
155 * @param {number} selfTime Amount of time that application spent in the
156 * corresponding function only.
Steve Block1e0659c2011-05-24 12:43:12 +0100157 * @param {ProfileView.Node} head Profile view head.
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 * @constructor
159 */
Steve Block1e0659c2011-05-24 12:43:12 +0100160ProfileView.Node = function(
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 internalFuncName, totalTime, selfTime, head) {
162 this.internalFuncName = internalFuncName;
163 this.totalTime = totalTime;
164 this.selfTime = selfTime;
165 this.head = head;
166 this.parent = null;
167 this.children = [];
168};
169
170
171/**
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 * Returns a share of the function's total time in its parent's total time.
173 */
Steve Block1e0659c2011-05-24 12:43:12 +0100174ProfileView.Node.prototype.__defineGetter__(
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 'parentTotalPercent',
176 function() { return this.totalTime /
177 (this.parent ? this.parent.totalTime : this.totalTime) * 100.0; });
178
179
180/**
181 * Adds a child to the node.
182 *
Steve Block1e0659c2011-05-24 12:43:12 +0100183 * @param {ProfileView.Node} node Child node.
Steve Blocka7e24c12009-10-30 11:49:00 +0000184 */
Steve Block1e0659c2011-05-24 12:43:12 +0100185ProfileView.Node.prototype.addChild = function(node) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000186 node.parent = this;
187 this.children.push(node);
188};
189
190
191/**
192 * Sorts all the node's children recursively.
193 *
Steve Block1e0659c2011-05-24 12:43:12 +0100194 * @param {function(ProfileView.Node,
195 * ProfileView.Node):number} sortFunc A sorting
Steve Blocka7e24c12009-10-30 11:49:00 +0000196 * functions. Must comply with Array.sort sorting function requirements.
197 */
Steve Block1e0659c2011-05-24 12:43:12 +0100198ProfileView.Node.prototype.sortChildren = function(
Steve Blocka7e24c12009-10-30 11:49:00 +0000199 sortFunc) {
200 this.children.sort(sortFunc);
201};