DominatorTree.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/devtools/DominatorTree.h"
  6. #include "mozilla/dom/DominatorTreeBinding.h"
  7. namespace mozilla {
  8. namespace devtools {
  9. dom::Nullable<uint64_t>
  10. DominatorTree::GetRetainedSize(uint64_t aNodeId, ErrorResult& aRv)
  11. {
  12. JS::ubi::Node::Id id(aNodeId);
  13. auto node = mHeapSnapshot->getNodeById(id);
  14. if (node.isNothing())
  15. return dom::Nullable<uint64_t>();
  16. auto mallocSizeOf = GetCurrentThreadDebuggerMallocSizeOf();
  17. JS::ubi::Node::Size size = 0;
  18. if (!mDominatorTree.getRetainedSize(*node, mallocSizeOf, size)) {
  19. aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
  20. return dom::Nullable<uint64_t>();
  21. }
  22. MOZ_ASSERT(size != 0,
  23. "The node should not have been unknown since we got it from the heap snapshot.");
  24. return dom::Nullable<uint64_t>(size);
  25. }
  26. struct NodeAndRetainedSize
  27. {
  28. JS::ubi::Node mNode;
  29. JS::ubi::Node::Size mSize;
  30. NodeAndRetainedSize(const JS::ubi::Node& aNode, JS::ubi::Node::Size aSize)
  31. : mNode(aNode)
  32. , mSize(aSize)
  33. { }
  34. struct Comparator
  35. {
  36. static bool
  37. Equals(const NodeAndRetainedSize& aLhs, const NodeAndRetainedSize& aRhs)
  38. {
  39. return aLhs.mSize == aRhs.mSize;
  40. }
  41. static bool
  42. LessThan(const NodeAndRetainedSize& aLhs, const NodeAndRetainedSize& aRhs)
  43. {
  44. // Use > because we want to sort from greatest to least retained size.
  45. return aLhs.mSize > aRhs.mSize;
  46. }
  47. };
  48. };
  49. void
  50. DominatorTree::GetImmediatelyDominated(uint64_t aNodeId,
  51. dom::Nullable<nsTArray<uint64_t>>& aOutResult,
  52. ErrorResult& aRv)
  53. {
  54. MOZ_ASSERT(aOutResult.IsNull());
  55. JS::ubi::Node::Id id(aNodeId);
  56. Maybe<JS::ubi::Node> node = mHeapSnapshot->getNodeById(id);
  57. if (node.isNothing())
  58. return;
  59. // Get all immediately dominated nodes and their retained sizes.
  60. MallocSizeOf mallocSizeOf = GetCurrentThreadDebuggerMallocSizeOf();
  61. Maybe<JS::ubi::DominatorTree::DominatedSetRange> range = mDominatorTree.getDominatedSet(*node);
  62. MOZ_ASSERT(range.isSome(), "The node should be known, since we got it from the heap snapshot.");
  63. size_t length = range->length();
  64. nsTArray<NodeAndRetainedSize> dominatedNodes(length);
  65. for (const JS::ubi::Node& dominatedNode : *range) {
  66. JS::ubi::Node::Size retainedSize = 0;
  67. if (NS_WARN_IF(!mDominatorTree.getRetainedSize(dominatedNode, mallocSizeOf, retainedSize))) {
  68. aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
  69. return;
  70. }
  71. MOZ_ASSERT(retainedSize != 0,
  72. "retainedSize should not be zero since we know the node is in the dominator tree.");
  73. dominatedNodes.AppendElement(NodeAndRetainedSize(dominatedNode, retainedSize));
  74. }
  75. // Sort them by retained size.
  76. NodeAndRetainedSize::Comparator comparator;
  77. dominatedNodes.Sort(comparator);
  78. // Fill the result with the nodes' ids.
  79. JS::ubi::Node root = mDominatorTree.root();
  80. aOutResult.SetValue(nsTArray<uint64_t>(length));
  81. for (const NodeAndRetainedSize& entry : dominatedNodes) {
  82. // The root dominates itself, but we don't want to expose that to JS.
  83. if (entry.mNode == root)
  84. continue;
  85. aOutResult.Value().AppendElement(entry.mNode.identifier());
  86. }
  87. }
  88. dom::Nullable<uint64_t>
  89. DominatorTree::GetImmediateDominator(uint64_t aNodeId) const
  90. {
  91. JS::ubi::Node::Id id(aNodeId);
  92. Maybe<JS::ubi::Node> node = mHeapSnapshot->getNodeById(id);
  93. if (node.isNothing())
  94. return dom::Nullable<uint64_t>();
  95. JS::ubi::Node dominator = mDominatorTree.getImmediateDominator(*node);
  96. if (!dominator || dominator == *node)
  97. return dom::Nullable<uint64_t>();
  98. return dom::Nullable<uint64_t>(dominator.identifier());
  99. }
  100. /*** Cycle Collection Boilerplate *****************************************************************/
  101. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DominatorTree, mParent, mHeapSnapshot)
  102. NS_IMPL_CYCLE_COLLECTING_ADDREF(DominatorTree)
  103. NS_IMPL_CYCLE_COLLECTING_RELEASE(DominatorTree)
  104. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DominatorTree)
  105. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  106. NS_INTERFACE_MAP_ENTRY(nsISupports)
  107. NS_INTERFACE_MAP_END
  108. /* virtual */ JSObject*
  109. DominatorTree::WrapObject(JSContext* aCx, JS::HandleObject aGivenProto)
  110. {
  111. return dom::DominatorTreeBinding::Wrap(aCx, this, aGivenProto);
  112. }
  113. } // namespace devtools
  114. } // namespace mozilla