xray-account.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include <map>
  16. #include <utility>
  17. #include <vector>
  18. #include "func-id-helper.h"
  19. #include "llvm/ADT/Bitfields.h"
  20. #include "llvm/Support/Program.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include "llvm/XRay/XRayRecord.h"
  23. namespace llvm {
  24. namespace xray {
  25. class LatencyAccountant {
  26. public:
  27. typedef llvm::DenseMap<int32_t, llvm::SmallVector<uint64_t, 0>>
  28. FunctionLatencyMap;
  29. typedef llvm::DenseMap<uint32_t, std::pair<uint64_t, uint64_t>>
  30. PerThreadMinMaxTSCMap;
  31. typedef llvm::DenseMap<uint8_t, std::pair<uint64_t, uint64_t>>
  32. PerCPUMinMaxTSCMap;
  33. struct FunctionStack {
  34. llvm::SmallVector<std::pair<int32_t, uint64_t>, 32> Stack;
  35. class RecursionStatus {
  36. uint32_t Storage = 0;
  37. using Depth = Bitfield::Element<int32_t, 0, 31>; // Low 31 bits.
  38. using IsRecursive = Bitfield::Element<bool, 31, 1>; // Sign bit.
  39. public:
  40. RecursionStatus &operator++();
  41. RecursionStatus &operator--();
  42. bool isRecursive() const;
  43. };
  44. Optional<llvm::DenseMap<int32_t, RecursionStatus>> RecursionDepth;
  45. };
  46. typedef llvm::DenseMap<uint32_t, FunctionStack> PerThreadFunctionStackMap;
  47. private:
  48. PerThreadFunctionStackMap PerThreadFunctionStack;
  49. FunctionLatencyMap FunctionLatencies;
  50. PerThreadMinMaxTSCMap PerThreadMinMaxTSC;
  51. PerCPUMinMaxTSCMap PerCPUMinMaxTSC;
  52. FuncIdConversionHelper &FuncIdHelper;
  53. bool RecursiveCallsOnly = false;
  54. bool DeduceSiblingCalls = false;
  55. uint64_t CurrentMaxTSC = 0;
  56. void recordLatency(int32_t FuncId, uint64_t Latency) {
  57. FunctionLatencies[FuncId].push_back(Latency);
  58. }
  59. public:
  60. explicit LatencyAccountant(FuncIdConversionHelper &FuncIdHelper,
  61. bool RecursiveCallsOnly, bool DeduceSiblingCalls)
  62. : FuncIdHelper(FuncIdHelper), RecursiveCallsOnly(RecursiveCallsOnly),
  63. DeduceSiblingCalls(DeduceSiblingCalls) {}
  64. const FunctionLatencyMap &getFunctionLatencies() const {
  65. return FunctionLatencies;
  66. }
  67. const PerThreadMinMaxTSCMap &getPerThreadMinMaxTSC() const {
  68. return PerThreadMinMaxTSC;
  69. }
  70. const PerCPUMinMaxTSCMap &getPerCPUMinMaxTSC() const {
  71. return PerCPUMinMaxTSC;
  72. }
  73. /// Returns false in case we fail to account the provided record. This happens
  74. /// in the following cases:
  75. ///
  76. /// - An exit record does not match any entry records for the same function.
  77. /// If we've been set to deduce sibling calls, we try walking up the stack
  78. /// and recording times for the higher level functions.
  79. /// - A record has a TSC that's before the latest TSC that has been
  80. /// recorded. We still record the TSC for the min-max.
  81. ///
  82. bool accountRecord(const XRayRecord &Record);
  83. const PerThreadFunctionStackMap &getPerThreadFunctionStack() const {
  84. return PerThreadFunctionStack;
  85. }
  86. // Output Functions
  87. // ================
  88. void exportStatsAsText(raw_ostream &OS, const XRayFileHeader &Header) const;
  89. void exportStatsAsCSV(raw_ostream &OS, const XRayFileHeader &Header) const;
  90. private:
  91. // Internal helper to implement common parts of the exportStatsAs...
  92. // functions.
  93. template <class F> void exportStats(const XRayFileHeader &Header, F fn) const;
  94. };
  95. } // namespace xray
  96. } // namespace llvm
  97. #endif // LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H