license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
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> |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 16 | #elif defined(OS_LINUX) |
| 17 | #include <dirent.h> |
| 18 | #include <limits.h> |
| 19 | #include <sys/types.h> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 20 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 22 | #include <string> |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 23 | #include <vector> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 24 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 25 | #include "base/command_line.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 26 | #include "base/process.h" |
| 27 | |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 28 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 29 | typedef PROCESSENTRY32 ProcessEntry; |
| 30 | typedef IO_COUNTERS IoCounters; |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 31 | #elif defined(OS_POSIX) |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 32 | // TODO(port): we should not rely on a Win32 structure. |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 33 | struct ProcessEntry { |
| 34 | int pid; |
| 35 | int ppid; |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 36 | char szExeFile[NAME_MAX + 1]; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 37 | }; |
| 38 | |
evanm@google.com | 0b100bc8 | 2008-10-14 20:49:16 | [diff] [blame] | 39 | struct IoCounters { |
| 40 | unsigned long long ReadOperationCount; |
| 41 | unsigned long long WriteOperationCount; |
| 42 | unsigned long long OtherOperationCount; |
| 43 | unsigned long long ReadTransferCount; |
| 44 | unsigned long long WriteTransferCount; |
| 45 | unsigned long long OtherTransferCount; |
| 46 | }; |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | #if defined(OS_MACOSX) |
| 50 | struct kinfo_proc; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 51 | #endif |
| 52 | |
brettw@google.com | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 53 | namespace base { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 54 | |
cpu@google.com | eef576f | 2008-11-03 23:28:06 | [diff] [blame] | 55 | // A minimalistic but hopefully cross-platform set of exit codes. |
| 56 | // Do not change the enumeration values or you will break third-party |
| 57 | // installers. |
| 58 | enum { |
| 59 | PROCESS_END_NORMAL_TERMINATON = 0, |
| 60 | PROCESS_END_KILLED_BY_USER = 1, |
| 61 | PROCESS_END_PROCESS_WAS_HUNG = 2 |
| 62 | }; |
| 63 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 64 | // Returns the id of the current process. |
| 65 | int GetCurrentProcId(); |
| 66 | |
erikkay@google.com | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 67 | // Returns the ProcessHandle of the current process. |
| 68 | ProcessHandle GetCurrentProcessHandle(); |
brettw@google.com | 5986ed2 | 2009-02-06 00:19:17 | [diff] [blame^] | 69 | |
| 70 | // Converts a PID to a process handle. This handle must be closed by |
| 71 | // CloseProcessHandle when you are done with it. |
| 72 | ProcessHandle OpenProcessHandle(int pid); |
| 73 | |
| 74 | // Closes the process handle opened by OpenProcessHandle. |
| 75 | void CloseProcessHandle(ProcessHandle process); |
erikkay@google.com | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 76 | |
cpu@google.com | eef576f | 2008-11-03 23:28:06 | [diff] [blame] | 77 | // Returns the unique ID for the specified process. This is functionally the |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 78 | // same as Windows' GetProcessId(), but works on versions of Windows before |
| 79 | // Win XP SP1 as well. |
| 80 | int GetProcId(ProcessHandle process); |
| 81 | |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 82 | #if defined(OS_POSIX) |
jeremy@chromium.org | 05d4b0a | 2009-01-29 17:51:51 | [diff] [blame] | 83 | // Sets all file descriptors to close on exec except for stdin, stdout |
| 84 | // and stderr. |
| 85 | void SetAllFDsToCloseOnExec(); |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 86 | #endif |
| 87 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 88 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 89 | // Runs the given application name with the given command line. Normally, the |
| 90 | // first command line argument should be the path to the process, and don't |
| 91 | // forget to quote it. |
| 92 | // |
| 93 | // If wait is true, it will block and wait for the other process to finish, |
| 94 | // otherwise, it will just continue asynchronously. |
| 95 | // |
| 96 | // Example (including literal quotes) |
| 97 | // cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" |
| 98 | // |
| 99 | // If process_handle is non-NULL, the process handle of the launched app will be |
| 100 | // stored there on a successful launch. |
| 101 | // NOTE: In this case, the caller is responsible for closing the handle so |
| 102 | // that it doesn't leak! |
| 103 | bool LaunchApp(const std::wstring& cmdline, |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 104 | bool wait, bool start_hidden, ProcessHandle* process_handle); |
| 105 | #elif defined(OS_POSIX) |
| 106 | // Runs the application specified in argv[0] with the command line argv. |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 107 | // Before launching all FDs open in the parent process will be marked as |
| 108 | // close-on-exec. |fds_to_remap| defines a mapping of src fd->dest fd to |
| 109 | // propagate FDs into the child process. |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 110 | // |
| 111 | // As above, if wait is true, execute synchronously. The pid will be stored |
| 112 | // in process_handle if that pointer is non-null. |
| 113 | // |
| 114 | // Note that the first argument in argv must point to the filename, |
| 115 | // and must be fully specified. |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 116 | typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 117 | bool LaunchApp(const std::vector<std::string>& argv, |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 118 | const file_handle_mapping_vector& fds_to_remap, |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 119 | bool wait, ProcessHandle* process_handle); |
| 120 | #endif |
| 121 | |
| 122 | // Execute the application specified by cl. This function delegates to one |
| 123 | // of the above two platform-specific functions. |
| 124 | bool LaunchApp(const CommandLine& cl, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 125 | bool wait, bool start_hidden, ProcessHandle* process_handle); |
| 126 | |
| 127 | // Used to filter processes by process ID. |
| 128 | class ProcessFilter { |
| 129 | public: |
| 130 | // Returns true to indicate set-inclusion and false otherwise. This method |
| 131 | // should not have side-effects and should be idempotent. |
| 132 | virtual bool Includes(uint32 pid, uint32 parent_pid) const = 0; |
avi@google.com | dd950aa | 2008-08-12 21:24:39 | [diff] [blame] | 133 | virtual ~ProcessFilter() { } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | // Returns the number of processes on the machine that are running from the |
| 137 | // given executable name. If filter is non-null, then only processes selected |
| 138 | // by the filter will be counted. |
| 139 | int GetProcessCount(const std::wstring& executable_name, |
| 140 | const ProcessFilter* filter); |
| 141 | |
| 142 | // Attempts to kill all the processes on the current machine that were launched |
| 143 | // from the given executable name, ending them with the given exit code. If |
| 144 | // filter is non-null, then only processes selected by the filter are killed. |
| 145 | // Returns false if all processes were able to be killed off, false if at least |
| 146 | // one couldn't be killed. |
| 147 | bool KillProcesses(const std::wstring& executable_name, int exit_code, |
| 148 | const ProcessFilter* filter); |
| 149 | |
| 150 | // Attempts to kill the process identified by the given process |
| 151 | // entry structure, giving it the specified exit code. If |wait| is true, wait |
| 152 | // for the process to be actually terminated before returning. |
| 153 | // Returns true if this is successful, false otherwise. |
| 154 | bool KillProcess(int process_id, int exit_code, bool wait); |
agl@chromium.org | dfe1486 | 2009-01-22 01:23:11 | [diff] [blame] | 155 | #if defined(OS_WIN) |
| 156 | bool KillProcess(HANDLE process, int exit_code, bool wait); |
| 157 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 158 | |
| 159 | // Get the termination status (exit code) of the process and return true if the |
| 160 | // status indicates the process crashed. It is an error to call this if the |
| 161 | // process hasn't terminated yet. |
| 162 | bool DidProcessCrash(ProcessHandle handle); |
| 163 | |
phajdan.jr@chromium.org | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 164 | // Waits for process to exit. In POSIX systems, if the process hasn't been |
| 165 | // signaled then puts the exit code in |exit_code|; otherwise it's considered |
| 166 | // a failure. On Windows |exit_code| is always filled. Returns true on success, |
| 167 | // and closes |handle| in any case. |
| 168 | bool WaitForExitCode(ProcessHandle handle, int* exit_code); |
| 169 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | // Wait for all the processes based on the named executable to exit. If filter |
| 171 | // is non-null, then only processes selected by the filter are waited on. |
| 172 | // Returns after all processes have exited or wait_milliseconds have expired. |
| 173 | // Returns true if all the processes exited, false otherwise. |
| 174 | bool WaitForProcessesToExit(const std::wstring& executable_name, |
| 175 | int wait_milliseconds, |
| 176 | const ProcessFilter* filter); |
| 177 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 178 | // Wait for a single process to exit. Return true if it exited cleanly within |
| 179 | // the given time limit. |
| 180 | bool WaitForSingleProcess(ProcessHandle handle, |
| 181 | int wait_milliseconds); |
| 182 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 183 | // Waits a certain amount of time (can be 0) for all the processes with a given |
| 184 | // executable name to exit, then kills off any of them that are still around. |
| 185 | // If filter is non-null, then only processes selected by the filter are waited |
| 186 | // on. Killed processes are ended with the given exit code. Returns false if |
| 187 | // any processes needed to be killed, true if they all exited cleanly within |
| 188 | // the wait_milliseconds delay. |
| 189 | bool CleanupProcesses(const std::wstring& executable_name, |
| 190 | int wait_milliseconds, |
| 191 | int exit_code, |
| 192 | const ProcessFilter* filter); |
| 193 | |
| 194 | // This class provides a way to iterate through the list of processes |
| 195 | // on the current machine that were started from the given executable |
| 196 | // name. To use, create an instance and then call NextProcessEntry() |
| 197 | // until it returns false. |
| 198 | class NamedProcessIterator { |
| 199 | public: |
| 200 | NamedProcessIterator(const std::wstring& executable_name, |
| 201 | const ProcessFilter* filter); |
| 202 | ~NamedProcessIterator(); |
| 203 | |
| 204 | // If there's another process that matches the given executable name, |
| 205 | // returns a const pointer to the corresponding PROCESSENTRY32. |
| 206 | // If there are no more matching processes, returns NULL. |
| 207 | // The returned pointer will remain valid until NextProcessEntry() |
| 208 | // is called again or this NamedProcessIterator goes out of scope. |
| 209 | const ProcessEntry* NextProcessEntry(); |
| 210 | |
| 211 | private: |
| 212 | // Determines whether there's another process (regardless of executable) |
| 213 | // left in the list of all processes. Returns true and sets entry_ to |
| 214 | // that process's info if there is one, false otherwise. |
| 215 | bool CheckForNextProcess(); |
| 216 | |
| 217 | bool IncludeEntry(); |
| 218 | |
| 219 | // Initializes a PROCESSENTRY32 data structure so that it's ready for |
| 220 | // use with Process32First/Process32Next. |
| 221 | void InitProcessEntry(ProcessEntry* entry); |
| 222 | |
| 223 | std::wstring executable_name_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 224 | |
| 225 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | HANDLE snapshot_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 227 | bool started_iteration_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 228 | #elif defined(OS_LINUX) |
| 229 | DIR *procfs_dir_; |
| 230 | #elif defined(OS_MACOSX) |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 231 | std::vector<kinfo_proc> kinfo_procs_; |
| 232 | size_t index_of_kinfo_proc_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 233 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 234 | ProcessEntry entry_; |
| 235 | const ProcessFilter* filter_; |
| 236 | |
| 237 | DISALLOW_EVIL_CONSTRUCTORS(NamedProcessIterator); |
| 238 | }; |
| 239 | |
| 240 | // Working Set (resident) memory usage broken down by |
| 241 | // priv (private): These pages (kbytes) cannot be shared with any other process. |
| 242 | // shareable: These pages (kbytes) can be shared with other processes under |
| 243 | // the right circumstances. |
| 244 | // shared : These pages (kbytes) are currently shared with at least one |
| 245 | // other process. |
| 246 | struct WorkingSetKBytes { |
| 247 | size_t priv; |
| 248 | size_t shareable; |
| 249 | size_t shared; |
| 250 | }; |
| 251 | |
| 252 | // Committed (resident + paged) memory usage broken down by |
| 253 | // private: These pages cannot be shared with any other process. |
| 254 | // mapped: These pages are mapped into the view of a section (backed by |
| 255 | // pagefile.sys) |
| 256 | // image: These pages are mapped into the view of an image section (backed by |
| 257 | // file system) |
| 258 | struct CommittedKBytes { |
| 259 | size_t priv; |
| 260 | size_t mapped; |
| 261 | size_t image; |
| 262 | }; |
| 263 | |
| 264 | // Free memory (Megabytes marked as free) in the 2G process address space. |
| 265 | // total : total amount in megabytes marked as free. Maximum value is 2048. |
| 266 | // largest : size of the largest contiguous amount of memory found. It is |
| 267 | // always smaller or equal to FreeMBytes::total. |
| 268 | // largest_ptr: starting address of the largest memory block. |
| 269 | struct FreeMBytes { |
| 270 | size_t total; |
| 271 | size_t largest; |
| 272 | void* largest_ptr; |
| 273 | }; |
| 274 | |
| 275 | // Provides performance metrics for a specified process (CPU usage, memory and |
| 276 | // IO counters). To use it, invoke CreateProcessMetrics() to get an instance |
| 277 | // for a specific process, then access the information with the different get |
| 278 | // methods. |
| 279 | class ProcessMetrics { |
| 280 | public: |
| 281 | // Creates a ProcessMetrics for the specified process. |
| 282 | // The caller owns the returned object. |
| 283 | static ProcessMetrics* CreateProcessMetrics(ProcessHandle process); |
| 284 | |
| 285 | ~ProcessMetrics(); |
| 286 | |
| 287 | // Returns the current space allocated for the pagefile, in bytes (these pages |
| 288 | // may or may not be in memory). |
| 289 | size_t GetPagefileUsage(); |
| 290 | // Returns the peak space allocated for the pagefile, in bytes. |
| 291 | size_t GetPeakPagefileUsage(); |
| 292 | // Returns the current working set size, in bytes. |
| 293 | size_t GetWorkingSetSize(); |
| 294 | // Returns private usage, in bytes. Private bytes is the amount |
| 295 | // of memory currently allocated to a process that cannot be shared. |
| 296 | // Note: returns 0 on unsupported OSes: prior to XP SP2. |
| 297 | size_t GetPrivateBytes(); |
| 298 | // Fills a CommittedKBytes with both resident and paged |
| 299 | // memory usage as per definition of CommittedBytes. |
| 300 | void GetCommittedKBytes(CommittedKBytes* usage); |
| 301 | // Fills a WorkingSetKBytes containing resident private and shared memory |
| 302 | // usage in bytes, as per definition of WorkingSetBytes. |
| 303 | bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage); |
| 304 | |
| 305 | // Computes the current process available memory for allocation. |
| 306 | // It does a linear scan of the address space querying each memory region |
| 307 | // for its free (unallocated) status. It is useful for estimating the memory |
| 308 | // load and fragmentation. |
| 309 | bool CalculateFreeMemory(FreeMBytes* free); |
| 310 | |
| 311 | // Returns the CPU usage in percent since the last time this method was |
| 312 | // called. The first time this method is called it returns 0 and will return |
| 313 | // the actual CPU info on subsequent calls. |
| 314 | // Note that on multi-processor machines, the CPU usage value is for all |
| 315 | // CPUs. So if you have 2 CPUs and your process is using all the cycles |
| 316 | // of 1 CPU and not the other CPU, this method returns 50. |
| 317 | int GetCPUUsage(); |
| 318 | |
| 319 | // Retrieves accounting information for all I/O operations performed by the |
| 320 | // process. |
| 321 | // If IO information is retrieved successfully, the function returns true |
| 322 | // and fills in the IO_COUNTERS passed in. The function returns false |
| 323 | // otherwise. |
| 324 | bool GetIOCounters(IoCounters* io_counters); |
| 325 | |
| 326 | private: |
| 327 | explicit ProcessMetrics(ProcessHandle process); |
| 328 | |
| 329 | ProcessHandle process_; |
| 330 | |
| 331 | int processor_count_; |
| 332 | |
| 333 | // Used to store the previous times so we can compute the CPU usage. |
| 334 | int64 last_time_; |
| 335 | int64 last_system_time_; |
| 336 | |
| 337 | DISALLOW_EVIL_CONSTRUCTORS(ProcessMetrics); |
| 338 | }; |
| 339 | |
| 340 | // Enables low fragmentation heap (LFH) for every heaps of this process. This |
| 341 | // won't have any effect on heaps created after this function call. It will not |
| 342 | // modify data allocated in the heaps before calling this function. So it is |
| 343 | // better to call this function early in initialization and again before |
| 344 | // entering the main loop. |
| 345 | // Note: Returns true on Windows 2000 without doing anything. |
| 346 | bool EnableLowFragmentationHeap(); |
| 347 | |
maruel@google.com | c9d4087 | 2008-09-24 12:58:37 | [diff] [blame] | 348 | // Enable 'terminate on heap corruption' flag. Helps protect against heap |
| 349 | // overflow. Has no effect if the OS doesn't provide the necessary facility. |
| 350 | void EnableTerminationOnHeapCorruption(); |
| 351 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 352 | // If supported on the platform, and the user has sufficent rights, increase |
| 353 | // the current process's scheduling priority to a high priority. |
| 354 | void RaiseProcessToHighPriority(); |
| 355 | |
brettw@google.com | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 356 | } // namespace base |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 357 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 358 | #endif // BASE_PROCESS_UTIL_H_ |