1*f6aab3d8Srobert //===-- TaskTimer.h ---------------------------------------------*- C++ -*-===//
2*f6aab3d8Srobert //
3*f6aab3d8Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*f6aab3d8Srobert // See https://llvm.org/LICENSE.txt for license information.
5*f6aab3d8Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*f6aab3d8Srobert //
7*f6aab3d8Srobert //===----------------------------------------------------------------------===//
8*f6aab3d8Srobert 
9*f6aab3d8Srobert #ifndef LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H
10*f6aab3d8Srobert #define LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H
11*f6aab3d8Srobert 
12*f6aab3d8Srobert #include "lldb/lldb-types.h"
13*f6aab3d8Srobert #include "llvm/ADT/DenseMap.h"
14*f6aab3d8Srobert #include "llvm/ADT/StringRef.h"
15*f6aab3d8Srobert #include <chrono>
16*f6aab3d8Srobert #include <functional>
17*f6aab3d8Srobert #include <unordered_map>
18*f6aab3d8Srobert 
19*f6aab3d8Srobert namespace lldb_private {
20*f6aab3d8Srobert namespace trace_intel_pt {
21*f6aab3d8Srobert 
22*f6aab3d8Srobert /// Class used to track the duration of long running tasks related to a single
23*f6aab3d8Srobert /// scope for reporting.
24*f6aab3d8Srobert class ScopedTaskTimer {
25*f6aab3d8Srobert public:
26*f6aab3d8Srobert   /// Execute the given \p task and record its duration.
27*f6aab3d8Srobert   ///
28*f6aab3d8Srobert   /// \param[in] name
29*f6aab3d8Srobert   ///     The name used to identify this task for reporting.
30*f6aab3d8Srobert   ///
31*f6aab3d8Srobert   /// \param[in] task
32*f6aab3d8Srobert   ///     The task function.
33*f6aab3d8Srobert   ///
34*f6aab3d8Srobert   /// \return
35*f6aab3d8Srobert   ///     The return value of the task.
TimeTask(llvm::StringRef name,C task)36*f6aab3d8Srobert   template <typename C> auto TimeTask(llvm::StringRef name, C task) {
37*f6aab3d8Srobert     auto start = std::chrono::steady_clock::now();
38*f6aab3d8Srobert     auto result = task();
39*f6aab3d8Srobert     auto end = std::chrono::steady_clock::now();
40*f6aab3d8Srobert     std::chrono::milliseconds duration =
41*f6aab3d8Srobert         std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
42*f6aab3d8Srobert     m_timed_tasks.insert({name.str(), duration});
43*f6aab3d8Srobert     return result;
44*f6aab3d8Srobert   }
45*f6aab3d8Srobert 
46*f6aab3d8Srobert   /// Executive the given \p callback on each recorded task.
47*f6aab3d8Srobert   ///
48*f6aab3d8Srobert   /// \param[in] callback
49*f6aab3d8Srobert   ///     The first parameter of the callback is the name of the recorded task,
50*f6aab3d8Srobert   ///     and the second parameter is the duration of that task.
51*f6aab3d8Srobert   void ForEachTimedTask(std::function<void(const std::string &name,
52*f6aab3d8Srobert                                            std::chrono::milliseconds duration)>
53*f6aab3d8Srobert                             callback);
54*f6aab3d8Srobert 
55*f6aab3d8Srobert private:
56*f6aab3d8Srobert   std::unordered_map<std::string, std::chrono::milliseconds> m_timed_tasks;
57*f6aab3d8Srobert };
58*f6aab3d8Srobert 
59*f6aab3d8Srobert /// Class used to track the duration of long running tasks for reporting.
60*f6aab3d8Srobert class TaskTimer {
61*f6aab3d8Srobert public:
62*f6aab3d8Srobert   /// \return
63*f6aab3d8Srobert   ///     The timer object for the given thread.
64*f6aab3d8Srobert   ScopedTaskTimer &ForThread(lldb::tid_t tid);
65*f6aab3d8Srobert 
66*f6aab3d8Srobert   /// \return
67*f6aab3d8Srobert   ///     The timer object for global tasks.
68*f6aab3d8Srobert   ScopedTaskTimer &ForGlobal();
69*f6aab3d8Srobert 
70*f6aab3d8Srobert private:
71*f6aab3d8Srobert   llvm::DenseMap<lldb::tid_t, ScopedTaskTimer> m_thread_timers;
72*f6aab3d8Srobert   ScopedTaskTimer m_global_timer;
73*f6aab3d8Srobert };
74*f6aab3d8Srobert 
75*f6aab3d8Srobert } // namespace trace_intel_pt
76*f6aab3d8Srobert } // namespace lldb_private
77*f6aab3d8Srobert 
78*f6aab3d8Srobert #endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H
79