aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/common/debug_metrics.cpp
blob: 6235e55f7e6c36710088e7d476bfd2b0149585bf (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
#include "debug_metrics.h"

#include <util/generic/hash.h>
#include <util/generic/singleton.h>

#include <util/string/cast.h>
#include <util/system/mutex.h>

namespace NYT {
namespace NDetail {

////////////////////////////////////////////////////////////////////////////////

class TDebugMetrics {
public:
    static TDebugMetrics& Get()
    {
        return *Singleton<TDebugMetrics>();
    }

    void Inc(TStringBuf name)
    {
        auto g = Guard(Lock_);
        auto it = Metrics_.find(name);
        if (it == Metrics_.end()) {
            it = Metrics_.emplace(ToString(name), 0).first;
        }
        ++it->second;
    }

    ui64 Get(TStringBuf name) const
    {
        auto g = Guard(Lock_);
        auto it = Metrics_.find(name);
        if (it == Metrics_.end()) {
            return 0;
        } else {
            return it->second;
        }
    }

private:
    TMutex Lock_;
    THashMap<TString, ui64> Metrics_;
};

////////////////////////////////////////////////////////////////////////////////

void IncDebugMetricImpl(TStringBuf name)
{
    TDebugMetrics::Get().Inc(name);
}

ui64 GetDebugMetric(TStringBuf name)
{
    return TDebugMetrics::Get().Get(name);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NDetail
} // namespace NYT