1 //===- xray-account.h - XRay Function Call Accounting ---------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the interface for performing some basic function call
10 // accounting from an XRay trace.
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H
14 #define LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H
20 #include "func-id-helper.h"
21 #include "llvm/Support/Program.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/XRay/XRayRecord.h"
28 class LatencyAccountant
{
30 typedef std::map
<int32_t, std::vector
<uint64_t>> FunctionLatencyMap
;
31 typedef std::map
<uint32_t, std::pair
<uint64_t, uint64_t>>
32 PerThreadMinMaxTSCMap
;
33 typedef std::map
<uint8_t, std::pair
<uint64_t, uint64_t>> PerCPUMinMaxTSCMap
;
34 typedef std::vector
<std::pair
<int32_t, uint64_t>> FunctionStack
;
35 typedef std::map
<uint32_t, FunctionStack
> PerThreadFunctionStackMap
;
38 PerThreadFunctionStackMap PerThreadFunctionStack
;
39 FunctionLatencyMap FunctionLatencies
;
40 PerThreadMinMaxTSCMap PerThreadMinMaxTSC
;
41 PerCPUMinMaxTSCMap PerCPUMinMaxTSC
;
42 FuncIdConversionHelper
&FuncIdHelper
;
44 bool DeduceSiblingCalls
= false;
45 uint64_t CurrentMaxTSC
= 0;
47 void recordLatency(int32_t FuncId
, uint64_t Latency
) {
48 FunctionLatencies
[FuncId
].push_back(Latency
);
52 explicit LatencyAccountant(FuncIdConversionHelper
&FuncIdHelper
,
53 bool DeduceSiblingCalls
)
54 : FuncIdHelper(FuncIdHelper
), DeduceSiblingCalls(DeduceSiblingCalls
) {}
56 const FunctionLatencyMap
&getFunctionLatencies() const {
57 return FunctionLatencies
;
60 const PerThreadMinMaxTSCMap
&getPerThreadMinMaxTSC() const {
61 return PerThreadMinMaxTSC
;
64 const PerCPUMinMaxTSCMap
&getPerCPUMinMaxTSC() const {
65 return PerCPUMinMaxTSC
;
68 /// Returns false in case we fail to account the provided record. This happens
69 /// in the following cases:
71 /// - An exit record does not match any entry records for the same function.
72 /// If we've been set to deduce sibling calls, we try walking up the stack
73 /// and recording times for the higher level functions.
74 /// - A record has a TSC that's before the latest TSC that has been
75 /// recorded. We still record the TSC for the min-max.
77 bool accountRecord(const XRayRecord
&Record
);
79 const PerThreadFunctionStackMap
&getPerThreadFunctionStack() const {
80 return PerThreadFunctionStack
;
86 void exportStatsAsText(raw_ostream
&OS
, const XRayFileHeader
&Header
) const;
87 void exportStatsAsCSV(raw_ostream
&OS
, const XRayFileHeader
&Header
) const;
90 // Internal helper to implement common parts of the exportStatsAs...
92 template <class F
> void exportStats(const XRayFileHeader
&Header
, F fn
) const;
98 #endif // LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H