| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_OOM_PRIORITY_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_OOM_PRIORITY_MANAGER_H_ |
| 6 #define CHROME_BROWSER_OOM_PRIORITY_MANAGER_H_ | 6 #define CHROME_BROWSER_OOM_PRIORITY_MANAGER_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
| 11 #include "base/process.h" | 11 #include "base/process.h" |
| 12 #include "base/string16.h" | 12 #include "base/string16.h" |
| 13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 14 #include "base/task.h" | 14 #include "base/task.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "base/timer.h" | 16 #include "base/timer.h" |
| 17 #include "content/common/notification_observer.h" | 17 #include "content/common/notification_observer.h" |
| 18 #include "content/common/notification_registrar.h" | 18 #include "content/common/notification_registrar.h" |
| 19 | 19 |
| 20 class TabContents; |
| 21 |
| 20 namespace browser { | 22 namespace browser { |
| 21 | 23 |
| 22 // The OomPriorityManager periodically checks (see | 24 // The OomPriorityManager periodically checks (see |
| 23 // ADJUSTMENT_INTERVAL_SECONDS in the source) the status of renderers | 25 // ADJUSTMENT_INTERVAL_SECONDS in the source) the status of renderers |
| 24 // and adjusts the out of memory (OOM) adjustment value (in | 26 // and adjusts the out of memory (OOM) adjustment value (in |
| 25 // /proc/<pid>/oom_score_adj) of the renderers so that they match the | 27 // /proc/<pid>/oom_score_adj) of the renderers so that they match the |
| 26 // algorithm embedded here for priority in being killed upon OOM | 28 // algorithm embedded here for priority in being killed upon OOM |
| 27 // conditions. | 29 // conditions. |
| 28 // | 30 // |
| 29 // The algorithm used favors killing tabs that are not pinned, have | 31 // The algorithm used favors killing tabs that are not selected, not pinned, |
| 30 // been idle for longest, and take up the most memory, in that order | 32 // and have been idle for longest, in that order of priority. |
| 31 // of priority. We round the idle times to the nearest few minutes | |
| 32 // (see BUCKET_INTERVAL_MINUTES in the source) so that we can bucket | |
| 33 // them, as no two tabs will have exactly the same idle time. | |
| 34 class OomPriorityManager : public NotificationObserver { | 33 class OomPriorityManager : public NotificationObserver { |
| 35 public: | 34 public: |
| 36 OomPriorityManager(); | 35 OomPriorityManager(); |
| 37 virtual ~OomPriorityManager(); | 36 virtual ~OomPriorityManager(); |
| 38 | 37 |
| 39 void Start(); | 38 void Start(); |
| 40 void Stop(); | 39 void Stop(); |
| 41 | 40 |
| 42 // Returns list of tab titles sorted from most interesting (don't kill) | 41 // Returns list of tab titles sorted from most interesting (don't kill) |
| 43 // to least interesting (OK to kill). | 42 // to least interesting (OK to kill). |
| 44 std::vector<string16> GetTabTitles(); | 43 std::vector<string16> GetTabTitles(); |
| 45 | 44 |
| 45 // Discards a tab to free the memory occupied by its renderer. |
| 46 // Tab still exists in the tab-strip; clicking on it will reload it. |
| 47 void DiscardTab(); |
| 48 |
| 46 private: | 49 private: |
| 47 struct RendererStats { | 50 struct TabStats { |
| 48 RendererStats(); | 51 TabStats(); |
| 49 ~RendererStats(); | 52 ~TabStats(); |
| 50 bool is_pinned; | 53 bool is_pinned; |
| 51 bool is_selected; | 54 bool is_selected; |
| 52 base::TimeTicks last_selected; | 55 base::TimeTicks last_selected; |
| 53 size_t memory_used; | |
| 54 base::ProcessHandle renderer_handle; | 56 base::ProcessHandle renderer_handle; |
| 55 string16 title; | 57 string16 title; |
| 58 int64 tab_contents_id; // unique ID per TabContents |
| 56 }; | 59 }; |
| 57 typedef std::vector<RendererStats> StatsList; | 60 typedef std::vector<TabStats> TabStatsList; |
| 58 typedef base::hash_map<base::ProcessHandle, int> ProcessScoreMap; | |
| 59 | 61 |
| 60 // Posts DoAdjustOomPriorities task to the file thread. Called when | 62 TabStatsList GetTabStatsOnUIThread(); |
| 61 // the timer fires. | 63 |
| 64 // Called when the timer fires, sets oom_adjust_score for all renderers. |
| 62 void AdjustOomPriorities(); | 65 void AdjustOomPriorities(); |
| 63 | 66 |
| 67 // Called by AdjustOomPriorities. |
| 68 void AdjustOomPrioritiesOnFileThread(TabStatsList stats_list); |
| 69 |
| 64 // Posts AdjustFocusedTabScore task to the file thread. | 70 // Posts AdjustFocusedTabScore task to the file thread. |
| 65 void OnFocusTabScoreAdjustmentTimeout(); | 71 void OnFocusTabScoreAdjustmentTimeout(); |
| 66 | 72 |
| 67 // Called by AdjustOomPriorities. Runs on the file thread. | 73 // Sets the score of the focused tab to the least value. |
| 68 void DoAdjustOomPriorities(); | 74 void AdjustFocusedTabScoreOnFileThread(); |
| 69 | 75 |
| 70 // Called when a tab comes into focus. Runs on the file thread. | 76 static bool CompareTabStats(TabStats first, TabStats second); |
| 71 // Sets the score of only the currently focused tab to the least value. | |
| 72 void AdjustFocusedTabScore(); | |
| 73 | |
| 74 static bool CompareRendererStats(RendererStats first, RendererStats second); | |
| 75 | 77 |
| 76 virtual void Observe(int type, | 78 virtual void Observe(int type, |
| 77 const NotificationSource& source, | 79 const NotificationSource& source, |
| 78 const NotificationDetails& details); | 80 const NotificationDetails& details); |
| 79 | 81 |
| 80 base::RepeatingTimer<OomPriorityManager> timer_; | 82 base::RepeatingTimer<OomPriorityManager> timer_; |
| 81 base::OneShotTimer<OomPriorityManager> focus_tab_score_adjust_timer_; | 83 base::OneShotTimer<OomPriorityManager> focus_tab_score_adjust_timer_; |
| 82 // renderer_stats_ is used on both UI and file threads. | 84 NotificationRegistrar registrar_; |
| 83 base::Lock renderer_stats_lock_; | 85 |
| 84 StatsList renderer_stats_; | |
| 85 // This lock is for pid_to_oom_score_ and focus_tab_pid_. | 86 // This lock is for pid_to_oom_score_ and focus_tab_pid_. |
| 86 base::Lock pid_to_oom_score_lock_; | 87 base::Lock pid_to_oom_score_lock_; |
| 87 // map maintaining the process - oom_score mapping. | 88 // map maintaining the process - oom_score mapping. |
| 89 typedef base::hash_map<base::ProcessHandle, int> ProcessScoreMap; |
| 88 ProcessScoreMap pid_to_oom_score_; | 90 ProcessScoreMap pid_to_oom_score_; |
| 89 NotificationRegistrar registrar_; | |
| 90 base::ProcessHandle focused_tab_pid_; | 91 base::ProcessHandle focused_tab_pid_; |
| 91 | 92 |
| 92 DISALLOW_COPY_AND_ASSIGN(OomPriorityManager); | 93 DISALLOW_COPY_AND_ASSIGN(OomPriorityManager); |
| 93 }; | 94 }; |
| 94 | 95 |
| 95 } // namespace browser | 96 } // namespace browser |
| 96 | 97 |
| 97 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser::OomPriorityManager); | 98 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser::OomPriorityManager); |
| 98 | 99 |
| 99 #endif // CHROME_BROWSER_OOM_PRIORITY_MANAGER_H_ | 100 #endif // CHROME_BROWSER_OOM_PRIORITY_MANAGER_H_ |
| OLD | NEW |