1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222

ash / hud_display / memory_graph_page_view.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/hud_display/memory_graph_page_view.h"

#include <algorithm>
#include <numeric>
#include <string>

#include "ash/hud_display/hud_constants.h"
#include "ash/hud_display/reference_lines.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/canvas.h"

namespace ash {
namespace hud_display {

////////////////////////////////////////////////////////////////////////////////
// MemoryGraphPageView, public:

BEGIN_METADATA(MemoryGraphPageView)
END_METADATA

MemoryGraphPageView::MemoryGraphPageView(const base::TimeDelta refresh_interval)
    : graph_chrome_rss_private_(kHUDGraphWidth,
                                Graph::Baseline::kBaselineBottom,
                                Graph::Fill::kSolid,
                                Graph::Style::kLines,
                                SkColorSetA(SK_ColorRED, kHUDAlpha)),
      graph_mem_free_(kHUDGraphWidth,
                      Graph::Baseline::kBaselineBottom,
                      Graph::Fill::kNone,
                      Graph::Style::kLines,
                      SkColorSetA(SK_ColorDKGRAY, kHUDAlpha)),
      graph_mem_used_unknown_(kHUDGraphWidth,
                              Graph::Baseline::kBaselineBottom,
                              Graph::Fill::kSolid,
                              Graph::Style::kLines,
                              SkColorSetA(SK_ColorLTGRAY, kHUDAlpha)),
      graph_renderers_rss_private_(kHUDGraphWidth,
                                   Graph::Baseline::kBaselineBottom,
                                   Graph::Fill::kSolid,
                                   Graph::Style::kLines,
                                   SkColorSetA(SK_ColorCYAN, kHUDAlpha)),
      graph_arc_rss_private_(kHUDGraphWidth,
                             Graph::Baseline::kBaselineBottom,
                             Graph::Fill::kSolid,
                             Graph::Style::kLines,
                             SkColorSetA(SK_ColorMAGENTA, kHUDAlpha)),
      graph_gpu_rss_private_(kHUDGraphWidth,
                             Graph::Baseline::kBaselineBottom,
                             Graph::Fill::kSolid,
                             Graph::Style::kLines,
                             SkColorSetA(SK_ColorRED, kHUDAlpha)),
      graph_gpu_kernel_(kHUDGraphWidth,
                        Graph::Baseline::kBaselineBottom,
                        Graph::Fill::kSolid,
                        Graph::Style::kLines,
                        SkColorSetA(SK_ColorYELLOW, kHUDAlpha)),
      graph_chrome_rss_shared_(kHUDGraphWidth,
                               Graph::Baseline::kBaselineBottom,
                               Graph::Fill::kNone,
                               Graph::Style::kLines,
                               SkColorSetA(SK_ColorBLUE, kHUDAlpha)) {
  const int data_width = graph_arc_rss_private_.max_data_points();
  // Verical ticks are drawn every 10% (10/100 interval).
  constexpr float vertical_ticks_interval = 10 / 100.0;
  // -XX seconds on the left, 0Gb top (will be updated later), 0 seconds on the
  // right, 0 Gb on the bottom. Seconds and Gigabytes are dimensions. Number of
  // data points is data_width. horizontal tick marks are drawn every 10
  // seconds.
  reference_lines_ = CreateReferenceLines(
      static_cast<int>(/*left=*/-data_width * refresh_interval.InSecondsF()),
      /*top=*/0, /*right=*/0, /*bottom=*/0, /*x_unit=*/u"s",
      /*y_unit=*/u"Gb",
      /*horizontal_points_number=*/data_width,
      /*horizontal_ticks_interval=*/10 / refresh_interval.InSecondsF(),
      vertical_ticks_interval);
  // Hide reference lines until we know total memory size.
  reference_lines_->SetVisible(false);

  Legend::Formatter formatter = base::BindRepeating([](float value) {
    return base::ASCIIToUTF16(
        base::StringPrintf("%d Mb", std::max(0, (int)(value * 1024))));
  });

  const std::vector<Legend::Entry> legend({
      {graph_gpu_kernel_, u"GPU Driver",
       u"Kernel GPU buffers as reported\nby base::SystemMemoryInfo::gem_size.",
       formatter},
      {graph_gpu_rss_private_, u"Chrome GPU",
       u"RSS private memory of\n --type=gpu-process Chrome process.",
       formatter},
      // ARC memory is not usually visible (skipped)
      {graph_renderers_rss_private_, u"Renderers",
       u"Sum of RSS private memory of\n--type=renderer Chrome process.",
       formatter},
      {graph_mem_used_unknown_, u"Other",
       u"Amount of other used memory.\nEquals to total used minus known.",
       formatter},
      {graph_mem_free_, u"Free", u"Free memory as reported by kernel.",
       formatter},
      {graph_chrome_rss_private_, u"Browser",
       u"RSS private memory of the\nmain Chrome process.", formatter}
      // Browser RSS hairline skipped.
  });
  CreateLegend(legend);
}

MemoryGraphPageView::~MemoryGraphPageView() = default;

////////////////////////////////////////////////////////////////////////////////

void MemoryGraphPageView::OnPaint(gfx::Canvas* canvas) {
  // TODO: Should probably update last graph point more often than shift graph.

  // Layout graphs.
  gfx::Rect rect = GetContentsBounds();
  // Adjust bounds to not overlap with bordering reference lines.
  rect.Inset(kHUDGraphReferenceLineWidth);
  graph_chrome_rss_private_.Layout(rect, /*base=*/nullptr);
  graph_mem_free_.Layout(rect, &graph_chrome_rss_private_);
  graph_mem_used_unknown_.Layout(rect, &graph_mem_free_);
  graph_renderers_rss_private_.Layout(rect, &graph_mem_used_unknown_);
  graph_arc_rss_private_.Layout(rect, &graph_renderers_rss_private_);
  graph_gpu_rss_private_.Layout(rect, &graph_arc_rss_private_);
  graph_gpu_kernel_.Layout(rect, &graph_gpu_rss_private_);
  // Not stacked.
  graph_chrome_rss_shared_.Layout(rect, /*base=*/nullptr);

  // Paint damaged area now that all parameters have been determined.
  graph_chrome_rss_private_.Draw(canvas);
  graph_mem_free_.Draw(canvas);
  graph_mem_used_unknown_.Draw(canvas);
  graph_renderers_rss_private_.Draw(canvas);
  graph_arc_rss_private_.Draw(canvas);
  graph_gpu_rss_private_.Draw(canvas);
  graph_gpu_kernel_.Draw(canvas);

  graph_chrome_rss_shared_.Draw(canvas);
}

void MemoryGraphPageView::UpdateData(const DataSource::Snapshot& snapshot) {
  // TODO: Should probably update last graph point more often than shift graph.
  const double total = snapshot.total_ram;
  // Nothing to do if data is not available yet.
  if (total < 1)
    return;

  constexpr float one_gigabyte = 1024 * 1024 * 1024;

  if (total_ram_ != total) {
    total_ram_ = total;
    reference_lines_->SetTopLabel(total / one_gigabyte);  // In Gigabytes.
    reference_lines_->SetVisible(true);
  }

  const float chrome_rss_private_unscaled =
      (snapshot.browser_rss - snapshot.browser_rss_shared);
  const float chrome_rss_private = chrome_rss_private_unscaled / total;
  const float mem_free_unscaled = snapshot.free_ram;
  const float mem_free = mem_free_unscaled / total;
  // mem_used_unknown is calculated below.
  const float renderers_rss_private_unscaled =
      snapshot.renderers_rss - snapshot.renderers_rss_shared;
  const float renderers_rss_private = renderers_rss_private_unscaled / total;
  const float arc_rss_private_unscaled =
      snapshot.arc_rss - snapshot.arc_rss_shared;
  const float arc_rss_private = arc_rss_private_unscaled / total;
  const float gpu_rss_private_unscaled =
      snapshot.gpu_rss - snapshot.gpu_rss_shared;
  const float gpu_rss_private = gpu_rss_private_unscaled / total;
  const float gpu_kernel_unscaled = snapshot.gpu_kernel;
  const float gpu_kernel = gpu_kernel_unscaled / total;

  // not stacked.
  const float chrome_rss_shared_unscaled = snapshot.browser_rss_shared;
  const float chrome_rss_shared = chrome_rss_shared_unscaled / total;

  std::vector<float> used_buckets;
  used_buckets.push_back(chrome_rss_private);
  used_buckets.push_back(mem_free);
  used_buckets.push_back(renderers_rss_private);
  used_buckets.push_back(arc_rss_private);
  used_buckets.push_back(gpu_rss_private);
  used_buckets.push_back(gpu_kernel);

  const float mem_used_unknown =
      1 - std::reduce(used_buckets.begin(), used_buckets.end(), 0.0f);
  const float mem_used_unknown_unscaled = mem_used_unknown * total;

  if (mem_used_unknown < 0)
    LOG(WARNING) << "mem_used_unknown=" << mem_used_unknown << " < 0 !";

  // Update graph data.
  graph_chrome_rss_private_.AddValue(
      chrome_rss_private, chrome_rss_private_unscaled / one_gigabyte);
  graph_mem_free_.AddValue(mem_free, mem_free_unscaled / one_gigabyte);
  graph_mem_used_unknown_.AddValue(
      std::max(mem_used_unknown, 0.0f),
      std::max(mem_used_unknown_unscaled / one_gigabyte, 0.0f));
  graph_renderers_rss_private_.AddValue(
      renderers_rss_private, renderers_rss_private_unscaled / one_gigabyte);
  graph_arc_rss_private_.AddValue(arc_rss_private,
                                  arc_rss_private_unscaled / one_gigabyte);
  graph_gpu_rss_private_.AddValue(gpu_rss_private,
                                  gpu_rss_private_unscaled / one_gigabyte);
  graph_gpu_kernel_.AddValue(gpu_kernel, gpu_kernel_unscaled / one_gigabyte);
  // Not stacked.
  graph_chrome_rss_shared_.AddValue(chrome_rss_shared,
                                    chrome_rss_shared_unscaled / one_gigabyte);

  RefreshLegendValues();
}

}  // namespace hud_display
}  // namespace ash