| 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" |
| 11 #include "base/memory/memory_pressure_monitor.h" | 11 #include "base/memory/memory_pressure_monitor.h" |
| 12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 17 #include "content/common/memory_coordinator.mojom.h" | 17 #include "content/common/memory_coordinator.mojom.h" |
| 18 #include "content/public/browser/memory_coordinator.h" | 18 #include "content/public/browser/memory_coordinator.h" |
| 19 #include "content/public/browser/memory_coordinator_delegate.h" | 19 #include "content/public/browser/memory_coordinator_delegate.h" |
| 20 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
| 21 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 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 MemoryConditionObserver; |
| 27 class MemoryCoordinatorHandleImpl; | 28 class MemoryCoordinatorHandleImpl; |
| 28 class MemoryCoordinatorImplTest; | 29 class MemoryCoordinatorImplTest; |
| 29 class MemoryMonitor; | 30 class MemoryMonitor; |
| 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 | |
| 36 // memory state. See comments in MemoryStateUpdater for details. | |
| 37 class CONTENT_EXPORT MemoryCoordinatorImpl : public base::MemoryCoordinator, | 44 class CONTENT_EXPORT MemoryCoordinatorImpl : public base::MemoryCoordinator, |
| 38 public MemoryCoordinator, | 45 public MemoryCoordinator, |
| 39 public NotificationObserver, | 46 public NotificationObserver, |
| 40 public base::NonThreadSafe { | 47 public base::NonThreadSafe { |
| 41 public: | 48 public: |
| 42 using MemoryState = base::MemoryState; | 49 using MemoryState = base::MemoryState; |
| 43 | 50 |
| 44 static MemoryCoordinatorImpl* GetInstance(); | 51 static MemoryCoordinatorImpl* GetInstance(); |
| 45 | 52 |
| 46 MemoryCoordinatorImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 53 MemoryCoordinatorImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 64 | 71 |
| 65 // Returns the memory state of the specified render process. Returns UNKNOWN | 72 // Returns the memory state of the specified render process. Returns UNKNOWN |
| 66 // if the process is not tracked by this coordinator. | 73 // if the process is not tracked by this coordinator. |
| 67 MemoryState GetChildMemoryState(int render_process_id) const; | 74 MemoryState GetChildMemoryState(int render_process_id) const; |
| 68 | 75 |
| 69 // Records memory pressure notifications. Called by MemoryPressureMonitor. | 76 // Records memory pressure notifications. Called by MemoryPressureMonitor. |
| 70 // TODO(bashi): Remove this when MemoryPressureMonitor is retired. | 77 // TODO(bashi): Remove this when MemoryPressureMonitor is retired. |
| 71 void RecordMemoryPressure( | 78 void RecordMemoryPressure( |
| 72 base::MemoryPressureMonitor::MemoryPressureLevel level); | 79 base::MemoryPressureMonitor::MemoryPressureLevel level); |
| 73 | 80 |
| 74 // Returns the global memory state. | 81 // Returns the browser's current memory state. |
| 75 virtual MemoryState GetGlobalMemoryState() const; | 82 MemoryState GetBrowserMemoryState() const; |
| 76 | 83 |
| 77 // Returns the browser's current memory state. Note that the current state | 84 // base::MemoryCoordinator implementations: |
| 78 // could be different from the global memory state as the browser won't be | |
| 79 // suspended. | |
| 80 MemoryState GetCurrentMemoryState() const override; | 85 MemoryState GetCurrentMemoryState() const override; |
| 81 | 86 // Temporarily sets memory state of the browser process for testing. |
| 82 // Sets the global memory state for testing. | |
| 83 void SetCurrentMemoryStateForTesting(MemoryState memory_state) override; | 87 void SetCurrentMemoryStateForTesting(MemoryState memory_state) override; |
| 84 | 88 |
| 85 // MemoryCoordinator implementation: | 89 // content::MemoryCoordinator implementation: |
| 86 MemoryState GetStateForProcess(base::ProcessHandle handle) override; | 90 MemoryState GetStateForProcess(base::ProcessHandle handle) override; |
| 87 | 91 |
| 88 // NotificationObserver implementation: | 92 // NotificationObserver implementation: |
| 89 void Observe(int type, | 93 void Observe(int type, |
| 90 const NotificationSource& source, | 94 const NotificationSource& source, |
| 91 const NotificationDetails& details) override; | 95 const NotificationDetails& details) override; |
| 92 | 96 |
| 93 // Overrides the global state to |new_state|. State update tasks won't be | 97 // Returns the current memory condition. |
| 94 // scheduled until |duration| is passed. This means that the global state | 98 MemoryCondition GetMemoryCondition() const { return memory_condition_; } |
| 95 // remains the same until |duration| is passed or another call of this method. | |
| 96 void ForceSetGlobalState(base::MemoryState new_state, | |
| 97 base::TimeDelta duration); | |
| 98 | 99 |
| 99 // Changes the global state and notifies state changes to clients (lives in | 100 // Overrides the current memory condition to |condition|. Memory condition |
| 100 // the browser) and child processes (renderers) if needed. Returns true when | 101 // update tasks won't be scheduled until |duration| is passed. This means that |
| 101 // the state is actually changed. | 102 // the memory condition remains the same until |duration| is passed or |
| 102 bool ChangeStateIfNeeded(MemoryState prev_state, MemoryState next_state); | 103 // another call of this method. |
| 104 void ForceSetMemoryCondition(MemoryCondition condition, |
| 105 base::TimeDelta duration); |
| 106 |
| 107 // Changes current memory condition if needed. This may trigger some actions |
| 108 // like purging memory and memory state changes. |
| 109 void UpdateConditionIfNeeded(MemoryCondition condition); |
| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 ChildInfoMap& children() { return children_; } | 153 ChildInfoMap& children() { return children_; } |
| 147 | 154 |
| 148 private: | 155 private: |
| 149 #if !defined(OS_MACOSX) | 156 #if !defined(OS_MACOSX) |
| 150 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, HandleAdded); | 157 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, HandleAdded); |
| 151 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, | 158 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, |
| 152 CanSuspendRenderer); | 159 CanSuspendRenderer); |
| 153 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorWithServiceWorkerTest, | 160 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorWithServiceWorkerTest, |
| 154 CannotSuspendRendererWithServiceWorker); | 161 CannotSuspendRendererWithServiceWorker); |
| 155 #endif | 162 #endif |
| 156 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState); | 163 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextCondition); |
| 157 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState); | 164 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateCondition); |
| 158 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting); | 165 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting); |
| 159 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetGlobalState); | 166 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetMemoryCondition); |
| 160 | 167 |
| 161 friend struct MemoryCoordinatorSingletonTraits; | 168 friend struct MemoryCoordinatorSingletonTraits; |
| 162 friend class MemoryCoordinatorHandleImpl; | 169 friend class MemoryCoordinatorHandleImpl; |
| 163 | 170 |
| 164 // Called when ChildMemoryCoordinator calls AddChild(). | 171 // Called when ChildMemoryCoordinator calls AddChild(). |
| 165 void OnChildAdded(int render_process_id); | 172 void OnChildAdded(int render_process_id); |
| 166 | 173 |
| 167 // Called by SetChildMemoryState() to determine a child memory state based on | 174 // Called by SetChildMemoryState() to determine a child memory state based on |
| 168 // the current status of the child process. | 175 // the current status of the child process. |
| 169 MemoryState OverrideGlobalState(MemoryState memroy_state, | 176 MemoryState OverrideState(MemoryState memroy_state, const ChildInfo& child); |
| 170 const ChildInfo& child); | |
| 171 | 177 |
| 172 // Helper function of CreateHandle and AddChildForTesting. | 178 // Helper function of CreateHandle and AddChildForTesting. |
| 173 void CreateChildInfoMapEntry( | 179 void CreateChildInfoMapEntry( |
| 174 int render_process_id, | 180 int render_process_id, |
| 175 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); | 181 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); |
| 176 | 182 |
| 183 // Updates the browser's memory state and notifies it to in-process clients. |
| 184 void UpdateBrowserStateAndNotifyStateToClients(MemoryState state); |
| 185 |
| 177 // Notifies a state change to in-process clients. | 186 // Notifies a state change to in-process clients. |
| 178 void NotifyStateToClients(); | 187 void NotifyStateToClients(MemoryState state); |
| 179 | 188 |
| 180 // Notifies a state change to child processes. | 189 // Notifies a state change to child processes. |
| 181 void NotifyStateToChildren(); | 190 void NotifyStateToChildren(MemoryState state); |
| 182 | 191 |
| 183 std::unique_ptr<MemoryCoordinatorDelegate> delegate_; | 192 std::unique_ptr<MemoryCoordinatorDelegate> delegate_; |
| 184 std::unique_ptr<MemoryMonitor> memory_monitor_; | 193 std::unique_ptr<MemoryMonitor> memory_monitor_; |
| 185 std::unique_ptr<MemoryStateUpdater> state_updater_; | 194 std::unique_ptr<MemoryConditionObserver> condition_observer_; |
| 186 NotificationRegistrar notification_registrar_; | 195 NotificationRegistrar notification_registrar_; |
| 187 // The global memory state. | 196 |
| 188 MemoryState current_state_ = MemoryState::NORMAL; | 197 // The current memory condition. |
| 189 // The time tick of last global state change. | 198 MemoryCondition memory_condition_ = MemoryCondition::NORMAL; |
| 199 |
| 200 // The memory state of the browser process. |
| 201 MemoryState browser_memory_state_ = MemoryState::NORMAL; |
| 202 |
| 203 // The time tick of last state change of the browser process. |
| 190 base::TimeTicks last_state_change_; | 204 base::TimeTicks last_state_change_; |
| 205 |
| 206 // Memory state for a process will remain unchanged until this period of time |
| 207 // passes. |
| 208 base::TimeDelta minimum_state_transition_period_; |
| 209 |
| 191 // Tracks child processes. An entry is added when a renderer connects to | 210 // Tracks child processes. An entry is added when a renderer connects to |
| 192 // MemoryCoordinator and removed automatically when an underlying binding is | 211 // MemoryCoordinator and removed automatically when an underlying binding is |
| 193 // disconnected. | 212 // disconnected. |
| 194 ChildInfoMap children_; | 213 ChildInfoMap children_; |
| 195 | 214 |
| 196 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); | 215 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); |
| 197 }; | 216 }; |
| 198 | 217 |
| 199 } // namespace content | 218 } // namespace content |
| 200 | 219 |
| 201 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ | 220 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ |
| OLD | NEW |