1 //===- llvm/unittest/XRay/FDRTraceWriterTest.cpp ----------------*- C++ -*-===//
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
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"
18 using ::testing::ElementsAre
;
20 using ::testing::Field
;
22 using ::testing::SizeIs
;
24 // This test ensures that we can index blocks that follow version 3 of the log
26 TEST(FDRBlockIndexerTest
, IndexBlocksV3
) {
27 auto Block0
= LogBuilder()
28 .add
<BufferExtents
>(80)
29 .add
<NewBufferRecord
>(1)
30 .add
<WallclockRecord
>(1, 2)
32 .add
<NewCPUIDRecord
>(1, 2)
33 .add
<FunctionRecord
>(RecordTypes::ENTER
, 1, 1)
34 .add
<FunctionRecord
>(RecordTypes::EXIT
, 1, 100)
36 auto Block1
= LogBuilder()
37 .add
<BufferExtents
>(80)
38 .add
<NewBufferRecord
>(1)
39 .add
<WallclockRecord
>(1, 2)
41 .add
<NewCPUIDRecord
>(1, 2)
42 .add
<FunctionRecord
>(RecordTypes::ENTER
, 1, 1)
43 .add
<FunctionRecord
>(RecordTypes::EXIT
, 1, 100)
45 auto Block2
= LogBuilder()
46 .add
<BufferExtents
>(80)
47 .add
<NewBufferRecord
>(2)
48 .add
<WallclockRecord
>(1, 2)
50 .add
<NewCPUIDRecord
>(2, 2)
51 .add
<FunctionRecord
>(RecordTypes::ENTER
, 1, 1)
52 .add
<FunctionRecord
>(RecordTypes::EXIT
, 1, 100)
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()));
64 ASSERT_THAT(Index
.size(), Eq(2u));
65 auto T1Blocks
= Index
.find({1, 1});
66 ASSERT_THAT(T1Blocks
, Not(Eq(Index
.end())));
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
,
78 // FIXME: Support indexing V2 and V1 blocks.