Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 1 | //=-- SampleProf.cpp - Sample profiling format support --------------------===// |
| 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 contains common definitions used in the reading and writing of |
| 11 | // sample profile data. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Mircea Trofin | 5695097 | 2018-02-22 06:42:57 +0000 | [diff] [blame] | 15 | #include "llvm/IR/DebugInfoMetadata.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 16 | #include "llvm/ProfileData/SampleProf.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
| 18 | #include "llvm/Support/Debug.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
| 20 | #include "llvm/Support/ManagedStatic.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include <string> |
| 23 | #include <system_error> |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 26 | using namespace sampleprof; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 27 | |
| 28 | namespace { |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 29 | |
Peter Collingbourne | 4718f8b | 2016-05-24 20:13:46 +0000 | [diff] [blame] | 30 | // FIXME: This class is only here to support the transition to llvm::Error. It |
| 31 | // will be removed once this transition is complete. Clients should prefer to |
| 32 | // deal with the Error value directly, rather than converting to error_code. |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 33 | class SampleProfErrorCategoryType : public std::error_category { |
Reid Kleckner | 990504e | 2016-10-19 23:52:38 +0000 | [diff] [blame] | 34 | const char *name() const noexcept override { return "llvm.sampleprof"; } |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 35 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 36 | std::string message(int IE) const override { |
| 37 | sampleprof_error E = static_cast<sampleprof_error>(IE); |
| 38 | switch (E) { |
| 39 | case sampleprof_error::success: |
| 40 | return "Success"; |
| 41 | case sampleprof_error::bad_magic: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 42 | return "Invalid sample profile data (bad magic)"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 43 | case sampleprof_error::unsupported_version: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 44 | return "Unsupported sample profile format version"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 45 | case sampleprof_error::too_large: |
| 46 | return "Too much profile data"; |
| 47 | case sampleprof_error::truncated: |
| 48 | return "Truncated profile data"; |
| 49 | case sampleprof_error::malformed: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 50 | return "Malformed sample profile data"; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 51 | case sampleprof_error::unrecognized_format: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 52 | return "Unrecognized sample profile encoding format"; |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 53 | case sampleprof_error::unsupported_writing_format: |
| 54 | return "Profile encoding format unsupported for writing operations"; |
| 55 | case sampleprof_error::truncated_name_table: |
| 56 | return "Truncated function name table"; |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 57 | case sampleprof_error::not_implemented: |
| 58 | return "Unimplemented feature"; |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 59 | case sampleprof_error::counter_overflow: |
| 60 | return "Counter overflow"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 61 | } |
| 62 | llvm_unreachable("A value of sampleprof_error has no message."); |
| 63 | } |
| 64 | }; |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 65 | |
| 66 | } // end anonymous namespace |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 67 | |
| 68 | static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory; |
| 69 | |
| 70 | const std::error_category &llvm::sampleprof_category() { |
| 71 | return *ErrorCategory; |
| 72 | } |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 73 | |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 74 | void LineLocation::print(raw_ostream &OS) const { |
| 75 | OS << LineOffset; |
| 76 | if (Discriminator > 0) |
| 77 | OS << "." << Discriminator; |
| 78 | } |
| 79 | |
| 80 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 81 | const LineLocation &Loc) { |
| 82 | Loc.print(OS); |
| 83 | return OS; |
| 84 | } |
| 85 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 86 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 87 | LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 88 | #endif |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 89 | |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 90 | /// \brief Print the sample record to the stream \p OS indented by \p Indent. |
| 91 | void SampleRecord::print(raw_ostream &OS, unsigned Indent) const { |
| 92 | OS << NumSamples; |
| 93 | if (hasCalls()) { |
| 94 | OS << ", calls:"; |
| 95 | for (const auto &I : getCallTargets()) |
| 96 | OS << " " << I.first() << ":" << I.second; |
| 97 | } |
| 98 | OS << "\n"; |
| 99 | } |
| 100 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 101 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 102 | LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 103 | #endif |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 104 | |
| 105 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 106 | const SampleRecord &Sample) { |
| 107 | Sample.print(OS, 0); |
| 108 | return OS; |
| 109 | } |
| 110 | |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 111 | /// \brief Print the samples collected for a function on stream \p OS. |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 112 | void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const { |
| 113 | OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size() |
| 114 | << " sampled lines\n"; |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 115 | |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 116 | OS.indent(Indent); |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 117 | if (!BodySamples.empty()) { |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 118 | OS << "Samples collected in the function's body {\n"; |
| 119 | SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples); |
| 120 | for (const auto &SI : SortedBodySamples.get()) { |
| 121 | OS.indent(Indent + 2); |
| 122 | OS << SI->first << ": " << SI->second; |
| 123 | } |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 124 | OS.indent(Indent); |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 125 | OS << "}\n"; |
| 126 | } else { |
| 127 | OS << "No samples collected in the function's body\n"; |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 128 | } |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 129 | |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 130 | OS.indent(Indent); |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 131 | if (!CallsiteSamples.empty()) { |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 132 | OS << "Samples collected in inlined callsites {\n"; |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 133 | SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples( |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 134 | CallsiteSamples); |
| 135 | for (const auto &CS : SortedCallsiteSamples.get()) { |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 136 | for (const auto &FS : CS->second) { |
| 137 | OS.indent(Indent + 2); |
| 138 | OS << CS->first << ": inlined callee: " << FS.second.getName() << ": "; |
| 139 | FS.second.print(OS, Indent + 4); |
| 140 | } |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 141 | } |
| 142 | OS << "}\n"; |
| 143 | } else { |
| 144 | OS << "No inlined callsites in this function\n"; |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 147 | |
| 148 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 149 | const FunctionSamples &FS) { |
| 150 | FS.print(OS); |
| 151 | return OS; |
| 152 | } |
| 153 | |
Mircea Trofin | 5695097 | 2018-02-22 06:42:57 +0000 | [diff] [blame] | 154 | unsigned FunctionSamples::getOffset(const DILocation *DIL) { |
| 155 | return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) & |
| 156 | 0xffff; |
| 157 | } |
| 158 | |
| 159 | const FunctionSamples * |
| 160 | FunctionSamples::findFunctionSamples(const DILocation *DIL) const { |
| 161 | assert(DIL); |
| 162 | SmallVector<std::pair<LineLocation, StringRef>, 10> S; |
| 163 | |
| 164 | const DILocation *PrevDIL = DIL; |
| 165 | for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) { |
| 166 | S.push_back(std::make_pair( |
| 167 | LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()), |
| 168 | PrevDIL->getScope()->getSubprogram()->getLinkageName())); |
| 169 | PrevDIL = DIL; |
| 170 | } |
| 171 | if (S.size() == 0) |
| 172 | return this; |
| 173 | const FunctionSamples *FS = this; |
| 174 | for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) { |
| 175 | FS = FS->findFunctionSamplesAt(S[i].first, S[i].second); |
| 176 | } |
| 177 | return FS; |
| 178 | } |
| 179 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 180 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 181 | LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 182 | #endif |