blob: 569cbbcb10b4a4d351077bdc36a6578250d44caf [file] [log] [blame]
[email protected]76543b92009-08-31 17:27:451// Copyright (c) 2009 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]5097dc82010-07-15 17:23:238#include "base/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]dcccb942009-02-01 18:23:0013#include "chrome/browser/chrome_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
27// About threading:
28//
29// This operation will hit no fewer than 3 threads.
30//
[email protected]a436d922009-02-13 23:16:4231// The ChildProcessInfo::Iterator can only be accessed from the IO thread.
initial.commit09911bf2008-07-26 23:55:2932//
33// The RenderProcessHostIterator can only be accessed from the UI thread.
34//
35// This operation can take 30-100ms to complete. We never want to have
36// one task run for that long on the UI or IO threads. So, we run the
37// expensive parts of this operation over on the file thread.
38//
39
initial.commit09911bf2008-07-26 23:55:2940void MemoryDetails::StartFetch() {
[email protected]6fad2632009-11-02 05:59:3741 DCHECK(!ChromeThread::CurrentlyOn(ChromeThread::IO));
42 DCHECK(!ChromeThread::CurrentlyOn(ChromeThread::FILE));
initial.commit09911bf2008-07-26 23:55:2943
44 // In order to process this request, we need to use the plugin information.
45 // However, plugin process information is only available from the IO thread.
[email protected]6fad2632009-11-02 05:59:3746 ChromeThread::PostTask(
47 ChromeThread::IO, FROM_HERE,
[email protected]a27a9382009-02-11 23:55:1048 NewRunnableMethod(this, &MemoryDetails::CollectChildInfoOnIOThread));
initial.commit09911bf2008-07-26 23:55:2949}
50
[email protected]a27a9382009-02-11 23:55:1051void MemoryDetails::CollectChildInfoOnIOThread() {
[email protected]d85cf072009-10-27 03:59:3152 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
initial.commit09911bf2008-07-26 23:55:2953
[email protected]a27a9382009-02-11 23:55:1054 std::vector<ProcessMemoryInformation> child_info;
55
[email protected]a436d922009-02-13 23:16:4256 // Collect the list of child processes.
[email protected]d27893f62010-07-03 05:47:4257 for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) {
[email protected]a27a9382009-02-11 23:55:1058 ProcessMemoryInformation info;
[email protected]76543b92009-08-31 17:27:4559 info.pid = base::GetProcId(iter->handle());
[email protected]a27a9382009-02-11 23:55:1060 if (!info.pid)
61 continue;
62
[email protected]a436d922009-02-13 23:16:4263 info.type = iter->type();
64 info.titles.push_back(iter->name());
[email protected]a27a9382009-02-11 23:55:1065 child_info.push_back(info);
initial.commit09911bf2008-07-26 23:55:2966 }
67
68 // Now go do expensive memory lookups from the file thread.
[email protected]d85cf072009-10-27 03:59:3169 ChromeThread::PostTask(
70 ChromeThread::FILE, FROM_HERE,
[email protected]a27a9382009-02-11 23:55:1071 NewRunnableMethod(this, &MemoryDetails::CollectProcessData, child_info));
initial.commit09911bf2008-07-26 23:55:2972}
73
[email protected]a27a9382009-02-11 23:55:1074void MemoryDetails::CollectChildInfoOnUIThread() {
[email protected]6fad2632009-11-02 05:59:3775 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
initial.commit09911bf2008-07-26 23:55:2976
[email protected]54fd1d32009-09-01 00:12:5877#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]a27a9382009-02-11 23:55:1083 // Get more information about the process.
[email protected]54fd1d32009-09-01 00:12:5884 for (size_t index = 0; index < chrome_browser->processes.size();
initial.commit09911bf2008-07-26 23:55:2985 index++) {
[email protected]a27a9382009-02-11 23:55:1086 // 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]cd3d7892009-03-04 23:55:0689 // the tab contents.
[email protected]54fd1d32009-09-01 00:12:5890 ProcessMemoryInformation& process =
91 chrome_browser->processes[index];
92
[email protected]019191a2009-10-02 20:37:2793 for (RenderProcessHost::iterator renderer_iter(
94 RenderProcessHost::AllHostsIterator()); !renderer_iter.IsAtEnd();
95 renderer_iter.Advance()) {
[email protected]9de09f82009-08-17 20:13:5396 DCHECK(renderer_iter.GetCurrentValue());
[email protected]062fdf12010-02-18 23:54:5497 // Ignore processes that don't have a connection, such as crashed tabs or
98 // phantom tabs.
99 if (!renderer_iter.GetCurrentValue()->HasConnection() || process.pid !=
[email protected]201b2732009-11-13 18:57:46100 base::GetProcId(renderer_iter.GetCurrentValue()->GetHandle())) {
[email protected]a27a9382009-02-11 23:55:10101 continue;
[email protected]201b2732009-11-13 18:57:46102 }
[email protected]a27a9382009-02-11 23:55:10103 process.type = ChildProcessInfo::RENDER_PROCESS;
104 // The RenderProcessHost may host multiple TabContents. Any
105 // of them which contain diagnostics information make the whole
106 // process be considered a diagnostics process.
107 //
108 // NOTE: This is a bit dangerous. We know that for now, listeners
109 // are always RenderWidgetHosts. But in theory, they don't
110 // have to be.
[email protected]9de09f82009-08-17 20:13:53111 RenderProcessHost::listeners_iterator iter(
112 renderer_iter.GetCurrentValue()->ListenersIterator());
113 for (; !iter.IsAtEnd(); iter.Advance()) {
114 const RenderWidgetHost* widget =
115 static_cast<const RenderWidgetHost*>(iter.GetCurrentValue());
[email protected]a27a9382009-02-11 23:55:10116 DCHECK(widget);
117 if (!widget || !widget->IsRenderView())
118 continue;
initial.commit09911bf2008-07-26 23:55:29119
[email protected]9de09f82009-08-17 20:13:53120 const RenderViewHost* host = static_cast<const RenderViewHost*>(widget);
[email protected]6d53eb22009-02-13 20:48:39121 TabContents* contents = NULL;
122 if (host->delegate())
[email protected]57c6a652009-05-04 07:58:34123 contents = host->delegate()->GetAsTabContents();
[email protected]a27a9382009-02-11 23:55:10124 if (!contents)
125 continue;
[email protected]4c4d8d22009-03-04 05:29:27126 std::wstring title = UTF16ToWideHack(contents->GetTitle());
[email protected]a27a9382009-02-11 23:55:10127 if (!title.length())
128 title = L"Untitled";
129 process.titles.push_back(title);
[email protected]cd3d7892009-03-04 23:55:06130
[email protected]ebe89e062009-08-13 23:16:54131 // We need to check the pending entry as well as the virtual_url to
[email protected]cd3d7892009-03-04 23:55:06132 // see if it's an about:memory URL (we don't want to count these in the
133 // total memory usage of the browser).
134 //
135 // When we reach here, about:memory will be the pending entry since we
136 // haven't responded with any data such that it would be committed. If
137 // you have another about:memory tab open (which would be committed),
138 // we don't want to count it either, so we also check the last committed
139 // entry.
140 //
141 // Either the pending or last committed entries can be NULL.
[email protected]6df40742009-03-19 22:24:50142 const NavigationEntry* pending_entry =
[email protected]ce3fa3c2009-04-20 19:55:57143 contents->controller().pending_entry();
[email protected]cd3d7892009-03-04 23:55:06144 const NavigationEntry* last_committed_entry =
[email protected]ce3fa3c2009-04-20 19:55:57145 contents->controller().GetLastCommittedEntry();
[email protected]cd3d7892009-03-04 23:55:06146 if ((last_committed_entry &&
[email protected]ebe89e062009-08-13 23:16:54147 LowerCaseEqualsASCII(last_committed_entry->virtual_url().spec(),
[email protected]cd3d7892009-03-04 23:55:06148 chrome::kAboutMemoryURL)) ||
149 (pending_entry &&
[email protected]ebe89e062009-08-13 23:16:54150 LowerCaseEqualsASCII(pending_entry->virtual_url().spec(),
[email protected]cd3d7892009-03-04 23:55:06151 chrome::kAboutMemoryURL)))
[email protected]a27a9382009-02-11 23:55:10152 process.is_diagnostics = true;
initial.commit09911bf2008-07-26 23:55:29153 }
154 }
[email protected]54fd1d32009-09-01 00:12:58155
156#if defined(OS_LINUX)
157 if (process.pid == zygote_pid) {
158 process.type = ChildProcessInfo::ZYGOTE_PROCESS;
159 } else if (process.pid == sandbox_helper_pid) {
160 process.type = ChildProcessInfo::SANDBOX_HELPER_PROCESS;
161 }
162#endif
initial.commit09911bf2008-07-26 23:55:29163 }
164
[email protected]a27a9382009-02-11 23:55:10165 // Get rid of other Chrome processes that are from a different profile.
[email protected]54fd1d32009-09-01 00:12:58166 for (size_t index = 0; index < chrome_browser->processes.size();
[email protected]a27a9382009-02-11 23:55:10167 index++) {
[email protected]54fd1d32009-09-01 00:12:58168 if (chrome_browser->processes[index].type ==
[email protected]a27a9382009-02-11 23:55:10169 ChildProcessInfo::UNKNOWN_PROCESS) {
[email protected]54fd1d32009-09-01 00:12:58170 chrome_browser->processes.erase(
171 chrome_browser->processes.begin() + index);
[email protected]a436d922009-02-13 23:16:42172 index--;
[email protected]a27a9382009-02-11 23:55:10173 }
174 }
175
initial.commit09911bf2008-07-26 23:55:29176 UpdateHistograms();
177
178 OnDetailsAvailable();
179}
180
181void MemoryDetails::UpdateHistograms() {
182 // Reports a set of memory metrics to UMA.
[email protected]57c4b852009-08-17 21:59:29183 // Memory is measured in KB.
initial.commit09911bf2008-07-26 23:55:29184
[email protected]54fd1d32009-09-01 00:12:58185 const ProcessData& browser = *ChromeBrowser();
initial.commit09911bf2008-07-26 23:55:29186 size_t aggregate_memory = 0;
[email protected]a27a9382009-02-11 23:55:10187 int plugin_count = 0;
188 int worker_count = 0;
initial.commit09911bf2008-07-26 23:55:29189 for (size_t index = 0; index < browser.processes.size(); index++) {
[email protected]921cd0cc2008-10-21 22:30:55190 int sample = static_cast<int>(browser.processes[index].working_set.priv);
191 aggregate_memory += sample;
[email protected]a27a9382009-02-11 23:55:10192 switch (browser.processes[index].type) {
[email protected]f164cea2009-11-05 23:37:40193 case ChildProcessInfo::BROWSER_PROCESS:
194 UMA_HISTOGRAM_MEMORY_KB("Memory.Browser", sample);
195 break;
196 case ChildProcessInfo::RENDER_PROCESS:
197 UMA_HISTOGRAM_MEMORY_KB("Memory.Renderer", sample);
198 break;
199 case ChildProcessInfo::PLUGIN_PROCESS:
200 UMA_HISTOGRAM_MEMORY_KB("Memory.Plugin", sample);
201 plugin_count++;
202 break;
203 case ChildProcessInfo::WORKER_PROCESS:
204 UMA_HISTOGRAM_MEMORY_KB("Memory.Worker", sample);
205 worker_count++;
206 break;
207 case ChildProcessInfo::UTILITY_PROCESS:
208 UMA_HISTOGRAM_MEMORY_KB("Memory.Utility", sample);
209 break;
210 case ChildProcessInfo::ZYGOTE_PROCESS:
211 UMA_HISTOGRAM_MEMORY_KB("Memory.Zygote", sample);
212 break;
213 case ChildProcessInfo::SANDBOX_HELPER_PROCESS:
214 UMA_HISTOGRAM_MEMORY_KB("Memory.SandboxHelper", sample);
215 break;
[email protected]103607e2010-02-01 18:57:09216 case ChildProcessInfo::NACL_LOADER_PROCESS:
[email protected]f164cea2009-11-05 23:37:40217 UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClient", sample);
218 break;
[email protected]aef8d5ae2010-03-17 22:40:52219 case ChildProcessInfo::NACL_BROKER_PROCESS:
220 UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClientBroker", sample);
221 break;
[email protected]f164cea2009-11-05 23:37:40222 default:
223 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29224 }
225 }
[email protected]57c4b852009-08-17 21:59:29226 UMA_HISTOGRAM_MEMORY_KB("Memory.BackingStore",
227 BackingStoreManager::MemorySize() / 1024);
[email protected]a27a9382009-02-11 23:55:10228
[email protected]553dba62009-02-24 19:08:23229 UMA_HISTOGRAM_COUNTS_100("Memory.ProcessCount",
initial.commit09911bf2008-07-26 23:55:29230 static_cast<int>(browser.processes.size()));
[email protected]553dba62009-02-24 19:08:23231 UMA_HISTOGRAM_COUNTS_100("Memory.PluginProcessCount", plugin_count);
232 UMA_HISTOGRAM_COUNTS_100("Memory.WorkerProcessCount", worker_count);
[email protected]f164cea2009-11-05 23:37:40233 // TODO(viettrungluu): Do we want separate counts for the other
234 // (platform-specific) process types?
[email protected]921cd0cc2008-10-21 22:30:55235
236 int total_sample = static_cast<int>(aggregate_memory / 1000);
[email protected]553dba62009-02-24 19:08:23237 UMA_HISTOGRAM_MEMORY_MB("Memory.Total", total_sample);
initial.commit09911bf2008-07-26 23:55:29238}