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",
117 return make_error<StringError>(
"Unknown key: '" + Key +
"'",
127 using namespace std::chrono;
145 DEBUG(
dbgs() <<
"No pruning settings set, exit early\n");
164 if (Policy.
Interval == seconds(0)) {
167 const auto TimeStampModTime = FileStatus.getLastModificationTime();
168 auto TimeStampAge = CurrentTime - TimeStampModTime;
169 if (TimeStampAge <= Policy.
Interval) {
170 DEBUG(
dbgs() <<
"Timestamp file too recent ("
171 << duration_cast<seconds>(TimeStampAge).
count()
172 <<
"s old), do not prune.\n");
182 bool ShouldComputeSize =
186 std::set<std::pair<uint64_t, std::string>> FileSizes;
187 uint64_t TotalSize = 0;
190 auto AddToFileListForSizePruning =
192 if (!ShouldComputeSize)
194 TotalSize += FileStatus.getSize();
196 std::make_pair(FileStatus.getSize(), std::string(Path)));
205 File != FileEnd && !EC;
File.increment(EC)) {
216 DEBUG(
dbgs() <<
"Ignore " <<
File->path() <<
" (can't stat)\n");
221 const auto FileAccessTime = FileStatus.getLastAccessedTime();
222 auto FileAge = CurrentTime - FileAccessTime;
225 << duration_cast<seconds>(FileAge).
count() <<
"s old)\n");
231 AddToFileListForSizePruning(
File->path());
235 if (ShouldComputeSize) {
237 if (!ErrOrSpaceInfo) {
241 auto AvailableSpace = TotalSize + SpaceInfo.
free;
247 auto TotalSizeTarget = std::min<uint64_t>(
251 DEBUG(
dbgs() <<
"Occupancy: " << ((100 * TotalSize) / AvailableSpace)
255 auto FileAndSize = FileSizes.rbegin();
257 while (TotalSize > TotalSizeTarget && FileAndSize != FileSizes.rend()) {
261 TotalSize -= FileAndSize->first;
262 DEBUG(
dbgs() <<
" - Remove " << FileAndSize->second <<
" (size "
263 << FileAndSize->first <<
"), new occupancy is " << TotalSize
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.
uint64_t MaxSizeBytes
The maximum size for the cache directory in bytes.
void native(const Twine &path, SmallVectorImpl< char > &result, Style style=Style::native)
Convert path to the native form.
space_info - Self explanatory.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
std::chrono::seconds Expiration
The expiration for a file.
std::chrono::seconds Interval
The pruning interval.
LLVM_NODISCARD char back() const
back - Get the last character in the string.
file_status - Represents the result of a call to stat and friends.
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 the di...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
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(std::begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
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().
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
bool pruneCache(StringRef Path, CachePruningPolicy Policy)
Peform pruning using the supplied policy, returns true if pruning occured, i.e.
static Expected< std::chrono::seconds > parseDuration(StringRef Duration)
bool is_directory(file_status status)
Does status represent a directory?
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
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 bool empty() const
empty - Check if the string is empty.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
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.
StringRef - Represent a constant reference to a string, i.e.
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::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...