LLVM  10.0.0svn
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  typename std::enable_if<
24  (Index <
25  std::tuple_size<typename std::remove_reference<Tuple>::type>::value),
26  int>::type = 0>
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);
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>
38  static size_t write(support::endian::Writer &OS, Tuple &&) {
39  return 0;
40  }
41 };
42 
43 template <uint8_t Kind, class... Values>
44 Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) {
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};
48  auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);
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');
56  return Error::success();
57 }
58 
59 } // namespace
60 
62  : OS(O, support::endianness::native) {
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.
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,
76  OS.write(FreeFormBytes);
77 }
78 
80 
82  return writeMetadata<7u>(OS, R.size());
83 }
84 
86  return writeMetadata<4u>(OS, R.seconds(), R.nanos());
87 }
88 
90  return writeMetadata<2u>(OS, R.cpuid(), R.tsc());
91 }
92 
94  return writeMetadata<3u>(OS, R.tsc());
95 }
96 
98  if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu()))
99  return E;
100  auto D = R.data();
101  ArrayRef<char> Bytes(D.data(), D.size());
102  OS.write(Bytes);
103  return Error::success();
104 }
105 
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 
116  if (auto E = writeMetadata<8u>(OS, R.size(), R.delta(), R.eventType()))
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 
125  return writeMetadata<6u>(OS, R.arg());
126 }
127 
129  return writeMetadata<9u>(OS, R.pid());
130 }
131 
133  return writeMetadata<0u>(OS, R.tid());
134 }
135 
137  return writeMetadata<1u>(OS, 0);
138 }
139 
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());
149  return Error::success();
150 }
151 
152 } // namespace xray
153 } // namespace llvm
int32_t functionId() const
Definition: FDRRecords.h:389
This class represents lattice values for constants.
Definition: AllocatorList.h:23
uint16_t cpuid() const
Definition: FDRRecords.h:173
RecordTypes recordType() const
Definition: FDRRecords.h:388
uint64_t arg() const
Definition: FDRRecords.h:304
int32_t pid() const
Definition: FDRRecords.h:327
uint64_t CycleFrequency
The number of cycles per second for the CPU that produced the timestamp counter (TSC) values...
Definition: XRayRecord.h:45
uint64_t seconds() const
Definition: FDRRecords.h:148
uint16_t Type
A numeric identifier for the type of file this is.
Definition: XRayRecord.h:33
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
Error visit(BufferExtents &) override
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
uint64_t tsc() const
Definition: FDRRecords.h:175
uint16_t eventType() const
Definition: FDRRecords.h:281
#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:40
static void write(bool isBE, void *P, T V)
uint64_t tsc() const
Definition: FDRRecords.h:197
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
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:282
uint32_t nanos() const
Definition: FDRRecords.h:149
StringRef data() const
Definition: FDRRecords.h:226
bool ConstantTSC
Whether the CPU that produced the timestamp counters (TSC) move at a constant rate.
Definition: XRayRecord.h:37
uint32_t delta() const
Definition: FDRRecords.h:390
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
uint64_t size() const
Definition: FDRRecords.h:124
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
XRay traces all have a header providing some top-matter information useful to help tools determine ho...
Definition: XRayRecord.h:27
uint16_t Version
Version of the XRay implementation that produced this file.
Definition: XRayRecord.h:29