Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: chrome/browser/memory/tab_manager.h

Issue 1641813002: Provide renderers with memory pressure signals. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@contentapi
Patch Set: Cleaned up. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_MEMORY_TAB_MANAGER_H_ 5 #ifndef CHROME_BROWSER_MEMORY_TAB_MANAGER_H_
6 #define CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ 6 #define CHROME_BROWSER_MEMORY_TAB_MANAGER_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <set>
10 #include <utility> 11 #include <utility>
11 #include <vector> 12 #include <vector>
12 13
14 #include "base/callback.h"
13 #include "base/compiler_specific.h" 15 #include "base/compiler_specific.h"
14 #include "base/gtest_prod_util.h" 16 #include "base/gtest_prod_util.h"
15 #include "base/macros.h" 17 #include "base/macros.h"
16 #include "base/memory/memory_pressure_listener.h" 18 #include "base/memory/memory_pressure_listener.h"
17 #include "base/memory/scoped_ptr.h" 19 #include "base/memory/scoped_ptr.h"
20 #include "base/memory/weak_ptr.h"
18 #include "base/strings/string16.h" 21 #include "base/strings/string16.h"
22 #include "base/task_runner.h"
19 #include "base/timer/timer.h" 23 #include "base/timer/timer.h"
20 #include "build/build_config.h" 24 #include "build/build_config.h"
21 #include "chrome/browser/memory/tab_stats.h" 25 #include "chrome/browser/memory/tab_stats.h"
22 #include "chrome/browser/ui/browser_tab_strip_tracker.h" 26 #include "chrome/browser/ui/browser_tab_strip_tracker.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" 27 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
24 28
25 class BrowserList; 29 class BrowserList;
26 class GURL; 30 class GURL;
31 class TabStripModel;
27 32
28 namespace base { 33 namespace base {
29 class TickClock; 34 class TickClock;
30 } 35 }
31 36
32 namespace content { 37 namespace content {
38 class RenderProcessHost;
33 class WebContents; 39 class WebContents;
34 } 40 }
35 41
36 namespace memory { 42 namespace memory {
37 43
38 #if defined(OS_CHROMEOS) 44 #if defined(OS_CHROMEOS)
39 class TabManagerDelegate; 45 class TabManagerDelegate;
40 #endif 46 #endif
41 47
42 // The TabManager periodically updates (see 48 // The TabManager periodically updates (see
(...skipping 28 matching lines...) Expand all
71 bool recent_tab_discard() const { return recent_tab_discard_; } 77 bool recent_tab_discard() const { return recent_tab_discard_; }
72 78
73 // Start/Stop the Tab Manager. 79 // Start/Stop the Tab Manager.
74 void Start(); 80 void Start();
75 void Stop(); 81 void Stop();
76 82
77 // Returns the list of the stats for all renderers. Must be called on the UI 83 // Returns the list of the stats for all renderers. Must be called on the UI
78 // thread. 84 // thread.
79 TabStatsList GetTabStats(); 85 TabStatsList GetTabStats();
80 86
87 // Returns a sorted list of renderers, from most important to least important.
88 std::vector<content::RenderProcessHost*> GetOrderedRenderers();
89
81 // Returns true if |contents| is currently discarded. 90 // Returns true if |contents| is currently discarded.
82 bool IsTabDiscarded(content::WebContents* contents) const; 91 bool IsTabDiscarded(content::WebContents* contents) const;
83 92
84 // Discards a tab to free the memory occupied by its renderer. The tab still 93 // Discards a tab to free the memory occupied by its renderer. The tab still
85 // exists in the tab-strip; clicking on it will reload it. Returns true if it 94 // exists in the tab-strip; clicking on it will reload it. Returns true if it
86 // successfully found a tab and discarded it. 95 // successfully found a tab and discarded it.
87 bool DiscardTab(); 96 bool DiscardTab();
88 97
89 // Discards a tab with the given unique ID. The tab still exists in the 98 // Discards a tab with the given unique ID. The tab still exists in the
90 // tab-strip; clicking on it will reload it. Returns true if it successfully 99 // tab-strip; clicking on it will reload it. Returns true if it successfully
(...skipping 13 matching lines...) Expand all
104 void set_test_tick_clock(base::TickClock* test_tick_clock); 113 void set_test_tick_clock(base::TickClock* test_tick_clock);
105 114
106 private: 115 private:
107 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, Comparator); 116 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, Comparator);
108 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardedTabKeepsLastActiveTime); 117 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardedTabKeepsLastActiveTime);
109 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardWebContentsAt); 118 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardWebContentsAt);
110 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, IsInternalPage); 119 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, IsInternalPage);
111 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectRecentlyUsedTabs); 120 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectRecentlyUsedTabs);
112 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ReloadDiscardedTabContextMenu); 121 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ReloadDiscardedTabContextMenu);
113 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, TabManagerBasics); 122 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, TabManagerBasics);
123 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ChildProcessNotifications);
Georges Khalil 2016/01/28 15:41:02 nit: alphabetical order.
chrisha 2016/01/29 16:17:54 Done.
124
125 // The time that a renderer is given to react to a memory pressure
126 // notification before another renderer is also notified. This prevents all
127 // renderers from receiving and acting upon notifications simultaneously,
128 // which can quickly overload a system. Exposed for unittesting.
129 enum : int { kRendererNotificationDelayInSeconds = 2 };
Georges Khalil 2016/01/28 15:41:02 Add a comment explaining where that value comes fr
chrisha 2016/01/29 16:17:54 Done.
130
131 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
132
133 // A callback that returns the current memory pressure level.
134 using MemoryPressureLevelCallback = base::Callback<MemoryPressureLevel()>;
135
136 // Callback that notifies a |renderer| of the memory pressure at a given
137 // |level|. Provides a testing seam.
138 using RendererNotificationCallback = base::Callback<
139 void(const content::RenderProcessHost* /* renderer */,
140 MemoryPressureLevel /* level */)>;
114 141
115 static void PurgeMemoryAndDiscardTab(); 142 static void PurgeMemoryAndDiscardTab();
116 143
117 // Returns true if the |url| represents an internal Chrome web UI page that 144 // Returns true if the |url| represents an internal Chrome web UI page that
118 // can be easily reloaded and hence makes a good choice to discard. 145 // can be easily reloaded and hence makes a good choice to discard.
119 static bool IsInternalPage(const GURL& url); 146 static bool IsInternalPage(const GURL& url);
120 147
121 // Records UMA histogram statistics for a tab discard. Record statistics for 148 // Records UMA histogram statistics for a tab discard. Record statistics for
122 // user triggered discards via chrome://discards/ because that allows to 149 // user triggered discards via chrome://discards/ because that allows to
123 // manually test the system. 150 // manually test the system.
124 void RecordDiscardStatistics(); 151 void RecordDiscardStatistics();
125 152
126 // Record whether an out of memory occured during a recent time interval. This 153 // Record whether an out of memory occured during a recent time interval. This
127 // allows the normalization of low memory statistics versus usage. 154 // allows the normalization of low memory statistics versus usage.
128 void RecordRecentTabDiscard(); 155 void RecordRecentTabDiscard();
129 156
130 // Purges data structures in the browser that can be easily recomputed. 157 // Purges data structures in the browser that can be easily recomputed.
131 void PurgeBrowserMemory(); 158 void PurgeBrowserMemory();
132 159
133 // Returns the number of tabs open in all browser instances. 160 // Returns the number of tabs open in all browser instances.
134 int GetTabCount() const; 161 int GetTabCount() const;
135 162
136 // Adds all the stats of the tabs in |browser_list| into |stats_list|. If 163 // Adds all the stats of the tabs in |browser_list| into |stats_list|. If
137 // |active_desktop| is true, consider its first window as being active. 164 // |active_desktop| is true, consider its first window as being active.
138 void AddTabStats(BrowserList* browser_list, 165 void AddTabStats(BrowserList* browser_list,
139 bool active_desktop, 166 bool active_desktop,
140 TabStatsList* stats_list); 167 TabStatsList* stats_list);
141 168
169 // Adds all the stats of the tabs in |tab_strip_model| into |stats_list|.
170 // If |active_model| is true, consider its first window as being active.
171 void AddTabStats(const TabStripModel* model,
172 bool is_app,
173 bool active_model,
174 TabStatsList* stats_list);
175
142 // Callback for when |update_timer_| fires. Takes care of executing the tasks 176 // Callback for when |update_timer_| fires. Takes care of executing the tasks
143 // that need to be run periodically (see comment in implementation). 177 // that need to be run periodically (see comment in implementation).
144 void UpdateTimerCallback(); 178 void UpdateTimerCallback();
145 179
146 // Goes through a list of checks to see if a tab is allowed to be discarded by 180 // Goes through a list of checks to see if a tab is allowed to be discarded by
147 // the automatic tab discarding mechanism. Note that this is not used when 181 // the automatic tab discarding mechanism. Note that this is not used when
148 // discarding a particular tab from about:discards. 182 // discarding a particular tab from about:discards.
149 bool CanDiscardTab(int64_t target_web_contents_id) const; 183 bool CanDiscardTab(int64_t target_web_contents_id) const;
150 184
151 // Does the actual discard by destroying the WebContents in |model| at |index| 185 // Does the actual discard by destroying the WebContents in |model| at |index|
(...skipping 24 matching lines...) Expand all
176 WebContentsData* GetWebContentsData(content::WebContents* contents) const; 210 WebContentsData* GetWebContentsData(content::WebContents* contents) const;
177 211
178 // Returns true if |first| is considered less desirable to be killed than 212 // Returns true if |first| is considered less desirable to be killed than
179 // |second|. 213 // |second|.
180 static bool CompareTabStats(TabStats first, TabStats second); 214 static bool CompareTabStats(TabStats first, TabStats second);
181 215
182 // Returns either the system's clock or the test clock. See |test_tick_clock_| 216 // Returns either the system's clock or the test clock. See |test_tick_clock_|
183 // for more details. 217 // for more details.
184 base::TimeTicks NowTicks() const; 218 base::TimeTicks NowTicks() const;
185 219
220 // Dispatches a memory pressure message to a single child process, and
221 // schedules another call to itself as long as memory pressure continues.
222 void DoChildProcessDispatch();
223
186 // Timer to periodically update the stats of the renderers. 224 // Timer to periodically update the stats of the renderers.
187 base::RepeatingTimer update_timer_; 225 base::RepeatingTimer update_timer_;
188 226
189 // Timer to periodically report whether a tab has been discarded since the 227 // Timer to periodically report whether a tab has been discarded since the
190 // last time the timer has fired. 228 // last time the timer has fired.
191 base::RepeatingTimer recent_tab_discard_timer_; 229 base::RepeatingTimer recent_tab_discard_timer_;
192 230
193 // A listener to global memory pressure events. 231 // A listener to global memory pressure events.
194 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_; 232 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
195 233
(...skipping 19 matching lines...) Expand all
215 bool discard_once_; 253 bool discard_once_;
216 254
217 // This allows protecting tabs for a certain amount of time after being 255 // This allows protecting tabs for a certain amount of time after being
218 // backgrounded. 256 // backgrounded.
219 base::TimeDelta minimum_protection_time_; 257 base::TimeDelta minimum_protection_time_;
220 258
221 #if defined(OS_CHROMEOS) 259 #if defined(OS_CHROMEOS)
222 scoped_ptr<TabManagerDelegate> delegate_; 260 scoped_ptr<TabManagerDelegate> delegate_;
223 #endif 261 #endif
224 262
263 // Responsible for automatically registering this class as an observer of all
264 // TabStripModels. Automatically tracks browsers as they come and go.
225 BrowserTabStripTracker browser_tab_strip_tracker_; 265 BrowserTabStripTracker browser_tab_strip_tracker_;
226 266
227 // Pointer to a test clock. If this is set, NowTicks() returns the value of 267 // Pointer to a test clock. If this is set, NowTicks() returns the value of
228 // this test clock. Otherwise it returns the system clock's value. 268 // this test clock. Otherwise it returns the system clock's value.
229 base::TickClock* test_tick_clock_; 269 base::TickClock* test_tick_clock_;
230 270
271 // The task runner used for child process notifications. Defaults to the
272 // thread task runner handle that is used by the memory pressure subsystem,
273 // but may be explicitly set for unittesting.
274 scoped_refptr<base::TaskRunner> task_runner_;
275
276 // Indicates that the system is currently experiencing memory pressure. Used
277 // to determine whether a new round of child-process memory pressure
278 // dispatches is starting, or whether an existing one is continuing.
279 bool under_memory_pressure_;
280
281 // The set of child renderers that have received memory pressure notifications
282 // during the current bout of memory pressure. This is emptied when all
283 // children have been notified (restarting another round of notification) and
284 // when a bout of memory pressure ends.
285 std::set<const content::RenderProcessHost*> notified_renderers_;
286
287 // The callback that returns the current memory pressure level. Defaults to
288 // querying base::MemoryPressureMonitor, but can be overridden for testing.
289 MemoryPressureLevelCallback get_current_pressure_level_;
290
291 // The callback to be invoked to notify renderers. Defaults to calling
292 // content::SendPressureNotification, but can be overridden for testing.
293 RendererNotificationCallback notify_renderer_process_;
294
295 // Injected tab strip models. Allows this to be tested end-to-end without
296 // requiring a full browser environment. If specified these tab strips will be
297 // crawled as the authoritative source of tabs, otherwise the BrowserList and
298 // associated Browser objects are crawled. The first of these is considered to
299 // be the 'active' tab strip model.
300 // TODO(chrisha): Factor out tab-strip model enumeration to a helper class,
301 // and make a delegate that centralizes all testing seams.
302 using TestTabStripModel = std::pair<const TabStripModel*, bool>;
303 std::vector<TestTabStripModel> test_tab_strip_models_;
304
305 // Weak pointer factory used for posting delayed tasks to task_runner_.
306 base::WeakPtrFactory<TabManager> weak_ptr_factory_;
307
231 DISALLOW_COPY_AND_ASSIGN(TabManager); 308 DISALLOW_COPY_AND_ASSIGN(TabManager);
232 }; 309 };
233 310
234 } // namespace memory 311 } // namespace memory
235 312
236 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ 313 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/memory/tab_manager.cc » ('j') | chrome/browser/memory/tab_manager.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698