Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / memory / oom_priority_manager.h
blob5401e61571fdfbdbd7f04fc51f8a2ea55ca64372
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.
5 #ifndef CHROME_BROWSER_MEMORY_OOM_PRIORITY_MANAGER_H_
6 #define CHROME_BROWSER_MEMORY_OOM_PRIORITY_MANAGER_H_
8 #include <utility>
9 #include <vector>
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/memory_pressure_listener.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/timer/timer.h"
17 #include "chrome/browser/memory/tab_stats.h"
19 class BrowserList;
20 class GURL;
22 namespace memory {
24 #if defined(OS_CHROMEOS)
25 class OomPriorityManagerDelegate;
26 #endif
28 // The OomPriorityManager periodically updates (see
29 // |kAdjustmentIntervalSeconds| in the source) the status of renderers
30 // which are then used by the algorithm embedded here for priority in being
31 // killed upon OOM conditions.
33 // The algorithm used favors killing tabs that are not selected, not pinned,
34 // and have been idle for longest, in that order of priority.
36 // On Chrome OS (via the delegate), the kernel (via /proc/<pid>/oom_score_adj)
37 // will be informed of each renderer's score, which is based on the status, so
38 // in case Chrome is not able to relieve the pressure quickly enough and the
39 // kernel is forced to kill processes, it will be able to do so using the same
40 // algorithm as the one used here.
42 // Note that the browser tests are only active for platforms that use
43 // OomPriorityManager (CrOS only for now) and need to be adjusted accordingly if
44 // support for new platforms is added.
45 class OomPriorityManager {
46 public:
47 OomPriorityManager();
48 ~OomPriorityManager();
50 // Number of discard events since Chrome started.
51 int discard_count() const { return discard_count_; }
53 // See member comment.
54 bool recent_tab_discard() const { return recent_tab_discard_; }
56 void Start();
57 void Stop();
59 // Returns the list of the stats for all renderers. Must be called on the UI
60 // thread.
61 TabStatsList GetTabStats();
63 // Discards a tab to free the memory occupied by its renderer. The tab still
64 // exists in the tab-strip; clicking on it will reload it. Returns true if it
65 // successfully found a tab and discarded it.
66 bool DiscardTab();
68 // Discards a tab with the given unique ID. The tab still exists in the
69 // tab-strip; clicking on it will reload it. Returns true if it successfully
70 // found a tab and discarded it.
71 bool DiscardTabById(int64 target_web_contents_id);
73 // Log memory statistics for the running processes, then discards a tab.
74 // Tab discard happens sometime later, as collecting the statistics touches
75 // multiple threads and takes time.
76 void LogMemoryAndDiscardTab();
78 // Log memory statistics for the running processes, then call the callback.
79 void LogMemory(const std::string& title, const base::Closure& callback);
81 private:
82 FRIEND_TEST_ALL_PREFIXES(OomPriorityManagerTest, Comparator);
83 FRIEND_TEST_ALL_PREFIXES(OomPriorityManagerTest, IsInternalPage);
85 static void PurgeMemoryAndDiscardTab();
87 // Returns true if the |url| represents an internal Chrome web UI page that
88 // can be easily reloaded and hence makes a good choice to discard.
89 static bool IsInternalPage(const GURL& url);
91 // Records UMA histogram statistics for a tab discard. We record statistics
92 // for user triggered discards via chrome://discards/ because that allows us
93 // to manually test the system.
94 void RecordDiscardStatistics();
96 // Record whether we ran out of memory during a recent time interval.
97 // This allows us to normalize low memory statistics versus usage.
98 void RecordRecentTabDiscard();
100 // Purges data structures in the browser that can be easily recomputed.
101 void PurgeBrowserMemory();
103 // Returns the number of tabs open in all browser instances.
104 int GetTabCount() const;
106 // Adds all the stats of the tabs in |browser_list| into |stats_list|. If
107 // |active_desktop| is true, we consider its first window as being active.
108 void AddTabStats(BrowserList* browser_list,
109 bool active_desktop,
110 TabStatsList* stats_list);
112 // Callback for when |update_timer_| fires. Takes care of executing the tasks
113 // that need to be run periodically (see comment in implementation).
114 void UpdateTimerCallback();
116 static bool CompareTabStats(TabStats first, TabStats second);
118 // Called by the memory pressure listener when the memory pressure rises.
119 void OnMemoryPressure(
120 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
122 // Timer to periodically update the stats of the renderers.
123 base::RepeatingTimer<OomPriorityManager> update_timer_;
125 // Timer to periodically report whether a tab has been discarded since the
126 // last time the timer has fired.
127 base::RepeatingTimer<OomPriorityManager> recent_tab_discard_timer_;
129 // A listener to global memory pressure events.
130 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
132 // Wall-clock time when the priority manager started running.
133 base::TimeTicks start_time_;
135 // Wall-clock time of last tab discard during this browsing session, or 0 if
136 // no discard has happened yet.
137 base::TimeTicks last_discard_time_;
139 // Wall-clock time of last priority adjustment, used to correct the above
140 // times for discontinuities caused by suspend/resume.
141 base::TimeTicks last_adjust_time_;
143 // Number of times we have discarded a tab, for statistics.
144 int discard_count_;
146 // Whether a tab discard event has occurred during the last time interval,
147 // used for statistics normalized by usage.
148 bool recent_tab_discard_;
150 #if defined(OS_CHROMEOS)
151 scoped_ptr<OomPriorityManagerDelegate> delegate_;
152 #endif
154 DISALLOW_COPY_AND_ASSIGN(OomPriorityManager);
157 } // namespace memory
159 #endif // CHROME_BROWSER_MEMORY_OOM_PRIORITY_MANAGER_H_