1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
//===- llvm/unittest/XRay/FDRProducerConsumerTest.cpp -----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Test for round-trip record writing and reading.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/XRay/FDRLogBuilder.h"
#include "llvm/XRay/FDRRecordConsumer.h"
#include "llvm/XRay/FDRRecordProducer.h"
#include "llvm/XRay/FDRRecords.h"
#include "llvm/XRay/FDRTraceWriter.h"
#include "llvm/XRay/FileHeaderReader.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <string>
#include <tuple>
namespace llvm {
namespace xray {
namespace {
using ::testing::Eq;
using ::testing::IsEmpty;
using ::testing::Not;
template <class RecordType> std::unique_ptr<Record> MakeRecord();
template <> std::unique_ptr<Record> MakeRecord<BufferExtents>() {
return make_unique<BufferExtents>(1);
}
template <> std::unique_ptr<Record> MakeRecord<NewBufferRecord>() {
return make_unique<NewBufferRecord>(1);
}
template <> std::unique_ptr<Record> MakeRecord<NewCPUIDRecord>() {
return make_unique<NewCPUIDRecord>(1, 2);
}
template <> std::unique_ptr<Record> MakeRecord<TSCWrapRecord>() {
return make_unique<TSCWrapRecord>(1);
}
template <> std::unique_ptr<Record> MakeRecord<WallclockRecord>() {
return make_unique<WallclockRecord>(1, 2);
}
template <> std::unique_ptr<Record> MakeRecord<CustomEventRecord>() {
return make_unique<CustomEventRecord>(4, 1, "data");
}
template <> std::unique_ptr<Record> MakeRecord<CallArgRecord>() {
return make_unique<CallArgRecord>(1);
}
template <> std::unique_ptr<Record> MakeRecord<PIDRecord>() {
return make_unique<PIDRecord>(1);
}
template <> std::unique_ptr<Record> MakeRecord<FunctionRecord>() {
return make_unique<FunctionRecord>(RecordTypes::ENTER, 1, 2);
}
template <class T> class RoundTripTest : public ::testing::Test {
public:
RoundTripTest() : Data(), OS(Data) {
H.Version = 3;
H.Type = 1;
H.ConstantTSC = true;
H.NonstopTSC = true;
H.CycleFrequency = 3e9;
Writer = make_unique<FDRTraceWriter>(OS, H);
Rec = MakeRecord<T>();
}
protected:
std::string Data;
raw_string_ostream OS;
XRayFileHeader H;
std::unique_ptr<FDRTraceWriter> Writer;
std::unique_ptr<Record> Rec;
};
TYPED_TEST_CASE_P(RoundTripTest);
// This test ensures that the writing and reading implementations are in sync --
// that given write(read(write(R))) == R.
TYPED_TEST_P(RoundTripTest, RoundTripsSingleValue) {
auto &R = this->Rec;
ASSERT_FALSE(errorToBool(R->apply(*this->Writer)));
this->OS.flush();
DataExtractor DE(this->Data, sys::IsLittleEndianHost, 8);
uint32_t OffsetPtr = 0;
auto HeaderOrErr = readBinaryFormatHeader(DE, OffsetPtr);
if (!HeaderOrErr)
FAIL() << HeaderOrErr.takeError();
FileBasedRecordProducer P(HeaderOrErr.get(), DE, OffsetPtr);
std::vector<std::unique_ptr<Record>> Records;
LogBuilderConsumer C(Records);
while (DE.isValidOffsetForDataOfSize(OffsetPtr, 1)) {
auto R = P.produce();
if (!R)
FAIL() << R.takeError();
if (auto E = C.consume(std::move(R.get())))
FAIL() << E;
}
ASSERT_THAT(Records, Not(IsEmpty()));
std::string Data2;
raw_string_ostream OS2(Data2);
FDRTraceWriter Writer2(OS2, this->H);
for (auto &P : Records)
ASSERT_FALSE(errorToBool(P->apply(Writer2)));
OS2.flush();
EXPECT_EQ(Data2.substr(sizeof(XRayFileHeader)),
this->Data.substr(sizeof(XRayFileHeader)));
EXPECT_THAT(Records[0]->type(), Eq(R->type()));
}
REGISTER_TYPED_TEST_CASE_P(RoundTripTest, RoundTripsSingleValue);
using RecordTypes =
::testing::Types<BufferExtents, NewBufferRecord, NewCPUIDRecord,
TSCWrapRecord, WallclockRecord, CustomEventRecord,
CallArgRecord, BufferExtents, PIDRecord, FunctionRecord>;
INSTANTIATE_TYPED_TEST_CASE_P(Records, RoundTripTest, RecordTypes);
} // namespace
} // namespace xray
} // namespace llvm
|