blob: efc067cfc28678577945047854f80641ad79cbdd [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"
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +000020#include "llvm/Support/JSON.h"
Pavel Labathd79f6382017-01-16 16:38:23 +000021#include "llvm/Support/ScopedPrinter.h"
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000022#include "llvm/Support/YAMLTraits.h"
23#include "llvm/Support/raw_ostream.h"
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000024#include "llvm/XRay/InstrumentationMap.h"
Dean Michael Berrisd6c18652017-01-11 06:39:09 +000025#include "llvm/XRay/Trace.h"
26#include "llvm/XRay/YAMLXRayRecord.h"
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000027
28using namespace llvm;
29using namespace xray;
30
31// llvm-xray convert
32// ----------------------------------------------------------------------------
33static cl::SubCommand Convert("convert", "Trace Format Conversion");
34static cl::opt<std::string> ConvertInput(cl::Positional,
35 cl::desc("<xray log file>"),
36 cl::Required, cl::sub(Convert));
Keith Wyss42427992017-11-07 00:28:28 +000037enum class ConvertFormats { BINARY, YAML, CHROME_TRACE_EVENT };
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000038static cl::opt<ConvertFormats> ConvertOutputFormat(
39 "output-format", cl::desc("output format"),
40 cl::values(clEnumValN(ConvertFormats::BINARY, "raw", "output in binary"),
Keith Wyss42427992017-11-07 00:28:28 +000041 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.")),
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000045 cl::sub(Convert));
46static cl::alias ConvertOutputFormat2("f", cl::aliasopt(ConvertOutputFormat),
47 cl::desc("Alias for -output-format"),
48 cl::sub(Convert));
49static cl::opt<std::string>
50 ConvertOutput("output", cl::value_desc("output file"), cl::init("-"),
51 cl::desc("output file; use '-' for stdout"),
52 cl::sub(Convert));
53static cl::alias ConvertOutput2("o", cl::aliasopt(ConvertOutput),
54 cl::desc("Alias for -output"),
55 cl::sub(Convert));
56
57static cl::opt<bool>
58 ConvertSymbolize("symbolize",
59 cl::desc("symbolize function ids from the input log"),
60 cl::init(false), cl::sub(Convert));
61static cl::alias ConvertSymbolize2("y", cl::aliasopt(ConvertSymbolize),
62 cl::desc("Alias for -symbolize"),
63 cl::sub(Convert));
64
65static 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(""));
71static cl::alias ConvertInstrMap2("m", cl::aliasopt(ConvertInstrMap),
72 cl::desc("Alias for -instr_map"),
73 cl::sub(Convert));
74static cl::opt<bool> ConvertSortInput(
75 "sort",
76 cl::desc("determines whether to sort input log records by timestamp"),
77 cl::sub(Convert), cl::init(true));
78static cl::alias ConvertSortInput2("s", cl::aliasopt(ConvertSortInput),
79 cl::desc("Alias for -sort"),
80 cl::sub(Convert));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000081
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000082using llvm::yaml::Output;
83
Dean Michael Berrisd6c18652017-01-11 06:39:09 +000084void TraceConverter::exportAsYAML(const Trace &Records, raw_ostream &OS) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000085 YAMLXRayTrace Trace;
86 const auto &FH = Records.getFileHeader();
87 Trace.Header = {FH.Version, FH.Type, FH.ConstantTSC, FH.NonstopTSC,
88 FH.CycleFrequency};
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)
Pavel Labathd79f6382017-01-16 16:38:23 +000093 : llvm::to_string(R.FuncId),
Dean Michael Berris25f8d202018-11-06 08:51:37 +000094 R.TSC, R.TId, R.PId, R.CallArgs, R.Data});
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000095 }
Dimitry Andric9afed032017-02-14 22:49:49 +000096 Output Out(OS, nullptr, 0);
Dean Michael Berris25f8d202018-11-06 08:51:37 +000097 Out.setWriteDefaultValues(false);
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000098 Out << Trace;
99}
100
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000101void TraceConverter::exportAsRAWv1(const Trace &Records, raw_ostream &OS) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000102 // First write out the file header, in the correct endian-appropriate format
103 // (XRay assumes currently little endian).
Peter Collingbournee3f65292018-05-18 19:46:24 +0000104 support::endian::Writer Writer(OS, support::endianness::little);
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000105 const auto &FH = Records.getFileHeader();
106 Writer.write(FH.Version);
107 Writer.write(FH.Type);
108 uint32_t Bitfield{0};
109 if (FH.ConstantTSC)
110 Bitfield |= 1uL;
111 if (FH.NonstopTSC)
112 Bitfield |= 1uL << 1;
113 Writer.write(Bitfield);
114 Writer.write(FH.CycleFrequency);
115
116 // There's 16 bytes of padding at the end of the file header.
117 static constexpr uint32_t Padding4B = 0;
118 Writer.write(Padding4B);
119 Writer.write(Padding4B);
120 Writer.write(Padding4B);
121 Writer.write(Padding4B);
122
123 // Then write out the rest of the records, still in an endian-appropriate
124 // format.
125 for (const auto &R : Records) {
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000126 switch (R.Type) {
127 case RecordTypes::ENTER:
Martin Pelikan10c873f2017-09-27 04:48:03 +0000128 case RecordTypes::ENTER_ARG:
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000129 Writer.write(R.RecordType);
130 Writer.write(static_cast<uint8_t>(R.CPU));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000131 Writer.write(uint8_t{0});
132 break;
133 case RecordTypes::EXIT:
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000134 Writer.write(R.RecordType);
135 Writer.write(static_cast<uint8_t>(R.CPU));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000136 Writer.write(uint8_t{1});
137 break;
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000138 case RecordTypes::TAIL_EXIT:
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000139 Writer.write(R.RecordType);
140 Writer.write(static_cast<uint8_t>(R.CPU));
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000141 Writer.write(uint8_t{2});
142 break;
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000143 case RecordTypes::CUSTOM_EVENT:
144 case RecordTypes::TYPED_EVENT:
145 // Skip custom and typed event records for v1 logs.
146 continue;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000147 }
148 Writer.write(R.FuncId);
149 Writer.write(R.TSC);
150 Writer.write(R.TId);
Dean Michael Berris10141262018-07-13 05:38:22 +0000151
152 if (FH.Version >= 3)
153 Writer.write(R.PId);
154 else
155 Writer.write(Padding4B);
156
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000157 Writer.write(Padding4B);
158 Writer.write(Padding4B);
159 }
160}
161
Keith Wyss42427992017-11-07 00:28:28 +0000162namespace {
163
164// A structure that allows building a dictionary of stack ids for the Chrome
165// trace event format.
166struct StackIdData {
167 // Each Stack of function calls has a unique ID.
168 unsigned id;
169
170 // Bookkeeping so that IDs can be maintained uniquely across threads.
171 // Traversal keeps sibling pointers to other threads stacks. This is helpful
172 // to determine when a thread encounters a new stack and should assign a new
173 // unique ID.
174 SmallVector<TrieNode<StackIdData> *, 4> siblings;
175};
176
177using StackTrieNode = TrieNode<StackIdData>;
178
179// A helper function to find the sibling nodes for an encountered function in a
180// thread of execution. Relies on the invariant that each time a new node is
181// traversed in a thread, sibling bidirectional pointers are maintained.
182SmallVector<StackTrieNode *, 4>
183findSiblings(StackTrieNode *parent, int32_t FnId, uint32_t TId,
184 const DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>>
185 &StackRootsByThreadId) {
186
187 SmallVector<StackTrieNode *, 4> Siblings{};
188
189 if (parent == nullptr) {
190 for (auto map_iter : StackRootsByThreadId) {
191 // Only look for siblings in other threads.
192 if (map_iter.first != TId)
193 for (auto node_iter : map_iter.second) {
194 if (node_iter->FuncId == FnId)
195 Siblings.push_back(node_iter);
196 }
197 }
198 return Siblings;
199 }
200
201 for (auto *ParentSibling : parent->ExtraData.siblings)
202 for (auto node_iter : ParentSibling->Callees)
203 if (node_iter->FuncId == FnId)
204 Siblings.push_back(node_iter);
205
206 return Siblings;
207}
208
209// Given a function being invoked in a thread with id TId, finds and returns the
210// StackTrie representing the function call stack. If no node exists, creates
211// the node. Assigns unique IDs to stacks newly encountered among all threads
212// and keeps sibling links up to when creating new nodes.
213StackTrieNode *findOrCreateStackNode(
214 StackTrieNode *Parent, int32_t FuncId, uint32_t TId,
215 DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> &StackRootsByThreadId,
216 DenseMap<unsigned, StackTrieNode *> &StacksByStackId, unsigned *id_counter,
217 std::forward_list<StackTrieNode> &NodeStore) {
218 SmallVector<StackTrieNode *, 4> &ParentCallees =
219 Parent == nullptr ? StackRootsByThreadId[TId] : Parent->Callees;
220 auto match = find_if(ParentCallees, [FuncId](StackTrieNode *ParentCallee) {
221 return FuncId == ParentCallee->FuncId;
222 });
223 if (match != ParentCallees.end())
224 return *match;
225
226 SmallVector<StackTrieNode *, 4> siblings =
227 findSiblings(Parent, FuncId, TId, StackRootsByThreadId);
228 if (siblings.empty()) {
229 NodeStore.push_front({FuncId, Parent, {}, {(*id_counter)++, {}}});
230 StackTrieNode *CurrentStack = &NodeStore.front();
231 StacksByStackId[*id_counter - 1] = CurrentStack;
232 ParentCallees.push_back(CurrentStack);
233 return CurrentStack;
234 }
235 unsigned stack_id = siblings[0]->ExtraData.id;
236 NodeStore.push_front({FuncId, Parent, {}, {stack_id, std::move(siblings)}});
237 StackTrieNode *CurrentStack = &NodeStore.front();
238 for (auto *sibling : CurrentStack->ExtraData.siblings)
239 sibling->ExtraData.siblings.push_back(CurrentStack);
240 ParentCallees.push_back(CurrentStack);
241 return CurrentStack;
242}
243
Keith Wyss42427992017-11-07 00:28:28 +0000244} // namespace
245
246void TraceConverter::exportAsChromeTraceEventFormat(const Trace &Records,
247 raw_ostream &OS) {
248 const auto &FH = Records.getFileHeader();
Dean Michael Berris10141262018-07-13 05:38:22 +0000249 auto Version = FH.Version;
Keith Wyss42427992017-11-07 00:28:28 +0000250 auto CycleFreq = FH.CycleFrequency;
251
252 unsigned id_counter = 0;
253
Keith Wyss42427992017-11-07 00:28:28 +0000254 DenseMap<uint32_t, StackTrieNode *> StackCursorByThreadId{};
255 DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> StackRootsByThreadId{};
256 DenseMap<unsigned, StackTrieNode *> StacksByStackId{};
257 std::forward_list<StackTrieNode> NodeStore{};
Keith Wyss42427992017-11-07 00:28:28 +0000258
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +0000259 // Create a JSON Array which will hold all trace events.
260 json::Array TraceEvents;
261 for (const auto &R : Records) {
Keith Wyss42427992017-11-07 00:28:28 +0000262 // Chrome trace event format always wants data in micros.
263 // CyclesPerMicro = CycleHertz / 10^6
264 // TSC / CyclesPerMicro == TSC * 10^6 / CycleHertz == MicroTimestamp
265 // Could lose some precision here by converting the TSC to a double to
266 // multiply by the period in micros. 52 bit mantissa is a good start though.
267 // TODO: Make feature request to Chrome Trace viewer to accept ticks and a
268 // frequency or do some more involved calculation to avoid dangers of
269 // conversion.
270 double EventTimestampUs = double(1000000) / CycleFreq * double(R.TSC);
271 StackTrieNode *&StackCursor = StackCursorByThreadId[R.TId];
272 switch (R.Type) {
Dean Michael Berris25f8d202018-11-06 08:51:37 +0000273 case RecordTypes::CUSTOM_EVENT:
274 case RecordTypes::TYPED_EVENT:
275 // TODO: Support typed and custom event rendering on Chrome Trace Viewer.
276 break;
Keith Wyss42427992017-11-07 00:28:28 +0000277 case RecordTypes::ENTER:
278 case RecordTypes::ENTER_ARG:
279 StackCursor = findOrCreateStackNode(StackCursor, R.FuncId, R.TId,
280 StackRootsByThreadId, StacksByStackId,
281 &id_counter, NodeStore);
282 // Each record is represented as a json dictionary with function name,
Dean Michael Berris10141262018-07-13 05:38:22 +0000283 // type of B for begin or E for end, thread id, process id,
Keith Wyss42427992017-11-07 00:28:28 +0000284 // timestamp in microseconds, and a stack frame id. The ids are logged
285 // in an id dictionary after the events.
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +0000286 TraceEvents.push_back(json::Object({
287 {"name", Symbolize ? FuncIdHelper.SymbolOrNumber(R.FuncId)
288 : llvm::to_string(R.FuncId)},
289 {"ph", "B"},
290 {"tid", llvm::to_string(R.TId)},
291 {"pid", llvm::to_string(Version >= 3 ? R.PId : 1)},
292 {"ts", llvm::formatv("{0:f4}", EventTimestampUs)},
293 {"sf", llvm::to_string(StackCursor->ExtraData.id)},
294 }));
Keith Wyss42427992017-11-07 00:28:28 +0000295 break;
296 case RecordTypes::EXIT:
297 case RecordTypes::TAIL_EXIT:
298 // No entries to record end for.
299 if (StackCursor == nullptr)
300 break;
301 // Should we emit an END record anyway or account this condition?
302 // (And/Or in loop termination below)
303 StackTrieNode *PreviousCursor = nullptr;
304 do {
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +0000305 TraceEvents.push_back(json::Object({
306 {"name", Symbolize
307 ? FuncIdHelper.SymbolOrNumber(StackCursor->FuncId)
308 : llvm::to_string(StackCursor->FuncId)},
309 {"ph", "E"},
310 {"tid", llvm::to_string(R.TId)},
311 {"pid", llvm::to_string(Version >= 3 ? R.PId : 1)},
312 {"ts", llvm::formatv("{0:f4}", EventTimestampUs)},
313 {"sf", llvm::to_string(StackCursor->ExtraData.id)},
314 }));
Keith Wyss42427992017-11-07 00:28:28 +0000315 PreviousCursor = StackCursor;
316 StackCursor = StackCursor->Parent;
317 } while (PreviousCursor->FuncId != R.FuncId && StackCursor != nullptr);
318 break;
319 }
320 }
Keith Wyss42427992017-11-07 00:28:28 +0000321
322 // The stackFrames dictionary substantially reduces size of the output file by
323 // avoiding repeating the entire call stack of function names for each entry.
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +0000324 json::Object StackFrames;
325 for (const auto &Stack : StacksByStackId) {
326 const auto &StackId = Stack.first;
327 const auto &StackFunctionNode = Stack.second;
328 json::Object::iterator It;
329 std::tie(It, std::ignore) = StackFrames.insert({
330 llvm::to_string(StackId),
331 json::Object{
332 {"name",
333 Symbolize ? FuncIdHelper.SymbolOrNumber(StackFunctionNode->FuncId)
334 : llvm::to_string(StackFunctionNode->FuncId)}},
335 });
336
337 if (StackFunctionNode->Parent != nullptr)
338 It->second.getAsObject()->insert(
339 {"parent", llvm::to_string(StackFunctionNode->Parent->ExtraData.id)});
Keith Wyss42427992017-11-07 00:28:28 +0000340 }
Dean Michael Berris2c4dcf02018-08-03 09:21:31 +0000341
342 json::Object TraceJSON{
343 {"displayTimeUnit", "ns"},
344 {"traceEvents", std::move(TraceEvents)},
345 {"stackFrames", std::move(StackFrames)},
346 };
347
348 // Pretty-print the JSON using two spaces for indentations.
349 OS << formatv("{0:2}", json::Value(std::move(TraceJSON)));
Keith Wyss42427992017-11-07 00:28:28 +0000350}
351
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000352namespace llvm {
353namespace xray {
354
355static CommandRegistration Unused(&Convert, []() -> Error {
356 // FIXME: Support conversion to BINARY when upgrading XRay trace versions.
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000357 InstrumentationMap Map;
358 if (!ConvertInstrMap.empty()) {
359 auto InstrumentationMapOrError = loadInstrumentationMap(ConvertInstrMap);
360 if (!InstrumentationMapOrError)
361 return joinErrors(make_error<StringError>(
362 Twine("Cannot open instrumentation map '") +
363 ConvertInstrMap + "'",
364 std::make_error_code(std::errc::invalid_argument)),
365 InstrumentationMapOrError.takeError());
366 Map = std::move(*InstrumentationMapOrError);
367 }
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000368
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000369 const auto &FunctionAddresses = Map.getFunctionAddresses();
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000370 symbolize::LLVMSymbolizer::Options Opts(
371 symbolize::FunctionNameKind::LinkageName, true, true, false, "");
372 symbolize::LLVMSymbolizer Symbolizer(Opts);
373 llvm::xray::FuncIdConversionHelper FuncIdHelper(ConvertInstrMap, Symbolizer,
374 FunctionAddresses);
375 llvm::xray::TraceConverter TC(FuncIdHelper, ConvertSymbolize);
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000376 std::error_code EC;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000377 raw_fd_ostream OS(ConvertOutput, EC,
378 ConvertOutputFormat == ConvertFormats::BINARY
379 ? sys::fs::OpenFlags::F_None
380 : sys::fs::OpenFlags::F_Text);
381 if (EC)
382 return make_error<StringError>(
383 Twine("Cannot open file '") + ConvertOutput + "' for writing.", EC);
384
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000385 auto TraceOrErr = loadTraceFile(ConvertInput, ConvertSortInput);
386 if (!TraceOrErr)
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000387 return joinErrors(
388 make_error<StringError>(
389 Twine("Failed loading input file '") + ConvertInput + "'.",
Hans Wennborg84da6612017-01-12 18:33:14 +0000390 std::make_error_code(std::errc::executable_format_error)),
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000391 TraceOrErr.takeError());
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000392
393 auto &T = *TraceOrErr;
394 switch (ConvertOutputFormat) {
395 case ConvertFormats::YAML:
396 TC.exportAsYAML(T, OS);
397 break;
398 case ConvertFormats::BINARY:
399 TC.exportAsRAWv1(T, OS);
400 break;
Keith Wyss42427992017-11-07 00:28:28 +0000401 case ConvertFormats::CHROME_TRACE_EVENT:
402 TC.exportAsChromeTraceEventFormat(T, OS);
403 break;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000404 }
405 return Error::success();
406});
407
408} // namespace xray
409} // namespace llvm