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: 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 } 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 65 ExecState* callerCallFrame() const { return m_callerCallFrame; } 66 const CallIdentifier& callIdentifier() const { return m_callIdentifier; } 67 const UString& functionName() const { return m_callIdentifier.m_name; } 68 const UString& url() const { return m_callIdentifier.m_url; } 69 unsigned lineNumber() const { return m_callIdentifier.m_lineNumber; } 70 71 // Relationships 72 ProfileNode* head() const { return m_head; } 73 void setHead(ProfileNode* head) { m_head = head; } 74 ProfileNode* parent() const { return m_parent; } 75 void setParent(ProfileNode* parent) { m_parent = parent; } 76 ProfileNode* nextSibling() const { return m_nextSibling; } 77 void setNextSibling(ProfileNode* nextSibling) { m_nextSibling = nextSibling; } 78 79 // Time members 80 double startTime() const { return m_startTime; } 81 void setStartTime(double startTime) { m_startTime = startTime; } 82 double totalTime() const { return m_visibleTotalTime; } 83 double actualTotalTime() const { return m_actualTotalTime; } 84 void setTotalTime(double time) { m_actualTotalTime = time; m_visibleTotalTime = time; } 85 void setActualTotalTime(double time) { m_actualTotalTime = time; } 86 void setVisibleTotalTime(double time) { m_visibleTotalTime = time; } 87 double selfTime() const { return m_visibleSelfTime; } 88 double actualSelfTime() const { return m_actualSelfTime; } 89 void setSelfTime(double time) {m_actualSelfTime = time; m_visibleSelfTime = time; } 90 void setActualSelfTime(double time) { m_actualSelfTime = time; } 91 void setVisibleSelfTime(double time) { m_visibleSelfTime = time; } 92 93 double totalPercent() const { return (m_visibleTotalTime / (m_head ? m_head->totalTime() : totalTime())) * 100.0; } 94 double selfPercent() const { return (m_visibleSelfTime / (m_head ? m_head->totalTime() : totalTime())) * 100.0; } 95 96 unsigned numberOfCalls() const { return m_numberOfCalls; } 97 void setNumberOfCalls(unsigned number) { m_numberOfCalls = number; } 98 99 // Children members 100 const Vector<RefPtr<ProfileNode> >& children() const { return m_children; } 101 ProfileNode* firstChild() const { return m_children.size() ? m_children.first().get() : 0; } 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 109 bool visible() const { return m_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 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 139 RefPtr<ProfileNode>* childrenBegin() { return m_children.begin(); } 140 RefPtr<ProfileNode>* childrenEnd() { return m_children.end(); } 141 142 // Sorting comparators 143 static inline bool totalTimeDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->totalTime() > b->totalTime(); } 144 static inline bool totalTimeAscendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->totalTime() < b->totalTime(); } 145 static inline bool selfTimeDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->selfTime() > b->selfTime(); } 146 static inline bool selfTimeAscendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->selfTime() < b->selfTime(); } 147 static inline bool callsDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->numberOfCalls() > b->numberOfCalls(); } 148 static inline bool callsAscendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->numberOfCalls() < b->numberOfCalls(); } 149 static inline bool functionNameDescendingComparator(const RefPtr<ProfileNode>& a, const RefPtr<ProfileNode>& b) { return a->functionName() > b->functionName(); } 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