================
@@ -63,6 +65,86 @@ void ModuleCacheEntries::flush() {
   }
 }
 
+void ModuleCacheEntries::startFlushing(AtomicLineLogger &Logger) {
+  assert(!Flusher.joinable() && "Already flushing in the background");
+  Flusher = std::thread([this, &Logger] { flushInBackground(Logger); });
+}
+
+void ModuleCacheEntries::stopFlushing() {
+  if (!Flusher.joinable())
+    return;
+  {
+    std::lock_guard<std::mutex> Lock(PendingMutex);
+    StopFlushing = true;
+  }
+  PendingCondVar.notify_one();
+  Flusher.join();
+}
+
+void ModuleCacheEntries::addPending(ModuleCacheMapEntry &MapEntry) {
+  // Note: The background thread only gets started or stopped while no PCMs
+  // are being written.
+  if (!Flusher.joinable())
+    return;
+  {
+    std::lock_guard<std::mutex> Lock(PendingMutex);
+    Pending.push_back(&MapEntry);
+  }
+  PendingCondVar.notify_one();
+}
+
+void ModuleCacheEntries::flushInBackground(AtomicLineLogger &Logger) {
+  // This is a compiler-internal output, let's bypass the sandbox.
+  auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
+  std::unique_lock<std::mutex> Lock(PendingMutex);
+  while (true) {
+    PendingCondVar.wait(Lock, [&] { return StopFlushing || !Pending.empty(); 
});
+    // Flush everything that's been written before stopping.
+    if (Pending.empty())
+      return;
+    std::vector<ModuleCacheMapEntry *> Batch;
+    std::swap(Batch, Pending);
+    Lock.unlock();
+    for (ModuleCacheMapEntry *MapEntry : Batch)
+      flushEntry(*MapEntry, Logger);
+    Lock.lock();
+  }
+}
+
+void ModuleCacheEntries::flushEntry(ModuleCacheMapEntry &MapEntry,
+                                    AtomicLineLogger &Logger) {
+  StringRef Path = MapEntry.getKey();
+  ModuleCacheEntry &Entry = *MapEntry.second;
+  std::shared_ptr<const llvm::MemoryBuffer> InMemory;
+  {
+    std::lock_guard<std::mutex> Lock(Entry.Mutex);
+    if (Entry.State != ModuleCacheEntry::S_Written)
+      return;
+    InMemory = Entry.Buffer;
+  }
+
+  // The contents of an entry in the S_Written state don't change, so we can
+  // write them without holding the lock. This way, importers of the freshly
+  // built PCM don't need to wait for the IO.
+  off_t Size;
+  time_t ModTime;
+  auto Written = writeImpl(Path, *InMemory, Size, ModTime, /*ReadBack=*/true);
----------------
qiongsiwu wrote:

Are we concerned about write failures, for example, if the disk is out of 
space? I think before this change, if the dependency scanning service runs 
long, it may not run into the write failure for quite a while, until the files 
are written to disk. With our change, would we crash early if we do not check 
for errors when we, say, do `OS.flush()` in `writeImpl`? I suppose before this 
PR, the long-running service may have a better chance of producing some 
scanning results (everything is in memory till the very end where the pcms are 
flushed), while with this PR the service may crash earlier (immediately after 
the disk is full). 

I am not sure how big a concern this is though - if your disk is out of space, 
something else is probably quite wrong already. I think maybe we can handle the 
`flush` or `close` error more gracefully.

https://github.com/llvm/llvm-project/pull/242385
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to