Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / Plugins / Trace / intel-pt / TaskTimer.h
blobfb196e8fc32afcf5ca6c6aba031022a6acb8d55f
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 //===----------------------------------------------------------------------===//
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"
15 #include <chrono>
16 #include <functional>
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 {
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;
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);
55 private:
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.
60 class TaskTimer {
61 public:
62 /// \return
63 /// The timer object for the given thread.
64 ScopedTaskTimer &ForThread(lldb::tid_t tid);
66 /// \return
67 /// The timer object for global tasks.
68 ScopedTaskTimer &ForGlobal();
70 private:
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