blob: d0206e775a85db54eab3b8f9ec468e5aff4f461a [file] [log] [blame]
Dean Michael Berrisa6c63432018-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 Berris98717972018-08-31 16:08:38 +000021template <size_t Index> struct IndexedWriter {
Dean Michael Berrisa6c63432018-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 Berris98717972018-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 Berrisa6c63432018-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 Berris98717972018-08-31 16:08:38 +000039 static size_t write(support::endian::Writer &OS, Tuple &&) {
40 return 0;
41 }
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000042};
43
Dean Michael Berris17045972018-08-30 09:04:12 +000044template <uint8_t Kind, class... Values>
Dean Michael Berris98717972018-08-31 16:08:38 +000045Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) {
46 uint8_t FirstByte = (Kind << 1) | uint8_t{0x01};
Dean Michael Berris17045972018-08-30 09:04:12 +000047 auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);
Dean Michael Berris98717972018-08-31 16:08:38 +000048 // Write in field order.
49 OS.write(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('\0');
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000055 return Error::success();
56}
57
58} // namespace
59
60FDRTraceWriter::FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H)
Dean Michael Berris98717972018-08-31 16:08:38 +000061 : OS(O, support::endianness::native) {
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000062 // We need to re-construct a header, by writing the fields we care about for
63 // traces, in the format that the runtime would have written.
Dean Michael Berris98717972018-08-31 16:08:38 +000064 uint32_t BitField =
65 (H.ConstantTSC ? 0x01 : 0x0) | (H.NonstopTSC ? 0x02 : 0x0);
66
67 // For endian-correctness, we need to write these fields in the order they
68 // appear and that we expect, instead of blasting bytes of the struct through.
69 OS.write(H.Version);
70 OS.write(H.Type);
71 OS.write(BitField);
72 OS.write(H.CycleFrequency);
73 ArrayRef<char> FreeFormBytes(H.FreeFormData,
74 sizeof(XRayFileHeader::FreeFormData));
75 OS.write(FreeFormBytes);
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000076}
77
78FDRTraceWriter::~FDRTraceWriter() {}
79
80Error FDRTraceWriter::visit(BufferExtents &R) {
81 return writeMetadata<7u>(OS, R.size());
82}
83
84Error FDRTraceWriter::visit(WallclockRecord &R) {
85 return writeMetadata<4u>(OS, R.seconds(), R.nanos());
86}
87
88Error FDRTraceWriter::visit(NewCPUIDRecord &R) {
Dean Michael Berrisec605d32018-09-11 07:27:59 +000089 return writeMetadata<2u>(OS, R.cpuid(), R.tsc());
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000090}
91
92Error FDRTraceWriter::visit(TSCWrapRecord &R) {
93 return writeMetadata<3u>(OS, R.tsc());
94}
95
96Error FDRTraceWriter::visit(CustomEventRecord &R) {
97 if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc()))
98 return E;
Dean Michael Berris98717972018-08-31 16:08:38 +000099 ArrayRef<char> Bytes(R.data().data(), R.data().size());
100 OS.write(Bytes);
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000101 return Error::success();
102}
103
104Error FDRTraceWriter::visit(CallArgRecord &R) {
105 return writeMetadata<6u>(OS, R.arg());
106}
107
108Error FDRTraceWriter::visit(PIDRecord &R) {
109 return writeMetadata<9u>(OS, R.pid());
110}
111
112Error FDRTraceWriter::visit(NewBufferRecord &R) {
113 return writeMetadata<0u>(OS, R.tid());
114}
115
116Error FDRTraceWriter::visit(EndBufferRecord &R) {
117 return writeMetadata<1u>(OS, 0);
118}
119
120Error FDRTraceWriter::visit(FunctionRecord &R) {
Dean Michael Berrisc1dceee2018-08-31 17:49:59 +0000121 // Write out the data in "field" order, to be endian-aware.
122 uint32_t TypeRecordFuncId = uint32_t{R.functionId() & ~uint32_t{0x0Fu << 28}};
123 TypeRecordFuncId <<= 3;
124 TypeRecordFuncId |= static_cast<uint32_t>(R.recordType());
125 TypeRecordFuncId <<= 1;
126 TypeRecordFuncId &= ~uint32_t{0x01};
127 OS.write(TypeRecordFuncId);
128 OS.write(R.delta());
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000129 return Error::success();
Dean Michael Berrisc1dceee2018-08-31 17:49:59 +0000130} // namespace xray
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000131
132} // namespace xray
133} // namespace llvm