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
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
//===- FDRRecords.cpp - Unit Tests for XRay FDR Record Loading ------------===//
//
// 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 "gmock/gmock.h"
#include "gtest/gtest.h"

#include "llvm/XRay/BlockIndexer.h"
#include "llvm/XRay/BlockPrinter.h"
#include "llvm/XRay/BlockVerifier.h"
#include "llvm/XRay/FDRLogBuilder.h"
#include "llvm/XRay/FDRRecords.h"
#include "llvm/XRay/RecordPrinter.h"

namespace llvm {
namespace xray {
namespace {

using ::testing::Eq;
using ::testing::Not;

TEST(XRayFDRTest, BuilderAndBlockIndexer) {
  // We recreate a single block of valid records, then ensure that we find all
  // of them belonging in the same index. We do this for three blocks, and
  // ensure we find the same records in the blocks we deduce.
  auto Block0 = LogBuilder()
                    .add<BufferExtents>(100)
                    .add<NewBufferRecord>(1)
                    .add<WallclockRecord>(1, 1)
                    .add<PIDRecord>(1)
                    .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                    .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                    .add<CustomEventRecordV5>(1, 4, "XRAY")
                    .add<TypedEventRecord>(1, 4, 2, "XRAY")
                    .consume();
  auto Block1 = LogBuilder()
                    .add<BufferExtents>(100)
                    .add<NewBufferRecord>(1)
                    .add<WallclockRecord>(1, 2)
                    .add<PIDRecord>(1)
                    .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                    .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                    .add<CustomEventRecordV5>(1, 4, "XRAY")
                    .add<TypedEventRecord>(1, 4, 2, "XRAY")
                    .consume();
  auto Block2 = LogBuilder()
                    .add<BufferExtents>(100)
                    .add<NewBufferRecord>(2)
                    .add<WallclockRecord>(1, 3)
                    .add<PIDRecord>(1)
                    .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                    .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                    .add<CustomEventRecordV5>(1, 4, "XRAY")
                    .add<TypedEventRecord>(1, 4, 2, "XRAY")
                    .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()));
  }

  // We have two threads worth of blocks.
  ASSERT_THAT(Index.size(), Eq(2u));
  auto T1Blocks = Index.find({1, 1});
  ASSERT_THAT(T1Blocks, Not(Eq(Index.end())));
  ASSERT_THAT(T1Blocks->second.size(), Eq(2u));
  auto T2Blocks = Index.find({1, 2});
  ASSERT_THAT(T2Blocks, Not(Eq(Index.end())));
  ASSERT_THAT(T2Blocks->second.size(), Eq(1u));
}

TEST(XRayFDRTest, BuilderAndBlockVerifier) {
  auto Block = LogBuilder()
                   .add<BufferExtents>(48)
                   .add<NewBufferRecord>(1)
                   .add<WallclockRecord>(1, 1)
                   .add<PIDRecord>(1)
                   .add<NewCPUIDRecord>(1, 2)
                   .consume();
  BlockVerifier Verifier;
  for (auto &R : Block)
    ASSERT_FALSE(errorToBool(R->apply(Verifier)));
  ASSERT_FALSE(errorToBool(Verifier.verify()));
}

TEST(XRayFDRTest, IndexAndVerifyBlocks) {
  auto Block0 = LogBuilder()
                    .add<BufferExtents>(64)
                    .add<NewBufferRecord>(1)
                    .add<WallclockRecord>(1, 1)
                    .add<PIDRecord>(1)
                    .add<NewCPUIDRecord>(1, 2)
                    .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                    .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                    .add<CustomEventRecordV5>(1, 4, "XRAY")
                    .add<TypedEventRecord>(1, 4, 2, "XRAY")
                    .consume();
  auto Block1 = LogBuilder()
                    .add<BufferExtents>(64)
                    .add<NewBufferRecord>(1)
                    .add<WallclockRecord>(1, 1)
                    .add<PIDRecord>(1)
                    .add<NewCPUIDRecord>(1, 2)
                    .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                    .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                    .add<CustomEventRecordV5>(1, 4, "XRAY")
                    .add<TypedEventRecord>(1, 4, 2, "XRAY")
                    .consume();
  auto Block2 = LogBuilder()
                    .add<BufferExtents>(64)
                    .add<NewBufferRecord>(1)
                    .add<WallclockRecord>(1, 1)
                    .add<PIDRecord>(1)
                    .add<NewCPUIDRecord>(1, 2)
                    .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
                    .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
                    .add<CustomEventRecordV5>(1, 4, "XRAY")
                    .add<TypedEventRecord>(1, 4, 2, "XRAY")
                    .consume();

  // First, index the records in different blocks.
  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()));
  }

  // Next, verify that each block is consistently defined.
  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();
    }
  }

  // Then set up the printing mechanisms.
  std::string Output;
  raw_string_ostream OS(Output);
  RecordPrinter RP(OS);
  BlockPrinter BP(OS, RP);
  for (auto &ProcessThreadBlocks : Index) {
    auto &Blocks = ProcessThreadBlocks.second;
    for (auto &B : Blocks) {
      for (auto *R : B.Records)
        ASSERT_FALSE(errorToBool(R->apply(BP)));
      BP.reset();
    }
  }

  OS.flush();
  EXPECT_THAT(Output, Not(Eq("")));
}

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