v8_detailed_memory_decorator.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_V8_DETAILED_MEMORY_DECORATOR_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_V8_DETAILED_MEMORY_DECORATOR_H_
  6. #include <memory>
  7. #include "base/callback_forward.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/sequence_checker.h"
  10. #include "base/types/pass_key.h"
  11. #include "components/performance_manager/public/graph/graph.h"
  12. #include "components/performance_manager/public/graph/graph_registered.h"
  13. #include "components/performance_manager/public/graph/node_data_describer.h"
  14. #include "components/performance_manager/public/graph/process_node.h"
  15. #include "components/performance_manager/public/v8_memory/v8_detailed_memory.h"
  16. #include "mojo/public/cpp/bindings/pending_receiver.h"
  17. #include "third_party/blink/public/mojom/performance_manager/v8_detailed_memory_reporter.mojom.h"
  18. namespace performance_manager {
  19. class FrameNode;
  20. namespace v8_memory {
  21. class V8DetailedMemoryRequestQueue;
  22. // A decorator that queries each renderer process for the amount of memory used
  23. // by V8 in each frame. The public interface to create requests for this
  24. // decorator is in
  25. // //components/performance_manager/public/v8_detailed_memory.h.
  26. class V8DetailedMemoryDecorator
  27. : public GraphOwned,
  28. public GraphRegisteredImpl<V8DetailedMemoryDecorator>,
  29. public ProcessNode::ObserverDefaultImpl,
  30. public NodeDataDescriberDefaultImpl {
  31. public:
  32. V8DetailedMemoryDecorator();
  33. ~V8DetailedMemoryDecorator() override;
  34. V8DetailedMemoryDecorator(const V8DetailedMemoryDecorator&) = delete;
  35. V8DetailedMemoryDecorator& operator=(const V8DetailedMemoryDecorator&) =
  36. delete;
  37. // GraphOwned implementation.
  38. void OnPassedToGraph(Graph* graph) override;
  39. void OnTakenFromGraph(Graph* graph) override;
  40. // ProcessNodeObserver overrides.
  41. void OnProcessNodeAdded(const ProcessNode* process_node) override;
  42. void OnBeforeProcessNodeRemoved(const ProcessNode* process_node) override;
  43. // NodeDataDescriber overrides.
  44. base::Value DescribeFrameNodeData(const FrameNode* node) const override;
  45. base::Value DescribeProcessNodeData(const ProcessNode* node) const override;
  46. // Returns the next measurement request that should be scheduled.
  47. const V8DetailedMemoryRequest* GetNextRequest() const;
  48. // Returns the next measurement request with mode kBounded or
  49. // kEagerForTesting that should be scheduled.
  50. const V8DetailedMemoryRequest* GetNextBoundedRequest() const;
  51. // Implementation details below this point.
  52. // V8DetailedMemoryRequest objects register themselves with the decorator.
  53. // If |process_node| is null, the request will be sent to every renderer
  54. // process, otherwise it will be sent only to |process_node|.
  55. void AddMeasurementRequest(base::PassKey<V8DetailedMemoryRequest>,
  56. V8DetailedMemoryRequest* request,
  57. const ProcessNode* process_node = nullptr);
  58. void RemoveMeasurementRequest(base::PassKey<V8DetailedMemoryRequest>,
  59. V8DetailedMemoryRequest* request);
  60. // Internal helper class that can call NotifyObserversOnMeasurementAvailable
  61. // when a measurement is received.
  62. class ObserverNotifier;
  63. void NotifyObserversOnMeasurementAvailable(
  64. base::PassKey<ObserverNotifier>,
  65. const ProcessNode* process_node) const;
  66. // The following functions retrieve data maintained by the decorator.
  67. static const V8DetailedMemoryExecutionContextData* GetExecutionContextData(
  68. const FrameNode* node);
  69. static const V8DetailedMemoryExecutionContextData* GetExecutionContextData(
  70. const WorkerNode* node);
  71. static const V8DetailedMemoryExecutionContextData* GetExecutionContextData(
  72. const execution_context::ExecutionContext* ec);
  73. static V8DetailedMemoryExecutionContextData*
  74. CreateExecutionContextDataForTesting(const FrameNode* node);
  75. static V8DetailedMemoryExecutionContextData*
  76. CreateExecutionContextDataForTesting(const WorkerNode* node);
  77. static const V8DetailedMemoryProcessData* GetProcessData(
  78. const ProcessNode* node);
  79. static V8DetailedMemoryProcessData* CreateProcessDataForTesting(
  80. const ProcessNode* node);
  81. private:
  82. using RequestQueueCallback =
  83. base::RepeatingCallback<void(V8DetailedMemoryRequestQueue*)>;
  84. // Runs the given |callback| for every V8DetailedMemoryRequestQueue (global
  85. // and per-process).
  86. void ApplyToAllRequestQueues(RequestQueueCallback callback) const;
  87. void UpdateProcessMeasurementSchedules() const;
  88. raw_ptr<Graph> graph_ GUARDED_BY_CONTEXT(sequence_checker_) = nullptr;
  89. std::unique_ptr<V8DetailedMemoryRequestQueue> measurement_requests_
  90. GUARDED_BY_CONTEXT(sequence_checker_);
  91. SEQUENCE_CHECKER(sequence_checker_);
  92. };
  93. //////////////////////////////////////////////////////////////////////////////
  94. // The following internal functions are exposed in the header for testing.
  95. namespace internal {
  96. // A callback that will bind a V8DetailedMemoryReporter interface to
  97. // communicate with the given process. Exposed so that it can be overridden to
  98. // implement the interface with a test fake.
  99. using BindV8DetailedMemoryReporterCallback = base::RepeatingCallback<void(
  100. mojo::PendingReceiver<blink::mojom::V8DetailedMemoryReporter>,
  101. RenderProcessHostProxy)>;
  102. // Sets a callback that will be used to bind the V8DetailedMemoryReporter
  103. // interface. The callback is owned by the caller and must live until this
  104. // function is called again with nullptr.
  105. void SetBindV8DetailedMemoryReporterCallbackForTesting(
  106. BindV8DetailedMemoryReporterCallback* callback);
  107. // Destroys the V8DetailedMemoryDecorator. Exposed for testing.
  108. void DestroyV8DetailedMemoryDecoratorForTesting(Graph* graph);
  109. } // namespace internal
  110. } // namespace v8_memory
  111. } // namespace performance_manager
  112. #endif // COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_V8_DETAILED_MEMORY_DECORATOR_H_