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"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/file_version_info.h"
13 #include "base/files/file_path.h"
14 #include "base/mac/foundation_util.h"
15 #include "base/process/process_iterator.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread.h"
19 #include "chrome/browser/process_info_snapshot.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "chrome/common/url_constants.h"
23 #include "chrome/grit/chromium_strings.h"
24 #include "content/public/browser/browser_child_process_host.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/common/process_type.h"
27 #include "ui/base/l10n/l10n_util.h"
29 using content::BrowserThread
;
31 // TODO(viettrungluu): Many of the TODOs below are subsumed by a general need to
32 // refactor the about:memory code (not just on Mac, but probably on other
33 // platforms as well). I've filed crbug.com/25456.
35 // Known browsers which we collect details for. |CHROME_BROWSER| *must* be the
36 // first browser listed. The order here must match those in |process_template|
37 // (in |MemoryDetails::MemoryDetails()| below).
38 // TODO(viettrungluu): In the big refactoring (see above), get rid of this order
41 // TODO(viettrungluu): possibly add more?
53 // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium
54 // process with PID |pid|. The collected data is added to |processes|.
55 void CollectProcessDataForChromeProcess(
56 const std::vector
<ProcessMemoryInformation
>& child_info
,
58 ProcessMemoryInformationList
* processes
) {
59 ProcessMemoryInformation info
;
61 if (info
.pid
== base::GetCurrentProcId())
62 info
.process_type
= content::PROCESS_TYPE_BROWSER
;
64 info
.process_type
= content::PROCESS_TYPE_UNKNOWN
;
66 chrome::VersionInfo version_info
;
67 info
.product_name
= base::ASCIIToUTF16(version_info
.Name());
68 info
.version
= base::ASCIIToUTF16(version_info
.Version());
70 // Check if this is one of the child processes whose data was already
71 // collected and exists in |child_data|.
72 for (const ProcessMemoryInformation
& child
: child_info
) {
73 if (child
.pid
== info
.pid
) {
74 info
.titles
= child
.titles
;
75 info
.process_type
= child
.process_type
;
80 scoped_ptr
<base::ProcessMetrics
> metrics
;
81 metrics
.reset(base::ProcessMetrics::CreateProcessMetrics(
82 pid
, content::BrowserChildProcessHost::GetPortProvider()));
83 metrics
->GetCommittedAndWorkingSetKBytes(&info
.committed
, &info
.working_set
);
85 processes
->push_back(info
);
88 // Collects memory information from non-Chrome browser processes, using the
89 // ProcessInfoSnapshot helper (which runs an external command - PS or TOP).
90 // Updates |process_data| with the collected information.
91 void CollectProcessDataAboutNonChromeProcesses(
92 const std::vector
<base::ProcessId
>& all_pids
,
93 const std::vector
<base::ProcessId
> pids_by_browser
[MAX_BROWSERS
],
94 std::vector
<ProcessData
>* process_data
) {
95 DCHECK_EQ(MAX_BROWSERS
, process_data
->size());
97 // Capture information about the processes we're interested in.
98 ProcessInfoSnapshot process_info
;
99 process_info
.Sample(all_pids
);
101 // Handle the other processes first.
102 for (size_t index
= CHROME_BROWSER
+ 1; index
< MAX_BROWSERS
; ++index
) {
103 for (const base::ProcessId
& pid
: pids_by_browser
[index
]) {
104 ProcessMemoryInformation info
;
106 info
.process_type
= content::PROCESS_TYPE_UNKNOWN
;
108 // Try to get version information. To do this, we need first to get the
109 // executable's name (we can only believe |proc_info.command| if it looks
110 // like an absolute path). Then we need strip the executable's name back
111 // to the bundle's name. And only then can we try to get the version.
112 scoped_ptr
<FileVersionInfo
> version_info
;
113 ProcessInfoSnapshot::ProcInfoEntry proc_info
;
114 if (process_info
.GetProcInfo(info
.pid
, &proc_info
)) {
115 if (proc_info
.command
.length() > 1 && proc_info
.command
[0] == '/') {
116 base::FilePath bundle_name
=
117 base::mac::GetAppBundlePath(base::FilePath(proc_info
.command
));
118 if (!bundle_name
.empty()) {
120 FileVersionInfo::CreateFileVersionInfo(bundle_name
));
125 info
.product_name
= version_info
->product_name();
126 info
.version
= version_info
->product_version();
128 info
.product_name
= (*process_data
)[index
].name
;
129 info
.version
.clear();
133 process_info
.GetCommittedKBytesOfPID(info
.pid
, &info
.committed
);
134 process_info
.GetWorkingSetKBytesOfPID(info
.pid
, &info
.working_set
);
136 // Add the process info to our list.
137 (*process_data
)[index
].processes
.push_back(info
);
144 MemoryDetails::MemoryDetails() {
145 const base::FilePath browser_process_path
=
146 base::GetProcessExecutablePath(base::GetCurrentProcessHandle());
147 const std::string browser_process_name
=
148 browser_process_path
.BaseName().value();
149 const std::string google_browser_name
=
150 l10n_util::GetStringUTF8(IDS_PRODUCT_NAME
);
152 // (Human and process) names of browsers; should match the ordering for
153 // |BrowserType| enum.
154 // TODO(viettrungluu): The current setup means that we can't detect both
155 // Chrome and Chromium at the same time!
156 // TODO(viettrungluu): Get localized browser names for other browsers
157 // (crbug.com/25779).
160 const char* process_name
;
161 } process_template
[MAX_BROWSERS
] = {
162 { google_browser_name
.c_str(), browser_process_name
.c_str(), },
163 { "Safari", "Safari", },
164 { "Firefox", "firefox-bin", },
165 { "Camino", "Camino", },
166 { "Opera", "Opera", },
167 { "OmniWeb", "OmniWeb", },
170 for (size_t index
= 0; index
< MAX_BROWSERS
; ++index
) {
172 process
.name
= base::UTF8ToUTF16(process_template
[index
].name
);
173 process
.process_name
=
174 base::UTF8ToUTF16(process_template
[index
].process_name
);
175 process_data_
.push_back(process
);
179 ProcessData
* MemoryDetails::ChromeBrowser() {
180 return &process_data_
[CHROME_BROWSER
];
183 void MemoryDetails::CollectProcessData(
185 const std::vector
<ProcessMemoryInformation
>& child_info
) {
186 // This must be run on the blocking pool to avoid jank (|ProcessInfoSnapshot|
187 // runs /bin/ps, which isn't instantaneous).
188 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
191 for (size_t index
= 0; index
< MAX_BROWSERS
; index
++)
192 process_data_
[index
].processes
.clear();
194 // First, we use |NamedProcessIterator| to get the PIDs of the processes we're
195 // interested in; we save our results to avoid extra calls to
196 // |NamedProcessIterator| (for performance reasons) and to avoid additional
197 // inconsistencies caused by racing. Then we run |/bin/ps| *once* to get
198 // information on those PIDs. Then we used our saved information to iterate
199 // over browsers, then over PIDs.
201 // Get PIDs of main browser processes.
202 std::vector
<base::ProcessId
> pids_by_browser
[MAX_BROWSERS
];
203 std::vector
<base::ProcessId
> all_pids
;
204 for (size_t index
= CHROME_BROWSER
; index
< MAX_BROWSERS
; index
++) {
205 base::NamedProcessIterator
process_it(
206 base::UTF16ToUTF8(process_data_
[index
].process_name
), NULL
);
208 while (const base::ProcessEntry
* entry
= process_it
.NextProcessEntry()) {
209 pids_by_browser
[index
].push_back(entry
->pid());
210 all_pids
.push_back(entry
->pid());
214 // The helper might show up as these different flavors depending on the
215 // executable flags required.
216 std::vector
<std::string
> helper_names
;
217 helper_names
.push_back(chrome::kHelperProcessExecutableName
);
218 for (const char* const* suffix
= chrome::kHelperFlavorSuffixes
;
221 std::string helper_name
= chrome::kHelperProcessExecutableName
;
222 helper_name
.append(1, ' ');
223 helper_name
.append(*suffix
);
224 helper_names
.push_back(helper_name
);
227 // Get PIDs of helpers.
228 std::vector
<base::ProcessId
> helper_pids
;
229 for (size_t i
= 0; i
< helper_names
.size(); ++i
) {
230 std::string helper_name
= helper_names
[i
];
231 base::NamedProcessIterator
helper_it(helper_name
, NULL
);
232 while (const base::ProcessEntry
* entry
= helper_it
.NextProcessEntry()) {
233 helper_pids
.push_back(entry
->pid());
234 all_pids
.push_back(entry
->pid());
238 if (mode
== FROM_ALL_BROWSERS
) {
239 CollectProcessDataAboutNonChromeProcesses(all_pids
, pids_by_browser
,
243 ProcessMemoryInformationList
* chrome_processes
=
244 &process_data_
[CHROME_BROWSER
].processes
;
246 // Collect data about Chrome/Chromium.
247 for (const base::ProcessId
& pid
: pids_by_browser
[CHROME_BROWSER
])
248 CollectProcessDataForChromeProcess(child_info
, pid
, chrome_processes
);
250 // And collect data about the helpers.
251 for (const base::ProcessId
& pid
: helper_pids
)
252 CollectProcessDataForChromeProcess(child_info
, pid
, chrome_processes
);
254 // Finally return to the browser thread.
255 BrowserThread::PostTask(
256 BrowserThread::UI
, FROM_HERE
,
257 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread
, this));