ProfileTreeNode.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2012 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. #ifndef ProfileTreeNode_h
  26. #define ProfileTreeNode_h
  27. namespace JSC {
  28. class ProfileTreeNode {
  29. typedef HashMap<String, ProfileTreeNode> Map;
  30. typedef Map::ValueType MapEntry;
  31. public:
  32. ProfileTreeNode()
  33. : m_count(0)
  34. , m_children(0)
  35. {
  36. }
  37. ~ProfileTreeNode()
  38. {
  39. delete m_children;
  40. }
  41. ProfileTreeNode* sampleChild(const char* name)
  42. {
  43. if (!m_children)
  44. m_children = new Map();
  45. ProfileTreeNode newEntry;
  46. Map::AddResult result = m_children->add(String(name), newEntry);
  47. ProfileTreeNode* childInMap = &result.iterator->value;
  48. ++childInMap->m_count;
  49. return childInMap;
  50. }
  51. void dump()
  52. {
  53. dumpInternal(0);
  54. }
  55. uint64_t count()
  56. {
  57. return m_count;
  58. }
  59. uint64_t childCount()
  60. {
  61. if (!m_children)
  62. return 0;
  63. uint64_t childCount = 0;
  64. for (Map::iterator it = m_children->begin(); it != m_children->end(); ++it)
  65. childCount += it->value.count();
  66. return childCount;
  67. }
  68. private:
  69. void dumpInternal(unsigned indent)
  70. {
  71. if (!m_children)
  72. return;
  73. // Copy pointers to all children into a vector, and sort the vector by sample count.
  74. Vector<MapEntry*> entries;
  75. for (Map::iterator it = m_children->begin(); it != m_children->end(); ++it)
  76. entries.append(&*it);
  77. qsort(entries.begin(), entries.size(), sizeof(MapEntry*), compareEntries);
  78. // Iterate over the children in sample-frequency order.
  79. for (size_t e = 0; e < entries.size(); ++e) {
  80. MapEntry* entry = entries[e];
  81. // Print the number of samples, the name of this node, and the number of samples that are stack-top
  82. // in this node (samples directly within this node, excluding samples in children.
  83. for (unsigned i = 0; i < indent; ++i)
  84. dataLogF(" ");
  85. dataLogF("% 8lld: %s (%lld stack top)\n",
  86. static_cast<long long>(entry->value.count()),
  87. entry->key.utf8().data(),
  88. static_cast<long long>(entry->value.count() - entry->value.childCount()));
  89. // Recursively dump the child nodes.
  90. entry->value.dumpInternal(indent + 1);
  91. }
  92. }
  93. static int compareEntries(const void* a, const void* b)
  94. {
  95. uint64_t da = (*static_cast<MapEntry* const *>(a))->value.count();
  96. uint64_t db = (*static_cast<MapEntry* const *>(b))->value.count();
  97. return (da < db) - (da > db);
  98. }
  99. uint64_t m_count;
  100. Map* m_children;
  101. };
  102. }
  103. #endif // ProfileTreeNode_h