23 #define DEBUG_TYPE "cache-pruning" 26 #include <system_error> 39 if (Time < Other.Time)
41 else if (Other.Time < Time)
43 if (Other.Size < Size)
45 else if (Size < Other.Size)
47 return Path < Other.Path;
61 return make_error<StringError>(
"Duration must not be empty",
67 return make_error<StringError>(
"'" + NumStr +
"' not an integer",
70 switch (Duration.
back()) {
72 return std::chrono::seconds(Num);
74 return std::chrono::minutes(Num);
76 return std::chrono::hours(Num);
78 return make_error<StringError>(
"'" + Duration +
79 "' must end with one of 's', 'm' or 'h'",
87 std::pair<StringRef, StringRef>
P = {
"", PolicyStr};
88 while (!P.second.empty()) {
89 P = P.second.
split(
':');
92 std::tie(Key, Value) = P.first.split(
'=');
93 if (Key ==
"prune_interval") {
96 return DurationOrErr.takeError();
98 }
else if (Key ==
"prune_after") {
101 return DurationOrErr.takeError();
103 }
else if (Key ==
"cache_size") {
104 if (Value.
back() !=
'%')
105 return make_error<StringError>(
"'" + Value +
"' must be a percentage",
110 return make_error<StringError>(
"'" + SizeStr +
"' not an integer",
113 return make_error<StringError>(
"'" + SizeStr +
114 "' must be between 0 and 100",
117 }
else if (Key ==
"cache_size_bytes") {
119 switch (tolower(Value.
back())) {
129 Mult = 1024 * 1024 * 1024;
135 return make_error<StringError>(
"'" + Value +
"' not an integer",
138 }
else if (Key ==
"cache_size_files") {
140 return make_error<StringError>(
"'" + Value +
"' not an integer",
143 return make_error<StringError>(
"Unknown key: '" + Key +
"'",
153 using namespace std::chrono;
192 if (Policy.
Interval != seconds(0)) {
196 auto TimeStampAge = CurrentTime - TimeStampModTime;
197 if (TimeStampAge <= *Policy.
Interval) {
199 << duration_cast<seconds>(TimeStampAge).
count()
200 <<
"s old), do not prune.\n");
212 std::set<FileInfo> FileInfos;
213 uint64_t TotalSize = 0;
221 File != FileEnd && !EC;
File.increment(EC)) {
238 const auto FileAccessTime = StatusOrErr->getLastAccessedTime();
239 auto FileAge = CurrentTime - FileAccessTime;
242 << duration_cast<seconds>(FileAge).
count()
249 TotalSize += StatusOrErr->getSize();
250 FileInfos.insert({FileAccessTime, StatusOrErr->getSize(),
File->path()});
254 size_t NumFiles = FileInfos.size();
256 auto RemoveCacheFile = [&]() {
263 <<
FileInfo->Size <<
"), new occupancy is " << TotalSize
276 if (!ErrOrSpaceInfo) {
280 auto AvailableSpace = TotalSize + SpaceInfo.
free;
286 auto TotalSizeTarget = std::min<uint64_t>(
290 LLVM_DEBUG(
dbgs() <<
"Occupancy: " << ((100 * TotalSize) / AvailableSpace)
296 while (TotalSize > TotalSizeTarget &&
FileInfo != FileInfos.end())
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.
This class represents lattice values for constants.
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
The file modification time as reported from the underlying file system.
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.
FileInfo(const GCOV::Options &Options)
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.
bool operator<(int64_t V1, const APSInt &V2)
LLVM Value Representation.
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...
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.