Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 1 | //===- unittest/ProfileData/InstrProfTest.cpp -------------------*- C++ -*-===// |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 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/InstrProfReader.h" |
| 11 | #include "llvm/ProfileData/InstrProfWriter.h" |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | #include <cstdarg> |
| 15 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | static ::testing::AssertionResult NoError(std::error_code EC) { |
| 19 | if (!EC) |
| 20 | return ::testing::AssertionSuccess(); |
| 21 | return ::testing::AssertionFailure() << "error " << EC.value() |
| 22 | << ": " << EC.message(); |
| 23 | } |
| 24 | |
| 25 | static ::testing::AssertionResult ErrorEquals(std::error_code Expected, |
| 26 | std::error_code Found) { |
| 27 | if (Expected == Found) |
| 28 | return ::testing::AssertionSuccess(); |
| 29 | return ::testing::AssertionFailure() << "error " << Found.value() |
| 30 | << ": " << Found.message(); |
| 31 | } |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | struct InstrProfTest : ::testing::Test { |
| 36 | InstrProfWriter Writer; |
| 37 | std::unique_ptr<IndexedInstrProfReader> Reader; |
| 38 | |
| 39 | void readProfile(std::unique_ptr<MemoryBuffer> Profile) { |
| 40 | auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile)); |
| 41 | ASSERT_TRUE(NoError(ReaderOrErr.getError())); |
| 42 | Reader = std::move(ReaderOrErr.get()); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | TEST_F(InstrProfTest, write_and_read_empty_profile) { |
| 47 | auto Profile = Writer.writeBuffer(); |
| 48 | readProfile(std::move(Profile)); |
| 49 | ASSERT_TRUE(Reader->begin() == Reader->end()); |
| 50 | } |
| 51 | |
| 52 | TEST_F(InstrProfTest, write_and_read_one_function) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 53 | InstrProfRecord Record("foo", 0x1234, {1, 2, 3, 4}); |
| 54 | Writer.addRecord(std::move(Record)); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 55 | auto Profile = Writer.writeBuffer(); |
| 56 | readProfile(std::move(Profile)); |
| 57 | |
| 58 | auto I = Reader->begin(), E = Reader->end(); |
| 59 | ASSERT_TRUE(I != E); |
| 60 | ASSERT_EQ(StringRef("foo"), I->Name); |
| 61 | ASSERT_EQ(0x1234U, I->Hash); |
| 62 | ASSERT_EQ(4U, I->Counts.size()); |
| 63 | ASSERT_EQ(1U, I->Counts[0]); |
| 64 | ASSERT_EQ(2U, I->Counts[1]); |
| 65 | ASSERT_EQ(3U, I->Counts[2]); |
| 66 | ASSERT_EQ(4U, I->Counts[3]); |
| 67 | ASSERT_TRUE(++I == E); |
| 68 | } |
| 69 | |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 70 | TEST_F(InstrProfTest, get_instr_prof_record) { |
| 71 | InstrProfRecord Record1("foo", 0x1234, {1, 2}); |
| 72 | InstrProfRecord Record2("foo", 0x1235, {3, 4}); |
| 73 | Writer.addRecord(std::move(Record1)); |
| 74 | Writer.addRecord(std::move(Record2)); |
| 75 | auto Profile = Writer.writeBuffer(); |
| 76 | readProfile(std::move(Profile)); |
| 77 | |
| 78 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("foo", 0x1234); |
| 79 | ASSERT_TRUE(NoError(R.getError())); |
| 80 | ASSERT_EQ(2U, R.get().Counts.size()); |
| 81 | ASSERT_EQ(1U, R.get().Counts[0]); |
| 82 | ASSERT_EQ(2U, R.get().Counts[1]); |
| 83 | |
| 84 | R = Reader->getInstrProfRecord("foo", 0x1235); |
| 85 | ASSERT_TRUE(NoError(R.getError())); |
| 86 | ASSERT_EQ(2U, R.get().Counts.size()); |
| 87 | ASSERT_EQ(3U, R.get().Counts[0]); |
| 88 | ASSERT_EQ(4U, R.get().Counts[1]); |
| 89 | |
| 90 | R = Reader->getInstrProfRecord("foo", 0x5678); |
| 91 | ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.getError())); |
| 92 | |
| 93 | R = Reader->getInstrProfRecord("bar", 0x1234); |
| 94 | ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.getError())); |
| 95 | } |
| 96 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 97 | TEST_F(InstrProfTest, get_function_counts) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 98 | InstrProfRecord Record1("foo", 0x1234, {1, 2}); |
| 99 | InstrProfRecord Record2("foo", 0x1235, {3, 4}); |
| 100 | Writer.addRecord(std::move(Record1)); |
| 101 | Writer.addRecord(std::move(Record2)); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 102 | auto Profile = Writer.writeBuffer(); |
| 103 | readProfile(std::move(Profile)); |
| 104 | |
| 105 | std::vector<uint64_t> Counts; |
| 106 | ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts))); |
| 107 | ASSERT_EQ(2U, Counts.size()); |
| 108 | ASSERT_EQ(1U, Counts[0]); |
| 109 | ASSERT_EQ(2U, Counts[1]); |
| 110 | |
Justin Bogner | 09829f4 | 2015-06-22 23:56:53 +0000 | [diff] [blame] | 111 | ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts))); |
| 112 | ASSERT_EQ(2U, Counts.size()); |
| 113 | ASSERT_EQ(3U, Counts[0]); |
| 114 | ASSERT_EQ(4U, Counts[1]); |
| 115 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 116 | std::error_code EC; |
| 117 | EC = Reader->getFunctionCounts("foo", 0x5678, Counts); |
| 118 | ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC)); |
| 119 | |
| 120 | EC = Reader->getFunctionCounts("bar", 0x1234, Counts); |
| 121 | ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC)); |
| 122 | } |
| 123 | |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 124 | TEST_F(InstrProfTest, get_icall_data_read_write) { |
| 125 | InstrProfRecord Record1("caller", 0x1234, {1, 2}); |
| 126 | InstrProfRecord Record2("callee1", 0x1235, {3, 4}); |
| 127 | InstrProfRecord Record3("callee2", 0x1235, {3, 4}); |
| 128 | InstrProfRecord Record4("callee3", 0x1235, {3, 4}); |
| 129 | |
| 130 | // 4 value sites. |
| 131 | Record1.reserveSites(IPVK_IndirectCallTarget, 4); |
| 132 | InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1}, |
| 133 | {(uint64_t) "callee2", 2}, |
| 134 | {(uint64_t) "callee3", 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 135 | Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 136 | // No valeu profile data at the second site. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 137 | Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 138 | InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1}, |
| 139 | {(uint64_t) "callee2", 2}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 140 | Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 141 | InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 142 | Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 143 | |
| 144 | Writer.addRecord(std::move(Record1)); |
| 145 | Writer.addRecord(std::move(Record2)); |
| 146 | Writer.addRecord(std::move(Record3)); |
| 147 | Writer.addRecord(std::move(Record4)); |
| 148 | auto Profile = Writer.writeBuffer(); |
| 149 | readProfile(std::move(Profile)); |
| 150 | |
| 151 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 152 | ASSERT_TRUE(NoError(R.getError())); |
| 153 | ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget)); |
| 154 | ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 155 | ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 156 | ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 157 | ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
| 158 | |
| 159 | std::unique_ptr<InstrProfValueData[]> VD = |
| 160 | R.get().getValueForSite(IPVK_IndirectCallTarget, 0); |
| 161 | // Now sort the target acording to frequency. |
| 162 | std::sort(&VD[0], &VD[3], |
| 163 | [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) { |
| 164 | return VD1.Count > VD2.Count; |
| 165 | }); |
| 166 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3")); |
| 167 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2")); |
| 168 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1")); |
| 169 | } |
| 170 | |
| 171 | TEST_F(InstrProfTest, get_icall_data_merge1) { |
| 172 | InstrProfRecord Record11("caller", 0x1234, {1, 2}); |
| 173 | InstrProfRecord Record12("caller", 0x1234, {1, 2}); |
| 174 | InstrProfRecord Record2("callee1", 0x1235, {3, 4}); |
| 175 | InstrProfRecord Record3("callee2", 0x1235, {3, 4}); |
| 176 | InstrProfRecord Record4("callee3", 0x1235, {3, 4}); |
| 177 | InstrProfRecord Record5("callee3", 0x1235, {3, 4}); |
| 178 | InstrProfRecord Record6("callee4", 0x1235, {3, 5}); |
| 179 | |
| 180 | // 5 value sites. |
| 181 | Record11.reserveSites(IPVK_IndirectCallTarget, 5); |
| 182 | InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1}, |
| 183 | {(uint64_t) "callee2", 2}, |
| 184 | {(uint64_t) "callee3", 3}, |
| 185 | {(uint64_t) "callee4", 4}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 186 | Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 4, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 187 | |
| 188 | // No valeu profile data at the second site. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 189 | Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 190 | |
| 191 | InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1}, |
| 192 | {(uint64_t) "callee2", 2}, |
| 193 | {(uint64_t) "callee3", 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 194 | Record11.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 195 | |
| 196 | InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 197 | Record11.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 198 | |
| 199 | InstrProfValueData VD4[] = {{(uint64_t) "callee1", 1}, |
| 200 | {(uint64_t) "callee2", 2}, |
| 201 | {(uint64_t) "callee3", 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 202 | Record11.addValueData(IPVK_IndirectCallTarget, 4, VD4, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 203 | |
| 204 | // A differnt record for the same caller. |
| 205 | Record12.reserveSites(IPVK_IndirectCallTarget, 5); |
| 206 | InstrProfValueData VD02[] = {{(uint64_t) "callee2", 5}, |
| 207 | {(uint64_t) "callee3", 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 208 | Record12.addValueData(IPVK_IndirectCallTarget, 0, VD02, 2, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 209 | |
| 210 | // No valeu profile data at the second site. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 211 | Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 212 | |
| 213 | InstrProfValueData VD22[] = {{(uint64_t) "callee2", 1}, |
| 214 | {(uint64_t) "callee3", 3}, |
| 215 | {(uint64_t) "callee4", 4}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 216 | Record12.addValueData(IPVK_IndirectCallTarget, 2, VD22, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 217 | |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 218 | Record12.addValueData(IPVK_IndirectCallTarget, 3, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 219 | |
| 220 | InstrProfValueData VD42[] = {{(uint64_t) "callee1", 1}, |
| 221 | {(uint64_t) "callee2", 2}, |
| 222 | {(uint64_t) "callee3", 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame^] | 223 | Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 224 | |
| 225 | Writer.addRecord(std::move(Record11)); |
| 226 | // Merge profile data. |
| 227 | Writer.addRecord(std::move(Record12)); |
| 228 | |
| 229 | Writer.addRecord(std::move(Record2)); |
| 230 | Writer.addRecord(std::move(Record3)); |
| 231 | Writer.addRecord(std::move(Record4)); |
| 232 | Writer.addRecord(std::move(Record5)); |
| 233 | Writer.addRecord(std::move(Record6)); |
| 234 | auto Profile = Writer.writeBuffer(); |
| 235 | readProfile(std::move(Profile)); |
| 236 | |
| 237 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 238 | ASSERT_TRUE(NoError(R.getError())); |
| 239 | ASSERT_EQ(5U, R.get().getNumValueSites(IPVK_IndirectCallTarget)); |
| 240 | ASSERT_EQ(4U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 241 | ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 242 | ASSERT_EQ(4U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 243 | ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
| 244 | ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 4)); |
| 245 | |
| 246 | std::unique_ptr<InstrProfValueData[]> VD = |
| 247 | R.get().getValueForSite(IPVK_IndirectCallTarget, 0); |
| 248 | // Now sort the target acording to frequency. |
| 249 | std::sort(&VD[0], &VD[4], |
| 250 | [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) { |
| 251 | return VD1.Count > VD2.Count; |
| 252 | }); |
| 253 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee2")); |
| 254 | ASSERT_EQ(7U, VD[0].Count); |
| 255 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee3")); |
| 256 | ASSERT_EQ(6U, VD[1].Count); |
| 257 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee4")); |
| 258 | ASSERT_EQ(4U, VD[2].Count); |
| 259 | ASSERT_EQ(StringRef((const char *)VD[3].Value, 7), StringRef("callee1")); |
| 260 | ASSERT_EQ(1U, VD[3].Count); |
| 261 | |
| 262 | std::unique_ptr<InstrProfValueData[]> VD_2( |
| 263 | R.get().getValueForSite(IPVK_IndirectCallTarget, 2)); |
| 264 | std::sort(&VD_2[0], &VD_2[4], |
| 265 | [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) { |
| 266 | return VD1.Count > VD2.Count; |
| 267 | }); |
| 268 | ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee3")); |
| 269 | ASSERT_EQ(6U, VD_2[0].Count); |
| 270 | ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee4")); |
| 271 | ASSERT_EQ(4U, VD_2[1].Count); |
| 272 | ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee2")); |
| 273 | ASSERT_EQ(3U, VD_2[2].Count); |
| 274 | ASSERT_EQ(StringRef((const char *)VD_2[3].Value, 7), StringRef("callee1")); |
| 275 | ASSERT_EQ(1U, VD_2[3].Count); |
| 276 | |
| 277 | std::unique_ptr<InstrProfValueData[]> VD_3( |
| 278 | R.get().getValueForSite(IPVK_IndirectCallTarget, 3)); |
| 279 | ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee1")); |
| 280 | ASSERT_EQ(1U, VD_3[0].Count); |
| 281 | |
| 282 | std::unique_ptr<InstrProfValueData[]> VD_4( |
| 283 | R.get().getValueForSite(IPVK_IndirectCallTarget, 4)); |
| 284 | std::sort(&VD_4[0], &VD_4[3], |
| 285 | [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) { |
| 286 | return VD1.Count > VD2.Count; |
| 287 | }); |
| 288 | ASSERT_EQ(StringRef((const char *)VD_4[0].Value, 7), StringRef("callee3")); |
| 289 | ASSERT_EQ(6U, VD_4[0].Count); |
| 290 | ASSERT_EQ(StringRef((const char *)VD_4[1].Value, 7), StringRef("callee2")); |
| 291 | ASSERT_EQ(4U, VD_4[1].Count); |
| 292 | ASSERT_EQ(StringRef((const char *)VD_4[2].Value, 7), StringRef("callee1")); |
| 293 | ASSERT_EQ(2U, VD_4[2].Count); |
| 294 | } |
| 295 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 296 | TEST_F(InstrProfTest, get_max_function_count) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 297 | InstrProfRecord Record1("foo", 0x1234, {1ULL << 31, 2}); |
| 298 | InstrProfRecord Record2("bar", 0, {1ULL << 63}); |
| 299 | InstrProfRecord Record3("baz", 0x5678, {0, 0, 0, 0}); |
| 300 | Writer.addRecord(std::move(Record1)); |
| 301 | Writer.addRecord(std::move(Record2)); |
| 302 | Writer.addRecord(std::move(Record3)); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 303 | auto Profile = Writer.writeBuffer(); |
| 304 | readProfile(std::move(Profile)); |
| 305 | |
| 306 | ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount()); |
| 307 | } |
| 308 | |
| 309 | } // end anonymous namespace |