1 //===- FDRTraceWriter.cpp - XRay FDR Trace Writer ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Test a utility that can write out XRay FDR Mode formatted trace files.
12 //===----------------------------------------------------------------------===//
13 #include "llvm/XRay/FDRTraceWriter.h"
21 template <size_t Index> struct IndexedWriter {
24 typename std::enable_if<
26 std::tuple_size<typename std::remove_reference<Tuple>::type>::value),
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);
35 typename std::enable_if<
37 std::tuple_size<typename std::remove_reference<Tuple>::type>::value),
39 static size_t write(support::endian::Writer &OS, Tuple &&) {
44 template <uint8_t Kind, class... Values>
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.
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)
57 return Error::success();
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.
67 (H.ConstantTSC ? 0x01 : 0x0) | (H.NonstopTSC ? 0x02 : 0x0);
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.
74 OS.write(H.CycleFrequency);
75 ArrayRef<char> FreeFormBytes(H.FreeFormData,
76 sizeof(XRayFileHeader::FreeFormData));
77 OS.write(FreeFormBytes);
80 FDRTraceWriter::~FDRTraceWriter() {}
82 Error FDRTraceWriter::visit(BufferExtents &R) {
83 return writeMetadata<7u>(OS, R.size());
86 Error FDRTraceWriter::visit(WallclockRecord &R) {
87 return writeMetadata<4u>(OS, R.seconds(), R.nanos());
90 Error FDRTraceWriter::visit(NewCPUIDRecord &R) {
91 return writeMetadata<2u>(OS, R.cpuid(), R.tsc());
94 Error FDRTraceWriter::visit(TSCWrapRecord &R) {
95 return writeMetadata<3u>(OS, R.tsc());
98 Error FDRTraceWriter::visit(CustomEventRecord &R) {
99 if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu()))
102 ArrayRef<char> Bytes(D.data(), D.size());
104 return Error::success();
107 Error FDRTraceWriter::visit(CustomEventRecordV5 &R) {
108 if (auto E = writeMetadata<5u>(OS, R.size(), R.delta()))
111 ArrayRef<char> Bytes(D.data(), D.size());
113 return Error::success();
116 Error FDRTraceWriter::visit(TypedEventRecord &R) {
117 if (auto E = writeMetadata<8u>(OS, R.size(), R.delta(), R.eventType()))
120 ArrayRef<char> Bytes(D.data(), D.size());
122 return Error::success();
125 Error FDRTraceWriter::visit(CallArgRecord &R) {
126 return writeMetadata<6u>(OS, R.arg());
129 Error FDRTraceWriter::visit(PIDRecord &R) {
130 return writeMetadata<9u>(OS, R.pid());
133 Error FDRTraceWriter::visit(NewBufferRecord &R) {
134 return writeMetadata<0u>(OS, R.tid());
137 Error FDRTraceWriter::visit(EndBufferRecord &R) {
138 return writeMetadata<1u>(OS, 0);
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);
150 return Error::success();