2016-04-02 03:28:26 +00:00
|
|
|
//===-CachePruning.cpp - LLVM Cache Directory Pruning ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the pruning of a directory based on least recently used.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/CachePruning.h"
|
|
|
|
|
2016-04-18 21:54:00 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2016-05-14 14:21:39 +00:00
|
|
|
#include "llvm/Support/Errc.h"
|
2016-04-02 03:28:26 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2016-04-18 21:54:00 +00:00
|
|
|
#define DEBUG_TYPE "cache-pruning"
|
|
|
|
|
2016-04-02 03:28:26 +00:00
|
|
|
#include <set>
|
2016-09-14 08:55:18 +00:00
|
|
|
#include <system_error>
|
2016-04-02 03:28:26 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// Write a new timestamp file with the given path. This is used for the pruning
|
|
|
|
/// interval option.
|
|
|
|
static void writeTimestampFile(StringRef TimestampFile) {
|
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream Out(TimestampFile.str(), EC, sys::fs::F_None);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prune the cache of files that haven't been accessed in a long time.
|
|
|
|
bool CachePruning::prune() {
|
2016-10-24 10:59:17 +00:00
|
|
|
using namespace std::chrono;
|
|
|
|
|
2016-04-21 06:43:45 +00:00
|
|
|
if (Path.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool isPathDir;
|
|
|
|
if (sys::fs::is_directory(Path, isPathDir))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!isPathDir)
|
|
|
|
return false;
|
2016-04-02 03:28:26 +00:00
|
|
|
|
2016-10-24 10:59:17 +00:00
|
|
|
if (Expiration == seconds(0) && PercentageOfAvailableSpace == 0) {
|
2016-04-18 21:54:00 +00:00
|
|
|
DEBUG(dbgs() << "No pruning settings set, exit early\n");
|
2016-04-02 03:28:26 +00:00
|
|
|
// Nothing will be pruned, early exit
|
|
|
|
return false;
|
2016-04-18 21:54:00 +00:00
|
|
|
}
|
2016-04-02 03:28:26 +00:00
|
|
|
|
|
|
|
// Try to stat() the timestamp file.
|
2016-04-21 06:43:45 +00:00
|
|
|
SmallString<128> TimestampFile(Path);
|
|
|
|
sys::path::append(TimestampFile, "llvmcache.timestamp");
|
2016-04-02 03:28:26 +00:00
|
|
|
sys::fs::file_status FileStatus;
|
2016-10-24 10:59:17 +00:00
|
|
|
const auto CurrentTime = system_clock::now();
|
2016-05-14 14:21:39 +00:00
|
|
|
if (auto EC = sys::fs::status(TimestampFile, FileStatus)) {
|
|
|
|
if (EC == errc::no_such_file_or_directory) {
|
2016-04-02 03:28:26 +00:00
|
|
|
// If the timestamp file wasn't there, create one now.
|
|
|
|
writeTimestampFile(TimestampFile);
|
|
|
|
} else {
|
|
|
|
// Unknown error?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2016-10-24 10:59:17 +00:00
|
|
|
if (Interval == seconds(0)) {
|
2016-04-02 03:28:26 +00:00
|
|
|
// Check whether the time stamp is older than our pruning interval.
|
|
|
|
// If not, do nothing.
|
2016-10-24 10:59:17 +00:00
|
|
|
const auto TimeStampModTime = FileStatus.getLastModificationTime();
|
2016-04-18 21:54:00 +00:00
|
|
|
auto TimeStampAge = CurrentTime - TimeStampModTime;
|
2016-10-24 10:59:17 +00:00
|
|
|
if (TimeStampAge <= Interval) {
|
|
|
|
DEBUG(dbgs() << "Timestamp file too recent ("
|
|
|
|
<< duration_cast<seconds>(TimeStampAge).count()
|
2016-04-18 21:54:00 +00:00
|
|
|
<< "s old), do not prune.\n");
|
2016-04-02 03:28:26 +00:00
|
|
|
return false;
|
2016-04-18 21:54:00 +00:00
|
|
|
}
|
2016-04-02 03:28:26 +00:00
|
|
|
}
|
|
|
|
// Write a new timestamp file so that nobody else attempts to prune.
|
|
|
|
// There is a benign race condition here, if two processes happen to
|
|
|
|
// notice at the same time that the timestamp is out-of-date.
|
|
|
|
writeTimestampFile(TimestampFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShouldComputeSize = (PercentageOfAvailableSpace > 0);
|
|
|
|
|
|
|
|
// Keep track of space
|
|
|
|
std::set<std::pair<uint64_t, std::string>> FileSizes;
|
|
|
|
uint64_t TotalSize = 0;
|
|
|
|
// Helper to add a path to the set of files to consider for size-based
|
2016-06-27 08:46:23 +00:00
|
|
|
// pruning, sorted by size.
|
2016-04-02 03:28:26 +00:00
|
|
|
auto AddToFileListForSizePruning =
|
2016-06-27 08:46:23 +00:00
|
|
|
[&](StringRef Path) {
|
2016-04-02 03:28:26 +00:00
|
|
|
if (!ShouldComputeSize)
|
|
|
|
return;
|
|
|
|
TotalSize += FileStatus.getSize();
|
|
|
|
FileSizes.insert(
|
2016-04-18 21:53:55 +00:00
|
|
|
std::make_pair(FileStatus.getSize(), std::string(Path)));
|
2016-04-02 03:28:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Walk the entire directory cache, looking for unused files.
|
|
|
|
std::error_code EC;
|
|
|
|
SmallString<128> CachePathNative;
|
|
|
|
sys::path::native(Path, CachePathNative);
|
|
|
|
// Walk all of the files within this directory.
|
|
|
|
for (sys::fs::directory_iterator File(CachePathNative, EC), FileEnd;
|
|
|
|
File != FileEnd && !EC; File.increment(EC)) {
|
|
|
|
// Do not touch the timestamp.
|
|
|
|
if (File->path() == TimestampFile)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Look at this file. If we can't stat it, there's nothing interesting
|
|
|
|
// there.
|
2016-04-18 21:54:00 +00:00
|
|
|
if (sys::fs::status(File->path(), FileStatus)) {
|
|
|
|
DEBUG(dbgs() << "Ignore " << File->path() << " (can't stat)\n");
|
2016-04-02 03:28:26 +00:00
|
|
|
continue;
|
2016-04-18 21:54:00 +00:00
|
|
|
}
|
2016-04-02 03:28:26 +00:00
|
|
|
|
|
|
|
// If the file hasn't been used recently enough, delete it
|
2016-10-24 10:59:17 +00:00
|
|
|
const auto FileAccessTime = FileStatus.getLastAccessedTime();
|
2016-04-18 21:54:00 +00:00
|
|
|
auto FileAge = CurrentTime - FileAccessTime;
|
2016-10-24 10:59:17 +00:00
|
|
|
if (FileAge > Expiration) {
|
|
|
|
DEBUG(dbgs() << "Remove " << File->path() << " ("
|
|
|
|
<< duration_cast<seconds>(FileAge).count() << "s old)\n");
|
2016-04-02 03:28:26 +00:00
|
|
|
sys::fs::remove(File->path());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave it here for now, but add it to the list of size-based pruning.
|
2016-06-27 08:46:23 +00:00
|
|
|
AddToFileListForSizePruning(File->path());
|
2016-04-02 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prune for size now if needed
|
|
|
|
if (ShouldComputeSize) {
|
|
|
|
auto ErrOrSpaceInfo = sys::fs::disk_space(Path);
|
|
|
|
if (!ErrOrSpaceInfo) {
|
|
|
|
report_fatal_error("Can't get available size");
|
|
|
|
}
|
|
|
|
sys::fs::space_info SpaceInfo = ErrOrSpaceInfo.get();
|
|
|
|
auto AvailableSpace = TotalSize + SpaceInfo.free;
|
|
|
|
auto FileAndSize = FileSizes.rbegin();
|
2016-04-18 21:54:00 +00:00
|
|
|
DEBUG(dbgs() << "Occupancy: " << ((100 * TotalSize) / AvailableSpace)
|
|
|
|
<< "% target is: " << PercentageOfAvailableSpace << "\n");
|
2016-04-02 03:28:26 +00:00
|
|
|
// Remove the oldest accessed files first, till we get below the threshold
|
|
|
|
while (((100 * TotalSize) / AvailableSpace) > PercentageOfAvailableSpace &&
|
|
|
|
FileAndSize != FileSizes.rend()) {
|
|
|
|
// Remove the file.
|
|
|
|
sys::fs::remove(FileAndSize->second);
|
|
|
|
// Update size
|
|
|
|
TotalSize -= FileAndSize->first;
|
2016-04-18 21:54:00 +00:00
|
|
|
DEBUG(dbgs() << " - Remove " << FileAndSize->second << " (size "
|
|
|
|
<< FileAndSize->first << "), new occupancy is " << TotalSize
|
|
|
|
<< "%\n");
|
2016-04-02 03:28:26 +00:00
|
|
|
++FileAndSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|