blob: 7326c4fc91fbd2160d50a18d7145ab9d7cd6c18f [file] [log] [blame]
Mehdi Amini27814982016-04-02 03:28:26 +00001//===-CachePruning.cpp - LLVM Cache Directory Pruning ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the pruning of a directory based on least recently used.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/CachePruning.h"
15
Mehdi Amini045d4752016-04-18 21:54:00 +000016#include "llvm/Support/Debug.h"
NAKAMURA Takumif5e36ad2016-05-14 14:21:39 +000017#include "llvm/Support/Errc.h"
Peter Collingbourned1eac7b2017-03-16 03:42:00 +000018#include "llvm/Support/Error.h"
Mehdi Amini27814982016-04-02 03:28:26 +000019#include "llvm/Support/FileSystem.h"
20#include "llvm/Support/Path.h"
21#include "llvm/Support/raw_ostream.h"
22
Mehdi Amini045d4752016-04-18 21:54:00 +000023#define DEBUG_TYPE "cache-pruning"
24
Mehdi Amini27814982016-04-02 03:28:26 +000025#include <set>
Vassil Vassilev2ec8b152016-09-14 08:55:18 +000026#include <system_error>
Mehdi Amini27814982016-04-02 03:28:26 +000027
28using namespace llvm;
29
30/// Write a new timestamp file with the given path. This is used for the pruning
31/// interval option.
32static void writeTimestampFile(StringRef TimestampFile) {
33 std::error_code EC;
34 raw_fd_ostream Out(TimestampFile.str(), EC, sys::fs::F_None);
35}
36
Peter Collingbourned1eac7b2017-03-16 03:42:00 +000037static Expected<std::chrono::seconds> parseDuration(StringRef Duration) {
38 if (Duration.empty())
39 return make_error<StringError>("Duration must not be empty",
40 inconvertibleErrorCode());
41
42 StringRef NumStr = Duration.slice(0, Duration.size()-1);
43 uint64_t Num;
44 if (NumStr.getAsInteger(0, Num))
45 return make_error<StringError>("'" + NumStr + "' not an integer",
46 inconvertibleErrorCode());
47
48 switch (Duration.back()) {
49 case 's':
50 return std::chrono::seconds(Num);
51 case 'm':
52 return std::chrono::minutes(Num);
53 case 'h':
54 return std::chrono::hours(Num);
55 default:
56 return make_error<StringError>("'" + Duration +
57 "' must end with one of 's', 'm' or 'h'",
58 inconvertibleErrorCode());
59 }
60}
61
62Expected<CachePruningPolicy>
63llvm::parseCachePruningPolicy(StringRef PolicyStr) {
64 CachePruningPolicy Policy;
65 std::pair<StringRef, StringRef> P = {"", PolicyStr};
66 while (!P.second.empty()) {
67 P = P.second.split(':');
68
69 StringRef Key, Value;
70 std::tie(Key, Value) = P.first.split('=');
71 if (Key == "prune_interval") {
72 auto DurationOrErr = parseDuration(Value);
73 if (!DurationOrErr)
Peter Collingbourne255c6e12017-03-16 03:54:38 +000074 return DurationOrErr.takeError();
Peter Collingbourned1eac7b2017-03-16 03:42:00 +000075 Policy.Interval = *DurationOrErr;
76 } else if (Key == "prune_after") {
77 auto DurationOrErr = parseDuration(Value);
78 if (!DurationOrErr)
Peter Collingbourne255c6e12017-03-16 03:54:38 +000079 return DurationOrErr.takeError();
Peter Collingbourned1eac7b2017-03-16 03:42:00 +000080 Policy.Expiration = *DurationOrErr;
81 } else if (Key == "cache_size") {
Peter Collingbourne15ab17202017-06-23 17:17:47 +000082 if (Value.back() != '%')
83 return make_error<StringError>("'" + Value + "' must be a percentage",
84 inconvertibleErrorCode());
Peter Collingbourne8d292232017-06-23 17:05:03 +000085 StringRef SizeStr = Value.drop_back();
Peter Collingbourned1eac7b2017-03-16 03:42:00 +000086 uint64_t Size;
87 if (SizeStr.getAsInteger(0, Size))
88 return make_error<StringError>("'" + SizeStr + "' not an integer",
89 inconvertibleErrorCode());
90 if (Size > 100)
91 return make_error<StringError>("'" + SizeStr +
92 "' must be between 0 and 100",
93 inconvertibleErrorCode());
Peter Collingbourne8d292232017-06-23 17:05:03 +000094 Policy.MaxSizePercentageOfAvailableSpace = Size;
95 } else if (Key == "cache_size_bytes") {
96 uint64_t Mult = 1;
Peter Collingbourne30aaa2f2017-06-23 17:13:51 +000097 switch (tolower(Value.back())) {
Peter Collingbourne8d292232017-06-23 17:05:03 +000098 case 'k':
99 Mult = 1024;
100 Value = Value.drop_back();
101 break;
102 case 'm':
103 Mult = 1024 * 1024;
104 Value = Value.drop_back();
105 break;
106 case 'g':
107 Mult = 1024 * 1024 * 1024;
108 Value = Value.drop_back();
109 break;
110 }
111 uint64_t Size;
112 if (Value.getAsInteger(0, Size))
113 return make_error<StringError>("'" + Value + "' not an integer",
114 inconvertibleErrorCode());
115 Policy.MaxSizeBytes = Size * Mult;
Peter Collingbourne048ac832017-11-22 18:27:31 +0000116 } else if (Key == "cache_size_files") {
117 if (Value.getAsInteger(0, Policy.MaxSizeFiles))
118 return make_error<StringError>("'" + Value + "' not an integer",
119 inconvertibleErrorCode());
Peter Collingbourned1eac7b2017-03-16 03:42:00 +0000120 } else {
121 return make_error<StringError>("Unknown key: '" + Key + "'",
122 inconvertibleErrorCode());
123 }
124 }
125
126 return Policy;
127}
128
Mehdi Amini27814982016-04-02 03:28:26 +0000129/// Prune the cache of files that haven't been accessed in a long time.
Peter Collingbournecead56f2017-03-15 22:54:18 +0000130bool llvm::pruneCache(StringRef Path, CachePruningPolicy Policy) {
Pavel Labath757ca882016-10-24 10:59:17 +0000131 using namespace std::chrono;
132
Mehdi Amini721800d2016-04-21 06:43:45 +0000133 if (Path.empty())
134 return false;
135
136 bool isPathDir;
137 if (sys::fs::is_directory(Path, isPathDir))
138 return false;
139
140 if (!isPathDir)
141 return false;
Mehdi Amini27814982016-04-02 03:28:26 +0000142
Peter Collingbourne8d292232017-06-23 17:05:03 +0000143 Policy.MaxSizePercentageOfAvailableSpace =
144 std::min(Policy.MaxSizePercentageOfAvailableSpace, 100u);
Peter Collingbournecead56f2017-03-15 22:54:18 +0000145
146 if (Policy.Expiration == seconds(0) &&
Peter Collingbourne8d292232017-06-23 17:05:03 +0000147 Policy.MaxSizePercentageOfAvailableSpace == 0 &&
Peter Collingbourne048ac832017-11-22 18:27:31 +0000148 Policy.MaxSizeBytes == 0 && Policy.MaxSizeFiles == 0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000149 LLVM_DEBUG(dbgs() << "No pruning settings set, exit early\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000150 // Nothing will be pruned, early exit
151 return false;
Mehdi Amini045d4752016-04-18 21:54:00 +0000152 }
Mehdi Amini27814982016-04-02 03:28:26 +0000153
154 // Try to stat() the timestamp file.
Mehdi Amini721800d2016-04-21 06:43:45 +0000155 SmallString<128> TimestampFile(Path);
156 sys::path::append(TimestampFile, "llvmcache.timestamp");
Mehdi Amini27814982016-04-02 03:28:26 +0000157 sys::fs::file_status FileStatus;
Ben Dunbobbinbb534b12017-12-22 18:32:15 +0000158 const auto CurrentTime = system_clock::now();
NAKAMURA Takumif5e36ad2016-05-14 14:21:39 +0000159 if (auto EC = sys::fs::status(TimestampFile, FileStatus)) {
160 if (EC == errc::no_such_file_or_directory) {
Mehdi Amini27814982016-04-02 03:28:26 +0000161 // If the timestamp file wasn't there, create one now.
162 writeTimestampFile(TimestampFile);
163 } else {
164 // Unknown error?
165 return false;
166 }
167 } else {
Ben Dunbobbinbb534b12017-12-22 18:32:15 +0000168 if (!Policy.Interval)
169 return false;
Ben Dunbobbincac52142017-11-17 14:42:18 +0000170 if (Policy.Interval != seconds(0)) {
Mehdi Amini27814982016-04-02 03:28:26 +0000171 // Check whether the time stamp is older than our pruning interval.
172 // If not, do nothing.
Ben Dunbobbinbb534b12017-12-22 18:32:15 +0000173 const auto TimeStampModTime = FileStatus.getLastModificationTime();
Mehdi Amini045d4752016-04-18 21:54:00 +0000174 auto TimeStampAge = CurrentTime - TimeStampModTime;
Ben Dunbobbinbb534b12017-12-22 18:32:15 +0000175 if (TimeStampAge <= *Policy.Interval) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000176 LLVM_DEBUG(dbgs() << "Timestamp file too recent ("
177 << duration_cast<seconds>(TimeStampAge).count()
178 << "s old), do not prune.\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000179 return false;
Mehdi Amini045d4752016-04-18 21:54:00 +0000180 }
Mehdi Amini27814982016-04-02 03:28:26 +0000181 }
182 // Write a new timestamp file so that nobody else attempts to prune.
183 // There is a benign race condition here, if two processes happen to
184 // notice at the same time that the timestamp is out-of-date.
185 writeTimestampFile(TimestampFile);
186 }
187
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000188 // Keep track of space. Needs to be kept ordered by size for determinism.
Mehdi Amini27814982016-04-02 03:28:26 +0000189 std::set<std::pair<uint64_t, std::string>> FileSizes;
190 uint64_t TotalSize = 0;
Mehdi Amini27814982016-04-02 03:28:26 +0000191
192 // Walk the entire directory cache, looking for unused files.
193 std::error_code EC;
194 SmallString<128> CachePathNative;
195 sys::path::native(Path, CachePathNative);
Mehdi Amini27814982016-04-02 03:28:26 +0000196 // Walk all of the files within this directory.
197 for (sys::fs::directory_iterator File(CachePathNative, EC), FileEnd;
198 File != FileEnd && !EC; File.increment(EC)) {
Peter Collingbourne25a17ba2017-03-20 16:41:57 +0000199 // Ignore any files not beginning with the string "llvmcache-". This
200 // includes the timestamp file as well as any files created by the user.
201 // This acts as a safeguard against data loss if the user specifies the
202 // wrong directory as their cache directory.
203 if (!sys::path::filename(File->path()).startswith("llvmcache-"))
Mehdi Amini27814982016-04-02 03:28:26 +0000204 continue;
205
206 // Look at this file. If we can't stat it, there's nothing interesting
207 // there.
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000208 ErrorOr<sys::fs::basic_file_status> StatusOrErr = File->status();
209 if (!StatusOrErr) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000210 LLVM_DEBUG(dbgs() << "Ignore " << File->path() << " (can't stat)\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000211 continue;
Mehdi Amini045d4752016-04-18 21:54:00 +0000212 }
Mehdi Amini27814982016-04-02 03:28:26 +0000213
214 // If the file hasn't been used recently enough, delete it
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000215 const auto FileAccessTime = StatusOrErr->getLastAccessedTime();
Mehdi Amini045d4752016-04-18 21:54:00 +0000216 auto FileAge = CurrentTime - FileAccessTime;
Peter Collingbourne048ac832017-11-22 18:27:31 +0000217 if (Policy.Expiration != seconds(0) && FileAge > Policy.Expiration) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000218 LLVM_DEBUG(dbgs() << "Remove " << File->path() << " ("
219 << duration_cast<seconds>(FileAge).count()
220 << "s old)\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000221 sys::fs::remove(File->path());
222 continue;
223 }
224
225 // Leave it here for now, but add it to the list of size-based pruning.
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000226 TotalSize += StatusOrErr->getSize();
227 FileSizes.insert({StatusOrErr->getSize(), std::string(File->path())});
Mehdi Amini27814982016-04-02 03:28:26 +0000228 }
229
Peter Collingbourne048ac832017-11-22 18:27:31 +0000230 auto FileAndSize = FileSizes.rbegin();
231 size_t NumFiles = FileSizes.size();
232
233 auto RemoveCacheFile = [&]() {
234 // Remove the file.
235 sys::fs::remove(FileAndSize->second);
236 // Update size
237 TotalSize -= FileAndSize->first;
238 NumFiles--;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000239 LLVM_DEBUG(dbgs() << " - Remove " << FileAndSize->second << " (size "
240 << FileAndSize->first << "), new occupancy is "
241 << TotalSize << "%\n");
Peter Collingbourne048ac832017-11-22 18:27:31 +0000242 ++FileAndSize;
243 };
244
245 // Prune for number of files.
246 if (Policy.MaxSizeFiles)
247 while (NumFiles > Policy.MaxSizeFiles)
248 RemoveCacheFile();
249
Mehdi Amini27814982016-04-02 03:28:26 +0000250 // Prune for size now if needed
Peter Collingbourne048ac832017-11-22 18:27:31 +0000251 if (Policy.MaxSizePercentageOfAvailableSpace > 0 || Policy.MaxSizeBytes > 0) {
Mehdi Amini27814982016-04-02 03:28:26 +0000252 auto ErrOrSpaceInfo = sys::fs::disk_space(Path);
253 if (!ErrOrSpaceInfo) {
254 report_fatal_error("Can't get available size");
255 }
256 sys::fs::space_info SpaceInfo = ErrOrSpaceInfo.get();
257 auto AvailableSpace = TotalSize + SpaceInfo.free;
Peter Collingbourne8d292232017-06-23 17:05:03 +0000258
259 if (Policy.MaxSizePercentageOfAvailableSpace == 0)
260 Policy.MaxSizePercentageOfAvailableSpace = 100;
261 if (Policy.MaxSizeBytes == 0)
262 Policy.MaxSizeBytes = AvailableSpace;
263 auto TotalSizeTarget = std::min<uint64_t>(
264 AvailableSpace * Policy.MaxSizePercentageOfAvailableSpace / 100ull,
265 Policy.MaxSizeBytes);
266
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000267 LLVM_DEBUG(dbgs() << "Occupancy: " << ((100 * TotalSize) / AvailableSpace)
268 << "% target is: "
269 << Policy.MaxSizePercentageOfAvailableSpace << "%, "
270 << Policy.MaxSizeBytes << " bytes\n");
Peter Collingbourne8d292232017-06-23 17:05:03 +0000271
Peter Collingbourne048ac832017-11-22 18:27:31 +0000272 // Remove the oldest accessed files first, till we get below the threshold.
273 while (TotalSize > TotalSizeTarget && FileAndSize != FileSizes.rend())
274 RemoveCacheFile();
Mehdi Amini27814982016-04-02 03:28:26 +0000275 }
276 return true;
277}