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