Path: blob/main/contrib/llvm-project/llvm/lib/XRay/FDRTraceWriter.cpp
35233 views
//===- FDRTraceWriter.cpp - XRay FDR Trace Writer ---------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Test a utility that can write out XRay FDR Mode formatted trace files.9//10//===----------------------------------------------------------------------===//11#include "llvm/XRay/FDRTraceWriter.h"12#include <tuple>1314namespace llvm {15namespace xray {1617namespace {1819template <size_t Index> struct IndexedWriter {20template <21class Tuple,22std::enable_if_t<(Index <23std::tuple_size<std::remove_reference_t<Tuple>>::value),24int> = 0>25static size_t write(support::endian::Writer &OS, Tuple &&T) {26OS.write(std::get<Index>(T));27return sizeof(std::get<Index>(T)) + IndexedWriter<Index + 1>::write(OS, T);28}2930template <31class Tuple,32std::enable_if_t<(Index >=33std::tuple_size<std::remove_reference_t<Tuple>>::value),34int> = 0>35static size_t write(support::endian::Writer &OS, Tuple &&) {36return 0;37}38};3940template <uint8_t Kind, class... Values>41Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) {42// The first bit in the first byte of metadata records is always set to 1, so43// we ensure this is the case when we write out the first byte of the record.44uint8_t FirstByte = (static_cast<uint8_t>(Kind) << 1) | uint8_t{0x01u};45auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);46// Write in field order.47OS.write(FirstByte);48auto Bytes = IndexedWriter<0>::write(OS, T);49assert(Bytes <= 15 && "Must only ever write at most 16 byte metadata!");50// Pad out with appropriate numbers of zero's.51for (; Bytes < 15; ++Bytes)52OS.write('\0');53return Error::success();54}5556} // namespace5758FDRTraceWriter::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 for61// traces, in the format that the runtime would have written.62uint32_t BitField =63(H.ConstantTSC ? 0x01 : 0x0) | (H.NonstopTSC ? 0x02 : 0x0);6465// For endian-correctness, we need to write these fields in the order they66// appear and that we expect, instead of blasting bytes of the struct through.67OS.write(H.Version);68OS.write(H.Type);69OS.write(BitField);70OS.write(H.CycleFrequency);71ArrayRef<char> FreeFormBytes(H.FreeFormData,72sizeof(XRayFileHeader::FreeFormData));73OS.write(FreeFormBytes);74}7576FDRTraceWriter::~FDRTraceWriter() = default;7778Error FDRTraceWriter::visit(BufferExtents &R) {79return writeMetadata<7u>(OS, R.size());80}8182Error FDRTraceWriter::visit(WallclockRecord &R) {83return writeMetadata<4u>(OS, R.seconds(), R.nanos());84}8586Error FDRTraceWriter::visit(NewCPUIDRecord &R) {87return writeMetadata<2u>(OS, R.cpuid(), R.tsc());88}8990Error FDRTraceWriter::visit(TSCWrapRecord &R) {91return writeMetadata<3u>(OS, R.tsc());92}9394Error FDRTraceWriter::visit(CustomEventRecord &R) {95if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu()))96return E;97auto D = R.data();98ArrayRef<char> Bytes(D.data(), D.size());99OS.write(Bytes);100return Error::success();101}102103Error FDRTraceWriter::visit(CustomEventRecordV5 &R) {104if (auto E = writeMetadata<5u>(OS, R.size(), R.delta()))105return E;106auto D = R.data();107ArrayRef<char> Bytes(D.data(), D.size());108OS.write(Bytes);109return Error::success();110}111112Error FDRTraceWriter::visit(TypedEventRecord &R) {113if (auto E = writeMetadata<8u>(OS, R.size(), R.delta(), R.eventType()))114return E;115auto D = R.data();116ArrayRef<char> Bytes(D.data(), D.size());117OS.write(Bytes);118return Error::success();119}120121Error FDRTraceWriter::visit(CallArgRecord &R) {122return writeMetadata<6u>(OS, R.arg());123}124125Error FDRTraceWriter::visit(PIDRecord &R) {126return writeMetadata<9u>(OS, R.pid());127}128129Error FDRTraceWriter::visit(NewBufferRecord &R) {130return writeMetadata<0u>(OS, R.tid());131}132133Error FDRTraceWriter::visit(EndBufferRecord &R) {134return writeMetadata<1u>(OS, 0);135}136137Error FDRTraceWriter::visit(FunctionRecord &R) {138// Write out the data in "field" order, to be endian-aware.139uint32_t TypeRecordFuncId = uint32_t{R.functionId() & ~uint32_t{0x0Fu << 28}};140TypeRecordFuncId <<= 3;141TypeRecordFuncId |= static_cast<uint32_t>(R.recordType());142TypeRecordFuncId <<= 1;143TypeRecordFuncId &= ~uint32_t{0x01};144OS.write(TypeRecordFuncId);145OS.write(R.delta());146return Error::success();147}148149} // namespace xray150} // namespace llvm151152153