Index: include/llvm/Support/CachePruning.h =================================================================== --- /dev/null +++ include/llvm/Support/CachePruning.h @@ -0,0 +1,71 @@ +//=- CachePruning.h - Helper to nanage the pruning of a cache dir -*- C++ -*-=// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements pruning of a directory intended for cache storage, using +// various policies. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_CACHE_PRUNING_H +#define LLVM_SUPPORT_CACHE_PRUNING_H + +#include "llvm/ADT/StringRef.h" + + +namespace llvm { + +/// Handle pruning a directory provided a path and some options to control what +/// to prune. +class CachePruning { +public: + /// Prepare to prune \p Path. + CachePruning(StringRef Path) : Path(Path) {} + + /// Define the pruning interval. This is intended to be used to avoid scanning + /// the directory too often. It does not impact the decision of which file to + /// prune. A value of 0 forces the scan to occurs. + CachePruning &setPruningInterval(int PruningInterval) { + Interval = PruningInterval; + return *this; + } + + /// Define the expiration for a file. When a file hasn't been accessed for + /// \p ExpireAfter seconds, it is removed from the cache. A value of 0 disable + /// the expiration-based pruning. + CachePruning &setEntryExpiration(unsigned ExpireAfter) { + Expiration = ExpireAfter; + return *this; + } + + /// Define the maximum size for the cache directory, in terms of percentage of + /// the available space on the the disk. Set to 100 to indicate no limit, 50 + /// to indicate that the cache size will not be left over half the + /// available disk space. A value over 100 will be reduced to 100. A value of + /// 0 disable the size-based pruning. + CachePruning &setMaxSize(unsigned Percentage) { + PercentageOfAvailableSpace = std::min(100u, Percentage); + return *this; + } + + /// Peform pruning using the supplied options, returns true if pruning + /// occured, i.e. if PruningInterval was expired. + bool prune(); + +private: + + // Options that matches the setters above. + std::string Path; + unsigned Expiration = 0; + unsigned Interval = 0; + unsigned PercentageOfAvailableSpace = 0; +}; + +} // namespace llvm + +#endif \ No newline at end of file Index: lib/Support/CMakeLists.txt =================================================================== --- lib/Support/CMakeLists.txt +++ lib/Support/CMakeLists.txt @@ -35,6 +35,7 @@ Allocator.cpp BlockFrequency.cpp BranchProbability.cpp + CachePruning.cpp circular_raw_ostream.cpp COM.cpp CommandLine.cpp Index: lib/Support/CachePruning.cpp =================================================================== --- /dev/null +++ lib/Support/CachePruning.cpp @@ -0,0 +1,134 @@ +//===-CachePruning.cpp - LLVM Cache Director 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 Thin Link Time Optimization library. This library is +// intended to be used by linker to optimize code at link time. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/CachePruning.h" + +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/raw_ostream.h" + +#include +#include +#include +#if defined(__APPLE__) +#include +#else +#include +#endif + + +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; + llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::F_None); +} + +/// Prune the cache of files that haven't been accessed in a long time. +bool CachePruning::prune() { + llvm::SmallString<128> TimestampFile(Path); + llvm::sys::path::append(TimestampFile, "llvmcache.timestamp"); + + if (Expiration == 0 && PercentageOfAvailableSpace == 0) + // Nothing will be pruned, early exit + return false; + + // Try to stat() the timestamp file. + struct stat StatBuf; + time_t CurrentTime = time(nullptr); + StatBuf.st_mtime = 0; + if (::stat(TimestampFile.c_str(), &StatBuf)) { + if (errno == ENOENT) { + // If the timestamp file wasn't there, create one now. + writeTimestampFile(TimestampFile); + } else { + // Unknown error? + return false; + } + } else { + if (Interval) { + // Check whether the time stamp is older than our pruning interval. + // If not, do nothing. + time_t TimeStampModTime = StatBuf.st_mtime; + if (CurrentTime - TimeStampModTime <= time_t(Interval)) + return false; + } + // 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> FileSizes; + uint64_t TotalSize = 0; + // Helper to add a path to the set of files to consider for size-based + // pruning, sorted by last accessed time. + auto AddToFileListForSizePruning = [&] (StringRef Path, + time_t FileAccessTime) { + if (!ShouldComputeSize) + return; + TotalSize += StatBuf.st_size; + FileSizes.insert(std::make_pair(FileAccessTime, std::string(Path))); + }; + + // Walk the entire directory cache, looking for unused files. + std::error_code EC; + SmallString<128> CachePathNative; + llvm::sys::path::native(Path, CachePathNative); + // Walk all of the files within this directory. + for (llvm::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. + if (::stat(File->path().c_str(), &StatBuf)) + continue; + + // If the file has been used recently enough, leave it there. + time_t FileAccessTime = StatBuf.st_atime; + if (CurrentTime - FileAccessTime <= time_t(Expiration)) { + AddToFileListForSizePruning(File->path(), FileAccessTime); + continue; + } + + // Remove the file. + llvm::sys::fs::remove(File->path()); + } + + // Prune for size now if needed + if (ShouldComputeSize) { + struct statfs statf; + statfs(Path.data(), &statf); + auto AvailableSpace = TotalSize + ((uint64_t)statf.f_bfree) * statf.f_bsize; + auto FileAndSize = FileSizes.rbegin(); + // Remove the oldest accessed files first, till we get below the threshold + while (((100 * TotalSize) / AvailableSpace) > PercentageOfAvailableSpace && + FileAndSize != FileSizes.rend()) { + // Remove the file. + llvm::sys::fs::remove(FileAndSize->second); + // Update size + TotalSize -= FileAndSize->first; + ++FileAndSize; + } + } + return true; +}