reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
//===- llvm/unittest/XRay/FDRBlockVerifierTest.cpp --------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Testing/Support/Error.h"
#include "llvm/XRay/BlockIndexer.h"
#include "llvm/XRay/BlockVerifier.h"
#include "llvm/XRay/FDRLogBuilder.h"
#include "llvm/XRay/FDRRecords.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace llvm {
namespace xray {
namespace {

using ::testing::ElementsAre;
using ::testing::Not;
using ::testing::SizeIs;

TEST(FDRBlockVerifierTest, ValidBlocksV3) {
  auto Block0 = LogBuilder()
                    .add<BufferExtents>(80)
                    .add<NewBufferRecord>(1)
                    .add<WallclockRecord>(1, 2)
                    .add<PIDRecord>(1)
                    .add<NewCPUIDRecord>(1, 2)
                    .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                    .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                    .consume();
  auto Block1 = LogBuilder()
                    .add<BufferExtents>(80)
                    .add<NewBufferRecord>(1)
                    .add<WallclockRecord>(1, 2)
                    .add<PIDRecord>(1)
                    .add<NewCPUIDRecord>(1, 2)
                    .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                    .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                    .consume();
  auto Block2 = LogBuilder()
                    .add<BufferExtents>(80)
                    .add<NewBufferRecord>(2)
                    .add<WallclockRecord>(1, 2)
                    .add<PIDRecord>(1)
                    .add<NewCPUIDRecord>(2, 2)
                    .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                    .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                    .consume();
  BlockIndexer::Index Index;
  BlockIndexer Indexer(Index);
  for (auto B : {std::ref(Block0), std::ref(Block1), std::ref(Block2)}) {
    for (auto &R : B.get())
      ASSERT_FALSE(errorToBool(R->apply(Indexer)));
    ASSERT_FALSE(errorToBool(Indexer.flush()));
  }

  BlockVerifier Verifier;
  for (auto &ProcessThreadBlocks : Index) {
    auto &Blocks = ProcessThreadBlocks.second;
    for (auto &B : Blocks) {
      for (auto *R : B.Records)
        ASSERT_FALSE(errorToBool(R->apply(Verifier)));
      ASSERT_FALSE(errorToBool(Verifier.verify()));
      Verifier.reset();
    }
  }
}

TEST(FDRBlockVerifierTest, MissingPIDRecord) {
  auto Block = LogBuilder()
                   .add<BufferExtents>(20)
                   .add<NewBufferRecord>(1)
                   .add<WallclockRecord>(1, 2)
                   .add<NewCPUIDRecord>(1, 2)
                   .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                   .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                   .consume();
  BlockVerifier Verifier;
  for (auto &R : Block)
    ASSERT_FALSE(errorToBool(R->apply(Verifier)));
  ASSERT_FALSE(errorToBool(Verifier.verify()));
}

TEST(FDRBlockVerifierTest, MissingBufferExtents) {
  auto Block = LogBuilder()
                   .add<NewBufferRecord>(1)
                   .add<WallclockRecord>(1, 2)
                   .add<NewCPUIDRecord>(1, 2)
                   .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                   .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                   .consume();
  BlockVerifier Verifier;
  for (auto &R : Block)
    ASSERT_FALSE(errorToBool(R->apply(Verifier)));
  ASSERT_FALSE(errorToBool(Verifier.verify()));
}

TEST(FDRBlockVerifierTest, IgnoreRecordsAfterEOB) {
  auto Block = LogBuilder()
                   .add<NewBufferRecord>(1)
                   .add<WallclockRecord>(1, 2)
                   .add<NewCPUIDRecord>(1, 2)
                   .add<EndBufferRecord>()
                   .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                   .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                   .consume();
  BlockVerifier Verifier;
  for (auto &R : Block)
    ASSERT_FALSE(errorToBool(R->apply(Verifier)));
  ASSERT_FALSE(errorToBool(Verifier.verify()));
}

TEST(FDRBlockVerifierTest, MalformedV2) {
  auto Block = LogBuilder()
                   .add<NewBufferRecord>(1)
                   .add<WallclockRecord>(1, 2)
                   .add<NewCPUIDRecord>(1, 2)
                   .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                   .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                   .add<NewBufferRecord>(2)
                   .consume();
  BlockVerifier Verifier;

  ASSERT_THAT(Block, SizeIs(6u));
  EXPECT_THAT_ERROR(Block[0]->apply(Verifier), Succeeded());
  EXPECT_THAT_ERROR(Block[1]->apply(Verifier), Succeeded());
  EXPECT_THAT_ERROR(Block[2]->apply(Verifier), Succeeded());
  EXPECT_THAT_ERROR(Block[3]->apply(Verifier), Succeeded());
  EXPECT_THAT_ERROR(Block[4]->apply(Verifier), Succeeded());
  EXPECT_THAT_ERROR(Block[5]->apply(Verifier), Failed());
}

} // namespace
} // namespace xray
} // namespace llvm