xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-xray/xray-account.h (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 //===- xray-account.h - XRay Function Call Accounting ---------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the interface for performing some basic function call
10 // accounting from an XRay trace.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H
14 #define LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H
15 
16 #include <map>
17 #include <utility>
18 #include <vector>
19 
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"
24 
25 namespace llvm {
26 namespace xray {
27 
28 class LatencyAccountant {
29 public:
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;
36 
37 private:
38   PerThreadFunctionStackMap PerThreadFunctionStack;
39   FunctionLatencyMap FunctionLatencies;
40   PerThreadMinMaxTSCMap PerThreadMinMaxTSC;
41   PerCPUMinMaxTSCMap PerCPUMinMaxTSC;
42   FuncIdConversionHelper &FuncIdHelper;
43 
44   bool DeduceSiblingCalls = false;
45   uint64_t CurrentMaxTSC = 0;
46 
47   void recordLatency(int32_t FuncId, uint64_t Latency) {
48     FunctionLatencies[FuncId].push_back(Latency);
49   }
50 
51 public:
52   explicit LatencyAccountant(FuncIdConversionHelper &FuncIdHelper,
53                              bool DeduceSiblingCalls)
54       : FuncIdHelper(FuncIdHelper), DeduceSiblingCalls(DeduceSiblingCalls) {}
55 
56   const FunctionLatencyMap &getFunctionLatencies() const {
57     return FunctionLatencies;
58   }
59 
60   const PerThreadMinMaxTSCMap &getPerThreadMinMaxTSC() const {
61     return PerThreadMinMaxTSC;
62   }
63 
64   const PerCPUMinMaxTSCMap &getPerCPUMinMaxTSC() const {
65     return PerCPUMinMaxTSC;
66   }
67 
68   /// Returns false in case we fail to account the provided record. This happens
69   /// in the following cases:
70   ///
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.
76   ///
77   bool accountRecord(const XRayRecord &Record);
78 
79   const PerThreadFunctionStackMap &getPerThreadFunctionStack() const {
80     return PerThreadFunctionStack;
81   }
82 
83   // Output Functions
84   // ================
85 
86   void exportStatsAsText(raw_ostream &OS, const XRayFileHeader &Header) const;
87   void exportStatsAsCSV(raw_ostream &OS, const XRayFileHeader &Header) const;
88 
89 private:
90   // Internal helper to implement common parts of the exportStatsAs...
91   // functions.
92   template <class F> void exportStats(const XRayFileHeader &Header, F fn) const;
93 };
94 
95 } // namespace xray
96 } // namespace llvm
97 
98 #endif // LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H
99