jcampan@chromium.org | 1e31211 | 2009-04-21 21:44:12 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | // This file/namespace contains utility functions for enumerating, ending and |
| 6 | // computing statistics of processes. |
| 7 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 8 | #ifndef BASE_PROCESS_UTIL_H_ |
| 9 | #define BASE_PROCESS_UTIL_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 11 | #include "base/basictypes.h" |
| 12 | |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 13 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | #include <windows.h> |
| 15 | #include <tlhelp32.h> |
wtc@chromium.org | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 16 | #elif defined(OS_MACOSX) |
| 17 | // kinfo_proc is defined in <sys/sysctl.h>, but this forward declaration |
| 18 | // is sufficient for the vector<kinfo_proc> below. |
| 19 | struct kinfo_proc; |
| 20 | #elif defined(OS_POSIX) |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | #include <limits.h> |
| 23 | #include <sys/types.h> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 24 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 25 | |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 26 | #include <string> |
thestig@chromium.org | 9ec8db0 | 2009-07-21 07:00:13 | [diff] [blame] | 27 | #include <utility> |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 28 | #include <vector> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 29 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 30 | #include "base/command_line.h" |
dkegel@google.com | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 31 | #include "base/file_path.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | #include "base/process.h" |
| 33 | |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 34 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 35 | typedef PROCESSENTRY32 ProcessEntry; |
| 36 | typedef IO_COUNTERS IoCounters; |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 37 | #elif defined(OS_POSIX) |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 38 | // TODO(port): we should not rely on a Win32 structure. |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 39 | struct ProcessEntry { |
viettrungluu@chromium.org | a4dc33f | 2009-10-20 15:09:55 | [diff] [blame] | 40 | base::ProcessId pid; |
| 41 | base::ProcessId ppid; |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 42 | char szExeFile[NAME_MAX + 1]; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 43 | }; |
| 44 | |
evanm@google.com | 0b100bc8 | 2008-10-14 20:49:16 | [diff] [blame] | 45 | struct IoCounters { |
| 46 | unsigned long long ReadOperationCount; |
| 47 | unsigned long long WriteOperationCount; |
| 48 | unsigned long long OtherOperationCount; |
| 49 | unsigned long long ReadTransferCount; |
| 50 | unsigned long long WriteTransferCount; |
| 51 | unsigned long long OtherTransferCount; |
| 52 | }; |
agl@chromium.org | 3f04f2b | 2009-04-30 19:40:03 | [diff] [blame] | 53 | |
| 54 | #include "base/file_descriptor_shuffle.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 55 | #endif |
| 56 | |
brettw@google.com | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 57 | namespace base { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 58 | |
cpu@google.com | eef576f | 2008-11-03 23:28:06 | [diff] [blame] | 59 | // A minimalistic but hopefully cross-platform set of exit codes. |
| 60 | // Do not change the enumeration values or you will break third-party |
| 61 | // installers. |
| 62 | enum { |
| 63 | PROCESS_END_NORMAL_TERMINATON = 0, |
| 64 | PROCESS_END_KILLED_BY_USER = 1, |
| 65 | PROCESS_END_PROCESS_WAS_HUNG = 2 |
| 66 | }; |
| 67 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 68 | // Returns the id of the current process. |
phajdan.jr@chromium.org | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 69 | ProcessId GetCurrentProcId(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 70 | |
erikkay@google.com | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 71 | // Returns the ProcessHandle of the current process. |
| 72 | ProcessHandle GetCurrentProcessHandle(); |
maruel@chromium.org | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 73 | |
brettw@google.com | 5986ed2 | 2009-02-06 00:19:17 | [diff] [blame] | 74 | // Converts a PID to a process handle. This handle must be closed by |
phajdan.jr@chromium.org | 6c6cc80 | 2009-04-03 17:01:36 | [diff] [blame] | 75 | // CloseProcessHandle when you are done with it. Returns true on success. |
| 76 | bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle); |
maruel@chromium.org | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 77 | |
phajdan.jr@chromium.org | 5d438dbad | 2009-04-30 08:59:39 | [diff] [blame] | 78 | // Converts a PID to a process handle. On Windows the handle is opened |
| 79 | // with more access rights and must only be used by trusted code. |
| 80 | // You have to close returned handle using CloseProcessHandle. Returns true |
| 81 | // on success. |
| 82 | bool OpenPrivilegedProcessHandle(ProcessId pid, ProcessHandle* handle); |
| 83 | |
brettw@google.com | 5986ed2 | 2009-02-06 00:19:17 | [diff] [blame] | 84 | // Closes the process handle opened by OpenProcessHandle. |
| 85 | void CloseProcessHandle(ProcessHandle process); |
erikkay@google.com | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 86 | |
cpu@google.com | eef576f | 2008-11-03 23:28:06 | [diff] [blame] | 87 | // Returns the unique ID for the specified process. This is functionally the |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 88 | // same as Windows' GetProcessId(), but works on versions of Windows before |
| 89 | // Win XP SP1 as well. |
phajdan.jr@chromium.org | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 90 | ProcessId GetProcId(ProcessHandle process); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 91 | |
dkegel@google.com | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 92 | #if defined(OS_LINUX) |
| 93 | // Returns the ID for the parent of the given process. |
| 94 | ProcessId GetParentProcessId(ProcessHandle process); |
| 95 | |
| 96 | // Returns the path to the executable of the given process. |
| 97 | FilePath GetProcessExecutablePath(ProcessHandle process); |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 98 | |
| 99 | // Parse the data found in /proc/<pid>/stat and return the sum of the |
| 100 | // CPU-related ticks. Returns -1 on parse error. |
| 101 | // Exposed for testing. |
| 102 | int ParseProcStatCPU(const std::string& input); |
dkegel@google.com | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 103 | #endif |
| 104 | |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 105 | #if defined(OS_POSIX) |
jeremy@chromium.org | 05d4b0a | 2009-01-29 17:51:51 | [diff] [blame] | 106 | // Sets all file descriptors to close on exec except for stdin, stdout |
| 107 | // and stderr. |
agl@chromium.org | 3f04f2b | 2009-04-30 19:40:03 | [diff] [blame] | 108 | // TODO(agl): remove this function |
| 109 | // WARNING: do not use. It's inherently race-prone in the face of |
| 110 | // multi-threading. |
jeremy@chromium.org | 05d4b0a | 2009-01-29 17:51:51 | [diff] [blame] | 111 | void SetAllFDsToCloseOnExec(); |
agl@chromium.org | 3f04f2b | 2009-04-30 19:40:03 | [diff] [blame] | 112 | // Close all file descriptors, expect those which are a destination in the |
| 113 | // given multimap. Only call this function in a child process where you know |
| 114 | // that there aren't any other threads. |
| 115 | void CloseSuperfluousFds(const base::InjectiveMultimap& saved_map); |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 116 | #endif |
| 117 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 118 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 119 | // Runs the given application name with the given command line. Normally, the |
| 120 | // first command line argument should be the path to the process, and don't |
| 121 | // forget to quote it. |
| 122 | // |
| 123 | // If wait is true, it will block and wait for the other process to finish, |
| 124 | // otherwise, it will just continue asynchronously. |
| 125 | // |
| 126 | // Example (including literal quotes) |
| 127 | // cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" |
| 128 | // |
| 129 | // If process_handle is non-NULL, the process handle of the launched app will be |
| 130 | // stored there on a successful launch. |
| 131 | // NOTE: In this case, the caller is responsible for closing the handle so |
| 132 | // that it doesn't leak! |
| 133 | bool LaunchApp(const std::wstring& cmdline, |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 134 | bool wait, bool start_hidden, ProcessHandle* process_handle); |
| 135 | #elif defined(OS_POSIX) |
| 136 | // Runs the application specified in argv[0] with the command line argv. |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 137 | // Before launching all FDs open in the parent process will be marked as |
| 138 | // close-on-exec. |fds_to_remap| defines a mapping of src fd->dest fd to |
| 139 | // propagate FDs into the child process. |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 140 | // |
| 141 | // As above, if wait is true, execute synchronously. The pid will be stored |
| 142 | // in process_handle if that pointer is non-null. |
| 143 | // |
mattm@chromium.org | b74d21b3 | 2009-07-17 19:36:00 | [diff] [blame] | 144 | // Note that the first argument in argv must point to the executable filename. |
| 145 | // If the filename is not fully specified, PATH will be searched. |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 146 | typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 147 | bool LaunchApp(const std::vector<std::string>& argv, |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 148 | const file_handle_mapping_vector& fds_to_remap, |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 149 | bool wait, ProcessHandle* process_handle); |
stuartmorgan@google.com | 2aea9e09 | 2009-08-06 20:03:01 | [diff] [blame] | 150 | |
thestig@chromium.org | 9ec8db0 | 2009-07-21 07:00:13 | [diff] [blame] | 151 | // Similar to above, but also (un)set environment variables in child process |
| 152 | // through |environ|. |
| 153 | typedef std::vector<std::pair<const char*, const char*> > environment_vector; |
| 154 | bool LaunchApp(const std::vector<std::string>& argv, |
| 155 | const environment_vector& environ, |
| 156 | const file_handle_mapping_vector& fds_to_remap, |
| 157 | bool wait, ProcessHandle* process_handle); |
thestig@chromium.org | 9ec8db0 | 2009-07-21 07:00:13 | [diff] [blame] | 158 | #endif // defined(OS_POSIX) |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 159 | |
jcampan@chromium.org | 1e31211 | 2009-04-21 21:44:12 | [diff] [blame] | 160 | // Executes the application specified by cl. This function delegates to one |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 161 | // of the above two platform-specific functions. |
| 162 | bool LaunchApp(const CommandLine& cl, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 163 | bool wait, bool start_hidden, ProcessHandle* process_handle); |
| 164 | |
jcampan@chromium.org | 1e31211 | 2009-04-21 21:44:12 | [diff] [blame] | 165 | // Executes the application specified by |cl| and wait for it to exit. Stores |
phajdan.jr@chromium.org | 1912cfe | 2009-04-21 08:09:30 | [diff] [blame] | 166 | // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true |
| 167 | // on success (application launched and exited cleanly, with exit code |
| 168 | // indicating success). |output| is modified only when the function finished |
| 169 | // successfully. |
phajdan.jr@chromium.org | c0b210ee | 2009-04-17 09:57:52 | [diff] [blame] | 170 | bool GetAppOutput(const CommandLine& cl, std::string* output); |
phajdan.jr@chromium.org | c0b210ee | 2009-04-17 09:57:52 | [diff] [blame] | 171 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 172 | // Used to filter processes by process ID. |
| 173 | class ProcessFilter { |
| 174 | public: |
| 175 | // Returns true to indicate set-inclusion and false otherwise. This method |
| 176 | // should not have side-effects and should be idempotent. |
phajdan.jr@chromium.org | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 177 | virtual bool Includes(ProcessId pid, ProcessId parent_pid) const = 0; |
avi@google.com | dd950aa | 2008-08-12 21:24:39 | [diff] [blame] | 178 | virtual ~ProcessFilter() { } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | // Returns the number of processes on the machine that are running from the |
| 182 | // given executable name. If filter is non-null, then only processes selected |
| 183 | // by the filter will be counted. |
| 184 | int GetProcessCount(const std::wstring& executable_name, |
| 185 | const ProcessFilter* filter); |
| 186 | |
| 187 | // Attempts to kill all the processes on the current machine that were launched |
| 188 | // from the given executable name, ending them with the given exit code. If |
| 189 | // filter is non-null, then only processes selected by the filter are killed. |
| 190 | // Returns false if all processes were able to be killed off, false if at least |
| 191 | // one couldn't be killed. |
| 192 | bool KillProcesses(const std::wstring& executable_name, int exit_code, |
| 193 | const ProcessFilter* filter); |
| 194 | |
| 195 | // Attempts to kill the process identified by the given process |
| 196 | // entry structure, giving it the specified exit code. If |wait| is true, wait |
| 197 | // for the process to be actually terminated before returning. |
| 198 | // Returns true if this is successful, false otherwise. |
stoyan@chromium.org | cd4fd15 | 2009-02-09 19:28:41 | [diff] [blame] | 199 | bool KillProcess(ProcessHandle process, int exit_code, bool wait); |
agl@chromium.org | dfe1486 | 2009-01-22 01:23:11 | [diff] [blame] | 200 | #if defined(OS_WIN) |
phajdan.jr@chromium.org | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 201 | bool KillProcessById(ProcessId process_id, int exit_code, bool wait); |
agl@chromium.org | dfe1486 | 2009-01-22 01:23:11 | [diff] [blame] | 202 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 203 | |
| 204 | // Get the termination status (exit code) of the process and return true if the |
agl@chromium.org | 140a7cd | 2009-04-28 01:37:23 | [diff] [blame] | 205 | // status indicates the process crashed. |child_exited| is set to true iff the |
| 206 | // child process has terminated. (|child_exited| may be NULL.) |
| 207 | // |
| 208 | // On Windows, it is an error to call this if the process hasn't terminated |
| 209 | // yet. On POSIX, |child_exited| is set correctly since we detect terminate in |
| 210 | // a different manner on POSIX. |
| 211 | bool DidProcessCrash(bool* child_exited, ProcessHandle handle); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 212 | |
phajdan.jr@chromium.org | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 213 | // Waits for process to exit. In POSIX systems, if the process hasn't been |
| 214 | // signaled then puts the exit code in |exit_code|; otherwise it's considered |
| 215 | // a failure. On Windows |exit_code| is always filled. Returns true on success, |
| 216 | // and closes |handle| in any case. |
| 217 | bool WaitForExitCode(ProcessHandle handle, int* exit_code); |
| 218 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 219 | // Wait for all the processes based on the named executable to exit. If filter |
| 220 | // is non-null, then only processes selected by the filter are waited on. |
| 221 | // Returns after all processes have exited or wait_milliseconds have expired. |
| 222 | // Returns true if all the processes exited, false otherwise. |
| 223 | bool WaitForProcessesToExit(const std::wstring& executable_name, |
phajdan.jr@chromium.org | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 224 | int64 wait_milliseconds, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 225 | const ProcessFilter* filter); |
| 226 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 227 | // Wait for a single process to exit. Return true if it exited cleanly within |
| 228 | // the given time limit. |
| 229 | bool WaitForSingleProcess(ProcessHandle handle, |
phajdan.jr@chromium.org | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 230 | int64 wait_milliseconds); |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 231 | |
phajdan.jr@chromium.org | 076bf0b6 | 2009-03-04 20:57:58 | [diff] [blame] | 232 | // Returns true when |wait_milliseconds| have elapsed and the process |
| 233 | // is still running. |
phajdan.jr@chromium.org | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 234 | bool CrashAwareSleep(ProcessHandle handle, int64 wait_milliseconds); |
phajdan.jr@chromium.org | 076bf0b6 | 2009-03-04 20:57:58 | [diff] [blame] | 235 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 236 | // Waits a certain amount of time (can be 0) for all the processes with a given |
| 237 | // executable name to exit, then kills off any of them that are still around. |
| 238 | // If filter is non-null, then only processes selected by the filter are waited |
| 239 | // on. Killed processes are ended with the given exit code. Returns false if |
| 240 | // any processes needed to be killed, true if they all exited cleanly within |
| 241 | // the wait_milliseconds delay. |
| 242 | bool CleanupProcesses(const std::wstring& executable_name, |
phajdan.jr@chromium.org | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 243 | int64 wait_milliseconds, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 244 | int exit_code, |
| 245 | const ProcessFilter* filter); |
| 246 | |
| 247 | // This class provides a way to iterate through the list of processes |
| 248 | // on the current machine that were started from the given executable |
| 249 | // name. To use, create an instance and then call NextProcessEntry() |
| 250 | // until it returns false. |
| 251 | class NamedProcessIterator { |
| 252 | public: |
| 253 | NamedProcessIterator(const std::wstring& executable_name, |
| 254 | const ProcessFilter* filter); |
| 255 | ~NamedProcessIterator(); |
| 256 | |
| 257 | // If there's another process that matches the given executable name, |
| 258 | // returns a const pointer to the corresponding PROCESSENTRY32. |
| 259 | // If there are no more matching processes, returns NULL. |
| 260 | // The returned pointer will remain valid until NextProcessEntry() |
| 261 | // is called again or this NamedProcessIterator goes out of scope. |
| 262 | const ProcessEntry* NextProcessEntry(); |
| 263 | |
| 264 | private: |
| 265 | // Determines whether there's another process (regardless of executable) |
| 266 | // left in the list of all processes. Returns true and sets entry_ to |
| 267 | // that process's info if there is one, false otherwise. |
| 268 | bool CheckForNextProcess(); |
| 269 | |
| 270 | bool IncludeEntry(); |
| 271 | |
| 272 | // Initializes a PROCESSENTRY32 data structure so that it's ready for |
| 273 | // use with Process32First/Process32Next. |
| 274 | void InitProcessEntry(ProcessEntry* entry); |
| 275 | |
| 276 | std::wstring executable_name_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 277 | |
| 278 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 279 | HANDLE snapshot_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 280 | bool started_iteration_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 281 | #elif defined(OS_MACOSX) |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 282 | std::vector<kinfo_proc> kinfo_procs_; |
| 283 | size_t index_of_kinfo_proc_; |
wtc@chromium.org | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 284 | #elif defined(OS_POSIX) |
| 285 | DIR *procfs_dir_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 286 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 287 | ProcessEntry entry_; |
| 288 | const ProcessFilter* filter_; |
| 289 | |
| 290 | DISALLOW_EVIL_CONSTRUCTORS(NamedProcessIterator); |
| 291 | }; |
| 292 | |
| 293 | // Working Set (resident) memory usage broken down by |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 294 | // |
| 295 | // On Windows: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 296 | // priv (private): These pages (kbytes) cannot be shared with any other process. |
| 297 | // shareable: These pages (kbytes) can be shared with other processes under |
| 298 | // the right circumstances. |
| 299 | // shared : These pages (kbytes) are currently shared with at least one |
| 300 | // other process. |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 301 | // |
| 302 | // On Linux: |
| 303 | // priv: Pages mapped only by this process |
| 304 | // shared: PSS or 0 if the kernel doesn't support this |
| 305 | // shareable: 0 |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 306 | struct WorkingSetKBytes { |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 307 | WorkingSetKBytes() : priv(0), shareable(0), shared(0) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 308 | size_t priv; |
| 309 | size_t shareable; |
| 310 | size_t shared; |
| 311 | }; |
| 312 | |
| 313 | // Committed (resident + paged) memory usage broken down by |
| 314 | // private: These pages cannot be shared with any other process. |
| 315 | // mapped: These pages are mapped into the view of a section (backed by |
| 316 | // pagefile.sys) |
| 317 | // image: These pages are mapped into the view of an image section (backed by |
| 318 | // file system) |
| 319 | struct CommittedKBytes { |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 320 | CommittedKBytes() : priv(0), mapped(0), image(0) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 321 | size_t priv; |
| 322 | size_t mapped; |
| 323 | size_t image; |
| 324 | }; |
| 325 | |
| 326 | // Free memory (Megabytes marked as free) in the 2G process address space. |
| 327 | // total : total amount in megabytes marked as free. Maximum value is 2048. |
| 328 | // largest : size of the largest contiguous amount of memory found. It is |
| 329 | // always smaller or equal to FreeMBytes::total. |
| 330 | // largest_ptr: starting address of the largest memory block. |
| 331 | struct FreeMBytes { |
| 332 | size_t total; |
| 333 | size_t largest; |
| 334 | void* largest_ptr; |
| 335 | }; |
| 336 | |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 337 | // Convert a POSIX timeval to microseconds. |
| 338 | int64 TimeValToMicroseconds(const struct timeval& tv); |
| 339 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 340 | // Provides performance metrics for a specified process (CPU usage, memory and |
| 341 | // IO counters). To use it, invoke CreateProcessMetrics() to get an instance |
| 342 | // for a specific process, then access the information with the different get |
| 343 | // methods. |
| 344 | class ProcessMetrics { |
| 345 | public: |
| 346 | // Creates a ProcessMetrics for the specified process. |
| 347 | // The caller owns the returned object. |
| 348 | static ProcessMetrics* CreateProcessMetrics(ProcessHandle process); |
| 349 | |
| 350 | ~ProcessMetrics(); |
| 351 | |
| 352 | // Returns the current space allocated for the pagefile, in bytes (these pages |
thomasvl@chromium.org | 796da7c | 2009-06-11 12:45:45 | [diff] [blame] | 353 | // may or may not be in memory). On Linux, this returns the total virtual |
| 354 | // memory size. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 355 | size_t GetPagefileUsage() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 356 | // Returns the peak space allocated for the pagefile, in bytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 357 | size_t GetPeakPagefileUsage() const; |
thomasvl@chromium.org | 796da7c | 2009-06-11 12:45:45 | [diff] [blame] | 358 | // Returns the current working set size, in bytes. On Linux, this returns |
| 359 | // the resident set size. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 360 | size_t GetWorkingSetSize() const; |
tc@google.com | 0c557f1 | 2009-05-11 23:35:52 | [diff] [blame] | 361 | // Returns the peak working set size, in bytes. |
| 362 | size_t GetPeakWorkingSetSize() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 363 | // Returns private usage, in bytes. Private bytes is the amount |
| 364 | // of memory currently allocated to a process that cannot be shared. |
| 365 | // Note: returns 0 on unsupported OSes: prior to XP SP2. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 366 | size_t GetPrivateBytes() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 367 | // Fills a CommittedKBytes with both resident and paged |
| 368 | // memory usage as per definition of CommittedBytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 369 | void GetCommittedKBytes(CommittedKBytes* usage) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 370 | // Fills a WorkingSetKBytes containing resident private and shared memory |
| 371 | // usage in bytes, as per definition of WorkingSetBytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 372 | bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 373 | |
| 374 | // Computes the current process available memory for allocation. |
| 375 | // It does a linear scan of the address space querying each memory region |
| 376 | // for its free (unallocated) status. It is useful for estimating the memory |
| 377 | // load and fragmentation. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 378 | bool CalculateFreeMemory(FreeMBytes* free) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 379 | |
| 380 | // Returns the CPU usage in percent since the last time this method was |
| 381 | // called. The first time this method is called it returns 0 and will return |
| 382 | // the actual CPU info on subsequent calls. |
| 383 | // Note that on multi-processor machines, the CPU usage value is for all |
| 384 | // CPUs. So if you have 2 CPUs and your process is using all the cycles |
| 385 | // of 1 CPU and not the other CPU, this method returns 50. |
| 386 | int GetCPUUsage(); |
| 387 | |
| 388 | // Retrieves accounting information for all I/O operations performed by the |
| 389 | // process. |
| 390 | // If IO information is retrieved successfully, the function returns true |
| 391 | // and fills in the IO_COUNTERS passed in. The function returns false |
| 392 | // otherwise. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 393 | bool GetIOCounters(IoCounters* io_counters) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 394 | |
| 395 | private: |
| 396 | explicit ProcessMetrics(ProcessHandle process); |
| 397 | |
| 398 | ProcessHandle process_; |
| 399 | |
| 400 | int processor_count_; |
| 401 | |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 402 | // Used to store the previous times and CPU usage counts so we can |
| 403 | // compute the CPU usage between calls. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 404 | int64 last_time_; |
| 405 | int64 last_system_time_; |
| 406 | |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 407 | #if defined(OS_LINUX) |
| 408 | // Jiffie count at the last_time_ we updated. |
| 409 | int last_cpu_; |
| 410 | #endif |
| 411 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 412 | DISALLOW_EVIL_CONSTRUCTORS(ProcessMetrics); |
| 413 | }; |
| 414 | |
| 415 | // Enables low fragmentation heap (LFH) for every heaps of this process. This |
| 416 | // won't have any effect on heaps created after this function call. It will not |
| 417 | // modify data allocated in the heaps before calling this function. So it is |
| 418 | // better to call this function early in initialization and again before |
| 419 | // entering the main loop. |
| 420 | // Note: Returns true on Windows 2000 without doing anything. |
| 421 | bool EnableLowFragmentationHeap(); |
| 422 | |
maruel@chromium.org | d6fc9fd | 2009-10-27 18:03:47 | [diff] [blame^] | 423 | // Enables 'terminate on heap corruption' flag. Helps protect against heap |
maruel@google.com | c9d4087 | 2008-09-24 12:58:37 | [diff] [blame] | 424 | // overflow. Has no effect if the OS doesn't provide the necessary facility. |
| 425 | void EnableTerminationOnHeapCorruption(); |
| 426 | |
maruel@chromium.org | d6fc9fd | 2009-10-27 18:03:47 | [diff] [blame^] | 427 | #if defined(UNIT_TEST) |
| 428 | // Enables stack dump to console output on exception and signals. |
| 429 | // When enabled, the process will quit immediately. This is meant to be used in |
| 430 | // unit_tests only! |
| 431 | bool EnableInProcessStackDumping(); |
| 432 | #endif // defined(UNIT_TEST) |
| 433 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 434 | // If supported on the platform, and the user has sufficent rights, increase |
| 435 | // the current process's scheduling priority to a high priority. |
| 436 | void RaiseProcessToHighPriority(); |
| 437 | |
mark@chromium.org | e9a8c19f | 2009-09-03 21:27:36 | [diff] [blame] | 438 | #if defined(OS_MACOSX) |
| 439 | // Restore the default exception handler, setting it to Apple Crash Reporter |
| 440 | // (ReportCrash). When forking and execing a new process, the child will |
| 441 | // inherit the parent's exception ports, which may be set to the Breakpad |
| 442 | // instance running inside the parent. The parent's Breakpad instance should |
| 443 | // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
| 444 | // in the child after forking will restore the standard exception handler. |
| 445 | // See http://crbug.com/20371/ for more details. |
| 446 | void RestoreDefaultExceptionHandler(); |
| 447 | #endif |
| 448 | |
brettw@google.com | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 449 | } // namespace base |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 450 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 451 | #endif // BASE_PROCESS_UTIL_H_ |