OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 CHROME_BROWSER_MEMORY_TAB_MANAGER_DELEGATE_CHROMEOS_H_ | 5 #ifndef CHROME_BROWSER_MEMORY_TAB_MANAGER_DELEGATE_CHROMEOS_H_ |
6 #define CHROME_BROWSER_MEMORY_TAB_MANAGER_DELEGATE_CHROMEOS_H_ | 6 #define CHROME_BROWSER_MEMORY_TAB_MANAGER_DELEGATE_CHROMEOS_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 28 matching lines...) Expand all Loading... |
39 FOCUSED_TAB = FOCUSED_APP, | 39 FOCUSED_TAB = FOCUSED_APP, |
40 | 40 |
41 VISIBLE_APP = 2, | 41 VISIBLE_APP = 2, |
42 BACKGROUND_TAB = 3, | 42 BACKGROUND_TAB = 3, |
43 BACKGROUND_APP = 4, | 43 BACKGROUND_APP = 4, |
44 UNKNOWN_TYPE = 5, | 44 UNKNOWN_TYPE = 5, |
45 }; | 45 }; |
46 | 46 |
47 // The Chrome OS TabManagerDelegate is responsible for keeping the | 47 // The Chrome OS TabManagerDelegate is responsible for keeping the |
48 // renderers' scores up to date in /proc/<pid>/oom_score_adj. | 48 // renderers' scores up to date in /proc/<pid>/oom_score_adj. |
49 // | |
50 // Note that AdjustOomPriorities will be called on the UI thread by | |
51 // TabManager, but the actual work will take place on the file thread | |
52 // (see implementation of AdjustOomPriorities). | |
53 class TabManagerDelegate | 49 class TabManagerDelegate |
54 : public arc::InstanceHolder<arc::mojom::ProcessInstance>::Observer, | 50 : public arc::InstanceHolder<arc::mojom::ProcessInstance>::Observer, |
55 public aura::client::ActivationChangeObserver, | 51 public aura::client::ActivationChangeObserver, |
56 public content::NotificationObserver, | 52 public content::NotificationObserver, |
57 public chrome::BrowserListObserver { | 53 public chrome::BrowserListObserver { |
58 public: | 54 public: |
59 class MemoryStat; | 55 class MemoryStat; |
60 | 56 |
61 explicit TabManagerDelegate(const base::WeakPtr<TabManager>& tab_manager); | 57 explicit TabManagerDelegate(const base::WeakPtr<TabManager>& tab_manager); |
62 | 58 |
(...skipping 19 matching lines...) Expand all Loading... |
82 | 78 |
83 // Returns oom_score_adj of a process if the score is cached by |this|. | 79 // Returns oom_score_adj of a process if the score is cached by |this|. |
84 // If couldn't find the score in the cache, returns -1001 since the valid | 80 // If couldn't find the score in the cache, returns -1001 since the valid |
85 // range of oom_score_adj is [-1000, 1000]. | 81 // range of oom_score_adj is [-1000, 1000]. |
86 int GetCachedOomScore(base::ProcessHandle process_handle); | 82 int GetCachedOomScore(base::ProcessHandle process_handle); |
87 | 83 |
88 // Called when the timer fires, sets oom_adjust_score for all renderers. | 84 // Called when the timer fires, sets oom_adjust_score for all renderers. |
89 void AdjustOomPriorities(const TabStatsList& tab_list); | 85 void AdjustOomPriorities(const TabStatsList& tab_list); |
90 | 86 |
91 protected: | 87 protected: |
92 // Sets oom_score_adj for a list of tabs. | |
93 // This is a delegator to to SetOomScoreAdjForTabsOnFileThread(), | |
94 // also as a seam for unit test. | |
95 virtual void SetOomScoreAdjForTabs( | |
96 const std::vector<std::pair<base::ProcessHandle, int>>& entries); | |
97 | |
98 // Kills an Arc process. Returns true if the kill request is successfully sent | 88 // Kills an Arc process. Returns true if the kill request is successfully sent |
99 // to Android. Virtual for unit testing. | 89 // to Android. Virtual for unit testing. |
100 virtual bool KillArcProcess(const int nspid); | 90 virtual bool KillArcProcess(const int nspid); |
101 | 91 |
102 // Kills a tab. Returns true if the tab is killed successfully. | 92 // Kills a tab. Returns true if the tab is killed successfully. |
103 // Virtual for unit testing. | 93 // Virtual for unit testing. |
104 virtual bool KillTab(int64_t tab_id); | 94 virtual bool KillTab(int64_t tab_id); |
105 | 95 |
106 private: | 96 private: |
107 FRIEND_TEST_ALL_PREFIXES(TabManagerDelegateTest, CandidatesSorted); | 97 FRIEND_TEST_ALL_PREFIXES(TabManagerDelegateTest, CandidatesSorted); |
(...skipping 16 matching lines...) Expand all Loading... |
124 typedef std::pair<int, base::ProcessHandle> ProcessInfo; | 114 typedef std::pair<int, base::ProcessHandle> ProcessInfo; |
125 | 115 |
126 // Cache OOM scores in memory. | 116 // Cache OOM scores in memory. |
127 typedef base::hash_map<base::ProcessHandle, int> ProcessScoreMap; | 117 typedef base::hash_map<base::ProcessHandle, int> ProcessScoreMap; |
128 | 118 |
129 // Get the list of candidates to kill, sorted by descending importance. | 119 // Get the list of candidates to kill, sorted by descending importance. |
130 static std::vector<Candidate> GetSortedCandidates( | 120 static std::vector<Candidate> GetSortedCandidates( |
131 const TabStatsList& tab_list, | 121 const TabStatsList& tab_list, |
132 const std::vector<arc::ArcProcess>& arc_processes); | 122 const std::vector<arc::ArcProcess>& arc_processes); |
133 | 123 |
134 // Posts AdjustFocusedTabScore task to the file thread. | 124 // Sets OOM score for the focused tab. |
135 void OnFocusTabScoreAdjustmentTimeout(); | 125 void OnFocusTabScoreAdjustmentTimeout(); |
136 | 126 |
137 // Kills a process after getting all info of tabs and apps. | 127 // Kills a process after getting all info of tabs and apps. |
138 void LowMemoryKillImpl(const TabStatsList& tab_list, | 128 void LowMemoryKillImpl(const TabStatsList& tab_list, |
139 const std::vector<arc::ArcProcess>& arc_processes); | 129 const std::vector<arc::ArcProcess>& arc_processes); |
140 | 130 |
141 // Public interface to adjust OOM scores. | 131 // Public interface to adjust OOM scores. |
142 void AdjustOomPriorities(const TabStatsList& tab_list, | 132 void AdjustOomPriorities(const TabStatsList& tab_list, |
143 const std::vector<arc::ArcProcess>& arc_processes); | 133 const std::vector<arc::ArcProcess>& arc_processes); |
144 | 134 |
145 // Sets the score of the focused tab to the least value. | |
146 void AdjustFocusedTabScoreOnFileThread(); | |
147 | |
148 // Sets a newly focused tab the highest priority process if it wasn't. | 135 // Sets a newly focused tab the highest priority process if it wasn't. |
149 void AdjustFocusedTabScore(base::ProcessHandle pid); | 136 void AdjustFocusedTabScore(base::ProcessHandle pid); |
150 | 137 |
151 // Called by AdjustOomPriorities. Runs on the main thread. | 138 // Called by AdjustOomPriorities. Runs on the main thread. |
152 void AdjustOomPrioritiesImpl( | 139 void AdjustOomPrioritiesImpl( |
153 const TabStatsList& tab_list, | 140 const TabStatsList& tab_list, |
154 const std::vector<arc::ArcProcess>& arc_processes); | 141 const std::vector<arc::ArcProcess>& arc_processes); |
155 | 142 |
156 // Sets oom_score_adj of an ARC app. | |
157 void SetOomScoreAdjForApp(int nspid, int score); | |
158 | |
159 // Sets oom_score_adj for a list of tabs on the file thread. | |
160 void SetOomScoreAdjForTabsOnFileThread( | |
161 const std::vector<std::pair<base::ProcessHandle, int>>& entries); | |
162 | |
163 // Sets OOM score for processes in the range [|rbegin|, |rend|) to integers | 143 // Sets OOM score for processes in the range [|rbegin|, |rend|) to integers |
164 // distributed evenly in [|range_begin|, |range_end|). | 144 // distributed evenly in [|range_begin|, |range_end|). |
165 // The new score is set in |new_map|. | 145 // The new score is set in |new_map|. |
166 void DistributeOomScoreInRange( | 146 void DistributeOomScoreInRange( |
167 std::vector<TabManagerDelegate::Candidate>::const_iterator begin, | 147 std::vector<TabManagerDelegate::Candidate>::const_iterator begin, |
168 std::vector<TabManagerDelegate::Candidate>::const_iterator end, | 148 std::vector<TabManagerDelegate::Candidate>::const_iterator end, |
169 int range_begin, | 149 int range_begin, |
170 int range_end, | 150 int range_end, |
171 ProcessScoreMap* new_map); | 151 ProcessScoreMap* new_map); |
172 | 152 |
173 // Initiates an oom priority adjustment. | 153 // Initiates an oom priority adjustment. |
174 void ScheduleEarlyOomPrioritiesAdjustment(); | 154 void ScheduleEarlyOomPrioritiesAdjustment(); |
175 | 155 |
176 // Holds a reference to the owning TabManager. | 156 // Holds a reference to the owning TabManager. |
177 const base::WeakPtr<TabManager> tab_manager_; | 157 const base::WeakPtr<TabManager> tab_manager_; |
178 | 158 |
179 // Registrar to receive renderer notifications. | 159 // Registrar to receive renderer notifications. |
180 content::NotificationRegistrar registrar_; | 160 content::NotificationRegistrar registrar_; |
181 | 161 |
182 // Timer to guarantee that the tab or app is focused for a certain amount of | 162 // Timer to guarantee that the tab or app is focused for a certain amount of |
183 // time. | 163 // time. |
184 base::OneShotTimer focus_process_score_adjust_timer_; | 164 base::OneShotTimer focus_process_score_adjust_timer_; |
185 // Holds the info of the newly focused tab or app. Its OOM score would be | 165 // Holds the info of the newly focused tab or app. Its OOM score would be |
186 // adjusted when |focus_process_score_adjust_timer_| is expired. | 166 // adjusted when |focus_process_score_adjust_timer_| is expired. |
187 std::unique_ptr<FocusedProcess> focused_process_; | 167 std::unique_ptr<FocusedProcess> focused_process_; |
188 | 168 |
189 // This lock is for |oom_score_map_|. | 169 // Map maintaining the process handle - oom_score mapping. |
190 base::Lock oom_score_lock_; | |
191 // Map maintaining the process handle - oom_score mapping. Behind | |
192 // |oom_score_lock_|. | |
193 ProcessScoreMap oom_score_map_; | 170 ProcessScoreMap oom_score_map_; |
194 | 171 |
195 // Util for getting system memory satatus. | 172 // Util for getting system memory satatus. |
196 std::unique_ptr<TabManagerDelegate::MemoryStat> mem_stat_; | 173 std::unique_ptr<TabManagerDelegate::MemoryStat> mem_stat_; |
197 | 174 |
198 // Holds a weak pointer to arc::mojom::ProcessInstance. | 175 // Holds a weak pointer to arc::mojom::ProcessInstance. |
199 arc::mojom::ProcessInstance* arc_process_instance_; | 176 arc::mojom::ProcessInstance* arc_process_instance_; |
200 // Current ProcessInstance version. | 177 // Current ProcessInstance version. |
201 int arc_process_instance_version_; | 178 int arc_process_instance_version_; |
202 | 179 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 // reported if available memory is under the number. | 241 // reported if available memory is under the number. |
265 static int LowMemoryMarginKB(); | 242 static int LowMemoryMarginKB(); |
266 | 243 |
267 // Reads in an integer. | 244 // Reads in an integer. |
268 static int ReadIntFromFile(const char* file_name, int default_val); | 245 static int ReadIntFromFile(const char* file_name, int default_val); |
269 }; | 246 }; |
270 | 247 |
271 } // namespace memory | 248 } // namespace memory |
272 | 249 |
273 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_DELEGATE_CHROMEOS_H_ | 250 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_DELEGATE_CHROMEOS_H_ |
OLD | NEW |