xray-graph-diff.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. //===-- xray-graph-diff.cpp: XRay Function Call Graph Renderer ------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Generate a DOT file to represent the function call graph encountered in
  10. // the trace.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include <cassert>
  14. #include <cmath>
  15. #include <limits>
  16. #include <string>
  17. #include "xray-graph-diff.h"
  18. #include "xray-graph.h"
  19. #include "xray-registry.h"
  20. #include "xray-color-helper.h"
  21. #include "llvm/ADT/iterator_range.h"
  22. #include "llvm/Support/FormatVariadic.h"
  23. #include "llvm/XRay/Trace.h"
  24. using namespace llvm;
  25. using namespace xray;
  26. static cl::SubCommand GraphDiff("graph-diff",
  27. "Generate diff of function-call graphs");
  28. static cl::opt<std::string> GraphDiffInput1(cl::Positional,
  29. cl::desc("<xray log file 1>"),
  30. cl::Required, cl::sub(GraphDiff));
  31. static cl::opt<std::string> GraphDiffInput2(cl::Positional,
  32. cl::desc("<xray log file 2>"),
  33. cl::Required, cl::sub(GraphDiff));
  34. static cl::opt<bool>
  35. GraphDiffKeepGoing("keep-going",
  36. cl::desc("Keep going on errors encountered"),
  37. cl::sub(GraphDiff), cl::init(false));
  38. static cl::alias GraphDiffKeepGoingA("k", cl::aliasopt(GraphDiffKeepGoing),
  39. cl::desc("Alias for -keep-going"));
  40. static cl::opt<bool>
  41. GraphDiffKeepGoing1("keep-going-1",
  42. cl::desc("Keep going on errors encountered in trace 1"),
  43. cl::sub(GraphDiff), cl::init(false));
  44. static cl::alias GraphDiffKeepGoing1A("k1", cl::aliasopt(GraphDiffKeepGoing1),
  45. cl::desc("Alias for -keep-going-1"));
  46. static cl::opt<bool>
  47. GraphDiffKeepGoing2("keep-going-2",
  48. cl::desc("Keep going on errors encountered in trace 2"),
  49. cl::sub(GraphDiff), cl::init(false));
  50. static cl::alias GraphDiffKeepGoing2A("k2", cl::aliasopt(GraphDiffKeepGoing2),
  51. cl::desc("Alias for -keep-going-2"));
  52. static cl::opt<std::string>
  53. GraphDiffInstrMap("instr-map",
  54. cl::desc("binary with the instrumentation map, or "
  55. "a separate instrumentation map for graph"),
  56. cl::value_desc("binary with xray_instr_map or yaml"),
  57. cl::sub(GraphDiff), cl::init(""));
  58. static cl::alias GraphDiffInstrMapA("m", cl::aliasopt(GraphDiffInstrMap),
  59. cl::desc("Alias for -instr-map"));
  60. static cl::opt<std::string>
  61. GraphDiffInstrMap1("instr-map-1",
  62. cl::desc("binary with the instrumentation map, or "
  63. "a separate instrumentation map for graph 1"),
  64. cl::value_desc("binary with xray_instr_map or yaml"),
  65. cl::sub(GraphDiff), cl::init(""));
  66. static cl::alias GraphDiffInstrMap1A("m1", cl::aliasopt(GraphDiffInstrMap1),
  67. cl::desc("Alias for -instr-map-1"));
  68. static cl::opt<std::string>
  69. GraphDiffInstrMap2("instr-map-2",
  70. cl::desc("binary with the instrumentation map, or "
  71. "a separate instrumentation map for graph 2"),
  72. cl::value_desc("binary with xray_instr_map or yaml"),
  73. cl::sub(GraphDiff), cl::init(""));
  74. static cl::alias GraphDiffInstrMap2A("m2", cl::aliasopt(GraphDiffInstrMap2),
  75. cl::desc("Alias for -instr-map-2"));
  76. static cl::opt<bool> GraphDiffDeduceSiblingCalls(
  77. "deduce-sibling-calls",
  78. cl::desc("Deduce sibling calls when unrolling function call stacks"),
  79. cl::sub(GraphDiff), cl::init(false));
  80. static cl::alias
  81. GraphDiffDeduceSiblingCallsA("d", cl::aliasopt(GraphDiffDeduceSiblingCalls),
  82. cl::desc("Alias for -deduce-sibling-calls"));
  83. static cl::opt<bool> GraphDiffDeduceSiblingCalls1(
  84. "deduce-sibling-calls-1",
  85. cl::desc("Deduce sibling calls when unrolling function call stacks"),
  86. cl::sub(GraphDiff), cl::init(false));
  87. static cl::alias GraphDiffDeduceSiblingCalls1A(
  88. "d1", cl::aliasopt(GraphDiffDeduceSiblingCalls1),
  89. cl::desc("Alias for -deduce-sibling-calls-1"));
  90. static cl::opt<bool> GraphDiffDeduceSiblingCalls2(
  91. "deduce-sibling-calls-2",
  92. cl::desc("Deduce sibling calls when unrolling function call stacks"),
  93. cl::sub(GraphDiff), cl::init(false));
  94. static cl::alias GraphDiffDeduceSiblingCalls2A(
  95. "d2", cl::aliasopt(GraphDiffDeduceSiblingCalls2),
  96. cl::desc("Alias for -deduce-sibling-calls-2"));
  97. static cl::opt<GraphRenderer::StatType> GraphDiffEdgeLabel(
  98. "edge-label", cl::desc("Output graphs with edges labeled with this field"),
  99. cl::value_desc("field"), cl::sub(GraphDiff),
  100. cl::init(GraphRenderer::StatType::NONE),
  101. cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
  102. "Do not label Edges"),
  103. clEnumValN(GraphRenderer::StatType::COUNT, "count",
  104. "function call counts"),
  105. clEnumValN(GraphRenderer::StatType::MIN, "min",
  106. "minimum function durations"),
  107. clEnumValN(GraphRenderer::StatType::MED, "med",
  108. "median function durations"),
  109. clEnumValN(GraphRenderer::StatType::PCT90, "90p",
  110. "90th percentile durations"),
  111. clEnumValN(GraphRenderer::StatType::PCT99, "99p",
  112. "99th percentile durations"),
  113. clEnumValN(GraphRenderer::StatType::MAX, "max",
  114. "maximum function durations"),
  115. clEnumValN(GraphRenderer::StatType::SUM, "sum",
  116. "sum of call durations")));
  117. static cl::alias GraphDiffEdgeLabelA("e", cl::aliasopt(GraphDiffEdgeLabel),
  118. cl::desc("Alias for -edge-label"));
  119. static cl::opt<GraphRenderer::StatType> GraphDiffEdgeColor(
  120. "edge-color", cl::desc("Output graphs with edges colored by this field"),
  121. cl::value_desc("field"), cl::sub(GraphDiff),
  122. cl::init(GraphRenderer::StatType::NONE),
  123. cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
  124. "Do not color Edges"),
  125. clEnumValN(GraphRenderer::StatType::COUNT, "count",
  126. "function call counts"),
  127. clEnumValN(GraphRenderer::StatType::MIN, "min",
  128. "minimum function durations"),
  129. clEnumValN(GraphRenderer::StatType::MED, "med",
  130. "median function durations"),
  131. clEnumValN(GraphRenderer::StatType::PCT90, "90p",
  132. "90th percentile durations"),
  133. clEnumValN(GraphRenderer::StatType::PCT99, "99p",
  134. "99th percentile durations"),
  135. clEnumValN(GraphRenderer::StatType::MAX, "max",
  136. "maximum function durations"),
  137. clEnumValN(GraphRenderer::StatType::SUM, "sum",
  138. "sum of call durations")));
  139. static cl::alias GraphDiffEdgeColorA("c", cl::aliasopt(GraphDiffEdgeColor),
  140. cl::desc("Alias for -edge-color"));
  141. static cl::opt<GraphRenderer::StatType> GraphDiffVertexLabel(
  142. "vertex-label",
  143. cl::desc("Output graphs with vertices labeled with this field"),
  144. cl::value_desc("field"), cl::sub(GraphDiff),
  145. cl::init(GraphRenderer::StatType::NONE),
  146. cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
  147. "Do not label Vertices"),
  148. clEnumValN(GraphRenderer::StatType::COUNT, "count",
  149. "function call counts"),
  150. clEnumValN(GraphRenderer::StatType::MIN, "min",
  151. "minimum function durations"),
  152. clEnumValN(GraphRenderer::StatType::MED, "med",
  153. "median function durations"),
  154. clEnumValN(GraphRenderer::StatType::PCT90, "90p",
  155. "90th percentile durations"),
  156. clEnumValN(GraphRenderer::StatType::PCT99, "99p",
  157. "99th percentile durations"),
  158. clEnumValN(GraphRenderer::StatType::MAX, "max",
  159. "maximum function durations"),
  160. clEnumValN(GraphRenderer::StatType::SUM, "sum",
  161. "sum of call durations")));
  162. static cl::alias GraphDiffVertexLabelA("v", cl::aliasopt(GraphDiffVertexLabel),
  163. cl::desc("Alias for -vertex-label"));
  164. static cl::opt<GraphRenderer::StatType> GraphDiffVertexColor(
  165. "vertex-color",
  166. cl::desc("Output graphs with vertices colored by this field"),
  167. cl::value_desc("field"), cl::sub(GraphDiff),
  168. cl::init(GraphRenderer::StatType::NONE),
  169. cl::values(clEnumValN(GraphRenderer::StatType::NONE, "none",
  170. "Do not color Vertices"),
  171. clEnumValN(GraphRenderer::StatType::COUNT, "count",
  172. "function call counts"),
  173. clEnumValN(GraphRenderer::StatType::MIN, "min",
  174. "minimum function durations"),
  175. clEnumValN(GraphRenderer::StatType::MED, "med",
  176. "median function durations"),
  177. clEnumValN(GraphRenderer::StatType::PCT90, "90p",
  178. "90th percentile durations"),
  179. clEnumValN(GraphRenderer::StatType::PCT99, "99p",
  180. "99th percentile durations"),
  181. clEnumValN(GraphRenderer::StatType::MAX, "max",
  182. "maximum function durations"),
  183. clEnumValN(GraphRenderer::StatType::SUM, "sum",
  184. "sum of call durations")));
  185. static cl::alias GraphDiffVertexColorA("b", cl::aliasopt(GraphDiffVertexColor),
  186. cl::desc("Alias for -vertex-color"));
  187. static cl::opt<int> GraphDiffVertexLabelTrunc(
  188. "vertex-label-trun", cl::desc("What length to truncate vertex labels to "),
  189. cl::sub(GraphDiff), cl::init(40));
  190. static cl::alias
  191. GraphDiffVertexLabelTrunc1("t", cl::aliasopt(GraphDiffVertexLabelTrunc),
  192. cl::desc("Alias for -vertex-label-trun"));
  193. static cl::opt<std::string>
  194. GraphDiffOutput("output", cl::value_desc("Output file"), cl::init("-"),
  195. cl::desc("output file; use '-' for stdout"),
  196. cl::sub(GraphDiff));
  197. static cl::alias GraphDiffOutputA("o", cl::aliasopt(GraphDiffOutput),
  198. cl::desc("Alias for -output"));
  199. Expected<GraphDiffRenderer> GraphDiffRenderer::Factory::getGraphDiffRenderer() {
  200. GraphDiffRenderer R;
  201. for (int i = 0; i < N; ++i) {
  202. const auto &G = this->G[i].get();
  203. for (const auto &V : G.vertices()) {
  204. const auto &VAttr = V.second;
  205. R.G[VAttr.SymbolName].CorrVertexPtr[i] = &V;
  206. }
  207. for (const auto &E : G.edges()) {
  208. auto &EdgeTailID = E.first.first;
  209. auto &EdgeHeadID = E.first.second;
  210. auto EdgeTailAttrOrErr = G.at(EdgeTailID);
  211. auto EdgeHeadAttrOrErr = G.at(EdgeHeadID);
  212. if (!EdgeTailAttrOrErr)
  213. return EdgeTailAttrOrErr.takeError();
  214. if (!EdgeHeadAttrOrErr)
  215. return EdgeHeadAttrOrErr.takeError();
  216. GraphT::EdgeIdentifier ID{EdgeTailAttrOrErr->SymbolName,
  217. EdgeHeadAttrOrErr->SymbolName};
  218. R.G[ID].CorrEdgePtr[i] = &E;
  219. }
  220. }
  221. return R;
  222. }
  223. // Returns the Relative change With respect to LeftStat between LeftStat
  224. // and RightStat.
  225. static double statRelDiff(const GraphDiffRenderer::TimeStat &LeftStat,
  226. const GraphDiffRenderer::TimeStat &RightStat,
  227. GraphDiffRenderer::StatType T) {
  228. double LeftAttr = LeftStat.getDouble(T);
  229. double RightAttr = RightStat.getDouble(T);
  230. return RightAttr / LeftAttr - 1.0;
  231. }
  232. static std::string getColor(const GraphDiffRenderer::GraphT::EdgeValueType &E,
  233. const GraphDiffRenderer::GraphT &G, ColorHelper H,
  234. GraphDiffRenderer::StatType T) {
  235. auto &EdgeAttr = E.second;
  236. if (EdgeAttr.CorrEdgePtr[0] == nullptr)
  237. return H.getColorString(2.0); // A number greater than 1.0
  238. if (EdgeAttr.CorrEdgePtr[1] == nullptr)
  239. return H.getColorString(-2.0); // A number less than -1.0
  240. if (T == GraphDiffRenderer::StatType::NONE)
  241. return H.getDefaultColorString();
  242. const auto &LeftStat = EdgeAttr.CorrEdgePtr[0]->second.S;
  243. const auto &RightStat = EdgeAttr.CorrEdgePtr[1]->second.S;
  244. double RelDiff = statRelDiff(LeftStat, RightStat, T);
  245. double CappedRelDiff = std::min(1.0, std::max(-1.0, RelDiff));
  246. return H.getColorString(CappedRelDiff);
  247. }
  248. static std::string getColor(const GraphDiffRenderer::GraphT::VertexValueType &V,
  249. const GraphDiffRenderer::GraphT &G, ColorHelper H,
  250. GraphDiffRenderer::StatType T) {
  251. auto &VertexAttr = V.second;
  252. if (VertexAttr.CorrVertexPtr[0] == nullptr)
  253. return H.getColorString(2.0); // A number greater than 1.0
  254. if (VertexAttr.CorrVertexPtr[1] == nullptr)
  255. return H.getColorString(-2.0); // A number less than -1.0
  256. if (T == GraphDiffRenderer::StatType::NONE)
  257. return H.getDefaultColorString();
  258. const auto &LeftStat = VertexAttr.CorrVertexPtr[0]->second.S;
  259. const auto &RightStat = VertexAttr.CorrVertexPtr[1]->second.S;
  260. double RelDiff = statRelDiff(LeftStat, RightStat, T);
  261. double CappedRelDiff = std::min(1.0, std::max(-1.0, RelDiff));
  262. return H.getColorString(CappedRelDiff);
  263. }
  264. static Twine truncateString(const StringRef &S, size_t n) {
  265. return (S.size() > n) ? Twine(S.substr(0, n)) + "..." : Twine(S);
  266. }
  267. template <typename T> static bool containsNullptr(const T &Collection) {
  268. return llvm::is_contained(Collection, nullptr);
  269. }
  270. static std::string getLabel(const GraphDiffRenderer::GraphT::EdgeValueType &E,
  271. GraphDiffRenderer::StatType EL) {
  272. auto &EdgeAttr = E.second;
  273. switch (EL) {
  274. case GraphDiffRenderer::StatType::NONE:
  275. return "";
  276. default:
  277. if (containsNullptr(EdgeAttr.CorrEdgePtr))
  278. return "";
  279. const auto &LeftStat = EdgeAttr.CorrEdgePtr[0]->second.S;
  280. const auto &RightStat = EdgeAttr.CorrEdgePtr[1]->second.S;
  281. double RelDiff = statRelDiff(LeftStat, RightStat, EL);
  282. return std::string(formatv(R"({0:P})", RelDiff));
  283. }
  284. }
  285. static std::string getLabel(const GraphDiffRenderer::GraphT::VertexValueType &V,
  286. GraphDiffRenderer::StatType VL, int TrunLen) {
  287. const auto &VertexId = V.first;
  288. const auto &VertexAttr = V.second;
  289. switch (VL) {
  290. case GraphDiffRenderer::StatType::NONE:
  291. return std::string(
  292. formatv(R"({0})", truncateString(VertexId, TrunLen).str()));
  293. default:
  294. if (containsNullptr(VertexAttr.CorrVertexPtr))
  295. return std::string(
  296. formatv(R"({0})", truncateString(VertexId, TrunLen).str()));
  297. const auto &LeftStat = VertexAttr.CorrVertexPtr[0]->second.S;
  298. const auto &RightStat = VertexAttr.CorrVertexPtr[1]->second.S;
  299. double RelDiff = statRelDiff(LeftStat, RightStat, VL);
  300. return std::string(formatv(
  301. R"({{{0}|{1:P}})", truncateString(VertexId, TrunLen).str(), RelDiff));
  302. }
  303. }
  304. static double getLineWidth(const GraphDiffRenderer::GraphT::EdgeValueType &E,
  305. GraphDiffRenderer::StatType EL) {
  306. auto &EdgeAttr = E.second;
  307. switch (EL) {
  308. case GraphDiffRenderer::StatType::NONE:
  309. return 1.0;
  310. default:
  311. if (containsNullptr(EdgeAttr.CorrEdgePtr))
  312. return 1.0;
  313. const auto &LeftStat = EdgeAttr.CorrEdgePtr[0]->second.S;
  314. const auto &RightStat = EdgeAttr.CorrEdgePtr[1]->second.S;
  315. double RelDiff = statRelDiff(LeftStat, RightStat, EL);
  316. return (RelDiff > 1.0) ? RelDiff : 1.0;
  317. }
  318. }
  319. void GraphDiffRenderer::exportGraphAsDOT(raw_ostream &OS, StatType EdgeLabel,
  320. StatType EdgeColor,
  321. StatType VertexLabel,
  322. StatType VertexColor, int TruncLen) {
  323. // Get numbering of vertices for dot output.
  324. StringMap<int32_t> VertexNo;
  325. int i = 0;
  326. for (const auto &V : G.vertices()) {
  327. VertexNo[V.first] = i++;
  328. }
  329. ColorHelper H(ColorHelper::DivergingScheme::PiYG);
  330. OS << "digraph xrayDiff {\n";
  331. if (VertexLabel != StatType::NONE)
  332. OS << "node [shape=record]\n";
  333. for (const auto &E : G.edges()) {
  334. const auto &HeadId = E.first.first;
  335. const auto &TailId = E.first.second;
  336. OS << formatv(R"(F{0} -> F{1} [tooltip="{2} -> {3}" label="{4}" )"
  337. R"(color="{5}" labelfontcolor="{5}" penwidth={6}])"
  338. "\n",
  339. VertexNo[HeadId], VertexNo[TailId],
  340. (HeadId.equals("")) ? static_cast<StringRef>("F0") : HeadId,
  341. TailId, getLabel(E, EdgeLabel), getColor(E, G, H, EdgeColor),
  342. getLineWidth(E, EdgeColor));
  343. }
  344. for (const auto &V : G.vertices()) {
  345. const auto &VertexId = V.first;
  346. if (VertexId.equals("")) {
  347. OS << formatv(R"(F{0} [label="F0"])"
  348. "\n",
  349. VertexNo[VertexId]);
  350. continue;
  351. }
  352. OS << formatv(R"(F{0} [label="{1}" color="{2}"])"
  353. "\n",
  354. VertexNo[VertexId], getLabel(V, VertexLabel, TruncLen),
  355. getColor(V, G, H, VertexColor));
  356. }
  357. OS << "}\n";
  358. }
  359. template <typename T> static T &ifSpecified(T &A, cl::alias &AA, T &B) {
  360. if (A.getPosition() == 0 && AA.getPosition() == 0)
  361. return B;
  362. return A;
  363. }
  364. static CommandRegistration Unused(&GraphDiff, []() -> Error {
  365. std::array<GraphRenderer::Factory, 2> Factories{
  366. {{ifSpecified(GraphDiffKeepGoing1, GraphDiffKeepGoing1A,
  367. GraphDiffKeepGoing),
  368. ifSpecified(GraphDiffDeduceSiblingCalls1, GraphDiffDeduceSiblingCalls1A,
  369. GraphDiffDeduceSiblingCalls),
  370. ifSpecified(GraphDiffInstrMap1, GraphDiffInstrMap1A, GraphDiffInstrMap),
  371. Trace()},
  372. {ifSpecified(GraphDiffKeepGoing2, GraphDiffKeepGoing2A,
  373. GraphDiffKeepGoing),
  374. ifSpecified(GraphDiffDeduceSiblingCalls2, GraphDiffDeduceSiblingCalls2A,
  375. GraphDiffDeduceSiblingCalls),
  376. ifSpecified(GraphDiffInstrMap2, GraphDiffInstrMap2A, GraphDiffInstrMap),
  377. Trace()}}};
  378. std::array<std::string, 2> Inputs{{GraphDiffInput1, GraphDiffInput2}};
  379. std::array<GraphRenderer::GraphT, 2> Graphs;
  380. for (int i = 0; i < 2; i++) {
  381. auto TraceOrErr = loadTraceFile(Inputs[i], true);
  382. if (!TraceOrErr)
  383. return make_error<StringError>(
  384. Twine("Failed Loading Input File '") + Inputs[i] + "'",
  385. make_error_code(llvm::errc::invalid_argument));
  386. Factories[i].Trace = std::move(*TraceOrErr);
  387. auto GraphRendererOrErr = Factories[i].getGraphRenderer();
  388. if (!GraphRendererOrErr)
  389. return GraphRendererOrErr.takeError();
  390. auto GraphRenderer = *GraphRendererOrErr;
  391. Graphs[i] = GraphRenderer.getGraph();
  392. }
  393. GraphDiffRenderer::Factory DGF(Graphs[0], Graphs[1]);
  394. auto GDROrErr = DGF.getGraphDiffRenderer();
  395. if (!GDROrErr)
  396. return GDROrErr.takeError();
  397. auto &GDR = *GDROrErr;
  398. std::error_code EC;
  399. raw_fd_ostream OS(GraphDiffOutput, EC, sys::fs::OpenFlags::OF_TextWithCRLF);
  400. if (EC)
  401. return make_error<StringError>(
  402. Twine("Cannot open file '") + GraphDiffOutput + "' for writing.", EC);
  403. GDR.exportGraphAsDOT(OS, GraphDiffEdgeLabel, GraphDiffEdgeColor,
  404. GraphDiffVertexLabel, GraphDiffVertexColor,
  405. GraphDiffVertexLabelTrunc);
  406. return Error::success();
  407. });