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