blob: d1545acd99699adc8672afad809a969105ab0f74 [file] [log] [blame]
ager@chromium.org3a37e9b2009-04-27 09:26:21 +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
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000029/**
30 * Creates a Profile View builder object.
31 *
32 * @param {number} samplingRate Number of ms between profiler ticks.
33 * @constructor
34 */
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +000035function ViewBuilder(samplingRate) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000036 this.samplingRate = samplingRate;
37};
38
39
40/**
41 * Builds a profile view for the specified call tree.
42 *
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +000043 * @param {CallTree} callTree A call tree.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000044 * @param {boolean} opt_bottomUpViewWeights Whether remapping
45 * of self weights for a bottom up view is needed.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000046 */
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +000047ViewBuilder.prototype.buildView = function(
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000048 callTree, opt_bottomUpViewWeights) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000049 var head;
50 var samplingRate = this.samplingRate;
ager@chromium.orge2902be2009-06-08 12:21:35 +000051 var createViewNode = this.createViewNode;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000052 callTree.traverse(function(node, viewParent) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000053 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 }
ager@chromium.orge2902be2009-06-08 12:21:35 +000062 var viewNode = createViewNode(node.label, totalWeight, selfWeight, head);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000063 if (viewParent) {
64 viewParent.addChild(viewNode);
65 } else {
66 head = viewNode;
67 }
68 return viewNode;
69 });
ager@chromium.orge2902be2009-06-08 12:21:35 +000070 var view = this.createView(head);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000071 return view;
72};
73
74
75/**
ager@chromium.orge2902be2009-06-08 12:21:35 +000076 * Factory method for a profile view.
77 *
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +000078 * @param {ProfileView.Node} head View head node.
79 * @return {ProfileView} Profile view.
ager@chromium.orge2902be2009-06-08 12:21:35 +000080 */
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +000081ViewBuilder.prototype.createView = function(head) {
82 return new ProfileView(head);
ager@chromium.orge2902be2009-06-08 12:21:35 +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.
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +000095 * @param {ProfileView.Node} head Profile view head.
96 * @return {ProfileView.Node} Profile view node.
ager@chromium.orge2902be2009-06-08 12:21:35 +000097 */
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +000098ViewBuilder.prototype.createViewNode = function(
ager@chromium.orge2902be2009-06-08 12:21:35 +000099 funcName, totalTime, selfTime, head) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000100 return new ProfileView.Node(
ager@chromium.orge2902be2009-06-08 12:21:35 +0000101 funcName, totalTime, selfTime, head);
102};
103
104
105/**
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000106 * Creates a Profile View object. It allows to perform sorting
ager@chromium.orge2902be2009-06-08 12:21:35 +0000107 * and filtering actions on the profile.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000108 *
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000109 * @param {ProfileView.Node} head Head (root) node.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000110 * @constructor
111 */
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000112function ProfileView(head) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000113 this.head = head;
ager@chromium.org5ec48922009-05-05 07:25:34 +0000114};
115
116
117/**
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000118 * Sorts the profile view using the specified sort function.
119 *
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000120 * @param {function(ProfileView.Node,
121 * ProfileView.Node):number} sortFunc A sorting
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000122 * functions. Must comply with Array.sort sorting function requirements.
123 */
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000124ProfileView.prototype.sort = function(sortFunc) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000125 this.traverse(function (node) {
126 node.sortChildren(sortFunc);
127 });
128};
129
130
131/**
132 * Traverses profile view nodes in preorder.
133 *
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000134 * @param {function(ProfileView.Node)} f Visitor function.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000135 */
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000136ProfileView.prototype.traverse = function(f) {
ager@chromium.org5ec48922009-05-05 07:25:34 +0000137 var nodesToTraverse = new ConsArray();
138 nodesToTraverse.concat([this.head]);
139 while (!nodesToTraverse.atEnd()) {
140 var node = nodesToTraverse.next();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000141 f(node);
ager@chromium.org5ec48922009-05-05 07:25:34 +0000142 nodesToTraverse.concat(node.children);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000143 }
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.
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000157 * @param {ProfileView.Node} head Profile view head.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000158 * @constructor
159 */
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000160ProfileView.Node = function(
ager@chromium.org3a37e9b2009-04-27 09:26:21 +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 = [];
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000168};
169
170
171/**
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000172 * Returns a share of the function's total time in its parent's total time.
173 */
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000174ProfileView.Node.prototype.__defineGetter__(
ager@chromium.org3a37e9b2009-04-27 09:26:21 +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 *
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000183 * @param {ProfileView.Node} node Child node.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000184 */
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000185ProfileView.Node.prototype.addChild = function(node) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000186 node.parent = this;
187 this.children.push(node);
188};
189
190
191/**
192 * Sorts all the node's children recursively.
193 *
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000194 * @param {function(ProfileView.Node,
195 * ProfileView.Node):number} sortFunc A sorting
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000196 * functions. Must comply with Array.sort sorting function requirements.
197 */
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000198ProfileView.Node.prototype.sortChildren = function(
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000199 sortFunc) {
200 this.children.sort(sortFunc);
201};