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