blob: d865a0b7a03e4ed156b8efa18730a6a76b342fab [file] [log] [blame]
Dean Michael Berrisa0e3ae42018-05-02 00:43:17 +00001//===- xray-converter.cpp: XRay Trace Conversion --------------------------===//
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Implements the trace conversion functions.
10//
11//===----------------------------------------------------------------------===//
12#include "xray-converter.h"
13
Keith Wyss42427992017-11-07 00:28:28 +000014#include "trie-node.h"
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000015#include "xray-registry.h"
16#include "llvm/DebugInfo/Symbolize/Symbolize.h"
17#include "llvm/Support/EndianStream.h"
18#include "llvm/Support/FileSystem.h"
Keith Wyss42427992017-11-07 00:28:28 +000019#include "llvm/Support/FormatVariadic.h"
Pavel Labathd79f6382017-01-16 16:38:23 +000020#include "llvm/Support/ScopedPrinter.h"
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000021#include "llvm/Support/YAMLTraits.h"
22#include "llvm/Support/raw_ostream.h"
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000023#include "llvm/XRay/InstrumentationMap.h"
Dean Michael Berrisd6c18652017-01-11 06:39:09 +000024#include "llvm/XRay/Trace.h"
25#include "llvm/XRay/YAMLXRayRecord.h"
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000026
27using namespace llvm;
28using namespace xray;
29
30// llvm-xray convert
31// ----------------------------------------------------------------------------
32static cl::SubCommand Convert("convert", "Trace Format Conversion");
33static cl::opt<std::string> ConvertInput(cl::Positional,
34 cl::desc("<xray log file>"),
35 cl::Required, cl::sub(Convert));
Keith Wyss42427992017-11-07 00:28:28 +000036enum class ConvertFormats { BINARY, YAML, CHROME_TRACE_EVENT };
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000037static cl::opt<ConvertFormats> ConvertOutputFormat(
38 "output-format", cl::desc("output format"),
39 cl::values(clEnumValN(ConvertFormats::BINARY, "raw", "output in binary"),
Keith Wyss42427992017-11-07 00:28:28 +000040 clEnumValN(ConvertFormats::YAML, "yaml", "output in yaml"),
41 clEnumValN(ConvertFormats::CHROME_TRACE_EVENT, "trace_event",
42 "Output in chrome's trace event format. "
43 "May be visualized with the Catapult trace viewer.")),
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000044 cl::sub(Convert));
45static cl::alias ConvertOutputFormat2("f", cl::aliasopt(ConvertOutputFormat),
46 cl::desc("Alias for -output-format"),
47 cl::sub(Convert));
48static cl::opt<std::string>
49 ConvertOutput("output", cl::value_desc("output file"), cl::init("-"),
50 cl::desc("output file; use '-' for stdout"),
51 cl::sub(Convert));
52static cl::alias ConvertOutput2("o", cl::aliasopt(ConvertOutput),
53 cl::desc("Alias for -output"),
54 cl::sub(Convert));
55
56static cl::opt<bool>
57 ConvertSymbolize("symbolize",
58 cl::desc("symbolize function ids from the input log"),
59 cl::init(false), cl::sub(Convert));
60static cl::alias ConvertSymbolize2("y", cl::aliasopt(ConvertSymbolize),
61 cl::desc("Alias for -symbolize"),
62 cl::sub(Convert));
63
64static cl::opt<std::string>
65 ConvertInstrMap("instr_map",
66 cl::desc("binary with the instrumentation map, or "
67 "a separate instrumentation map"),
68 cl::value_desc("binary with xray_instr_map"),
69 cl::sub(Convert), cl::init(""));
70static cl::alias ConvertInstrMap2("m", cl::aliasopt(ConvertInstrMap),
71 cl::desc("Alias for -instr_map"),
72 cl::sub(Convert));
73static cl::opt<bool> ConvertSortInput(
74 "sort",
75 cl::desc("determines whether to sort input log records by timestamp"),
76 cl::sub(Convert), cl::init(true));
77static cl::alias ConvertSortInput2("s", cl::aliasopt(ConvertSortInput),
78 cl::desc("Alias for -sort"),
79 cl::sub(Convert));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000080
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000081using llvm::yaml::Output;
82
Dean Michael Berrisd6c18652017-01-11 06:39:09 +000083void TraceConverter::exportAsYAML(const Trace &Records, raw_ostream &OS) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000084 YAMLXRayTrace Trace;
85 const auto &FH = Records.getFileHeader();
86 Trace.Header = {FH.Version, FH.Type, FH.ConstantTSC, FH.NonstopTSC,
87 FH.CycleFrequency};
88 Trace.Records.reserve(Records.size());
89 for (const auto &R : Records) {
90 Trace.Records.push_back({R.RecordType, R.CPU, R.Type, R.FuncId,
91 Symbolize ? FuncIdHelper.SymbolOrNumber(R.FuncId)
Pavel Labathd79f6382017-01-16 16:38:23 +000092 : llvm::to_string(R.FuncId),
Dean Michael Berris25f8d202018-11-06 08:51:37 +000093 R.TSC, R.TId, R.PId, R.CallArgs, R.Data});
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000094 }
Dimitry Andric9afed032017-02-14 22:49:49 +000095 Output Out(OS, nullptr, 0);
Dean Michael Berris25f8d202018-11-06 08:51:37 +000096 Out.setWriteDefaultValues(false);
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000097 Out << Trace;
98}
99
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000100void TraceConverter::exportAsRAWv1(const Trace &Records, raw_ostream &OS) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000101 // First write out the file header, in the correct endian-appropriate format
102 // (XRay assumes currently little endian).
Peter Collingbournee3f65292018-05-18 19:46:24 +0000103 support::endian::Writer Writer(OS, support::endianness::little);
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000104 const auto &FH = Records.getFileHeader();
105 Writer.write(FH.Version);
106 Writer.write(FH.Type);
107 uint32_t Bitfield{0};
108 if (FH.ConstantTSC)
109 Bitfield |= 1uL;
110 if (FH.NonstopTSC)
111 Bitfield |= 1uL << 1;
112 Writer.write(Bitfield);
113 Writer.write(FH.CycleFrequency);
114
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);
121
122 // Then write out the rest of the records, still in an endian-appropriate
123 // format.
124 for (const auto &R : Records) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000125 switch (R.Type) {
126 case RecordTypes::ENTER:
Martin Pelikan10c873f2017-09-27 04:48:03 +0000127 case RecordTypes::ENTER_ARG:
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000128 Writer.write(R.RecordType);
129 Writer.write(static_cast<uint8_t>(R.CPU));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000130 Writer.write(uint8_t{0});
131 break;
132 case RecordTypes::EXIT:
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000133 Writer.write(R.RecordType);
134 Writer.write(static_cast<uint8_t>(R.CPU));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000135 Writer.write(uint8_t{1});
136 break;
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000137 case RecordTypes::TAIL_EXIT:
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000138 Writer.write(R.RecordType);
139 Writer.write(static_cast<uint8_t>(R.CPU));
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000140 Writer.write(uint8_t{2});
141 break;
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000142 case RecordTypes::CUSTOM_EVENT:
143 case RecordTypes::TYPED_EVENT:
144 // Skip custom and typed event records for v1 logs.
145 continue;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000146 }
147 Writer.write(R.FuncId);
148 Writer.write(R.TSC);
149 Writer.write(R.TId);
Dean Michael Berris10141262018-07-13 05:38:22 +0000150
151 if (FH.Version >= 3)
152 Writer.write(R.PId);
153 else
154 Writer.write(Padding4B);
155
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000156 Writer.write(Padding4B);
157 Writer.write(Padding4B);
158 }
159}
160
Keith Wyss42427992017-11-07 00:28:28 +0000161namespace {
162
163// A structure that allows building a dictionary of stack ids for the Chrome
164// trace event format.
165struct StackIdData {
166 // Each Stack of function calls has a unique ID.
167 unsigned id;
168
169 // Bookkeeping so that IDs can be maintained uniquely across threads.
170 // Traversal keeps sibling pointers to other threads stacks. This is helpful
171 // to determine when a thread encounters a new stack and should assign a new
172 // unique ID.
173 SmallVector<TrieNode<StackIdData> *, 4> siblings;
174};
175
176using StackTrieNode = TrieNode<StackIdData>;
177
178// A helper function to find the sibling nodes for an encountered function in a
179// thread of execution. Relies on the invariant that each time a new node is
180// traversed in a thread, sibling bidirectional pointers are maintained.
181SmallVector<StackTrieNode *, 4>
182findSiblings(StackTrieNode *parent, int32_t FnId, uint32_t TId,
183 const DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>>
184 &StackRootsByThreadId) {
185
186 SmallVector<StackTrieNode *, 4> Siblings{};
187
188 if (parent == nullptr) {
189 for (auto map_iter : StackRootsByThreadId) {
190 // Only look for siblings in other threads.
191 if (map_iter.first != TId)
192 for (auto node_iter : map_iter.second) {
193 if (node_iter->FuncId == FnId)
194 Siblings.push_back(node_iter);
195 }
196 }
197 return Siblings;
198 }
199
200 for (auto *ParentSibling : parent->ExtraData.siblings)
201 for (auto node_iter : ParentSibling->Callees)
202 if (node_iter->FuncId == FnId)
203 Siblings.push_back(node_iter);
204
205 return Siblings;
206}
207
208// Given a function being invoked in a thread with id TId, finds and returns the
209// StackTrie representing the function call stack. If no node exists, creates
210// the node. Assigns unique IDs to stacks newly encountered among all threads
211// and keeps sibling links up to when creating new nodes.
212StackTrieNode *findOrCreateStackNode(
213 StackTrieNode *Parent, int32_t FuncId, uint32_t TId,
214 DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> &StackRootsByThreadId,
215 DenseMap<unsigned, StackTrieNode *> &StacksByStackId, unsigned *id_counter,
216 std::forward_list<StackTrieNode> &NodeStore) {
217 SmallVector<StackTrieNode *, 4> &ParentCallees =
218 Parent == nullptr ? StackRootsByThreadId[TId] : Parent->Callees;
219 auto match = find_if(ParentCallees, [FuncId](StackTrieNode *ParentCallee) {
220 return FuncId == ParentCallee->FuncId;
221 });
222 if (match != ParentCallees.end())
223 return *match;
224
225 SmallVector<StackTrieNode *, 4> siblings =
226 findSiblings(Parent, FuncId, TId, StackRootsByThreadId);
227 if (siblings.empty()) {
228 NodeStore.push_front({FuncId, Parent, {}, {(*id_counter)++, {}}});
229 StackTrieNode *CurrentStack = &NodeStore.front();
230 StacksByStackId[*id_counter - 1] = CurrentStack;
231 ParentCallees.push_back(CurrentStack);
232 return CurrentStack;
233 }
234 unsigned stack_id = siblings[0]->ExtraData.id;
235 NodeStore.push_front({FuncId, Parent, {}, {stack_id, std::move(siblings)}});
236 StackTrieNode *CurrentStack = &NodeStore.front();
237 for (auto *sibling : CurrentStack->ExtraData.siblings)
238 sibling->ExtraData.siblings.push_back(CurrentStack);
239 ParentCallees.push_back(CurrentStack);
240 return CurrentStack;
241}
242
Roman Lebedev49b6f812019-02-25 07:39:07 +0000243void writeTraceViewerRecord(uint16_t Version, raw_ostream &OS, int32_t FuncId,
244 uint32_t TId, uint32_t PId, bool Symbolize,
245 const FuncIdConversionHelper &FuncIdHelper,
246 double EventTimestampUs,
247 const StackTrieNode &StackCursor,
248 StringRef FunctionPhenotype) {
249 OS << " ";
250 if (Version >= 3) {
251 OS << llvm::formatv(
252 R"({ "name" : "{0}", "ph" : "{1}", "tid" : "{2}", "pid" : "{3}", )"
253 R"("ts" : "{4:f4}", "sf" : "{5}" })",
254 (Symbolize ? FuncIdHelper.SymbolOrNumber(FuncId)
255 : llvm::to_string(FuncId)),
256 FunctionPhenotype, TId, PId, EventTimestampUs,
257 StackCursor.ExtraData.id);
258 } else {
259 OS << llvm::formatv(
260 R"({ "name" : "{0}", "ph" : "{1}", "tid" : "{2}", "pid" : "1", )"
261 R"("ts" : "{3:f3}", "sf" : "{4}" })",
262 (Symbolize ? FuncIdHelper.SymbolOrNumber(FuncId)
263 : llvm::to_string(FuncId)),
264 FunctionPhenotype, TId, EventTimestampUs, StackCursor.ExtraData.id);
265 }
266}
267
Keith Wyss42427992017-11-07 00:28:28 +0000268} // namespace
269
270void TraceConverter::exportAsChromeTraceEventFormat(const Trace &Records,
271 raw_ostream &OS) {
272 const auto &FH = Records.getFileHeader();
Dean Michael Berris10141262018-07-13 05:38:22 +0000273 auto Version = FH.Version;
Keith Wyss42427992017-11-07 00:28:28 +0000274 auto CycleFreq = FH.CycleFrequency;
275
276 unsigned id_counter = 0;
277
Roman Lebedev49b6f812019-02-25 07:39:07 +0000278 OS << "{\n \"traceEvents\": [";
Keith Wyss42427992017-11-07 00:28:28 +0000279 DenseMap<uint32_t, StackTrieNode *> StackCursorByThreadId{};
280 DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> StackRootsByThreadId{};
281 DenseMap<unsigned, StackTrieNode *> StacksByStackId{};
282 std::forward_list<StackTrieNode> NodeStore{};
Roman Lebedev49b6f812019-02-25 07:39:07 +0000283 int loop_count = 0;
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +0000284 for (const auto &R : Records) {
Roman Lebedev49b6f812019-02-25 07:39:07 +0000285 if (loop_count++ == 0)
286 OS << "\n";
287 else
288 OS << ",\n";
289
Keith Wyss42427992017-11-07 00:28:28 +0000290 // Chrome trace event format always wants data in micros.
291 // CyclesPerMicro = CycleHertz / 10^6
292 // TSC / CyclesPerMicro == TSC * 10^6 / CycleHertz == MicroTimestamp
293 // Could lose some precision here by converting the TSC to a double to
294 // multiply by the period in micros. 52 bit mantissa is a good start though.
295 // TODO: Make feature request to Chrome Trace viewer to accept ticks and a
296 // frequency or do some more involved calculation to avoid dangers of
297 // conversion.
298 double EventTimestampUs = double(1000000) / CycleFreq * double(R.TSC);
299 StackTrieNode *&StackCursor = StackCursorByThreadId[R.TId];
300 switch (R.Type) {
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000301 case RecordTypes::CUSTOM_EVENT:
302 case RecordTypes::TYPED_EVENT:
303 // TODO: Support typed and custom event rendering on Chrome Trace Viewer.
304 break;
Keith Wyss42427992017-11-07 00:28:28 +0000305 case RecordTypes::ENTER:
306 case RecordTypes::ENTER_ARG:
307 StackCursor = findOrCreateStackNode(StackCursor, R.FuncId, R.TId,
308 StackRootsByThreadId, StacksByStackId,
309 &id_counter, NodeStore);
310 // Each record is represented as a json dictionary with function name,
Dean Michael Berris10141262018-07-13 05:38:22 +0000311 // type of B for begin or E for end, thread id, process id,
Keith Wyss42427992017-11-07 00:28:28 +0000312 // timestamp in microseconds, and a stack frame id. The ids are logged
313 // in an id dictionary after the events.
Roman Lebedev49b6f812019-02-25 07:39:07 +0000314 writeTraceViewerRecord(Version, OS, R.FuncId, R.TId, R.PId, Symbolize,
315 FuncIdHelper, EventTimestampUs, *StackCursor, "B");
Keith Wyss42427992017-11-07 00:28:28 +0000316 break;
317 case RecordTypes::EXIT:
318 case RecordTypes::TAIL_EXIT:
319 // No entries to record end for.
320 if (StackCursor == nullptr)
321 break;
322 // Should we emit an END record anyway or account this condition?
323 // (And/Or in loop termination below)
324 StackTrieNode *PreviousCursor = nullptr;
325 do {
Roman Lebedev49b6f812019-02-25 07:39:07 +0000326 if (PreviousCursor != nullptr) {
327 OS << ",\n";
328 }
329 writeTraceViewerRecord(Version, OS, StackCursor->FuncId, R.TId, R.PId,
330 Symbolize, FuncIdHelper, EventTimestampUs,
331 *StackCursor, "E");
Keith Wyss42427992017-11-07 00:28:28 +0000332 PreviousCursor = StackCursor;
333 StackCursor = StackCursor->Parent;
334 } while (PreviousCursor->FuncId != R.FuncId && StackCursor != nullptr);
335 break;
336 }
337 }
Roman Lebedev49b6f812019-02-25 07:39:07 +0000338 OS << "\n ],\n"; // Close the Trace Events array.
339 OS << " "
340 << "\"displayTimeUnit\": \"ns\",\n";
Keith Wyss42427992017-11-07 00:28:28 +0000341
342 // The stackFrames dictionary substantially reduces size of the output file by
343 // avoiding repeating the entire call stack of function names for each entry.
Roman Lebedev49b6f812019-02-25 07:39:07 +0000344 OS << R"( "stackFrames": {)";
345 int stack_frame_count = 0;
346 for (auto map_iter : StacksByStackId) {
347 if (stack_frame_count++ == 0)
348 OS << "\n";
349 else
350 OS << ",\n";
351 OS << " ";
352 OS << llvm::formatv(
353 R"("{0}" : { "name" : "{1}")", map_iter.first,
354 (Symbolize ? FuncIdHelper.SymbolOrNumber(map_iter.second->FuncId)
355 : llvm::to_string(map_iter.second->FuncId)));
356 if (map_iter.second->Parent != nullptr)
357 OS << llvm::formatv(R"(, "parent": "{0}")",
358 map_iter.second->Parent->ExtraData.id);
359 OS << " }";
Keith Wyss42427992017-11-07 00:28:28 +0000360 }
Roman Lebedev49b6f812019-02-25 07:39:07 +0000361 OS << "\n }\n"; // Close the stack frames map.
362 OS << "}\n"; // Close the JSON entry.
Keith Wyss42427992017-11-07 00:28:28 +0000363}
364
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000365namespace llvm {
366namespace xray {
367
368static CommandRegistration Unused(&Convert, []() -> Error {
369 // FIXME: Support conversion to BINARY when upgrading XRay trace versions.
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000370 InstrumentationMap Map;
371 if (!ConvertInstrMap.empty()) {
372 auto InstrumentationMapOrError = loadInstrumentationMap(ConvertInstrMap);
373 if (!InstrumentationMapOrError)
374 return joinErrors(make_error<StringError>(
375 Twine("Cannot open instrumentation map '") +
376 ConvertInstrMap + "'",
377 std::make_error_code(std::errc::invalid_argument)),
378 InstrumentationMapOrError.takeError());
379 Map = std::move(*InstrumentationMapOrError);
380 }
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000381
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000382 const auto &FunctionAddresses = Map.getFunctionAddresses();
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000383 symbolize::LLVMSymbolizer::Options Opts(
384 symbolize::FunctionNameKind::LinkageName, true, true, false, "");
385 symbolize::LLVMSymbolizer Symbolizer(Opts);
386 llvm::xray::FuncIdConversionHelper FuncIdHelper(ConvertInstrMap, Symbolizer,
387 FunctionAddresses);
388 llvm::xray::TraceConverter TC(FuncIdHelper, ConvertSymbolize);
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000389 std::error_code EC;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000390 raw_fd_ostream OS(ConvertOutput, EC,
391 ConvertOutputFormat == ConvertFormats::BINARY
392 ? sys::fs::OpenFlags::F_None
393 : sys::fs::OpenFlags::F_Text);
394 if (EC)
395 return make_error<StringError>(
396 Twine("Cannot open file '") + ConvertOutput + "' for writing.", EC);
397
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000398 auto TraceOrErr = loadTraceFile(ConvertInput, ConvertSortInput);
399 if (!TraceOrErr)
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000400 return joinErrors(
401 make_error<StringError>(
402 Twine("Failed loading input file '") + ConvertInput + "'.",
Hans Wennborg84da6612017-01-12 18:33:14 +0000403 std::make_error_code(std::errc::executable_format_error)),
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000404 TraceOrErr.takeError());
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000405
406 auto &T = *TraceOrErr;
407 switch (ConvertOutputFormat) {
408 case ConvertFormats::YAML:
409 TC.exportAsYAML(T, OS);
410 break;
411 case ConvertFormats::BINARY:
412 TC.exportAsRAWv1(T, OS);
413 break;
Keith Wyss42427992017-11-07 00:28:28 +0000414 case ConvertFormats::CHROME_TRACE_EVENT:
415 TC.exportAsChromeTraceEventFormat(T, OS);
416 break;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000417 }
418 return Error::success();
419});
420
421} // namespace xray
422} // namespace llvm