blob: f50dc19b4be8d91deba67860b6615295f83d9af4 [file] [log] [blame]
Dean Michael Berrisa6c63432018-08-30 07:22:21 +00001//===- FDRTraceWriter.cpp - XRay FDR Trace Writer ---------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dean Michael Berrisa6c63432018-08-30 07:22:21 +00006//
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
15namespace llvm {
16namespace xray {
17
18namespace {
19
Dean Michael Berris98717972018-08-31 16:08:38 +000020template <size_t Index> struct IndexedWriter {
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000021 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>
Dean Michael Berris98717972018-08-31 16:08:38 +000027 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);
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000030 }
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>
Dean Michael Berris98717972018-08-31 16:08:38 +000038 static size_t write(support::endian::Writer &OS, Tuple &&) {
39 return 0;
40 }
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000041};
42
Dean Michael Berris17045972018-08-30 09:04:12 +000043template <uint8_t Kind, class... Values>
Dean Michael Berris98717972018-08-31 16:08:38 +000044Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) {
Dean Michael Berrisda375a62018-11-09 06:26:48 +000045 // 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};
Dean Michael Berris17045972018-08-30 09:04:12 +000048 auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);
Dean Michael Berris98717972018-08-31 16:08:38 +000049 // 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');
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000056 return Error::success();
57}
58
59} // namespace
60
61FDRTraceWriter::FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H)
Dean Michael Berris98717972018-08-31 16:08:38 +000062 : OS(O, support::endianness::native) {
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000063 // 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.
Dean Michael Berris98717972018-08-31 16:08:38 +000065 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,
75 sizeof(XRayFileHeader::FreeFormData));
76 OS.write(FreeFormBytes);
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000077}
78
79FDRTraceWriter::~FDRTraceWriter() {}
80
81Error FDRTraceWriter::visit(BufferExtents &R) {
82 return writeMetadata<7u>(OS, R.size());
83}
84
85Error FDRTraceWriter::visit(WallclockRecord &R) {
86 return writeMetadata<4u>(OS, R.seconds(), R.nanos());
87}
88
89Error FDRTraceWriter::visit(NewCPUIDRecord &R) {
Dean Michael Berrisec605d32018-09-11 07:27:59 +000090 return writeMetadata<2u>(OS, R.cpuid(), R.tsc());
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000091}
92
93Error FDRTraceWriter::visit(TSCWrapRecord &R) {
94 return writeMetadata<3u>(OS, R.tsc());
95}
96
97Error FDRTraceWriter::visit(CustomEventRecord &R) {
Dean Michael Berris6b67ff02018-11-01 00:18:52 +000098 if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu()))
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000099 return E;
Dean Michael Berris6b67ff02018-11-01 00:18:52 +0000100 auto D = R.data();
101 ArrayRef<char> Bytes(D.data(), D.size());
Dean Michael Berris98717972018-08-31 16:08:38 +0000102 OS.write(Bytes);
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000103 return Error::success();
104}
105
Dean Michael Berris59439dd2018-11-07 04:37:42 +0000106Error FDRTraceWriter::visit(CustomEventRecordV5 &R) {
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
115Error FDRTraceWriter::visit(TypedEventRecord &R) {
Dean Michael Berrisda375a62018-11-09 06:26:48 +0000116 if (auto E = writeMetadata<8u>(OS, R.size(), R.delta(), R.eventType()))
Dean Michael Berris59439dd2018-11-07 04:37:42 +0000117 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
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000124Error FDRTraceWriter::visit(CallArgRecord &R) {
125 return writeMetadata<6u>(OS, R.arg());
126}
127
128Error FDRTraceWriter::visit(PIDRecord &R) {
129 return writeMetadata<9u>(OS, R.pid());
130}
131
132Error FDRTraceWriter::visit(NewBufferRecord &R) {
133 return writeMetadata<0u>(OS, R.tid());
134}
135
136Error FDRTraceWriter::visit(EndBufferRecord &R) {
137 return writeMetadata<1u>(OS, 0);
138}
139
140Error FDRTraceWriter::visit(FunctionRecord &R) {
Dean Michael Berrisc1dceee2018-08-31 17:49:59 +0000141 // 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());
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000149 return Error::success();
Dean Michael Berris6b67ff02018-11-01 00:18:52 +0000150}
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000151
152} // namespace xray
153} // namespace llvm