Skip to content

Commit ced993b

Browse files
qiongsiwurlavaee
authored andcommitted
[clang Dependency Scanning] Enhance File Caching Diagnostics (llvm#144105)
`DependencyScanningFileSystemSharedCache` can currently report out-of-date negatively stat cached paths. This PR enhances the reporting with two modifications. 1. The reported path are now null terminated char arrays instead of `StringRef`s. This way the API's user can avoid copying `StringRef`s to other containers because the char arrays can be used directly. 2. The API now reports out-of-date cache entry due to file size changes. Specifically, we check each file's cached size against the size of the same file on the underlying FS. If the sizes are different, diagnostics will be reported. rdar://152247357
1 parent 30baa9d commit ced993b

File tree

3 files changed

+84
-23
lines changed

3 files changed

+84
-23
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Support/VirtualFileSystem.h"
1919
#include <mutex>
2020
#include <optional>
21+
#include <variant>
2122

2223
namespace clang {
2324
namespace tooling {
@@ -220,13 +221,34 @@ class DependencyScanningFilesystemSharedCache {
220221
CacheShard &getShardForFilename(StringRef Filename) const;
221222
CacheShard &getShardForUID(llvm::sys::fs::UniqueID UID) const;
222223

223-
/// Visits all cached entries and re-stat an entry using FS if
224-
/// it is negatively stat cached. If re-stat succeeds on a path,
225-
/// the path is added to InvalidPaths, indicating that the cache
226-
/// may have erroneously negatively cached it. The caller can then
227-
/// use InvalidPaths to issue diagnostics.
228-
std::vector<StringRef>
229-
getInvalidNegativeStatCachedPaths(llvm::vfs::FileSystem &UnderlyingFS) const;
224+
struct OutOfDateEntry {
225+
// A null terminated string that contains a path.
226+
const char *Path = nullptr;
227+
228+
struct NegativelyCachedInfo {};
229+
struct SizeChangedInfo {
230+
uint64_t CachedSize = 0;
231+
uint64_t ActualSize = 0;
232+
};
233+
234+
std::variant<NegativelyCachedInfo, SizeChangedInfo> Info;
235+
236+
OutOfDateEntry(const char *Path)
237+
: Path(Path), Info(NegativelyCachedInfo{}) {}
238+
239+
OutOfDateEntry(const char *Path, uint64_t CachedSize, uint64_t ActualSize)
240+
: Path(Path), Info(SizeChangedInfo{CachedSize, ActualSize}) {}
241+
};
242+
243+
/// Visits all cached entries and re-stat an entry using UnderlyingFS to check
244+
/// if the cache contains out-of-date entries. An entry can be out-of-date for
245+
/// two reasons:
246+
/// 1. The entry contains a stat error, indicating the file did not exist
247+
/// in the cache, but the file exists on the UnderlyingFS.
248+
/// 2. The entry is associated with a file whose size is different from the
249+
/// size of the file on the same path on the UnderlyingFS.
250+
std::vector<OutOfDateEntry>
251+
getOutOfDateEntries(llvm::vfs::FileSystem &UnderlyingFS) const;
230252

231253
private:
232254
std::unique_ptr<CacheShard[]> CacheShards;

clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,31 +107,39 @@ DependencyScanningFilesystemSharedCache::getShardForUID(
107107
return CacheShards[Hash % NumShards];
108108
}
109109

110-
std::vector<StringRef>
111-
DependencyScanningFilesystemSharedCache::getInvalidNegativeStatCachedPaths(
110+
std::vector<DependencyScanningFilesystemSharedCache::OutOfDateEntry>
111+
DependencyScanningFilesystemSharedCache::getOutOfDateEntries(
112112
llvm::vfs::FileSystem &UnderlyingFS) const {
113113
// Iterate through all shards and look for cached stat errors.
114-
std::vector<StringRef> InvalidPaths;
114+
std::vector<OutOfDateEntry> InvalidDiagInfo;
115115
for (unsigned i = 0; i < NumShards; i++) {
116116
const CacheShard &Shard = CacheShards[i];
117117
std::lock_guard<std::mutex> LockGuard(Shard.CacheLock);
118118
for (const auto &[Path, CachedPair] : Shard.CacheByFilename) {
119119
const CachedFileSystemEntry *Entry = CachedPair.first;
120120

121-
if (Entry->getError()) {
122-
// Only examine cached errors.
123-
llvm::ErrorOr<llvm::vfs::Status> Stat = UnderlyingFS.status(Path);
124-
if (Stat) {
121+
llvm::ErrorOr<llvm::vfs::Status> Status = UnderlyingFS.status(Path);
122+
if (Status) {
123+
if (Entry->getError()) {
125124
// This is the case where we have cached the non-existence
126-
// of the file at Path first, and a a file at the path is created
125+
// of the file at Path first, and a file at the path is created
127126
// later. The cache entry is not invalidated (as we have no good
128127
// way to do it now), which may lead to missing file build errors.
129-
InvalidPaths.push_back(Path);
128+
InvalidDiagInfo.emplace_back(Path.data());
129+
} else {
130+
llvm::vfs::Status CachedStatus = Entry->getStatus();
131+
uint64_t CachedSize = CachedStatus.getSize();
132+
uint64_t ActualSize = Status->getSize();
133+
if (CachedSize != ActualSize) {
134+
// This is the case where the cached file has a different size
135+
// from the actual file that comes from the underlying FS.
136+
InvalidDiagInfo.emplace_back(Path.data(), CachedSize, ActualSize);
137+
}
130138
}
131139
}
132140
}
133141
}
134-
return InvalidPaths;
142+
return InvalidDiagInfo;
135143
}
136144

137145
const CachedFileSystemEntry *

clang/unittests/Tooling/DependencyScanning/DependencyScanningFilesystemTest.cpp

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,49 @@ TEST(DependencyScanningFilesystem, DiagnoseStaleStatFailures) {
187187
DependencyScanningWorkerFilesystem DepFS(SharedCache, InMemoryFS);
188188

189189
bool Path1Exists = DepFS.exists("/path1.suffix");
190-
EXPECT_EQ(Path1Exists, false);
190+
ASSERT_EQ(Path1Exists, false);
191191

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

199-
std::vector<llvm::StringRef> InvalidPaths =
200-
SharedCache.getInvalidNegativeStatCachedPaths(*InMemoryFS);
199+
auto InvalidEntries = SharedCache.getOutOfDateEntries(*InMemoryFS);
201200

202-
EXPECT_EQ(InvalidPaths.size(), 1u);
203-
ASSERT_STREQ("/path1.suffix", InvalidPaths[0].str().c_str());
201+
EXPECT_EQ(InvalidEntries.size(), 1u);
202+
ASSERT_STREQ("/path1.suffix", InvalidEntries[0].Path);
203+
}
204+
205+
TEST(DependencyScanningFilesystem, DiagnoseCachedFileSizeChange) {
206+
auto InMemoryFS1 = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
207+
auto InMemoryFS2 = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
208+
InMemoryFS1->setCurrentWorkingDirectory("/");
209+
InMemoryFS2->setCurrentWorkingDirectory("/");
210+
211+
DependencyScanningFilesystemSharedCache SharedCache;
212+
DependencyScanningWorkerFilesystem DepFS(SharedCache, InMemoryFS1);
213+
214+
InMemoryFS1->addFile("/path1.suffix", 0,
215+
llvm::MemoryBuffer::getMemBuffer(""));
216+
bool Path1Exists = DepFS.exists("/path1.suffix");
217+
ASSERT_EQ(Path1Exists, true);
218+
219+
// Add a file to a new FS that has the same path but different content.
220+
InMemoryFS2->addFile("/path1.suffix", 1,
221+
llvm::MemoryBuffer::getMemBuffer(" "));
222+
223+
// Check against the new file system. InMemoryFS2 could be the underlying
224+
// physical system in the real world.
225+
auto InvalidEntries = SharedCache.getOutOfDateEntries(*InMemoryFS2);
226+
227+
ASSERT_EQ(InvalidEntries.size(), 1u);
228+
ASSERT_STREQ("/path1.suffix", InvalidEntries[0].Path);
229+
auto SizeInfo = std::get_if<
230+
DependencyScanningFilesystemSharedCache::OutOfDateEntry::SizeChangedInfo>(
231+
&InvalidEntries[0].Info);
232+
ASSERT_TRUE(SizeInfo);
233+
ASSERT_EQ(SizeInfo->CachedSize, 0u);
234+
ASSERT_EQ(SizeInfo->ActualSize, 8u);
204235
}

0 commit comments

Comments
 (0)