[lldb] parallelize calling of Module::PreloadSymbols()

If LLDB index cache is enabled and everything is cached, then loading of debug
info is essentially single-threaded, because it's done from PreloadSymbols()
called from GetOrCreateModule(), which is called from a loop calling
LoadModuleAtAddress() in DynamicLoaderPOSIXDYLD. Parallelizing the entire
loop could be unsafe because of GetOrCreateModule() operating on a module
list, so instead move only the PreloadSymbols() call to Target::ModulesDidLoad()
and parallelize there, which should be safe.

This may greatly reduce the load time if the debugged program uses a large
number of binaries (as opposed to monolithic programs where this presumably
doesn't make a difference). In my specific case of LibreOffice Calc this reduces
startup time from 6s to 2s.

Differential Revision: https://reviews.llvm.org/D122975
This commit is contained in:
Luboš Luňák 2022-04-05 15:40:21 +02:00
parent 5f841c71fc
commit b7d807dbcf

View File

@ -62,6 +62,7 @@
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Support/ThreadPool.h"
#include <memory>
#include <mutex>
@ -1623,6 +1624,17 @@ void Target::NotifyModulesRemoved(lldb_private::ModuleList &module_list) {
void Target::ModulesDidLoad(ModuleList &module_list) {
const size_t num_images = module_list.GetSize();
if (m_valid && num_images) {
if (GetPreloadSymbols()) {
// Try to preload symbols in parallel.
llvm::ThreadPoolTaskGroup task_group(Debugger::GetThreadPool());
auto preload_symbols_fn = [&](size_t idx) {
ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
module_sp->PreloadSymbols();
};
for (size_t idx = 0; idx < num_images; ++idx)
task_group.async(preload_symbols_fn, idx);
task_group.wait();
}
for (size_t idx = 0; idx < num_images; ++idx) {
ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
LoadScriptingResourceForModule(module_sp, this);
@ -2170,11 +2182,6 @@ ModuleSP Target::GetOrCreateModule(const ModuleSpec &module_spec, bool notify,
});
}
// Preload symbols outside of any lock, so hopefully we can do this for
// each library in parallel.
if (GetPreloadSymbols())
module_sp->PreloadSymbols();
llvm::SmallVector<ModuleSP, 1> replaced_modules;
for (ModuleSP &old_module_sp : old_modules) {
if (m_images.GetIndexForModule(old_module_sp.get()) !=