Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 1 | //===- FDRTraceWriter.cpp - XRay FDR Trace Writer ---------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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 | |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 20 | template <size_t Index> struct IndexedWriter { |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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> |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 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); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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> |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 38 | static size_t write(support::endian::Writer &OS, Tuple &&) { |
| 39 | return 0; |
| 40 | } |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Dean Michael Berris | 1704597 | 2018-08-30 09:04:12 +0000 | [diff] [blame] | 43 | template <uint8_t Kind, class... Values> |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 44 | Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) { |
Dean Michael Berris | da375a6 | 2018-11-09 06:26:48 +0000 | [diff] [blame] | 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}; |
Dean Michael Berris | 1704597 | 2018-08-30 09:04:12 +0000 | [diff] [blame] | 48 | auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...); |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 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'); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 56 | return Error::success(); |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
| 61 | FDRTraceWriter::FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H) |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 62 | : OS(O, support::endianness::native) { |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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. |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 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); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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) { |
Dean Michael Berris | ec605d3 | 2018-09-11 07:27:59 +0000 | [diff] [blame] | 90 | return writeMetadata<2u>(OS, R.cpuid(), R.tsc()); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | Error FDRTraceWriter::visit(TSCWrapRecord &R) { |
| 94 | return writeMetadata<3u>(OS, R.tsc()); |
| 95 | } |
| 96 | |
| 97 | Error FDRTraceWriter::visit(CustomEventRecord &R) { |
Dean Michael Berris | 6b67ff0 | 2018-11-01 00:18:52 +0000 | [diff] [blame] | 98 | if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu())) |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 99 | return E; |
Dean Michael Berris | 6b67ff0 | 2018-11-01 00:18:52 +0000 | [diff] [blame] | 100 | auto D = R.data(); |
| 101 | ArrayRef<char> Bytes(D.data(), D.size()); |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 102 | OS.write(Bytes); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 103 | return Error::success(); |
| 104 | } |
| 105 | |
Dean Michael Berris | 59439dd | 2018-11-07 04:37:42 +0000 | [diff] [blame] | 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) { |
Dean Michael Berris | da375a6 | 2018-11-09 06:26:48 +0000 | [diff] [blame] | 116 | if (auto E = writeMetadata<8u>(OS, R.size(), R.delta(), R.eventType())) |
Dean Michael Berris | 59439dd | 2018-11-07 04:37:42 +0000 | [diff] [blame] | 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 | |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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) { |
Dean Michael Berris | c1dceee | 2018-08-31 17:49:59 +0000 | [diff] [blame] | 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()); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 149 | return Error::success(); |
Dean Michael Berris | 6b67ff0 | 2018-11-01 00:18:52 +0000 | [diff] [blame] | 150 | } |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 151 | |
| 152 | } // namespace xray |
| 153 | } // namespace llvm |