Skip to content

Commit 49ca61f

Browse files
jansvoboda11petrhosek
authored andcommitted
[clang][modules][deps] Optimize in-process timestamping of PCMs (llvm#137363)
In the past, timestamps used for `-fmodules-validate-once-per-build-session` were found to be a source of contention in the dependency scanner ([D149802](https://reviews.llvm.org/D149802), llvm#112452). This PR is yet another attempt to optimize these. We now make use of the new `ModuleCache` interface to implement the in-process version in terms of atomic `std::time_t` variables rather the mtime attribute on `.timestamp` files.
1 parent 56d80d3 commit 49ca61f

File tree

12 files changed

+109
-47
lines changed

12 files changed

+109
-47
lines changed

clang/include/clang/Serialization/ModuleCache.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "clang/Basic/LLVM.h"
1313
#include "llvm/ADT/IntrusiveRefCntPtr.h"
1414

15+
#include <ctime>
16+
1517
namespace llvm {
1618
class AdvisoryLock;
1719
} // namespace llvm
@@ -31,11 +33,23 @@ class ModuleCache : public RefCountedBase<ModuleCache> {
3133
virtual std::unique_ptr<llvm::AdvisoryLock>
3234
getLock(StringRef ModuleFilename) = 0;
3335

36+
// TODO: Abstract away timestamps with isUpToDate() and markUpToDate().
37+
// TODO: Consider exposing a "validation lock" API to prevent multiple clients
38+
// concurrently noticing an out-of-date module file and validating its inputs.
39+
40+
/// Returns the timestamp denoting the last time inputs of the module file
41+
/// were validated.
42+
virtual std::time_t getModuleTimestamp(StringRef ModuleFilename) = 0;
43+
44+
/// Updates the timestamp denoting the last time inputs of the module file
45+
/// were validated.
46+
virtual void updateModuleTimestamp(StringRef ModuleFilename) = 0;
47+
3448
/// Returns this process's view of the module cache.
3549
virtual InMemoryModuleCache &getInMemoryModuleCache() = 0;
3650
virtual const InMemoryModuleCache &getInMemoryModuleCache() const = 0;
3751

38-
// TODO: Virtualize writing/reading PCM files, timestamping, pruning, etc.
52+
// TODO: Virtualize writing/reading PCM files, pruning, etc.
3953

4054
virtual ~ModuleCache() = default;
4155
};

clang/include/clang/Tooling/DependencyScanning/DependencyScanningService.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
1313
#include "clang/Tooling/DependencyScanning/InProcessModuleCache.h"
1414
#include "llvm/ADT/BitmaskEnum.h"
15+
#include "llvm/Support/Chrono.h"
1516

1617
namespace clang {
1718
namespace tooling {
@@ -84,7 +85,9 @@ class DependencyScanningService {
8485
DependencyScanningService(
8586
ScanningMode Mode, ScanningOutputFormat Format,
8687
ScanningOptimizations OptimizeArgs = ScanningOptimizations::Default,
87-
bool EagerLoadModules = false, bool TraceVFS = false);
88+
bool EagerLoadModules = false, bool TraceVFS = false,
89+
std::time_t BuildSessionTimestamp =
90+
llvm::sys::toTimeT(std::chrono::system_clock::now()));
8891

8992
ScanningMode getMode() const { return Mode; }
9093

@@ -100,7 +103,9 @@ class DependencyScanningService {
100103
return SharedCache;
101104
}
102105

103-
ModuleCacheMutexes &getModuleCacheMutexes() { return ModCacheMutexes; }
106+
ModuleCacheEntries &getModuleCacheEntries() { return ModCacheEntries; }
107+
108+
std::time_t getBuildSessionTimestamp() const { return BuildSessionTimestamp; }
104109

105110
private:
106111
const ScanningMode Mode;
@@ -113,8 +118,10 @@ class DependencyScanningService {
113118
const bool TraceVFS;
114119
/// The global file system cache.
115120
DependencyScanningFilesystemSharedCache SharedCache;
116-
/// The global module cache mutexes.
117-
ModuleCacheMutexes ModCacheMutexes;
121+
/// The global module cache entries.
122+
ModuleCacheEntries ModCacheEntries;
123+
/// The build session timestamp.
124+
std::time_t BuildSessionTimestamp;
118125
};
119126

120127
} // end namespace dependencies

clang/include/clang/Tooling/DependencyScanning/InProcessModuleCache.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818
namespace clang {
1919
namespace tooling {
2020
namespace dependencies {
21-
struct ModuleCacheMutexes {
21+
struct ModuleCacheEntry {
22+
std::shared_mutex CompilationMutex;
23+
std::atomic<std::time_t> Timestamp = 0;
24+
};
25+
26+
struct ModuleCacheEntries {
2227
std::mutex Mutex;
23-
llvm::StringMap<std::unique_ptr<std::shared_mutex>> Map;
28+
llvm::StringMap<std::unique_ptr<ModuleCacheEntry>> Map;
2429
};
2530

2631
IntrusiveRefCntPtr<ModuleCache>
27-
makeInProcessModuleCache(ModuleCacheMutexes &Mutexes);
32+
makeInProcessModuleCache(ModuleCacheEntries &Entries);
2833
} // namespace dependencies
2934
} // namespace tooling
3035
} // namespace clang

clang/lib/Serialization/ASTCommon.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,3 @@ bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
510510
return false;
511511
return isa<TagDecl, FieldDecl>(D);
512512
}
513-
514-
void serialization::updateModuleTimestamp(StringRef ModuleFilename) {
515-
// Overwrite the timestamp file contents so that file's mtime changes.
516-
std::error_code EC;
517-
llvm::raw_fd_ostream OS(ModuleFile::getTimestampFilename(ModuleFilename), EC,
518-
llvm::sys::fs::OF_TextWithCRLF);
519-
if (EC)
520-
return;
521-
OS << "Timestamp file\n";
522-
OS.close();
523-
OS.clear_error(); // Avoid triggering a fatal error.
524-
}

clang/lib/Serialization/ASTCommon.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ inline bool isPartOfPerModuleInitializer(const Decl *D) {
100100
return false;
101101
}
102102

103-
void updateModuleTimestamp(StringRef ModuleFilename);
104-
105103
} // namespace serialization
106104

107105
} // namespace clang

clang/lib/Serialization/ASTReader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4952,7 +4952,8 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
49524952
ImportedModule &M = Loaded[I];
49534953
if (M.Mod->Kind == MK_ImplicitModule &&
49544954
M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4955-
updateModuleTimestamp(M.Mod->FileName);
4955+
getModuleManager().getModuleCache().updateModuleTimestamp(
4956+
M.Mod->FileName);
49564957
}
49574958
}
49584959

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5394,7 +5394,7 @@ ASTWriter::WriteAST(llvm::PointerUnion<Sema *, Preprocessor *> Subject,
53945394
if (WritingModule && PPRef.getHeaderSearchInfo()
53955395
.getHeaderSearchOpts()
53965396
.ModulesValidateOncePerBuildSession)
5397-
updateModuleTimestamp(OutputFile);
5397+
ModCache.updateModuleTimestamp(OutputFile);
53985398

53995399
if (ShouldCacheASTInMemory) {
54005400
// Construct MemoryBuffer and update buffer manager.

clang/lib/Serialization/ModuleCache.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "clang/Serialization/ModuleCache.h"
1010

1111
#include "clang/Serialization/InMemoryModuleCache.h"
12+
#include "clang/Serialization/ModuleFile.h"
1213
#include "llvm/Support/FileSystem.h"
1314
#include "llvm/Support/LockFileManager.h"
1415
#include "llvm/Support/Path.h"
@@ -32,6 +33,28 @@ class CrossProcessModuleCache : public ModuleCache {
3233
return std::make_unique<llvm::LockFileManager>(ModuleFilename);
3334
}
3435

36+
std::time_t getModuleTimestamp(StringRef ModuleFilename) override {
37+
std::string TimestampFilename =
38+
serialization::ModuleFile::getTimestampFilename(ModuleFilename);
39+
llvm::sys::fs::file_status Status;
40+
if (llvm::sys::fs::status(ModuleFilename, Status) != std::error_code{})
41+
return 0;
42+
return llvm::sys::toTimeT(Status.getLastModificationTime());
43+
}
44+
45+
void updateModuleTimestamp(StringRef ModuleFilename) override {
46+
// Overwrite the timestamp file contents so that file's mtime changes.
47+
std::error_code EC;
48+
llvm::raw_fd_ostream OS(
49+
serialization::ModuleFile::getTimestampFilename(ModuleFilename), EC,
50+
llvm::sys::fs::OF_TextWithCRLF);
51+
if (EC)
52+
return;
53+
OS << "Timestamp file\n";
54+
OS.close();
55+
OS.clear_error(); // Avoid triggering a fatal error.
56+
}
57+
3558
InMemoryModuleCache &getInMemoryModuleCache() override { return InMemory; }
3659
const InMemoryModuleCache &getInMemoryModuleCache() const override {
3760
return InMemory;

clang/lib/Serialization/ModuleManager.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,9 @@ ModuleManager::addModule(StringRef FileName, ModuleKind Type,
174174
NewModule->ImportLoc = ImportLoc;
175175
NewModule->InputFilesValidationTimestamp = 0;
176176

177-
if (NewModule->Kind == MK_ImplicitModule) {
178-
std::string TimestampFilename =
179-
ModuleFile::getTimestampFilename(NewModule->FileName);
180-
llvm::vfs::Status Status;
181-
// A cached stat value would be fine as well.
182-
if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
183-
NewModule->InputFilesValidationTimestamp =
184-
llvm::sys::toTimeT(Status.getLastModificationTime());
185-
}
177+
if (NewModule->Kind == MK_ImplicitModule)
178+
NewModule->InputFilesValidationTimestamp =
179+
ModCache->getModuleTimestamp(NewModule->FileName);
186180

187181
// Load the contents of the module
188182
if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {

clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ using namespace dependencies;
1414

1515
DependencyScanningService::DependencyScanningService(
1616
ScanningMode Mode, ScanningOutputFormat Format,
17-
ScanningOptimizations OptimizeArgs, bool EagerLoadModules, bool TraceVFS)
17+
ScanningOptimizations OptimizeArgs, bool EagerLoadModules, bool TraceVFS,
18+
std::time_t BuildSessionTimestamp)
1819
: Mode(Mode), Format(Format), OptimizeArgs(OptimizeArgs),
19-
EagerLoadModules(EagerLoadModules), TraceVFS(TraceVFS) {}
20+
EagerLoadModules(EagerLoadModules), TraceVFS(TraceVFS),
21+
BuildSessionTimestamp(BuildSessionTimestamp) {}

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ class DependencyScanningAction : public tooling::ToolAction {
411411
Scanned = true;
412412

413413
// Create a compiler instance to handle the actual work.
414-
auto ModCache = makeInProcessModuleCache(Service.getModuleCacheMutexes());
414+
auto ModCache = makeInProcessModuleCache(Service.getModuleCacheEntries());
415415
ScanInstanceStorage.emplace(std::move(Invocation),
416416
std::move(PCHContainerOps), ModCache.get());
417417
CompilerInstance &ScanInstance = *ScanInstanceStorage;
@@ -428,6 +428,10 @@ class DependencyScanningAction : public tooling::ToolAction {
428428
ScanInstance.getPreprocessorOpts().AllowPCHWithDifferentModulesCachePath =
429429
true;
430430

431+
if (ScanInstance.getHeaderSearchOpts().ModulesValidateOncePerBuildSession)
432+
ScanInstance.getHeaderSearchOpts().BuildSessionTimestamp =
433+
Service.getBuildSessionTimestamp();
434+
431435
ScanInstance.getFrontendOpts().GenerateGlobalModuleIndex = false;
432436
ScanInstance.getFrontendOpts().UseGlobalModuleIndex = false;
433437
// This will prevent us compiling individual modules asynchronously since

clang/lib/Tooling/DependencyScanning/InProcessModuleCache.cpp

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "clang/Serialization/InMemoryModuleCache.h"
1212
#include "llvm/Support/AdvisoryLock.h"
13+
#include "llvm/Support/Chrono.h"
1314

1415
#include <mutex>
1516

@@ -50,7 +51,7 @@ class ReaderWriterLock : public llvm::AdvisoryLock {
5051
};
5152

5253
class InProcessModuleCache : public ModuleCache {
53-
ModuleCacheMutexes &Mutexes;
54+
ModuleCacheEntries &Entries;
5455

5556
// TODO: If we changed the InMemoryModuleCache API and relied on strict
5657
// context hash, we could probably create more efficient thread-safe
@@ -59,19 +60,44 @@ class InProcessModuleCache : public ModuleCache {
5960
InMemoryModuleCache InMemory;
6061

6162
public:
62-
InProcessModuleCache(ModuleCacheMutexes &Mutexes) : Mutexes(Mutexes) {}
63+
InProcessModuleCache(ModuleCacheEntries &Entries) : Entries(Entries) {}
6364

6465
void prepareForGetLock(StringRef Filename) override {}
6566

6667
std::unique_ptr<llvm::AdvisoryLock> getLock(StringRef Filename) override {
67-
auto &Mtx = [&]() -> std::shared_mutex & {
68-
std::lock_guard<std::mutex> Lock(Mutexes.Mutex);
69-
auto &Mutex = Mutexes.Map[Filename];
70-
if (!Mutex)
71-
Mutex = std::make_unique<std::shared_mutex>();
72-
return *Mutex;
68+
auto &CompilationMutex = [&]() -> std::shared_mutex & {
69+
std::lock_guard Lock(Entries.Mutex);
70+
auto &Entry = Entries.Map[Filename];
71+
if (!Entry)
72+
Entry = std::make_unique<ModuleCacheEntry>();
73+
return Entry->CompilationMutex;
7374
}();
74-
return std::make_unique<ReaderWriterLock>(Mtx);
75+
return std::make_unique<ReaderWriterLock>(CompilationMutex);
76+
}
77+
78+
std::time_t getModuleTimestamp(StringRef Filename) override {
79+
auto &Timestamp = [&]() -> std::atomic<std::time_t> & {
80+
std::lock_guard Lock(Entries.Mutex);
81+
auto &Entry = Entries.Map[Filename];
82+
if (!Entry)
83+
Entry = std::make_unique<ModuleCacheEntry>();
84+
return Entry->Timestamp;
85+
}();
86+
87+
return Timestamp.load();
88+
}
89+
90+
void updateModuleTimestamp(StringRef Filename) override {
91+
// Note: This essentially replaces FS contention with mutex contention.
92+
auto &Timestamp = [&]() -> std::atomic<std::time_t> & {
93+
std::lock_guard Lock(Entries.Mutex);
94+
auto &Entry = Entries.Map[Filename];
95+
if (!Entry)
96+
Entry = std::make_unique<ModuleCacheEntry>();
97+
return Entry->Timestamp;
98+
}();
99+
100+
Timestamp.store(llvm::sys::toTimeT(std::chrono::system_clock::now()));
75101
}
76102

77103
InMemoryModuleCache &getInMemoryModuleCache() override { return InMemory; }
@@ -82,6 +108,6 @@ class InProcessModuleCache : public ModuleCache {
82108
} // namespace
83109

84110
IntrusiveRefCntPtr<ModuleCache>
85-
dependencies::makeInProcessModuleCache(ModuleCacheMutexes &Mutexes) {
86-
return llvm::makeIntrusiveRefCnt<InProcessModuleCache>(Mutexes);
111+
dependencies::makeInProcessModuleCache(ModuleCacheEntries &Entries) {
112+
return llvm::makeIntrusiveRefCnt<InProcessModuleCache>(Entries);
87113
}

0 commit comments

Comments
 (0)