Skip to content

Commit 6a55169

Browse files
authored
Merge pull request #8483 from apple/jdevlieghere/progress-reports
🍒 Progress Report Cherrypicks
2 parents 5e0ae86 + 0067c04 commit 6a55169

File tree

10 files changed

+784
-154
lines changed

10 files changed

+784
-154
lines changed

lldb/include/lldb/Core/Progress.h

Lines changed: 37 additions & 5 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"
@@ -150,14 +151,45 @@ class ProgressManager {
150151
void Increment(const Progress::ProgressData &);
151152
void Decrement(const Progress::ProgressData &);
152153

154+
static void Initialize();
155+
static void Terminate();
156+
static bool Enabled();
153157
static ProgressManager &Instance();
154158

155-
static void ReportProgress(const Progress::ProgressData &);
159+
protected:
160+
enum class EventType {
161+
Begin,
162+
End,
163+
};
164+
static void ReportProgress(const Progress::ProgressData &progress_data,
165+
EventType type);
156166

157-
private:
158-
llvm::StringMap<std::pair<uint64_t, Progress::ProgressData>>
159-
m_progress_category_map;
160-
std::mutex m_progress_map_mutex;
167+
static std::optional<ProgressManager> &InstanceImpl();
168+
169+
/// Helper function for reporting progress when the alarm in the corresponding
170+
/// entry in the map expires.
171+
void Expire(llvm::StringRef key);
172+
173+
/// Entry used for bookkeeping.
174+
struct Entry {
175+
/// Reference count used for overlapping events.
176+
uint64_t refcount = 0;
177+
178+
/// Data used to emit progress events.
179+
Progress::ProgressData data;
180+
181+
/// Alarm handle used when the refcount reaches zero.
182+
Alarm::Handle handle = Alarm::INVALID_HANDLE;
183+
};
184+
185+
/// Map used for bookkeeping.
186+
llvm::StringMap<Entry> m_entries;
187+
188+
/// Mutex to provide the map.
189+
std::mutex m_entries_mutex;
190+
191+
/// Alarm instance to coalesce progress events.
192+
Alarm m_alarm;
161193
};
162194

163195
} // namespace lldb_private

lldb/include/lldb/Host/Alarm.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//===-- Alarm.h -------------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_HOST_ALARM_H
10+
#define LLDB_HOST_ALARM_H
11+
12+
#include "lldb/Host/HostThread.h"
13+
#include "lldb/lldb-types.h"
14+
#include "llvm/Support/Chrono.h"
15+
16+
#include <condition_variable>
17+
#include <mutex>
18+
19+
namespace lldb_private {
20+
21+
/// \class Alarm abstraction that enables scheduling a callback function after a
22+
/// specified timeout. Creating an alarm for a callback returns a Handle that
23+
/// can be used to restart or cancel the alarm.
24+
class Alarm {
25+
public:
26+
using Handle = uint64_t;
27+
using Callback = std::function<void()>;
28+
using TimePoint = llvm::sys::TimePoint<>;
29+
using Duration = std::chrono::milliseconds;
30+
31+
Alarm(Duration timeout, bool run_callback_on_exit = false);
32+
~Alarm();
33+
34+
/// Create an alarm for the given callback. The alarm will expire and the
35+
/// callback will be called after the timeout.
36+
///
37+
/// \returns
38+
/// Handle which can be used to restart or cancel the alarm.
39+
Handle Create(Callback callback);
40+
41+
/// Restart the alarm for the given Handle. The alarm will expire and the
42+
/// callback will be called after the timeout.
43+
///
44+
/// \returns
45+
/// True if the alarm was successfully restarted. False if there is no alarm
46+
/// for the given Handle or the alarm already expired.
47+
bool Restart(Handle handle);
48+
49+
/// Cancel the alarm for the given Handle. The alarm and its handle will be
50+
/// removed.
51+
///
52+
/// \returns
53+
/// True if the alarm was successfully canceled and the Handle removed.
54+
/// False if there is no alarm for the given Handle or the alarm already
55+
/// expired.
56+
bool Cancel(Handle handle);
57+
58+
static constexpr Handle INVALID_HANDLE = 0;
59+
60+
private:
61+
/// Helper functions to start, stop and check the status of the alarm thread.
62+
/// @{
63+
void StartAlarmThread();
64+
void StopAlarmThread();
65+
bool AlarmThreadRunning();
66+
/// @}
67+
68+
/// Return an unique, monotonically increasing handle.
69+
static Handle GetNextUniqueHandle();
70+
71+
/// Helper to compute the next time the alarm thread needs to wake up.
72+
TimePoint GetNextExpiration() const;
73+
74+
/// Alarm entry.
75+
struct Entry {
76+
Handle handle;
77+
Callback callback;
78+
TimePoint expiration;
79+
80+
Entry(Callback callback, TimePoint expiration);
81+
bool operator==(const Entry &rhs) { return handle == rhs.handle; }
82+
};
83+
84+
/// List of alarm entries.
85+
std::vector<Entry> m_entries;
86+
87+
/// Timeout between when an alarm is created and when it fires.
88+
Duration m_timeout;
89+
90+
/// The alarm thread.
91+
/// @{
92+
HostThread m_alarm_thread;
93+
lldb::thread_result_t AlarmThread();
94+
/// @}
95+
96+
/// Synchronize access between the alarm thread and the main thread.
97+
std::mutex m_alarm_mutex;
98+
99+
/// Condition variable used to wake up the alarm thread.
100+
std::condition_variable m_alarm_cv;
101+
102+
/// Flag to signal the alarm thread that something changed and we need to
103+
/// recompute the next alarm.
104+
bool m_recompute_next_alarm = false;
105+
106+
/// Flag to signal the alarm thread to exit.
107+
bool m_exit = false;
108+
109+
/// Flag to signal we should run all callbacks on exit.
110+
bool m_run_callbacks_on_exit = false;
111+
};
112+
113+
} // namespace lldb_private
114+
115+
#endif // LLDB_HOST_ALARM_H

lldb/source/API/SystemInitializerFull.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "lldb/API/SBCommandInterpreter.h"
1111
#include "lldb/Core/Debugger.h"
1212
#include "lldb/Core/PluginManager.h"
13+
#include "lldb/Core/Progress.h"
1314
#include "lldb/Host/Config.h"
1415
#include "lldb/Host/Host.h"
1516
#include "lldb/Initialization/SystemInitializerCommon.h"
@@ -57,6 +58,7 @@ llvm::Error SystemInitializerFull::Initialize() {
5758
llvm::InitializeAllAsmPrinters();
5859
llvm::InitializeAllTargetMCs();
5960
llvm::InitializeAllDisassemblers();
61+
6062
// Initialize the command line parser in LLVM. This usually isn't necessary
6163
// as we aren't dealing with command line options here, but otherwise some
6264
// other code in Clang/LLVM might be tempted to call this function from a
@@ -65,13 +67,16 @@ llvm::Error SystemInitializerFull::Initialize() {
6567
const char *arg0 = "lldb";
6668
llvm::cl::ParseCommandLineOptions(1, &arg0);
6769

70+
// Initialize the progress manager.
71+
ProgressManager::Initialize();
72+
6873
#define LLDB_PLUGIN(p) LLDB_PLUGIN_INITIALIZE(p);
6974
#include "Plugins/Plugins.def"
7075

7176
// Initialize plug-ins in core LLDB
7277
ProcessTrace::Initialize();
7378

74-
// Scan for any system or user LLDB plug-ins
79+
// Scan for any system or user LLDB plug-ins.
7580
PluginManager::Initialize();
7681

7782
// The process settings need to know about installed plug-ins, so the
@@ -87,15 +92,18 @@ llvm::Error SystemInitializerFull::Initialize() {
8792
void SystemInitializerFull::Terminate() {
8893
Debugger::SettingsTerminate();
8994

90-
// Terminate plug-ins in core LLDB
95+
// Terminate plug-ins in core LLDB.
9196
ProcessTrace::Terminate();
9297

93-
// Terminate and unload and loaded system or user LLDB plug-ins
98+
// Terminate and unload and loaded system or user LLDB plug-ins.
9499
PluginManager::Terminate();
95100

96101
#define LLDB_PLUGIN(p) LLDB_PLUGIN_TERMINATE(p);
97102
#include "Plugins/Plugins.def"
98103

104+
// Terminate the progress manager.
105+
ProgressManager::Terminate();
106+
99107
// Now shutdown the common parts, in reverse order.
100108
SystemInitializerCommon::Terminate();
101109
}

lldb/source/Core/Progress.cpp

Lines changed: 96 additions & 32 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,55 +81,113 @@ void Progress::ReportProgress() {
7581
}
7682
}
7783

78-
ProgressManager::ProgressManager() : m_progress_category_map() {}
84+
ProgressManager::ProgressManager()
85+
: m_entries(), m_alarm(std::chrono::milliseconds(100)) {}
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;
100-
ReportProgress(progress_data);
112+
std::lock_guard<std::mutex> lock(m_entries_mutex);
113+
114+
llvm::StringRef key = progress_data.title;
115+
bool new_entry = !m_entries.contains(key);
116+
Entry &entry = m_entries[progress_data.title];
117+
118+
if (new_entry) {
119+
// This is a new progress event. Report progress and store the progress
120+
// data.
121+
ReportProgress(progress_data, EventType::Begin);
122+
entry.data = progress_data;
123+
} else if (entry.refcount == 0) {
124+
// This is an existing entry that was scheduled to be deleted but a new one
125+
// came in before the timer expired.
126+
assert(entry.handle != Alarm::INVALID_HANDLE);
127+
128+
if (!m_alarm.Cancel(entry.handle)) {
129+
// The timer expired before we had a chance to cancel it. We have to treat
130+
// this as an entirely new progress event.
131+
ReportProgress(progress_data, EventType::Begin);
132+
}
133+
// Clear the alarm handle.
134+
entry.handle = Alarm::INVALID_HANDLE;
101135
}
102-
m_progress_category_map[progress_data.title].first++;
136+
137+
// Regardless of how we got here, we need to bump the reference count.
138+
entry.refcount++;
103139
}
104140

105141
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);
142+
std::lock_guard<std::mutex> lock(m_entries_mutex);
143+
llvm::StringRef key = progress_data.title;
108144

109-
if (pos == m_progress_category_map.end())
145+
if (!m_entries.contains(key))
110146
return;
111147

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

120165
void ProgressManager::ReportProgress(
121-
const Progress::ProgressData &progress_data) {
166+
const Progress::ProgressData &progress_data, EventType type) {
122167
// The category bit only keeps track of when progress report categories have
123168
// started and ended, so clear the details and reset other fields when
124169
// broadcasting to it since that bit doesn't need that information.
125-
Debugger::ReportProgress(
126-
progress_data.progress_id, progress_data.title, "",
127-
Progress::kNonDeterministicTotal, Progress::kNonDeterministicTotal,
128-
progress_data.debugger_id, Debugger::eBroadcastBitProgressCategory);
170+
const uint64_t completed =
171+
(type == EventType::Begin) ? 0 : Progress::kNonDeterministicTotal;
172+
Debugger::ReportProgress(progress_data.progress_id, progress_data.title, "",
173+
completed, Progress::kNonDeterministicTotal,
174+
progress_data.debugger_id,
175+
Debugger::eBroadcastBitProgressCategory);
176+
}
177+
178+
void ProgressManager::Expire(llvm::StringRef key) {
179+
std::lock_guard<std::mutex> lock(m_entries_mutex);
180+
181+
// This shouldn't happen but be resilient anyway.
182+
if (!m_entries.contains(key))
183+
return;
184+
185+
// A new event came in and the alarm fired before we had a chance to restart
186+
// it.
187+
if (m_entries[key].refcount != 0)
188+
return;
189+
190+
// We're done with this entry.
191+
ReportProgress(m_entries[key].data, EventType::End);
192+
m_entries.erase(key);
129193
}

0 commit comments

Comments
 (0)