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. |
26 | TEST(FDRBlockIndexerTest, IndexBlocksV3) { |
27 | auto Block0 = LogBuilder() |
28 | .add<BufferExtents>(A: 80) |
29 | .add<NewBufferRecord>(A: 1) |
30 | .add<WallclockRecord>(A: 1, A: 2) |
31 | .add<PIDRecord>(A: 1) |
32 | .add<NewCPUIDRecord>(A: 1, A: 2) |
33 | .add<FunctionRecord>(A: RecordTypes::ENTER, A: 1, A: 1) |
34 | .add<FunctionRecord>(A: RecordTypes::EXIT, A: 1, A: 100) |
35 | .consume(); |
36 | auto Block1 = LogBuilder() |
37 | .add<BufferExtents>(A: 80) |
38 | .add<NewBufferRecord>(A: 1) |
39 | .add<WallclockRecord>(A: 1, A: 2) |
40 | .add<PIDRecord>(A: 1) |
41 | .add<NewCPUIDRecord>(A: 1, A: 2) |
42 | .add<FunctionRecord>(A: RecordTypes::ENTER, A: 1, A: 1) |
43 | .add<FunctionRecord>(A: RecordTypes::EXIT, A: 1, A: 100) |
44 | .consume(); |
45 | auto Block2 = LogBuilder() |
46 | .add<BufferExtents>(A: 80) |
47 | .add<NewBufferRecord>(A: 2) |
48 | .add<WallclockRecord>(A: 1, A: 2) |
49 | .add<PIDRecord>(A: 1) |
50 | .add<NewCPUIDRecord>(A: 2, A: 2) |
51 | .add<FunctionRecord>(A: RecordTypes::ENTER, A: 1, A: 1) |
52 | .add<FunctionRecord>(A: RecordTypes::EXIT, A: 1, A: 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(t&: Block0), std::ref(t&: Block1), std::ref(t&: 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(Val: {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(Val: {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 | |