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