Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Trace/intel-pt/TaskTimer.h
39645 views
//===-- TaskTimer.h ---------------------------------------------*- C++ -*-===//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#ifndef LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H9#define LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H1011#include "lldb/lldb-types.h"12#include "llvm/ADT/DenseMap.h"13#include "llvm/ADT/StringRef.h"14#include <chrono>15#include <functional>16#include <unordered_map>1718namespace lldb_private {19namespace trace_intel_pt {2021/// Class used to track the duration of long running tasks related to a single22/// scope for reporting.23class ScopedTaskTimer {24public:25/// Execute the given \p task and record its duration.26///27/// \param[in] name28/// The name used to identify this task for reporting.29///30/// \param[in] task31/// The task function.32///33/// \return34/// The return value of the task.35template <typename C> auto TimeTask(llvm::StringRef name, C task) {36auto start = std::chrono::steady_clock::now();37auto result = task();38auto end = std::chrono::steady_clock::now();39std::chrono::milliseconds duration =40std::chrono::duration_cast<std::chrono::milliseconds>(end - start);41m_timed_tasks.insert({name.str(), duration});42return result;43}4445/// Executive the given \p callback on each recorded task.46///47/// \param[in] callback48/// The first parameter of the callback is the name of the recorded task,49/// and the second parameter is the duration of that task.50void ForEachTimedTask(std::function<void(const std::string &name,51std::chrono::milliseconds duration)>52callback);5354private:55std::unordered_map<std::string, std::chrono::milliseconds> m_timed_tasks;56};5758/// Class used to track the duration of long running tasks for reporting.59class TaskTimer {60public:61/// \return62/// The timer object for the given thread.63ScopedTaskTimer &ForThread(lldb::tid_t tid);6465/// \return66/// The timer object for global tasks.67ScopedTaskTimer &ForGlobal();6869private:70llvm::DenseMap<lldb::tid_t, ScopedTaskTimer> m_thread_timers;71ScopedTaskTimer m_global_timer;72};7374} // namespace trace_intel_pt75} // namespace lldb_private7677#endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H787980