LLVM  14.0.0git
FDRTraceWriter.cpp
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
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  std::enable_if_t<(Index <
24  std::tuple_size<std::remove_reference_t<Tuple>>::value),
25  int> = 0>
26  static size_t write(support::endian::Writer &OS, Tuple &&T) {
27  OS.write(std::get<Index>(T));
28  return sizeof(std::get<Index>(T)) + IndexedWriter<Index + 1>::write(OS, T);
29  }
30 
31  template <
32  class Tuple,
33  std::enable_if_t<(Index >=
34  std::tuple_size<std::remove_reference_t<Tuple>>::value),
35  int> = 0>
36  static size_t write(support::endian::Writer &OS, Tuple &&) {
37  return 0;
38  }
39 };
40 
41 template <uint8_t Kind, class... Values>
42 Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) {
43  // The first bit in the first byte of metadata records is always set to 1, so
44  // we ensure this is the case when we write out the first byte of the record.
45  uint8_t FirstByte = (static_cast<uint8_t>(Kind) << 1) | uint8_t{0x01u};
46  auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);
47  // Write in field order.
48  OS.write(FirstByte);
49  auto Bytes = IndexedWriter<0>::write(OS, T);
50  assert(Bytes <= 15 && "Must only ever write at most 16 byte metadata!");
51  // Pad out with appropriate numbers of zero's.
52  for (; Bytes < 15; ++Bytes)
53  OS.write('\0');
54  return Error::success();
55 }
56 
57 } // namespace
58 
60  : OS(O, support::endianness::native) {
61  // We need to re-construct a header, by writing the fields we care about for
62  // traces, in the format that the runtime would have written.
63  uint32_t BitField =
64  (H.ConstantTSC ? 0x01 : 0x0) | (H.NonstopTSC ? 0x02 : 0x0);
65 
66  // For endian-correctness, we need to write these fields in the order they
67  // appear and that we expect, instead of blasting bytes of the struct through.
68  OS.write(H.Version);
69  OS.write(H.Type);
70  OS.write(BitField);
71  OS.write(H.CycleFrequency);
72  ArrayRef<char> FreeFormBytes(H.FreeFormData,
74  OS.write(FreeFormBytes);
75 }
76 
78 
80  return writeMetadata<7u>(OS, R.size());
81 }
82 
84  return writeMetadata<4u>(OS, R.seconds(), R.nanos());
85 }
86 
88  return writeMetadata<2u>(OS, R.cpuid(), R.tsc());
89 }
90 
92  return writeMetadata<3u>(OS, R.tsc());
93 }
94 
96  if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu()))
97  return E;
98  auto D = R.data();
99  ArrayRef<char> Bytes(D.data(), D.size());
100  OS.write(Bytes);
101  return Error::success();
102 }
103 
105  if (auto E = writeMetadata<5u>(OS, R.size(), R.delta()))
106  return E;
107  auto D = R.data();
108  ArrayRef<char> Bytes(D.data(), D.size());
109  OS.write(Bytes);
110  return Error::success();
111 }
112 
114  if (auto E = writeMetadata<8u>(OS, R.size(), R.delta(), R.eventType()))
115  return E;
116  auto D = R.data();
117  ArrayRef<char> Bytes(D.data(), D.size());
118  OS.write(Bytes);
119  return Error::success();
120 }
121 
123  return writeMetadata<6u>(OS, R.arg());
124 }
125 
127  return writeMetadata<9u>(OS, R.pid());
128 }
129 
131  return writeMetadata<0u>(OS, R.tid());
132 }
133 
135  return writeMetadata<1u>(OS, 0);
136 }
137 
139  // Write out the data in "field" order, to be endian-aware.
140  uint32_t TypeRecordFuncId = uint32_t{R.functionId() & ~uint32_t{0x0Fu << 28}};
141  TypeRecordFuncId <<= 3;
142  TypeRecordFuncId |= static_cast<uint32_t>(R.recordType());
143  TypeRecordFuncId <<= 1;
144  TypeRecordFuncId &= ~uint32_t{0x01};
145  OS.write(TypeRecordFuncId);
146  OS.write(R.delta());
147  return Error::success();
148 }
149 
150 } // namespace xray
151 } // namespace llvm
llvm::xray::FDRTraceWriter::~FDRTraceWriter
~FDRTraceWriter()
Definition: FDRTraceWriter.cpp:77
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::lltok::Error
@ Error
Definition: LLToken.h:21
llvm::xray::FunctionRecord
Definition: FDRRecords.h:372
llvm::write
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs)
Definition: DWP.cpp:535
llvm::xray::PIDRecord
Definition: FDRRecords.h:313
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
FDRTraceWriter.h
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::xray::BufferExtents
Definition: FDRRecords.h:110
llvm::xray::NewCPUIDRecord
Definition: FDRRecords.h:158
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::xray::NewBufferRecord
Definition: FDRRecords.h:336
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:192
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::xray::EndBufferRecord
Definition: FDRRecords.h:359
llvm::xray::WallclockRecord
Definition: FDRRecords.h:133
llvm::xray::CallArgRecord
Definition: FDRRecords.h:291
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::xray::TypedEventRecord
Definition: FDRRecords.h:262
llvm::xray::TSCWrapRecord
Definition: FDRRecords.h:184
llvm::ArrayRef< char >
uint32_t
H
#define H(x, y, z)
Definition: MD5.cpp:58
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::xray::XRayFileHeader::FreeFormData
char FreeFormData[16]
Definition: XRayRecord.h:50
llvm::xray::XRayFileHeader
XRay traces all have a header providing some top-matter information useful to help tools determine ho...
Definition: XRayRecord.h:27
llvm::xray::CustomEventRecord
Definition: FDRRecords.h:206
support
Reimplement select in terms of SEL *We would really like to support but we need to prove that the add doesn t need to overflow between the two bit chunks *Implement pre post increment support(e.g. PR935) *Implement smarter const ant generation for binops with large immediates. A few ARMv6T2 ops should be pattern matched
Definition: README.txt:10
llvm::xray::CustomEventRecordV5
Definition: FDRRecords.h:235
llvm::xray::FDRTraceWriter::visit
Error visit(BufferExtents &) override
Definition: FDRTraceWriter.cpp:79
llvm::support::endianness
endianness
Definition: Endian.h:27
llvm::xray::FDRTraceWriter::FDRTraceWriter
FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H)
Definition: FDRTraceWriter.cpp:59
write
static void write(bool isBE, void *P, T V)
Definition: RuntimeDyldELF.cpp:37