blob: feb676331f8974e252bd9b878b3ed2dc6e89d932 [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//===----------------------------------------------------------------------===//
David Blaikie87299ad2017-01-16 20:36:26 +000014
David Blaikie87299ad2017-01-16 20:36:26 +000015#include "xray-graph.h"
16#include "xray-registry.h"
17#include "llvm/Support/ErrorHandling.h"
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000018#include "llvm/XRay/InstrumentationMap.h"
David Blaikie87299ad2017-01-16 20:36:26 +000019#include "llvm/XRay/Trace.h"
David Blaikie87299ad2017-01-16 20:36:26 +000020
21using namespace llvm;
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000022using namespace llvm::xray;
David Blaikie87299ad2017-01-16 20:36:26 +000023
24// Setup llvm-xray graph subcommand and its options.
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000025static cl::SubCommand GraphC("graph", "Generate function-call graph");
David Blaikie87299ad2017-01-16 20:36:26 +000026static cl::opt<std::string> GraphInput(cl::Positional,
27 cl::desc("<xray log file>"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000028 cl::Required, cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000029
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000030static cl::opt<bool>
31 GraphKeepGoing("keep-going", cl::desc("Keep going on errors encountered"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000032 cl::sub(GraphC), cl::init(false));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000033static cl::alias GraphKeepGoing2("k", cl::aliasopt(GraphKeepGoing),
34 cl::desc("Alias for -keep-going"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000035 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000036
David Blaikie87299ad2017-01-16 20:36:26 +000037static cl::opt<std::string>
38 GraphOutput("output", cl::value_desc("Output file"), cl::init("-"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000039 cl::desc("output file; use '-' for stdout"), cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000040static cl::alias GraphOutput2("o", cl::aliasopt(GraphOutput),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000041 cl::desc("Alias for -output"), cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000042
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000043static cl::opt<std::string>
44 GraphInstrMap("instr_map",
45 cl::desc("binary with the instrumrntation map, or "
46 "a separate instrumentation map"),
47 cl::value_desc("binary with xray_instr_map"), cl::sub(GraphC),
48 cl::init(""));
David Blaikie87299ad2017-01-16 20:36:26 +000049static cl::alias GraphInstrMap2("m", cl::aliasopt(GraphInstrMap),
50 cl::desc("alias for -instr_map"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000051 cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000052
David Blaikie87299ad2017-01-16 20:36:26 +000053static cl::opt<bool> GraphDeduceSiblingCalls(
54 "deduce-sibling-calls",
55 cl::desc("Deduce sibling calls when unrolling function call stacks"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000056 cl::sub(GraphC), cl::init(false));
David Blaikie87299ad2017-01-16 20:36:26 +000057static cl::alias
58 GraphDeduceSiblingCalls2("d", cl::aliasopt(GraphDeduceSiblingCalls),
59 cl::desc("Alias for -deduce-sibling-calls"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000060 cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000061
62static cl::opt<GraphRenderer::StatType>
63 GraphEdgeLabel("edge-label",
64 cl::desc("Output graphs with edges labeled with this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000065 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000066 cl::init(GraphRenderer::StatType::NONE),
67 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
68 "Do not label Edges"),
69 clEnumValN(GraphRenderer::StatType::COUNT,
David Blaikie87299ad2017-01-16 20:36:26 +000070 "count", "function call counts"),
71 clEnumValN(GraphRenderer::StatType::MIN, "min",
72 "minimum function durations"),
73 clEnumValN(GraphRenderer::StatType::MED, "med",
74 "median function durations"),
75 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
76 "90th percentile durations"),
77 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
78 "99th percentile durations"),
79 clEnumValN(GraphRenderer::StatType::MAX, "max",
80 "maximum function durations"),
81 clEnumValN(GraphRenderer::StatType::SUM, "sum",
82 "sum of call durations")));
83static cl::alias GraphEdgeLabel2("e", cl::aliasopt(GraphEdgeLabel),
84 cl::desc("Alias for -edge-label"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000085 cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000086
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000087static cl::opt<GraphRenderer::StatType> GraphVertexLabel(
88 "vertex-label",
89 cl::desc("Output graphs with vertices labeled with this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000090 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000091 cl::init(GraphRenderer::StatType::NONE),
92 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
Dean Michael Berrisca780b52017-04-24 05:54:33 +000093 "Do not label Vertices"),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000094 clEnumValN(GraphRenderer::StatType::COUNT, "count",
95 "function call counts"),
96 clEnumValN(GraphRenderer::StatType::MIN, "min",
97 "minimum function durations"),
98 clEnumValN(GraphRenderer::StatType::MED, "med",
99 "median function durations"),
100 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
101 "90th percentile durations"),
102 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
103 "99th percentile durations"),
104 clEnumValN(GraphRenderer::StatType::MAX, "max",
105 "maximum function durations"),
106 clEnumValN(GraphRenderer::StatType::SUM, "sum",
107 "sum of call durations")));
108static cl::alias GraphVertexLabel2("v", cl::aliasopt(GraphVertexLabel),
109 cl::desc("Alias for -edge-label"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000110 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000111
112static cl::opt<GraphRenderer::StatType> GraphEdgeColorType(
113 "color-edges",
114 cl::desc("Output graphs with edge colors determined by this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000115 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000116 cl::init(GraphRenderer::StatType::NONE),
117 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000118 "Do not color Edges"),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000119 clEnumValN(GraphRenderer::StatType::COUNT, "count",
120 "function call counts"),
121 clEnumValN(GraphRenderer::StatType::MIN, "min",
122 "minimum function durations"),
123 clEnumValN(GraphRenderer::StatType::MED, "med",
124 "median function durations"),
125 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
126 "90th percentile durations"),
127 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
128 "99th percentile durations"),
129 clEnumValN(GraphRenderer::StatType::MAX, "max",
130 "maximum function durations"),
131 clEnumValN(GraphRenderer::StatType::SUM, "sum",
132 "sum of call durations")));
133static cl::alias GraphEdgeColorType2("c", cl::aliasopt(GraphEdgeColorType),
134 cl::desc("Alias for -color-edges"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000135 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000136
137static cl::opt<GraphRenderer::StatType> GraphVertexColorType(
138 "color-vertices",
139 cl::desc("Output graphs with vertex colors determined by this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000140 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000141 cl::init(GraphRenderer::StatType::NONE),
142 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000143 "Do not color vertices"),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000144 clEnumValN(GraphRenderer::StatType::COUNT, "count",
145 "function call counts"),
146 clEnumValN(GraphRenderer::StatType::MIN, "min",
147 "minimum function durations"),
148 clEnumValN(GraphRenderer::StatType::MED, "med",
149 "median function durations"),
150 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
151 "90th percentile durations"),
152 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
153 "99th percentile durations"),
154 clEnumValN(GraphRenderer::StatType::MAX, "max",
155 "maximum function durations"),
156 clEnumValN(GraphRenderer::StatType::SUM, "sum",
157 "sum of call durations")));
158static cl::alias GraphVertexColorType2("b", cl::aliasopt(GraphVertexColorType),
159 cl::desc("Alias for -edge-label"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000160 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000161
David Blaikie87299ad2017-01-16 20:36:26 +0000162template <class T> T diff(T L, T R) { return std::max(L, R) - std::min(L, R); }
163
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000164// Updates the statistics for a GraphRenderer::TimeStat
165static void updateStat(GraphRenderer::TimeStat &S, int64_t L) {
David Blaikie87299ad2017-01-16 20:36:26 +0000166 S.Count++;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000167 if (S.Min > L || S.Min == 0)
168 S.Min = L;
169 if (S.Max < L)
170 S.Max = L;
171 S.Sum += L;
David Blaikie87299ad2017-01-16 20:36:26 +0000172}
173
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000174// Evaluates an XRay record and performs accounting on it.
David Blaikie87299ad2017-01-16 20:36:26 +0000175//
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000176// If the record is an ENTER record it pushes the FuncID and TSC onto a
177// structure representing the call stack for that function.
178// If the record is an EXIT record it checks computes computes the ammount of
179// time the function took to complete and then stores that information in an
180// edge of the graph. If there is no matching ENTER record the function tries
181// to recover by assuming that there were EXIT records which were missed, for
182// example caused by tail call elimination and if the option is enabled then
183// then tries to recover from this.
184//
185// This funciton will also error if the records are out of order, as the trace
186// is expected to be sorted.
187//
188// The graph generated has an immaginary root for functions called by no-one at
David Blaikie87299ad2017-01-16 20:36:26 +0000189// FuncId 0.
190//
David Blaikie87299ad2017-01-16 20:36:26 +0000191// FIXME: Refactor this and account subcommand to reduce code duplication.
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000192Error GraphRenderer::accountRecord(const XRayRecord &Record) {
193 using std::make_error_code;
194 using std::errc;
David Blaikie87299ad2017-01-16 20:36:26 +0000195 if (CurrentMaxTSC == 0)
196 CurrentMaxTSC = Record.TSC;
197
198 if (Record.TSC < CurrentMaxTSC)
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000199 return make_error<StringError>("Records not in order",
200 make_error_code(errc::invalid_argument));
David Blaikie87299ad2017-01-16 20:36:26 +0000201
202 auto &ThreadStack = PerThreadFunctionStack[Record.TId];
203 switch (Record.Type) {
Martin Pelikan10c873f2017-09-27 04:48:03 +0000204 case RecordTypes::ENTER:
205 case RecordTypes::ENTER_ARG: {
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000206 if (Record.FuncId != 0 && G.count(Record.FuncId) == 0)
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000207 G[Record.FuncId].SymbolName = FuncIdHelper.SymbolOrNumber(Record.FuncId);
David Blaikie87299ad2017-01-16 20:36:26 +0000208 ThreadStack.push_back({Record.FuncId, Record.TSC});
209 break;
210 }
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000211 case RecordTypes::EXIT:
212 case RecordTypes::TAIL_EXIT: {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000213 // FIXME: Refactor this and the account subcommand to reduce code
David Blaikie87299ad2017-01-16 20:36:26 +0000214 // duplication
215 if (ThreadStack.size() == 0 || ThreadStack.back().FuncId != Record.FuncId) {
216 if (!DeduceSiblingCalls)
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000217 return make_error<StringError>("No matching ENTRY record",
218 make_error_code(errc::invalid_argument));
David Blaikie87299ad2017-01-16 20:36:26 +0000219 auto Parent = std::find_if(
220 ThreadStack.rbegin(), ThreadStack.rend(),
221 [&](const FunctionAttr &A) { return A.FuncId == Record.FuncId; });
222 if (Parent == ThreadStack.rend())
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000223 return make_error<StringError>(
224 "No matching Entry record in stack",
225 make_error_code(errc::invalid_argument)); // There is no matching
226 // Function for this exit.
David Blaikie87299ad2017-01-16 20:36:26 +0000227 while (ThreadStack.back().FuncId != Record.FuncId) {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000228 TimestampT D = diff(ThreadStack.back().TSC, Record.TSC);
229 VertexIdentifier TopFuncId = ThreadStack.back().FuncId;
David Blaikie87299ad2017-01-16 20:36:26 +0000230 ThreadStack.pop_back();
231 assert(ThreadStack.size() != 0);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000232 EdgeIdentifier EI(ThreadStack.back().FuncId, TopFuncId);
233 auto &EA = G[EI];
David Blaikie87299ad2017-01-16 20:36:26 +0000234 EA.Timings.push_back(D);
235 updateStat(EA.S, D);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000236 updateStat(G[TopFuncId].S, D);
David Blaikie87299ad2017-01-16 20:36:26 +0000237 }
238 }
239 uint64_t D = diff(ThreadStack.back().TSC, Record.TSC);
240 ThreadStack.pop_back();
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000241 VertexIdentifier VI = ThreadStack.empty() ? 0 : ThreadStack.back().FuncId;
242 EdgeIdentifier EI(VI, Record.FuncId);
243 auto &EA = G[EI];
David Blaikie87299ad2017-01-16 20:36:26 +0000244 EA.Timings.push_back(D);
245 updateStat(EA.S, D);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000246 updateStat(G[Record.FuncId].S, D);
David Blaikie87299ad2017-01-16 20:36:26 +0000247 break;
248 }
249 }
250
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000251 return Error::success();
David Blaikie87299ad2017-01-16 20:36:26 +0000252}
253
254template <typename U>
255void GraphRenderer::getStats(U begin, U end, GraphRenderer::TimeStat &S) {
Dean Michael Berris56854682017-03-31 01:56:45 +0000256 if (begin == end) return;
David Blaikie87299ad2017-01-16 20:36:26 +0000257 std::ptrdiff_t MedianOff = S.Count / 2;
258 std::nth_element(begin, begin + MedianOff, end);
259 S.Median = *(begin + MedianOff);
260 std::ptrdiff_t Pct90Off = (S.Count * 9) / 10;
261 std::nth_element(begin, begin + Pct90Off, end);
262 S.Pct90 = *(begin + Pct90Off);
263 std::ptrdiff_t Pct99Off = (S.Count * 99) / 100;
264 std::nth_element(begin, begin + Pct99Off, end);
265 S.Pct99 = *(begin + Pct99Off);
266}
267
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000268void GraphRenderer::updateMaxStats(const GraphRenderer::TimeStat &S,
269 GraphRenderer::TimeStat &M) {
270 M.Count = std::max(M.Count, S.Count);
271 M.Min = std::max(M.Min, S.Min);
272 M.Median = std::max(M.Median, S.Median);
273 M.Pct90 = std::max(M.Pct90, S.Pct90);
274 M.Pct99 = std::max(M.Pct99, S.Pct99);
275 M.Max = std::max(M.Max, S.Max);
276 M.Sum = std::max(M.Sum, S.Sum);
277}
278
David Blaikie87299ad2017-01-16 20:36:26 +0000279void GraphRenderer::calculateEdgeStatistics() {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000280 assert(!G.edges().empty());
281 for (auto &E : G.edges()) {
282 auto &A = E.second;
283 assert(!A.Timings.empty());
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000284 getStats(A.Timings.begin(), A.Timings.end(), A.S);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000285 updateMaxStats(A.S, G.GraphEdgeMax);
David Blaikie87299ad2017-01-16 20:36:26 +0000286 }
287}
288
289void GraphRenderer::calculateVertexStatistics() {
Dean Michael Berris79f57462017-02-10 06:05:46 +0000290 std::vector<uint64_t> TempTimings;
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000291 for (auto &V : G.vertices()) {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000292 if (V.first != 0) {
293 for (auto &E : G.inEdges(V.first)) {
294 auto &A = E.second;
295 TempTimings.insert(TempTimings.end(), A.Timings.begin(),
296 A.Timings.end());
297 }
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000298 getStats(TempTimings.begin(), TempTimings.end(), G[V.first].S);
299 updateMaxStats(G[V.first].S, G.GraphVertexMax);
300 TempTimings.clear();
Dean Michael Berris79f57462017-02-10 06:05:46 +0000301 }
Dean Michael Berris79f57462017-02-10 06:05:46 +0000302 }
David Blaikie87299ad2017-01-16 20:36:26 +0000303}
304
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000305// A Helper function for normalizeStatistics which normalises a single
306// TimeStat element.
307static void normalizeTimeStat(GraphRenderer::TimeStat &S,
308 double CycleFrequency) {
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000309 int64_t OldCount = S.Count;
310 S = S / CycleFrequency;
311 S.Count = OldCount;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000312}
313
314// Normalises the statistics in the graph for a given TSC frequency.
315void GraphRenderer::normalizeStatistics(double CycleFrequency) {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000316 for (auto &E : G.edges()) {
317 auto &S = E.second.S;
318 normalizeTimeStat(S, CycleFrequency);
David Blaikie87299ad2017-01-16 20:36:26 +0000319 }
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000320 for (auto &V : G.vertices()) {
David Blaikie87299ad2017-01-16 20:36:26 +0000321 auto &S = V.second.S;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000322 normalizeTimeStat(S, CycleFrequency);
David Blaikie87299ad2017-01-16 20:36:26 +0000323 }
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000324
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000325 normalizeTimeStat(G.GraphEdgeMax, CycleFrequency);
326 normalizeTimeStat(G.GraphVertexMax, CycleFrequency);
David Blaikie87299ad2017-01-16 20:36:26 +0000327}
328
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000329// Returns a string containing the value of statistic field T
330std::string
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000331GraphRenderer::TimeStat::getString(GraphRenderer::StatType T) const {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000332 std::string St;
333 raw_string_ostream S{St};
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000334 double TimeStat::*DoubleStatPtrs[] = {&TimeStat::Min, &TimeStat::Median,
335 &TimeStat::Pct90, &TimeStat::Pct99,
336 &TimeStat::Max, &TimeStat::Sum};
David Blaikie87299ad2017-01-16 20:36:26 +0000337 switch (T) {
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000338 case GraphRenderer::StatType::NONE:
339 break;
David Blaikie87299ad2017-01-16 20:36:26 +0000340 case GraphRenderer::StatType::COUNT:
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000341 S << Count;
David Blaikie87299ad2017-01-16 20:36:26 +0000342 break;
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000343 default:
344 S << (*this).*
345 DoubleStatPtrs[static_cast<int>(T) -
346 static_cast<int>(GraphRenderer::StatType::MIN)];
David Blaikie87299ad2017-01-16 20:36:26 +0000347 break;
348 }
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000349 return S.str();
David Blaikie87299ad2017-01-16 20:36:26 +0000350}
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000351
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000352// Returns the quotient between the property T of this and another TimeStat as
353// a double
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000354double GraphRenderer::TimeStat::getDouble(StatType T) const {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000355 double retval = 0;
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000356 double TimeStat::*DoubleStatPtrs[] = {&TimeStat::Min, &TimeStat::Median,
357 &TimeStat::Pct90, &TimeStat::Pct99,
358 &TimeStat::Max, &TimeStat::Sum};
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000359 switch (T) {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000360 case GraphRenderer::StatType::NONE:
361 retval = 0.0;
362 break;
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000363 case GraphRenderer::StatType::COUNT:
364 retval = static_cast<double>(Count);
365 break;
366 default:
367 retval =
368 (*this).*DoubleStatPtrs[static_cast<int>(T) -
369 static_cast<int>(GraphRenderer::StatType::MIN)];
370 break;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000371 }
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000372 return retval;
David Blaikie87299ad2017-01-16 20:36:26 +0000373}
374
375// Outputs a DOT format version of the Graph embedded in the GraphRenderer
376// object on OS. It does this in the expected way by itterating
377// through all edges then vertices and then outputting them and their
378// annotations.
379//
380// FIXME: output more information, better presented.
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000381void GraphRenderer::exportGraphAsDOT(raw_ostream &OS, StatType ET, StatType EC,
382 StatType VT, StatType VC) {
David Blaikie87299ad2017-01-16 20:36:26 +0000383 OS << "digraph xray {\n";
384
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000385 if (VT != StatType::NONE)
386 OS << "node [shape=record];\n";
387
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000388 for (const auto &E : G.edges()) {
389 const auto &S = E.second.S;
390 OS << "F" << E.first.first << " -> "
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000391 << "F" << E.first.second << " [label=\"" << S.getString(ET) << "\"";
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000392 if (EC != StatType::NONE)
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000393 OS << " color=\""
394 << CHelper.getColorString(
395 std::sqrt(S.getDouble(EC) / G.GraphEdgeMax.getDouble(EC)))
Dean Michael Berrisf0cb13d2017-02-25 00:26:42 +0000396 << "\"";
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000397 OS << "];\n";
398 }
David Blaikie87299ad2017-01-16 20:36:26 +0000399
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000400 for (const auto &V : G.vertices()) {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000401 const auto &VA = V.second;
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000402 if (V.first == 0)
403 continue;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000404 OS << "F" << V.first << " [label=\"" << (VT != StatType::NONE ? "{" : "")
405 << (VA.SymbolName.size() > 40 ? VA.SymbolName.substr(0, 40) + "..."
406 : VA.SymbolName);
407 if (VT != StatType::NONE)
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000408 OS << "|" << VA.S.getString(VT) << "}\"";
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000409 else
410 OS << "\"";
411 if (VC != StatType::NONE)
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000412 OS << " color=\""
413 << CHelper.getColorString(
414 std::sqrt(VA.S.getDouble(VC) / G.GraphVertexMax.getDouble(VC)))
Dean Michael Berrisf0cb13d2017-02-25 00:26:42 +0000415 << "\"";
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000416 OS << "];\n";
417 }
David Blaikie87299ad2017-01-16 20:36:26 +0000418 OS << "}\n";
419}
420
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000421Expected<GraphRenderer> GraphRenderer::Factory::getGraphRenderer() {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000422 InstrumentationMap Map;
423 if (!GraphInstrMap.empty()) {
424 auto InstrumentationMapOrError = loadInstrumentationMap(GraphInstrMap);
425 if (!InstrumentationMapOrError)
426 return joinErrors(
427 make_error<StringError>(
428 Twine("Cannot open instrumentation map '") + GraphInstrMap + "'",
429 std::make_error_code(std::errc::invalid_argument)),
430 InstrumentationMapOrError.takeError());
431 Map = std::move(*InstrumentationMapOrError);
432 }
David Blaikie87299ad2017-01-16 20:36:26 +0000433
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000434 const auto &FunctionAddresses = Map.getFunctionAddresses();
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000435
David Blaikie87299ad2017-01-16 20:36:26 +0000436 symbolize::LLVMSymbolizer::Options Opts(
437 symbolize::FunctionNameKind::LinkageName, true, true, false, "");
David Blaikie87299ad2017-01-16 20:36:26 +0000438 symbolize::LLVMSymbolizer Symbolizer(Opts);
David Blaikie87299ad2017-01-16 20:36:26 +0000439 const auto &Header = Trace.getFileHeader();
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000440
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000441 llvm::xray::FuncIdConversionHelper FuncIdHelper(InstrMap, Symbolizer,
442 FunctionAddresses);
443
444 xray::GraphRenderer GR(FuncIdHelper, DeduceSiblingCalls);
David Blaikie87299ad2017-01-16 20:36:26 +0000445 for (const auto &Record : Trace) {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000446 auto E = GR.accountRecord(Record);
447 if (!E)
448 continue;
449
450 for (const auto &ThreadStack : GR.getPerThreadFunctionStack()) {
451 errs() << "Thread ID: " << ThreadStack.first << "\n";
452 auto Level = ThreadStack.second.size();
453 for (const auto &Entry : llvm::reverse(ThreadStack.second))
454 errs() << "#" << Level-- << "\t"
455 << FuncIdHelper.SymbolOrNumber(Entry.FuncId) << '\n';
David Blaikie87299ad2017-01-16 20:36:26 +0000456 }
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000457
458 if (!GraphKeepGoing)
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000459 return joinErrors(make_error<StringError>(
460 "Error encountered generating the call graph.",
Dean Michael Berrisda2673c2017-02-01 00:22:20 +0000461 std::make_error_code(std::errc::invalid_argument)),
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000462 std::move(E));
463
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000464 handleAllErrors(std::move(E),
465 [&](const ErrorInfoBase &E) { E.log(errs()); });
David Blaikie87299ad2017-01-16 20:36:26 +0000466 }
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000467
468 GR.G.GraphEdgeMax = {};
469 GR.G.GraphVertexMax = {};
470 GR.calculateEdgeStatistics();
471 GR.calculateVertexStatistics();
472
473 if (Header.CycleFrequency)
474 GR.normalizeStatistics(Header.CycleFrequency);
475
476 return GR;
477}
478
479// Here we register and implement the llvm-xray graph subcommand.
480// The bulk of this code reads in the options, opens the required files, uses
481// those files to create a context for analysing the xray trace, then there is a
482// short loop which actually analyses the trace, generates the graph and then
483// outputs it as a DOT.
484//
485// FIXME: include additional filtering and annalysis passes to provide more
486// specific useful information.
487static CommandRegistration Unused(&GraphC, []() -> Error {
488 GraphRenderer::Factory F;
489
490 F.KeepGoing = GraphKeepGoing;
491 F.DeduceSiblingCalls = GraphDeduceSiblingCalls;
492 F.InstrMap = GraphInstrMap;
493
494 auto TraceOrErr = loadTraceFile(GraphInput, true);
495
496 if (!TraceOrErr)
497 return make_error<StringError>(
498 Twine("Failed loading input file '") + GraphInput + "'",
499 make_error_code(llvm::errc::invalid_argument));
500
501 F.Trace = std::move(*TraceOrErr);
502 auto GROrError = F.getGraphRenderer();
503 if (!GROrError)
504 return GROrError.takeError();
505 auto &GR = *GROrError;
506
507 std::error_code EC;
508 raw_fd_ostream OS(GraphOutput, EC, sys::fs::OpenFlags::F_Text);
509 if (EC)
510 return make_error<StringError>(
511 Twine("Cannot open file '") + GraphOutput + "' for writing.", EC);
512
513 GR.exportGraphAsDOT(OS, GraphEdgeLabel, GraphEdgeColorType, GraphVertexLabel,
514 GraphVertexColorType);
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000515 return Error::success();
David Blaikie87299ad2017-01-16 20:36:26 +0000516});