22 #define DEBUG_TYPE "cache-pruning"
25 #include <system_error>
38 using namespace std::chrono;
50 if (Expiration == seconds(0) && PercentageOfAvailableSpace == 0) {
51 DEBUG(
dbgs() <<
"No pruning settings set, exit early\n");
73 const auto TimeStampModTime = FileStatus.getLastModificationTime();
74 auto TimeStampAge = CurrentTime - TimeStampModTime;
76 DEBUG(
dbgs() <<
"Timestamp file too recent ("
77 << duration_cast<seconds>(TimeStampAge).
count()
78 <<
"s old), do not prune.\n");
88 bool ShouldComputeSize = (PercentageOfAvailableSpace > 0);
91 std::set<std::pair<uint64_t, std::string>> FileSizes;
92 uint64_t TotalSize = 0;
95 auto AddToFileListForSizePruning =
97 if (!ShouldComputeSize)
99 TotalSize += FileStatus.getSize();
101 std::make_pair(FileStatus.getSize(), std::string(Path)));
110 File != FileEnd && !EC;
File.increment(EC)) {
112 if (
File->path() == TimestampFile)
118 DEBUG(
dbgs() <<
"Ignore " <<
File->path() <<
" (can't stat)\n");
123 const auto FileAccessTime = FileStatus.getLastAccessedTime();
124 auto FileAge = CurrentTime - FileAccessTime;
125 if (FileAge > Expiration) {
127 << duration_cast<seconds>(FileAge).
count() <<
"s old)\n");
133 AddToFileListForSizePruning(
File->path());
137 if (ShouldComputeSize) {
139 if (!ErrOrSpaceInfo) {
143 auto AvailableSpace = TotalSize + SpaceInfo.
free;
144 auto FileAndSize = FileSizes.rbegin();
145 DEBUG(
dbgs() <<
"Occupancy: " << ((100 * TotalSize) / AvailableSpace)
146 <<
"% target is: " << PercentageOfAvailableSpace <<
"\n");
148 while (((100 * TotalSize) / AvailableSpace) > PercentageOfAvailableSpace &&
149 FileAndSize != FileSizes.rend()) {
153 TotalSize -= FileAndSize->first;
154 DEBUG(
dbgs() <<
" - Remove " << FileAndSize->second <<
" (size "
155 << FileAndSize->first <<
"), new occupancy is " << TotalSize
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.
Interval Class - An Interval is a set of nodes defined such that every node in the interval has all o...
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
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.
void native(const Twine &path, SmallVectorImpl< char > &result)
Convert path to the native form.
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...
static void writeTimestampFile(StringRef TimestampFile)
Write a new timestamp file with the given path.
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
bool prune()
Peform pruning using the supplied options, returns true if pruning occured, i.e.
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.
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 - Represent a constant reference to a string, i.e.
std::error_code status(const Twine &path, file_status &result)
Get file status as if by POSIX stat().