process_metrics.h revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// This file contains routines for gathering resource statistics for processes 6// running on the system. 7 8#ifndef BASE_PROCESS_PROCESS_METRICS_H_ 9#define BASE_PROCESS_PROCESS_METRICS_H_ 10 11#include <string> 12 13#include "base/base_export.h" 14#include "base/basictypes.h" 15#include "base/process.h" 16#include "base/time.h" 17 18#if defined(OS_MACOSX) 19#include <mach/mach.h> 20#endif 21 22namespace base { 23 24#if defined(OS_WIN) 25struct IoCounters : public IO_COUNTERS { 26}; 27#elif defined(OS_POSIX) 28struct IoCounters { 29 uint64_t ReadOperationCount; 30 uint64_t WriteOperationCount; 31 uint64_t OtherOperationCount; 32 uint64_t ReadTransferCount; 33 uint64_t WriteTransferCount; 34 uint64_t OtherTransferCount; 35}; 36#endif 37 38// Working Set (resident) memory usage broken down by 39// 40// On Windows: 41// priv (private): These pages (kbytes) cannot be shared with any other process. 42// shareable: These pages (kbytes) can be shared with other processes under 43// the right circumstances. 44// shared : These pages (kbytes) are currently shared with at least one 45// other process. 46// 47// On Linux: 48// priv: Pages mapped only by this process 49// shared: PSS or 0 if the kernel doesn't support this 50// shareable: 0 51// 52// On OS X: TODO(thakis): Revise. 53// priv: Memory. 54// shared: 0 55// shareable: 0 56struct WorkingSetKBytes { 57 WorkingSetKBytes() : priv(0), shareable(0), shared(0) {} 58 size_t priv; 59 size_t shareable; 60 size_t shared; 61}; 62 63// Committed (resident + paged) memory usage broken down by 64// private: These pages cannot be shared with any other process. 65// mapped: These pages are mapped into the view of a section (backed by 66// pagefile.sys) 67// image: These pages are mapped into the view of an image section (backed by 68// file system) 69struct CommittedKBytes { 70 CommittedKBytes() : priv(0), mapped(0), image(0) {} 71 size_t priv; 72 size_t mapped; 73 size_t image; 74}; 75 76// Free memory (Megabytes marked as free) in the 2G process address space. 77// total : total amount in megabytes marked as free. Maximum value is 2048. 78// largest : size of the largest contiguous amount of memory found. It is 79// always smaller or equal to FreeMBytes::total. 80// largest_ptr: starting address of the largest memory block. 81struct FreeMBytes { 82 size_t total; 83 size_t largest; 84 void* largest_ptr; 85}; 86 87// Convert a POSIX timeval to microseconds. 88BASE_EXPORT int64 TimeValToMicroseconds(const struct timeval& tv); 89 90// Provides performance metrics for a specified process (CPU usage, memory and 91// IO counters). To use it, invoke CreateProcessMetrics() to get an instance 92// for a specific process, then access the information with the different get 93// methods. 94class BASE_EXPORT ProcessMetrics { 95 public: 96 ~ProcessMetrics(); 97 98 // Creates a ProcessMetrics for the specified process. 99 // The caller owns the returned object. 100#if !defined(OS_MACOSX) || defined(OS_IOS) 101 static ProcessMetrics* CreateProcessMetrics(ProcessHandle process); 102#else 103 class PortProvider { 104 public: 105 virtual ~PortProvider() {} 106 107 // Should return the mach task for |process| if possible, or else 108 // |MACH_PORT_NULL|. Only processes that this returns tasks for will have 109 // metrics on OS X (except for the current process, which always gets 110 // metrics). 111 virtual mach_port_t TaskForPid(ProcessHandle process) const = 0; 112 }; 113 114 // The port provider needs to outlive the ProcessMetrics object returned by 115 // this function. If NULL is passed as provider, the returned object 116 // only returns valid metrics if |process| is the current process. 117 static ProcessMetrics* CreateProcessMetrics(ProcessHandle process, 118 PortProvider* port_provider); 119#endif // !defined(OS_MACOSX) || defined(OS_IOS) 120 121 // Returns the current space allocated for the pagefile, in bytes (these pages 122 // may or may not be in memory). On Linux, this returns the total virtual 123 // memory size. 124 size_t GetPagefileUsage() const; 125 // Returns the peak space allocated for the pagefile, in bytes. 126 size_t GetPeakPagefileUsage() const; 127 // Returns the current working set size, in bytes. On Linux, this returns 128 // the resident set size. 129 size_t GetWorkingSetSize() const; 130 // Returns the peak working set size, in bytes. 131 size_t GetPeakWorkingSetSize() const; 132 // Returns private and sharedusage, in bytes. Private bytes is the amount of 133 // memory currently allocated to a process that cannot be shared. Returns 134 // false on platform specific error conditions. Note: |private_bytes| 135 // returns 0 on unsupported OSes: prior to XP SP2. 136 bool GetMemoryBytes(size_t* private_bytes, 137 size_t* shared_bytes); 138 // Fills a CommittedKBytes with both resident and paged 139 // memory usage as per definition of CommittedBytes. 140 void GetCommittedKBytes(CommittedKBytes* usage) const; 141 // Fills a WorkingSetKBytes containing resident private and shared memory 142 // usage in bytes, as per definition of WorkingSetBytes. 143 bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const; 144 145 // Computes the current process available memory for allocation. 146 // It does a linear scan of the address space querying each memory region 147 // for its free (unallocated) status. It is useful for estimating the memory 148 // load and fragmentation. 149 bool CalculateFreeMemory(FreeMBytes* free) const; 150 151 // Returns the CPU usage in percent since the last time this method was 152 // called. The first time this method is called it returns 0 and will return 153 // the actual CPU info on subsequent calls. 154 // On Windows, the CPU usage value is for all CPUs. So if you have 2 CPUs and 155 // your process is using all the cycles of 1 CPU and not the other CPU, this 156 // method returns 50. 157 double GetCPUUsage(); 158 159 // Retrieves accounting information for all I/O operations performed by the 160 // process. 161 // If IO information is retrieved successfully, the function returns true 162 // and fills in the IO_COUNTERS passed in. The function returns false 163 // otherwise. 164 bool GetIOCounters(IoCounters* io_counters) const; 165 166 private: 167#if !defined(OS_MACOSX) || defined(OS_IOS) 168 explicit ProcessMetrics(ProcessHandle process); 169#else 170 ProcessMetrics(ProcessHandle process, PortProvider* port_provider); 171#endif // !defined(OS_MACOSX) || defined(OS_IOS) 172 173#if defined(OS_LINUX) || defined(OS_ANDROID) 174 bool GetWorkingSetKBytesStatm(WorkingSetKBytes* ws_usage) const; 175#endif 176 177#if defined(OS_CHROMEOS) 178 bool GetWorkingSetKBytesTotmaps(WorkingSetKBytes *ws_usage) const; 179#endif 180 181 ProcessHandle process_; 182 183 int processor_count_; 184 185 // Used to store the previous times and CPU usage counts so we can 186 // compute the CPU usage between calls. 187 int64 last_time_; 188 int64 last_system_time_; 189 190#if !defined(OS_IOS) 191#if defined(OS_MACOSX) 192 // Queries the port provider if it's set. 193 mach_port_t TaskForPid(ProcessHandle process) const; 194 195 PortProvider* port_provider_; 196#elif defined(OS_POSIX) 197 // Jiffie count at the last_time_ we updated. 198 int last_cpu_; 199#endif // defined(OS_POSIX) 200#endif // !defined(OS_IOS) 201 202 DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); 203}; 204 205// Returns the memory committed by the system in KBytes. 206// Returns 0 if it can't compute the commit charge. 207BASE_EXPORT size_t GetSystemCommitCharge(); 208 209#if defined(OS_LINUX) || defined(OS_ANDROID) 210// Parse the data found in /proc/<pid>/stat and return the sum of the 211// CPU-related ticks. Returns -1 on parse error. 212// Exposed for testing. 213BASE_EXPORT int ParseProcStatCPU(const std::string& input); 214 215// Data from /proc/meminfo about system-wide memory consumption. 216// Values are in KB. 217struct BASE_EXPORT SystemMemoryInfoKB { 218 SystemMemoryInfoKB(); 219 220 int total; 221 int free; 222 int buffers; 223 int cached; 224 int active_anon; 225 int inactive_anon; 226 int active_file; 227 int inactive_file; 228 int shmem; 229 230 // Gem data will be -1 if not supported. 231 int gem_objects; 232 long long gem_size; 233}; 234// Retrieves data from /proc/meminfo about system-wide memory consumption. 235// Fills in the provided |meminfo| structure. Returns true on success. 236// Exposed for memory debugging widget. 237BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo); 238#endif // defined(OS_LINUX) || defined(OS_ANDROID) 239 240} // namespace base 241 242#endif // BASE_PROCESS_PROCESS_METRICS_H_ 243