Skip to content

Commit 0924ab7

Browse files
committed
[lldb] Implement coalescing of disjoint progress events
This implements coalescing of progress events using a timeout, as discussed in the RFC on Discourse [1]. [1] https://discourse.llvm.org/t/rfc-improve-lldb-progress-reporting/75717/
1 parent 3aae84b commit 0924ab7

File tree

4 files changed

+146
-31
lines changed

4 files changed

+146
-31
lines changed

lldb/include/lldb/Core/Progress.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLDB_CORE_PROGRESS_H
1010
#define LLDB_CORE_PROGRESS_H
1111

12+
#include "lldb/Host/Alarm.h"
1213
#include "lldb/lldb-forward.h"
1314
#include "lldb/lldb-types.h"
1415
#include "llvm/ADT/StringMap.h"
@@ -146,19 +147,31 @@ class ProgressManager {
146147
void Increment(const Progress::ProgressData &);
147148
void Decrement(const Progress::ProgressData &);
148149

150+
static void Initialize();
151+
static void Terminate();
152+
static bool Enabled();
149153
static ProgressManager &Instance();
150154

151-
private:
155+
protected:
152156
enum class EventType {
153157
Begin,
154158
End,
155159
};
156160
static void ReportProgress(const Progress::ProgressData &progress_data,
157161
EventType type);
158162

159-
llvm::StringMap<std::pair<uint64_t, Progress::ProgressData>>
160-
m_progress_category_map;
161-
std::mutex m_progress_map_mutex;
163+
static std::optional<ProgressManager> &InstanceImpl();
164+
165+
void Expire(llvm::StringRef key);
166+
struct Entry {
167+
uint64_t refcount = 0;
168+
Progress::ProgressData data;
169+
Alarm::Handle handle = Alarm::INVALID_HANDLE;
170+
};
171+
172+
Alarm m_alarm;
173+
llvm::StringMap<Entry> m_entries;
174+
std::mutex m_entries_mutex;
162175
};
163176

164177
} // namespace lldb_private

lldb/source/Core/Progress.cpp

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ Progress::Progress(std::string title, std::string details,
3535

3636
std::lock_guard<std::mutex> guard(m_mutex);
3737
ReportProgress();
38-
ProgressManager::Instance().Increment(m_progress_data);
38+
39+
// Report to the ProgressManager if that subsystem is enabled.
40+
if (ProgressManager::Enabled())
41+
ProgressManager::Instance().Increment(m_progress_data);
3942
}
4043

4144
Progress::~Progress() {
@@ -45,7 +48,10 @@ Progress::~Progress() {
4548
if (!m_completed)
4649
m_completed = m_total;
4750
ReportProgress();
48-
ProgressManager::Instance().Decrement(m_progress_data);
51+
52+
// Report to the ProgressManager if that subsystem is enabled.
53+
if (ProgressManager::Enabled())
54+
ProgressManager::Instance().Decrement(m_progress_data);
4955
}
5056

5157
void Progress::Increment(uint64_t amount,
@@ -75,45 +81,86 @@ void Progress::ReportProgress() {
7581
}
7682
}
7783

78-
ProgressManager::ProgressManager() : m_progress_category_map() {}
84+
ProgressManager::ProgressManager()
85+
: m_alarm(std::chrono::milliseconds(100)), m_entries() {}
7986

8087
ProgressManager::~ProgressManager() {}
8188

89+
void ProgressManager::Initialize() {
90+
assert(!InstanceImpl() && "Already initialized.");
91+
InstanceImpl().emplace();
92+
}
93+
94+
void ProgressManager::Terminate() {
95+
assert(InstanceImpl() && "Already terminated.");
96+
InstanceImpl().reset();
97+
}
98+
99+
bool ProgressManager::Enabled() { return InstanceImpl().operator bool(); }
100+
82101
ProgressManager &ProgressManager::Instance() {
83-
static std::once_flag g_once_flag;
84-
static ProgressManager *g_progress_manager = nullptr;
85-
std::call_once(g_once_flag, []() {
86-
// NOTE: known leak to avoid global destructor chain issues.
87-
g_progress_manager = new ProgressManager();
88-
});
89-
return *g_progress_manager;
102+
assert(InstanceImpl() && "ProgressManager must be initialized");
103+
return *InstanceImpl();
104+
}
105+
106+
std::optional<ProgressManager> &ProgressManager::InstanceImpl() {
107+
static std::optional<ProgressManager> g_progress_manager;
108+
return g_progress_manager;
90109
}
91110

92111
void ProgressManager::Increment(const Progress::ProgressData &progress_data) {
93-
std::lock_guard<std::mutex> lock(m_progress_map_mutex);
94-
// If the current category exists in the map then it is not an initial report,
95-
// therefore don't broadcast to the category bit. Also, store the current
96-
// progress data in the map so that we have a note of the ID used for the
97-
// initial progress report.
98-
if (!m_progress_category_map.contains(progress_data.title)) {
99-
m_progress_category_map[progress_data.title].second = progress_data;
112+
std::lock_guard<std::mutex> lock(m_entries_mutex);
113+
llvm::StringRef key = progress_data.title;
114+
115+
// This is a new progress event.
116+
if (!m_entries.contains(key)) {
100117
ReportProgress(progress_data, EventType::Begin);
118+
Entry &entry = m_entries[key];
119+
entry.data = progress_data;
120+
entry.refcount = 1;
121+
return;
122+
}
123+
124+
// This is an existing progress event.
125+
Entry &entry = m_entries[key];
126+
127+
// The progress event was scheduled to be deleted but a new one came in before
128+
// the timer expired.
129+
if (entry.refcount == 0) {
130+
assert(entry.handle != Alarm::INVALID_HANDLE);
131+
132+
if (!m_alarm.Cancel(entry.handle)) {
133+
// The timer expired before we had a chance to cancel it. We have to treat
134+
// this as an entirely new progress event.
135+
ReportProgress(progress_data, EventType::Begin);
136+
}
137+
entry.refcount = 1;
138+
entry.handle = Alarm::INVALID_HANDLE;
139+
} else {
140+
entry.refcount++;
101141
}
102-
m_progress_category_map[progress_data.title].first++;
103142
}
104143

105144
void ProgressManager::Decrement(const Progress::ProgressData &progress_data) {
106-
std::lock_guard<std::mutex> lock(m_progress_map_mutex);
107-
auto pos = m_progress_category_map.find(progress_data.title);
145+
std::lock_guard<std::mutex> lock(m_entries_mutex);
146+
llvm::StringRef key = progress_data.title;
108147

109-
if (pos == m_progress_category_map.end())
148+
if (!m_entries.contains(key))
110149
return;
111150

112-
if (pos->second.first <= 1) {
113-
ReportProgress(pos->second.second, EventType::End);
114-
m_progress_category_map.erase(progress_data.title);
151+
Entry &entry = m_entries[key];
152+
if (entry.refcount <= 1) {
153+
assert(entry.handle == Alarm::INVALID_HANDLE);
154+
// Start a timer. If it expires before we see another progress event, it
155+
// will be reported.
156+
entry.refcount = 0;
157+
// Copy the key to a std::string so we can pass it by value to the lambda.
158+
// The underlying StringRef will not exist by the time the callback is
159+
// called.
160+
std::string key_str = std::string(key);
161+
entry.handle = m_alarm.Create([=]() { Expire(key_str); });
115162
} else {
116-
--pos->second.first;
163+
entry.refcount--;
117164
}
118165
}
119166

@@ -129,3 +176,20 @@ void ProgressManager::ReportProgress(
129176
progress_data.debugger_id,
130177
Debugger::eBroadcastBitProgressCategory);
131178
}
179+
180+
void ProgressManager::Expire(llvm::StringRef key) {
181+
std::lock_guard<std::mutex> lock(m_entries_mutex);
182+
183+
// This shouldn't happen but be resilient anyway.
184+
if (!m_entries.contains(key))
185+
return;
186+
187+
// A new event came in and the alarm fired before we had a chance to restart
188+
// it.
189+
if (m_entries[key].refcount != 0)
190+
return;
191+
192+
// We're done with this entry.
193+
ReportProgress(m_entries[key].data, EventType::End);
194+
m_entries.erase(key);
195+
}

lldb/source/Initialization/SystemInitializerCommon.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "lldb/Initialization/SystemInitializerCommon.h"
1010

1111
#include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
12+
#include "lldb/Core/Progress.h"
1213
#include "lldb/Host/FileSystem.h"
1314
#include "lldb/Host/Host.h"
1415
#include "lldb/Host/Socket.h"
@@ -69,6 +70,7 @@ llvm::Error SystemInitializerCommon::Initialize() {
6970
Diagnostics::Initialize();
7071
FileSystem::Initialize();
7172
HostInfo::Initialize(m_shlib_dir_helper);
73+
ProgressManager::Initialize();
7274

7375
llvm::Error error = Socket::Initialize();
7476
if (error)
@@ -97,6 +99,7 @@ void SystemInitializerCommon::Terminate() {
9799
#endif
98100

99101
Socket::Terminate();
102+
ProgressManager::Terminate();
100103
HostInfo::Terminate();
101104
Log::DisableAllLogChannels();
102105
FileSystem::Terminate();

lldb/unittests/Core/ProgressReportTest.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
using namespace lldb;
2323
using namespace lldb_private;
2424

25-
static std::chrono::milliseconds TIMEOUT(100);
25+
static std::chrono::milliseconds TIMEOUT(500);
2626

2727
class ProgressReportTest : public ::testing::Test {
2828
public:
@@ -56,7 +56,8 @@ class ProgressReportTest : public ::testing::Test {
5656

5757
DebuggerSP m_debugger_sp;
5858
ListenerSP m_listener_sp;
59-
SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX> subsystems;
59+
SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX, ProgressManager>
60+
subsystems;
6061
};
6162

6263
TEST_F(ProgressReportTest, TestReportCreation) {
@@ -210,3 +211,37 @@ TEST_F(ProgressReportTest, TestOverlappingEvents) {
210211
// initial report.
211212
EXPECT_EQ(data->GetID(), expected_progress_id);
212213
}
214+
215+
TEST_F(ProgressReportTest, TestProgressManagerDisjointReports) {
216+
ListenerSP listener_sp =
217+
CreateListenerFor(Debugger::eBroadcastBitProgressCategory);
218+
EventSP event_sp;
219+
const ProgressEventData *data;
220+
uint64_t expected_progress_id;
221+
222+
{ Progress progress("Coalesced report 1", "Starting report 1"); }
223+
{ Progress progress("Coalesced report 1", "Starting report 2"); }
224+
{ Progress progress("Coalesced report 1", "Starting report 3"); }
225+
226+
ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
227+
data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
228+
expected_progress_id = data->GetID();
229+
230+
EXPECT_EQ(data->GetDetails(), "");
231+
EXPECT_FALSE(data->IsFinite());
232+
EXPECT_FALSE(data->GetCompleted());
233+
EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
234+
EXPECT_EQ(data->GetMessage(), "Coalesced report 1");
235+
236+
ASSERT_TRUE(listener_sp->GetEvent(event_sp, TIMEOUT));
237+
data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
238+
239+
EXPECT_EQ(data->GetID(), expected_progress_id);
240+
EXPECT_EQ(data->GetDetails(), "");
241+
EXPECT_FALSE(data->IsFinite());
242+
EXPECT_TRUE(data->GetCompleted());
243+
EXPECT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
244+
EXPECT_EQ(data->GetMessage(), "Coalesced report 1");
245+
246+
ASSERT_FALSE(listener_sp->GetEvent(event_sp, TIMEOUT));
247+
}

0 commit comments

Comments
 (0)