blob: 27cffb7721a9b9bed8b39ca99d43189c44b214bd [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.
Peter Collingbournecead56f2017-03-15 22:54:18 +000037bool llvm::pruneCache(StringRef Path, CachePruningPolicy Policy) {
Pavel Labath757ca882016-10-24 10:59:17 +000038 using namespace std::chrono;
39
Mehdi Amini721800d2016-04-21 06:43:45 +000040 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;
Mehdi Amini27814982016-04-02 03:28:26 +000049
Peter Collingbournecead56f2017-03-15 22:54:18 +000050 Policy.PercentageOfAvailableSpace =
51 std::min(Policy.PercentageOfAvailableSpace, 100u);
52
53 if (Policy.Expiration == seconds(0) &&
54 Policy.PercentageOfAvailableSpace == 0) {
Mehdi Amini045d4752016-04-18 21:54:00 +000055 DEBUG(dbgs() << "No pruning settings set, exit early\n");
Mehdi Amini27814982016-04-02 03:28:26 +000056 // Nothing will be pruned, early exit
57 return false;
Mehdi Amini045d4752016-04-18 21:54:00 +000058 }
Mehdi Amini27814982016-04-02 03:28:26 +000059
60 // Try to stat() the timestamp file.
Mehdi Amini721800d2016-04-21 06:43:45 +000061 SmallString<128> TimestampFile(Path);
62 sys::path::append(TimestampFile, "llvmcache.timestamp");
Mehdi Amini27814982016-04-02 03:28:26 +000063 sys::fs::file_status FileStatus;
Pavel Labath757ca882016-10-24 10:59:17 +000064 const auto CurrentTime = system_clock::now();
NAKAMURA Takumif5e36ad2016-05-14 14:21:39 +000065 if (auto EC = sys::fs::status(TimestampFile, FileStatus)) {
66 if (EC == errc::no_such_file_or_directory) {
Mehdi Amini27814982016-04-02 03:28:26 +000067 // If the timestamp file wasn't there, create one now.
68 writeTimestampFile(TimestampFile);
69 } else {
70 // Unknown error?
71 return false;
72 }
73 } else {
Peter Collingbournecead56f2017-03-15 22:54:18 +000074 if (Policy.Interval == seconds(0)) {
Mehdi Amini27814982016-04-02 03:28:26 +000075 // Check whether the time stamp is older than our pruning interval.
76 // If not, do nothing.
Pavel Labath757ca882016-10-24 10:59:17 +000077 const auto TimeStampModTime = FileStatus.getLastModificationTime();
Mehdi Amini045d4752016-04-18 21:54:00 +000078 auto TimeStampAge = CurrentTime - TimeStampModTime;
Peter Collingbournecead56f2017-03-15 22:54:18 +000079 if (TimeStampAge <= Policy.Interval) {
Pavel Labath757ca882016-10-24 10:59:17 +000080 DEBUG(dbgs() << "Timestamp file too recent ("
81 << duration_cast<seconds>(TimeStampAge).count()
Mehdi Amini045d4752016-04-18 21:54:00 +000082 << "s old), do not prune.\n");
Mehdi Amini27814982016-04-02 03:28:26 +000083 return false;
Mehdi Amini045d4752016-04-18 21:54:00 +000084 }
Mehdi Amini27814982016-04-02 03:28:26 +000085 }
86 // Write a new timestamp file so that nobody else attempts to prune.
87 // There is a benign race condition here, if two processes happen to
88 // notice at the same time that the timestamp is out-of-date.
89 writeTimestampFile(TimestampFile);
90 }
91
Peter Collingbournecead56f2017-03-15 22:54:18 +000092 bool ShouldComputeSize = (Policy.PercentageOfAvailableSpace > 0);
Mehdi Amini27814982016-04-02 03:28:26 +000093
94 // Keep track of space
95 std::set<std::pair<uint64_t, std::string>> FileSizes;
96 uint64_t TotalSize = 0;
97 // Helper to add a path to the set of files to consider for size-based
Pawel Bylica4cc79552016-06-27 08:46:23 +000098 // pruning, sorted by size.
Mehdi Amini27814982016-04-02 03:28:26 +000099 auto AddToFileListForSizePruning =
Pawel Bylica4cc79552016-06-27 08:46:23 +0000100 [&](StringRef Path) {
Mehdi Amini27814982016-04-02 03:28:26 +0000101 if (!ShouldComputeSize)
102 return;
103 TotalSize += FileStatus.getSize();
104 FileSizes.insert(
Mehdi Aminie9a04ae2016-04-18 21:53:55 +0000105 std::make_pair(FileStatus.getSize(), std::string(Path)));
Mehdi Amini27814982016-04-02 03:28:26 +0000106 };
107
108 // Walk the entire directory cache, looking for unused files.
109 std::error_code EC;
110 SmallString<128> CachePathNative;
111 sys::path::native(Path, CachePathNative);
Mehdi Amini27814982016-04-02 03:28:26 +0000112 // Walk all of the files within this directory.
113 for (sys::fs::directory_iterator File(CachePathNative, EC), FileEnd;
114 File != FileEnd && !EC; File.increment(EC)) {
115 // Do not touch the timestamp.
116 if (File->path() == TimestampFile)
117 continue;
118
119 // Look at this file. If we can't stat it, there's nothing interesting
120 // there.
Mehdi Amini045d4752016-04-18 21:54:00 +0000121 if (sys::fs::status(File->path(), FileStatus)) {
122 DEBUG(dbgs() << "Ignore " << File->path() << " (can't stat)\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000123 continue;
Mehdi Amini045d4752016-04-18 21:54:00 +0000124 }
Mehdi Amini27814982016-04-02 03:28:26 +0000125
126 // If the file hasn't been used recently enough, delete it
Pavel Labath757ca882016-10-24 10:59:17 +0000127 const auto FileAccessTime = FileStatus.getLastAccessedTime();
Mehdi Amini045d4752016-04-18 21:54:00 +0000128 auto FileAge = CurrentTime - FileAccessTime;
Peter Collingbournecead56f2017-03-15 22:54:18 +0000129 if (FileAge > Policy.Expiration) {
Pavel Labath757ca882016-10-24 10:59:17 +0000130 DEBUG(dbgs() << "Remove " << File->path() << " ("
131 << duration_cast<seconds>(FileAge).count() << "s old)\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000132 sys::fs::remove(File->path());
133 continue;
134 }
135
136 // Leave it here for now, but add it to the list of size-based pruning.
Pawel Bylica4cc79552016-06-27 08:46:23 +0000137 AddToFileListForSizePruning(File->path());
Mehdi Amini27814982016-04-02 03:28:26 +0000138 }
139
140 // Prune for size now if needed
141 if (ShouldComputeSize) {
142 auto ErrOrSpaceInfo = sys::fs::disk_space(Path);
143 if (!ErrOrSpaceInfo) {
144 report_fatal_error("Can't get available size");
145 }
146 sys::fs::space_info SpaceInfo = ErrOrSpaceInfo.get();
147 auto AvailableSpace = TotalSize + SpaceInfo.free;
148 auto FileAndSize = FileSizes.rbegin();
Mehdi Amini045d4752016-04-18 21:54:00 +0000149 DEBUG(dbgs() << "Occupancy: " << ((100 * TotalSize) / AvailableSpace)
Peter Collingbournecead56f2017-03-15 22:54:18 +0000150 << "% target is: " << Policy.PercentageOfAvailableSpace
151 << "\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000152 // Remove the oldest accessed files first, till we get below the threshold
Peter Collingbournecead56f2017-03-15 22:54:18 +0000153 while (((100 * TotalSize) / AvailableSpace) >
154 Policy.PercentageOfAvailableSpace &&
Mehdi Amini27814982016-04-02 03:28:26 +0000155 FileAndSize != FileSizes.rend()) {
156 // Remove the file.
157 sys::fs::remove(FileAndSize->second);
158 // Update size
159 TotalSize -= FileAndSize->first;
Mehdi Amini045d4752016-04-18 21:54:00 +0000160 DEBUG(dbgs() << " - Remove " << FileAndSize->second << " (size "
161 << FileAndSize->first << "), new occupancy is " << TotalSize
162 << "%\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000163 ++FileAndSize;
164 }
165 }
166 return true;
167}