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 37 namespace 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 // Views 116 void calculateVisibleTotalTime(); 117 bool focus(const CallIdentifier&); 118 void exclude(const CallIdentifier&); 119 void restore(); 120 121 void endAndRecordCall(); 122 123 #ifndef NDEBUG 124 const char* c_str() const { return m_callIdentifier; } 125 void debugPrintData(int indentLevel) const; 126 double debugPrintDataSampleStyle(int indentLevel, FunctionCallHashCount&) const; 127 #endif 128 129 private: 130 ProfileNode(const CallIdentifier&, ProfileNode* headNode, ProfileNode* parentNode); 131 ProfileNode(ProfileNode* headNode, ProfileNode* nodeToCopy); 132 133 void startTimer(); 134 void resetChildrensSiblings(); 135 136 RefPtr<ProfileNode>* childrenBegin() { return m_children.begin(); } 137 RefPtr<ProfileNode>* childrenEnd() { return m_children.end(); } 138 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(); } 148 149 CallIdentifier m_callIdentifier; 150 ProfileNode* m_head; 151 ProfileNode* m_parent; 152 ProfileNode* m_nextSibling; 153 154 double m_startTime; 155 double m_actualTotalTime; 156 double m_visibleTotalTime; 157 double m_actualSelfTime; 158 double m_visibleSelfTime; 159 unsigned m_numberOfCalls; 160 161 bool m_visible; 162 163 Vector<RefPtr<ProfileNode> > m_children; 164 }; 165 166 } // namespace JSC 167 168 #endif // ProfileNode_h 169