aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/counters/timer.h
blob: 29bdc17a98224594cfd3660c71d5ddd5a5565209 (plain) (blame)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once

#include "histogram.h"

#include <util/generic/scope.h> 
 
#include <chrono>

namespace NMonitoring {
    /**
     * A timer counter which aggregates timing durations and provides duration
     * statistics in selected time resolution.
     */
    template <typename TResolution>
    class TTimerImpl {
    public:
        /**
         * Construct a timer given the Lowest and Highest values to be tracked
         * and a number of significant decimal digits. Providing a
         * lowestDiscernibleValue is useful in situations where the units used for
         * the timer's values are much smaller that the minimal accuracy
         * required. E.g. when tracking time values stated in nanosecond units,
         * where the minimal accuracy required is a microsecond, the proper value
         * for lowestDiscernibleValue would be 1000.
         *
         * @param min The lowest value that can be discerned (distinguished from
         *        0) by the timer. Must be a positive integer that is >= 1.
         *        May be internally rounded down to nearest power of 2.
         *
         * @param max The highest value to be tracked by the timer. Must be a
         *        positive integer that is >= (2 * min).
         *
         * @param numberOfSignificantValueDigits Specifies the precision to use.
         *        This is the number of significant decimal digits to which the
         *        timer will maintain value resolution and separation. Must be
         *        a non-negative integer between 0 and 5.
         */
        TTimerImpl(ui64 min, ui64 max, i32 numberOfSignificantValueDigits = 3)
            : TTimerImpl(TResolution(min), TResolution(max),
                         numberOfSignificantValueDigits) {
        }

        /**
         * Construct a timer given the Lowest and Highest values to be tracked
         * and a number of significant decimal digits.
         *
         * @param min The lowest value that can be discerned (distinguished from
         *        0) by the timer.
         *
         * @param max The highest value to be tracked by the histogram. Must be a
         *        positive integer that is >= (2 * min).
         *
         * @param numberOfSignificantValueDigits Specifies the precision to use.
         */
        template <typename TDurationMin, typename TDurationMax>
        TTimerImpl(TDurationMin min, TDurationMax max,
                   i32 numberOfSignificantValueDigits = 3)
            : Histogram_(std::chrono::duration_cast<TResolution>(min).count(),
                         std::chrono::duration_cast<TResolution>(max).count(),
                         numberOfSignificantValueDigits) {
        }

        /**
         * Records a value in the timer with timer resulution. Recorded value will
         * be rounded to a precision at or better than the
         * NumberOfSignificantValueDigits specified at construction time.
         *
         * @param duration duration to add to the timer
         * @return false if the value is larger than the max and can't be recorded,
         *         true otherwise.
         */
        bool RecordValue(ui64 duration) {
            return Histogram_.RecordValue(duration);
        }

        /**
         * Records a duration in the timer. Recorded value will be converted to
         * the timer resulution and rounded to a precision at or better than the
         * NumberOfSignificantValueDigits specified at construction time.
         *
         * @param duration duration to add to the timer
         * @return false if the value is larger than the max and can't be recorded,
         *         true otherwise.
         */
        template <typename TDuration>
        bool RecordValue(TDuration duration) {
            auto count = static_cast<ui64>(
                std::chrono::duration_cast<TResolution>(duration).count());
            return RecordValue(count);
        }

        /**
         * Records count values in the timer with timer resulution. Recorded value will
         * be rounded to a precision at or better than the
         * NumberOfSignificantValueDigits specified at construction time.
         *
         * @param duration duration to add to the timer
         * @param count number of values to add to the histogram
         * @return false if the value is larger than the max and can't be recorded,
         *         true otherwise.
         */
        bool RecordValues(ui64 duration, ui64 count) {
            return Histogram_.RecordValues(duration, count);
        }

        /**
         * Measures a time of functor execution.
         *
         * @param fn functor whose duration should be timed
         */
        template <typename TFunc>
        void Measure(TFunc&& fn) {
            using TClock = std::chrono::high_resolution_clock;

            auto start = TClock::now();
 
            Y_SCOPE_EXIT(this, start) { 
                RecordValue(TClock::now() - start);
            };

            fn();
        }

        /**
         * Place a copy of the value counts accumulated since the last snapshot
         * was taken into {@code snapshot}. Calling this member-function will
         * reset the value counts, and start accumulating value counts for the
         * next interval.
         *
         * @param snapshot the structure into which the values should be copied.
         */
        void TakeSnapshot(THistogramSnapshot* snapshot) {
            Histogram_.TakeSnaphot(snapshot);
        }

    private:
        THdrHistogram Histogram_;
    };

    /**
     * Timer template instantiations for certain time resolutions.
     */
    using TTimerNs = TTimerImpl<std::chrono::nanoseconds>;
    using TTimerUs = TTimerImpl<std::chrono::microseconds>;
    using TTimerMs = TTimerImpl<std::chrono::milliseconds>;
    using TTimerS = TTimerImpl<std::chrono::seconds>;

    /**
     * A timing scope to record elapsed time since creation.
     */
    template <typename TTimer, typename TFunc = std::function<void(std::chrono::high_resolution_clock::duration)>>
    class TTimerScope {
        using TClock = std::chrono::high_resolution_clock;

    public:
        explicit TTimerScope(TTimer* timer, TFunc* callback = nullptr)
            : Timer_(timer)
            , StartTime_(TClock::now())
            , Callback_(callback)
        {
        }

        ~TTimerScope() {
            TClock::duration duration = TClock::now() - StartTime_;
            if (Callback_) {
                (*Callback_)(duration);
            }
            Timer_->RecordValue(duration);
        }

    private:
        TTimer* Timer_;
        TClock::time_point StartTime_;
        TFunc* Callback_;
    };
}