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