Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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 | |
Dean Michael Berris | bff6dcb | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 21 | template <size_t Index> struct IndexedWriter { |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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> |
Dean Michael Berris | bff6dcb | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 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); |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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> |
Dean Michael Berris | bff6dcb | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 39 | static size_t write(support::endian::Writer &OS, Tuple &&) { |
| 40 | return 0; |
| 41 | } |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Dean Michael Berris | b4ef1e8 | 2018-08-30 09:04:12 +0000 | [diff] [blame] | 44 | template <uint8_t Kind, class... Values> |
Dean Michael Berris | bff6dcb | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 45 | Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) { |
Dean Michael Berris | 5de005f | 2018-11-09 06:26:48 +0000 | [diff] [blame] | 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}; |
Dean Michael Berris | b4ef1e8 | 2018-08-30 09:04:12 +0000 | [diff] [blame] | 49 | auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...); |
Dean Michael Berris | bff6dcb | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 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'); |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 57 | return Error::success(); |
| 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 | |
| 62 | FDRTraceWriter::FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H) |
Dean Michael Berris | bff6dcb | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 63 | : OS(O, support::endianness::native) { |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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. |
Dean Michael Berris | bff6dcb | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 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); |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | FDRTraceWriter::~FDRTraceWriter() {} |
| 81 | |
| 82 | Error FDRTraceWriter::visit(BufferExtents &R) { |
| 83 | return writeMetadata<7u>(OS, R.size()); |
| 84 | } |
| 85 | |
| 86 | Error FDRTraceWriter::visit(WallclockRecord &R) { |
| 87 | return writeMetadata<4u>(OS, R.seconds(), R.nanos()); |
| 88 | } |
| 89 | |
| 90 | Error FDRTraceWriter::visit(NewCPUIDRecord &R) { |
Dean Michael Berris | 5fd44bd | 2018-09-11 07:27:59 +0000 | [diff] [blame] | 91 | return writeMetadata<2u>(OS, R.cpuid(), R.tsc()); |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | Error FDRTraceWriter::visit(TSCWrapRecord &R) { |
| 95 | return writeMetadata<3u>(OS, R.tsc()); |
| 96 | } |
| 97 | |
| 98 | Error FDRTraceWriter::visit(CustomEventRecord &R) { |
Dean Michael Berris | 3fe1b12 | 2018-11-01 00:18:52 +0000 | [diff] [blame] | 99 | if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu())) |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 100 | return E; |
Dean Michael Berris | 3fe1b12 | 2018-11-01 00:18:52 +0000 | [diff] [blame] | 101 | auto D = R.data(); |
| 102 | ArrayRef<char> Bytes(D.data(), D.size()); |
Dean Michael Berris | bff6dcb | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 103 | OS.write(Bytes); |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 104 | return Error::success(); |
| 105 | } |
| 106 | |
Dean Michael Berris | 8fa10fd | 2018-11-07 04:37:42 +0000 | [diff] [blame] | 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 | |
| 116 | Error FDRTraceWriter::visit(TypedEventRecord &R) { |
Dean Michael Berris | 5de005f | 2018-11-09 06:26:48 +0000 | [diff] [blame] | 117 | if (auto E = writeMetadata<8u>(OS, R.size(), R.delta(), R.eventType())) |
Dean Michael Berris | 8fa10fd | 2018-11-07 04:37:42 +0000 | [diff] [blame] | 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 | |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 125 | Error FDRTraceWriter::visit(CallArgRecord &R) { |
| 126 | return writeMetadata<6u>(OS, R.arg()); |
| 127 | } |
| 128 | |
| 129 | Error FDRTraceWriter::visit(PIDRecord &R) { |
| 130 | return writeMetadata<9u>(OS, R.pid()); |
| 131 | } |
| 132 | |
| 133 | Error FDRTraceWriter::visit(NewBufferRecord &R) { |
| 134 | return writeMetadata<0u>(OS, R.tid()); |
| 135 | } |
| 136 | |
| 137 | Error FDRTraceWriter::visit(EndBufferRecord &R) { |
| 138 | return writeMetadata<1u>(OS, 0); |
| 139 | } |
| 140 | |
| 141 | Error FDRTraceWriter::visit(FunctionRecord &R) { |
Dean Michael Berris | 817cfcb | 2018-08-31 17:49:59 +0000 | [diff] [blame] | 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()); |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 150 | return Error::success(); |
Dean Michael Berris | 3fe1b12 | 2018-11-01 00:18:52 +0000 | [diff] [blame] | 151 | } |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 152 | |
| 153 | } // namespace xray |
| 154 | } // namespace llvm |