maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 1 | // Copyright (c) 2010 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_ |
thakis@chromium.org | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 10 | #pragma once |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 12 | #include "base/basictypes.h" |
| 13 | |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 14 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | #include <windows.h> |
| 16 | #include <tlhelp32.h> |
wtc@chromium.org | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 17 | #elif defined(OS_MACOSX) |
| 18 | // kinfo_proc is defined in <sys/sysctl.h>, but this forward declaration |
| 19 | // is sufficient for the vector<kinfo_proc> below. |
| 20 | struct kinfo_proc; |
avi@chromium.org | 6cfa339 | 2010-07-01 20:11:43 | [diff] [blame] | 21 | // malloc_zone_t is defined in <malloc/malloc.h>, but this forward declaration |
| 22 | // is sufficient for GetPurgeableZone() below. |
| 23 | typedef struct _malloc_zone_t malloc_zone_t; |
thakis@chromium.org | c002879 | 2010-01-12 00:39:15 | [diff] [blame] | 24 | #include <mach/mach.h> |
wtc@chromium.org | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 25 | #elif defined(OS_POSIX) |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 26 | #include <dirent.h> |
| 27 | #include <limits.h> |
| 28 | #include <sys/types.h> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 29 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 31 | #include <list> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 32 | #include <string> |
thestig@chromium.org | 9ec8db0 | 2009-07-21 07:00:13 | [diff] [blame] | 33 | #include <utility> |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 34 | #include <vector> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 35 | |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 36 | #include "base/file_descriptor_shuffle.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 37 | #include "base/process.h" |
| 38 | |
evan@chromium.org | 93f21e4 | 2010-04-01 00:35:15 | [diff] [blame] | 39 | #ifndef NAME_MAX // Solaris and some BSDs have no NAME_MAX |
| 40 | #ifdef MAXNAMLEN |
| 41 | #define NAME_MAX MAXNAMLEN |
| 42 | #else |
| 43 | #define NAME_MAX 256 |
| 44 | #endif |
| 45 | #endif |
| 46 | |
erg@google.com | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 47 | class CommandLine; |
| 48 | class FilePath; |
| 49 | |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 50 | namespace base { |
| 51 | |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 52 | #if defined(OS_WIN) |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 53 | struct ProcessEntry : public PROCESSENTRY32 { |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 54 | ProcessId pid() const { return th32ProcessID; } |
| 55 | ProcessId parent_pid() const { return th32ParentProcessID; } |
| 56 | const wchar_t* exe_file() const { return szExeFile; } |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 57 | }; |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 58 | |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 59 | struct IoCounters : public IO_COUNTERS { |
| 60 | }; |
| 61 | |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame^] | 62 | // Process access masks. These constants provide platform-independent |
| 63 | // definitions for the standard Windows access masks. |
| 64 | // See http://msdn.microsoft.com/en-us/library/ms684880(VS.85).aspx for |
| 65 | // the specific semantics of each mask value. |
| 66 | const uint32 kProcessAccessTerminate = PROCESS_TERMINATE; |
| 67 | const uint32 kProcessAccessCreateThread = PROCESS_CREATE_THREAD; |
| 68 | const uint32 kProcessAccessSetSessionId = PROCESS_SET_SESSIONID; |
| 69 | const uint32 kProcessAccessVMOperation = PROCESS_VM_OPERATION; |
| 70 | const uint32 kProcessAccessVMRead = PROCESS_VM_READ; |
| 71 | const uint32 kProcessAccessVMWrite = PROCESS_VM_WRITE; |
| 72 | const uint32 kProcessAccessDuplicateHandle = PROCESS_DUP_HANDLE; |
| 73 | const uint32 kProcessAccessCreateProcess = PROCESS_CREATE_PROCESS; |
| 74 | const uint32 kProcessAccessSetQuota = PROCESS_SET_QUOTA; |
| 75 | const uint32 kProcessAccessSetInformation = PROCESS_SET_INFORMATION; |
| 76 | const uint32 kProcessAccessQueryInformation = PROCESS_QUERY_INFORMATION; |
| 77 | const uint32 kProcessAccessSuspendResume = PROCESS_SUSPEND_RESUME; |
| 78 | const uint32 kProcessAccessQueryLimitedInfomation = |
| 79 | PROCESS_QUERY_LIMITED_INFORMATION; |
| 80 | const uint32 kProcessAccessWaitForTermination = SYNCHRONIZE; |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 81 | #elif defined(OS_POSIX) |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 82 | |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 83 | struct ProcessEntry { |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 84 | ProcessId pid_; |
| 85 | ProcessId ppid_; |
| 86 | ProcessId gid_; |
| 87 | std::string exe_file_; |
rsimha@chromium.org | c47d81d | 2010-10-05 23:41:04 | [diff] [blame] | 88 | std::vector<std::string> cmd_line_args_; |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 89 | |
| 90 | ProcessId pid() const { return pid_; } |
| 91 | ProcessId parent_pid() const { return ppid_; } |
rsimha@chromium.org | c47d81d | 2010-10-05 23:41:04 | [diff] [blame] | 92 | ProcessId gid() const { return gid_; } |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 93 | const char* exe_file() const { return exe_file_.c_str(); } |
rsimha@chromium.org | c47d81d | 2010-10-05 23:41:04 | [diff] [blame] | 94 | const std::vector<std::string>& cmd_line_args() const { |
| 95 | return cmd_line_args_; |
| 96 | } |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 97 | }; |
| 98 | |
evanm@google.com | 0b100bc8 | 2008-10-14 20:49:16 | [diff] [blame] | 99 | struct IoCounters { |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 100 | uint64_t ReadOperationCount; |
| 101 | uint64_t WriteOperationCount; |
| 102 | uint64_t OtherOperationCount; |
| 103 | uint64_t ReadTransferCount; |
| 104 | uint64_t WriteTransferCount; |
| 105 | uint64_t OtherTransferCount; |
evanm@google.com | 0b100bc8 | 2008-10-14 20:49:16 | [diff] [blame] | 106 | }; |
agl@chromium.org | 3f04f2b | 2009-04-30 19:40:03 | [diff] [blame] | 107 | |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame^] | 108 | // Process access masks. They are not used on Posix because access checking |
| 109 | // does not happen during handle creation. |
| 110 | const uint32 kProcessAccessTerminate = 0; |
| 111 | const uint32 kProcessAccessCreateThread = 0; |
| 112 | const uint32 kProcessAccessSetSessionId = 0; |
| 113 | const uint32 kProcessAccessVMOperation = 0; |
| 114 | const uint32 kProcessAccessVMRead = 0; |
| 115 | const uint32 kProcessAccessVMWrite = 0; |
| 116 | const uint32 kProcessAccessDuplicateHandle = 0; |
| 117 | const uint32 kProcessAccessCreateProcess = 0; |
| 118 | const uint32 kProcessAccessSetQuota = 0; |
| 119 | const uint32 kProcessAccessSetInformation = 0; |
| 120 | const uint32 kProcessAccessQueryInformation = 0; |
| 121 | const uint32 kProcessAccessSuspendResume = 0; |
| 122 | const uint32 kProcessAccessQueryLimitedInfomation = 0; |
| 123 | const uint32 kProcessAccessWaitForTermination = 0; |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 124 | #endif // defined(OS_POSIX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 125 | |
cpu@google.com | eef576f | 2008-11-03 23:28:06 | [diff] [blame] | 126 | // A minimalistic but hopefully cross-platform set of exit codes. |
| 127 | // Do not change the enumeration values or you will break third-party |
| 128 | // installers. |
| 129 | enum { |
thestig@chromium.org | e68639f5 | 2010-04-08 19:52:21 | [diff] [blame] | 130 | PROCESS_END_NORMAL_TERMINATION = 0, |
| 131 | PROCESS_END_KILLED_BY_USER = 1, |
| 132 | PROCESS_END_PROCESS_WAS_HUNG = 2 |
cpu@google.com | eef576f | 2008-11-03 23:28:06 | [diff] [blame] | 133 | }; |
| 134 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 135 | // Returns the id of the current process. |
phajdan.jr@chromium.org | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 136 | ProcessId GetCurrentProcId(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 137 | |
erikkay@google.com | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 138 | // Returns the ProcessHandle of the current process. |
| 139 | ProcessHandle GetCurrentProcessHandle(); |
maruel@chromium.org | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 140 | |
brettw@google.com | 5986ed2 | 2009-02-06 00:19:17 | [diff] [blame] | 141 | // 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] | 142 | // CloseProcessHandle when you are done with it. Returns true on success. |
| 143 | bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle); |
maruel@chromium.org | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 144 | |
phajdan.jr@chromium.org | 5d438dbad | 2009-04-30 08:59:39 | [diff] [blame] | 145 | // Converts a PID to a process handle. On Windows the handle is opened |
| 146 | // with more access rights and must only be used by trusted code. |
| 147 | // You have to close returned handle using CloseProcessHandle. Returns true |
| 148 | // on success. |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame^] | 149 | // TODO(sanjeevr): Replace all calls to OpenPrivilegedProcessHandle with the |
| 150 | // more specific OpenProcessHandleWithAccess method and delete this. |
phajdan.jr@chromium.org | 5d438dbad | 2009-04-30 08:59:39 | [diff] [blame] | 151 | bool OpenPrivilegedProcessHandle(ProcessId pid, ProcessHandle* handle); |
| 152 | |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame^] | 153 | // Converts a PID to a process handle using the desired access flags. Use a |
| 154 | // combination of the kProcessAccess* flags defined above for |access_flags|. |
| 155 | bool OpenProcessHandleWithAccess(ProcessId pid, |
| 156 | uint32 access_flags, |
| 157 | ProcessHandle* handle); |
| 158 | |
brettw@google.com | 5986ed2 | 2009-02-06 00:19:17 | [diff] [blame] | 159 | // Closes the process handle opened by OpenProcessHandle. |
| 160 | void CloseProcessHandle(ProcessHandle process); |
erikkay@google.com | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 161 | |
cpu@google.com | eef576f | 2008-11-03 23:28:06 | [diff] [blame] | 162 | // Returns the unique ID for the specified process. This is functionally the |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 163 | // same as Windows' GetProcessId(), but works on versions of Windows before |
| 164 | // Win XP SP1 as well. |
phajdan.jr@chromium.org | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 165 | ProcessId GetProcId(ProcessHandle process); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 166 | |
dkegel@google.com | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 167 | #if defined(OS_LINUX) |
| 168 | // Returns the ID for the parent of the given process. |
| 169 | ProcessId GetParentProcessId(ProcessHandle process); |
| 170 | |
| 171 | // Returns the path to the executable of the given process. |
| 172 | FilePath GetProcessExecutablePath(ProcessHandle process); |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 173 | |
| 174 | // Parse the data found in /proc/<pid>/stat and return the sum of the |
| 175 | // CPU-related ticks. Returns -1 on parse error. |
| 176 | // Exposed for testing. |
| 177 | int ParseProcStatCPU(const std::string& input); |
thestig@chromium.org | e5856a7a | 2009-12-10 02:08:10 | [diff] [blame] | 178 | |
| 179 | static const char kAdjustOOMScoreSwitch[] = "--adjust-oom-score"; |
| 180 | |
| 181 | // This adjusts /proc/process/oom_adj so the Linux OOM killer will prefer |
| 182 | // certain process types over others. The range for the adjustment is |
| 183 | // [-17,15], with [0,15] being user accessible. |
| 184 | bool AdjustOOMScore(ProcessId process, int score); |
dkegel@google.com | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 185 | #endif |
| 186 | |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 187 | #if defined(OS_POSIX) |
agl@chromium.org | 3f04f2b | 2009-04-30 19:40:03 | [diff] [blame] | 188 | // Close all file descriptors, expect those which are a destination in the |
| 189 | // given multimap. Only call this function in a child process where you know |
| 190 | // that there aren't any other threads. |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 191 | void CloseSuperfluousFds(const InjectiveMultimap& saved_map); |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 192 | #endif |
| 193 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 194 | #if defined(OS_WIN) |
tommi@chromium.org | 48dc9e1 | 2010-08-26 19:49:57 | [diff] [blame] | 195 | |
| 196 | enum IntegrityLevel { |
| 197 | INTEGRITY_UNKNOWN, |
| 198 | LOW_INTEGRITY, |
| 199 | MEDIUM_INTEGRITY, |
| 200 | HIGH_INTEGRITY, |
| 201 | }; |
| 202 | // Determine the integrity level of the specified process. Returns false |
| 203 | // if the system does not support integrity levels (pre-Vista) or in the case |
| 204 | // of an underlying system failure. |
| 205 | bool GetProcessIntegrityLevel(ProcessHandle process, IntegrityLevel *level); |
| 206 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 207 | // Runs the given application name with the given command line. Normally, the |
| 208 | // first command line argument should be the path to the process, and don't |
| 209 | // forget to quote it. |
| 210 | // |
| 211 | // If wait is true, it will block and wait for the other process to finish, |
| 212 | // otherwise, it will just continue asynchronously. |
| 213 | // |
| 214 | // Example (including literal quotes) |
| 215 | // cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" |
| 216 | // |
| 217 | // If process_handle is non-NULL, the process handle of the launched app will be |
| 218 | // stored there on a successful launch. |
| 219 | // NOTE: In this case, the caller is responsible for closing the handle so |
| 220 | // that it doesn't leak! |
| 221 | bool LaunchApp(const std::wstring& cmdline, |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 222 | bool wait, bool start_hidden, ProcessHandle* process_handle); |
cpu@chromium.org | e50130b | 2010-02-01 03:28:47 | [diff] [blame] | 223 | |
finnur@chromium.org | 81e3260 | 2010-09-07 14:01:31 | [diff] [blame] | 224 | // Same as LaunchApp, except allows the new process to inherit handles of the |
| 225 | // parent process. |
| 226 | bool LaunchAppWithHandleInheritance(const std::wstring& cmdline, |
| 227 | bool wait, |
| 228 | bool start_hidden, |
| 229 | ProcessHandle* process_handle); |
| 230 | |
cpu@chromium.org | e50130b | 2010-02-01 03:28:47 | [diff] [blame] | 231 | // Runs the given application name with the given command line as if the user |
| 232 | // represented by |token| had launched it. The caveats about |cmdline| and |
| 233 | // |process_handle| explained for LaunchApp above apply as well. |
| 234 | // |
| 235 | // Whether the application is visible on the interactive desktop depends on |
| 236 | // the token belonging to an interactive logon session. |
| 237 | // |
| 238 | // To avoid hard to diagnose problems, this function internally loads the |
| 239 | // environment variables associated with the user and if this operation fails |
| 240 | // the entire call fails as well. |
| 241 | bool LaunchAppAsUser(UserTokenHandle token, const std::wstring& cmdline, |
| 242 | bool start_hidden, ProcessHandle* process_handle); |
| 243 | |
gwilson@google.com | c020ddc | 2010-02-18 23:01:52 | [diff] [blame] | 244 | // Has the same behavior as LaunchAppAsUser, but offers the boolean option to |
finnur@chromium.org | 81e3260 | 2010-09-07 14:01:31 | [diff] [blame] | 245 | // use an empty string for the desktop name and a boolean for allowing the |
| 246 | // child process to inherit handles from its parent. |
gwilson@google.com | c020ddc | 2010-02-18 23:01:52 | [diff] [blame] | 247 | bool LaunchAppAsUser(UserTokenHandle token, const std::wstring& cmdline, |
| 248 | bool start_hidden, ProcessHandle* process_handle, |
finnur@chromium.org | 81e3260 | 2010-09-07 14:01:31 | [diff] [blame] | 249 | bool empty_desktop_name, bool inherit_handles); |
gwilson@google.com | c020ddc | 2010-02-18 23:01:52 | [diff] [blame] | 250 | |
| 251 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 252 | #elif defined(OS_POSIX) |
| 253 | // Runs the application specified in argv[0] with the command line argv. |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 254 | // Before launching all FDs open in the parent process will be marked as |
| 255 | // close-on-exec. |fds_to_remap| defines a mapping of src fd->dest fd to |
| 256 | // propagate FDs into the child process. |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 257 | // |
| 258 | // As above, if wait is true, execute synchronously. The pid will be stored |
| 259 | // in process_handle if that pointer is non-null. |
| 260 | // |
mattm@chromium.org | b74d21b3 | 2009-07-17 19:36:00 | [diff] [blame] | 261 | // Note that the first argument in argv must point to the executable filename. |
| 262 | // If the filename is not fully specified, PATH will be searched. |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 263 | typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 264 | bool LaunchApp(const std::vector<std::string>& argv, |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 265 | const file_handle_mapping_vector& fds_to_remap, |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 266 | bool wait, ProcessHandle* process_handle); |
stuartmorgan@google.com | 2aea9e09 | 2009-08-06 20:03:01 | [diff] [blame] | 267 | |
thakis@chromium.org | c002879 | 2010-01-12 00:39:15 | [diff] [blame] | 268 | // Similar to the above, but also (un)set environment variables in child process |
thestig@chromium.org | 9ec8db0 | 2009-07-21 07:00:13 | [diff] [blame] | 269 | // through |environ|. |
jam@chromium.org | 3d2217d | 2009-11-23 21:26:47 | [diff] [blame] | 270 | typedef std::vector<std::pair<std::string, std::string> > environment_vector; |
thestig@chromium.org | 9ec8db0 | 2009-07-21 07:00:13 | [diff] [blame] | 271 | bool LaunchApp(const std::vector<std::string>& argv, |
| 272 | const environment_vector& environ, |
| 273 | const file_handle_mapping_vector& fds_to_remap, |
| 274 | bool wait, ProcessHandle* process_handle); |
thakis@chromium.org | c002879 | 2010-01-12 00:39:15 | [diff] [blame] | 275 | |
rsimha@chromium.org | 61b93f88f | 2010-09-22 17:28:30 | [diff] [blame] | 276 | // Similar to the above two methods, but starts the child process in a process |
| 277 | // group of its own, instead of allowing it to inherit the parent's process |
| 278 | // group. The pgid of the child process will be the same as its pid. |
| 279 | bool LaunchAppInNewProcessGroup(const std::vector<std::string>& argv, |
| 280 | const environment_vector& environ, |
| 281 | const file_handle_mapping_vector& fds_to_remap, |
| 282 | bool wait, ProcessHandle* process_handle); |
| 283 | |
agl@chromium.org | ef73044e | 2010-03-11 15:25:54 | [diff] [blame] | 284 | // AlterEnvironment returns a modified environment vector, constructed from the |
| 285 | // given environment and the list of changes given in |changes|. Each key in |
| 286 | // the environment is matched against the first element of the pairs. In the |
| 287 | // event of a match, the value is replaced by the second of the pair, unless |
| 288 | // the second is empty, in which case the key-value is removed. |
| 289 | // |
| 290 | // The returned array is allocated using new[] and must be freed by the caller. |
| 291 | char** AlterEnvironment(const environment_vector& changes, |
| 292 | const char* const* const env); |
thestig@chromium.org | 9ec8db0 | 2009-07-21 07:00:13 | [diff] [blame] | 293 | #endif // defined(OS_POSIX) |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 294 | |
jcampan@chromium.org | 1e31211 | 2009-04-21 21:44:12 | [diff] [blame] | 295 | // Executes the application specified by cl. This function delegates to one |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 296 | // of the above two platform-specific functions. |
| 297 | bool LaunchApp(const CommandLine& cl, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 298 | bool wait, bool start_hidden, ProcessHandle* process_handle); |
| 299 | |
jcampan@chromium.org | 1e31211 | 2009-04-21 21:44:12 | [diff] [blame] | 300 | // 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] | 301 | // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true |
| 302 | // on success (application launched and exited cleanly, with exit code |
maruel@chromium.org | 96878a21 | 2010-06-10 18:26:33 | [diff] [blame] | 303 | // indicating success). |
phajdan.jr@chromium.org | c0b210ee | 2009-04-17 09:57:52 | [diff] [blame] | 304 | bool GetAppOutput(const CommandLine& cl, std::string* output); |
phajdan.jr@chromium.org | c0b210ee | 2009-04-17 09:57:52 | [diff] [blame] | 305 | |
viettrungluu@chromium.org | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 306 | #if defined(OS_POSIX) |
| 307 | // A restricted version of |GetAppOutput()| which (a) clears the environment, |
| 308 | // and (b) stores at most |max_output| bytes; also, it doesn't search the path |
| 309 | // for the command. |
| 310 | bool GetAppOutputRestricted(const CommandLine& cl, |
| 311 | std::string* output, size_t max_output); |
| 312 | #endif |
| 313 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 314 | // Used to filter processes by process ID. |
| 315 | class ProcessFilter { |
| 316 | public: |
| 317 | // Returns true to indicate set-inclusion and false otherwise. This method |
| 318 | // should not have side-effects and should be idempotent. |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 319 | virtual bool Includes(const ProcessEntry& entry) const = 0; |
ziadh@chromium.org | 695092f | 2010-08-02 16:34:16 | [diff] [blame] | 320 | |
| 321 | protected: |
| 322 | virtual ~ProcessFilter() {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | // Returns the number of processes on the machine that are running from the |
| 326 | // given executable name. If filter is non-null, then only processes selected |
| 327 | // by the filter will be counted. |
| 328 | int GetProcessCount(const std::wstring& executable_name, |
| 329 | const ProcessFilter* filter); |
| 330 | |
| 331 | // Attempts to kill all the processes on the current machine that were launched |
| 332 | // from the given executable name, ending them with the given exit code. If |
| 333 | // filter is non-null, then only processes selected by the filter are killed. |
rsimha@chromium.org | 61b93f88f | 2010-09-22 17:28:30 | [diff] [blame] | 334 | // Returns true if all processes were able to be killed off, false if at least |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 335 | // one couldn't be killed. |
| 336 | bool KillProcesses(const std::wstring& executable_name, int exit_code, |
| 337 | const ProcessFilter* filter); |
| 338 | |
| 339 | // Attempts to kill the process identified by the given process |
| 340 | // entry structure, giving it the specified exit code. If |wait| is true, wait |
| 341 | // for the process to be actually terminated before returning. |
| 342 | // Returns true if this is successful, false otherwise. |
stoyan@chromium.org | cd4fd15 | 2009-02-09 19:28:41 | [diff] [blame] | 343 | bool KillProcess(ProcessHandle process, int exit_code, bool wait); |
rsimha@chromium.org | 61b93f88f | 2010-09-22 17:28:30 | [diff] [blame] | 344 | |
| 345 | #if defined(OS_POSIX) |
| 346 | // Attempts to kill the process group identified by |process_group_id|. Returns |
| 347 | // true on success. |
| 348 | bool KillProcessGroup(ProcessHandle process_group_id); |
| 349 | #endif |
| 350 | |
agl@chromium.org | dfe1486 | 2009-01-22 01:23:11 | [diff] [blame] | 351 | #if defined(OS_WIN) |
phajdan.jr@chromium.org | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 352 | bool KillProcessById(ProcessId process_id, int exit_code, bool wait); |
agl@chromium.org | dfe1486 | 2009-01-22 01:23:11 | [diff] [blame] | 353 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 354 | |
| 355 | // 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] | 356 | // status indicates the process crashed. |child_exited| is set to true iff the |
| 357 | // child process has terminated. (|child_exited| may be NULL.) |
agl@chromium.org | 140a7cd | 2009-04-28 01:37:23 | [diff] [blame] | 358 | bool DidProcessCrash(bool* child_exited, ProcessHandle handle); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 359 | |
phajdan.jr@chromium.org | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 360 | // Waits for process to exit. In POSIX systems, if the process hasn't been |
| 361 | // signaled then puts the exit code in |exit_code|; otherwise it's considered |
| 362 | // a failure. On Windows |exit_code| is always filled. Returns true on success, |
| 363 | // and closes |handle| in any case. |
| 364 | bool WaitForExitCode(ProcessHandle handle, int* exit_code); |
| 365 | |
phajdan.jr@chromium.org | 8004e68 | 2010-03-16 07:41:22 | [diff] [blame] | 366 | // Waits for process to exit. If it did exit within |timeout_milliseconds|, |
| 367 | // then puts the exit code in |exit_code|, closes |handle|, and returns true. |
| 368 | // In POSIX systems, if the process has been signaled then |exit_code| is set |
| 369 | // to -1. Returns false on failure (the caller is then responsible for closing |
| 370 | // |handle|). |
| 371 | bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, |
| 372 | int64 timeout_milliseconds); |
| 373 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 374 | // Wait for all the processes based on the named executable to exit. If filter |
| 375 | // is non-null, then only processes selected by the filter are waited on. |
| 376 | // Returns after all processes have exited or wait_milliseconds have expired. |
| 377 | // Returns true if all the processes exited, false otherwise. |
| 378 | bool WaitForProcessesToExit(const std::wstring& executable_name, |
phajdan.jr@chromium.org | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 379 | int64 wait_milliseconds, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 380 | const ProcessFilter* filter); |
| 381 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 382 | // Wait for a single process to exit. Return true if it exited cleanly within |
| 383 | // the given time limit. |
| 384 | bool WaitForSingleProcess(ProcessHandle handle, |
phajdan.jr@chromium.org | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 385 | int64 wait_milliseconds); |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 386 | |
phajdan.jr@chromium.org | 076bf0b6 | 2009-03-04 20:57:58 | [diff] [blame] | 387 | // Returns true when |wait_milliseconds| have elapsed and the process |
| 388 | // is still running. |
phajdan.jr@chromium.org | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 389 | bool CrashAwareSleep(ProcessHandle handle, int64 wait_milliseconds); |
phajdan.jr@chromium.org | 076bf0b6 | 2009-03-04 20:57:58 | [diff] [blame] | 390 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 391 | // Waits a certain amount of time (can be 0) for all the processes with a given |
| 392 | // executable name to exit, then kills off any of them that are still around. |
| 393 | // If filter is non-null, then only processes selected by the filter are waited |
| 394 | // on. Killed processes are ended with the given exit code. Returns false if |
| 395 | // any processes needed to be killed, true if they all exited cleanly within |
| 396 | // the wait_milliseconds delay. |
| 397 | bool CleanupProcesses(const std::wstring& executable_name, |
phajdan.jr@chromium.org | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 398 | int64 wait_milliseconds, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 399 | int exit_code, |
| 400 | const ProcessFilter* filter); |
| 401 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 402 | // This class provides a way to iterate through a list of processes on the |
| 403 | // current machine with a specified filter. |
| 404 | // To use, create an instance and then call NextProcessEntry() until it returns |
| 405 | // false. |
| 406 | class ProcessIterator { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 407 | public: |
maruel@chromium.org | baead4f | 2010-06-11 19:10:30 | [diff] [blame] | 408 | typedef std::list<ProcessEntry> ProcessEntries; |
| 409 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 410 | explicit ProcessIterator(const ProcessFilter* filter); |
| 411 | virtual ~ProcessIterator(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 412 | |
| 413 | // If there's another process that matches the given executable name, |
| 414 | // returns a const pointer to the corresponding PROCESSENTRY32. |
| 415 | // If there are no more matching processes, returns NULL. |
| 416 | // The returned pointer will remain valid until NextProcessEntry() |
| 417 | // is called again or this NamedProcessIterator goes out of scope. |
| 418 | const ProcessEntry* NextProcessEntry(); |
| 419 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 420 | // Takes a snapshot of all the ProcessEntry found. |
maruel@chromium.org | baead4f | 2010-06-11 19:10:30 | [diff] [blame] | 421 | ProcessEntries Snapshot(); |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 422 | |
| 423 | protected: |
| 424 | virtual bool IncludeEntry(); |
| 425 | const ProcessEntry& entry() { return entry_; } |
| 426 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 427 | private: |
| 428 | // Determines whether there's another process (regardless of executable) |
| 429 | // left in the list of all processes. Returns true and sets entry_ to |
| 430 | // that process's info if there is one, false otherwise. |
| 431 | bool CheckForNextProcess(); |
| 432 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 433 | // Initializes a PROCESSENTRY32 data structure so that it's ready for |
| 434 | // use with Process32First/Process32Next. |
| 435 | void InitProcessEntry(ProcessEntry* entry); |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 436 | |
| 437 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 438 | HANDLE snapshot_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 439 | bool started_iteration_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 440 | #elif defined(OS_MACOSX) |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 441 | std::vector<kinfo_proc> kinfo_procs_; |
| 442 | size_t index_of_kinfo_proc_; |
wtc@chromium.org | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 443 | #elif defined(OS_POSIX) |
| 444 | DIR *procfs_dir_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 445 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 446 | ProcessEntry entry_; |
| 447 | const ProcessFilter* filter_; |
| 448 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 449 | DISALLOW_COPY_AND_ASSIGN(ProcessIterator); |
| 450 | }; |
| 451 | |
| 452 | // This class provides a way to iterate through the list of processes |
| 453 | // on the current machine that were started from the given executable |
| 454 | // name. To use, create an instance and then call NextProcessEntry() |
| 455 | // until it returns false. |
| 456 | class NamedProcessIterator : public ProcessIterator { |
| 457 | public: |
| 458 | NamedProcessIterator(const std::wstring& executable_name, |
| 459 | const ProcessFilter* filter); |
| 460 | virtual ~NamedProcessIterator(); |
| 461 | |
| 462 | protected: |
| 463 | virtual bool IncludeEntry(); |
| 464 | |
| 465 | private: |
| 466 | std::wstring executable_name_; |
| 467 | |
| 468 | DISALLOW_COPY_AND_ASSIGN(NamedProcessIterator); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 469 | }; |
| 470 | |
| 471 | // Working Set (resident) memory usage broken down by |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 472 | // |
| 473 | // On Windows: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 474 | // priv (private): These pages (kbytes) cannot be shared with any other process. |
| 475 | // shareable: These pages (kbytes) can be shared with other processes under |
| 476 | // the right circumstances. |
| 477 | // shared : These pages (kbytes) are currently shared with at least one |
| 478 | // other process. |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 479 | // |
| 480 | // On Linux: |
| 481 | // priv: Pages mapped only by this process |
| 482 | // shared: PSS or 0 if the kernel doesn't support this |
| 483 | // shareable: 0 |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 484 | // |
| 485 | // On OS X: TODO(thakis): Revise. |
| 486 | // priv: Memory. |
| 487 | // shared: 0 |
| 488 | // shareable: 0 |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 489 | struct WorkingSetKBytes { |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 490 | WorkingSetKBytes() : priv(0), shareable(0), shared(0) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 491 | size_t priv; |
| 492 | size_t shareable; |
| 493 | size_t shared; |
| 494 | }; |
| 495 | |
| 496 | // Committed (resident + paged) memory usage broken down by |
| 497 | // private: These pages cannot be shared with any other process. |
| 498 | // mapped: These pages are mapped into the view of a section (backed by |
| 499 | // pagefile.sys) |
| 500 | // image: These pages are mapped into the view of an image section (backed by |
| 501 | // file system) |
| 502 | struct CommittedKBytes { |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 503 | CommittedKBytes() : priv(0), mapped(0), image(0) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 504 | size_t priv; |
| 505 | size_t mapped; |
| 506 | size_t image; |
| 507 | }; |
| 508 | |
| 509 | // Free memory (Megabytes marked as free) in the 2G process address space. |
| 510 | // total : total amount in megabytes marked as free. Maximum value is 2048. |
| 511 | // largest : size of the largest contiguous amount of memory found. It is |
| 512 | // always smaller or equal to FreeMBytes::total. |
| 513 | // largest_ptr: starting address of the largest memory block. |
| 514 | struct FreeMBytes { |
| 515 | size_t total; |
| 516 | size_t largest; |
| 517 | void* largest_ptr; |
| 518 | }; |
| 519 | |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 520 | // Convert a POSIX timeval to microseconds. |
| 521 | int64 TimeValToMicroseconds(const struct timeval& tv); |
| 522 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 523 | // Provides performance metrics for a specified process (CPU usage, memory and |
| 524 | // IO counters). To use it, invoke CreateProcessMetrics() to get an instance |
| 525 | // for a specific process, then access the information with the different get |
| 526 | // methods. |
| 527 | class ProcessMetrics { |
| 528 | public: |
| 529 | // Creates a ProcessMetrics for the specified process. |
| 530 | // The caller owns the returned object. |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 531 | #if !defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 532 | static ProcessMetrics* CreateProcessMetrics(ProcessHandle process); |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 533 | #else |
| 534 | class PortProvider { |
| 535 | public: |
thakis@chromium.org | b2e8e08 | 2009-12-21 17:44:20 | [diff] [blame] | 536 | // Should return the mach task for |process| if possible, or else |
| 537 | // |MACH_PORT_NULL|. Only processes that this returns tasks for will have |
| 538 | // metrics on OS X (except for the current process, which always gets |
| 539 | // metrics). |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 540 | virtual mach_port_t TaskForPid(ProcessHandle process) const = 0; |
| 541 | }; |
| 542 | |
| 543 | // The port provider needs to outlive the ProcessMetrics object returned by |
| 544 | // this function. If NULL is passed as provider, the returned object |
| 545 | // only returns valid metrics if |process| is the current process. |
| 546 | static ProcessMetrics* CreateProcessMetrics(ProcessHandle process, |
| 547 | PortProvider* port_provider); |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 548 | #endif // !defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 549 | |
| 550 | ~ProcessMetrics(); |
| 551 | |
| 552 | // Returns the current space allocated for the pagefile, in bytes (these pages |
thomasvl@chromium.org | 796da7c | 2009-06-11 12:45:45 | [diff] [blame] | 553 | // may or may not be in memory). On Linux, this returns the total virtual |
| 554 | // memory size. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 555 | size_t GetPagefileUsage() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 556 | // Returns the peak space allocated for the pagefile, in bytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 557 | size_t GetPeakPagefileUsage() const; |
thomasvl@chromium.org | 796da7c | 2009-06-11 12:45:45 | [diff] [blame] | 558 | // Returns the current working set size, in bytes. On Linux, this returns |
| 559 | // the resident set size. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 560 | size_t GetWorkingSetSize() const; |
tc@google.com | 0c557f1 | 2009-05-11 23:35:52 | [diff] [blame] | 561 | // Returns the peak working set size, in bytes. |
| 562 | size_t GetPeakWorkingSetSize() const; |
erg@chromium.org | 98947a0 | 2010-05-11 17:46:08 | [diff] [blame] | 563 | // Returns private and sharedusage, in bytes. Private bytes is the amount of |
| 564 | // memory currently allocated to a process that cannot be shared. Returns |
| 565 | // false on platform specific error conditions. Note: |private_bytes| |
| 566 | // returns 0 on unsupported OSes: prior to XP SP2. |
| 567 | bool GetMemoryBytes(size_t* private_bytes, |
| 568 | size_t* shared_bytes); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 569 | // Fills a CommittedKBytes with both resident and paged |
| 570 | // memory usage as per definition of CommittedBytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 571 | void GetCommittedKBytes(CommittedKBytes* usage) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 572 | // Fills a WorkingSetKBytes containing resident private and shared memory |
| 573 | // usage in bytes, as per definition of WorkingSetBytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 574 | bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 575 | |
| 576 | // Computes the current process available memory for allocation. |
| 577 | // It does a linear scan of the address space querying each memory region |
| 578 | // for its free (unallocated) status. It is useful for estimating the memory |
| 579 | // load and fragmentation. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 580 | bool CalculateFreeMemory(FreeMBytes* free) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 581 | |
| 582 | // Returns the CPU usage in percent since the last time this method was |
| 583 | // called. The first time this method is called it returns 0 and will return |
| 584 | // the actual CPU info on subsequent calls. |
thakis@chromium.org | 022eab6 | 2010-01-13 04:55:06 | [diff] [blame] | 585 | // On Windows, the CPU usage value is for all CPUs. So if you have 2 CPUs and |
| 586 | // your process is using all the cycles of 1 CPU and not the other CPU, this |
| 587 | // method returns 50. |
| 588 | double GetCPUUsage(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 589 | |
| 590 | // Retrieves accounting information for all I/O operations performed by the |
| 591 | // process. |
| 592 | // If IO information is retrieved successfully, the function returns true |
| 593 | // and fills in the IO_COUNTERS passed in. The function returns false |
| 594 | // otherwise. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 595 | bool GetIOCounters(IoCounters* io_counters) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 596 | |
| 597 | private: |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 598 | #if !defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 599 | explicit ProcessMetrics(ProcessHandle process); |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 600 | #else |
| 601 | ProcessMetrics(ProcessHandle process, PortProvider* port_provider); |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 602 | #endif // !defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 603 | |
| 604 | ProcessHandle process_; |
| 605 | |
| 606 | int processor_count_; |
| 607 | |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 608 | // Used to store the previous times and CPU usage counts so we can |
| 609 | // compute the CPU usage between calls. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 610 | int64 last_time_; |
| 611 | int64 last_system_time_; |
| 612 | |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 613 | #if defined(OS_MACOSX) |
| 614 | // Queries the port provider if it's set. |
| 615 | mach_port_t TaskForPid(ProcessHandle process) const; |
| 616 | |
| 617 | PortProvider* port_provider_; |
pvalchev@google.com | 66700d4 | 2010-03-10 07:46:43 | [diff] [blame] | 618 | #elif defined(OS_POSIX) |
| 619 | // Jiffie count at the last_time_ we updated. |
| 620 | int last_cpu_; |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 621 | #endif // defined(OS_MACOSX) |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 622 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 623 | DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 624 | }; |
| 625 | |
sgk@chromium.org | ed26d94 | 2009-11-09 06:57:28 | [diff] [blame] | 626 | // Returns the memory commited by the system in KBytes. |
| 627 | // Returns 0 if it can't compute the commit charge. |
| 628 | size_t GetSystemCommitCharge(); |
| 629 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 630 | // Enables low fragmentation heap (LFH) for every heaps of this process. This |
| 631 | // won't have any effect on heaps created after this function call. It will not |
| 632 | // modify data allocated in the heaps before calling this function. So it is |
| 633 | // better to call this function early in initialization and again before |
| 634 | // entering the main loop. |
| 635 | // Note: Returns true on Windows 2000 without doing anything. |
| 636 | bool EnableLowFragmentationHeap(); |
| 637 | |
maruel@chromium.org | d6fc9fd | 2009-10-27 18:03:47 | [diff] [blame] | 638 | // Enables 'terminate on heap corruption' flag. Helps protect against heap |
maruel@google.com | c9d4087 | 2008-09-24 12:58:37 | [diff] [blame] | 639 | // overflow. Has no effect if the OS doesn't provide the necessary facility. |
| 640 | void EnableTerminationOnHeapCorruption(); |
| 641 | |
avi@chromium.org | cccb2121 | 2009-11-12 20:39:56 | [diff] [blame] | 642 | #if !defined(OS_WIN) |
| 643 | // Turns on process termination if memory runs out. This is handled on Windows |
| 644 | // inside RegisterInvalidParamHandler(). |
| 645 | void EnableTerminationOnOutOfMemory(); |
avi@chromium.org | 6cfa339 | 2010-07-01 20:11:43 | [diff] [blame] | 646 | #if defined(OS_MACOSX) |
| 647 | // Exposed for testing. |
| 648 | malloc_zone_t* GetPurgeableZone(); |
| 649 | #endif |
avi@chromium.org | cccb2121 | 2009-11-12 20:39:56 | [diff] [blame] | 650 | #endif |
| 651 | |
maruel@chromium.org | d6fc9fd | 2009-10-27 18:03:47 | [diff] [blame] | 652 | #if defined(UNIT_TEST) |
| 653 | // Enables stack dump to console output on exception and signals. |
| 654 | // When enabled, the process will quit immediately. This is meant to be used in |
| 655 | // unit_tests only! |
| 656 | bool EnableInProcessStackDumping(); |
| 657 | #endif // defined(UNIT_TEST) |
| 658 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 659 | // If supported on the platform, and the user has sufficent rights, increase |
| 660 | // the current process's scheduling priority to a high priority. |
| 661 | void RaiseProcessToHighPriority(); |
| 662 | |
mark@chromium.org | e9a8c19f | 2009-09-03 21:27:36 | [diff] [blame] | 663 | #if defined(OS_MACOSX) |
| 664 | // Restore the default exception handler, setting it to Apple Crash Reporter |
| 665 | // (ReportCrash). When forking and execing a new process, the child will |
| 666 | // inherit the parent's exception ports, which may be set to the Breakpad |
| 667 | // instance running inside the parent. The parent's Breakpad instance should |
| 668 | // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
| 669 | // in the child after forking will restore the standard exception handler. |
| 670 | // See http://crbug.com/20371/ for more details. |
| 671 | void RestoreDefaultExceptionHandler(); |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 672 | #endif // defined(OS_MACOSX) |
mark@chromium.org | e9a8c19f | 2009-09-03 21:27:36 | [diff] [blame] | 673 | |
brettw@google.com | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 674 | } // namespace base |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 675 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 676 | #endif // BASE_PROCESS_UTIL_H_ |