xray-graph-diff.cpp 20 KB

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