]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-xray/xray-account.h
MFV r329770: 9035 zfs: this statement may fall through
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-xray / xray-account.h
1 //===- xray-account.h - XRay Function Call Accounting ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the interface for performing some basic function call
11 // accounting from an XRay trace.
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H
15 #define LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H
16
17 #include <map>
18 #include <utility>
19 #include <vector>
20
21 #include "func-id-helper.h"
22 #include "llvm/Support/Program.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/XRay/XRayRecord.h"
25
26 namespace llvm {
27 namespace xray {
28
29 class LatencyAccountant {
30 public:
31   typedef std::map<int32_t, std::vector<uint64_t>> FunctionLatencyMap;
32   typedef std::map<llvm::sys::ProcessInfo::ProcessId,
33                    std::pair<uint64_t, uint64_t>>
34       PerThreadMinMaxTSCMap;
35   typedef std::map<uint8_t, std::pair<uint64_t, uint64_t>> PerCPUMinMaxTSCMap;
36   typedef std::vector<std::pair<int32_t, uint64_t>> FunctionStack;
37   typedef std::map<llvm::sys::ProcessInfo::ProcessId, FunctionStack>
38       PerThreadFunctionStackMap;
39
40 private:
41   PerThreadFunctionStackMap PerThreadFunctionStack;
42   FunctionLatencyMap FunctionLatencies;
43   PerThreadMinMaxTSCMap PerThreadMinMaxTSC;
44   PerCPUMinMaxTSCMap PerCPUMinMaxTSC;
45   FuncIdConversionHelper &FuncIdHelper;
46
47   bool DeduceSiblingCalls = false;
48   uint64_t CurrentMaxTSC = 0;
49
50   void recordLatency(int32_t FuncId, uint64_t Latency) {
51     FunctionLatencies[FuncId].push_back(Latency);
52   }
53
54 public:
55   explicit LatencyAccountant(FuncIdConversionHelper &FuncIdHelper,
56                              bool DeduceSiblingCalls)
57       : FuncIdHelper(FuncIdHelper), DeduceSiblingCalls(DeduceSiblingCalls) {}
58
59   const FunctionLatencyMap &getFunctionLatencies() const {
60     return FunctionLatencies;
61   }
62
63   const PerThreadMinMaxTSCMap &getPerThreadMinMaxTSC() const {
64     return PerThreadMinMaxTSC;
65   }
66
67   const PerCPUMinMaxTSCMap &getPerCPUMinMaxTSC() const {
68     return PerCPUMinMaxTSC;
69   }
70
71   /// Returns false in case we fail to account the provided record. This happens
72   /// in the following cases:
73   ///
74   ///   - An exit record does not match any entry records for the same function.
75   ///     If we've been set to deduce sibling calls, we try walking up the stack
76   ///     and recording times for the higher level functions.
77   ///   - A record has a TSC that's before the latest TSC that has been
78   ///     recorded. We still record the TSC for the min-max.
79   ///
80   bool accountRecord(const XRayRecord &Record);
81
82   const FunctionStack *
83   getThreadFunctionStack(llvm::sys::ProcessInfo::ProcessId TId) const {
84     auto I = PerThreadFunctionStack.find(TId);
85     if (I == PerThreadFunctionStack.end())
86       return nullptr;
87     return &I->second;
88   }
89
90   const PerThreadFunctionStackMap &getPerThreadFunctionStack() const {
91     return PerThreadFunctionStack;
92   }
93
94   // Output Functions
95   // ================
96
97   void exportStatsAsText(raw_ostream &OS, const XRayFileHeader &Header) const;
98   void exportStatsAsCSV(raw_ostream &OS, const XRayFileHeader &Header) const;
99
100 private:
101   // Internal helper to implement common parts of the exportStatsAs...
102   // functions.
103   template <class F> void exportStats(const XRayFileHeader &Header, F fn) const;
104 };
105
106 } // namespace xray
107 } // namespace llvm
108
109 #endif // LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H