LLDB  mainline
TaskTimer.h
Go to the documentation of this file.
1 //===-- TaskTimer.h ---------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H
10 #define LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H
11 
12 #include "lldb/lldb-types.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/StringRef.h"
15 #include <chrono>
16 #include <functional>
17 #include <unordered_map>
18 
19 namespace lldb_private {
20 namespace trace_intel_pt {
21 
22 /// Class used to track the duration of long running tasks related to a single
23 /// scope for reporting.
25 public:
26  /// Execute the given \p task and record its duration.
27  ///
28  /// \param[in] name
29  /// The name used to identify this task for reporting.
30  ///
31  /// \param[in] task
32  /// The task function.
33  ///
34  /// \return
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();
38  auto result = task();
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});
43  return result;
44  }
45 
46  /// Executive the given \p callback on each recorded task.
47  ///
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)>
53  callback);
54 
55 private:
56  std::unordered_map<std::string, std::chrono::milliseconds> m_timed_tasks;
57 };
58 
59 /// Class used to track the duration of long running tasks for reporting.
60 class TaskTimer {
61 public:
62  /// \return
63  /// The timer object for the given thread.
65 
66  /// \return
67  /// The timer object for global tasks.
69 
70 private:
71  llvm::DenseMap<lldb::tid_t, ScopedTaskTimer> m_thread_timers;
73 };
74 
75 } // namespace trace_intel_pt
76 } // namespace lldb_private
77 
78 #endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H
lldb_private::trace_intel_pt::TaskTimer::ForGlobal
ScopedTaskTimer & ForGlobal()
Definition: TaskTimer.cpp:32
lldb_private::trace_intel_pt::ScopedTaskTimer::TimeTask
auto TimeTask(llvm::StringRef name, C task)
Execute the given task and record its duration.
Definition: TaskTimer.h:36
lldb_private::trace_intel_pt::TaskTimer::m_thread_timers
llvm::DenseMap< lldb::tid_t, ScopedTaskTimer > m_thread_timers
Definition: TaskTimer.h:71
lldb_private::trace_intel_pt::TaskTimer::ForThread
ScopedTaskTimer & ForThread(lldb::tid_t tid)
Definition: TaskTimer.cpp:25
lldb_private::trace_intel_pt::ScopedTaskTimer::m_timed_tasks
std::unordered_map< std::string, std::chrono::milliseconds > m_timed_tasks
Definition: TaskTimer.h:56
lldb_private::trace_intel_pt::ScopedTaskTimer
Class used to track the duration of long running tasks related to a single scope for reporting.
Definition: TaskTimer.h:24
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb-types.h
lldb_private::trace_intel_pt::ScopedTaskTimer::ForEachTimedTask
void ForEachTimedTask(std::function< void(const std::string &name, std::chrono::milliseconds duration)> callback)
Executive the given callback on each recorded task.
Definition: TaskTimer.cpp:16
lldb_private::trace_intel_pt::TaskTimer::m_global_timer
ScopedTaskTimer m_global_timer
Definition: TaskTimer.h:72
lldb_private::trace_intel_pt::TaskTimer
Class used to track the duration of long running tasks for reporting.
Definition: TaskTimer.h:60
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86