LLVM  8.0.1
FDRTraceWriter.cpp
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
14 #include <tuple>
15 
16 namespace llvm {
17 namespace xray {
18 
19 namespace {
20 
21 template <size_t Index> struct IndexedWriter {
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>
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);
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>
39  static size_t write(support::endian::Writer &OS, Tuple &&) {
40  return 0;
41  }
42 };
43 
44 template <uint8_t Kind, class... Values>
45 Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) {
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};
49  auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);
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');
57  return Error::success();
58 }
59 
60 } // namespace
61 
63  : OS(O, support::endianness::native) {
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.
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,
77  OS.write(FreeFormBytes);
78 }
79 
81 
83  return writeMetadata<7u>(OS, R.size());
84 }
85 
87  return writeMetadata<4u>(OS, R.seconds(), R.nanos());
88 }
89 
91  return writeMetadata<2u>(OS, R.cpuid(), R.tsc());
92 }
93 
95  return writeMetadata<3u>(OS, R.tsc());
96 }
97 
99  if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu()))
100  return E;
101  auto D = R.data();
102  ArrayRef<char> Bytes(D.data(), D.size());
103  OS.write(Bytes);
104  return Error::success();
105 }
106 
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 
117  if (auto E = writeMetadata<8u>(OS, R.size(), R.delta(), R.eventType()))
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 
126  return writeMetadata<6u>(OS, R.arg());
127 }
128 
130  return writeMetadata<9u>(OS, R.pid());
131 }
132 
134  return writeMetadata<0u>(OS, R.tid());
135 }
136 
138  return writeMetadata<1u>(OS, 0);
139 }
140 
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());
150  return Error::success();
151 }
152 
153 } // namespace xray
154 } // namespace llvm
int32_t functionId() const
Definition: FDRRecords.h:390
This class represents lattice values for constants.
Definition: AllocatorList.h:24
uint16_t cpuid() const
Definition: FDRRecords.h:174
RecordTypes recordType() const
Definition: FDRRecords.h:389
uint64_t arg() const
Definition: FDRRecords.h:305
int32_t pid() const
Definition: FDRRecords.h:328
uint64_t CycleFrequency
The number of cycles per second for the CPU that produced the timestamp counter (TSC) values...
Definition: XRayRecord.h:46
uint64_t seconds() const
Definition: FDRRecords.h:149
#define T
uint16_t Type
A numeric identifier for the type of file this is.
Definition: XRayRecord.h:34
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
Error visit(BufferExtents &) override
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
uint64_t tsc() const
Definition: FDRRecords.h:176
uint16_t eventType() const
Definition: FDRRecords.h:282
#define H(x, y, z)
Definition: MD5.cpp:57
bool NonstopTSC
Whether the CPU that produced the timestamp counters (TSC) do not stop.
Definition: XRayRecord.h:41
static void write(bool isBE, void *P, T V)
uint64_t tsc() const
Definition: FDRRecords.h:198
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
StringRef data() const
Definition: FDRRecords.h:283
uint32_t nanos() const
Definition: FDRRecords.h:150
StringRef data() const
Definition: FDRRecords.h:227
bool ConstantTSC
Whether the CPU that produced the timestamp counters (TSC) move at a constant rate.
Definition: XRayRecord.h:38
uint32_t delta() const
Definition: FDRRecords.h:391
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
uint64_t size() const
Definition: FDRRecords.h:125
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
XRay traces all have a header providing some top-matter information useful to help tools determine ho...
Definition: XRayRecord.h:28
uint16_t Version
Version of the XRay implementation that produced this file.
Definition: XRayRecord.h:30