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