process_metrics.h 24 KB

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