]> git.saurik.com Git - apple/javascriptcore.git/blame - profiler/ProfileNode.h
JavaScriptCore-525.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"
33#include <wtf/Vector.h>
34#include <wtf/RefCounted.h>
35#include <wtf/RefPtr.h>
36
37namespace JSC {
38
39 class ProfileNode;
40
41 typedef Vector<RefPtr<ProfileNode> >::const_iterator StackIterator;
42 typedef HashCountedSet<UString::Rep*> FunctionCallHashCount;
43
44 class ProfileNode : public RefCounted<ProfileNode> {
45 public:
46 static PassRefPtr<ProfileNode> create(const CallIdentifier& callIdentifier, ProfileNode* headNode, ProfileNode* parentNode)
47 {
48 return adoptRef(new ProfileNode(callIdentifier, headNode, parentNode));
49 }
50 static PassRefPtr<ProfileNode> create(ProfileNode* headNode, ProfileNode* node)
51 {
52 return adoptRef(new ProfileNode(headNode, node));
53 }
54
55 bool operator==(ProfileNode* node) { return m_callIdentifier == node->callIdentifier(); }
56
57 ProfileNode* willExecute(const CallIdentifier&);
58 ProfileNode* didExecute();
59
60 void stopProfiling();
61
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; }
67
68 // Relationships
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; }
75
76 // Time members
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; }
89
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; }
92
93 unsigned numberOfCalls() const { return m_numberOfCalls; }
94 void setNumberOfCalls(unsigned number) { m_numberOfCalls = number; }
95
96 // Children members
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);
104
105 // Visiblity
106 bool visible() const { return m_visible; }
107 void setVisible(bool visible) { m_visible = visible; }
108
109 static void setTreeVisible(ProfileNode*, bool visible);
110
111 // Sorting
112 ProfileNode* traverseNextNodePostOrder() const;
113 ProfileNode* traverseNextNodePreOrder(bool processChildren = true) const;
114
115 void sort(bool (*)(const RefPtr<ProfileNode>&, const RefPtr<ProfileNode>&));
116 void sortTotalTimeDescending() { sort(totalTimeDescendingComparator); }
117 void sortTotalTimeAscending() { sort(totalTimeAscendingComparator); }
118 void sortSelfTimeDescending() { sort(selfTimeDescendingComparator); }
119 void sortSelfTimeAscending() { sort(selfTimeAscendingComparator); }
120 void sortCallsDescending() { sort(callsDescendingComparator); }
121 void sortCallsAscending() { sort(callsAscendingComparator); }
122 void sortFunctionNameDescending() { sort(functionNameDescendingComparator); }
123 void sortFunctionNameAscending() { sort(functionNameAscendingComparator); }
124
125 // Views
126 void calculateVisibleTotalTime();
127 bool focus(const CallIdentifier&);
128 void exclude(const CallIdentifier&);
129 void restore();
130
131 void endAndRecordCall();
132
133#ifndef NDEBUG
134 const char* c_str() const { return m_callIdentifier; }
135 void debugPrintData(int indentLevel) const;
136 double debugPrintDataSampleStyle(int indentLevel, FunctionCallHashCount&) const;
137#endif
138
139 private:
140 ProfileNode(const CallIdentifier&, ProfileNode* headNode, ProfileNode* parentNode);
141 ProfileNode(ProfileNode* headNode, ProfileNode* nodeToCopy);
142
143 void startTimer();
144 void resetChildrensSiblings();
145
146 RefPtr<ProfileNode>* childrenBegin() { return m_children.begin(); }
147 RefPtr<ProfileNode>* childrenEnd() { return m_children.end(); }
148
149 // Sorting comparators
150 static inline bool totalTimeDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->totalTime() > b->totalTime(); }
151 static inline bool totalTimeAscendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->totalTime() < b->totalTime(); }
152 static inline bool selfTimeDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->selfTime() > b->selfTime(); }
153 static inline bool selfTimeAscendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->selfTime() < b->selfTime(); }
154 static inline bool callsDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->numberOfCalls() > b->numberOfCalls(); }
155 static inline bool callsAscendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->numberOfCalls() < b->numberOfCalls(); }
156 static inline bool functionNameDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->functionName() > b->functionName(); }
157 static inline bool functionNameAscendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->functionName() < b->functionName(); }
158
159 CallIdentifier m_callIdentifier;
160 ProfileNode* m_head;
161 ProfileNode* m_parent;
162 ProfileNode* m_nextSibling;
163
164 double m_startTime;
165 double m_actualTotalTime;
166 double m_visibleTotalTime;
167 double m_actualSelfTime;
168 double m_visibleSelfTime;
169 unsigned m_numberOfCalls;
170
171 bool m_visible;
172
173 Vector<RefPtr<ProfileNode> > m_children;
174 };
175
176} // namespace JSC
177
178#endif // ProfileNode_h