summaryrefslogtreecommitdiff
path: root/lib/xray/tests/unit/fdr_log_writer_test.cc
blob: 1ff880a96bebf70d502bb3c5ce63a7e4345debe3 (plain)
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
//===-- fdr_log_writer_test.cc --------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file is a part of XRay, a function call tracing system.
//
//===----------------------------------------------------------------------===//
#include <time.h>

#include "test_helpers.h"
#include "xray/xray_records.h"
#include "xray_fdr_log_writer.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Testing/Support/Error.h"
#include "llvm/XRay/Trace.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace __xray {
namespace {

static constexpr size_t kSize = 4096;

using ::llvm::HasValue;
using ::llvm::xray::testing::FuncId;
using ::llvm::xray::testing::RecordType;
using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::IsEmpty;
using ::testing::IsNull;

// Exercise the common code path where we initialize a buffer and are able to
// write some records successfully.
TEST(FdrLogWriterTest, WriteSomeRecords) {
  bool Success = false;
  BufferQueue Buffers(kSize, 1, Success);
  BufferQueue::Buffer B;
  ASSERT_EQ(Buffers.getBuffer(B), BufferQueue::ErrorCode::Ok);

  FDRLogWriter Writer(B);
  MetadataRecord Preamble[] = {
      createMetadataRecord<MetadataRecord::RecordKinds::NewBuffer>(int32_t{1}),
      createMetadataRecord<MetadataRecord::RecordKinds::WalltimeMarker>(
          int64_t{1}, int32_t{2}),
      createMetadataRecord<MetadataRecord::RecordKinds::Pid>(int32_t{1}),
  };
  ASSERT_THAT(Writer.writeMetadataRecords(Preamble),
              Eq(sizeof(MetadataRecord) * 3));
  ASSERT_TRUE(Writer.writeMetadata<MetadataRecord::RecordKinds::NewCPUId>(1));
  ASSERT_TRUE(
      Writer.writeFunction(FDRLogWriter::FunctionRecordKind::Enter, 1, 1));
  ASSERT_TRUE(
      Writer.writeFunction(FDRLogWriter::FunctionRecordKind::Exit, 1, 1));
  ASSERT_EQ(Buffers.releaseBuffer(B), BufferQueue::ErrorCode::Ok);
  ASSERT_EQ(B.Data, nullptr);
  ASSERT_EQ(Buffers.finalize(), BufferQueue::ErrorCode::Ok);

  // We then need to go through each element of the Buffers, and re-create a
  // flat buffer that we would see if they were laid out in a file. This also
  // means we need to write out the header manually.
  std::string Serialized = serialize(Buffers, 3);
  llvm::DataExtractor DE(Serialized, true, 8);
  auto TraceOrErr = llvm::xray::loadTrace(DE);
  EXPECT_THAT_EXPECTED(
      TraceOrErr,
      HasValue(ElementsAre(
          AllOf(FuncId(1), RecordType(llvm::xray::RecordTypes::ENTER)),
          AllOf(FuncId(1), RecordType(llvm::xray::RecordTypes::EXIT)))));
}

// Ensure that we can handle buffer re-use.
TEST(FdrLogWriterTest, ReuseBuffers) {
  bool Success = false;
  BufferQueue Buffers(kSize, 1, Success);
  BufferQueue::Buffer B;
  ASSERT_EQ(Buffers.getBuffer(B), BufferQueue::ErrorCode::Ok);

  FDRLogWriter Writer(B);
  MetadataRecord Preamble[] = {
      createMetadataRecord<MetadataRecord::RecordKinds::NewBuffer>(int32_t{1}),
      createMetadataRecord<MetadataRecord::RecordKinds::WalltimeMarker>(
          int64_t{1}, int32_t{2}),
      createMetadataRecord<MetadataRecord::RecordKinds::Pid>(int32_t{1}),
  };

  // First we write the first set of records into the single buffer in the
  // queue which includes one enter and one exit record.
  ASSERT_THAT(Writer.writeMetadataRecords(Preamble),
              Eq(sizeof(MetadataRecord) * 3));
  ASSERT_TRUE(Writer.writeMetadata<MetadataRecord::RecordKinds::NewCPUId>(
      uint16_t{1}, uint64_t{1}));
  uint64_t TSC = 1;
  ASSERT_TRUE(
      Writer.writeFunction(FDRLogWriter::FunctionRecordKind::Enter, 1, TSC++));
  ASSERT_TRUE(
      Writer.writeFunction(FDRLogWriter::FunctionRecordKind::Exit, 1, TSC++));
  ASSERT_EQ(Buffers.releaseBuffer(B), BufferQueue::ErrorCode::Ok);
  ASSERT_THAT(B.Data, IsNull());

  // Then we re-use the buffer, but only write one record.
  ASSERT_EQ(Buffers.getBuffer(B), BufferQueue::ErrorCode::Ok);
  Writer.resetRecord();
  ASSERT_THAT(Writer.writeMetadataRecords(Preamble),
              Eq(sizeof(MetadataRecord) * 3));
  ASSERT_TRUE(Writer.writeMetadata<MetadataRecord::RecordKinds::NewCPUId>(
      uint16_t{1}, uint64_t{1}));
  ASSERT_TRUE(
      Writer.writeFunction(FDRLogWriter::FunctionRecordKind::Enter, 1, TSC++));
  ASSERT_EQ(Buffers.releaseBuffer(B), BufferQueue::ErrorCode::Ok);
  ASSERT_THAT(B.Data, IsNull());
  ASSERT_EQ(Buffers.finalize(), BufferQueue::ErrorCode::Ok);

  // Then we validate that we only see the single enter record.
  std::string Serialized = serialize(Buffers, 3);
  llvm::DataExtractor DE(Serialized, true, 8);
  auto TraceOrErr = llvm::xray::loadTrace(DE);
  EXPECT_THAT_EXPECTED(
      TraceOrErr, HasValue(ElementsAre(AllOf(
                      FuncId(1), RecordType(llvm::xray::RecordTypes::ENTER)))));
}

TEST(FdrLogWriterTest, UnwriteRecords) {
  bool Success = false;
  BufferQueue Buffers(kSize, 1, Success);
  BufferQueue::Buffer B;
  ASSERT_EQ(Buffers.getBuffer(B), BufferQueue::ErrorCode::Ok);

  FDRLogWriter Writer(B);
  MetadataRecord Preamble[] = {
      createMetadataRecord<MetadataRecord::RecordKinds::NewBuffer>(int32_t{1}),
      createMetadataRecord<MetadataRecord::RecordKinds::WalltimeMarker>(
          int64_t{1}, int32_t{2}),
      createMetadataRecord<MetadataRecord::RecordKinds::Pid>(int32_t{1}),
  };
  ASSERT_THAT(Writer.writeMetadataRecords(Preamble),
              Eq(sizeof(MetadataRecord) * 3));
  ASSERT_TRUE(Writer.writeMetadata<MetadataRecord::RecordKinds::NewCPUId>(1));
  ASSERT_TRUE(
      Writer.writeFunction(FDRLogWriter::FunctionRecordKind::Enter, 1, 1));
  ASSERT_TRUE(
      Writer.writeFunction(FDRLogWriter::FunctionRecordKind::Exit, 1, 1));
  Writer.undoWrites(sizeof(FunctionRecord) * 2);
  ASSERT_EQ(Buffers.releaseBuffer(B), BufferQueue::ErrorCode::Ok);
  ASSERT_EQ(B.Data, nullptr);
  ASSERT_EQ(Buffers.finalize(), BufferQueue::ErrorCode::Ok);

  // We've un-done the two function records we've written, and now we expect
  // that we don't have any function records in the trace.
  std::string Serialized = serialize(Buffers, 3);
  llvm::DataExtractor DE(Serialized, true, 8);
  auto TraceOrErr = llvm::xray::loadTrace(DE);
  EXPECT_THAT_EXPECTED(TraceOrErr, HasValue(IsEmpty()));
}

} // namespace
} // namespace __xray