OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_CHROMEOS_MEMORY_OOM_PRIORITY_MANAGER_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_MEMORY_OOM_PRIORITY_MANAGER_H_ | |
7 | |
8 #include <utility> | |
9 #include <vector> | |
10 | |
11 #include "base/compiler_specific.h" | |
12 #include "base/containers/hash_tables.h" | |
13 #include "base/gtest_prod_util.h" | |
14 #include "base/memory/memory_pressure_listener.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/process/process.h" | |
17 #include "base/strings/string16.h" | |
18 #include "base/synchronization/lock.h" | |
19 #include "base/time/time.h" | |
20 #include "base/timer/timer.h" | |
21 #include "content/public/browser/notification_observer.h" | |
22 #include "content/public/browser/notification_registrar.h" | |
23 | |
24 class GURL; | |
25 | |
26 namespace chromeos { | |
27 | |
28 class LowMemoryObserver; | |
29 | |
30 // The OomPriorityManager periodically checks (see | |
31 // ADJUSTMENT_INTERVAL_SECONDS in the source) the status of renderers | |
32 // and adjusts the out of memory (OOM) adjustment value (in | |
33 // /proc/<pid>/oom_score_adj) of the renderers so that they match the | |
34 // algorithm embedded here for priority in being killed upon OOM | |
35 // conditions. | |
36 // | |
37 // The algorithm used favors killing tabs that are not selected, not pinned, | |
38 // and have been idle for longest, in that order of priority. | |
39 class OomPriorityManager : public content::NotificationObserver { | |
40 public: | |
41 OomPriorityManager(); | |
42 ~OomPriorityManager() override; | |
43 | |
44 // Number of discard events since Chrome started. | |
45 int discard_count() const { return discard_count_; } | |
46 | |
47 // See member comment. | |
48 bool recent_tab_discard() const { return recent_tab_discard_; } | |
49 | |
50 void Start(); | |
51 void Stop(); | |
52 | |
53 // Returns list of tab titles sorted from most interesting (don't kill) | |
54 // to least interesting (OK to kill). | |
55 std::vector<base::string16> GetTabTitles(); | |
56 | |
57 // Discards a tab to free the memory occupied by its renderer. | |
58 // Tab still exists in the tab-strip; clicking on it will reload it. | |
59 // Returns true if it successfully found a tab and discarded it. | |
60 bool DiscardTab(); | |
61 | |
62 // Log memory statistics for the running processes, then discards a tab. | |
63 // Tab discard happens sometime later, as collecting the statistics touches | |
64 // multiple threads and takes time. | |
65 void LogMemoryAndDiscardTab(); | |
66 | |
67 // Log memory statistics for the running processes, then call the callback. | |
68 void LogMemory(const std::string& title, const base::Closure& callback); | |
69 | |
70 private: | |
71 FRIEND_TEST_ALL_PREFIXES(OomPriorityManagerTest, Comparator); | |
72 FRIEND_TEST_ALL_PREFIXES(OomPriorityManagerTest, IsReloadableUI); | |
73 FRIEND_TEST_ALL_PREFIXES(OomPriorityManagerTest, GetProcessHandles); | |
74 | |
75 struct TabStats { | |
76 TabStats(); | |
77 ~TabStats(); | |
78 bool is_app; // browser window is an app | |
79 bool is_reloadable_ui; // Reloadable web UI page, like NTP or Settings. | |
80 bool is_playing_audio; | |
81 bool is_pinned; | |
82 bool is_selected; // selected in the currently active browser window | |
83 bool is_discarded; | |
84 base::TimeTicks last_active; | |
85 base::ProcessHandle renderer_handle; | |
86 int child_process_host_id; | |
87 base::string16 title; | |
88 int64 tab_contents_id; // unique ID per WebContents | |
89 }; | |
90 typedef std::vector<TabStats> TabStatsList; | |
91 | |
92 static void PurgeMemoryAndDiscardTabs(); | |
93 | |
94 // Returns true if the |url| represents an internal Chrome web UI page that | |
95 // can be easily reloaded and hence makes a good choice to discard. | |
96 static bool IsReloadableUI(const GURL& url); | |
97 | |
98 // Discards a tab with the given unique ID. Returns true if discard occurred. | |
99 bool DiscardTabById(int64 target_web_contents_id); | |
100 | |
101 // Records UMA histogram statistics for a tab discard. We record statistics | |
102 // for user triggered discards via chrome://discards/ because that allows us | |
103 // to manually test the system. | |
104 void RecordDiscardStatistics(); | |
105 | |
106 // Record whether we ran out of memory during a recent time interval. | |
107 // This allows us to normalize low memory statistics versus usage. | |
108 void RecordRecentTabDiscard(); | |
109 | |
110 // Purges data structures in the browser that can be easily recomputed. | |
111 void PurgeBrowserMemory(); | |
112 | |
113 // Returns the number of tabs open in all browser instances. | |
114 int GetTabCount() const; | |
115 | |
116 TabStatsList GetTabStatsOnUIThread(); | |
117 | |
118 // Called when the timer fires, sets oom_adjust_score for all renderers. | |
119 void AdjustOomPriorities(); | |
120 | |
121 // Pair to hold child process host id and ProcessHandle | |
122 typedef std::pair<int, base::ProcessHandle> ProcessInfo; | |
123 | |
124 // Returns a list of child process host ids and ProcessHandles from | |
125 // |stats_list| with unique pids. If multiple tabs use the same process, | |
126 // returns the first child process host id and corresponding pid. This implies | |
127 // that the processes are selected based on their "most important" tab. | |
128 static std::vector<ProcessInfo> GetChildProcessInfos( | |
129 const TabStatsList& stats_list); | |
130 | |
131 // Called by AdjustOomPriorities. | |
132 void AdjustOomPrioritiesOnFileThread(TabStatsList stats_list); | |
133 | |
134 // Posts AdjustFocusedTabScore task to the file thread. | |
135 void OnFocusTabScoreAdjustmentTimeout(); | |
136 | |
137 // Sets the score of the focused tab to the least value. | |
138 void AdjustFocusedTabScoreOnFileThread(); | |
139 | |
140 static bool CompareTabStats(TabStats first, TabStats second); | |
141 | |
142 void Observe(int type, | |
143 const content::NotificationSource& source, | |
144 const content::NotificationDetails& details) override; | |
145 | |
146 // Called by the memory pressure listener when the memory pressure rises. | |
147 void OnMemoryPressure( | |
148 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); | |
149 | |
150 base::RepeatingTimer<OomPriorityManager> timer_; | |
151 base::OneShotTimer<OomPriorityManager> focus_tab_score_adjust_timer_; | |
152 base::RepeatingTimer<OomPriorityManager> recent_tab_discard_timer_; | |
153 content::NotificationRegistrar registrar_; | |
154 | |
155 // This lock is for |oom_score_map_| and |focused_tab_process_info_|. | |
156 base::Lock oom_score_lock_; | |
157 // Map maintaining the child process host id - oom_score mapping. | |
158 typedef base::hash_map<int, int> ProcessScoreMap; | |
159 ProcessScoreMap oom_score_map_; | |
160 // Holds the focused tab's child process host id. | |
161 ProcessInfo focused_tab_process_info_; | |
162 | |
163 // The old observer for the kernel low memory signal. This is null if | |
164 // the new MemoryPressureListener is used. | |
165 // TODO(skuhne): Remove this when the enhanced memory observer is turned on | |
166 // by default. | |
167 scoped_ptr<LowMemoryObserver> low_memory_observer_; | |
168 | |
169 // A listener to global memory pressure events. This will be used if the | |
170 // memory pressure system was instantiated - otherwise the LowMemoryObserver | |
171 // will be used. | |
172 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_; | |
173 | |
174 // Wall-clock time when the priority manager started running. | |
175 base::TimeTicks start_time_; | |
176 | |
177 // Wall-clock time of last tab discard during this browsing session, or 0 if | |
178 // no discard has happened yet. | |
179 base::TimeTicks last_discard_time_; | |
180 | |
181 // Wall-clock time of last priority adjustment, used to correct the above | |
182 // times for discontinuities caused by suspend/resume. | |
183 base::TimeTicks last_adjust_time_; | |
184 | |
185 // Number of times we have discarded a tab, for statistics. | |
186 int discard_count_; | |
187 | |
188 // Whether a tab discard event has occurred during the last time interval, | |
189 // used for statistics normalized by usage. | |
190 bool recent_tab_discard_; | |
191 | |
192 DISALLOW_COPY_AND_ASSIGN(OomPriorityManager); | |
193 }; | |
194 | |
195 } // namespace chromeos | |
196 | |
197 #endif // CHROME_BROWSER_CHROMEOS_MEMORY_OOM_PRIORITY_MANAGER_H_ | |
OLD | NEW |