OLD | NEW |
---|---|
(Empty) | |
1 // 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 | |
29 // Initlialize namespaces | |
30 var devtools = devtools || {}; | |
31 devtools.profiler = devtools.profiler || {}; | |
32 | |
33 | |
34 /** | |
35 * Creates a Profile View builder object. | |
36 * | |
37 * @param {number} samplingRate Number of ms between profiler ticks. | |
38 * @constructor | |
39 */ | |
40 devtools.profiler.ViewBuilder = function(samplingRate) { | |
41 this.samplingRate = samplingRate; | |
42 }; | |
43 | |
44 | |
45 /** | |
46 * Builds a profile view for the specified call tree. | |
47 * | |
48 * @param {devtools.profiler.CallTree} callTree A call tree. | |
49 */ | |
50 devtools.profiler.ViewBuilder.prototype.buildView = function( | |
51 callTree) { | |
52 var head; | |
53 var samplingRate = this.samplingRate; | |
54 callTree.traverse(function(node, viewParent) { | |
Søren Thygesen Gjesse
2009/04/24 10:55:20
I woud prefer to have this function defined as an
Mikhail Naganov
2009/04/24 11:34:23
I see two obstacles to this. First, inside 'traver
| |
55 var viewNode = new devtools.profiler.ProfileView.Node( | |
56 node.label, node.totalWeight * samplingRate, | |
57 node.selfWeight * samplingRate, head); | |
58 if (viewParent) { | |
59 viewParent.addChild(viewNode); | |
60 } else { | |
61 head = viewNode; | |
62 } | |
63 return viewNode; | |
64 }); | |
65 var view = new devtools.profiler.ProfileView(head); | |
66 return view; | |
67 }; | |
68 | |
69 | |
70 /** | |
71 * Creates a Profile View object. It allows to perform sorting | |
72 * and filtering actions on the profile. Profile View mimicks | |
73 * the Profile object from WebKit's JSC profiler. | |
74 * | |
75 * @param {devtools.profiler.ProfileView.Node} head Head (root) node. | |
76 * @constructor | |
77 */ | |
78 devtools.profiler.ProfileView = function(head) { | |
79 this.head = head; | |
80 }; | |
81 | |
82 | |
83 /** | |
84 * Sorts the profile view using the specified sort function. | |
85 * | |
86 * @param {function(devtools.profiler.ProfileView.Node, | |
87 * devtools.profiler.ProfileView.Node):number} sortFunc A sorting | |
88 * functions. Must comply with Array.sort sorting function requirements. | |
89 */ | |
90 devtools.profiler.ProfileView.prototype.sort = function(sortFunc) { | |
91 this.traverse(function (node) { | |
92 node.sortChildren(sortFunc); | |
93 }); | |
94 }; | |
95 | |
96 | |
97 /** | |
98 * Traverses profile view nodes in preorder. | |
99 * | |
100 * @param {function(devtools.profiler.ProfileView.Node)} f Visitor function. | |
101 */ | |
102 devtools.profiler.ProfileView.prototype.traverse = function(f) { | |
103 var nodesToTraverse = [this.head]; | |
104 while (nodesToTraverse.length > 0) { | |
105 var node = nodesToTraverse.shift(); | |
106 f(node); | |
107 nodesToTraverse = nodesToTraverse.concat(node.children); | |
108 } | |
109 }; | |
110 | |
111 | |
112 /** | |
113 * Constructs a Profile View node object. Each node object corresponds to | |
114 * a function call. | |
115 * | |
116 * @param {string} internalFuncName A fully qualified function name. | |
117 * @param {number} totalTime Amount of time that application spent in the | |
118 * corresponding function and its descendants (not that depending on | |
119 * profile they can be either callees or callers.) | |
120 * @param {number} selfTime Amount of time that application spent in the | |
121 * corresponding function only. | |
122 * @param {devtools.profiler.ProfileView.Node} head Profile view head. | |
123 * @constructor | |
124 */ | |
125 devtools.profiler.ProfileView.Node = function( | |
126 internalFuncName, totalTime, selfTime, head) { | |
127 this.internalFuncName = internalFuncName; | |
128 this.totalTime = totalTime; | |
129 this.selfTime = selfTime; | |
130 this.head = head; | |
131 this.parent = null; | |
132 this.children = []; | |
133 }; | |
134 | |
135 | |
136 /** | |
137 * Returns a share of the function's total time in application's total time. | |
138 */ | |
139 devtools.profiler.ProfileView.Node.prototype.__defineGetter__( | |
140 'totalPercent', | |
141 function() { return this.totalTime / | |
142 (this.head ? this.head.totalTime : this.totalTime) * 100.0; }); | |
143 | |
144 | |
145 /** | |
146 * Returns a share of the function's self time in application's total time. | |
147 */ | |
148 devtools.profiler.ProfileView.Node.prototype.__defineGetter__( | |
149 'selfPercent', | |
150 function() { return this.selfTime / | |
151 (this.head ? this.head.totalTime : this.totalTime) * 100.0; }); | |
152 | |
153 | |
154 /** | |
155 * Returns a share of the function's total time in its parent's total time. | |
156 */ | |
157 devtools.profiler.ProfileView.Node.prototype.__defineGetter__( | |
158 'parentTotalPercent', | |
159 function() { return this.totalTime / | |
160 (this.parent ? this.parent.totalTime : this.totalTime) * 100.0; }); | |
161 | |
162 | |
163 /** | |
164 * Adds a child to the node. | |
165 * | |
166 * @param {devtools.profiler.ProfileView.Node} node Child node. | |
167 */ | |
168 devtools.profiler.ProfileView.Node.prototype.addChild = function(node) { | |
169 node.parent = this; | |
170 this.children.push(node); | |
171 }; | |
172 | |
173 | |
174 /** | |
175 * Sorts all the node's children recursively. | |
176 * | |
177 * @param {function(devtools.profiler.ProfileView.Node, | |
178 * devtools.profiler.ProfileView.Node):number} sortFunc A sorting | |
179 * functions. Must comply with Array.sort sorting function requirements. | |
180 */ | |
181 devtools.profiler.ProfileView.Node.prototype.sortChildren = function( | |
182 sortFunc) { | |
183 this.children.sort(sortFunc); | |
184 }; | |
OLD | NEW |