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.
5 // This file contains routines for gathering resource statistics for processes
6 // running on the system.
8 #ifndef BASE_PROCESS_PROCESS_METRICS_H_
9 #define BASE_PROCESS_PROCESS_METRICS_H_
13 #include "base/base_export.h"
14 #include "base/basictypes.h"
15 #include "base/process.h"
16 #include "base/time/time.h"
18 #if defined(OS_MACOSX)
19 #include <mach/mach.h>
25 struct IoCounters
: public IO_COUNTERS
{
27 #elif defined(OS_POSIX)
29 uint64_t ReadOperationCount
;
30 uint64_t WriteOperationCount
;
31 uint64_t OtherOperationCount
;
32 uint64_t ReadTransferCount
;
33 uint64_t WriteTransferCount
;
34 uint64_t OtherTransferCount
;
38 // Working Set (resident) memory usage broken down by
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
48 // priv: Pages mapped only by this process
49 // shared: PSS or 0 if the kernel doesn't support this
52 // On OS X: TODO(thakis): Revise.
56 struct WorkingSetKBytes
{
57 WorkingSetKBytes() : priv(0), shareable(0), shared(0) {}
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
67 // image: These pages are mapped into the view of an image section (backed by
69 struct CommittedKBytes
{
70 CommittedKBytes() : priv(0), mapped(0), image(0) {}
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.
87 // Convert a POSIX timeval to microseconds.
88 BASE_EXPORT int64
TimeValToMicroseconds(const struct timeval
& tv
);
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
94 class BASE_EXPORT ProcessMetrics
{
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
);
105 virtual ~PortProvider() {}
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
111 virtual mach_port_t
TaskForPid(ProcessHandle process
) const = 0;
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)
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
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;
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;
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();
159 // Retrieves accounting information for all I/O operations performed by the
161 // If IO information is retrieved successfully, the function returns true
162 // and fills in the IO_COUNTERS passed in. The function returns false
164 bool GetIOCounters(IoCounters
* io_counters
) const;
167 #if !defined(OS_MACOSX) || defined(OS_IOS)
168 explicit ProcessMetrics(ProcessHandle process
);
170 ProcessMetrics(ProcessHandle process
, PortProvider
* port_provider
);
171 #endif // !defined(OS_MACOSX) || defined(OS_IOS)
173 #if defined(OS_LINUX) || defined(OS_ANDROID)
174 bool GetWorkingSetKBytesStatm(WorkingSetKBytes
* ws_usage
) const;
177 #if defined(OS_CHROMEOS)
178 bool GetWorkingSetKBytesTotmaps(WorkingSetKBytes
*ws_usage
) const;
181 ProcessHandle process_
;
183 int processor_count_
;
185 // Used to store the previous times and CPU usage counts so we can
186 // compute the CPU usage between calls.
188 int64 last_system_time_
;
191 #if defined(OS_MACOSX)
192 // Queries the port provider if it's set.
193 mach_port_t
TaskForPid(ProcessHandle process
) const;
195 PortProvider
* port_provider_
;
196 #elif defined(OS_POSIX)
197 // Jiffie count at the last_time_ we updated.
199 #endif // defined(OS_POSIX)
200 #endif // !defined(OS_IOS)
202 DISALLOW_COPY_AND_ASSIGN(ProcessMetrics
);
205 // Returns the memory committed by the system in KBytes.
206 // Returns 0 if it can't compute the commit charge.
207 BASE_EXPORT
size_t GetSystemCommitCharge();
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.
213 BASE_EXPORT
int ParseProcStatCPU(const std::string
& input
);
215 // Data from /proc/meminfo about system-wide memory consumption.
217 struct BASE_EXPORT SystemMemoryInfoKB
{
218 SystemMemoryInfoKB();
230 // Gem data will be -1 if not supported.
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.
237 BASE_EXPORT
bool GetSystemMemoryInfo(SystemMemoryInfoKB
* meminfo
);
238 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
242 #endif // BASE_PROCESS_PROCESS_METRICS_H_