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
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
//===- llvm/unittest/XRay/FDRTraceWriterTest.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
//
//===----------------------------------------------------------------------===//
//
// Test a utility that can write out XRay FDR Mode formatted trace files.
//
//===----------------------------------------------------------------------===//
#include "llvm/XRay/FDRTraceWriter.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/XRay/FDRLogBuilder.h"
#include "llvm/XRay/FDRRecords.h"
#include "llvm/XRay/Trace.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <string>

namespace llvm {
namespace xray {
namespace {

using testing::ElementsAre;
using testing::Eq;
using testing::Field;
using testing::IsEmpty;
using testing::Not;

// We want to be able to create an instance of an FDRTraceWriter and associate
// it with a stream, which could be loaded and turned into a Trace instance.
// This test writes out version 3 trace logs.
TEST(FDRTraceWriterTest, WriteToStringBufferVersion3) {
  std::string Data;
  raw_string_ostream OS(Data);
  XRayFileHeader H;
  H.Version = 3;
  H.Type = 1;
  H.ConstantTSC = true;
  H.NonstopTSC = true;
  H.CycleFrequency = 3e9;
  FDRTraceWriter Writer(OS, H);
  auto L = LogBuilder()
               .add<BufferExtents>(80)
               .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)
               .consume();
  for (auto &P : L)
    ASSERT_FALSE(errorToBool(P->apply(Writer)));
  OS.flush();

  // Then from here we load the Trace file.
  DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
  auto TraceOrErr = loadTrace(DE, true);
  if (!TraceOrErr)
    FAIL() << TraceOrErr.takeError();
  auto &Trace = TraceOrErr.get();

  ASSERT_THAT(Trace, Not(IsEmpty()));
  EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
                                 Field(&XRayRecord::FuncId, Eq(1))));
  EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
                                 Field(&XRayRecord::TId, Eq(1u))));
  EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::PId, Eq(1u)),
                                 Field(&XRayRecord::PId, Eq(1u))));
  EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
                                 Field(&XRayRecord::CPU, Eq(1u))));
  EXPECT_THAT(Trace,
              ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
                          Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
}

// This version is almost exactly the same as above, except writing version 2
// logs, without the PID records.
TEST(FDRTraceWriterTest, WriteToStringBufferVersion2) {
  std::string Data;
  raw_string_ostream OS(Data);
  XRayFileHeader H;
  H.Version = 2;
  H.Type = 1;
  H.ConstantTSC = true;
  H.NonstopTSC = true;
  H.CycleFrequency = 3e9;
  FDRTraceWriter Writer(OS, H);
  auto L = LogBuilder()
               .add<BufferExtents>(64)
               .add<NewBufferRecord>(1)
               .add<WallclockRecord>(1, 1)
               .add<NewCPUIDRecord>(1, 2)
               .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
               .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
               .consume();
  for (auto &P : L)
    ASSERT_FALSE(errorToBool(P->apply(Writer)));
  OS.flush();

  // Then from here we load the Trace file.
  DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
  auto TraceOrErr = loadTrace(DE, true);
  if (!TraceOrErr)
    FAIL() << TraceOrErr.takeError();
  auto &Trace = TraceOrErr.get();

  ASSERT_THAT(Trace, Not(IsEmpty()));
  EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
                                 Field(&XRayRecord::FuncId, Eq(1))));
  EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
                                 Field(&XRayRecord::TId, Eq(1u))));
  EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
                                 Field(&XRayRecord::CPU, Eq(1u))));
  EXPECT_THAT(Trace,
              ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
                          Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
}

// This covers version 1 of the log, without a BufferExtents record but has an
// explicit EndOfBuffer record.
TEST(FDRTraceWriterTest, WriteToStringBufferVersion1) {
  std::string Data;
  raw_string_ostream OS(Data);
  XRayFileHeader H;
  H.Version = 1;
  H.Type = 1;
  H.ConstantTSC = true;
  H.NonstopTSC = true;
  H.CycleFrequency = 3e9;
  // Write the size of buffers out, arbitrarily it's 4k.
  constexpr uint64_t BufferSize = 4096;
  std::memcpy(H.FreeFormData, reinterpret_cast<const char *>(&BufferSize),
              sizeof(BufferSize));
  FDRTraceWriter Writer(OS, H);
  OS.flush();

  // Ensure that at this point the Data buffer has the file header serialized
  // size.
  ASSERT_THAT(Data.size(), Eq(32u));
  auto L = LogBuilder()
               .add<NewBufferRecord>(1)
               .add<WallclockRecord>(1, 1)
               .add<NewCPUIDRecord>(1, 2)
               .add<FunctionRecord>(RecordTypes::ENTER, 1, 1)
               .add<FunctionRecord>(RecordTypes::EXIT, 1, 100)
               .add<EndBufferRecord>()
               .consume();
  for (auto &P : L)
    ASSERT_FALSE(errorToBool(P->apply(Writer)));

  // We need to pad the buffer with 4016 (4096 - 80) bytes of zeros.
  OS.write_zeros(4016);
  OS.flush();

  // For version 1 of the log, we need the whole buffer to be the size of the
  // file header plus 32.
  ASSERT_THAT(Data.size(), Eq(BufferSize + 32));

  // Then from here we load the Trace file.
  DataExtractor DE(Data, sys::IsLittleEndianHost, 8);
  auto TraceOrErr = loadTrace(DE, true);
  if (!TraceOrErr)
    FAIL() << TraceOrErr.takeError();
  auto &Trace = TraceOrErr.get();

  ASSERT_THAT(Trace, Not(IsEmpty()));
  EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::FuncId, Eq(1)),
                                 Field(&XRayRecord::FuncId, Eq(1))));
  EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::TId, Eq(1u)),
                                 Field(&XRayRecord::TId, Eq(1u))));
  EXPECT_THAT(Trace, ElementsAre(Field(&XRayRecord::CPU, Eq(1u)),
                                 Field(&XRayRecord::CPU, Eq(1u))));
  EXPECT_THAT(Trace,
              ElementsAre(Field(&XRayRecord::Type, Eq(RecordTypes::ENTER)),
                          Field(&XRayRecord::Type, Eq(RecordTypes::EXIT))));
}

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