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
;
22 void TerminateAllChromeProcesses(const ChromeProcessList
& process_pids
) {
23 ChromeProcessList::const_iterator it
;
24 for (it
= process_pids
.begin(); it
!= process_pids
.end(); ++it
) {
25 base::Process process
= base::Process::Open(*it
);
26 if (process
.IsValid()) {
27 // Ignore processes for which we can't open the handle. We don't
28 // guarantee that all processes will terminate, only try to do so.
29 process
.Terminate(content::RESULT_CODE_KILLED
, true);
34 class ChildProcessFilter
: public base::ProcessFilter
{
36 explicit ChildProcessFilter(base::ProcessId parent_pid
)
37 : parent_pids_(&parent_pid
, (&parent_pid
) + 1) {}
39 explicit ChildProcessFilter(const std::vector
<base::ProcessId
>& parent_pids
)
40 : parent_pids_(parent_pids
.begin(), parent_pids
.end()) {}
42 bool Includes(const base::ProcessEntry
& entry
) const override
{
43 return parent_pids_
.find(entry
.parent_pid()) != parent_pids_
.end();
47 const std::set
<base::ProcessId
> parent_pids_
;
49 DISALLOW_COPY_AND_ASSIGN(ChildProcessFilter
);
52 ChromeProcessList
GetRunningChromeProcesses(base::ProcessId browser_pid
) {
53 const base::FilePath::CharType
* executable_name
=
54 chrome::kBrowserProcessExecutableName
;
55 ChromeProcessList result
;
56 if (browser_pid
== static_cast<base::ProcessId
>(-1))
59 ChildProcessFilter
filter(browser_pid
);
60 base::NamedProcessIterator
it(executable_name
, &filter
);
61 while (const base::ProcessEntry
* process_entry
= it
.NextProcessEntry()) {
62 result
.push_back(process_entry
->pid());
65 #if defined(OS_POSIX) && !defined(OS_MACOSX)
66 // On Unix we might be running with a zygote process for the renderers.
67 // Because of that we sweep the list of processes again and pick those which
68 // are children of one of the processes that we've already seen.
70 ChildProcessFilter
filter(result
);
71 base::NamedProcessIterator
it(executable_name
, &filter
);
72 while (const base::ProcessEntry
* process_entry
= it
.NextProcessEntry())
73 result
.push_back(process_entry
->pid());
75 #endif // defined(OS_POSIX) && !defined(OS_MACOSX)
78 // On Mac OS X we run the subprocesses with a different bundle, and
79 // on Linux via /proc/self/exe, so they end up with a different
80 // name. We must collect them in a second pass.
82 base::FilePath::StringType name
= chrome::kHelperProcessExecutableName
;
83 ChildProcessFilter
filter(browser_pid
);
84 base::NamedProcessIterator
it(name
, &filter
);
85 while (const base::ProcessEntry
* process_entry
= it
.NextProcessEntry())
86 result
.push_back(process_entry
->pid());
88 #endif // defined(OS_POSIX)
90 result
.push_back(browser_pid
);
95 #if !defined(OS_MACOSX)
97 size_t ChromeTestProcessMetrics::GetPagefileUsage() {
98 return process_metrics_
->GetPagefileUsage();
101 size_t ChromeTestProcessMetrics::GetWorkingSetSize() {
102 return process_metrics_
->GetWorkingSetSize();
105 #endif // !defined(OS_MACOSX)
107 ChromeTestProcessMetrics::~ChromeTestProcessMetrics() {}
109 ChromeTestProcessMetrics::ChromeTestProcessMetrics(
110 base::ProcessHandle process
) {
111 #if !defined(OS_MACOSX)
112 process_metrics_
.reset(
113 base::ProcessMetrics::CreateProcessMetrics(process
));
115 process_metrics_
.reset(
116 base::ProcessMetrics::CreateProcessMetrics(process
, NULL
));
118 process_handle_
= process
;