blob: 399049eb196775764ed6205d9c0b1842647425ba [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]e36717272010-10-12 12:07:1321#include "chrome/browser/browser_thread.h"
[email protected]f164cea2009-11-05 23:37:4022#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).
[email protected]93aa89c72010-10-20 21:32:0464 struct {
65 const wchar_t* name;
66 const wchar_t* process_name;
67 } process_template[MAX_BROWSERS] = {
[email protected]f164cea2009-11-05 23:37:4068 { google_browser_name.c_str(), chrome::kBrowserProcessExecutableName, },
69 { L"Safari", L"Safari", },
70 { L"Firefox", L"firefox-bin", },
71 { L"Camino", L"Camino", },
72 { L"Opera", L"Opera", },
73 { L"OmniWeb", L"OmniWeb", },
74 };
75
[email protected]93aa89c72010-10-20 21:32:0476 for (size_t index = 0; index < MAX_BROWSERS; ++index) {
[email protected]f164cea2009-11-05 23:37:4077 ProcessData process;
78 process.name = process_template[index].name;
79 process.process_name = process_template[index].process_name;
80 process_data_.push_back(process);
81 }
82}
83
84ProcessData* MemoryDetails::ChromeBrowser() {
85 return &process_data_[CHROME_BROWSER];
86}
87
88void MemoryDetails::CollectProcessData(
89 std::vector<ProcessMemoryInformation> child_info) {
90 // This must be run on the file thread to avoid jank (|ProcessInfoSnapshot|
91 // runs /bin/ps, which isn't instantaneous).
[email protected]f8b3ef82010-10-11 02:45:5292 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]f164cea2009-11-05 23:37:4093
94 // Clear old data.
95 for (size_t index = 0; index < MAX_BROWSERS; index++)
96 process_data_[index].processes.clear();
97
98 // First, we use |NamedProcessIterator| to get the PIDs of the processes we're
99 // interested in; we save our results to avoid extra calls to
100 // |NamedProcessIterator| (for performance reasons) and to avoid additional
101 // inconsistencies caused by racing. Then we run |/bin/ps| *once* to get
102 // information on those PIDs. Then we used our saved information to iterate
103 // over browsers, then over PIDs.
104
105 // Get PIDs of main browser processes.
106 std::vector<base::ProcessId> pids_by_browser[MAX_BROWSERS];
107 std::vector<base::ProcessId> all_pids;
108 for (size_t index = CHROME_BROWSER; index < MAX_BROWSERS; index++) {
109 base::NamedProcessIterator process_it(process_data_[index].process_name,
110 NULL);
111
[email protected]a5a00b1d2010-04-08 15:52:45112 while (const base::ProcessEntry* entry = process_it.NextProcessEntry()) {
[email protected]b6128aa2010-04-29 17:44:42113 pids_by_browser[index].push_back(entry->pid());
114 all_pids.push_back(entry->pid());
[email protected]f164cea2009-11-05 23:37:40115 }
116 }
117
118 // Get PIDs of helpers.
119 std::vector<base::ProcessId> helper_pids;
120 {
121 base::NamedProcessIterator helper_it(chrome::kHelperProcessExecutableName,
122 NULL);
[email protected]a5a00b1d2010-04-08 15:52:45123 while (const base::ProcessEntry* entry = helper_it.NextProcessEntry()) {
[email protected]b6128aa2010-04-29 17:44:42124 helper_pids.push_back(entry->pid());
125 all_pids.push_back(entry->pid());
[email protected]f164cea2009-11-05 23:37:40126 }
127 }
128
129 // Capture information about the processes we're interested in.
130 ProcessInfoSnapshot process_info;
131 process_info.Sample(all_pids);
132
133 // Handle the other processes first.
134 for (size_t index = CHROME_BROWSER + 1; index < MAX_BROWSERS; index++) {
135 for (std::vector<base::ProcessId>::const_iterator it =
136 pids_by_browser[index].begin();
137 it != pids_by_browser[index].end(); ++it) {
138 ProcessMemoryInformation info;
139 info.pid = *it;
140 info.type = ChildProcessInfo::UNKNOWN_PROCESS;
141
142 // Try to get version information. To do this, we need first to get the
143 // executable's name (we can only believe |proc_info.command| if it looks
144 // like an absolute path). Then we need strip the executable's name back
145 // to the bundle's name. And only then can we try to get the version.
146 scoped_ptr<FileVersionInfo> version_info;
147 ProcessInfoSnapshot::ProcInfoEntry proc_info;
148 if (process_info.GetProcInfo(info.pid, &proc_info)) {
149 if (proc_info.command.length() > 1 && proc_info.command[0] == '/') {
150 FilePath bundle_name =
151 mac_util::GetAppBundlePath(FilePath(proc_info.command));
152 if (!bundle_name.empty()) {
153 version_info.reset(FileVersionInfo::CreateFileVersionInfo(
154 bundle_name));
155 }
156 }
157 }
158 if (version_info.get()) {
159 info.product_name = version_info->product_name();
160 info.version = version_info->product_version();
161 } else {
162 info.product_name = process_data_[index].name;
163 info.version = L"";
164 }
165
166 // Memory info.
167 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed);
168 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set);
169
170 // Add the process info to our list.
171 process_data_[index].processes.push_back(info);
172 }
173 }
174
175 // Collect data about Chrome/Chromium.
176 for (std::vector<base::ProcessId>::const_iterator it =
177 pids_by_browser[CHROME_BROWSER].begin();
178 it != pids_by_browser[CHROME_BROWSER].end(); ++it) {
179 CollectProcessDataChrome(child_info, *it, process_info);
180 }
181
182 // And collect data about the helpers.
183 for (std::vector<base::ProcessId>::const_iterator it = helper_pids.begin();
184 it != helper_pids.end(); ++it) {
185 CollectProcessDataChrome(child_info, *it, process_info);
186 }
187
188 // Finally return to the browser thread.
[email protected]f8b3ef82010-10-11 02:45:52189 BrowserThread::PostTask(
190 BrowserThread::UI, FROM_HERE,
[email protected]f164cea2009-11-05 23:37:40191 NewRunnableMethod(this, &MemoryDetails::CollectChildInfoOnUIThread));
192}
193
194void MemoryDetails::CollectProcessDataChrome(
195 const std::vector<ProcessMemoryInformation>& child_info,
196 base::ProcessId pid,
197 const ProcessInfoSnapshot& process_info) {
198 ProcessMemoryInformation info;
199 info.pid = pid;
200 if (info.pid == base::GetCurrentProcId())
201 info.type = ChildProcessInfo::BROWSER_PROCESS;
202 else
203 info.type = ChildProcessInfo::UNKNOWN_PROCESS;
204
[email protected]0211f57e2010-08-27 20:28:42205 chrome::VersionInfo version_info;
206 if (version_info.is_valid()) {
207 info.product_name = ASCIIToWide(version_info.Name());
208 info.version = ASCIIToWide(version_info.Version());
[email protected]f164cea2009-11-05 23:37:40209 } else {
210 info.product_name = process_data_[CHROME_BROWSER].name;
211 info.version = L"";
212 }
213
214 // Check if this is one of the child processes whose data we collected
215 // on the IO thread, and if so copy over that data.
216 for (size_t child = 0; child < child_info.size(); child++) {
217 if (child_info[child].pid == info.pid) {
218 info.titles = child_info[child].titles;
219 info.type = child_info[child].type;
220 break;
221 }
222 }
223
224 // Memory info.
225 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed);
226 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set);
227
228 // Add the process info to our list.
229 process_data_[CHROME_BROWSER].processes.push_back(info);
230}