Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ | |
| 6 #define CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ | |
| 7 | |
| 8 #include "base/callback.h" | |
| 9 #include "base/memory/singleton.h" | |
| 10 #include "base/memory/weak_ptr.h" | |
| 11 #include "base/single_thread_task_runner.h" | |
| 12 #include "base/threading/non_thread_safe.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "content/browser/memory/memory_coordinator.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 class MemoryMonitor; | |
| 19 class MemoryCoordinatorImplTest; | |
| 20 struct MemoryCoordinatorSingletonTraits; | |
| 21 | |
| 22 // MemoryCoordinatorImpl is an internal implementation of MemoryCoordinator | |
| 23 // which uses a heuristic to determine a single global memory state. | |
| 24 // In the current implementation browser process and renderer processes share | |
| 25 // the global state; the memory coordinator will notify the global state to | |
| 26 // all background renderers if the state has changed. | |
| 27 // | |
| 28 // State calculation: | |
| 29 // MemoryCoordinatorImpl uses followings to determine the global state: | |
| 30 // * Compute "number of renderers which can be created until the system will | |
| 31 // be in a critical state". Call this N. | |
| 32 // (See memory_monitor.h for the definition of "critical") | |
| 33 // * 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 | |
| 35 // 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. | |
| 37 class CONTENT_EXPORT MemoryCoordinatorImpl : public MemoryCoordinator, | |
| 38 public base::NonThreadSafe { | |
| 39 public: | |
| 40 MemoryCoordinatorImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 41 std::unique_ptr<MemoryMonitor> monitor); | |
| 42 ~MemoryCoordinatorImpl() override; | |
| 43 | |
| 44 // MemoryCoordinator implementations: | |
| 45 void Start() override; | |
| 46 void OnChildAdded(int render_process_id) override; | |
| 47 | |
| 48 MemoryMonitor* memory_monitor() { return memory_monitor_.get(); } | |
| 49 | |
| 50 private: | |
| 51 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState); | |
| 52 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState); | |
| 53 | |
| 54 friend struct MemoryCoordinatorSingletonTraits; | |
| 55 | |
| 56 using MemoryState = base::MemoryState; | |
| 57 | |
| 58 // Calculates the next global state from the amount of free memory using | |
| 59 // a heuristic. | |
| 60 MemoryState CalculateNextState(); | |
| 61 | |
| 62 // Updates the global state and notifies state changes to clients (lives in | |
| 63 // the browser) and child processes (renderers) if necessary. | |
| 64 void UpdateState(); | |
| 65 | |
| 66 // Notifies a state change to clients. | |
| 67 void NotifyStateToClients(); | |
| 68 | |
| 69 // Notifies a state change to child processes. | |
| 70 void NotifyStateToChildren(); | |
| 71 | |
| 72 // Schedules a task to update the global state. The task will be executed | |
| 73 // after |delay| has passed. | |
| 74 void ScheduleUpdateState(base::TimeDelta delay); | |
| 75 | |
| 76 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 77 std::unique_ptr<MemoryMonitor> memory_monitor_; | |
| 78 base::Closure update_state_callback_; | |
| 79 base::MemoryState current_state_ = MemoryState::NORMAL; | |
| 80 base::TimeTicks last_state_change_; | |
| 81 | |
| 82 // Validates parameters defined below. | |
| 83 bool ValidateParameters(); | |
| 84 | |
| 85 // Parameters to determine the global state. | |
| 86 | |
| 87 // The median size of a renderer on the current platform. This is used to | |
| 88 // convert the amount of free memory to an expected number of new renderers | |
| 89 // that could be started before hitting critical memory pressure. | |
| 90 int predicted_renderer_size_; | |
| 91 // When in a NORMAL state and the potential number of new renderers drop below | |
|
haraken
2016/10/14 01:27:59
drops
bashi
2016/10/14 03:03:00
Done.
| |
| 92 // this level, the coordinator will transition to a THROTTLED state. | |
| 93 int new_renderers_until_throttled_; | |
| 94 // When in a NORMAL/THROTTLED state and the potential number of new renderers | |
| 95 // drop below this level, the coordinator will transition to a SUSPENDED | |
|
haraken
2016/10/14 01:27:59
drops
bashi
2016/10/14 03:03:00
Done.
| |
| 96 // state. | |
| 97 int new_renderers_until_suspended_; | |
| 98 // When in a THROTTLED/SUSPENDED state and the potential number of new | |
| 99 // renderers rise above this level, the coordinator will transition to a | |
|
haraken
2016/10/14 01:27:59
rises
bashi
2016/10/14 03:03:00
Done.
| |
| 100 // NORMAL state. | |
| 101 int new_renderers_back_to_normal_; | |
| 102 // When in a SUSPENDED state and the potential number of new renderers rise | |
|
haraken
2016/10/14 01:27:59
rises
bashi
2016/10/14 03:03:00
Done.
| |
| 103 // above this level, the coordinator will transition to a SUSPENDED state. | |
| 104 int new_renderers_back_to_throttled_; | |
| 105 // The memory coordinator stays a same state at least this duration even when | |
|
haraken
2016/10/14 01:27:59
stays in the same state
bashi
2016/10/14 03:03:00
Done.
| |
| 106 // there are considerable changes in the amount of free memory to prevent | |
| 107 // thrashing. | |
| 108 base::TimeDelta minimum_transition_period_; | |
| 109 // The interval between checking the amount of free memory. | |
|
haraken
2016/10/14 01:27:59
between => of ?
bashi
2016/10/14 03:03:00
Done.
| |
| 110 base::TimeDelta monitoring_interval_; | |
| 111 | |
| 112 base::WeakPtrFactory<MemoryCoordinatorImpl> weak_ptr_factory_; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); | |
| 115 }; | |
| 116 | |
| 117 } // namespace content | |
| 118 | |
| 119 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ | |
| OLD | NEW |