| OLD | NEW |
| 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 #include "chrome/browser/oom_priority_manager.h" | 5 #include "chrome/browser/oom_priority_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/process.h" | 14 #include "base/process.h" |
| 15 #include "base/process_util.h" | 15 #include "base/process_util.h" |
| 16 #include "base/string16.h" | 16 #include "base/string16.h" |
| 17 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
| 18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 20 #include "base/timer.h" | 20 #include "base/timer.h" |
| 21 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 23 #include "chrome/browser/browser_process.h" |
| 23 #include "chrome/browser/low_memory_observer.h" | 24 #include "chrome/browser/low_memory_observer.h" |
| 25 #include "chrome/browser/memory_details.h" |
| 24 #include "chrome/browser/tabs/tab_strip_model.h" | 26 #include "chrome/browser/tabs/tab_strip_model.h" |
| 25 #include "chrome/browser/ui/browser_list.h" | 27 #include "chrome/browser/ui/browser_list.h" |
| 26 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 27 #include "chrome/common/chrome_constants.h" | 29 #include "chrome/common/chrome_constants.h" |
| 28 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
| 29 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/notification_service.h" | 32 #include "content/public/browser/notification_service.h" |
| 31 #include "content/public/browser/notification_types.h" | 33 #include "content/public/browser/notification_types.h" |
| 32 #include "content/public/browser/render_process_host.h" | 34 #include "content/public/browser/render_process_host.h" |
| 33 #include "content/public/browser/render_widget_host.h" | 35 #include "content/public/browser/render_widget_host.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 // The default interval in milliseconds to wait before setting the score of | 71 // The default interval in milliseconds to wait before setting the score of |
| 70 // currently focused tab. | 72 // currently focused tab. |
| 71 const int kFocusedTabScoreAdjustIntervalMs = 500; | 73 const int kFocusedTabScoreAdjustIntervalMs = 500; |
| 72 | 74 |
| 73 // Returns a unique ID for a WebContents. Do not cast back to a pointer, as | 75 // Returns a unique ID for a WebContents. Do not cast back to a pointer, as |
| 74 // the WebContents could be deleted if the user closed the tab. | 76 // the WebContents could be deleted if the user closed the tab. |
| 75 int64 IdFromTabContents(WebContents* web_contents) { | 77 int64 IdFromTabContents(WebContents* web_contents) { |
| 76 return reinterpret_cast<int64>(web_contents); | 78 return reinterpret_cast<int64>(web_contents); |
| 77 } | 79 } |
| 78 | 80 |
| 81 //////////////////////////////////////////////////////////////////////////////// |
| 82 // OomMemoryDetails logs details about all Chrome processes during an out-of- |
| 83 // memory event in an attempt to identify the culprit, then discards a tab and |
| 84 // deletes itself. |
| 85 class OomMemoryDetails : public MemoryDetails { |
| 86 public: |
| 87 OomMemoryDetails(); |
| 88 |
| 89 // MemoryDetails overrides: |
| 90 virtual void OnDetailsAvailable() OVERRIDE; |
| 91 |
| 92 private: |
| 93 virtual ~OomMemoryDetails() {} |
| 94 |
| 95 TimeTicks start_time_; |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(OomMemoryDetails); |
| 98 }; |
| 99 |
| 100 OomMemoryDetails::OomMemoryDetails() { |
| 101 AddRef(); // Released in OnDetailsAvailable(). |
| 102 start_time_ = TimeTicks::Now(); |
| 103 } |
| 104 |
| 105 void OomMemoryDetails::OnDetailsAvailable() { |
| 106 TimeDelta delta = TimeTicks::Now() - start_time_; |
| 107 // These logs are collected by user feedback reports. We want them to help |
| 108 // diagnose user-reported problems with frequently discarded tabs. |
| 109 LOG(INFO) << "OOM details (" << delta.InMilliseconds() << " ms):\n" |
| 110 << ToLogString(); |
| 111 if (g_browser_process && g_browser_process->oom_priority_manager()) |
| 112 g_browser_process->oom_priority_manager()->DiscardTab(); |
| 113 // Delete ourselves so we don't have to worry about OomPriorityManager |
| 114 // deleting us when we're still working. |
| 115 Release(); |
| 116 } |
| 117 |
| 79 } // namespace | 118 } // namespace |
| 80 | 119 |
| 81 //////////////////////////////////////////////////////////////////////////////// | 120 //////////////////////////////////////////////////////////////////////////////// |
| 82 // OomPriorityManager | 121 // OomPriorityManager |
| 83 | 122 |
| 84 OomPriorityManager::TabStats::TabStats() | 123 OomPriorityManager::TabStats::TabStats() |
| 85 : is_pinned(false), | 124 : is_pinned(false), |
| 86 is_selected(false), | 125 is_selected(false), |
| 87 renderer_handle(0), | 126 renderer_handle(0), |
| 88 sudden_termination_allowed(false), | 127 sudden_termination_allowed(false), |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 for (TabStatsList::const_reverse_iterator stats_rit = stats.rbegin(); | 200 for (TabStatsList::const_reverse_iterator stats_rit = stats.rbegin(); |
| 162 stats_rit != stats.rend(); | 201 stats_rit != stats.rend(); |
| 163 ++stats_rit) { | 202 ++stats_rit) { |
| 164 int64 least_important_tab_id = stats_rit->tab_contents_id; | 203 int64 least_important_tab_id = stats_rit->tab_contents_id; |
| 165 if (DiscardTabById(least_important_tab_id)) | 204 if (DiscardTabById(least_important_tab_id)) |
| 166 return true; | 205 return true; |
| 167 } | 206 } |
| 168 return false; | 207 return false; |
| 169 } | 208 } |
| 170 | 209 |
| 210 void OomPriorityManager::LogMemoryAndDiscardTab() { |
| 211 // Deletes itself upon completion. |
| 212 OomMemoryDetails* details = new OomMemoryDetails(); |
| 213 details->StartFetch(MemoryDetails::SKIP_USER_METRICS); |
| 214 } |
| 215 |
| 171 bool OomPriorityManager::DiscardTabById(int64 target_web_contents_id) { | 216 bool OomPriorityManager::DiscardTabById(int64 target_web_contents_id) { |
| 172 for (BrowserList::const_iterator browser_iterator = BrowserList::begin(); | 217 for (BrowserList::const_iterator browser_iterator = BrowserList::begin(); |
| 173 browser_iterator != BrowserList::end(); ++browser_iterator) { | 218 browser_iterator != BrowserList::end(); ++browser_iterator) { |
| 174 Browser* browser = *browser_iterator; | 219 Browser* browser = *browser_iterator; |
| 175 TabStripModel* model = browser->tabstrip_model(); | 220 TabStripModel* model = browser->tabstrip_model(); |
| 176 for (int idx = 0; idx < model->count(); idx++) { | 221 for (int idx = 0; idx < model->count(); idx++) { |
| 177 // Can't discard tabs that are already discarded. | 222 // Can't discard tabs that are already discarded. |
| 178 if (model->IsTabDiscarded(idx)) | 223 if (model->IsTabDiscarded(idx)) |
| 179 continue; | 224 continue; |
| 180 WebContents* web_contents = model->GetTabContentsAt(idx)->web_contents(); | 225 WebContents* web_contents = model->GetTabContentsAt(idx)->web_contents(); |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 content::ZygoteHost::GetInstance()->AdjustRendererOOMScore( | 476 content::ZygoteHost::GetInstance()->AdjustRendererOOMScore( |
| 432 iterator->renderer_handle, score); | 477 iterator->renderer_handle, score); |
| 433 pid_to_oom_score_[iterator->renderer_handle] = score; | 478 pid_to_oom_score_[iterator->renderer_handle] = score; |
| 434 } | 479 } |
| 435 priority += priority_increment; | 480 priority += priority_increment; |
| 436 } | 481 } |
| 437 } | 482 } |
| 438 } | 483 } |
| 439 | 484 |
| 440 } // namespace browser | 485 } // namespace browser |
| OLD | NEW |