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 unsigned long callUID() const { return m_callIdentifier
.hash(); };
68 const String
& functionName() const { return m_callIdentifier
.m_name
; }
69 const String
& url() const { return m_callIdentifier
.m_url
; }
70 unsigned lineNumber() const { return m_callIdentifier
.m_lineNumber
; }
73 ProfileNode
* head() const { return m_head
; }
74 void setHead(ProfileNode
* head
) { m_head
= head
; }
75 ProfileNode
* parent() const { return m_parent
; }
76 void setParent(ProfileNode
* parent
) { m_parent
= parent
; }
77 ProfileNode
* nextSibling() const { return m_nextSibling
; }
78 void setNextSibling(ProfileNode
* nextSibling
) { m_nextSibling
= nextSibling
; }
81 double startTime() const { return m_startTime
; }
82 void setStartTime(double startTime
) { m_startTime
= startTime
; }
83 double totalTime() const { return m_visibleTotalTime
; }
84 double actualTotalTime() const { return m_actualTotalTime
; }
85 void setTotalTime(double time
) { m_actualTotalTime
= time
; m_visibleTotalTime
= time
; }
86 void setActualTotalTime(double time
) { m_actualTotalTime
= time
; }
87 void setVisibleTotalTime(double time
) { m_visibleTotalTime
= time
; }
88 double selfTime() const { return m_visibleSelfTime
; }
89 double actualSelfTime() const { return m_actualSelfTime
; }
90 void setSelfTime(double time
) {m_actualSelfTime
= time
; m_visibleSelfTime
= time
; }
91 void setActualSelfTime(double time
) { m_actualSelfTime
= time
; }
92 void setVisibleSelfTime(double time
) { m_visibleSelfTime
= time
; }
94 double totalPercent() const { return (m_visibleTotalTime
/ (m_head
? m_head
->totalTime() : totalTime())) * 100.0; }
95 double selfPercent() const { return (m_visibleSelfTime
/ (m_head
? m_head
->totalTime() : totalTime())) * 100.0; }
97 unsigned numberOfCalls() const { return m_numberOfCalls
; }
98 void setNumberOfCalls(unsigned number
) { m_numberOfCalls
= number
; }
101 const Vector
<RefPtr
<ProfileNode
> >& children() const { return m_children
; }
102 ProfileNode
* firstChild() const { return m_children
.size() ? m_children
.first().get() : 0; }
103 ProfileNode
* lastChild() const { return m_children
.size() ? m_children
.last().get() : 0; }
104 ProfileNode
* findChild(ProfileNode
*) const;
105 void removeChild(ProfileNode
*);
106 void addChild(PassRefPtr
<ProfileNode
> prpChild
);
107 void insertNode(PassRefPtr
<ProfileNode
> prpNode
);
110 bool visible() const { return m_visible
; }
111 void setVisible(bool visible
) { m_visible
= visible
; }
113 static void setTreeVisible(ProfileNode
*, bool visible
);
116 ProfileNode
* traverseNextNodePostOrder() const;
117 ProfileNode
* traverseNextNodePreOrder(bool processChildren
= true) const;
120 void calculateVisibleTotalTime();
121 bool focus(const CallIdentifier
&);
122 void exclude(const CallIdentifier
&);
125 void endAndRecordCall();
128 const char* c_str() const { return m_callIdentifier
; }
129 void debugPrintData(int indentLevel
) const;
130 double debugPrintDataSampleStyle(int indentLevel
, FunctionCallHashCount
&) const;
134 ProfileNode(ExecState
* callerCallFrame
, const CallIdentifier
&, ProfileNode
* headNode
, ProfileNode
* parentNode
);
135 ProfileNode(ExecState
* callerCallFrame
, ProfileNode
* headNode
, ProfileNode
* nodeToCopy
);
138 void resetChildrensSiblings();
140 RefPtr
<ProfileNode
>* childrenBegin() { return m_children
.begin(); }
141 RefPtr
<ProfileNode
>* childrenEnd() { return m_children
.end(); }
143 // Sorting comparators
144 static inline bool totalTimeDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->totalTime() > b
->totalTime(); }
145 static inline bool totalTimeAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->totalTime() < b
->totalTime(); }
146 static inline bool selfTimeDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->selfTime() > b
->selfTime(); }
147 static inline bool selfTimeAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->selfTime() < b
->selfTime(); }
148 static inline bool callsDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->numberOfCalls() > b
->numberOfCalls(); }
149 static inline bool callsAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->numberOfCalls() < b
->numberOfCalls(); }
150 static inline bool functionNameDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return codePointCompareLessThan(b
->functionName(), a
->functionName()); }
151 static inline bool functionNameAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return codePointCompareLessThan(a
->functionName(), b
->functionName()); }
153 ExecState
* m_callerCallFrame
;
154 CallIdentifier m_callIdentifier
;
156 ProfileNode
* m_parent
;
157 ProfileNode
* m_nextSibling
;
160 double m_actualTotalTime
;
161 double m_visibleTotalTime
;
162 double m_actualSelfTime
;
163 double m_visibleSelfTime
;
164 unsigned m_numberOfCalls
;
168 Vector
<RefPtr
<ProfileNode
> > m_children
;
173 #endif // ProfileNode_h