1 //===- xray-converter.cc - XRay Trace Conversion --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements the trace conversion functions.
12 //===----------------------------------------------------------------------===//
13 #include "xray-converter.h"
15 #include "trie-node.h"
16 #include "xray-registry.h"
17 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
18 #include "llvm/Support/EndianStream.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/FormatVariadic.h"
21 #include "llvm/Support/ScopedPrinter.h"
22 #include "llvm/Support/YAMLTraits.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/XRay/InstrumentationMap.h"
25 #include "llvm/XRay/Trace.h"
26 #include "llvm/XRay/YAMLXRayRecord.h"
32 // ----------------------------------------------------------------------------
33 static cl::SubCommand Convert("convert", "Trace Format Conversion");
34 static cl::opt<std::string> ConvertInput(cl::Positional,
35 cl::desc("<xray log file>"),
36 cl::Required, cl::sub(Convert));
37 enum class ConvertFormats { BINARY, YAML, CHROME_TRACE_EVENT };
38 static cl::opt<ConvertFormats> ConvertOutputFormat(
39 "output-format", cl::desc("output format"),
40 cl::values(clEnumValN(ConvertFormats::BINARY, "raw", "output in binary"),
41 clEnumValN(ConvertFormats::YAML, "yaml", "output in yaml"),
42 clEnumValN(ConvertFormats::CHROME_TRACE_EVENT, "trace_event",
43 "Output in chrome's trace event format. "
44 "May be visualized with the Catapult trace viewer.")),
46 static cl::alias ConvertOutputFormat2("f", cl::aliasopt(ConvertOutputFormat),
47 cl::desc("Alias for -output-format"),
49 static cl::opt<std::string>
50 ConvertOutput("output", cl::value_desc("output file"), cl::init("-"),
51 cl::desc("output file; use '-' for stdout"),
53 static cl::alias ConvertOutput2("o", cl::aliasopt(ConvertOutput),
54 cl::desc("Alias for -output"),
58 ConvertSymbolize("symbolize",
59 cl::desc("symbolize function ids from the input log"),
60 cl::init(false), cl::sub(Convert));
61 static cl::alias ConvertSymbolize2("y", cl::aliasopt(ConvertSymbolize),
62 cl::desc("Alias for -symbolize"),
65 static cl::opt<std::string>
66 ConvertInstrMap("instr_map",
67 cl::desc("binary with the instrumentation map, or "
68 "a separate instrumentation map"),
69 cl::value_desc("binary with xray_instr_map"),
70 cl::sub(Convert), cl::init(""));
71 static cl::alias ConvertInstrMap2("m", cl::aliasopt(ConvertInstrMap),
72 cl::desc("Alias for -instr_map"),
74 static cl::opt<bool> ConvertSortInput(
76 cl::desc("determines whether to sort input log records by timestamp"),
77 cl::sub(Convert), cl::init(true));
78 static cl::alias ConvertSortInput2("s", cl::aliasopt(ConvertSortInput),
79 cl::desc("Alias for -sort"),
82 using llvm::yaml::Output;
84 void TraceConverter::exportAsYAML(const Trace &Records, raw_ostream &OS) {
86 const auto &FH = Records.getFileHeader();
87 Trace.Header = {FH.Version, FH.Type, FH.ConstantTSC, FH.NonstopTSC,
89 Trace.Records.reserve(Records.size());
90 for (const auto &R : Records) {
91 Trace.Records.push_back({R.RecordType, R.CPU, R.Type, R.FuncId,
92 Symbolize ? FuncIdHelper.SymbolOrNumber(R.FuncId)
93 : llvm::to_string(R.FuncId),
94 R.TSC, R.TId, R.CallArgs});
96 Output Out(OS, nullptr, 0);
100 void TraceConverter::exportAsRAWv1(const Trace &Records, raw_ostream &OS) {
101 // First write out the file header, in the correct endian-appropriate format
102 // (XRay assumes currently little endian).
103 support::endian::Writer<support::endianness::little> Writer(OS);
104 const auto &FH = Records.getFileHeader();
105 Writer.write(FH.Version);
106 Writer.write(FH.Type);
107 uint32_t Bitfield{0};
111 Bitfield |= 1uL << 1;
112 Writer.write(Bitfield);
113 Writer.write(FH.CycleFrequency);
115 // There's 16 bytes of padding at the end of the file header.
116 static constexpr uint32_t Padding4B = 0;
117 Writer.write(Padding4B);
118 Writer.write(Padding4B);
119 Writer.write(Padding4B);
120 Writer.write(Padding4B);
122 // Then write out the rest of the records, still in an endian-appropriate
124 for (const auto &R : Records) {
125 Writer.write(R.RecordType);
126 // The on disk naive raw format uses 8 bit CPUs, but the record has 16.
127 // There's no choice but truncation.
128 Writer.write(static_cast<uint8_t>(R.CPU));
130 case RecordTypes::ENTER:
131 case RecordTypes::ENTER_ARG:
132 Writer.write(uint8_t{0});
134 case RecordTypes::EXIT:
135 Writer.write(uint8_t{1});
137 case RecordTypes::TAIL_EXIT:
138 Writer.write(uint8_t{2});
141 Writer.write(R.FuncId);
144 Writer.write(Padding4B);
145 Writer.write(Padding4B);
146 Writer.write(Padding4B);
152 // A structure that allows building a dictionary of stack ids for the Chrome
153 // trace event format.
155 // Each Stack of function calls has a unique ID.
158 // Bookkeeping so that IDs can be maintained uniquely across threads.
159 // Traversal keeps sibling pointers to other threads stacks. This is helpful
160 // to determine when a thread encounters a new stack and should assign a new
162 SmallVector<TrieNode<StackIdData> *, 4> siblings;
165 using StackTrieNode = TrieNode<StackIdData>;
167 // A helper function to find the sibling nodes for an encountered function in a
168 // thread of execution. Relies on the invariant that each time a new node is
169 // traversed in a thread, sibling bidirectional pointers are maintained.
170 SmallVector<StackTrieNode *, 4>
171 findSiblings(StackTrieNode *parent, int32_t FnId, uint32_t TId,
172 const DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>>
173 &StackRootsByThreadId) {
175 SmallVector<StackTrieNode *, 4> Siblings{};
177 if (parent == nullptr) {
178 for (auto map_iter : StackRootsByThreadId) {
179 // Only look for siblings in other threads.
180 if (map_iter.first != TId)
181 for (auto node_iter : map_iter.second) {
182 if (node_iter->FuncId == FnId)
183 Siblings.push_back(node_iter);
189 for (auto *ParentSibling : parent->ExtraData.siblings)
190 for (auto node_iter : ParentSibling->Callees)
191 if (node_iter->FuncId == FnId)
192 Siblings.push_back(node_iter);
197 // Given a function being invoked in a thread with id TId, finds and returns the
198 // StackTrie representing the function call stack. If no node exists, creates
199 // the node. Assigns unique IDs to stacks newly encountered among all threads
200 // and keeps sibling links up to when creating new nodes.
201 StackTrieNode *findOrCreateStackNode(
202 StackTrieNode *Parent, int32_t FuncId, uint32_t TId,
203 DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> &StackRootsByThreadId,
204 DenseMap<unsigned, StackTrieNode *> &StacksByStackId, unsigned *id_counter,
205 std::forward_list<StackTrieNode> &NodeStore) {
206 SmallVector<StackTrieNode *, 4> &ParentCallees =
207 Parent == nullptr ? StackRootsByThreadId[TId] : Parent->Callees;
208 auto match = find_if(ParentCallees, [FuncId](StackTrieNode *ParentCallee) {
209 return FuncId == ParentCallee->FuncId;
211 if (match != ParentCallees.end())
214 SmallVector<StackTrieNode *, 4> siblings =
215 findSiblings(Parent, FuncId, TId, StackRootsByThreadId);
216 if (siblings.empty()) {
217 NodeStore.push_front({FuncId, Parent, {}, {(*id_counter)++, {}}});
218 StackTrieNode *CurrentStack = &NodeStore.front();
219 StacksByStackId[*id_counter - 1] = CurrentStack;
220 ParentCallees.push_back(CurrentStack);
223 unsigned stack_id = siblings[0]->ExtraData.id;
224 NodeStore.push_front({FuncId, Parent, {}, {stack_id, std::move(siblings)}});
225 StackTrieNode *CurrentStack = &NodeStore.front();
226 for (auto *sibling : CurrentStack->ExtraData.siblings)
227 sibling->ExtraData.siblings.push_back(CurrentStack);
228 ParentCallees.push_back(CurrentStack);
232 void writeTraceViewerRecord(raw_ostream &OS, int32_t FuncId, uint32_t TId,
234 const FuncIdConversionHelper &FuncIdHelper,
235 double EventTimestampUs,
236 const StackTrieNode &StackCursor,
237 StringRef FunctionPhenotype) {
240 R"({ "name" : "{0}", "ph" : "{1}", "tid" : "{2}", "pid" : "1", )"
241 R"("ts" : "{3:f3}", "sf" : "{4}" })",
242 (Symbolize ? FuncIdHelper.SymbolOrNumber(FuncId)
243 : llvm::to_string(FuncId)),
244 FunctionPhenotype, TId, EventTimestampUs, StackCursor.ExtraData.id);
249 void TraceConverter::exportAsChromeTraceEventFormat(const Trace &Records,
251 const auto &FH = Records.getFileHeader();
252 auto CycleFreq = FH.CycleFrequency;
254 unsigned id_counter = 0;
256 OS << "{\n \"traceEvents\": [";
257 DenseMap<uint32_t, StackTrieNode *> StackCursorByThreadId{};
258 DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> StackRootsByThreadId{};
259 DenseMap<unsigned, StackTrieNode *> StacksByStackId{};
260 std::forward_list<StackTrieNode> NodeStore{};
262 for (const auto &R : Records) {
263 if (loop_count++ == 0)
268 // Chrome trace event format always wants data in micros.
269 // CyclesPerMicro = CycleHertz / 10^6
270 // TSC / CyclesPerMicro == TSC * 10^6 / CycleHertz == MicroTimestamp
271 // Could lose some precision here by converting the TSC to a double to
272 // multiply by the period in micros. 52 bit mantissa is a good start though.
273 // TODO: Make feature request to Chrome Trace viewer to accept ticks and a
274 // frequency or do some more involved calculation to avoid dangers of
276 double EventTimestampUs = double(1000000) / CycleFreq * double(R.TSC);
277 StackTrieNode *&StackCursor = StackCursorByThreadId[R.TId];
279 case RecordTypes::ENTER:
280 case RecordTypes::ENTER_ARG:
281 StackCursor = findOrCreateStackNode(StackCursor, R.FuncId, R.TId,
282 StackRootsByThreadId, StacksByStackId,
283 &id_counter, NodeStore);
284 // Each record is represented as a json dictionary with function name,
285 // type of B for begin or E for end, thread id, process id (faked),
286 // timestamp in microseconds, and a stack frame id. The ids are logged
287 // in an id dictionary after the events.
288 writeTraceViewerRecord(OS, R.FuncId, R.TId, Symbolize, FuncIdHelper,
289 EventTimestampUs, *StackCursor, "B");
291 case RecordTypes::EXIT:
292 case RecordTypes::TAIL_EXIT:
293 // No entries to record end for.
294 if (StackCursor == nullptr)
296 // Should we emit an END record anyway or account this condition?
297 // (And/Or in loop termination below)
298 StackTrieNode *PreviousCursor = nullptr;
300 writeTraceViewerRecord(OS, StackCursor->FuncId, R.TId, Symbolize,
301 FuncIdHelper, EventTimestampUs, *StackCursor,
303 PreviousCursor = StackCursor;
304 StackCursor = StackCursor->Parent;
305 } while (PreviousCursor->FuncId != R.FuncId && StackCursor != nullptr);
309 OS << "\n ],\n"; // Close the Trace Events array.
311 << "\"displayTimeUnit\": \"ns\",\n";
313 // The stackFrames dictionary substantially reduces size of the output file by
314 // avoiding repeating the entire call stack of function names for each entry.
315 OS << R"( "stackFrames": {)";
316 int stack_frame_count = 0;
317 for (auto map_iter : StacksByStackId) {
318 if (stack_frame_count++ == 0)
324 R"("{0}" : { "name" : "{1}")", map_iter.first,
325 (Symbolize ? FuncIdHelper.SymbolOrNumber(map_iter.second->FuncId)
326 : llvm::to_string(map_iter.second->FuncId)));
327 if (map_iter.second->Parent != nullptr)
328 OS << llvm::formatv(R"(, "parent": "{0}")",
329 map_iter.second->Parent->ExtraData.id);
332 OS << "\n }\n"; // Close the stack frames map.
333 OS << "}\n"; // Close the JSON entry.
339 static CommandRegistration Unused(&Convert, []() -> Error {
340 // FIXME: Support conversion to BINARY when upgrading XRay trace versions.
341 InstrumentationMap Map;
342 if (!ConvertInstrMap.empty()) {
343 auto InstrumentationMapOrError = loadInstrumentationMap(ConvertInstrMap);
344 if (!InstrumentationMapOrError)
345 return joinErrors(make_error<StringError>(
346 Twine("Cannot open instrumentation map '") +
347 ConvertInstrMap + "'",
348 std::make_error_code(std::errc::invalid_argument)),
349 InstrumentationMapOrError.takeError());
350 Map = std::move(*InstrumentationMapOrError);
353 const auto &FunctionAddresses = Map.getFunctionAddresses();
354 symbolize::LLVMSymbolizer::Options Opts(
355 symbolize::FunctionNameKind::LinkageName, true, true, false, "");
356 symbolize::LLVMSymbolizer Symbolizer(Opts);
357 llvm::xray::FuncIdConversionHelper FuncIdHelper(ConvertInstrMap, Symbolizer,
359 llvm::xray::TraceConverter TC(FuncIdHelper, ConvertSymbolize);
361 raw_fd_ostream OS(ConvertOutput, EC,
362 ConvertOutputFormat == ConvertFormats::BINARY
363 ? sys::fs::OpenFlags::F_None
364 : sys::fs::OpenFlags::F_Text);
366 return make_error<StringError>(
367 Twine("Cannot open file '") + ConvertOutput + "' for writing.", EC);
369 auto TraceOrErr = loadTraceFile(ConvertInput, ConvertSortInput);
372 make_error<StringError>(
373 Twine("Failed loading input file '") + ConvertInput + "'.",
374 std::make_error_code(std::errc::executable_format_error)),
375 TraceOrErr.takeError());
377 auto &T = *TraceOrErr;
378 switch (ConvertOutputFormat) {
379 case ConvertFormats::YAML:
380 TC.exportAsYAML(T, OS);
382 case ConvertFormats::BINARY:
383 TC.exportAsRAWv1(T, OS);
385 case ConvertFormats::CHROME_TRACE_EVENT:
386 TC.exportAsChromeTraceEventFormat(T, OS);
389 return Error::success();