23 #define DEBUG_TYPE "cache-pruning" 26 #include <system_error> 39 return make_error<StringError>(
"Duration must not be empty",
45 return make_error<StringError>(
"'" + NumStr +
"' not an integer",
48 switch (Duration.
back()) {
50 return std::chrono::seconds(Num);
52 return std::chrono::minutes(Num);
54 return std::chrono::hours(Num);
56 return make_error<StringError>(
"'" + Duration +
57 "' must end with one of 's', 'm' or 'h'",
65 std::pair<StringRef, StringRef>
P = {
"", PolicyStr};
66 while (!P.second.empty()) {
67 P = P.second.
split(
':');
70 std::tie(Key, Value) = P.first.split(
'=');
71 if (Key ==
"prune_interval") {
74 return DurationOrErr.takeError();
76 }
else if (Key ==
"prune_after") {
79 return DurationOrErr.takeError();
81 }
else if (Key ==
"cache_size") {
82 if (Value.
back() !=
'%')
83 return make_error<StringError>(
"'" + Value +
"' must be a percentage",
88 return make_error<StringError>(
"'" + SizeStr +
"' not an integer",
91 return make_error<StringError>(
"'" + SizeStr +
92 "' must be between 0 and 100",
95 }
else if (Key ==
"cache_size_bytes") {
97 switch (tolower(Value.
back())) {
107 Mult = 1024 * 1024 * 1024;
113 return make_error<StringError>(
"'" + Value +
"' not an integer",
116 }
else if (Key ==
"cache_size_files") {
118 return make_error<StringError>(
"'" + Value +
"' not an integer",
121 return make_error<StringError>(
"Unknown key: '" + Key +
"'",
131 using namespace std::chrono;
170 if (Policy.
Interval != seconds(0)) {
174 auto TimeStampAge = CurrentTime - TimeStampModTime;
175 if (TimeStampAge <= *Policy.
Interval) {
177 << duration_cast<seconds>(TimeStampAge).
count()
178 <<
"s old), do not prune.\n");
189 std::set<std::pair<uint64_t, std::string>> FileSizes;
190 uint64_t TotalSize = 0;
198 File != FileEnd && !EC;
File.increment(EC)) {
215 const auto FileAccessTime = StatusOrErr->getLastAccessedTime();
216 auto FileAge = CurrentTime - FileAccessTime;
219 << duration_cast<seconds>(FileAge).
count()
226 TotalSize += StatusOrErr->getSize();
227 FileSizes.insert({StatusOrErr->getSize(), std::string(
File->path())});
230 auto FileAndSize = FileSizes.rbegin();
231 size_t NumFiles = FileSizes.size();
233 auto RemoveCacheFile = [&]() {
237 TotalSize -= FileAndSize->first;
239 LLVM_DEBUG(
dbgs() <<
" - Remove " << FileAndSize->second <<
" (size " 240 << FileAndSize->first <<
"), new occupancy is " 241 << TotalSize <<
"%\n");
253 if (!ErrOrSpaceInfo) {
257 auto AvailableSpace = TotalSize + SpaceInfo.
free;
263 auto TotalSizeTarget = std::min<uint64_t>(
267 LLVM_DEBUG(
dbgs() <<
"Occupancy: " << ((100 * TotalSize) / AvailableSpace)
273 while (TotalSize > TotalSizeTarget && FileAndSize != FileSizes.rend())
uint64_t MaxSizeBytes
The maximum size for the cache directory in bytes.
Represents either an error or a value T.
void native(const Twine &path, SmallVectorImpl< char > &result, Style style=Style::native)
Convert path to the native form.
space_info - Self explanatory.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Compute iterated dominance frontiers using a linear time algorithm.
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
std::chrono::seconds Expiration
The expiration for a file.
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Represents the result of a call to sys::fs::status().
uint64_t MaxSizeFiles
The maximum number of files in the cache directory.
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Tagged union holding either a T or a Error.
static bool startswith(StringRef Magic, const char(&S)[N])
unsigned MaxSizePercentageOfAvailableSpace
The maximum size for the cache directory, in terms of percentage of the available space on the disk...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Expected< CachePruningPolicy > parseCachePruningPolicy(StringRef PolicyStr)
Parse the given string as a cache pruning policy.
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
static void writeTimestampFile(StringRef TimestampFile)
Write a new timestamp file with the given path.
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
TimePoint getLastModificationTime() const
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
bool pruneCache(StringRef Path, CachePruningPolicy Policy)
Peform pruning using the supplied policy, returns true if pruning occurred, i.e.
LLVM_NODISCARD char back() const
back - Get the last character in the string.
static Expected< std::chrono::seconds > parseDuration(StringRef Duration)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
llvm::Optional< std::chrono::seconds > Interval
The pruning interval.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Policy for the pruneCache() function.
ErrorOr< space_info > disk_space(const Twine &Path)
Get disk space usage information.
directory_iterator - Iterates through the entries in path.
A raw_ostream that writes to a file descriptor.
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
LLVM Value Representation.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
StringRef - Represent a constant reference to a string, i.e.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...