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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
//===-------------- PGOCtxProfReadWriteTest.cpp ---------------------------===//
//
// 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/Bitstream/BitstreamReader.h"
#include "llvm/ProfileData/CtxInstrContextNode.h"
#include "llvm/ProfileData/PGOCtxProfReader.h"
#include "llvm/ProfileData/PGOCtxProfWriter.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace llvm::ctx_profile;
class PGOCtxProfRWTest : public ::testing::Test {
std::vector<std::unique_ptr<char[]>> Nodes;
std::map<GUID, const ContextNode *> Roots;
public:
ContextNode *createNode(GUID Guid, uint32_t NrCounters, uint32_t NrCallsites,
ContextNode *Next = nullptr) {
auto AllocSize = ContextNode::getAllocSize(NrCounters, NrCallsites);
auto *Mem = Nodes.emplace_back(std::make_unique<char[]>(AllocSize)).get();
std::memset(Mem, 0, AllocSize);
auto *Ret = new (Mem) ContextNode(Guid, NrCounters, NrCallsites, Next);
return Ret;
}
void SetUp() override {
// Root (guid 1) has 2 callsites, one used for an indirect call to either
// guid 2 or 4.
// guid 2 calls guid 5
// guid 5 calls guid 2
// there's also a second root, guid3.
auto *Root1 = createNode(1, 2, 2);
Root1->counters()[0] = 10;
Root1->counters()[1] = 11;
Roots.insert({1, Root1});
auto *L1 = createNode(2, 1, 1);
L1->counters()[0] = 12;
Root1->subContexts()[1] = createNode(4, 3, 1, L1);
Root1->subContexts()[1]->counters()[0] = 13;
Root1->subContexts()[1]->counters()[1] = 14;
Root1->subContexts()[1]->counters()[2] = 15;
auto *L3 = createNode(5, 6, 3);
for (auto I = 0; I < 6; ++I)
L3->counters()[I] = 16 + I;
L1->subContexts()[0] = L3;
L3->subContexts()[2] = createNode(2, 1, 1);
L3->subContexts()[2]->counters()[0] = 30;
auto *Root2 = createNode(3, 1, 0);
Root2->counters()[0] = 40;
Roots.insert({3, Root2});
}
const std::map<GUID, const ContextNode *> &roots() const { return Roots; }
};
void checkSame(const ContextNode &Raw, const PGOContextualProfile &Profile) {
EXPECT_EQ(Raw.guid(), Profile.guid());
ASSERT_EQ(Raw.counters_size(), Profile.counters().size());
for (auto I = 0U; I < Raw.counters_size(); ++I)
EXPECT_EQ(Raw.counters()[I], Profile.counters()[I]);
for (auto I = 0U; I < Raw.callsites_size(); ++I) {
if (Raw.subContexts()[I] == nullptr)
continue;
EXPECT_TRUE(Profile.hasCallsite(I));
const auto &ProfileTargets = Profile.callsite(I);
std::map<GUID, const ContextNode *> Targets;
for (const auto *N = Raw.subContexts()[I]; N; N = N->next())
EXPECT_TRUE(Targets.insert({N->guid(), N}).second);
EXPECT_EQ(Targets.size(), ProfileTargets.size());
for (auto It : Targets) {
auto PIt = ProfileTargets.find(It.second->guid());
EXPECT_NE(PIt, ProfileTargets.end());
checkSame(*It.second, PIt->second);
}
}
}
TEST_F(PGOCtxProfRWTest, RoundTrip) {
llvm::unittest::TempFile ProfileFile("ctx_profile", "", "", /*Unique*/ true);
{
std::error_code EC;
raw_fd_stream Out(ProfileFile.path(), EC);
ASSERT_FALSE(EC);
{
PGOCtxProfileWriter Writer(Out);
for (auto &[_, R] : roots())
Writer.write(*R);
}
}
{
ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
MemoryBuffer::getFile(ProfileFile.path());
ASSERT_TRUE(!!MB);
ASSERT_NE(*MB, nullptr);
BitstreamCursor Cursor((*MB)->getBuffer());
PGOCtxProfileReader Reader(Cursor);
auto Expected = Reader.loadContexts();
ASSERT_TRUE(!!Expected);
auto &Ctxes = *Expected;
EXPECT_EQ(Ctxes.size(), roots().size());
EXPECT_EQ(Ctxes.size(), 2U);
for (auto &[G, R] : roots())
checkSame(*R, Ctxes.find(G)->second);
DenseSet<GlobalValue::GUID> Guids;
Ctxes.at(1U).getContainedGuids(Guids);
EXPECT_THAT(Guids,
testing::WhenSorted(testing::ElementsAre(1U, 2U, 4U, 5U)));
Guids.clear();
Ctxes.at(3U).getContainedGuids(Guids);
EXPECT_THAT(Guids, testing::ElementsAre(3U));
}
}
TEST_F(PGOCtxProfRWTest, InvalidCounters) {
auto *R = createNode(1, 0, 1);
llvm::unittest::TempFile ProfileFile("ctx_profile", "", "", /*Unique*/ true);
{
std::error_code EC;
raw_fd_stream Out(ProfileFile.path(), EC);
ASSERT_FALSE(EC);
{
PGOCtxProfileWriter Writer(Out);
Writer.write(*R);
}
}
{
auto MB = MemoryBuffer::getFile(ProfileFile.path());
ASSERT_TRUE(!!MB);
ASSERT_NE(*MB, nullptr);
BitstreamCursor Cursor((*MB)->getBuffer());
PGOCtxProfileReader Reader(Cursor);
auto Expected = Reader.loadContexts();
EXPECT_FALSE(Expected);
consumeError(Expected.takeError());
}
}
TEST_F(PGOCtxProfRWTest, Empty) {
BitstreamCursor Cursor("");
PGOCtxProfileReader Reader(Cursor);
auto Expected = Reader.loadContexts();
EXPECT_FALSE(Expected);
consumeError(Expected.takeError());
}
TEST_F(PGOCtxProfRWTest, Invalid) {
BitstreamCursor Cursor("Surely this is not valid");
PGOCtxProfileReader Reader(Cursor);
auto Expected = Reader.loadContexts();
EXPECT_FALSE(Expected);
consumeError(Expected.takeError());
}
TEST_F(PGOCtxProfRWTest, ValidButEmpty) {
llvm::unittest::TempFile ProfileFile("ctx_profile", "", "", /*Unique*/ true);
{
std::error_code EC;
raw_fd_stream Out(ProfileFile.path(), EC);
ASSERT_FALSE(EC);
{
PGOCtxProfileWriter Writer(Out);
// don't write anything - this will just produce the metadata subblock.
}
}
{
auto MB = MemoryBuffer::getFile(ProfileFile.path());
ASSERT_TRUE(!!MB);
ASSERT_NE(*MB, nullptr);
BitstreamCursor Cursor((*MB)->getBuffer());
PGOCtxProfileReader Reader(Cursor);
auto Expected = Reader.loadContexts();
EXPECT_TRUE(!!Expected);
EXPECT_TRUE(Expected->empty());
}
}
TEST_F(PGOCtxProfRWTest, WrongVersion) {
llvm::unittest::TempFile ProfileFile("ctx_profile", "", "", /*Unique*/ true);
{
std::error_code EC;
raw_fd_stream Out(ProfileFile.path(), EC);
ASSERT_FALSE(EC);
{
PGOCtxProfileWriter Writer(Out, PGOCtxProfileWriter::CurrentVersion + 1);
}
}
{
auto MB = MemoryBuffer::getFile(ProfileFile.path());
ASSERT_TRUE(!!MB);
ASSERT_NE(*MB, nullptr);
BitstreamCursor Cursor((*MB)->getBuffer());
PGOCtxProfileReader Reader(Cursor);
auto Expected = Reader.loadContexts();
EXPECT_FALSE(Expected);
consumeError(Expected.takeError());
}
}
TEST_F(PGOCtxProfRWTest, DuplicateRoots) {
llvm::unittest::TempFile ProfileFile("ctx_profile", "", "", /*Unique*/ true);
{
std::error_code EC;
raw_fd_stream Out(ProfileFile.path(), EC);
ASSERT_FALSE(EC);
{
PGOCtxProfileWriter Writer(Out);
Writer.write(*createNode(1, 1, 1));
Writer.write(*createNode(1, 1, 1));
}
}
{
auto MB = MemoryBuffer::getFile(ProfileFile.path());
ASSERT_TRUE(!!MB);
ASSERT_NE(*MB, nullptr);
BitstreamCursor Cursor((*MB)->getBuffer());
PGOCtxProfileReader Reader(Cursor);
auto Expected = Reader.loadContexts();
EXPECT_FALSE(Expected);
consumeError(Expected.takeError());
}
}
TEST_F(PGOCtxProfRWTest, DuplicateTargets) {
llvm::unittest::TempFile ProfileFile("ctx_profile", "", "", /*Unique*/ true);
{
std::error_code EC;
raw_fd_stream Out(ProfileFile.path(), EC);
ASSERT_FALSE(EC);
{
auto *R = createNode(1, 1, 1);
auto *L1 = createNode(2, 1, 0);
auto *L2 = createNode(2, 1, 0, L1);
R->subContexts()[0] = L2;
PGOCtxProfileWriter Writer(Out);
Writer.write(*R);
}
}
{
auto MB = MemoryBuffer::getFile(ProfileFile.path());
ASSERT_TRUE(!!MB);
ASSERT_NE(*MB, nullptr);
BitstreamCursor Cursor((*MB)->getBuffer());
PGOCtxProfileReader Reader(Cursor);
auto Expected = Reader.loadContexts();
EXPECT_FALSE(Expected);
consumeError(Expected.takeError());
}
}
|