]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-xray/xray-converter.cc
MFC r343601:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-xray / xray-converter.cc
1 //===- xray-converter.cc - XRay Trace Conversion --------------------------===//
2 //
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
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"
27
28 using namespace llvm;
29 using namespace xray;
30
31 // llvm-xray convert
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.")),
45     cl::sub(Convert));
46 static cl::alias ConvertOutputFormat2("f", cl::aliasopt(ConvertOutputFormat),
47                                       cl::desc("Alias for -output-format"),
48                                       cl::sub(Convert));
49 static 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));
53 static cl::alias ConvertOutput2("o", cl::aliasopt(ConvertOutput),
54                                 cl::desc("Alias for -output"),
55                                 cl::sub(Convert));
56
57 static cl::opt<bool>
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"),
63                                    cl::sub(Convert));
64
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"),
73                                   cl::sub(Convert));
74 static 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));
78 static cl::alias ConvertSortInput2("s", cl::aliasopt(ConvertSortInput),
79                                    cl::desc("Alias for -sort"),
80                                    cl::sub(Convert));
81
82 using llvm::yaml::Output;
83
84 void TraceConverter::exportAsYAML(const Trace &Records, raw_ostream &OS) {
85   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)
93                                        : llvm::to_string(R.FuncId),
94                              R.TSC, R.TId, R.CallArgs});
95   }
96   Output Out(OS, nullptr, 0);
97   Out << Trace;
98 }
99
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};
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);
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));
129     switch (R.Type) {
130     case RecordTypes::ENTER:
131     case RecordTypes::ENTER_ARG:
132       Writer.write(uint8_t{0});
133       break;
134     case RecordTypes::EXIT:
135       Writer.write(uint8_t{1});
136       break;
137     case RecordTypes::TAIL_EXIT:
138       Writer.write(uint8_t{2});
139       break;
140     }
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
150 namespace {
151
152 // A structure that allows building a dictionary of stack ids for the Chrome
153 // trace event format.
154 struct 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
165 using 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.
170 SmallVector<StackTrieNode *, 4>
171 findSiblings(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.
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;
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
232 void 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
249 void 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
336 namespace llvm {
337 namespace xray {
338
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);
351   }
352
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,
358                                                   FunctionAddresses);
359   llvm::xray::TraceConverter TC(FuncIdHelper, ConvertSymbolize);
360   std::error_code EC;
361   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
369   auto TraceOrErr = loadTraceFile(ConvertInput, ConvertSortInput);
370   if (!TraceOrErr)
371     return joinErrors(
372         make_error<StringError>(
373             Twine("Failed loading input file '") + ConvertInput + "'.",
374             std::make_error_code(std::errc::executable_format_error)),
375         TraceOrErr.takeError());
376
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;
385   case ConvertFormats::CHROME_TRACE_EVENT:
386     TC.exportAsChromeTraceEventFormat(T, OS);
387     break;
388   }
389   return Error::success();
390 });
391
392 } // namespace xray
393 } // namespace llvm