blob: ee6a76a21df927521504bc421aa4250e700866f1 [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"
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),
Martin Pelikan10c873f2017-09-27 04:48:03 +000094 R.TSC, R.TId, R.CallArgs});
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000095 }
Dimitry Andric9afed032017-02-14 22:49:49 +000096 Output Out(OS, nullptr, 0);
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).
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};
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) {
125 Writer.write(R.RecordType);
Dean Michael Berris4f83c4d2017-02-17 01:47:16 +0000126 // 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));
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000129 switch (R.Type) {
130 case RecordTypes::ENTER:
Martin Pelikan10c873f2017-09-27 04:48:03 +0000131 case RecordTypes::ENTER_ARG:
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000132 Writer.write(uint8_t{0});
133 break;
134 case RecordTypes::EXIT:
135 Writer.write(uint8_t{1});
136 break;
Dean Michael Berris0f84a7d2017-09-18 06:08:46 +0000137 case RecordTypes::TAIL_EXIT:
138 Writer.write(uint8_t{2});
139 break;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000140 }
141 Writer.write(R.FuncId);
142 Writer.write(R.TSC);
143 Writer.write(R.TId);
144 Writer.write(Padding4B);
145 Writer.write(Padding4B);
146 Writer.write(Padding4B);
147 }
148}
149
Keith Wyss42427992017-11-07 00:28:28 +0000150namespace {
151
152// A structure that allows building a dictionary of stack ids for the Chrome
153// trace event format.
154struct StackIdData {
155 // Each Stack of function calls has a unique ID.
156 unsigned id;
157
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
161 // unique ID.
162 SmallVector<TrieNode<StackIdData> *, 4> siblings;
163};
164
165using StackTrieNode = TrieNode<StackIdData>;
166
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.
170SmallVector<StackTrieNode *, 4>
171findSiblings(StackTrieNode *parent, int32_t FnId, uint32_t TId,
172 const DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>>
173 &StackRootsByThreadId) {
174
175 SmallVector<StackTrieNode *, 4> Siblings{};
176
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);
184 }
185 }
186 return Siblings;
187 }
188
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);
193
194 return Siblings;
195}
196
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.
201StackTrieNode *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;
210 });
211 if (match != ParentCallees.end())
212 return *match;
213
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);
221 return CurrentStack;
222 }
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);
229 return CurrentStack;
230}
231
232void writeTraceViewerRecord(raw_ostream &OS, int32_t FuncId, uint32_t TId,
233 bool Symbolize,
234 const FuncIdConversionHelper &FuncIdHelper,
235 double EventTimestampUs,
236 const StackTrieNode &StackCursor,
237 StringRef FunctionPhenotype) {
238 OS << " ";
239 OS << llvm::formatv(
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);
245}
246
247} // namespace
248
249void TraceConverter::exportAsChromeTraceEventFormat(const Trace &Records,
250 raw_ostream &OS) {
251 const auto &FH = Records.getFileHeader();
252 auto CycleFreq = FH.CycleFrequency;
253
254 unsigned id_counter = 0;
255
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{};
261 int loop_count = 0;
262 for (const auto &R : Records) {
263 if (loop_count++ == 0)
264 OS << "\n";
265 else
266 OS << ",\n";
267
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
275 // conversion.
276 double EventTimestampUs = double(1000000) / CycleFreq * double(R.TSC);
277 StackTrieNode *&StackCursor = StackCursorByThreadId[R.TId];
278 switch (R.Type) {
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");
290 break;
291 case RecordTypes::EXIT:
292 case RecordTypes::TAIL_EXIT:
293 // No entries to record end for.
294 if (StackCursor == nullptr)
295 break;
296 // Should we emit an END record anyway or account this condition?
297 // (And/Or in loop termination below)
298 StackTrieNode *PreviousCursor = nullptr;
299 do {
300 writeTraceViewerRecord(OS, StackCursor->FuncId, R.TId, Symbolize,
301 FuncIdHelper, EventTimestampUs, *StackCursor,
302 "E");
303 PreviousCursor = StackCursor;
304 StackCursor = StackCursor->Parent;
305 } while (PreviousCursor->FuncId != R.FuncId && StackCursor != nullptr);
306 break;
307 }
308 }
309 OS << "\n ],\n"; // Close the Trace Events array.
310 OS << " "
311 << "\"displayTimeUnit\": \"ns\",\n";
312
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)
319 OS << "\n";
320 else
321 OS << ",\n";
322 OS << " ";
323 OS << llvm::formatv(
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);
330 OS << " }";
331 }
332 OS << "\n }\n"; // Close the stack frames map.
333 OS << "}\n"; // Close the JSON entry.
334}
335
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000336namespace llvm {
337namespace xray {
338
339static CommandRegistration Unused(&Convert, []() -> Error {
340 // FIXME: Support conversion to BINARY when upgrading XRay trace versions.
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000341 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);
351 }
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000352
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000353 const auto &FunctionAddresses = Map.getFunctionAddresses();
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000354 symbolize::LLVMSymbolizer::Options Opts(
355 symbolize::FunctionNameKind::LinkageName, true, true, false, "");
356 symbolize::LLVMSymbolizer Symbolizer(Opts);
357 llvm::xray::FuncIdConversionHelper FuncIdHelper(ConvertInstrMap, Symbolizer,
358 FunctionAddresses);
359 llvm::xray::TraceConverter TC(FuncIdHelper, ConvertSymbolize);
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000360 std::error_code EC;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000361 raw_fd_ostream OS(ConvertOutput, EC,
362 ConvertOutputFormat == ConvertFormats::BINARY
363 ? sys::fs::OpenFlags::F_None
364 : sys::fs::OpenFlags::F_Text);
365 if (EC)
366 return make_error<StringError>(
367 Twine("Cannot open file '") + ConvertOutput + "' for writing.", EC);
368
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000369 auto TraceOrErr = loadTraceFile(ConvertInput, ConvertSortInput);
370 if (!TraceOrErr)
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000371 return joinErrors(
372 make_error<StringError>(
373 Twine("Failed loading input file '") + ConvertInput + "'.",
Hans Wennborg84da6612017-01-12 18:33:14 +0000374 std::make_error_code(std::errc::executable_format_error)),
Dean Michael Berrisd6c18652017-01-11 06:39:09 +0000375 TraceOrErr.takeError());
Dean Michael Berris0e8abab2017-02-01 00:05:29 +0000376
377 auto &T = *TraceOrErr;
378 switch (ConvertOutputFormat) {
379 case ConvertFormats::YAML:
380 TC.exportAsYAML(T, OS);
381 break;
382 case ConvertFormats::BINARY:
383 TC.exportAsRAWv1(T, OS);
384 break;
Keith Wyss42427992017-11-07 00:28:28 +0000385 case ConvertFormats::CHROME_TRACE_EVENT:
386 TC.exportAsChromeTraceEventFormat(T, OS);
387 break;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +0000388 }
389 return Error::success();
390});
391
392} // namespace xray
393} // namespace llvm