Path: blob/main/contrib/llvm-project/llvm/tools/llvm-xray/xray-account.h
35231 views
//===- xray-account.h - XRay Function Call Accounting ---------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines the interface for performing some basic function call9// accounting from an XRay trace.10//11//===----------------------------------------------------------------------===//12#ifndef LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H13#define LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H1415#include <utility>1617#include "func-id-helper.h"18#include "llvm/ADT/Bitfields.h"19#include "llvm/Support/Program.h"20#include "llvm/Support/raw_ostream.h"21#include "llvm/XRay/XRayRecord.h"2223namespace llvm {24namespace xray {2526class LatencyAccountant {27public:28typedef llvm::DenseMap<int32_t, llvm::SmallVector<uint64_t, 0>>29FunctionLatencyMap;30typedef llvm::DenseMap<uint32_t, std::pair<uint64_t, uint64_t>>31PerThreadMinMaxTSCMap;32typedef llvm::DenseMap<uint8_t, std::pair<uint64_t, uint64_t>>33PerCPUMinMaxTSCMap;34struct FunctionStack {35llvm::SmallVector<std::pair<int32_t, uint64_t>, 32> Stack;36class RecursionStatus {37uint32_t Storage = 0;38using Depth = Bitfield::Element<int32_t, 0, 31>; // Low 31 bits.39using IsRecursive = Bitfield::Element<bool, 31, 1>; // Sign bit.40public:41RecursionStatus &operator++();42RecursionStatus &operator--();43bool isRecursive() const;44};45std::optional<llvm::DenseMap<int32_t, RecursionStatus>> RecursionDepth;46};47typedef llvm::DenseMap<uint32_t, FunctionStack> PerThreadFunctionStackMap;4849private:50PerThreadFunctionStackMap PerThreadFunctionStack;51FunctionLatencyMap FunctionLatencies;52PerThreadMinMaxTSCMap PerThreadMinMaxTSC;53PerCPUMinMaxTSCMap PerCPUMinMaxTSC;54FuncIdConversionHelper &FuncIdHelper;5556bool RecursiveCallsOnly = false;57bool DeduceSiblingCalls = false;58uint64_t CurrentMaxTSC = 0;5960void recordLatency(int32_t FuncId, uint64_t Latency) {61FunctionLatencies[FuncId].push_back(Latency);62}6364public:65explicit LatencyAccountant(FuncIdConversionHelper &FuncIdHelper,66bool RecursiveCallsOnly, bool DeduceSiblingCalls)67: FuncIdHelper(FuncIdHelper), RecursiveCallsOnly(RecursiveCallsOnly),68DeduceSiblingCalls(DeduceSiblingCalls) {}6970const FunctionLatencyMap &getFunctionLatencies() const {71return FunctionLatencies;72}7374const PerThreadMinMaxTSCMap &getPerThreadMinMaxTSC() const {75return PerThreadMinMaxTSC;76}7778const PerCPUMinMaxTSCMap &getPerCPUMinMaxTSC() const {79return PerCPUMinMaxTSC;80}8182/// 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///91bool accountRecord(const XRayRecord &Record);9293const PerThreadFunctionStackMap &getPerThreadFunctionStack() const {94return PerThreadFunctionStack;95}9697// Output Functions98// ================99100void exportStatsAsText(raw_ostream &OS, const XRayFileHeader &Header) const;101void exportStatsAsCSV(raw_ostream &OS, const XRayFileHeader &Header) const;102103private:104// Internal helper to implement common parts of the exportStatsAs...105// functions.106template <class F> void exportStats(const XRayFileHeader &Header, F fn) const;107};108109} // namespace xray110} // namespace llvm111112#endif // LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H113114115