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 19 using namespace llvm::sampleprof; 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: 31 return "Invalid sample profile data (bad magic)"; 32 case sampleprof_error::unsupported_version: 33 return "Unsupported sample profile format version"; 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: 39 return "Malformed sample profile data"; 40 case sampleprof_error::unrecognized_format: 41 return "Unrecognized sample profile encoding format"; 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"; 46 case sampleprof_error::not_implemented: 47 return "Unimplemented feature"; 48 case sampleprof_error::counter_overflow: 49 return "Counter overflow"; 50 } 51 llvm_unreachable("A value of sampleprof_error has no message."); 52 } 53 }; 54 } 55 56 static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory; 57 58 const std::error_category &llvm::sampleprof_category() { 59 return *ErrorCategory; 60 } 61 62 void LineLocation::print(raw_ostream &OS) const { 63 OS << LineOffset; 64 if (Discriminator > 0) 65 OS << "." << Discriminator; 66 } 67 68 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, 69 const LineLocation &Loc) { 70 Loc.print(OS); 71 return OS; 72 } 73 74 LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); } 75 76 /// \brief Print the sample record to the stream \p OS indented by \p Indent. 77 void SampleRecord::print(raw_ostream &OS, unsigned Indent) const { 78 OS << NumSamples; 79 if (hasCalls()) { 80 OS << ", calls:"; 81 for (const auto &I : getCallTargets()) 82 OS << " " << I.first() << ":" << I.second; 83 } 84 OS << "\n"; 85 } 86 87 LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); } 88 89 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, 90 const SampleRecord &Sample) { 91 Sample.print(OS, 0); 92 return OS; 93 } 94 95 /// \brief Print the samples collected for a function on stream \p OS. 96 void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const { 97 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size() 98 << " sampled lines\n"; 99 100 OS.indent(Indent); 101 if (BodySamples.size() > 0) { 102 OS << "Samples collected in the function's body {\n"; 103 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples); 104 for (const auto &SI : SortedBodySamples.get()) { 105 OS.indent(Indent + 2); 106 OS << SI->first << ": " << SI->second; 107 } 108 OS.indent(Indent); 109 OS << "}\n"; 110 } else { 111 OS << "No samples collected in the function's body\n"; 112 } 113 114 OS.indent(Indent); 115 if (CallsiteSamples.size() > 0) { 116 OS << "Samples collected in inlined callsites {\n"; 117 SampleSorter<LineLocation, FunctionSamples> SortedCallsiteSamples( 118 CallsiteSamples); 119 for (const auto &CS : SortedCallsiteSamples.get()) { 120 OS.indent(Indent + 2); 121 OS << CS->first << ": inlined callee: " << CS->second.getName() << ": "; 122 CS->second.print(OS, Indent + 4); 123 } 124 OS << "}\n"; 125 } else { 126 OS << "No inlined callsites in this function\n"; 127 } 128 } 129 130 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, 131 const FunctionSamples &FS) { 132 FS.print(OS); 133 return OS; 134 } 135 136 void FunctionSamples::dump(void) const { print(dbgs(), 0); } 137