1 //=-- SampleProf.cpp - Sample profiling format support --------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains common definitions used in the reading and writing of 10 // sample profile data. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ProfileData/SampleProf.h" 15 #include "llvm/Config/llvm-config.h" 16 #include "llvm/IR/DebugInfoMetadata.h" 17 #include "llvm/Support/Compiler.h" 18 #include "llvm/Support/Debug.h" 19 #include "llvm/Support/Error.h" 20 #include "llvm/Support/ErrorHandling.h" 21 #include "llvm/Support/LEB128.h" 22 #include "llvm/Support/ManagedStatic.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include <string> 25 #include <system_error> 26 27 using namespace llvm; 28 using namespace sampleprof; 29 30 namespace llvm { 31 namespace sampleprof { 32 SampleProfileFormat FunctionSamples::Format; 33 bool FunctionSamples::UseMD5; 34 } // namespace sampleprof 35 } // namespace llvm 36 37 namespace { 38 39 // FIXME: This class is only here to support the transition to llvm::Error. It 40 // will be removed once this transition is complete. Clients should prefer to 41 // deal with the Error value directly, rather than converting to error_code. 42 class SampleProfErrorCategoryType : public std::error_category { 43 const char *name() const noexcept override { return "llvm.sampleprof"; } 44 45 std::string message(int IE) const override { 46 sampleprof_error E = static_cast<sampleprof_error>(IE); 47 switch (E) { 48 case sampleprof_error::success: 49 return "Success"; 50 case sampleprof_error::bad_magic: 51 return "Invalid sample profile data (bad magic)"; 52 case sampleprof_error::unsupported_version: 53 return "Unsupported sample profile format version"; 54 case sampleprof_error::too_large: 55 return "Too much profile data"; 56 case sampleprof_error::truncated: 57 return "Truncated profile data"; 58 case sampleprof_error::malformed: 59 return "Malformed sample profile data"; 60 case sampleprof_error::unrecognized_format: 61 return "Unrecognized sample profile encoding format"; 62 case sampleprof_error::unsupported_writing_format: 63 return "Profile encoding format unsupported for writing operations"; 64 case sampleprof_error::truncated_name_table: 65 return "Truncated function name table"; 66 case sampleprof_error::not_implemented: 67 return "Unimplemented feature"; 68 case sampleprof_error::counter_overflow: 69 return "Counter overflow"; 70 case sampleprof_error::ostream_seek_unsupported: 71 return "Ostream does not support seek"; 72 case sampleprof_error::compress_failed: 73 return "Compress failure"; 74 case sampleprof_error::uncompress_failed: 75 return "Uncompress failure"; 76 case sampleprof_error::zlib_unavailable: 77 return "Zlib is unavailable"; 78 } 79 llvm_unreachable("A value of sampleprof_error has no message."); 80 } 81 }; 82 83 } // end anonymous namespace 84 85 static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory; 86 87 const std::error_category &llvm::sampleprof_category() { 88 return *ErrorCategory; 89 } 90 91 void LineLocation::print(raw_ostream &OS) const { 92 OS << LineOffset; 93 if (Discriminator > 0) 94 OS << "." << Discriminator; 95 } 96 97 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, 98 const LineLocation &Loc) { 99 Loc.print(OS); 100 return OS; 101 } 102 103 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 104 LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); } 105 #endif 106 107 /// Print the sample record to the stream \p OS indented by \p Indent. 108 void SampleRecord::print(raw_ostream &OS, unsigned Indent) const { 109 OS << NumSamples; 110 if (hasCalls()) { 111 OS << ", calls:"; 112 for (const auto &I : getSortedCallTargets()) 113 OS << " " << I.first << ":" << I.second; 114 } 115 OS << "\n"; 116 } 117 118 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 119 LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); } 120 #endif 121 122 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, 123 const SampleRecord &Sample) { 124 Sample.print(OS, 0); 125 return OS; 126 } 127 128 /// Print the samples collected for a function on stream \p OS. 129 void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const { 130 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size() 131 << " sampled lines\n"; 132 133 OS.indent(Indent); 134 if (!BodySamples.empty()) { 135 OS << "Samples collected in the function's body {\n"; 136 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples); 137 for (const auto &SI : SortedBodySamples.get()) { 138 OS.indent(Indent + 2); 139 OS << SI->first << ": " << SI->second; 140 } 141 OS.indent(Indent); 142 OS << "}\n"; 143 } else { 144 OS << "No samples collected in the function's body\n"; 145 } 146 147 OS.indent(Indent); 148 if (!CallsiteSamples.empty()) { 149 OS << "Samples collected in inlined callsites {\n"; 150 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples( 151 CallsiteSamples); 152 for (const auto &CS : SortedCallsiteSamples.get()) { 153 for (const auto &FS : CS->second) { 154 OS.indent(Indent + 2); 155 OS << CS->first << ": inlined callee: " << FS.second.getName() << ": "; 156 FS.second.print(OS, Indent + 4); 157 } 158 } 159 OS.indent(Indent); 160 OS << "}\n"; 161 } else { 162 OS << "No inlined callsites in this function\n"; 163 } 164 } 165 166 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, 167 const FunctionSamples &FS) { 168 FS.print(OS); 169 return OS; 170 } 171 172 unsigned FunctionSamples::getOffset(const DILocation *DIL) { 173 return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) & 174 0xffff; 175 } 176 177 const FunctionSamples * 178 FunctionSamples::findFunctionSamples(const DILocation *DIL) const { 179 assert(DIL); 180 SmallVector<std::pair<LineLocation, StringRef>, 10> S; 181 182 const DILocation *PrevDIL = DIL; 183 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) { 184 S.push_back(std::make_pair( 185 LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()), 186 PrevDIL->getScope()->getSubprogram()->getLinkageName())); 187 PrevDIL = DIL; 188 } 189 if (S.size() == 0) 190 return this; 191 const FunctionSamples *FS = this; 192 for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) { 193 FS = FS->findFunctionSamplesAt(S[i].first, S[i].second); 194 } 195 return FS; 196 } 197 198 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 199 LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); } 200 #endif 201 202 std::error_code ProfileSymbolList::read(const uint8_t *Data, 203 uint64_t ListSize) { 204 const char *ListStart = reinterpret_cast<const char *>(Data); 205 uint64_t Size = 0; 206 while (Size < ListSize) { 207 StringRef Str(ListStart + Size); 208 add(Str); 209 Size += Str.size() + 1; 210 } 211 if (Size != ListSize) 212 return sampleprof_error::malformed; 213 return sampleprof_error::success; 214 } 215 216 std::error_code ProfileSymbolList::write(raw_ostream &OS) { 217 // Sort the symbols before output. If doing compression. 218 // It will make the compression much more effective. 219 std::vector<StringRef> SortedList; 220 SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end()); 221 llvm::sort(SortedList); 222 223 std::string OutputString; 224 for (auto &Sym : SortedList) { 225 OutputString.append(Sym.str()); 226 OutputString.append(1, '\0'); 227 } 228 229 OS << OutputString; 230 return sampleprof_error::success; 231 } 232 233 void ProfileSymbolList::dump(raw_ostream &OS) const { 234 OS << "======== Dump profile symbol list ========\n"; 235 std::vector<StringRef> SortedList; 236 SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end()); 237 llvm::sort(SortedList); 238 239 for (auto &Sym : SortedList) 240 OS << Sym << "\n"; 241 } 242