2016-04-02 05:28:26 +02:00
|
|
|
//===-CachePruning.cpp - LLVM Cache Directory Pruning ---------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-04-02 05:28:26 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the pruning of a directory based on least recently used.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/CachePruning.h"
|
2020-05-08 14:15:49 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2016-04-18 23:54:00 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
2016-05-14 16:21:39 +02:00
|
|
|
#include "llvm/Support/Errc.h"
|
2017-03-16 04:42:00 +01:00
|
|
|
#include "llvm/Support/Error.h"
|
2016-04-02 05:28:26 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2016-04-18 23:54:00 +02:00
|
|
|
#define DEBUG_TYPE "cache-pruning"
|
|
|
|
|
2016-04-02 05:28:26 +02:00
|
|
|
#include <set>
|
2016-09-14 10:55:18 +02:00
|
|
|
#include <system_error>
|
2016-04-02 05:28:26 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2018-08-22 02:52:16 +02:00
|
|
|
namespace {
|
|
|
|
struct FileInfo {
|
|
|
|
sys::TimePoint<> Time;
|
|
|
|
uint64_t Size;
|
|
|
|
std::string Path;
|
|
|
|
|
|
|
|
/// Used to determine which files to prune first. Also used to determine
|
|
|
|
/// set membership, so must take into account all fields.
|
|
|
|
bool operator<(const FileInfo &Other) const {
|
2019-04-21 08:17:40 +02:00
|
|
|
return std::tie(Time, Other.Size, Path) <
|
|
|
|
std::tie(Other.Time, Size, Other.Path);
|
2018-08-22 02:52:16 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-04-02 05:28:26 +02:00
|
|
|
/// 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;
|
2019-08-05 07:43:48 +02:00
|
|
|
raw_fd_ostream Out(TimestampFile.str(), EC, sys::fs::OF_None);
|
2016-04-02 05:28:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-16 04:42:00 +01:00
|
|
|
static Expected<std::chrono::seconds> parseDuration(StringRef Duration) {
|
|
|
|
if (Duration.empty())
|
|
|
|
return make_error<StringError>("Duration must not be empty",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
|
|
|
|
StringRef NumStr = Duration.slice(0, Duration.size()-1);
|
|
|
|
uint64_t Num;
|
|
|
|
if (NumStr.getAsInteger(0, Num))
|
|
|
|
return make_error<StringError>("'" + NumStr + "' not an integer",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
|
|
|
|
switch (Duration.back()) {
|
|
|
|
case 's':
|
|
|
|
return std::chrono::seconds(Num);
|
|
|
|
case 'm':
|
|
|
|
return std::chrono::minutes(Num);
|
|
|
|
case 'h':
|
|
|
|
return std::chrono::hours(Num);
|
|
|
|
default:
|
|
|
|
return make_error<StringError>("'" + Duration +
|
|
|
|
"' must end with one of 's', 'm' or 'h'",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Expected<CachePruningPolicy>
|
|
|
|
llvm::parseCachePruningPolicy(StringRef PolicyStr) {
|
|
|
|
CachePruningPolicy Policy;
|
|
|
|
std::pair<StringRef, StringRef> P = {"", PolicyStr};
|
|
|
|
while (!P.second.empty()) {
|
|
|
|
P = P.second.split(':');
|
|
|
|
|
|
|
|
StringRef Key, Value;
|
|
|
|
std::tie(Key, Value) = P.first.split('=');
|
|
|
|
if (Key == "prune_interval") {
|
|
|
|
auto DurationOrErr = parseDuration(Value);
|
|
|
|
if (!DurationOrErr)
|
2017-03-16 04:54:38 +01:00
|
|
|
return DurationOrErr.takeError();
|
2017-03-16 04:42:00 +01:00
|
|
|
Policy.Interval = *DurationOrErr;
|
|
|
|
} else if (Key == "prune_after") {
|
|
|
|
auto DurationOrErr = parseDuration(Value);
|
|
|
|
if (!DurationOrErr)
|
2017-03-16 04:54:38 +01:00
|
|
|
return DurationOrErr.takeError();
|
2017-03-16 04:42:00 +01:00
|
|
|
Policy.Expiration = *DurationOrErr;
|
|
|
|
} else if (Key == "cache_size") {
|
2017-06-23 19:17:47 +02:00
|
|
|
if (Value.back() != '%')
|
|
|
|
return make_error<StringError>("'" + Value + "' must be a percentage",
|
|
|
|
inconvertibleErrorCode());
|
2017-06-23 19:05:03 +02:00
|
|
|
StringRef SizeStr = Value.drop_back();
|
2017-03-16 04:42:00 +01:00
|
|
|
uint64_t Size;
|
|
|
|
if (SizeStr.getAsInteger(0, Size))
|
|
|
|
return make_error<StringError>("'" + SizeStr + "' not an integer",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
if (Size > 100)
|
|
|
|
return make_error<StringError>("'" + SizeStr +
|
|
|
|
"' must be between 0 and 100",
|
|
|
|
inconvertibleErrorCode());
|
2017-06-23 19:05:03 +02:00
|
|
|
Policy.MaxSizePercentageOfAvailableSpace = Size;
|
|
|
|
} else if (Key == "cache_size_bytes") {
|
|
|
|
uint64_t Mult = 1;
|
2017-06-23 19:13:51 +02:00
|
|
|
switch (tolower(Value.back())) {
|
2017-06-23 19:05:03 +02:00
|
|
|
case 'k':
|
|
|
|
Mult = 1024;
|
|
|
|
Value = Value.drop_back();
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
Mult = 1024 * 1024;
|
|
|
|
Value = Value.drop_back();
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
Mult = 1024 * 1024 * 1024;
|
|
|
|
Value = Value.drop_back();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uint64_t Size;
|
|
|
|
if (Value.getAsInteger(0, Size))
|
|
|
|
return make_error<StringError>("'" + Value + "' not an integer",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
Policy.MaxSizeBytes = Size * Mult;
|
2017-11-22 19:27:31 +01:00
|
|
|
} else if (Key == "cache_size_files") {
|
|
|
|
if (Value.getAsInteger(0, Policy.MaxSizeFiles))
|
|
|
|
return make_error<StringError>("'" + Value + "' not an integer",
|
|
|
|
inconvertibleErrorCode());
|
2017-03-16 04:42:00 +01:00
|
|
|
} else {
|
|
|
|
return make_error<StringError>("Unknown key: '" + Key + "'",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Policy;
|
|
|
|
}
|
|
|
|
|
2016-04-02 05:28:26 +02:00
|
|
|
/// Prune the cache of files that haven't been accessed in a long time.
|
2017-03-15 23:54:18 +01:00
|
|
|
bool llvm::pruneCache(StringRef Path, CachePruningPolicy Policy) {
|
2016-10-24 12:59:17 +02:00
|
|
|
using namespace std::chrono;
|
|
|
|
|
2016-04-21 08:43:45 +02:00
|
|
|
if (Path.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool isPathDir;
|
|
|
|
if (sys::fs::is_directory(Path, isPathDir))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!isPathDir)
|
|
|
|
return false;
|
2016-04-02 05:28:26 +02:00
|
|
|
|
2017-06-23 19:05:03 +02:00
|
|
|
Policy.MaxSizePercentageOfAvailableSpace =
|
|
|
|
std::min(Policy.MaxSizePercentageOfAvailableSpace, 100u);
|
2017-03-15 23:54:18 +01:00
|
|
|
|
|
|
|
if (Policy.Expiration == seconds(0) &&
|
2017-06-23 19:05:03 +02:00
|
|
|
Policy.MaxSizePercentageOfAvailableSpace == 0 &&
|
2017-11-22 19:27:31 +01:00
|
|
|
Policy.MaxSizeBytes == 0 && Policy.MaxSizeFiles == 0) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "No pruning settings set, exit early\n");
|
2016-04-02 05:28:26 +02:00
|
|
|
// Nothing will be pruned, early exit
|
|
|
|
return false;
|
2016-04-18 23:54:00 +02:00
|
|
|
}
|
2016-04-02 05:28:26 +02:00
|
|
|
|
|
|
|
// Try to stat() the timestamp file.
|
2016-04-21 08:43:45 +02:00
|
|
|
SmallString<128> TimestampFile(Path);
|
|
|
|
sys::path::append(TimestampFile, "llvmcache.timestamp");
|
2016-04-02 05:28:26 +02:00
|
|
|
sys::fs::file_status FileStatus;
|
2017-12-22 19:32:15 +01:00
|
|
|
const auto CurrentTime = system_clock::now();
|
2016-05-14 16:21:39 +02:00
|
|
|
if (auto EC = sys::fs::status(TimestampFile, FileStatus)) {
|
|
|
|
if (EC == errc::no_such_file_or_directory) {
|
2016-04-02 05:28:26 +02:00
|
|
|
// If the timestamp file wasn't there, create one now.
|
|
|
|
writeTimestampFile(TimestampFile);
|
|
|
|
} else {
|
|
|
|
// Unknown error?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-22 19:32:15 +01:00
|
|
|
if (!Policy.Interval)
|
|
|
|
return false;
|
2017-11-17 15:42:18 +01:00
|
|
|
if (Policy.Interval != seconds(0)) {
|
2016-04-02 05:28:26 +02:00
|
|
|
// Check whether the time stamp is older than our pruning interval.
|
|
|
|
// If not, do nothing.
|
2017-12-22 19:32:15 +01:00
|
|
|
const auto TimeStampModTime = FileStatus.getLastModificationTime();
|
2016-04-18 23:54:00 +02:00
|
|
|
auto TimeStampAge = CurrentTime - TimeStampModTime;
|
2017-12-22 19:32:15 +01:00
|
|
|
if (TimeStampAge <= *Policy.Interval) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Timestamp file too recent ("
|
|
|
|
<< duration_cast<seconds>(TimeStampAge).count()
|
|
|
|
<< "s old), do not prune.\n");
|
2016-04-02 05:28:26 +02:00
|
|
|
return false;
|
2016-04-18 23:54:00 +02:00
|
|
|
}
|
2016-04-02 05:28:26 +02: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);
|
|
|
|
}
|
|
|
|
|
2018-08-22 02:52:16 +02:00
|
|
|
// Keep track of files to delete to get below the size limit.
|
|
|
|
// Order by time of last use so that recently used files are preserved.
|
|
|
|
std::set<FileInfo> FileInfos;
|
2016-04-02 05:28:26 +02:00
|
|
|
uint64_t TotalSize = 0;
|
|
|
|
|
|
|
|
// 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)) {
|
2021-01-19 13:41:41 +01:00
|
|
|
// Ignore filenames not beginning with "llvmcache-" or "Thin-". This
|
2017-03-20 17:41:57 +01:00
|
|
|
// includes the timestamp file as well as any files created by the user.
|
|
|
|
// This acts as a safeguard against data loss if the user specifies the
|
|
|
|
// wrong directory as their cache directory.
|
2021-01-19 13:41:41 +01:00
|
|
|
StringRef filename = sys::path::filename(File->path());
|
|
|
|
if (!filename.startswith("llvmcache-") && !filename.startswith("Thin-"))
|
2016-04-02 05:28:26 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Look at this file. If we can't stat it, there's nothing interesting
|
|
|
|
// there.
|
2017-10-11 00:19:46 +02:00
|
|
|
ErrorOr<sys::fs::basic_file_status> StatusOrErr = File->status();
|
|
|
|
if (!StatusOrErr) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Ignore " << File->path() << " (can't stat)\n");
|
2016-04-02 05:28:26 +02:00
|
|
|
continue;
|
2016-04-18 23:54:00 +02:00
|
|
|
}
|
2016-04-02 05:28:26 +02:00
|
|
|
|
|
|
|
// If the file hasn't been used recently enough, delete it
|
2017-10-11 00:19:46 +02:00
|
|
|
const auto FileAccessTime = StatusOrErr->getLastAccessedTime();
|
2016-04-18 23:54:00 +02:00
|
|
|
auto FileAge = CurrentTime - FileAccessTime;
|
2017-11-22 19:27:31 +01:00
|
|
|
if (Policy.Expiration != seconds(0) && FileAge > Policy.Expiration) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Remove " << File->path() << " ("
|
|
|
|
<< duration_cast<seconds>(FileAge).count()
|
|
|
|
<< "s old)\n");
|
2016-04-02 05:28:26 +02:00
|
|
|
sys::fs::remove(File->path());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave it here for now, but add it to the list of size-based pruning.
|
2017-10-11 00:19:46 +02:00
|
|
|
TotalSize += StatusOrErr->getSize();
|
2018-08-22 02:52:16 +02:00
|
|
|
FileInfos.insert({FileAccessTime, StatusOrErr->getSize(), File->path()});
|
2016-04-02 05:28:26 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 02:52:16 +02:00
|
|
|
auto FileInfo = FileInfos.begin();
|
|
|
|
size_t NumFiles = FileInfos.size();
|
2017-11-22 19:27:31 +01:00
|
|
|
|
|
|
|
auto RemoveCacheFile = [&]() {
|
|
|
|
// Remove the file.
|
2018-08-22 02:52:16 +02:00
|
|
|
sys::fs::remove(FileInfo->Path);
|
2017-11-22 19:27:31 +01:00
|
|
|
// Update size
|
2018-08-22 02:52:16 +02:00
|
|
|
TotalSize -= FileInfo->Size;
|
2017-11-22 19:27:31 +01:00
|
|
|
NumFiles--;
|
2018-08-22 02:52:16 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " - Remove " << FileInfo->Path << " (size "
|
|
|
|
<< FileInfo->Size << "), new occupancy is " << TotalSize
|
|
|
|
<< "%\n");
|
|
|
|
++FileInfo;
|
2017-11-22 19:27:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Prune for number of files.
|
|
|
|
if (Policy.MaxSizeFiles)
|
|
|
|
while (NumFiles > Policy.MaxSizeFiles)
|
|
|
|
RemoveCacheFile();
|
|
|
|
|
2016-04-02 05:28:26 +02:00
|
|
|
// Prune for size now if needed
|
2017-11-22 19:27:31 +01:00
|
|
|
if (Policy.MaxSizePercentageOfAvailableSpace > 0 || Policy.MaxSizeBytes > 0) {
|
2016-04-02 05:28:26 +02:00
|
|
|
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;
|
2017-06-23 19:05:03 +02:00
|
|
|
|
|
|
|
if (Policy.MaxSizePercentageOfAvailableSpace == 0)
|
|
|
|
Policy.MaxSizePercentageOfAvailableSpace = 100;
|
|
|
|
if (Policy.MaxSizeBytes == 0)
|
|
|
|
Policy.MaxSizeBytes = AvailableSpace;
|
|
|
|
auto TotalSizeTarget = std::min<uint64_t>(
|
|
|
|
AvailableSpace * Policy.MaxSizePercentageOfAvailableSpace / 100ull,
|
|
|
|
Policy.MaxSizeBytes);
|
|
|
|
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Occupancy: " << ((100 * TotalSize) / AvailableSpace)
|
|
|
|
<< "% target is: "
|
|
|
|
<< Policy.MaxSizePercentageOfAvailableSpace << "%, "
|
|
|
|
<< Policy.MaxSizeBytes << " bytes\n");
|
2017-06-23 19:05:03 +02:00
|
|
|
|
2017-11-22 19:27:31 +01:00
|
|
|
// Remove the oldest accessed files first, till we get below the threshold.
|
2018-08-22 02:52:16 +02:00
|
|
|
while (TotalSize > TotalSizeTarget && FileInfo != FileInfos.end())
|
2017-11-22 19:27:31 +01:00
|
|
|
RemoveCacheFile();
|
2016-04-02 05:28:26 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|