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/Compiler.h" 17 #include "llvm/Support/Debug.h" 18 #include "llvm/Support/ErrorHandling.h" 19 #include "llvm/Support/ManagedStatic.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include <string> 22 #include <system_error> 23 24 using namespace llvm; 25 using namespace sampleprof; 26 27 namespace { 28 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. 32 class SampleProfErrorCategoryType : public std::error_category { 33 const char *name() const noexcept override { return "llvm.sampleprof"; } 34 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: 41 return "Invalid sample profile data (bad magic)"; 42 case sampleprof_error::unsupported_version: 43 return "Unsupported sample profile format version"; 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: 49 return "Malformed sample profile data"; 50 case sampleprof_error::unrecognized_format: 51 return "Unrecognized sample profile encoding format"; 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"; 56 case sampleprof_error::not_implemented: 57 return "Unimplemented feature"; 58 case sampleprof_error::counter_overflow: 59 return "Counter overflow"; 60 } 61 llvm_unreachable("A value of sampleprof_error has no message."); 62 } 63 }; 64 65 } // end anonymous namespace 66 67 static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory; 68 69 const std::error_category &llvm::sampleprof_category() { 70 return *ErrorCategory; 71 } 72 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 85 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 86 LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); } 87 #endif 88 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 100 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 101 LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); } 102 #endif 103 104 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, 105 const SampleRecord &Sample) { 106 Sample.print(OS, 0); 107 return OS; 108 } 109 110 /// \brief Print the samples collected for a function on stream \p OS. 111 void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const { 112 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size() 113 << " sampled lines\n"; 114 115 OS.indent(Indent); 116 if (!BodySamples.empty()) { 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 } 123 OS.indent(Indent); 124 OS << "}\n"; 125 } else { 126 OS << "No samples collected in the function's body\n"; 127 } 128 129 OS.indent(Indent); 130 if (!CallsiteSamples.empty()) { 131 OS << "Samples collected in inlined callsites {\n"; 132 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples( 133 CallsiteSamples); 134 for (const auto &CS : SortedCallsiteSamples.get()) { 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 } 140 } 141 OS << "}\n"; 142 } else { 143 OS << "No inlined callsites in this function\n"; 144 } 145 } 146 147 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, 148 const FunctionSamples &FS) { 149 FS.print(OS); 150 return OS; 151 } 152 153 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 154 LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); } 155 #endif 156