process_metrics.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 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. #include "base/process/process_metrics.h"
  5. #include <utility>
  6. #include "base/check.h"
  7. #include "base/notreached.h"
  8. #include "base/numerics/safe_conversions.h"
  9. #include "base/values.h"
  10. #include "build/build_config.h"
  11. namespace base {
  12. namespace {
  13. #if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
  14. BUILDFLAG(IS_AIX)
  15. int CalculateEventsPerSecond(uint64_t event_count,
  16. uint64_t* last_event_count,
  17. base::TimeTicks* last_calculated) {
  18. const base::TimeTicks time = base::TimeTicks::Now();
  19. int events_per_second = 0;
  20. if (*last_event_count != 0) {
  21. const uint64_t events_delta = event_count - *last_event_count;
  22. const base::TimeDelta time_delta = time - *last_calculated;
  23. DCHECK(!time_delta.is_zero());
  24. events_per_second = ClampRound(events_delta / time_delta.InSecondsF());
  25. }
  26. *last_calculated = time;
  27. *last_event_count = event_count;
  28. return events_per_second;
  29. }
  30. #endif
  31. } // namespace
  32. SystemMemoryInfoKB::SystemMemoryInfoKB() = default;
  33. SystemMemoryInfoKB::SystemMemoryInfoKB(const SystemMemoryInfoKB&) = default;
  34. SystemMemoryInfoKB& SystemMemoryInfoKB::operator=(const SystemMemoryInfoKB&) =
  35. default;
  36. SystemMetrics::SystemMetrics() {
  37. committed_memory_ = 0;
  38. }
  39. SystemMetrics SystemMetrics::Sample() {
  40. SystemMetrics system_metrics;
  41. system_metrics.committed_memory_ = GetSystemCommitCharge();
  42. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  43. GetSystemMemoryInfo(&system_metrics.memory_info_);
  44. GetVmStatInfo(&system_metrics.vmstat_info_);
  45. GetSystemDiskInfo(&system_metrics.disk_info_);
  46. #endif
  47. #if BUILDFLAG(IS_CHROMEOS)
  48. GetSwapInfo(&system_metrics.swap_info_);
  49. GetGraphicsMemoryInfo(&system_metrics.gpu_memory_info_);
  50. #endif
  51. #if BUILDFLAG(IS_WIN)
  52. GetSystemPerformanceInfo(&system_metrics.performance_);
  53. #endif
  54. return system_metrics;
  55. }
  56. Value SystemMetrics::ToValue() const {
  57. Value res(Value::Type::DICTIONARY);
  58. res.SetIntKey("committed_memory", static_cast<int>(committed_memory_));
  59. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  60. Value meminfo = memory_info_.ToValue();
  61. Value vmstat = vmstat_info_.ToValue();
  62. meminfo.MergeDictionary(&vmstat);
  63. res.SetKey("meminfo", std::move(meminfo));
  64. res.SetKey("diskinfo", disk_info_.ToValue());
  65. #endif
  66. #if BUILDFLAG(IS_CHROMEOS)
  67. res.SetKey("swapinfo", swap_info_.ToValue());
  68. res.SetKey("gpu_meminfo", gpu_memory_info_.ToValue());
  69. #endif
  70. #if BUILDFLAG(IS_WIN)
  71. res.SetKey("perfinfo", performance_.ToValue());
  72. #endif
  73. return res;
  74. }
  75. std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateCurrentProcessMetrics() {
  76. #if !BUILDFLAG(IS_MAC)
  77. return CreateProcessMetrics(base::GetCurrentProcessHandle());
  78. #else
  79. return CreateProcessMetrics(base::GetCurrentProcessHandle(), nullptr);
  80. #endif // !BUILDFLAG(IS_MAC)
  81. }
  82. #if !BUILDFLAG(IS_FREEBSD) || !BUILDFLAG(IS_POSIX)
  83. double ProcessMetrics::GetPlatformIndependentCPUUsage() {
  84. TimeDelta cumulative_cpu = GetCumulativeCPUUsage();
  85. TimeTicks time = TimeTicks::Now();
  86. if (last_cumulative_cpu_.is_zero()) {
  87. // First call, just set the last values.
  88. last_cumulative_cpu_ = cumulative_cpu;
  89. last_cpu_time_ = time;
  90. return 0;
  91. }
  92. TimeDelta cpu_time_delta = cumulative_cpu - last_cumulative_cpu_;
  93. TimeDelta time_delta = time - last_cpu_time_;
  94. DCHECK(!time_delta.is_zero());
  95. if (time_delta.is_zero())
  96. return 0;
  97. last_cumulative_cpu_ = cumulative_cpu;
  98. last_cpu_time_ = time;
  99. return 100.0 * cpu_time_delta / time_delta;
  100. }
  101. #endif
  102. #if BUILDFLAG(IS_WIN)
  103. double ProcessMetrics::GetPreciseCPUUsage() {
  104. TimeDelta cumulative_cpu = GetPreciseCumulativeCPUUsage();
  105. TimeTicks time = TimeTicks::Now();
  106. if (last_precise_cumulative_cpu_.is_zero()) {
  107. // First call, just set the last values.
  108. last_precise_cumulative_cpu_ = cumulative_cpu;
  109. last_cpu_time_for_precise_cpu_usage_ = time;
  110. return 0;
  111. }
  112. TimeDelta cpu_time_delta = cumulative_cpu - last_precise_cumulative_cpu_;
  113. TimeDelta time_delta = time - last_cpu_time_for_precise_cpu_usage_;
  114. DCHECK(!time_delta.is_zero());
  115. if (time_delta.is_zero())
  116. return 0;
  117. last_precise_cumulative_cpu_ = cumulative_cpu;
  118. last_cpu_time_for_precise_cpu_usage_ = time;
  119. return 100.0 * cpu_time_delta / time_delta;
  120. }
  121. #endif // BUILDFLAG(IS_WIN)
  122. #if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
  123. BUILDFLAG(IS_AIX)
  124. int ProcessMetrics::CalculateIdleWakeupsPerSecond(
  125. uint64_t absolute_idle_wakeups) {
  126. return CalculateEventsPerSecond(absolute_idle_wakeups,
  127. &last_absolute_idle_wakeups_,
  128. &last_idle_wakeups_time_);
  129. }
  130. #else
  131. int ProcessMetrics::GetIdleWakeupsPerSecond() {
  132. NOTIMPLEMENTED(); // http://crbug.com/120488
  133. return 0;
  134. }
  135. #endif // BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  136. // || BUILDFLAG(IS_AIX)
  137. #if BUILDFLAG(IS_APPLE)
  138. int ProcessMetrics::CalculatePackageIdleWakeupsPerSecond(
  139. uint64_t absolute_package_idle_wakeups) {
  140. return CalculateEventsPerSecond(absolute_package_idle_wakeups,
  141. &last_absolute_package_idle_wakeups_,
  142. &last_package_idle_wakeups_time_);
  143. }
  144. #endif // BUILDFLAG(IS_APPLE)
  145. #if !BUILDFLAG(IS_WIN)
  146. uint64_t ProcessMetrics::GetCumulativeDiskUsageInBytes() {
  147. // Not implemented.
  148. return 0;
  149. }
  150. #endif
  151. } // namespace base