[llvm-exegesis][NFC] Return many CodeTemplates instead of one.
[llvm-complete.git] / unittests / XRay / FDRTraceWriterTest.cpp
blobe78b71d6ff748d7b8f6502e62708628fdba20550
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.
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>
22 namespace llvm {
23 namespace xray {
24 namespace {
26 using testing::ElementsAre;
27 using testing::Eq;
28 using testing::Field;
29 using testing::IsEmpty;
30 using testing::Not;
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, 2)
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();
58 // Then from here we load the Trace file.
59 DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
60 auto TraceOrErr = loadTrace(DE, true);
61 if (!TraceOrErr)
62 FAIL() << TraceOrErr.takeError();
63 auto &Trace = TraceOrErr.get();
65 ASSERT_THAT(Trace, Not(IsEmpty()));
66 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
67 Field(&XRayRecord::FuncId, Eq(1))));
68 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
69 Field(&XRayRecord::TId, Eq(1u))));
70 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::PId, Eq(1u)),
71 Field(&XRayRecord::PId, Eq(1u))));
72 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
73 Field(&XRayRecord::CPU, Eq(1u))));
74 EXPECT_THAT(Trace,
75 ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
76 Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
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)
95 .add<NewCPUIDRecord>(1, 2)
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();
103 // Then from here we load the Trace file.
104 DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
105 auto TraceOrErr = loadTrace(DE, true);
106 if (!TraceOrErr)
107 FAIL() << TraceOrErr.takeError();
108 auto &Trace = TraceOrErr.get();
110 ASSERT_THAT(Trace, Not(IsEmpty()));
111 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
112 Field(&XRayRecord::FuncId, Eq(1))));
113 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
114 Field(&XRayRecord::TId, Eq(1u))));
115 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
116 Field(&XRayRecord::CPU, Eq(1u))));
117 EXPECT_THAT(Trace,
118 ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
119 Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
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);
138 OS.flush();
140 // Ensure that at this point the Data buffer has the file header serialized
141 // size.
142 ASSERT_THAT(Data.size(), Eq(32u));
143 auto L = LogBuilder()
144 .add<NewBufferRecord>(1)
145 .add<WallclockRecord>(1, 1)
146 .add<NewCPUIDRecord>(1, 2)
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)));
154 // We need to pad the buffer with 4016 (4096 - 80) bytes of zeros.
155 OS.write_zeros(4016);
156 OS.flush();
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));
162 // Then from here we load the Trace file.
163 DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
164 auto TraceOrErr = loadTrace(DE, true);
165 if (!TraceOrErr)
166 FAIL() << TraceOrErr.takeError();
167 auto &Trace = TraceOrErr.get();
169 ASSERT_THAT(Trace, Not(IsEmpty()));
170 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
171 Field(&XRayRecord::FuncId, Eq(1))));
172 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
173 Field(&XRayRecord::TId, Eq(1u))));
174 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
175 Field(&XRayRecord::CPU, Eq(1u))));
176 EXPECT_THAT(Trace,
177 ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
178 Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
181 } // namespace
182 } // namespace xray
183 } // namespace llvm