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