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/singleton.h" | 9 #include "base/memory/singleton.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "content/browser/memory/memory_coordinator.h" | 14 #include "content/browser/memory/memory_coordinator.h" |
| 15 #include "content/public/browser/notification_observer.h" |
| 16 #include "content/public/browser/notification_registrar.h" |
15 | 17 |
16 namespace content { | 18 namespace content { |
17 | 19 |
18 class MemoryMonitor; | 20 class MemoryMonitor; |
19 class MemoryCoordinatorImplTest; | 21 class MemoryCoordinatorImplTest; |
20 struct MemoryCoordinatorSingletonTraits; | 22 struct MemoryCoordinatorSingletonTraits; |
21 | 23 |
22 // MemoryCoordinatorImpl is an internal implementation of MemoryCoordinator | 24 // MemoryCoordinatorImpl is an internal implementation of MemoryCoordinator |
23 // which uses a heuristic to determine a single global memory state. | 25 // which uses a heuristic to determine a single global memory state. |
24 // In the current implementation browser process and renderer processes share | 26 // In the current implementation browser process and renderer processes share |
25 // the global state; the memory coordinator will notify the global state to | 27 // the global state; the memory coordinator will notify the global state to |
26 // all background renderers if the state has changed. | 28 // all background renderers if the state has changed. |
27 // | 29 // |
28 // State calculation: | 30 // State calculation: |
29 // MemoryCoordinatorImpl uses followings to determine the global state: | 31 // MemoryCoordinatorImpl uses followings to determine the global state: |
30 // * Compute "number of renderers which can be created until the system will | 32 // * Compute "number of renderers which can be created until the system will |
31 // be in a critical state". Call this N. | 33 // be in a critical state". Call this N. |
32 // (See memory_monitor.h for the definition of "critical") | 34 // (See memory_monitor.h for the definition of "critical") |
33 // * Covert N to a memory state using some thresholds/hysteresis for each state. | 35 // * Covert N to a memory state using some thresholds/hysteresis for each state. |
34 // Once a state is changed to a limited state, larger N will be needed to go | 36 // Once a state is changed to a limited state, larger N will be needed to go |
35 // back to a relaxed state. (e.g. THROTTLED -> NORMAL) | 37 // back to a relaxed state. (e.g. THROTTLED -> NORMAL) |
36 // * Once a state is changed, it remains the same for a certain period of time. | 38 // * Once a state is changed, it remains the same for a certain period of time. |
37 class CONTENT_EXPORT MemoryCoordinatorImpl : public MemoryCoordinator, | 39 class CONTENT_EXPORT MemoryCoordinatorImpl : public MemoryCoordinator, |
| 40 public NotificationObserver, |
38 public base::NonThreadSafe { | 41 public base::NonThreadSafe { |
39 public: | 42 public: |
40 MemoryCoordinatorImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 43 MemoryCoordinatorImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
41 std::unique_ptr<MemoryMonitor> monitor); | 44 std::unique_ptr<MemoryMonitor> monitor); |
42 ~MemoryCoordinatorImpl() override; | 45 ~MemoryCoordinatorImpl() override; |
43 | 46 |
44 // MemoryCoordinator implementations: | 47 // MemoryCoordinator implementations: |
45 void Start() override; | 48 void Start() override; |
46 void OnChildAdded(int render_process_id) override; | 49 void OnChildAdded(int render_process_id) override; |
47 | 50 |
48 MemoryMonitor* memory_monitor() { return memory_monitor_.get(); } | 51 MemoryMonitor* memory_monitor() { return memory_monitor_.get(); } |
49 | 52 |
50 base::MemoryState GetCurrentMemoryState() const override; | 53 base::MemoryState GetCurrentMemoryState() const override; |
51 | 54 |
| 55 // NotificationObserver implementation: |
| 56 void Observe(int type, |
| 57 const NotificationSource& source, |
| 58 const NotificationDetails& details) override; |
| 59 |
52 private: | 60 private: |
53 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState); | 61 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState); |
54 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState); | 62 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState); |
55 | 63 |
56 friend struct MemoryCoordinatorSingletonTraits; | 64 friend struct MemoryCoordinatorSingletonTraits; |
57 | 65 |
58 using MemoryState = base::MemoryState; | 66 using MemoryState = base::MemoryState; |
59 | 67 |
60 // Calculates the next global state from the amount of free memory using | 68 // Calculates the next global state from the amount of free memory using |
61 // a heuristic. | 69 // a heuristic. |
62 MemoryState CalculateNextState(); | 70 MemoryState CalculateNextState(); |
63 | 71 |
64 // Updates the global state and notifies state changes to clients (lives in | 72 // Updates the global state and notifies state changes to clients (lives in |
65 // the browser) and child processes (renderers) if necessary. | 73 // the browser) and child processes (renderers) if necessary. |
66 void UpdateState(); | 74 void UpdateState(); |
67 | 75 |
68 // Notifies a state change to in-process clients. | 76 // Notifies a state change to in-process clients. |
69 void NotifyStateToClients(); | 77 void NotifyStateToClients(); |
70 | 78 |
71 // Notifies a state change to child processes. | 79 // Notifies a state change to child processes. |
72 void NotifyStateToChildren(); | 80 void NotifyStateToChildren(); |
73 | 81 |
74 // Schedules a task to update the global state. The task will be executed | 82 // Schedules a task to update the global state. The task will be executed |
75 // after |delay| has passed. | 83 // after |delay| has passed. |
76 void ScheduleUpdateState(base::TimeDelta delay); | 84 void ScheduleUpdateState(base::TimeDelta delay); |
77 | 85 |
78 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 86 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 87 NotificationRegistrar notification_registrar_; |
79 std::unique_ptr<MemoryMonitor> memory_monitor_; | 88 std::unique_ptr<MemoryMonitor> memory_monitor_; |
80 base::Closure update_state_callback_; | 89 base::Closure update_state_callback_; |
81 base::MemoryState current_state_ = MemoryState::NORMAL; | 90 base::MemoryState current_state_ = MemoryState::NORMAL; |
82 base::TimeTicks last_state_change_; | 91 base::TimeTicks last_state_change_; |
83 | 92 |
84 // Validates parameters defined below. | 93 // Validates parameters defined below. |
85 bool ValidateParameters(); | 94 bool ValidateParameters(); |
86 | 95 |
87 // Parameters to control the heuristic. | 96 // Parameters to control the heuristic. |
88 | 97 |
(...skipping 23 matching lines...) Expand all Loading... |
112 base::TimeDelta monitoring_interval_; | 121 base::TimeDelta monitoring_interval_; |
113 | 122 |
114 base::WeakPtrFactory<MemoryCoordinatorImpl> weak_ptr_factory_; | 123 base::WeakPtrFactory<MemoryCoordinatorImpl> weak_ptr_factory_; |
115 | 124 |
116 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); | 125 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); |
117 }; | 126 }; |
118 | 127 |
119 } // namespace content | 128 } // namespace content |
120 | 129 |
121 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ | 130 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ |
OLD | NEW |