1 //===-- TaskTimer.h ---------------------------------------------*- C++ -*-===//
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 #ifndef LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H
10 #define LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H
12 #include "lldb/lldb-types.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/StringRef.h"
17 #include <unordered_map>
19 namespace lldb_private
{
20 namespace trace_intel_pt
{
22 /// Class used to track the duration of long running tasks related to a single
23 /// scope for reporting.
24 class ScopedTaskTimer
{
26 /// Execute the given \p task and record its duration.
29 /// The name used to identify this task for reporting.
32 /// The task function.
35 /// The return value of the task.
36 template <typename C
> auto TimeTask(llvm::StringRef name
, C task
) {
37 auto start
= std::chrono::steady_clock::now();
39 auto end
= std::chrono::steady_clock::now();
40 std::chrono::milliseconds duration
=
41 std::chrono::duration_cast
<std::chrono::milliseconds
>(end
- start
);
42 m_timed_tasks
.insert({name
.str(), duration
});
46 /// Executive the given \p callback on each recorded task.
48 /// \param[in] callback
49 /// The first parameter of the callback is the name of the recorded task,
50 /// and the second parameter is the duration of that task.
51 void ForEachTimedTask(std::function
<void(const std::string
&name
,
52 std::chrono::milliseconds duration
)>
56 std::unordered_map
<std::string
, std::chrono::milliseconds
> m_timed_tasks
;
59 /// Class used to track the duration of long running tasks for reporting.
63 /// The timer object for the given thread.
64 ScopedTaskTimer
&ForThread(lldb::tid_t tid
);
67 /// The timer object for global tasks.
68 ScopedTaskTimer
&ForGlobal();
71 llvm::DenseMap
<lldb::tid_t
, ScopedTaskTimer
> m_thread_timers
;
72 ScopedTaskTimer m_global_timer
;
75 } // namespace trace_intel_pt
76 } // namespace lldb_private
78 #endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H