112 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines the interface for performing some basic function call10// accounting from an XRay trace.11//12//===----------------------------------------------------------------------===//13#ifndef LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H14#define LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H15 16#include <utility>17 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 24namespace llvm::xray {25 26class LatencyAccountant {27public:28 typedef llvm::DenseMap<int32_t, llvm::SmallVector<uint64_t, 0>>29 FunctionLatencyMap;30 typedef llvm::DenseMap<uint32_t, std::pair<uint64_t, uint64_t>>31 PerThreadMinMaxTSCMap;32 typedef llvm::DenseMap<uint8_t, std::pair<uint64_t, uint64_t>>33 PerCPUMinMaxTSCMap;34 struct FunctionStack {35 llvm::SmallVector<std::pair<int32_t, uint64_t>, 32> Stack;36 class RecursionStatus {37 uint32_t Storage = 0;38 using Depth = Bitfield::Element<int32_t, 0, 31>; // Low 31 bits.39 using IsRecursive = Bitfield::Element<bool, 31, 1>; // Sign bit.40 public:41 RecursionStatus &operator++();42 RecursionStatus &operator--();43 bool isRecursive() const;44 };45 std::optional<llvm::DenseMap<int32_t, RecursionStatus>> RecursionDepth;46 };47 typedef llvm::DenseMap<uint32_t, FunctionStack> PerThreadFunctionStackMap;48 49private:50 PerThreadFunctionStackMap PerThreadFunctionStack;51 FunctionLatencyMap FunctionLatencies;52 PerThreadMinMaxTSCMap PerThreadMinMaxTSC;53 PerCPUMinMaxTSCMap PerCPUMinMaxTSC;54 FuncIdConversionHelper &FuncIdHelper;55 56 bool RecursiveCallsOnly = false;57 bool DeduceSiblingCalls = false;58 uint64_t CurrentMaxTSC = 0;59 60 void recordLatency(int32_t FuncId, uint64_t Latency) {61 FunctionLatencies[FuncId].push_back(Latency);62 }63 64public:65 explicit LatencyAccountant(FuncIdConversionHelper &FuncIdHelper,66 bool RecursiveCallsOnly, bool DeduceSiblingCalls)67 : FuncIdHelper(FuncIdHelper), RecursiveCallsOnly(RecursiveCallsOnly),68 DeduceSiblingCalls(DeduceSiblingCalls) {}69 70 const FunctionLatencyMap &getFunctionLatencies() const {71 return FunctionLatencies;72 }73 74 const PerThreadMinMaxTSCMap &getPerThreadMinMaxTSC() const {75 return PerThreadMinMaxTSC;76 }77 78 const PerCPUMinMaxTSCMap &getPerCPUMinMaxTSC() const {79 return PerCPUMinMaxTSC;80 }81 82 /// Returns false in case we fail to account the provided record. This happens83 /// in the following cases:84 ///85 /// - An exit record does not match any entry records for the same function.86 /// If we've been set to deduce sibling calls, we try walking up the stack87 /// and recording times for the higher level functions.88 /// - A record has a TSC that's before the latest TSC that has been89 /// recorded. We still record the TSC for the min-max.90 ///91 bool accountRecord(const XRayRecord &Record);92 93 const PerThreadFunctionStackMap &getPerThreadFunctionStack() const {94 return PerThreadFunctionStack;95 }96 97 // Output Functions98 // ================99 100 void exportStatsAsText(raw_ostream &OS, const XRayFileHeader &Header) const;101 void exportStatsAsCSV(raw_ostream &OS, const XRayFileHeader &Header) const;102 103private:104 // Internal helper to implement common parts of the exportStatsAs...105 // functions.106 template <class F> void exportStats(const XRayFileHeader &Header, F fn) const;107};108 109} // namespace llvm::xray110 111#endif // LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H112