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

#include <util/stream/output.h>
#include <util/string/builder.h>
#include <util/system/src_location.h>

#include <unordered_map>

#ifndef NDEBUG

namespace NKikimr {
namespace NMiniKQL {

using TMkqlLocation = TSourceLocation;

}
}

#define __MKQL_LOCATION__ __LOCATION__
#   define MKQL_MEM_TAKE3(MemInfo, Mem, Size) \
        ::NKikimr::NMiniKQL::Take(MemInfo, (Mem), (Size), __MKQL_LOCATION__)
#   define MKQL_MEM_TAKE4(MemInfo, Mem, Size, Location) \
        ::NKikimr::NMiniKQL::Take(MemInfo, (Mem), (Size), Location)
#   define MKQL_MEM_RETURN(MemInfo, Mem, Size) \
        ::NKikimr::NMiniKQL::Return(MemInfo, (Mem), (Size))
#   define MKQL_MEM_RETURN_PTR(MemInfo, Mem) \
        ::NKikimr::NMiniKQL::Return(MemInfo, (Mem))
#else

namespace NKikimr {
namespace NMiniKQL {

using TMkqlLocation = int;

}
}

#define __MKQL_LOCATION__ 0
#   define MKQL_MEM_TAKE3(MemInfo, Mem, Size) \
        Y_UNUSED(MemInfo); Y_UNUSED(Mem); Y_UNUSED(Size);
#   define MKQL_MEM_TAKE4(MemInfo, Mem, Size, Location) \
    Y_UNUSED(MemInfo); Y_UNUSED(Mem); Y_UNUSED(Size); Y_UNUSED(Location);
#   define MKQL_MEM_RETURN(MemInfo, Mem, Size) \
        Y_UNUSED(MemInfo); Y_UNUSED(Mem); Y_UNUSED(Size);
#   define MKQL_MEM_RETURN_PTR(MemInfo, Mem) \
        Y_UNUSED(MemInfo); Y_UNUSED(Mem);
#endif

#define GET_MKQL_MEM_TAKE(_1, _2, _3, _4, IMPL, ...) IMPL
#define MKQL_MEM_TAKE(...) Y_PASS_VA_ARGS(GET_MKQL_MEM_TAKE(__VA_ARGS__, MKQL_MEM_TAKE4, MKQL_MEM_TAKE3)(__VA_ARGS__))


namespace NKikimr {
namespace NMiniKQL {

class TMemoryUsageInfo : public TThrRefBase
{
    struct TAllocationInfo {
        ui64 Size;
        TMkqlLocation Location;
        bool IsDeleted;
    };
public:
    explicit TMemoryUsageInfo(const TStringBuf& title);
    ~TMemoryUsageInfo();

    void AllowMissing();
    void CheckOnExit(bool check);

#ifndef NDEBUG
    void Take(const void* mem, ui64 size, TMkqlLocation location);
#endif

#ifndef NDEBUG
    void Return(const void* mem, ui64 size);
#endif

#ifndef NDEBUG
    void Return(const void* mem);
#endif

    i64 GetUsage() const;

    ui64 GetAllocated() const;
    ui64 GetFreed() const;
    ui64 GetPeak() const;

    void PrintTo(IOutputStream& out) const;

    void VerifyDebug() const;

private:
    const TString Title_;
    ui64 Allocated_;
    ui64 Freed_;
    ui64 Peak_;
    bool AllowMissing_;
    bool CheckOnExit_;

#ifndef NDEBUG
    std::unordered_map<const void*, TAllocationInfo> AllocationsMap_;
#endif
};

#ifndef NDEBUG
inline void Take(TMemoryUsageInfo& memInfo, const void* mem, ui64 size, TMkqlLocation location)
{
    memInfo.Take(mem, size, std::move(location));
}

inline void Take(TMemoryUsageInfo* memInfo, const void* mem, ui64 size, TMkqlLocation location)
{
    memInfo->Take(mem, size, std::move(location));
}

inline void Return(TMemoryUsageInfo& memInfo, const void* mem, ui64 size)
{
    memInfo.Return(mem, size);
}

inline void Return(TMemoryUsageInfo* memInfo, const void* mem, ui64 size)
{
    memInfo->Return(mem, size);
}

inline void Return(TMemoryUsageInfo& memInfo, const void* mem)
{
    memInfo.Return(mem);
}

inline void Return(TMemoryUsageInfo* memInfo, const void* mem)
{
    memInfo->Return(mem);
}
#endif

} // namespace NMiniKQL
} // namespace NKikimr

template <>
inline void Out<NKikimr::NMiniKQL::TMemoryUsageInfo>(
        IOutputStream& out,
        const NKikimr::NMiniKQL::TMemoryUsageInfo& memInfo)
{
    memInfo.PrintTo(out);
}