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"
26 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
27 for (
auto &[Path, Entry] :
Map) {
29 assert(Entry->WrittenBuffer &&
"Wrote PCM with no contents");
36 (void)
writeImpl(Path, *Entry->WrittenBuffer, Size, ModTime);
42class ReaderWriterLock :
public llvm::AdvisoryLock {
44 std::optional<unsigned> OwnedGeneration;
50 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
58 llvm::WaitForUnlockResult
59 waitForUnlockFor(std::chrono::seconds MaxSeconds)
override {
60 assert(!OwnedGeneration);
61 std::unique_lock<std::mutex> Lock(Entry.
Mutex);
68 return Success ? llvm::WaitForUnlockResult::Success
69 : llvm::WaitForUnlockResult::Timeout;
72 std::error_code unsafeUnlock()
override {
74 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
82 ~ReaderWriterLock()
override {
83 if (OwnedGeneration) {
85 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
96class InProcessModuleCache :
public ModuleCache {
97 ModuleCacheEntries &Entries;
103 InMemoryModuleCache InMemory;
105 ModuleCacheEntry &getOrCreateEntry(StringRef Filename) {
106 std::lock_guard<std::mutex> Lock(Entries.
Mutex);
107 auto &Entry = Entries.
Map[Filename];
109 Entry = std::make_unique<ModuleCacheEntry>();
114 InProcessModuleCache(ModuleCacheEntries &Entries, AtomicLineLogger &Logger)
115 : ModuleCache(Logger), Entries(Entries), InMemory(Logger) {}
117 std::unique_ptr<llvm::AdvisoryLock> getLock(StringRef Filename)
override {
118 auto &Entry = getOrCreateEntry(Filename);
119 return std::make_unique<ReaderWriterLock>(Entry);
122 std::time_t getModuleTimestamp(StringRef Filename)
override {
123 auto &Timestamp = getOrCreateEntry(Filename).Timestamp;
125 Logger.log() <<
"timestamp_read: " << Filename;
126 return Timestamp.load();
129 void updateModuleTimestamp(StringRef Filename)
override {
131 auto &Timestamp = getOrCreateEntry(Filename).Timestamp;
133 Logger.log() <<
"timestamp_write: " << Filename;
134 Timestamp.store(llvm::sys::toTimeT(std::chrono::system_clock::now()));
137 void maybePrune(StringRef Path, time_t PruneInterval,
138 time_t PruneAfter)
override {
144 InMemoryModuleCache &getInMemoryModuleCache()
override {
return InMemory; }
145 const InMemoryModuleCache &getInMemoryModuleCache()
const override {
149 std::error_code write(StringRef Path, llvm::MemoryBufferRef Buffer,
150 off_t &Size, time_t &ModTime)
override {
151 ModuleCacheEntry &Entry = getOrCreateEntry(Path);
152 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
153 Logger.log() <<
"pcm_write: " << Path;
156 assert(Entry.
WrittenBuffer->getBuffer() == Buffer.getBuffer() &&
157 "Wrote the same PCM with different contents");
163 llvm::MemoryBuffer::getMemBufferCopy(Buffer.getBuffer(), Path);
164 Entry.
ModTime = llvm::sys::toTimeT(std::chrono::system_clock::now());
171 Expected<std::unique_ptr<llvm::MemoryBuffer>>
172 read(StringRef
FileName, off_t &Size, time_t &ModTime)
override {
173 Logger.log() <<
"pcm_read_disk: " <<
FileName;
174 ModuleCacheEntry &Entry = getOrCreateEntry(
FileName);
175 std::lock_guard<std::mutex> Lock(Entry.
Mutex);
178 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
183 return ReadBuffer.takeError();
191 Size = Buffer->getBufferSize();
194 return llvm::MemoryBuffer::getMemBuffer(*Buffer,
200std::shared_ptr<ModuleCache>
203 return std::make_shared<InProcessModuleCache>(Entries, Logger);
Defines a logger where each line is written atomically to the file.
std::shared_ptr< ModuleCache > makeInProcessModuleCache(ModuleCacheEntries &Entries, AtomicLineLogger &Logger)
The JSON file list parser is used to communicate input to InstallAPI.
Expected< std::unique_ptr< llvm::MemoryBuffer > > readImpl(StringRef FileName, off_t &Size, time_t &ModTime)
Shared implementation of ModuleCache::read().
@ Success
Annotation was successful.
std::error_code writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer, off_t &Size, time_t &ModTime)
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 flush()
Flushes all PCMs built in-process to disk.
enum clang::dependencies::ModuleCacheEntry::@010254116217305143125116322142262271134241253225 State
std::condition_variable CondVar
time_t ModTime
The modification time of the entry.
std::unique_ptr< llvm::MemoryBuffer > WrittenBuffer
The buffer we've written to module cache, if any.
std::unique_ptr< llvm::MemoryBuffer > ReadBuffer
The buffer we've read from disk, if any.