1 // Copyright 2015 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.
5 #ifndef CC_BASE_HISTOGRAMS_H_
6 #define CC_BASE_HISTOGRAMS_H_
8 #include "base/compiler_specific.h"
9 #include "base/metrics/histogram_base.h"
10 #include "base/metrics/histogram_macros.h"
11 #include "base/numerics/safe_math.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/time/time.h"
14 #include "base/timer/elapsed_timer.h"
15 #include "cc/base/cc_export.h"
19 // Supplies a client name to be inserted into histogram names.
20 // These are known so far: Renderer, Browser
22 // We currently assume that there is only one distinct client per process.
23 // Not thread-safe. If called multiple times, warns and skips metrics.
24 CC_EXPORT
void SetClientNameForMetrics(const char* client_name
);
26 // Returns the client name, for use by applicable cc metrics code.
27 // May return null, in which case no clients, or at least two clients, set the
28 // client name, and these metrics should be omitted.
30 // This method guarantees that it will never return two distinct non-null
31 // values over the lifetime of the process.
32 const char* GetClientNameForMetrics();
34 // Emits UMA histogram trackers for time spent as well as area (in pixels)
35 // processed per unit time. Time is measured in microseconds, and work in
36 // pixels per millisecond. Histogram name should include a %s to grab the client
40 // // Outside of a method, perhaps in a namespace.
41 // DEFINE_SCOPED_UMA_HISTOGRAM_AREA_TIMER(
42 // ScopedReticulateSplinesTimer,
43 // "Compositing.%s.ReticulateSplinesUs",
44 // "Compositing.%s.ReticulateSplinesPixelsPerMs");
46 // // Inside a method.
47 // ScopedReticulateSplinesTimer timer;
48 // timer.AddArea(some_rect.size().GetArea());
50 #define DEFINE_SCOPED_UMA_HISTOGRAM_AREA_TIMER(class_name, time_histogram, \
52 class class_name : public ::cc::ScopedUMAHistogramAreaTimerBase { \
56 class_name::~class_name() { \
59 const char* client_name = ::cc::GetClientNameForMetrics(); \
60 if (client_name && GetHistogramValues(&time_sample, &area_sample)) { \
61 /* GetClientNameForMetrics only returns one non-null value over */ \
62 /* the lifetime of the process, so these histogram names are */ \
63 /* runtime constant. */ \
64 UMA_HISTOGRAM_COUNTS(base::StringPrintf(time_histogram, client_name), \
66 UMA_HISTOGRAM_COUNTS(base::StringPrintf(area_histogram, client_name), \
71 class CC_EXPORT ScopedUMAHistogramAreaTimerBase
{
73 void AddArea(int area
) { area_
+= area
; }
74 void SetArea(int area
) { area_
= area
; }
77 using Sample
= base::HistogramBase::Sample
;
79 ScopedUMAHistogramAreaTimerBase();
80 ~ScopedUMAHistogramAreaTimerBase();
82 // Returns true if histograms should be recorded (i.e. values are valid).
83 bool GetHistogramValues(Sample
* time_microseconds
,
84 Sample
* pixels_per_ms
) const;
87 static bool GetHistogramValues(base::TimeDelta elapsed
,
89 Sample
* time_microseconds
,
90 Sample
* pixels_per_ms
);
92 base::ElapsedTimer timer_
;
93 base::CheckedNumeric
<int> area_
;
95 friend class ScopedUMAHistogramAreaTimerBaseTest
;
96 DISALLOW_COPY_AND_ASSIGN(ScopedUMAHistogramAreaTimerBase
);
101 #endif // CC_BASE_HISTOGRAMS_H_