aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/counters/meter.h
blob: 1219f95c4d8f4b12bcb50f0005890e55e01dcb7d (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#pragma once

#include <util/system/types.h>
#include <util/generic/noncopyable.h>
#include <util/system/atomic.h>

#include <chrono>
#include <cstdlib>
#include <cmath>

namespace NMonitoring {
    /**
     * An exponentially-weighted moving average.
     *
     * @see <a href="http://www.teamquest.com/pdfs/whitepaper/ldavg1.pdf">
     *      UNIX Load Average Part 1: How It Works</a>
     * @see <a href="http://www.teamquest.com/pdfs/whitepaper/ldavg2.pdf">
     *      UNIX Load Average Part 2: Not Your Average Average</a>
     * @see <a href="http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average">EMA</a>
     */
    class TMovingAverage {
    public:
        enum {
            INTERVAL = 5 // in seconds
        };

    public:
        /**
         * Creates a new EWMA which is equivalent to the UNIX one minute load
         * average and which expects to be ticked every 5 seconds.
         *
         * @return a one-minute EWMA
         */
        static TMovingAverage OneMinute() {
            static const double M1_ALPHA = 1 - std::exp(-INTERVAL / 60.0 / 1);
            return {M1_ALPHA, std::chrono::seconds(INTERVAL)};
        }

        /**
         * Creates a new EWMA which is equivalent to the UNIX five minute load
         * average and which expects to be ticked every 5 seconds.
         *
         * @return a five-minute EWMA
         */
        static TMovingAverage FiveMinutes() {
            static const double M5_ALPHA = 1 - std::exp(-INTERVAL / 60.0 / 5);
            return {M5_ALPHA, std::chrono::seconds(INTERVAL)};
        }

        /**
         * Creates a new EWMA which is equivalent to the UNIX fifteen minute load
         * average and which expects to be ticked every 5 seconds.
         *
         * @return a fifteen-minute EWMA
         */
        static TMovingAverage FifteenMinutes() {
            static const double M15_ALPHA = 1 - std::exp(-INTERVAL / 60.0 / 15);
            return {M15_ALPHA, std::chrono::seconds(INTERVAL)};
        }

        /**
         * Create a new EWMA with a specific smoothing constant.
         *
         * @param alpha        the smoothing constant
         * @param interval     the expected tick interval
         */
        TMovingAverage(double alpha, std::chrono::seconds interval)
            : Initialized_(0)
            , Rate_(0)
            , Uncounted_(0)
            , Alpha_(alpha)
            , Interval_(std::chrono::nanoseconds(interval).count())
        {
        }

        TMovingAverage(const TMovingAverage& rhs)
            : Initialized_(AtomicGet(rhs.Initialized_))
            , Rate_(AtomicGet(rhs.Rate_))
            , Uncounted_(AtomicGet(rhs.Uncounted_))
            , Alpha_(rhs.Alpha_)
            , Interval_(rhs.Interval_)
        {
        }

        TMovingAverage& operator=(const TMovingAverage& rhs) {
            AtomicSet(Initialized_, AtomicGet(Initialized_));
            AtomicSet(Rate_, AtomicGet(rhs.Rate_));
            AtomicSet(Uncounted_, AtomicGet(rhs.Uncounted_));
            Alpha_ = rhs.Alpha_;
            Interval_ = rhs.Interval_;
            return *this;
        }

        /**
         * Update the moving average with a new value.
         *
         * @param n the new value
         */
        void Update(ui64 n = 1) {
            AtomicAdd(Uncounted_, n);
        }

        /**
         * Mark the passage of time and decay the current rate accordingly.
         */
        void Tick() {
            double instantRate = AtomicSwap(&Uncounted_, 0) / Interval_;
            if (AtomicGet(Initialized_)) {
                double rate = AsDouble(AtomicGet(Rate_));
                rate += (Alpha_ * (instantRate - rate));
                AtomicSet(Rate_, AsAtomic(rate));
            } else {
                AtomicSet(Rate_, AsAtomic(instantRate));
                AtomicSet(Initialized_, 1);
            }
        }

        /**
         * @return the rate in the seconds
         */
        double GetRate() const {
            double rate = AsDouble(AtomicGet(Rate_));
            return rate * std::nano::den;
        }

    private:
        static double AsDouble(TAtomicBase val) {
            union {
                double D;
                TAtomicBase A;
            } doubleAtomic;
            doubleAtomic.A = val;
            return doubleAtomic.D;
        }

        static TAtomicBase AsAtomic(double val) {
            union {
                double D;
                TAtomicBase A;
            } doubleAtomic;
            doubleAtomic.D = val;
            return doubleAtomic.A;
        }

    private:
        TAtomic Initialized_;
        TAtomic Rate_;
        TAtomic Uncounted_;
        double Alpha_;
        double Interval_;
    };

    /**
     * A meter metric which measures mean throughput and one-, five-, and
     * fifteen-minute exponentially-weighted moving average throughputs.
     */
    template <typename TClock>
    class TMeterImpl: private TNonCopyable {
    public:
        TMeterImpl()
            : StartTime_(TClock::now())
            , LastTick_(StartTime_.time_since_epoch().count())
            , Count_(0)
            , OneMinuteRate_(TMovingAverage::OneMinute())
            , FiveMinutesRate_(TMovingAverage::FiveMinutes())
            , FifteenMinutesRate_(TMovingAverage::FifteenMinutes())
        {
        }

        /**
         * Mark the occurrence of events.
         *
         * @param n the number of events
         */
        void Mark(ui64 n = 1) {
            TickIfNecessary();
            AtomicAdd(Count_, n);
            OneMinuteRate_.Update(n);
            FiveMinutesRate_.Update(n);
            FifteenMinutesRate_.Update(n);
        }

        /**
         * Returns the one-minute exponentially-weighted moving average rate at
         * which events have occurred since the meter was created.
         *
         * This rate has the same exponential decay factor as the one-minute load
         * average in the top Unix command.
         *
         * @return the one-minute exponentially-weighted moving average rate at
         *         which events have occurred since the meter was created
         */
        double GetOneMinuteRate() const {
            return OneMinuteRate_.GetRate();
        }

        /**
         * Returns the five-minute exponentially-weighted moving average rate at
         * which events have occurred since the meter was created.
         *
         * This rate has the same exponential decay factor as the five-minute load
         * average in the top Unix command.
         *
         * @return the five-minute exponentially-weighted moving average rate at
         *         which events have occurred since the meter was created
         */
        double GetFiveMinutesRate() const {
            return FiveMinutesRate_.GetRate();
        }

        /**
         * Returns the fifteen-minute exponentially-weighted moving average rate
         * at which events have occurred since the meter was created.
         *
         * This rate has the same exponential decay factor as the fifteen-minute
         * load average in the top Unix command.
         *
         * @return the fifteen-minute exponentially-weighted moving average rate
         *         at which events have occurred since the meter was created
         */
        double GetFifteenMinutesRate() const {
            return FifteenMinutesRate_.GetRate();
        }

        /**
         * @return the mean rate at which events have occurred since the meter
         *         was created
         */
        double GetMeanRate() const {
            if (GetCount() == 0) {
                return 0.0;
            }

            auto now = TClock::now();
            std::chrono::duration<double> elapsedSeconds = now - StartTime_;
            return GetCount() / elapsedSeconds.count();
        }

        /**
         * @return the number of events which have been marked
         */
        ui64 GetCount() const {
            return AtomicGet(Count_);
        }

    private:
        void TickIfNecessary() {
            static ui64 TICK_INTERVAL_NS =
                std::chrono::nanoseconds(
                    std::chrono::seconds(TMovingAverage::INTERVAL))
                    .count();

            auto oldTickNs = AtomicGet(LastTick_);
            auto newTickNs = TClock::now().time_since_epoch().count();
            ui64 elapsedNs = std::abs(newTickNs - oldTickNs);

            if (elapsedNs > TICK_INTERVAL_NS) {
                // adjust to interval begining
                newTickNs -= elapsedNs % TICK_INTERVAL_NS;
                if (AtomicCas(&LastTick_, newTickNs, oldTickNs)) {
                    ui64 requiredTicks = elapsedNs / TICK_INTERVAL_NS;
                    for (ui64 i = 0; i < requiredTicks; ++i) {
                        OneMinuteRate_.Tick();
                        FiveMinutesRate_.Tick();
                        FifteenMinutesRate_.Tick();
                    }
                }
            }
        }

    private:
        const typename TClock::time_point StartTime_;
        TAtomic LastTick_;
        TAtomic Count_;
        TMovingAverage OneMinuteRate_;
        TMovingAverage FiveMinutesRate_;
        TMovingAverage FifteenMinutesRate_;
    };

    using TSystemMeter = TMeterImpl<std::chrono::system_clock>;
    using TSteadyMeter = TMeterImpl<std::chrono::steady_clock>;
    using THighResMeter = TMeterImpl<std::chrono::high_resolution_clock>;
    using TMeter = THighResMeter;

}