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

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

Issue 2579233002: Merge MemoryCoordinator and MemoryCoordinatorImpl into one class (Closed)
Patch Set: addressed comments Created 4 years 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"
10 #include "base/memory/memory_pressure_monitor.h"
9 #include "base/memory/singleton.h" 11 #include "base/memory/singleton.h"
10 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
11 #include "base/threading/non_thread_safe.h" 13 #include "base/threading/non_thread_safe.h"
12 #include "base/time/time.h" 14 #include "base/time/time.h"
13 #include "content/browser/memory/memory_coordinator.h" 15 #include "content/common/content_export.h"
16 #include "content/common/memory_coordinator.mojom.h"
17 #include "content/public/browser/memory_coordinator_delegate.h"
14 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h" 19 #include "content/public/browser/notification_registrar.h"
16 20
17 namespace content { 21 namespace content {
18 22
23 // NOTE: Memory coordinator is under development and not fully working.
24 // TODO(bashi): Add content::MemoryCoordinator which is a pure virtual
25 // interface to expose APIs outside content/.
26
27 class MemoryCoordinatorHandleImpl;
28 class MemoryCoordinatorImplTest;
19 class MemoryMonitor; 29 class MemoryMonitor;
20 class MemoryCoordinatorImplTest;
21 class MemoryStateUpdater; 30 class MemoryStateUpdater;
22 struct MemoryCoordinatorSingletonTraits; 31 struct MemoryCoordinatorSingletonTraits;
23 32
24 // MemoryCoordinatorImpl is an implementation of MemoryCoordinator. 33 // MemoryCoordinatorImpl is an implementation of MemoryCoordinator.
25 // The current implementation uses MemoryStateUpdater to update the global 34 // The current implementation uses MemoryStateUpdater to update the global
26 // memory state. See comments in MemoryStateUpdater for details. 35 // memory state. See comments in MemoryStateUpdater for details.
27 class CONTENT_EXPORT MemoryCoordinatorImpl : public MemoryCoordinator, 36 class CONTENT_EXPORT MemoryCoordinatorImpl : public NotificationObserver,
28 public NotificationObserver,
29 public base::NonThreadSafe { 37 public base::NonThreadSafe {
30 public: 38 public:
31 using MemoryState = base::MemoryState; 39 using MemoryState = base::MemoryState;
32 40
41 static MemoryCoordinatorImpl* GetInstance();
42
33 MemoryCoordinatorImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner, 43 MemoryCoordinatorImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
34 std::unique_ptr<MemoryMonitor> monitor); 44 std::unique_ptr<MemoryMonitor> monitor);
35 ~MemoryCoordinatorImpl() override; 45 ~MemoryCoordinatorImpl() override;
36 46
37 MemoryMonitor* memory_monitor() { return memory_monitor_.get(); } 47 MemoryMonitor* memory_monitor() { return memory_monitor_.get(); }
38 MemoryStateUpdater* state_updater() { return state_updater_.get(); }
39 48
40 // MemoryCoordinator implementations: 49 // Starts monitoring memory usage. After calling this method, memory
41 void Start() override; 50 // coordinator will start dispatching state changes.
42 void OnChildAdded(int render_process_id) override; 51 void Start();
43 52
44 MemoryState GetGlobalMemoryState() const override; 53 // Creates a handle to the provided child process.
45 MemoryState GetCurrentMemoryState() const override; 54 void CreateHandle(int render_process_id,
46 void SetCurrentMemoryStateForTesting(MemoryState memory_state) override; 55 mojom::MemoryCoordinatorHandleRequest request);
56
57 // Dispatches a memory state change to the provided process. Returns true if
58 // the process is tracked by this coordinator and successfully dispatches,
59 // returns false otherwise.
60 bool SetChildMemoryState(
61 int render_process_id, mojom::MemoryState memory_state);
62
63 // Returns the memory state of the specified render process. Returns UNKNOWN
64 // if the process is not tracked by this coordinator.
65 mojom::MemoryState GetChildMemoryState(int render_process_id) const;
66
67 // Records memory pressure notifications. Called by MemoryPressureMonitor.
68 // TODO(bashi): Remove this when MemoryPressureMonitor is retired.
69 void RecordMemoryPressure(
70 base::MemoryPressureMonitor::MemoryPressureLevel level);
71
72 // Returns the global memory state.
73 virtual MemoryState GetGlobalMemoryState() const;
74
75 // Returns the browser's current memory state. Note that the current state
76 // could be different from the global memory state as the browser won't be
77 // suspended.
78 MemoryState GetCurrentMemoryState() const;
79
80 // Sets the global memory state for testing.
81 void SetCurrentMemoryStateForTesting(MemoryState memory_state);
47 82
48 // NotificationObserver implementation: 83 // NotificationObserver implementation:
49 void Observe(int type, 84 void Observe(int type,
50 const NotificationSource& source, 85 const NotificationSource& source,
51 const NotificationDetails& details) override; 86 const NotificationDetails& details) override;
52 87
53 // Overrides the global state to |new_state|. State update tasks won't be 88 // Overrides the global state to |new_state|. State update tasks won't be
54 // scheduled until |duration| is passed. This means that the global state 89 // scheduled until |duration| is passed. This means that the global state
55 // remains the same until |duration| is passed or another call of this method. 90 // remains the same until |duration| is passed or another call of this method.
56 void ForceSetGlobalState(base::MemoryState new_state, 91 void ForceSetGlobalState(base::MemoryState new_state,
57 base::TimeDelta duration); 92 base::TimeDelta duration);
58 93
59 // Changes the global state and notifies state changes to clients (lives in 94 // Changes the global state and notifies state changes to clients (lives in
60 // the browser) and child processes (renderers) if needed. Returns true when 95 // the browser) and child processes (renderers) if needed. Returns true when
61 // the state is actually changed. 96 // the state is actually changed.
62 bool ChangeStateIfNeeded(MemoryState prev_state, MemoryState next_state); 97 bool ChangeStateIfNeeded(MemoryState prev_state, MemoryState next_state);
63 98
99 protected:
100 // Adds the given ChildMemoryCoordinator as a child of this coordinator.
101 void AddChildForTesting(int dummy_render_process_id,
102 mojom::ChildMemoryCoordinatorPtr child);
103
104 // Callback invoked by mojo when the child connection goes down. Exposed
105 // for testing.
106 void OnConnectionError(int render_process_id);
107
108 // Returns true when a given renderer can be suspended.
109 bool CanSuspendRenderer(int render_process_id);
110
111 // Stores information about any known child processes.
112 struct ChildInfo {
113 // This object must be compatible with STL containers.
114 ChildInfo();
115 ChildInfo(const ChildInfo& rhs);
116 ~ChildInfo();
117
118 mojom::MemoryState memory_state;
119 bool is_visible = false;
120 std::unique_ptr<MemoryCoordinatorHandleImpl> handle;
121 };
122
123 // A map from process ID (RenderProcessHost::GetID()) to child process info.
124 using ChildInfoMap = std::map<int, ChildInfo>;
125
126 ChildInfoMap& children() { return children_; }
127
64 private: 128 private:
129 #if !defined(OS_MACOSX)
130 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, HandleAdded);
131 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest,
132 CanSuspendRenderer);
133 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorWithServiceWorkerTest,
134 CannotSuspendRendererWithServiceWorker);
135 #endif
65 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState); 136 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState);
66 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState); 137 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState);
67 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting); 138 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting);
68 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetGlobalState); 139 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetGlobalState);
69 140
70 friend struct MemoryCoordinatorSingletonTraits; 141 friend struct MemoryCoordinatorSingletonTraits;
142 friend class MemoryCoordinatorHandleImpl;
143
144 // Called when ChildMemoryCoordinator calls AddChild().
145 void OnChildAdded(int render_process_id);
146
147 // Called by SetChildMemoryState() to determine a child memory state based on
148 // the current status of the child process.
149 mojom::MemoryState OverrideGlobalState(mojom::MemoryState memroy_state,
150 const ChildInfo& child);
151
152 void SetDelegateForTesting(
153 std::unique_ptr<MemoryCoordinatorDelegate> delegate);
154
155 // Helper function of CreateHandle and AddChildForTesting.
156 void CreateChildInfoMapEntry(
157 int render_process_id,
158 std::unique_ptr<MemoryCoordinatorHandleImpl> handle);
71 159
72 // Notifies a state change to in-process clients. 160 // Notifies a state change to in-process clients.
73 void NotifyStateToClients(); 161 void NotifyStateToClients();
74 162
75 // Notifies a state change to child processes. 163 // Notifies a state change to child processes.
76 void NotifyStateToChildren(); 164 void NotifyStateToChildren();
77 165
78 // Records metrics. This is called when the global state is changed. 166 // Records metrics. This is called when the global state is changed.
79 void RecordStateChange(MemoryState prev_state, 167 void RecordStateChange(MemoryState prev_state,
80 MemoryState next_state, 168 MemoryState next_state,
81 base::TimeDelta duration); 169 base::TimeDelta duration);
82 170
171 std::unique_ptr<MemoryCoordinatorDelegate> delegate_;
83 std::unique_ptr<MemoryMonitor> memory_monitor_; 172 std::unique_ptr<MemoryMonitor> memory_monitor_;
173 std::unique_ptr<MemoryStateUpdater> state_updater_;
84 NotificationRegistrar notification_registrar_; 174 NotificationRegistrar notification_registrar_;
85 std::unique_ptr<MemoryStateUpdater> state_updater_; 175 // The global memory state.
86 MemoryState current_state_ = MemoryState::NORMAL; 176 MemoryState current_state_ = MemoryState::NORMAL;
177 // The time tick of last global state change.
87 base::TimeTicks last_state_change_; 178 base::TimeTicks last_state_change_;
179 // Tracks child processes. An entry is added when a renderer connects to
180 // MemoryCoordinator and removed automatically when an underlying binding is
181 // disconnected.
182 ChildInfoMap children_;
88 183
89 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); 184 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl);
90 }; 185 };
91 186
92 } // namespace content 187 } // namespace content
93 188
94 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ 189 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_
OLDNEW
« no previous file with comments | « content/browser/memory/memory_coordinator_browsertest.cc ('k') | content/browser/memory/memory_coordinator_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698