File: histograms.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (126 lines) | stat: -rw-r--r-- 6,057 bytes parent folder | download | duplicates (6)
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
// Copyright 2015 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_BASE_HISTOGRAMS_H_
#define CC_BASE_HISTOGRAMS_H_

#include "base/compiler_specific.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_macros.h"
#include "base/numerics/safe_math.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "cc/base/base_export.h"

namespace cc {

// Supplies a client name to be inserted into histogram names.
// These are known so far: Renderer, Browser
//
// We currently assume that there is only one distinct client per process.
// Not thread-safe. If called multiple times, warns and skips metrics.
CC_BASE_EXPORT void SetClientNameForMetrics(const char* client_name);

// Returns the client name, for use by applicable cc metrics code.
// May return null, in which case no clients, or at least two clients, set the
// client name, and these metrics should be omitted.
//
// This method guarantees that it will never return two distinct non-null
// values over the lifetime of the process.
CC_BASE_EXPORT const char* GetClientNameForMetrics();

// Emits UMA histogram trackers for time spent as well as area (in pixels)
// processed per unit time. Time is measured in microseconds, and work in
// pixels per millisecond. Histogram name should include a %s to grab the client
// name.
//
// Usage:
//   // Outside of a method, perhaps in a namespace.
//   DEFINE_SCOPED_UMA_HISTOGRAM_AREA_TIMER(
//       ScopedReticulateSplinesTimer,
//       "Compositing.%s.ReticulateSplinesUs",
//       "Compositing.%s.ReticulateSplinesPixelsPerMs");
//
//   // Inside a method.
//   ScopedReticulateSplinesTimer timer;
//   timer.AddArea(some_rect.size().GetArea());
//
#define DEFINE_SCOPED_UMA_HISTOGRAM_AREA_TIMER(class_name, time_histogram,     \
                                               area_histogram)                 \
  class class_name : public ScopedUMAHistogramAreaTimerBase {                  \
   public:                                                                     \
    ~class_name();                                                             \
  };                                                                           \
  class_name::~class_name() {                                                  \
    Sample time_sample;                                                        \
    Sample area_sample;                                                        \
    const char* client_name = GetClientNameForMetrics();                       \
    if (client_name && GetHistogramValues(&time_sample, &area_sample)) {       \
      /* GetClientNameForMetrics only returns one non-null value over */       \
      /* the lifetime of the process, so these histogram names are */          \
      /* runtime constant. */                                                  \
      UMA_HISTOGRAM_COUNTS_1M(base::StringPrintf(time_histogram, client_name), \
                              time_sample);                                    \
      UMA_HISTOGRAM_CUSTOM_COUNTS(                                             \
          base::StringPrintf(area_histogram, client_name), area_sample, 1,     \
          100000000, 50);                                                      \
    }                                                                          \
  }

// Version of the above macro for cases which only care about time, not area.
#define DEFINE_SCOPED_UMA_HISTOGRAM_TIMER(class_name, time_histogram)          \
  class class_name : public ScopedUMAHistogramAreaTimerBase {                  \
   public:                                                                     \
    ~class_name();                                                             \
  };                                                                           \
  class_name::~class_name() {                                                  \
    Sample time_sample;                                                        \
    Sample area_sample;                                                        \
    const char* client_name = GetClientNameForMetrics();                       \
    if (client_name && GetHistogramValues(&time_sample, &area_sample)) {       \
      DCHECK_EQ(0, area_sample);                                               \
      /* GetClientNameForMetrics only returns one non-null value over */       \
      /* the lifetime of the process, so these histogram names are */          \
      /* runtime constant. */                                                  \
      UMA_HISTOGRAM_COUNTS_1M(base::StringPrintf(time_histogram, client_name), \
                              time_sample);                                    \
    }                                                                          \
  }

class CC_BASE_EXPORT ScopedUMAHistogramAreaTimerBase {
 public:
  ScopedUMAHistogramAreaTimerBase(const ScopedUMAHistogramAreaTimerBase&) =
      delete;
  ScopedUMAHistogramAreaTimerBase& operator=(
      const ScopedUMAHistogramAreaTimerBase&) = delete;

  void AddArea(const base::CheckedNumeric<int>& area) { area_ += area; }
  void SetArea(const base::CheckedNumeric<int>& area) { area_ = area; }

 protected:
  using Sample32 = base::HistogramBase::Sample32;

  ScopedUMAHistogramAreaTimerBase();
  ~ScopedUMAHistogramAreaTimerBase();

  // Returns true if histograms should be recorded (i.e. values are valid).
  bool GetHistogramValues(Sample32* time_microseconds,
                          Sample32* pixels_per_ms) const;

 private:
  static bool GetHistogramValues(base::TimeDelta elapsed,
                                 int area,
                                 Sample32* time_microseconds,
                                 Sample32* pixels_per_ms);

  base::ElapsedTimer timer_;
  base::CheckedNumeric<int> area_;

  friend class ScopedUMAHistogramAreaTimerBaseTest;
};

}  // namespace cc

#endif  // CC_BASE_HISTOGRAMS_H_