Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Trace/intel-pt/PerfContextSwitchDecoder.h
39648 views
//===-- PerfContextSwitchDecoder.h --======----------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_PERFCONTEXTSWITCHDECODER_H9#define LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_PERFCONTEXTSWITCHDECODER_H1011#include "lldb/Utility/TraceIntelPTGDBRemotePackets.h"12#include "lldb/lldb-types.h"13#include "llvm/Support/Error.h"14#include <set>15#include <vector>1617namespace lldb_private {18namespace trace_intel_pt {1920/// This class indicates the time interval in which a thread was running21/// continuously on a cpu core.22struct ThreadContinuousExecution {2324/// In most cases both the start and end of a continuous execution can be25/// accurately recovered from the context switch trace, but in some cases one26/// of these endpoints might be guessed or not known at all, due to contention27/// problems in the trace or because tracing was interrupted, e.g. with ioctl28/// calls, which causes gaps in the trace. Because of that, we identify which29/// situation we fall into with the following variants.30enum class Variant {31/// Both endpoints are known.32Complete,33/// The end is known and we have a lower bound for the start, i.e. the34/// previous execution in the same cpu happens strictly before the hinted35/// start.36HintedStart,37/// The start is known and we have an upper bound for the end, i.e. the next38/// execution in the same cpu happens strictly after the hinted end.39HintedEnd,40/// We only know the start. This might be the last entry of a cpu trace.41OnlyStart,42/// We only know the end. This might be the first entry or a cpu trace.43OnlyEnd,44} variant;4546/// \return47/// The lowest tsc that we are sure of, i.e. not hinted.48uint64_t GetLowestKnownTSC() const;4950/// \return51/// The known or hinted start tsc, or 0 if the variant is \a OnlyEnd.52uint64_t GetStartTSC() const;5354/// \return55/// The known or hinted end tsc, or max \a uint64_t if the variant is \a56/// OnlyStart.57uint64_t GetEndTSC() const;5859/// Constructors for the different variants of this object60///61/// \{62static ThreadContinuousExecution63CreateCompleteExecution(lldb::cpu_id_t cpu_id, lldb::tid_t tid,64lldb::pid_t pid, uint64_t start, uint64_t end);6566static ThreadContinuousExecution67CreateHintedStartExecution(lldb::cpu_id_t cpu_id, lldb::tid_t tid,68lldb::pid_t pid, uint64_t hinted_start,69uint64_t end);7071static ThreadContinuousExecution72CreateHintedEndExecution(lldb::cpu_id_t cpu_id, lldb::tid_t tid,73lldb::pid_t pid, uint64_t start,74uint64_t hinted_end);7576static ThreadContinuousExecution CreateOnlyEndExecution(lldb::cpu_id_t cpu_id,77lldb::tid_t tid,78lldb::pid_t pid,79uint64_t end);8081static ThreadContinuousExecution82CreateOnlyStartExecution(lldb::cpu_id_t cpu_id, lldb::tid_t tid,83lldb::pid_t pid, uint64_t start);84/// \}8586union {87struct {88uint64_t start;89uint64_t end;90} complete;91struct {92uint64_t start;93} only_start;94struct {95uint64_t end;96} only_end;97/// The following 'hinted' structures are useful when there are contention98/// problems in the trace99struct {100uint64_t hinted_start;101uint64_t end;102} hinted_start;103struct {104uint64_t start;105uint64_t hinted_end;106} hinted_end;107} tscs;108109lldb::cpu_id_t cpu_id;110lldb::tid_t tid;111lldb::pid_t pid;112113private:114/// We keep this constructor private to force the usage of the static named115/// constructors.116ThreadContinuousExecution(lldb::cpu_id_t cpu_id, lldb::tid_t tid,117lldb::pid_t pid)118: cpu_id(cpu_id), tid(tid), pid(pid) {}119};120121/// Decodes a context switch trace collected with perf_event_open.122///123/// \param[in] data124/// The context switch trace in binary format.125///126/// \param[i] cpu_id127/// The cpu_id where the trace were gotten from.128///129/// \param[in] tsc_conversion130/// The conversion values used to confert nanoseconds to TSC.131///132/// \return133/// A list of continuous executions recovered from the raw trace sorted by134/// time, or an \a llvm::Error if the data is malformed.135llvm::Expected<std::vector<ThreadContinuousExecution>>136DecodePerfContextSwitchTrace(llvm::ArrayRef<uint8_t> data,137lldb::cpu_id_t cpu_id,138const LinuxPerfZeroTscConversion &tsc_conversion);139140llvm::Expected<std::vector<uint8_t>>141FilterProcessesFromContextSwitchTrace(llvm::ArrayRef<uint8_t> data,142const std::set<lldb::pid_t> &pids);143144} // namespace trace_intel_pt145} // namespace lldb_private146147#endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_PERFCONTEXTSWITCHDECODER_H148149150