Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/XRay/FDRTraceWriterTest.cpp ----------------*- 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 "llvm/Support/raw_ostream.h" |
| 15 | #include "llvm/XRay/FDRLogBuilder.h" |
| 16 | #include "llvm/XRay/FDRRecords.h" |
| 17 | #include "llvm/XRay/Trace.h" |
| 18 | #include "gmock/gmock.h" |
| 19 | #include "gtest/gtest.h" |
| 20 | #include <string> |
| 21 | |
| 22 | namespace llvm { |
| 23 | namespace xray { |
| 24 | namespace { |
| 25 | |
| 26 | using testing::ElementsAre; |
| 27 | using testing::Eq; |
| 28 | using testing::Field; |
| 29 | using testing::IsEmpty; |
| 30 | using testing::Not; |
| 31 | |
| 32 | // We want to be able to create an instance of an FDRTraceWriter and associate |
| 33 | // it with a stream, which could be loaded and turned into a Trace instance. |
| 34 | // This test writes out version 3 trace logs. |
| 35 | TEST(FDRTraceWriterTest, WriteToStringBufferVersion3) { |
| 36 | std::string Data; |
| 37 | raw_string_ostream OS(Data); |
| 38 | XRayFileHeader H; |
| 39 | H.Version = 3; |
| 40 | H.Type = 1; |
| 41 | H.ConstantTSC = true; |
| 42 | H.NonstopTSC = true; |
| 43 | H.CycleFrequency = 3e9; |
| 44 | FDRTraceWriter Writer(OS, H); |
| 45 | auto L = LogBuilder() |
| 46 | .add<BufferExtents>(80) |
| 47 | .add<NewBufferRecord>(1) |
| 48 | .add<WallclockRecord>(1, 1) |
| 49 | .add<PIDRecord>(1) |
| 50 | .add<NewCPUIDRecord>(1) |
| 51 | .add<FunctionRecord>(RecordTypes::ENTER, 1, 1) |
| 52 | .add<FunctionRecord>(RecordTypes::EXIT, 1, 100) |
| 53 | .consume(); |
| 54 | for (auto &P : L) |
| 55 | ASSERT_FALSE(errorToBool(P->apply(Writer))); |
| 56 | OS.flush(); |
| 57 | |
| 58 | // Then from here we load the Trace file. |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame^] | 59 | DataExtractor DE(Data, sys::IsLittleEndianHost, 8); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 60 | auto TraceOrErr = loadTrace(DE, true); |
| 61 | if (!TraceOrErr) |
| 62 | FAIL() << TraceOrErr.takeError(); |
| 63 | auto &Trace = TraceOrErr.get(); |
| 64 | |
| 65 | ASSERT_THAT(Trace, Not(IsEmpty())); |
| 66 | ASSERT_THAT( |
| 67 | Trace, |
| 68 | ElementsAre(AllOf(Field(&XRayRecord::FuncId, Eq(1)), |
| 69 | Field(&XRayRecord::TId, Eq(1u)), |
| 70 | Field(&XRayRecord::CPU, Eq(1u)), |
| 71 | Field(&XRayRecord::Type, Eq(RecordTypes::ENTER))), |
| 72 | AllOf(Field(&XRayRecord::FuncId, Eq(1)), |
| 73 | Field(&XRayRecord::TId, Eq(1u)), |
| 74 | Field(&XRayRecord::CPU, Eq(1u)), |
| 75 | Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))))); |
| 76 | } |
| 77 | |
| 78 | // This version is almost exactly the same as above, except writing version 2 |
| 79 | // logs, without the PID records. |
| 80 | TEST(FDRTraceWriterTest, WriteToStringBufferVersion2) { |
| 81 | std::string Data; |
| 82 | raw_string_ostream OS(Data); |
| 83 | XRayFileHeader H; |
| 84 | H.Version = 2; |
| 85 | H.Type = 1; |
| 86 | H.ConstantTSC = true; |
| 87 | H.NonstopTSC = true; |
| 88 | H.CycleFrequency = 3e9; |
| 89 | FDRTraceWriter Writer(OS, H); |
| 90 | auto L = LogBuilder() |
| 91 | .add<BufferExtents>(64) |
| 92 | .add<NewBufferRecord>(1) |
| 93 | .add<WallclockRecord>(1, 1) |
| 94 | .add<NewCPUIDRecord>(1) |
| 95 | .add<FunctionRecord>(RecordTypes::ENTER, 1, 1) |
| 96 | .add<FunctionRecord>(RecordTypes::EXIT, 1, 100) |
| 97 | .consume(); |
| 98 | for (auto &P : L) |
| 99 | ASSERT_FALSE(errorToBool(P->apply(Writer))); |
| 100 | OS.flush(); |
| 101 | |
| 102 | // Then from here we load the Trace file. |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame^] | 103 | DataExtractor DE(Data, sys::IsLittleEndianHost, 8); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 104 | auto TraceOrErr = loadTrace(DE, true); |
| 105 | if (!TraceOrErr) |
| 106 | FAIL() << TraceOrErr.takeError(); |
| 107 | auto &Trace = TraceOrErr.get(); |
| 108 | |
| 109 | ASSERT_THAT(Trace, Not(IsEmpty())); |
| 110 | ASSERT_THAT( |
| 111 | Trace, |
| 112 | ElementsAre(AllOf(Field(&XRayRecord::FuncId, Eq(1)), |
| 113 | Field(&XRayRecord::TId, Eq(1u)), |
| 114 | Field(&XRayRecord::CPU, Eq(1u)), |
| 115 | Field(&XRayRecord::Type, Eq(RecordTypes::ENTER))), |
| 116 | AllOf(Field(&XRayRecord::FuncId, Eq(1)), |
| 117 | Field(&XRayRecord::TId, Eq(1u)), |
| 118 | Field(&XRayRecord::CPU, Eq(1u)), |
| 119 | Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))))); |
| 120 | } |
| 121 | |
| 122 | // This covers version 1 of the log, without a BufferExtents record but has an |
| 123 | // explicit EndOfBuffer record. |
| 124 | TEST(FDRTraceWriterTest, WriteToStringBufferVersion1) { |
| 125 | std::string Data; |
| 126 | raw_string_ostream OS(Data); |
| 127 | XRayFileHeader H; |
| 128 | H.Version = 1; |
| 129 | H.Type = 1; |
| 130 | H.ConstantTSC = true; |
| 131 | H.NonstopTSC = true; |
| 132 | H.CycleFrequency = 3e9; |
| 133 | // Write the size of buffers out, arbitrarily it's 4k. |
| 134 | constexpr uint64_t BufferSize = 4096; |
| 135 | std::memcpy(H.FreeFormData, reinterpret_cast<const char *>(&BufferSize), |
| 136 | sizeof(BufferSize)); |
| 137 | FDRTraceWriter Writer(OS, H); |
Dean Michael Berris | 7a07a41 | 2018-08-31 10:03:52 +0000 | [diff] [blame] | 138 | OS.flush(); |
| 139 | |
| 140 | // Ensure that at this point the Data buffer has the file header serialized |
| 141 | // size. |
| 142 | ASSERT_THAT(Data.size(), Eq(32u)); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 143 | auto L = LogBuilder() |
| 144 | .add<NewBufferRecord>(1) |
| 145 | .add<WallclockRecord>(1, 1) |
| 146 | .add<NewCPUIDRecord>(1) |
| 147 | .add<FunctionRecord>(RecordTypes::ENTER, 1, 1) |
| 148 | .add<FunctionRecord>(RecordTypes::EXIT, 1, 100) |
| 149 | .add<EndBufferRecord>() |
| 150 | .consume(); |
| 151 | for (auto &P : L) |
| 152 | ASSERT_FALSE(errorToBool(P->apply(Writer))); |
| 153 | |
| 154 | // We need to pad the buffer with 4016 (4096 - 80) bytes of zeros. |
| 155 | OS.write_zeros(4016); |
| 156 | OS.flush(); |
| 157 | |
Dean Michael Berris | 7a07a41 | 2018-08-31 10:03:52 +0000 | [diff] [blame] | 158 | // For version 1 of the log, we need the whole buffer to be the size of the |
| 159 | // file header plus 32. |
| 160 | ASSERT_THAT(Data.size(), Eq(BufferSize + 32)); |
| 161 | |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 162 | // Then from here we load the Trace file. |
Dean Michael Berris | 9871797 | 2018-08-31 16:08:38 +0000 | [diff] [blame^] | 163 | DataExtractor DE(Data, sys::IsLittleEndianHost, 8); |
Dean Michael Berris | a6c6343 | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 164 | auto TraceOrErr = loadTrace(DE, true); |
| 165 | if (!TraceOrErr) |
| 166 | FAIL() << TraceOrErr.takeError(); |
| 167 | auto &Trace = TraceOrErr.get(); |
| 168 | |
| 169 | ASSERT_THAT(Trace, Not(IsEmpty())); |
| 170 | ASSERT_THAT( |
| 171 | Trace, |
| 172 | ElementsAre(AllOf(Field(&XRayRecord::FuncId, Eq(1)), |
| 173 | Field(&XRayRecord::TId, Eq(1u)), |
| 174 | Field(&XRayRecord::CPU, Eq(1u)), |
| 175 | Field(&XRayRecord::Type, Eq(RecordTypes::ENTER))), |
| 176 | AllOf(Field(&XRayRecord::FuncId, Eq(1)), |
| 177 | Field(&XRayRecord::TId, Eq(1u)), |
| 178 | Field(&XRayRecord::CPU, Eq(1u)), |
| 179 | Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))))); |
| 180 | } |
| 181 | |
| 182 | } // namespace |
| 183 | } // namespace xray |
| 184 | } // namespace llvm |