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 #include "chrome/browser/memory/oom_priority_manager.h"
11 #include "ash/multi_profile_uma.h"
12 #include "ash/session/session_state_delegate.h"
13 #include "ash/shell.h"
14 #include "base/bind.h"
15 #include "base/bind_helpers.h"
16 #include "base/command_line.h"
17 #include "base/memory/memory_pressure_monitor.h"
18 #include "base/metrics/field_trial.h"
19 #include "base/metrics/histogram.h"
20 #include "base/process/process.h"
21 #include "base/strings/string16.h"
22 #include "base/strings/string_number_conversions.h"
23 #include "base/strings/string_util.h"
24 #include "base/strings/utf_string_conversions.h"
25 #include "base/threading/thread.h"
26 #include "build/build_config.h"
27 #include "chrome/browser/browser_process.h"
28 #include "chrome/browser/memory/oom_memory_details.h"
29 #include "chrome/browser/memory/system_memory_stats_recorder.h"
30 #include "chrome/browser/ui/browser.h"
31 #include "chrome/browser/ui/browser_iterator.h"
32 #include "chrome/browser/ui/browser_list.h"
33 #include "chrome/browser/ui/host_desktop.h"
34 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
35 #include "chrome/browser/ui/tabs/tab_strip_model.h"
36 #include "chrome/browser/ui/tabs/tab_utils.h"
37 #include "chrome/common/chrome_constants.h"
38 #include "chrome/common/url_constants.h"
39 #include "content/public/browser/browser_thread.h"
40 #include "content/public/browser/render_process_host.h"
41 #include "content/public/browser/web_contents.h"
43 #if defined(OS_CHROMEOS)
44 #include "chrome/browser/memory/oom_priority_manager_delegate_chromeos.h"
47 using base::TimeDelta
;
48 using base::TimeTicks
;
49 using content::BrowserThread
;
50 using content::WebContents
;
55 // The default interval in seconds after which to adjust the oom_score_adj
57 const int kAdjustmentIntervalSeconds
= 10;
59 // For each period of this length we record a statistic to indicate whether
60 // or not the user experienced a low memory event. If you change this interval
61 // you must replace Tabs.Discard.DiscardInLastMinute with a new statistic.
62 const int kRecentTabDiscardIntervalSeconds
= 60;
64 // If there has been no priority adjustment in this interval, we assume the
65 // machine was suspended and correct our timing statistics.
66 const int kSuspendThresholdSeconds
= kAdjustmentIntervalSeconds
* 4;
68 // Returns a unique ID for a WebContents. Do not cast back to a pointer, as
69 // the WebContents could be deleted if the user closed the tab.
70 int64
IdFromWebContents(WebContents
* web_contents
) {
71 return reinterpret_cast<int64
>(web_contents
);
76 ////////////////////////////////////////////////////////////////////////////////
79 OomPriorityManager::OomPriorityManager()
80 : discard_count_(0), recent_tab_discard_(false) {
81 #if defined(OS_CHROMEOS)
82 delegate_
.reset(new OomPriorityManagerDelegate
);
86 OomPriorityManager::~OomPriorityManager() {
90 void OomPriorityManager::Start() {
91 if (!update_timer_
.IsRunning()) {
92 update_timer_
.Start(FROM_HERE
,
93 TimeDelta::FromSeconds(kAdjustmentIntervalSeconds
),
94 this, &OomPriorityManager::UpdateTimerCallback
);
96 if (!recent_tab_discard_timer_
.IsRunning()) {
97 recent_tab_discard_timer_
.Start(
98 FROM_HERE
, TimeDelta::FromSeconds(kRecentTabDiscardIntervalSeconds
),
99 this, &OomPriorityManager::RecordRecentTabDiscard
);
101 start_time_
= TimeTicks::Now();
102 // Create a |MemoryPressureListener| to listen for memory events.
103 base::MemoryPressureMonitor
* monitor
= base::MemoryPressureMonitor::Get();
105 memory_pressure_listener_
.reset(new base::MemoryPressureListener(base::Bind(
106 &OomPriorityManager::OnMemoryPressure
, base::Unretained(this))));
107 base::MemoryPressureListener::MemoryPressureLevel level
=
108 monitor
->GetCurrentPressureLevel();
109 if (level
== base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
) {
110 OnMemoryPressure(level
);
115 void OomPriorityManager::Stop() {
116 update_timer_
.Stop();
117 recent_tab_discard_timer_
.Stop();
118 memory_pressure_listener_
.reset();
121 // Things we need to collect on the browser thread (because TabStripModel isn't
123 // 1) whether or not a tab is pinned
124 // 2) last time a tab was selected
125 // 3) is the tab currently selected
126 TabStatsList
OomPriorityManager::GetTabStats() {
127 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
128 TabStatsList stats_list
;
129 stats_list
.reserve(32); // 99% of users have < 30 tabs open
131 // We go through each window to get all the tabs. Depending on the platform,
132 // windows are either native or ash or both. We want to make sure to go
133 // through them all, starting with the active window first (we use
134 // chrome::GetActiveDesktop to get the current used type).
135 AddTabStats(BrowserList::GetInstance(chrome::GetActiveDesktop()), true,
137 if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_NATIVE
) {
138 AddTabStats(BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE
),
140 } else if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH
) {
141 AddTabStats(BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH
), false,
145 // Sort the data we collected so that least desirable to be
146 // killed is first, most desirable is last.
147 std::sort(stats_list
.begin(), stats_list
.end(), CompareTabStats
);
151 // TODO(jamescook): This should consider tabs with references to other tabs,
152 // such as tabs created with JavaScript window.open(). We might want to
153 // discard the entire set together, or use that in the priority computation.
154 bool OomPriorityManager::DiscardTab() {
155 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
156 TabStatsList stats
= GetTabStats();
159 // Loop until we find a non-discarded tab to kill.
160 for (TabStatsList::const_reverse_iterator stats_rit
= stats
.rbegin();
161 stats_rit
!= stats
.rend(); ++stats_rit
) {
162 int64 least_important_tab_id
= stats_rit
->tab_contents_id
;
163 if (DiscardTabById(least_important_tab_id
))
169 bool OomPriorityManager::DiscardTabById(int64 target_web_contents_id
) {
170 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
171 Browser
* browser
= *it
;
172 TabStripModel
* model
= browser
->tab_strip_model();
173 for (int idx
= 0; idx
< model
->count(); idx
++) {
174 // Can't discard tabs that are already discarded or active.
175 if (model
->IsTabDiscarded(idx
) || (model
->active_index() == idx
))
177 WebContents
* web_contents
= model
->GetWebContentsAt(idx
);
178 int64 web_contents_id
= IdFromWebContents(web_contents
);
179 if (web_contents_id
== target_web_contents_id
) {
180 VLOG(1) << "Discarding tab " << idx
<< " id " << target_web_contents_id
;
181 // Record statistics before discarding because we want to capture the
182 // memory state that lead to the discard.
183 RecordDiscardStatistics();
184 model
->DiscardWebContentsAt(idx
);
185 recent_tab_discard_
= true;
193 void OomPriorityManager::LogMemoryAndDiscardTab() {
194 LogMemory("Tab Discards Memory details",
195 base::Bind(&OomPriorityManager::PurgeMemoryAndDiscardTab
));
198 void OomPriorityManager::LogMemory(const std::string
& title
,
199 const base::Closure
& callback
) {
200 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
201 OomMemoryDetails::Log(title
, callback
);
204 ///////////////////////////////////////////////////////////////////////////////
205 // OomPriorityManager, private:
208 void OomPriorityManager::PurgeMemoryAndDiscardTab() {
209 if (g_browser_process
&& g_browser_process
->GetOomPriorityManager()) {
210 OomPriorityManager
* manager
= g_browser_process
->GetOomPriorityManager();
211 manager
->PurgeBrowserMemory();
212 manager
->DiscardTab();
217 bool OomPriorityManager::IsInternalPage(const GURL
& url
) {
218 // There are many chrome:// UI URLs, but only look for the ones that users
219 // are likely to have open. Most of the benefit is the from NTP URL.
220 const char* const kInternalPagePrefixes
[] = {
221 chrome::kChromeUIDownloadsURL
,
222 chrome::kChromeUIHistoryURL
,
223 chrome::kChromeUINewTabURL
,
224 chrome::kChromeUISettingsURL
,
226 // Prefix-match against the table above. Use strncmp to avoid allocating
227 // memory to convert the URL prefix constants into std::strings.
228 for (size_t i
= 0; i
< arraysize(kInternalPagePrefixes
); ++i
) {
229 if (!strncmp(url
.spec().c_str(), kInternalPagePrefixes
[i
],
230 strlen(kInternalPagePrefixes
[i
])))
236 void OomPriorityManager::RecordDiscardStatistics() {
237 // Record a raw count so we can compare to discard reloads.
239 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.Discard.DiscardCount", discard_count_
, 1,
242 // TODO(jamescook): Maybe incorporate extension count?
243 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.Discard.TabCount", GetTabCount(), 1, 100,
245 #if defined(OS_CHROMEOS)
246 // Record the discarded tab in relation to the amount of simultaneously
248 ash::MultiProfileUMA::RecordDiscardedTab(ash::Shell::GetInstance()
249 ->session_state_delegate()
250 ->NumberOfLoggedInUsers());
252 // TODO(jamescook): If the time stats prove too noisy, then divide up users
253 // based on how heavily they use Chrome using tab count as a proxy.
254 // Bin into <= 1, <= 2, <= 4, <= 8, etc.
255 if (last_discard_time_
.is_null()) {
256 // This is the first discard this session.
257 TimeDelta interval
= TimeTicks::Now() - start_time_
;
258 int interval_seconds
= static_cast<int>(interval
.InSeconds());
259 // Record time in seconds over an interval of approximately 1 day.
260 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.Discard.InitialTime2", interval_seconds
,
263 // Not the first discard, so compute time since last discard.
264 TimeDelta interval
= TimeTicks::Now() - last_discard_time_
;
265 int interval_ms
= static_cast<int>(interval
.InMilliseconds());
266 // Record time in milliseconds over an interval of approximately 1 day.
267 // Start at 100 ms to get extra resolution in the target 750 ms range.
268 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.Discard.IntervalTime2", interval_ms
, 100,
271 // TODO(georgesak): Remove this #if when RecordMemoryStats is implemented for
273 #if defined(OS_WIN) || defined(OS_CHROMEOS)
274 // Record system memory usage at the time of the discard.
275 RecordMemoryStats(RECORD_MEMORY_STATS_TAB_DISCARDED
);
277 // Set up to record the next interval.
278 last_discard_time_
= TimeTicks::Now();
281 void OomPriorityManager::RecordRecentTabDiscard() {
282 // If we are shutting down, do not do anything.
283 if (g_browser_process
->IsShuttingDown())
286 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
287 // If we change the interval we need to change the histogram name.
288 UMA_HISTOGRAM_BOOLEAN("Tabs.Discard.DiscardInLastMinute",
289 recent_tab_discard_
);
290 // Reset for the next interval.
291 recent_tab_discard_
= false;
294 void OomPriorityManager::PurgeBrowserMemory() {
295 // Based on experimental evidence, attempts to free memory from renderers
296 // have been too slow to use in OOM situations (V8 garbage collection) or
297 // do not lead to persistent decreased usage (image/bitmap caches). This
298 // function therefore only targets large blocks of memory in the browser.
299 // Note that other objects will listen to MemoryPressureListener events
300 // to release memory.
301 for (TabContentsIterator it
; !it
.done(); it
.Next()) {
302 WebContents
* web_contents
= *it
;
303 // Screenshots can consume ~5 MB per web contents for platforms that do
304 // touch back/forward.
305 web_contents
->GetController().ClearAllScreenshots();
309 int OomPriorityManager::GetTabCount() const {
311 for (chrome::BrowserIterator it
; !it
.done(); it
.Next())
312 tab_count
+= it
->tab_strip_model()->count();
316 // Returns true if |first| is considered less desirable to be killed
318 bool OomPriorityManager::CompareTabStats(TabStats first
, TabStats second
) {
319 // Being currently selected is most important to protect.
320 if (first
.is_selected
!= second
.is_selected
)
321 return first
.is_selected
;
323 // Tab with internal web UI like NTP or Settings are good choices to discard,
324 // so protect non-Web UI and let the other conditionals finish the sort.
325 if (first
.is_internal_page
!= second
.is_internal_page
)
326 return !first
.is_internal_page
;
328 // Being pinned is important to protect.
329 if (first
.is_pinned
!= second
.is_pinned
)
330 return first
.is_pinned
;
332 // Being an app is important too, as you're the only visible surface in the
333 // window and we don't want to discard that.
334 if (first
.is_app
!= second
.is_app
)
337 // Protect streaming audio and video conferencing tabs.
338 if (first
.is_playing_audio
!= second
.is_playing_audio
)
339 return first
.is_playing_audio
;
341 // TODO(jamescook): Incorporate sudden_termination_allowed into the sort
342 // order. We don't do this now because pages with unload handlers set
343 // sudden_termination_allowed false, and that covers too many common pages
344 // with ad networks and statistics scripts. Ideally we would like to check
345 // for beforeUnload handlers, which are likely to present a dialog asking
346 // if the user wants to discard state. crbug.com/123049
348 // Being more recently active is more important.
349 return first
.last_active
> second
.last_active
;
352 // This function is called when |update_timer_| fires. It will adjust the clock
353 // if needed (if we detect that the machine was asleep) and will fire the stats
354 // updating on ChromeOS via the delegate.
355 void OomPriorityManager::UpdateTimerCallback() {
356 // If we shutting down, do not do anything.
357 if (g_browser_process
->IsShuttingDown())
360 if (BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH
)->empty() &&
361 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE
)->empty())
364 // Check for a discontinuity in time caused by the machine being suspended.
365 if (!last_adjust_time_
.is_null()) {
366 TimeDelta suspend_time
= TimeTicks::Now() - last_adjust_time_
;
367 if (suspend_time
.InSeconds() > kSuspendThresholdSeconds
) {
368 // We were probably suspended, move our event timers forward in time so
369 // when we subtract them out later we are counting "uptime".
370 start_time_
+= suspend_time
;
371 if (!last_discard_time_
.is_null())
372 last_discard_time_
+= suspend_time
;
375 last_adjust_time_
= TimeTicks::Now();
377 #if defined(OS_CHROMEOS)
378 TabStatsList stats_list
= GetTabStats();
379 // This starts the CrOS specific OOM adjustments in /proc/<pid>/oom_score_adj.
380 delegate_
->AdjustOomPriorities(stats_list
);
384 void OomPriorityManager::AddTabStats(BrowserList
* browser_list
,
386 TabStatsList
* stats_list
) {
387 // If it's the active desktop, the first window will be the active one.
388 // Otherwise, we assume no active windows.
389 bool browser_active
= active_desktop
;
390 for (BrowserList::const_reverse_iterator browser_iterator
=
391 browser_list
->begin_last_active();
392 browser_iterator
!= browser_list
->end_last_active();
393 ++browser_iterator
) {
394 Browser
* browser
= *browser_iterator
;
395 bool is_browser_for_app
= browser
->is_app();
396 const TabStripModel
* model
= browser
->tab_strip_model();
397 for (int i
= 0; i
< model
->count(); i
++) {
398 WebContents
* contents
= model
->GetWebContentsAt(i
);
399 if (!contents
->IsCrashed()) {
401 stats
.is_app
= is_browser_for_app
;
402 stats
.is_internal_page
=
403 IsInternalPage(contents
->GetLastCommittedURL());
404 stats
.is_playing_audio
= chrome::IsPlayingAudio(contents
);
405 stats
.is_pinned
= model
->IsTabPinned(i
);
406 stats
.is_selected
= browser_active
&& model
->IsTabSelected(i
);
407 stats
.is_discarded
= model
->IsTabDiscarded(i
);
408 stats
.last_active
= contents
->GetLastActiveTime();
409 stats
.renderer_handle
= contents
->GetRenderProcessHost()->GetHandle();
410 stats
.child_process_host_id
= contents
->GetRenderProcessHost()->GetID();
411 #if defined(OS_CHROMEOS)
412 stats
.oom_score
= delegate_
->GetOomScore(stats
.child_process_host_id
);
414 stats
.title
= contents
->GetTitle();
415 stats
.tab_contents_id
= IdFromWebContents(contents
);
416 stats_list
->push_back(stats
);
419 // We process the active browser window in the first iteration.
420 browser_active
= false;
424 void OomPriorityManager::OnMemoryPressure(
425 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level
) {
426 // If we are shutting down, do not do anything.
427 if (g_browser_process
->IsShuttingDown())
430 // For the moment we only do something when we reach a critical state.
431 if (memory_pressure_level
==
432 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
) {
433 LogMemoryAndDiscardTab();
435 // TODO(skuhne): If more memory pressure levels are introduced, we might
436 // consider to call PurgeBrowserMemory() before CRITICAL is reached.
439 } // namespace memory