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

Side by Side Diff: content/browser/memory/memory_state_updater.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
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_STATE_UPDATER_H_
6 #define CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ 6 #define CONTENT_BROWSER_MEMORY_MEMORY_STATE_UPDATER_H_
7 7
8 #include "base/callback.h" 8 #include "base/cancelable_callback.h"
9 #include "base/memory/singleton.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/single_thread_task_runner.h" 9 #include "base/single_thread_task_runner.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/time/time.h" 10 #include "base/time/time.h"
14 #include "content/browser/memory/memory_coordinator.h" 11 #include "content/browser/memory/memory_coordinator_impl.h"
15 #include "content/public/browser/notification_observer.h" 12 #include "content/common/content_export.h"
16 #include "content/public/browser/notification_registrar.h"
17 13
18 namespace content { 14 namespace content {
19 15
20 class MemoryMonitor; 16 // MemoryStateUpdater is an internal implementation of MemoryCoordinator
21 class MemoryCoordinatorImplTest;
22 struct MemoryCoordinatorSingletonTraits;
23
24 // MemoryCoordinatorImpl is an internal implementation of MemoryCoordinator
25 // which uses a heuristic to determine a single global memory state. 17 // which uses a heuristic to determine a single global memory state.
26 // In the current implementation browser process and renderer processes share 18 // In the current implementation browser process and renderer processes share
27 // the global state; the memory coordinator will notify the global state to 19 // the global state; the memory coordinator will notify the global state to
28 // all background renderers if the state has changed. 20 // all background renderers if the state has changed.
29 // 21 //
30 // State calculation: 22 // State calculation:
31 // MemoryCoordinatorImpl uses followings to determine the global state: 23 // MemoryStateUpdater uses followings to determine the global state:
32 // * Compute "number of renderers which can be created until the system will 24 // * Compute "number of renderers which can be created until the system will
33 // be in a critical state". Call this N. 25 // be in a critical state". Call this N.
34 // (See memory_monitor.h for the definition of "critical") 26 // (See memory_monitor.h for the definition of "critical")
35 // * Covert N to a memory state using some thresholds/hysteresis for each state. 27 // * 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 28 // 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) 29 // 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. 30 // * Once a state is changed, it remains the same for a certain period of time.
39 class CONTENT_EXPORT MemoryCoordinatorImpl : public MemoryCoordinator, 31 class CONTENT_EXPORT MemoryStateUpdater {
40 public NotificationObserver,
41 public base::NonThreadSafe {
42 public: 32 public:
43 MemoryCoordinatorImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner, 33 // |coordinator| must outlive than this instance.
44 std::unique_ptr<MemoryMonitor> monitor); 34 MemoryStateUpdater(MemoryCoordinatorImpl* coordinator,
45 ~MemoryCoordinatorImpl() override; 35 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
36 ~MemoryStateUpdater();
46 37
47 // MemoryCoordinator implementations: 38 // Calculates the next global state from the amount of free memory using
48 void Start() override; 39 // a heuristic.
49 void OnChildAdded(int render_process_id) override; 40 base::MemoryState CalculateNextState();
50 41
51 MemoryMonitor* memory_monitor() { return memory_monitor_.get(); } 42 // Schedules a task to update the global state. The task will be executed
52 43 // after |delay| has passed.
53 base::MemoryState GetGlobalMemoryState() const override; 44 void ScheduleUpdateState(base::TimeDelta delay);
54 base::MemoryState GetCurrentMemoryState() const override;
55 void SetCurrentMemoryStateForTesting(base::MemoryState memory_state) override;
56
57 // NotificationObserver implementation:
58 void Observe(int type,
59 const NotificationSource& source,
60 const NotificationDetails& details) override;
61
62 // 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
64 // remains the same until |duration| is passed or another call of this method.
65 void ForceSetGlobalState(base::MemoryState new_state,
66 base::TimeDelta duration);
67 45
68 private: 46 private:
69 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState); 47 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState);
70 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState); 48 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState);
71 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting); 49 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting);
72 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetGlobalState); 50 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetGlobalState);
73 51
74 friend struct MemoryCoordinatorSingletonTraits;
75
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. 52 // Periodically called to update the global state.
88 void UpdateState(); 53 void UpdateState();
89 54
90 // Notifies a state change to in-process clients. 55 MemoryCoordinatorImpl* coordinator_;
91 void NotifyStateToClients();
92
93 // Notifies a state change to child processes.
94 void NotifyStateToChildren();
95
96 // Records metrics. This is called when the global state is changed.
97 void RecordStateChange(MemoryState prev_state,
98 MemoryState next_state,
99 base::TimeDelta duration);
100
101 // Schedules a task to update the global state. The task will be executed
102 // after |delay| has passed.
103 void ScheduleUpdateState(base::TimeDelta delay);
104
105 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 56 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
106 NotificationRegistrar notification_registrar_;
107 std::unique_ptr<MemoryMonitor> memory_monitor_;
108 base::CancelableClosure update_state_closure_; 57 base::CancelableClosure update_state_closure_;
109 base::MemoryState current_state_ = MemoryState::NORMAL;
110 base::TimeTicks last_state_change_;
111 58
112 // Sets up parameters for the heuristic. 59 // Sets up parameters for the heuristic.
113 void InitializeParameters(); 60 void InitializeParameters();
114 61
115 // Validates parameters defined below. 62 // Validates parameters defined below.
116 bool ValidateParameters(); 63 bool ValidateParameters();
117 64
118 // Parameters to control the heuristic. 65 // Parameters to control the heuristic.
119 66
120 // The median size of a renderer on the current platform. This is used to 67 // The median size of a renderer on the current platform. This is used to
(...skipping 14 matching lines...) Expand all
135 // When in a SUSPENDED state and the potential number of new renderers rises 82 // 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. 83 // above this level, the coordinator will transition to a SUSPENDED state.
137 int new_renderers_back_to_throttled_; 84 int new_renderers_back_to_throttled_;
138 // The memory coordinator stays in the same state at least this duration even 85 // 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 86 // when there are considerable changes in the amount of free memory to prevent
140 // thrashing. 87 // thrashing.
141 base::TimeDelta minimum_transition_period_; 88 base::TimeDelta minimum_transition_period_;
142 // The interval of checking the amount of free memory. 89 // The interval of checking the amount of free memory.
143 base::TimeDelta monitoring_interval_; 90 base::TimeDelta monitoring_interval_;
144 91
145 base::WeakPtrFactory<MemoryCoordinatorImpl> weak_ptr_factory_; 92 DISALLOW_COPY_AND_ASSIGN(MemoryStateUpdater);
146
147 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl);
148 }; 93 };
149 94
150 } // namespace content 95 } // namespace content
151 96
152 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ 97 #endif // CONTENT_BROWSER_MEMORY_MEMORY_STATE_UPDATER_H_
OLDNEW
« no previous file with comments | « content/browser/memory/memory_coordinator_impl_unittest.cc ('k') | content/browser/memory/memory_state_updater.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698