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