data_source.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2020 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 "ash/hud_display/data_source.h"
  5. #include <algorithm>
  6. #include "ash/hud_display/memory_status.h"
  7. #include "base/bind.h"
  8. #include "base/cxx17_backports.h"
  9. #include "base/threading/thread_restrictions.h"
  10. #include "base/threading/thread_task_runner_handle.h"
  11. namespace ash {
  12. namespace hud_display {
  13. namespace {
  14. // Returns number of bytes rounded up to next Gigabyte.
  15. int64_t EstimatePhysicalRAMSize(int64_t total_ram) {
  16. // Round up to nearest Gigabyte.
  17. constexpr int64_t one_gig = 1024 * 1024 * 1024;
  18. if (total_ram % one_gig) {
  19. return ((total_ram / one_gig) + 1) * one_gig;
  20. }
  21. return total_ram;
  22. }
  23. // Calculates counter difference with respect to overflow.
  24. CpuStats Delta(const CpuStats& newer, const CpuStats& older) {
  25. static_assert(sizeof(CpuStats) == sizeof(uint64_t) * 10,
  26. "This method should be updated when CpuStats is changed.");
  27. // Calculates (left - right) assuming |left| and |right| are increasing
  28. // unsigned counters with respect to possible counter overflow.
  29. auto minus = [](const uint64_t& left, const uint64_t right) {
  30. return left > right
  31. ? (left - right)
  32. : (left + (std::numeric_limits<uint64_t>::max() - right));
  33. };
  34. CpuStats result;
  35. result.user = minus(newer.user, older.user);
  36. result.nice = minus(newer.nice, older.nice);
  37. result.system = minus(newer.system, older.system);
  38. result.idle = minus(newer.idle, older.idle);
  39. result.iowait = minus(newer.iowait, older.iowait);
  40. result.irq = minus(newer.irq, older.irq);
  41. result.softirq = minus(newer.softirq, older.softirq);
  42. result.steal = minus(newer.steal, older.steal);
  43. result.guest = minus(newer.guest, older.guest);
  44. result.guest_nice = minus(newer.guest_nice, older.guest_nice);
  45. return result;
  46. }
  47. // Returns sum of all entries. This is useful for deltas to calculate
  48. // percentage.
  49. uint64_t Sum(const CpuStats& stats) {
  50. static_assert(sizeof(CpuStats) == sizeof(uint64_t) * 10,
  51. "This method should be updated when CpuStats is changed.");
  52. return stats.user + stats.nice + stats.system + stats.idle + stats.iowait +
  53. stats.irq + stats.softirq + stats.steal + stats.guest +
  54. stats.guest_nice;
  55. }
  56. } // anonymous namespace
  57. // --------------------------------
  58. ////////////////////////////////////////////////////////////////////////////////
  59. // DataSource, public:
  60. DataSource::Snapshot::Snapshot() = default;
  61. DataSource::Snapshot::Snapshot(const Snapshot&) = default;
  62. DataSource::Snapshot& DataSource::Snapshot::operator=(const Snapshot&) =
  63. default;
  64. DataSource::DataSource() {
  65. cpu_stats_base_ = {0};
  66. cpu_stats_latest_ = {0};
  67. }
  68. DataSource::~DataSource() = default;
  69. DataSource::Snapshot DataSource::GetSnapshotAndReset() {
  70. // Refresh data synchronously.
  71. Refresh();
  72. Snapshot snapshot = GetSnapshot();
  73. if (cpu_stats_base_.user > 0) {
  74. // Calculate CPU graph values for the last interval.
  75. CpuStats cpu_stats_delta = Delta(cpu_stats_latest_, cpu_stats_base_);
  76. const double cpu_ticks_total = Sum(cpu_stats_delta);
  77. // Makes sure that the given value is between 0 and 1 and converts to
  78. // float.
  79. auto to_0_1 = [](const double& value) -> float {
  80. return base::clamp(static_cast<float>(value), 0.0f, 1.0f);
  81. };
  82. snapshot.cpu_idle_part = cpu_stats_delta.idle / cpu_ticks_total;
  83. snapshot.cpu_user_part =
  84. (cpu_stats_delta.user + cpu_stats_delta.nice) / cpu_ticks_total;
  85. snapshot.cpu_system_part = cpu_stats_delta.system / cpu_ticks_total;
  86. // The remaining part is "other".
  87. snapshot.cpu_other_part =
  88. to_0_1(1 - snapshot.cpu_idle_part - snapshot.cpu_user_part -
  89. snapshot.cpu_system_part);
  90. }
  91. ResetCounters();
  92. return snapshot;
  93. }
  94. DataSource::Snapshot DataSource::GetSnapshot() const {
  95. return snapshot_;
  96. }
  97. void DataSource::ResetCounters() {
  98. snapshot_ = Snapshot();
  99. cpu_stats_base_ = cpu_stats_latest_;
  100. cpu_stats_latest_ = {0};
  101. }
  102. ////////////////////////////////////////////////////////////////////////////////
  103. // DataSource, private:
  104. void DataSource::Refresh() {
  105. const MemoryStatus memory_status;
  106. snapshot_.physical_ram =
  107. std::max(snapshot_.physical_ram,
  108. EstimatePhysicalRAMSize(memory_status.total_ram_size()));
  109. snapshot_.total_ram =
  110. std::max(snapshot_.total_ram, memory_status.total_ram_size());
  111. snapshot_.free_ram = std::min(snapshot_.free_ram, memory_status.total_free());
  112. snapshot_.arc_rss = std::max(snapshot_.arc_rss, memory_status.arc_rss());
  113. snapshot_.arc_rss_shared =
  114. std::max(snapshot_.arc_rss_shared, memory_status.arc_rss_shared());
  115. snapshot_.browser_rss =
  116. std::max(snapshot_.browser_rss, memory_status.browser_rss());
  117. snapshot_.browser_rss_shared = std::max(snapshot_.browser_rss_shared,
  118. memory_status.browser_rss_shared());
  119. snapshot_.renderers_rss =
  120. std::max(snapshot_.renderers_rss, memory_status.renderers_rss());
  121. snapshot_.renderers_rss_shared = std::max(
  122. snapshot_.renderers_rss_shared, memory_status.renderers_rss_shared());
  123. snapshot_.gpu_rss_shared =
  124. std::max(snapshot_.gpu_rss_shared, memory_status.gpu_rss_shared());
  125. snapshot_.gpu_rss = std::max(snapshot_.gpu_rss, memory_status.gpu_rss());
  126. snapshot_.gpu_kernel =
  127. std::max(snapshot_.gpu_kernel, memory_status.gpu_kernel());
  128. cpu_stats_latest_ = GetProcStatCPU();
  129. }
  130. } // namespace hud_display
  131. } // namespace ash