Dean Michael Berris | a6c6343 | 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 | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 21 | template <size_t Index> struct IndexedWriter { |
Dean Michael Berris | a6c6343 | 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 | 9871797 | 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 | a6c6343 | 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 | 9871797 | 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 | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Dean Michael Berris | 1704597 | 2018-08-30 09:04:12 +0000 | [diff] [blame] | 44 | template <uint8_t Kind, class... Values> |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 45 | Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) { |
| 46 | uint8_t FirstByte = (Kind << 1) | uint8_t{0x01}; |
Dean Michael Berris | 1704597 | 2018-08-30 09:04:12 +0000 | [diff] [blame] | 47 | auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...); |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 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'); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 55 | return Error::success(); |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | FDRTraceWriter::FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H) |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 61 | : OS(O, support::endianness::native) { |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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. |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 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); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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) { |
Dean Michael Berris | ec605d3 | 2018-09-11 07:27:59 +0000 | [diff] [blame^] | 89 | return writeMetadata<2u>(OS, R.cpuid(), R.tsc()); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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())) |
| 98 | return E; |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 99 | ArrayRef<char> Bytes(R.data().data(), R.data().size()); |
| 100 | OS.write(Bytes); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 101 | return Error::success(); |
| 102 | } |
| 103 | |
| 104 | Error FDRTraceWriter::visit(CallArgRecord &R) { |
| 105 | return writeMetadata<6u>(OS, R.arg()); |
| 106 | } |
| 107 | |
| 108 | Error FDRTraceWriter::visit(PIDRecord &R) { |
| 109 | return writeMetadata<9u>(OS, R.pid()); |
| 110 | } |
| 111 | |
| 112 | Error FDRTraceWriter::visit(NewBufferRecord &R) { |
| 113 | return writeMetadata<0u>(OS, R.tid()); |
| 114 | } |
| 115 | |
| 116 | Error FDRTraceWriter::visit(EndBufferRecord &R) { |
| 117 | return writeMetadata<1u>(OS, 0); |
| 118 | } |
| 119 | |
| 120 | Error FDRTraceWriter::visit(FunctionRecord &R) { |
Dean Michael Berris | c1dceee | 2018-08-31 17:49:59 +0000 | [diff] [blame] | 121 | // Write out the data in "field" order, to be endian-aware. |
| 122 | uint32_t TypeRecordFuncId = uint32_t{R.functionId() & ~uint32_t{0x0Fu << 28}}; |
| 123 | TypeRecordFuncId <<= 3; |
| 124 | TypeRecordFuncId |= static_cast<uint32_t>(R.recordType()); |
| 125 | TypeRecordFuncId <<= 1; |
| 126 | TypeRecordFuncId &= ~uint32_t{0x01}; |
| 127 | OS.write(TypeRecordFuncId); |
| 128 | OS.write(R.delta()); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 129 | return Error::success(); |
Dean Michael Berris | c1dceee | 2018-08-31 17:49:59 +0000 | [diff] [blame] | 130 | } // namespace xray |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 131 | |
| 132 | } // namespace xray |
| 133 | } // namespace llvm |