1 //===- llvm/unittest/XRay/FDRTraceWriterTest.cpp ----------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Test a utility that can write out XRay FDR Mode formatted trace files.
11 //===----------------------------------------------------------------------===//
12 #include "llvm/XRay/FDRTraceWriter.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "llvm/XRay/FDRLogBuilder.h"
15 #include "llvm/XRay/FDRRecords.h"
16 #include "llvm/XRay/Trace.h"
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
25 using testing::ElementsAre
;
28 using testing::IsEmpty
;
31 // We want to be able to create an instance of an FDRTraceWriter and associate
32 // it with a stream, which could be loaded and turned into a Trace instance.
33 // This test writes out version 3 trace logs.
34 TEST(FDRTraceWriterTest
, WriteToStringBufferVersion3
) {
36 raw_string_ostream
OS(Data
);
42 H
.CycleFrequency
= 3e9
;
43 FDRTraceWriter
Writer(OS
, H
);
45 .add
<BufferExtents
>(80)
46 .add
<NewBufferRecord
>(1)
47 .add
<WallclockRecord
>(1, 1)
49 .add
<NewCPUIDRecord
>(1, 2)
50 .add
<FunctionRecord
>(RecordTypes::ENTER
, 1, 1)
51 .add
<FunctionRecord
>(RecordTypes::EXIT
, 1, 100)
54 ASSERT_FALSE(errorToBool(P
->apply(Writer
)));
57 // Then from here we load the Trace file.
58 DataExtractor
DE(Data
, sys::IsLittleEndianHost
, 8);
59 auto TraceOrErr
= loadTrace(DE
, true);
61 FAIL() << TraceOrErr
.takeError();
62 auto &Trace
= TraceOrErr
.get();
64 ASSERT_THAT(Trace
, Not(IsEmpty()));
65 EXPECT_THAT(Trace
, ElementsAre(Field(&XRayRecord::FuncId
, Eq(1)),
66 Field(&XRayRecord::FuncId
, Eq(1))));
67 EXPECT_THAT(Trace
, ElementsAre(Field(&XRayRecord::TId
, Eq(1u)),
68 Field(&XRayRecord::TId
, Eq(1u))));
69 EXPECT_THAT(Trace
, ElementsAre(Field(&XRayRecord::PId
, Eq(1u)),
70 Field(&XRayRecord::PId
, Eq(1u))));
71 EXPECT_THAT(Trace
, ElementsAre(Field(&XRayRecord::CPU
, Eq(1u)),
72 Field(&XRayRecord::CPU
, Eq(1u))));
74 ElementsAre(Field(&XRayRecord::Type
, Eq(RecordTypes::ENTER
)),
75 Field(&XRayRecord::Type
, Eq(RecordTypes::EXIT
))));
78 // This version is almost exactly the same as above, except writing version 2
79 // logs, without the PID records.
80 TEST(FDRTraceWriterTest
, WriteToStringBufferVersion2
) {
82 raw_string_ostream
OS(Data
);
88 H
.CycleFrequency
= 3e9
;
89 FDRTraceWriter
Writer(OS
, H
);
91 .add
<BufferExtents
>(64)
92 .add
<NewBufferRecord
>(1)
93 .add
<WallclockRecord
>(1, 1)
94 .add
<NewCPUIDRecord
>(1, 2)
95 .add
<FunctionRecord
>(RecordTypes::ENTER
, 1, 1)
96 .add
<FunctionRecord
>(RecordTypes::EXIT
, 1, 100)
99 ASSERT_FALSE(errorToBool(P
->apply(Writer
)));
102 // Then from here we load the Trace file.
103 DataExtractor
DE(Data
, sys::IsLittleEndianHost
, 8);
104 auto TraceOrErr
= loadTrace(DE
, true);
106 FAIL() << TraceOrErr
.takeError();
107 auto &Trace
= TraceOrErr
.get();
109 ASSERT_THAT(Trace
, Not(IsEmpty()));
110 EXPECT_THAT(Trace
, ElementsAre(Field(&XRayRecord::FuncId
, Eq(1)),
111 Field(&XRayRecord::FuncId
, Eq(1))));
112 EXPECT_THAT(Trace
, ElementsAre(Field(&XRayRecord::TId
, Eq(1u)),
113 Field(&XRayRecord::TId
, Eq(1u))));
114 EXPECT_THAT(Trace
, ElementsAre(Field(&XRayRecord::CPU
, Eq(1u)),
115 Field(&XRayRecord::CPU
, Eq(1u))));
117 ElementsAre(Field(&XRayRecord::Type
, Eq(RecordTypes::ENTER
)),
118 Field(&XRayRecord::Type
, Eq(RecordTypes::EXIT
))));
121 // This covers version 1 of the log, without a BufferExtents record but has an
122 // explicit EndOfBuffer record.
123 TEST(FDRTraceWriterTest
, WriteToStringBufferVersion1
) {
125 raw_string_ostream
OS(Data
);
129 H
.ConstantTSC
= true;
131 H
.CycleFrequency
= 3e9
;
132 // Write the size of buffers out, arbitrarily it's 4k.
133 constexpr uint64_t BufferSize
= 4096;
134 std::memcpy(H
.FreeFormData
, reinterpret_cast<const char *>(&BufferSize
),
136 FDRTraceWriter
Writer(OS
, H
);
139 // Ensure that at this point the Data buffer has the file header serialized
141 ASSERT_THAT(Data
.size(), Eq(32u));
142 auto L
= LogBuilder()
143 .add
<NewBufferRecord
>(1)
144 .add
<WallclockRecord
>(1, 1)
145 .add
<NewCPUIDRecord
>(1, 2)
146 .add
<FunctionRecord
>(RecordTypes::ENTER
, 1, 1)
147 .add
<FunctionRecord
>(RecordTypes::EXIT
, 1, 100)
148 .add
<EndBufferRecord
>()
151 ASSERT_FALSE(errorToBool(P
->apply(Writer
)));
153 // We need to pad the buffer with 4016 (4096 - 80) bytes of zeros.
154 OS
.write_zeros(4016);
157 // For version 1 of the log, we need the whole buffer to be the size of the
158 // file header plus 32.
159 ASSERT_THAT(Data
.size(), Eq(BufferSize
+ 32));
161 // Then from here we load the Trace file.
162 DataExtractor
DE(Data
, sys::IsLittleEndianHost
, 8);
163 auto TraceOrErr
= loadTrace(DE
, true);
165 FAIL() << TraceOrErr
.takeError();
166 auto &Trace
= TraceOrErr
.get();
168 ASSERT_THAT(Trace
, Not(IsEmpty()));
169 EXPECT_THAT(Trace
, ElementsAre(Field(&XRayRecord::FuncId
, Eq(1)),
170 Field(&XRayRecord::FuncId
, Eq(1))));
171 EXPECT_THAT(Trace
, ElementsAre(Field(&XRayRecord::TId
, Eq(1u)),
172 Field(&XRayRecord::TId
, Eq(1u))));
173 EXPECT_THAT(Trace
, ElementsAre(Field(&XRayRecord::CPU
, Eq(1u)),
174 Field(&XRayRecord::CPU
, Eq(1u))));
176 ElementsAre(Field(&XRayRecord::Type
, Eq(RecordTypes::ENTER
)),
177 Field(&XRayRecord::Type
, Eq(RecordTypes::EXIT
))));