llvm-project/lldb/unittests/Core/SourceManagerTest.cpp
Jonas Devlieghere 51944e78bb
[lldb] Add two-level caching in the source manager
We recently saw an uptick in internal reports complaining that LLDB is
slow when sources on network file systems are inaccessible. I looked at
the SourceManger and its cache and I think there’s some room for
improvement in terms of reducing file system accesses:

 1. We always resolve the path.
 2. We always check the timestamp.
 3. We always recheck the file system for negative cache hits.

D153726 fixes (1) but (2) and (3) are necessary because of the cache’s
current design. Source files are cached at the debugger level which
means that the source file cache can span multiple targets and
processes. It wouldn't be correct to not reload a modified or new file
from disk.

We can however significantly reduce the number of file system accesses
by using a two level cache design: one cache at the debugger level and
one at the process level:

 - The cache at the debugger level works the way it does today. There is
   no negative cache: if we can't find the file on disk, we'll try again
   next time the cache is queried. If a cached file's timestamp changes
   or if its path remapping changes, the cached file is evicted and we
   reload it from disk.
 - The cache at the process level is design to avoid accessing the file
   system. It doesn't check the file's modification time. It caches
   negative results, so if a file didn't exist, it doesn't try to reread
   it from disk. Checking if the path remapping changed is cheap
   (doesn't involve checking the file system) and is the only way for a
   file to get evicted from the process cache.

The result of this patch is that LLDB will not show you new content if a
file is modified or created while a process is running. I would argue
that this is what most people would expect, but it is a change from how
LLDB behaves today.

For an average stop, we query the source cache 4 times. With the current
implementation, that's 4 stats to get the modification time, If the file
doesn't exist on disk, that's an additional 4 stats. Before D153726, if
the path starts with a ~ there are another additional 4 calls to
realpath. When debugging sources on a slow (network) file system, this
quickly adds up.

In addition to the two level caching, this patch also adds a source
logging channel and synchronization to the source file cache. The
logging was helpful during development and hopefully will help us triage
issues in the future. The synchronization isn't a new requirement: as
the cache is shared across targets, there is no guarantees that it can't
be accessed concurrently. The patch also fixes a bug where we would only
set the source remapping ID if the un-remapped file didn't exist, which
led to the file getting evicted from the cache on every access.

rdar://110787562

Differential revision: https://reviews.llvm.org/D153834
2023-07-03 14:12:39 -07:00

77 lines
2.4 KiB
C++

//===-- SourceManagerTest.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/SourceManager.h"
#include "lldb/Host/FileSystem.h"
#include "gtest/gtest.h"
#include "TestingSupport/MockTildeExpressionResolver.h"
using namespace lldb;
using namespace lldb_private;
class SourceFileCache : public ::testing::Test {
public:
void SetUp() override {
FileSystem::Initialize(std::unique_ptr<TildeExpressionResolver>(
new MockTildeExpressionResolver("Jonas", "/jonas")));
}
void TearDown() override { FileSystem::Terminate(); }
};
TEST_F(SourceFileCache, FindSourceFileFound) {
SourceManager::SourceFileCache cache;
// Insert: foo
FileSpec foo_file_spec("foo");
auto foo_file_sp =
std::make_shared<SourceManager::File>(foo_file_spec, lldb::DebuggerSP());
cache.AddSourceFile(foo_file_spec, foo_file_sp);
// Query: foo, expect found.
FileSpec another_foo_file_spec("foo");
ASSERT_EQ(cache.FindSourceFile(another_foo_file_spec), foo_file_sp);
}
TEST_F(SourceFileCache, FindSourceFileNotFound) {
SourceManager::SourceFileCache cache;
// Insert: foo
FileSpec foo_file_spec("foo");
auto foo_file_sp =
std::make_shared<SourceManager::File>(foo_file_spec, lldb::DebuggerSP());
cache.AddSourceFile(foo_file_spec, foo_file_sp);
// Query: bar, expect not found.
FileSpec bar_file_spec("bar");
ASSERT_EQ(cache.FindSourceFile(bar_file_spec), nullptr);
}
TEST_F(SourceFileCache, FindSourceFileByUnresolvedPath) {
SourceManager::SourceFileCache cache;
FileSpec foo_file_spec("~/foo");
// Mimic the resolution in SourceManager::GetFile.
FileSpec resolved_foo_file_spec = foo_file_spec;
FileSystem::Instance().Resolve(resolved_foo_file_spec);
// Create the file with the resolved file spec.
auto foo_file_sp = std::make_shared<SourceManager::File>(
resolved_foo_file_spec, lldb::DebuggerSP());
// Cache the result with the unresolved file spec.
cache.AddSourceFile(foo_file_spec, foo_file_sp);
// Query the unresolved path.
EXPECT_EQ(cache.FindSourceFile(FileSpec("~/foo")), foo_file_sp);
// Query the resolved path.
EXPECT_EQ(cache.FindSourceFile(FileSpec("/jonas/foo")), foo_file_sp);
}