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/ADT/StringMap.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/IR/Metadata.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/ProfileData/ProfileCommon.h"
16 #include "llvm/ProfileData/SampleProf.h"
17 #include "llvm/ProfileData/SampleProfReader.h"
18 #include "llvm/ProfileData/SampleProfWriter.h"
19 #include "llvm/Support/Casting.h"
20 #include "llvm/Support/ErrorOr.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "gtest/gtest.h"
24 #include <algorithm>
25 #include <cstdint>
26 #include <limits>
27 #include <memory>
28 #include <string>
29 #include <system_error>
30 #include <vector>
31 
32 using namespace llvm;
33 using namespace sampleprof;
34 
NoError(std::error_code EC)35 static ::testing::AssertionResult NoError(std::error_code EC) {
36   if (!EC)
37     return ::testing::AssertionSuccess();
38   return ::testing::AssertionFailure() << "error " << EC.value() << ": "
39                                        << EC.message();
40 }
41 
42 namespace {
43 
44 struct SampleProfTest : ::testing::Test {
45   std::string Data;
46   LLVMContext Context;
47   std::unique_ptr<raw_ostream> OS;
48   std::unique_ptr<SampleProfileWriter> Writer;
49   std::unique_ptr<SampleProfileReader> Reader;
50 
SampleProfTest__anon04ae7cc10111::SampleProfTest51   SampleProfTest()
52       : Data(), OS(new raw_string_ostream(Data)), Writer(), Reader() {}
53 
createWriter__anon04ae7cc10111::SampleProfTest54   void createWriter(SampleProfileFormat Format) {
55     auto WriterOrErr = SampleProfileWriter::create(OS, Format);
56     ASSERT_TRUE(NoError(WriterOrErr.getError()));
57     Writer = std::move(WriterOrErr.get());
58   }
59 
readProfile__anon04ae7cc10111::SampleProfTest60   void readProfile(std::unique_ptr<MemoryBuffer> &Profile) {
61     auto ReaderOrErr = SampleProfileReader::create(Profile, Context);
62     ASSERT_TRUE(NoError(ReaderOrErr.getError()));
63     Reader = std::move(ReaderOrErr.get());
64   }
65 
testRoundTrip__anon04ae7cc10111::SampleProfTest66   void testRoundTrip(SampleProfileFormat Format) {
67     createWriter(Format);
68 
69     StringRef FooName("_Z3fooi");
70     FunctionSamples FooSamples;
71     FooSamples.setName(FooName);
72     FooSamples.addTotalSamples(7711);
73     FooSamples.addHeadSamples(610);
74     FooSamples.addBodySamples(1, 0, 610);
75     FooSamples.addBodySamples(2, 0, 600);
76     FooSamples.addBodySamples(4, 0, 60000);
77     FooSamples.addBodySamples(8, 0, 60351);
78     FooSamples.addBodySamples(10, 0, 605);
79 
80     StringRef BarName("_Z3bari");
81     FunctionSamples BarSamples;
82     BarSamples.setName(BarName);
83     BarSamples.addTotalSamples(20301);
84     BarSamples.addHeadSamples(1437);
85     BarSamples.addBodySamples(1, 0, 1437);
86 
87     StringMap<FunctionSamples> Profiles;
88     Profiles[FooName] = std::move(FooSamples);
89     Profiles[BarName] = std::move(BarSamples);
90 
91     std::error_code EC;
92     EC = Writer->write(Profiles);
93     ASSERT_TRUE(NoError(EC));
94 
95     Writer->getOutputStream().flush();
96 
97     auto Profile = MemoryBuffer::getMemBufferCopy(Data);
98     readProfile(Profile);
99 
100     EC = Reader->read();
101     ASSERT_TRUE(NoError(EC));
102 
103     StringMap<FunctionSamples> &ReadProfiles = Reader->getProfiles();
104     ASSERT_EQ(2u, ReadProfiles.size());
105 
106     FunctionSamples &ReadFooSamples = ReadProfiles[FooName];
107     ASSERT_EQ(7711u, ReadFooSamples.getTotalSamples());
108     ASSERT_EQ(610u, ReadFooSamples.getHeadSamples());
109 
110     FunctionSamples &ReadBarSamples = ReadProfiles[BarName];
111     ASSERT_EQ(20301u, ReadBarSamples.getTotalSamples());
112     ASSERT_EQ(1437u, ReadBarSamples.getHeadSamples());
113 
114     auto VerifySummary = [](ProfileSummary &Summary) mutable {
115       ASSERT_EQ(ProfileSummary::PSK_Sample, Summary.getKind());
116       ASSERT_EQ(123603u, Summary.getTotalCount());
117       ASSERT_EQ(6u, Summary.getNumCounts());
118       ASSERT_EQ(2u, Summary.getNumFunctions());
119       ASSERT_EQ(1437u, Summary.getMaxFunctionCount());
120       ASSERT_EQ(60351u, Summary.getMaxCount());
121 
122       uint32_t Cutoff = 800000;
123       auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {
124         return PE.Cutoff == Cutoff;
125       };
126       std::vector<ProfileSummaryEntry> &Details = Summary.getDetailedSummary();
127       auto EightyPerc = std::find_if(Details.begin(), Details.end(), Predicate);
128       Cutoff = 900000;
129       auto NinetyPerc = std::find_if(Details.begin(), Details.end(), Predicate);
130       Cutoff = 950000;
131       auto NinetyFivePerc =
132           std::find_if(Details.begin(), Details.end(), Predicate);
133       Cutoff = 990000;
134       auto NinetyNinePerc =
135           std::find_if(Details.begin(), Details.end(), Predicate);
136       ASSERT_EQ(60000u, EightyPerc->MinCount);
137       ASSERT_EQ(60000u, NinetyPerc->MinCount);
138       ASSERT_EQ(60000u, NinetyFivePerc->MinCount);
139       ASSERT_EQ(610u, NinetyNinePerc->MinCount);
140     };
141 
142     ProfileSummary &Summary = Reader->getSummary();
143     VerifySummary(Summary);
144 
145     // Test that conversion of summary to and from Metadata works.
146     Metadata *MD = Summary.getMD(Context);
147     ASSERT_TRUE(MD);
148     ProfileSummary *PS = ProfileSummary::getFromMD(MD);
149     ASSERT_TRUE(PS);
150     VerifySummary(*PS);
151     delete PS;
152 
153     // Test that summary can be attached to and read back from module.
154     Module M("my_module", Context);
155     M.setProfileSummary(MD);
156     MD = M.getProfileSummary();
157     ASSERT_TRUE(MD);
158     PS = ProfileSummary::getFromMD(MD);
159     ASSERT_TRUE(PS);
160     VerifySummary(*PS);
161     delete PS;
162   }
163 };
164 
TEST_F(SampleProfTest,roundtrip_text_profile)165 TEST_F(SampleProfTest, roundtrip_text_profile) {
166   testRoundTrip(SampleProfileFormat::SPF_Text);
167 }
168 
TEST_F(SampleProfTest,roundtrip_binary_profile)169 TEST_F(SampleProfTest, roundtrip_binary_profile) {
170   testRoundTrip(SampleProfileFormat::SPF_Binary);
171 }
172 
TEST_F(SampleProfTest,sample_overflow_saturation)173 TEST_F(SampleProfTest, sample_overflow_saturation) {
174   const uint64_t Max = std::numeric_limits<uint64_t>::max();
175   sampleprof_error Result;
176 
177   StringRef FooName("_Z3fooi");
178   FunctionSamples FooSamples;
179   Result = FooSamples.addTotalSamples(1);
180   ASSERT_EQ(Result, sampleprof_error::success);
181 
182   Result = FooSamples.addHeadSamples(1);
183   ASSERT_EQ(Result, sampleprof_error::success);
184 
185   Result = FooSamples.addBodySamples(10, 0, 1);
186   ASSERT_EQ(Result, sampleprof_error::success);
187 
188   Result = FooSamples.addTotalSamples(Max);
189   ASSERT_EQ(Result, sampleprof_error::counter_overflow);
190   ASSERT_EQ(FooSamples.getTotalSamples(), Max);
191 
192   Result = FooSamples.addHeadSamples(Max);
193   ASSERT_EQ(Result, sampleprof_error::counter_overflow);
194   ASSERT_EQ(FooSamples.getHeadSamples(), Max);
195 
196   Result = FooSamples.addBodySamples(10, 0, Max);
197   ASSERT_EQ(Result, sampleprof_error::counter_overflow);
198   ErrorOr<uint64_t> BodySamples = FooSamples.findSamplesAt(10, 0);
199   ASSERT_FALSE(BodySamples.getError());
200   ASSERT_EQ(BodySamples.get(), Max);
201 }
202 
203 } // end anonymous namespace
204