This repository was archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcache.cpp
More file actions
319 lines (309 loc) · 11.1 KB
/
cache.cpp
File metadata and controls
319 lines (309 loc) · 11.1 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "server/cache.hpp"
#include <capnp/message.h>
#include <capnp/serialize.h>
#include <kj/debug.h>
#include <algorithm>
#include <fstream>
#include "capnp/cache.capnp.h"
#include "util/file.hpp"
#include "util/flags.hpp"
namespace server {
namespace detail {
template <class T>
inline void hash_combine(std::size_t* hash, const T& v) {
std::hash<T> hasher;
*hash ^= hasher(v) + 0x9e3779b9 + (*hash << 6) + (*hash >> 2);
}
uint64_t RequestHasher::operator()(capnproto::Request::Reader reader_) const {
size_t hash = reader_.getProcesses().size();
for (auto reader : reader_.getProcesses()) {
hash_combine(&hash, static_cast<int>(reader.getExecutable().which()));
switch (reader.getExecutable().which()) {
case capnproto::ProcessRequest::Executable::SYSTEM:
hash_combine(&hash, std::string(reader.getExecutable().getSystem()));
break;
case capnproto::ProcessRequest::Executable::LOCAL_FILE:
hash_combine(
&hash,
std::string(reader.getExecutable().getLocalFile().getName()));
hash_combine(&hash, util::SHA256_t(
reader.getExecutable().getLocalFile().getHash())
.Hex());
break;
}
for (std::string arg : reader.getArgs()) {
hash_combine(&hash, arg);
}
hash_combine(&hash, static_cast<int>(reader.getStdin().which()));
switch (reader.getStdin().which()) {
case capnproto::ProcessRequest::Stdin::FIFO:
hash_combine(&hash, reader.getStdin().getFifo());
break;
case capnproto::ProcessRequest::Stdin::HASH:
hash_combine(&hash, util::SHA256_t(reader.getStdin().getHash()).Hex());
break;
}
hash_combine(&hash, reader.getStdout());
hash_combine(&hash, reader.getStderr());
// TODO: be consistent if the files are permuted.
for (auto in : reader.getInputFiles()) {
hash_combine(&hash, std::string(in.getName()));
hash_combine(&hash, util::SHA256_t(in.getHash()).Hex());
hash_combine(&hash, in.getExecutable());
}
for (std::string out : reader.getOutputFiles()) {
hash_combine(&hash, out);
}
for (auto in : reader.getFifos()) {
hash_combine(&hash, std::string(in.getName()));
hash_combine(&hash, in.getId());
}
hash_combine(&hash, reader.getLimits().getCpuTime());
hash_combine(&hash, reader.getLimits().getWallTime());
hash_combine(&hash, reader.getLimits().getMemory());
hash_combine(&hash, reader.getLimits().getNproc());
hash_combine(&hash, reader.getLimits().getNofiles());
hash_combine(&hash, reader.getLimits().getFsize());
hash_combine(&hash, reader.getLimits().getMemlock());
hash_combine(&hash, reader.getLimits().getStack());
hash_combine(&hash, reader.getExtraTime());
}
hash_combine(&hash, reader_.getExclusive());
return hash;
}
bool RequestComparator::operator()(capnproto::Request::Reader a_,
capnproto::Request::Reader b_) const {
if (a_.getProcesses().size() != b_.getProcesses().size()) return false;
if (a_.getExclusive() != b_.getExclusive()) return false;
for (size_t i = 0; i < a_.getProcesses().size(); i++) {
auto a = a_.getProcesses()[i];
auto b = b_.getProcesses()[i];
if (a.getExecutable().which() != b.getExecutable().which()) return false;
switch (a.getExecutable().which()) {
case capnproto::ProcessRequest::Executable::SYSTEM:
if (a.getExecutable().getSystem() != b.getExecutable().getSystem()) {
return false;
}
break;
case capnproto::ProcessRequest::Executable::LOCAL_FILE:
if (a.getExecutable().getLocalFile().getName() !=
b.getExecutable().getLocalFile().getName()) {
return false;
}
if (util::SHA256_t(a.getExecutable().getLocalFile().getHash()).Hex() !=
util::SHA256_t(b.getExecutable().getLocalFile().getHash()).Hex()) {
return false;
}
break;
}
auto aargs = a.getArgs();
auto bargs = b.getArgs();
for (size_t i = 0; i < aargs.size(); i++) {
if (aargs[i] != bargs[i]) return false;
}
if (a.getStdin().which() != b.getStdin().which()) {
return false;
}
switch (a.getStdin().which()) {
case capnproto::ProcessRequest::Stdin::FIFO:
if (a.getStdin().getFifo() != b.getStdin().getFifo()) {
return false;
}
break;
case capnproto::ProcessRequest::Stdin::HASH:
if (util::SHA256_t(a.getStdin().getHash()).Hex() !=
util::SHA256_t(b.getStdin().getHash()).Hex()) {
return false;
}
break;
}
if (a.getStdout() != b.getStdout()) return false;
if (a.getStderr() != b.getStderr()) return false;
std::vector<std::tuple<std::string, std::string, bool>> ainput;
std::vector<std::tuple<std::string, std::string, bool>> binput;
for (auto in : a.getInputFiles()) {
ainput.emplace_back(in.getName(), util::SHA256_t(in.getHash()).Hex(),
in.getExecutable());
}
for (auto in : b.getInputFiles()) {
binput.emplace_back(in.getName(), util::SHA256_t(in.getHash()).Hex(),
in.getExecutable());
}
std::sort(ainput.begin(), ainput.end());
std::sort(binput.begin(), binput.end());
if (ainput != binput) return false;
std::vector<std::tuple<std::string, uint32_t>> afifo;
std::vector<std::tuple<std::string, uint32_t>> bfifo;
for (auto f : a.getFifos()) {
afifo.emplace_back(f.getName(), f.getId());
}
for (auto f : b.getFifos()) {
bfifo.emplace_back(f.getName(), f.getId());
}
std::sort(afifo.begin(), afifo.end());
std::sort(bfifo.begin(), bfifo.end());
if (afifo != bfifo) return false;
std::vector<std::string> aoutput;
std::vector<std::string> boutput;
for (auto out : a.getOutputFiles()) {
aoutput.emplace_back(out);
}
for (auto out : b.getOutputFiles()) {
boutput.emplace_back(out);
}
std::sort(aoutput.begin(), aoutput.end());
std::sort(boutput.begin(), boutput.end());
if (aoutput != boutput) return false;
if (a.getLimits().getCpuTime() != b.getLimits().getCpuTime()) {
return false;
}
if (a.getLimits().getWallTime() != b.getLimits().getWallTime()) {
return false;
}
if (a.getLimits().getMemory() != b.getLimits().getMemory()) {
return false;
}
if (a.getLimits().getNproc() != b.getLimits().getNproc()) {
return false;
}
if (a.getLimits().getNofiles() != b.getLimits().getNofiles()) {
return false;
}
if (a.getLimits().getFsize() != b.getLimits().getFsize()) {
return false;
}
if (a.getLimits().getMemory() != b.getLimits().getMemory()) {
return false;
}
if (a.getLimits().getStack() != b.getLimits().getStack()) {
return false;
}
if (a.getExtraTime() != b.getExtraTime()) {
return false;
}
}
return true;
}
std::vector<util::SHA256_t> Hashes(capnproto::Request::Reader req_,
capnproto::Result::Reader res_) {
std::vector<util::SHA256_t> ans;
auto add = [&ans](util::SHA256_t hash) {
if (!hash.isZero()) ans.push_back(hash);
};
for (auto req : req_.getProcesses()) {
if (req.getStdin().isHash()) {
add(req.getStdin().getHash());
}
if (req.getExecutable().isLocalFile()) {
add(req.getExecutable().getLocalFile().getHash());
}
for (auto f : req.getInputFiles()) add(f.getHash());
}
for (auto res : res_.getProcesses()) {
add(res.getStdout());
add(res.getStderr());
for (auto f : res.getOutputFiles()) add(f.getHash());
}
return ans;
};
} // namespace detail
CacheManager::CacheManager() {
std::ifstream fin(Path());
if (fin) {
kj::std::StdInputStream is(fin);
while (true) {
try {
builders_.emplace_back(kj::heap<capnp::MallocMessageBuilder>());
capnp::readMessageCopy(is, *builders_.back());
auto entry = builders_.back()->getRoot<capnproto::CacheEntry>();
bool missing_files = false;
for (const auto& hash :
detail::Hashes(entry.getRequest(), entry.getResult())) {
int64_t fsz;
if ((fsz = util::File::Size(util::File::PathForHash(hash))) < 0) {
missing_files = true;
break;
}
}
if (missing_files) continue;
for (const auto& hash :
detail::Hashes(entry.getRequest(), entry.getResult())) {
size_t fsz = util::File::Size(util::File::PathForHash(hash));
if (!file_sizes_.count(hash)) {
file_sizes_.emplace(hash, fsz);
total_size_ += fsz;
}
file_access_times_[hash] = last_access_time_++;
}
for (const auto& kv : file_access_times_) {
sorted_files_.emplace(kv.second, kv.first);
}
data_.emplace(entry.getRequest(), entry.getResult());
} catch (kj::Exception& exc) {
break;
}
}
fin.close();
}
util::File::MakeDirs(Flags::store_directory);
fout_.open(Path(), std::ios_base::out | std::ios_base::app);
}
bool CacheManager::Has(capnproto::Request::Reader req) {
if (!data_.count(req)) return false;
for (const auto& hash : detail::Hashes(req, data_.at(req))) {
if (!file_sizes_.count(hash)) return false;
}
return true;
}
capnproto::Result::Reader CacheManager::Get(capnproto::Request::Reader req) {
for (const auto& hash : detail::Hashes(req, data_.at(req))) {
sorted_files_.erase(file_access_times_.at(hash));
file_access_times_[hash] = last_access_time_++;
sorted_files_.emplace(file_access_times_[hash], hash);
}
return data_.at(req);
}
void CacheManager::Set(capnproto::Request::Reader req,
capnproto::Result::Reader res) {
if (Has(req)) return;
for (const auto& hash : detail::Hashes(req, res)) {
if (!file_sizes_.count(hash)) {
size_t sz = util::File::Size(util::File::PathForHash(hash));
total_size_ += sz;
file_sizes_.emplace(hash, sz);
} else {
sorted_files_.erase(file_access_times_.at(hash));
}
file_access_times_[hash] = last_access_time_++;
sorted_files_.emplace(file_access_times_[hash], hash);
}
while (Flags::cache_size != 0 &&
total_size_ > 1024ULL * 1024 * Flags::cache_size) {
KJ_ASSERT(!sorted_files_.empty());
auto to_del = sorted_files_.begin()->second;
try {
util::File::Remove(util::File::PathForHash(to_del));
} catch (...) {
// If we could not remove the file, skip shrinking the cache.
break;
}
sorted_files_.erase(sorted_files_.begin());
total_size_ -= file_sizes_.at(to_del);
file_sizes_.erase(to_del);
file_access_times_.erase(to_del);
}
for (const auto& hash : detail::Hashes(req, res)) {
KJ_ASSERT(file_sizes_.count(hash), "Cache size is too small!");
}
builders_.emplace_back(kj::heap<capnp::MallocMessageBuilder>());
auto entry = builders_.back()->getRoot<capnproto::CacheEntry>();
entry.setRequest(req);
entry.setResult(res);
data_.emplace(entry.getRequest(), entry.getResult());
capnp::writeMessage(os_, *builders_.back());
fout_.flush();
}
std::string CacheManager::Path() {
return util::File::JoinPath(Flags::store_directory, "cache");
}
} // namespace server