• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ProfileNode_h
30 #define ProfileNode_h
31 
32 #include "CallIdentifier.h"
33 #include <wtf/HashCountedSet.h>
34 #include <wtf/RefCounted.h>
35 #include <wtf/RefPtr.h>
36 #include <wtf/Vector.h>
37 
38 namespace JSC {
39 
40     class ExecState;
41     class ProfileNode;
42 
43     typedef Vector<RefPtr<ProfileNode> >::const_iterator StackIterator;
44     typedef HashCountedSet<StringImpl*> FunctionCallHashCount;
45 
46     class ProfileNode : public RefCounted<ProfileNode> {
47     public:
create(ExecState * callerCallFrame,const CallIdentifier & callIdentifier,ProfileNode * headNode,ProfileNode * parentNode)48         static PassRefPtr<ProfileNode> create(ExecState* callerCallFrame, const CallIdentifier& callIdentifier, ProfileNode* headNode, ProfileNode* parentNode)
49         {
50             return adoptRef(new ProfileNode(callerCallFrame, callIdentifier, headNode, parentNode));
51         }
create(ExecState * callerCallFrame,ProfileNode * headNode,ProfileNode * node)52         static PassRefPtr<ProfileNode> create(ExecState* callerCallFrame, ProfileNode* headNode, ProfileNode* node)
53         {
54             return adoptRef(new ProfileNode(callerCallFrame, headNode, node));
55         }
56 
57         bool operator==(ProfileNode* node) { return m_callIdentifier == node->callIdentifier(); }
58 
59         ProfileNode* willExecute(ExecState* callerCallFrame, const CallIdentifier&);
60         ProfileNode* didExecute();
61 
62         void stopProfiling();
63 
64         // CallIdentifier members
callerCallFrame()65         ExecState* callerCallFrame() const { return m_callerCallFrame; }
callIdentifier()66         const CallIdentifier& callIdentifier() const { return m_callIdentifier; }
functionName()67         const UString& functionName() const { return m_callIdentifier.m_name; }
url()68         const UString& url() const { return m_callIdentifier.m_url; }
lineNumber()69         unsigned lineNumber() const { return m_callIdentifier.m_lineNumber; }
70 
71         // Relationships
head()72         ProfileNode* head() const { return m_head; }
setHead(ProfileNode * head)73         void setHead(ProfileNode* head) { m_head = head; }
parent()74         ProfileNode* parent() const { return m_parent; }
setParent(ProfileNode * parent)75         void setParent(ProfileNode* parent) { m_parent = parent; }
nextSibling()76         ProfileNode* nextSibling() const { return m_nextSibling; }
setNextSibling(ProfileNode * nextSibling)77         void setNextSibling(ProfileNode* nextSibling) { m_nextSibling = nextSibling; }
78 
79         // Time members
startTime()80         double startTime() const { return m_startTime; }
setStartTime(double startTime)81         void setStartTime(double startTime) { m_startTime = startTime; }
totalTime()82         double totalTime() const { return m_visibleTotalTime; }
actualTotalTime()83         double actualTotalTime() const { return m_actualTotalTime; }
setTotalTime(double time)84         void setTotalTime(double time) { m_actualTotalTime = time; m_visibleTotalTime = time; }
setActualTotalTime(double time)85         void setActualTotalTime(double time) { m_actualTotalTime = time; }
setVisibleTotalTime(double time)86         void setVisibleTotalTime(double time) { m_visibleTotalTime = time; }
selfTime()87         double selfTime() const { return m_visibleSelfTime; }
actualSelfTime()88         double actualSelfTime() const { return m_actualSelfTime; }
setSelfTime(double time)89         void setSelfTime(double time) {m_actualSelfTime = time; m_visibleSelfTime = time; }
setActualSelfTime(double time)90         void setActualSelfTime(double time) { m_actualSelfTime = time; }
setVisibleSelfTime(double time)91         void setVisibleSelfTime(double time) { m_visibleSelfTime = time; }
92 
totalPercent()93         double totalPercent() const { return (m_visibleTotalTime / (m_head ? m_head->totalTime() : totalTime())) * 100.0; }
selfPercent()94         double selfPercent() const { return (m_visibleSelfTime / (m_head ? m_head->totalTime() : totalTime())) * 100.0; }
95 
numberOfCalls()96         unsigned numberOfCalls() const { return m_numberOfCalls; }
setNumberOfCalls(unsigned number)97         void setNumberOfCalls(unsigned number) { m_numberOfCalls = number; }
98 
99         // Children members
children()100         const Vector<RefPtr<ProfileNode> >& children() const { return m_children; }
firstChild()101         ProfileNode* firstChild() const { return m_children.size() ? m_children.first().get() : 0; }
lastChild()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);
107 
108         // Visiblity
visible()109         bool visible() const { return m_visible; }
setVisible(bool visible)110         void setVisible(bool visible) { m_visible = visible; }
111 
112         static void setTreeVisible(ProfileNode*, bool visible);
113 
114         // Sorting
115         ProfileNode* traverseNextNodePostOrder() const;
116         ProfileNode* traverseNextNodePreOrder(bool processChildren = true) const;
117 
118         // Views
119         void calculateVisibleTotalTime();
120         bool focus(const CallIdentifier&);
121         void exclude(const CallIdentifier&);
122         void restore();
123 
124         void endAndRecordCall();
125 
126 #ifndef NDEBUG
c_str()127         const char* c_str() const { return m_callIdentifier; }
128         void debugPrintData(int indentLevel) const;
129         double debugPrintDataSampleStyle(int indentLevel, FunctionCallHashCount&) const;
130 #endif
131 
132     private:
133         ProfileNode(ExecState* callerCallFrame, const CallIdentifier&, ProfileNode* headNode, ProfileNode* parentNode);
134         ProfileNode(ExecState* callerCallFrame, ProfileNode* headNode, ProfileNode* nodeToCopy);
135 
136         void startTimer();
137         void resetChildrensSiblings();
138 
childrenBegin()139         RefPtr<ProfileNode>* childrenBegin() { return m_children.begin(); }
childrenEnd()140         RefPtr<ProfileNode>* childrenEnd() { return m_children.end(); }
141 
142         // Sorting comparators
totalTimeDescendingComparator(const RefPtr<ProfileNode> & a,const RefPtr<ProfileNode> & b)143         static inline bool totalTimeDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->totalTime() > b->totalTime(); }
totalTimeAscendingComparator(const RefPtr<ProfileNode> & a,const RefPtr<ProfileNode> & b)144         static inline bool totalTimeAscendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->totalTime() < b->totalTime(); }
selfTimeDescendingComparator(const RefPtr<ProfileNode> & a,const RefPtr<ProfileNode> & b)145         static inline bool selfTimeDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->selfTime() > b->selfTime(); }
selfTimeAscendingComparator(const RefPtr<ProfileNode> & a,const RefPtr<ProfileNode> & b)146         static inline bool selfTimeAscendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->selfTime() < b->selfTime(); }
callsDescendingComparator(const RefPtr<ProfileNode> & a,const RefPtr<ProfileNode> & b)147         static inline bool callsDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->numberOfCalls() > b->numberOfCalls(); }
callsAscendingComparator(const RefPtr<ProfileNode> & a,const RefPtr<ProfileNode> & b)148         static inline bool callsAscendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->numberOfCalls() < b->numberOfCalls(); }
functionNameDescendingComparator(const RefPtr<ProfileNode> & a,const RefPtr<ProfileNode> & b)149         static inline bool functionNameDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->functionName() > b->functionName(); }
functionNameAscendingComparator(const RefPtr<ProfileNode> & a,const RefPtr<ProfileNode> & b)150         static inline bool functionNameAscendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->functionName() < b->functionName(); }
151 
152         ExecState* m_callerCallFrame;
153         CallIdentifier m_callIdentifier;
154         ProfileNode* m_head;
155         ProfileNode* m_parent;
156         ProfileNode* m_nextSibling;
157 
158         double m_startTime;
159         double m_actualTotalTime;
160         double m_visibleTotalTime;
161         double m_actualSelfTime;
162         double m_visibleSelfTime;
163         unsigned m_numberOfCalls;
164 
165         bool m_visible;
166 
167         Vector<RefPtr<ProfileNode> > m_children;
168     };
169 
170 } // namespace JSC
171 
172 #endif // ProfileNode_h
173