Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Trace/intel-pt/TaskTimer.h
39645 views
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.
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;
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.
64
ScopedTaskTimer &ForThread(lldb::tid_t tid);
65
66
/// \return
67
/// The timer object for global tasks.
68
ScopedTaskTimer &ForGlobal();
69
70
private:
71
llvm::DenseMap<lldb::tid_t, ScopedTaskTimer> m_thread_timers;
72
ScopedTaskTimer m_global_timer;
73
};
74
75
} // namespace trace_intel_pt
76
} // namespace lldb_private
77
78
#endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TASKTIMER_H
79
80