[clang] 635f308 - [clang][Modules] Refresh a module file's access time when it is read (#239747)

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 11:57:54 PST 2026


Author: Qiongsi Wu
Date: 2026-11-07T11:57:49-08:00
New Revision: 635f30899dd0914fa89f0b3dc9a9e548e6d0ceec

URL: https://github.com/llvm/llvm-project/commit/635f30899dd0914fa89f0b3dc9a9e548e6d0ceec
DIFF: https://github.com/llvm/llvm-project/commit/635f30899dd0914fa89f0b3dc9a9e548e6d0ceec.diff

LOG: [clang][Modules] Refresh a module file's access time when it is read (#239747)

Added: 
    

Modified: 
    clang/lib/Serialization/ModuleCache.cpp
    clang/unittests/Serialization/ModuleCacheTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Serialization/ModuleCache.cpp b/clang/lib/Serialization/ModuleCache.cpp
index 8cb860074faf97..835c6e76ab4f76 100644
--- a/clang/lib/Serialization/ModuleCache.cpp
+++ b/clang/lib/Serialization/ModuleCache.cpp
@@ -18,6 +18,8 @@
 #include "llvm/Support/LockFileManager.h"
 #include "llvm/Support/Path.h"
 
+#include <chrono>
+
 using namespace clang;
 
 const ModuleCacheDirectory *ModuleCache::getDirectoryPtr(StringRef Path) {
@@ -200,6 +202,32 @@ std::error_code clang::writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer,
   return {};
 }
 
+/// The access time of a module file is only refreshed explicitly when it is
+/// older than this interval so we avoid frequent yet unnecessary updates.
+static constexpr std::chrono::seconds AccessTimeRefreshInterval(60);
+
+/// Record that the module file open as \p FD has just been used by setting its
+/// access time to now, if it is older than AccessTimeRefreshInterval. Module
+/// cache pruning goes by access time, and file systems don't reliably update it
+/// when a file is read (e.g., Linux relatime).
+static void
+refreshAccessTime([[maybe_unused]] llvm::sys::fs::file_t FD,
+                  [[maybe_unused]] const llvm::sys::fs::file_status &Status) {
+// FIXME: Support Windows. The handle (FD) is opened for reading only, and
+// SetFileTime needs one with FILE_WRITE_ATTRIBUTES access.
+#ifndef _WIN32
+  llvm::sys::TimePoint<> Now = std::chrono::system_clock::now();
+  if (Now - Status.getLastAccessedTime() <= AccessTimeRefreshInterval)
+    return;
+
+  // Keep the modification time: modules importing this one record it.
+  // Go through the descriptor so a module file replaced meanwhile is left
+  // alone.
+  (void)llvm::sys::fs::setLastAccessAndModificationTime(
+      FD.get(), Now, Status.getLastModificationTime());
+#endif
+}
+
 Expected<std::unique_ptr<llvm::MemoryBuffer>>
 clang::readImpl(StringRef FileName, off_t &Size, time_t &ModTime) {
   Expected<llvm::sys::fs::file_t> FD =
@@ -217,6 +245,7 @@ clang::readImpl(StringRef FileName, off_t &Size, time_t &ModTime) {
     return llvm::errorCodeToError(Buf.getError());
   Size = Status.getSize();
   ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
+  refreshAccessTime(*FD, Status);
   return std::move(*Buf);
 }
 

diff  --git a/clang/unittests/Serialization/ModuleCacheTest.cpp b/clang/unittests/Serialization/ModuleCacheTest.cpp
index cadded504c2967..a8e7abfca4ab1a 100644
--- a/clang/unittests/Serialization/ModuleCacheTest.cpp
+++ b/clang/unittests/Serialization/ModuleCacheTest.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "clang/Serialization/ModuleCache.h"
 #include "clang/Basic/AllDiagnostics.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Driver/CreateInvocationFromArgs.h"
@@ -16,6 +17,7 @@
 #include "clang/Lex/HeaderSearch.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -31,7 +33,7 @@ class ModuleCacheTest : public ::testing::Test {
     ASSERT_FALSE(sys::fs::createUniqueDirectory("modulecache-test", TestDir));
 
     ModuleCachePath = SmallString<256>(TestDir);
-    sys::path::append(ModuleCachePath, "mcp");
+    sys::path::append(ModuleCachePath, ModuleCacheDir);
     ASSERT_FALSE(sys::fs::create_directories(ModuleCachePath));
   }
 
@@ -40,6 +42,7 @@ class ModuleCacheTest : public ::testing::Test {
 public:
   SmallString<256> TestDir;
   SmallString<256> ModuleCachePath;
+  static constexpr StringRef ModuleCacheDir = "mcp";
 
   void addFile(StringRef Path, StringRef Contents) {
     ASSERT_FALSE(sys::path::is_absolute(Path));
@@ -295,4 +298,35 @@ TEST_F(ModuleCacheTest, RebuildFinalizedModuleAfterInputChange) {
             (unsigned)diag::note_ast_file_input_files_validation_status);
 }
 
+// Test that even for a small file not mmapped, the access time is still updated
+// upon read. Accurate atime is necessary for clang's pruning logic.
+#ifndef _WIN32
+TEST_F(ModuleCacheTest, ReadRefreshesAccessTime) {
+  SmallString<256> RelPath(ModuleCacheDir);
+  sys::path::append(RelPath, "Small.pcm");
+  addFile(RelPath, "not really a module file");
+  SmallString<256> Path(TestDir);
+  sys::path::append(Path, RelPath);
+
+  using namespace std::chrono;
+  sys::TimePoint<> Now = time_point_cast<seconds>(system_clock::now());
+  sys::TimePoint<> OldModTime = Now - hours(24 * 10);
+  sys::TimePoint<> OldAccessTime = Now - hours(2);
+  ASSERT_FALSE(sys::fs::setLastAccessAndModificationTime(Path, OldAccessTime,
+                                                         OldModTime));
+
+  off_t Size;
+  time_t ModTime;
+  Expected<std::unique_ptr<MemoryBuffer>> Buf = readImpl(Path, Size, ModTime);
+  ASSERT_TRUE(static_cast<bool>(Buf));
+  EXPECT_EQ(Size, 24);
+  EXPECT_EQ(ModTime, sys::toTimeT(OldModTime));
+
+  sys::fs::file_status Status;
+  ASSERT_FALSE(sys::fs::status(Path, Status));
+  EXPECT_GE(Status.getLastAccessedTime(), Now);
+  EXPECT_EQ(Status.getLastModificationTime(), OldModTime);
+}
+#endif
+
 } // anonymous namespace


        


More information about the cfe-commits mailing list