OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_CHROMEOS_MEMORY_OOM_PRIORITY_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_MEMORY_OOM_PRIORITY_MANAGER_H_ |
6 #define CHROME_BROWSER_CHROMEOS_MEMORY_OOM_PRIORITY_MANAGER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_MEMORY_OOM_PRIORITY_MANAGER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/hash_tables.h" | 12 #include "base/hash_tables.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/process.h" | 14 #include "base/process.h" |
15 #include "base/string16.h" | 15 #include "base/string16.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 #include "base/time.h" | 17 #include "base/time.h" |
18 #include "base/timer.h" | 18 #include "base/timer.h" |
| 19 #include "chromeos/memory/low_memory_listener_delegate.h" |
19 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
20 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
21 | 22 |
22 class GURL; | 23 class GURL; |
23 | 24 |
24 namespace chromeos { | 25 namespace chromeos { |
25 | 26 |
26 class LowMemoryObserver; | 27 class LowMemoryListener; |
27 | 28 |
28 // The OomPriorityManager periodically checks (see | 29 // The OomPriorityManager periodically checks (see |
29 // ADJUSTMENT_INTERVAL_SECONDS in the source) the status of renderers | 30 // ADJUSTMENT_INTERVAL_SECONDS in the source) the status of renderers |
30 // and adjusts the out of memory (OOM) adjustment value (in | 31 // and adjusts the out of memory (OOM) adjustment value (in |
31 // /proc/<pid>/oom_score_adj) of the renderers so that they match the | 32 // /proc/<pid>/oom_score_adj) of the renderers so that they match the |
32 // algorithm embedded here for priority in being killed upon OOM | 33 // algorithm embedded here for priority in being killed upon OOM |
33 // conditions. | 34 // conditions. |
34 // | 35 // |
35 // The algorithm used favors killing tabs that are not selected, not pinned, | 36 // The algorithm used favors killing tabs that are not selected, not pinned, |
36 // and have been idle for longest, in that order of priority. | 37 // and have been idle for longest, in that order of priority. |
37 class OomPriorityManager : public content::NotificationObserver { | 38 class OomPriorityManager : public content::NotificationObserver, |
| 39 public LowMemoryListenerDelegate { |
38 public: | 40 public: |
39 OomPriorityManager(); | 41 OomPriorityManager(); |
40 virtual ~OomPriorityManager(); | 42 virtual ~OomPriorityManager(); |
41 | 43 |
42 // Number of discard events since Chrome started. | 44 // Number of discard events since Chrome started. |
43 int discard_count() const { return discard_count_; } | 45 int discard_count() const { return discard_count_; } |
44 | 46 |
45 // See member comment. | 47 // See member comment. |
46 bool recent_tab_discard() const { return recent_tab_discard_; } | 48 bool recent_tab_discard() const { return recent_tab_discard_; } |
47 | 49 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 void AdjustOomPrioritiesOnFileThread(TabStatsList stats_list); | 116 void AdjustOomPrioritiesOnFileThread(TabStatsList stats_list); |
115 | 117 |
116 // Posts AdjustFocusedTabScore task to the file thread. | 118 // Posts AdjustFocusedTabScore task to the file thread. |
117 void OnFocusTabScoreAdjustmentTimeout(); | 119 void OnFocusTabScoreAdjustmentTimeout(); |
118 | 120 |
119 // Sets the score of the focused tab to the least value. | 121 // Sets the score of the focused tab to the least value. |
120 void AdjustFocusedTabScoreOnFileThread(); | 122 void AdjustFocusedTabScoreOnFileThread(); |
121 | 123 |
122 static bool CompareTabStats(TabStats first, TabStats second); | 124 static bool CompareTabStats(TabStats first, TabStats second); |
123 | 125 |
| 126 // NotificationObserver overrides: |
124 virtual void Observe(int type, | 127 virtual void Observe(int type, |
125 const content::NotificationSource& source, | 128 const content::NotificationSource& source, |
126 const content::NotificationDetails& details) OVERRIDE; | 129 const content::NotificationDetails& details) OVERRIDE; |
127 | 130 |
| 131 // LowMemoryListenerDelegate overrides: |
| 132 virtual void OnMemoryLow() OVERRIDE; |
| 133 |
128 base::RepeatingTimer<OomPriorityManager> timer_; | 134 base::RepeatingTimer<OomPriorityManager> timer_; |
129 base::OneShotTimer<OomPriorityManager> focus_tab_score_adjust_timer_; | 135 base::OneShotTimer<OomPriorityManager> focus_tab_score_adjust_timer_; |
130 base::RepeatingTimer<OomPriorityManager> recent_tab_discard_timer_; | 136 base::RepeatingTimer<OomPriorityManager> recent_tab_discard_timer_; |
131 content::NotificationRegistrar registrar_; | 137 content::NotificationRegistrar registrar_; |
132 | 138 |
133 // This lock is for pid_to_oom_score_ and focus_tab_pid_. | 139 // This lock is for pid_to_oom_score_ and focus_tab_pid_. |
134 base::Lock pid_to_oom_score_lock_; | 140 base::Lock pid_to_oom_score_lock_; |
135 // map maintaining the process - oom_score mapping. | 141 // map maintaining the process - oom_score mapping. |
136 typedef base::hash_map<base::ProcessHandle, int> ProcessScoreMap; | 142 typedef base::hash_map<base::ProcessHandle, int> ProcessScoreMap; |
137 ProcessScoreMap pid_to_oom_score_; | 143 ProcessScoreMap pid_to_oom_score_; |
138 base::ProcessHandle focused_tab_pid_; | 144 base::ProcessHandle focused_tab_pid_; |
139 | 145 |
140 // Observer for the kernel low memory signal. NULL if tab discarding is | 146 // Observer for the kernel low memory signal. NULL if tab discarding is |
141 // disabled. | 147 // disabled. |
142 scoped_ptr<LowMemoryObserver> low_memory_observer_; | 148 scoped_ptr<LowMemoryListener> low_memory_listener_; |
143 | 149 |
144 // Wall-clock time when the priority manager started running. | 150 // Wall-clock time when the priority manager started running. |
145 base::TimeTicks start_time_; | 151 base::TimeTicks start_time_; |
146 | 152 |
147 // Wall-clock time of last tab discard during this browsing session, or 0 if | 153 // Wall-clock time of last tab discard during this browsing session, or 0 if |
148 // no discard has happened yet. | 154 // no discard has happened yet. |
149 base::TimeTicks last_discard_time_; | 155 base::TimeTicks last_discard_time_; |
150 | 156 |
151 // Wall-clock time of last priority adjustment, used to correct the above | 157 // Wall-clock time of last priority adjustment, used to correct the above |
152 // times for discontinuities caused by suspend/resume. | 158 // times for discontinuities caused by suspend/resume. |
153 base::TimeTicks last_adjust_time_; | 159 base::TimeTicks last_adjust_time_; |
154 | 160 |
155 // Number of times we have discarded a tab, for statistics. | 161 // Number of times we have discarded a tab, for statistics. |
156 int discard_count_; | 162 int discard_count_; |
157 | 163 |
158 // Whether a tab discard event has occurred during the last time interval, | 164 // Whether a tab discard event has occurred during the last time interval, |
159 // used for statistics normalized by usage. | 165 // used for statistics normalized by usage. |
160 bool recent_tab_discard_; | 166 bool recent_tab_discard_; |
161 | 167 |
162 DISALLOW_COPY_AND_ASSIGN(OomPriorityManager); | 168 DISALLOW_COPY_AND_ASSIGN(OomPriorityManager); |
163 }; | 169 }; |
164 | 170 |
165 } // namespace chrome | 171 } // namespace chrome |
166 | 172 |
167 #endif // CHROME_BROWSER_CHROMEOS_MEMORY_OOM_PRIORITY_MANAGER_H_ | 173 #endif // CHROME_BROWSER_CHROMEOS_MEMORY_OOM_PRIORITY_MANAGER_H_ |
OLD | NEW |