blob: cd72b258cef458972966dcf362ae3aba6c58aace [file] [log] [blame]
[email protected]8a34e6602010-10-02 17:29:431// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/memory_details.h"
initial.commit09911bf2008-07-26 23:55:296
initial.commit09911bf2008-07-26 23:55:297#include "base/file_version_info.h"
[email protected]835d7c82010-10-14 04:38:388#include "base/metrics/histogram.h"
[email protected]76543b92009-08-31 17:27:459#include "base/process_util.h"
[email protected]33b9df42010-07-29 06:46:3910#include "base/string_util.h"
[email protected]64048bd2010-03-08 23:28:5811#include "base/utf_string_conversions.h"
[email protected]d27893f62010-07-03 05:47:4212#include "chrome/browser/browser_child_process_host.h"
[email protected]e36717272010-10-12 12:07:1313#include "chrome/browser/browser_thread.h"
[email protected]57c4b852009-08-17 21:59:2914#include "chrome/browser/renderer_host/backing_store_manager.h"
[email protected]8c8657d62009-01-16 18:31:2615#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1616#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]cd3d7892009-03-04 23:55:0617#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]57c6a652009-05-04 07:58:3418#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]cd3d7892009-03-04 23:55:0619#include "chrome/common/url_constants.h"
[email protected]ae5ca892009-07-30 18:00:4720#include "grit/chromium_strings.h"
initial.commit09911bf2008-07-26 23:55:2921
[email protected]54fd1d32009-09-01 00:12:5822#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.commit09911bf2008-07-26 23:55:2926
[email protected]8e383412010-10-19 16:57:0327ProcessMemoryInformation::ProcessMemoryInformation()
28 : pid(0),
29 num_processes(0),
30 is_diagnostics(false),
31 type(ChildProcessInfo::UNKNOWN_PROCESS) {
32}
33
34ProcessMemoryInformation::~ProcessMemoryInformation() {}
35
[email protected]93aa89c72010-10-20 21:32:0436ProcessData::ProcessData() {}
37
38ProcessData::ProcessData(const ProcessData& rhs)
39 : name(rhs.name),
40 process_name(rhs.process_name),
41 processes(rhs.processes) {
42}
43
44ProcessData::~ProcessData() {}
45
46ProcessData& ProcessData::operator=(const ProcessData& rhs) {
47 name = rhs.name;
48 process_name = rhs.process_name;
49 processes = rhs.processes;
50 return *this;
51}
52
initial.commit09911bf2008-07-26 23:55:2953// About threading:
54//
55// This operation will hit no fewer than 3 threads.
56//
[email protected]a436d922009-02-13 23:16:4257// The ChildProcessInfo::Iterator can only be accessed from the IO thread.
initial.commit09911bf2008-07-26 23:55:2958//
59// The RenderProcessHostIterator can only be accessed from the UI thread.
60//
61// This operation can take 30-100ms to complete. We never want to have
62// one task run for that long on the UI or IO threads. So, we run the
63// expensive parts of this operation over on the file thread.
64//
initial.commit09911bf2008-07-26 23:55:2965void MemoryDetails::StartFetch() {
[email protected]f8b3ef82010-10-11 02:45:5266 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::IO));
67 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE));
initial.commit09911bf2008-07-26 23:55:2968
69 // In order to process this request, we need to use the plugin information.
70 // However, plugin process information is only available from the IO thread.
[email protected]f8b3ef82010-10-11 02:45:5271 BrowserThread::PostTask(
72 BrowserThread::IO, FROM_HERE,
[email protected]a27a9382009-02-11 23:55:1073 NewRunnableMethod(this, &MemoryDetails::CollectChildInfoOnIOThread));
initial.commit09911bf2008-07-26 23:55:2974}
75
[email protected]8e383412010-10-19 16:57:0376MemoryDetails::~MemoryDetails() {}
77
[email protected]a27a9382009-02-11 23:55:1078void MemoryDetails::CollectChildInfoOnIOThread() {
[email protected]f8b3ef82010-10-11 02:45:5279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
initial.commit09911bf2008-07-26 23:55:2980
[email protected]a27a9382009-02-11 23:55:1081 std::vector<ProcessMemoryInformation> child_info;
82
[email protected]a436d922009-02-13 23:16:4283 // Collect the list of child processes.
[email protected]d27893f62010-07-03 05:47:4284 for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) {
[email protected]a27a9382009-02-11 23:55:1085 ProcessMemoryInformation info;
[email protected]76543b92009-08-31 17:27:4586 info.pid = base::GetProcId(iter->handle());
[email protected]a27a9382009-02-11 23:55:1087 if (!info.pid)
88 continue;
89
[email protected]a436d922009-02-13 23:16:4290 info.type = iter->type();
91 info.titles.push_back(iter->name());
[email protected]a27a9382009-02-11 23:55:1092 child_info.push_back(info);
initial.commit09911bf2008-07-26 23:55:2993 }
94
95 // Now go do expensive memory lookups from the file thread.
[email protected]f8b3ef82010-10-11 02:45:5296 BrowserThread::PostTask(
97 BrowserThread::FILE, FROM_HERE,
[email protected]a27a9382009-02-11 23:55:1098 NewRunnableMethod(this, &MemoryDetails::CollectProcessData, child_info));
initial.commit09911bf2008-07-26 23:55:2999}
100
[email protected]a27a9382009-02-11 23:55:10101void MemoryDetails::CollectChildInfoOnUIThread() {
[email protected]f8b3ef82010-10-11 02:45:52102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29103
[email protected]54fd1d32009-09-01 00:12:58104#if defined(OS_LINUX)
[email protected]d3c6c0d72010-12-09 08:15:04105 const pid_t zygote_pid = ZygoteHost::GetInstance()->pid();
106 const pid_t sandbox_helper_pid = RenderSandboxHostLinux::GetInstance()->pid();
[email protected]54fd1d32009-09-01 00:12:58107#endif
108
109 ProcessData* const chrome_browser = ChromeBrowser();
[email protected]a27a9382009-02-11 23:55:10110 // Get more information about the process.
[email protected]54fd1d32009-09-01 00:12:58111 for (size_t index = 0; index < chrome_browser->processes.size();
initial.commit09911bf2008-07-26 23:55:29112 index++) {
[email protected]a27a9382009-02-11 23:55:10113 // 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
115 // pages (e.g. "about:xxx" URLs). Iterate the RenderProcessHosts to find
[email protected]cd3d7892009-03-04 23:55:06116 // the tab contents.
[email protected]54fd1d32009-09-01 00:12:58117 ProcessMemoryInformation& process =
118 chrome_browser->processes[index];
119
[email protected]019191a2009-10-02 20:37:27120 for (RenderProcessHost::iterator renderer_iter(
121 RenderProcessHost::AllHostsIterator()); !renderer_iter.IsAtEnd();
122 renderer_iter.Advance()) {
[email protected]9de09f82009-08-17 20:13:53123 DCHECK(renderer_iter.GetCurrentValue());
[email protected]8a34e6602010-10-02 17:29:43124 // Ignore processes that don't have a connection, such as crashed tabs.
[email protected]062fdf12010-02-18 23:54:54125 if (!renderer_iter.GetCurrentValue()->HasConnection() || process.pid !=
[email protected]201b2732009-11-13 18:57:46126 base::GetProcId(renderer_iter.GetCurrentValue()->GetHandle())) {
[email protected]a27a9382009-02-11 23:55:10127 continue;
[email protected]201b2732009-11-13 18:57:46128 }
[email protected]a27a9382009-02-11 23:55:10129 process.type = ChildProcessInfo::RENDER_PROCESS;
130 // The RenderProcessHost may host multiple TabContents. Any
131 // of them which contain diagnostics information make the whole
132 // process be considered a diagnostics process.
133 //
134 // NOTE: This is a bit dangerous. We know that for now, listeners
135 // are always RenderWidgetHosts. But in theory, they don't
136 // have to be.
[email protected]9de09f82009-08-17 20:13:53137 RenderProcessHost::listeners_iterator iter(
138 renderer_iter.GetCurrentValue()->ListenersIterator());
139 for (; !iter.IsAtEnd(); iter.Advance()) {
140 const RenderWidgetHost* widget =
141 static_cast<const RenderWidgetHost*>(iter.GetCurrentValue());
[email protected]a27a9382009-02-11 23:55:10142 DCHECK(widget);
143 if (!widget || !widget->IsRenderView())
144 continue;
initial.commit09911bf2008-07-26 23:55:29145
[email protected]9de09f82009-08-17 20:13:53146 const RenderViewHost* host = static_cast<const RenderViewHost*>(widget);
[email protected]6d53eb22009-02-13 20:48:39147 TabContents* contents = NULL;
148 if (host->delegate())
[email protected]57c6a652009-05-04 07:58:34149 contents = host->delegate()->GetAsTabContents();
[email protected]a27a9382009-02-11 23:55:10150 if (!contents)
151 continue;
[email protected]4c4d8d22009-03-04 05:29:27152 std::wstring title = UTF16ToWideHack(contents->GetTitle());
[email protected]a27a9382009-02-11 23:55:10153 if (!title.length())
154 title = L"Untitled";
155 process.titles.push_back(title);
[email protected]cd3d7892009-03-04 23:55:06156
[email protected]ebe89e062009-08-13 23:16:54157 // We need to check the pending entry as well as the virtual_url to
[email protected]cd3d7892009-03-04 23:55:06158 // see if it's an about:memory URL (we don't want to count these in the
159 // total memory usage of the browser).
160 //
161 // When we reach here, about:memory will be the pending entry since we
162 // haven't responded with any data such that it would be committed. If
163 // you have another about:memory tab open (which would be committed),
164 // we don't want to count it either, so we also check the last committed
165 // entry.
166 //
167 // Either the pending or last committed entries can be NULL.
[email protected]6df40742009-03-19 22:24:50168 const NavigationEntry* pending_entry =
[email protected]ce3fa3c2009-04-20 19:55:57169 contents->controller().pending_entry();
[email protected]cd3d7892009-03-04 23:55:06170 const NavigationEntry* last_committed_entry =
[email protected]ce3fa3c2009-04-20 19:55:57171 contents->controller().GetLastCommittedEntry();
[email protected]cd3d7892009-03-04 23:55:06172 if ((last_committed_entry &&
[email protected]ebe89e062009-08-13 23:16:54173 LowerCaseEqualsASCII(last_committed_entry->virtual_url().spec(),
[email protected]cd3d7892009-03-04 23:55:06174 chrome::kAboutMemoryURL)) ||
175 (pending_entry &&
[email protected]ebe89e062009-08-13 23:16:54176 LowerCaseEqualsASCII(pending_entry->virtual_url().spec(),
[email protected]cd3d7892009-03-04 23:55:06177 chrome::kAboutMemoryURL)))
[email protected]a27a9382009-02-11 23:55:10178 process.is_diagnostics = true;
initial.commit09911bf2008-07-26 23:55:29179 }
180 }
[email protected]54fd1d32009-09-01 00:12:58181
182#if defined(OS_LINUX)
183 if (process.pid == zygote_pid) {
184 process.type = ChildProcessInfo::ZYGOTE_PROCESS;
185 } else if (process.pid == sandbox_helper_pid) {
186 process.type = ChildProcessInfo::SANDBOX_HELPER_PROCESS;
187 }
188#endif
initial.commit09911bf2008-07-26 23:55:29189 }
190
[email protected]a27a9382009-02-11 23:55:10191 // Get rid of other Chrome processes that are from a different profile.
[email protected]54fd1d32009-09-01 00:12:58192 for (size_t index = 0; index < chrome_browser->processes.size();
[email protected]a27a9382009-02-11 23:55:10193 index++) {
[email protected]54fd1d32009-09-01 00:12:58194 if (chrome_browser->processes[index].type ==
[email protected]a27a9382009-02-11 23:55:10195 ChildProcessInfo::UNKNOWN_PROCESS) {
[email protected]54fd1d32009-09-01 00:12:58196 chrome_browser->processes.erase(
197 chrome_browser->processes.begin() + index);
[email protected]a436d922009-02-13 23:16:42198 index--;
[email protected]a27a9382009-02-11 23:55:10199 }
200 }
201
initial.commit09911bf2008-07-26 23:55:29202 UpdateHistograms();
203
204 OnDetailsAvailable();
205}
206
207void MemoryDetails::UpdateHistograms() {
208 // Reports a set of memory metrics to UMA.
[email protected]57c4b852009-08-17 21:59:29209 // Memory is measured in KB.
initial.commit09911bf2008-07-26 23:55:29210
[email protected]54fd1d32009-09-01 00:12:58211 const ProcessData& browser = *ChromeBrowser();
initial.commit09911bf2008-07-26 23:55:29212 size_t aggregate_memory = 0;
[email protected]a27a9382009-02-11 23:55:10213 int plugin_count = 0;
214 int worker_count = 0;
initial.commit09911bf2008-07-26 23:55:29215 for (size_t index = 0; index < browser.processes.size(); index++) {
[email protected]921cd0cc2008-10-21 22:30:55216 int sample = static_cast<int>(browser.processes[index].working_set.priv);
217 aggregate_memory += sample;
[email protected]a27a9382009-02-11 23:55:10218 switch (browser.processes[index].type) {
[email protected]f164cea2009-11-05 23:37:40219 case ChildProcessInfo::BROWSER_PROCESS:
220 UMA_HISTOGRAM_MEMORY_KB("Memory.Browser", sample);
221 break;
222 case ChildProcessInfo::RENDER_PROCESS:
223 UMA_HISTOGRAM_MEMORY_KB("Memory.Renderer", sample);
224 break;
225 case ChildProcessInfo::PLUGIN_PROCESS:
226 UMA_HISTOGRAM_MEMORY_KB("Memory.Plugin", sample);
227 plugin_count++;
228 break;
229 case ChildProcessInfo::WORKER_PROCESS:
230 UMA_HISTOGRAM_MEMORY_KB("Memory.Worker", sample);
231 worker_count++;
232 break;
233 case ChildProcessInfo::UTILITY_PROCESS:
234 UMA_HISTOGRAM_MEMORY_KB("Memory.Utility", sample);
235 break;
236 case ChildProcessInfo::ZYGOTE_PROCESS:
237 UMA_HISTOGRAM_MEMORY_KB("Memory.Zygote", sample);
238 break;
239 case ChildProcessInfo::SANDBOX_HELPER_PROCESS:
240 UMA_HISTOGRAM_MEMORY_KB("Memory.SandboxHelper", sample);
241 break;
[email protected]103607e2010-02-01 18:57:09242 case ChildProcessInfo::NACL_LOADER_PROCESS:
[email protected]f164cea2009-11-05 23:37:40243 UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClient", sample);
244 break;
[email protected]aef8d5ae2010-03-17 22:40:52245 case ChildProcessInfo::NACL_BROKER_PROCESS:
246 UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClientBroker", sample);
247 break;
[email protected]96fcbf2d2010-08-03 16:10:27248 case ChildProcessInfo::GPU_PROCESS:
249 UMA_HISTOGRAM_MEMORY_KB("Memory.Gpu", sample);
250 break;
[email protected]f164cea2009-11-05 23:37:40251 default:
252 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29253 }
254 }
[email protected]57c4b852009-08-17 21:59:29255 UMA_HISTOGRAM_MEMORY_KB("Memory.BackingStore",
256 BackingStoreManager::MemorySize() / 1024);
[email protected]a27a9382009-02-11 23:55:10257
[email protected]553dba62009-02-24 19:08:23258 UMA_HISTOGRAM_COUNTS_100("Memory.ProcessCount",
initial.commit09911bf2008-07-26 23:55:29259 static_cast<int>(browser.processes.size()));
[email protected]553dba62009-02-24 19:08:23260 UMA_HISTOGRAM_COUNTS_100("Memory.PluginProcessCount", plugin_count);
261 UMA_HISTOGRAM_COUNTS_100("Memory.WorkerProcessCount", worker_count);
[email protected]f164cea2009-11-05 23:37:40262 // TODO(viettrungluu): Do we want separate counts for the other
263 // (platform-specific) process types?
[email protected]921cd0cc2008-10-21 22:30:55264
265 int total_sample = static_cast<int>(aggregate_memory / 1000);
[email protected]553dba62009-02-24 19:08:23266 UMA_HISTOGRAM_MEMORY_MB("Memory.Total", total_sample);
initial.commit09911bf2008-07-26 23:55:29267}