87509c41f42609ec74949bde8ccb6fbb83877684
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Models / ProfileNode.js
1 /*
2  * Copyright (C) 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.ProfileNode = class ProfileNode extends WebInspector.Object
27 {
28     constructor(id, type, functionName, sourceCodeLocation, calls, childNodes)
29     {
30         super();
31
32         childNodes = childNodes || [];
33
34         console.assert(id);
35         console.assert(calls instanceof Array);
36         console.assert(calls.length >= 1);
37         console.assert(calls.reduce(function(previousValue, call) { return previousValue && call instanceof WebInspector.ProfileNodeCall; }, true));
38         console.assert(childNodes instanceof Array);
39         console.assert(childNodes.reduce(function(previousValue, node) { return previousValue && node instanceof WebInspector.ProfileNode; }, true));
40
41         this._id = id;
42         this._type = type || WebInspector.ProfileNode.Type.Function;
43         this._functionName = functionName || null;
44         this._sourceCodeLocation = sourceCodeLocation || null;
45         this._calls = calls;
46         this._childNodes = childNodes;
47         this._parentNode = null;
48         this._previousSibling = null;
49         this._nextSibling = null;
50         this._computedTotalTimes = false;
51
52         for (var i = 0; i < this._childNodes.length; ++i)
53             this._childNodes[i].establishRelationships(this, this._childNodes[i - 1], this._childNodes[i + 1]);
54
55         for (var i = 0; i < this._calls.length; ++i)
56             this._calls[i].establishRelationships(this, this._calls[i - 1], this._calls[i + 1]);
57     }
58
59     // Public
60
61     get id()
62     {
63         return this._id;
64     }
65
66     get type()
67     {
68         return this._type;
69     }
70
71     get functionName()
72     {
73         return this._functionName;
74     }
75
76     get sourceCodeLocation()
77     {
78         return this._sourceCodeLocation;
79     }
80
81     get startTime()
82     {
83         if (this._startTime === undefined)
84             this._startTime =  Math.max(0, this._calls[0].startTime);
85         return this._startTime;
86     }
87
88     get endTime()
89     {
90         if (this._endTime === undefined)
91             this._endTime = Math.min(this._calls.lastValue.endTime, Infinity);
92         return this._endTime;
93     }
94
95     get selfTime()
96     {
97         this._computeTotalTimesIfNeeded();
98         return this._selfTime;
99     }
100
101     get totalTime()
102     {
103         this._computeTotalTimesIfNeeded();
104         return this._totalTime;
105     }
106
107     get calls()
108     {
109         return this._calls;
110     }
111
112     get previousSibling()
113     {
114         return this._previousSibling;
115     }
116
117     get nextSibling()
118     {
119         return this._nextSibling;
120     }
121
122     get parentNode()
123     {
124         return this._parentNode;
125     }
126
127     get childNodes()
128     {
129         return this._childNodes;
130     }
131
132     computeCallInfoForTimeRange(rangeStartTime, rangeEndTime)
133     {
134         console.assert(typeof rangeStartTime === "number");
135         console.assert(typeof rangeEndTime === "number");
136
137         var recordCallCount = true;
138         var callCount = 0;
139
140         function totalTimeInRange(previousValue, call)
141         {
142             if (rangeStartTime > call.endTime || rangeEndTime < call.startTime)
143                 return previousValue;
144
145             if (recordCallCount)
146                 ++callCount;
147
148             return previousValue + Math.min(call.endTime, rangeEndTime) - Math.max(rangeStartTime, call.startTime);
149         }
150
151         var startTime = Math.max(rangeStartTime, this._calls[0].startTime);
152         var endTime = Math.min(this._calls.lastValue.endTime, rangeEndTime);
153         var totalTime = this._calls.reduce(totalTimeInRange, 0);
154
155         recordCallCount = false;
156
157         var childNodesTotalTime = 0;
158         for (var childNode of this._childNodes)
159             childNodesTotalTime += childNode.calls.reduce(totalTimeInRange, 0);
160
161         var selfTime = totalTime - childNodesTotalTime;
162         var averageTime = selfTime / callCount;
163
164         return {startTime, endTime, totalTime, selfTime, callCount, averageTime};
165     }
166
167     traverseNextProfileNode(stayWithin)
168     {
169         var profileNode = this._childNodes[0];
170         if (profileNode)
171             return profileNode;
172
173         if (this === stayWithin)
174             return null;
175
176         profileNode = this._nextSibling;
177         if (profileNode)
178             return profileNode;
179
180         profileNode = this;
181         while (profileNode && !profileNode.nextSibling && profileNode.parentNode !== stayWithin)
182             profileNode = profileNode.parentNode;
183
184         if (!profileNode)
185             return null;
186
187         return profileNode.nextSibling;
188     }
189
190     saveIdentityToCookie(cookie)
191     {
192         cookie[WebInspector.ProfileNode.TypeCookieKey] = this._type || null;
193         cookie[WebInspector.ProfileNode.FunctionNameCookieKey] = this._functionName || null;
194         cookie[WebInspector.ProfileNode.SourceCodeURLCookieKey] = this._sourceCodeLocation ? this._sourceCodeLocation.sourceCode.url ? this._sourceCodeLocation.sourceCode.url.hash : null : null;
195         cookie[WebInspector.ProfileNode.SourceCodeLocationLineCookieKey] = this._sourceCodeLocation ? this._sourceCodeLocation.lineNumber : null;
196         cookie[WebInspector.ProfileNode.SourceCodeLocationColumnCookieKey] = this._sourceCodeLocation ? this._sourceCodeLocation.columnNumber : null;
197     }
198
199     // Protected
200
201     establishRelationships(parentNode, previousSibling, nextSibling)
202     {
203         this._parentNode = parentNode || null;
204         this._previousSibling = previousSibling || null;
205         this._nextSibling = nextSibling || null;
206     }
207
208     // Private
209
210     _computeTotalTimes()
211     {
212         if (this._computedTotalTimes)
213             return;
214
215         this._computedTotalTimes = true;
216
217         var info = this.computeCallInfoForTimeRange(0, Infinity);
218         this._startTime = info.startTime;
219         this._endTime = info.endTime;
220         this._selfTime = info.selfTime;
221         this._totalTime = info.totalTime;
222     }
223 };
224
225 WebInspector.ProfileNode.Type = {
226     Function: "profile-node-type-function",
227     Program: "profile-node-type-program"
228 };
229
230 WebInspector.ProfileNode.TypeIdentifier = "profile-node";
231 WebInspector.ProfileNode.TypeCookieKey = "profile-node-type";
232 WebInspector.ProfileNode.FunctionNameCookieKey = "profile-node-function-name";
233 WebInspector.ProfileNode.SourceCodeURLCookieKey = "profile-node-source-code-url";
234 WebInspector.ProfileNode.SourceCodeLocationLineCookieKey = "profile-node-source-code-location-line";
235 WebInspector.ProfileNode.SourceCodeLocationColumnCookieKey = "profile-node-source-code-location-column";