aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/library/yql/minikql/aligned_page_pool.h
blob: 92e35c8ef2b7552068c0ae202d34cb6b544ea985 (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
#pragma once

#include <util/generic/yexception.h>
#include <util/stream/output.h>
#include <util/system/yassert.h>
#include <util/system/defaults.h>

#include <library/cpp/monlib/dynamic_counters/counters.h>

#include <type_traits>
#include <stack>
#include <vector>
#include <unordered_set>
#include <unordered_map>

namespace NKikimr {

struct TAlignedPagePoolCounters {
    explicit TAlignedPagePoolCounters(NMonitoring::TDynamicCounterPtr countersRoot = nullptr, const TString& name = TString());

    NMonitoring::TDynamicCounters::TCounterPtr TotalBytesAllocatedCntr;
    NMonitoring::TDynamicCounters::TCounterPtr AllocationsCntr;
    NMonitoring::TDynamicCounters::TCounterPtr PoolsCntr;
    NMonitoring::TDynamicCounters::TCounterPtr LostPagesBytesFreeCntr;

    void Swap(TAlignedPagePoolCounters& other) {
        DoSwap(TotalBytesAllocatedCntr, other.TotalBytesAllocatedCntr);
        DoSwap(AllocationsCntr, other.AllocationsCntr);
        DoSwap(PoolsCntr, other.PoolsCntr);
        DoSwap(LostPagesBytesFreeCntr, other.LostPagesBytesFreeCntr);
    }
};

// NOTE: We intentionally avoid inheritance from std::exception here to make it harder
// to catch this exception in UDFs code, so we can handle it in the host.
class TMemoryLimitExceededException {};

class TAlignedPagePool {
public:
    static constexpr ui64 POOL_PAGE_SIZE = 1ULL << 16; // 64k 
    static constexpr ui64 PAGE_ADDR_MASK = ~(POOL_PAGE_SIZE - 1); 

    explicit TAlignedPagePool(const TAlignedPagePoolCounters& counters = TAlignedPagePoolCounters())
        : Counters(counters)
    {
        if (Counters.PoolsCntr) {
            ++(*Counters.PoolsCntr);
        }
    }

    TAlignedPagePool(const TAlignedPagePool&) = delete;
    TAlignedPagePool(TAlignedPagePool&& other) = delete;

    TAlignedPagePool& operator = (const TAlignedPagePool&) = delete;
    TAlignedPagePool& operator = (TAlignedPagePool&& other) = delete;

    ~TAlignedPagePool();

    inline size_t GetAllocated() const noexcept {
        return TotalAllocated;
    }

    inline size_t GetUsed() const noexcept {
        return TotalAllocated - GetFreePageCount() * POOL_PAGE_SIZE; 
    }

    inline size_t GetFreePageCount() const noexcept {
        return FreePages.size();
    }

    static inline const void* GetPageStart(const void* addr) noexcept {
        return reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(addr) & PAGE_ADDR_MASK);
    }

    static inline void* GetPageStart(void* addr) noexcept {
        return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) & PAGE_ADDR_MASK);
    }

    void* GetPage();

    void ReturnPage(void* addr) noexcept;

    void Swap(TAlignedPagePool& other) {
        DoSwap(FreePages, other.FreePages);
        DoSwap(AllPages, other.AllPages);
        DoSwap(ActiveBlocks, other.ActiveBlocks);
        DoSwap(TotalAllocated, other.TotalAllocated);
        DoSwap(PeakAllocated, other.PeakAllocated);
        DoSwap(PeakUsed, other.PeakUsed);
        DoSwap(Limit, other.Limit);
        DoSwap(AllocCount, other.AllocCount);
        DoSwap(PageAllocCount, other.PageAllocCount);
        DoSwap(PageHitCount, other.PageHitCount);
        DoSwap(PageGlobalHitCount, other.PageGlobalHitCount);
        DoSwap(PageMissCount, other.PageMissCount);
        DoSwap(OffloadedAllocCount, other.OffloadedAllocCount);
        DoSwap(OffloadedBytes, other.OffloadedBytes);
        DoSwap(OffloadedActiveBytes, other.OffloadedActiveBytes);
        DoSwap(Counters, other.Counters);
        DoSwap(CheckLostMem, other.CheckLostMem);
        DoSwap(AllocNotifyCallback, other.AllocNotifyCallback);
        DoSwap(IncreaseMemoryLimitCallback, other.IncreaseMemoryLimitCallback);
    }

    void PrintStat(size_t usedPages, IOutputStream& out) const; 

    void* GetBlock(size_t size);

    void ReturnBlock(void* ptr, size_t size) noexcept;

    size_t GetPeakAllocated() const noexcept {
        return PeakAllocated;
    }

    size_t GetPeakUsed() const noexcept {
        return PeakUsed;
    }

    ui64 GetAllocCount() const noexcept {
        return AllocCount;
    }

    ui64 GetPageAllocCount() const noexcept {
        return PageAllocCount;
    }

    ui64 GetPageHitCount() const noexcept {
        return PageHitCount;
    }

    ui64 GetPageGlobalHitCount() const noexcept {
        return PageGlobalHitCount;
    }

    ui64 GetPageMissCount() const noexcept {
        return PageMissCount;
    }

    ui64 GetOffloadedAllocCount() const noexcept {
        return OffloadedAllocCount;
    }

    ui64 GetOffloadedBytes() const noexcept {
        return OffloadedBytes;
    }

    void OffloadAlloc(ui64 size);
    void OffloadFree(ui64 size) noexcept;

    static ui64 GetGlobalPagePoolSize();

    ui64 GetLimit() const noexcept {
        return Limit;
    }

    void SetLimit(size_t limit) noexcept {
        Limit = limit;
    }

    void ReleaseFreePages();

    void DisableStrictAllocationCheck() noexcept {
        CheckLostMem = false;
    }

    using TAllocNotifyCallback = std::function<void()>;
    void SetAllocNotifyCallback(TAllocNotifyCallback&& callback, ui64 notifyBytes = 0) {
        AllocNotifyCallback = std::move(callback);
        AllocNotifyBytes = notifyBytes;
        AllocNotifyCurrentBytes = 0;
    }

    using TIncreaseMemoryLimitCallback = std::function<void(ui64 currentLimit, ui64 required)>;

    void SetIncreaseMemoryLimitCallback(TIncreaseMemoryLimitCallback&& callback) {
        IncreaseMemoryLimitCallback = std::move(callback);
    }

protected:
    void* Alloc(size_t size);
    void Free(void* ptr, size_t size) noexcept;

    void UpdatePeaks() {
        PeakAllocated = Max(PeakAllocated, GetAllocated());
        PeakUsed = Max(PeakUsed, GetUsed());
    }

    bool TryIncreaseLimit(ui64 required);

protected:
    std::stack<void*, std::vector<void*>> FreePages;
    std::unordered_set<void*> AllPages;
    std::unordered_map<void*, size_t> ActiveBlocks;
    size_t TotalAllocated = 0;
    size_t PeakAllocated = 0;
    size_t PeakUsed = 0;
    size_t Limit = 0;

    ui64 AllocCount = 0;
    ui64 PageAllocCount = 0;
    ui64 PageHitCount = 0;
    ui64 PageGlobalHitCount = 0;
    ui64 PageMissCount = 0;

    ui64 OffloadedAllocCount = 0;
    ui64 OffloadedBytes = 0;
    ui64 OffloadedActiveBytes = 0;

    TAlignedPagePoolCounters Counters;
    bool CheckLostMem = true;

    TAllocNotifyCallback AllocNotifyCallback;
    ui64 AllocNotifyBytes = 0;
    ui64 AllocNotifyCurrentBytes = 0;

    TIncreaseMemoryLimitCallback IncreaseMemoryLimitCallback;
};

} // NKikimr