aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm12/tools/llvm-xray/xray-account.h
blob: 371a9cc708e9a1a48aea13fa632566b9e09893f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//===- xray-account.h - XRay Function Call Accounting ---------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the interface for performing some basic function call
// accounting from an XRay trace.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H
#define LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H

#include <map>
#include <utility>
#include <vector>

#include "func-id-helper.h"
#include "llvm/ADT/Bitfields.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/XRay/XRayRecord.h"

namespace llvm {
namespace xray {

class LatencyAccountant {
public:
  typedef llvm::DenseMap<int32_t, llvm::SmallVector<uint64_t, 0>>
      FunctionLatencyMap;
  typedef llvm::DenseMap<uint32_t, std::pair<uint64_t, uint64_t>>
      PerThreadMinMaxTSCMap;
  typedef llvm::DenseMap<uint8_t, std::pair<uint64_t, uint64_t>>
      PerCPUMinMaxTSCMap;
  struct FunctionStack {
    llvm::SmallVector<std::pair<int32_t, uint64_t>, 32> Stack;
    class RecursionStatus {
      uint32_t Storage = 0;
      using Depth = Bitfield::Element<int32_t, 0, 31>;    // Low 31 bits.
      using IsRecursive = Bitfield::Element<bool, 31, 1>; // Sign bit.
    public:
      RecursionStatus &operator++();
      RecursionStatus &operator--();
      bool isRecursive() const;
    };
    Optional<llvm::DenseMap<int32_t, RecursionStatus>> RecursionDepth;
  };
  typedef llvm::DenseMap<uint32_t, FunctionStack> PerThreadFunctionStackMap;

private:
  PerThreadFunctionStackMap PerThreadFunctionStack;
  FunctionLatencyMap FunctionLatencies;
  PerThreadMinMaxTSCMap PerThreadMinMaxTSC;
  PerCPUMinMaxTSCMap PerCPUMinMaxTSC;
  FuncIdConversionHelper &FuncIdHelper;

  bool RecursiveCallsOnly = false;
  bool DeduceSiblingCalls = false;
  uint64_t CurrentMaxTSC = 0;

  void recordLatency(int32_t FuncId, uint64_t Latency) {
    FunctionLatencies[FuncId].push_back(Latency);
  }

public:
  explicit LatencyAccountant(FuncIdConversionHelper &FuncIdHelper,
                             bool RecursiveCallsOnly, bool DeduceSiblingCalls)
      : FuncIdHelper(FuncIdHelper), RecursiveCallsOnly(RecursiveCallsOnly),
        DeduceSiblingCalls(DeduceSiblingCalls) {}

  const FunctionLatencyMap &getFunctionLatencies() const {
    return FunctionLatencies;
  }

  const PerThreadMinMaxTSCMap &getPerThreadMinMaxTSC() const {
    return PerThreadMinMaxTSC;
  }

  const PerCPUMinMaxTSCMap &getPerCPUMinMaxTSC() const {
    return PerCPUMinMaxTSC;
  }

  /// Returns false in case we fail to account the provided record. This happens
  /// in the following cases:
  ///
  ///   - An exit record does not match any entry records for the same function.
  ///     If we've been set to deduce sibling calls, we try walking up the stack
  ///     and recording times for the higher level functions.
  ///   - A record has a TSC that's before the latest TSC that has been
  ///     recorded. We still record the TSC for the min-max.
  ///
  bool accountRecord(const XRayRecord &Record);

  const PerThreadFunctionStackMap &getPerThreadFunctionStack() const {
    return PerThreadFunctionStack;
  }

  // Output Functions
  // ================

  void exportStatsAsText(raw_ostream &OS, const XRayFileHeader &Header) const;
  void exportStatsAsCSV(raw_ostream &OS, const XRayFileHeader &Header) const;

private:
  // Internal helper to implement common parts of the exportStatsAs...
  // functions.
  template <class F> void exportStats(const XRayFileHeader &Header, F fn) const;
};

} // namespace xray
} // namespace llvm

#endif // LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H