• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_TEST_BASE_CHROME_PROCESS_UTIL_H_
6 #define CHROME_TEST_BASE_CHROME_PROCESS_UTIL_H_
7 
8 #include <vector>
9 
10 #include "base/memory/scoped_ptr.h"
11 #include "base/process/process_handle.h"
12 #include "base/process/process_metrics.h"
13 
14 typedef std::vector<base::ProcessId> ChromeProcessList;
15 
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);
19 
20 // Attempts to terminate all chrome processes in |process_list|.
21 void TerminateAllChromeProcesses(const ChromeProcessList& process_list);
22 
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 {
30  public:
CreateProcessMetrics(base::ProcessHandle process)31   static ChromeTestProcessMetrics* CreateProcessMetrics(
32         base::ProcessHandle process) {
33     return new ChromeTestProcessMetrics(process);
34   }
35 
36   size_t GetPagefileUsage();
37 
38   size_t GetWorkingSetSize();
39 
GetPeakPagefileUsage()40   size_t GetPeakPagefileUsage() {
41     return process_metrics_->GetPeakPagefileUsage();
42   }
43 
GetPeakWorkingSetSize()44   size_t GetPeakWorkingSetSize() {
45     return process_metrics_->GetPeakWorkingSetSize();
46   }
47 
GetIOCounters(base::IoCounters * io_counters)48   bool GetIOCounters(base::IoCounters* io_counters) {
49     return process_metrics_->GetIOCounters(io_counters);
50   }
51 
52   base::ProcessHandle process_handle_;
53 
54   ~ChromeTestProcessMetrics();
55 
56  private:
57   explicit ChromeTestProcessMetrics(base::ProcessHandle process);
58 
59   scoped_ptr<base::ProcessMetrics> process_metrics_;
60 
61   DISALLOW_COPY_AND_ASSIGN(ChromeTestProcessMetrics);
62 };
63 
64 #if defined(OS_MACOSX)
65 
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.
74 
75 struct MacChromeProcessInfo {
76   base::ProcessId pid;
77   int rsz_in_kb;
78   int vsz_in_kb;
79 };
80 
81 typedef std::vector<MacChromeProcessInfo> MacChromeProcessInfoList;
82 
83 // Any ProcessId that info can't be found for will be left out.
84 MacChromeProcessInfoList GetRunningMacProcessInfo(
85                                         const ChromeProcessList& process_list);
86 
87 #endif  // defined(OS_MACOSX)
88 
89 #endif  // CHROME_TEST_BASE_CHROME_PROCESS_UTIL_H_
90