Skip to content

[clang-scan-deps] Fix contention when updating TrackingStatistics in hot code paths in FileManager. #88427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clang/include/clang/Basic/FileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ class FileManager : public RefCountedBase<FileManager> {
///
unsigned NextFileUID;

/// Statistics gathered during the lifetime of the FileManager.
unsigned NumDirLookups = 0;
unsigned NumFileLookups = 0;
unsigned NumDirCacheMisses = 0;
unsigned NumFileCacheMisses = 0;

// Caching.
std::unique_ptr<FileSystemStatCache> StatCache;

Expand Down Expand Up @@ -341,6 +347,10 @@ class FileManager : public RefCountedBase<FileManager> {

public:
void PrintStats() const;

/// Import statistics from a child FileManager and add them to this current
/// FileManager.
void AddStats(const FileManager &Other);
};

} // end namespace clang
Expand Down
14 changes: 8 additions & 6 deletions clang/lib/Basic/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ using namespace clang;

#define DEBUG_TYPE "file-search"

ALWAYS_ENABLED_STATISTIC(NumDirLookups, "Number of directory lookups.");
ALWAYS_ENABLED_STATISTIC(NumFileLookups, "Number of file lookups.");
ALWAYS_ENABLED_STATISTIC(NumDirCacheMisses,
"Number of directory cache misses.");
ALWAYS_ENABLED_STATISTIC(NumFileCacheMisses, "Number of file cache misses.");

//===----------------------------------------------------------------------===//
// Common logic.
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -656,6 +650,14 @@ StringRef FileManager::getCanonicalName(const void *Entry, StringRef Name) {
return CanonicalName;
}

void FileManager::AddStats(const FileManager &Other) {
assert(&Other != this && "Collecting stats into the same FileManager");
NumDirLookups += Other.NumDirLookups;
NumFileLookups += Other.NumFileLookups;
NumDirCacheMisses += Other.NumDirCacheMisses;
NumFileCacheMisses += Other.NumFileCacheMisses;
}

void FileManager::PrintStats() const {
llvm::errs() << "\n*** File Manager Stats:\n";
llvm::errs() << UniqueRealFiles.size() << " real files found, "
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,10 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
diag::remark_module_build_done)
<< ModuleName;

// Propagate the statistics to the parent FileManager.
if (!FrontendOpts.ModulesShareFileManager)
ImportingInstance.getFileManager().AddStats(Instance.getFileManager());

if (Crashed) {
// Clear the ASTConsumer if it hasn't been already, in case it owns streams
// that must be closed before clearing output files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ class DependencyScanningAction : public tooling::ToolAction {
if (Result)
setLastCC1Arguments(std::move(OriginalInvocation));

// Propagate the statistics to the parent FileManager.
DriverFileMgr->AddStats(ScanInstance.getFileManager());

return Result;
}

Expand Down