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//===----------------------------------------------------------------------===//
12#include "llvm/XRay/FDRTraceWriter.h"
13#include <tuple>
14
15using namespace llvm;
16using namespace llvm::xray;
17
18namespace {
19
20template <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} // namespace
41
42template <uint8_t Kind, class... Values>
43static Error writeMetadata(support::endian::Writer &OS, Values &&...Ds) {
44 // The first bit in the first byte of metadata records is always set to 1, so
45 // we ensure this is the case when we write out the first byte of the record.
46 uint8_t FirstByte = (static_cast<uint8_t>(Kind) << 1) | uint8_t{0x01u};
47 auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);
48 // Write in field order.
49 OS.write(Val: 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(Val: '\0');
55 return Error::success();
56}
57
58FDRTraceWriter::FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H)
59 : OS(O, llvm::endianness::native) {
60 // We need to re-construct a header, by writing the fields we care about for
61 // traces, in the format that the runtime would have written.
62 uint32_t BitField =
63 (H.ConstantTSC ? 0x01 : 0x0) | (H.NonstopTSC ? 0x02 : 0x0);
64
65 // For endian-correctness, we need to write these fields in the order they
66 // appear and that we expect, instead of blasting bytes of the struct through.
67 OS.write(Val: H.Version);
68 OS.write(Val: H.Type);
69 OS.write(Val: BitField);
70 OS.write(Val: H.CycleFrequency);
71 ArrayRef<char> FreeFormBytes(H.FreeFormData,
72 sizeof(XRayFileHeader::FreeFormData));
73 OS.write(Val: FreeFormBytes);
74}
75
76FDRTraceWriter::~FDRTraceWriter() = default;
77
78Error FDRTraceWriter::visit(BufferExtents &R) {
79 return writeMetadata<7u>(OS, Ds: R.size());
80}
81
82Error FDRTraceWriter::visit(WallclockRecord &R) {
83 return writeMetadata<4u>(OS, Ds: R.seconds(), Ds: R.nanos());
84}
85
86Error FDRTraceWriter::visit(NewCPUIDRecord &R) {
87 return writeMetadata<2u>(OS, Ds: R.cpuid(), Ds: R.tsc());
88}
89
90Error FDRTraceWriter::visit(TSCWrapRecord &R) {
91 return writeMetadata<3u>(OS, Ds: R.tsc());
92}
93
94Error FDRTraceWriter::visit(CustomEventRecord &R) {
95 if (auto E = writeMetadata<5u>(OS, Ds: R.size(), Ds: R.tsc(), Ds: R.cpu()))
96 return E;
97 auto D = R.data();
98 ArrayRef<char> Bytes(D);
99 OS.write(Val: Bytes);
100 return Error::success();
101}
102
103Error FDRTraceWriter::visit(CustomEventRecordV5 &R) {
104 if (auto E = writeMetadata<5u>(OS, Ds: R.size(), Ds: R.delta()))
105 return E;
106 auto D = R.data();
107 ArrayRef<char> Bytes(D);
108 OS.write(Val: Bytes);
109 return Error::success();
110}
111
112Error FDRTraceWriter::visit(TypedEventRecord &R) {
113 if (auto E = writeMetadata<8u>(OS, Ds: R.size(), Ds: R.delta(), Ds: R.eventType()))
114 return E;
115 auto D = R.data();
116 ArrayRef<char> Bytes(D);
117 OS.write(Val: Bytes);
118 return Error::success();
119}
120
121Error FDRTraceWriter::visit(CallArgRecord &R) {
122 return writeMetadata<6u>(OS, Ds: R.arg());
123}
124
125Error FDRTraceWriter::visit(PIDRecord &R) {
126 return writeMetadata<9u>(OS, Ds: R.pid());
127}
128
129Error FDRTraceWriter::visit(NewBufferRecord &R) {
130 return writeMetadata<0u>(OS, Ds: R.tid());
131}
132
133Error FDRTraceWriter::visit(EndBufferRecord &R) {
134 return writeMetadata<1u>(OS, Ds: 0);
135}
136
137Error FDRTraceWriter::visit(FunctionRecord &R) {
138 // Write out the data in "field" order, to be endian-aware.
139 uint32_t TypeRecordFuncId = uint32_t{R.functionId() & ~uint32_t{0x0Fu << 28}};
140 TypeRecordFuncId <<= 3;
141 TypeRecordFuncId |= static_cast<uint32_t>(R.recordType());
142 TypeRecordFuncId <<= 1;
143 TypeRecordFuncId &= ~uint32_t{0x01};
144 OS.write(Val: TypeRecordFuncId);
145 OS.write(Val: R.delta());
146 return Error::success();
147}
148