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 #ifndef CHROME_TEST_BASE_CHROME_PROCESS_UTIL_H_
6 #define CHROME_TEST_BASE_CHROME_PROCESS_UTIL_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/process/process_handle.h"
12 #include "base/process/process_metrics.h"
14 typedef std::vector
<base::ProcessId
> ChromeProcessList
;
16 // Returns a vector of PIDs of all chrome processes (main and renderers etc)
17 // based on |browser_pid|, the PID of the main browser process.
18 ChromeProcessList
GetRunningChromeProcesses(base::ProcessId browser_pid
);
20 // Attempts to terminate all chrome processes in |process_list|.
21 void TerminateAllChromeProcesses(const ChromeProcessList
& process_list
);
23 // A wrapper class for tests to use in fetching process metrics.
24 // Delegates everything we need to base::ProcessMetrics, except
25 // memory stats on Mac (which have to parse ps output due to privilege
26 // restrictions, behavior we don't want in base). Long-term, if
27 // the production base::ProcessMetrics gets updated to return
28 // acceptable metrics on Mac, this class should disappear.
29 class ChromeTestProcessMetrics
{
31 static ChromeTestProcessMetrics
* CreateProcessMetrics(
32 base::ProcessHandle process
) {
33 return new ChromeTestProcessMetrics(process
);
36 size_t GetPagefileUsage();
38 size_t GetWorkingSetSize();
40 size_t GetPeakPagefileUsage() {
41 return process_metrics_
->GetPeakPagefileUsage();
44 size_t GetPeakWorkingSetSize() {
45 return process_metrics_
->GetPeakWorkingSetSize();
48 bool GetIOCounters(base::IoCounters
* io_counters
) {
49 return process_metrics_
->GetIOCounters(io_counters
);
52 base::ProcessHandle process_handle_
;
54 ~ChromeTestProcessMetrics();
57 explicit ChromeTestProcessMetrics(base::ProcessHandle process
);
59 scoped_ptr
<base::ProcessMetrics
> process_metrics_
;
61 DISALLOW_COPY_AND_ASSIGN(ChromeTestProcessMetrics
);
64 #if defined(OS_MACOSX)
66 // These types and API are here to fetch the information about a set of running
67 // processes by ID on the Mac. There are also APIs in base, but fetching the
68 // information for another process requires privileges that a normal executable
69 // does not have. This API fetches the data by spawning ps (which is setuid so
70 // it has the needed privileges) and processing its output. The API is provided
71 // here because we don't want code spawning processes like this in base, where
72 // someone writing cross platform code might use it without realizing that it's
73 // a heavyweight call on the Mac.
75 struct MacChromeProcessInfo
{
81 typedef std::vector
<MacChromeProcessInfo
> MacChromeProcessInfoList
;
83 // Any ProcessId that info can't be found for will be left out.
84 MacChromeProcessInfoList
GetRunningMacProcessInfo(
85 const ChromeProcessList
& process_list
);
87 #endif // defined(OS_MACOSX)
89 #endif // CHROME_TEST_BASE_CHROME_PROCESS_UTIL_H_