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/Vector.h>
34 #include <wtf/RefCounted.h>
35 #include <wtf/RefPtr.h>
41 typedef Vector
<RefPtr
<ProfileNode
> >::const_iterator StackIterator
;
42 typedef HashCountedSet
<UString::Rep
*> FunctionCallHashCount
;
44 class ProfileNode
: public RefCounted
<ProfileNode
> {
46 static PassRefPtr
<ProfileNode
> create(const CallIdentifier
& callIdentifier
, ProfileNode
* headNode
, ProfileNode
* parentNode
)
48 return adoptRef(new ProfileNode(callIdentifier
, headNode
, parentNode
));
50 static PassRefPtr
<ProfileNode
> create(ProfileNode
* headNode
, ProfileNode
* node
)
52 return adoptRef(new ProfileNode(headNode
, node
));
55 bool operator==(ProfileNode
* node
) { return m_callIdentifier
== node
->callIdentifier(); }
57 ProfileNode
* willExecute(const CallIdentifier
&);
58 ProfileNode
* didExecute();
62 // CallIdentifier members
63 const CallIdentifier
& callIdentifier() const { return m_callIdentifier
; }
64 const UString
& functionName() const { return m_callIdentifier
.m_name
; }
65 const UString
& url() const { return m_callIdentifier
.m_url
; }
66 unsigned lineNumber() const { return m_callIdentifier
.m_lineNumber
; }
69 ProfileNode
* head() const { return m_head
; }
70 void setHead(ProfileNode
* head
) { m_head
= head
; }
71 ProfileNode
* parent() const { return m_parent
; }
72 void setParent(ProfileNode
* parent
) { m_parent
= parent
; }
73 ProfileNode
* nextSibling() const { return m_nextSibling
; }
74 void setNextSibling(ProfileNode
* nextSibling
) { m_nextSibling
= nextSibling
; }
77 double startTime() const { return m_startTime
; }
78 void setStartTime(double startTime
) { m_startTime
= startTime
; }
79 double totalTime() const { return m_visibleTotalTime
; }
80 double actualTotalTime() const { return m_actualTotalTime
; }
81 void setTotalTime(double time
) { m_actualTotalTime
= time
; m_visibleTotalTime
= time
; }
82 void setActualTotalTime(double time
) { m_actualTotalTime
= time
; }
83 void setVisibleTotalTime(double time
) { m_visibleTotalTime
= time
; }
84 double selfTime() const { return m_visibleSelfTime
; }
85 double actualSelfTime() const { return m_actualSelfTime
; }
86 void setSelfTime(double time
) {m_actualSelfTime
= time
; m_visibleSelfTime
= time
; }
87 void setActualSelfTime(double time
) { m_actualSelfTime
= time
; }
88 void setVisibleSelfTime(double time
) { m_visibleSelfTime
= time
; }
90 double totalPercent() const { return (m_visibleTotalTime
/ (m_head
? m_head
->totalTime() : totalTime())) * 100.0; }
91 double selfPercent() const { return (m_visibleSelfTime
/ (m_head
? m_head
->totalTime() : totalTime())) * 100.0; }
93 unsigned numberOfCalls() const { return m_numberOfCalls
; }
94 void setNumberOfCalls(unsigned number
) { m_numberOfCalls
= number
; }
97 const Vector
<RefPtr
<ProfileNode
> >& children() const { return m_children
; }
98 ProfileNode
* firstChild() const { return m_children
.size() ? m_children
.first().get() : 0; }
99 ProfileNode
* lastChild() const { return m_children
.size() ? m_children
.last().get() : 0; }
100 ProfileNode
* findChild(ProfileNode
*) const;
101 void removeChild(ProfileNode
*);
102 void addChild(PassRefPtr
<ProfileNode
> prpChild
);
103 void insertNode(PassRefPtr
<ProfileNode
> prpNode
);
106 bool visible() const { return m_visible
; }
107 void setVisible(bool visible
) { m_visible
= visible
; }
109 static void setTreeVisible(ProfileNode
*, bool visible
);
112 ProfileNode
* traverseNextNodePostOrder() const;
113 ProfileNode
* traverseNextNodePreOrder(bool processChildren
= true) const;
116 void calculateVisibleTotalTime();
117 bool focus(const CallIdentifier
&);
118 void exclude(const CallIdentifier
&);
121 void endAndRecordCall();
124 const char* c_str() const { return m_callIdentifier
; }
125 void debugPrintData(int indentLevel
) const;
126 double debugPrintDataSampleStyle(int indentLevel
, FunctionCallHashCount
&) const;
130 ProfileNode(const CallIdentifier
&, ProfileNode
* headNode
, ProfileNode
* parentNode
);
131 ProfileNode(ProfileNode
* headNode
, ProfileNode
* nodeToCopy
);
134 void resetChildrensSiblings();
136 RefPtr
<ProfileNode
>* childrenBegin() { return m_children
.begin(); }
137 RefPtr
<ProfileNode
>* childrenEnd() { return m_children
.end(); }
139 // Sorting comparators
140 static inline bool totalTimeDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->totalTime() > b
->totalTime(); }
141 static inline bool totalTimeAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->totalTime() < b
->totalTime(); }
142 static inline bool selfTimeDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->selfTime() > b
->selfTime(); }
143 static inline bool selfTimeAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->selfTime() < b
->selfTime(); }
144 static inline bool callsDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->numberOfCalls() > b
->numberOfCalls(); }
145 static inline bool callsAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->numberOfCalls() < b
->numberOfCalls(); }
146 static inline bool functionNameDescendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->functionName() > b
->functionName(); }
147 static inline bool functionNameAscendingComparator(const RefPtr
<ProfileNode
>& a
, const RefPtr
<ProfileNode
>& b
) { return a
->functionName() < b
->functionName(); }
149 CallIdentifier m_callIdentifier
;
151 ProfileNode
* m_parent
;
152 ProfileNode
* m_nextSibling
;
155 double m_actualTotalTime
;
156 double m_visibleTotalTime
;
157 double m_actualSelfTime
;
158 double m_visibleSelfTime
;
159 unsigned m_numberOfCalls
;
163 Vector
<RefPtr
<ProfileNode
> > m_children
;
168 #endif // ProfileNode_h