aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/util/memory_tracker.cpp
blob: 8a12452c71c65311ac515718ccdaf9d3932798de (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
#include "memory_tracker.h"

#include <util/generic/xrange.h>

namespace NActors {
namespace NMemory {

namespace NPrivate {

TMemoryTracker* TMemoryTracker::Instance() {
    return SingletonWithPriority<TMemoryTracker, 0>();
}

void TMemoryTracker::Initialize() {
    GlobalMetrics.resize(Indices.size());
}

const std::map<TString, size_t>& TMemoryTracker::GetMetricIndices() const {
    return Indices;
}

const std::unordered_set<size_t>& TMemoryTracker::GetSensors() const {
    return Sensors;
}

TString TMemoryTracker::GetName(size_t index) const {
    return Names[index];
}

size_t TMemoryTracker::GetCount() const {
    return Indices.size();
}

void TMemoryTracker::GatherMetrics(std::vector<TMetric>& metrics) const {
    metrics.resize(0);
    auto count = GetCount();

    if (!count || GlobalMetrics.size() != count) {
        return;
    }

    TReadGuard guard(LockThreadInfo);

    metrics.resize(count);
    for (size_t i : xrange(count)) {
        metrics[i] += GlobalMetrics[i];
    }

    for (auto info : ThreadInfo) {
        auto& localMetrics = info->GetMetrics();
        if (localMetrics.size() == count) {
            for (size_t i : xrange(count)) {
                metrics[i] += localMetrics[i];
            }
        }
    }
}

size_t TMemoryTracker::RegisterStaticMemoryLabel(const char* name, bool hasSensor) {
    size_t index = 0;
    auto found = Indices.find(name);
    if (found == Indices.end()) {
        TString str(name);
        auto next = Names.size();
        Indices.emplace(str, next);
        Names.push_back(str);
        index = next;
    } else {
        index = found->second;
    }

    if (hasSensor) {
        Sensors.emplace(index);
    }
    return index;
}

void TMemoryTracker::OnCreateThread(TThreadLocalInfo* info) {
    TWriteGuard guard(LockThreadInfo);
    ThreadInfo.insert(info);
}

void TMemoryTracker::OnDestroyThread(TThreadLocalInfo* info) {
    TWriteGuard guard(LockThreadInfo);

    auto count = GetCount();
    if (count && GlobalMetrics.size() == count) {
        const auto& localMetrics = info->GetMetrics();
        if (localMetrics.size() == count) {
            for (size_t i : xrange(count)) {
                GlobalMetrics[i] += localMetrics[i];
            }
        }
    }

    ThreadInfo.erase(info);
}

}

}
}