Skip to content

Commit 9f9464e

Browse files
wupeicongmetaWalter Erquinigo
authored andcommitted
[trace][intel pt] Measure the time it takes to decode a thread in per-cpu mode
This metric was missing. We were only measuring in per-thread mode, and this completes the work. For a sample trace I have, the `dump info` command shows ``` Timing for this thread: Decoding instructions: 0.12s ``` I also improved a bit the TaskTime function so that callers don't need to specify the template argument Differential Revision: https://reviews.llvm.org/D129249
1 parent dde2a7f commit 9f9464e

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

lldb/source/Plugins/Trace/intel-pt/TaskTimer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class ScopedTaskTimer {
3535
///
3636
/// \return
3737
/// The return value of the task.
38-
template <class R> R TimeTask(llvm::StringRef name, std::function<R()> task) {
38+
template <typename C> auto TimeTask(llvm::StringRef name, C task) {
3939
auto start = std::chrono::steady_clock::now();
40-
R result = task();
40+
auto result = task();
4141
auto end = std::chrono::steady_clock::now();
4242
std::chrono::milliseconds duration =
4343
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);

lldb/source/Plugins/Trace/intel-pt/ThreadDecoder.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ Expected<DecodedThreadSP> ThreadDecoder::Decode() {
3535
}
3636

3737
llvm::Expected<DecodedThreadSP> ThreadDecoder::DoDecode() {
38-
return m_trace.GetTimer()
39-
.ForThread(m_thread_sp->GetID())
40-
.TimeTask<Expected<DecodedThreadSP>>(
38+
return m_trace.GetThreadTimer(m_thread_sp->GetID())
39+
.TimeTask(
4140
"Decoding instructions", [&]() -> Expected<DecodedThreadSP> {
4241
DecodedThreadSP decoded_thread_sp =
4342
std::make_shared<DecodedThread>(m_thread_sp);

lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ void TraceIntelPT::DumpTraceInfo(Thread &thread, Stream &s, bool verbose) {
199199
std::chrono::milliseconds duration) {
200200
s.Format(" {0}: {1:2}s\n", name, duration.count() / 1000.0);
201201
};
202-
GetTimer().ForThread(tid).ForEachTimedTask(print_duration);
202+
GetThreadTimer(tid).ForEachTimedTask(print_duration);
203203

204204
s << "\n Timing for global tasks:\n";
205-
GetTimer().ForGlobal().ForEachTimedTask(print_duration);
205+
GetGlobalTimer().ForEachTimedTask(print_duration);
206206
}
207207

208208
// Instruction events stats
@@ -507,3 +507,11 @@ Error TraceIntelPT::OnThreadBufferRead(lldb::tid_t tid,
507507
}
508508

509509
TaskTimer &TraceIntelPT::GetTimer() { return GetUpdatedStorage().task_timer; }
510+
511+
ScopedTaskTimer &TraceIntelPT::GetThreadTimer(lldb::tid_t tid) {
512+
return GetTimer().ForThread(tid);
513+
}
514+
515+
ScopedTaskTimer &TraceIntelPT::GetGlobalTimer() {
516+
return GetTimer().ForGlobal();
517+
}

lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ class TraceIntelPT : public Trace {
157157
/// The timer object for this trace.
158158
TaskTimer &GetTimer();
159159

160+
/// \return
161+
/// The ScopedTaskTimer object for the given thread in this trace.
162+
ScopedTaskTimer &GetThreadTimer(lldb::tid_t tid);
163+
164+
/// \return
165+
/// The global copedTaskTimer object for this trace.
166+
ScopedTaskTimer &GetGlobalTimer();
167+
160168
TraceIntelPTSP GetSharedPtr();
161169

162170
private:

lldb/source/Plugins/Trace/intel-pt/TraceIntelPTMultiCpuDecoder.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,35 @@ Expected<DecodedThreadSP> TraceIntelPTMultiCpuDecoder::Decode(Thread &thread) {
3939
if (Error err = CorrelateContextSwitchesAndIntelPtTraces())
4040
return std::move(err);
4141

42-
auto it = m_decoded_threads.find(thread.GetID());
43-
if (it != m_decoded_threads.end())
44-
return it->second;
45-
46-
DecodedThreadSP decoded_thread_sp =
47-
std::make_shared<DecodedThread>(thread.shared_from_this());
48-
4942
TraceIntelPTSP trace_sp = GetTrace();
5043

51-
Error err = trace_sp->OnAllCpusBinaryDataRead(
52-
IntelPTDataKinds::kIptTrace,
53-
[&](const DenseMap<cpu_id_t, ArrayRef<uint8_t>> &buffers) -> Error {
54-
auto it = m_continuous_executions_per_thread->find(thread.GetID());
55-
if (it != m_continuous_executions_per_thread->end())
56-
return DecodeSystemWideTraceForThread(*decoded_thread_sp, *trace_sp,
57-
buffers, it->second);
58-
59-
return Error::success();
44+
return trace_sp
45+
->GetThreadTimer(thread.GetID())
46+
.TimeTask("Decoding instructions", [&]() -> Expected<DecodedThreadSP> {
47+
auto it = m_decoded_threads.find(thread.GetID());
48+
if (it != m_decoded_threads.end())
49+
return it->second;
50+
51+
DecodedThreadSP decoded_thread_sp =
52+
std::make_shared<DecodedThread>(thread.shared_from_this());
53+
54+
Error err = trace_sp->OnAllCpusBinaryDataRead(
55+
IntelPTDataKinds::kIptTrace,
56+
[&](const DenseMap<cpu_id_t, ArrayRef<uint8_t>> &buffers) -> Error {
57+
auto it =
58+
m_continuous_executions_per_thread->find(thread.GetID());
59+
if (it != m_continuous_executions_per_thread->end())
60+
return DecodeSystemWideTraceForThread(
61+
*decoded_thread_sp, *trace_sp, buffers, it->second);
62+
63+
return Error::success();
64+
});
65+
if (err)
66+
return std::move(err);
67+
68+
m_decoded_threads.try_emplace(thread.GetID(), decoded_thread_sp);
69+
return decoded_thread_sp;
6070
});
61-
if (err)
62-
return std::move(err);
63-
64-
m_decoded_threads.try_emplace(thread.GetID(), decoded_thread_sp);
65-
return decoded_thread_sp;
6671
}
6772

6873
static Expected<std::vector<IntelPTThreadSubtrace>>
@@ -153,7 +158,7 @@ Error TraceIntelPTMultiCpuDecoder::CorrelateContextSwitchesAndIntelPtTraces() {
153158
if (m_continuous_executions_per_thread)
154159
return Error::success();
155160

156-
Error err = GetTrace()->GetTimer().ForGlobal().TimeTask<Error>(
161+
Error err = GetTrace()->GetGlobalTimer().TimeTask(
157162
"Context switch and Intel PT traces correlation", [&]() -> Error {
158163
if (auto correlation = DoCorrelateContextSwitchesAndIntelPtTraces()) {
159164
m_continuous_executions_per_thread.emplace(std::move(*correlation));

0 commit comments

Comments
 (0)