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 | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 11 | // supports two file formats: text and binary. The textual representation |
| 12 | // is useful for debugging and testing purposes. The binary representation |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 13 | // is more compact, resulting in smaller file sizes. However, they can |
| 14 | // both be used interchangeably. |
| 15 | // |
| 16 | // NOTE: If you are making changes to the file format, please remember |
| 17 | // to document them in the Clang documentation at |
| 18 | // tools/clang/docs/UsersManual.rst. |
| 19 | // |
| 20 | // Text format |
| 21 | // ----------- |
| 22 | // |
| 23 | // Sample profiles are written as ASCII text. The file is divided into |
| 24 | // sections, which correspond to each of the functions executed at runtime. |
| 25 | // Each section has the following format |
| 26 | // |
| 27 | // function1:total_samples:total_head_samples |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame^] | 28 | // offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ] |
| 29 | // offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ] |
| 30 | // ... |
| 31 | // offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ] |
| 32 | // offsetA[.discriminator]: fnA:num_of_total_samples |
| 33 | // offsetA1[.discriminator]: number_of_samples [fn7:num fn8:num ... ] |
| 34 | // ... |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 35 | // |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame^] | 36 | // This is a nested tree in which the identation represent the nest level |
| 37 | // of the inline stack. There is no blank line in the file. And the spacing |
| 38 | // within a single line is fixed. Additional spaces will result in an error |
| 39 | // while reading the file. |
| 40 | // |
| 41 | // Inline stack is a stack of source locations in which the top of the stack |
| 42 | // represents the leaf function, and the bottom of the stack represents the |
| 43 | // actual symbol in which the instruction belongs. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 44 | // |
| 45 | // Function names must be mangled in order for the profile loader to |
| 46 | // match them in the current translation unit. The two numbers in the |
| 47 | // function header specify how many total samples were accumulated in the |
| 48 | // function (first number), and the total number of samples accumulated |
| 49 | // in the prologue of the function (second number). This head sample |
| 50 | // count provides an indicator of how frequently the function is invoked. |
| 51 | // |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame^] | 52 | // There are two types of lines in the function body. |
| 53 | // |
| 54 | // * Sampled line represents the profile information of a source location. |
| 55 | // * Callsite line represents the profile inofrmation of a callsite. |
| 56 | // |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 57 | // Each sampled line may contain several items. Some are optional (marked |
| 58 | // below): |
| 59 | // |
| 60 | // a. Source line offset. This number represents the line number |
| 61 | // in the function where the sample was collected. The line number is |
| 62 | // always relative to the line where symbol of the function is |
| 63 | // defined. So, if the function has its header at line 280, the offset |
| 64 | // 13 is at line 293 in the file. |
| 65 | // |
| 66 | // Note that this offset should never be a negative number. This could |
| 67 | // happen in cases like macros. The debug machinery will register the |
| 68 | // line number at the point of macro expansion. So, if the macro was |
| 69 | // expanded in a line before the start of the function, the profile |
| 70 | // converter should emit a 0 as the offset (this means that the optimizers |
| 71 | // will not be able to associate a meaningful weight to the instructions |
| 72 | // in the macro). |
| 73 | // |
| 74 | // b. [OPTIONAL] Discriminator. This is used if the sampled program |
| 75 | // was compiled with DWARF discriminator support |
| 76 | // (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators). |
| 77 | // DWARF discriminators are unsigned integer values that allow the |
| 78 | // compiler to distinguish between multiple execution paths on the |
| 79 | // same source line location. |
| 80 | // |
| 81 | // For example, consider the line of code ``if (cond) foo(); else bar();``. |
| 82 | // If the predicate ``cond`` is true 80% of the time, then the edge |
| 83 | // into function ``foo`` should be considered to be taken most of the |
| 84 | // time. But both calls to ``foo`` and ``bar`` are at the same source |
| 85 | // line, so a sample count at that line is not sufficient. The |
| 86 | // compiler needs to know which part of that line is taken more |
| 87 | // frequently. |
| 88 | // |
| 89 | // This is what discriminators provide. In this case, the calls to |
| 90 | // ``foo`` and ``bar`` will be at the same line, but will have |
| 91 | // different discriminator values. This allows the compiler to correctly |
| 92 | // set edge weights into ``foo`` and ``bar``. |
| 93 | // |
| 94 | // c. Number of samples. This is an integer quantity representing the |
| 95 | // number of samples collected by the profiler at this source |
| 96 | // location. |
| 97 | // |
| 98 | // d. [OPTIONAL] Potential call targets and samples. If present, this |
| 99 | // line contains a call instruction. This models both direct and |
| 100 | // number of samples. For example, |
| 101 | // |
| 102 | // 130: 7 foo:3 bar:2 baz:7 |
| 103 | // |
| 104 | // The above means that at relative line offset 130 there is a call |
| 105 | // instruction that calls one of ``foo()``, ``bar()`` and ``baz()``, |
| 106 | // with ``baz()`` being the relatively more frequently called target. |
| 107 | // |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame^] | 108 | // Each callsite line may contain several items. Some are optional. |
| 109 | // |
| 110 | // a. Source line offset. This number represents the line number of the |
| 111 | // callsite that is inlined in the profiled binary. |
| 112 | // |
| 113 | // b. [OPTIONAL] Discriminator. Same as the discriminator for sampled line. |
| 114 | // |
| 115 | // c. Number of samples. This is an integer quantity representing the |
| 116 | // total number of samples collected for the inlined instance at this |
| 117 | // callsite |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 118 | //===----------------------------------------------------------------------===// |
| 119 | |
| 120 | #include "llvm/ProfileData/SampleProfReader.h" |
| 121 | #include "llvm/Support/Debug.h" |
| 122 | #include "llvm/Support/ErrorOr.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 123 | #include "llvm/Support/LEB128.h" |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 124 | #include "llvm/Support/LineIterator.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 125 | #include "llvm/Support/MemoryBuffer.h" |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame^] | 126 | #include "llvm/ADT/DenseMap.h" |
| 127 | #include "llvm/ADT/SmallVector.h" |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 128 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 129 | using namespace llvm::sampleprof; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 130 | using namespace llvm; |
| 131 | |
| 132 | /// \brief Print the samples collected for a function on stream \p OS. |
| 133 | /// |
| 134 | /// \param OS Stream to emit the output to. |
| 135 | void FunctionSamples::print(raw_ostream &OS) { |
| 136 | OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size() |
| 137 | << " sampled lines\n"; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 138 | for (const auto &SI : BodySamples) { |
| 139 | LineLocation Loc = SI.first; |
| 140 | const SampleRecord &Sample = SI.second; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 141 | OS << "\tline offset: " << Loc.LineOffset |
| 142 | << ", discriminator: " << Loc.Discriminator |
| 143 | << ", number of samples: " << Sample.getSamples(); |
| 144 | if (Sample.hasCalls()) { |
| 145 | OS << ", calls:"; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 146 | for (const auto &I : Sample.getCallTargets()) |
| 147 | OS << " " << I.first() << ":" << I.second; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 148 | } |
| 149 | OS << "\n"; |
| 150 | } |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 151 | OS << "\n"; |
| 152 | } |
| 153 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 154 | /// \brief Dump the function profile for \p FName. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 155 | /// |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 156 | /// \param FName Name of the function to print. |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 157 | /// \param OS Stream to emit the output to. |
| 158 | void SampleProfileReader::dumpFunctionProfile(StringRef FName, |
| 159 | raw_ostream &OS) { |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 160 | OS << "Function: " << FName << ": "; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 161 | Profiles[FName].print(OS); |
| 162 | } |
| 163 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 164 | /// \brief Dump all the function profiles found on stream \p OS. |
| 165 | void SampleProfileReader::dump(raw_ostream &OS) { |
| 166 | for (const auto &I : Profiles) |
| 167 | dumpFunctionProfile(I.getKey(), OS); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame^] | 170 | /// \brief Parse \p Input as function head. |
| 171 | /// |
| 172 | /// Parse one line of \p Input, and update function name in \p FName, |
| 173 | /// function's total sample count in \p NumSamples, function's entry |
| 174 | /// count in \p NumHeadSamples. |
| 175 | /// |
| 176 | /// \returns true if parsing is successful. |
| 177 | static bool ParseHead(const StringRef &Input, StringRef &FName, |
| 178 | unsigned &NumSamples, unsigned &NumHeadSamples) { |
| 179 | if (Input[0] == ' ') |
| 180 | return false; |
| 181 | size_t n2 = Input.rfind(':'); |
| 182 | size_t n1 = Input.rfind(':', n2 - 1); |
| 183 | FName = Input.substr(0, n1); |
| 184 | if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples)) |
| 185 | return false; |
| 186 | if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples)) |
| 187 | return false; |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | /// \brief Parse \p Input as line sample. |
| 192 | /// |
| 193 | /// \param Input input line. |
| 194 | /// \param IsCallsite true if the line represents an inlined callsite. |
| 195 | /// \param Depth the depth of the inline stack. |
| 196 | /// \param NumSamples total samples of the line/inlined callsite. |
| 197 | /// \param LineOffset line offset to the start of the function. |
| 198 | /// \param Discriminator discriminator of the line. |
| 199 | /// \param TargetCountMap map from indirect call target to count. |
| 200 | /// |
| 201 | /// returns true if parsing is successful. |
| 202 | static bool ParseLine(const StringRef &Input, bool &IsCallsite, unsigned &Depth, |
| 203 | unsigned &NumSamples, unsigned &LineOffset, |
| 204 | unsigned &Discriminator, StringRef &CalleeName, |
| 205 | DenseMap<StringRef, unsigned> &TargetCountMap) { |
| 206 | for (Depth = 0; Input[Depth] == ' '; Depth++) |
| 207 | ; |
| 208 | if (Depth == 0) |
| 209 | return false; |
| 210 | |
| 211 | size_t n1 = Input.find(':'); |
| 212 | StringRef Loc = Input.substr(Depth, n1 - Depth); |
| 213 | size_t n2 = Loc.find('.'); |
| 214 | if (n2 == StringRef::npos) { |
| 215 | if (Loc.getAsInteger(10, LineOffset)) |
| 216 | return false; |
| 217 | Discriminator = 0; |
| 218 | } else { |
| 219 | if (Loc.substr(0, n2).getAsInteger(10, LineOffset)) |
| 220 | return false; |
| 221 | if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator)) |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | StringRef Rest = Input.substr(n1 + 2); |
| 226 | if (Rest[0] >= '0' && Rest[0] <= '9') { |
| 227 | IsCallsite = false; |
| 228 | size_t n3 = Rest.find(' '); |
| 229 | if (n3 == StringRef::npos) { |
| 230 | if (Rest.getAsInteger(10, NumSamples)) |
| 231 | return false; |
| 232 | } else { |
| 233 | if (Rest.substr(0, n3).getAsInteger(10, NumSamples)) |
| 234 | return false; |
| 235 | } |
| 236 | while (n3 != StringRef::npos) { |
| 237 | n3 += Rest.substr(n3).find_first_not_of(' '); |
| 238 | Rest = Rest.substr(n3); |
| 239 | n3 = Rest.find(' '); |
| 240 | StringRef pair = Rest; |
| 241 | if (n3 != StringRef::npos) { |
| 242 | pair = Rest.substr(0, n3); |
| 243 | } |
| 244 | int n4 = pair.find(':'); |
| 245 | unsigned count; |
| 246 | if (pair.substr(n4 + 1).getAsInteger(10, count)) |
| 247 | return false; |
| 248 | TargetCountMap[pair.substr(0, n4)] = count; |
| 249 | } |
| 250 | } else { |
| 251 | IsCallsite = true; |
| 252 | int n3 = Rest.find_last_of(':'); |
| 253 | CalleeName = Rest.substr(0, n3); |
| 254 | if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples)) |
| 255 | return false; |
| 256 | } |
| 257 | return true; |
| 258 | } |
| 259 | |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 260 | /// \brief Load samples from a text file. |
| 261 | /// |
| 262 | /// See the documentation at the top of the file for an explanation of |
| 263 | /// the expected format. |
| 264 | /// |
| 265 | /// \returns true if the file was loaded successfully, false otherwise. |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 266 | std::error_code SampleProfileReaderText::read() { |
| 267 | line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#'); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 268 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame^] | 269 | SmallVector<FunctionSamples *, 10> InlineStack; |
| 270 | |
| 271 | for (; !LineIt.is_at_eof(); ++LineIt) { |
| 272 | if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#') |
| 273 | continue; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 274 | // Read the header of each function. |
| 275 | // |
| 276 | // Note that for function identifiers we are actually expecting |
| 277 | // mangled names, but we may not always get them. This happens when |
| 278 | // the compiler decides not to emit the function (e.g., it was inlined |
| 279 | // and removed). In this case, the binary will not have the linkage |
| 280 | // name for the function, so the profiler will emit the function's |
| 281 | // unmangled name, which may contain characters like ':' and '>' in its |
| 282 | // name (member functions, templates, etc). |
| 283 | // |
| 284 | // The only requirement we place on the identifier, then, is that it |
| 285 | // should not begin with a number. |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame^] | 286 | if ((*LineIt)[0] != ' ') { |
| 287 | unsigned NumSamples, NumHeadSamples; |
| 288 | StringRef FName; |
| 289 | if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) { |
| 290 | reportError(LineIt.line_number(), |
| 291 | "Expected 'mangled_name:NUM:NUM', found " + *LineIt); |
| 292 | return sampleprof_error::malformed; |
| 293 | } |
| 294 | Profiles[FName] = FunctionSamples(); |
| 295 | FunctionSamples &FProfile = Profiles[FName]; |
| 296 | FProfile.addTotalSamples(NumSamples); |
| 297 | FProfile.addHeadSamples(NumHeadSamples); |
| 298 | InlineStack.clear(); |
| 299 | InlineStack.push_back(&FProfile); |
| 300 | } else { |
| 301 | unsigned NumSamples; |
| 302 | StringRef FName; |
| 303 | DenseMap<StringRef, unsigned> TargetCountMap; |
| 304 | bool IsCallsite; |
| 305 | unsigned Depth, LineOffset, Discriminator; |
| 306 | if (!ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset, |
| 307 | Discriminator, FName, TargetCountMap)) { |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 308 | reportError(LineIt.line_number(), |
| 309 | "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " + |
| 310 | *LineIt); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 311 | return sampleprof_error::malformed; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 312 | } |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame^] | 313 | if (IsCallsite) { |
| 314 | while (InlineStack.size() > Depth) { |
| 315 | InlineStack.pop_back(); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 316 | } |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame^] | 317 | FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt( |
| 318 | CallsiteLocation(LineOffset, Discriminator, FName)); |
| 319 | FSamples.addTotalSamples(NumSamples); |
| 320 | InlineStack.push_back(&FSamples); |
| 321 | } else { |
| 322 | while (InlineStack.size() > Depth) { |
| 323 | InlineStack.pop_back(); |
| 324 | } |
| 325 | FunctionSamples &FProfile = *InlineStack.back(); |
| 326 | for (const auto &name_count : TargetCountMap) { |
| 327 | FProfile.addCalledTargetSamples(LineOffset, Discriminator, |
| 328 | name_count.first, name_count.second); |
| 329 | } |
| 330 | FProfile.addBodySamples(LineOffset, Discriminator, NumSamples); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 331 | } |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 335 | return sampleprof_error::success; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 338 | template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() { |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 339 | unsigned NumBytesRead = 0; |
| 340 | std::error_code EC; |
| 341 | uint64_t Val = decodeULEB128(Data, &NumBytesRead); |
| 342 | |
| 343 | if (Val > std::numeric_limits<T>::max()) |
| 344 | EC = sampleprof_error::malformed; |
| 345 | else if (Data + NumBytesRead > End) |
| 346 | EC = sampleprof_error::truncated; |
| 347 | else |
| 348 | EC = sampleprof_error::success; |
| 349 | |
| 350 | if (EC) { |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 351 | reportError(0, EC.message()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 352 | return EC; |
| 353 | } |
| 354 | |
| 355 | Data += NumBytesRead; |
| 356 | return static_cast<T>(Val); |
| 357 | } |
| 358 | |
| 359 | ErrorOr<StringRef> SampleProfileReaderBinary::readString() { |
| 360 | std::error_code EC; |
| 361 | StringRef Str(reinterpret_cast<const char *>(Data)); |
| 362 | if (Data + Str.size() + 1 > End) { |
| 363 | EC = sampleprof_error::truncated; |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 364 | reportError(0, EC.message()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 365 | return EC; |
| 366 | } |
| 367 | |
| 368 | Data += Str.size() + 1; |
| 369 | return Str; |
| 370 | } |
| 371 | |
| 372 | std::error_code SampleProfileReaderBinary::read() { |
| 373 | while (!at_eof()) { |
| 374 | auto FName(readString()); |
| 375 | if (std::error_code EC = FName.getError()) |
| 376 | return EC; |
| 377 | |
| 378 | Profiles[*FName] = FunctionSamples(); |
| 379 | FunctionSamples &FProfile = Profiles[*FName]; |
| 380 | |
| 381 | auto Val = readNumber<unsigned>(); |
| 382 | if (std::error_code EC = Val.getError()) |
| 383 | return EC; |
| 384 | FProfile.addTotalSamples(*Val); |
| 385 | |
| 386 | Val = readNumber<unsigned>(); |
| 387 | if (std::error_code EC = Val.getError()) |
| 388 | return EC; |
| 389 | FProfile.addHeadSamples(*Val); |
| 390 | |
| 391 | // Read the samples in the body. |
| 392 | auto NumRecords = readNumber<unsigned>(); |
| 393 | if (std::error_code EC = NumRecords.getError()) |
| 394 | return EC; |
| 395 | for (unsigned I = 0; I < *NumRecords; ++I) { |
| 396 | auto LineOffset = readNumber<uint64_t>(); |
| 397 | if (std::error_code EC = LineOffset.getError()) |
| 398 | return EC; |
| 399 | |
| 400 | auto Discriminator = readNumber<uint64_t>(); |
| 401 | if (std::error_code EC = Discriminator.getError()) |
| 402 | return EC; |
| 403 | |
| 404 | auto NumSamples = readNumber<uint64_t>(); |
| 405 | if (std::error_code EC = NumSamples.getError()) |
| 406 | return EC; |
| 407 | |
| 408 | auto NumCalls = readNumber<unsigned>(); |
| 409 | if (std::error_code EC = NumCalls.getError()) |
| 410 | return EC; |
| 411 | |
| 412 | for (unsigned J = 0; J < *NumCalls; ++J) { |
| 413 | auto CalledFunction(readString()); |
| 414 | if (std::error_code EC = CalledFunction.getError()) |
| 415 | return EC; |
| 416 | |
| 417 | auto CalledFunctionSamples = readNumber<uint64_t>(); |
| 418 | if (std::error_code EC = CalledFunctionSamples.getError()) |
| 419 | return EC; |
| 420 | |
| 421 | FProfile.addCalledTargetSamples(*LineOffset, *Discriminator, |
| 422 | *CalledFunction, |
| 423 | *CalledFunctionSamples); |
| 424 | } |
| 425 | |
| 426 | FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return sampleprof_error::success; |
| 431 | } |
| 432 | |
| 433 | std::error_code SampleProfileReaderBinary::readHeader() { |
| 434 | Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); |
| 435 | End = Data + Buffer->getBufferSize(); |
| 436 | |
| 437 | // Read and check the magic identifier. |
| 438 | auto Magic = readNumber<uint64_t>(); |
| 439 | if (std::error_code EC = Magic.getError()) |
| 440 | return EC; |
| 441 | else if (*Magic != SPMagic()) |
| 442 | return sampleprof_error::bad_magic; |
| 443 | |
| 444 | // Read the version number. |
| 445 | auto Version = readNumber<uint64_t>(); |
| 446 | if (std::error_code EC = Version.getError()) |
| 447 | return EC; |
| 448 | else if (*Version != SPVersion()) |
| 449 | return sampleprof_error::unsupported_version; |
| 450 | |
| 451 | return sampleprof_error::success; |
| 452 | } |
| 453 | |
| 454 | bool SampleProfileReaderBinary::hasFormat(const MemoryBuffer &Buffer) { |
| 455 | const uint8_t *Data = |
| 456 | reinterpret_cast<const uint8_t *>(Buffer.getBufferStart()); |
| 457 | uint64_t Magic = decodeULEB128(Data); |
| 458 | return Magic == SPMagic(); |
| 459 | } |
| 460 | |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 461 | bool SourceInfo::operator<(const SourceInfo &P) const { |
| 462 | if (Line != P.Line) |
| 463 | return Line < P.Line; |
| 464 | if (StartLine != P.StartLine) |
| 465 | return StartLine < P.StartLine; |
| 466 | if (Discriminator != P.Discriminator) |
| 467 | return Discriminator < P.Discriminator; |
| 468 | return FuncName < P.FuncName; |
| 469 | } |
| 470 | |
| 471 | std::error_code SampleProfileReaderGCC::skipNextWord() { |
| 472 | uint32_t dummy; |
| 473 | if (!GcovBuffer.readInt(dummy)) |
| 474 | return sampleprof_error::truncated; |
| 475 | return sampleprof_error::success; |
| 476 | } |
| 477 | |
| 478 | template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() { |
| 479 | if (sizeof(T) <= sizeof(uint32_t)) { |
| 480 | uint32_t Val; |
| 481 | if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max()) |
| 482 | return static_cast<T>(Val); |
| 483 | } else if (sizeof(T) <= sizeof(uint64_t)) { |
| 484 | uint64_t Val; |
| 485 | if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max()) |
| 486 | return static_cast<T>(Val); |
| 487 | } |
| 488 | |
| 489 | std::error_code EC = sampleprof_error::malformed; |
| 490 | reportError(0, EC.message()); |
| 491 | return EC; |
| 492 | } |
| 493 | |
| 494 | ErrorOr<StringRef> SampleProfileReaderGCC::readString() { |
| 495 | StringRef Str; |
| 496 | if (!GcovBuffer.readString(Str)) |
| 497 | return sampleprof_error::truncated; |
| 498 | return Str; |
| 499 | } |
| 500 | |
| 501 | std::error_code SampleProfileReaderGCC::readHeader() { |
| 502 | // Read the magic identifier. |
| 503 | if (!GcovBuffer.readGCDAFormat()) |
| 504 | return sampleprof_error::unrecognized_format; |
| 505 | |
| 506 | // Read the version number. Note - the GCC reader does not validate this |
| 507 | // version, but the profile creator generates v704. |
| 508 | GCOV::GCOVVersion version; |
| 509 | if (!GcovBuffer.readGCOVVersion(version)) |
| 510 | return sampleprof_error::unrecognized_format; |
| 511 | |
| 512 | if (version != GCOV::V704) |
| 513 | return sampleprof_error::unsupported_version; |
| 514 | |
| 515 | // Skip the empty integer. |
| 516 | if (std::error_code EC = skipNextWord()) |
| 517 | return EC; |
| 518 | |
| 519 | return sampleprof_error::success; |
| 520 | } |
| 521 | |
| 522 | std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) { |
| 523 | uint32_t Tag; |
| 524 | if (!GcovBuffer.readInt(Tag)) |
| 525 | return sampleprof_error::truncated; |
| 526 | |
| 527 | if (Tag != Expected) |
| 528 | return sampleprof_error::malformed; |
| 529 | |
| 530 | if (std::error_code EC = skipNextWord()) |
| 531 | return EC; |
| 532 | |
| 533 | return sampleprof_error::success; |
| 534 | } |
| 535 | |
| 536 | std::error_code SampleProfileReaderGCC::readNameTable() { |
| 537 | if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames)) |
| 538 | return EC; |
| 539 | |
| 540 | uint32_t Size; |
| 541 | if (!GcovBuffer.readInt(Size)) |
| 542 | return sampleprof_error::truncated; |
| 543 | |
| 544 | for (uint32_t I = 0; I < Size; ++I) { |
| 545 | StringRef Str; |
| 546 | if (!GcovBuffer.readString(Str)) |
| 547 | return sampleprof_error::truncated; |
| 548 | Names.push_back(Str); |
| 549 | } |
| 550 | |
| 551 | return sampleprof_error::success; |
| 552 | } |
| 553 | |
| 554 | std::error_code SampleProfileReaderGCC::readFunctionProfiles() { |
| 555 | if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction)) |
| 556 | return EC; |
| 557 | |
| 558 | uint32_t NumFunctions; |
| 559 | if (!GcovBuffer.readInt(NumFunctions)) |
| 560 | return sampleprof_error::truncated; |
| 561 | |
| 562 | SourceStack Stack; |
| 563 | for (uint32_t I = 0; I < NumFunctions; ++I) |
| 564 | if (std::error_code EC = readOneFunctionProfile(Stack, true)) |
| 565 | return EC; |
| 566 | |
| 567 | return sampleprof_error::success; |
| 568 | } |
| 569 | |
| 570 | std::error_code SampleProfileReaderGCC::addSourceCount(StringRef Name, |
| 571 | const SourceStack &Src, |
| 572 | uint64_t Count) { |
| 573 | if (Src.size() == 0 || Src[0].Malformed()) |
| 574 | return sampleprof_error::malformed; |
| 575 | FunctionSamples &FProfile = Profiles[Name]; |
| 576 | FProfile.addTotalSamples(Count); |
| 577 | // FIXME(dnovillo) - Properly update inline stack for FnName. |
| 578 | FProfile.addBodySamples(Src[0].Line, Src[0].Discriminator, Count); |
| 579 | return sampleprof_error::success; |
| 580 | } |
| 581 | |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 582 | std::error_code |
| 583 | SampleProfileReaderGCC::readOneFunctionProfile(const SourceStack &Stack, |
| 584 | bool Update) { |
| 585 | uint64_t HeadCount = 0; |
| 586 | if (Stack.size() == 0) |
| 587 | if (!GcovBuffer.readInt64(HeadCount)) |
| 588 | return sampleprof_error::truncated; |
| 589 | |
| 590 | uint32_t NameIdx; |
| 591 | if (!GcovBuffer.readInt(NameIdx)) |
| 592 | return sampleprof_error::truncated; |
| 593 | |
| 594 | StringRef Name(Names[NameIdx]); |
| 595 | |
| 596 | uint32_t NumPosCounts; |
| 597 | if (!GcovBuffer.readInt(NumPosCounts)) |
| 598 | return sampleprof_error::truncated; |
| 599 | |
| 600 | uint32_t NumCallSites; |
| 601 | if (!GcovBuffer.readInt(NumCallSites)) |
| 602 | return sampleprof_error::truncated; |
| 603 | |
| 604 | if (Stack.size() == 0) { |
| 605 | FunctionSamples &FProfile = Profiles[Name]; |
| 606 | FProfile.addHeadSamples(HeadCount); |
| 607 | if (FProfile.getTotalSamples() > 0) |
| 608 | Update = false; |
| 609 | } |
| 610 | |
| 611 | for (uint32_t I = 0; I < NumPosCounts; ++I) { |
| 612 | uint32_t Offset; |
| 613 | if (!GcovBuffer.readInt(Offset)) |
| 614 | return sampleprof_error::truncated; |
| 615 | |
| 616 | uint32_t NumTargets; |
| 617 | if (!GcovBuffer.readInt(NumTargets)) |
| 618 | return sampleprof_error::truncated; |
| 619 | |
| 620 | uint64_t Count; |
| 621 | if (!GcovBuffer.readInt64(Count)) |
| 622 | return sampleprof_error::truncated; |
| 623 | |
| 624 | SourceInfo Info(Name, "", "", 0, Offset >> 16, Offset & 0xffff); |
| 625 | SourceStack NewStack; |
| 626 | NewStack.push_back(Info); |
| 627 | NewStack.insert(NewStack.end(), Stack.begin(), Stack.end()); |
| 628 | if (Update) |
| 629 | addSourceCount(NewStack[NewStack.size() - 1].FuncName, NewStack, Count); |
| 630 | |
| 631 | for (uint32_t J = 0; J < NumTargets; J++) { |
| 632 | uint32_t HistVal; |
| 633 | if (!GcovBuffer.readInt(HistVal)) |
| 634 | return sampleprof_error::truncated; |
| 635 | |
| 636 | if (HistVal != HIST_TYPE_INDIR_CALL_TOPN) |
| 637 | return sampleprof_error::malformed; |
| 638 | |
| 639 | uint64_t TargetIdx; |
| 640 | if (!GcovBuffer.readInt64(TargetIdx)) |
| 641 | return sampleprof_error::truncated; |
| 642 | StringRef TargetName(Names[TargetIdx]); |
| 643 | |
| 644 | uint64_t TargetCount; |
| 645 | if (!GcovBuffer.readInt64(TargetCount)) |
| 646 | return sampleprof_error::truncated; |
| 647 | |
| 648 | if (Update) { |
| 649 | FunctionSamples &TargetProfile = Profiles[TargetName]; |
| 650 | TargetProfile.addBodySamples(NewStack[0].Line, |
| 651 | NewStack[0].Discriminator, TargetCount); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | for (uint32_t I = 0; I < NumCallSites; I++) { |
| 657 | // The offset is encoded as: |
| 658 | // high 16 bits: line offset to the start of the function. |
| 659 | // low 16 bits: discriminator. |
| 660 | uint32_t Offset; |
| 661 | if (!GcovBuffer.readInt(Offset)) |
| 662 | return sampleprof_error::truncated; |
| 663 | SourceInfo Info(Name, "", "", 0, Offset >> 16, Offset & 0xffff); |
| 664 | SourceStack NewStack; |
| 665 | NewStack.push_back(Info); |
| 666 | NewStack.insert(NewStack.end(), Stack.begin(), Stack.end()); |
| 667 | if (std::error_code EC = readOneFunctionProfile(NewStack, Update)) |
| 668 | return EC; |
| 669 | } |
| 670 | |
| 671 | return sampleprof_error::success; |
| 672 | } |
| 673 | |
| 674 | std::error_code SampleProfileReaderGCC::readModuleGroup() { |
| 675 | // FIXME(dnovillo) - Module support still not implemented. |
| 676 | return sampleprof_error::not_implemented; |
| 677 | } |
| 678 | |
| 679 | std::error_code SampleProfileReaderGCC::readWorkingSet() { |
| 680 | // FIXME(dnovillo) - Working sets still not implemented. |
| 681 | return sampleprof_error::not_implemented; |
| 682 | } |
| 683 | |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 684 | /// \brief Read a GCC AutoFDO profile. |
| 685 | /// |
| 686 | /// This format is generated by the Linux Perf conversion tool at |
| 687 | /// https://github.com/google/autofdo. |
| 688 | std::error_code SampleProfileReaderGCC::read() { |
| 689 | // Read the string table. |
| 690 | if (std::error_code EC = readNameTable()) |
| 691 | return EC; |
| 692 | |
| 693 | // Read the source profile. |
| 694 | if (std::error_code EC = readFunctionProfiles()) |
| 695 | return EC; |
| 696 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame^] | 697 | // FIXME(dnovillo) - Module groups and working set support are not |
| 698 | // yet implemented. |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 699 | #if 0 |
| 700 | // Read the module group file. |
| 701 | if (std::error_code EC = readModuleGroup()) |
| 702 | return EC; |
| 703 | |
| 704 | // Read the working set. |
| 705 | if (std::error_code EC = readWorkingSet()) |
| 706 | return EC; |
| 707 | #endif |
| 708 | |
| 709 | return sampleprof_error::success; |
| 710 | } |
| 711 | |
| 712 | bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) { |
| 713 | StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart())); |
| 714 | return Magic == "adcg*704"; |
| 715 | } |
| 716 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 717 | /// \brief Prepare a memory buffer for the contents of \p Filename. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 718 | /// |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 719 | /// \returns an error code indicating the status of the buffer. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 720 | static ErrorOr<std::unique_ptr<MemoryBuffer>> |
| 721 | setupMemoryBuffer(std::string Filename) { |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 722 | auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename); |
| 723 | if (std::error_code EC = BufferOrErr.getError()) |
| 724 | return EC; |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 725 | auto Buffer = std::move(BufferOrErr.get()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 726 | |
| 727 | // Sanity check the file. |
| 728 | if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max()) |
| 729 | return sampleprof_error::too_large; |
| 730 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 731 | return std::move(Buffer); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /// \brief Create a sample profile reader based on the format of the input file. |
| 735 | /// |
| 736 | /// \param Filename The file to open. |
| 737 | /// |
| 738 | /// \param Reader The reader to instantiate according to \p Filename's format. |
| 739 | /// |
| 740 | /// \param C The LLVM context to use to emit diagnostics. |
| 741 | /// |
| 742 | /// \returns an error code indicating the status of the created reader. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 743 | ErrorOr<std::unique_ptr<SampleProfileReader>> |
| 744 | SampleProfileReader::create(StringRef Filename, LLVMContext &C) { |
| 745 | auto BufferOrError = setupMemoryBuffer(Filename); |
| 746 | if (std::error_code EC = BufferOrError.getError()) |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 747 | return EC; |
| 748 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 749 | auto Buffer = std::move(BufferOrError.get()); |
| 750 | std::unique_ptr<SampleProfileReader> Reader; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 751 | if (SampleProfileReaderBinary::hasFormat(*Buffer)) |
| 752 | Reader.reset(new SampleProfileReaderBinary(std::move(Buffer), C)); |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 753 | else if (SampleProfileReaderGCC::hasFormat(*Buffer)) |
| 754 | Reader.reset(new SampleProfileReaderGCC(std::move(Buffer), C)); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 755 | else |
| 756 | Reader.reset(new SampleProfileReaderText(std::move(Buffer), C)); |
| 757 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 758 | if (std::error_code EC = Reader->readHeader()) |
| 759 | return EC; |
| 760 | |
| 761 | return std::move(Reader); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 762 | } |