0
|
1 |
/*
|
|
2 |
* Copyright (C) 2008 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 |
*
|
|
8 |
* 1. Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
11 |
* notice, this list of conditions and the following disclaimer in the
|
|
12 |
* documentation and/or other materials provided with the distribution.
|
|
13 |
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
|
|
14 |
* its contributors may be used to endorse or promote products derived
|
|
15 |
* from this software without specific prior written permission.
|
|
16 |
*
|
|
17 |
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
|
|
18 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
19 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
20 |
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
|
|
21 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
22 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
23 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
24 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
25 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
26 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27 |
*/
|
|
28 |
|
|
29 |
#include "config.h"
|
|
30 |
#include "ProfileNode.h"
|
|
31 |
|
|
32 |
// QTPROD-235: pick up proper Profiler.h file
|
|
33 |
#include "profiler/Profiler.h"
|
|
34 |
#include <stdio.h>
|
|
35 |
#include <wtf/DateMath.h>
|
|
36 |
|
|
37 |
#if PLATFORM(WIN_OS)
|
|
38 |
#include <windows.h>
|
|
39 |
#endif
|
|
40 |
|
|
41 |
namespace JSC {
|
|
42 |
|
|
43 |
static double getCount()
|
|
44 |
{
|
|
45 |
#if PLATFORM(WIN_OS)
|
|
46 |
static LARGE_INTEGER frequency = {0};
|
|
47 |
if (!frequency.QuadPart)
|
|
48 |
QueryPerformanceFrequency(&frequency);
|
|
49 |
LARGE_INTEGER counter;
|
|
50 |
QueryPerformanceCounter(&counter);
|
|
51 |
return static_cast<double>(counter.QuadPart) / frequency.QuadPart;
|
|
52 |
#else
|
|
53 |
return WTF::getCurrentUTCTimeWithMicroseconds();
|
|
54 |
#endif
|
|
55 |
}
|
|
56 |
|
|
57 |
ProfileNode::ProfileNode(const CallIdentifier& callIdentifier, ProfileNode* headNode, ProfileNode* parentNode)
|
|
58 |
: m_callIdentifier(callIdentifier)
|
|
59 |
, m_head(headNode)
|
|
60 |
, m_parent(parentNode)
|
|
61 |
, m_nextSibling(0)
|
|
62 |
, m_startTime(0.0)
|
|
63 |
, m_actualTotalTime(0.0)
|
|
64 |
, m_visibleTotalTime(0.0)
|
|
65 |
, m_actualSelfTime(0.0)
|
|
66 |
, m_visibleSelfTime(0.0)
|
|
67 |
, m_numberOfCalls(0)
|
|
68 |
, m_visible(true)
|
|
69 |
{
|
|
70 |
startTimer();
|
|
71 |
}
|
|
72 |
|
|
73 |
ProfileNode::ProfileNode(ProfileNode* headNode, ProfileNode* nodeToCopy)
|
|
74 |
: m_callIdentifier(nodeToCopy->callIdentifier())
|
|
75 |
, m_head(headNode)
|
|
76 |
, m_parent(nodeToCopy->parent())
|
|
77 |
, m_nextSibling(0)
|
|
78 |
, m_startTime(0.0)
|
|
79 |
, m_actualTotalTime(nodeToCopy->actualTotalTime())
|
|
80 |
, m_visibleTotalTime(nodeToCopy->totalTime())
|
|
81 |
, m_actualSelfTime(nodeToCopy->actualSelfTime())
|
|
82 |
, m_visibleSelfTime(nodeToCopy->selfTime())
|
|
83 |
, m_numberOfCalls(nodeToCopy->numberOfCalls())
|
|
84 |
, m_visible(nodeToCopy->visible())
|
|
85 |
{
|
|
86 |
}
|
|
87 |
|
|
88 |
ProfileNode* ProfileNode::willExecute(const CallIdentifier& callIdentifier)
|
|
89 |
{
|
|
90 |
for (StackIterator currentChild = m_children.begin(); currentChild != m_children.end(); ++currentChild) {
|
|
91 |
if ((*currentChild)->callIdentifier() == callIdentifier) {
|
|
92 |
(*currentChild)->startTimer();
|
|
93 |
return (*currentChild).get();
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
RefPtr<ProfileNode> newChild = ProfileNode::create(callIdentifier, m_head ? m_head : this, this); // If this ProfileNode has no head it is the head.
|
|
98 |
if (m_children.size())
|
|
99 |
m_children.last()->setNextSibling(newChild.get());
|
|
100 |
m_children.append(newChild.release());
|
|
101 |
return m_children.last().get();
|
|
102 |
}
|
|
103 |
|
|
104 |
ProfileNode* ProfileNode::didExecute()
|
|
105 |
{
|
|
106 |
endAndRecordCall();
|
|
107 |
return m_parent;
|
|
108 |
}
|
|
109 |
|
|
110 |
void ProfileNode::addChild(PassRefPtr<ProfileNode> prpChild)
|
|
111 |
{
|
|
112 |
RefPtr<ProfileNode> child = prpChild;
|
|
113 |
child->setParent(this);
|
|
114 |
if (m_children.size())
|
|
115 |
m_children.last()->setNextSibling(child.get());
|
|
116 |
m_children.append(child.release());
|
|
117 |
}
|
|
118 |
|
|
119 |
ProfileNode* ProfileNode::findChild(ProfileNode* node) const
|
|
120 |
{
|
|
121 |
if (!node)
|
|
122 |
return 0;
|
|
123 |
|
|
124 |
for (size_t i = 0; i < m_children.size(); ++i) {
|
|
125 |
if (*node == m_children[i].get())
|
|
126 |
return m_children[i].get();
|
|
127 |
}
|
|
128 |
|
|
129 |
return 0;
|
|
130 |
}
|
|
131 |
|
|
132 |
void ProfileNode::removeChild(ProfileNode* node)
|
|
133 |
{
|
|
134 |
if (!node)
|
|
135 |
return;
|
|
136 |
|
|
137 |
for (size_t i = 0; i < m_children.size(); ++i) {
|
|
138 |
if (*node == m_children[i].get()) {
|
|
139 |
m_children.remove(i);
|
|
140 |
break;
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
resetChildrensSiblings();
|
|
145 |
}
|
|
146 |
|
|
147 |
void ProfileNode::insertNode(PassRefPtr<ProfileNode> prpNode)
|
|
148 |
{
|
|
149 |
RefPtr<ProfileNode> node = prpNode;
|
|
150 |
|
|
151 |
for (unsigned i = 0; i < m_children.size(); ++i)
|
|
152 |
node->addChild(m_children[i].release());
|
|
153 |
|
|
154 |
m_children.clear();
|
|
155 |
m_children.append(node.release());
|
|
156 |
}
|
|
157 |
|
|
158 |
void ProfileNode::stopProfiling()
|
|
159 |
{
|
|
160 |
if (m_startTime)
|
|
161 |
endAndRecordCall();
|
|
162 |
|
|
163 |
m_visibleTotalTime = m_actualTotalTime;
|
|
164 |
|
|
165 |
ASSERT(m_actualSelfTime == 0.0 && m_startTime == 0.0);
|
|
166 |
|
|
167 |
// Because we iterate in post order all of our children have been stopped before us.
|
|
168 |
for (unsigned i = 0; i < m_children.size(); ++i)
|
|
169 |
m_actualSelfTime += m_children[i]->totalTime();
|
|
170 |
|
|
171 |
ASSERT(m_actualSelfTime <= m_actualTotalTime);
|
|
172 |
m_actualSelfTime = m_actualTotalTime - m_actualSelfTime;
|
|
173 |
m_visibleSelfTime = m_actualSelfTime;
|
|
174 |
}
|
|
175 |
|
|
176 |
ProfileNode* ProfileNode::traverseNextNodePostOrder() const
|
|
177 |
{
|
|
178 |
ProfileNode* next = m_nextSibling;
|
|
179 |
if (!next)
|
|
180 |
return m_parent;
|
|
181 |
while (ProfileNode* firstChild = next->firstChild())
|
|
182 |
next = firstChild;
|
|
183 |
return next;
|
|
184 |
}
|
|
185 |
|
|
186 |
ProfileNode* ProfileNode::traverseNextNodePreOrder(bool processChildren) const
|
|
187 |
{
|
|
188 |
if (processChildren && m_children.size())
|
|
189 |
return m_children[0].get();
|
|
190 |
|
|
191 |
if (m_nextSibling)
|
|
192 |
return m_nextSibling;
|
|
193 |
|
|
194 |
ProfileNode* nextParent = m_parent;
|
|
195 |
if (!nextParent)
|
|
196 |
return 0;
|
|
197 |
|
|
198 |
ProfileNode* next;
|
|
199 |
for (next = m_parent->nextSibling(); !next; next = nextParent->nextSibling()) {
|
|
200 |
nextParent = nextParent->parent();
|
|
201 |
if (!nextParent)
|
|
202 |
return 0;
|
|
203 |
}
|
|
204 |
|
|
205 |
return next;
|
|
206 |
}
|
|
207 |
|
|
208 |
void ProfileNode::setTreeVisible(ProfileNode* node, bool visible)
|
|
209 |
{
|
|
210 |
ProfileNode* nodeParent = node->parent();
|
|
211 |
ProfileNode* nodeSibling = node->nextSibling();
|
|
212 |
node->setParent(0);
|
|
213 |
node->setNextSibling(0);
|
|
214 |
|
|
215 |
for (ProfileNode* currentNode = node; currentNode; currentNode = currentNode->traverseNextNodePreOrder())
|
|
216 |
currentNode->setVisible(visible);
|
|
217 |
|
|
218 |
node->setParent(nodeParent);
|
|
219 |
node->setNextSibling(nodeSibling);
|
|
220 |
}
|
|
221 |
|
|
222 |
void ProfileNode::calculateVisibleTotalTime()
|
|
223 |
{
|
|
224 |
double sumOfVisibleChildrensTime = 0.0;
|
|
225 |
|
|
226 |
for (unsigned i = 0; i < m_children.size(); ++i) {
|
|
227 |
if (m_children[i]->visible())
|
|
228 |
sumOfVisibleChildrensTime += m_children[i]->totalTime();
|
|
229 |
}
|
|
230 |
|
|
231 |
m_visibleTotalTime = m_visibleSelfTime + sumOfVisibleChildrensTime;
|
|
232 |
}
|
|
233 |
|
|
234 |
bool ProfileNode::focus(const CallIdentifier& callIdentifier)
|
|
235 |
{
|
|
236 |
if (!m_visible)
|
|
237 |
return false;
|
|
238 |
|
|
239 |
if (m_callIdentifier != callIdentifier) {
|
|
240 |
m_visible = false;
|
|
241 |
return true;
|
|
242 |
}
|
|
243 |
|
|
244 |
for (ProfileNode* currentParent = m_parent; currentParent; currentParent = currentParent->parent())
|
|
245 |
currentParent->setVisible(true);
|
|
246 |
|
|
247 |
return false;
|
|
248 |
}
|
|
249 |
|
|
250 |
void ProfileNode::exclude(const CallIdentifier& callIdentifier)
|
|
251 |
{
|
|
252 |
if (m_visible && m_callIdentifier == callIdentifier) {
|
|
253 |
setTreeVisible(this, false);
|
|
254 |
|
|
255 |
m_parent->setVisibleSelfTime(m_parent->selfTime() + m_visibleTotalTime);
|
|
256 |
}
|
|
257 |
}
|
|
258 |
|
|
259 |
void ProfileNode::restore()
|
|
260 |
{
|
|
261 |
m_visibleTotalTime = m_actualTotalTime;
|
|
262 |
m_visibleSelfTime = m_actualSelfTime;
|
|
263 |
m_visible = true;
|
|
264 |
}
|
|
265 |
|
|
266 |
void ProfileNode::endAndRecordCall()
|
|
267 |
{
|
|
268 |
m_actualTotalTime += m_startTime ? getCount() - m_startTime : 0.0;
|
|
269 |
m_startTime = 0.0;
|
|
270 |
|
|
271 |
++m_numberOfCalls;
|
|
272 |
}
|
|
273 |
|
|
274 |
void ProfileNode::startTimer()
|
|
275 |
{
|
|
276 |
if (!m_startTime)
|
|
277 |
m_startTime = getCount();
|
|
278 |
}
|
|
279 |
|
|
280 |
void ProfileNode::resetChildrensSiblings()
|
|
281 |
{
|
|
282 |
unsigned size = m_children.size();
|
|
283 |
for (unsigned i = 0; i < size; ++i)
|
|
284 |
m_children[i]->setNextSibling(i + 1 == size ? 0 : m_children[i + 1].get());
|
|
285 |
}
|
|
286 |
|
|
287 |
#ifndef NDEBUG
|
|
288 |
void ProfileNode::debugPrintData(int indentLevel) const
|
|
289 |
{
|
|
290 |
// Print function names
|
|
291 |
for (int i = 0; i < indentLevel; ++i)
|
|
292 |
printf(" ");
|
|
293 |
|
|
294 |
printf("Function Name %s %d SelfTime %.3fms/%.3f%% TotalTime %.3fms/%.3f%% VSelf %.3fms VTotal %.3fms Visible %s Next Sibling %s\n",
|
|
295 |
functionName().UTF8String().c_str(),
|
|
296 |
m_numberOfCalls, m_actualSelfTime, selfPercent(), m_actualTotalTime, totalPercent(),
|
|
297 |
m_visibleSelfTime, m_visibleTotalTime,
|
|
298 |
(m_visible ? "True" : "False"),
|
|
299 |
m_nextSibling ? m_nextSibling->functionName().UTF8String().c_str() : "");
|
|
300 |
|
|
301 |
++indentLevel;
|
|
302 |
|
|
303 |
// Print children's names and information
|
|
304 |
for (StackIterator currentChild = m_children.begin(); currentChild != m_children.end(); ++currentChild)
|
|
305 |
(*currentChild)->debugPrintData(indentLevel);
|
|
306 |
}
|
|
307 |
|
|
308 |
// print the profiled data in a format that matches the tool sample's output.
|
|
309 |
double ProfileNode::debugPrintDataSampleStyle(int indentLevel, FunctionCallHashCount& countedFunctions) const
|
|
310 |
{
|
|
311 |
printf(" ");
|
|
312 |
|
|
313 |
// Print function names
|
|
314 |
const char* name = functionName().UTF8String().c_str();
|
|
315 |
double sampleCount = m_actualTotalTime * 1000;
|
|
316 |
if (indentLevel) {
|
|
317 |
for (int i = 0; i < indentLevel; ++i)
|
|
318 |
printf(" ");
|
|
319 |
|
|
320 |
countedFunctions.add(functionName().rep());
|
|
321 |
|
|
322 |
printf("%.0f %s\n", sampleCount ? sampleCount : 1, name);
|
|
323 |
} else
|
|
324 |
printf("%s\n", name);
|
|
325 |
|
|
326 |
++indentLevel;
|
|
327 |
|
|
328 |
// Print children's names and information
|
|
329 |
double sumOfChildrensCount = 0.0;
|
|
330 |
for (StackIterator currentChild = m_children.begin(); currentChild != m_children.end(); ++currentChild)
|
|
331 |
sumOfChildrensCount += (*currentChild)->debugPrintDataSampleStyle(indentLevel, countedFunctions);
|
|
332 |
|
|
333 |
sumOfChildrensCount *= 1000; //
|
|
334 |
// Print remainder of samples to match sample's output
|
|
335 |
if (sumOfChildrensCount < sampleCount) {
|
|
336 |
printf(" ");
|
|
337 |
while (indentLevel--)
|
|
338 |
printf(" ");
|
|
339 |
|
|
340 |
printf("%.0f %s\n", sampleCount - sumOfChildrensCount, functionName().UTF8String().c_str());
|
|
341 |
}
|
|
342 |
|
|
343 |
return m_actualTotalTime;
|
|
344 |
}
|
|
345 |
#endif
|
|
346 |
|
|
347 |
} // namespace JSC
|