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_STATE_UPDATER_H_ | |
6 #define CONTENT_BROWSER_MEMORY_MEMORY_STATE_UPDATER_H_ | |
7 | |
8 #include "base/cancelable_callback.h" | |
9 #include "base/single_thread_task_runner.h" | |
10 #include "base/time/time.h" | |
11 #include "content/browser/memory/memory_coordinator_impl.h" | |
12 #include "content/common/content_export.h" | |
13 | |
14 namespace content { | |
15 | |
16 // MemoryStateUpdater is an internal implementation of MemoryCoordinator | |
17 // which uses a heuristic to determine a single global memory state. | |
18 // In the current implementation browser process and renderer processes share | |
19 // the global state; the memory coordinator will notify the global state to | |
20 // all background renderers if the state has changed. | |
21 // | |
22 // State calculation: | |
23 // MemoryStateUpdater uses followings to determine the global state: | |
24 // * Compute "number of renderers which can be created until the system will | |
25 // be in a critical state". Call this N. | |
26 // (See memory_monitor.h for the definition of "critical") | |
27 // * Covert N to a memory state using some thresholds/hysteresis for each state. | |
28 // Once a state is changed to a limited state, larger N will be needed to go | |
29 // back to a relaxed state. (e.g. THROTTLED -> NORMAL) | |
30 // * Once a state is changed, it remains the same for a certain period of time. | |
31 class CONTENT_EXPORT MemoryStateUpdater { | |
32 public: | |
33 // |coordinator| must outlive than this instance. | |
34 MemoryStateUpdater(MemoryCoordinatorImpl* coordinator, | |
35 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
36 ~MemoryStateUpdater(); | |
37 | |
38 // Calculates the next global state from the amount of free memory using | |
39 // a heuristic. | |
40 base::MemoryState CalculateNextState(); | |
41 | |
42 // Schedules a task to update the global state. The task will be executed | |
43 // after |delay| has passed. | |
44 void ScheduleUpdateState(base::TimeDelta delay); | |
45 | |
46 private: | |
47 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState); | |
48 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState); | |
49 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting); | |
50 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetGlobalState); | |
51 | |
52 // Periodically called to update the global state. | |
53 void UpdateState(); | |
54 | |
55 MemoryCoordinatorImpl* coordinator_; | |
56 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
57 base::CancelableClosure update_state_closure_; | |
58 | |
59 // Sets up parameters for the heuristic. | |
60 void InitializeParameters(); | |
61 | |
62 // Validates parameters defined below. | |
63 bool ValidateParameters(); | |
64 | |
65 // Parameters to control the heuristic. | |
66 | |
67 // The median size of a renderer on the current platform. This is used to | |
68 // convert the amount of free memory to an expected number of new renderers | |
69 // that could be started before hitting critical memory pressure. | |
70 int expected_renderer_size_; | |
71 // When in a NORMAL state and the potential number of new renderers drops | |
72 // below this level, the coordinator will transition to a THROTTLED state. | |
73 int new_renderers_until_throttled_; | |
74 // When in a NORMAL/THROTTLED state and the potential number of new renderers | |
75 // drops below this level, the coordinator will transition to a SUSPENDED | |
76 // state. | |
77 int new_renderers_until_suspended_; | |
78 // When in a THROTTLED/SUSPENDED state and the potential number of new | |
79 // renderers rises above this level, the coordinator will transition to a | |
80 // NORMAL state. | |
81 int new_renderers_back_to_normal_; | |
82 // When in a SUSPENDED state and the potential number of new renderers rises | |
83 // above this level, the coordinator will transition to a SUSPENDED state. | |
84 int new_renderers_back_to_throttled_; | |
85 // The memory coordinator stays in the same state at least this duration even | |
86 // when there are considerable changes in the amount of free memory to prevent | |
87 // thrashing. | |
88 base::TimeDelta minimum_transition_period_; | |
89 // The interval of checking the amount of free memory. | |
90 base::TimeDelta monitoring_interval_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(MemoryStateUpdater); | |
93 }; | |
94 | |
95 } // namespace content | |
96 | |
97 #endif // CONTENT_BROWSER_MEMORY_MEMORY_STATE_UPDATER_H_ | |
OLD | NEW |