SampleProf.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #include "llvm/ProfileData/SampleProf.h"
  14. #include "llvm/Config/llvm-config.h"
  15. #include "llvm/IR/DebugInfoMetadata.h"
  16. #include "llvm/IR/PseudoProbe.h"
  17. #include "llvm/ProfileData/SampleProfReader.h"
  18. #include "llvm/Support/Compiler.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/Error.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include "llvm/Support/LEB128.h"
  23. #include "llvm/Support/ManagedStatic.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include <string>
  26. #include <system_error>
  27. using namespace llvm;
  28. using namespace sampleprof;
  29. namespace llvm {
  30. namespace sampleprof {
  31. SampleProfileFormat FunctionSamples::Format;
  32. bool FunctionSamples::ProfileIsProbeBased = false;
  33. bool FunctionSamples::ProfileIsCS = false;
  34. bool FunctionSamples::UseMD5;
  35. } // namespace sampleprof
  36. } // namespace llvm
  37. namespace {
  38. // FIXME: This class is only here to support the transition to llvm::Error. It
  39. // will be removed once this transition is complete. Clients should prefer to
  40. // deal with the Error value directly, rather than converting to error_code.
  41. class SampleProfErrorCategoryType : public std::error_category {
  42. const char *name() const noexcept override { return "llvm.sampleprof"; }
  43. std::string message(int IE) const override {
  44. sampleprof_error E = static_cast<sampleprof_error>(IE);
  45. switch (E) {
  46. case sampleprof_error::success:
  47. return "Success";
  48. case sampleprof_error::bad_magic:
  49. return "Invalid sample profile data (bad magic)";
  50. case sampleprof_error::unsupported_version:
  51. return "Unsupported sample profile format version";
  52. case sampleprof_error::too_large:
  53. return "Too much profile data";
  54. case sampleprof_error::truncated:
  55. return "Truncated profile data";
  56. case sampleprof_error::malformed:
  57. return "Malformed sample profile data";
  58. case sampleprof_error::unrecognized_format:
  59. return "Unrecognized sample profile encoding format";
  60. case sampleprof_error::unsupported_writing_format:
  61. return "Profile encoding format unsupported for writing operations";
  62. case sampleprof_error::truncated_name_table:
  63. return "Truncated function name table";
  64. case sampleprof_error::not_implemented:
  65. return "Unimplemented feature";
  66. case sampleprof_error::counter_overflow:
  67. return "Counter overflow";
  68. case sampleprof_error::ostream_seek_unsupported:
  69. return "Ostream does not support seek";
  70. case sampleprof_error::compress_failed:
  71. return "Compress failure";
  72. case sampleprof_error::uncompress_failed:
  73. return "Uncompress failure";
  74. case sampleprof_error::zlib_unavailable:
  75. return "Zlib is unavailable";
  76. case sampleprof_error::hash_mismatch:
  77. return "Function hash mismatch";
  78. }
  79. llvm_unreachable("A value of sampleprof_error has no message.");
  80. }
  81. };
  82. } // end anonymous namespace
  83. static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
  84. const std::error_category &llvm::sampleprof_category() {
  85. return *ErrorCategory;
  86. }
  87. void LineLocation::print(raw_ostream &OS) const {
  88. OS << LineOffset;
  89. if (Discriminator > 0)
  90. OS << "." << Discriminator;
  91. }
  92. raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
  93. const LineLocation &Loc) {
  94. Loc.print(OS);
  95. return OS;
  96. }
  97. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  98. LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
  99. #endif
  100. /// Print the sample record to the stream \p OS indented by \p Indent.
  101. void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
  102. OS << NumSamples;
  103. if (hasCalls()) {
  104. OS << ", calls:";
  105. for (const auto &I : getSortedCallTargets())
  106. OS << " " << I.first << ":" << I.second;
  107. }
  108. OS << "\n";
  109. }
  110. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  111. LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
  112. #endif
  113. raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
  114. const SampleRecord &Sample) {
  115. Sample.print(OS, 0);
  116. return OS;
  117. }
  118. /// Print the samples collected for a function on stream \p OS.
  119. void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
  120. if (getFunctionHash())
  121. OS << "CFG checksum " << getFunctionHash() << "\n";
  122. OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
  123. << " sampled lines\n";
  124. OS.indent(Indent);
  125. if (!BodySamples.empty()) {
  126. OS << "Samples collected in the function's body {\n";
  127. SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
  128. for (const auto &SI : SortedBodySamples.get()) {
  129. OS.indent(Indent + 2);
  130. OS << SI->first << ": " << SI->second;
  131. }
  132. OS.indent(Indent);
  133. OS << "}\n";
  134. } else {
  135. OS << "No samples collected in the function's body\n";
  136. }
  137. OS.indent(Indent);
  138. if (!CallsiteSamples.empty()) {
  139. OS << "Samples collected in inlined callsites {\n";
  140. SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
  141. CallsiteSamples);
  142. for (const auto &CS : SortedCallsiteSamples.get()) {
  143. for (const auto &FS : CS->second) {
  144. OS.indent(Indent + 2);
  145. OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
  146. FS.second.print(OS, Indent + 4);
  147. }
  148. }
  149. OS.indent(Indent);
  150. OS << "}\n";
  151. } else {
  152. OS << "No inlined callsites in this function\n";
  153. }
  154. }
  155. raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
  156. const FunctionSamples &FS) {
  157. FS.print(OS);
  158. return OS;
  159. }
  160. unsigned FunctionSamples::getOffset(const DILocation *DIL) {
  161. return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
  162. 0xffff;
  163. }
  164. LineLocation FunctionSamples::getCallSiteIdentifier(const DILocation *DIL) {
  165. if (FunctionSamples::ProfileIsProbeBased)
  166. // In a pseudo-probe based profile, a callsite is simply represented by the
  167. // ID of the probe associated with the call instruction. The probe ID is
  168. // encoded in the Discriminator field of the call instruction's debug
  169. // metadata.
  170. return LineLocation(PseudoProbeDwarfDiscriminator::extractProbeIndex(
  171. DIL->getDiscriminator()),
  172. 0);
  173. else
  174. return LineLocation(FunctionSamples::getOffset(DIL),
  175. DIL->getBaseDiscriminator());
  176. }
  177. const FunctionSamples *FunctionSamples::findFunctionSamples(
  178. const DILocation *DIL, SampleProfileReaderItaniumRemapper *Remapper) const {
  179. assert(DIL);
  180. SmallVector<std::pair<LineLocation, StringRef>, 10> S;
  181. const DILocation *PrevDIL = DIL;
  182. for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
  183. S.push_back(std::make_pair(
  184. LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
  185. PrevDIL->getScope()->getSubprogram()->getLinkageName()));
  186. PrevDIL = DIL;
  187. }
  188. if (S.size() == 0)
  189. return this;
  190. const FunctionSamples *FS = this;
  191. for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
  192. FS = FS->findFunctionSamplesAt(S[i].first, S[i].second, Remapper);
  193. }
  194. return FS;
  195. }
  196. void FunctionSamples::findAllNames(DenseSet<StringRef> &NameSet) const {
  197. NameSet.insert(Name);
  198. for (const auto &BS : BodySamples)
  199. for (const auto &TS : BS.second.getCallTargets())
  200. NameSet.insert(TS.getKey());
  201. for (const auto &CS : CallsiteSamples) {
  202. for (const auto &NameFS : CS.second) {
  203. NameSet.insert(NameFS.first);
  204. NameFS.second.findAllNames(NameSet);
  205. }
  206. }
  207. }
  208. const FunctionSamples *FunctionSamples::findFunctionSamplesAt(
  209. const LineLocation &Loc, StringRef CalleeName,
  210. SampleProfileReaderItaniumRemapper *Remapper) const {
  211. std::string CalleeGUID;
  212. CalleeName = getRepInFormat(CalleeName, UseMD5, CalleeGUID);
  213. auto iter = CallsiteSamples.find(Loc);
  214. if (iter == CallsiteSamples.end())
  215. return nullptr;
  216. auto FS = iter->second.find(CalleeName);
  217. if (FS != iter->second.end())
  218. return &FS->second;
  219. if (Remapper) {
  220. if (auto NameInProfile = Remapper->lookUpNameInProfile(CalleeName)) {
  221. auto FS = iter->second.find(*NameInProfile);
  222. if (FS != iter->second.end())
  223. return &FS->second;
  224. }
  225. }
  226. // If we cannot find exact match of the callee name, return the FS with
  227. // the max total count. Only do this when CalleeName is not provided,
  228. // i.e., only for indirect calls.
  229. if (!CalleeName.empty())
  230. return nullptr;
  231. uint64_t MaxTotalSamples = 0;
  232. const FunctionSamples *R = nullptr;
  233. for (const auto &NameFS : iter->second)
  234. if (NameFS.second.getTotalSamples() >= MaxTotalSamples) {
  235. MaxTotalSamples = NameFS.second.getTotalSamples();
  236. R = &NameFS.second;
  237. }
  238. return R;
  239. }
  240. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  241. LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
  242. #endif
  243. std::error_code ProfileSymbolList::read(const uint8_t *Data,
  244. uint64_t ListSize) {
  245. const char *ListStart = reinterpret_cast<const char *>(Data);
  246. uint64_t Size = 0;
  247. while (Size < ListSize) {
  248. StringRef Str(ListStart + Size);
  249. add(Str);
  250. Size += Str.size() + 1;
  251. }
  252. if (Size != ListSize)
  253. return sampleprof_error::malformed;
  254. return sampleprof_error::success;
  255. }
  256. std::error_code ProfileSymbolList::write(raw_ostream &OS) {
  257. // Sort the symbols before output. If doing compression.
  258. // It will make the compression much more effective.
  259. std::vector<StringRef> SortedList(Syms.begin(), Syms.end());
  260. llvm::sort(SortedList);
  261. std::string OutputString;
  262. for (auto &Sym : SortedList) {
  263. OutputString.append(Sym.str());
  264. OutputString.append(1, '\0');
  265. }
  266. OS << OutputString;
  267. return sampleprof_error::success;
  268. }
  269. void ProfileSymbolList::dump(raw_ostream &OS) const {
  270. OS << "======== Dump profile symbol list ========\n";
  271. std::vector<StringRef> SortedList(Syms.begin(), Syms.end());
  272. llvm::sort(SortedList);
  273. for (auto &Sym : SortedList)
  274. OS << Sym << "\n";
  275. }