xref: /aosp_15_r20/external/libchrome/base/process/process_metrics.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker // This file contains routines for gathering resource statistics for processes
6*635a8641SAndroid Build Coastguard Worker // running on the system.
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #ifndef BASE_PROCESS_PROCESS_METRICS_H_
9*635a8641SAndroid Build Coastguard Worker #define BASE_PROCESS_PROCESS_METRICS_H_
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
12*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker #include <memory>
15*635a8641SAndroid Build Coastguard Worker #include <string>
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/gtest_prod_util.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/process/process_handle.h"
21*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h"
22*635a8641SAndroid Build Coastguard Worker #include "base/values.h"
23*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
24*635a8641SAndroid Build Coastguard Worker 
25*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
26*635a8641SAndroid Build Coastguard Worker #include <mach/mach.h>
27*635a8641SAndroid Build Coastguard Worker #include "base/process/port_provider_mac.h"
28*635a8641SAndroid Build Coastguard Worker 
29*635a8641SAndroid Build Coastguard Worker #if !defined(OS_IOS)
30*635a8641SAndroid Build Coastguard Worker #include <mach/mach_vm.h>
31*635a8641SAndroid Build Coastguard Worker #endif
32*635a8641SAndroid Build Coastguard Worker #endif
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
35*635a8641SAndroid Build Coastguard Worker #include "base/win/scoped_handle.h"
36*635a8641SAndroid Build Coastguard Worker #include "base/win/windows_types.h"
37*635a8641SAndroid Build Coastguard Worker #endif
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker namespace base {
40*635a8641SAndroid Build Coastguard Worker 
41*635a8641SAndroid Build Coastguard Worker // Full declaration is in process_metrics_iocounters.h.
42*635a8641SAndroid Build Coastguard Worker struct IoCounters;
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) || defined(OS_ANDROID)
45*635a8641SAndroid Build Coastguard Worker // Minor and major page fault counts since the process creation.
46*635a8641SAndroid Build Coastguard Worker // Both counts are process-wide, and exclude child processes.
47*635a8641SAndroid Build Coastguard Worker //
48*635a8641SAndroid Build Coastguard Worker // minor: Number of page faults that didn't require disk IO.
49*635a8641SAndroid Build Coastguard Worker // major: Number of page faults that required disk IO.
50*635a8641SAndroid Build Coastguard Worker struct PageFaultCounts {
51*635a8641SAndroid Build Coastguard Worker   int64_t minor;
52*635a8641SAndroid Build Coastguard Worker   int64_t major;
53*635a8641SAndroid Build Coastguard Worker };
54*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_LINUX) || defined(OS_ANDROID)
55*635a8641SAndroid Build Coastguard Worker 
56*635a8641SAndroid Build Coastguard Worker // Convert a POSIX timeval to microseconds.
57*635a8641SAndroid Build Coastguard Worker BASE_EXPORT int64_t TimeValToMicroseconds(const struct timeval& tv);
58*635a8641SAndroid Build Coastguard Worker 
59*635a8641SAndroid Build Coastguard Worker // Provides performance metrics for a specified process (CPU usage and IO
60*635a8641SAndroid Build Coastguard Worker // counters). Use CreateCurrentProcessMetrics() to get an instance for the
61*635a8641SAndroid Build Coastguard Worker // current process, or CreateProcessMetrics() to get an instance for an
62*635a8641SAndroid Build Coastguard Worker // arbitrary process. Then, access the information with the different get
63*635a8641SAndroid Build Coastguard Worker // methods.
64*635a8641SAndroid Build Coastguard Worker //
65*635a8641SAndroid Build Coastguard Worker // This class exposes a few platform-specific APIs for parsing memory usage, but
66*635a8641SAndroid Build Coastguard Worker // these are not intended to generalize to other platforms, since the memory
67*635a8641SAndroid Build Coastguard Worker // models differ substantially.
68*635a8641SAndroid Build Coastguard Worker //
69*635a8641SAndroid Build Coastguard Worker // To obtain consistent memory metrics, use the memory_instrumentation service.
70*635a8641SAndroid Build Coastguard Worker //
71*635a8641SAndroid Build Coastguard Worker // For further documentation on memory, see
72*635a8641SAndroid Build Coastguard Worker // https://chromium.googlesource.com/chromium/src/+/HEAD/docs/README.md
73*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT ProcessMetrics {
74*635a8641SAndroid Build Coastguard Worker  public:
75*635a8641SAndroid Build Coastguard Worker   ~ProcessMetrics();
76*635a8641SAndroid Build Coastguard Worker 
77*635a8641SAndroid Build Coastguard Worker   // Creates a ProcessMetrics for the specified process.
78*635a8641SAndroid Build Coastguard Worker #if !defined(OS_MACOSX) || defined(OS_IOS)
79*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<ProcessMetrics> CreateProcessMetrics(
80*635a8641SAndroid Build Coastguard Worker       ProcessHandle process);
81*635a8641SAndroid Build Coastguard Worker #else
82*635a8641SAndroid Build Coastguard Worker 
83*635a8641SAndroid Build Coastguard Worker   // The port provider needs to outlive the ProcessMetrics object returned by
84*635a8641SAndroid Build Coastguard Worker   // this function. If NULL is passed as provider, the returned object
85*635a8641SAndroid Build Coastguard Worker   // only returns valid metrics if |process| is the current process.
86*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<ProcessMetrics> CreateProcessMetrics(
87*635a8641SAndroid Build Coastguard Worker       ProcessHandle process,
88*635a8641SAndroid Build Coastguard Worker       PortProvider* port_provider);
89*635a8641SAndroid Build Coastguard Worker #endif  // !defined(OS_MACOSX) || defined(OS_IOS)
90*635a8641SAndroid Build Coastguard Worker 
91*635a8641SAndroid Build Coastguard Worker   // Creates a ProcessMetrics for the current process. This a cross-platform
92*635a8641SAndroid Build Coastguard Worker   // convenience wrapper for CreateProcessMetrics().
93*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<ProcessMetrics> CreateCurrentProcessMetrics();
94*635a8641SAndroid Build Coastguard Worker 
95*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) || defined(OS_ANDROID)
96*635a8641SAndroid Build Coastguard Worker   // Resident Set Size is a Linux/Android specific memory concept. Do not
97*635a8641SAndroid Build Coastguard Worker   // attempt to extend this to other platforms.
98*635a8641SAndroid Build Coastguard Worker   BASE_EXPORT size_t GetResidentSetSize() const;
99*635a8641SAndroid Build Coastguard Worker #endif
100*635a8641SAndroid Build Coastguard Worker 
101*635a8641SAndroid Build Coastguard Worker #if defined(OS_CHROMEOS)
102*635a8641SAndroid Build Coastguard Worker   // /proc/<pid>/totmaps is a syscall that returns memory summary statistics for
103*635a8641SAndroid Build Coastguard Worker   // the process.
104*635a8641SAndroid Build Coastguard Worker   // totmaps is a Linux specific concept, currently only being used on ChromeOS.
105*635a8641SAndroid Build Coastguard Worker   // Do not attempt to extend this to other platforms.
106*635a8641SAndroid Build Coastguard Worker   //
107*635a8641SAndroid Build Coastguard Worker   struct TotalsSummary {
108*635a8641SAndroid Build Coastguard Worker     size_t private_clean_kb;
109*635a8641SAndroid Build Coastguard Worker     size_t private_dirty_kb;
110*635a8641SAndroid Build Coastguard Worker     size_t swap_kb;
111*635a8641SAndroid Build Coastguard Worker   };
112*635a8641SAndroid Build Coastguard Worker   BASE_EXPORT TotalsSummary GetTotalsSummary() const;
113*635a8641SAndroid Build Coastguard Worker #endif
114*635a8641SAndroid Build Coastguard Worker 
115*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
116*635a8641SAndroid Build Coastguard Worker   struct TaskVMInfo {
117*635a8641SAndroid Build Coastguard Worker     // Only available on macOS 10.12+.
118*635a8641SAndroid Build Coastguard Worker     // Anonymous, non-discardable memory, including non-volatile IOKit.
119*635a8641SAndroid Build Coastguard Worker     // Measured in bytes.
120*635a8641SAndroid Build Coastguard Worker     uint64_t phys_footprint = 0;
121*635a8641SAndroid Build Coastguard Worker 
122*635a8641SAndroid Build Coastguard Worker     // Anonymous, non-discardable, non-compressed memory, excluding IOKit.
123*635a8641SAndroid Build Coastguard Worker     // Measured in bytes.
124*635a8641SAndroid Build Coastguard Worker     uint64_t internal = 0;
125*635a8641SAndroid Build Coastguard Worker 
126*635a8641SAndroid Build Coastguard Worker     // Compressed memory measured in bytes.
127*635a8641SAndroid Build Coastguard Worker     uint64_t compressed = 0;
128*635a8641SAndroid Build Coastguard Worker   };
129*635a8641SAndroid Build Coastguard Worker   TaskVMInfo GetTaskVMInfo() const;
130*635a8641SAndroid Build Coastguard Worker #endif
131*635a8641SAndroid Build Coastguard Worker 
132*635a8641SAndroid Build Coastguard Worker   // Returns the percentage of time spent executing, across all threads of the
133*635a8641SAndroid Build Coastguard Worker   // process, in the interval since the last time the method was called. Since
134*635a8641SAndroid Build Coastguard Worker   // this considers the total execution time across all threads in a process,
135*635a8641SAndroid Build Coastguard Worker   // the result can easily exceed 100% in multi-thread processes running on
136*635a8641SAndroid Build Coastguard Worker   // multi-core systems. In general the result is therefore a value in the
137*635a8641SAndroid Build Coastguard Worker   // range 0% to SysInfo::NumberOfProcessors() * 100%.
138*635a8641SAndroid Build Coastguard Worker   //
139*635a8641SAndroid Build Coastguard Worker   // To obtain the percentage of total available CPU resources consumed by this
140*635a8641SAndroid Build Coastguard Worker   // process over the interval, the caller must divide by NumberOfProcessors().
141*635a8641SAndroid Build Coastguard Worker   //
142*635a8641SAndroid Build Coastguard Worker   // Since this API measures usage over an interval, it will return zero on the
143*635a8641SAndroid Build Coastguard Worker   // first call, and an actual value only on the second and subsequent calls.
144*635a8641SAndroid Build Coastguard Worker   double GetPlatformIndependentCPUUsage();
145*635a8641SAndroid Build Coastguard Worker 
146*635a8641SAndroid Build Coastguard Worker   // Returns the cumulative CPU usage across all threads of the process since
147*635a8641SAndroid Build Coastguard Worker   // process start. In case of multi-core processors, a process can consume CPU
148*635a8641SAndroid Build Coastguard Worker   // at a rate higher than wall-clock time, e.g. two cores at full utilization
149*635a8641SAndroid Build Coastguard Worker   // will result in a time delta of 2 seconds/per 1 wall-clock second.
150*635a8641SAndroid Build Coastguard Worker   TimeDelta GetCumulativeCPUUsage();
151*635a8641SAndroid Build Coastguard Worker 
152*635a8641SAndroid Build Coastguard Worker   // Returns the number of average idle cpu wakeups per second since the last
153*635a8641SAndroid Build Coastguard Worker   // call.
154*635a8641SAndroid Build Coastguard Worker   int GetIdleWakeupsPerSecond();
155*635a8641SAndroid Build Coastguard Worker 
156*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
157*635a8641SAndroid Build Coastguard Worker   // Returns the number of average "package idle exits" per second, which have
158*635a8641SAndroid Build Coastguard Worker   // a higher energy impact than a regular wakeup, since the last call.
159*635a8641SAndroid Build Coastguard Worker   //
160*635a8641SAndroid Build Coastguard Worker   // From the powermetrics man page:
161*635a8641SAndroid Build Coastguard Worker   // "With the exception of some Mac Pro systems, Mac and
162*635a8641SAndroid Build Coastguard Worker   // iOS systems are typically single package systems, wherein all CPUs are
163*635a8641SAndroid Build Coastguard Worker   // part of a single processor complex (typically a single IC die) with shared
164*635a8641SAndroid Build Coastguard Worker   // logic that can include (depending on system specifics) shared last level
165*635a8641SAndroid Build Coastguard Worker   // caches, an integrated memory controller etc. When all CPUs in the package
166*635a8641SAndroid Build Coastguard Worker   // are idle, the hardware can power-gate significant portions of the shared
167*635a8641SAndroid Build Coastguard Worker   // logic in addition to each individual processor's logic, as well as take
168*635a8641SAndroid Build Coastguard Worker   // measures such as placing DRAM in to self-refresh (also referred to as
169*635a8641SAndroid Build Coastguard Worker   // auto-refresh), place interconnects into lower-power states etc"
170*635a8641SAndroid Build Coastguard Worker   int GetPackageIdleWakeupsPerSecond();
171*635a8641SAndroid Build Coastguard Worker #endif
172*635a8641SAndroid Build Coastguard Worker 
173*635a8641SAndroid Build Coastguard Worker   // Retrieves accounting information for all I/O operations performed by the
174*635a8641SAndroid Build Coastguard Worker   // process.
175*635a8641SAndroid Build Coastguard Worker   // If IO information is retrieved successfully, the function returns true
176*635a8641SAndroid Build Coastguard Worker   // and fills in the IO_COUNTERS passed in. The function returns false
177*635a8641SAndroid Build Coastguard Worker   // otherwise.
178*635a8641SAndroid Build Coastguard Worker   bool GetIOCounters(IoCounters* io_counters) const;
179*635a8641SAndroid Build Coastguard Worker 
180*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) || defined(OS_AIX) || defined(OS_ANDROID)
181*635a8641SAndroid Build Coastguard Worker   // Returns the number of file descriptors currently open by the process, or
182*635a8641SAndroid Build Coastguard Worker   // -1 on error.
183*635a8641SAndroid Build Coastguard Worker   int GetOpenFdCount() const;
184*635a8641SAndroid Build Coastguard Worker 
185*635a8641SAndroid Build Coastguard Worker   // Returns the soft limit of file descriptors that can be opened by the
186*635a8641SAndroid Build Coastguard Worker   // process, or -1 on error.
187*635a8641SAndroid Build Coastguard Worker   int GetOpenFdSoftLimit() const;
188*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_LINUX) || defined(OS_AIX) || defined(OS_ANDROID)
189*635a8641SAndroid Build Coastguard Worker 
190*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) || defined(OS_ANDROID)
191*635a8641SAndroid Build Coastguard Worker   // Bytes of swap as reported by /proc/[pid]/status.
192*635a8641SAndroid Build Coastguard Worker   uint64_t GetVmSwapBytes() const;
193*635a8641SAndroid Build Coastguard Worker 
194*635a8641SAndroid Build Coastguard Worker   // Minor and major page fault count as reported by /proc/[pid]/stat.
195*635a8641SAndroid Build Coastguard Worker   // Returns true for success.
196*635a8641SAndroid Build Coastguard Worker   bool GetPageFaultCounts(PageFaultCounts* counts) const;
197*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_LINUX) || defined(OS_ANDROID)
198*635a8641SAndroid Build Coastguard Worker 
199*635a8641SAndroid Build Coastguard Worker   // Returns total memory usage of malloc.
200*635a8641SAndroid Build Coastguard Worker   size_t GetMallocUsage();
201*635a8641SAndroid Build Coastguard Worker 
202*635a8641SAndroid Build Coastguard Worker  private:
203*635a8641SAndroid Build Coastguard Worker #if !defined(OS_MACOSX) || defined(OS_IOS)
204*635a8641SAndroid Build Coastguard Worker   explicit ProcessMetrics(ProcessHandle process);
205*635a8641SAndroid Build Coastguard Worker #else
206*635a8641SAndroid Build Coastguard Worker   ProcessMetrics(ProcessHandle process, PortProvider* port_provider);
207*635a8641SAndroid Build Coastguard Worker #endif  // !defined(OS_MACOSX) || defined(OS_IOS)
208*635a8641SAndroid Build Coastguard Worker 
209*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_AIX)
210*635a8641SAndroid Build Coastguard Worker   int CalculateIdleWakeupsPerSecond(uint64_t absolute_idle_wakeups);
211*635a8641SAndroid Build Coastguard Worker #endif
212*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
213*635a8641SAndroid Build Coastguard Worker   // The subset of wakeups that cause a "package exit" can be tracked on macOS.
214*635a8641SAndroid Build Coastguard Worker   // See |GetPackageIdleWakeupsForSecond| comment for more info.
215*635a8641SAndroid Build Coastguard Worker   int CalculatePackageIdleWakeupsPerSecond(
216*635a8641SAndroid Build Coastguard Worker       uint64_t absolute_package_idle_wakeups);
217*635a8641SAndroid Build Coastguard Worker #endif
218*635a8641SAndroid Build Coastguard Worker 
219*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
220*635a8641SAndroid Build Coastguard Worker   win::ScopedHandle process_;
221*635a8641SAndroid Build Coastguard Worker #else
222*635a8641SAndroid Build Coastguard Worker   ProcessHandle process_;
223*635a8641SAndroid Build Coastguard Worker #endif
224*635a8641SAndroid Build Coastguard Worker 
225*635a8641SAndroid Build Coastguard Worker   // Used to store the previous times and CPU usage counts so we can
226*635a8641SAndroid Build Coastguard Worker   // compute the CPU usage between calls.
227*635a8641SAndroid Build Coastguard Worker   TimeTicks last_cpu_time_;
228*635a8641SAndroid Build Coastguard Worker #if !defined(OS_FREEBSD) || !defined(OS_POSIX)
229*635a8641SAndroid Build Coastguard Worker   TimeDelta last_cumulative_cpu_;
230*635a8641SAndroid Build Coastguard Worker #endif
231*635a8641SAndroid Build Coastguard Worker 
232*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_AIX)
233*635a8641SAndroid Build Coastguard Worker   // Same thing for idle wakeups.
234*635a8641SAndroid Build Coastguard Worker   TimeTicks last_idle_wakeups_time_;
235*635a8641SAndroid Build Coastguard Worker   uint64_t last_absolute_idle_wakeups_;
236*635a8641SAndroid Build Coastguard Worker #endif
237*635a8641SAndroid Build Coastguard Worker 
238*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
239*635a8641SAndroid Build Coastguard Worker   // And same thing for package idle exit wakeups.
240*635a8641SAndroid Build Coastguard Worker   TimeTicks last_package_idle_wakeups_time_;
241*635a8641SAndroid Build Coastguard Worker   uint64_t last_absolute_package_idle_wakeups_;
242*635a8641SAndroid Build Coastguard Worker #endif
243*635a8641SAndroid Build Coastguard Worker 
244*635a8641SAndroid Build Coastguard Worker #if !defined(OS_IOS)
245*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
246*635a8641SAndroid Build Coastguard Worker   // Queries the port provider if it's set.
247*635a8641SAndroid Build Coastguard Worker   mach_port_t TaskForPid(ProcessHandle process) const;
248*635a8641SAndroid Build Coastguard Worker 
249*635a8641SAndroid Build Coastguard Worker   PortProvider* port_provider_;
250*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_MACOSX)
251*635a8641SAndroid Build Coastguard Worker #endif  // !defined(OS_IOS)
252*635a8641SAndroid Build Coastguard Worker 
253*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(ProcessMetrics);
254*635a8641SAndroid Build Coastguard Worker };
255*635a8641SAndroid Build Coastguard Worker 
256*635a8641SAndroid Build Coastguard Worker // Returns the memory committed by the system in KBytes.
257*635a8641SAndroid Build Coastguard Worker // Returns 0 if it can't compute the commit charge.
258*635a8641SAndroid Build Coastguard Worker BASE_EXPORT size_t GetSystemCommitCharge();
259*635a8641SAndroid Build Coastguard Worker 
260*635a8641SAndroid Build Coastguard Worker // Returns the number of bytes in a memory page. Do not use this to compute
261*635a8641SAndroid Build Coastguard Worker // the number of pages in a block of memory for calling mincore(). On some
262*635a8641SAndroid Build Coastguard Worker // platforms, e.g. iOS, mincore() uses a different page size from what is
263*635a8641SAndroid Build Coastguard Worker // returned by GetPageSize().
264*635a8641SAndroid Build Coastguard Worker BASE_EXPORT size_t GetPageSize();
265*635a8641SAndroid Build Coastguard Worker 
266*635a8641SAndroid Build Coastguard Worker // Returns the maximum number of file descriptors that can be open by a process
267*635a8641SAndroid Build Coastguard Worker // at once. If the number is unavailable, a conservative best guess is returned.
268*635a8641SAndroid Build Coastguard Worker BASE_EXPORT size_t GetMaxFds();
269*635a8641SAndroid Build Coastguard Worker 
270*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX)
271*635a8641SAndroid Build Coastguard Worker // Increases the file descriptor soft limit to |max_descriptors| or the OS hard
272*635a8641SAndroid Build Coastguard Worker // limit, whichever is lower. If the limit is already higher than
273*635a8641SAndroid Build Coastguard Worker // |max_descriptors|, then nothing happens.
274*635a8641SAndroid Build Coastguard Worker BASE_EXPORT void IncreaseFdLimitTo(unsigned int max_descriptors);
275*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_POSIX)
276*635a8641SAndroid Build Coastguard Worker 
277*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
278*635a8641SAndroid Build Coastguard Worker     defined(OS_ANDROID) || defined(OS_AIX) || defined(OS_FUCHSIA)
279*635a8641SAndroid Build Coastguard Worker // Data about system-wide memory consumption. Values are in KB. Available on
280*635a8641SAndroid Build Coastguard Worker // Windows, Mac, Linux, Android and Chrome OS.
281*635a8641SAndroid Build Coastguard Worker //
282*635a8641SAndroid Build Coastguard Worker // Total memory are available on all platforms that implement
283*635a8641SAndroid Build Coastguard Worker // GetSystemMemoryInfo(). Total/free swap memory are available on all platforms
284*635a8641SAndroid Build Coastguard Worker // except on Mac. Buffers/cached/active_anon/inactive_anon/active_file/
285*635a8641SAndroid Build Coastguard Worker // inactive_file/dirty/reclaimable/pswpin/pswpout/pgmajfault are available on
286*635a8641SAndroid Build Coastguard Worker // Linux/Android/Chrome OS. Shmem/slab/gem_objects/gem_size are Chrome OS only.
287*635a8641SAndroid Build Coastguard Worker // Speculative/file_backed/purgeable are Mac and iOS only.
288*635a8641SAndroid Build Coastguard Worker // Free is absent on Windows (see "avail_phys" below).
289*635a8641SAndroid Build Coastguard Worker struct BASE_EXPORT SystemMemoryInfoKB {
290*635a8641SAndroid Build Coastguard Worker   SystemMemoryInfoKB();
291*635a8641SAndroid Build Coastguard Worker   SystemMemoryInfoKB(const SystemMemoryInfoKB& other);
292*635a8641SAndroid Build Coastguard Worker 
293*635a8641SAndroid Build Coastguard Worker   // Serializes the platform specific fields to value.
294*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<DictionaryValue> ToValue() const;
295*635a8641SAndroid Build Coastguard Worker 
296*635a8641SAndroid Build Coastguard Worker   int total = 0;
297*635a8641SAndroid Build Coastguard Worker 
298*635a8641SAndroid Build Coastguard Worker #if !defined(OS_WIN)
299*635a8641SAndroid Build Coastguard Worker   int free = 0;
300*635a8641SAndroid Build Coastguard Worker #endif
301*635a8641SAndroid Build Coastguard Worker 
302*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
303*635a8641SAndroid Build Coastguard Worker   // "This is the amount of physical memory that can be immediately reused
304*635a8641SAndroid Build Coastguard Worker   // without having to write its contents to disk first. It is the sum of the
305*635a8641SAndroid Build Coastguard Worker   // size of the standby, free, and zero lists." (MSDN).
306*635a8641SAndroid Build Coastguard Worker   // Standby: not modified pages of physical ram (file-backed memory) that are
307*635a8641SAndroid Build Coastguard Worker   // not actively being used.
308*635a8641SAndroid Build Coastguard Worker   int avail_phys = 0;
309*635a8641SAndroid Build Coastguard Worker #endif
310*635a8641SAndroid Build Coastguard Worker 
311*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX)
312*635a8641SAndroid Build Coastguard Worker   // This provides an estimate of available memory as described here:
313*635a8641SAndroid Build Coastguard Worker   // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
314*635a8641SAndroid Build Coastguard Worker   // NOTE: this is ONLY valid in kernels 3.14 and up.  Its value will always
315*635a8641SAndroid Build Coastguard Worker   // be 0 in earlier kernel versions.
316*635a8641SAndroid Build Coastguard Worker   // Note: it includes _all_ file-backed memory (active + inactive).
317*635a8641SAndroid Build Coastguard Worker   int available = 0;
318*635a8641SAndroid Build Coastguard Worker #endif
319*635a8641SAndroid Build Coastguard Worker 
320*635a8641SAndroid Build Coastguard Worker #if !defined(OS_MACOSX)
321*635a8641SAndroid Build Coastguard Worker   int swap_total = 0;
322*635a8641SAndroid Build Coastguard Worker   int swap_free = 0;
323*635a8641SAndroid Build Coastguard Worker #endif
324*635a8641SAndroid Build Coastguard Worker 
325*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID) || defined(OS_LINUX) || defined(OS_AIX) || \
326*635a8641SAndroid Build Coastguard Worker     defined(OS_FUCHSIA)
327*635a8641SAndroid Build Coastguard Worker   int buffers = 0;
328*635a8641SAndroid Build Coastguard Worker   int cached = 0;
329*635a8641SAndroid Build Coastguard Worker   int active_anon = 0;
330*635a8641SAndroid Build Coastguard Worker   int inactive_anon = 0;
331*635a8641SAndroid Build Coastguard Worker   int active_file = 0;
332*635a8641SAndroid Build Coastguard Worker   int inactive_file = 0;
333*635a8641SAndroid Build Coastguard Worker   int dirty = 0;
334*635a8641SAndroid Build Coastguard Worker   int reclaimable = 0;
335*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_ANDROID) || defined(OS_LINUX) || defined(OS_AIX) ||
336*635a8641SAndroid Build Coastguard Worker         // defined(OS_FUCHSIA)
337*635a8641SAndroid Build Coastguard Worker 
338*635a8641SAndroid Build Coastguard Worker #if defined(OS_CHROMEOS)
339*635a8641SAndroid Build Coastguard Worker   int shmem = 0;
340*635a8641SAndroid Build Coastguard Worker   int slab = 0;
341*635a8641SAndroid Build Coastguard Worker   // Gem data will be -1 if not supported.
342*635a8641SAndroid Build Coastguard Worker   int gem_objects = -1;
343*635a8641SAndroid Build Coastguard Worker   long long gem_size = -1;
344*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_CHROMEOS)
345*635a8641SAndroid Build Coastguard Worker 
346*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
347*635a8641SAndroid Build Coastguard Worker   int speculative = 0;
348*635a8641SAndroid Build Coastguard Worker   int file_backed = 0;
349*635a8641SAndroid Build Coastguard Worker   int purgeable = 0;
350*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_MACOSX)
351*635a8641SAndroid Build Coastguard Worker };
352*635a8641SAndroid Build Coastguard Worker 
353*635a8641SAndroid Build Coastguard Worker // On Linux/Android/Chrome OS, system-wide memory consumption data is parsed
354*635a8641SAndroid Build Coastguard Worker // from /proc/meminfo and /proc/vmstat. On Windows/Mac, it is obtained using
355*635a8641SAndroid Build Coastguard Worker // system API calls.
356*635a8641SAndroid Build Coastguard Worker //
357*635a8641SAndroid Build Coastguard Worker // Fills in the provided |meminfo| structure. Returns true on success.
358*635a8641SAndroid Build Coastguard Worker // Exposed for memory debugging widget.
359*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo);
360*635a8641SAndroid Build Coastguard Worker 
361*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) ||
362*635a8641SAndroid Build Coastguard Worker         // defined(OS_ANDROID) || defined(OS_AIX) || defined(OS_FUCHSIA)
363*635a8641SAndroid Build Coastguard Worker 
364*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX)
365*635a8641SAndroid Build Coastguard Worker // Parse the data found in /proc/<pid>/stat and return the sum of the
366*635a8641SAndroid Build Coastguard Worker // CPU-related ticks.  Returns -1 on parse error.
367*635a8641SAndroid Build Coastguard Worker // Exposed for testing.
368*635a8641SAndroid Build Coastguard Worker BASE_EXPORT int ParseProcStatCPU(StringPiece input);
369*635a8641SAndroid Build Coastguard Worker 
370*635a8641SAndroid Build Coastguard Worker // Get the number of threads of |process| as available in /proc/<pid>/stat.
371*635a8641SAndroid Build Coastguard Worker // This should be used with care as no synchronization with running threads is
372*635a8641SAndroid Build Coastguard Worker // done. This is mostly useful to guarantee being single-threaded.
373*635a8641SAndroid Build Coastguard Worker // Returns 0 on failure.
374*635a8641SAndroid Build Coastguard Worker BASE_EXPORT int GetNumberOfThreads(ProcessHandle process);
375*635a8641SAndroid Build Coastguard Worker 
376*635a8641SAndroid Build Coastguard Worker // /proc/self/exe refers to the current executable.
377*635a8641SAndroid Build Coastguard Worker BASE_EXPORT extern const char kProcSelfExe[];
378*635a8641SAndroid Build Coastguard Worker 
379*635a8641SAndroid Build Coastguard Worker // Parses a string containing the contents of /proc/meminfo
380*635a8641SAndroid Build Coastguard Worker // returns true on success or false for a parsing error
381*635a8641SAndroid Build Coastguard Worker // Exposed for testing.
382*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool ParseProcMeminfo(StringPiece input,
383*635a8641SAndroid Build Coastguard Worker                                   SystemMemoryInfoKB* meminfo);
384*635a8641SAndroid Build Coastguard Worker 
385*635a8641SAndroid Build Coastguard Worker // Data from /proc/vmstat.
386*635a8641SAndroid Build Coastguard Worker struct BASE_EXPORT VmStatInfo {
387*635a8641SAndroid Build Coastguard Worker   // Serializes the platform specific fields to value.
388*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<DictionaryValue> ToValue() const;
389*635a8641SAndroid Build Coastguard Worker 
390*635a8641SAndroid Build Coastguard Worker   unsigned long pswpin = 0;
391*635a8641SAndroid Build Coastguard Worker   unsigned long pswpout = 0;
392*635a8641SAndroid Build Coastguard Worker   unsigned long pgmajfault = 0;
393*635a8641SAndroid Build Coastguard Worker };
394*635a8641SAndroid Build Coastguard Worker 
395*635a8641SAndroid Build Coastguard Worker // Retrieves data from /proc/vmstat about system-wide vm operations.
396*635a8641SAndroid Build Coastguard Worker // Fills in the provided |vmstat| structure. Returns true on success.
397*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool GetVmStatInfo(VmStatInfo* vmstat);
398*635a8641SAndroid Build Coastguard Worker 
399*635a8641SAndroid Build Coastguard Worker // Parses a string containing the contents of /proc/vmstat
400*635a8641SAndroid Build Coastguard Worker // returns true on success or false for a parsing error
401*635a8641SAndroid Build Coastguard Worker // Exposed for testing.
402*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool ParseProcVmstat(StringPiece input, VmStatInfo* vmstat);
403*635a8641SAndroid Build Coastguard Worker 
404*635a8641SAndroid Build Coastguard Worker // Data from /proc/diskstats about system-wide disk I/O.
405*635a8641SAndroid Build Coastguard Worker struct BASE_EXPORT SystemDiskInfo {
406*635a8641SAndroid Build Coastguard Worker   SystemDiskInfo();
407*635a8641SAndroid Build Coastguard Worker   SystemDiskInfo(const SystemDiskInfo& other);
408*635a8641SAndroid Build Coastguard Worker 
409*635a8641SAndroid Build Coastguard Worker   // Serializes the platform specific fields to value.
410*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<Value> ToValue() const;
411*635a8641SAndroid Build Coastguard Worker 
412*635a8641SAndroid Build Coastguard Worker   uint64_t reads = 0;
413*635a8641SAndroid Build Coastguard Worker   uint64_t reads_merged = 0;
414*635a8641SAndroid Build Coastguard Worker   uint64_t sectors_read = 0;
415*635a8641SAndroid Build Coastguard Worker   uint64_t read_time = 0;
416*635a8641SAndroid Build Coastguard Worker   uint64_t writes = 0;
417*635a8641SAndroid Build Coastguard Worker   uint64_t writes_merged = 0;
418*635a8641SAndroid Build Coastguard Worker   uint64_t sectors_written = 0;
419*635a8641SAndroid Build Coastguard Worker   uint64_t write_time = 0;
420*635a8641SAndroid Build Coastguard Worker   uint64_t io = 0;
421*635a8641SAndroid Build Coastguard Worker   uint64_t io_time = 0;
422*635a8641SAndroid Build Coastguard Worker   uint64_t weighted_io_time = 0;
423*635a8641SAndroid Build Coastguard Worker };
424*635a8641SAndroid Build Coastguard Worker 
425*635a8641SAndroid Build Coastguard Worker // Checks whether the candidate string is a valid disk name, [hsv]d[a-z]+
426*635a8641SAndroid Build Coastguard Worker // for a generic disk or mmcblk[0-9]+ for the MMC case.
427*635a8641SAndroid Build Coastguard Worker // Names of disk partitions (e.g. sda1) are not valid.
428*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool IsValidDiskName(StringPiece candidate);
429*635a8641SAndroid Build Coastguard Worker 
430*635a8641SAndroid Build Coastguard Worker // Retrieves data from /proc/diskstats about system-wide disk I/O.
431*635a8641SAndroid Build Coastguard Worker // Fills in the provided |diskinfo| structure. Returns true on success.
432*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool GetSystemDiskInfo(SystemDiskInfo* diskinfo);
433*635a8641SAndroid Build Coastguard Worker 
434*635a8641SAndroid Build Coastguard Worker // Returns the amount of time spent in user space since boot across all CPUs.
435*635a8641SAndroid Build Coastguard Worker BASE_EXPORT TimeDelta GetUserCpuTimeSinceBoot();
436*635a8641SAndroid Build Coastguard Worker 
437*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_LINUX) || defined(OS_ANDROID)
438*635a8641SAndroid Build Coastguard Worker 
439*635a8641SAndroid Build Coastguard Worker #if defined(OS_CHROMEOS)
440*635a8641SAndroid Build Coastguard Worker // Data from files in directory /sys/block/zram0 about ZRAM usage.
441*635a8641SAndroid Build Coastguard Worker struct BASE_EXPORT SwapInfo {
SwapInfoSwapInfo442*635a8641SAndroid Build Coastguard Worker   SwapInfo()
443*635a8641SAndroid Build Coastguard Worker       : num_reads(0),
444*635a8641SAndroid Build Coastguard Worker         num_writes(0),
445*635a8641SAndroid Build Coastguard Worker         compr_data_size(0),
446*635a8641SAndroid Build Coastguard Worker         orig_data_size(0),
447*635a8641SAndroid Build Coastguard Worker         mem_used_total(0) {
448*635a8641SAndroid Build Coastguard Worker   }
449*635a8641SAndroid Build Coastguard Worker 
450*635a8641SAndroid Build Coastguard Worker   // Serializes the platform specific fields to value.
451*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<Value> ToValue() const;
452*635a8641SAndroid Build Coastguard Worker 
453*635a8641SAndroid Build Coastguard Worker   uint64_t num_reads = 0;
454*635a8641SAndroid Build Coastguard Worker   uint64_t num_writes = 0;
455*635a8641SAndroid Build Coastguard Worker   uint64_t compr_data_size = 0;
456*635a8641SAndroid Build Coastguard Worker   uint64_t orig_data_size = 0;
457*635a8641SAndroid Build Coastguard Worker   uint64_t mem_used_total = 0;
458*635a8641SAndroid Build Coastguard Worker };
459*635a8641SAndroid Build Coastguard Worker 
460*635a8641SAndroid Build Coastguard Worker // Parses a string containing the contents of /sys/block/zram0/mm_stat.
461*635a8641SAndroid Build Coastguard Worker // This should be used for the new ZRAM sysfs interfaces.
462*635a8641SAndroid Build Coastguard Worker // Returns true on success or false for a parsing error.
463*635a8641SAndroid Build Coastguard Worker // Exposed for testing.
464*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool ParseZramMmStat(StringPiece mm_stat_data, SwapInfo* swap_info);
465*635a8641SAndroid Build Coastguard Worker 
466*635a8641SAndroid Build Coastguard Worker // Parses a string containing the contents of /sys/block/zram0/stat
467*635a8641SAndroid Build Coastguard Worker // This should be used for the new ZRAM sysfs interfaces.
468*635a8641SAndroid Build Coastguard Worker // Returns true on success or false for a parsing error.
469*635a8641SAndroid Build Coastguard Worker // Exposed for testing.
470*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool ParseZramStat(StringPiece stat_data, SwapInfo* swap_info);
471*635a8641SAndroid Build Coastguard Worker 
472*635a8641SAndroid Build Coastguard Worker // In ChromeOS, reads files from /sys/block/zram0 that contain ZRAM usage data.
473*635a8641SAndroid Build Coastguard Worker // Fills in the provided |swap_data| structure.
474*635a8641SAndroid Build Coastguard Worker // Returns true on success or false for a parsing error.
475*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool GetSwapInfo(SwapInfo* swap_info);
476*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_CHROMEOS)
477*635a8641SAndroid Build Coastguard Worker 
478*635a8641SAndroid Build Coastguard Worker // Collects and holds performance metrics for system memory and disk.
479*635a8641SAndroid Build Coastguard Worker // Provides functionality to retrieve the data on various platforms and
480*635a8641SAndroid Build Coastguard Worker // to serialize the stored data.
481*635a8641SAndroid Build Coastguard Worker class SystemMetrics {
482*635a8641SAndroid Build Coastguard Worker  public:
483*635a8641SAndroid Build Coastguard Worker   SystemMetrics();
484*635a8641SAndroid Build Coastguard Worker 
485*635a8641SAndroid Build Coastguard Worker   static SystemMetrics Sample();
486*635a8641SAndroid Build Coastguard Worker 
487*635a8641SAndroid Build Coastguard Worker   // Serializes the system metrics to value.
488*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<Value> ToValue() const;
489*635a8641SAndroid Build Coastguard Worker 
490*635a8641SAndroid Build Coastguard Worker  private:
491*635a8641SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(SystemMetricsTest, SystemMetrics);
492*635a8641SAndroid Build Coastguard Worker 
493*635a8641SAndroid Build Coastguard Worker   size_t committed_memory_;
494*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) || defined(OS_ANDROID)
495*635a8641SAndroid Build Coastguard Worker   SystemMemoryInfoKB memory_info_;
496*635a8641SAndroid Build Coastguard Worker   VmStatInfo vmstat_info_;
497*635a8641SAndroid Build Coastguard Worker   SystemDiskInfo disk_info_;
498*635a8641SAndroid Build Coastguard Worker #endif
499*635a8641SAndroid Build Coastguard Worker #if defined(OS_CHROMEOS)
500*635a8641SAndroid Build Coastguard Worker   SwapInfo swap_info_;
501*635a8641SAndroid Build Coastguard Worker #endif
502*635a8641SAndroid Build Coastguard Worker };
503*635a8641SAndroid Build Coastguard Worker 
504*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX) && !defined(OS_IOS)
505*635a8641SAndroid Build Coastguard Worker enum class MachVMRegionResult {
506*635a8641SAndroid Build Coastguard Worker   // There were no more memory regions between |address| and the end of the
507*635a8641SAndroid Build Coastguard Worker   // virtual address space.
508*635a8641SAndroid Build Coastguard Worker   Finished,
509*635a8641SAndroid Build Coastguard Worker 
510*635a8641SAndroid Build Coastguard Worker   // All output parameters are invalid.
511*635a8641SAndroid Build Coastguard Worker   Error,
512*635a8641SAndroid Build Coastguard Worker 
513*635a8641SAndroid Build Coastguard Worker   // All output parameters are filled in.
514*635a8641SAndroid Build Coastguard Worker   Success
515*635a8641SAndroid Build Coastguard Worker };
516*635a8641SAndroid Build Coastguard Worker 
517*635a8641SAndroid Build Coastguard Worker // Returns info on the first memory region at or after |address|, including
518*635a8641SAndroid Build Coastguard Worker // resident memory and share mode. On Success, |size| reflects the size of the
519*635a8641SAndroid Build Coastguard Worker // memory region.
520*635a8641SAndroid Build Coastguard Worker // |size| and |info| are output parameters, only valid on Success.
521*635a8641SAndroid Build Coastguard Worker // |address| is an in-out parameter, than represents both the address to start
522*635a8641SAndroid Build Coastguard Worker // looking, and the start address of the memory region.
523*635a8641SAndroid Build Coastguard Worker BASE_EXPORT MachVMRegionResult GetTopInfo(mach_port_t task,
524*635a8641SAndroid Build Coastguard Worker                                           mach_vm_size_t* size,
525*635a8641SAndroid Build Coastguard Worker                                           mach_vm_address_t* address,
526*635a8641SAndroid Build Coastguard Worker                                           vm_region_top_info_data_t* info);
527*635a8641SAndroid Build Coastguard Worker 
528*635a8641SAndroid Build Coastguard Worker // Returns info on the first memory region at or after |address|, including
529*635a8641SAndroid Build Coastguard Worker // protection values. On Success, |size| reflects the size of the
530*635a8641SAndroid Build Coastguard Worker // memory region.
531*635a8641SAndroid Build Coastguard Worker // Returns info on the first memory region at or after |address|, including
532*635a8641SAndroid Build Coastguard Worker // resident memory and share mode.
533*635a8641SAndroid Build Coastguard Worker // |size| and |info| are output parameters, only valid on Success.
534*635a8641SAndroid Build Coastguard Worker BASE_EXPORT MachVMRegionResult GetBasicInfo(mach_port_t task,
535*635a8641SAndroid Build Coastguard Worker                                             mach_vm_size_t* size,
536*635a8641SAndroid Build Coastguard Worker                                             mach_vm_address_t* address,
537*635a8641SAndroid Build Coastguard Worker                                             vm_region_basic_info_64* info);
538*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_MACOSX) && !defined(OS_IOS)
539*635a8641SAndroid Build Coastguard Worker 
540*635a8641SAndroid Build Coastguard Worker }  // namespace base
541*635a8641SAndroid Build Coastguard Worker 
542*635a8641SAndroid Build Coastguard Worker #endif  // BASE_PROCESS_PROCESS_METRICS_H_
543