blob: e78b71d6ff748d7b8f6502e62708628fdba20550 [file] [log] [blame]
Dean Michael Berris20baaad2018-08-30 07:22:21 +00001//===- 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
22namespace llvm {
23namespace xray {
24namespace {
25
26using testing::ElementsAre;
27using testing::Eq;
28using testing::Field;
29using testing::IsEmpty;
30using 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.
35TEST(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 Berris2b5cea52018-09-11 06:36:51 +000050 .add<NewCPUIDRecord>(1, 2)
Dean Michael Berris20baaad2018-08-30 07:22:21 +000051 .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 Berrisbff6dcb2018-08-31 16:08:38 +000059 DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
Dean Michael Berris20baaad2018-08-30 07:22:21 +000060 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 Berris0ce82022018-08-31 19:32:46 +000066 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
Dean Michael Berris889f7032018-08-31 18:56:42 +000067 Field(&XRayRecord::FuncId, Eq(1))));
Dean Michael Berris0ce82022018-08-31 19:32:46 +000068 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
Dean Michael Berris889f7032018-08-31 18:56:42 +000069 Field(&XRayRecord::TId, Eq(1u))));
Dean Michael Berris0ce82022018-08-31 19:32:46 +000070 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::PId, Eq(1u)),
Dean Michael Berris889f7032018-08-31 18:56:42 +000071 Field(&XRayRecord::PId, Eq(1u))));
Dean Michael Berris0ce82022018-08-31 19:32:46 +000072 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
Dean Michael Berris889f7032018-08-31 18:56:42 +000073 Field(&XRayRecord::CPU, Eq(1u))));
Dean Michael Berris0ce82022018-08-31 19:32:46 +000074 EXPECT_THAT(Trace,
Dean Michael Berris889f7032018-08-31 18:56:42 +000075 ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
76 Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
Dean Michael Berris20baaad2018-08-30 07:22:21 +000077}
78
79// This version is almost exactly the same as above, except writing version 2
80// logs, without the PID records.
81TEST(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 Berris2b5cea52018-09-11 06:36:51 +000095 .add<NewCPUIDRecord>(1, 2)
Dean Michael Berris20baaad2018-08-30 07:22:21 +000096 .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 Berrisbff6dcb2018-08-31 16:08:38 +0000104 DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
Dean Michael Berris20baaad2018-08-30 07:22:21 +0000105 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 Berris0ce82022018-08-31 19:32:46 +0000111 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
Dean Michael Berris889f7032018-08-31 18:56:42 +0000112 Field(&XRayRecord::FuncId, Eq(1))));
Dean Michael Berris0ce82022018-08-31 19:32:46 +0000113 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
Dean Michael Berris889f7032018-08-31 18:56:42 +0000114 Field(&XRayRecord::TId, Eq(1u))));
Dean Michael Berris0ce82022018-08-31 19:32:46 +0000115 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
Dean Michael Berris889f7032018-08-31 18:56:42 +0000116 Field(&XRayRecord::CPU, Eq(1u))));
Dean Michael Berris0ce82022018-08-31 19:32:46 +0000117 EXPECT_THAT(Trace,
Dean Michael Berris889f7032018-08-31 18:56:42 +0000118 ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
119 Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
Dean Michael Berris20baaad2018-08-30 07:22:21 +0000120}
121
122// This covers version 1 of the log, without a BufferExtents record but has an
123// explicit EndOfBuffer record.
124TEST(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 Berris50365ce2018-08-31 10:03:52 +0000138 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 Berris20baaad2018-08-30 07:22:21 +0000143 auto L = LogBuilder()
144 .add<NewBufferRecord>(1)
145 .add<WallclockRecord>(1, 1)
Dean Michael Berris2b5cea52018-09-11 06:36:51 +0000146 .add<NewCPUIDRecord>(1, 2)
Dean Michael Berris20baaad2018-08-30 07:22:21 +0000147 .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 Berris50365ce2018-08-31 10:03:52 +0000158 // 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 Berris20baaad2018-08-30 07:22:21 +0000162 // Then from here we load the Trace file.
Dean Michael Berrisbff6dcb2018-08-31 16:08:38 +0000163 DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
Dean Michael Berris20baaad2018-08-30 07:22:21 +0000164 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 Berris0ce82022018-08-31 19:32:46 +0000170 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
Dean Michael Berris889f7032018-08-31 18:56:42 +0000171 Field(&XRayRecord::FuncId, Eq(1))));
Dean Michael Berris0ce82022018-08-31 19:32:46 +0000172 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
Dean Michael Berris889f7032018-08-31 18:56:42 +0000173 Field(&XRayRecord::TId, Eq(1u))));
Dean Michael Berris0ce82022018-08-31 19:32:46 +0000174 EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
Dean Michael Berris889f7032018-08-31 18:56:42 +0000175 Field(&XRayRecord::CPU, Eq(1u))));
Dean Michael Berris0ce82022018-08-31 19:32:46 +0000176 EXPECT_THAT(Trace,
Dean Michael Berris889f7032018-08-31 18:56:42 +0000177 ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
178 Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
Dean Michael Berris20baaad2018-08-30 07:22:21 +0000179}
180
181} // namespace
182} // namespace xray
183} // namespace llvm