blob: 26b9fbff592b43c6896b66af8c36e4dde53d5e86 [file] [log] [blame]
[email protected]a5a00b1d2010-04-08 15:52:451// Copyright (c) 2010 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
10#include "app/l10n_util.h"
11#include "base/basictypes.h"
12#include "base/file_path.h"
13#include "base/file_version_info.h"
14#include "base/mac_util.h"
15#include "base/string_util.h"
16#include "base/process_util.h"
17#include "base/thread.h"
[email protected]0211f57e2010-08-27 20:28:4218#include "base/utf_string_conversions.h"
[email protected]d27893f62010-07-03 05:47:4219#include "chrome/browser/browser_child_process_host.h"
[email protected]f164cea2009-11-05 23:37:4020#include "chrome/browser/browser_process.h"
[email protected]f164cea2009-11-05 23:37:4021#include "chrome/browser/chrome_thread.h"
22#include "chrome/browser/process_info_snapshot.h"
23#include "chrome/browser/renderer_host/backing_store_manager.h"
24#include "chrome/browser/renderer_host/render_process_host.h"
25#include "chrome/browser/tab_contents/navigation_entry.h"
26#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]f164cea2009-11-05 23:37:4027#include "chrome/common/chrome_constants.h"
[email protected]1eeb5e02010-07-20 23:02:1128#include "chrome/common/chrome_version_info.h"
[email protected]f164cea2009-11-05 23:37:4029#include "chrome/common/url_constants.h"
30#include "grit/chromium_strings.h"
31
32// TODO(viettrungluu): Many of the TODOs below are subsumed by a general need to
33// refactor the about:memory code (not just on Mac, but probably on other
34// platforms as well). I've filed crbug.com/25456.
35
36class RenderViewHostDelegate;
37
38// Known browsers which we collect details for. |CHROME_BROWSER| *must* be the
39// first browser listed. The order here must match those in |process_template|
40// (in |MemoryDetails::MemoryDetails()| below).
41// TODO(viettrungluu): In the big refactoring (see above), get rid of this order
42// dependence.
43enum BrowserType {
44 // TODO(viettrungluu): possibly add more?
45 CHROME_BROWSER = 0,
46 SAFARI_BROWSER,
47 FIREFOX_BROWSER,
48 CAMINO_BROWSER,
49 OPERA_BROWSER,
50 OMNIWEB_BROWSER,
51 MAX_BROWSERS
52} BrowserProcess;
53
54
55MemoryDetails::MemoryDetails() {
56 static const std::wstring google_browser_name =
57 l10n_util::GetString(IDS_PRODUCT_NAME);
58 // (Human and process) names of browsers; should match the ordering for
59 // |BrowserProcess| (i.e., |BrowserType|).
60 // TODO(viettrungluu): The current setup means that we can't detect both
61 // Chrome and Chromium at the same time!
62 // TODO(viettrungluu): Get localized browser names for other browsers
63 // (crbug.com/25779).
64 ProcessData process_template[MAX_BROWSERS] = {
65 { google_browser_name.c_str(), chrome::kBrowserProcessExecutableName, },
66 { L"Safari", L"Safari", },
67 { L"Firefox", L"firefox-bin", },
68 { L"Camino", L"Camino", },
69 { L"Opera", L"Opera", },
70 { L"OmniWeb", L"OmniWeb", },
71 };
72
73 for (size_t index = 0; index < arraysize(process_template); ++index) {
74 ProcessData process;
75 process.name = process_template[index].name;
76 process.process_name = process_template[index].process_name;
77 process_data_.push_back(process);
78 }
79}
80
81ProcessData* MemoryDetails::ChromeBrowser() {
82 return &process_data_[CHROME_BROWSER];
83}
84
85void MemoryDetails::CollectProcessData(
86 std::vector<ProcessMemoryInformation> child_info) {
87 // This must be run on the file thread to avoid jank (|ProcessInfoSnapshot|
88 // runs /bin/ps, which isn't instantaneous).
89 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
90
91 // Clear old data.
92 for (size_t index = 0; index < MAX_BROWSERS; index++)
93 process_data_[index].processes.clear();
94
95 // First, we use |NamedProcessIterator| to get the PIDs of the processes we're
96 // interested in; we save our results to avoid extra calls to
97 // |NamedProcessIterator| (for performance reasons) and to avoid additional
98 // inconsistencies caused by racing. Then we run |/bin/ps| *once* to get
99 // information on those PIDs. Then we used our saved information to iterate
100 // over browsers, then over PIDs.
101
102 // Get PIDs of main browser processes.
103 std::vector<base::ProcessId> pids_by_browser[MAX_BROWSERS];
104 std::vector<base::ProcessId> all_pids;
105 for (size_t index = CHROME_BROWSER; index < MAX_BROWSERS; index++) {
106 base::NamedProcessIterator process_it(process_data_[index].process_name,
107 NULL);
108
[email protected]a5a00b1d2010-04-08 15:52:45109 while (const base::ProcessEntry* entry = process_it.NextProcessEntry()) {
[email protected]b6128aa2010-04-29 17:44:42110 pids_by_browser[index].push_back(entry->pid());
111 all_pids.push_back(entry->pid());
[email protected]f164cea2009-11-05 23:37:40112 }
113 }
114
115 // Get PIDs of helpers.
116 std::vector<base::ProcessId> helper_pids;
117 {
118 base::NamedProcessIterator helper_it(chrome::kHelperProcessExecutableName,
119 NULL);
[email protected]a5a00b1d2010-04-08 15:52:45120 while (const base::ProcessEntry* entry = helper_it.NextProcessEntry()) {
[email protected]b6128aa2010-04-29 17:44:42121 helper_pids.push_back(entry->pid());
122 all_pids.push_back(entry->pid());
[email protected]f164cea2009-11-05 23:37:40123 }
124 }
125
126 // Capture information about the processes we're interested in.
127 ProcessInfoSnapshot process_info;
128 process_info.Sample(all_pids);
129
130 // Handle the other processes first.
131 for (size_t index = CHROME_BROWSER + 1; index < MAX_BROWSERS; index++) {
132 for (std::vector<base::ProcessId>::const_iterator it =
133 pids_by_browser[index].begin();
134 it != pids_by_browser[index].end(); ++it) {
135 ProcessMemoryInformation info;
136 info.pid = *it;
137 info.type = ChildProcessInfo::UNKNOWN_PROCESS;
138
139 // Try to get version information. To do this, we need first to get the
140 // executable's name (we can only believe |proc_info.command| if it looks
141 // like an absolute path). Then we need strip the executable's name back
142 // to the bundle's name. And only then can we try to get the version.
143 scoped_ptr<FileVersionInfo> version_info;
144 ProcessInfoSnapshot::ProcInfoEntry proc_info;
145 if (process_info.GetProcInfo(info.pid, &proc_info)) {
146 if (proc_info.command.length() > 1 && proc_info.command[0] == '/') {
147 FilePath bundle_name =
148 mac_util::GetAppBundlePath(FilePath(proc_info.command));
149 if (!bundle_name.empty()) {
150 version_info.reset(FileVersionInfo::CreateFileVersionInfo(
151 bundle_name));
152 }
153 }
154 }
155 if (version_info.get()) {
156 info.product_name = version_info->product_name();
157 info.version = version_info->product_version();
158 } else {
159 info.product_name = process_data_[index].name;
160 info.version = L"";
161 }
162
163 // Memory info.
164 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed);
165 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set);
166
167 // Add the process info to our list.
168 process_data_[index].processes.push_back(info);
169 }
170 }
171
172 // Collect data about Chrome/Chromium.
173 for (std::vector<base::ProcessId>::const_iterator it =
174 pids_by_browser[CHROME_BROWSER].begin();
175 it != pids_by_browser[CHROME_BROWSER].end(); ++it) {
176 CollectProcessDataChrome(child_info, *it, process_info);
177 }
178
179 // And collect data about the helpers.
180 for (std::vector<base::ProcessId>::const_iterator it = helper_pids.begin();
181 it != helper_pids.end(); ++it) {
182 CollectProcessDataChrome(child_info, *it, process_info);
183 }
184
185 // Finally return to the browser thread.
186 ChromeThread::PostTask(
187 ChromeThread::UI, FROM_HERE,
188 NewRunnableMethod(this, &MemoryDetails::CollectChildInfoOnUIThread));
189}
190
191void MemoryDetails::CollectProcessDataChrome(
192 const std::vector<ProcessMemoryInformation>& child_info,
193 base::ProcessId pid,
194 const ProcessInfoSnapshot& process_info) {
195 ProcessMemoryInformation info;
196 info.pid = pid;
197 if (info.pid == base::GetCurrentProcId())
198 info.type = ChildProcessInfo::BROWSER_PROCESS;
199 else
200 info.type = ChildProcessInfo::UNKNOWN_PROCESS;
201
[email protected]0211f57e2010-08-27 20:28:42202 chrome::VersionInfo version_info;
203 if (version_info.is_valid()) {
204 info.product_name = ASCIIToWide(version_info.Name());
205 info.version = ASCIIToWide(version_info.Version());
[email protected]f164cea2009-11-05 23:37:40206 } else {
207 info.product_name = process_data_[CHROME_BROWSER].name;
208 info.version = L"";
209 }
210
211 // Check if this is one of the child processes whose data we collected
212 // on the IO thread, and if so copy over that data.
213 for (size_t child = 0; child < child_info.size(); child++) {
214 if (child_info[child].pid == info.pid) {
215 info.titles = child_info[child].titles;
216 info.type = child_info[child].type;
217 break;
218 }
219 }
220
221 // Memory info.
222 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed);
223 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set);
224
225 // Add the process info to our list.
226 process_data_[CHROME_BROWSER].processes.push_back(info);
227}