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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "cachetracking.h"
#include <zencore/filesystem.h>
#include <zencore/logging.h>
#include <zencore/string.h>
#include <zencore/testing.h>
#include <zencore/testutils.h>
ZEN_THIRD_PARTY_INCLUDES_START
#pragma comment(lib, "Rpcrt4.lib") // RocksDB made me do this
#include <fmt/format.h>
#include <rocksdb/db.h>
#include <tsl/robin_map.h>
#include <tsl/robin_set.h>
#include <gsl/gsl-lite.hpp>
ZEN_THIRD_PARTY_INCLUDES_END
namespace zen {
using namespace fmt::literals;
namespace rocksdb = ROCKSDB_NAMESPACE;
static constinit auto Epoch = std::chrono::time_point<std::chrono::system_clock>{};
uint32_t
GetCurrentTimeStamp()
{
auto Duration = std::chrono::system_clock::now() - Epoch;
auto Minutes = std::chrono::duration_cast<std::chrono::minutes>(Duration).count();
return Minutes;
}
struct CacheAccessSnapshot
{
public:
void TrackAccess(std::string_view BucketSegment, const IoHash& HashKey)
{
BucketTracker* Tracker = MapBucketToIndex(std::string(BucketSegment));
Tracker->Track(HashKey);
}
private:
struct BucketTracker
{
RwLock Lock;
tsl::robin_set<IoHash> AccessedKeys;
void Track(const IoHash& HashKey)
{
RwLock::ExclusiveLockScope _(Lock);
AccessedKeys.insert(HashKey);
}
};
BucketTracker* MapBucketToIndex(const std::string& BucketName)
{
RwLock::SharedLockScope _(m_Lock);
if (auto It = m_BucketMapping.find(BucketName); It == m_BucketMapping.end())
{
_.ReleaseNow();
return AddNewBucket(BucketName);
}
else
{
return m_Buckets[It->second].get();
}
}
BucketTracker* AddNewBucket(const std::string& BucketName)
{
RwLock::ExclusiveLockScope _(m_Lock);
if (auto It = m_BucketMapping.find(BucketName); It == m_BucketMapping.end())
{
const uint32_t BucketIndex = gsl::narrow<uint32_t>(m_Buckets.size());
m_Buckets.emplace_back(std::make_unique<BucketTracker>());
m_BucketMapping[BucketName] = BucketIndex;
return m_Buckets[BucketIndex].get();
}
else
{
return m_Buckets[It->second].get();
}
}
RwLock m_Lock;
std::vector<std::unique_ptr<BucketTracker>> m_Buckets;
tsl::robin_map<std::string, uint32_t> m_BucketMapping;
};
struct ZenCacheTracker::Impl
{
Impl(std::filesystem::path StateDirectory)
{
std::filesystem::path StatsDbPath{StateDirectory / ".zdb"};
std::string RocksdbPath = ToUtf8(StatsDbPath);
ZEN_DEBUG("opening tracker db at '{}'", RocksdbPath);
rocksdb::DB* Db = nullptr;
rocksdb::DBOptions Options;
Options.create_if_missing = true;
std::vector<std::string> ExistingColumnFamilies;
rocksdb::Status Status = rocksdb::DB::ListColumnFamilies(Options, RocksdbPath, &ExistingColumnFamilies);
std::vector<rocksdb::ColumnFamilyDescriptor> ColumnDescriptors;
if (Status.IsPathNotFound())
{
ColumnDescriptors.emplace_back(rocksdb::ColumnFamilyDescriptor{rocksdb::kDefaultColumnFamilyName, {}});
}
else if (Status.ok())
{
for (const std::string& Column : ExistingColumnFamilies)
{
rocksdb::ColumnFamilyDescriptor ColumnFamily;
ColumnFamily.name = Column;
ColumnDescriptors.push_back(ColumnFamily);
}
}
else
{
throw std::runtime_error("column family iteration failed for '{}': '{}'"_format(RocksdbPath, Status.getState()).c_str());
}
Status = rocksdb::DB::Open(Options, RocksdbPath, ColumnDescriptors, &m_RocksDbColumnHandles, &Db);
if (!Status.ok())
{
throw std::runtime_error("database open failed for '{}': '{}'"_format(RocksdbPath, Status.getState()).c_str());
}
m_RocksDb.reset(Db);
}
~Impl()
{
for (auto* Column : m_RocksDbColumnHandles)
{
delete Column;
}
m_RocksDbColumnHandles.clear();
}
struct KeyStruct
{
uint16_t BucketId;
IoHash HashKey;
};
struct ValueStruct
{
uint32_t CreateTime = 0;
uint32_t AccessTime = 0;
uint32_t AccessCount = 0;
uint32_t LastGcCount = 0;
};
void TrackAccess(std::string_view BucketSegment, const IoHash& HashKey)
{
const uint32_t Ts = GetCurrentTimeStamp();
rocksdb::WriteOptions Wo;
rocksdb::ReadOptions Ro;
ZEN_UNUSED(BucketSegment);
ValueStruct Value;
rocksdb::Slice ValueSlice{(char*)&Value, sizeof Value};
KeyStruct Key;
Key.BucketId = 0;
Key.HashKey = HashKey;
rocksdb::Slice KeySlice{(char*)&Key, sizeof Key};
std::string ValueString;
rocksdb::Status Status = m_RocksDb->Get(Ro, KeySlice, &ValueString);
if (Status.ok() && ValueString.size() == sizeof(ValueStruct))
{
memcpy(&Value, ValueString.data(), ValueString.size());
++Value.AccessCount;
}
else
{
Value.CreateTime = Ts;
Value.AccessCount = 1;
}
Value.AccessTime = Ts;
m_RocksDb->Put(Wo, KeySlice, ValueSlice);
}
std::unique_ptr<rocksdb::DB> m_RocksDb;
std::vector<rocksdb::ColumnFamilyHandle*> m_RocksDbColumnHandles;
CacheAccessSnapshot m_CurrentSnapshot;
};
ZenCacheTracker::ZenCacheTracker(std::filesystem::path StateDirectory) : m_Impl(new Impl(StateDirectory))
{
}
ZenCacheTracker::~ZenCacheTracker()
{
delete m_Impl;
}
void
ZenCacheTracker::TrackAccess(std::string_view BucketSegment, const IoHash& HashKey)
{
m_Impl->TrackAccess(BucketSegment, HashKey);
}
#if ZEN_WITH_TESTS
TEST_CASE("z$.tracker")
{
using namespace fmt::literals;
using namespace std::literals;
ScopedTemporaryDirectory TempDir;
ZenCacheTracker Zcs(TempDir.Path());
for (int i = 0; i < 10000; ++i)
{
IoHash KeyHash = IoHash::HashBuffer(&i, sizeof i);
Zcs.TrackAccess("foo", KeyHash);
}
for (int i = 0; i < 10000; ++i)
{
IoHash KeyHash = IoHash::HashBuffer(&i, sizeof i);
Zcs.TrackAccess("foo", KeyHash);
}
}
#endif
void
cachetracker_forcelink()
{
}
} // namespace zen
|