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