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