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

cc / metrics / frame_sequence_metrics.h [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.

#ifndef CC_METRICS_FRAME_SEQUENCE_METRICS_H_
#define CC_METRICS_FRAME_SEQUENCE_METRICS_H_

#include <bitset>
#include <cmath>
#include <memory>
#include <vector>

#include "base/check.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/trace_event/traced_value.h"
#include "cc/cc_export.h"
#include "cc/metrics/frame_info.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"

namespace viz {
struct BeginFrameArgs;
}  // namespace viz

namespace cc {
struct FrameInfo;

enum class FrameSequenceTrackerType {
  // Used as an enum for metrics. DO NOT reorder or delete values. Rather,
  // add them at the end and increment kMaxType.
  kCompositorAnimation = 0,
  kMainThreadAnimation = 1,
  kPinchZoom = 2,
  kRAF = 3,
  kTouchScroll = 4,
  kVideo = 6,
  kWheelScroll = 7,
  kScrollbarScroll = 8,
  kCustom = 9,  // Note that the metrics for kCustom are not reported on UMA,
                // and instead are dispatched back to the LayerTreeHostClient.
  kCanvasAnimation = 10,
  kJSAnimation = 11,
  kSETMainThreadAnimation = 12,
  kSETCompositorAnimation = 13,
  kCompositorRasterAnimation = 14,
  kCompositorNativeAnimation = 15,
  kMaxType
};

using ActiveTrackers =
    std::bitset<static_cast<size_t>(FrameSequenceTrackerType::kMaxType)>;

inline bool IsScrollActive(const ActiveTrackers& trackers) {
  return trackers.test(
             static_cast<size_t>(FrameSequenceTrackerType::kWheelScroll)) ||
         trackers.test(
             static_cast<size_t>(FrameSequenceTrackerType::kTouchScroll)) ||
         trackers.test(
             static_cast<size_t>(FrameSequenceTrackerType::kScrollbarScroll));
}

inline bool HasMainThreadAnimation(const ActiveTrackers& trackers) {
  return trackers.test(static_cast<size_t>(
             FrameSequenceTrackerType::kMainThreadAnimation)) ||
         trackers.test(
             static_cast<size_t>(FrameSequenceTrackerType::kCanvasAnimation)) ||
         trackers.test(
             static_cast<size_t>(FrameSequenceTrackerType::kJSAnimation)) ||
         trackers.test(static_cast<size_t>(FrameSequenceTrackerType::kRAF));
}

inline bool HasCompositorThreadAnimation(const ActiveTrackers& trackers) {
  return trackers.test(static_cast<size_t>(
             FrameSequenceTrackerType::kCompositorAnimation)) ||
         trackers.test(static_cast<size_t>(
             FrameSequenceTrackerType::kCompositorRasterAnimation)) ||
         trackers.test(static_cast<size_t>(
             FrameSequenceTrackerType::kCompositorNativeAnimation));
}

class CC_EXPORT FrameSequenceMetrics {
 public:
  explicit FrameSequenceMetrics(FrameSequenceTrackerType type);
  ~FrameSequenceMetrics();

  FrameSequenceMetrics(const FrameSequenceMetrics&) = delete;
  FrameSequenceMetrics& operator=(const FrameSequenceMetrics&) = delete;

  void SetScrollingThread(FrameInfo::SmoothEffectDrivingThread thread);

  struct Jank {
    // The start time of a jank.
    base::TimeTicks start_time;
    // The duration of a jank.
    base::TimeDelta duration;
  };

  struct CC_EXPORT CustomReportData {
    CustomReportData();

    CustomReportData(const CustomReportData&);
    CustomReportData& operator=(const CustomReportData&);

    ~CustomReportData();

    uint32_t frames_expected_v3 = 0;
    uint32_t frames_dropped_v3 = 0;
    uint32_t jank_count_v3 = 0;
    std::vector<Jank> janks;
  };
  using CustomReporter = base::OnceCallback<void(const CustomReportData& data)>;
  // Sets reporter callback for kCustom typed sequence.
  void SetCustomReporter(CustomReporter custom_reporter);

  // Returns the 'effective thread' for the metrics (i.e. the thread most
  // relevant for this metric).
  FrameInfo::SmoothEffectDrivingThread GetEffectiveThread() const;

  void Merge(std::unique_ptr<FrameSequenceMetrics> metrics);
  bool HasEnoughDataForReporting() const;
  bool HasDataLeftForReporting() const;
  // Report related metrics: throughput, checkboarding...
  void ReportMetrics();

  void AddSortedFrame(const viz::BeginFrameArgs& args,
                      const FrameInfo& frame_info);

  FrameSequenceTrackerType type() const { return type_; }

  // Must be called before destructor.
  void ReportLeftoverData();

  void AdoptTrace(FrameSequenceMetrics* adopt_from);

 private:
  friend class FrameSequenceMetricsTest;
  friend class FrameSequenceTrackerTest;
  // FrameInfo is a merger of two threads' frame production. We should only look
  // at the `final_state`, `last_presented_termination_time` and
  // `termination_time` for the GetEffectiveThread.
  void CalculateJankV3(const viz::BeginFrameArgs& args,
                       const FrameInfo& frame_info,
                       FrameInfo::FrameFinalState final_state,
                       base::TimeTicks last_presented_termination_time,
                       base::TimeTicks termination_time);
  void CalculateCheckerboarding(const FrameInfo& frame_info,
                                FrameInfo::FrameFinalState final_state);
  void IncrementJankIdleTimeV3(base::TimeTicks last_presented_termination_time,
                               base::TimeTicks termination_time);
  void TraceJankV3(uint64_t sequence_number,
                   base::TimeTicks last_termination_time,
                   base::TimeTicks termination_time);

  const FrameSequenceTrackerType type_;

  // Track state for measuring the various Graphics.Smoothness V3 metrics.
  struct V3 {
    V3();
    ~V3();
    uint32_t frames_expected = 0;
    uint32_t frames_dropped = 0;
    uint32_t frames_missing_content = 0;
    uint32_t no_update_count = 0;
    uint32_t jank_count = 0;
    viz::BeginFrameArgs last_begin_frame_args;
    FrameInfo last_frame;
    FrameInfo last_presented_frame;
    base::TimeDelta last_frame_delta;
    base::TimeDelta no_update_duration;
    // Note: janks are only recorded for kCustom types sequences
    std::vector<Jank> janks;
  } v3_;

  struct V4 {
    uint32_t frames_dropped = 0;
    uint32_t frames_checkerboarded = 0;
    uint32_t frames_checkerboarded_need_raster = 0;
    uint32_t frames_checkerboarded_need_record = 0;
  } v4_;

  // Tracks some data to generate useful trace events.
  struct TraceData {
    explicit TraceData(FrameSequenceMetrics* metrics);
    ~TraceData();
    raw_ptr<FrameSequenceMetrics> metrics;
    uint64_t last_presented_sequence_number = 0u;
    base::TimeTicks last_timestamp = base::TimeTicks::Now();
    int frame_count = 0;
    bool enabled = false;
    uint64_t trace_id = 0u;

    void Advance(base::TimeTicks start_timestamp,
                 base::TimeTicks new_timestamp,
                 uint32_t expected,
                 uint32_t dropped,
                 uint64_t sequence_number,
                 const char* histogram_name);
    void Terminate(const V3& v3,
                   const V4& v4,
                   FrameInfo::SmoothEffectDrivingThread effective_thread);
  } trace_data_{this};

  FrameInfo::SmoothEffectDrivingThread scrolling_thread_ =
      FrameInfo::SmoothEffectDrivingThread::kUnknown;

  // Callback invoked to report metrics for kCustom typed sequence.
  CustomReporter custom_reporter_;
};

bool ShouldReportForAnimation(FrameSequenceTrackerType sequence_type,
                              FrameInfo::SmoothEffectDrivingThread thread_type);

bool ShouldReportForInteraction(
    FrameSequenceTrackerType sequence_type,
    FrameInfo::SmoothEffectDrivingThread reporting_thread_type,
    FrameInfo::SmoothEffectDrivingThread metrics_effective_thread_type);

}  // namespace cc

#endif  // CC_METRICS_FRAME_SEQUENCE_METRICS_H_