FDRTraceWriter.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. namespace llvm {
  15. namespace xray {
  16. namespace {
  17. template <size_t Index> struct IndexedWriter {
  18. template <
  19. class Tuple,
  20. std::enable_if_t<(Index <
  21. std::tuple_size<std::remove_reference_t<Tuple>>::value),
  22. int> = 0>
  23. static size_t write(support::endian::Writer &OS, Tuple &&T) {
  24. OS.write(std::get<Index>(T));
  25. return sizeof(std::get<Index>(T)) + IndexedWriter<Index + 1>::write(OS, T);
  26. }
  27. template <
  28. class Tuple,
  29. std::enable_if_t<(Index >=
  30. std::tuple_size<std::remove_reference_t<Tuple>>::value),
  31. int> = 0>
  32. static size_t write(support::endian::Writer &OS, Tuple &&) {
  33. return 0;
  34. }
  35. };
  36. template <uint8_t Kind, class... Values>
  37. Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) {
  38. // The first bit in the first byte of metadata records is always set to 1, so
  39. // we ensure this is the case when we write out the first byte of the record.
  40. uint8_t FirstByte = (static_cast<uint8_t>(Kind) << 1) | uint8_t{0x01u};
  41. auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);
  42. // Write in field order.
  43. OS.write(FirstByte);
  44. auto Bytes = IndexedWriter<0>::write(OS, T);
  45. assert(Bytes <= 15 && "Must only ever write at most 16 byte metadata!");
  46. // Pad out with appropriate numbers of zero's.
  47. for (; Bytes < 15; ++Bytes)
  48. OS.write('\0');
  49. return Error::success();
  50. }
  51. } // namespace
  52. FDRTraceWriter::FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H)
  53. : OS(O, support::endianness::native) {
  54. // We need to re-construct a header, by writing the fields we care about for
  55. // traces, in the format that the runtime would have written.
  56. uint32_t BitField =
  57. (H.ConstantTSC ? 0x01 : 0x0) | (H.NonstopTSC ? 0x02 : 0x0);
  58. // For endian-correctness, we need to write these fields in the order they
  59. // appear and that we expect, instead of blasting bytes of the struct through.
  60. OS.write(H.Version);
  61. OS.write(H.Type);
  62. OS.write(BitField);
  63. OS.write(H.CycleFrequency);
  64. ArrayRef<char> FreeFormBytes(H.FreeFormData,
  65. sizeof(XRayFileHeader::FreeFormData));
  66. OS.write(FreeFormBytes);
  67. }
  68. FDRTraceWriter::~FDRTraceWriter() = default;
  69. Error FDRTraceWriter::visit(BufferExtents &R) {
  70. return writeMetadata<7u>(OS, R.size());
  71. }
  72. Error FDRTraceWriter::visit(WallclockRecord &R) {
  73. return writeMetadata<4u>(OS, R.seconds(), R.nanos());
  74. }
  75. Error FDRTraceWriter::visit(NewCPUIDRecord &R) {
  76. return writeMetadata<2u>(OS, R.cpuid(), R.tsc());
  77. }
  78. Error FDRTraceWriter::visit(TSCWrapRecord &R) {
  79. return writeMetadata<3u>(OS, R.tsc());
  80. }
  81. Error FDRTraceWriter::visit(CustomEventRecord &R) {
  82. if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu()))
  83. return E;
  84. auto D = R.data();
  85. ArrayRef<char> Bytes(D.data(), D.size());
  86. OS.write(Bytes);
  87. return Error::success();
  88. }
  89. Error FDRTraceWriter::visit(CustomEventRecordV5 &R) {
  90. if (auto E = writeMetadata<5u>(OS, R.size(), R.delta()))
  91. return E;
  92. auto D = R.data();
  93. ArrayRef<char> Bytes(D.data(), D.size());
  94. OS.write(Bytes);
  95. return Error::success();
  96. }
  97. Error FDRTraceWriter::visit(TypedEventRecord &R) {
  98. if (auto E = writeMetadata<8u>(OS, R.size(), R.delta(), R.eventType()))
  99. return E;
  100. auto D = R.data();
  101. ArrayRef<char> Bytes(D.data(), D.size());
  102. OS.write(Bytes);
  103. return Error::success();
  104. }
  105. Error FDRTraceWriter::visit(CallArgRecord &R) {
  106. return writeMetadata<6u>(OS, R.arg());
  107. }
  108. Error FDRTraceWriter::visit(PIDRecord &R) {
  109. return writeMetadata<9u>(OS, R.pid());
  110. }
  111. Error FDRTraceWriter::visit(NewBufferRecord &R) {
  112. return writeMetadata<0u>(OS, R.tid());
  113. }
  114. Error FDRTraceWriter::visit(EndBufferRecord &R) {
  115. return writeMetadata<1u>(OS, 0);
  116. }
  117. Error FDRTraceWriter::visit(FunctionRecord &R) {
  118. // Write out the data in "field" order, to be endian-aware.
  119. uint32_t TypeRecordFuncId = uint32_t{R.functionId() & ~uint32_t{0x0Fu << 28}};
  120. TypeRecordFuncId <<= 3;
  121. TypeRecordFuncId |= static_cast<uint32_t>(R.recordType());
  122. TypeRecordFuncId <<= 1;
  123. TypeRecordFuncId &= ~uint32_t{0x01};
  124. OS.write(TypeRecordFuncId);
  125. OS.write(R.delta());
  126. return Error::success();
  127. }
  128. } // namespace xray
  129. } // namespace llvm