• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors
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 <stddef.h>
12 #include <stdint.h>
13 
14 #include <memory>
15 #include <string_view>
16 
17 #include "base/base_export.h"
18 #include "base/gtest_prod_util.h"
19 #include "base/memory/raw_ptr.h"
20 #include "base/process/process_handle.h"
21 #include "base/time/time.h"
22 #include "base/types/expected.h"
23 #include "base/values.h"
24 #include "build/build_config.h"
25 
26 #if BUILDFLAG(IS_APPLE)
27 #include <mach/mach.h>
28 #include "base/process/port_provider_mac.h"
29 
30 #if !BUILDFLAG(IS_IOS)
31 #include <mach/mach_vm.h>
32 #endif
33 #endif
34 
35 #if BUILDFLAG(IS_WIN)
36 #include "base/win/scoped_handle.h"
37 #include "base/win/windows_types.h"
38 #endif
39 
40 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
41     BUILDFLAG(IS_AIX)
42 #include <string>
43 #include <utility>
44 #include <vector>
45 
46 #include "base/threading/platform_thread.h"
47 #endif
48 
49 namespace base {
50 
51 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
52 // Minor and major page fault counts since the process creation.
53 // Both counts are process-wide, and exclude child processes.
54 //
55 // minor: Number of page faults that didn't require disk IO.
56 // major: Number of page faults that required disk IO.
57 struct PageFaultCounts {
58   int64_t minor;
59   int64_t major;
60 };
61 #endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) ||
62         // BUILDFLAG(IS_ANDROID)
63 
64 // Convert a POSIX timeval to microseconds.
65 BASE_EXPORT int64_t TimeValToMicroseconds(const struct timeval& tv);
66 
67 enum class ProcessCPUUsageError {
68   // The OS returned an error while measuring the CPU usage. The possible causes
69   // vary by platform.
70   kSystemError,
71 
72   // Process CPU usage couldn't be measured because the process wasn't running.
73   // Some platforms may return kSystemError instead in this situation.
74   kProcessNotFound,
75 };
76 
77 // Provides performance metrics for a specified process (CPU usage and IO
78 // counters). Use CreateCurrentProcessMetrics() to get an instance for the
79 // current process, or CreateProcessMetrics() to get an instance for an
80 // arbitrary process. Then, access the information with the different get
81 // methods.
82 //
83 // This class exposes a few platform-specific APIs for parsing memory usage, but
84 // these are not intended to generalize to other platforms, since the memory
85 // models differ substantially.
86 //
87 // To obtain consistent memory metrics, use the memory_instrumentation service.
88 //
89 // For further documentation on memory, see
90 // https://chromium.googlesource.com/chromium/src/+/HEAD/docs/README.md#Memory
91 class BASE_EXPORT ProcessMetrics {
92  public:
93   ProcessMetrics(const ProcessMetrics&) = delete;
94   ProcessMetrics& operator=(const ProcessMetrics&) = delete;
95 
96   ~ProcessMetrics();
97 
98   // Creates a ProcessMetrics for the specified process.
99 #if !BUILDFLAG(IS_MAC)
100   static std::unique_ptr<ProcessMetrics> CreateProcessMetrics(
101       ProcessHandle process);
102 #else
103 
104   // The port provider needs to outlive the ProcessMetrics object returned by
105   // this function. If NULL is passed as provider, the returned object
106   // only returns valid metrics if |process| is the current process.
107   static std::unique_ptr<ProcessMetrics> CreateProcessMetrics(
108       ProcessHandle process,
109       PortProvider* port_provider);
110 #endif  // !BUILDFLAG(IS_MAC)
111 
112   // Creates a ProcessMetrics for the current process. This a cross-platform
113   // convenience wrapper for CreateProcessMetrics().
114   static std::unique_ptr<ProcessMetrics> CreateCurrentProcessMetrics();
115 
116 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
117   // Resident Set Size is a Linux/Android specific memory concept. Do not
118   // attempt to extend this to other platforms.
119   BASE_EXPORT size_t GetResidentSetSize() const;
120 #endif
121 
122   // Returns the percentage of time spent executing, across all threads of the
123   // process, in the interval since the last time the method was called, using
124   // the current |cumulative_cpu|. Since this considers the total execution time
125   // across all threads in a process, the result can easily exceed 100% in
126   // multi-thread processes running on multi-core systems. In general the result
127   // is therefore a value in the range 0% to
128   // SysInfo::NumberOfProcessors() * 100%.
129   //
130   // To obtain the percentage of total available CPU resources consumed by this
131   // process over the interval, the caller must divide by NumberOfProcessors().
132   //
133   // Since this API measures usage over an interval, it will return zero on the
134   // first call, and an actual value only on the second and subsequent calls.
135   [[nodiscard]] double GetPlatformIndependentCPUUsage(TimeDelta cumulative_cpu);
136 
137   // Same as the above, but automatically calls GetCumulativeCPUUsage() to
138   // determine the current cumulative CPU. Returns nullopt if
139   // GetCumulativeCPUUsage() fails.
140   base::expected<double, ProcessCPUUsageError> GetPlatformIndependentCPUUsage();
141 
142   // Returns the cumulative CPU usage across all threads of the process since
143   // process start, or nullopt on error. In case of multi-core processors, a
144   // process can consume CPU at a rate higher than wall-clock time, e.g. two
145   // cores at full utilization will result in a time delta of 2 seconds/per 1
146   // wall-clock second.
147   base::expected<TimeDelta, ProcessCPUUsageError> GetCumulativeCPUUsage();
148 
149 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
150     BUILDFLAG(IS_AIX)
151   // Emits the cumulative CPU usage for all currently active threads since they
152   // were started into the output parameter (replacing its current contents).
153   // Threads that have already terminated will not be reported. Thus, the sum of
154   // these times may not equal the value returned by GetCumulativeCPUUsage().
155   // Returns false on failure. We return the usage via an output parameter to
156   // allow reuse of CPUUsagePerThread's std::vector by the caller, e.g. to avoid
157   // allocations between repeated calls to this method.
158   // NOTE: Currently only supported on Linux/Android.
159   using CPUUsagePerThread = std::vector<std::pair<PlatformThreadId, TimeDelta>>;
160   bool GetCumulativeCPUUsagePerThread(CPUUsagePerThread&);
161 #endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) ||
162         // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_AIX)
163 
164   // Returns the number of average idle cpu wakeups per second since the last
165   // call.
166   int GetIdleWakeupsPerSecond();
167 
168 #if BUILDFLAG(IS_APPLE)
169   // Returns the number of average "package idle exits" per second, which have
170   // a higher energy impact than a regular wakeup, since the last call.
171   //
172   // From the powermetrics man page:
173   // "With the exception of some Mac Pro systems, Mac and
174   // iOS systems are typically single package systems, wherein all CPUs are
175   // part of a single processor complex (typically a single IC die) with shared
176   // logic that can include (depending on system specifics) shared last level
177   // caches, an integrated memory controller etc. When all CPUs in the package
178   // are idle, the hardware can power-gate significant portions of the shared
179   // logic in addition to each individual processor's logic, as well as take
180   // measures such as placing DRAM in to self-refresh (also referred to as
181   // auto-refresh), place interconnects into lower-power states etc"
182   int GetPackageIdleWakeupsPerSecond();
183 #endif
184 
185 #if BUILDFLAG(IS_POSIX)
186   // Returns the number of file descriptors currently open by the process, or
187   // -1 on error.
188   int GetOpenFdCount() const;
189 
190   // Returns the soft limit of file descriptors that can be opened by the
191   // process, or -1 on error.
192   int GetOpenFdSoftLimit() const;
193 #endif  // BUILDFLAG(IS_POSIX)
194 
195 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
196   // Bytes of swap as reported by /proc/[pid]/status.
197   uint64_t GetVmSwapBytes() const;
198 
199   // Minor and major page fault count as reported by /proc/[pid]/stat.
200   // Returns true for success.
201   bool GetPageFaultCounts(PageFaultCounts* counts) const;
202 #endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) ||
203         // BUILDFLAG(IS_ANDROID)
204 
205   // Returns total memory usage of malloc.
206   size_t GetMallocUsage();
207 
208  private:
209 #if !BUILDFLAG(IS_MAC)
210   explicit ProcessMetrics(ProcessHandle process);
211 #else
212   ProcessMetrics(ProcessHandle process, PortProvider* port_provider);
213 #endif  // !BUILDFLAG(IS_MAC)
214 
215 #if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
216     BUILDFLAG(IS_AIX)
217   int CalculateIdleWakeupsPerSecond(uint64_t absolute_idle_wakeups);
218 #endif
219 #if BUILDFLAG(IS_APPLE)
220   // The subset of wakeups that cause a "package exit" can be tracked on macOS.
221   // See |GetPackageIdleWakeupsForSecond| comment for more info.
222   int CalculatePackageIdleWakeupsPerSecond(
223       uint64_t absolute_package_idle_wakeups);
224 
225   // Queries the port provider if it's set.
226   mach_port_t TaskForHandle(ProcessHandle process_handle) const;
227 #endif
228 
229 #if BUILDFLAG(IS_WIN)
230   win::ScopedHandle process_;
231 #else
232   ProcessHandle process_;
233 #endif
234 
235   // Used to store the previous times and CPU usage counts so we can
236   // compute the CPU usage between calls.
237   TimeTicks last_cpu_time_;
238 #if !BUILDFLAG(IS_FREEBSD) || !BUILDFLAG(IS_POSIX)
239   TimeDelta last_cumulative_cpu_;
240 #endif
241 
242 #if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
243     BUILDFLAG(IS_AIX)
244   // Same thing for idle wakeups.
245   TimeTicks last_idle_wakeups_time_;
246   uint64_t last_absolute_idle_wakeups_;
247 #endif
248 
249 #if BUILDFLAG(IS_APPLE)
250   // And same thing for package idle exit wakeups.
251   TimeTicks last_package_idle_wakeups_time_;
252   uint64_t last_absolute_package_idle_wakeups_;
253 
254   // Works around a race condition when combining two task_info() calls to
255   // measure CPU time.
256   TimeDelta last_measured_cpu_;
257 #endif
258 
259 #if BUILDFLAG(IS_MAC)
260   raw_ptr<PortProvider> port_provider_;
261 #endif  // BUILDFLAG(IS_MAC)
262 };
263 
264 // Returns the memory committed by the system in KBytes.
265 // Returns 0 if it can't compute the commit charge.
266 BASE_EXPORT size_t GetSystemCommitCharge();
267 
268 // Returns the maximum number of file descriptors that can be open by a process
269 // at once. If the number is unavailable, a conservative best guess is returned.
270 BASE_EXPORT size_t GetMaxFds();
271 
272 // Returns the maximum number of handles that can be open at once per process.
273 BASE_EXPORT size_t GetHandleLimit();
274 
275 #if BUILDFLAG(IS_POSIX)
276 // Increases the file descriptor soft limit to |max_descriptors| or the OS hard
277 // limit, whichever is lower. If the limit is already higher than
278 // |max_descriptors|, then nothing happens.
279 BASE_EXPORT void IncreaseFdLimitTo(unsigned int max_descriptors);
280 #endif  // BUILDFLAG(IS_POSIX)
281 
282 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_LINUX) ||      \
283     BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_AIX) || \
284     BUILDFLAG(IS_FUCHSIA)
285 // Data about system-wide memory consumption. Values are in KB. Available on
286 // Windows, Mac, Linux, Android and Chrome OS.
287 //
288 // Total memory are available on all platforms that implement
289 // GetSystemMemoryInfo(). Total/free swap memory are available on all platforms
290 // except on Mac. Buffers/cached/active_anon/inactive_anon/active_file/
291 // inactive_file/dirty/reclaimable/pswpin/pswpout/pgmajfault are available on
292 // Linux/Android/Chrome OS. Shmem/slab are Chrome OS only.
293 // Speculative/file_backed/purgeable are Mac and iOS only.
294 // Free is absent on Windows (see "avail_phys" below).
295 struct BASE_EXPORT SystemMemoryInfoKB {
296   SystemMemoryInfoKB();
297   SystemMemoryInfoKB(const SystemMemoryInfoKB& other);
298   SystemMemoryInfoKB& operator=(const SystemMemoryInfoKB& other);
299 
300   // Serializes the platform specific fields to value.
301   Value::Dict ToDict() const;
302 
303   int total = 0;
304 
305 #if !BUILDFLAG(IS_WIN)
306   int free = 0;
307 #endif
308 
309 #if BUILDFLAG(IS_WIN)
310   // "This is the amount of physical memory that can be immediately reused
311   // without having to write its contents to disk first. It is the sum of the
312   // size of the standby, free, and zero lists." (MSDN).
313   // Standby: not modified pages of physical ram (file-backed memory) that are
314   // not actively being used.
315   int avail_phys = 0;
316 #endif
317 
318 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
319     BUILDFLAG(IS_AIX)
320   // This provides an estimate of available memory as described here:
321   // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
322   // NOTE: this is ONLY valid in kernels 3.14 and up.  Its value will always
323   // be 0 in earlier kernel versions.
324   // Note: it includes _all_ file-backed memory (active + inactive).
325   int available = 0;
326 #endif
327 
328 #if !BUILDFLAG(IS_APPLE)
329   int swap_total = 0;
330   int swap_free = 0;
331 #endif
332 
333 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
334     BUILDFLAG(IS_AIX) || BUILDFLAG(IS_FUCHSIA)
335   int buffers = 0;
336   int cached = 0;
337   int active_anon = 0;
338   int inactive_anon = 0;
339   int active_file = 0;
340   int inactive_file = 0;
341   int dirty = 0;
342   int reclaimable = 0;
343 #endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) ||
344         // BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_AIX) BUILDFLAG(IS_FUCHSIA)
345 
346 #if BUILDFLAG(IS_CHROMEOS)
347   int shmem = 0;
348   int slab = 0;
349 #endif  // BUILDFLAG(IS_CHROMEOS)
350 
351 #if BUILDFLAG(IS_APPLE)
352   int speculative = 0;
353   int file_backed = 0;
354   int purgeable = 0;
355 #endif  // BUILDFLAG(IS_APPLE)
356 };
357 
358 // On Linux/Android/Chrome OS, system-wide memory consumption data is parsed
359 // from /proc/meminfo and /proc/vmstat. On Windows/Mac, it is obtained using
360 // system API calls.
361 //
362 // Fills in the provided |meminfo| structure. Returns true on success.
363 // Exposed for memory debugging widget.
364 BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo);
365 
366 #endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_LINUX) ||
367         // BUILDFLAG(IS_CHROMEOS) BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_AIX) ||
368         // BUILDFLAG(IS_FUCHSIA)
369 
370 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
371     BUILDFLAG(IS_AIX)
372 // Parse the data found in /proc/<pid>/stat and return the sum of the
373 // CPU-related ticks.  Returns -1 on parse error.
374 // Exposed for testing.
375 BASE_EXPORT int ParseProcStatCPU(std::string_view input);
376 
377 // Get the number of threads of |process| as available in /proc/<pid>/stat.
378 // This should be used with care as no synchronization with running threads is
379 // done. This is mostly useful to guarantee being single-threaded.
380 // Returns 0 on failure.
381 BASE_EXPORT int64_t GetNumberOfThreads(ProcessHandle process);
382 
383 // /proc/self/exe refers to the current executable.
384 BASE_EXPORT extern const char kProcSelfExe[];
385 
386 // Parses a string containing the contents of /proc/meminfo
387 // returns true on success or false for a parsing error
388 // Exposed for testing.
389 BASE_EXPORT bool ParseProcMeminfo(std::string_view input,
390                                   SystemMemoryInfoKB* meminfo);
391 
392 // Returns the memory committed by the system in KBytes, as from
393 // GetSystemCommitCharge(), using data from `meminfo` instead of /proc/meminfo.
394 // Exposed for testing.
395 BASE_EXPORT size_t
396 GetSystemCommitChargeFromMeminfo(const SystemMemoryInfoKB& meminfo);
397 
398 // Data from /proc/vmstat.
399 struct BASE_EXPORT VmStatInfo {
400   // Serializes the platform specific fields to value.
401   Value::Dict ToDict() const;
402 
403   uint64_t pswpin = 0;
404   uint64_t pswpout = 0;
405   uint64_t pgmajfault = 0;
406   uint64_t oom_kill = 0;
407 };
408 
409 // Retrieves data from /proc/vmstat about system-wide vm operations.
410 // Fills in the provided |vmstat| structure. Returns true on success.
411 BASE_EXPORT bool GetVmStatInfo(VmStatInfo* vmstat);
412 
413 // Parses a string containing the contents of /proc/vmstat
414 // returns true on success or false for a parsing error
415 // Exposed for testing.
416 BASE_EXPORT bool ParseProcVmstat(std::string_view input, VmStatInfo* vmstat);
417 
418 // Data from /proc/diskstats about system-wide disk I/O.
419 struct BASE_EXPORT SystemDiskInfo {
420   SystemDiskInfo();
421   SystemDiskInfo(const SystemDiskInfo&);
422   SystemDiskInfo& operator=(const SystemDiskInfo&);
423 
424   // Serializes the platform specific fields to value.
425   Value::Dict ToDict() const;
426 
427   uint64_t reads = 0;
428   uint64_t reads_merged = 0;
429   uint64_t sectors_read = 0;
430   uint64_t read_time = 0;
431   uint64_t writes = 0;
432   uint64_t writes_merged = 0;
433   uint64_t sectors_written = 0;
434   uint64_t write_time = 0;
435   uint64_t io = 0;
436   uint64_t io_time = 0;
437   uint64_t weighted_io_time = 0;
438 };
439 
440 // Checks whether the candidate string is a valid disk name, [hsv]d[a-z]+
441 // for a generic disk or mmcblk[0-9]+ for the MMC case.
442 // Names of disk partitions (e.g. sda1) are not valid.
443 BASE_EXPORT bool IsValidDiskName(std::string_view candidate);
444 
445 // Retrieves data from /proc/diskstats about system-wide disk I/O.
446 // Fills in the provided |diskinfo| structure. Returns true on success.
447 BASE_EXPORT bool GetSystemDiskInfo(SystemDiskInfo* diskinfo);
448 
449 // Returns the amount of time spent in user space since boot across all CPUs.
450 BASE_EXPORT TimeDelta GetUserCpuTimeSinceBoot();
451 
452 #endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) ||
453         // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_AIX)
454 
455 #if BUILDFLAG(IS_CHROMEOS)
456 // Data from files in directory /sys/block/zram0 about ZRAM usage.
457 struct BASE_EXPORT SwapInfo {
SwapInfoSwapInfo458   SwapInfo()
459       : num_reads(0),
460         num_writes(0),
461         compr_data_size(0),
462         orig_data_size(0),
463         mem_used_total(0) {
464   }
465 
466   // Serializes the platform specific fields to value.
467   Value::Dict ToDict() const;
468 
469   uint64_t num_reads = 0;
470   uint64_t num_writes = 0;
471   uint64_t compr_data_size = 0;
472   uint64_t orig_data_size = 0;
473   uint64_t mem_used_total = 0;
474 };
475 
476 // Parses a string containing the contents of /sys/block/zram0/mm_stat.
477 // This should be used for the new ZRAM sysfs interfaces.
478 // Returns true on success or false for a parsing error.
479 // Exposed for testing.
480 BASE_EXPORT bool ParseZramMmStat(std::string_view mm_stat_data,
481                                  SwapInfo* swap_info);
482 
483 // Parses a string containing the contents of /sys/block/zram0/stat
484 // This should be used for the new ZRAM sysfs interfaces.
485 // Returns true on success or false for a parsing error.
486 // Exposed for testing.
487 BASE_EXPORT bool ParseZramStat(std::string_view stat_data, SwapInfo* swap_info);
488 
489 // In ChromeOS, reads files from /sys/block/zram0 that contain ZRAM usage data.
490 // Fills in the provided |swap_data| structure.
491 // Returns true on success or false for a parsing error.
492 BASE_EXPORT bool GetSwapInfo(SwapInfo* swap_info);
493 
494 // Data about GPU memory usage. These fields will be -1 if not supported.
495 struct BASE_EXPORT GraphicsMemoryInfoKB {
496   // Serializes the platform specific fields to value.
497   Value::Dict ToDict() const;
498 
499   int gpu_objects = -1;
500   int64_t gpu_memory_size = -1;
501 };
502 
503 // Report on Chrome OS graphics memory. Returns true on success.
504 // /run/debugfs_gpu is a bind mount into /sys/kernel/debug and synchronously
505 // reading the in-memory files in /sys is fast in most cases. On platform that
506 // reading the graphics memory info is slow, this function returns false.
507 BASE_EXPORT bool GetGraphicsMemoryInfo(GraphicsMemoryInfoKB* gpu_meminfo);
508 
509 #endif  // BUILDFLAG(IS_CHROMEOS)
510 
511 struct BASE_EXPORT SystemPerformanceInfo {
512   SystemPerformanceInfo();
513   SystemPerformanceInfo(const SystemPerformanceInfo& other);
514   SystemPerformanceInfo& operator=(const SystemPerformanceInfo& other);
515 
516   // Serializes the platform specific fields to value.
517   Value::Dict ToDict() const;
518 
519   // Total idle time of all processes in the system (units of 100 ns).
520   uint64_t idle_time = 0;
521   // Number of bytes read.
522   uint64_t read_transfer_count = 0;
523   // Number of bytes written.
524   uint64_t write_transfer_count = 0;
525   // Number of bytes transferred (e.g. DeviceIoControlFile)
526   uint64_t other_transfer_count = 0;
527   // The amount of read operations.
528   uint64_t read_operation_count = 0;
529   // The amount of write operations.
530   uint64_t write_operation_count = 0;
531   // The amount of other operations.
532   uint64_t other_operation_count = 0;
533   // The number of pages written to the system's pagefiles.
534   uint64_t pagefile_pages_written = 0;
535   // The number of write operations performed on the system's pagefiles.
536   uint64_t pagefile_pages_write_ios = 0;
537   // The number of pages of physical memory available to processes running on
538   // the system.
539   uint64_t available_pages = 0;
540   // The number of pages read from disk to resolve page faults.
541   uint64_t pages_read = 0;
542   // The number of read operations initiated to resolve page faults.
543   uint64_t page_read_ios = 0;
544 };
545 
546 // Retrieves performance counters from the operating system.
547 // Fills in the provided |info| structure. Returns true on success.
548 BASE_EXPORT bool GetSystemPerformanceInfo(SystemPerformanceInfo* info);
549 
550 // Collects and holds performance metrics for system memory and disk.
551 // Provides functionality to retrieve the data on various platforms and
552 // to serialize the stored data.
553 class BASE_EXPORT SystemMetrics {
554  public:
555   SystemMetrics();
556 
557   static SystemMetrics Sample();
558 
559   // Serializes the system metrics to value.
560   Value::Dict ToDict() const;
561 
562  private:
563   FRIEND_TEST_ALL_PREFIXES(SystemMetricsTest, SystemMetrics);
564 
565   size_t committed_memory_;
566 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
567   SystemMemoryInfoKB memory_info_;
568   VmStatInfo vmstat_info_;
569   SystemDiskInfo disk_info_;
570 #endif
571 #if BUILDFLAG(IS_CHROMEOS)
572   SwapInfo swap_info_;
573   GraphicsMemoryInfoKB gpu_memory_info_;
574 #endif
575 #if BUILDFLAG(IS_WIN)
576   SystemPerformanceInfo performance_;
577 #endif
578 };
579 
580 #if BUILDFLAG(IS_APPLE)
581 enum class MachVMRegionResult {
582   // There were no more memory regions between |address| and the end of the
583   // virtual address space.
584   Finished,
585 
586   // All output parameters are invalid.
587   Error,
588 
589   // All output parameters are filled in.
590   Success
591 };
592 
593 // Returns info on the first memory region at or after |address|, including
594 // protection values. On Success, |size| reflects the size of the
595 // memory region.
596 // Returns info on the first memory region at or after |address|, including
597 // resident memory and share mode.
598 // |size| and |info| are output parameters, only valid on Success.
599 BASE_EXPORT MachVMRegionResult GetBasicInfo(mach_port_t task,
600                                             mach_vm_size_t* size,
601                                             mach_vm_address_t* address,
602                                             vm_region_basic_info_64* info);
603 
604 // Returns info on the first memory region at or after |address|, including
605 // resident memory and share mode. On Success, |size| reflects the size of the
606 // memory region.
607 // |size| and |info| are output parameters, only valid on Success.
608 // |address| is an in-out parameter, than represents both the address to start
609 // looking, and the start address of the memory region.
610 BASE_EXPORT MachVMRegionResult GetTopInfo(mach_port_t task,
611                                           mach_vm_size_t* size,
612                                           mach_vm_address_t* address,
613                                           vm_region_top_info_data_t* info);
614 #endif  // BUILDFLAG(IS_APPLE)
615 
616 }  // namespace base
617 
618 #endif  // BASE_PROCESS_PROCESS_METRICS_H_
619