1 //===--- FileCache.h - Revalidating cache of data from disk ------*- C++-*-===// 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_FILECACHE_H 10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_FILECACHE_H 11 12 #include "Path.h" 13 #include "ThreadsafeFS.h" 14 #include "llvm/Support/Chrono.h" 15 #include <mutex> 16 17 namespace clang { 18 namespace clangd { 19 20 /// Base class for threadsafe cache of data read from a file on disk. 21 /// 22 /// We want configuration files to be "live" as much as possible. 23 /// Reading them every time is simplest, but caching solves a few problems: 24 /// - reading and parsing is cheap but not free (and happens on hot paths) 25 /// - we can ignore invalid data and use the old value (we may see truncated 26 /// compile_commands.json from non-atomic writers) 27 /// - we avoid reporting the same errors repeatedly 28 /// 29 /// We still read and parse the data synchronously on demand, but skip as much 30 /// work as possible: 31 /// - if not enough wall-time has elapsed, assume the data is still up-to-date 32 /// - if we stat the file and it has the same mtime + size, don't read it 33 /// - obviously we only have to parse when we re-read the file 34 /// (Tracking OS change events is an alternative, but difficult to do portably.) 35 /// 36 /// Caches for particular data (e.g. compilation databases) should inherit and: 37 /// - add mutable storage for the cached parsed data 38 /// - add a public interface implemented on top of read() 39 class FileCache { 40 protected: 41 // Path must be absolute. 42 FileCache(PathRef Path); 43 44 // Updates the cached value if needed, then provides threadsafe access to it. 45 // 46 // Specifically: 47 // - Parse() may be called (if the cache was not up-to-date) 48 // The lock is held, so cache storage may be safely written. 49 // Parse(None) means the file doesn't exist. 50 // - Read() will always be called, to provide access to the value. 51 // The lock is again held, so the value can be copied or used. 52 // 53 // If the last Parse is newer than FreshTime, we don't check metadata. 54 // - time_point::min() means we only do IO if we never read the file before 55 // - time_point::max() means we always at least stat the file 56 // - steady_clock::now() + seconds(1) means we accept 1 second of staleness 57 void read(const ThreadsafeFS &TFS, 58 std::chrono::steady_clock::time_point FreshTime, 59 llvm::function_ref<void(llvm::Optional<llvm::StringRef>)> Parse, 60 llvm::function_ref<void()> Read) const; 61 path()62 PathRef path() const { return Path; } 63 64 private: 65 std::string Path; 66 // Members are mutable so read() can present a const interface. 67 // (It is threadsafe and approximates read-through to TFS). 68 mutable std::mutex Mu; 69 // Time when the cache was known valid (reflected disk state). 70 mutable std::chrono::steady_clock::time_point ValidTime; 71 // Filesystem metadata corresponding to the currently cached data. 72 mutable llvm::sys::TimePoint<> ModifiedTime; 73 mutable uint64_t Size; 74 }; 75 76 } // namespace clangd 77 } // namespace clang 78 79 #endif 80