Path: blob/main/contrib/llvm-project/llvm/tools/llvm-xray/xray-graph.cpp
35231 views
//===-- xray-graph.cpp: XRay Function Call Graph Renderer -----------------===//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// Generate a DOT file to represent the function call graph encountered in9// the trace.10//11//===----------------------------------------------------------------------===//1213#include "xray-graph.h"14#include "xray-registry.h"15#include "llvm/Support/ErrorHandling.h"16#include "llvm/XRay/InstrumentationMap.h"17#include "llvm/XRay/Trace.h"1819#include <cmath>2021using namespace llvm;22using namespace llvm::xray;2324// Setup llvm-xray graph subcommand and its options.25static cl::SubCommand GraphC("graph", "Generate function-call graph");26static cl::opt<std::string> GraphInput(cl::Positional,27cl::desc("<xray log file>"),28cl::Required, cl::sub(GraphC));2930static cl::opt<bool>31GraphKeepGoing("keep-going", cl::desc("Keep going on errors encountered"),32cl::sub(GraphC), cl::init(false));33static cl::alias GraphKeepGoing2("k", cl::aliasopt(GraphKeepGoing),34cl::desc("Alias for -keep-going"));3536static cl::opt<std::string>37GraphOutput("output", cl::value_desc("Output file"), cl::init("-"),38cl::desc("output file; use '-' for stdout"), cl::sub(GraphC));39static cl::alias GraphOutput2("o", cl::aliasopt(GraphOutput),40cl::desc("Alias for -output"));4142static cl::opt<std::string>43GraphInstrMap("instr_map",44cl::desc("binary with the instrumrntation map, or "45"a separate instrumentation map"),46cl::value_desc("binary with xray_instr_map"), cl::sub(GraphC),47cl::init(""));48static cl::alias GraphInstrMap2("m", cl::aliasopt(GraphInstrMap),49cl::desc("alias for -instr_map"));5051static cl::opt<bool> GraphDeduceSiblingCalls(52"deduce-sibling-calls",53cl::desc("Deduce sibling calls when unrolling function call stacks"),54cl::sub(GraphC), cl::init(false));55static cl::alias56GraphDeduceSiblingCalls2("d", cl::aliasopt(GraphDeduceSiblingCalls),57cl::desc("Alias for -deduce-sibling-calls"));5859static cl::opt<GraphRenderer::StatType>60GraphEdgeLabel("edge-label",61cl::desc("Output graphs with edges labeled with this field"),62cl::value_desc("field"), cl::sub(GraphC),63cl::init(GraphRenderer::StatType::NONE),64cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",65"Do not label Edges"),66clEnumValN(GraphRenderer::StatType::COUNT,67"count", "function call counts"),68clEnumValN(GraphRenderer::StatType::MIN, "min",69"minimum function durations"),70clEnumValN(GraphRenderer::StatType::MED, "med",71"median function durations"),72clEnumValN(GraphRenderer::StatType::PCT90, "90p",73"90th percentile durations"),74clEnumValN(GraphRenderer::StatType::PCT99, "99p",75"99th percentile durations"),76clEnumValN(GraphRenderer::StatType::MAX, "max",77"maximum function durations"),78clEnumValN(GraphRenderer::StatType::SUM, "sum",79"sum of call durations")));80static cl::alias GraphEdgeLabel2("e", cl::aliasopt(GraphEdgeLabel),81cl::desc("Alias for -edge-label"));8283static cl::opt<GraphRenderer::StatType> GraphVertexLabel(84"vertex-label",85cl::desc("Output graphs with vertices labeled with this field"),86cl::value_desc("field"), cl::sub(GraphC),87cl::init(GraphRenderer::StatType::NONE),88cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",89"Do not label Vertices"),90clEnumValN(GraphRenderer::StatType::COUNT, "count",91"function call counts"),92clEnumValN(GraphRenderer::StatType::MIN, "min",93"minimum function durations"),94clEnumValN(GraphRenderer::StatType::MED, "med",95"median function durations"),96clEnumValN(GraphRenderer::StatType::PCT90, "90p",97"90th percentile durations"),98clEnumValN(GraphRenderer::StatType::PCT99, "99p",99"99th percentile durations"),100clEnumValN(GraphRenderer::StatType::MAX, "max",101"maximum function durations"),102clEnumValN(GraphRenderer::StatType::SUM, "sum",103"sum of call durations")));104static cl::alias GraphVertexLabel2("v", cl::aliasopt(GraphVertexLabel),105cl::desc("Alias for -edge-label"));106107static cl::opt<GraphRenderer::StatType> GraphEdgeColorType(108"color-edges",109cl::desc("Output graphs with edge colors determined by this field"),110cl::value_desc("field"), cl::sub(GraphC),111cl::init(GraphRenderer::StatType::NONE),112cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",113"Do not color Edges"),114clEnumValN(GraphRenderer::StatType::COUNT, "count",115"function call counts"),116clEnumValN(GraphRenderer::StatType::MIN, "min",117"minimum function durations"),118clEnumValN(GraphRenderer::StatType::MED, "med",119"median function durations"),120clEnumValN(GraphRenderer::StatType::PCT90, "90p",121"90th percentile durations"),122clEnumValN(GraphRenderer::StatType::PCT99, "99p",123"99th percentile durations"),124clEnumValN(GraphRenderer::StatType::MAX, "max",125"maximum function durations"),126clEnumValN(GraphRenderer::StatType::SUM, "sum",127"sum of call durations")));128static cl::alias GraphEdgeColorType2("c", cl::aliasopt(GraphEdgeColorType),129cl::desc("Alias for -color-edges"));130131static cl::opt<GraphRenderer::StatType> GraphVertexColorType(132"color-vertices",133cl::desc("Output graphs with vertex colors determined by this field"),134cl::value_desc("field"), cl::sub(GraphC),135cl::init(GraphRenderer::StatType::NONE),136cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",137"Do not color vertices"),138clEnumValN(GraphRenderer::StatType::COUNT, "count",139"function call counts"),140clEnumValN(GraphRenderer::StatType::MIN, "min",141"minimum function durations"),142clEnumValN(GraphRenderer::StatType::MED, "med",143"median function durations"),144clEnumValN(GraphRenderer::StatType::PCT90, "90p",145"90th percentile durations"),146clEnumValN(GraphRenderer::StatType::PCT99, "99p",147"99th percentile durations"),148clEnumValN(GraphRenderer::StatType::MAX, "max",149"maximum function durations"),150clEnumValN(GraphRenderer::StatType::SUM, "sum",151"sum of call durations")));152static cl::alias GraphVertexColorType2("b", cl::aliasopt(GraphVertexColorType),153cl::desc("Alias for -edge-label"));154155template <class T> T diff(T L, T R) { return std::max(L, R) - std::min(L, R); }156157// Updates the statistics for a GraphRenderer::TimeStat158static void updateStat(GraphRenderer::TimeStat &S, int64_t L) {159S.Count++;160if (S.Min > L || S.Min == 0)161S.Min = L;162if (S.Max < L)163S.Max = L;164S.Sum += L;165}166167// Labels in a DOT graph must be legal XML strings so it's necessary to escape168// certain characters.169static std::string escapeString(StringRef Label) {170std::string Str;171Str.reserve(Label.size());172for (const auto C : Label) {173switch (C) {174case '&':175Str.append("&");176break;177case '<':178Str.append("<");179break;180case '>':181Str.append(">");182break;183default:184Str.push_back(C);185break;186}187}188return Str;189}190191// Evaluates an XRay record and performs accounting on it.192//193// If the record is an ENTER record it pushes the FuncID and TSC onto a194// structure representing the call stack for that function.195// If the record is an EXIT record it checks computes computes the ammount of196// time the function took to complete and then stores that information in an197// edge of the graph. If there is no matching ENTER record the function tries198// to recover by assuming that there were EXIT records which were missed, for199// example caused by tail call elimination and if the option is enabled then200// then tries to recover from this.201//202// This function will also error if the records are out of order, as the trace203// is expected to be sorted.204//205// The graph generated has an immaginary root for functions called by no-one at206// FuncId 0.207//208// FIXME: Refactor this and account subcommand to reduce code duplication.209Error GraphRenderer::accountRecord(const XRayRecord &Record) {210using std::make_error_code;211using std::errc;212if (CurrentMaxTSC == 0)213CurrentMaxTSC = Record.TSC;214215if (Record.TSC < CurrentMaxTSC)216return make_error<StringError>("Records not in order",217make_error_code(errc::invalid_argument));218219auto &ThreadStack = PerThreadFunctionStack[Record.TId];220switch (Record.Type) {221case RecordTypes::ENTER:222case RecordTypes::ENTER_ARG: {223if (Record.FuncId != 0 && G.count(Record.FuncId) == 0)224G[Record.FuncId].SymbolName = FuncIdHelper.SymbolOrNumber(Record.FuncId);225ThreadStack.push_back({Record.FuncId, Record.TSC});226break;227}228case RecordTypes::EXIT:229case RecordTypes::TAIL_EXIT: {230// FIXME: Refactor this and the account subcommand to reduce code231// duplication232if (ThreadStack.size() == 0 || ThreadStack.back().FuncId != Record.FuncId) {233if (!DeduceSiblingCalls)234return make_error<StringError>("No matching ENTRY record",235make_error_code(errc::invalid_argument));236bool FoundParent =237llvm::any_of(llvm::reverse(ThreadStack), [&](const FunctionAttr &A) {238return A.FuncId == Record.FuncId;239});240if (!FoundParent)241return make_error<StringError>(242"No matching Entry record in stack",243make_error_code(errc::invalid_argument)); // There is no matching244// Function for this exit.245while (ThreadStack.back().FuncId != Record.FuncId) {246TimestampT D = diff(ThreadStack.back().TSC, Record.TSC);247VertexIdentifier TopFuncId = ThreadStack.back().FuncId;248ThreadStack.pop_back();249assert(ThreadStack.size() != 0);250EdgeIdentifier EI(ThreadStack.back().FuncId, TopFuncId);251auto &EA = G[EI];252EA.Timings.push_back(D);253updateStat(EA.S, D);254updateStat(G[TopFuncId].S, D);255}256}257uint64_t D = diff(ThreadStack.back().TSC, Record.TSC);258ThreadStack.pop_back();259VertexIdentifier VI = ThreadStack.empty() ? 0 : ThreadStack.back().FuncId;260EdgeIdentifier EI(VI, Record.FuncId);261auto &EA = G[EI];262EA.Timings.push_back(D);263updateStat(EA.S, D);264updateStat(G[Record.FuncId].S, D);265break;266}267case RecordTypes::CUSTOM_EVENT:268case RecordTypes::TYPED_EVENT:269// TODO: Support custom and typed events in the graph processing?270break;271}272273return Error::success();274}275276template <typename U>277void GraphRenderer::getStats(U begin, U end, GraphRenderer::TimeStat &S) {278if (begin == end) return;279std::ptrdiff_t MedianOff = S.Count / 2;280std::nth_element(begin, begin + MedianOff, end);281S.Median = *(begin + MedianOff);282std::ptrdiff_t Pct90Off = (S.Count * 9) / 10;283std::nth_element(begin, begin + Pct90Off, end);284S.Pct90 = *(begin + Pct90Off);285std::ptrdiff_t Pct99Off = (S.Count * 99) / 100;286std::nth_element(begin, begin + Pct99Off, end);287S.Pct99 = *(begin + Pct99Off);288}289290void GraphRenderer::updateMaxStats(const GraphRenderer::TimeStat &S,291GraphRenderer::TimeStat &M) {292M.Count = std::max(M.Count, S.Count);293M.Min = std::max(M.Min, S.Min);294M.Median = std::max(M.Median, S.Median);295M.Pct90 = std::max(M.Pct90, S.Pct90);296M.Pct99 = std::max(M.Pct99, S.Pct99);297M.Max = std::max(M.Max, S.Max);298M.Sum = std::max(M.Sum, S.Sum);299}300301void GraphRenderer::calculateEdgeStatistics() {302assert(!G.edges().empty());303for (auto &E : G.edges()) {304auto &A = E.second;305assert(!A.Timings.empty());306getStats(A.Timings.begin(), A.Timings.end(), A.S);307updateMaxStats(A.S, G.GraphEdgeMax);308}309}310311void GraphRenderer::calculateVertexStatistics() {312std::vector<uint64_t> TempTimings;313for (auto &V : G.vertices()) {314if (V.first != 0) {315for (auto &E : G.inEdges(V.first)) {316auto &A = E.second;317llvm::append_range(TempTimings, A.Timings);318}319getStats(TempTimings.begin(), TempTimings.end(), G[V.first].S);320updateMaxStats(G[V.first].S, G.GraphVertexMax);321TempTimings.clear();322}323}324}325326// A Helper function for normalizeStatistics which normalises a single327// TimeStat element.328static void normalizeTimeStat(GraphRenderer::TimeStat &S,329double CycleFrequency) {330int64_t OldCount = S.Count;331S = S / CycleFrequency;332S.Count = OldCount;333}334335// Normalises the statistics in the graph for a given TSC frequency.336void GraphRenderer::normalizeStatistics(double CycleFrequency) {337for (auto &E : G.edges()) {338auto &S = E.second.S;339normalizeTimeStat(S, CycleFrequency);340}341for (auto &V : G.vertices()) {342auto &S = V.second.S;343normalizeTimeStat(S, CycleFrequency);344}345346normalizeTimeStat(G.GraphEdgeMax, CycleFrequency);347normalizeTimeStat(G.GraphVertexMax, CycleFrequency);348}349350// Returns a string containing the value of statistic field T351std::string352GraphRenderer::TimeStat::getString(GraphRenderer::StatType T) const {353std::string St;354raw_string_ostream S{St};355double TimeStat::*DoubleStatPtrs[] = {&TimeStat::Min, &TimeStat::Median,356&TimeStat::Pct90, &TimeStat::Pct99,357&TimeStat::Max, &TimeStat::Sum};358switch (T) {359case GraphRenderer::StatType::NONE:360break;361case GraphRenderer::StatType::COUNT:362S << Count;363break;364default:365S << (*this).*366DoubleStatPtrs[static_cast<int>(T) -367static_cast<int>(GraphRenderer::StatType::MIN)];368break;369}370return S.str();371}372373// Returns the quotient between the property T of this and another TimeStat as374// a double375double GraphRenderer::TimeStat::getDouble(StatType T) const {376double retval = 0;377double TimeStat::*DoubleStatPtrs[] = {&TimeStat::Min, &TimeStat::Median,378&TimeStat::Pct90, &TimeStat::Pct99,379&TimeStat::Max, &TimeStat::Sum};380switch (T) {381case GraphRenderer::StatType::NONE:382retval = 0.0;383break;384case GraphRenderer::StatType::COUNT:385retval = static_cast<double>(Count);386break;387default:388retval =389(*this).*DoubleStatPtrs[static_cast<int>(T) -390static_cast<int>(GraphRenderer::StatType::MIN)];391break;392}393return retval;394}395396// Outputs a DOT format version of the Graph embedded in the GraphRenderer397// object on OS. It does this in the expected way by itterating398// through all edges then vertices and then outputting them and their399// annotations.400//401// FIXME: output more information, better presented.402void GraphRenderer::exportGraphAsDOT(raw_ostream &OS, StatType ET, StatType EC,403StatType VT, StatType VC) {404OS << "digraph xray {\n";405406if (VT != StatType::NONE)407OS << "node [shape=record];\n";408409for (const auto &E : G.edges()) {410const auto &S = E.second.S;411OS << "F" << E.first.first << " -> "412<< "F" << E.first.second << " [label=\"" << S.getString(ET) << "\"";413if (EC != StatType::NONE)414OS << " color=\""415<< CHelper.getColorString(416std::sqrt(S.getDouble(EC) / G.GraphEdgeMax.getDouble(EC)))417<< "\"";418OS << "];\n";419}420421for (const auto &V : G.vertices()) {422const auto &VA = V.second;423if (V.first == 0)424continue;425OS << "F" << V.first << " [label=\"" << (VT != StatType::NONE ? "{" : "")426<< escapeString(VA.SymbolName.size() > 40427? VA.SymbolName.substr(0, 40) + "..."428: VA.SymbolName);429if (VT != StatType::NONE)430OS << "|" << VA.S.getString(VT) << "}\"";431else432OS << "\"";433if (VC != StatType::NONE)434OS << " color=\""435<< CHelper.getColorString(436std::sqrt(VA.S.getDouble(VC) / G.GraphVertexMax.getDouble(VC)))437<< "\"";438OS << "];\n";439}440OS << "}\n";441}442443Expected<GraphRenderer> GraphRenderer::Factory::getGraphRenderer() {444InstrumentationMap Map;445if (!GraphInstrMap.empty()) {446auto InstrumentationMapOrError = loadInstrumentationMap(GraphInstrMap);447if (!InstrumentationMapOrError)448return joinErrors(449make_error<StringError>(450Twine("Cannot open instrumentation map '") + GraphInstrMap + "'",451std::make_error_code(std::errc::invalid_argument)),452InstrumentationMapOrError.takeError());453Map = std::move(*InstrumentationMapOrError);454}455456const auto &FunctionAddresses = Map.getFunctionAddresses();457458symbolize::LLVMSymbolizer Symbolizer;459const auto &Header = Trace.getFileHeader();460461llvm::xray::FuncIdConversionHelper FuncIdHelper(InstrMap, Symbolizer,462FunctionAddresses);463464xray::GraphRenderer GR(FuncIdHelper, DeduceSiblingCalls);465for (const auto &Record : Trace) {466auto E = GR.accountRecord(Record);467if (!E)468continue;469470for (const auto &ThreadStack : GR.getPerThreadFunctionStack()) {471errs() << "Thread ID: " << ThreadStack.first << "\n";472auto Level = ThreadStack.second.size();473for (const auto &Entry : llvm::reverse(ThreadStack.second))474errs() << "#" << Level-- << "\t"475<< FuncIdHelper.SymbolOrNumber(Entry.FuncId) << '\n';476}477478if (!GraphKeepGoing)479return joinErrors(make_error<StringError>(480"Error encountered generating the call graph.",481std::make_error_code(std::errc::invalid_argument)),482std::move(E));483484handleAllErrors(std::move(E),485[&](const ErrorInfoBase &E) { E.log(errs()); });486}487488GR.G.GraphEdgeMax = {};489GR.G.GraphVertexMax = {};490GR.calculateEdgeStatistics();491GR.calculateVertexStatistics();492493if (Header.CycleFrequency)494GR.normalizeStatistics(Header.CycleFrequency);495496return GR;497}498499// Here we register and implement the llvm-xray graph subcommand.500// The bulk of this code reads in the options, opens the required files, uses501// those files to create a context for analysing the xray trace, then there is a502// short loop which actually analyses the trace, generates the graph and then503// outputs it as a DOT.504//505// FIXME: include additional filtering and annalysis passes to provide more506// specific useful information.507static CommandRegistration Unused(&GraphC, []() -> Error {508GraphRenderer::Factory F;509510F.KeepGoing = GraphKeepGoing;511F.DeduceSiblingCalls = GraphDeduceSiblingCalls;512F.InstrMap = GraphInstrMap;513514auto TraceOrErr = loadTraceFile(GraphInput, true);515516if (!TraceOrErr)517return make_error<StringError>(518Twine("Failed loading input file '") + GraphInput + "'",519make_error_code(llvm::errc::invalid_argument));520521F.Trace = std::move(*TraceOrErr);522auto GROrError = F.getGraphRenderer();523if (!GROrError)524return GROrError.takeError();525auto &GR = *GROrError;526527std::error_code EC;528raw_fd_ostream OS(GraphOutput, EC, sys::fs::OpenFlags::OF_TextWithCRLF);529if (EC)530return make_error<StringError>(531Twine("Cannot open file '") + GraphOutput + "' for writing.", EC);532533GR.exportGraphAsDOT(OS, GraphEdgeLabel, GraphEdgeColorType, GraphVertexLabel,534GraphVertexColorType);535return Error::success();536});537538539