[email protected] | 8a34e660 | 2010-10-02 17:29:43 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/memory_details.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | #include "base/file_version_info.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 8 | #include "base/metrics/histogram.h" |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 9 | #include "base/process_util.h" |
[email protected] | 33b9df4 | 2010-07-29 06:46:39 | [diff] [blame] | 10 | #include "base/string_util.h" |
[email protected] | 64048bd | 2010-03-08 23:28:58 | [diff] [blame] | 11 | #include "base/utf_string_conversions.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 12 | #include "chrome/browser/browser_child_process_host.h" |
[email protected] | e3671727 | 2010-10-12 12:07:13 | [diff] [blame] | 13 | #include "chrome/browser/browser_thread.h" |
[email protected] | 57c4b85 | 2009-08-17 21:59:29 | [diff] [blame] | 14 | #include "chrome/browser/renderer_host/backing_store_manager.h" |
[email protected] | 8c8657d6 | 2009-01-16 18:31:26 | [diff] [blame] | 15 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | 8cb5d5b | 2010-02-09 11:36:16 | [diff] [blame] | 16 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 17 | #include "chrome/browser/tab_contents/navigation_entry.h" |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 18 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 19 | #include "chrome/common/url_constants.h" |
[email protected] | ae5ca89 | 2009-07-30 18:00:47 | [diff] [blame] | 20 | #include "grit/chromium_strings.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 22 | #if defined(OS_LINUX) |
| 23 | #include "chrome/browser/zygote_host_linux.h" |
| 24 | #include "chrome/browser/renderer_host/render_sandbox_host_linux.h" |
| 25 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame^] | 27 | ProcessMemoryInformation::ProcessMemoryInformation() |
| 28 | : pid(0), |
| 29 | num_processes(0), |
| 30 | is_diagnostics(false), |
| 31 | type(ChildProcessInfo::UNKNOWN_PROCESS) { |
| 32 | } |
| 33 | |
| 34 | ProcessMemoryInformation::~ProcessMemoryInformation() {} |
| 35 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | // About threading: |
| 37 | // |
| 38 | // This operation will hit no fewer than 3 threads. |
| 39 | // |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 40 | // The ChildProcessInfo::Iterator can only be accessed from the IO thread. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | // |
| 42 | // The RenderProcessHostIterator can only be accessed from the UI thread. |
| 43 | // |
| 44 | // This operation can take 30-100ms to complete. We never want to have |
| 45 | // one task run for that long on the UI or IO threads. So, we run the |
| 46 | // expensive parts of this operation over on the file thread. |
| 47 | // |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | void MemoryDetails::StartFetch() { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 49 | DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 50 | DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | |
| 52 | // In order to process this request, we need to use the plugin information. |
| 53 | // However, plugin process information is only available from the IO thread. |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 54 | BrowserThread::PostTask( |
| 55 | BrowserThread::IO, FROM_HERE, |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 56 | NewRunnableMethod(this, &MemoryDetails::CollectChildInfoOnIOThread)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame^] | 59 | MemoryDetails::~MemoryDetails() {} |
| 60 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 61 | void MemoryDetails::CollectChildInfoOnIOThread() { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 62 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 64 | std::vector<ProcessMemoryInformation> child_info; |
| 65 | |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 66 | // Collect the list of child processes. |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 67 | for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) { |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 68 | ProcessMemoryInformation info; |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 69 | info.pid = base::GetProcId(iter->handle()); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 70 | if (!info.pid) |
| 71 | continue; |
| 72 | |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 73 | info.type = iter->type(); |
| 74 | info.titles.push_back(iter->name()); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 75 | child_info.push_back(info); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // Now go do expensive memory lookups from the file thread. |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 79 | BrowserThread::PostTask( |
| 80 | BrowserThread::FILE, FROM_HERE, |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 81 | NewRunnableMethod(this, &MemoryDetails::CollectProcessData, child_info)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 84 | void MemoryDetails::CollectChildInfoOnUIThread() { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 85 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 87 | #if defined(OS_LINUX) |
| 88 | const pid_t zygote_pid = Singleton<ZygoteHost>()->pid(); |
| 89 | const pid_t sandbox_helper_pid = Singleton<RenderSandboxHostLinux>()->pid(); |
| 90 | #endif |
| 91 | |
| 92 | ProcessData* const chrome_browser = ChromeBrowser(); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 93 | // Get more information about the process. |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 94 | for (size_t index = 0; index < chrome_browser->processes.size(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | index++) { |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 96 | // Check if it's a renderer, if so get the list of page titles in it and |
| 97 | // check if it's a diagnostics-related process. We skip all diagnostics |
| 98 | // pages (e.g. "about:xxx" URLs). Iterate the RenderProcessHosts to find |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 99 | // the tab contents. |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 100 | ProcessMemoryInformation& process = |
| 101 | chrome_browser->processes[index]; |
| 102 | |
[email protected] | 019191a | 2009-10-02 20:37:27 | [diff] [blame] | 103 | for (RenderProcessHost::iterator renderer_iter( |
| 104 | RenderProcessHost::AllHostsIterator()); !renderer_iter.IsAtEnd(); |
| 105 | renderer_iter.Advance()) { |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 106 | DCHECK(renderer_iter.GetCurrentValue()); |
[email protected] | 8a34e660 | 2010-10-02 17:29:43 | [diff] [blame] | 107 | // Ignore processes that don't have a connection, such as crashed tabs. |
[email protected] | 062fdf1 | 2010-02-18 23:54:54 | [diff] [blame] | 108 | if (!renderer_iter.GetCurrentValue()->HasConnection() || process.pid != |
[email protected] | 201b273 | 2009-11-13 18:57:46 | [diff] [blame] | 109 | base::GetProcId(renderer_iter.GetCurrentValue()->GetHandle())) { |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 110 | continue; |
[email protected] | 201b273 | 2009-11-13 18:57:46 | [diff] [blame] | 111 | } |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 112 | process.type = ChildProcessInfo::RENDER_PROCESS; |
| 113 | // The RenderProcessHost may host multiple TabContents. Any |
| 114 | // of them which contain diagnostics information make the whole |
| 115 | // process be considered a diagnostics process. |
| 116 | // |
| 117 | // NOTE: This is a bit dangerous. We know that for now, listeners |
| 118 | // are always RenderWidgetHosts. But in theory, they don't |
| 119 | // have to be. |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 120 | RenderProcessHost::listeners_iterator iter( |
| 121 | renderer_iter.GetCurrentValue()->ListenersIterator()); |
| 122 | for (; !iter.IsAtEnd(); iter.Advance()) { |
| 123 | const RenderWidgetHost* widget = |
| 124 | static_cast<const RenderWidgetHost*>(iter.GetCurrentValue()); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 125 | DCHECK(widget); |
| 126 | if (!widget || !widget->IsRenderView()) |
| 127 | continue; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 129 | const RenderViewHost* host = static_cast<const RenderViewHost*>(widget); |
[email protected] | 6d53eb2 | 2009-02-13 20:48:39 | [diff] [blame] | 130 | TabContents* contents = NULL; |
| 131 | if (host->delegate()) |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 132 | contents = host->delegate()->GetAsTabContents(); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 133 | if (!contents) |
| 134 | continue; |
[email protected] | 4c4d8d2 | 2009-03-04 05:29:27 | [diff] [blame] | 135 | std::wstring title = UTF16ToWideHack(contents->GetTitle()); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 136 | if (!title.length()) |
| 137 | title = L"Untitled"; |
| 138 | process.titles.push_back(title); |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 139 | |
[email protected] | ebe89e06 | 2009-08-13 23:16:54 | [diff] [blame] | 140 | // We need to check the pending entry as well as the virtual_url to |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 141 | // see if it's an about:memory URL (we don't want to count these in the |
| 142 | // total memory usage of the browser). |
| 143 | // |
| 144 | // When we reach here, about:memory will be the pending entry since we |
| 145 | // haven't responded with any data such that it would be committed. If |
| 146 | // you have another about:memory tab open (which would be committed), |
| 147 | // we don't want to count it either, so we also check the last committed |
| 148 | // entry. |
| 149 | // |
| 150 | // Either the pending or last committed entries can be NULL. |
[email protected] | 6df4074 | 2009-03-19 22:24:50 | [diff] [blame] | 151 | const NavigationEntry* pending_entry = |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 152 | contents->controller().pending_entry(); |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 153 | const NavigationEntry* last_committed_entry = |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 154 | contents->controller().GetLastCommittedEntry(); |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 155 | if ((last_committed_entry && |
[email protected] | ebe89e06 | 2009-08-13 23:16:54 | [diff] [blame] | 156 | LowerCaseEqualsASCII(last_committed_entry->virtual_url().spec(), |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 157 | chrome::kAboutMemoryURL)) || |
| 158 | (pending_entry && |
[email protected] | ebe89e06 | 2009-08-13 23:16:54 | [diff] [blame] | 159 | LowerCaseEqualsASCII(pending_entry->virtual_url().spec(), |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 160 | chrome::kAboutMemoryURL))) |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 161 | process.is_diagnostics = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | } |
| 163 | } |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 164 | |
| 165 | #if defined(OS_LINUX) |
| 166 | if (process.pid == zygote_pid) { |
| 167 | process.type = ChildProcessInfo::ZYGOTE_PROCESS; |
| 168 | } else if (process.pid == sandbox_helper_pid) { |
| 169 | process.type = ChildProcessInfo::SANDBOX_HELPER_PROCESS; |
| 170 | } |
| 171 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 174 | // Get rid of other Chrome processes that are from a different profile. |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 175 | for (size_t index = 0; index < chrome_browser->processes.size(); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 176 | index++) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 177 | if (chrome_browser->processes[index].type == |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 178 | ChildProcessInfo::UNKNOWN_PROCESS) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 179 | chrome_browser->processes.erase( |
| 180 | chrome_browser->processes.begin() + index); |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 181 | index--; |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | UpdateHistograms(); |
| 186 | |
| 187 | OnDetailsAvailable(); |
| 188 | } |
| 189 | |
| 190 | void MemoryDetails::UpdateHistograms() { |
| 191 | // Reports a set of memory metrics to UMA. |
[email protected] | 57c4b85 | 2009-08-17 21:59:29 | [diff] [blame] | 192 | // Memory is measured in KB. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 194 | const ProcessData& browser = *ChromeBrowser(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 195 | size_t aggregate_memory = 0; |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 196 | int plugin_count = 0; |
| 197 | int worker_count = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 198 | for (size_t index = 0; index < browser.processes.size(); index++) { |
[email protected] | 921cd0cc | 2008-10-21 22:30:55 | [diff] [blame] | 199 | int sample = static_cast<int>(browser.processes[index].working_set.priv); |
| 200 | aggregate_memory += sample; |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 201 | switch (browser.processes[index].type) { |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 202 | case ChildProcessInfo::BROWSER_PROCESS: |
| 203 | UMA_HISTOGRAM_MEMORY_KB("Memory.Browser", sample); |
| 204 | break; |
| 205 | case ChildProcessInfo::RENDER_PROCESS: |
| 206 | UMA_HISTOGRAM_MEMORY_KB("Memory.Renderer", sample); |
| 207 | break; |
| 208 | case ChildProcessInfo::PLUGIN_PROCESS: |
| 209 | UMA_HISTOGRAM_MEMORY_KB("Memory.Plugin", sample); |
| 210 | plugin_count++; |
| 211 | break; |
| 212 | case ChildProcessInfo::WORKER_PROCESS: |
| 213 | UMA_HISTOGRAM_MEMORY_KB("Memory.Worker", sample); |
| 214 | worker_count++; |
| 215 | break; |
| 216 | case ChildProcessInfo::UTILITY_PROCESS: |
| 217 | UMA_HISTOGRAM_MEMORY_KB("Memory.Utility", sample); |
| 218 | break; |
| 219 | case ChildProcessInfo::ZYGOTE_PROCESS: |
| 220 | UMA_HISTOGRAM_MEMORY_KB("Memory.Zygote", sample); |
| 221 | break; |
| 222 | case ChildProcessInfo::SANDBOX_HELPER_PROCESS: |
| 223 | UMA_HISTOGRAM_MEMORY_KB("Memory.SandboxHelper", sample); |
| 224 | break; |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 225 | case ChildProcessInfo::NACL_LOADER_PROCESS: |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 226 | UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClient", sample); |
| 227 | break; |
[email protected] | aef8d5ae | 2010-03-17 22:40:52 | [diff] [blame] | 228 | case ChildProcessInfo::NACL_BROKER_PROCESS: |
| 229 | UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClientBroker", sample); |
| 230 | break; |
[email protected] | 96fcbf2d | 2010-08-03 16:10:27 | [diff] [blame] | 231 | case ChildProcessInfo::GPU_PROCESS: |
| 232 | UMA_HISTOGRAM_MEMORY_KB("Memory.Gpu", sample); |
| 233 | break; |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 234 | default: |
| 235 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | } |
| 237 | } |
[email protected] | 57c4b85 | 2009-08-17 21:59:29 | [diff] [blame] | 238 | UMA_HISTOGRAM_MEMORY_KB("Memory.BackingStore", |
| 239 | BackingStoreManager::MemorySize() / 1024); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 240 | |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 241 | UMA_HISTOGRAM_COUNTS_100("Memory.ProcessCount", |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 242 | static_cast<int>(browser.processes.size())); |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 243 | UMA_HISTOGRAM_COUNTS_100("Memory.PluginProcessCount", plugin_count); |
| 244 | UMA_HISTOGRAM_COUNTS_100("Memory.WorkerProcessCount", worker_count); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 245 | // TODO(viettrungluu): Do we want separate counts for the other |
| 246 | // (platform-specific) process types? |
[email protected] | 921cd0cc | 2008-10-21 22:30:55 | [diff] [blame] | 247 | |
| 248 | int total_sample = static_cast<int>(aggregate_memory / 1000); |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 249 | UMA_HISTOGRAM_MEMORY_MB("Memory.Total", total_sample); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | } |