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>
writellvm::xray::__anon8fdffaf00111::IndexedWriter28 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>
writellvm::xray::__anon8fdffaf00111::IndexedWriter39 static size_t write(support::endian::Writer &OS, Tuple &&) {
40 return 0;
41 }
42 };
43
44 template <uint8_t Kind, class... Values>
writeMetadata(support::endian::Writer & OS,Values &&...Ds)45 Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) {
46 // The first bit in the first byte of metadata records is always set to 1, so
47 // we ensure this is the case when we write out the first byte of the record.
48 uint8_t FirstByte = (static_cast<uint8_t>(Kind) << 1) | uint8_t{0x01u};
49 auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);
50 // Write in field order.
51 OS.write(FirstByte);
52 auto Bytes = IndexedWriter<0>::write(OS, T);
53 assert(Bytes <= 15 && "Must only ever write at most 16 byte metadata!");
54 // Pad out with appropriate numbers of zero's.
55 for (; Bytes < 15; ++Bytes)
56 OS.write('\0');
57 return Error::success();
58 }
59
60 } // namespace
61
FDRTraceWriter(raw_ostream & O,const XRayFileHeader & H)62 FDRTraceWriter::FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H)
63 : OS(O, support::endianness::native) {
64 // We need to re-construct a header, by writing the fields we care about for
65 // traces, in the format that the runtime would have written.
66 uint32_t BitField =
67 (H.ConstantTSC ? 0x01 : 0x0) | (H.NonstopTSC ? 0x02 : 0x0);
68
69 // For endian-correctness, we need to write these fields in the order they
70 // appear and that we expect, instead of blasting bytes of the struct through.
71 OS.write(H.Version);
72 OS.write(H.Type);
73 OS.write(BitField);
74 OS.write(H.CycleFrequency);
75 ArrayRef<char> FreeFormBytes(H.FreeFormData,
76 sizeof(XRayFileHeader::FreeFormData));
77 OS.write(FreeFormBytes);
78 }
79
~FDRTraceWriter()80 FDRTraceWriter::~FDRTraceWriter() {}
81
visit(BufferExtents & R)82 Error FDRTraceWriter::visit(BufferExtents &R) {
83 return writeMetadata<7u>(OS, R.size());
84 }
85
visit(WallclockRecord & R)86 Error FDRTraceWriter::visit(WallclockRecord &R) {
87 return writeMetadata<4u>(OS, R.seconds(), R.nanos());
88 }
89
visit(NewCPUIDRecord & R)90 Error FDRTraceWriter::visit(NewCPUIDRecord &R) {
91 return writeMetadata<2u>(OS, R.cpuid(), R.tsc());
92 }
93
visit(TSCWrapRecord & R)94 Error FDRTraceWriter::visit(TSCWrapRecord &R) {
95 return writeMetadata<3u>(OS, R.tsc());
96 }
97
visit(CustomEventRecord & R)98 Error FDRTraceWriter::visit(CustomEventRecord &R) {
99 if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu()))
100 return E;
101 auto D = R.data();
102 ArrayRef<char> Bytes(D.data(), D.size());
103 OS.write(Bytes);
104 return Error::success();
105 }
106
visit(CustomEventRecordV5 & R)107 Error FDRTraceWriter::visit(CustomEventRecordV5 &R) {
108 if (auto E = writeMetadata<5u>(OS, R.size(), R.delta()))
109 return E;
110 auto D = R.data();
111 ArrayRef<char> Bytes(D.data(), D.size());
112 OS.write(Bytes);
113 return Error::success();
114 }
115
visit(TypedEventRecord & R)116 Error FDRTraceWriter::visit(TypedEventRecord &R) {
117 if (auto E = writeMetadata<8u>(OS, R.size(), R.delta(), R.eventType()))
118 return E;
119 auto D = R.data();
120 ArrayRef<char> Bytes(D.data(), D.size());
121 OS.write(Bytes);
122 return Error::success();
123 }
124
visit(CallArgRecord & R)125 Error FDRTraceWriter::visit(CallArgRecord &R) {
126 return writeMetadata<6u>(OS, R.arg());
127 }
128
visit(PIDRecord & R)129 Error FDRTraceWriter::visit(PIDRecord &R) {
130 return writeMetadata<9u>(OS, R.pid());
131 }
132
visit(NewBufferRecord & R)133 Error FDRTraceWriter::visit(NewBufferRecord &R) {
134 return writeMetadata<0u>(OS, R.tid());
135 }
136
visit(EndBufferRecord & R)137 Error FDRTraceWriter::visit(EndBufferRecord &R) {
138 return writeMetadata<1u>(OS, 0);
139 }
140
visit(FunctionRecord & R)141 Error FDRTraceWriter::visit(FunctionRecord &R) {
142 // Write out the data in "field" order, to be endian-aware.
143 uint32_t TypeRecordFuncId = uint32_t{R.functionId() & ~uint32_t{0x0Fu << 28}};
144 TypeRecordFuncId <<= 3;
145 TypeRecordFuncId |= static_cast<uint32_t>(R.recordType());
146 TypeRecordFuncId <<= 1;
147 TypeRecordFuncId &= ~uint32_t{0x01};
148 OS.write(TypeRecordFuncId);
149 OS.write(R.delta());
150 return Error::success();
151 }
152
153 } // namespace xray
154 } // namespace llvm
155