2 * Copyright (C) 2008, 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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 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.
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.
32 #include "CallIdentifier.h"
33 #include <wtf/HashCountedSet.h>
34 #include <wtf/RefCounted.h>
35 #include <wtf/RefPtr.h>
36 #include <wtf/Vector.h>
43 typedef HashCountedSet
<StringImpl
*> FunctionCallHashCount
;
45 class ProfileNode
: public RefCounted
<ProfileNode
> {
47 static PassRefPtr
<ProfileNode
> create(ExecState
* callerCallFrame
, const CallIdentifier
& callIdentifier
, ProfileNode
* headNode
, ProfileNode
* parentNode
)
49 return adoptRef(new ProfileNode(callerCallFrame
, callIdentifier
, headNode
, parentNode
));
52 static PassRefPtr
<ProfileNode
> create(ExecState
* callerCallFrame
, ProfileNode
* headNode
, ProfileNode
* node
)
54 return adoptRef(new ProfileNode(callerCallFrame
, headNode
, node
));
59 Call(double startTime
, double totalTime
= NAN
)
60 : m_startTime(startTime
)
61 , m_totalTime(totalTime
)
65 double startTime() const { return m_startTime
; }
66 void setStartTime(double time
) { m_startTime
= time
; }
68 double totalTime() const { return m_totalTime
; }
69 void setTotalTime(double time
) { m_totalTime
= time
; }
76 bool operator==(ProfileNode
* node
) { return m_callIdentifier
== node
->callIdentifier(); }
78 ProfileNode
* willExecute(ExecState
* callerCallFrame
, const CallIdentifier
&);
79 ProfileNode
* didExecute();
83 // CallIdentifier members
84 ExecState
* callerCallFrame() const { return m_callerCallFrame
; }
85 const CallIdentifier
& callIdentifier() const { return m_callIdentifier
; }
86 unsigned id() const { return m_callIdentifier
.hash(); }
87 const String
& functionName() const { return m_callIdentifier
.functionName(); }
88 const String
& url() const { return m_callIdentifier
.url(); }
89 unsigned lineNumber() const { return m_callIdentifier
.lineNumber(); }
90 unsigned columnNumber() const { return m_callIdentifier
.columnNumber(); }
93 ProfileNode
* head() const { return m_head
; }
94 void setHead(ProfileNode
* head
) { m_head
= head
; }
96 ProfileNode
* parent() const { return m_parent
; }
97 void setParent(ProfileNode
* parent
) { m_parent
= parent
; }
99 ProfileNode
* nextSibling() const { return m_nextSibling
; }
100 void setNextSibling(ProfileNode
* nextSibling
) { m_nextSibling
= nextSibling
; }
103 double totalTime() const { return m_totalTime
; }
104 void setTotalTime(double time
) { m_totalTime
= time
; }
106 double selfTime() const { return m_selfTime
; }
107 void setSelfTime(double time
) { m_selfTime
= time
; }
109 double totalPercent() const { return (m_totalTime
/ (m_head
? m_head
->totalTime() : totalTime())) * 100.0; }
110 double selfPercent() const { return (m_selfTime
/ (m_head
? m_head
->totalTime() : totalTime())) * 100.0; }
112 Vector
<Call
> calls() const { return m_calls
; }
113 Call
& lastCall() { ASSERT(!m_calls
.isEmpty()); return m_calls
.last(); }
114 size_t numberOfCalls() const { return m_calls
.size(); }
117 const Vector
<RefPtr
<ProfileNode
>>& children() const { return m_children
; }
118 ProfileNode
* firstChild() const { return m_children
.size() ? m_children
.first().get() : 0; }
119 ProfileNode
* lastChild() const { return m_children
.size() ? m_children
.last().get() : 0; }
120 void removeChild(ProfileNode
*);
121 void addChild(PassRefPtr
<ProfileNode
> prpChild
);
122 void insertNode(PassRefPtr
<ProfileNode
> prpNode
);
124 ProfileNode
* traverseNextNodePostOrder() const;
127 const char* c_str() const { return m_callIdentifier
; }
128 void debugPrintData(int indentLevel
) const;
129 double debugPrintDataSampleStyle(int indentLevel
, FunctionCallHashCount
&) const;
133 typedef Vector
<RefPtr
<ProfileNode
>>::const_iterator StackIterator
;
135 ProfileNode(ExecState
* callerCallFrame
, const CallIdentifier
&, ProfileNode
* headNode
, ProfileNode
* parentNode
);
136 ProfileNode(ExecState
* callerCallFrame
, ProfileNode
* headNode
, ProfileNode
* nodeToCopy
);
139 void resetChildrensSiblings();
140 void endAndRecordCall();
142 ExecState
* m_callerCallFrame
;
143 CallIdentifier m_callIdentifier
;
145 ProfileNode
* m_parent
;
146 ProfileNode
* m_nextSibling
;
151 Vector
<Call
, 1> m_calls
;
152 Vector
<RefPtr
<ProfileNode
>> m_children
;
157 #endif // ProfileNode_h