| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ |
| 6 #define CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ | 6 #define CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/memory/memory_coordinator_client.h" | 9 #include "base/memory/memory_coordinator_client.h" |
| 10 #include "base/memory/memory_coordinator_proxy.h" | 10 #include "base/memory/memory_coordinator_proxy.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 // NOTE: Memory coordinator is under development and not fully working. | 25 // NOTE: Memory coordinator is under development and not fully working. |
| 26 | 26 |
| 27 class MemoryCoordinatorHandleImpl; | 27 class MemoryCoordinatorHandleImpl; |
| 28 class MemoryCoordinatorImplTest; | 28 class MemoryCoordinatorImplTest; |
| 29 class MemoryMonitor; | 29 class MemoryMonitor; |
| 30 class MemoryStateUpdater; | 30 class MemoryStateUpdater; |
| 31 class RenderProcessHost; | 31 class RenderProcessHost; |
| 32 struct MemoryCoordinatorSingletonTraits; | 32 struct MemoryCoordinatorSingletonTraits; |
| 33 | 33 |
| 34 // MemoryCondition is an internal state of memory coordinator which is used for |
| 35 // various things; calculating memory state for processes, requesting processes |
| 36 // to purge memory, and scheduling tab discarding. |
| 37 enum class MemoryCondition : int { |
| 38 NORMAL = 0, |
| 39 WARNING = 1, |
| 40 CRITICAL = 2, |
| 41 }; |
| 42 |
| 34 // MemoryCoordinatorImpl is an implementation of MemoryCoordinator. | 43 // MemoryCoordinatorImpl is an implementation of MemoryCoordinator. |
| 35 // The current implementation uses MemoryStateUpdater to update the global | 44 // The current implementation uses MemoryStateUpdater to update the global |
| 36 // memory state. See comments in MemoryStateUpdater for details. | 45 // memory state. See comments in MemoryStateUpdater for details. |
| 37 class CONTENT_EXPORT MemoryCoordinatorImpl : public base::MemoryCoordinator, | 46 class CONTENT_EXPORT MemoryCoordinatorImpl : public base::MemoryCoordinator, |
| 38 public MemoryCoordinator, | 47 public MemoryCoordinator, |
| 39 public NotificationObserver, | 48 public NotificationObserver, |
| 40 public base::NonThreadSafe { | 49 public base::NonThreadSafe { |
| 41 public: | 50 public: |
| 42 using MemoryState = base::MemoryState; | 51 using MemoryState = base::MemoryState; |
| 43 | 52 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 64 | 73 |
| 65 // Returns the memory state of the specified render process. Returns UNKNOWN | 74 // Returns the memory state of the specified render process. Returns UNKNOWN |
| 66 // if the process is not tracked by this coordinator. | 75 // if the process is not tracked by this coordinator. |
| 67 MemoryState GetChildMemoryState(int render_process_id) const; | 76 MemoryState GetChildMemoryState(int render_process_id) const; |
| 68 | 77 |
| 69 // Records memory pressure notifications. Called by MemoryPressureMonitor. | 78 // Records memory pressure notifications. Called by MemoryPressureMonitor. |
| 70 // TODO(bashi): Remove this when MemoryPressureMonitor is retired. | 79 // TODO(bashi): Remove this when MemoryPressureMonitor is retired. |
| 71 void RecordMemoryPressure( | 80 void RecordMemoryPressure( |
| 72 base::MemoryPressureMonitor::MemoryPressureLevel level); | 81 base::MemoryPressureMonitor::MemoryPressureLevel level); |
| 73 | 82 |
| 74 // Returns the global memory state. | 83 // Returns the current memory condition. |
| 75 virtual MemoryState GetGlobalMemoryState() const; | 84 MemoryCondition GetMemoryCondtion() const { return memory_condition_; } |
| 76 | 85 |
| 77 // Returns the browser's current memory state. Note that the current state | 86 // Returns the browser's current memory state. |
| 78 // could be different from the global memory state as the browser won't be | |
| 79 // suspended. | |
| 80 MemoryState GetCurrentMemoryState() const override; | 87 MemoryState GetCurrentMemoryState() const override; |
| 81 | 88 |
| 82 // Sets the global memory state for testing. | 89 // Temporariy sets memory state of the browser process for testing. |
| 83 void SetCurrentMemoryStateForTesting(MemoryState memory_state) override; | 90 void SetCurrentMemoryStateForTesting(MemoryState memory_state) override; |
| 84 | 91 |
| 85 // MemoryCoordinator implementation: | 92 // content::MemoryCoordinator implementation: |
| 86 MemoryState GetStateForProcess(base::ProcessHandle handle) override; | 93 MemoryState GetStateForProcess(base::ProcessHandle handle) override; |
| 87 | 94 |
| 88 // NotificationObserver implementation: | 95 // NotificationObserver implementation: |
| 89 void Observe(int type, | 96 void Observe(int type, |
| 90 const NotificationSource& source, | 97 const NotificationSource& source, |
| 91 const NotificationDetails& details) override; | 98 const NotificationDetails& details) override; |
| 92 | 99 |
| 93 // Overrides the global state to |new_state|. State update tasks won't be | 100 // Overrides the current memory condition to |condition|. Memory condition |
| 94 // scheduled until |duration| is passed. This means that the global state | 101 // update tasks won't be scheduled until |duration| is passed. This means that |
| 95 // remains the same until |duration| is passed or another call of this method. | 102 // the memory condition remains the same until |duration| is passed or |
| 96 void ForceSetGlobalState(base::MemoryState new_state, | 103 // another call of this method. |
| 97 base::TimeDelta duration); | 104 void ForceSetMemoryCondition(MemoryCondition condition, |
| 105 base::TimeDelta duration); |
| 98 | 106 |
| 99 // Changes the global state and notifies state changes to clients (lives in | 107 // Changes current memory condition if needed. This may trigger some actions |
| 100 // the browser) and child processes (renderers) if needed. Returns true when | 108 // like purging memory and memory state changes. |
| 101 // the state is actually changed. | 109 void UpdateConditionIfNeeded(MemoryCondition condition); |
| 102 bool ChangeStateIfNeeded(MemoryState prev_state, MemoryState next_state); | |
| 103 | 110 |
| 104 // Asks the delegate to discard a tab. | 111 // Asks the delegate to discard a tab. |
| 105 void DiscardTab(); | 112 void DiscardTab(); |
| 106 | 113 |
| 107 protected: | 114 protected: |
| 108 // Returns the RenderProcessHost which is correspond to the given id. | 115 // Returns the RenderProcessHost which is correspond to the given id. |
| 109 // Returns nullptr if there is no corresponding RenderProcessHost. | 116 // Returns nullptr if there is no corresponding RenderProcessHost. |
| 110 // This is a virtual method so that we can write tests without having | 117 // This is a virtual method so that we can write tests without having |
| 111 // actual RenderProcessHost. | 118 // actual RenderProcessHost. |
| 112 virtual RenderProcessHost* GetRenderProcessHost(int render_process_id); | 119 virtual RenderProcessHost* GetRenderProcessHost(int render_process_id); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 129 bool CanSuspendRenderer(int render_process_id); | 136 bool CanSuspendRenderer(int render_process_id); |
| 130 | 137 |
| 131 // Stores information about any known child processes. | 138 // Stores information about any known child processes. |
| 132 struct ChildInfo { | 139 struct ChildInfo { |
| 133 // This object must be compatible with STL containers. | 140 // This object must be compatible with STL containers. |
| 134 ChildInfo(); | 141 ChildInfo(); |
| 135 ChildInfo(const ChildInfo& rhs); | 142 ChildInfo(const ChildInfo& rhs); |
| 136 ~ChildInfo(); | 143 ~ChildInfo(); |
| 137 | 144 |
| 138 MemoryState memory_state; | 145 MemoryState memory_state; |
| 146 base::TimeTicks last_state_change; |
| 139 bool is_visible = false; | 147 bool is_visible = false; |
| 140 std::unique_ptr<MemoryCoordinatorHandleImpl> handle; | 148 std::unique_ptr<MemoryCoordinatorHandleImpl> handle; |
| 141 }; | 149 }; |
| 142 | 150 |
| 143 // A map from process ID (RenderProcessHost::GetID()) to child process info. | 151 // A map from process ID (RenderProcessHost::GetID()) to child process info. |
| 144 using ChildInfoMap = std::map<int, ChildInfo>; | 152 using ChildInfoMap = std::map<int, ChildInfo>; |
| 145 | 153 |
| 146 ChildInfoMap& children() { return children_; } | 154 ChildInfoMap& children() { return children_; } |
| 147 | 155 |
| 148 private: | 156 private: |
| 149 #if !defined(OS_MACOSX) | 157 #if !defined(OS_MACOSX) |
| 150 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, HandleAdded); | 158 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, HandleAdded); |
| 151 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, | 159 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, |
| 152 CanSuspendRenderer); | 160 CanSuspendRenderer); |
| 153 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorWithServiceWorkerTest, | 161 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorWithServiceWorkerTest, |
| 154 CannotSuspendRendererWithServiceWorker); | 162 CannotSuspendRendererWithServiceWorker); |
| 155 #endif | 163 #endif |
| 156 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState); | 164 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState); |
| 157 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState); | 165 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState); |
| 158 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting); | 166 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting); |
| 159 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetGlobalState); | 167 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetGlobalState); |
| 160 | 168 |
| 161 friend struct MemoryCoordinatorSingletonTraits; | 169 friend struct MemoryCoordinatorSingletonTraits; |
| 162 friend class MemoryCoordinatorHandleImpl; | 170 friend class MemoryCoordinatorHandleImpl; |
| 163 | 171 |
| 172 // Updates the browser's memory state if needed. |
| 173 void UpdateCurrentMemoryState(MemoryState state); |
| 174 |
| 164 // Called when ChildMemoryCoordinator calls AddChild(). | 175 // Called when ChildMemoryCoordinator calls AddChild(). |
| 165 void OnChildAdded(int render_process_id); | 176 void OnChildAdded(int render_process_id); |
| 166 | 177 |
| 167 // Called by SetChildMemoryState() to determine a child memory state based on | 178 // Called by SetChildMemoryState() to determine a child memory state based on |
| 168 // the current status of the child process. | 179 // the current status of the child process. |
| 169 MemoryState OverrideGlobalState(MemoryState memroy_state, | 180 MemoryState OverrideState(MemoryState memroy_state, const ChildInfo& child); |
| 170 const ChildInfo& child); | |
| 171 | 181 |
| 172 // Helper function of CreateHandle and AddChildForTesting. | 182 // Helper function of CreateHandle and AddChildForTesting. |
| 173 void CreateChildInfoMapEntry( | 183 void CreateChildInfoMapEntry( |
| 174 int render_process_id, | 184 int render_process_id, |
| 175 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); | 185 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); |
| 176 | 186 |
| 177 // Notifies a state change to in-process clients. | 187 // Notifies a state change to in-process clients. |
| 178 void NotifyStateToClients(); | 188 void NotifyStateToClients(MemoryState state); |
| 179 | 189 |
| 180 // Notifies a state change to child processes. | 190 // Notifies a state change to child processes. |
| 181 void NotifyStateToChildren(); | 191 void NotifyStateToChildren(MemoryState state); |
| 182 | 192 |
| 183 // Records metrics. This is called when the global state is changed. | 193 // Records metrics. This is called when the global state is changed. |
| 184 void RecordStateChange(MemoryState prev_state, | 194 void RecordStateChange(MemoryState prev_state, |
| 185 MemoryState next_state, | 195 MemoryState next_state, |
| 186 base::TimeDelta duration); | 196 base::TimeDelta duration); |
| 187 | 197 |
| 188 std::unique_ptr<MemoryCoordinatorDelegate> delegate_; | 198 std::unique_ptr<MemoryCoordinatorDelegate> delegate_; |
| 189 std::unique_ptr<MemoryMonitor> memory_monitor_; | 199 std::unique_ptr<MemoryMonitor> memory_monitor_; |
| 190 std::unique_ptr<MemoryStateUpdater> state_updater_; | 200 std::unique_ptr<MemoryStateUpdater> state_updater_; |
| 191 NotificationRegistrar notification_registrar_; | 201 NotificationRegistrar notification_registrar_; |
| 192 // The global memory state. | 202 |
| 203 // The current memory condition. |
| 204 MemoryCondition memory_condition_ = MemoryCondition::NORMAL; |
| 205 |
| 206 // The memory state of the browser process. |
| 193 MemoryState current_state_ = MemoryState::NORMAL; | 207 MemoryState current_state_ = MemoryState::NORMAL; |
| 194 // The time tick of last global state change. | 208 |
| 209 // The time tick of last state change of the browser process. |
| 195 base::TimeTicks last_state_change_; | 210 base::TimeTicks last_state_change_; |
| 211 |
| 212 // Memory state for a process will remain unchanged until this period of time |
| 213 // passes. |
| 214 base::TimeDelta minimum_state_transition_period_; |
| 215 |
| 196 // Tracks child processes. An entry is added when a renderer connects to | 216 // Tracks child processes. An entry is added when a renderer connects to |
| 197 // MemoryCoordinator and removed automatically when an underlying binding is | 217 // MemoryCoordinator and removed automatically when an underlying binding is |
| 198 // disconnected. | 218 // disconnected. |
| 199 ChildInfoMap children_; | 219 ChildInfoMap children_; |
| 200 | 220 |
| 201 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); | 221 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); |
| 202 }; | 222 }; |
| 203 | 223 |
| 204 } // namespace content | 224 } // namespace content |
| 205 | 225 |
| 206 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ | 226 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ |
| OLD | NEW |