llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-modules Author: Qiongsi Wu (qiongsiwu) <details> <summary>Changes</summary> 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 --- Full diff: https://github.com/llvm/llvm-project/pull/239747.diff 2 Files Affected: - (modified) clang/lib/Serialization/ModuleCache.cpp (+33) - (modified) clang/unittests/Serialization/ModuleCacheTest.cpp (+35-1) ``````````diff diff --git a/clang/lib/Serialization/ModuleCache.cpp b/clang/lib/Serialization/ModuleCache.cpp index 8cb860074faf97..c6e36c6b828055 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,36 @@ 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 bringing +/// its access time up to date, so we can prune the pcms by access time more +/// reliably. Usually modules are mmapped, and on some systems (e.g., macOS) +/// mmapping a file updates its access time. When a module is read rather than +/// mmapped, or the file system updates access times lazily (e.g., Linux +/// relatime) or not at all (noatime), this closes the gap. Not implemented on +/// Windows. +static void refreshAccessTime([[maybe_unused]] llvm::sys::fs::file_t FD) { +#ifndef _WIN32 + // Stat again: mapping the file may already have updated the access time. + llvm::sys::fs::file_status Status; + if (llvm::sys::fs::status(FD, Status)) + return; + + 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 +249,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); 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 `````````` </details> https://github.com/llvm/llvm-project/pull/239747 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
