[llvm] r341223 - [XRay] Make the FDRTraceWriter Endian-aware

Dean Michael Berris via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 31 09:08:38 PDT 2018


Author: dberris
Date: Fri Aug 31 09:08:38 2018
New Revision: 341223

URL: http://llvm.org/viewvc/llvm-project?rev=341223&view=rev
Log:
[XRay] Make the FDRTraceWriter Endian-aware

Before this patch, the FDRTraceWriter would not take endianness into
account when writing data into the output stream.

This is a follow-up to D51289 and D51210.

Modified:
    llvm/trunk/include/llvm/XRay/FDRTraceWriter.h
    llvm/trunk/lib/XRay/FDRTraceWriter.cpp
    llvm/trunk/unittests/XRay/FDRProducerConsumerTest.cpp
    llvm/trunk/unittests/XRay/FDRTraceWriterTest.cpp

Modified: llvm/trunk/include/llvm/XRay/FDRTraceWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/XRay/FDRTraceWriter.h?rev=341223&r1=341222&r2=341223&view=diff
==============================================================================
--- llvm/trunk/include/llvm/XRay/FDRTraceWriter.h (original)
+++ llvm/trunk/include/llvm/XRay/FDRTraceWriter.h Fri Aug 31 09:08:38 2018
@@ -14,6 +14,7 @@
 #define LLVM_INCLUDE_LLVM_XRAY_FDRTRACEWRITER_H_
 
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/EndianStream.h"
 #include "llvm/XRay/FDRRecords.h"
 #include "llvm/XRay/XRayRecord.h"
 
@@ -44,7 +45,7 @@ public:
   Error visit(FunctionRecord &) override;
 
 private:
-  raw_ostream &OS;
+  support::endian::Writer OS;
 };
 
 } // namespace xray

Modified: llvm/trunk/lib/XRay/FDRTraceWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/XRay/FDRTraceWriter.cpp?rev=341223&r1=341222&r2=341223&view=diff
==============================================================================
--- llvm/trunk/lib/XRay/FDRTraceWriter.cpp (original)
+++ llvm/trunk/lib/XRay/FDRTraceWriter.cpp Fri Aug 31 09:08:38 2018
@@ -39,19 +39,16 @@ struct FunctionDeltaBlob {
   uint32_t TSCDelta;
 };
 
-template <size_t Index> struct IndexedMemcpy {
+template <size_t Index> struct IndexedWriter {
   template <
       class Tuple,
       typename std::enable_if<
           (Index <
            std::tuple_size<typename std::remove_reference<Tuple>::type>::value),
           int>::type = 0>
-  static void Copy(char *Dest, Tuple &&T) {
-    auto Next = static_cast<char *>(std::memcpy(
-                    Dest, reinterpret_cast<const char *>(&std::get<Index>(T)),
-                    sizeof(std::get<Index>(T)))) +
-                sizeof(std::get<Index>(T));
-    IndexedMemcpy<Index + 1>::Copy(Next, T);
+  static size_t write(support::endian::Writer &OS, Tuple &&T) {
+    OS.write(std::get<Index>(T));
+    return sizeof(std::get<Index>(T)) + IndexedWriter<Index + 1>::write(OS, T);
   }
 
   template <
@@ -60,34 +57,43 @@ template <size_t Index> struct IndexedMe
           (Index >=
            std::tuple_size<typename std::remove_reference<Tuple>::type>::value),
           int>::type = 0>
-  static void Copy(char *, Tuple &&) {}
+  static size_t write(support::endian::Writer &OS, Tuple &&) {
+    return 0;
+  }
 };
 
 template <uint8_t Kind, class... Values>
-Error writeMetadata(raw_ostream &OS, Values &&... Ds) {
-  MetadataBlob B;
-  B.Type = 1;
-  B.RecordKind = Kind;
-  std::memset(B.Data, 0, 15);
+Error writeMetadata(support::endian::Writer &OS, Values &&... Ds) {
+  uint8_t FirstByte = (Kind << 1) | uint8_t{0x01};
   auto T = std::make_tuple(std::forward<Values>(std::move(Ds))...);
-  IndexedMemcpy<0>::Copy(B.Data, T);
-  OS.write(reinterpret_cast<const char *>(&B), sizeof(MetadataBlob));
+  // Write in field order.
+  OS.write(FirstByte);
+  auto Bytes = IndexedWriter<0>::write(OS, T);
+  assert(Bytes <= 15 && "Must only ever write at most 16 byte metadata!");
+  // Pad out with appropriate numbers of zero's.
+  for (; Bytes < 15; ++Bytes)
+    OS.write('\0');
   return Error::success();
 }
 
 } // namespace
 
 FDRTraceWriter::FDRTraceWriter(raw_ostream &O, const XRayFileHeader &H)
-    : OS(O) {
+    : OS(O, support::endianness::native) {
   // We need to re-construct a header, by writing the fields we care about for
   // traces, in the format that the runtime would have written.
-  FileHeader Raw;
-  Raw.Version = H.Version;
-  Raw.Type = H.Type;
-  Raw.BitField = (H.ConstantTSC ? 0x01 : 0x0) | (H.NonstopTSC ? 0x02 : 0x0);
-  Raw.CycleFrequency = H.CycleFrequency;
-  memcpy(&Raw.FreeForm, H.FreeFormData, 16);
-  OS.write(reinterpret_cast<const char *>(&Raw), sizeof(XRayFileHeader));
+  uint32_t BitField =
+      (H.ConstantTSC ? 0x01 : 0x0) | (H.NonstopTSC ? 0x02 : 0x0);
+
+  // For endian-correctness, we need to write these fields in the order they
+  // appear and that we expect, instead of blasting bytes of the struct through.
+  OS.write(H.Version);
+  OS.write(H.Type);
+  OS.write(BitField);
+  OS.write(H.CycleFrequency);
+  ArrayRef<char> FreeFormBytes(H.FreeFormData,
+                               sizeof(XRayFileHeader::FreeFormData));
+  OS.write(FreeFormBytes);
 }
 
 FDRTraceWriter::~FDRTraceWriter() {}
@@ -111,7 +117,8 @@ Error FDRTraceWriter::visit(TSCWrapRecor
 Error FDRTraceWriter::visit(CustomEventRecord &R) {
   if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc()))
     return E;
-  OS.write(R.data().data(), R.data().size());
+  ArrayRef<char> Bytes(R.data().data(), R.data().size());
+  OS.write(Bytes);
   return Error::success();
 }
 
@@ -137,7 +144,9 @@ Error FDRTraceWriter::visit(FunctionReco
   B.RecordKind = static_cast<uint8_t>(R.recordType());
   B.FuncId = R.functionId();
   B.TSCDelta = R.delta();
-  OS.write(reinterpret_cast<const char *>(&B), sizeof(FunctionDeltaBlob));
+  ArrayRef<char> Bytes(reinterpret_cast<const char *>(&B),
+                       sizeof(FunctionDeltaBlob));
+  OS.write(Bytes);
   return Error::success();
 }
 

Modified: llvm/trunk/unittests/XRay/FDRProducerConsumerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/XRay/FDRProducerConsumerTest.cpp?rev=341223&r1=341222&r2=341223&view=diff
==============================================================================
--- llvm/trunk/unittests/XRay/FDRProducerConsumerTest.cpp (original)
+++ llvm/trunk/unittests/XRay/FDRProducerConsumerTest.cpp Fri Aug 31 09:08:38 2018
@@ -99,7 +99,7 @@ TYPED_TEST_P(RoundTripTest, RoundTripsSi
   ASSERT_FALSE(errorToBool(R->apply(*this->Writer)));
   this->OS.flush();
 
-  DataExtractor DE(this->Data, true, 8);
+  DataExtractor DE(this->Data, sys::IsLittleEndianHost, 8);
   uint32_t OffsetPtr = 0;
   auto HeaderOrErr = readBinaryFormatHeader(DE, OffsetPtr);
   if (!HeaderOrErr)

Modified: llvm/trunk/unittests/XRay/FDRTraceWriterTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/XRay/FDRTraceWriterTest.cpp?rev=341223&r1=341222&r2=341223&view=diff
==============================================================================
--- llvm/trunk/unittests/XRay/FDRTraceWriterTest.cpp (original)
+++ llvm/trunk/unittests/XRay/FDRTraceWriterTest.cpp Fri Aug 31 09:08:38 2018
@@ -56,7 +56,7 @@ TEST(FDRTraceWriterTest, WriteToStringBu
   OS.flush();
 
   // Then from here we load the Trace file.
-  DataExtractor DE(Data, true, 8);
+  DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
   auto TraceOrErr = loadTrace(DE, true);
   if (!TraceOrErr)
     FAIL() << TraceOrErr.takeError();
@@ -100,7 +100,7 @@ TEST(FDRTraceWriterTest, WriteToStringBu
   OS.flush();
 
   // Then from here we load the Trace file.
-  DataExtractor DE(Data, true, 8);
+  DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
   auto TraceOrErr = loadTrace(DE, true);
   if (!TraceOrErr)
     FAIL() << TraceOrErr.takeError();
@@ -160,7 +160,7 @@ TEST(FDRTraceWriterTest, WriteToStringBu
   ASSERT_THAT(Data.size(), Eq(BufferSize + 32));
 
   // Then from here we load the Trace file.
-  DataExtractor DE(Data, true, 8);
+  DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
   auto TraceOrErr = loadTrace(DE, true);
   if (!TraceOrErr)
     FAIL() << TraceOrErr.takeError();




More information about the llvm-commits mailing list