aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/mkql_stats_registry.h
blob: 02b74cd68ede24ad1aadbf88c7812bcf24999b44 (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
#pragma once

#include <util/generic/strbuf.h>
#include <util/generic/intrlist.h>
#include <util/system/datetime.h>
#include <util/system/hp_timer.h>
#include <util/system/yassert.h>

namespace NKikimr {
namespace NMiniKQL {

struct IStatsRegistry;

/**
 * @brief Statistic unique named key.
 *
 * NOTE: Instances of this class must be static objects.
 */
class TStatKey: public TIntrusiveSListItem<TStatKey> {
    friend struct IStatsRegistry;

// -- static part --
    static ui32 IdSequence_;
    static TStatKey* KeysChain_;

// -- instance part --
public:
    TStatKey(TStringBuf name, bool deriv);

    TStringBuf GetName() const noexcept { return Name_; }
    bool IsDeriv() const noexcept { return Deriv_; }
    ui32 GetId() const noexcept { return Id_; }
    static ui32 GetMaxId() { return IdSequence_; }

    template <typename F>
    static void ForEach(F f) {
        for (TStatKey* p = KeysChain_; p;) {
            f(*p);
            auto n = p->Next();
            p = n ? n->Node() : nullptr;
        }
    }

private:
    TStringBuf Name_;
    bool Deriv_;
    ui32 Id_;
};

/**
 * @brief Sase interface for statistics registry implementations.
 */
struct IStatsRegistry {
    virtual ~IStatsRegistry() = default;

    virtual i64 GetStat(const TStatKey& key) const = 0;

    virtual void SetStat(const TStatKey& key, i64 value) = 0;
    virtual void SetMaxStat(const TStatKey& key, i64 value) = 0;
    virtual void SetMinStat(const TStatKey& key, i64 value) = 0;

    virtual void AddStat(const TStatKey& key, i64 value) = 0;

    void IncStat(const TStatKey& key) {
        AddStat(key, 1);
    }

    void DecStat(const TStatKey& key) {
        AddStat(key, -1);
    }

    // TConsumer = void(const TStatKey& key, i64 value)
    template <typename TConsumer>
    void ForEachStat(TConsumer c) const {
        TStatKey::ForEach([this, &c](const auto& key) {
            c(key, GetStat(key));
        });
    }
};

using IStatsRegistryPtr = TAutoPtr<IStatsRegistry>;

IStatsRegistryPtr CreateDefaultStatsRegistry();


#define MKQL_STAT_MOD_IMPL(method, nullableStatsRegistry, key, value) \
    do { \
        if (nullableStatsRegistry) \
            nullableStatsRegistry->method(key, value); \
    } while (0)

#define MKQL_SET_STAT(statRegistry, key, value) \
    MKQL_STAT_MOD_IMPL(SetStat, statRegistry, key, value)

#define MKQL_SET_MIN_STAT(statRegistry, key, value) \
    MKQL_STAT_MOD_IMPL(SetMinStat, statRegistry, key, value)

#define MKQL_SET_MAX_STAT(statRegistry, key, value) \
    MKQL_STAT_MOD_IMPL(SetMaxStat, statRegistry, key, value)

#define MKQL_ADD_STAT(statRegistry, key, value) \
    MKQL_STAT_MOD_IMPL(AddStat, statRegistry, key, value)

#define MKQL_INC_STAT(statRegistry, key) MKQL_ADD_STAT(statRegistry, key, 1)
#define MKQL_DEC_STAT(statRegistry, key) MKQL_ADD_STAT(statRegistry, key, -1)

class TStatTimerBase {
public:
    TStatTimerBase(const TStatKey& key)
        : Key_(key)
        , Total_(0)
        , Start_(0)
    {}

    void Reset() {
        Total_ = 0;
    }

    void Report(IStatsRegistry* statRegistry) const {
        auto value = (i64)(1e3 * Total_ / NHPTimer::GetClockRate());
        MKQL_ADD_STAT(statRegistry, Key_, value);
    }

protected:
    const TStatKey& Key_;
    i64 Total_;
    i64 Start_;
};

class TStatTimer: public TStatTimerBase {
public:
    TStatTimer(const TStatKey& key)
        : TStatTimerBase(key)
    {}

    void Acquire() {
        Start_ = GetCycleCount();
    }

    void Release() {
        Total_ += GetCycleCount() - Start_;
    }
};

class TSamplingStatTimer: public TStatTimerBase {
public:
    TSamplingStatTimer(const TStatKey& key, ui64 frequency = 100)
        : TStatTimerBase(key)
        , Frequency_(frequency)
    {
        Y_ASSERT(Frequency_ > 0);
    }

    void Acquire() {
        if (Counter_ == 0) {
            Start_ = GetCycleCount();
        }
    }

    void Release() {
        if (Counter_ == 0) {
            Total_ += (GetCycleCount() - Start_) * Frequency_;
        }
        if (++Counter_ == Frequency_) {
            Counter_ = 0;
        }
    }
private:
    const ui64 Frequency_;
    ui64 Counter_ = 0;
};

} // namespace NMiniKQL
} // namespace NKikimr