xref: /llvm-project/llvm/lib/XRay/FDRTraceWriter.cpp (revision 6b67ff03002f39adf773f456aa4fd79bb22f3a04)
1 //===- FDRTraceWriter.cpp - XRay FDR Trace Writer ---------------*- C++ -*-===//
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 // Test a utility that can write out XRay FDR Mode formatted trace files.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "llvm/XRay/FDRTraceWriter.h"
14 #include <tuple>
15 
16 namespace llvm {
17 namespace xray {
18 
19 namespace {
20 
21 template <size_t Index> struct IndexedWriter {
22   template <
23       class Tuple,
24       typename std::enable_if<
25           (Index <
26            std::tuple_size<typename std::remove_reference<Tuple>::type>::value),
27           int>::type = 0>
28   static size_t write(support::endian::Writer &OS, Tuple &&T) {
29     OS.write(std::get<Index>(T));
30     return sizeof(std::get<Index>(T)) + IndexedWriter<Index + 1>::write(OS, T);
31   }
32 
33   template <
34       class Tuple,
35       typename std::enable_if<
36           (Index >=
37            std::tuple_size<typename std::remove_reference<Tuple>::type>::value),
38           int>::type = 0>
39   static size_t write(support::endian::Writer &OS, Tuple &&) {
40     return 0;
41   }
42 };
43 
44 template <uint8_t Kind, class... Values>
45 Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) {
46   uint8_t FirstByte = (Kind << 1) | uint8_t{0x01};
47   auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);
48   // Write in field order.
49   OS.write(FirstByte);
50   auto Bytes = IndexedWriter<0>::write(OS, T);
51   assert(Bytes <= 15 && "Must only ever write at most 16 byte metadata!");
52   // Pad out with appropriate numbers of zero's.
53   for (; Bytes < 15; ++Bytes)
54     OS.write('\0');
55   return Error::success();
56 }
57 
58 } // namespace
59 
60 FDRTraceWriter::FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H)
61     : OS(O, support::endianness::native) {
62   // We need to re-construct a header, by writing the fields we care about for
63   // traces, in the format that the runtime would have written.
64   uint32_t BitField =
65       (H.ConstantTSC ? 0x01 : 0x0) | (H.NonstopTSC ? 0x02 : 0x0);
66 
67   // For endian-correctness, we need to write these fields in the order they
68   // appear and that we expect, instead of blasting bytes of the struct through.
69   OS.write(H.Version);
70   OS.write(H.Type);
71   OS.write(BitField);
72   OS.write(H.CycleFrequency);
73   ArrayRef<char> FreeFormBytes(H.FreeFormData,
74                                sizeof(XRayFileHeader::FreeFormData));
75   OS.write(FreeFormBytes);
76 }
77 
78 FDRTraceWriter::~FDRTraceWriter() {}
79 
80 Error FDRTraceWriter::visit(BufferExtents &R) {
81   return writeMetadata<7u>(OS, R.size());
82 }
83 
84 Error FDRTraceWriter::visit(WallclockRecord &R) {
85   return writeMetadata<4u>(OS, R.seconds(), R.nanos());
86 }
87 
88 Error FDRTraceWriter::visit(NewCPUIDRecord &R) {
89   return writeMetadata<2u>(OS, R.cpuid(), R.tsc());
90 }
91 
92 Error FDRTraceWriter::visit(TSCWrapRecord &R) {
93   return writeMetadata<3u>(OS, R.tsc());
94 }
95 
96 Error FDRTraceWriter::visit(CustomEventRecord &R) {
97   if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu()))
98     return E;
99   auto D = R.data();
100   ArrayRef<char> Bytes(D.data(), D.size());
101   OS.write(Bytes);
102   return Error::success();
103 }
104 
105 Error FDRTraceWriter::visit(CallArgRecord &R) {
106   return writeMetadata<6u>(OS, R.arg());
107 }
108 
109 Error FDRTraceWriter::visit(PIDRecord &R) {
110   return writeMetadata<9u>(OS, R.pid());
111 }
112 
113 Error FDRTraceWriter::visit(NewBufferRecord &R) {
114   return writeMetadata<0u>(OS, R.tid());
115 }
116 
117 Error FDRTraceWriter::visit(EndBufferRecord &R) {
118   return writeMetadata<1u>(OS, 0);
119 }
120 
121 Error FDRTraceWriter::visit(FunctionRecord &R) {
122   // Write out the data in "field" order, to be endian-aware.
123   uint32_t TypeRecordFuncId = uint32_t{R.functionId() & ~uint32_t{0x0Fu << 28}};
124   TypeRecordFuncId <<= 3;
125   TypeRecordFuncId |= static_cast<uint32_t>(R.recordType());
126   TypeRecordFuncId <<= 1;
127   TypeRecordFuncId &= ~uint32_t{0x01};
128   OS.write(TypeRecordFuncId);
129   OS.write(R.delta());
130   return Error::success();
131 }
132 
133 } // namespace xray
134 } // namespace llvm
135