cpu_graph_page_view.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/cpu_graph_page_view.h"
  5. #include <numeric>
  6. #include "ash/hud_display/hud_constants.h"
  7. #include "base/bind.h"
  8. #include "base/cxx17_backports.h"
  9. #include "base/strings/stringprintf.h"
  10. #include "base/strings/utf_string_conversions.h"
  11. #include "ui/base/metadata/metadata_impl_macros.h"
  12. #include "ui/gfx/canvas.h"
  13. namespace ash {
  14. namespace hud_display {
  15. ////////////////////////////////////////////////////////////////////////////////
  16. // CpuGraphPageView, public:
  17. BEGIN_METADATA(CpuGraphPageView, GraphPageViewBase)
  18. END_METADATA
  19. CpuGraphPageView::CpuGraphPageView(const base::TimeDelta refresh_interval)
  20. : cpu_other_(kHUDGraphWidth,
  21. Graph::Baseline::BASELINE_BOTTOM,
  22. Graph::Fill::SOLID,
  23. Graph::Style::LINES,
  24. SkColorSetA(SK_ColorMAGENTA, kHUDAlpha)),
  25. cpu_system_(kHUDGraphWidth,
  26. Graph::Baseline::BASELINE_BOTTOM,
  27. Graph::Fill::SOLID,
  28. Graph::Style::LINES,
  29. SkColorSetA(SK_ColorRED, kHUDAlpha)),
  30. cpu_user_(kHUDGraphWidth,
  31. Graph::Baseline::BASELINE_BOTTOM,
  32. Graph::Fill::SOLID,
  33. Graph::Style::LINES,
  34. SkColorSetA(SK_ColorBLUE, kHUDAlpha)),
  35. cpu_idle_(kHUDGraphWidth,
  36. Graph::Baseline::BASELINE_BOTTOM,
  37. Graph::Fill::SOLID,
  38. Graph::Style::LINES,
  39. SkColorSetA(SK_ColorDKGRAY, kHUDAlpha)) {
  40. const int data_width = cpu_other_.max_data_points();
  41. // Verical ticks are drawn every 10% (10/100 interval).
  42. constexpr float vertical_ticks_interval = (10 / 100.0);
  43. // -XX seconds on the left, 100% top, 0 seconds on the right, 0% on the
  44. // bottom. Seconds and Gigabytes are dimensions. Number of data points is
  45. // cpu_other_.GetDataBufferSize(), horizontal tick marks are drawn every 10
  46. // seconds.
  47. CreateReferenceLines(
  48. /*left=*/static_cast<int>(-data_width * refresh_interval.InSecondsF()),
  49. /*top=*/100, /*right=*/0, /*bottom=*/0,
  50. /*x_unit=*/u"s",
  51. /*y_unit=*/u"%",
  52. /*horizontal_points_number=*/data_width,
  53. /*horizontal_ticks_interval=*/10 / refresh_interval.InSecondsF(),
  54. vertical_ticks_interval);
  55. Legend::Formatter formatter = base::BindRepeating([](float value) {
  56. return base::ASCIIToUTF16(
  57. base::StringPrintf("%d %%", base::clamp((int)(value * 100), 0, 100)));
  58. });
  59. const std::vector<Legend::Entry> legend(
  60. {{cpu_idle_, u"Idle", u"Total amount of CPU time spent\nin idle mode.",
  61. formatter},
  62. {cpu_user_, u"User",
  63. u"Total amount of CPU time spent\n running user processes.", formatter},
  64. {cpu_system_, u"System",
  65. u"Total amount of CPU time spent\nrunning system processes.",
  66. formatter},
  67. {cpu_other_, u"Other",
  68. u"Total amount of CPU time spent\nrunning other tasks.\nThis includes "
  69. u"IO wait, IRQ, guest OS, etc.",
  70. formatter}});
  71. CreateLegend(legend);
  72. }
  73. CpuGraphPageView::~CpuGraphPageView() = default;
  74. ////////////////////////////////////////////////////////////////////////////////
  75. void CpuGraphPageView::OnPaint(gfx::Canvas* canvas) {
  76. // TODO: Should probably update last graph point more often than shift graph.
  77. // Layout graphs.
  78. gfx::Rect rect = GetContentsBounds();
  79. // Adjust bounds to not overlap with bordering reference lines.
  80. rect.Inset(kHUDGraphReferenceLineWidth);
  81. cpu_other_.Layout(rect, nullptr /* base*/);
  82. cpu_system_.Layout(rect, &cpu_other_);
  83. cpu_user_.Layout(rect, &cpu_system_);
  84. cpu_idle_.Layout(rect, &cpu_user_);
  85. // Paint damaged area now that all parameters have been determined.
  86. cpu_other_.Draw(canvas);
  87. cpu_system_.Draw(canvas);
  88. cpu_user_.Draw(canvas);
  89. cpu_idle_.Draw(canvas);
  90. }
  91. void CpuGraphPageView::UpdateData(const DataSource::Snapshot& snapshot) {
  92. // TODO: Should probably update last graph point more often than shift graph.
  93. const float total = snapshot.cpu_idle_part + snapshot.cpu_user_part +
  94. snapshot.cpu_system_part + snapshot.cpu_other_part;
  95. // Nothing to do if data is not available yet (sum < 1%).
  96. if (total < 0.01)
  97. return;
  98. // Assume total already equals 1, no need to re-weight.
  99. // Update graph data.
  100. // unscaled values for CPU are the same. Formatter will display it as %.
  101. cpu_other_.AddValue(snapshot.cpu_other_part, snapshot.cpu_other_part);
  102. cpu_system_.AddValue(snapshot.cpu_system_part, snapshot.cpu_system_part);
  103. cpu_user_.AddValue(snapshot.cpu_user_part, snapshot.cpu_user_part);
  104. cpu_idle_.AddValue(snapshot.cpu_idle_part, snapshot.cpu_idle_part);
  105. RefreshLegendValues();
  106. }
  107. } // namespace hud_display
  108. } // namespace ash