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) { | |
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 = new ConsArray(); | |
104 nodesToTraverse.concat([this.head]); | |
105 while (!nodesToTraverse.atEnd()) { | |
106 var node = nodesToTraverse.next(); | |
107 f(node); | |
108 nodesToTraverse.concat(node.children); | |
109 } | |
110 }; | |
111 | |
112 | |
113 /** | |
114 * Constructs a Profile View node object. Each node object corresponds to | |
115 * a function call. | |
116 * | |
117 * @param {string} internalFuncName A fully qualified function name. | |
118 * @param {number} totalTime Amount of time that application spent in the | |
119 * corresponding function and its descendants (not that depending on | |
120 * profile they can be either callees or callers.) | |
121 * @param {number} selfTime Amount of time that application spent in the | |
122 * corresponding function only. | |
123 * @param {devtools.profiler.ProfileView.Node} head Profile view head. | |
124 * @constructor | |
125 */ | |
126 devtools.profiler.ProfileView.Node = function( | |
127 internalFuncName, totalTime, selfTime, head) { | |
128 this.internalFuncName = internalFuncName; | |
129 this.totalTime = totalTime; | |
130 this.selfTime = selfTime; | |
131 this.head = head; | |
132 this.parent = null; | |
133 this.children = []; | |
134 }; | |
135 | |
136 | |
137 /** | |
138 * Returns a share of the function's total time in application's total time. | |
139 */ | |
140 devtools.profiler.ProfileView.Node.prototype.__defineGetter__( | |
141 'totalPercent', | |
142 function() { return this.totalTime / | |
143 (this.head ? this.head.totalTime : this.totalTime) * 100.0; }); | |
144 | |
145 | |
146 /** | |
147 * Returns a share of the function's self time in application's total time. | |
148 */ | |
149 devtools.profiler.ProfileView.Node.prototype.__defineGetter__( | |
150 'selfPercent', | |
151 function() { return this.selfTime / | |
152 (this.head ? this.head.totalTime : this.totalTime) * 100.0; }); | |
153 | |
154 | |
155 /** | |
156 * Returns a share of the function's total time in its parent's total time. | |
157 */ | |
158 devtools.profiler.ProfileView.Node.prototype.__defineGetter__( | |
159 'parentTotalPercent', | |
160 function() { return this.totalTime / | |
161 (this.parent ? this.parent.totalTime : this.totalTime) * 100.0; }); | |
162 | |
163 | |
164 /** | |
165 * Adds a child to the node. | |
166 * | |
167 * @param {devtools.profiler.ProfileView.Node} node Child node. | |
168 */ | |
169 devtools.profiler.ProfileView.Node.prototype.addChild = function(node) { | |
170 node.parent = this; | |
171 this.children.push(node); | |
172 }; | |
173 | |
174 | |
175 /** | |
176 * Sorts all the node's children recursively. | |
177 * | |
178 * @param {function(devtools.profiler.ProfileView.Node, | |
179 * devtools.profiler.ProfileView.Node):number} sortFunc A sorting | |
180 * functions. Must comply with Array.sort sorting function requirements. | |
181 */ | |
182 devtools.profiler.ProfileView.Node.prototype.sortChildren = function( | |
183 sortFunc) { | |
184 this.children.sort(sortFunc); | |
185 }; | |
OLD | NEW |