process_node.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2019 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_PUBLIC_GRAPH_PROCESS_NODE_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_PROCESS_NODE_H_
  6. #include "base/callback_forward.h"
  7. #include "base/containers/enum_set.h"
  8. #include "base/containers/flat_set.h"
  9. #include "base/process/process.h"
  10. #include "base/task/task_traits.h"
  11. #include "components/performance_manager/public/graph/node.h"
  12. #include "components/performance_manager/public/render_process_host_id.h"
  13. #include "content/public/common/process_type.h"
  14. namespace base {
  15. class Process;
  16. } // namespace base
  17. namespace performance_manager {
  18. class FrameNode;
  19. class WorkerNode;
  20. class ProcessNodeObserver;
  21. class RenderProcessHostProxy;
  22. // A process node follows the lifetime of a RenderProcessHost.
  23. // It may reference zero or one processes at a time, but during its lifetime, it
  24. // may reference more than one process. This can happen if the associated
  25. // renderer crashes, and an associated frame is then reloaded or re-navigated.
  26. // The state of the process node goes through:
  27. // 1. Created, no PID.
  28. // 2. Process started, have PID - in the case where the associated render
  29. // process fails to start, this state may not occur.
  30. // 3. Process died or failed to start, have exit status.
  31. // 4. Back to 2.
  32. //
  33. // It is only valid to access this object on the sequence of the graph that owns
  34. // it.
  35. class ProcessNode : public Node {
  36. public:
  37. using FrameNodeVisitor = base::RepeatingCallback<bool(const FrameNode*)>;
  38. using Observer = ProcessNodeObserver;
  39. class ObserverDefaultImpl;
  40. // The type of content a renderer can host.
  41. enum class ContentType : uint32_t {
  42. // Hosted an extension.
  43. kExtension = 1 << 0,
  44. // Hosted a frame with no parent.
  45. kMainFrame = 1 << 1,
  46. // Hosted a frame with a parent.
  47. kSubframe = 1 << 2,
  48. // Hosted a frame (main frame or subframe) with a committed navigation. A
  49. // "speculative" frame will not have a committed navigation.
  50. kNavigatedFrame = 1 << 3,
  51. // Hosted a frame that was tagged as an ad.
  52. kAd = 1 << 4,
  53. // Hosted a worker (service worker, dedicated worker, shared worker).
  54. kWorker = 1 << 5,
  55. };
  56. using ContentTypes =
  57. base::EnumSet<ContentType, ContentType::kExtension, ContentType::kWorker>;
  58. ProcessNode();
  59. ProcessNode(const ProcessNode&) = delete;
  60. ProcessNode& operator=(const ProcessNode&) = delete;
  61. ~ProcessNode() override;
  62. // Returns the type of this process.
  63. virtual content::ProcessType GetProcessType() const = 0;
  64. // Returns the process ID associated with this process. Use this in preference
  65. // to querying GetProcess.Pid(). It's always valid to access, but will return
  66. // kNullProcessId if the process has yet started. It will also retain the
  67. // process ID for a process that has exited (at least until the underlying
  68. // RenderProcessHost gets reused in the case of a crash). Refrain from using
  69. // this as a unique identifier as on some platforms PIDs are reused
  70. // aggressively.
  71. virtual base::ProcessId GetProcessId() const = 0;
  72. // Returns the base::Process backing this process. This will be an invalid
  73. // process if it has not yet started, or if it has exited.
  74. virtual const base::Process& GetProcess() const = 0;
  75. // Returns a time captured as early as possible after the process is launched.
  76. virtual base::TimeTicks GetLaunchTime() const = 0;
  77. // Returns the exit status of this process. This will be empty if the process
  78. // has not yet exited.
  79. virtual absl::optional<int32_t> GetExitStatus() const = 0;
  80. // Returns the non-localized name of the process used for metrics reporting
  81. // metrics as specified in content::ChildProcessData during process creation.
  82. virtual const std::string& GetMetricsName() const = 0;
  83. // Visits the frame nodes that are hosted in this process. The iteration is
  84. // halted if the visitor returns false. Returns true if every call to the
  85. // visitor returned true, false otherwise.
  86. virtual bool VisitFrameNodes(const FrameNodeVisitor& visitor) const = 0;
  87. // Returns the set of frame nodes that are hosted in this process. Note that
  88. // calling this causes the set of nodes to be generated.
  89. virtual base::flat_set<const FrameNode*> GetFrameNodes() const = 0;
  90. // Returns the set of worker nodes that are hosted in this process. Note that
  91. // calling this causes the set of nodes to be generated.
  92. virtual base::flat_set<const WorkerNode*> GetWorkerNodes() const = 0;
  93. // Returns true if the main thread task load is low (below some threshold
  94. // of usage). See ProcessNodeObserver::OnMainThreadTaskLoadIsLow.
  95. virtual bool GetMainThreadTaskLoadIsLow() const = 0;
  96. // Returns the most recently measured private memory footprint of the process.
  97. // This is roughly private, anonymous, non-discardable, resident or swapped
  98. // memory in kilobytes. For more details, see https://goo.gl/3kPb9S.
  99. //
  100. // Note: This is only valid if at least one component has expressed interest
  101. // for process memory metrics by calling
  102. // ProcessMetricsDecorator::RegisterInterestForProcessMetrics.
  103. virtual uint64_t GetPrivateFootprintKb() const = 0;
  104. // Returns the most recently measured resident set of the process, in
  105. // kilobytes.
  106. virtual uint64_t GetResidentSetKb() const = 0;
  107. // Returns the render process id (equivalent to RenderProcessHost::GetID()),
  108. // or ChildProcessHost::kInvalidUniqueID if this is not a renderer.
  109. virtual RenderProcessHostId GetRenderProcessHostId() const = 0;
  110. // Returns a proxy to the RenderProcessHost associated with this node. The
  111. // proxy may only be dereferenced on the UI thread.
  112. virtual const RenderProcessHostProxy& GetRenderProcessHostProxy() const = 0;
  113. // Returns the current priority of the process.
  114. virtual base::TaskPriority GetPriority() const = 0;
  115. // Returns a bit field indicating what type of content this process has
  116. // hosted, either currently or in the past.
  117. virtual ContentTypes GetHostedContentTypes() const = 0;
  118. };
  119. // Pure virtual observer interface. Derive from this if you want to be forced to
  120. // implement the entire interface.
  121. class ProcessNodeObserver {
  122. public:
  123. ProcessNodeObserver();
  124. ProcessNodeObserver(const ProcessNodeObserver&) = delete;
  125. ProcessNodeObserver& operator=(const ProcessNodeObserver&) = delete;
  126. virtual ~ProcessNodeObserver();
  127. // Node lifetime notifications.
  128. // Called when a |process_node| is added to the graph. Observers must not make
  129. // any property changes or cause re-entrant notifications during the scope of
  130. // this call.
  131. virtual void OnProcessNodeAdded(const ProcessNode* process_node) = 0;
  132. // The process associated with |process_node| has been started or has exited.
  133. // This implies some or all of the process, process_id, launch time and/or
  134. // exit status properties have changed.
  135. virtual void OnProcessLifetimeChange(const ProcessNode* process_node) = 0;
  136. // Called before a |process_node| is removed from the graph. Observers must
  137. // not make any property changes or cause re-entrant notifications during the
  138. // scope of this call.
  139. virtual void OnBeforeProcessNodeRemoved(const ProcessNode* process_node) = 0;
  140. // Notifications of property changes.
  141. // Invoked when the |main_thread_task_load_is_low| property changes.
  142. virtual void OnMainThreadTaskLoadIsLow(const ProcessNode* process_node) = 0;
  143. // Invoked when the process priority changes.
  144. virtual void OnPriorityChanged(const ProcessNode* process_node,
  145. base::TaskPriority previous_value) = 0;
  146. // Events with no property changes.
  147. // Fired when all frames in a process have transitioned to being frozen.
  148. virtual void OnAllFramesInProcessFrozen(const ProcessNode* process_node) = 0;
  149. };
  150. // Default implementation of observer that provides dummy versions of each
  151. // function. Derive from this if you only need to implement a few of the
  152. // functions.
  153. class ProcessNode::ObserverDefaultImpl : public ProcessNodeObserver {
  154. public:
  155. ObserverDefaultImpl();
  156. ObserverDefaultImpl(const ObserverDefaultImpl&) = delete;
  157. ObserverDefaultImpl& operator=(const ObserverDefaultImpl&) = delete;
  158. ~ObserverDefaultImpl() override;
  159. // ProcessNodeObserver implementation:
  160. void OnProcessNodeAdded(const ProcessNode* process_node) override {}
  161. void OnProcessLifetimeChange(const ProcessNode* process_node) override {}
  162. void OnBeforeProcessNodeRemoved(const ProcessNode* process_node) override {}
  163. void OnMainThreadTaskLoadIsLow(const ProcessNode* process_node) override {}
  164. void OnPriorityChanged(const ProcessNode* process_node,
  165. base::TaskPriority previous_value) override {}
  166. void OnAllFramesInProcessFrozen(const ProcessNode* process_node) override {}
  167. };
  168. } // namespace performance_manager
  169. #endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_PROCESS_NODE_H_