13f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file. 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This file/namespace contains utility functions for enumerating, ending and 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// computing statistics of processes. 7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef BASE_PROCESS_UTIL_H_ 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define BASE_PROCESS_UTIL_H_ 103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once 11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/basictypes.h" 13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN) 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <windows.h> 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <tlhelp32.h> 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_MACOSX) 18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// kinfo_proc is defined in <sys/sysctl.h>, but this forward declaration 19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// is sufficient for the vector<kinfo_proc> below. 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct kinfo_proc; 21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// malloc_zone_t is defined in <malloc/malloc.h>, but this forward declaration 22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// is sufficient for GetPurgeableZone() below. 23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochtypedef struct _malloc_zone_t malloc_zone_t; 24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <mach/mach.h> 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX) 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <dirent.h> 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <limits.h> 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <sys/types.h> 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <list> 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <string> 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <utility> 34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <vector> 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 36ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/base_api.h" 37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/file_descriptor_shuffle.h" 3821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen#include "base/file_path.h" 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/process.h" 40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 413345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickclass CommandLine; 423345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochnamespace base { 44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN) 46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochstruct ProcessEntry : public PROCESSENTRY32 { 47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ProcessId pid() const { return th32ProcessID; } 48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ProcessId parent_pid() const { return th32ParentProcessID; } 49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const wchar_t* exe_file() const { return szExeFile; } 50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochstruct IoCounters : public IO_COUNTERS { 53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 55731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// Process access masks. These constants provide platform-independent 56731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// definitions for the standard Windows access masks. 57731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// See http://msdn.microsoft.com/en-us/library/ms684880(VS.85).aspx for 58731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// the specific semantics of each mask value. 59731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessTerminate = PROCESS_TERMINATE; 60731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessCreateThread = PROCESS_CREATE_THREAD; 61731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessSetSessionId = PROCESS_SET_SESSIONID; 62731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessVMOperation = PROCESS_VM_OPERATION; 63731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessVMRead = PROCESS_VM_READ; 64731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessVMWrite = PROCESS_VM_WRITE; 65731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessDuplicateHandle = PROCESS_DUP_HANDLE; 66731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessCreateProcess = PROCESS_CREATE_PROCESS; 67731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessSetQuota = PROCESS_SET_QUOTA; 68731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessSetInformation = PROCESS_SET_INFORMATION; 69731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessQueryInformation = PROCESS_QUERY_INFORMATION; 70731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessSuspendResume = PROCESS_SUSPEND_RESUME; 71731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessQueryLimitedInfomation = 72731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick PROCESS_QUERY_LIMITED_INFORMATION; 73731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessWaitForTermination = SYNCHRONIZE; 74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX) 75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct ProcessEntry { 77731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick ProcessEntry(); 78731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick ~ProcessEntry(); 79731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick 80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ProcessId pid() const { return pid_; } 81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ProcessId parent_pid() const { return ppid_; } 82731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick ProcessId gid() const { return gid_; } 83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const char* exe_file() const { return exe_file_.c_str(); } 84731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick const std::vector<std::string>& cmd_line_args() const { 85731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick return cmd_line_args_; 86731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick } 873f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen 883f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen ProcessId pid_; 893f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen ProcessId ppid_; 903f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen ProcessId gid_; 913f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen std::string exe_file_; 923f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen std::vector<std::string> cmd_line_args_; 93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 95c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct IoCounters { 96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott uint64_t ReadOperationCount; 97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott uint64_t WriteOperationCount; 98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott uint64_t OtherOperationCount; 99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott uint64_t ReadTransferCount; 100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott uint64_t WriteTransferCount; 101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott uint64_t OtherTransferCount; 102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 104731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// Process access masks. They are not used on Posix because access checking 105731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// does not happen during handle creation. 106731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessTerminate = 0; 107731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessCreateThread = 0; 108731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessSetSessionId = 0; 109731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessVMOperation = 0; 110731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessVMRead = 0; 111731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessVMWrite = 0; 112731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessDuplicateHandle = 0; 113731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessCreateProcess = 0; 114731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessSetQuota = 0; 115731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessSetInformation = 0; 116731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessQueryInformation = 0; 117731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessSuspendResume = 0; 118731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessQueryLimitedInfomation = 0; 119731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickconst uint32 kProcessAccessWaitForTermination = 0; 120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif // defined(OS_POSIX) 121c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 12221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// Return status values from GetTerminationStatus. Don't use these as 12321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// exit code arguments to KillProcess*(), use platform/application 12421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// specific values instead. 12521d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsenenum TerminationStatus { 12621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status 12721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-zero exit status 12821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill 12921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault 130ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet 131ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen TERMINATION_STATUS_MAX_ENUM 132c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 133c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 134c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns the id of the current process. 135ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API ProcessId GetCurrentProcId(); 136c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 137c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns the ProcessHandle of the current process. 138ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API ProcessHandle GetCurrentProcessHandle(); 139c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 140c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Converts a PID to a process handle. This handle must be closed by 141c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// CloseProcessHandle when you are done with it. Returns true on success. 142ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle); 143c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 144c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Converts a PID to a process handle. On Windows the handle is opened 145c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// with more access rights and must only be used by trusted code. 146c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// You have to close returned handle using CloseProcessHandle. Returns true 147c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// on success. 148731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// TODO(sanjeevr): Replace all calls to OpenPrivilegedProcessHandle with the 149731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// more specific OpenProcessHandleWithAccess method and delete this. 150ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool OpenPrivilegedProcessHandle(ProcessId pid, ProcessHandle* handle); 151c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 152731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// Converts a PID to a process handle using the desired access flags. Use a 153731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick// combination of the kProcessAccess* flags defined above for |access_flags|. 154ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool OpenProcessHandleWithAccess(ProcessId pid, 155ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen uint32 access_flags, 156ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen ProcessHandle* handle); 157731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick 158c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Closes the process handle opened by OpenProcessHandle. 159ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API void CloseProcessHandle(ProcessHandle process); 160c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 161c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns the unique ID for the specified process. This is functionally the 162c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// same as Windows' GetProcessId(), but works on versions of Windows before 163c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Win XP SP1 as well. 164ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API ProcessId GetProcId(ProcessHandle process); 165c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 166c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_LINUX) 167c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns the path to the executable of the given process. 168c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottFilePath GetProcessExecutablePath(ProcessHandle process); 169c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 170c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Parse the data found in /proc/<pid>/stat and return the sum of the 171c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// CPU-related ticks. Returns -1 on parse error. 172c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Exposed for testing. 173c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottint ParseProcStatCPU(const std::string& input); 174c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 175c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstatic const char kAdjustOOMScoreSwitch[] = "--adjust-oom-score"; 176c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 177c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This adjusts /proc/process/oom_adj so the Linux OOM killer will prefer 178c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// certain process types over others. The range for the adjustment is 179c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// [-17,15], with [0,15] being user accessible. 180c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool AdjustOOMScore(ProcessId process, int score); 181c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 182c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 183c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_POSIX) 184dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// Returns the ID for the parent of the given process. 185dc0f95d653279beabeb9817299e2902918ba123eKristian MonsenProcessId GetParentProcessId(ProcessHandle process); 186dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 18721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// Close all file descriptors, except those which are a destination in the 188c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// given multimap. Only call this function in a child process where you know 189c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// that there aren't any other threads. 190c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochvoid CloseSuperfluousFds(const InjectiveMultimap& saved_map); 191c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 192c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 193c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN) 1943345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 1953345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickenum IntegrityLevel { 1963345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick INTEGRITY_UNKNOWN, 1973345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick LOW_INTEGRITY, 1983345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick MEDIUM_INTEGRITY, 1993345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick HIGH_INTEGRITY, 2003345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick}; 2013345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Determine the integrity level of the specified process. Returns false 2023345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// if the system does not support integrity levels (pre-Vista) or in the case 2033345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// of an underlying system failure. 204ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool GetProcessIntegrityLevel(ProcessHandle process, 205ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen IntegrityLevel *level); 2063345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 207c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Runs the given application name with the given command line. Normally, the 208c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// first command line argument should be the path to the process, and don't 209c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// forget to quote it. 210c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 211c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If wait is true, it will block and wait for the other process to finish, 212c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// otherwise, it will just continue asynchronously. 213c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 214c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Example (including literal quotes) 215c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" 216c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 217c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If process_handle is non-NULL, the process handle of the launched app will be 218c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// stored there on a successful launch. 219c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// NOTE: In this case, the caller is responsible for closing the handle so 220c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// that it doesn't leak! 221ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool LaunchApp(const std::wstring& cmdline, 222ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool wait, bool start_hidden, 223ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen ProcessHandle* process_handle); 224c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 2253345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Same as LaunchApp, except allows the new process to inherit handles of the 2263345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// parent process. 227ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool LaunchAppWithHandleInheritance(const std::wstring& cmdline, 228ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool wait, bool start_hidden, 229ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen ProcessHandle* process_handle); 2303345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 231c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Runs the given application name with the given command line as if the user 232c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// represented by |token| had launched it. The caveats about |cmdline| and 233c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |process_handle| explained for LaunchApp above apply as well. 234c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 235c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Whether the application is visible on the interactive desktop depends on 236c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the token belonging to an interactive logon session. 237c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 238c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// To avoid hard to diagnose problems, this function internally loads the 239c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// environment variables associated with the user and if this operation fails 240c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the entire call fails as well. 241ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool LaunchAppAsUser(UserTokenHandle token, 242ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const std::wstring& cmdline, 243ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool start_hidden, 244ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen ProcessHandle* process_handle); 245c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 246c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Has the same behavior as LaunchAppAsUser, but offers the boolean option to 2473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// use an empty string for the desktop name and a boolean for allowing the 2483345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// child process to inherit handles from its parent. 249ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool LaunchAppAsUser(UserTokenHandle token, 250ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const std::wstring& cmdline, 251ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool start_hidden, ProcessHandle* process_handle, 252ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool empty_desktop_name, bool inherit_handles); 253c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 254c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 255c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX) 256c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Runs the application specified in argv[0] with the command line argv. 257c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Before launching all FDs open in the parent process will be marked as 258c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// close-on-exec. |fds_to_remap| defines a mapping of src fd->dest fd to 259c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// propagate FDs into the child process. 260c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 261c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// As above, if wait is true, execute synchronously. The pid will be stored 262c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// in process_handle if that pointer is non-null. 263c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 264c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Note that the first argument in argv must point to the executable filename. 265c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If the filename is not fully specified, PATH will be searched. 266c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttypedef std::vector<std::pair<int, int> > file_handle_mapping_vector; 267c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool LaunchApp(const std::vector<std::string>& argv, 268c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const file_handle_mapping_vector& fds_to_remap, 269c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool wait, ProcessHandle* process_handle); 270c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 271c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Similar to the above, but also (un)set environment variables in child process 272c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// through |environ|. 273c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttypedef std::vector<std::pair<std::string, std::string> > environment_vector; 274c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool LaunchApp(const std::vector<std::string>& argv, 275c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const environment_vector& environ, 276c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const file_handle_mapping_vector& fds_to_remap, 277c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool wait, ProcessHandle* process_handle); 278c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 2793345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Similar to the above two methods, but starts the child process in a process 2803345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// group of its own, instead of allowing it to inherit the parent's process 2813345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// group. The pgid of the child process will be the same as its pid. 2823345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickbool LaunchAppInNewProcessGroup(const std::vector<std::string>& argv, 2833345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick const environment_vector& environ, 2843345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick const file_handle_mapping_vector& fds_to_remap, 2853345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool wait, ProcessHandle* process_handle); 2863345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 287c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// AlterEnvironment returns a modified environment vector, constructed from the 288c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// given environment and the list of changes given in |changes|. Each key in 289c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// the environment is matched against the first element of the pairs. In the 290c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// event of a match, the value is replaced by the second of the pair, unless 291c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// the second is empty, in which case the key-value is removed. 292c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 293c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// The returned array is allocated using new[] and must be freed by the caller. 294c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochchar** AlterEnvironment(const environment_vector& changes, 295c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const char* const* const env); 296c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif // defined(OS_POSIX) 297c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 298c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Executes the application specified by cl. This function delegates to one 299c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// of the above two platform-specific functions. 300ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool LaunchApp(const CommandLine& cl, bool wait, bool start_hidden, 301ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen ProcessHandle* process_handle); 302c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 303c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Executes the application specified by |cl| and wait for it to exit. Stores 304c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true 305c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// on success (application launched and exited cleanly, with exit code 306c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// indicating success). 307ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool GetAppOutput(const CommandLine& cl, std::string* output); 308c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 309c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_POSIX) 310c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// A restricted version of |GetAppOutput()| which (a) clears the environment, 311c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// and (b) stores at most |max_output| bytes; also, it doesn't search the path 312c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// for the command. 313c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool GetAppOutputRestricted(const CommandLine& cl, 314c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string* output, size_t max_output); 315c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 316c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 317c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Used to filter processes by process ID. 318c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass ProcessFilter { 319c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public: 320c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns true to indicate set-inclusion and false otherwise. This method 321c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // should not have side-effects and should be idempotent. 322c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual bool Includes(const ProcessEntry& entry) const = 0; 3233345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 3243345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick protected: 3253345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick virtual ~ProcessFilter() {} 326c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 327c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 328c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns the number of processes on the machine that are running from the 329c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// given executable name. If filter is non-null, then only processes selected 330c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// by the filter will be counted. 331ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API int GetProcessCount(const FilePath::StringType& executable_name, 332ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const ProcessFilter* filter); 333c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 334c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Attempts to kill all the processes on the current machine that were launched 335c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// from the given executable name, ending them with the given exit code. If 336c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// filter is non-null, then only processes selected by the filter are killed. 3373345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Returns true if all processes were able to be killed off, false if at least 338c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// one couldn't be killed. 339ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool KillProcesses(const FilePath::StringType& executable_name, 340ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen int exit_code, const ProcessFilter* filter); 341c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 342c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Attempts to kill the process identified by the given process 343c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// entry structure, giving it the specified exit code. If |wait| is true, wait 344c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// for the process to be actually terminated before returning. 345c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if this is successful, false otherwise. 346ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool KillProcess(ProcessHandle process, int exit_code, bool wait); 3473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 3483345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#if defined(OS_POSIX) 3493345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Attempts to kill the process group identified by |process_group_id|. Returns 3503345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// true on success. 3513345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickbool KillProcessGroup(ProcessHandle process_group_id); 3523345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#endif 3533345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 354c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN) 355ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool KillProcessById(ProcessId process_id, int exit_code, bool wait); 356c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 357c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 35821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// Get the termination status of the process by interpreting the 35921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// circumstances of the child process' death. |exit_code| is set to 36021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// the status returned by waitpid() on POSIX, and from 36121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// GetExitCodeProcess() on Windows. |exit_code| may be NULL if the 36221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// caller is not interested in it. Note that on Linux, this function 36321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// will only return a useful result the first time it is called after 36421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// the child exits (because it will reap the child and the information 36521d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen// will no longer be available). 366ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API TerminationStatus GetTerminationStatus(ProcessHandle handle, 367ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen int* exit_code); 368c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 369dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// Waits for process to exit. On POSIX systems, if the process hasn't been 370c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// signaled then puts the exit code in |exit_code|; otherwise it's considered 371c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// a failure. On Windows |exit_code| is always filled. Returns true on success, 372c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// and closes |handle| in any case. 373ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool WaitForExitCode(ProcessHandle handle, int* exit_code); 374c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 375c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Waits for process to exit. If it did exit within |timeout_milliseconds|, 376ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// then puts the exit code in |exit_code|, and returns true. 377c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// In POSIX systems, if the process has been signaled then |exit_code| is set 378c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// to -1. Returns false on failure (the caller is then responsible for closing 379c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// |handle|). 380ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// The caller is always responsible for closing the |handle|. 381ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, 382ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen int64 timeout_milliseconds); 383c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 384c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Wait for all the processes based on the named executable to exit. If filter 385c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// is non-null, then only processes selected by the filter are waited on. 386c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns after all processes have exited or wait_milliseconds have expired. 387c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if all the processes exited, false otherwise. 388ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool WaitForProcessesToExit( 389ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const FilePath::StringType& executable_name, 390ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen int64 wait_milliseconds, 391ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const ProcessFilter* filter); 392c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 393c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Wait for a single process to exit. Return true if it exited cleanly within 394dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// the given time limit. On Linux |handle| must be a child process, however 395dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// on Mac and Windows it can be any process. 396ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool WaitForSingleProcess(ProcessHandle handle, 397ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen int64 wait_milliseconds); 398c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 399c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Waits a certain amount of time (can be 0) for all the processes with a given 400c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// executable name to exit, then kills off any of them that are still around. 401c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If filter is non-null, then only processes selected by the filter are waited 402c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// on. Killed processes are ended with the given exit code. Returns false if 403c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// any processes needed to be killed, true if they all exited cleanly within 404c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the wait_milliseconds delay. 405ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool CleanupProcesses(const FilePath::StringType& executable_name, 406ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen int64 wait_milliseconds, 407ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen int exit_code, 408ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const ProcessFilter* filter); 409c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 410c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// This class provides a way to iterate through a list of processes on the 411c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// current machine with a specified filter. 412c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// To use, create an instance and then call NextProcessEntry() until it returns 413c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// false. 414ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API ProcessIterator { 415c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public: 416c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch typedef std::list<ProcessEntry> ProcessEntries; 417c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 418c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch explicit ProcessIterator(const ProcessFilter* filter); 419c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual ~ProcessIterator(); 420c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 421c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // If there's another process that matches the given executable name, 422c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // returns a const pointer to the corresponding PROCESSENTRY32. 423c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // If there are no more matching processes, returns NULL. 424c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // The returned pointer will remain valid until NextProcessEntry() 425c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // is called again or this NamedProcessIterator goes out of scope. 426c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const ProcessEntry* NextProcessEntry(); 427c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 428c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Takes a snapshot of all the ProcessEntry found. 429c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ProcessEntries Snapshot(); 430c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 431c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch protected: 432c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual bool IncludeEntry(); 433c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const ProcessEntry& entry() { return entry_; } 434c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 435c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private: 436c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Determines whether there's another process (regardless of executable) 437c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // left in the list of all processes. Returns true and sets entry_ to 438c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // that process's info if there is one, false otherwise. 439c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool CheckForNextProcess(); 440c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 441c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Initializes a PROCESSENTRY32 data structure so that it's ready for 442c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // use with Process32First/Process32Next. 443c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott void InitProcessEntry(ProcessEntry* entry); 444c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 445c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN) 446c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott HANDLE snapshot_; 447c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool started_iteration_; 448c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_MACOSX) 449c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::vector<kinfo_proc> kinfo_procs_; 450c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t index_of_kinfo_proc_; 451c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX) 452c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott DIR *procfs_dir_; 453c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 454c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ProcessEntry entry_; 455c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const ProcessFilter* filter_; 456c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 457c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch DISALLOW_COPY_AND_ASSIGN(ProcessIterator); 458c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 459c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 460c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// This class provides a way to iterate through the list of processes 461c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// on the current machine that were started from the given executable 462c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// name. To use, create an instance and then call NextProcessEntry() 463c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// until it returns false. 464ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API NamedProcessIterator : public ProcessIterator { 465c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public: 46621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen NamedProcessIterator(const FilePath::StringType& executable_name, 467c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const ProcessFilter* filter); 468c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual ~NamedProcessIterator(); 469c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 470c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch protected: 471c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual bool IncludeEntry(); 472c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 473c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private: 47421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen FilePath::StringType executable_name_; 475c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 476c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch DISALLOW_COPY_AND_ASSIGN(NamedProcessIterator); 477c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 478c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 479c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Working Set (resident) memory usage broken down by 480c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 481c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// On Windows: 482c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// priv (private): These pages (kbytes) cannot be shared with any other process. 483c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// shareable: These pages (kbytes) can be shared with other processes under 484c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the right circumstances. 485c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// shared : These pages (kbytes) are currently shared with at least one 486c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// other process. 487c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 488c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// On Linux: 489c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// priv: Pages mapped only by this process 490c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// shared: PSS or 0 if the kernel doesn't support this 491c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// shareable: 0 492c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 493c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// On OS X: TODO(thakis): Revise. 494c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// priv: Memory. 495c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// shared: 0 496c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// shareable: 0 497c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct WorkingSetKBytes { 498c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott WorkingSetKBytes() : priv(0), shareable(0), shared(0) {} 499c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t priv; 500c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t shareable; 501c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t shared; 502c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 503c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 504c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Committed (resident + paged) memory usage broken down by 505c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// private: These pages cannot be shared with any other process. 506c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// mapped: These pages are mapped into the view of a section (backed by 507c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// pagefile.sys) 508c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// image: These pages are mapped into the view of an image section (backed by 509c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// file system) 510c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct CommittedKBytes { 511c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CommittedKBytes() : priv(0), mapped(0), image(0) {} 512c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t priv; 513c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t mapped; 514c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t image; 515c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 516c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 517c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Free memory (Megabytes marked as free) in the 2G process address space. 518c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// total : total amount in megabytes marked as free. Maximum value is 2048. 519c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// largest : size of the largest contiguous amount of memory found. It is 520c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// always smaller or equal to FreeMBytes::total. 521c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// largest_ptr: starting address of the largest memory block. 522c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct FreeMBytes { 523c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t total; 524c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t largest; 525c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott void* largest_ptr; 526c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 527c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 528c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Convert a POSIX timeval to microseconds. 529ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API int64 TimeValToMicroseconds(const struct timeval& tv); 530c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 531c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Provides performance metrics for a specified process (CPU usage, memory and 532c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// IO counters). To use it, invoke CreateProcessMetrics() to get an instance 533c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// for a specific process, then access the information with the different get 534c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// methods. 535ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API ProcessMetrics { 536c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public: 5373f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen ~ProcessMetrics(); 5383f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen 539c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Creates a ProcessMetrics for the specified process. 540c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // The caller owns the returned object. 541c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if !defined(OS_MACOSX) 542c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static ProcessMetrics* CreateProcessMetrics(ProcessHandle process); 543c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#else 544c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott class PortProvider { 545c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public: 546c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Should return the mach task for |process| if possible, or else 547c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // |MACH_PORT_NULL|. Only processes that this returns tasks for will have 548c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // metrics on OS X (except for the current process, which always gets 549c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // metrics). 550c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott virtual mach_port_t TaskForPid(ProcessHandle process) const = 0; 551c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott }; 552c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 553c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // The port provider needs to outlive the ProcessMetrics object returned by 554c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // this function. If NULL is passed as provider, the returned object 555c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // only returns valid metrics if |process| is the current process. 556c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static ProcessMetrics* CreateProcessMetrics(ProcessHandle process, 557c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott PortProvider* port_provider); 558c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif // !defined(OS_MACOSX) 559c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 560c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns the current space allocated for the pagefile, in bytes (these pages 561c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // may or may not be in memory). On Linux, this returns the total virtual 562c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // memory size. 563c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t GetPagefileUsage() const; 564c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns the peak space allocated for the pagefile, in bytes. 565c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t GetPeakPagefileUsage() const; 566c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns the current working set size, in bytes. On Linux, this returns 567c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // the resident set size. 568c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t GetWorkingSetSize() const; 569c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns the peak working set size, in bytes. 570c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t GetPeakWorkingSetSize() const; 571c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Returns private and sharedusage, in bytes. Private bytes is the amount of 572c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // memory currently allocated to a process that cannot be shared. Returns 573c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // false on platform specific error conditions. Note: |private_bytes| 574c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // returns 0 on unsupported OSes: prior to XP SP2. 575c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch bool GetMemoryBytes(size_t* private_bytes, 576c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch size_t* shared_bytes); 577c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Fills a CommittedKBytes with both resident and paged 578c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // memory usage as per definition of CommittedBytes. 579c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott void GetCommittedKBytes(CommittedKBytes* usage) const; 580c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Fills a WorkingSetKBytes containing resident private and shared memory 581c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // usage in bytes, as per definition of WorkingSetBytes. 582c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const; 583c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 584c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Computes the current process available memory for allocation. 585c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // It does a linear scan of the address space querying each memory region 586c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // for its free (unallocated) status. It is useful for estimating the memory 587c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // load and fragmentation. 588c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool CalculateFreeMemory(FreeMBytes* free) const; 589c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 590c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns the CPU usage in percent since the last time this method was 591c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // called. The first time this method is called it returns 0 and will return 592c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // the actual CPU info on subsequent calls. 593c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // On Windows, the CPU usage value is for all CPUs. So if you have 2 CPUs and 594c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // your process is using all the cycles of 1 CPU and not the other CPU, this 595c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // method returns 50. 596c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott double GetCPUUsage(); 597c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 598c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Retrieves accounting information for all I/O operations performed by the 599c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // process. 600c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // If IO information is retrieved successfully, the function returns true 601c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // and fills in the IO_COUNTERS passed in. The function returns false 602c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // otherwise. 603c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool GetIOCounters(IoCounters* io_counters) const; 604c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 605c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private: 606c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if !defined(OS_MACOSX) 607c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott explicit ProcessMetrics(ProcessHandle process); 608c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#else 609c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ProcessMetrics(ProcessHandle process, PortProvider* port_provider); 610c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif // !defined(OS_MACOSX) 611c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 612c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ProcessHandle process_; 613c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 614c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott int processor_count_; 615c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 616c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Used to store the previous times and CPU usage counts so we can 617c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // compute the CPU usage between calls. 618c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott int64 last_time_; 619c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott int64 last_system_time_; 620c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 621c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_MACOSX) 622c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Queries the port provider if it's set. 623c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott mach_port_t TaskForPid(ProcessHandle process) const; 624c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 625c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott PortProvider* port_provider_; 626c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#elif defined(OS_POSIX) 627c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Jiffie count at the last_time_ we updated. 628c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch int last_cpu_; 629c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif // defined(OS_MACOSX) 630c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 631c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); 632c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 633c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 634c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns the memory commited by the system in KBytes. 635c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns 0 if it can't compute the commit charge. 636ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API size_t GetSystemCommitCharge(); 637c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 638c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Enables low fragmentation heap (LFH) for every heaps of this process. This 639c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// won't have any effect on heaps created after this function call. It will not 640c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// modify data allocated in the heaps before calling this function. So it is 641c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// better to call this function early in initialization and again before 642c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// entering the main loop. 643c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Note: Returns true on Windows 2000 without doing anything. 644ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool EnableLowFragmentationHeap(); 645c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 646c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Enables 'terminate on heap corruption' flag. Helps protect against heap 647c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// overflow. Has no effect if the OS doesn't provide the necessary facility. 648ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API void EnableTerminationOnHeapCorruption(); 649c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 650c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if !defined(OS_WIN) 651c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Turns on process termination if memory runs out. This is handled on Windows 652c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// inside RegisterInvalidParamHandler(). 653c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid EnableTerminationOnOutOfMemory(); 654c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#if defined(OS_MACOSX) 655c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Exposed for testing. 656c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochmalloc_zone_t* GetPurgeableZone(); 657c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif 658c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 659c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 660c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Enables stack dump to console output on exception and signals. 661c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// When enabled, the process will quit immediately. This is meant to be used in 662c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// unit_tests only! 663ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool EnableInProcessStackDumping(); 664c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 665c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If supported on the platform, and the user has sufficent rights, increase 666c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the current process's scheduling priority to a high priority. 667ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API void RaiseProcessToHighPriority(); 668c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 669c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_MACOSX) 670c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Restore the default exception handler, setting it to Apple Crash Reporter 671c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// (ReportCrash). When forking and execing a new process, the child will 672c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// inherit the parent's exception ports, which may be set to the Breakpad 673c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// instance running inside the parent. The parent's Breakpad instance should 674c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// not handle the child's exceptions. Calling RestoreDefaultExceptionHandler 675c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// in the child after forking will restore the standard exception handler. 676c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// See http://crbug.com/20371/ for more details. 677c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid RestoreDefaultExceptionHandler(); 678c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif // defined(OS_MACOSX) 679c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 680c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} // namespace base 681c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 682c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif // BASE_PROCESS_UTIL_H_ 683