aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/udf/udf_counter.h
blob: 2350b597785da8517aa6df74ffb7a65baea33fc3 (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
#pragma once
#include "udf_types.h"
#include "udf_type_size_check.h"
#include <library/cpp/deprecated/atomic/atomic.h>

namespace NYql {
namespace NUdf {

class TCounter {
public:
    TCounter(i64* ptr = nullptr)
        : Ptr_(ptr)
    {}

    void Inc() {
        if (Ptr_) {
            AtomicIncrement(AsAtomic());
        }
    }

    void Dec() {
        if (Ptr_) {
            AtomicDecrement(AsAtomic());
        }
    }

    void Add(i64 delta) {
        if (Ptr_) {
            AtomicAdd(AsAtomic(), delta);
        }
    }

    void Sub(i64 delta) {
        if (Ptr_) {
            AtomicSub(AsAtomic(), delta);
        }
    }

    void Set(i64 value) {
        if (Ptr_) {
            AtomicSet(AsAtomic(), value);
        }
    }

private:
    TAtomic& AsAtomic() {
        return *reinterpret_cast<TAtomic*>(Ptr_);
    }

private:
    i64* Ptr_;
};

UDF_ASSERT_TYPE_SIZE(TCounter, 8);

class IScopedProbeHost {
public:
    virtual ~IScopedProbeHost() = default;

    virtual void Acquire(void* cookie) = 0;

    virtual void Release(void* cookie) = 0;
};

UDF_ASSERT_TYPE_SIZE(IScopedProbeHost, 8);

class TScopedProbe {
public:
    TScopedProbe(IScopedProbeHost* host = nullptr, void* cookie = nullptr)
        : Host_(host ? host : &NullHost_)
        , Cookie_(cookie)
    {}

    void Acquire() {
        Host_->Acquire(Cookie_);
    }

    void Release() {
        Host_->Release(Cookie_);
    }

private:
    class TNullHost : public IScopedProbeHost {
    public:
        void Acquire(void* cookie) override {
            Y_UNUSED(cookie);
        }

        void Release(void* cookie) override {
            Y_UNUSED(cookie);
        }
    };

    IScopedProbeHost* Host_;
    void* Cookie_;
    static TNullHost NullHost_;
};

UDF_ASSERT_TYPE_SIZE(TScopedProbe, 16);

class ICountersProvider {
public:
    virtual ~ICountersProvider() = default;

    virtual TCounter GetCounter(const TStringRef& module, const TStringRef& name, bool deriv) = 0;

    virtual TScopedProbe GetScopedProbe(const TStringRef& module, const TStringRef& name) = 0;
};

UDF_ASSERT_TYPE_SIZE(ICountersProvider, 8);

} // namspace NUdf
} // namspace NYql