summaryrefslogtreecommitdiff
path: root/chromium/ui/latency/histograms.h
blob: bbe4ddb76a8c33de6c14c874f8b3a5bdbafcb817 (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_LATENCY_HISTOGRAMS_H_
#define UI_LATENCY_HISTOGRAMS_H_

#include <array>
#include <memory>

#include "base/macros.h"

namespace ui {

// Used to communicate percentile results to clients.
struct PercentileResults {
  static constexpr double kPercentiles[] = {.50, .99};
  static constexpr size_t kCount = arraysize(kPercentiles);

  double values[kCount]{};
};

namespace frame_metrics {

// This is an interface different metrics will use to inject their ideal
// histogram implementations into the StreamAnalyzer.
class Histogram {
 public:
  Histogram() = default;
  virtual ~Histogram() = default;

  // Increases the bucket that contains |value| by |weight|.
  virtual void AddSample(uint32_t value, uint32_t weight) = 0;

  // Computes and returns the approximate percentiles based on the
  // histogram distribution.
  virtual PercentileResults ComputePercentiles() const = 0;

  // Resets all buckets in the histogram to 0.
  // Higher level logic may periodically reset the the counts after it
  // gathers the percentiles in order to avoid overflow.
  virtual void Reset() = 0;

 private:
  DISALLOW_COPY_AND_ASSIGN(Histogram);
};

// Ratio histogram, with a range of [0, 2^32) and most of it's precision
// just above kFixedPointMultiplier (i.e. a fixed point of 1).
class RatioHistogram : public Histogram {
 public:
  RatioHistogram();
  ~RatioHistogram() override;
  void AddSample(uint32_t ratio, uint32_t weight) override;
  PercentileResults ComputePercentiles() const override;
  void Reset() override;

 private:
  static constexpr size_t kBucketCount = 111;

  uint64_t total_samples_ = 0;
  using BucketArray = std::array<uint32_t, kBucketCount>;
  BucketArray buckets_{};

  DISALLOW_COPY_AND_ASSIGN(RatioHistogram);
};

// A histogram of 98 buckets from 0 to 64 seconds with extra precision
// around common vsync boundaries.
class VSyncHistogram : public Histogram {
 public:
  VSyncHistogram();
  ~VSyncHistogram() override;
  void AddSample(uint32_t microseconds, uint32_t weight) override;
  PercentileResults ComputePercentiles() const override;
  void Reset() override;

 private:
  static constexpr size_t kBucketCount = 98;

  uint64_t total_samples_ = 0;
  using BucketArray = std::array<uint32_t, kBucketCount>;
  BucketArray buckets_{};

  DISALLOW_COPY_AND_ASSIGN(VSyncHistogram);
};

// An interface that allows PercentileHelper to iterate through the
// bucket boundaries of the delegating histogram.
// This is an implemenation detail, but is exposed here for testing purposes.
struct BoundaryIterator {
  virtual ~BoundaryIterator() = default;
  virtual uint64_t Next() = 0;
};

// These expose the internal iterators, so they can be verified in tests.
std::unique_ptr<BoundaryIterator> CreateRatioIteratorForTesting();
std::unique_ptr<BoundaryIterator> CreateVSyncIteratorForTesting();

}  // namespace frame_metrics
}  // namespace ui

#endif  // UI_LATENCY_HISTOGRAMS_H_