Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / memory_details.cc
blob0d391d994098bd76558cbb04a83a273cf673c6a0
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/memory_details.h"
7 #include "base/bind.h"
8 #include "base/file_version_info.h"
9 #include "base/metrics/histogram.h"
10 #include "base/process_util.h"
11 #include "base/string_util.h"
12 #include "base/stringprintf.h"
13 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/extensions/extension_process_manager.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/view_type_utils.h"
18 #include "chrome/common/extensions/extension.h"
19 #include "chrome/common/url_constants.h"
20 #include "content/public/browser/browser_child_process_host_iterator.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/child_process_data.h"
23 #include "content/public/browser/navigation_controller.h"
24 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/render_process_host.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/common/bindings_policy.h"
29 #include "content/public/common/process_type.h"
30 #include "grit/chromium_strings.h"
31 #include "grit/generated_resources.h"
32 #include "ui/base/l10n/l10n_util.h"
34 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
35 #include "content/public/browser/zygote_host_linux.h"
36 #endif
38 using base::StringPrintf;
39 using content::BrowserChildProcessHostIterator;
40 using content::BrowserThread;
41 using content::NavigationEntry;
42 using content::RenderViewHost;
43 using content::RenderWidgetHost;
44 using content::WebContents;
45 using extensions::Extension;
47 // static
48 std::string ProcessMemoryInformation::GetRendererTypeNameInEnglish(
49 RendererProcessType type) {
50 switch (type) {
51 case RENDERER_NORMAL:
52 return "Tab";
53 case RENDERER_CHROME:
54 return "Tab (Chrome)";
55 case RENDERER_EXTENSION:
56 return "Extension";
57 case RENDERER_DEVTOOLS:
58 return "Devtools";
59 case RENDERER_INTERSTITIAL:
60 return "Interstitial";
61 case RENDERER_NOTIFICATION:
62 return "Notification";
63 case RENDERER_BACKGROUND_APP:
64 return "Background App";
65 case RENDERER_UNKNOWN:
66 default:
67 NOTREACHED() << "Unknown renderer process type!";
68 return "Unknown";
72 // static
73 std::string ProcessMemoryInformation::GetFullTypeNameInEnglish(
74 content::ProcessType type,
75 RendererProcessType rtype) {
76 if (type == content::PROCESS_TYPE_RENDERER)
77 return GetRendererTypeNameInEnglish(rtype);
78 return content::GetProcessTypeNameInEnglish(type);
81 ProcessMemoryInformation::ProcessMemoryInformation()
82 : pid(0),
83 num_processes(0),
84 is_diagnostics(false),
85 type(content::PROCESS_TYPE_UNKNOWN),
86 renderer_type(RENDERER_UNKNOWN) {
89 ProcessMemoryInformation::~ProcessMemoryInformation() {}
91 bool ProcessMemoryInformation::operator<(
92 const ProcessMemoryInformation& rhs) const {
93 return working_set.priv < rhs.working_set.priv;
96 ProcessData::ProcessData() {}
98 ProcessData::ProcessData(const ProcessData& rhs)
99 : name(rhs.name),
100 process_name(rhs.process_name),
101 processes(rhs.processes) {
104 ProcessData::~ProcessData() {}
106 ProcessData& ProcessData::operator=(const ProcessData& rhs) {
107 name = rhs.name;
108 process_name = rhs.process_name;
109 processes = rhs.processes;
110 return *this;
113 // About threading:
115 // This operation will hit no fewer than 3 threads.
117 // The BrowserChildProcessHostIterator can only be accessed from the IO thread.
119 // The RenderProcessHostIterator can only be accessed from the UI thread.
121 // This operation can take 30-100ms to complete. We never want to have
122 // one task run for that long on the UI or IO threads. So, we run the
123 // expensive parts of this operation over on the file thread.
125 void MemoryDetails::StartFetch(UserMetricsMode user_metrics_mode) {
126 // This might get called from the UI or FILE threads, but should not be
127 // getting called from the IO thread.
128 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::IO));
129 user_metrics_mode_ = user_metrics_mode;
131 // In order to process this request, we need to use the plugin information.
132 // However, plugin process information is only available from the IO thread.
133 BrowserThread::PostTask(
134 BrowserThread::IO, FROM_HERE,
135 base::Bind(&MemoryDetails::CollectChildInfoOnIOThread, this));
138 MemoryDetails::~MemoryDetails() {}
140 std::string MemoryDetails::ToLogString() {
141 std::string log;
142 log.reserve(4096);
143 ProcessMemoryInformationList processes = ChromeBrowser()->processes;
144 // Sort by memory consumption, low to high.
145 std::sort(processes.begin(), processes.end());
146 // Print from high to low.
147 for (ProcessMemoryInformationList::reverse_iterator iter1 =
148 processes.rbegin();
149 iter1 != processes.rend();
150 ++iter1) {
151 log += ProcessMemoryInformation::GetFullTypeNameInEnglish(
152 iter1->type, iter1->renderer_type);
153 if (!iter1->titles.empty()) {
154 log += " [";
155 for (std::vector<string16>::const_iterator iter2 =
156 iter1->titles.begin();
157 iter2 != iter1->titles.end(); ++iter2) {
158 if (iter2 != iter1->titles.begin())
159 log += "|";
160 log += UTF16ToUTF8(*iter2);
162 log += "]";
164 log += StringPrintf(" %d MB private, %d MB shared\n",
165 static_cast<int>(iter1->working_set.priv) / 1024,
166 static_cast<int>(iter1->working_set.shared) / 1024);
168 return log;
171 void MemoryDetails::CollectChildInfoOnIOThread() {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
174 std::vector<ProcessMemoryInformation> child_info;
176 // Collect the list of child processes. A 0 |handle| means that
177 // the process is being launched, so we skip it.
178 for (BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) {
179 ProcessMemoryInformation info;
180 if (!iter.GetData().handle)
181 continue;
182 info.pid = base::GetProcId(iter.GetData().handle);
183 if (!info.pid)
184 continue;
186 info.type = iter.GetData().type;
187 info.renderer_type = ProcessMemoryInformation::RENDERER_UNKNOWN;
188 info.titles.push_back(iter.GetData().name);
189 child_info.push_back(info);
192 // Now go do expensive memory lookups from the file thread.
193 BrowserThread::PostTask(
194 BrowserThread::FILE, FROM_HERE,
195 base::Bind(&MemoryDetails::CollectProcessData, this, child_info));
198 void MemoryDetails::CollectChildInfoOnUIThread() {
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
201 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
202 const pid_t zygote_pid = content::ZygoteHost::GetInstance()->GetPid();
203 const pid_t sandbox_helper_pid =
204 content::ZygoteHost::GetInstance()->GetSandboxHelperPid();
205 #endif
207 ProcessData* const chrome_browser = ChromeBrowser();
208 // Get more information about the process.
209 for (size_t index = 0; index < chrome_browser->processes.size();
210 index++) {
211 // Check if it's a renderer, if so get the list of page titles in it and
212 // check if it's a diagnostics-related process. We skip about:memory pages.
213 // Iterate the RenderProcessHosts to find the tab contents.
214 ProcessMemoryInformation& process =
215 chrome_browser->processes[index];
217 for (content::RenderProcessHost::iterator renderer_iter(
218 content::RenderProcessHost::AllHostsIterator());
219 !renderer_iter.IsAtEnd(); renderer_iter.Advance()) {
220 content::RenderProcessHost* render_process_host =
221 renderer_iter.GetCurrentValue();
222 DCHECK(render_process_host);
223 // Ignore processes that don't have a connection, such as crashed tabs.
224 if (!render_process_host->HasConnection() ||
225 process.pid != base::GetProcId(render_process_host->GetHandle())) {
226 continue;
228 process.type = content::PROCESS_TYPE_RENDERER;
229 Profile* profile =
230 Profile::FromBrowserContext(
231 render_process_host->GetBrowserContext());
232 ExtensionService* extension_service = profile->GetExtensionService();
233 extensions::ProcessMap* extension_process_map = NULL;
234 // No extensions on Android. So extension_service can be NULL.
235 if (extension_service)
236 extension_process_map = extension_service->process_map();
238 // The RenderProcessHost may host multiple WebContentses. Any
239 // of them which contain diagnostics information make the whole
240 // process be considered a diagnostics process.
241 content::RenderProcessHost::RenderWidgetHostsIterator iter(
242 render_process_host->GetRenderWidgetHostsIterator());
243 for (; !iter.IsAtEnd(); iter.Advance()) {
244 const RenderWidgetHost* widget = iter.GetCurrentValue();
245 DCHECK(widget);
246 if (!widget || !widget->IsRenderView())
247 continue;
249 RenderViewHost* host =
250 RenderViewHost::From(const_cast<RenderWidgetHost*>(widget));
251 WebContents* contents = WebContents::FromRenderViewHost(host);
252 GURL url;
253 if (contents)
254 url = contents->GetURL();
255 chrome::ViewType type = chrome::GetViewType(contents);
256 if (host->GetEnabledBindings() & content::BINDINGS_POLICY_WEB_UI) {
257 process.renderer_type = ProcessMemoryInformation::RENDERER_CHROME;
258 } else if (extension_process_map &&
259 extension_process_map->Contains(host->GetProcess()->GetID())) {
260 // For our purposes, don't count processes containing only hosted apps
261 // as extension processes. See also: crbug.com/102533.
262 std::set<std::string> extension_ids =
263 extension_process_map->GetExtensionsInProcess(
264 host->GetProcess()->GetID());
265 for (std::set<std::string>::iterator iter = extension_ids.begin();
266 iter != extension_ids.end(); ++iter) {
267 const Extension* extension =
268 extension_service->GetExtensionById(*iter, false);
269 if (extension && !extension->is_hosted_app()) {
270 process.renderer_type =
271 ProcessMemoryInformation::RENDERER_EXTENSION;
272 break;
276 if (extension_process_map &&
277 extension_process_map->Contains(host->GetProcess()->GetID())) {
278 const Extension* extension =
279 extension_service->extensions()->GetByID(url.host());
280 if (extension) {
281 string16 title = UTF8ToUTF16(extension->name());
282 process.titles.push_back(title);
283 process.renderer_type =
284 ProcessMemoryInformation::RENDERER_EXTENSION;
285 continue;
289 if (!contents) {
290 process.renderer_type =
291 ProcessMemoryInformation::RENDERER_INTERSTITIAL;
292 continue;
295 if (type == chrome::VIEW_TYPE_BACKGROUND_CONTENTS) {
296 process.titles.push_back(UTF8ToUTF16(url.spec()));
297 process.renderer_type =
298 ProcessMemoryInformation::RENDERER_BACKGROUND_APP;
299 continue;
302 if (type == chrome::VIEW_TYPE_NOTIFICATION) {
303 process.titles.push_back(UTF8ToUTF16(url.spec()));
304 process.renderer_type =
305 ProcessMemoryInformation::RENDERER_NOTIFICATION;
306 continue;
309 // Since we have a WebContents and and the renderer type hasn't been
310 // set yet, it must be a normal tabbed renderer.
311 if (process.renderer_type == ProcessMemoryInformation::RENDERER_UNKNOWN)
312 process.renderer_type = ProcessMemoryInformation::RENDERER_NORMAL;
314 string16 title = contents->GetTitle();
315 if (!title.length())
316 title = l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
317 process.titles.push_back(title);
319 // We need to check the pending entry as well as the virtual_url to
320 // see if it's a chrome://memory URL (we don't want to count these in
321 // the total memory usage of the browser).
323 // When we reach here, chrome://memory will be the pending entry since
324 // we haven't responded with any data such that it would be committed.
325 // If you have another chrome://memory tab open (which would be
326 // committed), we don't want to count it either, so we also check the
327 // last committed entry.
329 // Either the pending or last committed entries can be NULL.
330 const NavigationEntry* pending_entry =
331 contents->GetController().GetPendingEntry();
332 const NavigationEntry* last_committed_entry =
333 contents->GetController().GetLastCommittedEntry();
334 if ((last_committed_entry &&
335 LowerCaseEqualsASCII(last_committed_entry->GetVirtualURL().spec(),
336 chrome::kChromeUIMemoryURL)) ||
337 (pending_entry &&
338 LowerCaseEqualsASCII(pending_entry->GetVirtualURL().spec(),
339 chrome::kChromeUIMemoryURL)))
340 process.is_diagnostics = true;
344 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
345 if (process.pid == zygote_pid) {
346 process.type = content::PROCESS_TYPE_ZYGOTE;
347 } else if (process.pid == sandbox_helper_pid) {
348 process.type = content::PROCESS_TYPE_SANDBOX_HELPER;
350 #endif
353 // Get rid of other Chrome processes that are from a different profile.
354 for (size_t index = 0; index < chrome_browser->processes.size();
355 index++) {
356 if (chrome_browser->processes[index].type ==
357 content::PROCESS_TYPE_UNKNOWN) {
358 chrome_browser->processes.erase(
359 chrome_browser->processes.begin() + index);
360 index--;
364 if (user_metrics_mode_ == UPDATE_USER_METRICS)
365 UpdateHistograms();
367 OnDetailsAvailable();
370 void MemoryDetails::UpdateHistograms() {
371 // Reports a set of memory metrics to UMA.
372 // Memory is measured in KB.
374 const ProcessData& browser = *ChromeBrowser();
375 size_t aggregate_memory = 0;
376 int chrome_count = 0;
377 int extension_count = 0;
378 int plugin_count = 0;
379 int pepper_plugin_count = 0;
380 int pepper_plugin_broker_count = 0;
381 int renderer_count = 0;
382 int other_count = 0;
383 int worker_count = 0;
384 for (size_t index = 0; index < browser.processes.size(); index++) {
385 int sample = static_cast<int>(browser.processes[index].working_set.priv);
386 aggregate_memory += sample;
387 switch (browser.processes[index].type) {
388 case content::PROCESS_TYPE_BROWSER:
389 UMA_HISTOGRAM_MEMORY_KB("Memory.Browser", sample);
390 break;
391 case content::PROCESS_TYPE_RENDERER: {
392 ProcessMemoryInformation::RendererProcessType renderer_type =
393 browser.processes[index].renderer_type;
394 switch (renderer_type) {
395 case ProcessMemoryInformation::RENDERER_EXTENSION:
396 UMA_HISTOGRAM_MEMORY_KB("Memory.Extension", sample);
397 extension_count++;
398 break;
399 case ProcessMemoryInformation::RENDERER_CHROME:
400 UMA_HISTOGRAM_MEMORY_KB("Memory.Chrome", sample);
401 chrome_count++;
402 break;
403 case ProcessMemoryInformation::RENDERER_UNKNOWN:
404 NOTREACHED() << "Unknown renderer process type.";
405 break;
406 case ProcessMemoryInformation::RENDERER_NORMAL:
407 default:
408 // TODO(erikkay): Should we bother splitting out the other subtypes?
409 UMA_HISTOGRAM_MEMORY_KB("Memory.Renderer", sample);
410 renderer_count++;
411 break;
413 break;
415 case content::PROCESS_TYPE_PLUGIN:
416 UMA_HISTOGRAM_MEMORY_KB("Memory.Plugin", sample);
417 plugin_count++;
418 break;
419 case content::PROCESS_TYPE_WORKER:
420 UMA_HISTOGRAM_MEMORY_KB("Memory.Worker", sample);
421 worker_count++;
422 break;
423 case content::PROCESS_TYPE_UTILITY:
424 UMA_HISTOGRAM_MEMORY_KB("Memory.Utility", sample);
425 other_count++;
426 break;
427 case content::PROCESS_TYPE_ZYGOTE:
428 UMA_HISTOGRAM_MEMORY_KB("Memory.Zygote", sample);
429 other_count++;
430 break;
431 case content::PROCESS_TYPE_SANDBOX_HELPER:
432 UMA_HISTOGRAM_MEMORY_KB("Memory.SandboxHelper", sample);
433 other_count++;
434 break;
435 case content::PROCESS_TYPE_NACL_LOADER:
436 UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClient", sample);
437 other_count++;
438 break;
439 case content::PROCESS_TYPE_NACL_BROKER:
440 UMA_HISTOGRAM_MEMORY_KB("Memory.NativeClientBroker", sample);
441 other_count++;
442 break;
443 case content::PROCESS_TYPE_GPU:
444 UMA_HISTOGRAM_MEMORY_KB("Memory.Gpu", sample);
445 other_count++;
446 break;
447 case content::PROCESS_TYPE_PPAPI_PLUGIN:
448 UMA_HISTOGRAM_MEMORY_KB("Memory.PepperPlugin", sample);
449 pepper_plugin_count++;
450 break;
451 case content::PROCESS_TYPE_PPAPI_BROKER:
452 UMA_HISTOGRAM_MEMORY_KB("Memory.PepperPluginBroker", sample);
453 pepper_plugin_broker_count++;
454 break;
455 default:
456 NOTREACHED();
457 break;
460 UMA_HISTOGRAM_MEMORY_KB("Memory.BackingStore",
461 RenderWidgetHost::BackingStoreMemorySize() / 1024);
463 UMA_HISTOGRAM_COUNTS_100("Memory.ProcessCount",
464 static_cast<int>(browser.processes.size()));
465 UMA_HISTOGRAM_COUNTS_100("Memory.ChromeProcessCount", chrome_count);
466 UMA_HISTOGRAM_COUNTS_100("Memory.ExtensionProcessCount", extension_count);
467 UMA_HISTOGRAM_COUNTS_100("Memory.OtherProcessCount", other_count);
468 UMA_HISTOGRAM_COUNTS_100("Memory.PluginProcessCount", plugin_count);
469 UMA_HISTOGRAM_COUNTS_100("Memory.PepperPluginProcessCount",
470 pepper_plugin_count);
471 UMA_HISTOGRAM_COUNTS_100("Memory.PepperPluginBrokerProcessCount",
472 pepper_plugin_broker_count);
473 UMA_HISTOGRAM_COUNTS_100("Memory.RendererProcessCount", renderer_count);
474 UMA_HISTOGRAM_COUNTS_100("Memory.WorkerProcessCount", worker_count);
475 // TODO(viettrungluu): Do we want separate counts for the other
476 // (platform-specific) process types?
478 int total_sample = static_cast<int>(aggregate_memory / 1000);
479 UMA_HISTOGRAM_MEMORY_MB("Memory.Total", total_sample);