1 //===- llvm/unittest/XRay/FDRTraceWriterTest.cpp ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
26 using testing::ElementsAre
;
29 using testing::IsEmpty
;
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
) {
37 raw_string_ostream
OS(Data
);
43 H
.CycleFrequency
= 3e9
;
44 FDRTraceWriter
Writer(OS
, H
);
46 .add
<BufferExtents
>(80)
47 .add
<NewBufferRecord
>(1)
48 .add
<WallclockRecord
>(1, 1)
50 .add
<NewCPUIDRecord
>(1, 2)
51 .add
<FunctionRecord
>(RecordTypes::ENTER
, 1, 1)
52 .add
<FunctionRecord
>(RecordTypes::EXIT
, 1, 100)
55 ASSERT_FALSE(errorToBool(P
->apply(Writer
)));
58 // Then from here we load the Trace file.
59 DataExtractor
DE(Data
, sys::IsLittleEndianHost
, 8);
60 auto TraceOrErr
= loadTrace(DE
, true);
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))));
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
) {
83 raw_string_ostream
OS(Data
);
89 H
.CycleFrequency
= 3e9
;
90 FDRTraceWriter
Writer(OS
, H
);
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)
100 ASSERT_FALSE(errorToBool(P
->apply(Writer
)));
103 // Then from here we load the Trace file.
104 DataExtractor
DE(Data
, sys::IsLittleEndianHost
, 8);
105 auto TraceOrErr
= loadTrace(DE
, true);
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))));
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
) {
126 raw_string_ostream
OS(Data
);
130 H
.ConstantTSC
= 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
),
137 FDRTraceWriter
Writer(OS
, H
);
140 // Ensure that at this point the Data buffer has the file header serialized
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
>()
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);
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);
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))));
177 ElementsAre(Field(&XRayRecord::Type
, Eq(RecordTypes::ENTER
)),
178 Field(&XRayRecord::Type
, Eq(RecordTypes::EXIT
))));