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
<llvm::sys::procid_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
<llvm::sys::procid_t
, FunctionStack
>
36 PerThreadFunctionStackMap
;
39 PerThreadFunctionStackMap PerThreadFunctionStack
;
40 FunctionLatencyMap FunctionLatencies
;
41 PerThreadMinMaxTSCMap PerThreadMinMaxTSC
;
42 PerCPUMinMaxTSCMap PerCPUMinMaxTSC
;
43 FuncIdConversionHelper
&FuncIdHelper
;
45 bool DeduceSiblingCalls
= false;
46 uint64_t CurrentMaxTSC
= 0;
48 void recordLatency(int32_t FuncId
, uint64_t Latency
) {
49 FunctionLatencies
[FuncId
].push_back(Latency
);
53 explicit LatencyAccountant(FuncIdConversionHelper
&FuncIdHelper
,
54 bool DeduceSiblingCalls
)
55 : FuncIdHelper(FuncIdHelper
), DeduceSiblingCalls(DeduceSiblingCalls
) {}
57 const FunctionLatencyMap
&getFunctionLatencies() const {
58 return FunctionLatencies
;
61 const PerThreadMinMaxTSCMap
&getPerThreadMinMaxTSC() const {
62 return PerThreadMinMaxTSC
;
65 const PerCPUMinMaxTSCMap
&getPerCPUMinMaxTSC() const {
66 return PerCPUMinMaxTSC
;
69 /// Returns false in case we fail to account the provided record. This happens
70 /// in the following cases:
72 /// - An exit record does not match any entry records for the same function.
73 /// If we've been set to deduce sibling calls, we try walking up the stack
74 /// and recording times for the higher level functions.
75 /// - A record has a TSC that's before the latest TSC that has been
76 /// recorded. We still record the TSC for the min-max.
78 bool accountRecord(const XRayRecord
&Record
);
80 const FunctionStack
*getThreadFunctionStack(llvm::sys::procid_t TId
) const {
81 auto I
= PerThreadFunctionStack
.find(TId
);
82 if (I
== PerThreadFunctionStack
.end())
87 const PerThreadFunctionStackMap
&getPerThreadFunctionStack() const {
88 return PerThreadFunctionStack
;
94 void exportStatsAsText(raw_ostream
&OS
, const XRayFileHeader
&Header
) const;
95 void exportStatsAsCSV(raw_ostream
&OS
, const XRayFileHeader
&Header
) const;
98 // Internal helper to implement common parts of the exportStatsAs...
100 template <class F
> void exportStats(const XRayFileHeader
&Header
, F fn
) const;
106 #endif // LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H