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
|
#pragma once
#include "mkql_computation_node.h"
#include <yql/essentials/minikql/mkql_node.h>
#include <library/cpp/threading/future/future.h>
#include <memory>
#include <mutex>
namespace NKikimr::NMiniKQL {
struct TPatternCacheEntry {
TScopedAlloc Alloc;
TTypeEnvironment Env;
bool UseAlloc;
TRuntimeNode ProgramNode;
ui32 ProgramInputsCount;
TRuntimeNode ProgramParams;
TVector<TString> InputItemTypesRaw;
TVector<TType*> InputItemTypes;
TVector<TString> OutputItemTypesRaw;
TVector<TType*> OutputItemTypes;
TVector<TNode*> EntryPoints; // last entry node stands for parameters
TStructType* ParamsStruct;
IComputationPattern::TPtr Pattern;
size_t SizeForCache = 0; // set only by cache to lock the size, which can slightly vary when pattern is used
std::atomic<size_t> AccessTimes = 0; // set only by cache
std::atomic<bool> IsInCache = false; // set only by cache
void UpdateSizeForCache() {
Y_DEBUG_ABORT_UNLESS(!SizeForCache);
SizeForCache = Alloc.GetAllocated();
}
TPatternCacheEntry(bool useAlloc = true)
: Alloc(__LOCATION__)
, Env(Alloc)
, UseAlloc(useAlloc)
{
// Release Alloc since it was implicitly acquired in Alloc's ctor
Alloc.Release();
}
~TPatternCacheEntry() {
if (UseAlloc) {
// If alloc was used it should be acquired so dtors of all member fields will use it to free memory
// Release of Alloc will be called implicitly in Alloc's dtor
Alloc.Acquire();
}
}
};
using TPatternCacheEntryPtr = std::shared_ptr<TPatternCacheEntry>;
using TPatternCacheEntryFuture = NThreading::TFuture<TPatternCacheEntryPtr>;
class TComputationPatternLRUCache {
public:
struct Config {
Config(size_t maxSizeBytes, size_t maxCompiledSizeBytes)
: MaxSizeBytes(maxSizeBytes)
, MaxCompiledSizeBytes(maxCompiledSizeBytes)
{}
Config(size_t maxSizeBytes, size_t maxCompiledSizeBytes, size_t patternAccessTimesBeforeTryToCompile)
: MaxSizeBytes(maxSizeBytes)
, MaxCompiledSizeBytes(maxCompiledSizeBytes)
, PatternAccessTimesBeforeTryToCompile(patternAccessTimesBeforeTryToCompile)
{}
const size_t MaxSizeBytes;
const size_t MaxCompiledSizeBytes;
const std::optional<size_t> PatternAccessTimesBeforeTryToCompile;
bool operator==(const Config & rhs) {
return std::tie(MaxSizeBytes, MaxCompiledSizeBytes, PatternAccessTimesBeforeTryToCompile) ==
std::tie(rhs.MaxSizeBytes, rhs.MaxCompiledSizeBytes, rhs.PatternAccessTimesBeforeTryToCompile);
}
bool operator!=(const Config & rhs) {
return !(*this == rhs);
}
};
TComputationPatternLRUCache(const Config& configuration, NMonitoring::TDynamicCounterPtr counters = MakeIntrusive<NMonitoring::TDynamicCounters>());
~TComputationPatternLRUCache();
static TPatternCacheEntryPtr CreateCacheEntry(bool useAlloc = true) {
return std::make_shared<TPatternCacheEntry>(useAlloc);
}
TPatternCacheEntryPtr Find(const TString& serializedProgram);
TPatternCacheEntryFuture FindOrSubscribe(const TString& serializedProgram);
void EmplacePattern(const TString& serializedProgram, TPatternCacheEntryPtr patternWithEnv);
void NotifyPatternCompiled(const TString& serializedProgram);
void NotifyPatternMissing(const TString& serializedProgram);
size_t GetSize() const;
void CleanCache();
Config GetConfiguration() const {
std::lock_guard lock(Mutex);
return Configuration;
}
size_t GetMaxSizeBytes() const {
std::lock_guard lock(Mutex);
return Configuration.MaxSizeBytes;
}
i64 GetCacheHits() const {
return *Hits;
}
void IncNotSuitablePattern() {
++*NotSuitablePattern;
}
size_t GetPatternsToCompileSize() const {
std::lock_guard lock(Mutex);
return PatternsToCompile.size();
}
void GetPatternsToCompile(THashMap<TString, TPatternCacheEntryPtr> & result) {
std::lock_guard lock(Mutex);
result.swap(PatternsToCompile);
}
private:
class TLRUPatternCacheImpl;
static constexpr size_t CacheMaxElementsSize = 10000;
void AccessPattern(const TString& serializedProgram, TPatternCacheEntryPtr entry);
mutable std::mutex Mutex;
THashMap<TString, TVector<NThreading::TPromise<TPatternCacheEntryPtr>>> Notify; // protected by Mutex
std::unique_ptr<TLRUPatternCacheImpl> Cache; // protected by Mutex
THashMap<TString, TPatternCacheEntryPtr> PatternsToCompile; // protected by Mutex
const Config Configuration;
NMonitoring::TDynamicCounters::TCounterPtr Hits;
NMonitoring::TDynamicCounters::TCounterPtr HitsCompiled;
NMonitoring::TDynamicCounters::TCounterPtr Waits;
NMonitoring::TDynamicCounters::TCounterPtr Misses;
NMonitoring::TDynamicCounters::TCounterPtr NotSuitablePattern;
NMonitoring::TDynamicCounters::TCounterPtr SizeItems;
NMonitoring::TDynamicCounters::TCounterPtr SizeCompiledItems;
NMonitoring::TDynamicCounters::TCounterPtr SizeBytes;
NMonitoring::TDynamicCounters::TCounterPtr SizeCompiledBytes;
NMonitoring::TDynamicCounters::TCounterPtr MaxSizeBytesCounter;
NMonitoring::TDynamicCounters::TCounterPtr MaxCompiledSizeBytesCounter;
};
} // namespace NKikimr::NMiniKQL
|