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