14#include "llvm/Support/AdvisoryLock.h"
15#include "llvm/Support/Chrono.h"
16#include "llvm/Support/Error.h"
17#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/IOSandbox.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/Support/Path.h"
28 assert(llvm::sys::path::is_absolute(Path) &&
29 !llvm::sys::path::remove_dots(Canonical,
true) &&
30 "invalidated paths must be spelled like ModuleDeps::DirectoryDeps");
33 std::lock_guard<std::mutex> Lock(InvalidatedPathsMutex);
34 InvalidatedPaths.insert(Path);
36 AnyInvalidatedPaths.store(
true, std::memory_order_release);
43 if (!AnyInvalidatedPaths.load(std::memory_order_acquire))
45 std::lock_guard<std::mutex> Lock(InvalidatedPathsMutex);
46 return InvalidatedPaths.contains(Directory);
50 assert(!Flusher.joinable() &&
"Concurrent flushing was not stopped");
55 if (!Flusher.joinable())
59 std::lock_guard<std::mutex> Lock(FlushMutex);
60 FlushEntries.push_back(&MapEntry);
62 FlushCondVar.notify_one();
66 assert(!Flusher.joinable() &&
"Already started concurrent flushing");
67 Flusher = std::thread([
this, &Logger] {
68 std::unique_lock<std::mutex> PendingLock(FlushMutex);
70 FlushCondVar.wait(PendingLock,
71 [&] {
return StopFlushing || !FlushEntries.empty(); });
76 FlushEntries.pop_front();
78 flushEntry(MapEntry.getKey(), *MapEntry.second, Logger,
86 assert(Flusher.joinable() &&
"Never started concurrent flushing");
88 std::lock_guard<std::mutex> Lock(FlushMutex);
91 FlushCondVar.notify_one();
96 assert(!Flusher.joinable() &&
"Flushing leftovers concurrently");
97 for (
auto &[Name, Entry] : llvm::make_pointee_range(FlushEntries))
98 flushEntry(Name, *Entry, Logger,
false);
104 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
106 std::shared_ptr<const llvm::MemoryBuffer> InMemoryBuffer;
108 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
111 InMemoryBuffer = Entry.
Buffer;
116 auto Written =
writeImpl(Path, *InMemoryBuffer, Size, ModTime, ReadBack);
119 Logger.
log() <<
"pcm_flush_failed: " << Path;
122 Logger.
log() <<
"pcm_flush: " << Path;
124 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
126 Entry.
Buffer = std::move(*Written);
132class SharedMemoryBuffer :
public llvm::MemoryBuffer {
133 std::shared_ptr<const llvm::MemoryBuffer> Underlying;
136 SharedMemoryBuffer(std::shared_ptr<const llvm::MemoryBuffer> Underlying)
137 : Underlying(std::move(Underlying)) {
138 init(this->Underlying->getBufferStart(), this->Underlying->getBufferEnd(),
142 StringRef getBufferIdentifier()
const override {
143 return Underlying->getBufferIdentifier();
146 BufferKind getBufferKind()
const override {
147 return Underlying->getBufferKind();
151class ReaderWriterLock :
public llvm::AdvisoryLock {
152 ModuleCacheEntry &Entry;
153 std::optional<unsigned> OwnedGeneration;
156 ReaderWriterLock(ModuleCacheEntry &Entry) : Entry(Entry) {}
158 Expected<bool> tryLock()
override {
159 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
167 llvm::WaitForUnlockResult
168 waitForUnlockFor(std::chrono::seconds MaxSeconds)
override {
169 assert(!OwnedGeneration);
170 std::unique_lock<std::mutex> Lock(Entry.
Mutex);
171 unsigned CurrentGeneration = Entry.
Generation;
177 return Success ? llvm::WaitForUnlockResult::Success
178 : llvm::WaitForUnlockResult::Timeout;
181 std::error_code unsafeUnlock()
override {
183 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
191 ~ReaderWriterLock()
override {
192 if (OwnedGeneration) {
194 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
205class InProcessModuleCache :
public ModuleCache {
206 ModuleCacheEntries &Entries;
212 InMemoryModuleCache InMemory;
215 std::lock_guard<std::mutex> Lock(Entries.
Mutex);
216 auto &MapEntry = *Entries.
Map.try_emplace(Filename).first;
217 if (!MapEntry.second)
218 MapEntry.second = std::make_unique<ModuleCacheEntry>();
222 ModuleCacheEntry &getOrCreateEntry(StringRef Filename) {
223 return *getOrCreateMapEntry(Filename).second;
227 InProcessModuleCache(ModuleCacheEntries &Entries, AtomicLineLogger &Logger)
228 : ModuleCache(Logger), Entries(Entries), InMemory(Logger) {}
230 std::unique_ptr<llvm::AdvisoryLock> getLock(StringRef Filename)
override {
231 auto &Entry = getOrCreateEntry(Filename);
232 return std::make_unique<ReaderWriterLock>(Entry);
235 std::time_t getModuleTimestamp(StringRef Filename)
override {
236 auto &Timestamp = getOrCreateEntry(Filename).Timestamp;
238 Logger.
log() <<
"timestamp_read: " << Filename;
239 return Timestamp.load();
242 void updateModuleTimestamp(StringRef Filename)
override {
244 auto &Timestamp = getOrCreateEntry(Filename).Timestamp;
246 Logger.
log() <<
"timestamp_write: " << Filename;
247 Timestamp.store(llvm::sys::toTimeT(std::chrono::system_clock::now()));
250 std::optional<bool> isDirectoryInvalidated(StringRef Directory)
override {
254 void maybePrune(StringRef Path, time_t PruneInterval,
255 time_t PruneAfter)
override {
261 InMemoryModuleCache &getInMemoryModuleCache()
override {
return InMemory; }
262 const InMemoryModuleCache &getInMemoryModuleCache()
const override {
266 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
267 write(StringRef Path, std::unique_ptr<llvm::MemoryBuffer> Buffer, off_t &Size,
268 time_t &ModTime)
override {
270 ModuleCacheEntry &Entry = *MapEntry.second;
271 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
272 Logger.
log() <<
"pcm_write: " << Path;
275 assert(Entry.
Buffer &&
"Wrote PCM with no contents");
276 assert(Entry.
Buffer->getBuffer() == Buffer->getBuffer() &&
277 "Wrote the same PCM with different contents");
280 return std::make_unique<SharedMemoryBuffer>(Entry.
Buffer);
283 Entry.
Buffer = std::move(Buffer);
284 Entry.
ModTime = llvm::sys::toTimeT(std::chrono::system_clock::now());
289 return std::make_unique<SharedMemoryBuffer>(Entry.
Buffer);
292 Expected<std::unique_ptr<llvm::MemoryBuffer>>
293 read(StringRef
FileName, off_t &Size, time_t &ModTime)
override {
295 ModuleCacheEntry &Entry = getOrCreateEntry(
FileName);
296 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
299 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
304 return ReadBuffer.takeError();
305 Entry.
Buffer = std::move(*ReadBuffer);
311 return std::make_unique<SharedMemoryBuffer>(Entry.
Buffer);
316std::shared_ptr<ModuleCache>
319 return std::make_shared<InProcessModuleCache>(Entries, Logger);
Defines a logger where each line is written atomically to the file.
llvm::StringMapEntry< std::unique_ptr< ModuleCacheEntry > > ModuleCacheMapEntry
std::shared_ptr< ModuleCache > makeInProcessModuleCache(ModuleCacheEntries &Entries, AtomicLineLogger &Logger)
Top level wrappers for InstallAPI frontend operations.
Expected< std::unique_ptr< llvm::MemoryBuffer > > readImpl(StringRef FileName, off_t &Size, time_t &ModTime)
Shared implementation of ModuleCache::read().
@ Success
Annotation was successful.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer, off_t &Size, time_t &ModTime, bool ReadBack)
Shared implementation of ModuleCache::write().
void maybePruneImpl(StringRef Path, time_t PruneInterval, time_t PruneAfter, bool PruneTopLevel=false, llvm::function_ref< void(StringRef)> OnPrune={})
Shared implementation of ModuleCache::maybePrune().
llvm::StringMap< std::unique_ptr< ModuleCacheEntry > > Map
void stopBackgroundFlushing()
Stops the background thread as soon as it finishes flushing the current PCM.
void startBackgroundFlushing(AtomicLineLogger &Logger)
Starts a background thread that flushes PCMs built in-process to disk as soon as they're written,...
void scheduleFlush(ModuleCacheMapEntry &MapEntry)
Adds MapEntry into the queue of entries to be flushed.
std::optional< bool > isDirectoryInvalidated(StringRef Directory) const
void addInvalidatedPath(StringRef Path)
void flushLeftovers(AtomicLineLogger &Logger)
Flushes all PCMs built in-process that haven't been flushed yet.
bool ValidateAgainstInvalidatedPaths
enum clang::dependencies::ModuleCacheEntry::@010254116217305143125116322142262271134241253225 State
@ S_Flushed
Written to the module cache and flushed to disk.
@ S_Written
Written to the module cache, but only kept in memory.
std::condition_variable CondVar
time_t ModTime
The modification time of the entry.
std::shared_ptr< const llvm::MemoryBuffer > Buffer
The buffer we've read from disk or written to the module cache, if any.