2016-04-29 02:04:41 +08:00
|
|
|
//===- unittest/ProfileData/SampleProfTest.cpp ------------------*- C++ -*-===//
|
2015-12-11 01:21:42 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2015-12-11 01:21:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:06:56 +08:00
|
|
|
#include "llvm/ProfileData/SampleProf.h"
|
2016-04-29 02:04:41 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-03-02 02:30:58 +08:00
|
|
|
#include "llvm/IR/Metadata.h"
|
2016-04-29 02:04:41 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2015-12-11 01:21:42 +08:00
|
|
|
#include "llvm/ProfileData/SampleProfReader.h"
|
|
|
|
#include "llvm/ProfileData/SampleProfWriter.h"
|
2016-04-29 02:04:41 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-12-11 01:21:42 +08:00
|
|
|
#include "gtest/gtest.h"
|
2016-04-29 02:04:41 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-12-11 01:21:42 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace sampleprof;
|
|
|
|
|
|
|
|
static ::testing::AssertionResult NoError(std::error_code EC) {
|
|
|
|
if (!EC)
|
|
|
|
return ::testing::AssertionSuccess();
|
|
|
|
return ::testing::AssertionFailure() << "error " << EC.value() << ": "
|
|
|
|
<< EC.message();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct SampleProfTest : ::testing::Test {
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext Context;
|
2015-12-11 01:21:42 +08:00
|
|
|
std::unique_ptr<SampleProfileWriter> Writer;
|
|
|
|
std::unique_ptr<SampleProfileReader> Reader;
|
|
|
|
|
2018-09-15 08:04:15 +08:00
|
|
|
SampleProfTest() : Writer(), Reader() {}
|
2015-12-11 01:21:42 +08:00
|
|
|
|
2018-09-17 20:11:01 +08:00
|
|
|
void createWriter(SampleProfileFormat Format, StringRef Profile) {
|
2018-09-15 08:04:15 +08:00
|
|
|
std::error_code EC;
|
|
|
|
std::unique_ptr<raw_ostream> OS(
|
|
|
|
new raw_fd_ostream(Profile, EC, sys::fs::F_None));
|
2015-12-11 01:21:42 +08:00
|
|
|
auto WriterOrErr = SampleProfileWriter::create(OS, Format);
|
|
|
|
ASSERT_TRUE(NoError(WriterOrErr.getError()));
|
|
|
|
Writer = std::move(WriterOrErr.get());
|
|
|
|
}
|
|
|
|
|
2018-09-17 20:11:01 +08:00
|
|
|
void readProfile(const Module &M, StringRef Profile) {
|
2016-04-15 05:59:01 +08:00
|
|
|
auto ReaderOrErr = SampleProfileReader::create(Profile, Context);
|
2015-12-11 01:21:42 +08:00
|
|
|
ASSERT_TRUE(NoError(ReaderOrErr.getError()));
|
|
|
|
Reader = std::move(ReaderOrErr.get());
|
2018-09-15 04:52:59 +08:00
|
|
|
Reader->collectFuncsToUse(M);
|
2015-12-11 01:21:42 +08:00
|
|
|
}
|
|
|
|
|
2018-10-11 05:31:01 +08:00
|
|
|
void testRoundTrip(SampleProfileFormat Format, bool Remap) {
|
2018-09-17 20:11:01 +08:00
|
|
|
SmallVector<char, 128> ProfilePath;
|
|
|
|
ASSERT_TRUE(NoError(llvm::sys::fs::createTemporaryFile("profile", "", ProfilePath)));
|
|
|
|
StringRef Profile(ProfilePath.data(), ProfilePath.size());
|
2018-09-15 08:04:15 +08:00
|
|
|
createWriter(Format, Profile);
|
2015-12-11 01:21:42 +08:00
|
|
|
|
|
|
|
StringRef FooName("_Z3fooi");
|
|
|
|
FunctionSamples FooSamples;
|
2016-03-04 02:09:32 +08:00
|
|
|
FooSamples.setName(FooName);
|
2015-12-11 01:21:42 +08:00
|
|
|
FooSamples.addTotalSamples(7711);
|
|
|
|
FooSamples.addHeadSamples(610);
|
|
|
|
FooSamples.addBodySamples(1, 0, 610);
|
2016-02-19 11:15:33 +08:00
|
|
|
FooSamples.addBodySamples(2, 0, 600);
|
|
|
|
FooSamples.addBodySamples(4, 0, 60000);
|
|
|
|
FooSamples.addBodySamples(8, 0, 60351);
|
|
|
|
FooSamples.addBodySamples(10, 0, 605);
|
2015-12-11 01:21:42 +08:00
|
|
|
|
|
|
|
StringRef BarName("_Z3bari");
|
|
|
|
FunctionSamples BarSamples;
|
2016-03-04 02:09:32 +08:00
|
|
|
BarSamples.setName(BarName);
|
2015-12-11 01:21:42 +08:00
|
|
|
BarSamples.addTotalSamples(20301);
|
|
|
|
BarSamples.addHeadSamples(1437);
|
|
|
|
BarSamples.addBodySamples(1, 0, 1437);
|
2018-06-12 06:40:43 +08:00
|
|
|
// Test how reader/writer handles unmangled names.
|
|
|
|
StringRef MconstructName("_M_construct<char *>");
|
|
|
|
StringRef StringviewName("string_view<std::allocator<char> >");
|
|
|
|
BarSamples.addCalledTargetSamples(1, 0, MconstructName, 1000);
|
|
|
|
BarSamples.addCalledTargetSamples(1, 0, StringviewName, 437);
|
2015-12-11 01:21:42 +08:00
|
|
|
|
2018-09-15 04:52:59 +08:00
|
|
|
Module M("my_module", Context);
|
|
|
|
FunctionType *fn_type =
|
|
|
|
FunctionType::get(Type::getVoidTy(Context), {}, false);
|
|
|
|
M.getOrInsertFunction(FooName, fn_type);
|
|
|
|
M.getOrInsertFunction(BarName, fn_type);
|
|
|
|
|
2015-12-11 01:21:42 +08:00
|
|
|
StringMap<FunctionSamples> Profiles;
|
|
|
|
Profiles[FooName] = std::move(FooSamples);
|
|
|
|
Profiles[BarName] = std::move(BarSamples);
|
|
|
|
|
|
|
|
std::error_code EC;
|
|
|
|
EC = Writer->write(Profiles);
|
|
|
|
ASSERT_TRUE(NoError(EC));
|
|
|
|
|
|
|
|
Writer->getOutputStream().flush();
|
|
|
|
|
2018-09-15 08:04:15 +08:00
|
|
|
readProfile(M, Profile);
|
2015-12-11 01:21:42 +08:00
|
|
|
|
|
|
|
EC = Reader->read();
|
|
|
|
ASSERT_TRUE(NoError(EC));
|
|
|
|
|
2018-10-11 05:31:01 +08:00
|
|
|
if (Remap) {
|
|
|
|
auto MemBuffer = llvm::MemoryBuffer::getMemBuffer(R"(
|
|
|
|
# Types 'int' and 'long' are equivalent
|
|
|
|
type i l
|
|
|
|
# Function names 'foo' and 'faux' are equivalent
|
|
|
|
name 3foo 4faux
|
|
|
|
)");
|
|
|
|
Reader.reset(new SampleProfileReaderItaniumRemapper(
|
|
|
|
std::move(MemBuffer), Context, std::move(Reader)));
|
|
|
|
FooName = "_Z4fauxi";
|
|
|
|
BarName = "_Z3barl";
|
|
|
|
|
|
|
|
EC = Reader->read();
|
|
|
|
ASSERT_TRUE(NoError(EC));
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(2u, Reader->getProfiles().size());
|
|
|
|
|
|
|
|
FunctionSamples *ReadFooSamples = Reader->getSamplesFor(FooName);
|
|
|
|
ASSERT_TRUE(ReadFooSamples != nullptr);
|
2018-12-20 22:09:15 +08:00
|
|
|
if (Format != SampleProfileFormat::SPF_Compact_Binary) {
|
2018-12-19 11:24:03 +08:00
|
|
|
ASSERT_EQ("_Z3fooi", ReadFooSamples->getName());
|
2018-12-20 22:09:15 +08:00
|
|
|
}
|
2018-10-11 05:31:01 +08:00
|
|
|
ASSERT_EQ(7711u, ReadFooSamples->getTotalSamples());
|
|
|
|
ASSERT_EQ(610u, ReadFooSamples->getHeadSamples());
|
|
|
|
|
|
|
|
FunctionSamples *ReadBarSamples = Reader->getSamplesFor(BarName);
|
|
|
|
ASSERT_TRUE(ReadBarSamples != nullptr);
|
2018-12-20 22:09:15 +08:00
|
|
|
if (Format != SampleProfileFormat::SPF_Compact_Binary) {
|
2018-12-19 11:24:03 +08:00
|
|
|
ASSERT_EQ("_Z3bari", ReadBarSamples->getName());
|
2018-12-20 22:09:15 +08:00
|
|
|
}
|
2018-10-11 05:31:01 +08:00
|
|
|
ASSERT_EQ(20301u, ReadBarSamples->getTotalSamples());
|
|
|
|
ASSERT_EQ(1437u, ReadBarSamples->getHeadSamples());
|
2018-03-08 00:45:33 +08:00
|
|
|
ErrorOr<SampleRecord::CallTargetMap> CTMap =
|
2018-10-11 05:31:01 +08:00
|
|
|
ReadBarSamples->findCallTargetMapAt(1, 0);
|
2018-03-08 00:45:33 +08:00
|
|
|
ASSERT_FALSE(CTMap.getError());
|
2018-06-12 06:40:43 +08:00
|
|
|
|
|
|
|
std::string MconstructGUID;
|
|
|
|
StringRef MconstructRep =
|
|
|
|
getRepInFormat(MconstructName, Format, MconstructGUID);
|
|
|
|
std::string StringviewGUID;
|
|
|
|
StringRef StringviewRep =
|
|
|
|
getRepInFormat(StringviewName, Format, StringviewGUID);
|
|
|
|
ASSERT_EQ(1000u, CTMap.get()[MconstructRep]);
|
|
|
|
ASSERT_EQ(437u, CTMap.get()[StringviewRep]);
|
2016-02-19 11:15:33 +08:00
|
|
|
|
2016-05-20 05:53:28 +08:00
|
|
|
auto VerifySummary = [](ProfileSummary &Summary) mutable {
|
|
|
|
ASSERT_EQ(ProfileSummary::PSK_Sample, Summary.getKind());
|
|
|
|
ASSERT_EQ(123603u, Summary.getTotalCount());
|
|
|
|
ASSERT_EQ(6u, Summary.getNumCounts());
|
2016-03-02 02:30:58 +08:00
|
|
|
ASSERT_EQ(2u, Summary.getNumFunctions());
|
2016-03-29 07:14:29 +08:00
|
|
|
ASSERT_EQ(1437u, Summary.getMaxFunctionCount());
|
2016-05-20 05:53:28 +08:00
|
|
|
ASSERT_EQ(60351u, Summary.getMaxCount());
|
2016-03-02 02:30:58 +08:00
|
|
|
|
|
|
|
uint32_t Cutoff = 800000;
|
|
|
|
auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {
|
|
|
|
return PE.Cutoff == Cutoff;
|
|
|
|
};
|
|
|
|
std::vector<ProfileSummaryEntry> &Details = Summary.getDetailedSummary();
|
2016-08-12 08:18:03 +08:00
|
|
|
auto EightyPerc = find_if(Details, Predicate);
|
2016-03-02 02:30:58 +08:00
|
|
|
Cutoff = 900000;
|
2016-08-12 08:18:03 +08:00
|
|
|
auto NinetyPerc = find_if(Details, Predicate);
|
2016-03-02 02:30:58 +08:00
|
|
|
Cutoff = 950000;
|
2016-08-12 08:18:03 +08:00
|
|
|
auto NinetyFivePerc = find_if(Details, Predicate);
|
2016-03-02 02:30:58 +08:00
|
|
|
Cutoff = 990000;
|
2016-08-12 08:18:03 +08:00
|
|
|
auto NinetyNinePerc = find_if(Details, Predicate);
|
2016-03-02 02:30:58 +08:00
|
|
|
ASSERT_EQ(60000u, EightyPerc->MinCount);
|
|
|
|
ASSERT_EQ(60000u, NinetyPerc->MinCount);
|
|
|
|
ASSERT_EQ(60000u, NinetyFivePerc->MinCount);
|
|
|
|
ASSERT_EQ(610u, NinetyNinePerc->MinCount);
|
2016-02-19 11:15:33 +08:00
|
|
|
};
|
2016-03-02 02:30:58 +08:00
|
|
|
|
2016-05-20 05:53:28 +08:00
|
|
|
ProfileSummary &Summary = Reader->getSummary();
|
2016-03-02 02:30:58 +08:00
|
|
|
VerifySummary(Summary);
|
|
|
|
|
2016-03-19 05:29:30 +08:00
|
|
|
// Test that conversion of summary to and from Metadata works.
|
2016-04-15 05:59:01 +08:00
|
|
|
Metadata *MD = Summary.getMD(Context);
|
2016-03-02 02:30:58 +08:00
|
|
|
ASSERT_TRUE(MD);
|
|
|
|
ProfileSummary *PS = ProfileSummary::getFromMD(MD);
|
|
|
|
ASSERT_TRUE(PS);
|
2016-05-20 05:53:28 +08:00
|
|
|
VerifySummary(*PS);
|
|
|
|
delete PS;
|
2016-03-19 05:29:30 +08:00
|
|
|
|
|
|
|
// Test that summary can be attached to and read back from module.
|
|
|
|
M.setProfileSummary(MD);
|
|
|
|
MD = M.getProfileSummary();
|
|
|
|
ASSERT_TRUE(MD);
|
|
|
|
PS = ProfileSummary::getFromMD(MD);
|
|
|
|
ASSERT_TRUE(PS);
|
2016-05-20 05:53:28 +08:00
|
|
|
VerifySummary(*PS);
|
|
|
|
delete PS;
|
2015-12-11 01:21:42 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(SampleProfTest, roundtrip_text_profile) {
|
2018-10-11 05:31:01 +08:00
|
|
|
testRoundTrip(SampleProfileFormat::SPF_Text, false);
|
2015-12-11 01:21:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-12 06:40:43 +08:00
|
|
|
TEST_F(SampleProfTest, roundtrip_raw_binary_profile) {
|
2018-10-11 05:31:01 +08:00
|
|
|
testRoundTrip(SampleProfileFormat::SPF_Binary, false);
|
2018-06-12 06:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SampleProfTest, roundtrip_compact_binary_profile) {
|
2018-10-11 05:31:01 +08:00
|
|
|
testRoundTrip(SampleProfileFormat::SPF_Compact_Binary, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SampleProfTest, remap_text_profile) {
|
|
|
|
testRoundTrip(SampleProfileFormat::SPF_Text, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SampleProfTest, remap_raw_binary_profile) {
|
|
|
|
testRoundTrip(SampleProfileFormat::SPF_Binary, true);
|
2015-12-11 01:21:42 +08:00
|
|
|
}
|
|
|
|
|
2015-12-17 05:45:43 +08:00
|
|
|
TEST_F(SampleProfTest, sample_overflow_saturation) {
|
|
|
|
const uint64_t Max = std::numeric_limits<uint64_t>::max();
|
|
|
|
sampleprof_error Result;
|
|
|
|
|
|
|
|
StringRef FooName("_Z3fooi");
|
|
|
|
FunctionSamples FooSamples;
|
|
|
|
Result = FooSamples.addTotalSamples(1);
|
|
|
|
ASSERT_EQ(Result, sampleprof_error::success);
|
|
|
|
|
|
|
|
Result = FooSamples.addHeadSamples(1);
|
|
|
|
ASSERT_EQ(Result, sampleprof_error::success);
|
|
|
|
|
|
|
|
Result = FooSamples.addBodySamples(10, 0, 1);
|
|
|
|
ASSERT_EQ(Result, sampleprof_error::success);
|
|
|
|
|
|
|
|
Result = FooSamples.addTotalSamples(Max);
|
|
|
|
ASSERT_EQ(Result, sampleprof_error::counter_overflow);
|
|
|
|
ASSERT_EQ(FooSamples.getTotalSamples(), Max);
|
|
|
|
|
|
|
|
Result = FooSamples.addHeadSamples(Max);
|
|
|
|
ASSERT_EQ(Result, sampleprof_error::counter_overflow);
|
|
|
|
ASSERT_EQ(FooSamples.getHeadSamples(), Max);
|
|
|
|
|
|
|
|
Result = FooSamples.addBodySamples(10, 0, Max);
|
|
|
|
ASSERT_EQ(Result, sampleprof_error::counter_overflow);
|
|
|
|
ErrorOr<uint64_t> BodySamples = FooSamples.findSamplesAt(10, 0);
|
|
|
|
ASSERT_FALSE(BodySamples.getError());
|
|
|
|
ASSERT_EQ(BodySamples.get(), Max);
|
|
|
|
}
|
|
|
|
|
2015-12-11 01:21:42 +08:00
|
|
|
} // end anonymous namespace
|