2017-01-16 21:36:26 +01:00
|
|
|
//===-- xray-graph.h - XRay Function Call Graph Renderer --------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Generate a DOT file to represent the function call graph encountered in
|
|
|
|
// the trace.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef XRAY_GRAPH_H
|
|
|
|
#define XRAY_GRAPH_H
|
|
|
|
|
2017-01-25 08:14:43 +01:00
|
|
|
#include <string>
|
2017-01-16 21:36:26 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "func-id-helper.h"
|
2017-02-25 01:26:42 +01:00
|
|
|
#include "xray-color-helper.h"
|
2017-01-16 21:36:26 +01:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2017-01-25 08:14:43 +01:00
|
|
|
#include "llvm/Support/Errc.h"
|
2017-01-17 10:39:31 +01:00
|
|
|
#include "llvm/Support/Program.h"
|
2017-01-25 08:14:43 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-10 07:36:08 +01:00
|
|
|
#include "llvm/XRay/Graph.h"
|
2017-01-16 21:36:26 +01:00
|
|
|
#include "llvm/XRay/Trace.h"
|
|
|
|
#include "llvm/XRay/XRayRecord.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace xray {
|
|
|
|
|
|
|
|
/// A class encapsulating the logic related to analyzing XRay traces, producting
|
|
|
|
/// Graphs from them and then exporting those graphs for review.
|
|
|
|
class GraphRenderer {
|
|
|
|
public:
|
2017-01-25 08:14:43 +01:00
|
|
|
/// An enum for enumerating the various statistics gathered on latencies
|
|
|
|
enum class StatType { NONE, COUNT, MIN, MED, PCT90, PCT99, MAX, SUM };
|
|
|
|
|
2017-01-16 21:36:26 +01:00
|
|
|
/// An inner struct for common timing statistics information
|
|
|
|
struct TimeStat {
|
2017-04-24 07:54:33 +02:00
|
|
|
int64_t Count;
|
|
|
|
double Min;
|
|
|
|
double Median;
|
|
|
|
double Pct90;
|
|
|
|
double Pct99;
|
|
|
|
double Max;
|
|
|
|
double Sum;
|
|
|
|
|
|
|
|
std::string getString(StatType T) const;
|
|
|
|
double getDouble(StatType T) const;
|
2017-01-16 21:36:26 +01:00
|
|
|
};
|
2017-04-24 07:54:33 +02:00
|
|
|
using TimestampT = uint64_t;
|
2017-01-16 21:36:26 +01:00
|
|
|
|
|
|
|
/// An inner struct for storing edge attributes for our graph. Here the
|
|
|
|
/// attributes are mainly function call statistics.
|
|
|
|
///
|
|
|
|
/// FIXME: expand to contain more information eg call latencies.
|
2017-02-10 07:36:08 +01:00
|
|
|
struct CallStats {
|
2017-01-16 21:36:26 +01:00
|
|
|
TimeStat S;
|
2017-02-10 07:36:08 +01:00
|
|
|
std::vector<TimestampT> Timings;
|
2017-01-16 21:36:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// An Inner Struct for storing vertex attributes, at the moment just
|
|
|
|
/// SymbolNames, however in future we could store bulk function statistics.
|
|
|
|
///
|
|
|
|
/// FIXME: Store more attributes based on instrumentation map.
|
2017-02-10 07:36:08 +01:00
|
|
|
struct FunctionStats {
|
2017-01-16 21:36:26 +01:00
|
|
|
std::string SymbolName;
|
2017-04-24 07:54:33 +02:00
|
|
|
TimeStat S = {};
|
2017-01-16 21:36:26 +01:00
|
|
|
};
|
|
|
|
|
2017-01-25 08:14:43 +01:00
|
|
|
struct FunctionAttr {
|
|
|
|
int32_t FuncId;
|
|
|
|
uint64_t TSC;
|
|
|
|
};
|
|
|
|
|
2017-04-24 07:54:33 +02:00
|
|
|
using FunctionStack = SmallVector<FunctionAttr, 4>;
|
2017-01-25 08:14:43 +01:00
|
|
|
|
2017-04-24 07:54:33 +02:00
|
|
|
using PerThreadFunctionStackMap =
|
|
|
|
DenseMap<llvm::sys::ProcessInfo::ProcessId, FunctionStack>;
|
2017-01-25 08:14:43 +01:00
|
|
|
|
2017-02-10 07:36:08 +01:00
|
|
|
class GraphT : public Graph<FunctionStats, CallStats, int32_t> {
|
|
|
|
public:
|
|
|
|
TimeStat GraphEdgeMax = {};
|
|
|
|
TimeStat GraphVertexMax = {};
|
|
|
|
};
|
2017-01-16 21:36:26 +01:00
|
|
|
|
2017-02-10 07:36:08 +01:00
|
|
|
GraphT G;
|
2017-04-24 07:54:33 +02:00
|
|
|
using VertexIdentifier = typename decltype(G)::VertexIdentifier;
|
|
|
|
using EdgeIdentifier = decltype(G)::EdgeIdentifier;
|
2017-01-16 21:36:26 +01:00
|
|
|
|
|
|
|
/// Use a Map to store the Function stack for each thread whilst building the
|
|
|
|
/// graph.
|
|
|
|
///
|
|
|
|
/// FIXME: Perhaps we can Build this into LatencyAccountant? or vise versa?
|
2017-01-25 08:14:43 +01:00
|
|
|
PerThreadFunctionStackMap PerThreadFunctionStack;
|
2017-01-16 21:36:26 +01:00
|
|
|
|
|
|
|
/// Usefull object for getting human readable Symbol Names.
|
2017-04-24 07:54:33 +02:00
|
|
|
FuncIdConversionHelper FuncIdHelper;
|
2017-01-16 21:36:26 +01:00
|
|
|
bool DeduceSiblingCalls = false;
|
2017-02-10 07:36:08 +01:00
|
|
|
TimestampT CurrentMaxTSC = 0;
|
2017-01-16 21:36:26 +01:00
|
|
|
|
|
|
|
/// A private function to help implement the statistic generation functions;
|
|
|
|
template <typename U>
|
|
|
|
void getStats(U begin, U end, GraphRenderer::TimeStat &S);
|
2017-01-25 08:14:43 +01:00
|
|
|
void updateMaxStats(const TimeStat &S, TimeStat &M);
|
2017-01-16 21:36:26 +01:00
|
|
|
|
|
|
|
/// Calculates latency statistics for each edge and stores the data in the
|
|
|
|
/// Graph
|
|
|
|
void calculateEdgeStatistics();
|
|
|
|
|
|
|
|
/// Calculates latency statistics for each vertex and stores the data in the
|
|
|
|
/// Graph
|
|
|
|
void calculateVertexStatistics();
|
|
|
|
|
|
|
|
/// Normalises latency statistics for each edge and vertex by CycleFrequency;
|
2017-01-25 08:14:43 +01:00
|
|
|
void normalizeStatistics(double CycleFrequency);
|
2017-01-16 21:36:26 +01:00
|
|
|
|
2017-02-25 01:26:42 +01:00
|
|
|
/// An object to color gradients
|
|
|
|
ColorHelper CHelper;
|
|
|
|
|
2017-01-16 21:36:26 +01:00
|
|
|
public:
|
|
|
|
/// Takes in a reference to a FuncIdHelper in order to have ready access to
|
|
|
|
/// Symbol names.
|
2017-02-25 01:26:42 +01:00
|
|
|
explicit GraphRenderer(const FuncIdConversionHelper &FuncIdHelper, bool DSC)
|
|
|
|
: FuncIdHelper(FuncIdHelper), DeduceSiblingCalls(DSC),
|
|
|
|
CHelper(ColorHelper::SequentialScheme::OrRd) {
|
2017-02-10 07:36:08 +01:00
|
|
|
G[0] = {};
|
|
|
|
}
|
2017-01-16 21:36:26 +01:00
|
|
|
|
|
|
|
/// Process an Xray record and expand the graph.
|
|
|
|
///
|
|
|
|
/// This Function will return true on success, or false if records are not
|
|
|
|
/// presented in per-thread call-tree DFS order. (That is for each thread the
|
|
|
|
/// Records should be in order runtime on an ideal system.)
|
|
|
|
///
|
|
|
|
/// FIXME: Make this more robust against small irregularities.
|
2017-01-25 08:14:43 +01:00
|
|
|
Error accountRecord(const XRayRecord &Record);
|
2017-01-16 21:36:26 +01:00
|
|
|
|
2017-02-10 07:36:08 +01:00
|
|
|
const PerThreadFunctionStackMap &getPerThreadFunctionStack() const {
|
2017-01-25 08:14:43 +01:00
|
|
|
return PerThreadFunctionStack;
|
|
|
|
}
|
2017-01-16 21:36:26 +01:00
|
|
|
|
2017-04-24 07:54:33 +02:00
|
|
|
class Factory {
|
|
|
|
public:
|
|
|
|
bool KeepGoing;
|
|
|
|
bool DeduceSiblingCalls;
|
|
|
|
std::string InstrMap;
|
2017-04-24 08:15:53 +02:00
|
|
|
::llvm::xray::Trace Trace;
|
2017-04-24 07:54:33 +02:00
|
|
|
Expected<GraphRenderer> getGraphRenderer();
|
|
|
|
};
|
|
|
|
|
2017-01-16 21:36:26 +01:00
|
|
|
/// Output the Embedded graph in DOT format on \p OS, labeling the edges by
|
|
|
|
/// \p T
|
2017-04-24 07:54:33 +02:00
|
|
|
void exportGraphAsDOT(raw_ostream &OS, StatType EdgeLabel = StatType::NONE,
|
2017-01-25 08:14:43 +01:00
|
|
|
StatType EdgeColor = StatType::NONE,
|
|
|
|
StatType VertexLabel = StatType::NONE,
|
|
|
|
StatType VertexColor = StatType::NONE);
|
2017-02-10 07:36:08 +01:00
|
|
|
|
|
|
|
/// Get a reference to the internal graph.
|
2017-04-24 07:54:33 +02:00
|
|
|
const GraphT &getGraph() { return G; }
|
2017-01-16 21:36:26 +01:00
|
|
|
};
|
2017-04-24 07:54:33 +02:00
|
|
|
|
|
|
|
/// Vector Sum of TimeStats
|
|
|
|
inline GraphRenderer::TimeStat operator+(const GraphRenderer::TimeStat &A,
|
|
|
|
const GraphRenderer::TimeStat &B) {
|
|
|
|
return {A.Count + B.Count, A.Min + B.Min, A.Median + B.Median,
|
|
|
|
A.Pct90 + B.Pct90, A.Pct99 + B.Pct99, A.Max + B.Max,
|
|
|
|
A.Sum + B.Sum};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Vector Difference of Timestats
|
|
|
|
inline GraphRenderer::TimeStat operator-(const GraphRenderer::TimeStat &A,
|
|
|
|
const GraphRenderer::TimeStat &B) {
|
|
|
|
|
|
|
|
return {A.Count - B.Count, A.Min - B.Min, A.Median - B.Median,
|
|
|
|
A.Pct90 - B.Pct90, A.Pct99 - B.Pct99, A.Max - B.Max,
|
|
|
|
A.Sum - B.Sum};
|
2017-01-16 21:36:26 +01:00
|
|
|
}
|
2017-04-24 07:54:33 +02:00
|
|
|
|
|
|
|
/// Scalar Diference of TimeStat and double
|
|
|
|
inline GraphRenderer::TimeStat operator/(const GraphRenderer::TimeStat &A,
|
|
|
|
double B) {
|
|
|
|
|
|
|
|
return {static_cast<int64_t>(A.Count / B),
|
|
|
|
A.Min / B,
|
|
|
|
A.Median / B,
|
|
|
|
A.Pct90 / B,
|
|
|
|
A.Pct99 / B,
|
|
|
|
A.Max / B,
|
|
|
|
A.Sum / B};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scalar product of TimeStat and Double
|
|
|
|
inline GraphRenderer::TimeStat operator*(const GraphRenderer::TimeStat &A,
|
|
|
|
double B) {
|
|
|
|
return {static_cast<int64_t>(A.Count * B),
|
|
|
|
A.Min * B,
|
|
|
|
A.Median * B,
|
|
|
|
A.Pct90 * B,
|
|
|
|
A.Pct99 * B,
|
|
|
|
A.Max * B,
|
|
|
|
A.Sum * B};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scalar product of double TimeStat
|
|
|
|
inline GraphRenderer::TimeStat operator*(double A,
|
|
|
|
const GraphRenderer::TimeStat &B) {
|
|
|
|
return B * A;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hadamard Product of TimeStats
|
|
|
|
inline GraphRenderer::TimeStat operator*(const GraphRenderer::TimeStat &A,
|
|
|
|
const GraphRenderer::TimeStat &B) {
|
|
|
|
return {A.Count * B.Count, A.Min * B.Min, A.Median * B.Median,
|
|
|
|
A.Pct90 * B.Pct90, A.Pct99 * B.Pct99, A.Max * B.Max,
|
|
|
|
A.Sum * B.Sum};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hadamard Division of TimeStats
|
|
|
|
inline GraphRenderer::TimeStat operator/(const GraphRenderer::TimeStat &A,
|
|
|
|
const GraphRenderer::TimeStat &B) {
|
2017-04-26 03:35:23 +02:00
|
|
|
return {A.Count / B.Count, A.Min / B.Min, A.Median / B.Median,
|
|
|
|
A.Pct90 / B.Pct90, A.Pct99 / B.Pct99, A.Max / B.Max,
|
|
|
|
A.Sum / B.Sum};
|
2017-01-16 21:36:26 +01:00
|
|
|
}
|
2017-04-24 07:54:33 +02:00
|
|
|
} // namespace xray
|
|
|
|
} // namespace llvm
|
2017-01-16 21:36:26 +01:00
|
|
|
|
|
|
|
#endif // XRAY_GRAPH_H
|