#include "lldb/Core/Progress.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Utility/StreamString.h"
#include <cstdint>
#include <mutex>
#include <optional>
using namespace lldb;
using namespace lldb_private;
std::atomic<uint64_t> Progress::g_id(0);
Progress::Progress(std::string title, std::string details,
std::optional<uint64_t> total,
lldb_private::Debugger *debugger)
: m_details(details), m_completed(0),
m_total(Progress::kNonDeterministicTotal),
m_progress_data{title, ++g_id,
std::nullopt} {
if (total)
m_total = *total;
if (debugger)
m_progress_data.debugger_id = debugger->GetID();
std::lock_guard<std::mutex> guard(m_mutex);
ReportProgress();
if (ProgressManager::Enabled())
ProgressManager::Instance().Increment(m_progress_data);
}
Progress::~Progress() {
std::lock_guard<std::mutex> guard(m_mutex);
if (!m_completed)
m_completed = m_total;
ReportProgress();
if (ProgressManager::Enabled())
ProgressManager::Instance().Decrement(m_progress_data);
}
void Progress::Increment(uint64_t amount,
std::optional<std::string> updated_detail) {
if (amount > 0) {
std::lock_guard<std::mutex> guard(m_mutex);
if (updated_detail)
m_details = std::move(updated_detail.value());
if (m_total && (amount > (m_total - m_completed)))
m_completed = m_total;
else
m_completed += amount;
ReportProgress();
}
}
void Progress::ReportProgress() {
if (!m_complete) {
m_complete = m_completed == m_total;
Debugger::ReportProgress(m_progress_data.progress_id, m_progress_data.title,
m_details, m_completed, m_total,
m_progress_data.debugger_id);
}
}
ProgressManager::ProgressManager()
: m_entries(), m_alarm(std::chrono::milliseconds(100)) {}
ProgressManager::~ProgressManager() {}
void ProgressManager::Initialize() {
assert(!InstanceImpl() && "Already initialized.");
InstanceImpl().emplace();
}
void ProgressManager::Terminate() {
assert(InstanceImpl() && "Already terminated.");
InstanceImpl().reset();
}
bool ProgressManager::Enabled() { return InstanceImpl().operator bool(); }
ProgressManager &ProgressManager::Instance() {
assert(InstanceImpl() && "ProgressManager must be initialized");
return *InstanceImpl();
}
std::optional<ProgressManager> &ProgressManager::InstanceImpl() {
static std::optional<ProgressManager> g_progress_manager;
return g_progress_manager;
}
void ProgressManager::Increment(const Progress::ProgressData &progress_data) {
std::lock_guard<std::mutex> lock(m_entries_mutex);
llvm::StringRef key = progress_data.title;
bool new_entry = !m_entries.contains(key);
Entry &entry = m_entries[progress_data.title];
if (new_entry) {
ReportProgress(progress_data, EventType::Begin);
entry.data = progress_data;
} else if (entry.refcount == 0) {
assert(entry.handle != Alarm::INVALID_HANDLE);
if (!m_alarm.Cancel(entry.handle)) {
ReportProgress(progress_data, EventType::Begin);
}
entry.handle = Alarm::INVALID_HANDLE;
}
entry.refcount++;
}
void ProgressManager::Decrement(const Progress::ProgressData &progress_data) {
std::lock_guard<std::mutex> lock(m_entries_mutex);
llvm::StringRef key = progress_data.title;
if (!m_entries.contains(key))
return;
Entry &entry = m_entries[key];
entry.refcount--;
if (entry.refcount == 0) {
assert(entry.handle == Alarm::INVALID_HANDLE);
std::string key_str = std::string(key);
entry.handle = m_alarm.Create([=]() { Expire(key_str); });
}
}
void ProgressManager::ReportProgress(
const Progress::ProgressData &progress_data, EventType type) {
const uint64_t completed =
(type == EventType::Begin) ? 0 : Progress::kNonDeterministicTotal;
Debugger::ReportProgress(progress_data.progress_id, progress_data.title, "",
completed, Progress::kNonDeterministicTotal,
progress_data.debugger_id,
lldb::eBroadcastBitProgressCategory);
}
void ProgressManager::Expire(llvm::StringRef key) {
std::lock_guard<std::mutex> lock(m_entries_mutex);
if (!m_entries.contains(key))
return;
if (m_entries[key].refcount != 0)
return;
ReportProgress(m_entries[key].data, EventType::End);
m_entries.erase(key);
}