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

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

Issue 2568393002: Separate state updating code from MemoryCoordinatorImpl (Closed)
Patch Set: rebase 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
« no previous file with comments | « content/browser/BUILD.gn ('k') | content/browser/memory/memory_coordinator_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/singleton.h" 9 #include "base/memory/singleton.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
12 #include "base/threading/non_thread_safe.h" 11 #include "base/threading/non_thread_safe.h"
13 #include "base/time/time.h" 12 #include "base/time/time.h"
14 #include "content/browser/memory/memory_coordinator.h" 13 #include "content/browser/memory/memory_coordinator.h"
15 #include "content/public/browser/notification_observer.h" 14 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h" 15 #include "content/public/browser/notification_registrar.h"
17 16
18 namespace content { 17 namespace content {
19 18
20 class MemoryMonitor; 19 class MemoryMonitor;
21 class MemoryCoordinatorImplTest; 20 class MemoryCoordinatorImplTest;
21 class MemoryStateUpdater;
22 struct MemoryCoordinatorSingletonTraits; 22 struct MemoryCoordinatorSingletonTraits;
23 23
24 // MemoryCoordinatorImpl is an internal implementation of MemoryCoordinator 24 // MemoryCoordinatorImpl is an implementation of MemoryCoordinator.
25 // which uses a heuristic to determine a single global memory state. 25 // The current implementation uses MemoryStateUpdater to update the global
26 // In the current implementation browser process and renderer processes share 26 // memory state. See comments in MemoryStateUpdater for details.
27 // the global state; the memory coordinator will notify the global state to
28 // all background renderers if the state has changed.
29 //
30 // State calculation:
31 // MemoryCoordinatorImpl uses followings to determine the global state:
32 // * Compute "number of renderers which can be created until the system will
33 // be in a critical state". Call this N.
34 // (See memory_monitor.h for the definition of "critical")
35 // * Covert N to a memory state using some thresholds/hysteresis for each state.
36 // Once a state is changed to a limited state, larger N will be needed to go
37 // back to a relaxed state. (e.g. THROTTLED -> NORMAL)
38 // * Once a state is changed, it remains the same for a certain period of time.
39 class CONTENT_EXPORT MemoryCoordinatorImpl : public MemoryCoordinator, 27 class CONTENT_EXPORT MemoryCoordinatorImpl : public MemoryCoordinator,
40 public NotificationObserver, 28 public NotificationObserver,
41 public base::NonThreadSafe { 29 public base::NonThreadSafe {
42 public: 30 public:
31 using MemoryState = base::MemoryState;
32
43 MemoryCoordinatorImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner, 33 MemoryCoordinatorImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
44 std::unique_ptr<MemoryMonitor> monitor); 34 std::unique_ptr<MemoryMonitor> monitor);
45 ~MemoryCoordinatorImpl() override; 35 ~MemoryCoordinatorImpl() override;
46 36
37 MemoryMonitor* memory_monitor() { return memory_monitor_.get(); }
38 MemoryStateUpdater* state_updater() { return state_updater_.get(); }
39
47 // MemoryCoordinator implementations: 40 // MemoryCoordinator implementations:
48 void Start() override; 41 void Start() override;
49 void OnChildAdded(int render_process_id) override; 42 void OnChildAdded(int render_process_id) override;
50 43
51 MemoryMonitor* memory_monitor() { return memory_monitor_.get(); } 44 MemoryState GetGlobalMemoryState() const override;
52 45 MemoryState GetCurrentMemoryState() const override;
53 base::MemoryState GetGlobalMemoryState() const override; 46 void SetCurrentMemoryStateForTesting(MemoryState memory_state) override;
54 base::MemoryState GetCurrentMemoryState() const override;
55 void SetCurrentMemoryStateForTesting(base::MemoryState memory_state) override;
56 47
57 // NotificationObserver implementation: 48 // NotificationObserver implementation:
58 void Observe(int type, 49 void Observe(int type,
59 const NotificationSource& source, 50 const NotificationSource& source,
60 const NotificationDetails& details) override; 51 const NotificationDetails& details) override;
61 52
62 // Overrides the global state to |new_state|. State update tasks won't be 53 // Overrides the global state to |new_state|. State update tasks won't be
63 // scheduled until |duration| is passed. This means that the global state 54 // scheduled until |duration| is passed. This means that the global state
64 // remains the same until |duration| is passed or another call of this method. 55 // remains the same until |duration| is passed or another call of this method.
65 void ForceSetGlobalState(base::MemoryState new_state, 56 void ForceSetGlobalState(base::MemoryState new_state,
66 base::TimeDelta duration); 57 base::TimeDelta duration);
67 58
59 // Changes the global state and notifies state changes to clients (lives in
60 // the browser) and child processes (renderers) if needed. Returns true when
61 // the state is actually changed.
62 bool ChangeStateIfNeeded(MemoryState prev_state, MemoryState next_state);
63
68 private: 64 private:
69 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState); 65 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState);
70 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState); 66 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState);
71 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting); 67 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting);
72 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetGlobalState); 68 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetGlobalState);
73 69
74 friend struct MemoryCoordinatorSingletonTraits; 70 friend struct MemoryCoordinatorSingletonTraits;
75 71
76 using MemoryState = base::MemoryState;
77
78 // Changes the global state and notifies state changes to clients (lives in
79 // the browser) and child processes (renderers) if needed. Returns true when
80 // the state is actually changed.
81 bool ChangeStateIfNeeded(MemoryState prev_state, MemoryState next_state);
82
83 // Calculates the next global state from the amount of free memory using
84 // a heuristic.
85 MemoryState CalculateNextState();
86
87 // Periodically called to update the global state.
88 void UpdateState();
89
90 // Notifies a state change to in-process clients. 72 // Notifies a state change to in-process clients.
91 void NotifyStateToClients(); 73 void NotifyStateToClients();
92 74
93 // Notifies a state change to child processes. 75 // Notifies a state change to child processes.
94 void NotifyStateToChildren(); 76 void NotifyStateToChildren();
95 77
96 // Records metrics. This is called when the global state is changed. 78 // Records metrics. This is called when the global state is changed.
97 void RecordStateChange(MemoryState prev_state, 79 void RecordStateChange(MemoryState prev_state,
98 MemoryState next_state, 80 MemoryState next_state,
99 base::TimeDelta duration); 81 base::TimeDelta duration);
100 82
101 // Schedules a task to update the global state. The task will be executed 83 std::unique_ptr<MemoryMonitor> memory_monitor_;
102 // after |delay| has passed.
103 void ScheduleUpdateState(base::TimeDelta delay);
104
105 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
106 NotificationRegistrar notification_registrar_; 84 NotificationRegistrar notification_registrar_;
107 std::unique_ptr<MemoryMonitor> memory_monitor_; 85 std::unique_ptr<MemoryStateUpdater> state_updater_;
108 base::CancelableClosure update_state_closure_; 86 MemoryState current_state_ = MemoryState::NORMAL;
109 base::MemoryState current_state_ = MemoryState::NORMAL;
110 base::TimeTicks last_state_change_; 87 base::TimeTicks last_state_change_;
111 88
112 // Sets up parameters for the heuristic.
113 void InitializeParameters();
114
115 // Validates parameters defined below.
116 bool ValidateParameters();
117
118 // Parameters to control the heuristic.
119
120 // The median size of a renderer on the current platform. This is used to
121 // convert the amount of free memory to an expected number of new renderers
122 // that could be started before hitting critical memory pressure.
123 int expected_renderer_size_;
124 // When in a NORMAL state and the potential number of new renderers drops
125 // below this level, the coordinator will transition to a THROTTLED state.
126 int new_renderers_until_throttled_;
127 // When in a NORMAL/THROTTLED state and the potential number of new renderers
128 // drops below this level, the coordinator will transition to a SUSPENDED
129 // state.
130 int new_renderers_until_suspended_;
131 // When in a THROTTLED/SUSPENDED state and the potential number of new
132 // renderers rises above this level, the coordinator will transition to a
133 // NORMAL state.
134 int new_renderers_back_to_normal_;
135 // When in a SUSPENDED state and the potential number of new renderers rises
136 // above this level, the coordinator will transition to a SUSPENDED state.
137 int new_renderers_back_to_throttled_;
138 // The memory coordinator stays in the same state at least this duration even
139 // when there are considerable changes in the amount of free memory to prevent
140 // thrashing.
141 base::TimeDelta minimum_transition_period_;
142 // The interval of checking the amount of free memory.
143 base::TimeDelta monitoring_interval_;
144
145 base::WeakPtrFactory<MemoryCoordinatorImpl> weak_ptr_factory_;
146
147 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); 89 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl);
148 }; 90 };
149 91
150 } // namespace content 92 } // namespace content
151 93
152 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ 94 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_
OLDNEW
« no previous file with comments | « content/browser/BUILD.gn ('k') | content/browser/memory/memory_coordinator_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698