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