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"
13 #include "base/bind.h"
14 #include "base/files/file_util.h"
15 #include "base/process/process_iterator.h"
16 #include "base/process/process_metrics.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/grit/chromium_strings.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/common/process_type.h"
24 #include "ui/base/l10n/l10n_util.h"
26 using base::ProcessEntry
;
27 using content::BrowserThread
;
31 // Known browsers which we collect details for.
43 // The pretty printed names of those browsers. Matches up with enum
45 const char kBrowserPrettyNames
[][10] = {
55 // A mapping from process name to the type of browser.
57 const char process_name
[17];
59 } kBrowserBinaryNames
[] = {
60 { "firefox", FIREFOX
},
61 { "firefox-3.5", FIREFOX
},
62 { "firefox-3.0", FIREFOX
},
63 { "firefox-bin", FIREFOX
},
64 { "iceweasel", ICEWEASEL
},
66 { "konqueror", KONQUEROR
},
67 { "epiphany-browser", EPIPHANY
},
68 { "epiphany", EPIPHANY
},
79 typedef std::map
<pid_t
, Process
> ProcessMap
;
81 // Get information on all the processes running on the system.
82 ProcessMap
GetProcesses() {
85 base::ProcessIterator
process_iter(NULL
);
86 while (const ProcessEntry
* process_entry
= process_iter
.NextProcessEntry()) {
88 process
.pid
= process_entry
->pid();
89 process
.parent
= process_entry
->parent_pid();
90 process
.name
= process_entry
->exe_file();
91 map
.insert(std::make_pair(process
.pid
, process
));
96 // Given a process name, return the type of the browser which created that
97 // process, or |MAX_BROWSERS| if we don't know about it.
98 BrowserType
GetBrowserType(const std::string
& process_name
) {
99 for (unsigned i
= 0; kBrowserBinaryNames
[i
].process_name
[0]; ++i
) {
100 if (strcmp(process_name
.c_str(), kBrowserBinaryNames
[i
].process_name
) == 0)
101 return kBrowserBinaryNames
[i
].browser
;
107 // For each of a list of pids, collect memory information about that process.
108 ProcessData
GetProcessDataMemoryInformation(
109 const std::vector
<pid_t
>& pids
) {
110 ProcessData process_data
;
111 for (pid_t pid
: pids
) {
112 ProcessMemoryInformation pmi
;
115 pmi
.num_processes
= 1;
117 if (pmi
.pid
== base::GetCurrentProcId())
118 pmi
.process_type
= content::PROCESS_TYPE_BROWSER
;
120 pmi
.process_type
= content::PROCESS_TYPE_UNKNOWN
;
122 scoped_ptr
<base::ProcessMetrics
> metrics(
123 base::ProcessMetrics::CreateProcessMetrics(pid
));
124 metrics
->GetWorkingSetKBytes(&pmi
.working_set
);
126 process_data
.processes
.push_back(pmi
);
131 // Find all children of the given process with pid |root|.
132 std::vector
<pid_t
> GetAllChildren(const ProcessMap
& processes
, pid_t root
) {
133 std::vector
<pid_t
> children
;
134 children
.push_back(root
);
136 std::set
<pid_t
> wavefront
, next_wavefront
;
137 wavefront
.insert(root
);
139 while (wavefront
.size()) {
140 for (const auto& entry
: processes
) {
141 const Process
& process
= entry
.second
;
142 if (wavefront
.count(process
.parent
)) {
143 children
.push_back(process
.pid
);
144 next_wavefront
.insert(process
.pid
);
149 wavefront
.swap(next_wavefront
);
156 MemoryDetails::MemoryDetails() {
159 ProcessData
* MemoryDetails::ChromeBrowser() {
160 return &process_data_
[0];
163 void MemoryDetails::CollectProcessData(
165 const std::vector
<ProcessMemoryInformation
>& child_info
) {
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
168 ProcessMap process_map
= GetProcesses();
169 std::set
<pid_t
> browsers_found
;
171 // For each process on the system, if it appears to be a browser process and
172 // it's parent isn't a browser process, then record it in |browsers_found|.
173 for (const auto& entry
: process_map
) {
174 const Process
& current_process
= entry
.second
;
175 const BrowserType type
= GetBrowserType(current_process
.name
);
176 if (type
== MAX_BROWSERS
)
178 if (type
!= CHROME
&& mode
== FROM_CHROME_ONLY
)
181 ProcessMap::const_iterator parent_iter
=
182 process_map
.find(current_process
.parent
);
183 if (parent_iter
== process_map
.end()) {
184 browsers_found
.insert(current_process
.pid
);
188 if (GetBrowserType(parent_iter
->second
.name
) != type
) {
189 // We found a process whose type is different from its parent's type.
190 // That means it is the root process of the browser.
191 browsers_found
.insert(current_process
.pid
);
196 ProcessData current_browser
=
197 GetProcessDataMemoryInformation(GetAllChildren(process_map
, getpid()));
198 current_browser
.name
= l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME
);
199 current_browser
.process_name
= base::ASCIIToUTF16("chrome");
201 for (std::vector
<ProcessMemoryInformation
>::iterator
202 i
= current_browser
.processes
.begin();
203 i
!= current_browser
.processes
.end(); ++i
) {
204 // Check if this is one of the child processes whose data we collected
205 // on the IO thread, and if so copy over that data.
206 for (size_t child
= 0; child
< child_info
.size(); child
++) {
207 if (child_info
[child
].pid
!= i
->pid
)
209 i
->titles
= child_info
[child
].titles
;
210 i
->process_type
= child_info
[child
].process_type
;
215 process_data_
.push_back(current_browser
);
217 // For each browser process, collect a list of its children and get the
218 // memory usage of each.
219 for (pid_t pid
: browsers_found
) {
220 std::vector
<pid_t
> browser_processes
= GetAllChildren(process_map
, pid
);
221 ProcessData browser
= GetProcessDataMemoryInformation(browser_processes
);
223 ProcessMap::const_iterator process_iter
= process_map
.find(pid
);
224 if (process_iter
== process_map
.end())
226 BrowserType type
= GetBrowserType(process_iter
->second
.name
);
227 if (type
!= MAX_BROWSERS
)
228 browser
.name
= base::ASCIIToUTF16(kBrowserPrettyNames
[type
]);
229 process_data_
.push_back(browser
);
232 #if defined(OS_CHROMEOS)
233 base::GetSwapInfo(&swap_info_
);
236 // Finally return to the browser thread.
237 BrowserThread::PostTask(
238 BrowserThread::UI
, FROM_HERE
,
239 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread
, this));