blob: 6184ba2ea33d30120833c14ca97c4372cb1c4561 [file] [log] [blame]
Mehdi Amini27814982016-04-02 03:28:26 +00001//===-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
Mehdi Amini045d4752016-04-18 21:54:00 +000016#include "llvm/Support/Debug.h"
NAKAMURA Takumif5e36ad2016-05-14 14:21:39 +000017#include "llvm/Support/Errc.h"
Mehdi Amini27814982016-04-02 03:28:26 +000018#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/raw_ostream.h"
21
Mehdi Amini045d4752016-04-18 21:54:00 +000022#define DEBUG_TYPE "cache-pruning"
23
Mehdi Amini27814982016-04-02 03:28:26 +000024#include <set>
Vassil Vassilev2ec8b152016-09-14 08:55:18 +000025#include <system_error>
Mehdi Amini27814982016-04-02 03:28:26 +000026
27using namespace llvm;
28
29/// Write a new timestamp file with the given path. This is used for the pruning
30/// interval option.
31static 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.
37bool CachePruning::prune() {
Mehdi Amini721800d2016-04-21 06:43:45 +000038 if (Path.empty())
39 return false;
40
41 bool isPathDir;
42 if (sys::fs::is_directory(Path, isPathDir))
43 return false;
44
45 if (!isPathDir)
46 return false;
Mehdi Amini27814982016-04-02 03:28:26 +000047
Mehdi Amini045d4752016-04-18 21:54:00 +000048 if (Expiration == 0 && PercentageOfAvailableSpace == 0) {
49 DEBUG(dbgs() << "No pruning settings set, exit early\n");
Mehdi Amini27814982016-04-02 03:28:26 +000050 // Nothing will be pruned, early exit
51 return false;
Mehdi Amini045d4752016-04-18 21:54:00 +000052 }
Mehdi Amini27814982016-04-02 03:28:26 +000053
54 // Try to stat() the timestamp file.
Mehdi Amini721800d2016-04-21 06:43:45 +000055 SmallString<128> TimestampFile(Path);
56 sys::path::append(TimestampFile, "llvmcache.timestamp");
Mehdi Amini27814982016-04-02 03:28:26 +000057 sys::fs::file_status FileStatus;
58 sys::TimeValue CurrentTime = sys::TimeValue::now();
NAKAMURA Takumif5e36ad2016-05-14 14:21:39 +000059 if (auto EC = sys::fs::status(TimestampFile, FileStatus)) {
60 if (EC == errc::no_such_file_or_directory) {
Mehdi Amini27814982016-04-02 03:28:26 +000061 // If the timestamp file wasn't there, create one now.
62 writeTimestampFile(TimestampFile);
63 } else {
64 // Unknown error?
65 return false;
66 }
67 } else {
68 if (Interval) {
69 // Check whether the time stamp is older than our pruning interval.
70 // If not, do nothing.
71 sys::TimeValue TimeStampModTime = FileStatus.getLastModificationTime();
72 auto TimeInterval = sys::TimeValue(sys::TimeValue::SecondsType(Interval));
Mehdi Amini045d4752016-04-18 21:54:00 +000073 auto TimeStampAge = CurrentTime - TimeStampModTime;
74 if (TimeStampAge <= TimeInterval) {
75 DEBUG(dbgs() << "Timestamp file too recent (" << TimeStampAge.seconds()
76 << "s old), do not prune.\n");
Mehdi Amini27814982016-04-02 03:28:26 +000077 return false;
Mehdi Amini045d4752016-04-18 21:54:00 +000078 }
Mehdi Amini27814982016-04-02 03:28:26 +000079 }
80 // Write a new timestamp file so that nobody else attempts to prune.
81 // There is a benign race condition here, if two processes happen to
82 // notice at the same time that the timestamp is out-of-date.
83 writeTimestampFile(TimestampFile);
84 }
85
86 bool ShouldComputeSize = (PercentageOfAvailableSpace > 0);
87
88 // Keep track of space
89 std::set<std::pair<uint64_t, std::string>> FileSizes;
90 uint64_t TotalSize = 0;
91 // Helper to add a path to the set of files to consider for size-based
Pawel Bylica4cc79552016-06-27 08:46:23 +000092 // pruning, sorted by size.
Mehdi Amini27814982016-04-02 03:28:26 +000093 auto AddToFileListForSizePruning =
Pawel Bylica4cc79552016-06-27 08:46:23 +000094 [&](StringRef Path) {
Mehdi Amini27814982016-04-02 03:28:26 +000095 if (!ShouldComputeSize)
96 return;
97 TotalSize += FileStatus.getSize();
98 FileSizes.insert(
Mehdi Aminie9a04ae2016-04-18 21:53:55 +000099 std::make_pair(FileStatus.getSize(), std::string(Path)));
Mehdi Amini27814982016-04-02 03:28:26 +0000100 };
101
102 // Walk the entire directory cache, looking for unused files.
103 std::error_code EC;
104 SmallString<128> CachePathNative;
105 sys::path::native(Path, CachePathNative);
106 auto TimeExpiration = sys::TimeValue(sys::TimeValue::SecondsType(Expiration));
107 // Walk all of the files within this directory.
108 for (sys::fs::directory_iterator File(CachePathNative, EC), FileEnd;
109 File != FileEnd && !EC; File.increment(EC)) {
110 // Do not touch the timestamp.
111 if (File->path() == TimestampFile)
112 continue;
113
114 // Look at this file. If we can't stat it, there's nothing interesting
115 // there.
Mehdi Amini045d4752016-04-18 21:54:00 +0000116 if (sys::fs::status(File->path(), FileStatus)) {
117 DEBUG(dbgs() << "Ignore " << File->path() << " (can't stat)\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000118 continue;
Mehdi Amini045d4752016-04-18 21:54:00 +0000119 }
Mehdi Amini27814982016-04-02 03:28:26 +0000120
121 // If the file hasn't been used recently enough, delete it
122 sys::TimeValue FileAccessTime = FileStatus.getLastAccessedTime();
Mehdi Amini045d4752016-04-18 21:54:00 +0000123 auto FileAge = CurrentTime - FileAccessTime;
124 if (FileAge > TimeExpiration) {
125 DEBUG(dbgs() << "Remove " << File->path() << " (" << FileAge.seconds()
126 << "s old)\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000127 sys::fs::remove(File->path());
128 continue;
129 }
130
131 // Leave it here for now, but add it to the list of size-based pruning.
Pawel Bylica4cc79552016-06-27 08:46:23 +0000132 AddToFileListForSizePruning(File->path());
Mehdi Amini27814982016-04-02 03:28:26 +0000133 }
134
135 // Prune for size now if needed
136 if (ShouldComputeSize) {
137 auto ErrOrSpaceInfo = sys::fs::disk_space(Path);
138 if (!ErrOrSpaceInfo) {
139 report_fatal_error("Can't get available size");
140 }
141 sys::fs::space_info SpaceInfo = ErrOrSpaceInfo.get();
142 auto AvailableSpace = TotalSize + SpaceInfo.free;
143 auto FileAndSize = FileSizes.rbegin();
Mehdi Amini045d4752016-04-18 21:54:00 +0000144 DEBUG(dbgs() << "Occupancy: " << ((100 * TotalSize) / AvailableSpace)
145 << "% target is: " << PercentageOfAvailableSpace << "\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000146 // Remove the oldest accessed files first, till we get below the threshold
147 while (((100 * TotalSize) / AvailableSpace) > PercentageOfAvailableSpace &&
148 FileAndSize != FileSizes.rend()) {
149 // Remove the file.
150 sys::fs::remove(FileAndSize->second);
151 // Update size
152 TotalSize -= FileAndSize->first;
Mehdi Amini045d4752016-04-18 21:54:00 +0000153 DEBUG(dbgs() << " - Remove " << FileAndSize->second << " (size "
154 << FileAndSize->first << "), new occupancy is " << TotalSize
155 << "%\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000156 ++FileAndSize;
157 }
158 }
159 return true;
160}