]> git.saurik.com Git - apple/javascriptcore.git/blame - profiler/ProfileNode.h
JavaScriptCore-1218.tar.gz
[apple/javascriptcore.git] / profiler / ProfileNode.h
CommitLineData
9dae56ea
A
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"
14957cd0 33#include <wtf/HashCountedSet.h>
9dae56ea
A
34#include <wtf/RefCounted.h>
35#include <wtf/RefPtr.h>
14957cd0 36#include <wtf/Vector.h>
9dae56ea
A
37
38namespace JSC {
39
14957cd0 40 class ExecState;
9dae56ea
A
41 class ProfileNode;
42
43 typedef Vector<RefPtr<ProfileNode> >::const_iterator StackIterator;
14957cd0 44 typedef HashCountedSet<StringImpl*> FunctionCallHashCount;
9dae56ea
A
45
46 class ProfileNode : public RefCounted<ProfileNode> {
47 public:
14957cd0 48 static PassRefPtr<ProfileNode> create(ExecState* callerCallFrame, const CallIdentifier& callIdentifier, ProfileNode* headNode, ProfileNode* parentNode)
9dae56ea 49 {
14957cd0 50 return adoptRef(new ProfileNode(callerCallFrame, callIdentifier, headNode, parentNode));
9dae56ea 51 }
14957cd0 52 static PassRefPtr<ProfileNode> create(ExecState* callerCallFrame, ProfileNode* headNode, ProfileNode* node)
9dae56ea 53 {
14957cd0 54 return adoptRef(new ProfileNode(callerCallFrame, headNode, node));
9dae56ea
A
55 }
56
57 bool operator==(ProfileNode* node) { return m_callIdentifier == node->callIdentifier(); }
58
14957cd0 59 ProfileNode* willExecute(ExecState* callerCallFrame, const CallIdentifier&);
9dae56ea
A
60 ProfileNode* didExecute();
61
62 void stopProfiling();
63
64 // CallIdentifier members
14957cd0 65 ExecState* callerCallFrame() const { return m_callerCallFrame; }
9dae56ea 66 const CallIdentifier& callIdentifier() const { return m_callIdentifier; }
93a37866
A
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; }
9dae56ea
A
70 unsigned lineNumber() const { return m_callIdentifier.m_lineNumber; }
71
72 // Relationships
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; }
79
80 // Time members
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; }
93
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; }
96
97 unsigned numberOfCalls() const { return m_numberOfCalls; }
98 void setNumberOfCalls(unsigned number) { m_numberOfCalls = number; }
99
100 // Children members
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);
108
109 // Visiblity
110 bool visible() const { return m_visible; }
111 void setVisible(bool visible) { m_visible = visible; }
112
113 static void setTreeVisible(ProfileNode*, bool visible);
114
115 // Sorting
116 ProfileNode* traverseNextNodePostOrder() const;
117 ProfileNode* traverseNextNodePreOrder(bool processChildren = true) const;
118
9dae56ea
A
119 // Views
120 void calculateVisibleTotalTime();
121 bool focus(const CallIdentifier&);
122 void exclude(const CallIdentifier&);
123 void restore();
124
125 void endAndRecordCall();
126
127#ifndef NDEBUG
128 const char* c_str() const { return m_callIdentifier; }
129 void debugPrintData(int indentLevel) const;
130 double debugPrintDataSampleStyle(int indentLevel, FunctionCallHashCount&) const;
131#endif
132
133 private:
14957cd0
A
134 ProfileNode(ExecState* callerCallFrame, const CallIdentifier&, ProfileNode* headNode, ProfileNode* parentNode);
135 ProfileNode(ExecState* callerCallFrame, ProfileNode* headNode, ProfileNode* nodeToCopy);
9dae56ea
A
136
137 void startTimer();
138 void resetChildrensSiblings();
139
140 RefPtr<ProfileNode>* childrenBegin() { return m_children.begin(); }
141 RefPtr<ProfileNode>* childrenEnd() { return m_children.end(); }
142
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(); }
93a37866
A
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()); }
9dae56ea 152
14957cd0 153 ExecState* m_callerCallFrame;
9dae56ea
A
154 CallIdentifier m_callIdentifier;
155 ProfileNode* m_head;
156 ProfileNode* m_parent;
157 ProfileNode* m_nextSibling;
158
159 double m_startTime;
160 double m_actualTotalTime;
161 double m_visibleTotalTime;
162 double m_actualSelfTime;
163 double m_visibleSelfTime;
164 unsigned m_numberOfCalls;
165
166 bool m_visible;
167
168 Vector<RefPtr<ProfileNode> > m_children;
169 };
170
171} // namespace JSC
172
173#endif // ProfileNode_h