LLVM  4.0.0
CachePruning.cpp
Go to the documentation of this file.
1 //===-CachePruning.cpp - LLVM Cache Directory Pruning ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the pruning of a directory based on least recently used.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/Errc.h"
19 #include "llvm/Support/Path.h"
21 
22 #define DEBUG_TYPE "cache-pruning"
23 
24 #include <set>
25 #include <system_error>
26 
27 using namespace llvm;
28 
29 /// Write a new timestamp file with the given path. This is used for the pruning
30 /// interval option.
31 static void writeTimestampFile(StringRef TimestampFile) {
32  std::error_code EC;
33  raw_fd_ostream Out(TimestampFile.str(), EC, sys::fs::F_None);
34 }
35 
36 /// Prune the cache of files that haven't been accessed in a long time.
38  using namespace std::chrono;
39 
40  if (Path.empty())
41  return false;
42 
43  bool isPathDir;
44  if (sys::fs::is_directory(Path, isPathDir))
45  return false;
46 
47  if (!isPathDir)
48  return false;
49 
50  if (Expiration == seconds(0) && PercentageOfAvailableSpace == 0) {
51  DEBUG(dbgs() << "No pruning settings set, exit early\n");
52  // Nothing will be pruned, early exit
53  return false;
54  }
55 
56  // Try to stat() the timestamp file.
57  SmallString<128> TimestampFile(Path);
58  sys::path::append(TimestampFile, "llvmcache.timestamp");
59  sys::fs::file_status FileStatus;
60  const auto CurrentTime = system_clock::now();
61  if (auto EC = sys::fs::status(TimestampFile, FileStatus)) {
63  // If the timestamp file wasn't there, create one now.
64  writeTimestampFile(TimestampFile);
65  } else {
66  // Unknown error?
67  return false;
68  }
69  } else {
70  if (Interval == seconds(0)) {
71  // Check whether the time stamp is older than our pruning interval.
72  // If not, do nothing.
73  const auto TimeStampModTime = FileStatus.getLastModificationTime();
74  auto TimeStampAge = CurrentTime - TimeStampModTime;
75  if (TimeStampAge <= Interval) {
76  DEBUG(dbgs() << "Timestamp file too recent ("
77  << duration_cast<seconds>(TimeStampAge).count()
78  << "s old), do not prune.\n");
79  return false;
80  }
81  }
82  // Write a new timestamp file so that nobody else attempts to prune.
83  // There is a benign race condition here, if two processes happen to
84  // notice at the same time that the timestamp is out-of-date.
85  writeTimestampFile(TimestampFile);
86  }
87 
88  bool ShouldComputeSize = (PercentageOfAvailableSpace > 0);
89 
90  // Keep track of space
91  std::set<std::pair<uint64_t, std::string>> FileSizes;
92  uint64_t TotalSize = 0;
93  // Helper to add a path to the set of files to consider for size-based
94  // pruning, sorted by size.
95  auto AddToFileListForSizePruning =
96  [&](StringRef Path) {
97  if (!ShouldComputeSize)
98  return;
99  TotalSize += FileStatus.getSize();
100  FileSizes.insert(
101  std::make_pair(FileStatus.getSize(), std::string(Path)));
102  };
103 
104  // Walk the entire directory cache, looking for unused files.
105  std::error_code EC;
106  SmallString<128> CachePathNative;
107  sys::path::native(Path, CachePathNative);
108  // Walk all of the files within this directory.
109  for (sys::fs::directory_iterator File(CachePathNative, EC), FileEnd;
110  File != FileEnd && !EC; File.increment(EC)) {
111  // Do not touch the timestamp.
112  if (File->path() == TimestampFile)
113  continue;
114 
115  // Look at this file. If we can't stat it, there's nothing interesting
116  // there.
117  if (sys::fs::status(File->path(), FileStatus)) {
118  DEBUG(dbgs() << "Ignore " << File->path() << " (can't stat)\n");
119  continue;
120  }
121 
122  // If the file hasn't been used recently enough, delete it
123  const auto FileAccessTime = FileStatus.getLastAccessedTime();
124  auto FileAge = CurrentTime - FileAccessTime;
125  if (FileAge > Expiration) {
126  DEBUG(dbgs() << "Remove " << File->path() << " ("
127  << duration_cast<seconds>(FileAge).count() << "s old)\n");
128  sys::fs::remove(File->path());
129  continue;
130  }
131 
132  // Leave it here for now, but add it to the list of size-based pruning.
133  AddToFileListForSizePruning(File->path());
134  }
135 
136  // Prune for size now if needed
137  if (ShouldComputeSize) {
138  auto ErrOrSpaceInfo = sys::fs::disk_space(Path);
139  if (!ErrOrSpaceInfo) {
140  report_fatal_error("Can't get available size");
141  }
142  sys::fs::space_info SpaceInfo = ErrOrSpaceInfo.get();
143  auto AvailableSpace = TotalSize + SpaceInfo.free;
144  auto FileAndSize = FileSizes.rbegin();
145  DEBUG(dbgs() << "Occupancy: " << ((100 * TotalSize) / AvailableSpace)
146  << "% target is: " << PercentageOfAvailableSpace << "\n");
147  // Remove the oldest accessed files first, till we get below the threshold
148  while (((100 * TotalSize) / AvailableSpace) > PercentageOfAvailableSpace &&
149  FileAndSize != FileSizes.rend()) {
150  // Remove the file.
151  sys::fs::remove(FileAndSize->second);
152  // Update size
153  TotalSize -= FileAndSize->first;
154  DEBUG(dbgs() << " - Remove " << FileAndSize->second << " (size "
155  << FileAndSize->first << "), new occupancy is " << TotalSize
156  << "%\n");
157  ++FileAndSize;
158  }
159  }
160  return true;
161 }
space_info - Self explanatory.
Definition: FileSystem.h:69
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...
Definition: Interval.h:37
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
file_status - Represents the result of a call to stat and friends.
Definition: FileSystem.h:142
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:448
void native(const Twine &path, SmallVectorImpl< char > &result)
Convert path to the native form.
Definition: Path.cpp:548
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...
Definition: STLExtras.h:791
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.
Definition: StringRef.h:225
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?
Definition: Path.cpp:948
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
ErrorOr< space_info > disk_space(const Twine &Path)
Get disk space usage information.
directory_iterator - Iterates through the entries in path.
Definition: FileSystem.h:786
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:357
#define DEBUG(X)
Definition: Debug.h:100
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
std::error_code status(const Twine &path, file_status &result)
Get file status as if by POSIX stat().