|
@@ -34,7 +34,7 @@ static void writeTimestampFile(StringRef TimestampFile) {
|
|
}
|
|
}
|
|
|
|
|
|
/// Prune the cache of files that haven't been accessed in a long time.
|
|
/// Prune the cache of files that haven't been accessed in a long time.
|
|
-bool CachePruning::prune() {
|
|
|
|
|
|
+bool llvm::pruneCache(StringRef Path, CachePruningPolicy Policy) {
|
|
using namespace std::chrono;
|
|
using namespace std::chrono;
|
|
|
|
|
|
if (Path.empty())
|
|
if (Path.empty())
|
|
@@ -47,7 +47,11 @@ bool CachePruning::prune() {
|
|
if (!isPathDir)
|
|
if (!isPathDir)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
- if (Expiration == seconds(0) && PercentageOfAvailableSpace == 0) {
|
|
|
|
|
|
+ Policy.PercentageOfAvailableSpace =
|
|
|
|
+ std::min(Policy.PercentageOfAvailableSpace, 100u);
|
|
|
|
+
|
|
|
|
+ if (Policy.Expiration == seconds(0) &&
|
|
|
|
+ Policy.PercentageOfAvailableSpace == 0) {
|
|
DEBUG(dbgs() << "No pruning settings set, exit early\n");
|
|
DEBUG(dbgs() << "No pruning settings set, exit early\n");
|
|
// Nothing will be pruned, early exit
|
|
// Nothing will be pruned, early exit
|
|
return false;
|
|
return false;
|
|
@@ -67,12 +71,12 @@ bool CachePruning::prune() {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- if (Interval == seconds(0)) {
|
|
|
|
|
|
+ if (Policy.Interval == seconds(0)) {
|
|
// Check whether the time stamp is older than our pruning interval.
|
|
// Check whether the time stamp is older than our pruning interval.
|
|
// If not, do nothing.
|
|
// If not, do nothing.
|
|
const auto TimeStampModTime = FileStatus.getLastModificationTime();
|
|
const auto TimeStampModTime = FileStatus.getLastModificationTime();
|
|
auto TimeStampAge = CurrentTime - TimeStampModTime;
|
|
auto TimeStampAge = CurrentTime - TimeStampModTime;
|
|
- if (TimeStampAge <= Interval) {
|
|
|
|
|
|
+ if (TimeStampAge <= Policy.Interval) {
|
|
DEBUG(dbgs() << "Timestamp file too recent ("
|
|
DEBUG(dbgs() << "Timestamp file too recent ("
|
|
<< duration_cast<seconds>(TimeStampAge).count()
|
|
<< duration_cast<seconds>(TimeStampAge).count()
|
|
<< "s old), do not prune.\n");
|
|
<< "s old), do not prune.\n");
|
|
@@ -85,7 +89,7 @@ bool CachePruning::prune() {
|
|
writeTimestampFile(TimestampFile);
|
|
writeTimestampFile(TimestampFile);
|
|
}
|
|
}
|
|
|
|
|
|
- bool ShouldComputeSize = (PercentageOfAvailableSpace > 0);
|
|
|
|
|
|
+ bool ShouldComputeSize = (Policy.PercentageOfAvailableSpace > 0);
|
|
|
|
|
|
// Keep track of space
|
|
// Keep track of space
|
|
std::set<std::pair<uint64_t, std::string>> FileSizes;
|
|
std::set<std::pair<uint64_t, std::string>> FileSizes;
|
|
@@ -122,7 +126,7 @@ bool CachePruning::prune() {
|
|
// If the file hasn't been used recently enough, delete it
|
|
// If the file hasn't been used recently enough, delete it
|
|
const auto FileAccessTime = FileStatus.getLastAccessedTime();
|
|
const auto FileAccessTime = FileStatus.getLastAccessedTime();
|
|
auto FileAge = CurrentTime - FileAccessTime;
|
|
auto FileAge = CurrentTime - FileAccessTime;
|
|
- if (FileAge > Expiration) {
|
|
|
|
|
|
+ if (FileAge > Policy.Expiration) {
|
|
DEBUG(dbgs() << "Remove " << File->path() << " ("
|
|
DEBUG(dbgs() << "Remove " << File->path() << " ("
|
|
<< duration_cast<seconds>(FileAge).count() << "s old)\n");
|
|
<< duration_cast<seconds>(FileAge).count() << "s old)\n");
|
|
sys::fs::remove(File->path());
|
|
sys::fs::remove(File->path());
|
|
@@ -143,9 +147,11 @@ bool CachePruning::prune() {
|
|
auto AvailableSpace = TotalSize + SpaceInfo.free;
|
|
auto AvailableSpace = TotalSize + SpaceInfo.free;
|
|
auto FileAndSize = FileSizes.rbegin();
|
|
auto FileAndSize = FileSizes.rbegin();
|
|
DEBUG(dbgs() << "Occupancy: " << ((100 * TotalSize) / AvailableSpace)
|
|
DEBUG(dbgs() << "Occupancy: " << ((100 * TotalSize) / AvailableSpace)
|
|
- << "% target is: " << PercentageOfAvailableSpace << "\n");
|
|
|
|
|
|
+ << "% target is: " << Policy.PercentageOfAvailableSpace
|
|
|
|
+ << "\n");
|
|
// Remove the oldest accessed files first, till we get below the threshold
|
|
// Remove the oldest accessed files first, till we get below the threshold
|
|
- while (((100 * TotalSize) / AvailableSpace) > PercentageOfAvailableSpace &&
|
|
|
|
|
|
+ while (((100 * TotalSize) / AvailableSpace) >
|
|
|
|
+ Policy.PercentageOfAvailableSpace &&
|
|
FileAndSize != FileSizes.rend()) {
|
|
FileAndSize != FileSizes.rend()) {
|
|
// Remove the file.
|
|
// Remove the file.
|
|
sys::fs::remove(FileAndSize->second);
|
|
sys::fs::remove(FileAndSize->second);
|