blob: 3831625962ca7fb7e3f6279e3417e8bd9a2cff5a [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() {
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
Pavel Labath757ca882016-10-24 10:59:17 +000050 if (Expiration == seconds(0) && PercentageOfAvailableSpace == 0) {
Mehdi Amini045d4752016-04-18 21:54:00 +000051 DEBUG(dbgs() << "No pruning settings set, exit early\n");
Mehdi Amini27814982016-04-02 03:28:26 +000052 // Nothing will be pruned, early exit
53 return false;
Mehdi Amini045d4752016-04-18 21:54:00 +000054 }
Mehdi Amini27814982016-04-02 03:28:26 +000055
56 // Try to stat() the timestamp file.
Mehdi Amini721800d2016-04-21 06:43:45 +000057 SmallString<128> TimestampFile(Path);
58 sys::path::append(TimestampFile, "llvmcache.timestamp");
Mehdi Amini27814982016-04-02 03:28:26 +000059 sys::fs::file_status FileStatus;
Pavel Labath757ca882016-10-24 10:59:17 +000060 const auto CurrentTime = system_clock::now();
NAKAMURA Takumif5e36ad2016-05-14 14:21:39 +000061 if (auto EC = sys::fs::status(TimestampFile, FileStatus)) {
62 if (EC == errc::no_such_file_or_directory) {
Mehdi Amini27814982016-04-02 03:28:26 +000063 // If the timestamp file wasn't there, create one now.
64 writeTimestampFile(TimestampFile);
65 } else {
66 // Unknown error?
67 return false;
68 }
69 } else {
Pavel Labath757ca882016-10-24 10:59:17 +000070 if (Interval == seconds(0)) {
Mehdi Amini27814982016-04-02 03:28:26 +000071 // Check whether the time stamp is older than our pruning interval.
72 // If not, do nothing.
Pavel Labath757ca882016-10-24 10:59:17 +000073 const auto TimeStampModTime = FileStatus.getLastModificationTime();
Mehdi Amini045d4752016-04-18 21:54:00 +000074 auto TimeStampAge = CurrentTime - TimeStampModTime;
Pavel Labath757ca882016-10-24 10:59:17 +000075 if (TimeStampAge <= Interval) {
76 DEBUG(dbgs() << "Timestamp file too recent ("
77 << duration_cast<seconds>(TimeStampAge).count()
Mehdi Amini045d4752016-04-18 21:54:00 +000078 << "s old), do not prune.\n");
Mehdi Amini27814982016-04-02 03:28:26 +000079 return false;
Mehdi Amini045d4752016-04-18 21:54:00 +000080 }
Mehdi Amini27814982016-04-02 03:28:26 +000081 }
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
Pawel Bylica4cc79552016-06-27 08:46:23 +000094 // pruning, sorted by size.
Mehdi Amini27814982016-04-02 03:28:26 +000095 auto AddToFileListForSizePruning =
Pawel Bylica4cc79552016-06-27 08:46:23 +000096 [&](StringRef Path) {
Mehdi Amini27814982016-04-02 03:28:26 +000097 if (!ShouldComputeSize)
98 return;
99 TotalSize += FileStatus.getSize();
100 FileSizes.insert(
Mehdi Aminie9a04ae2016-04-18 21:53:55 +0000101 std::make_pair(FileStatus.getSize(), std::string(Path)));
Mehdi Amini27814982016-04-02 03:28:26 +0000102 };
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);
Mehdi Amini27814982016-04-02 03:28:26 +0000108 // 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.
Mehdi Amini045d4752016-04-18 21:54:00 +0000117 if (sys::fs::status(File->path(), FileStatus)) {
118 DEBUG(dbgs() << "Ignore " << File->path() << " (can't stat)\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000119 continue;
Mehdi Amini045d4752016-04-18 21:54:00 +0000120 }
Mehdi Amini27814982016-04-02 03:28:26 +0000121
122 // If the file hasn't been used recently enough, delete it
Pavel Labath757ca882016-10-24 10:59:17 +0000123 const auto FileAccessTime = FileStatus.getLastAccessedTime();
Mehdi Amini045d4752016-04-18 21:54:00 +0000124 auto FileAge = CurrentTime - FileAccessTime;
Pavel Labath757ca882016-10-24 10:59:17 +0000125 if (FileAge > Expiration) {
126 DEBUG(dbgs() << "Remove " << File->path() << " ("
127 << duration_cast<seconds>(FileAge).count() << "s old)\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000128 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.
Pawel Bylica4cc79552016-06-27 08:46:23 +0000133 AddToFileListForSizePruning(File->path());
Mehdi Amini27814982016-04-02 03:28:26 +0000134 }
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();
Mehdi Amini045d4752016-04-18 21:54:00 +0000145 DEBUG(dbgs() << "Occupancy: " << ((100 * TotalSize) / AvailableSpace)
146 << "% target is: " << PercentageOfAvailableSpace << "\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000147 // 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;
Mehdi Amini045d4752016-04-18 21:54:00 +0000154 DEBUG(dbgs() << " - Remove " << FileAndSize->second << " (size "
155 << FileAndSize->first << "), new occupancy is " << TotalSize
156 << "%\n");
Mehdi Amini27814982016-04-02 03:28:26 +0000157 ++FileAndSize;
158 }
159 }
160 return true;
161}