#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "llvm/XRay/BlockIndexer.h"
#include "llvm/XRay/BlockPrinter.h"
#include "llvm/XRay/BlockVerifier.h"
#include "llvm/XRay/FDRLogBuilder.h"
#include "llvm/XRay/FDRRecords.h"
#include "llvm/XRay/RecordPrinter.h"
namespace llvm {
namespace xray {
namespace {
using ::testing::Eq;
using ::testing::Not;
TEST(XRayFDRTest, BuilderAndBlockIndexer) {
auto Block0 = LogBuilder()
.add<BufferExtents>(100)
.add<NewBufferRecord>(1)
.add<WallclockRecord>(1, 1)
.add<PIDRecord>(1)
.add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
.add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
.add<CustomEventRecordV5>(1, 4, "XRAY")
.add<TypedEventRecord>(1, 4, 2, "XRAY")
.consume();
auto Block1 = LogBuilder()
.add<BufferExtents>(100)
.add<NewBufferRecord>(1)
.add<WallclockRecord>(1, 2)
.add<PIDRecord>(1)
.add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
.add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
.add<CustomEventRecordV5>(1, 4, "XRAY")
.add<TypedEventRecord>(1, 4, 2, "XRAY")
.consume();
auto Block2 = LogBuilder()
.add<BufferExtents>(100)
.add<NewBufferRecord>(2)
.add<WallclockRecord>(1, 3)
.add<PIDRecord>(1)
.add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
.add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
.add<CustomEventRecordV5>(1, 4, "XRAY")
.add<TypedEventRecord>(1, 4, 2, "XRAY")
.consume();
BlockIndexer::Index Index;
BlockIndexer Indexer(Index);
for (auto B : {std::ref(Block0), std::ref(Block1), std::ref(Block2)}) {
for (auto &R : B.get())
ASSERT_FALSE(errorToBool(R->apply(Indexer)));
ASSERT_FALSE(errorToBool(Indexer.flush()));
}
ASSERT_THAT(Index.size(), Eq(2u));
auto T1Blocks = Index.find({1, 1});
ASSERT_THAT(T1Blocks, Not(Eq(Index.end())));
ASSERT_THAT(T1Blocks->second.size(), Eq(2u));
auto T2Blocks = Index.find({1, 2});
ASSERT_THAT(T2Blocks, Not(Eq(Index.end())));
ASSERT_THAT(T2Blocks->second.size(), Eq(1u));
}
TEST(XRayFDRTest, BuilderAndBlockVerifier) {
auto Block = LogBuilder()
.add<BufferExtents>(48)
.add<NewBufferRecord>(1)
.add<WallclockRecord>(1, 1)
.add<PIDRecord>(1)
.add<NewCPUIDRecord>(1, 2)
.consume();
BlockVerifier Verifier;
for (auto &R : Block)
ASSERT_FALSE(errorToBool(R->apply(Verifier)));
ASSERT_FALSE(errorToBool(Verifier.verify()));
}
TEST(XRayFDRTest, IndexAndVerifyBlocks) {
auto Block0 = LogBuilder()
.add<BufferExtents>(64)
.add<NewBufferRecord>(1)
.add<WallclockRecord>(1, 1)
.add<PIDRecord>(1)
.add<NewCPUIDRecord>(1, 2)
.add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
.add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
.add<CustomEventRecordV5>(1, 4, "XRAY")
.add<TypedEventRecord>(1, 4, 2, "XRAY")
.consume();
auto Block1 = LogBuilder()
.add<BufferExtents>(64)
.add<NewBufferRecord>(1)
.add<WallclockRecord>(1, 1)
.add<PIDRecord>(1)
.add<NewCPUIDRecord>(1, 2)
.add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
.add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
.add<CustomEventRecordV5>(1, 4, "XRAY")
.add<TypedEventRecord>(1, 4, 2, "XRAY")
.consume();
auto Block2 = LogBuilder()
.add<BufferExtents>(64)
.add<NewBufferRecord>(1)
.add<WallclockRecord>(1, 1)
.add<PIDRecord>(1)
.add<NewCPUIDRecord>(1, 2)
.add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
.add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
.add<CustomEventRecordV5>(1, 4, "XRAY")
.add<TypedEventRecord>(1, 4, 2, "XRAY")
.consume();
BlockIndexer::Index Index;
BlockIndexer Indexer(Index);
for (auto B : {std::ref(Block0), std::ref(Block1), std::ref(Block2)}) {
for (auto &R : B.get())
ASSERT_FALSE(errorToBool(R->apply(Indexer)));
ASSERT_FALSE(errorToBool(Indexer.flush()));
}
BlockVerifier Verifier;
for (auto &ProcessThreadBlocks : Index) {
auto &Blocks = ProcessThreadBlocks.second;
for (auto &B : Blocks) {
for (auto *R : B.Records)
ASSERT_FALSE(errorToBool(R->apply(Verifier)));
ASSERT_FALSE(errorToBool(Verifier.verify()));
Verifier.reset();
}
}
std::string Output;
raw_string_ostream OS(Output);
RecordPrinter RP(OS);
BlockPrinter BP(OS, RP);
for (auto &ProcessThreadBlocks : Index) {
auto &Blocks = ProcessThreadBlocks.second;
for (auto &B : Blocks) {
for (auto *R : B.Records)
ASSERT_FALSE(errorToBool(R->apply(BP)));
BP.reset();
}
}
OS.flush();
EXPECT_THAT(Output, Not(Eq("")));
}
} } }