Dean Michael Berris | 20baaad | 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) |
Dean Michael Berris | 2b5cea5 | 2018-09-11 06:36:51 +0000 | [diff] [blame] | 50 | .add<NewCPUIDRecord>(1, 2) |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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 | bff6dcb | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 59 | DataExtractor DE(Data, sys::IsLittleEndianHost, 8); |
Dean Michael Berris | 20baaad | 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())); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 66 | EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)), |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 67 | Field(&XRayRecord::FuncId, Eq(1)))); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 68 | EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)), |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 69 | Field(&XRayRecord::TId, Eq(1u)))); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 70 | EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::PId, Eq(1u)), |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 71 | Field(&XRayRecord::PId, Eq(1u)))); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 72 | EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)), |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 73 | Field(&XRayRecord::CPU, Eq(1u)))); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 74 | EXPECT_THAT(Trace, |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 75 | ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)), |
| 76 | Field(&XRayRecord::Type, Eq(RecordTypes::EXIT)))); |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // This version is almost exactly the same as above, except writing version 2 |
| 80 | // logs, without the PID records. |
| 81 | TEST(FDRTraceWriterTest, WriteToStringBufferVersion2) { |
| 82 | std::string Data; |
| 83 | raw_string_ostream OS(Data); |
| 84 | XRayFileHeader H; |
| 85 | H.Version = 2; |
| 86 | H.Type = 1; |
| 87 | H.ConstantTSC = true; |
| 88 | H.NonstopTSC = true; |
| 89 | H.CycleFrequency = 3e9; |
| 90 | FDRTraceWriter Writer(OS, H); |
| 91 | auto L = LogBuilder() |
| 92 | .add<BufferExtents>(64) |
| 93 | .add<NewBufferRecord>(1) |
| 94 | .add<WallclockRecord>(1, 1) |
Dean Michael Berris | 2b5cea5 | 2018-09-11 06:36:51 +0000 | [diff] [blame] | 95 | .add<NewCPUIDRecord>(1, 2) |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 96 | .add<FunctionRecord>(RecordTypes::ENTER, 1, 1) |
| 97 | .add<FunctionRecord>(RecordTypes::EXIT, 1, 100) |
| 98 | .consume(); |
| 99 | for (auto &P : L) |
| 100 | ASSERT_FALSE(errorToBool(P->apply(Writer))); |
| 101 | OS.flush(); |
| 102 | |
| 103 | // Then from here we load the Trace file. |
Dean Michael Berris | bff6dcb | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 104 | DataExtractor DE(Data, sys::IsLittleEndianHost, 8); |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 105 | auto TraceOrErr = loadTrace(DE, true); |
| 106 | if (!TraceOrErr) |
| 107 | FAIL() << TraceOrErr.takeError(); |
| 108 | auto &Trace = TraceOrErr.get(); |
| 109 | |
| 110 | ASSERT_THAT(Trace, Not(IsEmpty())); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 111 | EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)), |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 112 | Field(&XRayRecord::FuncId, Eq(1)))); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 113 | EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)), |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 114 | Field(&XRayRecord::TId, Eq(1u)))); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 115 | EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)), |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 116 | Field(&XRayRecord::CPU, Eq(1u)))); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 117 | EXPECT_THAT(Trace, |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 118 | ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)), |
| 119 | Field(&XRayRecord::Type, Eq(RecordTypes::EXIT)))); |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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 | 50365ce | 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 | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 143 | auto L = LogBuilder() |
| 144 | .add<NewBufferRecord>(1) |
| 145 | .add<WallclockRecord>(1, 1) |
Dean Michael Berris | 2b5cea5 | 2018-09-11 06:36:51 +0000 | [diff] [blame] | 146 | .add<NewCPUIDRecord>(1, 2) |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 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 | 50365ce | 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 | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 162 | // Then from here we load the Trace file. |
Dean Michael Berris | bff6dcb | 2018-08-31 16:08:38 +0000 | [diff] [blame] | 163 | DataExtractor DE(Data, sys::IsLittleEndianHost, 8); |
Dean Michael Berris | 20baaad | 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())); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 170 | EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)), |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 171 | Field(&XRayRecord::FuncId, Eq(1)))); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 172 | EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)), |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 173 | Field(&XRayRecord::TId, Eq(1u)))); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 174 | EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)), |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 175 | Field(&XRayRecord::CPU, Eq(1u)))); |
Dean Michael Berris | 0ce8202 | 2018-08-31 19:32:46 +0000 | [diff] [blame] | 176 | EXPECT_THAT(Trace, |
Dean Michael Berris | 889f703 | 2018-08-31 18:56:42 +0000 | [diff] [blame] | 177 | ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)), |
| 178 | Field(&XRayRecord::Type, Eq(RecordTypes::EXIT)))); |
Dean Michael Berris | 20baaad | 2018-08-30 07:22:21 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | } // namespace |
| 182 | } // namespace xray |
| 183 | } // namespace llvm |