xray-converter.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //===- xray-converter.cpp: XRay Trace Conversion --------------------------===//
  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. // Implements the trace conversion functions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "xray-converter.h"
  13. #include "trie-node.h"
  14. #include "xray-registry.h"
  15. #include "llvm/DebugInfo/Symbolize/Symbolize.h"
  16. #include "llvm/Support/EndianStream.h"
  17. #include "llvm/Support/FileSystem.h"
  18. #include "llvm/Support/FormatVariadic.h"
  19. #include "llvm/Support/ScopedPrinter.h"
  20. #include "llvm/Support/YAMLTraits.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include "llvm/XRay/InstrumentationMap.h"
  23. #include "llvm/XRay/Trace.h"
  24. #include "llvm/XRay/YAMLXRayRecord.h"
  25. using namespace llvm;
  26. using namespace xray;
  27. // llvm-xray convert
  28. // ----------------------------------------------------------------------------
  29. static cl::SubCommand Convert("convert", "Trace Format Conversion");
  30. static cl::opt<std::string> ConvertInput(cl::Positional,
  31. cl::desc("<xray log file>"),
  32. cl::Required, cl::sub(Convert));
  33. enum class ConvertFormats { BINARY, YAML, CHROME_TRACE_EVENT };
  34. static cl::opt<ConvertFormats> ConvertOutputFormat(
  35. "output-format", cl::desc("output format"),
  36. cl::values(clEnumValN(ConvertFormats::BINARY, "raw", "output in binary"),
  37. clEnumValN(ConvertFormats::YAML, "yaml", "output in yaml"),
  38. clEnumValN(ConvertFormats::CHROME_TRACE_EVENT, "trace_event",
  39. "Output in chrome's trace event format. "
  40. "May be visualized with the Catapult trace viewer.")),
  41. cl::sub(Convert));
  42. static cl::alias ConvertOutputFormat2("f", cl::aliasopt(ConvertOutputFormat),
  43. cl::desc("Alias for -output-format"));
  44. static cl::opt<std::string>
  45. ConvertOutput("output", cl::value_desc("output file"), cl::init("-"),
  46. cl::desc("output file; use '-' for stdout"),
  47. cl::sub(Convert));
  48. static cl::alias ConvertOutput2("o", cl::aliasopt(ConvertOutput),
  49. cl::desc("Alias for -output"));
  50. static cl::opt<bool>
  51. ConvertSymbolize("symbolize",
  52. cl::desc("symbolize function ids from the input log"),
  53. cl::init(false), cl::sub(Convert));
  54. static cl::alias ConvertSymbolize2("y", cl::aliasopt(ConvertSymbolize),
  55. cl::desc("Alias for -symbolize"));
  56. static cl::opt<std::string>
  57. ConvertInstrMap("instr_map",
  58. cl::desc("binary with the instrumentation map, or "
  59. "a separate instrumentation map"),
  60. cl::value_desc("binary with xray_instr_map"),
  61. cl::sub(Convert), cl::init(""));
  62. static cl::alias ConvertInstrMap2("m", cl::aliasopt(ConvertInstrMap),
  63. cl::desc("Alias for -instr_map"));
  64. static cl::opt<bool> ConvertSortInput(
  65. "sort",
  66. cl::desc("determines whether to sort input log records by timestamp"),
  67. cl::sub(Convert), cl::init(true));
  68. static cl::alias ConvertSortInput2("s", cl::aliasopt(ConvertSortInput),
  69. cl::desc("Alias for -sort"));
  70. using llvm::yaml::Output;
  71. void TraceConverter::exportAsYAML(const Trace &Records, raw_ostream &OS) {
  72. YAMLXRayTrace Trace;
  73. const auto &FH = Records.getFileHeader();
  74. Trace.Header = {FH.Version, FH.Type, FH.ConstantTSC, FH.NonstopTSC,
  75. FH.CycleFrequency};
  76. Trace.Records.reserve(Records.size());
  77. for (const auto &R : Records) {
  78. Trace.Records.push_back({R.RecordType, R.CPU, R.Type, R.FuncId,
  79. Symbolize ? FuncIdHelper.SymbolOrNumber(R.FuncId)
  80. : llvm::to_string(R.FuncId),
  81. R.TSC, R.TId, R.PId, R.CallArgs, R.Data});
  82. }
  83. Output Out(OS, nullptr, 0);
  84. Out.setWriteDefaultValues(false);
  85. Out << Trace;
  86. }
  87. void TraceConverter::exportAsRAWv1(const Trace &Records, raw_ostream &OS) {
  88. // First write out the file header, in the correct endian-appropriate format
  89. // (XRay assumes currently little endian).
  90. support::endian::Writer Writer(OS, support::endianness::little);
  91. const auto &FH = Records.getFileHeader();
  92. Writer.write(FH.Version);
  93. Writer.write(FH.Type);
  94. uint32_t Bitfield{0};
  95. if (FH.ConstantTSC)
  96. Bitfield |= 1uL;
  97. if (FH.NonstopTSC)
  98. Bitfield |= 1uL << 1;
  99. Writer.write(Bitfield);
  100. Writer.write(FH.CycleFrequency);
  101. // There's 16 bytes of padding at the end of the file header.
  102. static constexpr uint32_t Padding4B = 0;
  103. Writer.write(Padding4B);
  104. Writer.write(Padding4B);
  105. Writer.write(Padding4B);
  106. Writer.write(Padding4B);
  107. // Then write out the rest of the records, still in an endian-appropriate
  108. // format.
  109. for (const auto &R : Records) {
  110. switch (R.Type) {
  111. case RecordTypes::ENTER:
  112. case RecordTypes::ENTER_ARG:
  113. Writer.write(R.RecordType);
  114. Writer.write(static_cast<uint8_t>(R.CPU));
  115. Writer.write(uint8_t{0});
  116. break;
  117. case RecordTypes::EXIT:
  118. Writer.write(R.RecordType);
  119. Writer.write(static_cast<uint8_t>(R.CPU));
  120. Writer.write(uint8_t{1});
  121. break;
  122. case RecordTypes::TAIL_EXIT:
  123. Writer.write(R.RecordType);
  124. Writer.write(static_cast<uint8_t>(R.CPU));
  125. Writer.write(uint8_t{2});
  126. break;
  127. case RecordTypes::CUSTOM_EVENT:
  128. case RecordTypes::TYPED_EVENT:
  129. // Skip custom and typed event records for v1 logs.
  130. continue;
  131. }
  132. Writer.write(R.FuncId);
  133. Writer.write(R.TSC);
  134. Writer.write(R.TId);
  135. if (FH.Version >= 3)
  136. Writer.write(R.PId);
  137. else
  138. Writer.write(Padding4B);
  139. Writer.write(Padding4B);
  140. Writer.write(Padding4B);
  141. }
  142. }
  143. namespace {
  144. // A structure that allows building a dictionary of stack ids for the Chrome
  145. // trace event format.
  146. struct StackIdData {
  147. // Each Stack of function calls has a unique ID.
  148. unsigned id;
  149. // Bookkeeping so that IDs can be maintained uniquely across threads.
  150. // Traversal keeps sibling pointers to other threads stacks. This is helpful
  151. // to determine when a thread encounters a new stack and should assign a new
  152. // unique ID.
  153. SmallVector<TrieNode<StackIdData> *, 4> siblings;
  154. };
  155. using StackTrieNode = TrieNode<StackIdData>;
  156. // A helper function to find the sibling nodes for an encountered function in a
  157. // thread of execution. Relies on the invariant that each time a new node is
  158. // traversed in a thread, sibling bidirectional pointers are maintained.
  159. SmallVector<StackTrieNode *, 4>
  160. findSiblings(StackTrieNode *parent, int32_t FnId, uint32_t TId,
  161. const DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>>
  162. &StackRootsByThreadId) {
  163. SmallVector<StackTrieNode *, 4> Siblings{};
  164. if (parent == nullptr) {
  165. for (auto map_iter : StackRootsByThreadId) {
  166. // Only look for siblings in other threads.
  167. if (map_iter.first != TId)
  168. for (auto node_iter : map_iter.second) {
  169. if (node_iter->FuncId == FnId)
  170. Siblings.push_back(node_iter);
  171. }
  172. }
  173. return Siblings;
  174. }
  175. for (auto *ParentSibling : parent->ExtraData.siblings)
  176. for (auto node_iter : ParentSibling->Callees)
  177. if (node_iter->FuncId == FnId)
  178. Siblings.push_back(node_iter);
  179. return Siblings;
  180. }
  181. // Given a function being invoked in a thread with id TId, finds and returns the
  182. // StackTrie representing the function call stack. If no node exists, creates
  183. // the node. Assigns unique IDs to stacks newly encountered among all threads
  184. // and keeps sibling links up to when creating new nodes.
  185. StackTrieNode *findOrCreateStackNode(
  186. StackTrieNode *Parent, int32_t FuncId, uint32_t TId,
  187. DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> &StackRootsByThreadId,
  188. DenseMap<unsigned, StackTrieNode *> &StacksByStackId, unsigned *id_counter,
  189. std::forward_list<StackTrieNode> &NodeStore) {
  190. SmallVector<StackTrieNode *, 4> &ParentCallees =
  191. Parent == nullptr ? StackRootsByThreadId[TId] : Parent->Callees;
  192. auto match = find_if(ParentCallees, [FuncId](StackTrieNode *ParentCallee) {
  193. return FuncId == ParentCallee->FuncId;
  194. });
  195. if (match != ParentCallees.end())
  196. return *match;
  197. SmallVector<StackTrieNode *, 4> siblings =
  198. findSiblings(Parent, FuncId, TId, StackRootsByThreadId);
  199. if (siblings.empty()) {
  200. NodeStore.push_front({FuncId, Parent, {}, {(*id_counter)++, {}}});
  201. StackTrieNode *CurrentStack = &NodeStore.front();
  202. StacksByStackId[*id_counter - 1] = CurrentStack;
  203. ParentCallees.push_back(CurrentStack);
  204. return CurrentStack;
  205. }
  206. unsigned stack_id = siblings[0]->ExtraData.id;
  207. NodeStore.push_front({FuncId, Parent, {}, {stack_id, std::move(siblings)}});
  208. StackTrieNode *CurrentStack = &NodeStore.front();
  209. for (auto *sibling : CurrentStack->ExtraData.siblings)
  210. sibling->ExtraData.siblings.push_back(CurrentStack);
  211. ParentCallees.push_back(CurrentStack);
  212. return CurrentStack;
  213. }
  214. void writeTraceViewerRecord(uint16_t Version, raw_ostream &OS, int32_t FuncId,
  215. uint32_t TId, uint32_t PId, bool Symbolize,
  216. const FuncIdConversionHelper &FuncIdHelper,
  217. double EventTimestampUs,
  218. const StackTrieNode &StackCursor,
  219. StringRef FunctionPhenotype) {
  220. OS << " ";
  221. if (Version >= 3) {
  222. OS << llvm::formatv(
  223. R"({ "name" : "{0}", "ph" : "{1}", "tid" : "{2}", "pid" : "{3}", )"
  224. R"("ts" : "{4:f4}", "sf" : "{5}" })",
  225. (Symbolize ? FuncIdHelper.SymbolOrNumber(FuncId)
  226. : llvm::to_string(FuncId)),
  227. FunctionPhenotype, TId, PId, EventTimestampUs,
  228. StackCursor.ExtraData.id);
  229. } else {
  230. OS << llvm::formatv(
  231. R"({ "name" : "{0}", "ph" : "{1}", "tid" : "{2}", "pid" : "1", )"
  232. R"("ts" : "{3:f3}", "sf" : "{4}" })",
  233. (Symbolize ? FuncIdHelper.SymbolOrNumber(FuncId)
  234. : llvm::to_string(FuncId)),
  235. FunctionPhenotype, TId, EventTimestampUs, StackCursor.ExtraData.id);
  236. }
  237. }
  238. } // namespace
  239. void TraceConverter::exportAsChromeTraceEventFormat(const Trace &Records,
  240. raw_ostream &OS) {
  241. const auto &FH = Records.getFileHeader();
  242. auto Version = FH.Version;
  243. auto CycleFreq = FH.CycleFrequency;
  244. unsigned id_counter = 0;
  245. OS << "{\n \"traceEvents\": [";
  246. DenseMap<uint32_t, StackTrieNode *> StackCursorByThreadId{};
  247. DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> StackRootsByThreadId{};
  248. DenseMap<unsigned, StackTrieNode *> StacksByStackId{};
  249. std::forward_list<StackTrieNode> NodeStore{};
  250. int loop_count = 0;
  251. for (const auto &R : Records) {
  252. if (loop_count++ == 0)
  253. OS << "\n";
  254. else
  255. OS << ",\n";
  256. // Chrome trace event format always wants data in micros.
  257. // CyclesPerMicro = CycleHertz / 10^6
  258. // TSC / CyclesPerMicro == TSC * 10^6 / CycleHertz == MicroTimestamp
  259. // Could lose some precision here by converting the TSC to a double to
  260. // multiply by the period in micros. 52 bit mantissa is a good start though.
  261. // TODO: Make feature request to Chrome Trace viewer to accept ticks and a
  262. // frequency or do some more involved calculation to avoid dangers of
  263. // conversion.
  264. double EventTimestampUs = double(1000000) / CycleFreq * double(R.TSC);
  265. StackTrieNode *&StackCursor = StackCursorByThreadId[R.TId];
  266. switch (R.Type) {
  267. case RecordTypes::CUSTOM_EVENT:
  268. case RecordTypes::TYPED_EVENT:
  269. // TODO: Support typed and custom event rendering on Chrome Trace Viewer.
  270. break;
  271. case RecordTypes::ENTER:
  272. case RecordTypes::ENTER_ARG:
  273. StackCursor = findOrCreateStackNode(StackCursor, R.FuncId, R.TId,
  274. StackRootsByThreadId, StacksByStackId,
  275. &id_counter, NodeStore);
  276. // Each record is represented as a json dictionary with function name,
  277. // type of B for begin or E for end, thread id, process id,
  278. // timestamp in microseconds, and a stack frame id. The ids are logged
  279. // in an id dictionary after the events.
  280. writeTraceViewerRecord(Version, OS, R.FuncId, R.TId, R.PId, Symbolize,
  281. FuncIdHelper, EventTimestampUs, *StackCursor, "B");
  282. break;
  283. case RecordTypes::EXIT:
  284. case RecordTypes::TAIL_EXIT:
  285. // No entries to record end for.
  286. if (StackCursor == nullptr)
  287. break;
  288. // Should we emit an END record anyway or account this condition?
  289. // (And/Or in loop termination below)
  290. StackTrieNode *PreviousCursor = nullptr;
  291. do {
  292. if (PreviousCursor != nullptr) {
  293. OS << ",\n";
  294. }
  295. writeTraceViewerRecord(Version, OS, StackCursor->FuncId, R.TId, R.PId,
  296. Symbolize, FuncIdHelper, EventTimestampUs,
  297. *StackCursor, "E");
  298. PreviousCursor = StackCursor;
  299. StackCursor = StackCursor->Parent;
  300. } while (PreviousCursor->FuncId != R.FuncId && StackCursor != nullptr);
  301. break;
  302. }
  303. }
  304. OS << "\n ],\n"; // Close the Trace Events array.
  305. OS << " "
  306. << "\"displayTimeUnit\": \"ns\",\n";
  307. // The stackFrames dictionary substantially reduces size of the output file by
  308. // avoiding repeating the entire call stack of function names for each entry.
  309. OS << R"( "stackFrames": {)";
  310. int stack_frame_count = 0;
  311. for (auto map_iter : StacksByStackId) {
  312. if (stack_frame_count++ == 0)
  313. OS << "\n";
  314. else
  315. OS << ",\n";
  316. OS << " ";
  317. OS << llvm::formatv(
  318. R"("{0}" : { "name" : "{1}")", map_iter.first,
  319. (Symbolize ? FuncIdHelper.SymbolOrNumber(map_iter.second->FuncId)
  320. : llvm::to_string(map_iter.second->FuncId)));
  321. if (map_iter.second->Parent != nullptr)
  322. OS << llvm::formatv(R"(, "parent": "{0}")",
  323. map_iter.second->Parent->ExtraData.id);
  324. OS << " }";
  325. }
  326. OS << "\n }\n"; // Close the stack frames map.
  327. OS << "}\n"; // Close the JSON entry.
  328. }
  329. namespace llvm {
  330. namespace xray {
  331. static CommandRegistration Unused(&Convert, []() -> Error {
  332. // FIXME: Support conversion to BINARY when upgrading XRay trace versions.
  333. InstrumentationMap Map;
  334. if (!ConvertInstrMap.empty()) {
  335. auto InstrumentationMapOrError = loadInstrumentationMap(ConvertInstrMap);
  336. if (!InstrumentationMapOrError)
  337. return joinErrors(make_error<StringError>(
  338. Twine("Cannot open instrumentation map '") +
  339. ConvertInstrMap + "'",
  340. std::make_error_code(std::errc::invalid_argument)),
  341. InstrumentationMapOrError.takeError());
  342. Map = std::move(*InstrumentationMapOrError);
  343. }
  344. const auto &FunctionAddresses = Map.getFunctionAddresses();
  345. symbolize::LLVMSymbolizer Symbolizer;
  346. llvm::xray::FuncIdConversionHelper FuncIdHelper(ConvertInstrMap, Symbolizer,
  347. FunctionAddresses);
  348. llvm::xray::TraceConverter TC(FuncIdHelper, ConvertSymbolize);
  349. std::error_code EC;
  350. raw_fd_ostream OS(ConvertOutput, EC,
  351. ConvertOutputFormat == ConvertFormats::BINARY
  352. ? sys::fs::OpenFlags::OF_None
  353. : sys::fs::OpenFlags::OF_Text);
  354. if (EC)
  355. return make_error<StringError>(
  356. Twine("Cannot open file '") + ConvertOutput + "' for writing.", EC);
  357. auto TraceOrErr = loadTraceFile(ConvertInput, ConvertSortInput);
  358. if (!TraceOrErr)
  359. return joinErrors(
  360. make_error<StringError>(
  361. Twine("Failed loading input file '") + ConvertInput + "'.",
  362. std::make_error_code(std::errc::executable_format_error)),
  363. TraceOrErr.takeError());
  364. auto &T = *TraceOrErr;
  365. switch (ConvertOutputFormat) {
  366. case ConvertFormats::YAML:
  367. TC.exportAsYAML(T, OS);
  368. break;
  369. case ConvertFormats::BINARY:
  370. TC.exportAsRAWv1(T, OS);
  371. break;
  372. case ConvertFormats::CHROME_TRACE_EVENT:
  373. TC.exportAsChromeTraceEventFormat(T, OS);
  374. break;
  375. }
  376. return Error::success();
  377. });
  378. } // namespace xray
  379. } // namespace llvm