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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include <zencore/compactbinary.h>
#include <zencore/iobuffer.h>
#include <zencore/iohash.h>
#include <zencore/thread.h>
#include <zencore/uid.h>
#include <zenstore/basicfile.h>
#include <zenstore/cas.h>
#include <zenstore/caslog.h>
ZEN_THIRD_PARTY_INCLUDES_START
#include <tsl/robin_map.h>
ZEN_THIRD_PARTY_INCLUDES_END
#include <compare>
#include <filesystem>
#include <unordered_map>
namespace zen {
class WideStringBuilderBase;
class CasStore;
/******************************************************************************
/$$$$$$$$ /$$$$$$ /$$
|_____ $$ /$$__ $$ | $$
/$$/ /$$$$$$ /$$$$$$$ | $$ \__/ /$$$$$$ /$$$$$$| $$$$$$$ /$$$$$$
/$$/ /$$__ $| $$__ $$ | $$ |____ $$/$$_____| $$__ $$/$$__ $$
/$$/ | $$$$$$$| $$ \ $$ | $$ /$$$$$$| $$ | $$ \ $| $$$$$$$$
/$$/ | $$_____| $$ | $$ | $$ $$/$$__ $| $$ | $$ | $| $$_____/
/$$$$$$$| $$$$$$| $$ | $$ | $$$$$$| $$$$$$| $$$$$$| $$ | $| $$$$$$$
|________/\_______|__/ |__/ \______/ \_______/\_______|__/ |__/\_______/
Cache store for UE5. Restricts keys to "{bucket}/{hash}" pairs where the hash
is 40 (hex) chars in size. Values may be opaque blobs or structured objects
which can in turn contain references to other objects (or blobs).
******************************************************************************/
struct ZenCacheValue
{
IoBuffer Value;
CbObject IndexData;
};
/** In-memory cache storage
Intended for small values which are frequently accessed
*/
class ZenCacheMemoryLayer
{
public:
ZenCacheMemoryLayer();
~ZenCacheMemoryLayer();
bool Get(std::string_view Bucket, const IoHash& HashKey, ZenCacheValue& OutValue);
void Put(std::string_view Bucket, const IoHash& HashKey, const ZenCacheValue& Value);
bool DropBucket(std::string_view Bucket);
void Scrub(ScrubContext& Ctx);
void GarbageCollect(GcContext& GcCtx);
struct Configuration
{
uint64_t TargetFootprintBytes = 16 * 1024 * 1024;
uint64_t ScavengeThreshold = 4 * 1024 * 1024;
};
const Configuration& GetConfiguration() const { return m_Configuration; }
void SetConfiguration(const Configuration& NewConfig) { m_Configuration = NewConfig; }
private:
struct CacheBucket
{
struct BucketValue
{
uint64_t LastAccess = 0;
IoBuffer Payload;
};
RwLock m_bucketLock;
tsl::robin_map<IoHash, BucketValue> m_cacheMap;
bool Get(const IoHash& HashKey, ZenCacheValue& OutValue);
void Put(const IoHash& HashKey, const ZenCacheValue& Value);
void Scrub(ScrubContext& Ctx);
void GarbageCollect(GcContext& GcCtx);
private:
uint64_t GetCurrentTimeStamp();
};
RwLock m_Lock;
std::unordered_map<std::string, CacheBucket> m_Buckets;
Configuration m_Configuration;
};
#pragma pack(push)
#pragma pack(1)
struct DiskLocation
{
static const uint64_t kOffsetMask = 0x0000'ffFF'ffFF'ffFFull;
static const uint64_t kSizeMask = 0x00FF'0000'0000'0000ull;
static const uint64_t kFlagsMask = 0xff00'0000'0000'0000ull;
static const uint64_t kStandaloneFile = 0x8000'0000'0000'0000ull;
static const uint64_t kStructured = 0x4000'0000'0000'0000ull;
static const uint64_t kTombStone = 0x2000'0000'0000'0000ull;
DiskLocation();
DiskLocation(uint64_t Offset, uint64_t ValueSize, uint32_t IndexSize, uint64_t Flags);
static uint64_t CombineOffsetAndFlags(uint64_t Offset, uint64_t Flags);
uint64_t Offset() const;
uint64_t Size() const;
uint64_t IsFlagSet(uint64_t Flag) const;
ZenContentType GetContentType() const;
private:
uint64_t OffsetAndFlags = 0;
uint32_t LowerSize = 0;
uint32_t IndexDataSize = 0;
};
struct DiskIndexEntry
{
IoHash Key;
DiskLocation Location;
};
#pragma pack(pop)
static_assert(sizeof(DiskIndexEntry) == 36);
class ZenCacheDiskLayer
{
public:
explicit ZenCacheDiskLayer(const std::filesystem::path& RootDir);
~ZenCacheDiskLayer();
bool Get(std::string_view Bucket, const IoHash& HashKey, ZenCacheValue& OutValue);
void Put(std::string_view Bucket, const IoHash& HashKey, const ZenCacheValue& Value);
bool DropBucket(std::string_view Bucket);
void Flush();
void Scrub(ScrubContext& Ctx);
void GarbageCollect(GcContext& GcCtx);
void DiscoverBuckets();
private:
/** A cache bucket manages a single directory containing
metadata and data for that bucket
*/
struct CacheBucket
{
CacheBucket();
~CacheBucket();
void OpenOrCreate(std::filesystem::path BucketDir, bool AllowCreate = true);
static bool Delete(std::filesystem::path BucketDir);
bool Get(const IoHash& HashKey, ZenCacheValue& OutValue);
void Put(const IoHash& HashKey, const ZenCacheValue& Value);
void Drop();
void Flush();
void Scrub(ScrubContext& Ctx);
void GarbageCollect(GcContext& GcCtx);
inline bool IsOk() const { return m_IsOk; }
private:
std::filesystem::path m_BucketDir;
Oid m_BucketId;
bool m_IsOk = false;
uint64_t m_LargeObjectThreshold = 64 * 1024;
// These files are used to manage storage of small objects for this bucket
BasicFile m_SobsFile;
TCasLogFile<DiskIndexEntry> m_SlogFile;
RwLock m_IndexLock;
tsl::robin_map<IoHash, DiskLocation, IoHash::Hasher> m_Index;
uint64_t m_WriteCursor = 0;
void BuildPath(WideStringBuilderBase& Path, const IoHash& HashKey);
void PutStandaloneCacheValue(const IoHash& HashKey, const ZenCacheValue& Value);
bool GetStandaloneCacheValue(const DiskLocation& Loc, const IoHash& HashKey, ZenCacheValue& OutValue);
bool GetInlineCacheValue(const DiskLocation& Loc, ZenCacheValue& OutValue);
// These locks are here to avoid contention on file creation, therefore it's sufficient
// that we take the same lock for the same hash
//
// These locks are small and should really be spaced out so they don't share cache lines,
// but we don't currently access them at particularly high frequency so it should not be
// an issue in practice
RwLock m_ShardedLocks[256];
inline RwLock& LockForHash(const IoHash& Hash) { return m_ShardedLocks[Hash.Hash[19]]; }
};
std::filesystem::path m_RootDir;
RwLock m_Lock;
std::unordered_map<std::string, CacheBucket> m_Buckets; // TODO: make this case insensitive
};
class ZenCacheStore
{
public:
explicit ZenCacheStore(const std::filesystem::path& RootDir);
~ZenCacheStore();
bool Get(std::string_view Bucket, const IoHash& HashKey, ZenCacheValue& OutValue);
void Put(std::string_view Bucket, const IoHash& HashKey, const ZenCacheValue& Value);
bool DropBucket(std::string_view Bucket);
void Flush();
void Scrub(ScrubContext& Ctx);
void GarbageCollect(GcContext& GcCtx);
private:
std::filesystem::path m_RootDir;
ZenCacheMemoryLayer m_MemLayer;
ZenCacheDiskLayer m_DiskLayer;
uint64_t m_DiskLayerSizeThreshold = 4 * 1024;
uint64_t m_LastScrubTime = 0;
};
void z$_forcelink();
} // namespace zen
|