| 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> |
| 11 | 11 |
| 12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/process/process.h" | 17 #include "base/process/process.h" |
| 18 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
| 19 #include "chrome/browser/chromeos/arc/arc_process.h" | 19 #include "chrome/browser/chromeos/arc/arc_process.h" |
| 20 #include "chrome/browser/memory/tab_manager.h" | 20 #include "chrome/browser/memory/tab_manager.h" |
| 21 #include "chrome/browser/memory/tab_stats.h" | 21 #include "chrome/browser/memory/tab_stats.h" |
| 22 #include "chrome/browser/ui/browser_list_observer.h" | 22 #include "chrome/browser/ui/browser_list_observer.h" |
| 23 #include "chromeos/dbus/debug_daemon_client.h" |
| 23 #include "components/arc/arc_bridge_service.h" | 24 #include "components/arc/arc_bridge_service.h" |
| 24 #include "components/arc/common/process.mojom.h" | 25 #include "components/arc/common/process.mojom.h" |
| 25 #include "components/arc/instance_holder.h" | 26 #include "components/arc/instance_holder.h" |
| 26 #include "content/public/browser/notification_observer.h" | 27 #include "content/public/browser/notification_observer.h" |
| 27 #include "content/public/browser/notification_registrar.h" | 28 #include "content/public/browser/notification_registrar.h" |
| 28 #include "ui/wm/public/activation_change_observer.h" | 29 #include "ui/wm/public/activation_change_observer.h" |
| 29 | 30 |
| 30 namespace memory { | 31 namespace memory { |
| 31 | 32 |
| 32 // Possible types of Apps/Tabs processes. From most important to least | 33 // Possible types of Apps/Tabs processes. From most important to least |
| 33 // important. | 34 // important. |
| 34 enum class ProcessType { | 35 enum class ProcessType { |
| 35 // There can be only one process having process type "FOCUSED_APP" | 36 // There can be only one process having process type "FOCUSED_APP" |
| 36 // or "FOCUSED_TAB" in the system at any give time (i.e., The focused window | 37 // or "FOCUSED_TAB" in the system at any give time (i.e., The focused window |
| 37 // could be either a chrome window or an Android app. But not both. | 38 // could be either a chrome window or an Android app. But not both. |
| 38 FOCUSED_APP = 1, | 39 FOCUSED_APP = 1, |
| 39 FOCUSED_TAB = FOCUSED_APP, | 40 FOCUSED_TAB = FOCUSED_APP, |
| 40 | 41 |
| 41 VISIBLE_APP = 2, | 42 VISIBLE_APP = 2, |
| 42 BACKGROUND_TAB = 3, | 43 BACKGROUND_TAB = 3, |
| 43 BACKGROUND_APP = 4, | 44 BACKGROUND_APP = 4, |
| 44 UNKNOWN_TYPE = 5, | 45 UNKNOWN_TYPE = 5, |
| 45 }; | 46 }; |
| 46 | 47 |
| 47 // The Chrome OS TabManagerDelegate is responsible for keeping the | 48 // The Chrome OS TabManagerDelegate is responsible for keeping the |
| 48 // renderers' scores up to date in /proc/<pid>/oom_score_adj. | 49 // 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 | 50 class TabManagerDelegate |
| 54 : public arc::InstanceHolder<arc::mojom::ProcessInstance>::Observer, | 51 : public arc::InstanceHolder<arc::mojom::ProcessInstance>::Observer, |
| 55 public aura::client::ActivationChangeObserver, | 52 public aura::client::ActivationChangeObserver, |
| 56 public content::NotificationObserver, | 53 public content::NotificationObserver, |
| 57 public chrome::BrowserListObserver { | 54 public chrome::BrowserListObserver { |
| 58 public: | 55 public: |
| 59 class MemoryStat; | 56 class MemoryStat; |
| 60 | 57 |
| 61 explicit TabManagerDelegate(const base::WeakPtr<TabManager>& tab_manager); | 58 explicit TabManagerDelegate(const base::WeakPtr<TabManager>& tab_manager); |
| 62 | 59 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 82 | 79 |
| 83 // Returns oom_score_adj of a process if the score is cached by |this|. | 80 // 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 | 81 // If couldn't find the score in the cache, returns -1001 since the valid |
| 85 // range of oom_score_adj is [-1000, 1000]. | 82 // range of oom_score_adj is [-1000, 1000]. |
| 86 int GetCachedOomScore(base::ProcessHandle process_handle); | 83 int GetCachedOomScore(base::ProcessHandle process_handle); |
| 87 | 84 |
| 88 // Called when the timer fires, sets oom_adjust_score for all renderers. | 85 // Called when the timer fires, sets oom_adjust_score for all renderers. |
| 89 void AdjustOomPriorities(const TabStatsList& tab_list); | 86 void AdjustOomPriorities(const TabStatsList& tab_list); |
| 90 | 87 |
| 91 protected: | 88 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 | 89 // Kills an Arc process. Returns true if the kill request is successfully sent |
| 99 // to Android. Virtual for unit testing. | 90 // to Android. Virtual for unit testing. |
| 100 virtual bool KillArcProcess(const int nspid); | 91 virtual bool KillArcProcess(const int nspid); |
| 101 | 92 |
| 102 // Kills a tab. Returns true if the tab is killed successfully. | 93 // Kills a tab. Returns true if the tab is killed successfully. |
| 103 // Virtual for unit testing. | 94 // Virtual for unit testing. |
| 104 virtual bool KillTab(int64_t tab_id); | 95 virtual bool KillTab(int64_t tab_id); |
| 105 | 96 |
| 97 // Get debugd client instance. |
| 98 virtual chromeos::DebugDaemonClient* GetDebugDaemonClient(); |
| 99 |
| 106 private: | 100 private: |
| 107 FRIEND_TEST_ALL_PREFIXES(TabManagerDelegateTest, CandidatesSorted); | 101 FRIEND_TEST_ALL_PREFIXES(TabManagerDelegateTest, CandidatesSorted); |
| 108 FRIEND_TEST_ALL_PREFIXES(TabManagerDelegateTest, KillMultipleProcesses); | 102 FRIEND_TEST_ALL_PREFIXES(TabManagerDelegateTest, KillMultipleProcesses); |
| 109 FRIEND_TEST_ALL_PREFIXES(TabManagerDelegateTest, SetOomScoreAdj); | 103 FRIEND_TEST_ALL_PREFIXES(TabManagerDelegateTest, SetOomScoreAdj); |
| 110 | 104 |
| 111 class Candidate; | 105 class Candidate; |
| 112 class FocusedProcess; | 106 class FocusedProcess; |
| 113 class UmaReporter; | 107 class UmaReporter; |
| 114 | 108 |
| 115 friend std::ostream& operator<<(std::ostream& out, | 109 friend std::ostream& operator<<(std::ostream& out, |
| 116 const Candidate& candidate); | 110 const Candidate& candidate); |
| 117 | 111 |
| 118 // content::NotificationObserver: | 112 // content::NotificationObserver: |
| 119 void Observe(int type, | 113 void Observe(int type, |
| 120 const content::NotificationSource& source, | 114 const content::NotificationSource& source, |
| 121 const content::NotificationDetails& details) override; | 115 const content::NotificationDetails& details) override; |
| 122 | 116 |
| 123 // Pair to hold child process host id and ProcessHandle. | 117 // Pair to hold child process host id and ProcessHandle. |
| 124 typedef std::pair<int, base::ProcessHandle> ProcessInfo; | 118 typedef std::pair<int, base::ProcessHandle> ProcessInfo; |
| 125 | 119 |
| 126 // Cache OOM scores in memory. | 120 // Cache OOM scores in memory. |
| 127 typedef base::hash_map<base::ProcessHandle, int> ProcessScoreMap; | 121 typedef base::hash_map<base::ProcessHandle, int> ProcessScoreMap; |
| 128 | 122 |
| 129 // Get the list of candidates to kill, sorted by descending importance. | 123 // Get the list of candidates to kill, sorted by descending importance. |
| 130 static std::vector<Candidate> GetSortedCandidates( | 124 static std::vector<Candidate> GetSortedCandidates( |
| 131 const TabStatsList& tab_list, | 125 const TabStatsList& tab_list, |
| 132 const std::vector<arc::ArcProcess>& arc_processes); | 126 const std::vector<arc::ArcProcess>& arc_processes); |
| 133 | 127 |
| 134 // Posts AdjustFocusedTabScore task to the file thread. | 128 // Sets OOM score for the focused tab. |
| 135 void OnFocusTabScoreAdjustmentTimeout(); | 129 void OnFocusTabScoreAdjustmentTimeout(); |
| 136 | 130 |
| 137 // Kills a process after getting all info of tabs and apps. | 131 // Kills a process after getting all info of tabs and apps. |
| 138 void LowMemoryKillImpl(const TabStatsList& tab_list, | 132 void LowMemoryKillImpl(const TabStatsList& tab_list, |
| 139 const std::vector<arc::ArcProcess>& arc_processes); | 133 const std::vector<arc::ArcProcess>& arc_processes); |
| 140 | 134 |
| 141 // Public interface to adjust OOM scores. | 135 // Public interface to adjust OOM scores. |
| 142 void AdjustOomPriorities(const TabStatsList& tab_list, | 136 void AdjustOomPriorities(const TabStatsList& tab_list, |
| 143 const std::vector<arc::ArcProcess>& arc_processes); | 137 const std::vector<arc::ArcProcess>& arc_processes); |
| 144 | 138 |
| 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. | 139 // Sets a newly focused tab the highest priority process if it wasn't. |
| 149 void AdjustFocusedTabScore(base::ProcessHandle pid); | 140 void AdjustFocusedTabScore(base::ProcessHandle pid); |
| 150 | 141 |
| 151 // Called by AdjustOomPriorities. Runs on the main thread. | 142 // Called by AdjustOomPriorities. Runs on the main thread. |
| 152 void AdjustOomPrioritiesImpl( | 143 void AdjustOomPrioritiesImpl( |
| 153 const TabStatsList& tab_list, | 144 const TabStatsList& tab_list, |
| 154 const std::vector<arc::ArcProcess>& arc_processes); | 145 const std::vector<arc::ArcProcess>& arc_processes); |
| 155 | 146 |
| 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 | 147 // Sets OOM score for processes in the range [|rbegin|, |rend|) to integers |
| 164 // distributed evenly in [|range_begin|, |range_end|). | 148 // distributed evenly in [|range_begin|, |range_end|). |
| 165 // The new score is set in |new_map|. | 149 // The new score is set in |new_map|. |
| 166 void DistributeOomScoreInRange( | 150 void DistributeOomScoreInRange( |
| 167 std::vector<TabManagerDelegate::Candidate>::const_iterator begin, | 151 std::vector<TabManagerDelegate::Candidate>::const_iterator begin, |
| 168 std::vector<TabManagerDelegate::Candidate>::const_iterator end, | 152 std::vector<TabManagerDelegate::Candidate>::const_iterator end, |
| 169 int range_begin, | 153 int range_begin, |
| 170 int range_end, | 154 int range_end, |
| 171 ProcessScoreMap* new_map); | 155 ProcessScoreMap* new_map); |
| 172 | 156 |
| 173 // Initiates an oom priority adjustment. | 157 // Initiates an oom priority adjustment. |
| 174 void ScheduleEarlyOomPrioritiesAdjustment(); | 158 void ScheduleEarlyOomPrioritiesAdjustment(); |
| 175 | 159 |
| 176 // Holds a reference to the owning TabManager. | 160 // Holds a reference to the owning TabManager. |
| 177 const base::WeakPtr<TabManager> tab_manager_; | 161 const base::WeakPtr<TabManager> tab_manager_; |
| 178 | 162 |
| 179 // Registrar to receive renderer notifications. | 163 // Registrar to receive renderer notifications. |
| 180 content::NotificationRegistrar registrar_; | 164 content::NotificationRegistrar registrar_; |
| 181 | 165 |
| 182 // Timer to guarantee that the tab or app is focused for a certain amount of | 166 // Timer to guarantee that the tab or app is focused for a certain amount of |
| 183 // time. | 167 // time. |
| 184 base::OneShotTimer focus_process_score_adjust_timer_; | 168 base::OneShotTimer focus_process_score_adjust_timer_; |
| 185 // Holds the info of the newly focused tab or app. Its OOM score would be | 169 // 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. | 170 // adjusted when |focus_process_score_adjust_timer_| is expired. |
| 187 std::unique_ptr<FocusedProcess> focused_process_; | 171 std::unique_ptr<FocusedProcess> focused_process_; |
| 188 | 172 |
| 189 // This lock is for |oom_score_map_|. | 173 // 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_; | 174 ProcessScoreMap oom_score_map_; |
| 194 | 175 |
| 195 // Util for getting system memory satatus. | 176 // Util for getting system memory status. |
| 196 std::unique_ptr<TabManagerDelegate::MemoryStat> mem_stat_; | 177 std::unique_ptr<TabManagerDelegate::MemoryStat> mem_stat_; |
| 197 | 178 |
| 198 // Holds a weak pointer to arc::mojom::ProcessInstance. | 179 // Holds a weak pointer to arc::mojom::ProcessInstance. |
| 199 arc::mojom::ProcessInstance* arc_process_instance_; | 180 arc::mojom::ProcessInstance* arc_process_instance_; |
| 200 // Current ProcessInstance version. | 181 // Current ProcessInstance version. |
| 201 int arc_process_instance_version_; | 182 int arc_process_instance_version_; |
| 202 | 183 |
| 203 // Reports UMA histograms. | 184 // Reports UMA histograms. |
| 204 std::unique_ptr<UmaReporter> uma_; | 185 std::unique_ptr<UmaReporter> uma_; |
| 205 | 186 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 // reported if available memory is under the number. | 245 // reported if available memory is under the number. |
| 265 static int LowMemoryMarginKB(); | 246 static int LowMemoryMarginKB(); |
| 266 | 247 |
| 267 // Reads in an integer. | 248 // Reads in an integer. |
| 268 static int ReadIntFromFile(const char* file_name, int default_val); | 249 static int ReadIntFromFile(const char* file_name, int default_val); |
| 269 }; | 250 }; |
| 270 | 251 |
| 271 } // namespace memory | 252 } // namespace memory |
| 272 | 253 |
| 273 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_DELEGATE_CHROMEOS_H_ | 254 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_DELEGATE_CHROMEOS_H_ |
| OLD | NEW |