1//===-CachePruning.cpp - LLVM Cache Directory Pruning ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the pruning of a directory based on least recently used.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/CachePruning.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Support/Debug.h"
16#include "llvm/Support/Errc.h"
17#include "llvm/Support/Error.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/WithColor.h"
21#include "llvm/Support/raw_ostream.h"
22
23#define DEBUG_TYPE "cache-pruning"
24
25#include <set>
26#include <system_error>
27
28using namespace llvm;
29
30namespace {
31struct FileInfo {
32 sys::TimePoint<> Time;
33 uint64_t Size;
34 std::string Path;
35
36 /// Used to determine which files to prune first. Also used to determine
37 /// set membership, so must take into account all fields.
38 bool operator<(const FileInfo &Other) const {
39 return std::tie(args: Time, args: Other.Size, args: Path) <
40 std::tie(args: Other.Time, args: Size, args: Other.Path);
41 }
42};
43} // anonymous namespace
44
45/// Write a new timestamp file with the given path. This is used for the pruning
46/// interval option.
47static void writeTimestampFile(StringRef TimestampFile) {
48 std::error_code EC;
49 raw_fd_ostream Out(TimestampFile.str(), EC, sys::fs::OF_None);
50}
51
52static Expected<std::chrono::seconds> parseDuration(StringRef Duration) {
53 if (Duration.empty())
54 return make_error<StringError>(Args: "Duration must not be empty",
55 Args: inconvertibleErrorCode());
56
57 StringRef NumStr = Duration.slice(Start: 0, End: Duration.size()-1);
58 uint64_t Num;
59 if (NumStr.getAsInteger(Radix: 0, Result&: Num))
60 return make_error<StringError>(Args: "'" + NumStr + "' not an integer",
61 Args: inconvertibleErrorCode());
62
63 switch (Duration.back()) {
64 case 's':
65 return std::chrono::seconds(Num);
66 case 'm':
67 return std::chrono::minutes(Num);
68 case 'h':
69 return std::chrono::hours(Num);
70 default:
71 return make_error<StringError>(Args: "'" + Duration +
72 "' must end with one of 's', 'm' or 'h'",
73 Args: inconvertibleErrorCode());
74 }
75}
76
77Expected<CachePruningPolicy>
78llvm::parseCachePruningPolicy(StringRef PolicyStr) {
79 CachePruningPolicy Policy;
80 std::pair<StringRef, StringRef> P = {"", PolicyStr};
81 while (!P.second.empty()) {
82 P = P.second.split(Separator: ':');
83
84 StringRef Key, Value;
85 std::tie(args&: Key, args&: Value) = P.first.split(Separator: '=');
86 if (Key == "prune_interval") {
87 auto DurationOrErr = parseDuration(Duration: Value);
88 if (!DurationOrErr)
89 return DurationOrErr.takeError();
90 Policy.Interval = *DurationOrErr;
91 } else if (Key == "prune_after") {
92 auto DurationOrErr = parseDuration(Duration: Value);
93 if (!DurationOrErr)
94 return DurationOrErr.takeError();
95 Policy.Expiration = *DurationOrErr;
96 } else if (Key == "cache_size") {
97 if (Value.back() != '%')
98 return make_error<StringError>(Args: "'" + Value + "' must be a percentage",
99 Args: inconvertibleErrorCode());
100 StringRef SizeStr = Value.drop_back();
101 uint64_t Size;
102 if (SizeStr.getAsInteger(Radix: 0, Result&: Size))
103 return make_error<StringError>(Args: "'" + SizeStr + "' not an integer",
104 Args: inconvertibleErrorCode());
105 if (Size > 100)
106 return make_error<StringError>(Args: "'" + SizeStr +
107 "' must be between 0 and 100",
108 Args: inconvertibleErrorCode());
109 Policy.MaxSizePercentageOfAvailableSpace = Size;
110 } else if (Key == "cache_size_bytes") {
111 uint64_t Mult = 1;
112 switch (tolower(c: Value.back())) {
113 case 'k':
114 Mult = 1024;
115 Value = Value.drop_back();
116 break;
117 case 'm':
118 Mult = 1024 * 1024;
119 Value = Value.drop_back();
120 break;
121 case 'g':
122 Mult = 1024 * 1024 * 1024;
123 Value = Value.drop_back();
124 break;
125 }
126 uint64_t Size;
127 if (Value.getAsInteger(Radix: 0, Result&: Size))
128 return make_error<StringError>(Args: "'" + Value + "' not an integer",
129 Args: inconvertibleErrorCode());
130 Policy.MaxSizeBytes = Size * Mult;
131 } else if (Key == "cache_size_files") {
132 if (Value.getAsInteger(Radix: 0, Result&: Policy.MaxSizeFiles))
133 return make_error<StringError>(Args: "'" + Value + "' not an integer",
134 Args: inconvertibleErrorCode());
135 } else {
136 return make_error<StringError>(Args: "Unknown key: '" + Key + "'",
137 Args: inconvertibleErrorCode());
138 }
139 }
140
141 return Policy;
142}
143
144/// Prune the cache of files that haven't been accessed in a long time.
145Expected<bool>
146llvm::pruneCache(StringRef Path, CachePruningPolicy Policy,
147 const std::vector<std::unique_ptr<MemoryBuffer>> &Files) {
148 using namespace std::chrono;
149
150 if (Path.empty())
151 return false;
152
153 bool isPathDir;
154 if (sys::fs::is_directory(path: Path, result&: isPathDir))
155 return false;
156
157 if (!isPathDir)
158 return false;
159
160 Policy.MaxSizePercentageOfAvailableSpace =
161 std::min(a: Policy.MaxSizePercentageOfAvailableSpace, b: 100u);
162
163 if (Policy.Expiration == seconds(0) &&
164 Policy.MaxSizePercentageOfAvailableSpace == 0 &&
165 Policy.MaxSizeBytes == 0 && Policy.MaxSizeFiles == 0) {
166 LLVM_DEBUG(dbgs() << "No pruning settings set, exit early\n");
167 // Nothing will be pruned, early exit
168 return false;
169 }
170
171 // Try to stat() the timestamp file.
172 SmallString<128> TimestampFile(Path);
173 sys::path::append(path&: TimestampFile, a: "llvmcache.timestamp");
174 sys::fs::file_status FileStatus;
175 const auto CurrentTime = system_clock::now();
176 if (auto EC = sys::fs::status(path: TimestampFile, result&: FileStatus)) {
177 if (EC == errc::no_such_file_or_directory) {
178 // If the timestamp file wasn't there, create one now.
179 writeTimestampFile(TimestampFile);
180 } else {
181 // Unknown error?
182 return false;
183 }
184 } else {
185 if (!Policy.Interval)
186 return false;
187 if (Policy.Interval != seconds(0)) {
188 // Check whether the time stamp is older than our pruning interval.
189 // If not, do nothing.
190 const auto TimeStampModTime = FileStatus.getLastModificationTime();
191 auto TimeStampAge = CurrentTime - TimeStampModTime;
192 if (TimeStampAge <= *Policy.Interval) {
193 LLVM_DEBUG(dbgs() << "Timestamp file too recent ("
194 << duration_cast<seconds>(TimeStampAge).count()
195 << "s old), do not prune.\n");
196 return false;
197 }
198 }
199 // Write a new timestamp file so that nobody else attempts to prune.
200 // There is a benign race condition here, if two processes happen to
201 // notice at the same time that the timestamp is out-of-date.
202 writeTimestampFile(TimestampFile);
203 }
204
205 // Keep track of files to delete to get below the size limit.
206 // Order by time of last use so that recently used files are preserved.
207 std::set<FileInfo> FileInfos;
208 uint64_t TotalSize = 0;
209
210 // Walk the entire directory cache, looking for unused files.
211 std::error_code EC;
212 SmallString<128> CachePathNative;
213 sys::path::native(path: Path, result&: CachePathNative);
214 // Walk all of the files within this directory.
215 for (sys::fs::directory_iterator File(CachePathNative, EC), FileEnd;
216 File != FileEnd && !EC; File.increment(ec&: EC)) {
217 // Ignore filenames not beginning with "llvmcache-" or "Thin-". This
218 // includes the timestamp file as well as any files created by the user.
219 // This acts as a safeguard against data loss if the user specifies the
220 // wrong directory as their cache directory.
221 StringRef filename = sys::path::filename(path: File->path());
222 if (!filename.starts_with(Prefix: "llvmcache-") && !filename.starts_with(Prefix: "Thin-"))
223 continue;
224
225 // Look at this file. If we can't stat it, there's nothing interesting
226 // there.
227 ErrorOr<sys::fs::basic_file_status> StatusOrErr = File->status();
228 if (!StatusOrErr) {
229 LLVM_DEBUG(dbgs() << "Ignore " << File->path() << " (can't stat)\n");
230 continue;
231 }
232
233 // If the file hasn't been used recently enough, delete it
234 const auto FileAccessTime = StatusOrErr->getLastAccessedTime();
235 auto FileAge = CurrentTime - FileAccessTime;
236 if (Policy.Expiration != seconds(0) && FileAge > Policy.Expiration) {
237 LLVM_DEBUG(dbgs() << "Remove " << File->path() << " ("
238 << duration_cast<seconds>(FileAge).count()
239 << "s old)\n");
240 sys::fs::remove(path: File->path());
241 continue;
242 }
243
244 // Leave it here for now, but add it to the list of size-based pruning.
245 TotalSize += StatusOrErr->getSize();
246 FileInfos.insert(x: {.Time: FileAccessTime, .Size: StatusOrErr->getSize(), .Path: File->path()});
247 }
248
249 auto FileInfo = FileInfos.begin();
250 size_t NumFiles = FileInfos.size();
251
252 auto RemoveCacheFile = [&]() {
253 // Remove the file.
254 sys::fs::remove(path: FileInfo->Path);
255 // Update size
256 TotalSize -= FileInfo->Size;
257 NumFiles--;
258 LLVM_DEBUG(dbgs() << " - Remove " << FileInfo->Path << " (size "
259 << FileInfo->Size << "), new occupancy is " << TotalSize
260 << "%\n");
261 ++FileInfo;
262 };
263
264 // files.size() is greater the number of inputs by one. However, a timestamp
265 // file is created and stored in the cache directory if --thinlto-cache-policy
266 // option is used. Therefore, files.size() is used as ActualNums.
267 const size_t ActualNums = Files.size();
268 if (Policy.MaxSizeFiles && ActualNums > Policy.MaxSizeFiles)
269 WithColor::warning()
270 << "ThinLTO cache pruning happens since the number of created files ("
271 << ActualNums << ") exceeds the maximum number of files ("
272 << Policy.MaxSizeFiles
273 << "); consider adjusting --thinlto-cache-policy\n";
274
275 // Prune for number of files.
276 if (Policy.MaxSizeFiles)
277 while (NumFiles > Policy.MaxSizeFiles)
278 RemoveCacheFile();
279
280 // Prune for size now if needed
281 if (Policy.MaxSizePercentageOfAvailableSpace > 0 || Policy.MaxSizeBytes > 0) {
282 auto SpaceInfoOrErr = sys::fs::disk_space(Path);
283 if (!SpaceInfoOrErr) {
284 auto EC = SpaceInfoOrErr.getError();
285 return createStringError(EC,
286 Fmt: "cannot get available disk space for '%s': '%s'",
287 Vals: Path.str().c_str(), Vals: EC.message().c_str());
288 }
289 sys::fs::space_info SpaceInfo = SpaceInfoOrErr.get();
290 auto AvailableSpace = TotalSize + SpaceInfo.free;
291
292 if (Policy.MaxSizePercentageOfAvailableSpace == 0)
293 Policy.MaxSizePercentageOfAvailableSpace = 100;
294 if (Policy.MaxSizeBytes == 0)
295 Policy.MaxSizeBytes = AvailableSpace;
296 auto TotalSizeTarget = std::min<uint64_t>(
297 a: AvailableSpace * Policy.MaxSizePercentageOfAvailableSpace / 100ull,
298 b: Policy.MaxSizeBytes);
299
300 LLVM_DEBUG(dbgs() << "Occupancy: " << ((100 * TotalSize) / AvailableSpace)
301 << "% target is: "
302 << Policy.MaxSizePercentageOfAvailableSpace << "%, "
303 << Policy.MaxSizeBytes << " bytes\n");
304
305 size_t ActualSizes = 0;
306 for (const auto &File : Files)
307 if (File)
308 ActualSizes += File->getBufferSize();
309
310 if (ActualSizes > TotalSizeTarget)
311 WithColor::warning()
312 << "ThinLTO cache pruning happens since the total size of the cache "
313 "files consumed by the current link job ("
314 << ActualSizes << " bytes) exceeds maximum cache size ("
315 << TotalSizeTarget
316 << " bytes); consider adjusting --thinlto-cache-policy\n";
317
318 // Remove the oldest accessed files first, till we get below the threshold.
319 while (TotalSize > TotalSizeTarget && FileInfo != FileInfos.end())
320 RemoveCacheFile();
321 }
322 return true;
323}
324