Path: blob/main/contrib/llvm-project/lldb/source/Core/Progress.cpp
39587 views
//===-- Progress.cpp ------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Core/Progress.h"910#include "lldb/Core/Debugger.h"11#include "lldb/Utility/StreamString.h"1213#include <cstdint>14#include <mutex>15#include <optional>1617using namespace lldb;18using namespace lldb_private;1920std::atomic<uint64_t> Progress::g_id(0);2122Progress::Progress(std::string title, std::string details,23std::optional<uint64_t> total,24lldb_private::Debugger *debugger)25: m_details(details), m_completed(0),26m_total(Progress::kNonDeterministicTotal),27m_progress_data{title, ++g_id,28/*m_progress_data.debugger_id=*/std::nullopt} {29if (total)30m_total = *total;3132if (debugger)33m_progress_data.debugger_id = debugger->GetID();3435std::lock_guard<std::mutex> guard(m_mutex);36ReportProgress();3738// Report to the ProgressManager if that subsystem is enabled.39if (ProgressManager::Enabled())40ProgressManager::Instance().Increment(m_progress_data);41}4243Progress::~Progress() {44// Make sure to always report progress completed when this object is45// destructed so it indicates the progress dialog/activity should go away.46std::lock_guard<std::mutex> guard(m_mutex);47if (!m_completed)48m_completed = m_total;49ReportProgress();5051// Report to the ProgressManager if that subsystem is enabled.52if (ProgressManager::Enabled())53ProgressManager::Instance().Decrement(m_progress_data);54}5556void Progress::Increment(uint64_t amount,57std::optional<std::string> updated_detail) {58if (amount > 0) {59std::lock_guard<std::mutex> guard(m_mutex);60if (updated_detail)61m_details = std::move(updated_detail.value());62// Watch out for unsigned overflow and make sure we don't increment too63// much and exceed the total.64if (m_total && (amount > (m_total - m_completed)))65m_completed = m_total;66else67m_completed += amount;68ReportProgress();69}70}7172void Progress::ReportProgress() {73if (!m_complete) {74// Make sure we only send one notification that indicates the progress is75// complete76m_complete = m_completed == m_total;77Debugger::ReportProgress(m_progress_data.progress_id, m_progress_data.title,78m_details, m_completed, m_total,79m_progress_data.debugger_id);80}81}8283ProgressManager::ProgressManager()84: m_entries(), m_alarm(std::chrono::milliseconds(100)) {}8586ProgressManager::~ProgressManager() {}8788void ProgressManager::Initialize() {89assert(!InstanceImpl() && "Already initialized.");90InstanceImpl().emplace();91}9293void ProgressManager::Terminate() {94assert(InstanceImpl() && "Already terminated.");95InstanceImpl().reset();96}9798bool ProgressManager::Enabled() { return InstanceImpl().operator bool(); }99100ProgressManager &ProgressManager::Instance() {101assert(InstanceImpl() && "ProgressManager must be initialized");102return *InstanceImpl();103}104105std::optional<ProgressManager> &ProgressManager::InstanceImpl() {106static std::optional<ProgressManager> g_progress_manager;107return g_progress_manager;108}109110void ProgressManager::Increment(const Progress::ProgressData &progress_data) {111std::lock_guard<std::mutex> lock(m_entries_mutex);112113llvm::StringRef key = progress_data.title;114bool new_entry = !m_entries.contains(key);115Entry &entry = m_entries[progress_data.title];116117if (new_entry) {118// This is a new progress event. Report progress and store the progress119// data.120ReportProgress(progress_data, EventType::Begin);121entry.data = progress_data;122} else if (entry.refcount == 0) {123// This is an existing entry that was scheduled to be deleted but a new one124// came in before the timer expired.125assert(entry.handle != Alarm::INVALID_HANDLE);126127if (!m_alarm.Cancel(entry.handle)) {128// The timer expired before we had a chance to cancel it. We have to treat129// this as an entirely new progress event.130ReportProgress(progress_data, EventType::Begin);131}132// Clear the alarm handle.133entry.handle = Alarm::INVALID_HANDLE;134}135136// Regardless of how we got here, we need to bump the reference count.137entry.refcount++;138}139140void ProgressManager::Decrement(const Progress::ProgressData &progress_data) {141std::lock_guard<std::mutex> lock(m_entries_mutex);142llvm::StringRef key = progress_data.title;143144if (!m_entries.contains(key))145return;146147Entry &entry = m_entries[key];148entry.refcount--;149150if (entry.refcount == 0) {151assert(entry.handle == Alarm::INVALID_HANDLE);152153// Copy the key to a std::string so we can pass it by value to the lambda.154// The underlying StringRef will not exist by the time the callback is155// called.156std::string key_str = std::string(key);157158// Start a timer. If it expires before we see another progress event, it159// will be reported.160entry.handle = m_alarm.Create([=]() { Expire(key_str); });161}162}163164void ProgressManager::ReportProgress(165const Progress::ProgressData &progress_data, EventType type) {166// The category bit only keeps track of when progress report categories have167// started and ended, so clear the details and reset other fields when168// broadcasting to it since that bit doesn't need that information.169const uint64_t completed =170(type == EventType::Begin) ? 0 : Progress::kNonDeterministicTotal;171Debugger::ReportProgress(progress_data.progress_id, progress_data.title, "",172completed, Progress::kNonDeterministicTotal,173progress_data.debugger_id,174lldb::eBroadcastBitProgressCategory);175}176177void ProgressManager::Expire(llvm::StringRef key) {178std::lock_guard<std::mutex> lock(m_entries_mutex);179180// This shouldn't happen but be resilient anyway.181if (!m_entries.contains(key))182return;183184// A new event came in and the alarm fired before we had a chance to restart185// it.186if (m_entries[key].refcount != 0)187return;188189// We're done with this entry.190ReportProgress(m_entries[key].data, EventType::End);191m_entries.erase(key);192}193194195