blob: 685c24cb918763ab0f75bcffa5b1ee6b8c8d1f99 [file] [log] [blame]
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +00001//===-- xray-graph.cc - XRay Function Call Graph Renderer -----------------===//
David Blaikie87299ad2017-01-16 20:36:26 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Generate a DOT file to represent the function call graph encountered in
11// the trace.
12//
13//===----------------------------------------------------------------------===//
14#include <algorithm>
15#include <cassert>
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000016#include <cmath>
David Blaikie87299ad2017-01-16 20:36:26 +000017#include <system_error>
18#include <utility>
19
David Blaikie87299ad2017-01-16 20:36:26 +000020#include "xray-graph.h"
21#include "xray-registry.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/FormatVariadic.h"
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000024#include "llvm/XRay/InstrumentationMap.h"
David Blaikie87299ad2017-01-16 20:36:26 +000025#include "llvm/XRay/Trace.h"
26#include "llvm/XRay/YAMLXRayRecord.h"
27
28using namespace llvm;
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000029using namespace llvm::xray;
David Blaikie87299ad2017-01-16 20:36:26 +000030
31// Setup llvm-xray graph subcommand and its options.
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000032static cl::SubCommand GraphC("graph", "Generate function-call graph");
David Blaikie87299ad2017-01-16 20:36:26 +000033static cl::opt<std::string> GraphInput(cl::Positional,
34 cl::desc("<xray log file>"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000035 cl::Required, cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000036
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000037static cl::opt<bool>
38 GraphKeepGoing("keep-going", cl::desc("Keep going on errors encountered"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000039 cl::sub(GraphC), cl::init(false));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000040static cl::alias GraphKeepGoing2("k", cl::aliasopt(GraphKeepGoing),
41 cl::desc("Alias for -keep-going"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000042 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000043
David Blaikie87299ad2017-01-16 20:36:26 +000044static cl::opt<std::string>
45 GraphOutput("output", cl::value_desc("Output file"), cl::init("-"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000046 cl::desc("output file; use '-' for stdout"), cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000047static cl::alias GraphOutput2("o", cl::aliasopt(GraphOutput),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000048 cl::desc("Alias for -output"), cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000049
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000050static cl::opt<std::string>
51 GraphInstrMap("instr_map",
52 cl::desc("binary with the instrumrntation map, or "
53 "a separate instrumentation map"),
54 cl::value_desc("binary with xray_instr_map"), cl::sub(GraphC),
55 cl::init(""));
David Blaikie87299ad2017-01-16 20:36:26 +000056static cl::alias GraphInstrMap2("m", cl::aliasopt(GraphInstrMap),
57 cl::desc("alias for -instr_map"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000058 cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000059
David Blaikie87299ad2017-01-16 20:36:26 +000060static cl::opt<bool> GraphDeduceSiblingCalls(
61 "deduce-sibling-calls",
62 cl::desc("Deduce sibling calls when unrolling function call stacks"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000063 cl::sub(GraphC), cl::init(false));
David Blaikie87299ad2017-01-16 20:36:26 +000064static cl::alias
65 GraphDeduceSiblingCalls2("d", cl::aliasopt(GraphDeduceSiblingCalls),
66 cl::desc("Alias for -deduce-sibling-calls"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000067 cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000068
69static cl::opt<GraphRenderer::StatType>
70 GraphEdgeLabel("edge-label",
71 cl::desc("Output graphs with edges labeled with this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000072 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000073 cl::init(GraphRenderer::StatType::NONE),
74 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
75 "Do not label Edges"),
76 clEnumValN(GraphRenderer::StatType::COUNT,
David Blaikie87299ad2017-01-16 20:36:26 +000077 "count", "function call counts"),
78 clEnumValN(GraphRenderer::StatType::MIN, "min",
79 "minimum function durations"),
80 clEnumValN(GraphRenderer::StatType::MED, "med",
81 "median function durations"),
82 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
83 "90th percentile durations"),
84 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
85 "99th percentile durations"),
86 clEnumValN(GraphRenderer::StatType::MAX, "max",
87 "maximum function durations"),
88 clEnumValN(GraphRenderer::StatType::SUM, "sum",
89 "sum of call durations")));
90static cl::alias GraphEdgeLabel2("e", cl::aliasopt(GraphEdgeLabel),
91 cl::desc("Alias for -edge-label"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000092 cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000093
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000094static cl::opt<GraphRenderer::StatType> GraphVertexLabel(
95 "vertex-label",
96 cl::desc("Output graphs with vertices labeled with this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000097 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000098 cl::init(GraphRenderer::StatType::NONE),
99 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000100 "Do not label Vertices"),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000101 clEnumValN(GraphRenderer::StatType::COUNT, "count",
102 "function call counts"),
103 clEnumValN(GraphRenderer::StatType::MIN, "min",
104 "minimum function durations"),
105 clEnumValN(GraphRenderer::StatType::MED, "med",
106 "median function durations"),
107 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
108 "90th percentile durations"),
109 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
110 "99th percentile durations"),
111 clEnumValN(GraphRenderer::StatType::MAX, "max",
112 "maximum function durations"),
113 clEnumValN(GraphRenderer::StatType::SUM, "sum",
114 "sum of call durations")));
115static cl::alias GraphVertexLabel2("v", cl::aliasopt(GraphVertexLabel),
116 cl::desc("Alias for -edge-label"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000117 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000118
119static cl::opt<GraphRenderer::StatType> GraphEdgeColorType(
120 "color-edges",
121 cl::desc("Output graphs with edge colors determined by this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000122 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000123 cl::init(GraphRenderer::StatType::NONE),
124 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000125 "Do not color Edges"),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000126 clEnumValN(GraphRenderer::StatType::COUNT, "count",
127 "function call counts"),
128 clEnumValN(GraphRenderer::StatType::MIN, "min",
129 "minimum function durations"),
130 clEnumValN(GraphRenderer::StatType::MED, "med",
131 "median function durations"),
132 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
133 "90th percentile durations"),
134 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
135 "99th percentile durations"),
136 clEnumValN(GraphRenderer::StatType::MAX, "max",
137 "maximum function durations"),
138 clEnumValN(GraphRenderer::StatType::SUM, "sum",
139 "sum of call durations")));
140static cl::alias GraphEdgeColorType2("c", cl::aliasopt(GraphEdgeColorType),
141 cl::desc("Alias for -color-edges"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000142 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000143
144static cl::opt<GraphRenderer::StatType> GraphVertexColorType(
145 "color-vertices",
146 cl::desc("Output graphs with vertex colors determined by this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000147 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000148 cl::init(GraphRenderer::StatType::NONE),
149 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000150 "Do not color vertices"),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000151 clEnumValN(GraphRenderer::StatType::COUNT, "count",
152 "function call counts"),
153 clEnumValN(GraphRenderer::StatType::MIN, "min",
154 "minimum function durations"),
155 clEnumValN(GraphRenderer::StatType::MED, "med",
156 "median function durations"),
157 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
158 "90th percentile durations"),
159 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
160 "99th percentile durations"),
161 clEnumValN(GraphRenderer::StatType::MAX, "max",
162 "maximum function durations"),
163 clEnumValN(GraphRenderer::StatType::SUM, "sum",
164 "sum of call durations")));
165static cl::alias GraphVertexColorType2("b", cl::aliasopt(GraphVertexColorType),
166 cl::desc("Alias for -edge-label"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000167 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000168
David Blaikie87299ad2017-01-16 20:36:26 +0000169template <class T> T diff(T L, T R) { return std::max(L, R) - std::min(L, R); }
170
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000171// Updates the statistics for a GraphRenderer::TimeStat
172static void updateStat(GraphRenderer::TimeStat &S, int64_t L) {
David Blaikie87299ad2017-01-16 20:36:26 +0000173 S.Count++;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000174 if (S.Min > L || S.Min == 0)
175 S.Min = L;
176 if (S.Max < L)
177 S.Max = L;
178 S.Sum += L;
David Blaikie87299ad2017-01-16 20:36:26 +0000179}
180
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000181// Evaluates an XRay record and performs accounting on it.
David Blaikie87299ad2017-01-16 20:36:26 +0000182//
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000183// If the record is an ENTER record it pushes the FuncID and TSC onto a
184// structure representing the call stack for that function.
185// If the record is an EXIT record it checks computes computes the ammount of
186// time the function took to complete and then stores that information in an
187// edge of the graph. If there is no matching ENTER record the function tries
188// to recover by assuming that there were EXIT records which were missed, for
189// example caused by tail call elimination and if the option is enabled then
190// then tries to recover from this.
191//
192// This funciton will also error if the records are out of order, as the trace
193// is expected to be sorted.
194//
195// The graph generated has an immaginary root for functions called by no-one at
David Blaikie87299ad2017-01-16 20:36:26 +0000196// FuncId 0.
197//
David Blaikie87299ad2017-01-16 20:36:26 +0000198// FIXME: Refactor this and account subcommand to reduce code duplication.
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000199Error GraphRenderer::accountRecord(const XRayRecord &Record) {
200 using std::make_error_code;
201 using std::errc;
David Blaikie87299ad2017-01-16 20:36:26 +0000202 if (CurrentMaxTSC == 0)
203 CurrentMaxTSC = Record.TSC;
204
205 if (Record.TSC < CurrentMaxTSC)
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000206 return make_error<StringError>("Records not in order",
207 make_error_code(errc::invalid_argument));
David Blaikie87299ad2017-01-16 20:36:26 +0000208
209 auto &ThreadStack = PerThreadFunctionStack[Record.TId];
210 switch (Record.Type) {
211 case RecordTypes::ENTER: {
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000212 if (Record.FuncId != 0 && G.count(Record.FuncId) == 0)
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000213 G[Record.FuncId].SymbolName = FuncIdHelper.SymbolOrNumber(Record.FuncId);
David Blaikie87299ad2017-01-16 20:36:26 +0000214 ThreadStack.push_back({Record.FuncId, Record.TSC});
215 break;
216 }
217 case RecordTypes::EXIT: {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000218 // FIXME: Refactor this and the account subcommand to reduce code
David Blaikie87299ad2017-01-16 20:36:26 +0000219 // duplication
220 if (ThreadStack.size() == 0 || ThreadStack.back().FuncId != Record.FuncId) {
221 if (!DeduceSiblingCalls)
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000222 return make_error<StringError>("No matching ENTRY record",
223 make_error_code(errc::invalid_argument));
David Blaikie87299ad2017-01-16 20:36:26 +0000224 auto Parent = std::find_if(
225 ThreadStack.rbegin(), ThreadStack.rend(),
226 [&](const FunctionAttr &A) { return A.FuncId == Record.FuncId; });
227 if (Parent == ThreadStack.rend())
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000228 return make_error<StringError>(
229 "No matching Entry record in stack",
230 make_error_code(errc::invalid_argument)); // There is no matching
231 // Function for this exit.
David Blaikie87299ad2017-01-16 20:36:26 +0000232 while (ThreadStack.back().FuncId != Record.FuncId) {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000233 TimestampT D = diff(ThreadStack.back().TSC, Record.TSC);
234 VertexIdentifier TopFuncId = ThreadStack.back().FuncId;
David Blaikie87299ad2017-01-16 20:36:26 +0000235 ThreadStack.pop_back();
236 assert(ThreadStack.size() != 0);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000237 EdgeIdentifier EI(ThreadStack.back().FuncId, TopFuncId);
238 auto &EA = G[EI];
David Blaikie87299ad2017-01-16 20:36:26 +0000239 EA.Timings.push_back(D);
240 updateStat(EA.S, D);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000241 updateStat(G[TopFuncId].S, D);
David Blaikie87299ad2017-01-16 20:36:26 +0000242 }
243 }
244 uint64_t D = diff(ThreadStack.back().TSC, Record.TSC);
245 ThreadStack.pop_back();
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000246 VertexIdentifier VI = ThreadStack.empty() ? 0 : ThreadStack.back().FuncId;
247 EdgeIdentifier EI(VI, Record.FuncId);
248 auto &EA = G[EI];
David Blaikie87299ad2017-01-16 20:36:26 +0000249 EA.Timings.push_back(D);
250 updateStat(EA.S, D);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000251 updateStat(G[Record.FuncId].S, D);
David Blaikie87299ad2017-01-16 20:36:26 +0000252 break;
253 }
254 }
255
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000256 return Error::success();
David Blaikie87299ad2017-01-16 20:36:26 +0000257}
258
259template <typename U>
260void GraphRenderer::getStats(U begin, U end, GraphRenderer::TimeStat &S) {
Dean Michael Berris56854682017-03-31 01:56:45 +0000261 if (begin == end) return;
David Blaikie87299ad2017-01-16 20:36:26 +0000262 std::ptrdiff_t MedianOff = S.Count / 2;
263 std::nth_element(begin, begin + MedianOff, end);
264 S.Median = *(begin + MedianOff);
265 std::ptrdiff_t Pct90Off = (S.Count * 9) / 10;
266 std::nth_element(begin, begin + Pct90Off, end);
267 S.Pct90 = *(begin + Pct90Off);
268 std::ptrdiff_t Pct99Off = (S.Count * 99) / 100;
269 std::nth_element(begin, begin + Pct99Off, end);
270 S.Pct99 = *(begin + Pct99Off);
271}
272
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000273void GraphRenderer::updateMaxStats(const GraphRenderer::TimeStat &S,
274 GraphRenderer::TimeStat &M) {
275 M.Count = std::max(M.Count, S.Count);
276 M.Min = std::max(M.Min, S.Min);
277 M.Median = std::max(M.Median, S.Median);
278 M.Pct90 = std::max(M.Pct90, S.Pct90);
279 M.Pct99 = std::max(M.Pct99, S.Pct99);
280 M.Max = std::max(M.Max, S.Max);
281 M.Sum = std::max(M.Sum, S.Sum);
282}
283
David Blaikie87299ad2017-01-16 20:36:26 +0000284void GraphRenderer::calculateEdgeStatistics() {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000285 assert(!G.edges().empty());
286 for (auto &E : G.edges()) {
287 auto &A = E.second;
288 assert(!A.Timings.empty());
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000289 getStats(A.Timings.begin(), A.Timings.end(), A.S);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000290 updateMaxStats(A.S, G.GraphEdgeMax);
David Blaikie87299ad2017-01-16 20:36:26 +0000291 }
292}
293
294void GraphRenderer::calculateVertexStatistics() {
Dean Michael Berris79f57462017-02-10 06:05:46 +0000295 std::vector<uint64_t> TempTimings;
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000296 for (auto &V : G.vertices()) {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000297 if (V.first != 0) {
298 for (auto &E : G.inEdges(V.first)) {
299 auto &A = E.second;
300 TempTimings.insert(TempTimings.end(), A.Timings.begin(),
301 A.Timings.end());
302 }
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000303 getStats(TempTimings.begin(), TempTimings.end(), G[V.first].S);
304 updateMaxStats(G[V.first].S, G.GraphVertexMax);
305 TempTimings.clear();
Dean Michael Berris79f57462017-02-10 06:05:46 +0000306 }
Dean Michael Berris79f57462017-02-10 06:05:46 +0000307 }
David Blaikie87299ad2017-01-16 20:36:26 +0000308}
309
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000310// A Helper function for normalizeStatistics which normalises a single
311// TimeStat element.
312static void normalizeTimeStat(GraphRenderer::TimeStat &S,
313 double CycleFrequency) {
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000314 int64_t OldCount = S.Count;
315 S = S / CycleFrequency;
316 S.Count = OldCount;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000317}
318
319// Normalises the statistics in the graph for a given TSC frequency.
320void GraphRenderer::normalizeStatistics(double CycleFrequency) {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000321 for (auto &E : G.edges()) {
322 auto &S = E.second.S;
323 normalizeTimeStat(S, CycleFrequency);
David Blaikie87299ad2017-01-16 20:36:26 +0000324 }
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000325 for (auto &V : G.vertices()) {
David Blaikie87299ad2017-01-16 20:36:26 +0000326 auto &S = V.second.S;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000327 normalizeTimeStat(S, CycleFrequency);
David Blaikie87299ad2017-01-16 20:36:26 +0000328 }
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000329
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000330 normalizeTimeStat(G.GraphEdgeMax, CycleFrequency);
331 normalizeTimeStat(G.GraphVertexMax, CycleFrequency);
David Blaikie87299ad2017-01-16 20:36:26 +0000332}
333
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000334// Returns a string containing the value of statistic field T
335std::string
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000336GraphRenderer::TimeStat::getString(GraphRenderer::StatType T) const {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000337 std::string St;
338 raw_string_ostream S{St};
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000339 double TimeStat::*DoubleStatPtrs[] = {&TimeStat::Min, &TimeStat::Median,
340 &TimeStat::Pct90, &TimeStat::Pct99,
341 &TimeStat::Max, &TimeStat::Sum};
David Blaikie87299ad2017-01-16 20:36:26 +0000342 switch (T) {
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000343 case GraphRenderer::StatType::NONE:
344 break;
David Blaikie87299ad2017-01-16 20:36:26 +0000345 case GraphRenderer::StatType::COUNT:
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000346 S << Count;
David Blaikie87299ad2017-01-16 20:36:26 +0000347 break;
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000348 default:
349 S << (*this).*
350 DoubleStatPtrs[static_cast<int>(T) -
351 static_cast<int>(GraphRenderer::StatType::MIN)];
David Blaikie87299ad2017-01-16 20:36:26 +0000352 break;
353 }
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000354 return S.str();
David Blaikie87299ad2017-01-16 20:36:26 +0000355}
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000356
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000357// Returns the quotient between the property T of this and another TimeStat as
358// a double
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000359double GraphRenderer::TimeStat::getDouble(StatType T) const {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000360 double retval = 0;
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000361 double TimeStat::*DoubleStatPtrs[] = {&TimeStat::Min, &TimeStat::Median,
362 &TimeStat::Pct90, &TimeStat::Pct99,
363 &TimeStat::Max, &TimeStat::Sum};
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000364 switch (T) {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000365 case GraphRenderer::StatType::NONE:
366 retval = 0.0;
367 break;
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000368 case GraphRenderer::StatType::COUNT:
369 retval = static_cast<double>(Count);
370 break;
371 default:
372 retval =
373 (*this).*DoubleStatPtrs[static_cast<int>(T) -
374 static_cast<int>(GraphRenderer::StatType::MIN)];
375 break;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000376 }
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000377 return retval;
David Blaikie87299ad2017-01-16 20:36:26 +0000378}
379
380// Outputs a DOT format version of the Graph embedded in the GraphRenderer
381// object on OS. It does this in the expected way by itterating
382// through all edges then vertices and then outputting them and their
383// annotations.
384//
385// FIXME: output more information, better presented.
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000386void GraphRenderer::exportGraphAsDOT(raw_ostream &OS, StatType ET, StatType EC,
387 StatType VT, StatType VC) {
David Blaikie87299ad2017-01-16 20:36:26 +0000388 OS << "digraph xray {\n";
389
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000390 if (VT != StatType::NONE)
391 OS << "node [shape=record];\n";
392
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000393 for (const auto &E : G.edges()) {
394 const auto &S = E.second.S;
395 OS << "F" << E.first.first << " -> "
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000396 << "F" << E.first.second << " [label=\"" << S.getString(ET) << "\"";
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000397 if (EC != StatType::NONE)
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000398 OS << " color=\""
399 << CHelper.getColorString(
400 std::sqrt(S.getDouble(EC) / G.GraphEdgeMax.getDouble(EC)))
Dean Michael Berrisf0cb13d2017-02-25 00:26:42 +0000401 << "\"";
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000402 OS << "];\n";
403 }
David Blaikie87299ad2017-01-16 20:36:26 +0000404
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000405 for (const auto &V : G.vertices()) {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000406 const auto &VA = V.second;
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000407 if (V.first == 0)
408 continue;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000409 OS << "F" << V.first << " [label=\"" << (VT != StatType::NONE ? "{" : "")
410 << (VA.SymbolName.size() > 40 ? VA.SymbolName.substr(0, 40) + "..."
411 : VA.SymbolName);
412 if (VT != StatType::NONE)
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000413 OS << "|" << VA.S.getString(VT) << "}\"";
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000414 else
415 OS << "\"";
416 if (VC != StatType::NONE)
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000417 OS << " color=\""
418 << CHelper.getColorString(
419 std::sqrt(VA.S.getDouble(VC) / G.GraphVertexMax.getDouble(VC)))
Dean Michael Berrisf0cb13d2017-02-25 00:26:42 +0000420 << "\"";
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000421 OS << "];\n";
422 }
David Blaikie87299ad2017-01-16 20:36:26 +0000423 OS << "}\n";
424}
425
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000426Expected<GraphRenderer> GraphRenderer::Factory::getGraphRenderer() {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000427 InstrumentationMap Map;
428 if (!GraphInstrMap.empty()) {
429 auto InstrumentationMapOrError = loadInstrumentationMap(GraphInstrMap);
430 if (!InstrumentationMapOrError)
431 return joinErrors(
432 make_error<StringError>(
433 Twine("Cannot open instrumentation map '") + GraphInstrMap + "'",
434 std::make_error_code(std::errc::invalid_argument)),
435 InstrumentationMapOrError.takeError());
436 Map = std::move(*InstrumentationMapOrError);
437 }
David Blaikie87299ad2017-01-16 20:36:26 +0000438
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000439 const auto &FunctionAddresses = Map.getFunctionAddresses();
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000440
David Blaikie87299ad2017-01-16 20:36:26 +0000441 symbolize::LLVMSymbolizer::Options Opts(
442 symbolize::FunctionNameKind::LinkageName, true, true, false, "");
David Blaikie87299ad2017-01-16 20:36:26 +0000443 symbolize::LLVMSymbolizer Symbolizer(Opts);
David Blaikie87299ad2017-01-16 20:36:26 +0000444 const auto &Header = Trace.getFileHeader();
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000445
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000446 llvm::xray::FuncIdConversionHelper FuncIdHelper(InstrMap, Symbolizer,
447 FunctionAddresses);
448
449 xray::GraphRenderer GR(FuncIdHelper, DeduceSiblingCalls);
David Blaikie87299ad2017-01-16 20:36:26 +0000450 for (const auto &Record : Trace) {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000451 auto E = GR.accountRecord(Record);
452 if (!E)
453 continue;
454
455 for (const auto &ThreadStack : GR.getPerThreadFunctionStack()) {
456 errs() << "Thread ID: " << ThreadStack.first << "\n";
457 auto Level = ThreadStack.second.size();
458 for (const auto &Entry : llvm::reverse(ThreadStack.second))
459 errs() << "#" << Level-- << "\t"
460 << FuncIdHelper.SymbolOrNumber(Entry.FuncId) << '\n';
David Blaikie87299ad2017-01-16 20:36:26 +0000461 }
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000462
463 if (!GraphKeepGoing)
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000464 return joinErrors(make_error<StringError>(
465 "Error encountered generating the call graph.",
Dean Michael Berrisda2673c2017-02-01 00:22:20 +0000466 std::make_error_code(std::errc::invalid_argument)),
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000467 std::move(E));
468
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000469 handleAllErrors(std::move(E),
470 [&](const ErrorInfoBase &E) { E.log(errs()); });
David Blaikie87299ad2017-01-16 20:36:26 +0000471 }
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000472
473 GR.G.GraphEdgeMax = {};
474 GR.G.GraphVertexMax = {};
475 GR.calculateEdgeStatistics();
476 GR.calculateVertexStatistics();
477
478 if (Header.CycleFrequency)
479 GR.normalizeStatistics(Header.CycleFrequency);
480
481 return GR;
482}
483
484// Here we register and implement the llvm-xray graph subcommand.
485// The bulk of this code reads in the options, opens the required files, uses
486// those files to create a context for analysing the xray trace, then there is a
487// short loop which actually analyses the trace, generates the graph and then
488// outputs it as a DOT.
489//
490// FIXME: include additional filtering and annalysis passes to provide more
491// specific useful information.
492static CommandRegistration Unused(&GraphC, []() -> Error {
493 GraphRenderer::Factory F;
494
495 F.KeepGoing = GraphKeepGoing;
496 F.DeduceSiblingCalls = GraphDeduceSiblingCalls;
497 F.InstrMap = GraphInstrMap;
498
499 auto TraceOrErr = loadTraceFile(GraphInput, true);
500
501 if (!TraceOrErr)
502 return make_error<StringError>(
503 Twine("Failed loading input file '") + GraphInput + "'",
504 make_error_code(llvm::errc::invalid_argument));
505
506 F.Trace = std::move(*TraceOrErr);
507 auto GROrError = F.getGraphRenderer();
508 if (!GROrError)
509 return GROrError.takeError();
510 auto &GR = *GROrError;
511
512 std::error_code EC;
513 raw_fd_ostream OS(GraphOutput, EC, sys::fs::OpenFlags::F_Text);
514 if (EC)
515 return make_error<StringError>(
516 Twine("Cannot open file '") + GraphOutput + "' for writing.", EC);
517
518 GR.exportGraphAsDOT(OS, GraphEdgeLabel, GraphEdgeColorType, GraphVertexLabel,
519 GraphVertexColorType);
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000520 return Error::success();
David Blaikie87299ad2017-01-16 20:36:26 +0000521});