Dean Michael Berris | a0e3ae4 | 2018-05-02 00:43:17 +0000 | [diff] [blame] | 1 | //===-- xray-graph.cpp: XRay Function Call Graph Renderer -----------------===// |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Generate a DOT file to represent the function call graph encountered in |
| 10 | // the trace. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 13 | |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 14 | #include "xray-graph.h" |
| 15 | #include "xray-registry.h" |
| 16 | #include "llvm/Support/ErrorHandling.h" |
Dean Michael Berris | 0e8abab | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 17 | #include "llvm/XRay/InstrumentationMap.h" |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 18 | #include "llvm/XRay/Trace.h" |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
Dean Michael Berris | 0e8abab | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 21 | using namespace llvm::xray; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 22 | |
| 23 | // Setup llvm-xray graph subcommand and its options. |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 24 | static cl::SubCommand GraphC("graph", "Generate function-call graph"); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 25 | static cl::opt<std::string> GraphInput(cl::Positional, |
| 26 | cl::desc("<xray log file>"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 27 | cl::Required, cl::sub(GraphC)); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 28 | |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 29 | static cl::opt<bool> |
| 30 | GraphKeepGoing("keep-going", cl::desc("Keep going on errors encountered"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 31 | cl::sub(GraphC), cl::init(false)); |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 32 | static cl::alias GraphKeepGoing2("k", cl::aliasopt(GraphKeepGoing), |
| 33 | cl::desc("Alias for -keep-going"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 34 | cl::sub(GraphC)); |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 35 | |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 36 | static cl::opt<std::string> |
| 37 | GraphOutput("output", cl::value_desc("Output file"), cl::init("-"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 38 | cl::desc("output file; use '-' for stdout"), cl::sub(GraphC)); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 39 | static cl::alias GraphOutput2("o", cl::aliasopt(GraphOutput), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 40 | cl::desc("Alias for -output"), cl::sub(GraphC)); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 41 | |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 42 | static 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 Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 48 | static cl::alias GraphInstrMap2("m", cl::aliasopt(GraphInstrMap), |
| 49 | cl::desc("alias for -instr_map"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 50 | cl::sub(GraphC)); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 51 | |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 52 | static cl::opt<bool> GraphDeduceSiblingCalls( |
| 53 | "deduce-sibling-calls", |
| 54 | cl::desc("Deduce sibling calls when unrolling function call stacks"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 55 | cl::sub(GraphC), cl::init(false)); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 56 | static cl::alias |
| 57 | GraphDeduceSiblingCalls2("d", cl::aliasopt(GraphDeduceSiblingCalls), |
| 58 | cl::desc("Alias for -deduce-sibling-calls"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 59 | cl::sub(GraphC)); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 60 | |
| 61 | static cl::opt<GraphRenderer::StatType> |
| 62 | GraphEdgeLabel("edge-label", |
| 63 | cl::desc("Output graphs with edges labeled with this field"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 64 | cl::value_desc("field"), cl::sub(GraphC), |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 65 | cl::init(GraphRenderer::StatType::NONE), |
| 66 | cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none", |
| 67 | "Do not label Edges"), |
| 68 | clEnumValN(GraphRenderer::StatType::COUNT, |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 69 | "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"))); |
| 82 | static cl::alias GraphEdgeLabel2("e", cl::aliasopt(GraphEdgeLabel), |
| 83 | cl::desc("Alias for -edge-label"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 84 | cl::sub(GraphC)); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 85 | |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 86 | static cl::opt<GraphRenderer::StatType> GraphVertexLabel( |
| 87 | "vertex-label", |
| 88 | cl::desc("Output graphs with vertices labeled with this field"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 89 | cl::value_desc("field"), cl::sub(GraphC), |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 90 | cl::init(GraphRenderer::StatType::NONE), |
| 91 | cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none", |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 92 | "Do not label Vertices"), |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 93 | 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"))); |
| 107 | static cl::alias GraphVertexLabel2("v", cl::aliasopt(GraphVertexLabel), |
| 108 | cl::desc("Alias for -edge-label"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 109 | cl::sub(GraphC)); |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 110 | |
| 111 | static cl::opt<GraphRenderer::StatType> GraphEdgeColorType( |
| 112 | "color-edges", |
| 113 | cl::desc("Output graphs with edge colors determined by this field"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 114 | cl::value_desc("field"), cl::sub(GraphC), |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 115 | cl::init(GraphRenderer::StatType::NONE), |
| 116 | cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none", |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 117 | "Do not color Edges"), |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 118 | 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"))); |
| 132 | static cl::alias GraphEdgeColorType2("c", cl::aliasopt(GraphEdgeColorType), |
| 133 | cl::desc("Alias for -color-edges"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 134 | cl::sub(GraphC)); |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 135 | |
| 136 | static cl::opt<GraphRenderer::StatType> GraphVertexColorType( |
| 137 | "color-vertices", |
| 138 | cl::desc("Output graphs with vertex colors determined by this field"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 139 | cl::value_desc("field"), cl::sub(GraphC), |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 140 | cl::init(GraphRenderer::StatType::NONE), |
| 141 | cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none", |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 142 | "Do not color vertices"), |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 143 | 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"))); |
| 157 | static cl::alias GraphVertexColorType2("b", cl::aliasopt(GraphVertexColorType), |
| 158 | cl::desc("Alias for -edge-label"), |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 159 | cl::sub(GraphC)); |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 160 | |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 161 | template <class T> T diff(T L, T R) { return std::max(L, R) - std::min(L, R); } |
| 162 | |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 163 | // Updates the statistics for a GraphRenderer::TimeStat |
| 164 | static void updateStat(GraphRenderer::TimeStat &S, int64_t L) { |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 165 | S.Count++; |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 166 | 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 Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 173 | // Evaluates an XRay record and performs accounting on it. |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 174 | // |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 175 | // 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 Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 188 | // FuncId 0. |
| 189 | // |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 190 | // FIXME: Refactor this and account subcommand to reduce code duplication. |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 191 | Error GraphRenderer::accountRecord(const XRayRecord &Record) { |
| 192 | using std::make_error_code; |
| 193 | using std::errc; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 194 | if (CurrentMaxTSC == 0) |
| 195 | CurrentMaxTSC = Record.TSC; |
| 196 | |
| 197 | if (Record.TSC < CurrentMaxTSC) |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 198 | return make_error<StringError>("Records not in order", |
| 199 | make_error_code(errc::invalid_argument)); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 200 | |
| 201 | auto &ThreadStack = PerThreadFunctionStack[Record.TId]; |
| 202 | switch (Record.Type) { |
Martin Pelikan | 10c873f | 2017-09-27 04:48:03 +0000 | [diff] [blame] | 203 | case RecordTypes::ENTER: |
| 204 | case RecordTypes::ENTER_ARG: { |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 205 | if (Record.FuncId != 0 && G.count(Record.FuncId) == 0) |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 206 | G[Record.FuncId].SymbolName = FuncIdHelper.SymbolOrNumber(Record.FuncId); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 207 | ThreadStack.push_back({Record.FuncId, Record.TSC}); |
| 208 | break; |
| 209 | } |
Dean Michael Berris | 0f84a7d | 2017-09-18 06:08:46 +0000 | [diff] [blame] | 210 | case RecordTypes::EXIT: |
| 211 | case RecordTypes::TAIL_EXIT: { |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 212 | // FIXME: Refactor this and the account subcommand to reduce code |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 213 | // duplication |
| 214 | if (ThreadStack.size() == 0 || ThreadStack.back().FuncId != Record.FuncId) { |
| 215 | if (!DeduceSiblingCalls) |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 216 | return make_error<StringError>("No matching ENTRY record", |
| 217 | make_error_code(errc::invalid_argument)); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 218 | 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 Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 222 | 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 Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 226 | while (ThreadStack.back().FuncId != Record.FuncId) { |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 227 | TimestampT D = diff(ThreadStack.back().TSC, Record.TSC); |
| 228 | VertexIdentifier TopFuncId = ThreadStack.back().FuncId; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 229 | ThreadStack.pop_back(); |
| 230 | assert(ThreadStack.size() != 0); |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 231 | EdgeIdentifier EI(ThreadStack.back().FuncId, TopFuncId); |
| 232 | auto &EA = G[EI]; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 233 | EA.Timings.push_back(D); |
| 234 | updateStat(EA.S, D); |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 235 | updateStat(G[TopFuncId].S, D); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | uint64_t D = diff(ThreadStack.back().TSC, Record.TSC); |
| 239 | ThreadStack.pop_back(); |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 240 | VertexIdentifier VI = ThreadStack.empty() ? 0 : ThreadStack.back().FuncId; |
| 241 | EdgeIdentifier EI(VI, Record.FuncId); |
| 242 | auto &EA = G[EI]; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 243 | EA.Timings.push_back(D); |
| 244 | updateStat(EA.S, D); |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 245 | updateStat(G[Record.FuncId].S, D); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 246 | break; |
| 247 | } |
Dean Michael Berris | 25f8d20 | 2018-11-06 08:51:37 +0000 | [diff] [blame] | 248 | case RecordTypes::CUSTOM_EVENT: |
| 249 | case RecordTypes::TYPED_EVENT: |
| 250 | // TODO: Support custom and typed events in the graph processing? |
| 251 | break; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 254 | return Error::success(); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | template <typename U> |
| 258 | void GraphRenderer::getStats(U begin, U end, GraphRenderer::TimeStat &S) { |
Dean Michael Berris | 5685468 | 2017-03-31 01:56:45 +0000 | [diff] [blame] | 259 | if (begin == end) return; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 260 | 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 Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 271 | void 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 Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 282 | void GraphRenderer::calculateEdgeStatistics() { |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 283 | assert(!G.edges().empty()); |
| 284 | for (auto &E : G.edges()) { |
| 285 | auto &A = E.second; |
| 286 | assert(!A.Timings.empty()); |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 287 | getStats(A.Timings.begin(), A.Timings.end(), A.S); |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 288 | updateMaxStats(A.S, G.GraphEdgeMax); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | void GraphRenderer::calculateVertexStatistics() { |
Dean Michael Berris | 79f5746 | 2017-02-10 06:05:46 +0000 | [diff] [blame] | 293 | std::vector<uint64_t> TempTimings; |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 294 | for (auto &V : G.vertices()) { |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 295 | 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 Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 301 | getStats(TempTimings.begin(), TempTimings.end(), G[V.first].S); |
| 302 | updateMaxStats(G[V.first].S, G.GraphVertexMax); |
| 303 | TempTimings.clear(); |
Dean Michael Berris | 79f5746 | 2017-02-10 06:05:46 +0000 | [diff] [blame] | 304 | } |
Dean Michael Berris | 79f5746 | 2017-02-10 06:05:46 +0000 | [diff] [blame] | 305 | } |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 308 | // A Helper function for normalizeStatistics which normalises a single |
| 309 | // TimeStat element. |
| 310 | static void normalizeTimeStat(GraphRenderer::TimeStat &S, |
| 311 | double CycleFrequency) { |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 312 | int64_t OldCount = S.Count; |
| 313 | S = S / CycleFrequency; |
| 314 | S.Count = OldCount; |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // Normalises the statistics in the graph for a given TSC frequency. |
| 318 | void GraphRenderer::normalizeStatistics(double CycleFrequency) { |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 319 | for (auto &E : G.edges()) { |
| 320 | auto &S = E.second.S; |
| 321 | normalizeTimeStat(S, CycleFrequency); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 322 | } |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 323 | for (auto &V : G.vertices()) { |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 324 | auto &S = V.second.S; |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 325 | normalizeTimeStat(S, CycleFrequency); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 326 | } |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 327 | |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 328 | normalizeTimeStat(G.GraphEdgeMax, CycleFrequency); |
| 329 | normalizeTimeStat(G.GraphVertexMax, CycleFrequency); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 332 | // Returns a string containing the value of statistic field T |
| 333 | std::string |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 334 | GraphRenderer::TimeStat::getString(GraphRenderer::StatType T) const { |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 335 | std::string St; |
| 336 | raw_string_ostream S{St}; |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 337 | double TimeStat::*DoubleStatPtrs[] = {&TimeStat::Min, &TimeStat::Median, |
| 338 | &TimeStat::Pct90, &TimeStat::Pct99, |
| 339 | &TimeStat::Max, &TimeStat::Sum}; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 340 | switch (T) { |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 341 | case GraphRenderer::StatType::NONE: |
| 342 | break; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 343 | case GraphRenderer::StatType::COUNT: |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 344 | S << Count; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 345 | break; |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 346 | default: |
| 347 | S << (*this).* |
| 348 | DoubleStatPtrs[static_cast<int>(T) - |
| 349 | static_cast<int>(GraphRenderer::StatType::MIN)]; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 352 | return S.str(); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 353 | } |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 354 | |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 355 | // Returns the quotient between the property T of this and another TimeStat as |
| 356 | // a double |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 357 | double GraphRenderer::TimeStat::getDouble(StatType T) const { |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 358 | double retval = 0; |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 359 | double TimeStat::*DoubleStatPtrs[] = {&TimeStat::Min, &TimeStat::Median, |
| 360 | &TimeStat::Pct90, &TimeStat::Pct99, |
| 361 | &TimeStat::Max, &TimeStat::Sum}; |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 362 | switch (T) { |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 363 | case GraphRenderer::StatType::NONE: |
| 364 | retval = 0.0; |
| 365 | break; |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 366 | 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 Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 374 | } |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 375 | return retval; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 376 | } |
| 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 Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 384 | void GraphRenderer::exportGraphAsDOT(raw_ostream &OS, StatType ET, StatType EC, |
| 385 | StatType VT, StatType VC) { |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 386 | OS << "digraph xray {\n"; |
| 387 | |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 388 | if (VT != StatType::NONE) |
| 389 | OS << "node [shape=record];\n"; |
| 390 | |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 391 | for (const auto &E : G.edges()) { |
| 392 | const auto &S = E.second.S; |
| 393 | OS << "F" << E.first.first << " -> " |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 394 | << "F" << E.first.second << " [label=\"" << S.getString(ET) << "\""; |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 395 | if (EC != StatType::NONE) |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 396 | OS << " color=\"" |
| 397 | << CHelper.getColorString( |
| 398 | std::sqrt(S.getDouble(EC) / G.GraphEdgeMax.getDouble(EC))) |
Dean Michael Berris | f0cb13d | 2017-02-25 00:26:42 +0000 | [diff] [blame] | 399 | << "\""; |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 400 | OS << "];\n"; |
| 401 | } |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 402 | |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 403 | for (const auto &V : G.vertices()) { |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 404 | const auto &VA = V.second; |
Dean Michael Berris | 6c97b3a | 2017-02-10 06:36:08 +0000 | [diff] [blame] | 405 | if (V.first == 0) |
| 406 | continue; |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 407 | 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 Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 411 | OS << "|" << VA.S.getString(VT) << "}\""; |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 412 | else |
| 413 | OS << "\""; |
| 414 | if (VC != StatType::NONE) |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 415 | OS << " color=\"" |
| 416 | << CHelper.getColorString( |
| 417 | std::sqrt(VA.S.getDouble(VC) / G.GraphVertexMax.getDouble(VC))) |
Dean Michael Berris | f0cb13d | 2017-02-25 00:26:42 +0000 | [diff] [blame] | 418 | << "\""; |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 419 | OS << "];\n"; |
| 420 | } |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 421 | OS << "}\n"; |
| 422 | } |
| 423 | |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 424 | Expected<GraphRenderer> GraphRenderer::Factory::getGraphRenderer() { |
Dean Michael Berris | 0e8abab | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 425 | 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 Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 436 | |
Dean Michael Berris | 0e8abab | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 437 | const auto &FunctionAddresses = Map.getFunctionAddresses(); |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 438 | |
Peter Collingbourne | a2048f8 | 2019-06-11 02:31:54 +0000 | [diff] [blame] | 439 | symbolize::LLVMSymbolizer Symbolizer; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 440 | const auto &Header = Trace.getFileHeader(); |
Dean Michael Berris | 0e8abab | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 441 | |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 442 | llvm::xray::FuncIdConversionHelper FuncIdHelper(InstrMap, Symbolizer, |
| 443 | FunctionAddresses); |
| 444 | |
| 445 | xray::GraphRenderer GR(FuncIdHelper, DeduceSiblingCalls); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 446 | for (const auto &Record : Trace) { |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 447 | auto E = GR.accountRecord(Record); |
| 448 | if (!E) |
| 449 | continue; |
| 450 | |
| 451 | for (const auto &ThreadStack : GR.getPerThreadFunctionStack()) { |
| 452 | errs() << "Thread ID: " << ThreadStack.first << "\n"; |
| 453 | auto Level = ThreadStack.second.size(); |
| 454 | for (const auto &Entry : llvm::reverse(ThreadStack.second)) |
| 455 | errs() << "#" << Level-- << "\t" |
| 456 | << FuncIdHelper.SymbolOrNumber(Entry.FuncId) << '\n'; |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 457 | } |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 458 | |
| 459 | if (!GraphKeepGoing) |
Dean Michael Berris | 0e8abab | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 460 | return joinErrors(make_error<StringError>( |
| 461 | "Error encountered generating the call graph.", |
Dean Michael Berris | da2673c | 2017-02-01 00:22:20 +0000 | [diff] [blame] | 462 | std::make_error_code(std::errc::invalid_argument)), |
Dean Michael Berris | 0e8abab | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 463 | std::move(E)); |
| 464 | |
Dean Michael Berris | d09bf19 | 2017-01-25 07:14:43 +0000 | [diff] [blame] | 465 | handleAllErrors(std::move(E), |
| 466 | [&](const ErrorInfoBase &E) { E.log(errs()); }); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 467 | } |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 468 | |
| 469 | GR.G.GraphEdgeMax = {}; |
| 470 | GR.G.GraphVertexMax = {}; |
| 471 | GR.calculateEdgeStatistics(); |
| 472 | GR.calculateVertexStatistics(); |
| 473 | |
| 474 | if (Header.CycleFrequency) |
| 475 | GR.normalizeStatistics(Header.CycleFrequency); |
| 476 | |
| 477 | return GR; |
| 478 | } |
| 479 | |
| 480 | // Here we register and implement the llvm-xray graph subcommand. |
| 481 | // The bulk of this code reads in the options, opens the required files, uses |
| 482 | // those files to create a context for analysing the xray trace, then there is a |
| 483 | // short loop which actually analyses the trace, generates the graph and then |
| 484 | // outputs it as a DOT. |
| 485 | // |
| 486 | // FIXME: include additional filtering and annalysis passes to provide more |
| 487 | // specific useful information. |
| 488 | static CommandRegistration Unused(&GraphC, []() -> Error { |
| 489 | GraphRenderer::Factory F; |
| 490 | |
| 491 | F.KeepGoing = GraphKeepGoing; |
| 492 | F.DeduceSiblingCalls = GraphDeduceSiblingCalls; |
| 493 | F.InstrMap = GraphInstrMap; |
| 494 | |
| 495 | auto TraceOrErr = loadTraceFile(GraphInput, true); |
| 496 | |
| 497 | if (!TraceOrErr) |
| 498 | return make_error<StringError>( |
| 499 | Twine("Failed loading input file '") + GraphInput + "'", |
| 500 | make_error_code(llvm::errc::invalid_argument)); |
| 501 | |
| 502 | F.Trace = std::move(*TraceOrErr); |
| 503 | auto GROrError = F.getGraphRenderer(); |
| 504 | if (!GROrError) |
| 505 | return GROrError.takeError(); |
| 506 | auto &GR = *GROrError; |
| 507 | |
| 508 | std::error_code EC; |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 509 | raw_fd_ostream OS(GraphOutput, EC, sys::fs::OpenFlags::OF_Text); |
Dean Michael Berris | ca780b5 | 2017-04-24 05:54:33 +0000 | [diff] [blame] | 510 | if (EC) |
| 511 | return make_error<StringError>( |
| 512 | Twine("Cannot open file '") + GraphOutput + "' for writing.", EC); |
| 513 | |
| 514 | GR.exportGraphAsDOT(OS, GraphEdgeLabel, GraphEdgeColorType, GraphVertexLabel, |
| 515 | GraphVertexColorType); |
Dean Michael Berris | 0e8abab | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 516 | return Error::success(); |
David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame] | 517 | }); |