blob: 558840b111a6b4ea589bc8651584075173600354 [file] [log] [blame]
Dean Michael Berrisafed9812018-09-06 05:55:57 +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#include "llvm/XRay/BlockIndexer.h"
10#include "llvm/XRay/FDRLogBuilder.h"
11#include "llvm/XRay/FDRRecords.h"
12#include "gmock/gmock.h"
13#include "gtest/gtest.h"
14
15namespace llvm {
16namespace xray {
17namespace {
18
19using ::testing::ElementsAre;
20using ::testing::Eq;
21using ::testing::Field;
22using ::testing::Not;
23using ::testing::SizeIs;
24
25// This test ensures that we can index blocks that follow version 3 of the log
26// format.
27TEST(FDRBlockIndexerTest, IndexBlocksV3) {
28 auto Block0 = LogBuilder()
29 .add<BufferExtents>(80)
30 .add<NewBufferRecord>(1)
31 .add<WallclockRecord>(1, 2)
32 .add<PIDRecord>(1)
Dean Michael Berris2b5cea52018-09-11 06:36:51 +000033 .add<NewCPUIDRecord>(1, 2)
Dean Michael Berrisafed9812018-09-06 05:55:57 +000034 .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
35 .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
36 .consume();
37 auto Block1 = LogBuilder()
38 .add<BufferExtents>(80)
39 .add<NewBufferRecord>(1)
40 .add<WallclockRecord>(1, 2)
41 .add<PIDRecord>(1)
Dean Michael Berris2b5cea52018-09-11 06:36:51 +000042 .add<NewCPUIDRecord>(1, 2)
Dean Michael Berrisafed9812018-09-06 05:55:57 +000043 .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
44 .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
45 .consume();
46 auto Block2 = LogBuilder()
47 .add<BufferExtents>(80)
48 .add<NewBufferRecord>(2)
49 .add<WallclockRecord>(1, 2)
50 .add<PIDRecord>(1)
Dean Michael Berris2b5cea52018-09-11 06:36:51 +000051 .add<NewCPUIDRecord>(2, 2)
Dean Michael Berrisafed9812018-09-06 05:55:57 +000052 .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
53 .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
54 .consume();
55 BlockIndexer::Index Index;
56 BlockIndexer Indexer(Index);
57 // Iterate through the contrived blocks we have created above.
58 for (auto B : {std::ref(Block0), std::ref(Block1), std::ref(Block2)}) {
59 // For each record in the block, we apply the indexer.
60 for (auto &R : B.get())
61 ASSERT_FALSE(errorToBool(R->apply(Indexer)));
62 ASSERT_FALSE(errorToBool(Indexer.flush()));
63 }
64
65 ASSERT_THAT(Index.size(), Eq(2u));
66 auto T1Blocks = Index.find({1, 1});
67 ASSERT_THAT(T1Blocks, Not(Eq(Index.end())));
68
69 // Expect only six records, because we're ignoring the BufferExtents record.
70 EXPECT_THAT(T1Blocks->second,
71 ElementsAre(Field(&BlockIndexer::Block::Records, SizeIs(6u)),
72 Field(&BlockIndexer::Block::Records, SizeIs(6u))));
73 auto T2Blocks = Index.find({1, 2});
74 ASSERT_THAT(T2Blocks, Not(Eq(Index.end())));
75 EXPECT_THAT(T2Blocks->second, ElementsAre(Field(&BlockIndexer::Block::Records,
76 SizeIs(Eq(6u)))));
77}
78
79// FIXME: Support indexing V2 and V1 blocks.
80
81} // namespace
82} // namespace xray
83} // namespace llvm