https://github.com/qiongsiwu updated https://github.com/llvm/llvm-project/pull/239747
>From eb86699ce00c56aa2e1685993a1244a2c9c7cbf3 Mon Sep 17 00:00:00 2001 From: Qiongsi Wu <[email protected]> Date: Thu, 5 Nov 2026 17:48:10 -0800 Subject: [PATCH] [clang][Modules] Refresh a module file's access time when it is read Update atime when a module is read from disk. Accurate atime is necessary for module cache pruning (`-fmodules-prune-after`). Most of the time, the module files are loaded through `mmap` which updates `atime`. This PR closes a gap where in some cases (e.g. `mmap` does not update `atime` due to file system settings, or the file is too small to be `mmap`-ed), `atime` is not updated upon a read. The scope is limited to what's relevant to module pruning, because it is not clear we should force an `atime` update for all kinds of different files `clang` may read. Assisted-by: Claude Code Opus 5.5 --- clang/lib/Serialization/ModuleCache.cpp | 29 +++++++++++++++ .../Serialization/ModuleCacheTest.cpp | 36 ++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) 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 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
