blob: 3f153b99bc9366adfc710f3cc518bbcd678602f7 [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implements the trace conversion functions.
11//
12//===----------------------------------------------------------------------===//
13#include "xray-converter.h"
14
Keith Wyss42427992017-11-07 00:28:28 +000015#include "trie-node.h"
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000016#include "xray-registry.h"
17#include "llvm/DebugInfo/Symbolize/Symbolize.h"
18#include "llvm/Support/EndianStream.h"
19#include "llvm/Support/FileSystem.h"
Keith Wyss42427992017-11-07 00:28:28 +000020#include "llvm/Support/FormatVariadic.h"
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +000021#include "llvm/Support/JSON.h"
Pavel Labathd79f6382017-01-16 16:38:23 +000022#include "llvm/Support/ScopedPrinter.h"
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000023#include "llvm/Support/YAMLTraits.h"
24#include "llvm/Support/raw_ostream.h"
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000025#include "llvm/XRay/InstrumentationMap.h"
Dean Michael Berrisd6c18652017-01-11 06:39:09 +000026#include "llvm/XRay/Trace.h"
27#include "llvm/XRay/YAMLXRayRecord.h"
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000028
29using namespace llvm;
30using namespace xray;
31
32// llvm-xray convert
33// ----------------------------------------------------------------------------
34static cl::SubCommand Convert("convert", "Trace Format Conversion");
35static cl::opt<std::string> ConvertInput(cl::Positional,
36 cl::desc("<xray log file>"),
37 cl::Required, cl::sub(Convert));
Keith Wyss42427992017-11-07 00:28:28 +000038enum class ConvertFormats { BINARY, YAML, CHROME_TRACE_EVENT };
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000039static cl::opt<ConvertFormats> ConvertOutputFormat(
40 "output-format", cl::desc("output format"),
41 cl::values(clEnumValN(ConvertFormats::BINARY, "raw", "output in binary"),
Keith Wyss42427992017-11-07 00:28:28 +000042 clEnumValN(ConvertFormats::YAML, "yaml", "output in yaml"),
43 clEnumValN(ConvertFormats::CHROME_TRACE_EVENT, "trace_event",
44 "Output in chrome's trace event format. "
45 "May be visualized with the Catapult trace viewer.")),
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000046 cl::sub(Convert));
47static cl::alias ConvertOutputFormat2("f", cl::aliasopt(ConvertOutputFormat),
48 cl::desc("Alias for -output-format"),
49 cl::sub(Convert));
50static cl::opt<std::string>
51 ConvertOutput("output", cl::value_desc("output file"), cl::init("-"),
52 cl::desc("output file; use '-' for stdout"),
53 cl::sub(Convert));
54static cl::alias ConvertOutput2("o", cl::aliasopt(ConvertOutput),
55 cl::desc("Alias for -output"),
56 cl::sub(Convert));
57
58static cl::opt<bool>
59 ConvertSymbolize("symbolize",
60 cl::desc("symbolize function ids from the input log"),
61 cl::init(false), cl::sub(Convert));
62static cl::alias ConvertSymbolize2("y", cl::aliasopt(ConvertSymbolize),
63 cl::desc("Alias for -symbolize"),
64 cl::sub(Convert));
65
66static cl::opt<std::string>
67 ConvertInstrMap("instr_map",
68 cl::desc("binary with the instrumentation map, or "
69 "a separate instrumentation map"),
70 cl::value_desc("binary with xray_instr_map"),
71 cl::sub(Convert), cl::init(""));
72static cl::alias ConvertInstrMap2("m", cl::aliasopt(ConvertInstrMap),
73 cl::desc("Alias for -instr_map"),
74 cl::sub(Convert));
75static cl::opt<bool> ConvertSortInput(
76 "sort",
77 cl::desc("determines whether to sort input log records by timestamp"),
78 cl::sub(Convert), cl::init(true));
79static cl::alias ConvertSortInput2("s", cl::aliasopt(ConvertSortInput),
80 cl::desc("Alias for -sort"),
81 cl::sub(Convert));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000082
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000083using llvm::yaml::Output;
84
Dean Michael Berrisd6c18652017-01-11 06:39:09 +000085void TraceConverter::exportAsYAML(const Trace &Records, raw_ostream &OS) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000086 YAMLXRayTrace Trace;
87 const auto &FH = Records.getFileHeader();
88 Trace.Header = {FH.Version, FH.Type, FH.ConstantTSC, FH.NonstopTSC,
89 FH.CycleFrequency};
90 Trace.Records.reserve(Records.size());
91 for (const auto &R : Records) {
92 Trace.Records.push_back({R.RecordType, R.CPU, R.Type, R.FuncId,
93 Symbolize ? FuncIdHelper.SymbolOrNumber(R.FuncId)
Pavel Labathd79f6382017-01-16 16:38:23 +000094 : llvm::to_string(R.FuncId),
Dean Michael Berris25f8d202018-11-06 08:51:37 +000095 R.TSC, R.TId, R.PId, R.CallArgs, R.Data});
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000096 }
Dimitry Andric9afed032017-02-14 22:49:49 +000097 Output Out(OS, nullptr, 0);
Dean Michael Berris25f8d202018-11-06 08:51:37 +000098 Out.setWriteDefaultValues(false);
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000099 Out << Trace;
100}
101
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000102void TraceConverter::exportAsRAWv1(const Trace &Records, raw_ostream &OS) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000103 // First write out the file header, in the correct endian-appropriate format
104 // (XRay assumes currently little endian).
Peter Collingbournee3f65292018-05-18 19:46:24 +0000105 support::endian::Writer Writer(OS, support::endianness::little);
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000106 const auto &FH = Records.getFileHeader();
107 Writer.write(FH.Version);
108 Writer.write(FH.Type);
109 uint32_t Bitfield{0};
110 if (FH.ConstantTSC)
111 Bitfield |= 1uL;
112 if (FH.NonstopTSC)
113 Bitfield |= 1uL << 1;
114 Writer.write(Bitfield);
115 Writer.write(FH.CycleFrequency);
116
117 // There's 16 bytes of padding at the end of the file header.
118 static constexpr uint32_t Padding4B = 0;
119 Writer.write(Padding4B);
120 Writer.write(Padding4B);
121 Writer.write(Padding4B);
122 Writer.write(Padding4B);
123
124 // Then write out the rest of the records, still in an endian-appropriate
125 // format.
126 for (const auto &R : Records) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000127 switch (R.Type) {
128 case RecordTypes::ENTER:
Martin Pelikan10c873f2017-09-27 04:48:03 +0000129 case RecordTypes::ENTER_ARG:
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000130 Writer.write(R.RecordType);
131 Writer.write(static_cast<uint8_t>(R.CPU));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000132 Writer.write(uint8_t{0});
133 break;
134 case RecordTypes::EXIT:
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000135 Writer.write(R.RecordType);
136 Writer.write(static_cast<uint8_t>(R.CPU));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000137 Writer.write(uint8_t{1});
138 break;
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000139 case RecordTypes::TAIL_EXIT:
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000140 Writer.write(R.RecordType);
141 Writer.write(static_cast<uint8_t>(R.CPU));
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000142 Writer.write(uint8_t{2});
143 break;
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000144 case RecordTypes::CUSTOM_EVENT:
145 case RecordTypes::TYPED_EVENT:
146 // Skip custom and typed event records for v1 logs.
147 continue;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000148 }
149 Writer.write(R.FuncId);
150 Writer.write(R.TSC);
151 Writer.write(R.TId);
Dean Michael Berris10141262018-07-13 05:38:22 +0000152
153 if (FH.Version >= 3)
154 Writer.write(R.PId);
155 else
156 Writer.write(Padding4B);
157
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000158 Writer.write(Padding4B);
159 Writer.write(Padding4B);
160 }
161}
162
Keith Wyss42427992017-11-07 00:28:28 +0000163namespace {
164
165// A structure that allows building a dictionary of stack ids for the Chrome
166// trace event format.
167struct StackIdData {
168 // Each Stack of function calls has a unique ID.
169 unsigned id;
170
171 // Bookkeeping so that IDs can be maintained uniquely across threads.
172 // Traversal keeps sibling pointers to other threads stacks. This is helpful
173 // to determine when a thread encounters a new stack and should assign a new
174 // unique ID.
175 SmallVector<TrieNode<StackIdData> *, 4> siblings;
176};
177
178using StackTrieNode = TrieNode<StackIdData>;
179
180// A helper function to find the sibling nodes for an encountered function in a
181// thread of execution. Relies on the invariant that each time a new node is
182// traversed in a thread, sibling bidirectional pointers are maintained.
183SmallVector<StackTrieNode *, 4>
184findSiblings(StackTrieNode *parent, int32_t FnId, uint32_t TId,
185 const DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>>
186 &StackRootsByThreadId) {
187
188 SmallVector<StackTrieNode *, 4> Siblings{};
189
190 if (parent == nullptr) {
191 for (auto map_iter : StackRootsByThreadId) {
192 // Only look for siblings in other threads.
193 if (map_iter.first != TId)
194 for (auto node_iter : map_iter.second) {
195 if (node_iter->FuncId == FnId)
196 Siblings.push_back(node_iter);
197 }
198 }
199 return Siblings;
200 }
201
202 for (auto *ParentSibling : parent->ExtraData.siblings)
203 for (auto node_iter : ParentSibling->Callees)
204 if (node_iter->FuncId == FnId)
205 Siblings.push_back(node_iter);
206
207 return Siblings;
208}
209
210// Given a function being invoked in a thread with id TId, finds and returns the
211// StackTrie representing the function call stack. If no node exists, creates
212// the node. Assigns unique IDs to stacks newly encountered among all threads
213// and keeps sibling links up to when creating new nodes.
214StackTrieNode *findOrCreateStackNode(
215 StackTrieNode *Parent, int32_t FuncId, uint32_t TId,
216 DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> &StackRootsByThreadId,
217 DenseMap<unsigned, StackTrieNode *> &StacksByStackId, unsigned *id_counter,
218 std::forward_list<StackTrieNode> &NodeStore) {
219 SmallVector<StackTrieNode *, 4> &ParentCallees =
220 Parent == nullptr ? StackRootsByThreadId[TId] : Parent->Callees;
221 auto match = find_if(ParentCallees, [FuncId](StackTrieNode *ParentCallee) {
222 return FuncId == ParentCallee->FuncId;
223 });
224 if (match != ParentCallees.end())
225 return *match;
226
227 SmallVector<StackTrieNode *, 4> siblings =
228 findSiblings(Parent, FuncId, TId, StackRootsByThreadId);
229 if (siblings.empty()) {
230 NodeStore.push_front({FuncId, Parent, {}, {(*id_counter)++, {}}});
231 StackTrieNode *CurrentStack = &NodeStore.front();
232 StacksByStackId[*id_counter - 1] = CurrentStack;
233 ParentCallees.push_back(CurrentStack);
234 return CurrentStack;
235 }
236 unsigned stack_id = siblings[0]->ExtraData.id;
237 NodeStore.push_front({FuncId, Parent, {}, {stack_id, std::move(siblings)}});
238 StackTrieNode *CurrentStack = &NodeStore.front();
239 for (auto *sibling : CurrentStack->ExtraData.siblings)
240 sibling->ExtraData.siblings.push_back(CurrentStack);
241 ParentCallees.push_back(CurrentStack);
242 return CurrentStack;
243}
244
Keith Wyss42427992017-11-07 00:28:28 +0000245} // namespace
246
247void TraceConverter::exportAsChromeTraceEventFormat(const Trace &Records,
248 raw_ostream &OS) {
249 const auto &FH = Records.getFileHeader();
Dean Michael Berris10141262018-07-13 05:38:22 +0000250 auto Version = FH.Version;
Keith Wyss42427992017-11-07 00:28:28 +0000251 auto CycleFreq = FH.CycleFrequency;
252
253 unsigned id_counter = 0;
254
Keith Wyss42427992017-11-07 00:28:28 +0000255 DenseMap<uint32_t, StackTrieNode *> StackCursorByThreadId{};
256 DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> StackRootsByThreadId{};
257 DenseMap<unsigned, StackTrieNode *> StacksByStackId{};
258 std::forward_list<StackTrieNode> NodeStore{};
Keith Wyss42427992017-11-07 00:28:28 +0000259
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +0000260 // Create a JSON Array which will hold all trace events.
261 json::Array TraceEvents;
262 for (const auto &R : Records) {
Keith Wyss42427992017-11-07 00:28:28 +0000263 // Chrome trace event format always wants data in micros.
264 // CyclesPerMicro = CycleHertz / 10^6
265 // TSC / CyclesPerMicro == TSC * 10^6 / CycleHertz == MicroTimestamp
266 // Could lose some precision here by converting the TSC to a double to
267 // multiply by the period in micros. 52 bit mantissa is a good start though.
268 // TODO: Make feature request to Chrome Trace viewer to accept ticks and a
269 // frequency or do some more involved calculation to avoid dangers of
270 // conversion.
271 double EventTimestampUs = double(1000000) / CycleFreq * double(R.TSC);
272 StackTrieNode *&StackCursor = StackCursorByThreadId[R.TId];
273 switch (R.Type) {
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000274 case RecordTypes::CUSTOM_EVENT:
275 case RecordTypes::TYPED_EVENT:
276 // TODO: Support typed and custom event rendering on Chrome Trace Viewer.
277 break;
Keith Wyss42427992017-11-07 00:28:28 +0000278 case RecordTypes::ENTER:
279 case RecordTypes::ENTER_ARG:
280 StackCursor = findOrCreateStackNode(StackCursor, R.FuncId, R.TId,
281 StackRootsByThreadId, StacksByStackId,
282 &id_counter, NodeStore);
283 // Each record is represented as a json dictionary with function name,
Dean Michael Berris10141262018-07-13 05:38:22 +0000284 // type of B for begin or E for end, thread id, process id,
Keith Wyss42427992017-11-07 00:28:28 +0000285 // timestamp in microseconds, and a stack frame id. The ids are logged
286 // in an id dictionary after the events.
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +0000287 TraceEvents.push_back(json::Object({
288 {"name", Symbolize ? FuncIdHelper.SymbolOrNumber(R.FuncId)
289 : llvm::to_string(R.FuncId)},
290 {"ph", "B"},
291 {"tid", llvm::to_string(R.TId)},
292 {"pid", llvm::to_string(Version >= 3 ? R.PId : 1)},
293 {"ts", llvm::formatv("{0:f4}", EventTimestampUs)},
294 {"sf", llvm::to_string(StackCursor->ExtraData.id)},
295 }));
Keith Wyss42427992017-11-07 00:28:28 +0000296 break;
297 case RecordTypes::EXIT:
298 case RecordTypes::TAIL_EXIT:
299 // No entries to record end for.
300 if (StackCursor == nullptr)
301 break;
302 // Should we emit an END record anyway or account this condition?
303 // (And/Or in loop termination below)
304 StackTrieNode *PreviousCursor = nullptr;
305 do {
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +0000306 TraceEvents.push_back(json::Object({
307 {"name", Symbolize
308 ? FuncIdHelper.SymbolOrNumber(StackCursor->FuncId)
309 : llvm::to_string(StackCursor->FuncId)},
310 {"ph", "E"},
311 {"tid", llvm::to_string(R.TId)},
312 {"pid", llvm::to_string(Version >= 3 ? R.PId : 1)},
313 {"ts", llvm::formatv("{0:f4}", EventTimestampUs)},
314 {"sf", llvm::to_string(StackCursor->ExtraData.id)},
315 }));
Keith Wyss42427992017-11-07 00:28:28 +0000316 PreviousCursor = StackCursor;
317 StackCursor = StackCursor->Parent;
318 } while (PreviousCursor->FuncId != R.FuncId && StackCursor != nullptr);
319 break;
320 }
321 }
Keith Wyss42427992017-11-07 00:28:28 +0000322
323 // The stackFrames dictionary substantially reduces size of the output file by
324 // avoiding repeating the entire call stack of function names for each entry.
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +0000325 json::Object StackFrames;
326 for (const auto &Stack : StacksByStackId) {
327 const auto &StackId = Stack.first;
328 const auto &StackFunctionNode = Stack.second;
329 json::Object::iterator It;
330 std::tie(It, std::ignore) = StackFrames.insert({
331 llvm::to_string(StackId),
332 json::Object{
333 {"name",
334 Symbolize ? FuncIdHelper.SymbolOrNumber(StackFunctionNode->FuncId)
335 : llvm::to_string(StackFunctionNode->FuncId)}},
336 });
337
338 if (StackFunctionNode->Parent != nullptr)
339 It->second.getAsObject()->insert(
340 {"parent", llvm::to_string(StackFunctionNode->Parent->ExtraData.id)});
Keith Wyss42427992017-11-07 00:28:28 +0000341 }
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +0000342
343 json::Object TraceJSON{
344 {"displayTimeUnit", "ns"},
345 {"traceEvents", std::move(TraceEvents)},
346 {"stackFrames", std::move(StackFrames)},
347 };
348
349 // Pretty-print the JSON using two spaces for indentations.
350 OS << formatv("{0:2}", json::Value(std::move(TraceJSON)));
Keith Wyss42427992017-11-07 00:28:28 +0000351}
352
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000353namespace llvm {
354namespace xray {
355
356static CommandRegistration Unused(&Convert, []() -> Error {
357 // FIXME: Support conversion to BINARY when upgrading XRay trace versions.
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000358 InstrumentationMap Map;
359 if (!ConvertInstrMap.empty()) {
360 auto InstrumentationMapOrError = loadInstrumentationMap(ConvertInstrMap);
361 if (!InstrumentationMapOrError)
362 return joinErrors(make_error<StringError>(
363 Twine("Cannot open instrumentation map '") +
364 ConvertInstrMap + "'",
365 std::make_error_code(std::errc::invalid_argument)),
366 InstrumentationMapOrError.takeError());
367 Map = std::move(*InstrumentationMapOrError);
368 }
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000369
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000370 const auto &FunctionAddresses = Map.getFunctionAddresses();
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000371 symbolize::LLVMSymbolizer::Options Opts(
372 symbolize::FunctionNameKind::LinkageName, true, true, false, "");
373 symbolize::LLVMSymbolizer Symbolizer(Opts);
374 llvm::xray::FuncIdConversionHelper FuncIdHelper(ConvertInstrMap, Symbolizer,
375 FunctionAddresses);
376 llvm::xray::TraceConverter TC(FuncIdHelper, ConvertSymbolize);
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000377 std::error_code EC;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000378 raw_fd_ostream OS(ConvertOutput, EC,
379 ConvertOutputFormat == ConvertFormats::BINARY
380 ? sys::fs::OpenFlags::F_None
381 : sys::fs::OpenFlags::F_Text);
382 if (EC)
383 return make_error<StringError>(
384 Twine("Cannot open file '") + ConvertOutput + "' for writing.", EC);
385
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000386 auto TraceOrErr = loadTraceFile(ConvertInput, ConvertSortInput);
387 if (!TraceOrErr)
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000388 return joinErrors(
389 make_error<StringError>(
390 Twine("Failed loading input file '") + ConvertInput + "'.",
Hans Wennborg84da6612017-01-12 18:33:14 +0000391 std::make_error_code(std::errc::executable_format_error)),
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000392 TraceOrErr.takeError());
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000393
394 auto &T = *TraceOrErr;
395 switch (ConvertOutputFormat) {
396 case ConvertFormats::YAML:
397 TC.exportAsYAML(T, OS);
398 break;
399 case ConvertFormats::BINARY:
400 TC.exportAsRAWv1(T, OS);
401 break;
Keith Wyss42427992017-11-07 00:28:28 +0000402 case ConvertFormats::CHROME_TRACE_EVENT:
403 TC.exportAsChromeTraceEventFormat(T, OS);
404 break;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000405 }
406 return Error::success();
407});
408
409} // namespace xray
410} // namespace llvm