blob: 2365ce56f921946f17bb5646738feea8baaf066e [file] [log] [blame]
Dean Michael Berrisa0e3ae42018-05-02 00:43:17 +00001//===-- xray-graph.cpp: XRay Function Call Graph Renderer -----------------===//
David Blaikie87299ad2017-01-16 20:36:26 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
David Blaikie87299ad2017-01-16 20:36:26 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Generate a DOT file to represent the function call graph encountered in
10// the trace.
11//
12//===----------------------------------------------------------------------===//
David Blaikie87299ad2017-01-16 20:36:26 +000013
David Blaikie87299ad2017-01-16 20:36:26 +000014#include "xray-graph.h"
15#include "xray-registry.h"
16#include "llvm/Support/ErrorHandling.h"
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000017#include "llvm/XRay/InstrumentationMap.h"
David Blaikie87299ad2017-01-16 20:36:26 +000018#include "llvm/XRay/Trace.h"
David Blaikie87299ad2017-01-16 20:36:26 +000019
20using namespace llvm;
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000021using namespace llvm::xray;
David Blaikie87299ad2017-01-16 20:36:26 +000022
23// Setup llvm-xray graph subcommand and its options.
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000024static cl::SubCommand GraphC("graph", "Generate function-call graph");
David Blaikie87299ad2017-01-16 20:36:26 +000025static cl::opt<std::string> GraphInput(cl::Positional,
26 cl::desc("<xray log file>"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000027 cl::Required, cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000028
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000029static cl::opt<bool>
30 GraphKeepGoing("keep-going", cl::desc("Keep going on errors encountered"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000031 cl::sub(GraphC), cl::init(false));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000032static cl::alias GraphKeepGoing2("k", cl::aliasopt(GraphKeepGoing),
33 cl::desc("Alias for -keep-going"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000034 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000035
David Blaikie87299ad2017-01-16 20:36:26 +000036static cl::opt<std::string>
37 GraphOutput("output", cl::value_desc("Output file"), cl::init("-"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000038 cl::desc("output file; use '-' for stdout"), cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000039static cl::alias GraphOutput2("o", cl::aliasopt(GraphOutput),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000040 cl::desc("Alias for -output"), cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000041
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000042static cl::opt<std::string>
43 GraphInstrMap("instr_map",
44 cl::desc("binary with the instrumrntation map, or "
45 "a separate instrumentation map"),
46 cl::value_desc("binary with xray_instr_map"), cl::sub(GraphC),
47 cl::init(""));
David Blaikie87299ad2017-01-16 20:36:26 +000048static cl::alias GraphInstrMap2("m", cl::aliasopt(GraphInstrMap),
49 cl::desc("alias for -instr_map"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000050 cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000051
David Blaikie87299ad2017-01-16 20:36:26 +000052static cl::opt<bool> GraphDeduceSiblingCalls(
53 "deduce-sibling-calls",
54 cl::desc("Deduce sibling calls when unrolling function call stacks"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000055 cl::sub(GraphC), cl::init(false));
David Blaikie87299ad2017-01-16 20:36:26 +000056static cl::alias
57 GraphDeduceSiblingCalls2("d", cl::aliasopt(GraphDeduceSiblingCalls),
58 cl::desc("Alias for -deduce-sibling-calls"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000059 cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000060
61static cl::opt<GraphRenderer::StatType>
62 GraphEdgeLabel("edge-label",
63 cl::desc("Output graphs with edges labeled with this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000064 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000065 cl::init(GraphRenderer::StatType::NONE),
66 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
67 "Do not label Edges"),
68 clEnumValN(GraphRenderer::StatType::COUNT,
David Blaikie87299ad2017-01-16 20:36:26 +000069 "count", "function call counts"),
70 clEnumValN(GraphRenderer::StatType::MIN, "min",
71 "minimum function durations"),
72 clEnumValN(GraphRenderer::StatType::MED, "med",
73 "median function durations"),
74 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
75 "90th percentile durations"),
76 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
77 "99th percentile durations"),
78 clEnumValN(GraphRenderer::StatType::MAX, "max",
79 "maximum function durations"),
80 clEnumValN(GraphRenderer::StatType::SUM, "sum",
81 "sum of call durations")));
82static cl::alias GraphEdgeLabel2("e", cl::aliasopt(GraphEdgeLabel),
83 cl::desc("Alias for -edge-label"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000084 cl::sub(GraphC));
David Blaikie87299ad2017-01-16 20:36:26 +000085
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000086static cl::opt<GraphRenderer::StatType> GraphVertexLabel(
87 "vertex-label",
88 cl::desc("Output graphs with vertices labeled with this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +000089 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000090 cl::init(GraphRenderer::StatType::NONE),
91 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
Dean Michael Berrisca780b52017-04-24 05:54:33 +000092 "Do not label Vertices"),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +000093 clEnumValN(GraphRenderer::StatType::COUNT, "count",
94 "function call counts"),
95 clEnumValN(GraphRenderer::StatType::MIN, "min",
96 "minimum function durations"),
97 clEnumValN(GraphRenderer::StatType::MED, "med",
98 "median function durations"),
99 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
100 "90th percentile durations"),
101 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
102 "99th percentile durations"),
103 clEnumValN(GraphRenderer::StatType::MAX, "max",
104 "maximum function durations"),
105 clEnumValN(GraphRenderer::StatType::SUM, "sum",
106 "sum of call durations")));
107static cl::alias GraphVertexLabel2("v", cl::aliasopt(GraphVertexLabel),
108 cl::desc("Alias for -edge-label"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000109 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000110
111static cl::opt<GraphRenderer::StatType> GraphEdgeColorType(
112 "color-edges",
113 cl::desc("Output graphs with edge colors determined by this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000114 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000115 cl::init(GraphRenderer::StatType::NONE),
116 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000117 "Do not color Edges"),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000118 clEnumValN(GraphRenderer::StatType::COUNT, "count",
119 "function call counts"),
120 clEnumValN(GraphRenderer::StatType::MIN, "min",
121 "minimum function durations"),
122 clEnumValN(GraphRenderer::StatType::MED, "med",
123 "median function durations"),
124 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
125 "90th percentile durations"),
126 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
127 "99th percentile durations"),
128 clEnumValN(GraphRenderer::StatType::MAX, "max",
129 "maximum function durations"),
130 clEnumValN(GraphRenderer::StatType::SUM, "sum",
131 "sum of call durations")));
132static cl::alias GraphEdgeColorType2("c", cl::aliasopt(GraphEdgeColorType),
133 cl::desc("Alias for -color-edges"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000134 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000135
136static cl::opt<GraphRenderer::StatType> GraphVertexColorType(
137 "color-vertices",
138 cl::desc("Output graphs with vertex colors determined by this field"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000139 cl::value_desc("field"), cl::sub(GraphC),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000140 cl::init(GraphRenderer::StatType::NONE),
141 cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000142 "Do not color vertices"),
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000143 clEnumValN(GraphRenderer::StatType::COUNT, "count",
144 "function call counts"),
145 clEnumValN(GraphRenderer::StatType::MIN, "min",
146 "minimum function durations"),
147 clEnumValN(GraphRenderer::StatType::MED, "med",
148 "median function durations"),
149 clEnumValN(GraphRenderer::StatType::PCT90, "90p",
150 "90th percentile durations"),
151 clEnumValN(GraphRenderer::StatType::PCT99, "99p",
152 "99th percentile durations"),
153 clEnumValN(GraphRenderer::StatType::MAX, "max",
154 "maximum function durations"),
155 clEnumValN(GraphRenderer::StatType::SUM, "sum",
156 "sum of call durations")));
157static cl::alias GraphVertexColorType2("b", cl::aliasopt(GraphVertexColorType),
158 cl::desc("Alias for -edge-label"),
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000159 cl::sub(GraphC));
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000160
David Blaikie87299ad2017-01-16 20:36:26 +0000161template <class T> T diff(T L, T R) { return std::max(L, R) - std::min(L, R); }
162
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000163// Updates the statistics for a GraphRenderer::TimeStat
164static void updateStat(GraphRenderer::TimeStat &S, int64_t L) {
David Blaikie87299ad2017-01-16 20:36:26 +0000165 S.Count++;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000166 if (S.Min > L || S.Min == 0)
167 S.Min = L;
168 if (S.Max < L)
169 S.Max = L;
170 S.Sum += L;
David Blaikie87299ad2017-01-16 20:36:26 +0000171}
172
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000173// Evaluates an XRay record and performs accounting on it.
David Blaikie87299ad2017-01-16 20:36:26 +0000174//
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000175// If the record is an ENTER record it pushes the FuncID and TSC onto a
176// structure representing the call stack for that function.
177// If the record is an EXIT record it checks computes computes the ammount of
178// time the function took to complete and then stores that information in an
179// edge of the graph. If there is no matching ENTER record the function tries
180// to recover by assuming that there were EXIT records which were missed, for
181// example caused by tail call elimination and if the option is enabled then
182// then tries to recover from this.
183//
184// This funciton will also error if the records are out of order, as the trace
185// is expected to be sorted.
186//
187// The graph generated has an immaginary root for functions called by no-one at
David Blaikie87299ad2017-01-16 20:36:26 +0000188// FuncId 0.
189//
David Blaikie87299ad2017-01-16 20:36:26 +0000190// FIXME: Refactor this and account subcommand to reduce code duplication.
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000191Error GraphRenderer::accountRecord(const XRayRecord &Record) {
192 using std::make_error_code;
193 using std::errc;
David Blaikie87299ad2017-01-16 20:36:26 +0000194 if (CurrentMaxTSC == 0)
195 CurrentMaxTSC = Record.TSC;
196
197 if (Record.TSC < CurrentMaxTSC)
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000198 return make_error<StringError>("Records not in order",
199 make_error_code(errc::invalid_argument));
David Blaikie87299ad2017-01-16 20:36:26 +0000200
201 auto &ThreadStack = PerThreadFunctionStack[Record.TId];
202 switch (Record.Type) {
Martin Pelikan10c873f2017-09-27 04:48:03 +0000203 case RecordTypes::ENTER:
204 case RecordTypes::ENTER_ARG: {
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000205 if (Record.FuncId != 0 && G.count(Record.FuncId) == 0)
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000206 G[Record.FuncId].SymbolName = FuncIdHelper.SymbolOrNumber(Record.FuncId);
David Blaikie87299ad2017-01-16 20:36:26 +0000207 ThreadStack.push_back({Record.FuncId, Record.TSC});
208 break;
209 }
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000210 case RecordTypes::EXIT:
211 case RecordTypes::TAIL_EXIT: {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000212 // FIXME: Refactor this and the account subcommand to reduce code
David Blaikie87299ad2017-01-16 20:36:26 +0000213 // duplication
214 if (ThreadStack.size() == 0 || ThreadStack.back().FuncId != Record.FuncId) {
215 if (!DeduceSiblingCalls)
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000216 return make_error<StringError>("No matching ENTRY record",
217 make_error_code(errc::invalid_argument));
David Blaikie87299ad2017-01-16 20:36:26 +0000218 auto Parent = std::find_if(
219 ThreadStack.rbegin(), ThreadStack.rend(),
220 [&](const FunctionAttr &A) { return A.FuncId == Record.FuncId; });
221 if (Parent == ThreadStack.rend())
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000222 return make_error<StringError>(
223 "No matching Entry record in stack",
224 make_error_code(errc::invalid_argument)); // There is no matching
225 // Function for this exit.
David Blaikie87299ad2017-01-16 20:36:26 +0000226 while (ThreadStack.back().FuncId != Record.FuncId) {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000227 TimestampT D = diff(ThreadStack.back().TSC, Record.TSC);
228 VertexIdentifier TopFuncId = ThreadStack.back().FuncId;
David Blaikie87299ad2017-01-16 20:36:26 +0000229 ThreadStack.pop_back();
230 assert(ThreadStack.size() != 0);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000231 EdgeIdentifier EI(ThreadStack.back().FuncId, TopFuncId);
232 auto &EA = G[EI];
David Blaikie87299ad2017-01-16 20:36:26 +0000233 EA.Timings.push_back(D);
234 updateStat(EA.S, D);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000235 updateStat(G[TopFuncId].S, D);
David Blaikie87299ad2017-01-16 20:36:26 +0000236 }
237 }
238 uint64_t D = diff(ThreadStack.back().TSC, Record.TSC);
239 ThreadStack.pop_back();
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000240 VertexIdentifier VI = ThreadStack.empty() ? 0 : ThreadStack.back().FuncId;
241 EdgeIdentifier EI(VI, Record.FuncId);
242 auto &EA = G[EI];
David Blaikie87299ad2017-01-16 20:36:26 +0000243 EA.Timings.push_back(D);
244 updateStat(EA.S, D);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000245 updateStat(G[Record.FuncId].S, D);
David Blaikie87299ad2017-01-16 20:36:26 +0000246 break;
247 }
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000248 case RecordTypes::CUSTOM_EVENT:
249 case RecordTypes::TYPED_EVENT:
250 // TODO: Support custom and typed events in the graph processing?
251 break;
David Blaikie87299ad2017-01-16 20:36:26 +0000252 }
253
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000254 return Error::success();
David Blaikie87299ad2017-01-16 20:36:26 +0000255}
256
257template <typename U>
258void GraphRenderer::getStats(U begin, U end, GraphRenderer::TimeStat &S) {
Dean Michael Berris56854682017-03-31 01:56:45 +0000259 if (begin == end) return;
David Blaikie87299ad2017-01-16 20:36:26 +0000260 std::ptrdiff_t MedianOff = S.Count / 2;
261 std::nth_element(begin, begin + MedianOff, end);
262 S.Median = *(begin + MedianOff);
263 std::ptrdiff_t Pct90Off = (S.Count * 9) / 10;
264 std::nth_element(begin, begin + Pct90Off, end);
265 S.Pct90 = *(begin + Pct90Off);
266 std::ptrdiff_t Pct99Off = (S.Count * 99) / 100;
267 std::nth_element(begin, begin + Pct99Off, end);
268 S.Pct99 = *(begin + Pct99Off);
269}
270
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000271void GraphRenderer::updateMaxStats(const GraphRenderer::TimeStat &S,
272 GraphRenderer::TimeStat &M) {
273 M.Count = std::max(M.Count, S.Count);
274 M.Min = std::max(M.Min, S.Min);
275 M.Median = std::max(M.Median, S.Median);
276 M.Pct90 = std::max(M.Pct90, S.Pct90);
277 M.Pct99 = std::max(M.Pct99, S.Pct99);
278 M.Max = std::max(M.Max, S.Max);
279 M.Sum = std::max(M.Sum, S.Sum);
280}
281
David Blaikie87299ad2017-01-16 20:36:26 +0000282void GraphRenderer::calculateEdgeStatistics() {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000283 assert(!G.edges().empty());
284 for (auto &E : G.edges()) {
285 auto &A = E.second;
286 assert(!A.Timings.empty());
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000287 getStats(A.Timings.begin(), A.Timings.end(), A.S);
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000288 updateMaxStats(A.S, G.GraphEdgeMax);
David Blaikie87299ad2017-01-16 20:36:26 +0000289 }
290}
291
292void GraphRenderer::calculateVertexStatistics() {
Dean Michael Berris79f57462017-02-10 06:05:46 +0000293 std::vector<uint64_t> TempTimings;
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000294 for (auto &V : G.vertices()) {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000295 if (V.first != 0) {
296 for (auto &E : G.inEdges(V.first)) {
297 auto &A = E.second;
298 TempTimings.insert(TempTimings.end(), A.Timings.begin(),
299 A.Timings.end());
300 }
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000301 getStats(TempTimings.begin(), TempTimings.end(), G[V.first].S);
302 updateMaxStats(G[V.first].S, G.GraphVertexMax);
303 TempTimings.clear();
Dean Michael Berris79f57462017-02-10 06:05:46 +0000304 }
Dean Michael Berris79f57462017-02-10 06:05:46 +0000305 }
David Blaikie87299ad2017-01-16 20:36:26 +0000306}
307
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000308// A Helper function for normalizeStatistics which normalises a single
309// TimeStat element.
310static void normalizeTimeStat(GraphRenderer::TimeStat &S,
311 double CycleFrequency) {
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000312 int64_t OldCount = S.Count;
313 S = S / CycleFrequency;
314 S.Count = OldCount;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000315}
316
317// Normalises the statistics in the graph for a given TSC frequency.
318void GraphRenderer::normalizeStatistics(double CycleFrequency) {
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000319 for (auto &E : G.edges()) {
320 auto &S = E.second.S;
321 normalizeTimeStat(S, CycleFrequency);
David Blaikie87299ad2017-01-16 20:36:26 +0000322 }
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000323 for (auto &V : G.vertices()) {
David Blaikie87299ad2017-01-16 20:36:26 +0000324 auto &S = V.second.S;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000325 normalizeTimeStat(S, CycleFrequency);
David Blaikie87299ad2017-01-16 20:36:26 +0000326 }
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000327
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000328 normalizeTimeStat(G.GraphEdgeMax, CycleFrequency);
329 normalizeTimeStat(G.GraphVertexMax, CycleFrequency);
David Blaikie87299ad2017-01-16 20:36:26 +0000330}
331
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000332// Returns a string containing the value of statistic field T
333std::string
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000334GraphRenderer::TimeStat::getString(GraphRenderer::StatType T) const {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000335 std::string St;
336 raw_string_ostream S{St};
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000337 double TimeStat::*DoubleStatPtrs[] = {&TimeStat::Min, &TimeStat::Median,
338 &TimeStat::Pct90, &TimeStat::Pct99,
339 &TimeStat::Max, &TimeStat::Sum};
David Blaikie87299ad2017-01-16 20:36:26 +0000340 switch (T) {
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000341 case GraphRenderer::StatType::NONE:
342 break;
David Blaikie87299ad2017-01-16 20:36:26 +0000343 case GraphRenderer::StatType::COUNT:
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000344 S << Count;
David Blaikie87299ad2017-01-16 20:36:26 +0000345 break;
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000346 default:
347 S << (*this).*
348 DoubleStatPtrs[static_cast<int>(T) -
349 static_cast<int>(GraphRenderer::StatType::MIN)];
David Blaikie87299ad2017-01-16 20:36:26 +0000350 break;
351 }
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000352 return S.str();
David Blaikie87299ad2017-01-16 20:36:26 +0000353}
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000354
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000355// Returns the quotient between the property T of this and another TimeStat as
356// a double
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000357double GraphRenderer::TimeStat::getDouble(StatType T) const {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000358 double retval = 0;
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000359 double TimeStat::*DoubleStatPtrs[] = {&TimeStat::Min, &TimeStat::Median,
360 &TimeStat::Pct90, &TimeStat::Pct99,
361 &TimeStat::Max, &TimeStat::Sum};
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000362 switch (T) {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000363 case GraphRenderer::StatType::NONE:
364 retval = 0.0;
365 break;
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000366 case GraphRenderer::StatType::COUNT:
367 retval = static_cast<double>(Count);
368 break;
369 default:
370 retval =
371 (*this).*DoubleStatPtrs[static_cast<int>(T) -
372 static_cast<int>(GraphRenderer::StatType::MIN)];
373 break;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000374 }
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000375 return retval;
David Blaikie87299ad2017-01-16 20:36:26 +0000376}
377
378// Outputs a DOT format version of the Graph embedded in the GraphRenderer
379// object on OS. It does this in the expected way by itterating
380// through all edges then vertices and then outputting them and their
381// annotations.
382//
383// FIXME: output more information, better presented.
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000384void GraphRenderer::exportGraphAsDOT(raw_ostream &OS, StatType ET, StatType EC,
385 StatType VT, StatType VC) {
David Blaikie87299ad2017-01-16 20:36:26 +0000386 OS << "digraph xray {\n";
387
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000388 if (VT != StatType::NONE)
389 OS << "node [shape=record];\n";
390
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000391 for (const auto &E : G.edges()) {
392 const auto &S = E.second.S;
393 OS << "F" << E.first.first << " -> "
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000394 << "F" << E.first.second << " [label=\"" << S.getString(ET) << "\"";
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000395 if (EC != StatType::NONE)
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000396 OS << " color=\""
397 << CHelper.getColorString(
398 std::sqrt(S.getDouble(EC) / G.GraphEdgeMax.getDouble(EC)))
Dean Michael Berrisf0cb13d2017-02-25 00:26:42 +0000399 << "\"";
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000400 OS << "];\n";
401 }
David Blaikie87299ad2017-01-16 20:36:26 +0000402
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000403 for (const auto &V : G.vertices()) {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000404 const auto &VA = V.second;
Dean Michael Berris6c97b3a2017-02-10 06:36:08 +0000405 if (V.first == 0)
406 continue;
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000407 OS << "F" << V.first << " [label=\"" << (VT != StatType::NONE ? "{" : "")
408 << (VA.SymbolName.size() > 40 ? VA.SymbolName.substr(0, 40) + "..."
409 : VA.SymbolName);
410 if (VT != StatType::NONE)
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000411 OS << "|" << VA.S.getString(VT) << "}\"";
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000412 else
413 OS << "\"";
414 if (VC != StatType::NONE)
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000415 OS << " color=\""
416 << CHelper.getColorString(
417 std::sqrt(VA.S.getDouble(VC) / G.GraphVertexMax.getDouble(VC)))
Dean Michael Berrisf0cb13d2017-02-25 00:26:42 +0000418 << "\"";
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000419 OS << "];\n";
420 }
David Blaikie87299ad2017-01-16 20:36:26 +0000421 OS << "}\n";
422}
423
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000424Expected<GraphRenderer> GraphRenderer::Factory::getGraphRenderer() {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000425 InstrumentationMap Map;
426 if (!GraphInstrMap.empty()) {
427 auto InstrumentationMapOrError = loadInstrumentationMap(GraphInstrMap);
428 if (!InstrumentationMapOrError)
429 return joinErrors(
430 make_error<StringError>(
431 Twine("Cannot open instrumentation map '") + GraphInstrMap + "'",
432 std::make_error_code(std::errc::invalid_argument)),
433 InstrumentationMapOrError.takeError());
434 Map = std::move(*InstrumentationMapOrError);
435 }
David Blaikie87299ad2017-01-16 20:36:26 +0000436
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000437 const auto &FunctionAddresses = Map.getFunctionAddresses();
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000438
David Blaikie87299ad2017-01-16 20:36:26 +0000439 symbolize::LLVMSymbolizer::Options Opts(
440 symbolize::FunctionNameKind::LinkageName, true, true, false, "");
David Blaikie87299ad2017-01-16 20:36:26 +0000441 symbolize::LLVMSymbolizer Symbolizer(Opts);
David Blaikie87299ad2017-01-16 20:36:26 +0000442 const auto &Header = Trace.getFileHeader();
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000443
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000444 llvm::xray::FuncIdConversionHelper FuncIdHelper(InstrMap, Symbolizer,
445 FunctionAddresses);
446
447 xray::GraphRenderer GR(FuncIdHelper, DeduceSiblingCalls);
David Blaikie87299ad2017-01-16 20:36:26 +0000448 for (const auto &Record : Trace) {
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000449 auto E = GR.accountRecord(Record);
450 if (!E)
451 continue;
452
453 for (const auto &ThreadStack : GR.getPerThreadFunctionStack()) {
454 errs() << "Thread ID: " << ThreadStack.first << "\n";
455 auto Level = ThreadStack.second.size();
456 for (const auto &Entry : llvm::reverse(ThreadStack.second))
457 errs() << "#" << Level-- << "\t"
458 << FuncIdHelper.SymbolOrNumber(Entry.FuncId) << '\n';
David Blaikie87299ad2017-01-16 20:36:26 +0000459 }
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000460
461 if (!GraphKeepGoing)
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000462 return joinErrors(make_error<StringError>(
463 "Error encountered generating the call graph.",
Dean Michael Berrisda2673c2017-02-01 00:22:20 +0000464 std::make_error_code(std::errc::invalid_argument)),
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000465 std::move(E));
466
Dean Michael Berrisd09bf192017-01-25 07:14:43 +0000467 handleAllErrors(std::move(E),
468 [&](const ErrorInfoBase &E) { E.log(errs()); });
David Blaikie87299ad2017-01-16 20:36:26 +0000469 }
Dean Michael Berrisca780b52017-04-24 05:54:33 +0000470
471 GR.G.GraphEdgeMax = {};
472 GR.G.GraphVertexMax = {};
473 GR.calculateEdgeStatistics();
474 GR.calculateVertexStatistics();
475
476 if (Header.CycleFrequency)
477 GR.normalizeStatistics(Header.CycleFrequency);
478
479 return GR;
480}
481
482// Here we register and implement the llvm-xray graph subcommand.
483// The bulk of this code reads in the options, opens the required files, uses
484// those files to create a context for analysing the xray trace, then there is a
485// short loop which actually analyses the trace, generates the graph and then
486// outputs it as a DOT.
487//
488// FIXME: include additional filtering and annalysis passes to provide more
489// specific useful information.
490static CommandRegistration Unused(&GraphC, []() -> Error {
491 GraphRenderer::Factory F;
492
493 F.KeepGoing = GraphKeepGoing;
494 F.DeduceSiblingCalls = GraphDeduceSiblingCalls;
495 F.InstrMap = GraphInstrMap;
496
497 auto TraceOrErr = loadTraceFile(GraphInput, true);
498
499 if (!TraceOrErr)
500 return make_error<StringError>(
501 Twine("Failed loading input file '") + GraphInput + "'",
502 make_error_code(llvm::errc::invalid_argument));
503
504 F.Trace = std::move(*TraceOrErr);
505 auto GROrError = F.getGraphRenderer();
506 if (!GROrError)
507 return GROrError.takeError();
508 auto &GR = *GROrError;
509
510 std::error_code EC;
511 raw_fd_ostream OS(GraphOutput, EC, sys::fs::OpenFlags::F_Text);
512 if (EC)
513 return make_error<StringError>(
514 Twine("Cannot open file '") + GraphOutput + "' for writing.", EC);
515
516 GR.exportGraphAsDOT(OS, GraphEdgeLabel, GraphEdgeColorType, GraphVertexLabel,
517 GraphVertexColorType);
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000518 return Error::success();
David Blaikie87299ad2017-01-16 20:36:26 +0000519});