1 //===-- Progress.cpp ------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Core/Progress.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Utility/StreamString.h"
13 #include "llvm/Support/Signposts.h"
21 using namespace lldb_private
;
23 std::atomic
<uint64_t> Progress::g_id(0);
25 // Instrument progress events with signposts when supported.
26 static llvm::ManagedStatic
<llvm::SignpostEmitter
> g_progress_signposts
;
28 Progress::Progress(std::string title
, std::string details
,
29 std::optional
<uint64_t> total
,
30 lldb_private::Debugger
*debugger
,
31 Timeout
<std::nano
> minimum_report_time
)
32 : m_total(total
.value_or(Progress::kNonDeterministicTotal
)),
33 m_minimum_report_time(minimum_report_time
),
34 m_progress_data
{title
, ++g_id
,
35 debugger
? std::optional
<user_id_t
>(debugger
->GetID())
37 m_last_report_time_ns(
38 std::chrono::nanoseconds(
39 std::chrono::steady_clock::now().time_since_epoch())
41 m_details(std::move(details
)) {
42 std::lock_guard
<std::mutex
> guard(m_mutex
);
45 // Report to the ProgressManager if that subsystem is enabled.
46 if (ProgressManager::Enabled())
47 ProgressManager::Instance().Increment(m_progress_data
);
49 // Start signpost interval right before the meaningful work starts.
50 g_progress_signposts
->startInterval(this, m_progress_data
.title
);
53 Progress::~Progress() {
54 // End signpost interval as soon as possible.
55 g_progress_signposts
->endInterval(this, m_progress_data
.title
);
57 // Make sure to always report progress completed when this object is
58 // destructed so it indicates the progress dialog/activity should go away.
59 std::lock_guard
<std::mutex
> guard(m_mutex
);
60 m_completed
= m_total
;
63 // Report to the ProgressManager if that subsystem is enabled.
64 if (ProgressManager::Enabled())
65 ProgressManager::Instance().Decrement(m_progress_data
);
68 void Progress::Increment(uint64_t amount
,
69 std::optional
<std::string
> updated_detail
) {
73 m_completed
.fetch_add(amount
, std::memory_order_relaxed
);
75 if (m_minimum_report_time
) {
76 using namespace std::chrono
;
79 uint64_t last_report_time_ns
=
80 m_last_report_time_ns
.load(std::memory_order_relaxed
);
83 now
= steady_clock::now().time_since_epoch();
84 if (now
< nanoseconds(last_report_time_ns
) + *m_minimum_report_time
)
85 return; // Too little time has passed since the last report.
87 } while (!m_last_report_time_ns
.compare_exchange_weak(
88 last_report_time_ns
, now
.count(), std::memory_order_relaxed
,
89 std::memory_order_relaxed
));
92 std::lock_guard
<std::mutex
> guard(m_mutex
);
94 m_details
= std::move(updated_detail
.value());
98 void Progress::ReportProgress() {
99 // NB: Comparisons with optional<T> rely on the fact that std::nullopt is
100 // "smaller" than zero.
101 if (m_prev_completed
>= m_total
)
102 return; // We've reported completion already.
105 std::min(m_completed
.load(std::memory_order_relaxed
), m_total
);
106 if (completed
< m_prev_completed
)
107 return; // An overflow in the m_completed counter. Just ignore these events.
109 Debugger::ReportProgress(m_progress_data
.progress_id
, m_progress_data
.title
,
110 m_details
, completed
, m_total
,
111 m_progress_data
.debugger_id
);
112 m_prev_completed
= completed
;
115 ProgressManager::ProgressManager()
116 : m_entries(), m_alarm(std::chrono::milliseconds(100)) {}
118 ProgressManager::~ProgressManager() {}
120 void ProgressManager::Initialize() {
121 assert(!InstanceImpl() && "Already initialized.");
122 InstanceImpl().emplace();
125 void ProgressManager::Terminate() {
126 assert(InstanceImpl() && "Already terminated.");
127 InstanceImpl().reset();
130 bool ProgressManager::Enabled() { return InstanceImpl().operator bool(); }
132 ProgressManager
&ProgressManager::Instance() {
133 assert(InstanceImpl() && "ProgressManager must be initialized");
134 return *InstanceImpl();
137 std::optional
<ProgressManager
> &ProgressManager::InstanceImpl() {
138 static std::optional
<ProgressManager
> g_progress_manager
;
139 return g_progress_manager
;
142 void ProgressManager::Increment(const Progress::ProgressData
&progress_data
) {
143 std::lock_guard
<std::mutex
> lock(m_entries_mutex
);
145 llvm::StringRef key
= progress_data
.title
;
146 bool new_entry
= !m_entries
.contains(key
);
147 Entry
&entry
= m_entries
[progress_data
.title
];
150 // This is a new progress event. Report progress and store the progress
152 ReportProgress(progress_data
, EventType::Begin
);
153 entry
.data
= progress_data
;
154 } else if (entry
.refcount
== 0) {
155 // This is an existing entry that was scheduled to be deleted but a new one
156 // came in before the timer expired.
157 assert(entry
.handle
!= Alarm::INVALID_HANDLE
);
159 if (!m_alarm
.Cancel(entry
.handle
)) {
160 // The timer expired before we had a chance to cancel it. We have to treat
161 // this as an entirely new progress event.
162 ReportProgress(progress_data
, EventType::Begin
);
164 // Clear the alarm handle.
165 entry
.handle
= Alarm::INVALID_HANDLE
;
168 // Regardless of how we got here, we need to bump the reference count.
172 void ProgressManager::Decrement(const Progress::ProgressData
&progress_data
) {
173 std::lock_guard
<std::mutex
> lock(m_entries_mutex
);
174 llvm::StringRef key
= progress_data
.title
;
176 auto it
= m_entries
.find(key
);
177 if (it
== m_entries
.end())
180 Entry
&entry
= it
->second
;
183 if (entry
.refcount
== 0) {
184 assert(entry
.handle
== Alarm::INVALID_HANDLE
);
186 // Copy the key to a std::string so we can pass it by value to the lambda.
187 // The underlying StringRef will not exist by the time the callback is
189 std::string key_str
= std::string(key
);
191 // Start a timer. If it expires before we see another progress event, it
193 entry
.handle
= m_alarm
.Create([=]() { Expire(key_str
); });
197 void ProgressManager::ReportProgress(
198 const Progress::ProgressData
&progress_data
, EventType type
) {
199 // The category bit only keeps track of when progress report categories have
200 // started and ended, so clear the details and reset other fields when
201 // broadcasting to it since that bit doesn't need that information.
202 const uint64_t completed
=
203 (type
== EventType::Begin
) ? 0 : Progress::kNonDeterministicTotal
;
204 Debugger::ReportProgress(progress_data
.progress_id
, progress_data
.title
, "",
205 completed
, Progress::kNonDeterministicTotal
,
206 progress_data
.debugger_id
,
207 lldb::eBroadcastBitProgressCategory
);
210 void ProgressManager::Expire(llvm::StringRef key
) {
211 std::lock_guard
<std::mutex
> lock(m_entries_mutex
);
213 // This shouldn't happen but be resilient anyway.
214 if (!m_entries
.contains(key
))
217 // A new event came in and the alarm fired before we had a chance to restart
219 if (m_entries
[key
].refcount
!= 0)
222 // We're done with this entry.
223 ReportProgress(m_entries
[key
].data
, EventType::End
);
224 m_entries
.erase(key
);