1 // Copyright (c) 2011 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/test/base/chrome_process_util.h"
11 #include "base/command_line.h"
12 #include "base/process/process.h"
13 #include "base/process/process_iterator.h"
14 #include "base/time/time.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/test/base/test_switches.h"
17 #include "content/public/common/result_codes.h"
19 using base::TimeDelta
;
20 using base::TimeTicks
;
24 // Returns the executable name of the current Chrome helper process.
25 std::vector
<base::FilePath::StringType
> GetRunningHelperExecutableNames() {
26 base::FilePath::StringType name
= chrome::kHelperProcessExecutableName
;
28 std::vector
<base::FilePath::StringType
> names
;
29 names
.push_back(name
);
31 #if defined(OS_MACOSX)
32 // The helper might show up as these different flavors depending on the
33 // executable flags required.
34 for (const char* const* suffix
= chrome::kHelperFlavorSuffixes
;
37 std::string
flavor_name(name
);
38 flavor_name
.append(1, ' ');
39 flavor_name
.append(*suffix
);
40 names
.push_back(flavor_name
);
49 void TerminateAllChromeProcesses(const ChromeProcessList
& process_pids
) {
50 ChromeProcessList::const_iterator it
;
51 for (it
= process_pids
.begin(); it
!= process_pids
.end(); ++it
) {
52 base::Process process
= base::Process::Open(*it
);
53 if (process
.IsValid()) {
54 // Ignore processes for which we can't open the handle. We don't
55 // guarantee that all processes will terminate, only try to do so.
56 process
.Terminate(content::RESULT_CODE_KILLED
, true);
61 class ChildProcessFilter
: public base::ProcessFilter
{
63 explicit ChildProcessFilter(base::ProcessId parent_pid
)
64 : parent_pids_(&parent_pid
, (&parent_pid
) + 1) {}
66 explicit ChildProcessFilter(const std::vector
<base::ProcessId
>& parent_pids
)
67 : parent_pids_(parent_pids
.begin(), parent_pids
.end()) {}
69 bool Includes(const base::ProcessEntry
& entry
) const override
{
70 return parent_pids_
.find(entry
.parent_pid()) != parent_pids_
.end();
74 const std::set
<base::ProcessId
> parent_pids_
;
76 DISALLOW_COPY_AND_ASSIGN(ChildProcessFilter
);
79 ChromeProcessList
GetRunningChromeProcesses(base::ProcessId browser_pid
) {
80 const base::FilePath::CharType
* executable_name
=
81 chrome::kBrowserProcessExecutableName
;
82 ChromeProcessList result
;
83 if (browser_pid
== static_cast<base::ProcessId
>(-1))
86 ChildProcessFilter
filter(browser_pid
);
87 base::NamedProcessIterator
it(executable_name
, &filter
);
88 while (const base::ProcessEntry
* process_entry
= it
.NextProcessEntry()) {
89 result
.push_back(process_entry
->pid());
92 #if defined(OS_POSIX) && !defined(OS_MACOSX)
93 // On Unix we might be running with a zygote process for the renderers.
94 // Because of that we sweep the list of processes again and pick those which
95 // are children of one of the processes that we've already seen.
97 ChildProcessFilter
filter(result
);
98 base::NamedProcessIterator
it(executable_name
, &filter
);
99 while (const base::ProcessEntry
* process_entry
= it
.NextProcessEntry())
100 result
.push_back(process_entry
->pid());
102 #endif // defined(OS_POSIX) && !defined(OS_MACOSX)
104 #if defined(OS_POSIX)
105 // On Mac OS X we run the subprocesses with a different bundle, and
106 // on Linux via /proc/self/exe, so they end up with a different
107 // name. We must collect them in a second pass.
109 std::vector
<base::FilePath::StringType
> names
=
110 GetRunningHelperExecutableNames();
111 for (size_t i
= 0; i
< names
.size(); ++i
) {
112 base::FilePath::StringType name
= names
[i
];
113 ChildProcessFilter
filter(browser_pid
);
114 base::NamedProcessIterator
it(name
, &filter
);
115 while (const base::ProcessEntry
* process_entry
= it
.NextProcessEntry())
116 result
.push_back(process_entry
->pid());
119 #endif // defined(OS_POSIX)
121 result
.push_back(browser_pid
);
126 #if !defined(OS_MACOSX)
128 size_t ChromeTestProcessMetrics::GetPagefileUsage() {
129 return process_metrics_
->GetPagefileUsage();
132 size_t ChromeTestProcessMetrics::GetWorkingSetSize() {
133 return process_metrics_
->GetWorkingSetSize();
136 #endif // !defined(OS_MACOSX)
138 ChromeTestProcessMetrics::~ChromeTestProcessMetrics() {}
140 ChromeTestProcessMetrics::ChromeTestProcessMetrics(
141 base::ProcessHandle process
) {
142 #if !defined(OS_MACOSX)
143 process_metrics_
.reset(
144 base::ProcessMetrics::CreateProcessMetrics(process
));
146 process_metrics_
.reset(
147 base::ProcessMetrics::CreateProcessMetrics(process
, NULL
));
149 process_handle_
= process
;