blob: b2554f86f08183b65a79d090c7612d570665322b [file] [log] [blame]
[email protected]24d69692011-10-21 18:26:511// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]f164cea2009-11-05 23:37:402// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/memory_details.h"
6
7#include <set>
8#include <string>
9
[email protected]f164cea2009-11-05 23:37:4010#include "base/basictypes.h"
[email protected]24d69692011-10-21 18:26:5111#include "base/bind.h"
[email protected]f164cea2009-11-05 23:37:4012#include "base/file_path.h"
13#include "base/file_version_info.h"
[email protected]0378bf42011-01-01 18:20:1414#include "base/mac/mac_util.h"
[email protected]f164cea2009-11-05 23:37:4015#include "base/process_util.h"
[email protected]c38831a12011-10-28 12:44:4916#include "base/string_util.h"
[email protected]34b99632011-01-01 01:01:0617#include "base/threading/thread.h"
[email protected]0211f57e2010-08-27 20:28:4218#include "base/utf_string_conversions.h"
[email protected]f164cea2009-11-05 23:37:4019#include "chrome/browser/process_info_snapshot.h"
[email protected]f164cea2009-11-05 23:37:4020#include "chrome/common/chrome_constants.h"
[email protected]1eeb5e02010-07-20 23:02:1121#include "chrome/common/chrome_version_info.h"
[email protected]f164cea2009-11-05 23:37:4022#include "chrome/common/url_constants.h"
[email protected]a01efd22011-03-01 00:38:3223#include "content/browser/browser_child_process_host.h"
[email protected]a01efd22011-03-01 00:38:3224#include "content/browser/renderer_host/backing_store_manager.h"
25#include "content/browser/renderer_host/render_process_host.h"
26#include "content/browser/tab_contents/navigation_entry.h"
[email protected]c38831a12011-10-28 12:44:4927#include "content/public/browser/browser_thread.h"
[email protected]f164cea2009-11-05 23:37:4028#include "grit/chromium_strings.h"
[email protected]c051a1b2011-01-21 23:30:1729#include "ui/base/l10n/l10n_util.h"
[email protected]f164cea2009-11-05 23:37:4030
[email protected]631bb742011-11-02 11:29:3931using content::BrowserThread;
32
[email protected]f164cea2009-11-05 23:37:4033// TODO(viettrungluu): Many of the TODOs below are subsumed by a general need to
34// refactor the about:memory code (not just on Mac, but probably on other
35// platforms as well). I've filed crbug.com/25456.
36
37class RenderViewHostDelegate;
38
39// Known browsers which we collect details for. |CHROME_BROWSER| *must* be the
40// first browser listed. The order here must match those in |process_template|
41// (in |MemoryDetails::MemoryDetails()| below).
42// TODO(viettrungluu): In the big refactoring (see above), get rid of this order
43// dependence.
44enum BrowserType {
45 // TODO(viettrungluu): possibly add more?
46 CHROME_BROWSER = 0,
47 SAFARI_BROWSER,
48 FIREFOX_BROWSER,
49 CAMINO_BROWSER,
50 OPERA_BROWSER,
51 OMNIWEB_BROWSER,
52 MAX_BROWSERS
53} BrowserProcess;
54
55
56MemoryDetails::MemoryDetails() {
[email protected]4f260d02010-12-23 18:35:4257 static const std::string google_browser_name =
58 l10n_util::GetStringUTF8(IDS_PRODUCT_NAME);
[email protected]f164cea2009-11-05 23:37:4059 // (Human and process) names of browsers; should match the ordering for
60 // |BrowserProcess| (i.e., |BrowserType|).
61 // TODO(viettrungluu): The current setup means that we can't detect both
62 // Chrome and Chromium at the same time!
63 // TODO(viettrungluu): Get localized browser names for other browsers
64 // (crbug.com/25779).
[email protected]93aa89c72010-10-20 21:32:0465 struct {
[email protected]4f260d02010-12-23 18:35:4266 const char* name;
67 const char* process_name;
[email protected]93aa89c72010-10-20 21:32:0468 } process_template[MAX_BROWSERS] = {
[email protected]f164cea2009-11-05 23:37:4069 { google_browser_name.c_str(), chrome::kBrowserProcessExecutableName, },
[email protected]4f260d02010-12-23 18:35:4270 { "Safari", "Safari", },
71 { "Firefox", "firefox-bin", },
72 { "Camino", "Camino", },
73 { "Opera", "Opera", },
74 { "OmniWeb", "OmniWeb", },
[email protected]f164cea2009-11-05 23:37:4075 };
76
[email protected]93aa89c72010-10-20 21:32:0477 for (size_t index = 0; index < MAX_BROWSERS; ++index) {
[email protected]f164cea2009-11-05 23:37:4078 ProcessData process;
[email protected]4f260d02010-12-23 18:35:4279 process.name = UTF8ToUTF16(process_template[index].name);
80 process.process_name = UTF8ToUTF16(process_template[index].process_name);
[email protected]f164cea2009-11-05 23:37:4081 process_data_.push_back(process);
82 }
83}
84
85ProcessData* MemoryDetails::ChromeBrowser() {
86 return &process_data_[CHROME_BROWSER];
87}
88
89void MemoryDetails::CollectProcessData(
[email protected]4df3ac62011-03-11 04:38:5290 const std::vector<ProcessMemoryInformation>& child_info) {
[email protected]f164cea2009-11-05 23:37:4091 // This must be run on the file thread to avoid jank (|ProcessInfoSnapshot|
92 // runs /bin/ps, which isn't instantaneous).
[email protected]f8b3ef82010-10-11 02:45:5293 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]f164cea2009-11-05 23:37:4094
95 // Clear old data.
96 for (size_t index = 0; index < MAX_BROWSERS; index++)
97 process_data_[index].processes.clear();
98
99 // First, we use |NamedProcessIterator| to get the PIDs of the processes we're
100 // interested in; we save our results to avoid extra calls to
101 // |NamedProcessIterator| (for performance reasons) and to avoid additional
102 // inconsistencies caused by racing. Then we run |/bin/ps| *once* to get
103 // information on those PIDs. Then we used our saved information to iterate
104 // over browsers, then over PIDs.
105
106 // Get PIDs of main browser processes.
107 std::vector<base::ProcessId> pids_by_browser[MAX_BROWSERS];
108 std::vector<base::ProcessId> all_pids;
109 for (size_t index = CHROME_BROWSER; index < MAX_BROWSERS; index++) {
[email protected]4f260d02010-12-23 18:35:42110 base::NamedProcessIterator process_it(
111 UTF16ToUTF8(process_data_[index].process_name), NULL);
[email protected]f164cea2009-11-05 23:37:40112
[email protected]a5a00b1d2010-04-08 15:52:45113 while (const base::ProcessEntry* entry = process_it.NextProcessEntry()) {
[email protected]b6128aa2010-04-29 17:44:42114 pids_by_browser[index].push_back(entry->pid());
115 all_pids.push_back(entry->pid());
[email protected]f164cea2009-11-05 23:37:40116 }
117 }
118
[email protected]8c40f322011-08-24 03:33:36119 // The helper might show up as these different flavors depending on the
120 // executable flags required.
121 std::vector<std::string> helper_names;
122 helper_names.push_back(chrome::kHelperProcessExecutableName);
123 for (const char* const* suffix = chrome::kHelperFlavorSuffixes;
124 *suffix;
125 ++suffix) {
126 std::string helper_name = chrome::kHelperProcessExecutableName;
127 helper_name.append(1, ' ');
128 helper_name.append(*suffix);
129 helper_names.push_back(helper_name);
130 }
131
[email protected]f164cea2009-11-05 23:37:40132 // Get PIDs of helpers.
133 std::vector<base::ProcessId> helper_pids;
[email protected]8c40f322011-08-24 03:33:36134 for (size_t i = 0; i < helper_names.size(); ++i) {
135 std::string helper_name = helper_names[i];
136 base::NamedProcessIterator helper_it(helper_name, NULL);
[email protected]a5a00b1d2010-04-08 15:52:45137 while (const base::ProcessEntry* entry = helper_it.NextProcessEntry()) {
[email protected]b6128aa2010-04-29 17:44:42138 helper_pids.push_back(entry->pid());
139 all_pids.push_back(entry->pid());
[email protected]f164cea2009-11-05 23:37:40140 }
141 }
142
143 // Capture information about the processes we're interested in.
144 ProcessInfoSnapshot process_info;
145 process_info.Sample(all_pids);
146
147 // Handle the other processes first.
148 for (size_t index = CHROME_BROWSER + 1; index < MAX_BROWSERS; index++) {
149 for (std::vector<base::ProcessId>::const_iterator it =
150 pids_by_browser[index].begin();
151 it != pids_by_browser[index].end(); ++it) {
152 ProcessMemoryInformation info;
153 info.pid = *it;
154 info.type = ChildProcessInfo::UNKNOWN_PROCESS;
155
156 // Try to get version information. To do this, we need first to get the
157 // executable's name (we can only believe |proc_info.command| if it looks
158 // like an absolute path). Then we need strip the executable's name back
159 // to the bundle's name. And only then can we try to get the version.
160 scoped_ptr<FileVersionInfo> version_info;
161 ProcessInfoSnapshot::ProcInfoEntry proc_info;
162 if (process_info.GetProcInfo(info.pid, &proc_info)) {
163 if (proc_info.command.length() > 1 && proc_info.command[0] == '/') {
164 FilePath bundle_name =
[email protected]0378bf42011-01-01 18:20:14165 base::mac::GetAppBundlePath(FilePath(proc_info.command));
[email protected]f164cea2009-11-05 23:37:40166 if (!bundle_name.empty()) {
167 version_info.reset(FileVersionInfo::CreateFileVersionInfo(
168 bundle_name));
169 }
170 }
171 }
172 if (version_info.get()) {
173 info.product_name = version_info->product_name();
174 info.version = version_info->product_version();
175 } else {
176 info.product_name = process_data_[index].name;
[email protected]4f260d02010-12-23 18:35:42177 info.version = string16();
[email protected]f164cea2009-11-05 23:37:40178 }
179
180 // Memory info.
181 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed);
182 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set);
183
184 // Add the process info to our list.
185 process_data_[index].processes.push_back(info);
186 }
187 }
188
189 // Collect data about Chrome/Chromium.
190 for (std::vector<base::ProcessId>::const_iterator it =
191 pids_by_browser[CHROME_BROWSER].begin();
192 it != pids_by_browser[CHROME_BROWSER].end(); ++it) {
193 CollectProcessDataChrome(child_info, *it, process_info);
194 }
195
196 // And collect data about the helpers.
197 for (std::vector<base::ProcessId>::const_iterator it = helper_pids.begin();
198 it != helper_pids.end(); ++it) {
199 CollectProcessDataChrome(child_info, *it, process_info);
200 }
201
202 // Finally return to the browser thread.
[email protected]f8b3ef82010-10-11 02:45:52203 BrowserThread::PostTask(
204 BrowserThread::UI, FROM_HERE,
[email protected]24d69692011-10-21 18:26:51205 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this));
[email protected]f164cea2009-11-05 23:37:40206}
207
208void MemoryDetails::CollectProcessDataChrome(
209 const std::vector<ProcessMemoryInformation>& child_info,
210 base::ProcessId pid,
211 const ProcessInfoSnapshot& process_info) {
212 ProcessMemoryInformation info;
213 info.pid = pid;
214 if (info.pid == base::GetCurrentProcId())
215 info.type = ChildProcessInfo::BROWSER_PROCESS;
216 else
217 info.type = ChildProcessInfo::UNKNOWN_PROCESS;
218
[email protected]0211f57e2010-08-27 20:28:42219 chrome::VersionInfo version_info;
[email protected]30c91802010-12-18 00:40:17220 if (version_info.is_valid()) {
[email protected]4f260d02010-12-23 18:35:42221 info.product_name = ASCIIToUTF16(version_info.Name());
222 info.version = ASCIIToUTF16(version_info.Version());
[email protected]30c91802010-12-18 00:40:17223 } else {
224 info.product_name = process_data_[CHROME_BROWSER].name;
[email protected]4f260d02010-12-23 18:35:42225 info.version = string16();
[email protected]30c91802010-12-18 00:40:17226 }
[email protected]f164cea2009-11-05 23:37:40227
228 // Check if this is one of the child processes whose data we collected
229 // on the IO thread, and if so copy over that data.
230 for (size_t child = 0; child < child_info.size(); child++) {
231 if (child_info[child].pid == info.pid) {
232 info.titles = child_info[child].titles;
233 info.type = child_info[child].type;
234 break;
235 }
236 }
237
238 // Memory info.
239 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed);
240 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set);
241
242 // Add the process info to our list.
243 process_data_[CHROME_BROWSER].processes.push_back(info);
244}