tbarzic@chromium.org | 927054d | 2012-01-21 01:48:25 | [diff] [blame] | 1 | // Copyright (c) 2012 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" |
tedvessenes@gmail.com | 7e1117c | 2012-03-31 20:42:41 | [diff] [blame] | 13 | #include "base/time.h" |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 14 | |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 15 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | #include <windows.h> |
| 17 | #include <tlhelp32.h> |
robert.nagy@gmail.com | af82436 | 2011-12-04 14:19:41 | [diff] [blame] | 18 | #elif defined(OS_MACOSX) || defined(OS_BSD) |
wtc@chromium.org | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 19 | // kinfo_proc is defined in <sys/sysctl.h>, but this forward declaration |
| 20 | // is sufficient for the vector<kinfo_proc> below. |
| 21 | struct kinfo_proc; |
avi@chromium.org | 6cfa339 | 2010-07-01 20:11:43 | [diff] [blame] | 22 | // malloc_zone_t is defined in <malloc/malloc.h>, but this forward declaration |
| 23 | // is sufficient for GetPurgeableZone() below. |
| 24 | typedef struct _malloc_zone_t malloc_zone_t; |
robert.nagy@gmail.com | af82436 | 2011-12-04 14:19:41 | [diff] [blame] | 25 | #if !defined(OS_BSD) |
thakis@chromium.org | c002879 | 2010-01-12 00:39:15 | [diff] [blame] | 26 | #include <mach/mach.h> |
mark@chromium.org | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 27 | #endif |
wtc@chromium.org | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 28 | #elif defined(OS_POSIX) |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 29 | #include <dirent.h> |
| 30 | #include <limits.h> |
| 31 | #include <sys/types.h> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 32 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 33 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 34 | #include <list> |
mcgrathr@chromium.org | 77540fdf | 2011-11-15 18:49:10 | [diff] [blame] | 35 | #include <set> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 36 | #include <string> |
thestig@chromium.org | 9ec8db0 | 2009-07-21 07:00:13 | [diff] [blame] | 37 | #include <utility> |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 38 | #include <vector> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 39 | |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 40 | #include "base/base_export.h" |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 41 | #include "base/file_descriptor_shuffle.h" |
avi@chromium.org | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 42 | #include "base/file_path.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | #include "base/process.h" |
| 44 | |
erg@google.com | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 45 | class CommandLine; |
erg@google.com | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 46 | |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 47 | namespace base { |
| 48 | |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 49 | #if defined(OS_WIN) |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 50 | struct ProcessEntry : public PROCESSENTRY32 { |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 51 | ProcessId pid() const { return th32ProcessID; } |
| 52 | ProcessId parent_pid() const { return th32ParentProcessID; } |
| 53 | const wchar_t* exe_file() const { return szExeFile; } |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 54 | }; |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 55 | |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 56 | struct IoCounters : public IO_COUNTERS { |
| 57 | }; |
| 58 | |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame] | 59 | // Process access masks. These constants provide platform-independent |
| 60 | // definitions for the standard Windows access masks. |
| 61 | // See http://msdn.microsoft.com/en-us/library/ms684880(VS.85).aspx for |
| 62 | // the specific semantics of each mask value. |
| 63 | const uint32 kProcessAccessTerminate = PROCESS_TERMINATE; |
| 64 | const uint32 kProcessAccessCreateThread = PROCESS_CREATE_THREAD; |
| 65 | const uint32 kProcessAccessSetSessionId = PROCESS_SET_SESSIONID; |
| 66 | const uint32 kProcessAccessVMOperation = PROCESS_VM_OPERATION; |
| 67 | const uint32 kProcessAccessVMRead = PROCESS_VM_READ; |
| 68 | const uint32 kProcessAccessVMWrite = PROCESS_VM_WRITE; |
| 69 | const uint32 kProcessAccessDuplicateHandle = PROCESS_DUP_HANDLE; |
| 70 | const uint32 kProcessAccessCreateProcess = PROCESS_CREATE_PROCESS; |
| 71 | const uint32 kProcessAccessSetQuota = PROCESS_SET_QUOTA; |
| 72 | const uint32 kProcessAccessSetInformation = PROCESS_SET_INFORMATION; |
| 73 | const uint32 kProcessAccessQueryInformation = PROCESS_QUERY_INFORMATION; |
| 74 | const uint32 kProcessAccessSuspendResume = PROCESS_SUSPEND_RESUME; |
| 75 | const uint32 kProcessAccessQueryLimitedInfomation = |
| 76 | PROCESS_QUERY_LIMITED_INFORMATION; |
| 77 | const uint32 kProcessAccessWaitForTermination = SYNCHRONIZE; |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 78 | #elif defined(OS_POSIX) |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 79 | |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 80 | struct ProcessEntry { |
erg@google.com | bbf94a3 | 2010-10-13 17:44:15 | [diff] [blame] | 81 | ProcessEntry(); |
| 82 | ~ProcessEntry(); |
| 83 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 84 | ProcessId pid() const { return pid_; } |
| 85 | ProcessId parent_pid() const { return ppid_; } |
rsimha@chromium.org | c47d81d | 2010-10-05 23:41:04 | [diff] [blame] | 86 | ProcessId gid() const { return gid_; } |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 87 | const char* exe_file() const { return exe_file_.c_str(); } |
rsimha@chromium.org | c47d81d | 2010-10-05 23:41:04 | [diff] [blame] | 88 | const std::vector<std::string>& cmd_line_args() const { |
| 89 | return cmd_line_args_; |
| 90 | } |
erg@google.com | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 91 | |
| 92 | ProcessId pid_; |
| 93 | ProcessId ppid_; |
| 94 | ProcessId gid_; |
| 95 | std::string exe_file_; |
| 96 | std::vector<std::string> cmd_line_args_; |
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 | |
gspencer@chromium.org | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 126 | // Return status values from GetTerminationStatus. Don't use these as |
| 127 | // exit code arguments to KillProcess*(), use platform/application |
| 128 | // specific values instead. |
| 129 | enum TerminationStatus { |
| 130 | TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status |
| 131 | TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-zero exit status |
| 132 | TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill |
| 133 | TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault |
scheib@chromium.org | bcb2c77 | 2011-04-01 20:20:53 | [diff] [blame] | 134 | TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet |
| 135 | TERMINATION_STATUS_MAX_ENUM |
cpu@google.com | eef576f | 2008-11-03 23:28:06 | [diff] [blame] | 136 | }; |
| 137 | |
thestig@chromium.org | 6a03359 | 2012-03-23 00:08:54 | [diff] [blame] | 138 | #if defined(USE_LINUX_BREAKPAD) |
yusukes@google.com | 7dc738a | 2012-03-23 05:56:35 | [diff] [blame] | 139 | BASE_EXPORT extern size_t g_oom_size; |
thestig@chromium.org | 6a03359 | 2012-03-23 00:08:54 | [diff] [blame] | 140 | #endif |
| 141 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 142 | // Returns the id of the current process. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 143 | BASE_EXPORT ProcessId GetCurrentProcId(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 144 | |
erikkay@google.com | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 145 | // Returns the ProcessHandle of the current process. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 146 | BASE_EXPORT ProcessHandle GetCurrentProcessHandle(); |
maruel@chromium.org | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 147 | |
joi@chromium.org | 9a18283 | 2012-02-10 18:45:58 | [diff] [blame] | 148 | #if defined(OS_WIN) |
alexeypa@chromium.org | f56d158 | 2012-05-16 22:46:17 | [diff] [blame^] | 149 | // Returns the module handle to which an address belongs. The reference count |
| 150 | // of the module is not incremented. |
joi@chromium.org | 9a18283 | 2012-02-10 18:45:58 | [diff] [blame] | 151 | BASE_EXPORT HMODULE GetModuleFromAddress(void* address); |
| 152 | #endif |
| 153 | |
brettw@google.com | 5986ed2 | 2009-02-06 00:19:17 | [diff] [blame] | 154 | // 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] | 155 | // CloseProcessHandle when you are done with it. Returns true on success. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 156 | BASE_EXPORT bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle); |
maruel@chromium.org | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 157 | |
phajdan.jr@chromium.org | 5d438dbad | 2009-04-30 08:59:39 | [diff] [blame] | 158 | // Converts a PID to a process handle. On Windows the handle is opened |
| 159 | // with more access rights and must only be used by trusted code. |
| 160 | // You have to close returned handle using CloseProcessHandle. Returns true |
| 161 | // on success. |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame] | 162 | // TODO(sanjeevr): Replace all calls to OpenPrivilegedProcessHandle with the |
| 163 | // more specific OpenProcessHandleWithAccess method and delete this. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 164 | BASE_EXPORT bool OpenPrivilegedProcessHandle(ProcessId pid, |
| 165 | ProcessHandle* handle); |
phajdan.jr@chromium.org | 5d438dbad | 2009-04-30 08:59:39 | [diff] [blame] | 166 | |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame] | 167 | // Converts a PID to a process handle using the desired access flags. Use a |
| 168 | // combination of the kProcessAccess* flags defined above for |access_flags|. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 169 | BASE_EXPORT bool OpenProcessHandleWithAccess(ProcessId pid, |
| 170 | uint32 access_flags, |
| 171 | ProcessHandle* handle); |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame] | 172 | |
brettw@google.com | 5986ed2 | 2009-02-06 00:19:17 | [diff] [blame] | 173 | // Closes the process handle opened by OpenProcessHandle. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 174 | BASE_EXPORT void CloseProcessHandle(ProcessHandle process); |
erikkay@google.com | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 175 | |
cpu@google.com | eef576f | 2008-11-03 23:28:06 | [diff] [blame] | 176 | // Returns the unique ID for the specified process. This is functionally the |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 177 | // same as Windows' GetProcessId(), but works on versions of Windows before |
| 178 | // Win XP SP1 as well. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 179 | BASE_EXPORT ProcessId GetProcId(ProcessHandle process); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 180 | |
robert.nagy@gmail.com | af82436 | 2011-12-04 14:19:41 | [diff] [blame] | 181 | #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_BSD) |
dkegel@google.com | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 182 | // Returns the path to the executable of the given process. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 183 | BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); |
robert.nagy@gmail.com | ea725b3 | 2011-10-25 17:43:05 | [diff] [blame] | 184 | #endif |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 185 | |
robert.nagy@gmail.com | ea725b3 | 2011-10-25 17:43:05 | [diff] [blame] | 186 | #if defined(OS_LINUX) || defined(OS_ANDROID) |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 187 | // Parse the data found in /proc/<pid>/stat and return the sum of the |
| 188 | // CPU-related ticks. Returns -1 on parse error. |
| 189 | // Exposed for testing. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 190 | BASE_EXPORT int ParseProcStatCPU(const std::string& input); |
thestig@chromium.org | e5856a7a | 2009-12-10 02:08:10 | [diff] [blame] | 191 | |
gspencer@google.com | 03eb9d2 | 2011-08-23 17:58:21 | [diff] [blame] | 192 | // The maximum allowed value for the OOM score. |
| 193 | const int kMaxOomScore = 1000; |
thestig@chromium.org | e5856a7a | 2009-12-10 02:08:10 | [diff] [blame] | 194 | |
gspencer@google.com | 03eb9d2 | 2011-08-23 17:58:21 | [diff] [blame] | 195 | // This adjusts /proc/<pid>/oom_score_adj so the Linux OOM killer will |
| 196 | // prefer to kill certain process types over others. The range for the |
| 197 | // adjustment is [-1000, 1000], with [0, 1000] being user accessible. |
| 198 | // If the Linux system doesn't support the newer oom_score_adj range |
| 199 | // of [0, 1000], then we revert to using the older oom_adj, and |
| 200 | // translate the given value into [0, 15]. Some aliasing of values |
| 201 | // may occur in that case, of course. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 202 | BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score); |
jingzhao@chromium.org | 4d0f93c | 2011-09-29 04:43:54 | [diff] [blame] | 203 | #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
dkegel@google.com | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 204 | |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 205 | #if defined(OS_POSIX) |
dmaclach@chromium.org | 56f0f26 | 2011-02-24 17:14:36 | [diff] [blame] | 206 | // Returns the ID for the parent of the given process. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 207 | BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process); |
dmaclach@chromium.org | 56f0f26 | 2011-02-24 17:14:36 | [diff] [blame] | 208 | |
gspencer@chromium.org | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 209 | // Close all file descriptors, except those which are a destination in the |
agl@chromium.org | 3f04f2b | 2009-04-30 19:40:03 | [diff] [blame] | 210 | // given multimap. Only call this function in a child process where you know |
| 211 | // that there aren't any other threads. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 212 | BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map); |
gspencer@google.com | 03eb9d2 | 2011-08-23 17:58:21 | [diff] [blame] | 213 | #endif // defined(OS_POSIX) |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 214 | |
thestig@chromium.org | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 215 | typedef std::vector<std::pair<std::string, std::string> > EnvironmentVector; |
| 216 | typedef std::vector<std::pair<int, int> > FileHandleMappingVector; |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 217 | |
mark@chromium.org | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 218 | #if defined(OS_MACOSX) |
| 219 | // Used with LaunchOptions::synchronize and LaunchSynchronize, a |
| 220 | // LaunchSynchronizationHandle is an opaque value that LaunchProcess will |
| 221 | // create and set, and that LaunchSynchronize will consume and destroy. |
| 222 | typedef int* LaunchSynchronizationHandle; |
| 223 | #endif // defined(OS_MACOSX) |
| 224 | |
evan@chromium.org | 075fb93 | 2011-07-19 16:31:28 | [diff] [blame] | 225 | // Options for launching a subprocess that are passed to LaunchProcess(). |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame] | 226 | // The default constructor constructs the object with default options. |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 227 | struct LaunchOptions { |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame] | 228 | LaunchOptions() : wait(false), |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 229 | #if defined(OS_WIN) |
| 230 | start_hidden(false), inherit_handles(false), as_user(NULL), |
phajdan.jr@chromium.org | 86f723ec2 | 2011-09-07 21:54:42 | [diff] [blame] | 231 | empty_desktop_name(false), job_handle(NULL) |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 232 | #else |
mcgrathr@chromium.org | 77540fdf | 2011-11-15 18:49:10 | [diff] [blame] | 233 | environ(NULL), fds_to_remap(NULL), maximize_rlimits(NULL), |
| 234 | new_process_group(false) |
mark@chromium.org | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 235 | #if defined(OS_LINUX) |
| 236 | , clone_flags(0) |
| 237 | #endif // OS_LINUX |
tbarzic@chromium.org | 927054d | 2012-01-21 01:48:25 | [diff] [blame] | 238 | #if defined(OS_CHROMEOS) |
| 239 | , ctrl_terminal_fd(-1) |
| 240 | #endif // OS_CHROMEOS |
mark@chromium.org | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 241 | #if defined(OS_MACOSX) |
| 242 | , synchronize(NULL) |
| 243 | #endif // defined(OS_MACOSX) |
gspencer@google.com | 03eb9d2 | 2011-08-23 17:58:21 | [diff] [blame] | 244 | #endif // !defined(OS_WIN) |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 245 | {} |
| 246 | |
| 247 | // If true, wait for the process to complete. |
| 248 | bool wait; |
| 249 | |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 250 | #if defined(OS_WIN) |
| 251 | bool start_hidden; |
| 252 | |
| 253 | // If true, the new process inherits handles from the parent. |
| 254 | bool inherit_handles; |
| 255 | |
| 256 | // If non-NULL, runs as if the user represented by the token had launched it. |
| 257 | // Whether the application is visible on the interactive desktop depends on |
| 258 | // the token belonging to an interactive logon session. |
| 259 | // |
| 260 | // To avoid hard to diagnose problems, when specified this loads the |
| 261 | // environment variables associated with the user and if this operation fails |
| 262 | // the entire call fails as well. |
| 263 | UserTokenHandle as_user; |
| 264 | |
| 265 | // If true, use an empty string for the desktop name. |
| 266 | bool empty_desktop_name; |
phajdan.jr@chromium.org | 86f723ec2 | 2011-09-07 21:54:42 | [diff] [blame] | 267 | |
erikwright@chromium.org | 0148e6e | 2012-03-15 12:40:04 | [diff] [blame] | 268 | // If non-NULL, launches the application in that job object. The process will |
| 269 | // be terminated immediately and LaunchProcess() will fail if assignment to |
| 270 | // the job object fails. |
phajdan.jr@chromium.org | 86f723ec2 | 2011-09-07 21:54:42 | [diff] [blame] | 271 | HANDLE job_handle; |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 272 | #else |
| 273 | // If non-NULL, set/unset environment variables. |
| 274 | // See documentation of AlterEnvironment(). |
| 275 | // This pointer is owned by the caller and must live through the |
| 276 | // call to LaunchProcess(). |
thestig@chromium.org | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 277 | const EnvironmentVector* environ; |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 278 | |
| 279 | // If non-NULL, remap file descriptors according to the mapping of |
| 280 | // src fd->dest fd to propagate FDs into the child process. |
| 281 | // This pointer is owned by the caller and must live through the |
| 282 | // call to LaunchProcess(). |
thestig@chromium.org | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 283 | const FileHandleMappingVector* fds_to_remap; |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 284 | |
mcgrathr@chromium.org | 77540fdf | 2011-11-15 18:49:10 | [diff] [blame] | 285 | // Each element is an RLIMIT_* constant that should be raised to its |
| 286 | // rlim_max. This pointer is owned by the caller and must live through |
| 287 | // the call to LaunchProcess(). |
| 288 | const std::set<int>* maximize_rlimits; |
| 289 | |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 290 | // If true, start the process in a new process group, instead of |
| 291 | // inheriting the parent's process group. The pgid of the child process |
| 292 | // will be the same as its pid. |
| 293 | bool new_process_group; |
bradchen@google.com | ca7c456 | 2011-07-15 23:55:21 | [diff] [blame] | 294 | |
mark@chromium.org | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 295 | #if defined(OS_LINUX) |
bradchen@google.com | ca7c456 | 2011-07-15 23:55:21 | [diff] [blame] | 296 | // If non-zero, start the process using clone(), using flags as provided. |
| 297 | int clone_flags; |
mark@chromium.org | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 298 | #endif // defined(OS_LINUX) |
| 299 | |
tbarzic@chromium.org | 927054d | 2012-01-21 01:48:25 | [diff] [blame] | 300 | #if defined(OS_CHROMEOS) |
| 301 | // If non-negative, the specified file descriptor will be set as the launched |
| 302 | // process' controlling terminal. |
| 303 | int ctrl_terminal_fd; |
| 304 | #endif // defined(OS_CHROMEOS) |
| 305 | |
mark@chromium.org | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 306 | #if defined(OS_MACOSX) |
| 307 | // When non-NULL, a new LaunchSynchronizationHandle will be created and |
| 308 | // stored in *synchronize whenever LaunchProcess returns true in the parent |
| 309 | // process. The child process will have been created (with fork) but will |
| 310 | // be waiting (before exec) for the parent to call LaunchSynchronize with |
| 311 | // this handle. Only when LaunchSynchronize is called will the child be |
| 312 | // permitted to continue execution and call exec. LaunchSynchronize |
| 313 | // destroys the handle created by LaunchProcess. |
| 314 | // |
| 315 | // When synchronize is non-NULL, the parent must call LaunchSynchronize |
| 316 | // whenever LaunchProcess returns true. No exceptions. |
| 317 | // |
| 318 | // Synchronization is useful when the parent process needs to guarantee that |
| 319 | // it can take some action (such as recording the newly-forked child's |
| 320 | // process ID) before the child does something (such as using its process ID |
| 321 | // to communicate with its parent). |
| 322 | // |
| 323 | // |synchronize| and |wait| must not both be set simultaneously. |
| 324 | LaunchSynchronizationHandle* synchronize; |
| 325 | #endif // defined(OS_MACOSX) |
| 326 | |
| 327 | #endif // !defined(OS_WIN) |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | // Launch a process via the command line |cmdline|. |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame] | 331 | // See the documentation of LaunchOptions for details on |options|. |
| 332 | // |
erikwright@chromium.org | 0148e6e | 2012-03-15 12:40:04 | [diff] [blame] | 333 | // Returns true upon success. |
| 334 | // |
| 335 | // Upon success, if |process_handle| is non-NULL, it will be filled in with the |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame] | 336 | // handle of the launched process. NOTE: In this case, the caller is |
| 337 | // responsible for closing the handle so that it doesn't leak! |
| 338 | // Otherwise, the process handle will be implicitly closed. |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 339 | // |
| 340 | // Unix-specific notes: |
mark@chromium.org | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 341 | // - All file descriptors open in the parent process will be closed in the |
| 342 | // child process except for any preserved by options::fds_to_remap, and |
| 343 | // stdin, stdout, and stderr. If not remapped by options::fds_to_remap, |
| 344 | // stdin is reopened as /dev/null, and the child is allowed to inherit its |
| 345 | // parent's stdout and stderr. |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 346 | // - If the first argument on the command line does not contain a slash, |
| 347 | // PATH will be searched. (See man execvp.) |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 348 | BASE_EXPORT bool LaunchProcess(const CommandLine& cmdline, |
| 349 | const LaunchOptions& options, |
| 350 | ProcessHandle* process_handle); |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 351 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 352 | #if defined(OS_WIN) |
tommi@chromium.org | 48dc9e1 | 2010-08-26 19:49:57 | [diff] [blame] | 353 | |
| 354 | enum IntegrityLevel { |
| 355 | INTEGRITY_UNKNOWN, |
| 356 | LOW_INTEGRITY, |
| 357 | MEDIUM_INTEGRITY, |
| 358 | HIGH_INTEGRITY, |
| 359 | }; |
| 360 | // Determine the integrity level of the specified process. Returns false |
| 361 | // if the system does not support integrity levels (pre-Vista) or in the case |
| 362 | // of an underlying system failure. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 363 | BASE_EXPORT bool GetProcessIntegrityLevel(ProcessHandle process, |
thestig@chromium.org | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 364 | IntegrityLevel* level); |
tommi@chromium.org | 48dc9e1 | 2010-08-26 19:49:57 | [diff] [blame] | 365 | |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 366 | // Windows-specific LaunchProcess that takes the command line as a |
| 367 | // string. Useful for situations where you need to control the |
| 368 | // command line arguments directly, but prefer the CommandLine version |
| 369 | // if launching Chrome itself. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 370 | // |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 371 | // The first command line argument should be the path to the process, |
| 372 | // and don't forget to quote it. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 373 | // |
| 374 | // Example (including literal quotes) |
| 375 | // cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 376 | BASE_EXPORT bool LaunchProcess(const string16& cmdline, |
| 377 | const LaunchOptions& options, |
| 378 | ProcessHandle* process_handle); |
cpu@chromium.org | e50130b | 2010-02-01 03:28:47 | [diff] [blame] | 379 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 380 | #elif defined(OS_POSIX) |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 381 | // A POSIX-specific version of LaunchProcess that takes an argv array |
| 382 | // instead of a CommandLine. Useful for situations where you need to |
| 383 | // control the command line arguments directly, but prefer the |
| 384 | // CommandLine version if launching Chrome itself. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 385 | BASE_EXPORT bool LaunchProcess(const std::vector<std::string>& argv, |
| 386 | const LaunchOptions& options, |
| 387 | ProcessHandle* process_handle); |
stuartmorgan@google.com | 2aea9e09 | 2009-08-06 20:03:01 | [diff] [blame] | 388 | |
agl@chromium.org | ef73044e | 2010-03-11 15:25:54 | [diff] [blame] | 389 | // AlterEnvironment returns a modified environment vector, constructed from the |
| 390 | // given environment and the list of changes given in |changes|. Each key in |
| 391 | // the environment is matched against the first element of the pairs. In the |
| 392 | // event of a match, the value is replaced by the second of the pair, unless |
| 393 | // the second is empty, in which case the key-value is removed. |
| 394 | // |
| 395 | // The returned array is allocated using new[] and must be freed by the caller. |
thestig@chromium.org | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 396 | BASE_EXPORT char** AlterEnvironment(const EnvironmentVector& changes, |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 397 | const char* const* const env); |
mark@chromium.org | 84b2d1d | 2011-09-01 21:44:24 | [diff] [blame] | 398 | |
| 399 | #if defined(OS_MACOSX) |
| 400 | |
| 401 | // After a successful call to LaunchProcess with LaunchOptions::synchronize |
| 402 | // set, the parent process must call LaunchSynchronize to allow the child |
| 403 | // process to proceed, and to destroy the LaunchSynchronizationHandle. |
| 404 | BASE_EXPORT void LaunchSynchronize(LaunchSynchronizationHandle handle); |
| 405 | |
| 406 | #endif // defined(OS_MACOSX) |
thestig@chromium.org | 9ec8db0 | 2009-07-21 07:00:13 | [diff] [blame] | 407 | #endif // defined(OS_POSIX) |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 408 | |
toyoshim@chromium.org | b330cab | 2011-11-23 23:37:19 | [diff] [blame] | 409 | #if defined(OS_WIN) |
| 410 | // Set JOBOBJECT_EXTENDED_LIMIT_INFORMATION to JobObject |job_object|. |
| 411 | // As its limit_info.BasicLimitInformation.LimitFlags has |
| 412 | // JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE. |
| 413 | // When the provide JobObject |job_object| is closed, the binded process will |
| 414 | // be terminated. |
| 415 | BASE_EXPORT bool SetJobObjectAsKillOnJobClose(HANDLE job_object); |
| 416 | #endif // defined(OS_WIN) |
| 417 | |
jcampan@chromium.org | 1e31211 | 2009-04-21 21:44:12 | [diff] [blame] | 418 | // 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] | 419 | // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true |
| 420 | // on success (application launched and exited cleanly, with exit code |
maruel@chromium.org | 96878a21 | 2010-06-10 18:26:33 | [diff] [blame] | 421 | // indicating success). |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 422 | BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output); |
phajdan.jr@chromium.org | c0b210ee | 2009-04-17 09:57:52 | [diff] [blame] | 423 | |
viettrungluu@chromium.org | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 424 | #if defined(OS_POSIX) |
ivankr@chromium.org | 1a6ec69 | 2012-04-26 20:24:37 | [diff] [blame] | 425 | // A POSIX-specific version of GetAppOutput that takes an argv array |
| 426 | // instead of a CommandLine. Useful for situations where you need to |
| 427 | // control the command line arguments directly. |
| 428 | BASE_EXPORT bool GetAppOutput(const std::vector<std::string>& argv, |
| 429 | std::string* output); |
| 430 | |
viettrungluu@chromium.org | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 431 | // A restricted version of |GetAppOutput()| which (a) clears the environment, |
| 432 | // and (b) stores at most |max_output| bytes; also, it doesn't search the path |
| 433 | // for the command. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 434 | BASE_EXPORT bool GetAppOutputRestricted(const CommandLine& cl, |
| 435 | std::string* output, size_t max_output); |
benwells@chromium.org | d03b05c | 2011-07-06 06:14:48 | [diff] [blame] | 436 | |
| 437 | // A version of |GetAppOutput()| which also returns the exit code of the |
| 438 | // executed command. Returns true if the application runs and exits cleanly. If |
| 439 | // this is the case the exit code of the application is available in |
| 440 | // |*exit_code|. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 441 | BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl, |
| 442 | std::string* output, int* exit_code); |
gspencer@google.com | 03eb9d2 | 2011-08-23 17:58:21 | [diff] [blame] | 443 | #endif // defined(OS_POSIX) |
viettrungluu@chromium.org | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 444 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 445 | // Used to filter processes by process ID. |
| 446 | class ProcessFilter { |
| 447 | public: |
| 448 | // Returns true to indicate set-inclusion and false otherwise. This method |
| 449 | // should not have side-effects and should be idempotent. |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 450 | virtual bool Includes(const ProcessEntry& entry) const = 0; |
ziadh@chromium.org | 695092f | 2010-08-02 16:34:16 | [diff] [blame] | 451 | |
| 452 | protected: |
| 453 | virtual ~ProcessFilter() {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | // Returns the number of processes on the machine that are running from the |
| 457 | // given executable name. If filter is non-null, then only processes selected |
| 458 | // by the filter will be counted. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 459 | BASE_EXPORT int GetProcessCount(const FilePath::StringType& executable_name, |
| 460 | const ProcessFilter* filter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 461 | |
| 462 | // Attempts to kill all the processes on the current machine that were launched |
| 463 | // from the given executable name, ending them with the given exit code. If |
| 464 | // 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] | 465 | // 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] | 466 | // one couldn't be killed. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 467 | BASE_EXPORT bool KillProcesses(const FilePath::StringType& executable_name, |
| 468 | int exit_code, const ProcessFilter* filter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 469 | |
| 470 | // Attempts to kill the process identified by the given process |
| 471 | // entry structure, giving it the specified exit code. If |wait| is true, wait |
| 472 | // for the process to be actually terminated before returning. |
| 473 | // Returns true if this is successful, false otherwise. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 474 | BASE_EXPORT bool KillProcess(ProcessHandle process, int exit_code, bool wait); |
rsimha@chromium.org | 61b93f88f | 2010-09-22 17:28:30 | [diff] [blame] | 475 | |
| 476 | #if defined(OS_POSIX) |
| 477 | // Attempts to kill the process group identified by |process_group_id|. Returns |
| 478 | // true on success. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 479 | BASE_EXPORT bool KillProcessGroup(ProcessHandle process_group_id); |
gspencer@google.com | 03eb9d2 | 2011-08-23 17:58:21 | [diff] [blame] | 480 | #endif // defined(OS_POSIX) |
rsimha@chromium.org | 61b93f88f | 2010-09-22 17:28:30 | [diff] [blame] | 481 | |
agl@chromium.org | dfe1486 | 2009-01-22 01:23:11 | [diff] [blame] | 482 | #if defined(OS_WIN) |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 483 | BASE_EXPORT bool KillProcessById(ProcessId process_id, int exit_code, |
| 484 | bool wait); |
gspencer@google.com | 03eb9d2 | 2011-08-23 17:58:21 | [diff] [blame] | 485 | #endif // defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 486 | |
gspencer@chromium.org | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 487 | // Get the termination status of the process by interpreting the |
| 488 | // circumstances of the child process' death. |exit_code| is set to |
| 489 | // the status returned by waitpid() on POSIX, and from |
| 490 | // GetExitCodeProcess() on Windows. |exit_code| may be NULL if the |
| 491 | // caller is not interested in it. Note that on Linux, this function |
| 492 | // will only return a useful result the first time it is called after |
| 493 | // the child exits (because it will reap the child and the information |
| 494 | // will no longer be available). |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 495 | BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle, |
| 496 | int* exit_code); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 497 | |
dmaclach@chromium.org | 56f0f26 | 2011-02-24 17:14:36 | [diff] [blame] | 498 | // Waits for process to exit. On POSIX systems, if the process hasn't been |
phajdan.jr@chromium.org | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 499 | // signaled then puts the exit code in |exit_code|; otherwise it's considered |
| 500 | // a failure. On Windows |exit_code| is always filled. Returns true on success, |
| 501 | // and closes |handle| in any case. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 502 | BASE_EXPORT bool WaitForExitCode(ProcessHandle handle, int* exit_code); |
phajdan.jr@chromium.org | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 503 | |
phajdan.jr@chromium.org | 8004e68 | 2010-03-16 07:41:22 | [diff] [blame] | 504 | // Waits for process to exit. If it did exit within |timeout_milliseconds|, |
phajdan.jr@chromium.org | 40bbe59 | 2011-04-06 12:18:20 | [diff] [blame] | 505 | // then puts the exit code in |exit_code|, and returns true. |
phajdan.jr@chromium.org | 8004e68 | 2010-03-16 07:41:22 | [diff] [blame] | 506 | // In POSIX systems, if the process has been signaled then |exit_code| is set |
| 507 | // to -1. Returns false on failure (the caller is then responsible for closing |
| 508 | // |handle|). |
phajdan.jr@chromium.org | 40bbe59 | 2011-04-06 12:18:20 | [diff] [blame] | 509 | // The caller is always responsible for closing the |handle|. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 510 | BASE_EXPORT bool WaitForExitCodeWithTimeout(ProcessHandle handle, |
| 511 | int* exit_code, |
| 512 | int64 timeout_milliseconds); |
phajdan.jr@chromium.org | 8004e68 | 2010-03-16 07:41:22 | [diff] [blame] | 513 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 514 | // Wait for all the processes based on the named executable to exit. If filter |
| 515 | // is non-null, then only processes selected by the filter are waited on. |
| 516 | // Returns after all processes have exited or wait_milliseconds have expired. |
| 517 | // Returns true if all the processes exited, false otherwise. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 518 | BASE_EXPORT bool WaitForProcessesToExit( |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 519 | const FilePath::StringType& executable_name, |
| 520 | int64 wait_milliseconds, |
| 521 | const ProcessFilter* filter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 522 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 523 | // Wait for a single process to exit. Return true if it exited cleanly within |
dmaclach@chromium.org | 56f0f26 | 2011-02-24 17:14:36 | [diff] [blame] | 524 | // the given time limit. On Linux |handle| must be a child process, however |
| 525 | // on Mac and Windows it can be any process. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 526 | BASE_EXPORT bool WaitForSingleProcess(ProcessHandle handle, |
| 527 | int64 wait_milliseconds); |
tedvessenes@gmail.com | 7e1117c | 2012-03-31 20:42:41 | [diff] [blame] | 528 | BASE_EXPORT bool WaitForSingleProcess(ProcessHandle handle, |
| 529 | base::TimeDelta wait); |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 530 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 531 | // Waits a certain amount of time (can be 0) for all the processes with a given |
| 532 | // executable name to exit, then kills off any of them that are still around. |
| 533 | // If filter is non-null, then only processes selected by the filter are waited |
| 534 | // on. Killed processes are ended with the given exit code. Returns false if |
| 535 | // any processes needed to be killed, true if they all exited cleanly within |
| 536 | // the wait_milliseconds delay. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 537 | BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name, |
| 538 | int64 wait_milliseconds, |
| 539 | int exit_code, |
| 540 | const ProcessFilter* filter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 541 | |
jam@chromium.org | eaac7159 | 2011-11-23 18:32:00 | [diff] [blame] | 542 | // This method ensures that the specified process eventually terminates, and |
| 543 | // then it closes the given process handle. |
| 544 | // |
| 545 | // It assumes that the process has already been signalled to exit, and it |
| 546 | // begins by waiting a small amount of time for it to exit. If the process |
| 547 | // does not appear to have exited, then this function starts to become |
| 548 | // aggressive about ensuring that the process terminates. |
| 549 | // |
| 550 | // On Linux this method does not block the calling thread. |
| 551 | // On OS X this method may block for up to 2 seconds. |
| 552 | // |
| 553 | // NOTE: The process handle must have been opened with the PROCESS_TERMINATE |
| 554 | // and SYNCHRONIZE permissions. |
| 555 | // |
| 556 | BASE_EXPORT void EnsureProcessTerminated(ProcessHandle process_handle); |
| 557 | |
| 558 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 559 | // The nicer version of EnsureProcessTerminated() that is patient and will |
| 560 | // wait for |process_handle| to finish and then reap it. |
| 561 | BASE_EXPORT void EnsureProcessGetsReaped(ProcessHandle process_handle); |
| 562 | #endif |
| 563 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 564 | // This class provides a way to iterate through a list of processes on the |
| 565 | // current machine with a specified filter. |
| 566 | // To use, create an instance and then call NextProcessEntry() until it returns |
| 567 | // false. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 568 | class BASE_EXPORT ProcessIterator { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 569 | public: |
maruel@chromium.org | baead4f | 2010-06-11 19:10:30 | [diff] [blame] | 570 | typedef std::list<ProcessEntry> ProcessEntries; |
| 571 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 572 | explicit ProcessIterator(const ProcessFilter* filter); |
| 573 | virtual ~ProcessIterator(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 574 | |
| 575 | // If there's another process that matches the given executable name, |
| 576 | // returns a const pointer to the corresponding PROCESSENTRY32. |
| 577 | // If there are no more matching processes, returns NULL. |
| 578 | // The returned pointer will remain valid until NextProcessEntry() |
| 579 | // is called again or this NamedProcessIterator goes out of scope. |
| 580 | const ProcessEntry* NextProcessEntry(); |
| 581 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 582 | // Takes a snapshot of all the ProcessEntry found. |
maruel@chromium.org | baead4f | 2010-06-11 19:10:30 | [diff] [blame] | 583 | ProcessEntries Snapshot(); |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 584 | |
| 585 | protected: |
| 586 | virtual bool IncludeEntry(); |
| 587 | const ProcessEntry& entry() { return entry_; } |
| 588 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 589 | private: |
| 590 | // Determines whether there's another process (regardless of executable) |
| 591 | // left in the list of all processes. Returns true and sets entry_ to |
| 592 | // that process's info if there is one, false otherwise. |
| 593 | bool CheckForNextProcess(); |
| 594 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 595 | // Initializes a PROCESSENTRY32 data structure so that it's ready for |
| 596 | // use with Process32First/Process32Next. |
| 597 | void InitProcessEntry(ProcessEntry* entry); |
| 598 | |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 599 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 600 | HANDLE snapshot_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 601 | bool started_iteration_; |
robert.nagy@gmail.com | af82436 | 2011-12-04 14:19:41 | [diff] [blame] | 602 | #elif defined(OS_MACOSX) || defined(OS_BSD) |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 603 | std::vector<kinfo_proc> kinfo_procs_; |
| 604 | size_t index_of_kinfo_proc_; |
wtc@chromium.org | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 605 | #elif defined(OS_POSIX) |
thestig@chromium.org | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 606 | DIR* procfs_dir_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 607 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 608 | ProcessEntry entry_; |
| 609 | const ProcessFilter* filter_; |
| 610 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 611 | DISALLOW_COPY_AND_ASSIGN(ProcessIterator); |
| 612 | }; |
| 613 | |
| 614 | // This class provides a way to iterate through the list of processes |
| 615 | // on the current machine that were started from the given executable |
| 616 | // name. To use, create an instance and then call NextProcessEntry() |
| 617 | // until it returns false. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 618 | class BASE_EXPORT NamedProcessIterator : public ProcessIterator { |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 619 | public: |
avi@chromium.org | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 620 | NamedProcessIterator(const FilePath::StringType& executable_name, |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 621 | const ProcessFilter* filter); |
| 622 | virtual ~NamedProcessIterator(); |
| 623 | |
| 624 | protected: |
avi@chromium.org | b171946 | 2011-11-16 00:08:08 | [diff] [blame] | 625 | virtual bool IncludeEntry() OVERRIDE; |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 626 | |
| 627 | private: |
avi@chromium.org | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 628 | FilePath::StringType executable_name_; |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 629 | |
| 630 | DISALLOW_COPY_AND_ASSIGN(NamedProcessIterator); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | // Working Set (resident) memory usage broken down by |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 634 | // |
| 635 | // On Windows: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 636 | // priv (private): These pages (kbytes) cannot be shared with any other process. |
| 637 | // shareable: These pages (kbytes) can be shared with other processes under |
| 638 | // the right circumstances. |
| 639 | // shared : These pages (kbytes) are currently shared with at least one |
| 640 | // other process. |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 641 | // |
| 642 | // On Linux: |
| 643 | // priv: Pages mapped only by this process |
| 644 | // shared: PSS or 0 if the kernel doesn't support this |
| 645 | // shareable: 0 |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 646 | // |
| 647 | // On OS X: TODO(thakis): Revise. |
| 648 | // priv: Memory. |
| 649 | // shared: 0 |
| 650 | // shareable: 0 |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 651 | struct WorkingSetKBytes { |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 652 | WorkingSetKBytes() : priv(0), shareable(0), shared(0) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 653 | size_t priv; |
| 654 | size_t shareable; |
| 655 | size_t shared; |
| 656 | }; |
| 657 | |
| 658 | // Committed (resident + paged) memory usage broken down by |
| 659 | // private: These pages cannot be shared with any other process. |
| 660 | // mapped: These pages are mapped into the view of a section (backed by |
| 661 | // pagefile.sys) |
| 662 | // image: These pages are mapped into the view of an image section (backed by |
| 663 | // file system) |
| 664 | struct CommittedKBytes { |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 665 | CommittedKBytes() : priv(0), mapped(0), image(0) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 666 | size_t priv; |
| 667 | size_t mapped; |
| 668 | size_t image; |
| 669 | }; |
| 670 | |
| 671 | // Free memory (Megabytes marked as free) in the 2G process address space. |
| 672 | // total : total amount in megabytes marked as free. Maximum value is 2048. |
| 673 | // largest : size of the largest contiguous amount of memory found. It is |
| 674 | // always smaller or equal to FreeMBytes::total. |
| 675 | // largest_ptr: starting address of the largest memory block. |
| 676 | struct FreeMBytes { |
| 677 | size_t total; |
| 678 | size_t largest; |
| 679 | void* largest_ptr; |
| 680 | }; |
| 681 | |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 682 | // Convert a POSIX timeval to microseconds. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 683 | BASE_EXPORT int64 TimeValToMicroseconds(const struct timeval& tv); |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 684 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 685 | // Provides performance metrics for a specified process (CPU usage, memory and |
| 686 | // IO counters). To use it, invoke CreateProcessMetrics() to get an instance |
| 687 | // for a specific process, then access the information with the different get |
| 688 | // methods. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 689 | class BASE_EXPORT ProcessMetrics { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 690 | public: |
erg@google.com | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 691 | ~ProcessMetrics(); |
| 692 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 693 | // Creates a ProcessMetrics for the specified process. |
| 694 | // The caller owns the returned object. |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 695 | #if !defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 696 | static ProcessMetrics* CreateProcessMetrics(ProcessHandle process); |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 697 | #else |
| 698 | class PortProvider { |
| 699 | public: |
thakis@chromium.org | b2e8e08 | 2009-12-21 17:44:20 | [diff] [blame] | 700 | // Should return the mach task for |process| if possible, or else |
| 701 | // |MACH_PORT_NULL|. Only processes that this returns tasks for will have |
| 702 | // metrics on OS X (except for the current process, which always gets |
| 703 | // metrics). |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 704 | virtual mach_port_t TaskForPid(ProcessHandle process) const = 0; |
| 705 | }; |
| 706 | |
| 707 | // The port provider needs to outlive the ProcessMetrics object returned by |
| 708 | // this function. If NULL is passed as provider, the returned object |
| 709 | // only returns valid metrics if |process| is the current process. |
| 710 | static ProcessMetrics* CreateProcessMetrics(ProcessHandle process, |
| 711 | PortProvider* port_provider); |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 712 | #endif // !defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 713 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 714 | // Returns the current space allocated for the pagefile, in bytes (these pages |
thomasvl@chromium.org | 796da7c | 2009-06-11 12:45:45 | [diff] [blame] | 715 | // may or may not be in memory). On Linux, this returns the total virtual |
| 716 | // memory size. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 717 | size_t GetPagefileUsage() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 718 | // Returns the peak space allocated for the pagefile, in bytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 719 | size_t GetPeakPagefileUsage() const; |
thomasvl@chromium.org | 796da7c | 2009-06-11 12:45:45 | [diff] [blame] | 720 | // Returns the current working set size, in bytes. On Linux, this returns |
| 721 | // the resident set size. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 722 | size_t GetWorkingSetSize() const; |
tc@google.com | 0c557f1 | 2009-05-11 23:35:52 | [diff] [blame] | 723 | // Returns the peak working set size, in bytes. |
| 724 | size_t GetPeakWorkingSetSize() const; |
erg@chromium.org | 98947a0 | 2010-05-11 17:46:08 | [diff] [blame] | 725 | // Returns private and sharedusage, in bytes. Private bytes is the amount of |
| 726 | // memory currently allocated to a process that cannot be shared. Returns |
| 727 | // false on platform specific error conditions. Note: |private_bytes| |
| 728 | // returns 0 on unsupported OSes: prior to XP SP2. |
| 729 | bool GetMemoryBytes(size_t* private_bytes, |
| 730 | size_t* shared_bytes); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 731 | // Fills a CommittedKBytes with both resident and paged |
| 732 | // memory usage as per definition of CommittedBytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 733 | void GetCommittedKBytes(CommittedKBytes* usage) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 734 | // Fills a WorkingSetKBytes containing resident private and shared memory |
| 735 | // usage in bytes, as per definition of WorkingSetBytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 736 | bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 737 | |
| 738 | // Computes the current process available memory for allocation. |
| 739 | // It does a linear scan of the address space querying each memory region |
| 740 | // for its free (unallocated) status. It is useful for estimating the memory |
| 741 | // load and fragmentation. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 742 | bool CalculateFreeMemory(FreeMBytes* free) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 743 | |
| 744 | // Returns the CPU usage in percent since the last time this method was |
| 745 | // called. The first time this method is called it returns 0 and will return |
| 746 | // the actual CPU info on subsequent calls. |
thakis@chromium.org | 022eab6 | 2010-01-13 04:55:06 | [diff] [blame] | 747 | // On Windows, the CPU usage value is for all CPUs. So if you have 2 CPUs and |
| 748 | // your process is using all the cycles of 1 CPU and not the other CPU, this |
| 749 | // method returns 50. |
| 750 | double GetCPUUsage(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 751 | |
| 752 | // Retrieves accounting information for all I/O operations performed by the |
| 753 | // process. |
| 754 | // If IO information is retrieved successfully, the function returns true |
| 755 | // and fills in the IO_COUNTERS passed in. The function returns false |
| 756 | // otherwise. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 757 | bool GetIOCounters(IoCounters* io_counters) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 758 | |
| 759 | private: |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 760 | #if !defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 761 | explicit ProcessMetrics(ProcessHandle process); |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 762 | #else |
| 763 | ProcessMetrics(ProcessHandle process, PortProvider* port_provider); |
gspencer@google.com | 03eb9d2 | 2011-08-23 17:58:21 | [diff] [blame] | 764 | #endif // defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 765 | |
| 766 | ProcessHandle process_; |
| 767 | |
| 768 | int processor_count_; |
| 769 | |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 770 | // Used to store the previous times and CPU usage counts so we can |
| 771 | // compute the CPU usage between calls. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 772 | int64 last_time_; |
| 773 | int64 last_system_time_; |
| 774 | |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 775 | #if defined(OS_MACOSX) |
| 776 | // Queries the port provider if it's set. |
| 777 | mach_port_t TaskForPid(ProcessHandle process) const; |
| 778 | |
| 779 | PortProvider* port_provider_; |
pvalchev@google.com | 66700d4 | 2010-03-10 07:46:43 | [diff] [blame] | 780 | #elif defined(OS_POSIX) |
| 781 | // Jiffie count at the last_time_ we updated. |
| 782 | int last_cpu_; |
gspencer@google.com | 03eb9d2 | 2011-08-23 17:58:21 | [diff] [blame] | 783 | #endif // defined(OS_POSIX) |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 784 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 785 | DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 786 | }; |
| 787 | |
jingzhao@chromium.org | 4d0f93c | 2011-09-29 04:43:54 | [diff] [blame] | 788 | #if defined(OS_LINUX) || defined(OS_ANDROID) |
jamescook@chromium.org | f37661dd | 2011-08-11 18:10:04 | [diff] [blame] | 789 | // Data from /proc/meminfo about system-wide memory consumption. |
| 790 | // Values are in KB. |
jamescook@chromium.org | d6d4dae | 2012-04-18 17:47:32 | [diff] [blame] | 791 | struct BASE_EXPORT SystemMemoryInfoKB { |
| 792 | SystemMemoryInfoKB(); |
| 793 | |
jamescook@chromium.org | f37661dd | 2011-08-11 18:10:04 | [diff] [blame] | 794 | int total; |
| 795 | int free; |
| 796 | int buffers; |
| 797 | int cached; |
| 798 | int active_anon; |
| 799 | int inactive_anon; |
jamescook@chromium.org | d6d4dae | 2012-04-18 17:47:32 | [diff] [blame] | 800 | int active_file; |
| 801 | int inactive_file; |
jamescook@chromium.org | f37661dd | 2011-08-11 18:10:04 | [diff] [blame] | 802 | int shmem; |
| 803 | }; |
jamescook@chromium.org | 14cb2b2d | 2011-08-02 16:33:24 | [diff] [blame] | 804 | // Retrieves data from /proc/meminfo about system-wide memory consumption. |
jamescook@chromium.org | f37661dd | 2011-08-11 18:10:04 | [diff] [blame] | 805 | // Fills in the provided |meminfo| structure. Returns true on success. |
| 806 | // Exposed for memory debugging widget. |
| 807 | BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo); |
jingzhao@chromium.org | 4d0f93c | 2011-09-29 04:43:54 | [diff] [blame] | 808 | #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
jamescook@chromium.org | 14cb2b2d | 2011-08-02 16:33:24 | [diff] [blame] | 809 | |
| 810 | // Returns the memory committed by the system in KBytes. |
sgk@chromium.org | ed26d94 | 2009-11-09 06:57:28 | [diff] [blame] | 811 | // Returns 0 if it can't compute the commit charge. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 812 | BASE_EXPORT size_t GetSystemCommitCharge(); |
sgk@chromium.org | ed26d94 | 2009-11-09 06:57:28 | [diff] [blame] | 813 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 814 | // Enables low fragmentation heap (LFH) for every heaps of this process. This |
| 815 | // won't have any effect on heaps created after this function call. It will not |
| 816 | // modify data allocated in the heaps before calling this function. So it is |
| 817 | // better to call this function early in initialization and again before |
| 818 | // entering the main loop. |
| 819 | // Note: Returns true on Windows 2000 without doing anything. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 820 | BASE_EXPORT bool EnableLowFragmentationHeap(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 821 | |
maruel@chromium.org | d6fc9fd | 2009-10-27 18:03:47 | [diff] [blame] | 822 | // Enables 'terminate on heap corruption' flag. Helps protect against heap |
maruel@google.com | c9d4087 | 2008-09-24 12:58:37 | [diff] [blame] | 823 | // overflow. Has no effect if the OS doesn't provide the necessary facility. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 824 | BASE_EXPORT void EnableTerminationOnHeapCorruption(); |
maruel@google.com | c9d4087 | 2008-09-24 12:58:37 | [diff] [blame] | 825 | |
jam@chromium.org | fecbc6c | 2011-08-31 20:37:59 | [diff] [blame] | 826 | // Turns on process termination if memory runs out. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 827 | BASE_EXPORT void EnableTerminationOnOutOfMemory(); |
jam@chromium.org | fecbc6c | 2011-08-31 20:37:59 | [diff] [blame] | 828 | |
avi@chromium.org | 6cfa339 | 2010-07-01 20:11:43 | [diff] [blame] | 829 | #if defined(OS_MACOSX) |
| 830 | // Exposed for testing. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 831 | BASE_EXPORT malloc_zone_t* GetPurgeableZone(); |
gspencer@google.com | 03eb9d2 | 2011-08-23 17:58:21 | [diff] [blame] | 832 | #endif // defined(OS_MACOSX) |
avi@chromium.org | cccb2121 | 2009-11-12 20:39:56 | [diff] [blame] | 833 | |
maruel@chromium.org | d6fc9fd | 2009-10-27 18:03:47 | [diff] [blame] | 834 | // Enables stack dump to console output on exception and signals. |
| 835 | // When enabled, the process will quit immediately. This is meant to be used in |
| 836 | // unit_tests only! |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 837 | BASE_EXPORT bool EnableInProcessStackDumping(); |
maruel@chromium.org | d6fc9fd | 2009-10-27 18:03:47 | [diff] [blame] | 838 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 839 | // If supported on the platform, and the user has sufficent rights, increase |
| 840 | // the current process's scheduling priority to a high priority. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 841 | BASE_EXPORT void RaiseProcessToHighPriority(); |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 842 | |
mark@chromium.org | e9a8c19f | 2009-09-03 21:27:36 | [diff] [blame] | 843 | #if defined(OS_MACOSX) |
| 844 | // Restore the default exception handler, setting it to Apple Crash Reporter |
| 845 | // (ReportCrash). When forking and execing a new process, the child will |
| 846 | // inherit the parent's exception ports, which may be set to the Breakpad |
| 847 | // instance running inside the parent. The parent's Breakpad instance should |
| 848 | // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
| 849 | // in the child after forking will restore the standard exception handler. |
| 850 | // See http://crbug.com/20371/ for more details. |
| 851 | void RestoreDefaultExceptionHandler(); |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 852 | #endif // defined(OS_MACOSX) |
mark@chromium.org | e9a8c19f | 2009-09-03 21:27:36 | [diff] [blame] | 853 | |
brettw@google.com | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 854 | } // namespace base |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 855 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 856 | #endif // BASE_PROCESS_UTIL_H_ |