| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/memory_details.h" | 5 #include "chrome/browser/memory_details.h" |
| 6 | 6 |
| 7 #include "base/file_version_info.h" | 7 #include "base/file_version_info.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/process_util.h" | 9 #include "base/process_util.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/browser_child_process_host.h" | 12 #include "chrome/browser/browser_child_process_host.h" |
| 13 #include "chrome/browser/browser_thread.h" | 13 #include "chrome/browser/browser_thread.h" |
| 14 #include "chrome/browser/extensions/extension_host.h" |
| 14 #include "chrome/browser/renderer_host/backing_store_manager.h" | 15 #include "chrome/browser/renderer_host/backing_store_manager.h" |
| 15 #include "chrome/browser/renderer_host/render_process_host.h" | 16 #include "chrome/browser/renderer_host/render_process_host.h" |
| 16 #include "chrome/browser/renderer_host/render_view_host.h" | 17 #include "chrome/browser/renderer_host/render_view_host.h" |
| 17 #include "chrome/browser/tab_contents/navigation_entry.h" | 18 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 18 #include "chrome/browser/tab_contents/tab_contents.h" | 19 #include "chrome/browser/tab_contents/tab_contents.h" |
| 20 #include "chrome/common/bindings_policy.h" |
| 21 #include "chrome/common/extensions/extension.h" |
| 19 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
| 20 #include "grit/chromium_strings.h" | 23 #include "grit/chromium_strings.h" |
| 21 | 24 |
| 22 #if defined(OS_LINUX) | 25 #if defined(OS_LINUX) |
| 23 #include "chrome/browser/zygote_host_linux.h" | 26 #include "chrome/browser/zygote_host_linux.h" |
| 24 #include "chrome/browser/renderer_host/render_sandbox_host_linux.h" | 27 #include "chrome/browser/renderer_host/render_sandbox_host_linux.h" |
| 25 #endif | 28 #endif |
| 26 | 29 |
| 27 ProcessMemoryInformation::ProcessMemoryInformation() | 30 ProcessMemoryInformation::ProcessMemoryInformation() |
| 28 : pid(0), | 31 : pid(0), |
| 29 num_processes(0), | 32 num_processes(0), |
| 30 is_diagnostics(false), | 33 is_diagnostics(false), |
| 31 type(ChildProcessInfo::UNKNOWN_PROCESS) { | 34 type(ChildProcessInfo::UNKNOWN_PROCESS), |
| 35 renderer_type(ChildProcessInfo::RENDERER_UNKNOWN) { |
| 32 } | 36 } |
| 33 | 37 |
| 34 ProcessMemoryInformation::~ProcessMemoryInformation() {} | 38 ProcessMemoryInformation::~ProcessMemoryInformation() {} |
| 35 | 39 |
| 36 ProcessData::ProcessData() {} | 40 ProcessData::ProcessData() {} |
| 37 | 41 |
| 38 ProcessData::ProcessData(const ProcessData& rhs) | 42 ProcessData::ProcessData(const ProcessData& rhs) |
| 39 : name(rhs.name), | 43 : name(rhs.name), |
| 40 process_name(rhs.process_name), | 44 process_name(rhs.process_name), |
| 41 processes(rhs.processes) { | 45 processes(rhs.processes) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 std::vector<ProcessMemoryInformation> child_info; | 85 std::vector<ProcessMemoryInformation> child_info; |
| 82 | 86 |
| 83 // Collect the list of child processes. | 87 // Collect the list of child processes. |
| 84 for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) { | 88 for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) { |
| 85 ProcessMemoryInformation info; | 89 ProcessMemoryInformation info; |
| 86 info.pid = base::GetProcId(iter->handle()); | 90 info.pid = base::GetProcId(iter->handle()); |
| 87 if (!info.pid) | 91 if (!info.pid) |
| 88 continue; | 92 continue; |
| 89 | 93 |
| 90 info.type = iter->type(); | 94 info.type = iter->type(); |
| 95 info.renderer_type = iter->renderer_type(); |
| 91 info.titles.push_back(iter->name()); | 96 info.titles.push_back(iter->name()); |
| 92 child_info.push_back(info); | 97 child_info.push_back(info); |
| 93 } | 98 } |
| 94 | 99 |
| 95 // Now go do expensive memory lookups from the file thread. | 100 // Now go do expensive memory lookups from the file thread. |
| 96 BrowserThread::PostTask( | 101 BrowserThread::PostTask( |
| 97 BrowserThread::FILE, FROM_HERE, | 102 BrowserThread::FILE, FROM_HERE, |
| 98 NewRunnableMethod(this, &MemoryDetails::CollectProcessData, child_info)); | 103 NewRunnableMethod(this, &MemoryDetails::CollectProcessData, child_info)); |
| 99 } | 104 } |
| 100 | 105 |
| 101 void MemoryDetails::CollectChildInfoOnUIThread() { | 106 void MemoryDetails::CollectChildInfoOnUIThread() { |
| 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 103 | 108 |
| 104 #if defined(OS_LINUX) | 109 #if defined(OS_LINUX) |
| 105 const pid_t zygote_pid = ZygoteHost::GetInstance()->pid(); | 110 const pid_t zygote_pid = ZygoteHost::GetInstance()->pid(); |
| 106 const pid_t sandbox_helper_pid = RenderSandboxHostLinux::GetInstance()->pid(); | 111 const pid_t sandbox_helper_pid = RenderSandboxHostLinux::GetInstance()->pid(); |
| 107 #endif | 112 #endif |
| 108 | 113 |
| 109 ProcessData* const chrome_browser = ChromeBrowser(); | 114 ProcessData* const chrome_browser = ChromeBrowser(); |
| 110 // Get more information about the process. | 115 // Get more information about the process. |
| 111 for (size_t index = 0; index < chrome_browser->processes.size(); | 116 for (size_t index = 0; index < chrome_browser->processes.size(); |
| 112 index++) { | 117 index++) { |
| 113 // Check if it's a renderer, if so get the list of page titles in it and | 118 // Check if it's a renderer, if so get the list of page titles in it and |
| 114 // check if it's a diagnostics-related process. We skip all diagnostics | 119 // check if it's a diagnostics-related process. We skip about:memory pages. |
| 115 // pages (e.g. "about:xxx" URLs). Iterate the RenderProcessHosts to find | 120 // Iterate the RenderProcessHosts to find the tab contents. |
| 116 // the tab contents. | |
| 117 ProcessMemoryInformation& process = | 121 ProcessMemoryInformation& process = |
| 118 chrome_browser->processes[index]; | 122 chrome_browser->processes[index]; |
| 119 | 123 |
| 120 for (RenderProcessHost::iterator renderer_iter( | 124 for (RenderProcessHost::iterator renderer_iter( |
| 121 RenderProcessHost::AllHostsIterator()); !renderer_iter.IsAtEnd(); | 125 RenderProcessHost::AllHostsIterator()); !renderer_iter.IsAtEnd(); |
| 122 renderer_iter.Advance()) { | 126 renderer_iter.Advance()) { |
| 123 DCHECK(renderer_iter.GetCurrentValue()); | 127 RenderProcessHost* rph = renderer_iter.GetCurrentValue(); |
| 128 DCHECK(rph); |
| 124 // Ignore processes that don't have a connection, such as crashed tabs. | 129 // Ignore processes that don't have a connection, such as crashed tabs. |
| 125 if (!renderer_iter.GetCurrentValue()->HasConnection() || process.pid != | 130 if (!rph->HasConnection() || |
| 126 base::GetProcId(renderer_iter.GetCurrentValue()->GetHandle())) { | 131 process.pid != base::GetProcId(rph->GetHandle())) { |
| 127 continue; | 132 continue; |
| 128 } | 133 } |
| 129 process.type = ChildProcessInfo::RENDER_PROCESS; | 134 process.type = ChildProcessInfo::RENDER_PROCESS; |
| 130 // The RenderProcessHost may host multiple TabContents. Any | 135 // The RenderProcessHost may host multiple TabContents. Any |
| 131 // of them which contain diagnostics information make the whole | 136 // of them which contain diagnostics information make the whole |
| 132 // process be considered a diagnostics process. | 137 // process be considered a diagnostics process. |
| 133 // | 138 // |
| 134 // NOTE: This is a bit dangerous. We know that for now, listeners | 139 // NOTE: This is a bit dangerous. We know that for now, listeners |
| 135 // are always RenderWidgetHosts. But in theory, they don't | 140 // are always RenderWidgetHosts. But in theory, they don't |
| 136 // have to be. | 141 // have to be. |
| 137 RenderProcessHost::listeners_iterator iter( | 142 RenderProcessHost::listeners_iterator iter(rph->ListenersIterator()); |
| 138 renderer_iter.GetCurrentValue()->ListenersIterator()); | |
| 139 for (; !iter.IsAtEnd(); iter.Advance()) { | 143 for (; !iter.IsAtEnd(); iter.Advance()) { |
| 140 const RenderWidgetHost* widget = | 144 const RenderWidgetHost* widget = |
| 141 static_cast<const RenderWidgetHost*>(iter.GetCurrentValue()); | 145 static_cast<const RenderWidgetHost*>(iter.GetCurrentValue()); |
| 142 DCHECK(widget); | 146 DCHECK(widget); |
| 143 if (!widget || !widget->IsRenderView()) | 147 if (!widget || !widget->IsRenderView()) |
| 144 continue; | 148 continue; |
| 145 | 149 |
| 146 const RenderViewHost* host = static_cast<const RenderViewHost*>(widget); | 150 const RenderViewHost* host = static_cast<const RenderViewHost*>(widget); |
| 151 if (host->enabled_bindings() && BindingsPolicy::EXTENSION) { |
| 152 process.renderer_type = ChildProcessInfo::RENDERER_EXTENSION; |
| 153 } else if (host->enabled_bindings() && BindingsPolicy::DOM_UI) { |
| 154 process.renderer_type = ChildProcessInfo::RENDERER_CHROME; |
| 155 } else { |
| 156 process.renderer_type = ChildProcessInfo::RENDERER_NORMAL; |
| 157 } |
| 147 TabContents* contents = NULL; | 158 TabContents* contents = NULL; |
| 148 if (host->delegate()) | 159 if (host->delegate()) |
| 149 contents = host->delegate()->GetAsTabContents(); | 160 contents = host->delegate()->GetAsTabContents(); |
| 150 if (!contents) | 161 if (!contents) { |
| 162 if (host->is_extension_process()) { |
| 163 // TODO(erikkay) should we just add GetAsExtensionHost to |
| 164 // TabContents? |
| 165 ExtensionHost* eh = static_cast<ExtensionHost*>(host->delegate()); |
| 166 std::wstring title = UTF8ToWide(eh->extension()->name()); |
| 167 process.titles.push_back(title); |
| 168 } else { |
| 169 process.renderer_type = ChildProcessInfo::RENDERER_UNKNOWN; |
| 170 } |
| 151 continue; | 171 continue; |
| 172 } |
| 173 |
| 152 std::wstring title = UTF16ToWideHack(contents->GetTitle()); | 174 std::wstring title = UTF16ToWideHack(contents->GetTitle()); |
| 153 if (!title.length()) | 175 if (!title.length()) |
| 154 title = L"Untitled"; | 176 title = L"Untitled"; |
| 155 process.titles.push_back(title); | 177 process.titles.push_back(title); |
| 156 | 178 |
| 157 // We need to check the pending entry as well as the virtual_url to | 179 // We need to check the pending entry as well as the virtual_url to |
| 158 // see if it's an about:memory URL (we don't want to count these in the | 180 // see if it's an about:memory URL (we don't want to count these in the |
| 159 // total memory usage of the browser). | 181 // total memory usage of the browser). |
| 160 // | 182 // |
| 161 // When we reach here, about:memory will be the pending entry since we | 183 // When we reach here, about:memory will be the pending entry since we |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 | 225 |
| 204 OnDetailsAvailable(); | 226 OnDetailsAvailable(); |
| 205 } | 227 } |
| 206 | 228 |
| 207 void MemoryDetails::UpdateHistograms() { | 229 void MemoryDetails::UpdateHistograms() { |
| 208 // Reports a set of memory metrics to UMA. | 230 // Reports a set of memory metrics to UMA. |
| 209 // Memory is measured in KB. | 231 // Memory is measured in KB. |
| 210 | 232 |
| 211 const ProcessData& browser = *ChromeBrowser(); | 233 const ProcessData& browser = *ChromeBrowser(); |
| 212 size_t aggregate_memory = 0; | 234 size_t aggregate_memory = 0; |
| 235 int chrome_count = 0; |
| 236 int extension_count = 0; |
| 213 int plugin_count = 0; | 237 int plugin_count = 0; |
| 238 int renderer_count = 0; |
| 239 int other_count = 0; |
| 214 int worker_count = 0; | 240 int worker_count = 0; |
| 215 for (size_t index = 0; index < browser.processes.size(); index++) { | 241 for (size_t index = 0; index < browser.processes.size(); index++) { |
| 216 int sample = static_cast<int>(browser.processes[index].working_set.priv); | 242 int sample = static_cast<int>(browser.processes[index].working_set.priv); |
| 217 aggregate_memory += sample; | 243 aggregate_memory += sample; |
| 218 switch (browser.processes[index].type) { | 244 switch (browser.processes[index].type) { |
| 219 case ChildProcessInfo::BROWSER_PROCESS: | 245 case ChildProcessInfo::BROWSER_PROCESS: |
| 220 UMA_HISTOGRAM_MEMORY_KB("Memory.Browser", sample); | 246 UMA_HISTOGRAM_MEMORY_KB("Memory.Browser", sample); |
| 221 break; | 247 break; |
| 222 case ChildProcessInfo::RENDER_PROCESS: | 248 case ChildProcessInfo::RENDER_PROCESS: { |
| 223 UMA_HISTOGRAM_MEMORY_KB("Memory.Renderer", sample); | 249 ChildProcessInfo::RendererProcessType renderer_type = |
| 250 browser.processes[index].renderer_type; |
| 251 switch (renderer_type) { |
| 252 case ChildProcessInfo::RENDERER_EXTENSION: |
| 253 UMA_HISTOGRAM_MEMORY_KB("Memory.Extension", sample); |
| 254 extension_count++; |
| 255 break; |
| 256 case ChildProcessInfo::RENDERER_CHROME: |
| 257 UMA_HISTOGRAM_MEMORY_KB("Memory.Chrome", sample); |
| 258 chrome_count++; |
| 259 break; |
| 260 case ChildProcessInfo::RENDERER_UNKNOWN: |
| 261 case ChildProcessInfo::RENDERER_NORMAL: |
| 262 default: |
| 263 UMA_HISTOGRAM_MEMORY_KB("Memory.Renderer", sample); |
| 264 renderer_count++; |
| 265 break; |
| 266 } |
| 224 break; | 267 break; |
| 268 } |
| 225 case ChildProcessInfo::PLUGIN_PROCESS: | 269 case ChildProcessInfo::PLUGIN_PROCESS: |
| 226 UMA_HISTOGRAM_MEMORY_KB("Memory.Plugin", sample); | 270 UMA_HISTOGRAM_MEMORY_KB("Memory.Plugin", sample); |
| 227 plugin_count++; | 271 plugin_count++; |
| 228 break; | 272 break; |
| 229 case ChildProcessInfo::WORKER_PROCESS: | 273 case ChildProcessInfo::WORKER_PROCESS: |
| 230 UMA_HISTOGRAM_MEMORY_KB("Memory.Worker", sample); | 274 UMA_HISTOGRAM_MEMORY_KB("Memory.Worker", sample); |
| 231 worker_count++; | 275 worker_count++; |
| 232 break; | 276 break; |
| 233 case ChildProcessInfo::UTILITY_PROCESS: | 277 case ChildProcessInfo::UTILITY_PROCESS: |
| 234 UMA_HISTOGRAM_MEMORY_KB("Memory.Utility", sample); | 278 UMA_HISTOGRAM_MEMORY_KB("Memory.Utility", sample); |
| 279 other_count++; |
| 235 break; | 280 break; |
| 236 case ChildProcessInfo::ZYGOTE_PROCESS: | 281 case ChildProcessInfo::ZYGOTE_PROCESS: |
| 237 UMA_HISTOGRAM_MEMORY_KB("Memory.Zygote", sample); | 282 UMA_HISTOGRAM_MEMORY_KB("Memory.Zygote", sample); |
| 283 other_count++; |
| 238 break; | 284 break; |
| 239 case ChildProcessInfo::SANDBOX_HELPER_PROCESS: | 285 case ChildProcessInfo::SANDBOX_HELPER_PROCESS: |
| 240 UMA_HISTOGRAM_MEMORY_KB("Memory.SandboxHelper", sample); | 286 UMA_HISTOGRAM_MEMORY_KB("Memory.SandboxHelper", sample); |
| 287 other_count++; |
| 241 break; | 288 break; |
| 242 case ChildProcessInfo::NACL_LOADER_PROCESS: | 289 case ChildProcessInfo::NACL_LOADER_PROCESS: |
| 243 UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClient", sample); | 290 UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClient", sample); |
| 291 other_count++; |
| 244 break; | 292 break; |
| 245 case ChildProcessInfo::NACL_BROKER_PROCESS: | 293 case ChildProcessInfo::NACL_BROKER_PROCESS: |
| 246 UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClientBroker", sample); | 294 UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClientBroker", sample); |
| 295 other_count++; |
| 247 break; | 296 break; |
| 248 case ChildProcessInfo::GPU_PROCESS: | 297 case ChildProcessInfo::GPU_PROCESS: |
| 249 UMA_HISTOGRAM_MEMORY_KB("Memory.Gpu", sample); | 298 UMA_HISTOGRAM_MEMORY_KB("Memory.Gpu", sample); |
| 299 other_count++; |
| 250 break; | 300 break; |
| 251 default: | 301 default: |
| 252 NOTREACHED(); | 302 NOTREACHED(); |
| 253 } | 303 } |
| 254 } | 304 } |
| 255 UMA_HISTOGRAM_MEMORY_KB("Memory.BackingStore", | 305 UMA_HISTOGRAM_MEMORY_KB("Memory.BackingStore", |
| 256 BackingStoreManager::MemorySize() / 1024); | 306 BackingStoreManager::MemorySize() / 1024); |
| 257 | 307 |
| 258 UMA_HISTOGRAM_COUNTS_100("Memory.ProcessCount", | 308 UMA_HISTOGRAM_COUNTS_100("Memory.ProcessCount", |
| 259 static_cast<int>(browser.processes.size())); | 309 static_cast<int>(browser.processes.size())); |
| 310 UMA_HISTOGRAM_COUNTS_100("Memory.ChromeProcessCount", chrome_count); |
| 311 UMA_HISTOGRAM_COUNTS_100("Memory.ExtensionProcessCount", extension_count); |
| 312 UMA_HISTOGRAM_COUNTS_100("Memory.OtherProcessCount", other_count); |
| 260 UMA_HISTOGRAM_COUNTS_100("Memory.PluginProcessCount", plugin_count); | 313 UMA_HISTOGRAM_COUNTS_100("Memory.PluginProcessCount", plugin_count); |
| 314 UMA_HISTOGRAM_COUNTS_100("Memory.RendererProcessCount", renderer_count); |
| 261 UMA_HISTOGRAM_COUNTS_100("Memory.WorkerProcessCount", worker_count); | 315 UMA_HISTOGRAM_COUNTS_100("Memory.WorkerProcessCount", worker_count); |
| 262 // TODO(viettrungluu): Do we want separate counts for the other | 316 // TODO(viettrungluu): Do we want separate counts for the other |
| 263 // (platform-specific) process types? | 317 // (platform-specific) process types? |
| 264 | 318 |
| 265 int total_sample = static_cast<int>(aggregate_memory / 1000); | 319 int total_sample = static_cast<int>(aggregate_memory / 1000); |
| 266 UMA_HISTOGRAM_MEMORY_MB("Memory.Total", total_sample); | 320 UMA_HISTOGRAM_MEMORY_MB("Memory.Total", total_sample); |
| 267 } | 321 } |
| OLD | NEW |