1 //===--- FileCache.cpp ----------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "support/FileCache.h" 10 11 namespace clang { 12 namespace clangd { 13 14 // Sentinel values for the Size cache key. In both cases, a successful stat of 15 // the file will never result in the cached value being reused. 16 17 // The cached value does not reflect the current content on disk. 18 static constexpr uint64_t CacheDiskMismatch = 19 std::numeric_limits<uint64_t>::max(); 20 // The cached value reflects that the file doesn't exist. 21 static constexpr uint64_t FileNotFound = CacheDiskMismatch - 1; 22 23 FileCache::FileCache(llvm::StringRef Path) 24 : Path(Path), ValidTime(std::chrono::steady_clock::time_point::min()), 25 ModifiedTime(), Size(CacheDiskMismatch) { 26 assert(llvm::sys::path::is_absolute(Path)); 27 } 28 29 void FileCache::read( 30 const ThreadsafeFS &TFS, std::chrono::steady_clock::time_point FreshTime, 31 llvm::function_ref<void(llvm::Optional<llvm::StringRef>)> Parse, 32 llvm::function_ref<void()> Read) const { 33 34 std::lock_guard<std::mutex> Lock(Mu); 35 // We're going to update the cache and return whatever's in it. 36 auto Return = llvm::make_scope_exit(Read); 37 38 // Return any sufficiently recent result without doing any further work. 39 if (ValidTime > FreshTime) 40 return; 41 42 // Ensure we always bump ValidTime, so that FreshTime imposes a hard limit on 43 // how often we do IO. 44 auto BumpValidTime = llvm::make_scope_exit( 45 [&] { ValidTime = std::chrono::steady_clock::now(); }); 46 47 // stat is cheaper than opening the file. It's usually unchanged. 48 assert(llvm::sys::path::is_absolute(Path)); 49 auto FS = TFS.view(/*CWD=*/llvm::None); 50 auto Stat = FS->status(Path); 51 if (!Stat || !Stat->isRegularFile()) { 52 if (Size != FileNotFound) // Allow "not found" value to be cached. 53 Parse(llvm::None); 54 // Ensure the cache key won't match any future stat(). 55 Size = FileNotFound; 56 return; 57 } 58 // If the modified-time and size match, assume the content does too. 59 if (Size == Stat->getSize() && 60 ModifiedTime == Stat->getLastModificationTime()) 61 return; 62 63 // OK, the file has actually changed. Update cache key, compute new value. 64 Size = Stat->getSize(); 65 ModifiedTime = Stat->getLastModificationTime(); 66 // Now read the file from disk. 67 if (auto Buf = FS->getBufferForFile(Path)) { 68 Parse(Buf->get()->getBuffer()); 69 // Result is cacheable if the actual read size matches the new cache key. 70 // (We can't update the cache key, because we don't know the new mtime). 71 if (Buf->get()->getBufferSize() != Size) 72 Size = CacheDiskMismatch; 73 } else { 74 // File was unreadable. Keep the old value and try again next time. 75 Size = CacheDiskMismatch; 76 } 77 } 78 79 } // namespace clangd 80 } // namespace clang 81