Profile.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "Profile.h"
  27. #include "ProfileNode.h"
  28. #include <stdio.h>
  29. #include <wtf/DataLog.h>
  30. namespace JSC {
  31. PassRefPtr<Profile> Profile::create(const String& title, unsigned uid)
  32. {
  33. return adoptRef(new Profile(title, uid));
  34. }
  35. Profile::Profile(const String& title, unsigned uid)
  36. : m_title(title)
  37. , m_uid(uid)
  38. {
  39. // FIXME: When multi-threading is supported this will be a vector and calls
  40. // into the profiler will need to know which thread it is executing on.
  41. m_head = ProfileNode::create(0, CallIdentifier("Thread_1", String(), 0), 0, 0);
  42. }
  43. Profile::~Profile()
  44. {
  45. }
  46. void Profile::forEach(void (ProfileNode::*function)())
  47. {
  48. ProfileNode* currentNode = m_head->firstChild();
  49. for (ProfileNode* nextNode = currentNode; nextNode; nextNode = nextNode->firstChild())
  50. currentNode = nextNode;
  51. if (!currentNode)
  52. currentNode = m_head.get();
  53. ProfileNode* endNode = m_head->traverseNextNodePostOrder();
  54. while (currentNode && currentNode != endNode) {
  55. (currentNode->*function)();
  56. currentNode = currentNode->traverseNextNodePostOrder();
  57. }
  58. }
  59. void Profile::focus(const ProfileNode* profileNode)
  60. {
  61. if (!profileNode || !m_head)
  62. return;
  63. bool processChildren;
  64. const CallIdentifier& callIdentifier = profileNode->callIdentifier();
  65. for (ProfileNode* currentNode = m_head.get(); currentNode; currentNode = currentNode->traverseNextNodePreOrder(processChildren))
  66. processChildren = currentNode->focus(callIdentifier);
  67. // Set the visible time of all nodes so that the %s display correctly.
  68. forEach(&ProfileNode::calculateVisibleTotalTime);
  69. }
  70. void Profile::exclude(const ProfileNode* profileNode)
  71. {
  72. if (!profileNode || !m_head)
  73. return;
  74. const CallIdentifier& callIdentifier = profileNode->callIdentifier();
  75. for (ProfileNode* currentNode = m_head.get(); currentNode; currentNode = currentNode->traverseNextNodePreOrder())
  76. currentNode->exclude(callIdentifier);
  77. // Set the visible time of the head so the %s display correctly.
  78. m_head->setVisibleTotalTime(m_head->totalTime() - m_head->selfTime());
  79. m_head->setVisibleSelfTime(0.0);
  80. }
  81. void Profile::restoreAll()
  82. {
  83. forEach(&ProfileNode::restore);
  84. }
  85. #ifndef NDEBUG
  86. void Profile::debugPrintData() const
  87. {
  88. dataLogF("Call graph:\n");
  89. m_head->debugPrintData(0);
  90. }
  91. typedef WTF::KeyValuePair<FunctionCallHashCount::ValueType, unsigned> NameCountPair;
  92. static inline bool functionNameCountPairComparator(const NameCountPair& a, const NameCountPair& b)
  93. {
  94. return a.value > b.value;
  95. }
  96. void Profile::debugPrintDataSampleStyle() const
  97. {
  98. typedef Vector<NameCountPair> NameCountPairVector;
  99. FunctionCallHashCount countedFunctions;
  100. dataLogF("Call graph:\n");
  101. m_head->debugPrintDataSampleStyle(0, countedFunctions);
  102. dataLogF("\nTotal number in stack:\n");
  103. NameCountPairVector sortedFunctions(countedFunctions.size());
  104. copyToVector(countedFunctions, sortedFunctions);
  105. std::sort(sortedFunctions.begin(), sortedFunctions.end(), functionNameCountPairComparator);
  106. for (NameCountPairVector::iterator it = sortedFunctions.begin(); it != sortedFunctions.end(); ++it)
  107. dataLogF(" %-12d%s\n", (*it).value, String((*it).key).utf8().data());
  108. dataLogF("\nSort by top of stack, same collapsed (when >= 5):\n");
  109. }
  110. #endif
  111. } // namespace JSC