ProfileNode.h 8.6 KB

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