1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // histogram_macros.h:
7 //   Helpers for making histograms, to keep consistency with Chromium's
8 //   histogram_macros.h.
9 
10 #ifndef LIBANGLE_HISTOGRAM_MACROS_H_
11 #define LIBANGLE_HISTOGRAM_MACROS_H_
12 
13 #include <platform/Platform.h>
14 
15 #define ANGLE_HISTOGRAM_TIMES(name, sample) ANGLE_HISTOGRAM_CUSTOM_TIMES(name, sample, 1, 10000, 50)
16 
17 #define ANGLE_HISTOGRAM_MEDIUM_TIMES(name, sample) \
18     ANGLE_HISTOGRAM_CUSTOM_TIMES(name, sample, 10, 180000, 50)
19 
20 // Use this macro when times can routinely be much longer than 10 seconds.
21 #define ANGLE_HISTOGRAM_LONG_TIMES(name, sample) \
22     ANGLE_HISTOGRAM_CUSTOM_TIMES(name, sample, 1, 3600000, 50)
23 
24 // Use this macro when times can routinely be much longer than 10 seconds and
25 // you want 100 buckets.
26 #define ANGLE_HISTOGRAM_LONG_TIMES_100(name, sample) \
27     ANGLE_HISTOGRAM_CUSTOM_TIMES(name, sample, 1, 3600000, 100)
28 
29 // For folks that need real specific times, use this to select a precise range
30 // of times you want plotted, and the number of buckets you want used.
31 #define ANGLE_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \
32     ANGLE_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count)
33 
34 #define ANGLE_HISTOGRAM_COUNTS(name, sample) \
35     ANGLE_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 1000000, 50)
36 
37 #define ANGLE_HISTOGRAM_COUNTS_100(name, sample) \
38     ANGLE_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 100, 50)
39 
40 #define ANGLE_HISTOGRAM_COUNTS_10000(name, sample) \
41     ANGLE_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 10000, 50)
42 
43 #define ANGLE_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count)                       \
44     ANGLEPlatformCurrent()->histogramCustomCounts(ANGLEPlatformCurrent(), name, sample, min, max, \
45                                                   bucket_count)
46 
47 #define ANGLE_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \
48     ANGLE_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101)
49 
50 #define ANGLE_HISTOGRAM_BOOLEAN(name, sample) \
51     ANGLEPlatformCurrent()->histogramBoolean(ANGLEPlatformCurrent(), name, sample)
52 
53 #define ANGLE_HISTOGRAM_ENUMERATION(name, sample, boundary_value)                      \
54     ANGLEPlatformCurrent()->histogramEnumeration(ANGLEPlatformCurrent(), name, sample, \
55                                                  boundary_value)
56 
57 #define ANGLE_HISTOGRAM_MEMORY_KB(name, sample) \
58     ANGLE_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1000, 500000, 50)
59 
60 #define ANGLE_HISTOGRAM_MEMORY_MB(name, sample) \
61     ANGLE_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 1000, 50)
62 
63 #define ANGLE_HISTOGRAM_SPARSE_SLOWLY(name, sample) \
64     ANGLEPlatformCurrent()->histogramSparse(ANGLEPlatformCurrent(), name, sample)
65 
66 // Scoped class which logs its time on this earth as a UMA statistic. This is
67 // recommended for when you want a histogram which measures the time it takes
68 // for a method to execute. This measures up to 10 seconds.
69 #define SCOPED_ANGLE_HISTOGRAM_TIMER(name) \
70     SCOPED_ANGLE_HISTOGRAM_TIMER_EXPANDER(name, false, __COUNTER__)
71 
72 // Similar scoped histogram timer, but this uses ANGLE_HISTOGRAM_LONG_TIMES_100,
73 // which measures up to an hour, and uses 100 buckets. This is more expensive
74 // to store, so only use if this often takes >10 seconds.
75 #define SCOPED_ANGLE_HISTOGRAM_LONG_TIMER(name) \
76     SCOPED_ANGLE_HISTOGRAM_TIMER_EXPANDER(name, true, __COUNTER__)
77 
78 // This nested macro is necessary to expand __COUNTER__ to an actual value.
79 #define SCOPED_ANGLE_HISTOGRAM_TIMER_EXPANDER(name, is_long, key) \
80     SCOPED_ANGLE_HISTOGRAM_TIMER_UNIQUE(name, is_long, key)
81 
82 #define SCOPED_ANGLE_HISTOGRAM_TIMER_UNIQUE(name, is_long, key)                         \
83     class ScopedHistogramTimer##key                                                     \
84     {                                                                                   \
85       public:                                                                           \
86         ScopedHistogramTimer##key()                                                     \
87             : constructed_(ANGLEPlatformCurrent()->currentTime(ANGLEPlatformCurrent())) \
88         {}                                                                              \
89         ~ScopedHistogramTimer##key()                                                    \
90         {                                                                               \
91             if (constructed_ == 0)                                                      \
92                 return;                                                                 \
93             auto *platform = ANGLEPlatformCurrent();                                    \
94             double elapsed = platform->currentTime(platform) - constructed_;            \
95             int elapsedMS  = static_cast<int>(elapsed * 1000.0);                        \
96             if (is_long)                                                                \
97             {                                                                           \
98                 ANGLE_HISTOGRAM_LONG_TIMES_100(name, elapsedMS);                        \
99             }                                                                           \
100             else                                                                        \
101             {                                                                           \
102                 ANGLE_HISTOGRAM_TIMES(name, elapsedMS);                                 \
103             }                                                                           \
104         }                                                                               \
105                                                                                         \
106       private:                                                                          \
107         double constructed_;                                                            \
108     } scoped_histogram_timer_##key
109 
110 #endif  // LIBANGLE_HISTOGRAM_MACROS_H_
111