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