Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: content/browser/memory/memory_coordinator_impl.h

Issue 2718963002: Drop the global memory state from memory coordinator (Closed)
Patch Set: format Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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;
76
77 // Returns the browser's current memory state. Note that the current state
78 // could be different from the global memory state as the browser won't be
79 // suspended.
80 MemoryState GetCurrentMemoryState() const override; 82 MemoryState GetCurrentMemoryState() const override;
81 83
82 // Sets the global memory state for testing. 84 // Temporariy sets memory state of the browser process for testing.
haraken 2017/02/28 11:33:45 Temporarily
bashi 2017/03/02 04:01:45 Done.
83 void SetCurrentMemoryStateForTesting(MemoryState memory_state) override; 85 void SetCurrentMemoryStateForTesting(MemoryState memory_state) override;
84 86
85 // MemoryCoordinator implementation: 87 // content::MemoryCoordinator implementation:
86 MemoryState GetStateForProcess(base::ProcessHandle handle) override; 88 MemoryState GetStateForProcess(base::ProcessHandle handle) override;
87 89
88 // NotificationObserver implementation: 90 // NotificationObserver implementation:
89 void Observe(int type, 91 void Observe(int type,
90 const NotificationSource& source, 92 const NotificationSource& source,
91 const NotificationDetails& details) override; 93 const NotificationDetails& details) override;
92 94
93 // Overrides the global state to |new_state|. State update tasks won't be 95 // Returns the current memory condition.
94 // scheduled until |duration| is passed. This means that the global state 96 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 97
99 // Changes the global state and notifies state changes to clients (lives in 98 // Overrides the current memory condition to |condition|. Memory condition
100 // the browser) and child processes (renderers) if needed. Returns true when 99 // update tasks won't be scheduled until |duration| is passed. This means that
101 // the state is actually changed. 100 // the memory condition remains the same until |duration| is passed or
102 bool ChangeStateIfNeeded(MemoryState prev_state, MemoryState next_state); 101 // another call of this method.
102 void ForceSetMemoryCondition(MemoryCondition condition,
103 base::TimeDelta duration);
104
105 // Changes current memory condition if needed. This may trigger some actions
106 // like purging memory and memory state changes.
107 void UpdateConditionIfNeeded(MemoryCondition condition);
103 108
104 // Asks the delegate to discard a tab. 109 // Asks the delegate to discard a tab.
105 void DiscardTab(); 110 void DiscardTab();
106 111
107 protected: 112 protected:
108 // Returns the RenderProcessHost which is correspond to the given id. 113 // Returns the RenderProcessHost which is correspond to the given id.
109 // Returns nullptr if there is no corresponding RenderProcessHost. 114 // Returns nullptr if there is no corresponding RenderProcessHost.
110 // This is a virtual method so that we can write tests without having 115 // This is a virtual method so that we can write tests without having
111 // actual RenderProcessHost. 116 // actual RenderProcessHost.
112 virtual RenderProcessHost* GetRenderProcessHost(int render_process_id); 117 virtual RenderProcessHost* GetRenderProcessHost(int render_process_id);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 ChildInfoMap& children() { return children_; } 151 ChildInfoMap& children() { return children_; }
147 152
148 private: 153 private:
149 #if !defined(OS_MACOSX) 154 #if !defined(OS_MACOSX)
150 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, HandleAdded); 155 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, HandleAdded);
151 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, 156 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest,
152 CanSuspendRenderer); 157 CanSuspendRenderer);
153 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorWithServiceWorkerTest, 158 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorWithServiceWorkerTest,
154 CannotSuspendRendererWithServiceWorker); 159 CannotSuspendRendererWithServiceWorker);
155 #endif 160 #endif
156 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState); 161 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextCondition);
157 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState); 162 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateCondition);
158 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting); 163 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting);
159 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetGlobalState); 164 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetMemoryCondition);
160 165
161 friend struct MemoryCoordinatorSingletonTraits; 166 friend struct MemoryCoordinatorSingletonTraits;
162 friend class MemoryCoordinatorHandleImpl; 167 friend class MemoryCoordinatorHandleImpl;
163 168
169 // Updates the browser's memory state if needed.
170 void UpdateCurrentMemoryState(MemoryState state);
171
164 // Called when ChildMemoryCoordinator calls AddChild(). 172 // Called when ChildMemoryCoordinator calls AddChild().
165 void OnChildAdded(int render_process_id); 173 void OnChildAdded(int render_process_id);
166 174
167 // Called by SetChildMemoryState() to determine a child memory state based on 175 // Called by SetChildMemoryState() to determine a child memory state based on
168 // the current status of the child process. 176 // the current status of the child process.
169 MemoryState OverrideGlobalState(MemoryState memroy_state, 177 MemoryState OverrideState(MemoryState memroy_state, const ChildInfo& child);
170 const ChildInfo& child);
171 178
172 // Helper function of CreateHandle and AddChildForTesting. 179 // Helper function of CreateHandle and AddChildForTesting.
173 void CreateChildInfoMapEntry( 180 void CreateChildInfoMapEntry(
174 int render_process_id, 181 int render_process_id,
175 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); 182 std::unique_ptr<MemoryCoordinatorHandleImpl> handle);
176 183
177 // Notifies a state change to in-process clients. 184 // Notifies a state change to in-process clients.
178 void NotifyStateToClients(); 185 void NotifyStateToClients(MemoryState state);
179 186
180 // Notifies a state change to child processes. 187 // Notifies a state change to child processes.
181 void NotifyStateToChildren(); 188 void NotifyStateToChildren(MemoryState state);
182 189
183 std::unique_ptr<MemoryCoordinatorDelegate> delegate_; 190 std::unique_ptr<MemoryCoordinatorDelegate> delegate_;
184 std::unique_ptr<MemoryMonitor> memory_monitor_; 191 std::unique_ptr<MemoryMonitor> memory_monitor_;
185 std::unique_ptr<MemoryStateUpdater> state_updater_; 192 std::unique_ptr<MemoryConditionObserver> condition_observer_;
186 NotificationRegistrar notification_registrar_; 193 NotificationRegistrar notification_registrar_;
187 // The global memory state. 194
195 // The current memory condition.
196 MemoryCondition memory_condition_ = MemoryCondition::NORMAL;
197
198 // The memory state of the browser process.
188 MemoryState current_state_ = MemoryState::NORMAL; 199 MemoryState current_state_ = MemoryState::NORMAL;
haraken 2017/02/28 11:33:45 current_state_ => browser_memory_state_ ?
bashi 2017/03/02 04:01:45 Yes. Done.
189 // The time tick of last global state change. 200
201 // The time tick of last state change of the browser process.
190 base::TimeTicks last_state_change_; 202 base::TimeTicks last_state_change_;
203
204 // Memory state for a process will remain unchanged until this period of time
205 // passes.
206 base::TimeDelta minimum_state_transition_period_;
207
191 // Tracks child processes. An entry is added when a renderer connects to 208 // Tracks child processes. An entry is added when a renderer connects to
192 // MemoryCoordinator and removed automatically when an underlying binding is 209 // MemoryCoordinator and removed automatically when an underlying binding is
193 // disconnected. 210 // disconnected.
194 ChildInfoMap children_; 211 ChildInfoMap children_;
195 212
196 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); 213 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl);
197 }; 214 };
198 215
199 } // namespace content 216 } // namespace content
200 217
201 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ 218 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698