blob: c5224f4be094d30c7fa551ea08418d884de51457 [file] [log] [blame]
Dean Michael Berris20baaad2018-08-30 07:22:21 +00001//===- 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//===----------------------------------------------------------------------===//
13#include "llvm/XRay/FDRTraceWriter.h"
14#include <tuple>
15
16namespace llvm {
17namespace xray {
18
19namespace {
20
Dean Michael Berrisbff6dcb2018-08-31 16:08:38 +000021template <size_t Index> struct IndexedWriter {
Dean Michael Berris20baaad2018-08-30 07:22:21 +000022 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>
Dean Michael Berrisbff6dcb2018-08-31 16:08:38 +000028 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);
Dean Michael Berris20baaad2018-08-30 07:22:21 +000031 }
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>
Dean Michael Berrisbff6dcb2018-08-31 16:08:38 +000039 static size_t write(support::endian::Writer &OS, Tuple &&) {
40 return 0;
41 }
Dean Michael Berris20baaad2018-08-30 07:22:21 +000042};
43
Dean Michael Berrisb4ef1e82018-08-30 09:04:12 +000044template <uint8_t Kind, class... Values>
Dean Michael Berrisbff6dcb2018-08-31 16:08:38 +000045Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) {
Dean Michael Berris5de005f2018-11-09 06:26:48 +000046 // 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};
Dean Michael Berrisb4ef1e82018-08-30 09:04:12 +000049 auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);
Dean Michael Berrisbff6dcb2018-08-31 16:08:38 +000050 // 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');
Dean Michael Berris20baaad2018-08-30 07:22:21 +000057 return Error::success();
58}
59
60} // namespace
61
62FDRTraceWriter::FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H)
Dean Michael Berrisbff6dcb2018-08-31 16:08:38 +000063 : OS(O, support::endianness::native) {
Dean Michael Berris20baaad2018-08-30 07:22:21 +000064 // 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.
Dean Michael Berrisbff6dcb2018-08-31 16:08:38 +000066 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,
76 sizeof(XRayFileHeader::FreeFormData));
77 OS.write(FreeFormBytes);
Dean Michael Berris20baaad2018-08-30 07:22:21 +000078}
79
80FDRTraceWriter::~FDRTraceWriter() {}
81
82Error FDRTraceWriter::visit(BufferExtents &R) {
83 return writeMetadata<7u>(OS, R.size());
84}
85
86Error FDRTraceWriter::visit(WallclockRecord &R) {
87 return writeMetadata<4u>(OS, R.seconds(), R.nanos());
88}
89
90Error FDRTraceWriter::visit(NewCPUIDRecord &R) {
Dean Michael Berris5fd44bd2018-09-11 07:27:59 +000091 return writeMetadata<2u>(OS, R.cpuid(), R.tsc());
Dean Michael Berris20baaad2018-08-30 07:22:21 +000092}
93
94Error FDRTraceWriter::visit(TSCWrapRecord &R) {
95 return writeMetadata<3u>(OS, R.tsc());
96}
97
98Error FDRTraceWriter::visit(CustomEventRecord &R) {
Dean Michael Berris3fe1b122018-11-01 00:18:52 +000099 if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu()))
Dean Michael Berris20baaad2018-08-30 07:22:21 +0000100 return E;
Dean Michael Berris3fe1b122018-11-01 00:18:52 +0000101 auto D = R.data();
102 ArrayRef<char> Bytes(D.data(), D.size());
Dean Michael Berrisbff6dcb2018-08-31 16:08:38 +0000103 OS.write(Bytes);
Dean Michael Berris20baaad2018-08-30 07:22:21 +0000104 return Error::success();
105}
106
Dean Michael Berris8fa10fd2018-11-07 04:37:42 +0000107Error FDRTraceWriter::visit(CustomEventRecordV5 &R) {
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
116Error FDRTraceWriter::visit(TypedEventRecord &R) {
Dean Michael Berris5de005f2018-11-09 06:26:48 +0000117 if (auto E = writeMetadata<8u>(OS, R.size(), R.delta(), R.eventType()))
Dean Michael Berris8fa10fd2018-11-07 04:37:42 +0000118 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
Dean Michael Berris20baaad2018-08-30 07:22:21 +0000125Error FDRTraceWriter::visit(CallArgRecord &R) {
126 return writeMetadata<6u>(OS, R.arg());
127}
128
129Error FDRTraceWriter::visit(PIDRecord &R) {
130 return writeMetadata<9u>(OS, R.pid());
131}
132
133Error FDRTraceWriter::visit(NewBufferRecord &R) {
134 return writeMetadata<0u>(OS, R.tid());
135}
136
137Error FDRTraceWriter::visit(EndBufferRecord &R) {
138 return writeMetadata<1u>(OS, 0);
139}
140
141Error FDRTraceWriter::visit(FunctionRecord &R) {
Dean Michael Berris817cfcb2018-08-31 17:49:59 +0000142 // 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());
Dean Michael Berris20baaad2018-08-30 07:22:21 +0000150 return Error::success();
Dean Michael Berris3fe1b122018-11-01 00:18:52 +0000151}
Dean Michael Berris20baaad2018-08-30 07:22:21 +0000152
153} // namespace xray
154} // namespace llvm