2 * Copyright (C) 2008 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 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.
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 Vector
<RefPtr
<ProfileNode
> >::const_iterator StackIterator
;
44 typedef HashCountedSet
<StringImpl
*> FunctionCallHashCount
;
46 class ProfileNode
: public RefCounted
<ProfileNode
> {
48 static PassRefPtr
<ProfileNode
> create(ExecState
* callerCallFrame
, const CallIdentifier
& callIdentifier
, ProfileNode
* headNode
, ProfileNode
* parentNode
)
50 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
));
57 bool operator==(ProfileNode
* node
) { return m_callIdentifier
== node
->callIdentifier(); }
59 ProfileNode
* willExecute(ExecState
* callerCallFrame
, const CallIdentifier
&);
60 ProfileNode
* didExecute();
64 // CallIdentifier members
65 ExecState
* callerCallFrame() const { return m_callerCallFrame
; }
66 const CallIdentifier
& callIdentifier() const { return m_callIdentifier
; }
67 const UString
& functionName() const { return m_callIdentifier
.m_name
; }
68 const UString
& url() const { return m_callIdentifier
.m_url
; }
69 unsigned lineNumber() const { return m_callIdentifier
.m_lineNumber
; }
72 ProfileNode
* head() const { return m_head
; }
73 void setHead(ProfileNode
* head
) { m_head
= head
; }
74 ProfileNode
* parent() const { return m_parent
; }
75 void setParent(ProfileNode
* parent
) { m_parent
= parent
; }
76 ProfileNode
* nextSibling() const { return m_nextSibling
; }
77 void setNextSibling(ProfileNode
* nextSibling
) { m_nextSibling
= nextSibling
; }
80 double startTime() const { return m_startTime
; }
81 void setStartTime(double startTime
) { m_startTime
= startTime
; }
82 double totalTime() const { return m_visibleTotalTime
; }
83 double actualTotalTime() const { return m_actualTotalTime
; }
84 void setTotalTime(double time
) { m_actualTotalTime
= time
; m_visibleTotalTime
= time
; }
85 void setActualTotalTime(double time
) { m_actualTotalTime
= time
; }
86 void setVisibleTotalTime(double time
) { m_visibleTotalTime
= time
; }
87 double selfTime() const { return m_visibleSelfTime
; }
88 double actualSelfTime() const { return m_actualSelfTime
; }
89 void setSelfTime(double time
) {m_actualSelfTime
= time
; m_visibleSelfTime
= time
; }
90 void setActualSelfTime(double time
) { m_actualSelfTime
= time
; }
91 void setVisibleSelfTime(double time
) { m_visibleSelfTime
= time
; }
93 double totalPercent() const { return (m_visibleTotalTime
/ (m_head
? m_head
->totalTime() : totalTime())) * 100.0; }
94 double selfPercent() const { return (m_visibleSelfTime
/ (m_head
? m_head
->totalTime() : totalTime())) * 100.0; }
96 unsigned numberOfCalls() const { return m_numberOfCalls
; }
97 void setNumberOfCalls(unsigned number
) { m_numberOfCalls
= number
; }
100 const Vector
<RefPtr
<ProfileNode
> >& children() const { return m_children
; }
101 ProfileNode
* firstChild() const { return m_children
.size() ? m_children
.first().get() : 0; }
102 ProfileNode
* lastChild() const { return m_children
.size() ? m_children
.last().get() : 0; }
103 ProfileNode
* findChild(ProfileNode
*) const;
104 void removeChild(ProfileNode
*);
105 void addChild(PassRefPtr
<ProfileNode
> prpChild
);
106 void insertNode(PassRefPtr
<ProfileNode
> prpNode
);
109 bool visible() const { return m_visible
; }
110 void setVisible(bool visible
) { m_visible
= visible
; }
112 static void setTreeVisible(ProfileNode
*, bool visible
);
115 ProfileNode
* traverseNextNodePostOrder() const;
116 ProfileNode
* traverseNextNodePreOrder(bool processChildren
= true) const;
119 void calculateVisibleTotalTime();
120 bool focus(const CallIdentifier
&);
121 void exclude(const CallIdentifier
&);
124 void endAndRecordCall();
127 const char* c_str() const { return m_callIdentifier
; }
128 void debugPrintData(int indentLevel
) const;
129 double debugPrintDataSampleStyle(int indentLevel
, FunctionCallHashCount
&) const;
133 ProfileNode(ExecState
* callerCallFrame
, const CallIdentifier
&, ProfileNode
* headNode
, ProfileNode
* parentNode
);
134 ProfileNode(ExecState
* callerCallFrame
, ProfileNode
* headNode
, ProfileNode
* nodeToCopy
);
137 void resetChildrensSiblings();
139 RefPtr
<ProfileNode
>* childrenBegin() { return m_children
.begin(); }
140 RefPtr
<ProfileNode
>* childrenEnd() { return m_children
.end(); }
142 // Sorting comparators
143 static inline bool totalTimeDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->totalTime() > b
->totalTime(); }
144 static inline bool totalTimeAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->totalTime() < b
->totalTime(); }
145 static inline bool selfTimeDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->selfTime() > b
->selfTime(); }
146 static inline bool selfTimeAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->selfTime() < b
->selfTime(); }
147 static inline bool callsDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->numberOfCalls() > b
->numberOfCalls(); }
148 static inline bool callsAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->numberOfCalls() < b
->numberOfCalls(); }
149 static inline bool functionNameDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->functionName() > b
->functionName(); }
150 static inline bool functionNameAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->functionName() < b
->functionName(); }
152 ExecState
* m_callerCallFrame
;
153 CallIdentifier m_callIdentifier
;
155 ProfileNode
* m_parent
;
156 ProfileNode
* m_nextSibling
;
159 double m_actualTotalTime
;
160 double m_visibleTotalTime
;
161 double m_actualSelfTime
;
162 double m_visibleSelfTime
;
163 unsigned m_numberOfCalls
;
167 Vector
<RefPtr
<ProfileNode
> > m_children
;
172 #endif // ProfileNode_h