blob: 4f2f06e468c68f6ad817d2be2311de7e5eddc6ce [file] [log] [blame]
Dean Michael Berrisa6c63432018-08-30 07:22:21 +00001//===- llvm/unittest/XRay/FDRTraceWriterTest.cpp ----------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dean Michael Berrisa6c63432018-08-30 07:22:21 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Test a utility that can write out XRay FDR Mode formatted trace files.
10//
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"
19#include <string>
20
21namespace llvm {
22namespace xray {
23namespace {
24
25using testing::ElementsAre;
26using testing::Eq;
27using testing::Field;
28using testing::IsEmpty;
29using testing::Not;
30
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.
34TEST(FDRTraceWriterTest, WriteToStringBufferVersion3) {
35 std::string Data;
36 raw_string_ostream OS(Data);
37 XRayFileHeader H;
38 H.Version = 3;
39 H.Type = 1;
40 H.ConstantTSC = true;
41 H.NonstopTSC = true;
42 H.CycleFrequency = 3e9;
43 FDRTraceWriter Writer(OS, H);
44 auto L = LogBuilder()
45 .add<BufferExtents>(80)
46 .add<NewBufferRecord>(1)
47 .add<WallclockRecord>(1, 1)
48 .add<PIDRecord>(1)
Dean Michael Berrisd2c50402018-09-11 06:36:51 +000049 .add<NewCPUIDRecord>(1, 2)
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000050 .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
51 .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
52 .consume();
53 for (auto &P : L)
54 ASSERT_FALSE(errorToBool(P->apply(Writer)));
55 OS.flush();
56
57 // Then from here we load the Trace file.
Dean Michael Berris98717972018-08-31 16:08:38 +000058 DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000059 auto TraceOrErr = loadTrace(DE, true);
60 if (!TraceOrErr)
61 FAIL() << TraceOrErr.takeError();
62 auto &Trace = TraceOrErr.get();
63
64 ASSERT_THAT(Trace, Not(IsEmpty()));
Dean Michael Berris4cae0482018-08-31 19:32:46 +000065 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
Dean Michael Berris7975e272018-08-31 18:56:42 +000066 Field(&XRayRecord::FuncId, Eq(1))));
Dean Michael Berris4cae0482018-08-31 19:32:46 +000067 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
Dean Michael Berris7975e272018-08-31 18:56:42 +000068 Field(&XRayRecord::TId, Eq(1u))));
Dean Michael Berris4cae0482018-08-31 19:32:46 +000069 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::PId, Eq(1u)),
Dean Michael Berris7975e272018-08-31 18:56:42 +000070 Field(&XRayRecord::PId, Eq(1u))));
Dean Michael Berris4cae0482018-08-31 19:32:46 +000071 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
Dean Michael Berris7975e272018-08-31 18:56:42 +000072 Field(&XRayRecord::CPU, Eq(1u))));
Dean Michael Berris4cae0482018-08-31 19:32:46 +000073 EXPECT_THAT(Trace,
Dean Michael Berris7975e272018-08-31 18:56:42 +000074 ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
75 Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000076}
77
78// This version is almost exactly the same as above, except writing version 2
79// logs, without the PID records.
80TEST(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)
Dean Michael Berrisd2c50402018-09-11 06:36:51 +000094 .add<NewCPUIDRecord>(1, 2)
Dean Michael Berrisa6c63432018-08-30 07:22:21 +000095 .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 Berris98717972018-08-31 16:08:38 +0000103 DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000104 auto TraceOrErr = loadTrace(DE, true);
105 if (!TraceOrErr)
106 FAIL() << TraceOrErr.takeError();
107 auto &Trace = TraceOrErr.get();
108
109 ASSERT_THAT(Trace, Not(IsEmpty()));
Dean Michael Berris4cae0482018-08-31 19:32:46 +0000110 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
Dean Michael Berris7975e272018-08-31 18:56:42 +0000111 Field(&XRayRecord::FuncId, Eq(1))));
Dean Michael Berris4cae0482018-08-31 19:32:46 +0000112 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
Dean Michael Berris7975e272018-08-31 18:56:42 +0000113 Field(&XRayRecord::TId, Eq(1u))));
Dean Michael Berris4cae0482018-08-31 19:32:46 +0000114 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
Dean Michael Berris7975e272018-08-31 18:56:42 +0000115 Field(&XRayRecord::CPU, Eq(1u))));
Dean Michael Berris4cae0482018-08-31 19:32:46 +0000116 EXPECT_THAT(Trace,
Dean Michael Berris7975e272018-08-31 18:56:42 +0000117 ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
118 Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000119}
120
121// This covers version 1 of the log, without a BufferExtents record but has an
122// explicit EndOfBuffer record.
123TEST(FDRTraceWriterTest, WriteToStringBufferVersion1) {
124 std::string Data;
125 raw_string_ostream OS(Data);
126 XRayFileHeader H;
127 H.Version = 1;
128 H.Type = 1;
129 H.ConstantTSC = true;
130 H.NonstopTSC = 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),
135 sizeof(BufferSize));
136 FDRTraceWriter Writer(OS, H);
Dean Michael Berris7a07a412018-08-31 10:03:52 +0000137 OS.flush();
138
139 // Ensure that at this point the Data buffer has the file header serialized
140 // size.
141 ASSERT_THAT(Data.size(), Eq(32u));
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000142 auto L = LogBuilder()
143 .add<NewBufferRecord>(1)
144 .add<WallclockRecord>(1, 1)
Dean Michael Berrisd2c50402018-09-11 06:36:51 +0000145 .add<NewCPUIDRecord>(1, 2)
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000146 .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
147 .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
148 .add<EndBufferRecord>()
149 .consume();
150 for (auto &P : L)
151 ASSERT_FALSE(errorToBool(P->apply(Writer)));
152
153 // We need to pad the buffer with 4016 (4096 - 80) bytes of zeros.
154 OS.write_zeros(4016);
155 OS.flush();
156
Dean Michael Berris7a07a412018-08-31 10:03:52 +0000157 // 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));
160
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000161 // Then from here we load the Trace file.
Dean Michael Berris98717972018-08-31 16:08:38 +0000162 DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000163 auto TraceOrErr = loadTrace(DE, true);
164 if (!TraceOrErr)
165 FAIL() << TraceOrErr.takeError();
166 auto &Trace = TraceOrErr.get();
167
168 ASSERT_THAT(Trace, Not(IsEmpty()));
Dean Michael Berris4cae0482018-08-31 19:32:46 +0000169 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
Dean Michael Berris7975e272018-08-31 18:56:42 +0000170 Field(&XRayRecord::FuncId, Eq(1))));
Dean Michael Berris4cae0482018-08-31 19:32:46 +0000171 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
Dean Michael Berris7975e272018-08-31 18:56:42 +0000172 Field(&XRayRecord::TId, Eq(1u))));
Dean Michael Berris4cae0482018-08-31 19:32:46 +0000173 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
Dean Michael Berris7975e272018-08-31 18:56:42 +0000174 Field(&XRayRecord::CPU, Eq(1u))));
Dean Michael Berris4cae0482018-08-31 19:32:46 +0000175 EXPECT_THAT(Trace,
Dean Michael Berris7975e272018-08-31 18:56:42 +0000176 ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
177 Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
Dean Michael Berrisa6c63432018-08-30 07:22:21 +0000178}
179
180} // namespace
181} // namespace xray
182} // namespace llvm