-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathcompiler_cache_linux.cpp
285 lines (226 loc) · 9.68 KB
/
compiler_cache_linux.cpp
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
/*
* Copyright (C) 2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/compiler_interface/compiler_cache.h"
#include "shared/source/compiler_interface/os_compiler_cache_helper.h"
#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/helpers/file_io.h"
#include "shared/source/helpers/path.h"
#include "shared/source/helpers/string.h"
#include "shared/source/os_interface/linux/sys_calls.h"
#include "shared/source/os_interface/os_handle.h"
#include "shared/source/utilities/io_functions.h"
#include "os_inc.h"
#include <algorithm>
#include <dirent.h>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include <string_view>
#include <sys/file.h>
#include <sys/types.h>
#include <unistd.h>
#include <vector>
namespace NEO {
int filterFunction(const struct dirent *file) {
std::string_view fileName = file->d_name;
if (fileName.find(".cl_cache") != fileName.npos ||
fileName.find(".l0_cache") != fileName.npos) {
return 1;
}
return 0;
}
struct ElementsStruct {
std::string path;
struct stat statEl;
};
bool compareByLastAccessTime(const ElementsStruct &a, ElementsStruct &b) {
return a.statEl.st_atime < b.statEl.st_atime;
}
bool CompilerCache::evictCache(uint64_t &bytesEvicted) {
struct dirent **files = 0;
const int filesCount = NEO::SysCalls::scandir(config.cacheDir.c_str(), &files, filterFunction, NULL);
if (filesCount == -1) {
NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "PID %d [Cache failure]: Scandir failed! errno: %d\n", NEO::SysCalls::getProcessId(), errno);
return false;
}
std::vector<ElementsStruct> cacheFiles;
cacheFiles.reserve(static_cast<size_t>(filesCount));
for (int i = 0; i < filesCount; ++i) {
ElementsStruct fileElement = {};
fileElement.path = joinPath(config.cacheDir, files[i]->d_name);
if (NEO::SysCalls::stat(fileElement.path, &fileElement.statEl) == 0) {
cacheFiles.push_back(std::move(fileElement));
}
free(files[i]);
}
free(files);
std::sort(cacheFiles.begin(), cacheFiles.end(), compareByLastAccessTime);
bytesEvicted = 0;
const auto evictionLimit = config.cacheSize / 3;
for (const auto &file : cacheFiles) {
auto res = NEO::SysCalls::unlink(file.path);
if (res == -1) {
continue;
}
bytesEvicted += file.statEl.st_size;
if (bytesEvicted > evictionLimit) {
return true;
}
}
return true;
}
bool CompilerCache::createUniqueTempFileAndWriteData(char *tmpFilePathTemplate, const char *pBinary, size_t binarySize) {
int fd = NEO::SysCalls::mkstemp(tmpFilePathTemplate);
if (fd == -1) {
NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "PID %d [Cache failure]: Creating temporary file failed! errno: %d\n", NEO::SysCalls::getProcessId(), errno);
return false;
}
if (NEO::SysCalls::pwrite(fd, pBinary, binarySize, 0) == -1) {
NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "PID %d [Cache failure]: Writing to temporary file failed! errno: %d\n", NEO::SysCalls::getProcessId(), errno);
NEO::SysCalls::close(fd);
NEO::SysCalls::unlink(tmpFilePathTemplate);
return false;
}
return NEO::SysCalls::close(fd) == 0;
}
bool CompilerCache::renameTempFileBinaryToProperName(const std::string &oldName, const std::string &kernelFileHash) {
int err = NEO::SysCalls::rename(oldName.c_str(), kernelFileHash.c_str());
if (err < 0) {
NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "PID %d [Cache failure]: Rename temp file failed! errno: %d\n", NEO::SysCalls::getProcessId(), errno);
NEO::SysCalls::unlink(oldName);
return false;
}
return true;
}
void unlockFileAndClose(int fd) {
int lockErr = NEO::SysCalls::flock(fd, LOCK_UN);
if (lockErr < 0) {
NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "PID %d [Cache failure]: unlock file failed! errno: %d\n", NEO::SysCalls::getProcessId(), errno);
}
NEO::SysCalls::close(fd);
}
void CompilerCache::lockConfigFileAndReadSize(const std::string &configFilePath, UnifiedHandle &fd, size_t &directorySize) {
bool countDirectorySize = false;
errno = 0;
std::get<int>(fd) = NEO::SysCalls::open(configFilePath.c_str(), O_RDWR);
if (std::get<int>(fd) < 0) {
if (errno == ENOENT) {
std::get<int>(fd) = NEO::SysCalls::openWithMode(configFilePath.c_str(), O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
if (std::get<int>(fd) < 0) {
std::get<int>(fd) = NEO::SysCalls::open(configFilePath.c_str(), O_RDWR);
} else {
countDirectorySize = true;
}
} else {
NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "PID %d [Cache failure]: Open config file failed! errno: %d\n", NEO::SysCalls::getProcessId(), errno);
return;
}
}
const int lockErr = NEO::SysCalls::flock(std::get<int>(fd), LOCK_EX);
if (lockErr < 0) {
NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "PID %d [Cache failure]: Lock config file failed! errno: %d\n", NEO::SysCalls::getProcessId(), errno);
NEO::SysCalls::close(std::get<int>(fd));
std::get<int>(fd) = -1;
return;
}
if (countDirectorySize) {
struct dirent **files = {};
const int filesCount = NEO::SysCalls::scandir(config.cacheDir.c_str(), &files, filterFunction, NULL);
if (filesCount == -1) {
unlockFileAndClose(std::get<int>(fd));
NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "PID %d [Cache failure]: Scandir failed! errno: %d\n", NEO::SysCalls::getProcessId(), errno);
std::get<int>(fd) = -1;
return;
}
std::vector<ElementsStruct> cacheFiles;
cacheFiles.reserve(static_cast<size_t>(filesCount));
for (int i = 0; i < filesCount; ++i) {
std::string_view fileName = files[i]->d_name;
if (fileName.find(config.cacheFileExtension) != fileName.npos) {
ElementsStruct fileElement = {};
fileElement.path = joinPath(config.cacheDir, files[i]->d_name);
if (NEO::SysCalls::stat(fileElement.path, &fileElement.statEl) == 0) {
cacheFiles.push_back(std::move(fileElement));
}
}
free(files[i]);
}
free(files);
for (const auto &element : cacheFiles) {
directorySize += element.statEl.st_size;
}
} else {
const ssize_t readErr = NEO::SysCalls::pread(std::get<int>(fd), &directorySize, sizeof(directorySize), 0);
if (readErr < 0) {
directorySize = 0;
NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "PID %d [Cache failure]: Read config failed! errno: %d\n", NEO::SysCalls::getProcessId(), errno);
unlockFileAndClose(std::get<int>(fd));
std::get<int>(fd) = -1;
}
}
}
class HandleGuard {
public:
HandleGuard() = delete;
explicit HandleGuard(int &fileDescriptor) : fd(fileDescriptor) {}
~HandleGuard() {
if (fd != -1) {
unlockFileAndClose(fd);
}
}
private:
int fd = -1;
};
bool CompilerCache::cacheBinary(const std::string &kernelFileHash, const char *pBinary, size_t binarySize) {
if (pBinary == nullptr || binarySize == 0 || binarySize > config.cacheSize) {
return false;
}
std::unique_lock<std::mutex> lock(cacheAccessMtx);
constexpr std::string_view configFileName = "config.file";
std::string configFilePath = joinPath(config.cacheDir, configFileName.data());
std::string cacheFilePath = joinPath(config.cacheDir, kernelFileHash + config.cacheFileExtension);
UnifiedHandle fd{-1};
size_t directorySize = 0u;
lockConfigFileAndReadSize(configFilePath, fd, directorySize);
if (std::get<int>(fd) < 0) {
return false;
}
HandleGuard configGuard(std::get<int>(fd));
struct stat statbuf = {};
if (NEO::SysCalls::stat(cacheFilePath, &statbuf) == 0) {
return true;
}
const size_t maxSize = config.cacheSize;
if (maxSize < (directorySize + binarySize)) {
uint64_t bytesEvicted{0u};
const auto evictSuccess = evictCache(bytesEvicted);
const auto availableSpace = maxSize - directorySize + bytesEvicted;
directorySize = std::max<size_t>(0, directorySize - bytesEvicted);
if (!evictSuccess || binarySize > availableSpace) {
if (bytesEvicted > 0) {
NEO::SysCalls::pwrite(std::get<int>(fd), &directorySize, sizeof(directorySize), 0);
}
return false;
}
}
std::string tmpFileName = "cl_cache.XXXXXX";
std::string tmpFilePath = joinPath(config.cacheDir, tmpFileName);
if (!createUniqueTempFileAndWriteData(tmpFilePath.data(), pBinary, binarySize)) {
return false;
}
if (!renameTempFileBinaryToProperName(tmpFilePath, cacheFilePath)) {
return false;
}
directorySize += binarySize;
NEO::SysCalls::pwrite(std::get<int>(fd), &directorySize, sizeof(directorySize), 0);
return true;
}
std::unique_ptr<char[]> CompilerCache::loadCachedBinary(const std::string &kernelFileHash, size_t &cachedBinarySize) {
std::string filePath = joinPath(config.cacheDir, kernelFileHash + config.cacheFileExtension);
return loadDataFromFile(filePath.c_str(), cachedBinarySize);
}
} // namespace NEO