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 
14 #include "llvm/Support/CachePruning.h"
15 
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 #define DEBUG_TYPE "cache-pruning"
22 
23 #include <set>
24 
25 using namespace llvm;
26 
27 /// Write a new timestamp file with the given path. This is used for the pruning
28 /// interval option.
29 static void writeTimestampFile(StringRef TimestampFile) {
30   std::error_code EC;
31   raw_fd_ostream Out(TimestampFile.str(), EC, sys::fs::F_None);
32 }
33 
34 /// Prune the cache of files that haven't been accessed in a long time.
35 bool CachePruning::prune() {
36   if (Path.empty())
37     return false;
38 
39   bool isPathDir;
40   if (sys::fs::is_directory(Path, isPathDir))
41     return false;
42 
43   if (!isPathDir)
44     return false;
45 
46   if (Expiration == 0 && PercentageOfAvailableSpace == 0) {
47     DEBUG(dbgs() << "No pruning settings set, exit early\n");
48     // Nothing will be pruned, early exit
49     return false;
50   }
51 
52   // Try to stat() the timestamp file.
53   SmallString<128> TimestampFile(Path);
54   sys::path::append(TimestampFile, "llvmcache.timestamp");
55   sys::fs::file_status FileStatus;
56   sys::TimeValue CurrentTime = sys::TimeValue::now();
57   if (sys::fs::status(TimestampFile, FileStatus)) {
58     if (errno == ENOENT) {
59       // If the timestamp file wasn't there, create one now.
60       writeTimestampFile(TimestampFile);
61     } else {
62       // Unknown error?
63       return false;
64     }
65   } else {
66     if (Interval) {
67       // Check whether the time stamp is older than our pruning interval.
68       // If not, do nothing.
69       sys::TimeValue TimeStampModTime = FileStatus.getLastModificationTime();
70       auto TimeInterval = sys::TimeValue(sys::TimeValue::SecondsType(Interval));
71       auto TimeStampAge = CurrentTime - TimeStampModTime;
72       if (TimeStampAge <= TimeInterval) {
73         DEBUG(dbgs() << "Timestamp file too recent (" << TimeStampAge.seconds()
74                      << "s old), do not prune.\n");
75         return false;
76       }
77     }
78     // Write a new timestamp file so that nobody else attempts to prune.
79     // There is a benign race condition here, if two processes happen to
80     // notice at the same time that the timestamp is out-of-date.
81     writeTimestampFile(TimestampFile);
82   }
83 
84   bool ShouldComputeSize = (PercentageOfAvailableSpace > 0);
85 
86   // Keep track of space
87   std::set<std::pair<uint64_t, std::string>> FileSizes;
88   uint64_t TotalSize = 0;
89   // Helper to add a path to the set of files to consider for size-based
90   // pruning, sorted by last accessed time.
91   auto AddToFileListForSizePruning =
92       [&](StringRef Path, sys::TimeValue FileAccessTime) {
93         if (!ShouldComputeSize)
94           return;
95         TotalSize += FileStatus.getSize();
96         FileSizes.insert(
97             std::make_pair(FileStatus.getSize(), std::string(Path)));
98       };
99 
100   // Walk the entire directory cache, looking for unused files.
101   std::error_code EC;
102   SmallString<128> CachePathNative;
103   sys::path::native(Path, CachePathNative);
104   auto TimeExpiration = sys::TimeValue(sys::TimeValue::SecondsType(Expiration));
105   // Walk all of the files within this directory.
106   for (sys::fs::directory_iterator File(CachePathNative, EC), FileEnd;
107        File != FileEnd && !EC; File.increment(EC)) {
108     // Do not touch the timestamp.
109     if (File->path() == TimestampFile)
110       continue;
111 
112     // Look at this file. If we can't stat it, there's nothing interesting
113     // there.
114     if (sys::fs::status(File->path(), FileStatus)) {
115       DEBUG(dbgs() << "Ignore " << File->path() << " (can't stat)\n");
116       continue;
117     }
118 
119     // If the file hasn't been used recently enough, delete it
120     sys::TimeValue FileAccessTime = FileStatus.getLastAccessedTime();
121     auto FileAge = CurrentTime - FileAccessTime;
122     if (FileAge > TimeExpiration) {
123       DEBUG(dbgs() << "Remove " << File->path() << " (" << FileAge.seconds()
124                    << "s old)\n");
125       sys::fs::remove(File->path());
126       continue;
127     }
128 
129     // Leave it here for now, but add it to the list of size-based pruning.
130     AddToFileListForSizePruning(File->path(), FileAccessTime);
131   }
132 
133   // Prune for size now if needed
134   if (ShouldComputeSize) {
135     auto ErrOrSpaceInfo = sys::fs::disk_space(Path);
136     if (!ErrOrSpaceInfo) {
137       report_fatal_error("Can't get available size");
138     }
139     sys::fs::space_info SpaceInfo = ErrOrSpaceInfo.get();
140     auto AvailableSpace = TotalSize + SpaceInfo.free;
141     auto FileAndSize = FileSizes.rbegin();
142     DEBUG(dbgs() << "Occupancy: " << ((100 * TotalSize) / AvailableSpace)
143                  << "% target is: " << PercentageOfAvailableSpace << "\n");
144     // Remove the oldest accessed files first, till we get below the threshold
145     while (((100 * TotalSize) / AvailableSpace) > PercentageOfAvailableSpace &&
146            FileAndSize != FileSizes.rend()) {
147       // Remove the file.
148       sys::fs::remove(FileAndSize->second);
149       // Update size
150       TotalSize -= FileAndSize->first;
151       DEBUG(dbgs() << " - Remove " << FileAndSize->second << " (size "
152                    << FileAndSize->first << "), new occupancy is " << TotalSize
153                    << "%\n");
154       ++FileAndSize;
155     }
156   }
157   return true;
158 }
159