Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 1 | //===- SampleProfReader.cpp - Read LLVM sample profile data ---------------===// |
| 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 | // This file implements the class that reads LLVM sample profiles. It |
Diego Novillo | bb5605c | 2015-10-14 18:36:30 +0000 | [diff] [blame] | 11 | // supports three file formats: text, binary and gcov. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 12 | // |
Diego Novillo | bb5605c | 2015-10-14 18:36:30 +0000 | [diff] [blame] | 13 | // The textual representation is useful for debugging and testing purposes. The |
| 14 | // binary representation is more compact, resulting in smaller file sizes. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 15 | // |
Diego Novillo | bb5605c | 2015-10-14 18:36:30 +0000 | [diff] [blame] | 16 | // The gcov encoding is the one generated by GCC's AutoFDO profile creation |
| 17 | // tool (https://github.com/google/autofdo) |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 18 | // |
Diego Novillo | bb5605c | 2015-10-14 18:36:30 +0000 | [diff] [blame] | 19 | // All three encodings can be used interchangeably as an input sample profile. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 20 | // |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | #include "llvm/ProfileData/SampleProfReader.h" |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseMap.h" |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/STLExtras.h" |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/ErrorOr.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 28 | #include "llvm/Support/LEB128.h" |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 29 | #include "llvm/Support/LineIterator.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MemoryBuffer.h" |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 31 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 32 | using namespace llvm::sampleprof; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 35 | /// \brief Dump the function profile for \p FName. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 36 | /// |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 37 | /// \param FName Name of the function to print. |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 38 | /// \param OS Stream to emit the output to. |
| 39 | void SampleProfileReader::dumpFunctionProfile(StringRef FName, |
| 40 | raw_ostream &OS) { |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 41 | OS << "Function: " << FName << ": " << Profiles[FName]; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 44 | /// \brief Dump all the function profiles found on stream \p OS. |
| 45 | void SampleProfileReader::dump(raw_ostream &OS) { |
| 46 | for (const auto &I : Profiles) |
| 47 | dumpFunctionProfile(I.getKey(), OS); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 50 | /// \brief Parse \p Input as function head. |
| 51 | /// |
| 52 | /// Parse one line of \p Input, and update function name in \p FName, |
| 53 | /// function's total sample count in \p NumSamples, function's entry |
| 54 | /// count in \p NumHeadSamples. |
| 55 | /// |
| 56 | /// \returns true if parsing is successful. |
| 57 | static bool ParseHead(const StringRef &Input, StringRef &FName, |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 58 | uint64_t &NumSamples, uint64_t &NumHeadSamples) { |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 59 | if (Input[0] == ' ') |
| 60 | return false; |
| 61 | size_t n2 = Input.rfind(':'); |
| 62 | size_t n1 = Input.rfind(':', n2 - 1); |
| 63 | FName = Input.substr(0, n1); |
| 64 | if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples)) |
| 65 | return false; |
| 66 | if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples)) |
| 67 | return false; |
| 68 | return true; |
| 69 | } |
| 70 | |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 71 | /// \brief Returns true if line offset \p L is legal (only has 16 bits). |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 72 | static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; } |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 73 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 74 | /// \brief Parse \p Input as line sample. |
| 75 | /// |
| 76 | /// \param Input input line. |
| 77 | /// \param IsCallsite true if the line represents an inlined callsite. |
| 78 | /// \param Depth the depth of the inline stack. |
| 79 | /// \param NumSamples total samples of the line/inlined callsite. |
| 80 | /// \param LineOffset line offset to the start of the function. |
| 81 | /// \param Discriminator discriminator of the line. |
| 82 | /// \param TargetCountMap map from indirect call target to count. |
| 83 | /// |
| 84 | /// returns true if parsing is successful. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 85 | static bool ParseLine(const StringRef &Input, bool &IsCallsite, uint32_t &Depth, |
| 86 | uint64_t &NumSamples, uint32_t &LineOffset, |
| 87 | uint32_t &Discriminator, StringRef &CalleeName, |
| 88 | DenseMap<StringRef, uint64_t> &TargetCountMap) { |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 89 | for (Depth = 0; Input[Depth] == ' '; Depth++) |
| 90 | ; |
| 91 | if (Depth == 0) |
| 92 | return false; |
| 93 | |
| 94 | size_t n1 = Input.find(':'); |
| 95 | StringRef Loc = Input.substr(Depth, n1 - Depth); |
| 96 | size_t n2 = Loc.find('.'); |
| 97 | if (n2 == StringRef::npos) { |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 98 | if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset)) |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 99 | return false; |
| 100 | Discriminator = 0; |
| 101 | } else { |
| 102 | if (Loc.substr(0, n2).getAsInteger(10, LineOffset)) |
| 103 | return false; |
| 104 | if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator)) |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | StringRef Rest = Input.substr(n1 + 2); |
| 109 | if (Rest[0] >= '0' && Rest[0] <= '9') { |
| 110 | IsCallsite = false; |
| 111 | size_t n3 = Rest.find(' '); |
| 112 | if (n3 == StringRef::npos) { |
| 113 | if (Rest.getAsInteger(10, NumSamples)) |
| 114 | return false; |
| 115 | } else { |
| 116 | if (Rest.substr(0, n3).getAsInteger(10, NumSamples)) |
| 117 | return false; |
| 118 | } |
| 119 | while (n3 != StringRef::npos) { |
| 120 | n3 += Rest.substr(n3).find_first_not_of(' '); |
| 121 | Rest = Rest.substr(n3); |
| 122 | n3 = Rest.find(' '); |
| 123 | StringRef pair = Rest; |
| 124 | if (n3 != StringRef::npos) { |
| 125 | pair = Rest.substr(0, n3); |
| 126 | } |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 127 | size_t n4 = pair.find(':'); |
| 128 | uint64_t count; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 129 | if (pair.substr(n4 + 1).getAsInteger(10, count)) |
| 130 | return false; |
| 131 | TargetCountMap[pair.substr(0, n4)] = count; |
| 132 | } |
| 133 | } else { |
| 134 | IsCallsite = true; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 135 | size_t n3 = Rest.find_last_of(':'); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 136 | CalleeName = Rest.substr(0, n3); |
| 137 | if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples)) |
| 138 | return false; |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 143 | /// \brief Load samples from a text file. |
| 144 | /// |
| 145 | /// See the documentation at the top of the file for an explanation of |
| 146 | /// the expected format. |
| 147 | /// |
| 148 | /// \returns true if the file was loaded successfully, false otherwise. |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 149 | std::error_code SampleProfileReaderText::read() { |
| 150 | line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#'); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 151 | sampleprof_error Result = sampleprof_error::success; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 152 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 153 | InlineCallStack InlineStack; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 154 | |
| 155 | for (; !LineIt.is_at_eof(); ++LineIt) { |
| 156 | if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#') |
| 157 | continue; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 158 | // Read the header of each function. |
| 159 | // |
| 160 | // Note that for function identifiers we are actually expecting |
| 161 | // mangled names, but we may not always get them. This happens when |
| 162 | // the compiler decides not to emit the function (e.g., it was inlined |
| 163 | // and removed). In this case, the binary will not have the linkage |
| 164 | // name for the function, so the profiler will emit the function's |
| 165 | // unmangled name, which may contain characters like ':' and '>' in its |
| 166 | // name (member functions, templates, etc). |
| 167 | // |
| 168 | // The only requirement we place on the identifier, then, is that it |
| 169 | // should not begin with a number. |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 170 | if ((*LineIt)[0] != ' ') { |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 171 | uint64_t NumSamples, NumHeadSamples; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 172 | StringRef FName; |
| 173 | if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) { |
| 174 | reportError(LineIt.line_number(), |
| 175 | "Expected 'mangled_name:NUM:NUM', found " + *LineIt); |
| 176 | return sampleprof_error::malformed; |
| 177 | } |
| 178 | Profiles[FName] = FunctionSamples(); |
| 179 | FunctionSamples &FProfile = Profiles[FName]; |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 180 | FProfile.setName(FName); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 181 | MergeResult(Result, FProfile.addTotalSamples(NumSamples)); |
| 182 | MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples)); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 183 | InlineStack.clear(); |
| 184 | InlineStack.push_back(&FProfile); |
| 185 | } else { |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 186 | uint64_t NumSamples; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 187 | StringRef FName; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 188 | DenseMap<StringRef, uint64_t> TargetCountMap; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 189 | bool IsCallsite; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 190 | uint32_t Depth, LineOffset, Discriminator; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 191 | if (!ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset, |
| 192 | Discriminator, FName, TargetCountMap)) { |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 193 | reportError(LineIt.line_number(), |
| 194 | "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " + |
| 195 | *LineIt); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 196 | return sampleprof_error::malformed; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 197 | } |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 198 | if (IsCallsite) { |
| 199 | while (InlineStack.size() > Depth) { |
| 200 | InlineStack.pop_back(); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 201 | } |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 202 | FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt( |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 203 | LineLocation(LineOffset, Discriminator)); |
| 204 | FSamples.setName(FName); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 205 | MergeResult(Result, FSamples.addTotalSamples(NumSamples)); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 206 | InlineStack.push_back(&FSamples); |
| 207 | } else { |
| 208 | while (InlineStack.size() > Depth) { |
| 209 | InlineStack.pop_back(); |
| 210 | } |
| 211 | FunctionSamples &FProfile = *InlineStack.back(); |
| 212 | for (const auto &name_count : TargetCountMap) { |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 213 | MergeResult(Result, FProfile.addCalledTargetSamples( |
| 214 | LineOffset, Discriminator, name_count.first, |
| 215 | name_count.second)); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 216 | } |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 217 | MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator, |
| 218 | NumSamples)); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 219 | } |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 222 | if (Result == sampleprof_error::success) |
| 223 | computeSummary(); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 224 | |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 225 | return Result; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 228 | bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) { |
| 229 | bool result = false; |
| 230 | |
| 231 | // Check that the first non-comment line is a valid function header. |
| 232 | line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#'); |
| 233 | if (!LineIt.is_at_eof()) { |
| 234 | if ((*LineIt)[0] != ' ') { |
| 235 | uint64_t NumSamples, NumHeadSamples; |
| 236 | StringRef FName; |
| 237 | result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return result; |
| 242 | } |
| 243 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 244 | template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() { |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 245 | unsigned NumBytesRead = 0; |
| 246 | std::error_code EC; |
| 247 | uint64_t Val = decodeULEB128(Data, &NumBytesRead); |
| 248 | |
| 249 | if (Val > std::numeric_limits<T>::max()) |
| 250 | EC = sampleprof_error::malformed; |
| 251 | else if (Data + NumBytesRead > End) |
| 252 | EC = sampleprof_error::truncated; |
| 253 | else |
| 254 | EC = sampleprof_error::success; |
| 255 | |
| 256 | if (EC) { |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 257 | reportError(0, EC.message()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 258 | return EC; |
| 259 | } |
| 260 | |
| 261 | Data += NumBytesRead; |
| 262 | return static_cast<T>(Val); |
| 263 | } |
| 264 | |
| 265 | ErrorOr<StringRef> SampleProfileReaderBinary::readString() { |
| 266 | std::error_code EC; |
| 267 | StringRef Str(reinterpret_cast<const char *>(Data)); |
| 268 | if (Data + Str.size() + 1 > End) { |
| 269 | EC = sampleprof_error::truncated; |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 270 | reportError(0, EC.message()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 271 | return EC; |
| 272 | } |
| 273 | |
| 274 | Data += Str.size() + 1; |
| 275 | return Str; |
| 276 | } |
| 277 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 278 | ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() { |
| 279 | std::error_code EC; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 280 | auto Idx = readNumber<uint32_t>(); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 281 | if (std::error_code EC = Idx.getError()) |
| 282 | return EC; |
| 283 | if (*Idx >= NameTable.size()) |
| 284 | return sampleprof_error::truncated_name_table; |
| 285 | return NameTable[*Idx]; |
| 286 | } |
| 287 | |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 288 | std::error_code |
| 289 | SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) { |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 290 | auto NumSamples = readNumber<uint64_t>(); |
| 291 | if (std::error_code EC = NumSamples.getError()) |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 292 | return EC; |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 293 | FProfile.addTotalSamples(*NumSamples); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 294 | |
| 295 | // Read the samples in the body. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 296 | auto NumRecords = readNumber<uint32_t>(); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 297 | if (std::error_code EC = NumRecords.getError()) |
| 298 | return EC; |
| 299 | |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 300 | for (uint32_t I = 0; I < *NumRecords; ++I) { |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 301 | auto LineOffset = readNumber<uint64_t>(); |
| 302 | if (std::error_code EC = LineOffset.getError()) |
| 303 | return EC; |
| 304 | |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 305 | if (!isOffsetLegal(*LineOffset)) { |
| 306 | return std::error_code(); |
| 307 | } |
| 308 | |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 309 | auto Discriminator = readNumber<uint64_t>(); |
| 310 | if (std::error_code EC = Discriminator.getError()) |
| 311 | return EC; |
| 312 | |
| 313 | auto NumSamples = readNumber<uint64_t>(); |
| 314 | if (std::error_code EC = NumSamples.getError()) |
| 315 | return EC; |
| 316 | |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 317 | auto NumCalls = readNumber<uint32_t>(); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 318 | if (std::error_code EC = NumCalls.getError()) |
| 319 | return EC; |
| 320 | |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 321 | for (uint32_t J = 0; J < *NumCalls; ++J) { |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 322 | auto CalledFunction(readStringFromTable()); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 323 | if (std::error_code EC = CalledFunction.getError()) |
| 324 | return EC; |
| 325 | |
| 326 | auto CalledFunctionSamples = readNumber<uint64_t>(); |
| 327 | if (std::error_code EC = CalledFunctionSamples.getError()) |
| 328 | return EC; |
| 329 | |
| 330 | FProfile.addCalledTargetSamples(*LineOffset, *Discriminator, |
| 331 | *CalledFunction, *CalledFunctionSamples); |
| 332 | } |
| 333 | |
| 334 | FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples); |
| 335 | } |
| 336 | |
| 337 | // Read all the samples for inlined function calls. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 338 | auto NumCallsites = readNumber<uint32_t>(); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 339 | if (std::error_code EC = NumCallsites.getError()) |
| 340 | return EC; |
| 341 | |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 342 | for (uint32_t J = 0; J < *NumCallsites; ++J) { |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 343 | auto LineOffset = readNumber<uint64_t>(); |
| 344 | if (std::error_code EC = LineOffset.getError()) |
| 345 | return EC; |
| 346 | |
| 347 | auto Discriminator = readNumber<uint64_t>(); |
| 348 | if (std::error_code EC = Discriminator.getError()) |
| 349 | return EC; |
| 350 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 351 | auto FName(readStringFromTable()); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 352 | if (std::error_code EC = FName.getError()) |
| 353 | return EC; |
| 354 | |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 355 | FunctionSamples &CalleeProfile = |
| 356 | FProfile.functionSamplesAt(LineLocation(*LineOffset, *Discriminator)); |
| 357 | CalleeProfile.setName(*FName); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 358 | if (std::error_code EC = readProfile(CalleeProfile)) |
| 359 | return EC; |
| 360 | } |
| 361 | |
| 362 | return sampleprof_error::success; |
| 363 | } |
| 364 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 365 | std::error_code SampleProfileReaderBinary::read() { |
| 366 | while (!at_eof()) { |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 367 | auto NumHeadSamples = readNumber<uint64_t>(); |
| 368 | if (std::error_code EC = NumHeadSamples.getError()) |
| 369 | return EC; |
| 370 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 371 | auto FName(readStringFromTable()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 372 | if (std::error_code EC = FName.getError()) |
| 373 | return EC; |
| 374 | |
| 375 | Profiles[*FName] = FunctionSamples(); |
| 376 | FunctionSamples &FProfile = Profiles[*FName]; |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 377 | FProfile.setName(*FName); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 378 | |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 379 | FProfile.addHeadSamples(*NumHeadSamples); |
| 380 | |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 381 | if (std::error_code EC = readProfile(FProfile)) |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 382 | return EC; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return sampleprof_error::success; |
| 386 | } |
| 387 | |
| 388 | std::error_code SampleProfileReaderBinary::readHeader() { |
| 389 | Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); |
| 390 | End = Data + Buffer->getBufferSize(); |
| 391 | |
| 392 | // Read and check the magic identifier. |
| 393 | auto Magic = readNumber<uint64_t>(); |
| 394 | if (std::error_code EC = Magic.getError()) |
| 395 | return EC; |
| 396 | else if (*Magic != SPMagic()) |
| 397 | return sampleprof_error::bad_magic; |
| 398 | |
| 399 | // Read the version number. |
| 400 | auto Version = readNumber<uint64_t>(); |
| 401 | if (std::error_code EC = Version.getError()) |
| 402 | return EC; |
| 403 | else if (*Version != SPVersion()) |
| 404 | return sampleprof_error::unsupported_version; |
| 405 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 406 | if (std::error_code EC = readSummary()) |
| 407 | return EC; |
| 408 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 409 | // Read the name table. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 410 | auto Size = readNumber<uint32_t>(); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 411 | if (std::error_code EC = Size.getError()) |
| 412 | return EC; |
| 413 | NameTable.reserve(*Size); |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 414 | for (uint32_t I = 0; I < *Size; ++I) { |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 415 | auto Name(readString()); |
| 416 | if (std::error_code EC = Name.getError()) |
| 417 | return EC; |
| 418 | NameTable.push_back(*Name); |
| 419 | } |
| 420 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 421 | return sampleprof_error::success; |
| 422 | } |
| 423 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 424 | std::error_code SampleProfileReaderBinary::readSummaryEntry( |
| 425 | std::vector<ProfileSummaryEntry> &Entries) { |
| 426 | auto Cutoff = readNumber<uint64_t>(); |
| 427 | if (std::error_code EC = Cutoff.getError()) |
| 428 | return EC; |
| 429 | |
| 430 | auto MinBlockCount = readNumber<uint64_t>(); |
| 431 | if (std::error_code EC = MinBlockCount.getError()) |
| 432 | return EC; |
| 433 | |
| 434 | auto NumBlocks = readNumber<uint64_t>(); |
| 435 | if (std::error_code EC = NumBlocks.getError()) |
| 436 | return EC; |
| 437 | |
| 438 | Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks); |
| 439 | return sampleprof_error::success; |
| 440 | } |
| 441 | |
| 442 | std::error_code SampleProfileReaderBinary::readSummary() { |
| 443 | auto TotalCount = readNumber<uint64_t>(); |
| 444 | if (std::error_code EC = TotalCount.getError()) |
| 445 | return EC; |
| 446 | |
| 447 | auto MaxBlockCount = readNumber<uint64_t>(); |
| 448 | if (std::error_code EC = MaxBlockCount.getError()) |
| 449 | return EC; |
| 450 | |
| 451 | auto MaxFunctionCount = readNumber<uint64_t>(); |
| 452 | if (std::error_code EC = MaxFunctionCount.getError()) |
| 453 | return EC; |
| 454 | |
| 455 | auto NumBlocks = readNumber<uint64_t>(); |
| 456 | if (std::error_code EC = NumBlocks.getError()) |
| 457 | return EC; |
| 458 | |
| 459 | auto NumFunctions = readNumber<uint64_t>(); |
| 460 | if (std::error_code EC = NumFunctions.getError()) |
| 461 | return EC; |
| 462 | |
| 463 | auto NumSummaryEntries = readNumber<uint64_t>(); |
| 464 | if (std::error_code EC = NumSummaryEntries.getError()) |
| 465 | return EC; |
| 466 | |
| 467 | std::vector<ProfileSummaryEntry> Entries; |
| 468 | for (unsigned i = 0; i < *NumSummaryEntries; i++) { |
| 469 | std::error_code EC = readSummaryEntry(Entries); |
| 470 | if (EC != sampleprof_error::success) |
| 471 | return EC; |
| 472 | } |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 473 | Summary = llvm::make_unique<ProfileSummary>( |
| 474 | ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0, |
| 475 | *MaxFunctionCount, *NumBlocks, *NumFunctions); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 476 | |
| 477 | return sampleprof_error::success; |
| 478 | } |
| 479 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 480 | bool SampleProfileReaderBinary::hasFormat(const MemoryBuffer &Buffer) { |
| 481 | const uint8_t *Data = |
| 482 | reinterpret_cast<const uint8_t *>(Buffer.getBufferStart()); |
| 483 | uint64_t Magic = decodeULEB128(Data); |
| 484 | return Magic == SPMagic(); |
| 485 | } |
| 486 | |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 487 | std::error_code SampleProfileReaderGCC::skipNextWord() { |
| 488 | uint32_t dummy; |
| 489 | if (!GcovBuffer.readInt(dummy)) |
| 490 | return sampleprof_error::truncated; |
| 491 | return sampleprof_error::success; |
| 492 | } |
| 493 | |
| 494 | template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() { |
| 495 | if (sizeof(T) <= sizeof(uint32_t)) { |
| 496 | uint32_t Val; |
| 497 | if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max()) |
| 498 | return static_cast<T>(Val); |
| 499 | } else if (sizeof(T) <= sizeof(uint64_t)) { |
| 500 | uint64_t Val; |
| 501 | if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max()) |
| 502 | return static_cast<T>(Val); |
| 503 | } |
| 504 | |
| 505 | std::error_code EC = sampleprof_error::malformed; |
| 506 | reportError(0, EC.message()); |
| 507 | return EC; |
| 508 | } |
| 509 | |
| 510 | ErrorOr<StringRef> SampleProfileReaderGCC::readString() { |
| 511 | StringRef Str; |
| 512 | if (!GcovBuffer.readString(Str)) |
| 513 | return sampleprof_error::truncated; |
| 514 | return Str; |
| 515 | } |
| 516 | |
| 517 | std::error_code SampleProfileReaderGCC::readHeader() { |
| 518 | // Read the magic identifier. |
| 519 | if (!GcovBuffer.readGCDAFormat()) |
| 520 | return sampleprof_error::unrecognized_format; |
| 521 | |
| 522 | // Read the version number. Note - the GCC reader does not validate this |
| 523 | // version, but the profile creator generates v704. |
| 524 | GCOV::GCOVVersion version; |
| 525 | if (!GcovBuffer.readGCOVVersion(version)) |
| 526 | return sampleprof_error::unrecognized_format; |
| 527 | |
| 528 | if (version != GCOV::V704) |
| 529 | return sampleprof_error::unsupported_version; |
| 530 | |
| 531 | // Skip the empty integer. |
| 532 | if (std::error_code EC = skipNextWord()) |
| 533 | return EC; |
| 534 | |
| 535 | return sampleprof_error::success; |
| 536 | } |
| 537 | |
| 538 | std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) { |
| 539 | uint32_t Tag; |
| 540 | if (!GcovBuffer.readInt(Tag)) |
| 541 | return sampleprof_error::truncated; |
| 542 | |
| 543 | if (Tag != Expected) |
| 544 | return sampleprof_error::malformed; |
| 545 | |
| 546 | if (std::error_code EC = skipNextWord()) |
| 547 | return EC; |
| 548 | |
| 549 | return sampleprof_error::success; |
| 550 | } |
| 551 | |
| 552 | std::error_code SampleProfileReaderGCC::readNameTable() { |
| 553 | if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames)) |
| 554 | return EC; |
| 555 | |
| 556 | uint32_t Size; |
| 557 | if (!GcovBuffer.readInt(Size)) |
| 558 | return sampleprof_error::truncated; |
| 559 | |
| 560 | for (uint32_t I = 0; I < Size; ++I) { |
| 561 | StringRef Str; |
| 562 | if (!GcovBuffer.readString(Str)) |
| 563 | return sampleprof_error::truncated; |
| 564 | Names.push_back(Str); |
| 565 | } |
| 566 | |
| 567 | return sampleprof_error::success; |
| 568 | } |
| 569 | |
| 570 | std::error_code SampleProfileReaderGCC::readFunctionProfiles() { |
| 571 | if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction)) |
| 572 | return EC; |
| 573 | |
| 574 | uint32_t NumFunctions; |
| 575 | if (!GcovBuffer.readInt(NumFunctions)) |
| 576 | return sampleprof_error::truncated; |
| 577 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 578 | InlineCallStack Stack; |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 579 | for (uint32_t I = 0; I < NumFunctions; ++I) |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 580 | if (std::error_code EC = readOneFunctionProfile(Stack, true, 0)) |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 581 | return EC; |
| 582 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 583 | computeSummary(); |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 584 | return sampleprof_error::success; |
| 585 | } |
| 586 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 587 | std::error_code SampleProfileReaderGCC::readOneFunctionProfile( |
| 588 | const InlineCallStack &InlineStack, bool Update, uint32_t Offset) { |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 589 | uint64_t HeadCount = 0; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 590 | if (InlineStack.size() == 0) |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 591 | if (!GcovBuffer.readInt64(HeadCount)) |
| 592 | return sampleprof_error::truncated; |
| 593 | |
| 594 | uint32_t NameIdx; |
| 595 | if (!GcovBuffer.readInt(NameIdx)) |
| 596 | return sampleprof_error::truncated; |
| 597 | |
| 598 | StringRef Name(Names[NameIdx]); |
| 599 | |
| 600 | uint32_t NumPosCounts; |
| 601 | if (!GcovBuffer.readInt(NumPosCounts)) |
| 602 | return sampleprof_error::truncated; |
| 603 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 604 | uint32_t NumCallsites; |
| 605 | if (!GcovBuffer.readInt(NumCallsites)) |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 606 | return sampleprof_error::truncated; |
| 607 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 608 | FunctionSamples *FProfile = nullptr; |
| 609 | if (InlineStack.size() == 0) { |
| 610 | // If this is a top function that we have already processed, do not |
| 611 | // update its profile again. This happens in the presence of |
| 612 | // function aliases. Since these aliases share the same function |
| 613 | // body, there will be identical replicated profiles for the |
| 614 | // original function. In this case, we simply not bother updating |
| 615 | // the profile of the original function. |
| 616 | FProfile = &Profiles[Name]; |
| 617 | FProfile->addHeadSamples(HeadCount); |
| 618 | if (FProfile->getTotalSamples() > 0) |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 619 | Update = false; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 620 | } else { |
| 621 | // Otherwise, we are reading an inlined instance. The top of the |
| 622 | // inline stack contains the profile of the caller. Insert this |
| 623 | // callee in the caller's CallsiteMap. |
| 624 | FunctionSamples *CallerProfile = InlineStack.front(); |
| 625 | uint32_t LineOffset = Offset >> 16; |
| 626 | uint32_t Discriminator = Offset & 0xffff; |
| 627 | FProfile = &CallerProfile->functionSamplesAt( |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 628 | LineLocation(LineOffset, Discriminator)); |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 629 | } |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 630 | FProfile->setName(Name); |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 631 | |
| 632 | for (uint32_t I = 0; I < NumPosCounts; ++I) { |
| 633 | uint32_t Offset; |
| 634 | if (!GcovBuffer.readInt(Offset)) |
| 635 | return sampleprof_error::truncated; |
| 636 | |
| 637 | uint32_t NumTargets; |
| 638 | if (!GcovBuffer.readInt(NumTargets)) |
| 639 | return sampleprof_error::truncated; |
| 640 | |
| 641 | uint64_t Count; |
| 642 | if (!GcovBuffer.readInt64(Count)) |
| 643 | return sampleprof_error::truncated; |
| 644 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 645 | // The line location is encoded in the offset as: |
| 646 | // high 16 bits: line offset to the start of the function. |
| 647 | // low 16 bits: discriminator. |
| 648 | uint32_t LineOffset = Offset >> 16; |
| 649 | uint32_t Discriminator = Offset & 0xffff; |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 650 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 651 | InlineCallStack NewStack; |
| 652 | NewStack.push_back(FProfile); |
| 653 | NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end()); |
| 654 | if (Update) { |
| 655 | // Walk up the inline stack, adding the samples on this line to |
| 656 | // the total sample count of the callers in the chain. |
| 657 | for (auto CallerProfile : NewStack) |
| 658 | CallerProfile->addTotalSamples(Count); |
| 659 | |
| 660 | // Update the body samples for the current profile. |
| 661 | FProfile->addBodySamples(LineOffset, Discriminator, Count); |
| 662 | } |
| 663 | |
| 664 | // Process the list of functions called at an indirect call site. |
| 665 | // These are all the targets that a function pointer (or virtual |
| 666 | // function) resolved at runtime. |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 667 | for (uint32_t J = 0; J < NumTargets; J++) { |
| 668 | uint32_t HistVal; |
| 669 | if (!GcovBuffer.readInt(HistVal)) |
| 670 | return sampleprof_error::truncated; |
| 671 | |
| 672 | if (HistVal != HIST_TYPE_INDIR_CALL_TOPN) |
| 673 | return sampleprof_error::malformed; |
| 674 | |
| 675 | uint64_t TargetIdx; |
| 676 | if (!GcovBuffer.readInt64(TargetIdx)) |
| 677 | return sampleprof_error::truncated; |
| 678 | StringRef TargetName(Names[TargetIdx]); |
| 679 | |
| 680 | uint64_t TargetCount; |
| 681 | if (!GcovBuffer.readInt64(TargetCount)) |
| 682 | return sampleprof_error::truncated; |
| 683 | |
| 684 | if (Update) { |
| 685 | FunctionSamples &TargetProfile = Profiles[TargetName]; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 686 | TargetProfile.addCalledTargetSamples(LineOffset, Discriminator, |
| 687 | TargetName, TargetCount); |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 692 | // Process all the inlined callers into the current function. These |
| 693 | // are all the callsites that were inlined into this function. |
| 694 | for (uint32_t I = 0; I < NumCallsites; I++) { |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 695 | // The offset is encoded as: |
| 696 | // high 16 bits: line offset to the start of the function. |
| 697 | // low 16 bits: discriminator. |
| 698 | uint32_t Offset; |
| 699 | if (!GcovBuffer.readInt(Offset)) |
| 700 | return sampleprof_error::truncated; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 701 | InlineCallStack NewStack; |
| 702 | NewStack.push_back(FProfile); |
| 703 | NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end()); |
| 704 | if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset)) |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 705 | return EC; |
| 706 | } |
| 707 | |
| 708 | return sampleprof_error::success; |
| 709 | } |
| 710 | |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 711 | /// \brief Read a GCC AutoFDO profile. |
| 712 | /// |
| 713 | /// This format is generated by the Linux Perf conversion tool at |
| 714 | /// https://github.com/google/autofdo. |
| 715 | std::error_code SampleProfileReaderGCC::read() { |
| 716 | // Read the string table. |
| 717 | if (std::error_code EC = readNameTable()) |
| 718 | return EC; |
| 719 | |
| 720 | // Read the source profile. |
| 721 | if (std::error_code EC = readFunctionProfiles()) |
| 722 | return EC; |
| 723 | |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 724 | return sampleprof_error::success; |
| 725 | } |
| 726 | |
| 727 | bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) { |
| 728 | StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart())); |
| 729 | return Magic == "adcg*704"; |
| 730 | } |
| 731 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 732 | /// \brief Prepare a memory buffer for the contents of \p Filename. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 733 | /// |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 734 | /// \returns an error code indicating the status of the buffer. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 735 | static ErrorOr<std::unique_ptr<MemoryBuffer>> |
Benjamin Kramer | 0da23a2 | 2016-05-29 10:31:00 +0000 | [diff] [blame] | 736 | setupMemoryBuffer(const Twine &Filename) { |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 737 | auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename); |
| 738 | if (std::error_code EC = BufferOrErr.getError()) |
| 739 | return EC; |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 740 | auto Buffer = std::move(BufferOrErr.get()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 741 | |
| 742 | // Sanity check the file. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 743 | if (Buffer->getBufferSize() > std::numeric_limits<uint32_t>::max()) |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 744 | return sampleprof_error::too_large; |
| 745 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 746 | return std::move(Buffer); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /// \brief Create a sample profile reader based on the format of the input file. |
| 750 | /// |
| 751 | /// \param Filename The file to open. |
| 752 | /// |
| 753 | /// \param Reader The reader to instantiate according to \p Filename's format. |
| 754 | /// |
| 755 | /// \param C The LLVM context to use to emit diagnostics. |
| 756 | /// |
| 757 | /// \returns an error code indicating the status of the created reader. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 758 | ErrorOr<std::unique_ptr<SampleProfileReader>> |
Benjamin Kramer | 0da23a2 | 2016-05-29 10:31:00 +0000 | [diff] [blame] | 759 | SampleProfileReader::create(const Twine &Filename, LLVMContext &C) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 760 | auto BufferOrError = setupMemoryBuffer(Filename); |
| 761 | if (std::error_code EC = BufferOrError.getError()) |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 762 | return EC; |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 763 | return create(BufferOrError.get(), C); |
| 764 | } |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 765 | |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 766 | /// \brief Create a sample profile reader based on the format of the input data. |
| 767 | /// |
| 768 | /// \param B The memory buffer to create the reader from (assumes ownership). |
| 769 | /// |
| 770 | /// \param Reader The reader to instantiate according to \p Filename's format. |
| 771 | /// |
| 772 | /// \param C The LLVM context to use to emit diagnostics. |
| 773 | /// |
| 774 | /// \returns an error code indicating the status of the created reader. |
| 775 | ErrorOr<std::unique_ptr<SampleProfileReader>> |
| 776 | SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 777 | std::unique_ptr<SampleProfileReader> Reader; |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 778 | if (SampleProfileReaderBinary::hasFormat(*B)) |
| 779 | Reader.reset(new SampleProfileReaderBinary(std::move(B), C)); |
| 780 | else if (SampleProfileReaderGCC::hasFormat(*B)) |
| 781 | Reader.reset(new SampleProfileReaderGCC(std::move(B), C)); |
| 782 | else if (SampleProfileReaderText::hasFormat(*B)) |
| 783 | Reader.reset(new SampleProfileReaderText(std::move(B), C)); |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 784 | else |
| 785 | return sampleprof_error::unrecognized_format; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 786 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 787 | if (std::error_code EC = Reader->readHeader()) |
| 788 | return EC; |
| 789 | |
| 790 | return std::move(Reader); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 791 | } |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 792 | |
| 793 | // For text and GCC file formats, we compute the summary after reading the |
| 794 | // profile. Binary format has the profile summary in its header. |
| 795 | void SampleProfileReader::computeSummary() { |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 796 | SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 797 | for (const auto &I : Profiles) { |
| 798 | const FunctionSamples &Profile = I.second; |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 799 | Builder.addRecord(Profile); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 800 | } |
Benjamin Kramer | 38de59e | 2016-05-20 09:18:37 +0000 | [diff] [blame] | 801 | Summary = Builder.getSummary(); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 802 | } |