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 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
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 FS if
/// it is negatively stat cached. If re-stat succeeds on a path,
/// the path is added to InvalidPaths, indicating that the cache
/// may have erroneously negatively cached it. The caller can then
/// use InvalidPaths to issue diagnostics.
std::vector<StringRef>
getInvalidNegativeStatCachedPaths(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,33 @@ DependencyScanningFilesystemSharedCache::getShardForUID(
return CacheShards[Hash % NumShards];
}

std::vector<StringRef>
DependencyScanningFilesystemSharedCache::getInvalidNegativeStatCachedPaths(
llvm::vfs::FileSystem &UnderlyingFS) const {
// Iterate through all shards and look for cached stat errors.
std::vector<StringRef> InvalidPaths;
for (unsigned i = 0; i < NumShards; i++) {
const CacheShard &Shard = CacheShards[i];
std::lock_guard<std::mutex> LockGuard(Shard.CacheLock);
for (const auto &[Path, CachedPair] : Shard.CacheByFilename) {
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 cached the non-existence
// of the file at Path first, and a a file at the path is created
// later. The cache entry is not invalidated (as we have no good
// way to do it now), which may lead to missing file build errors.
InvalidPaths.push_back(Path);
}
}
}
}
return InvalidPaths;
}

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,27 @@ 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("/");

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::vector<llvm::StringRef> InvalidPaths =
SharedCache.getInvalidNegativeStatCachedPaths(*InMemoryFS.get());

EXPECT_EQ(InvalidPaths.size(), 1u);
ASSERT_STREQ("/path1", InvalidPaths[0].str().c_str());
}
Loading