1 //===- unittest/ProfileData/SampleProfTest.cpp ------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ProfileData/SampleProf.h"
11 #include "llvm/ADT/StringMap.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Metadata.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/ProfileData/SampleProfReader.h"
17 #include "llvm/ProfileData/SampleProfWriter.h"
18 #include "llvm/Support/Casting.h"
19 #include "llvm/Support/ErrorOr.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "gtest/gtest.h"
23 #include <string>
24 #include <vector>
25
26 using namespace llvm;
27 using namespace sampleprof;
28
NoError(std::error_code EC)29 static ::testing::AssertionResult NoError(std::error_code EC) {
30 if (!EC)
31 return ::testing::AssertionSuccess();
32 return ::testing::AssertionFailure() << "error " << EC.value() << ": "
33 << EC.message();
34 }
35
36 namespace {
37
38 struct SampleProfTest : ::testing::Test {
39 std::string Data;
40 LLVMContext Context;
41 std::unique_ptr<raw_ostream> OS;
42 std::unique_ptr<SampleProfileWriter> Writer;
43 std::unique_ptr<SampleProfileReader> Reader;
44
SampleProfTest__anonaddd3f240111::SampleProfTest45 SampleProfTest()
46 : Data(), OS(new raw_string_ostream(Data)), Writer(), Reader() {}
47
createWriter__anonaddd3f240111::SampleProfTest48 void createWriter(SampleProfileFormat Format) {
49 auto WriterOrErr = SampleProfileWriter::create(OS, Format);
50 ASSERT_TRUE(NoError(WriterOrErr.getError()));
51 Writer = std::move(WriterOrErr.get());
52 }
53
readProfile__anonaddd3f240111::SampleProfTest54 void readProfile(std::unique_ptr<MemoryBuffer> &Profile) {
55 auto ReaderOrErr = SampleProfileReader::create(Profile, Context);
56 ASSERT_TRUE(NoError(ReaderOrErr.getError()));
57 Reader = std::move(ReaderOrErr.get());
58 }
59
testRoundTrip__anonaddd3f240111::SampleProfTest60 void testRoundTrip(SampleProfileFormat Format) {
61 createWriter(Format);
62
63 StringRef FooName("_Z3fooi");
64 FunctionSamples FooSamples;
65 FooSamples.setName(FooName);
66 FooSamples.addTotalSamples(7711);
67 FooSamples.addHeadSamples(610);
68 FooSamples.addBodySamples(1, 0, 610);
69 FooSamples.addBodySamples(2, 0, 600);
70 FooSamples.addBodySamples(4, 0, 60000);
71 FooSamples.addBodySamples(8, 0, 60351);
72 FooSamples.addBodySamples(10, 0, 605);
73
74 StringRef BarName("_Z3bari");
75 FunctionSamples BarSamples;
76 BarSamples.setName(BarName);
77 BarSamples.addTotalSamples(20301);
78 BarSamples.addHeadSamples(1437);
79 BarSamples.addBodySamples(1, 0, 1437);
80 // Test how reader/writer handles unmangled names.
81 StringRef MconstructName("_M_construct<char *>");
82 StringRef StringviewName("string_view<std::allocator<char> >");
83 BarSamples.addCalledTargetSamples(1, 0, MconstructName, 1000);
84 BarSamples.addCalledTargetSamples(1, 0, StringviewName, 437);
85
86 StringMap<FunctionSamples> Profiles;
87 Profiles[FooName] = std::move(FooSamples);
88 Profiles[BarName] = std::move(BarSamples);
89
90 std::error_code EC;
91 EC = Writer->write(Profiles);
92 ASSERT_TRUE(NoError(EC));
93
94 Writer->getOutputStream().flush();
95
96 auto Profile = MemoryBuffer::getMemBufferCopy(Data);
97 readProfile(Profile);
98
99 EC = Reader->read();
100 ASSERT_TRUE(NoError(EC));
101
102 StringMap<FunctionSamples> &ReadProfiles = Reader->getProfiles();
103 ASSERT_EQ(2u, ReadProfiles.size());
104
105 std::string FooGUID;
106 StringRef FooRep = getRepInFormat(FooName, Format, FooGUID);
107 FunctionSamples &ReadFooSamples = ReadProfiles[FooRep];
108 ASSERT_EQ(7711u, ReadFooSamples.getTotalSamples());
109 ASSERT_EQ(610u, ReadFooSamples.getHeadSamples());
110
111 std::string BarGUID;
112 StringRef BarRep = getRepInFormat(BarName, Format, BarGUID);
113 FunctionSamples &ReadBarSamples = ReadProfiles[BarRep];
114 ASSERT_EQ(20301u, ReadBarSamples.getTotalSamples());
115 ASSERT_EQ(1437u, ReadBarSamples.getHeadSamples());
116 ErrorOr<SampleRecord::CallTargetMap> CTMap =
117 ReadBarSamples.findCallTargetMapAt(1, 0);
118 ASSERT_FALSE(CTMap.getError());
119
120 std::string MconstructGUID;
121 StringRef MconstructRep =
122 getRepInFormat(MconstructName, Format, MconstructGUID);
123 std::string StringviewGUID;
124 StringRef StringviewRep =
125 getRepInFormat(StringviewName, Format, StringviewGUID);
126 ASSERT_EQ(1000u, CTMap.get()[MconstructRep]);
127 ASSERT_EQ(437u, CTMap.get()[StringviewRep]);
128
129 auto VerifySummary = [](ProfileSummary &Summary) mutable {
130 ASSERT_EQ(ProfileSummary::PSK_Sample, Summary.getKind());
131 ASSERT_EQ(123603u, Summary.getTotalCount());
132 ASSERT_EQ(6u, Summary.getNumCounts());
133 ASSERT_EQ(2u, Summary.getNumFunctions());
134 ASSERT_EQ(1437u, Summary.getMaxFunctionCount());
135 ASSERT_EQ(60351u, Summary.getMaxCount());
136
137 uint32_t Cutoff = 800000;
138 auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {
139 return PE.Cutoff == Cutoff;
140 };
141 std::vector<ProfileSummaryEntry> &Details = Summary.getDetailedSummary();
142 auto EightyPerc = find_if(Details, Predicate);
143 Cutoff = 900000;
144 auto NinetyPerc = find_if(Details, Predicate);
145 Cutoff = 950000;
146 auto NinetyFivePerc = find_if(Details, Predicate);
147 Cutoff = 990000;
148 auto NinetyNinePerc = find_if(Details, Predicate);
149 ASSERT_EQ(60000u, EightyPerc->MinCount);
150 ASSERT_EQ(60000u, NinetyPerc->MinCount);
151 ASSERT_EQ(60000u, NinetyFivePerc->MinCount);
152 ASSERT_EQ(610u, NinetyNinePerc->MinCount);
153 };
154
155 ProfileSummary &Summary = Reader->getSummary();
156 VerifySummary(Summary);
157
158 // Test that conversion of summary to and from Metadata works.
159 Metadata *MD = Summary.getMD(Context);
160 ASSERT_TRUE(MD);
161 ProfileSummary *PS = ProfileSummary::getFromMD(MD);
162 ASSERT_TRUE(PS);
163 VerifySummary(*PS);
164 delete PS;
165
166 // Test that summary can be attached to and read back from module.
167 Module M("my_module", Context);
168 M.setProfileSummary(MD);
169 MD = M.getProfileSummary();
170 ASSERT_TRUE(MD);
171 PS = ProfileSummary::getFromMD(MD);
172 ASSERT_TRUE(PS);
173 VerifySummary(*PS);
174 delete PS;
175 }
176 };
177
TEST_F(SampleProfTest,roundtrip_text_profile)178 TEST_F(SampleProfTest, roundtrip_text_profile) {
179 testRoundTrip(SampleProfileFormat::SPF_Text);
180 }
181
TEST_F(SampleProfTest,roundtrip_raw_binary_profile)182 TEST_F(SampleProfTest, roundtrip_raw_binary_profile) {
183 testRoundTrip(SampleProfileFormat::SPF_Binary);
184 }
185
TEST_F(SampleProfTest,roundtrip_compact_binary_profile)186 TEST_F(SampleProfTest, roundtrip_compact_binary_profile) {
187 testRoundTrip(SampleProfileFormat::SPF_Compact_Binary);
188 }
189
TEST_F(SampleProfTest,sample_overflow_saturation)190 TEST_F(SampleProfTest, sample_overflow_saturation) {
191 const uint64_t Max = std::numeric_limits<uint64_t>::max();
192 sampleprof_error Result;
193
194 StringRef FooName("_Z3fooi");
195 FunctionSamples FooSamples;
196 Result = FooSamples.addTotalSamples(1);
197 ASSERT_EQ(Result, sampleprof_error::success);
198
199 Result = FooSamples.addHeadSamples(1);
200 ASSERT_EQ(Result, sampleprof_error::success);
201
202 Result = FooSamples.addBodySamples(10, 0, 1);
203 ASSERT_EQ(Result, sampleprof_error::success);
204
205 Result = FooSamples.addTotalSamples(Max);
206 ASSERT_EQ(Result, sampleprof_error::counter_overflow);
207 ASSERT_EQ(FooSamples.getTotalSamples(), Max);
208
209 Result = FooSamples.addHeadSamples(Max);
210 ASSERT_EQ(Result, sampleprof_error::counter_overflow);
211 ASSERT_EQ(FooSamples.getHeadSamples(), Max);
212
213 Result = FooSamples.addBodySamples(10, 0, Max);
214 ASSERT_EQ(Result, sampleprof_error::counter_overflow);
215 ErrorOr<uint64_t> BodySamples = FooSamples.findSamplesAt(10, 0);
216 ASSERT_FALSE(BodySamples.getError());
217 ASSERT_EQ(BodySamples.get(), Max);
218 }
219
220 } // end anonymous namespace
221