Skip to content

[clang][Dependency Scanning] Adding an API to Diagnose Invalid Negative Stat Cache Entries #135703

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 6 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Initial commit.
  • Loading branch information
qiongsiwu committed Apr 14, 2025
commit d4b1210c16b4fccc6faa9445bee457a1e330a025
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ class DependencyScanningFilesystemSharedCache {
CacheShard &getShardForFilename(StringRef Filename) const;
CacheShard &getShardForUID(llvm::sys::fs::UniqueID UID) const;

/// Visits all cached entries and re-stat an entry using the UnderlyingFS if
/// it is negatively stat cached. If the re-stat succeeds, print diagnostics
/// information to OS indicating that negative stat caching has been
/// invalidated.
void
diagnoseNegativeStatCachedPaths(llvm::raw_ostream &OS,
llvm::vfs::FileSystem &UnderlyingFS) const;

private:
std::unique_ptr<CacheShard[]> CacheShards;
unsigned NumShards;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,32 @@ DependencyScanningFilesystemSharedCache::getShardForUID(
return CacheShards[Hash % NumShards];
}

void DependencyScanningFilesystemSharedCache::diagnoseNegativeStatCachedPaths(
llvm::raw_ostream &OS, llvm::vfs::FileSystem &UnderlyingFS) const {
// Iterate through all shards and look for cached stat errors.
for (unsigned i = 0; i < NumShards; i++) {
const CacheShard &Shard = CacheShards[i];
for (const auto &Path : Shard.CacheByFilename.keys()) {
auto CachedPairIt = Shard.CacheByFilename.find(Path);
auto CachedPair = CachedPairIt->getValue();
const CachedFileSystemEntry *Entry = CachedPair.first;

if (Entry->getError()) {
// Only examine cached errors.
llvm::ErrorOr<llvm::vfs::Status> Stat = UnderlyingFS.status(Path);
if (Stat) {
// This is the case where we have negatively cached the non-existence
// of the file at Path, but the cache is later invalidated. Report
// diagnostics.
OS << Path << " did not exist when it was first searched "
<< "but was created later. This may have led to a build failure "
"due to missing files.\n";
}
}
}
}
}

const CachedFileSystemEntry *
DependencyScanningFilesystemSharedCache::CacheShard::findEntryByFilename(
StringRef Filename) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,31 @@ TEST(DependencyScanningFilesystem, CacheStatFailures) {
DepFS.exists("/cache/a.pcm");
EXPECT_EQ(InstrumentingFS->NumStatusCalls, 5u);
}

TEST(DependencyScanningFilesystem, DiagnoseStaleStatFailures) {
auto InMemoryFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
InMemoryFS->setCurrentWorkingDirectory("/");
InMemoryFS->addFile("/dir", 0, llvm::MemoryBuffer::getMemBuffer(""));

DependencyScanningFilesystemSharedCache SharedCache;
DependencyScanningWorkerFilesystem DepFS(SharedCache, InMemoryFS);

bool Path1Exists = DepFS.exists("/path1");
EXPECT_EQ(Path1Exists, false);

// Adding a file that has been stat-ed,
InMemoryFS->addFile("/path1", 0, llvm::MemoryBuffer::getMemBuffer(""));
Path1Exists = DepFS.exists("/path1");
// Due to caching in SharedCache, path1 should not exist in
// DepFS's eyes.
EXPECT_EQ(Path1Exists, false);

std::string Diags;
llvm::raw_string_ostream DiagsStream(Diags);
SharedCache.diagnoseNegativeStatCachedPaths(DiagsStream, *InMemoryFS.get());

ASSERT_STREQ(
"/path1 did not exist when it was first searched but was created later. "
"This may have led to a build failure due to missing files.\n",
Diags.c_str());
}
Loading