Dean Michael Berris | afed981 | 2018-09-06 05:55:57 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 15 | namespace llvm { |
| 16 | namespace xray { |
| 17 | namespace { |
| 18 | |
| 19 | using ::testing::ElementsAre; |
| 20 | using ::testing::Eq; |
| 21 | using ::testing::Field; |
| 22 | using ::testing::Not; |
| 23 | using ::testing::SizeIs; |
| 24 | |
| 25 | // This test ensures that we can index blocks that follow version 3 of the log |
| 26 | // format. |
| 27 | TEST(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 Berris | 2b5cea5 | 2018-09-11 06:36:51 +0000 | [diff] [blame] | 33 | .add<NewCPUIDRecord>(1, 2) |
Dean Michael Berris | afed981 | 2018-09-06 05:55:57 +0000 | [diff] [blame] | 34 | .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 Berris | 2b5cea5 | 2018-09-11 06:36:51 +0000 | [diff] [blame] | 42 | .add<NewCPUIDRecord>(1, 2) |
Dean Michael Berris | afed981 | 2018-09-06 05:55:57 +0000 | [diff] [blame] | 43 | .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 Berris | 2b5cea5 | 2018-09-11 06:36:51 +0000 | [diff] [blame] | 51 | .add<NewCPUIDRecord>(2, 2) |
Dean Michael Berris | afed981 | 2018-09-06 05:55:57 +0000 | [diff] [blame] | 52 | .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 |