22 #define DEBUG_TYPE "cache-pruning" 25 #include <system_error> 38 return std::tie(Time, Other.Size, Path) <
39 std::tie(Other.Time, Size, Other.Path);
53 return make_error<StringError>(
"Duration must not be empty",
59 return make_error<StringError>(
"'" + NumStr +
"' not an integer",
62 switch (Duration.
back()) {
64 return std::chrono::seconds(Num);
66 return std::chrono::minutes(Num);
68 return std::chrono::hours(Num);
70 return make_error<StringError>(
"'" + Duration +
71 "' must end with one of 's', 'm' or 'h'",
79 std::pair<StringRef, StringRef>
P = {
"", PolicyStr};
80 while (!P.second.empty()) {
81 P = P.second.
split(
':');
84 std::tie(Key, Value) = P.first.split(
'=');
85 if (Key ==
"prune_interval") {
88 return DurationOrErr.takeError();
90 }
else if (Key ==
"prune_after") {
93 return DurationOrErr.takeError();
95 }
else if (Key ==
"cache_size") {
96 if (Value.
back() !=
'%')
97 return make_error<StringError>(
"'" + Value +
"' must be a percentage",
102 return make_error<StringError>(
"'" + SizeStr +
"' not an integer",
105 return make_error<StringError>(
"'" + SizeStr +
106 "' must be between 0 and 100",
109 }
else if (Key ==
"cache_size_bytes") {
111 switch (tolower(Value.
back())) {
121 Mult = 1024 * 1024 * 1024;
127 return make_error<StringError>(
"'" + Value +
"' not an integer",
130 }
else if (Key ==
"cache_size_files") {
132 return make_error<StringError>(
"'" + Value +
"' not an integer",
135 return make_error<StringError>(
"Unknown key: '" + Key +
"'",
184 if (Policy.
Interval != seconds(0)) {
188 auto TimeStampAge = CurrentTime - TimeStampModTime;
189 if (TimeStampAge <= *Policy.
Interval) {
191 << duration_cast<seconds>(TimeStampAge).
count()
192 <<
"s old), do not prune.\n");
204 std::set<FileInfo> FileInfos;
205 uint64_t TotalSize = 0;
213 File != FileEnd && !EC;
File.increment(EC)) {
230 const auto FileAccessTime = StatusOrErr->getLastAccessedTime();
231 auto FileAge = CurrentTime - FileAccessTime;
234 << duration_cast<seconds>(FileAge).
count()
241 TotalSize += StatusOrErr->getSize();
242 FileInfos.insert({FileAccessTime, StatusOrErr->getSize(),
File->path()});
246 size_t NumFiles = FileInfos.size();
248 auto RemoveCacheFile = [&]() {
255 <<
FileInfo->Size <<
"), new occupancy is " << TotalSize
268 if (!ErrOrSpaceInfo) {
272 auto AvailableSpace = TotalSize + SpaceInfo.
free;
278 auto TotalSizeTarget = std::min<uint64_t>(
282 LLVM_DEBUG(
dbgs() <<
"Occupancy: " << ((100 * TotalSize) / AvailableSpace)
288 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.
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.
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Tagged union holding either a T or a Error.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
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 size_t size() const
size - Get the string size.
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().
Instrumentation for Order File
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)
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.
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_NODISCARD StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
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.