SampleProfWriter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //===- SampleProfWriter.cpp - Write LLVM sample profile data --------------===//
  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 implements the class that writes LLVM sample profiles. It
  11. // supports two file formats: text and binary. The textual representation
  12. // is useful for debugging and testing purposes. The binary representation
  13. // is more compact, resulting in smaller file sizes. However, they can
  14. // both be used interchangeably.
  15. //
  16. // See lib/ProfileData/SampleProfReader.cpp for documentation on each of the
  17. // supported formats.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "llvm/ProfileData/SampleProfWriter.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/ProfileData/ProfileCommon.h"
  23. #include "llvm/ProfileData/SampleProf.h"
  24. #include "llvm/Support/ErrorOr.h"
  25. #include "llvm/Support/FileSystem.h"
  26. #include "llvm/Support/LEB128.h"
  27. #include "llvm/Support/MD5.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include <algorithm>
  30. #include <cstdint>
  31. #include <memory>
  32. #include <set>
  33. #include <system_error>
  34. #include <utility>
  35. #include <vector>
  36. using namespace llvm;
  37. using namespace sampleprof;
  38. std::error_code
  39. SampleProfileWriter::write(const StringMap<FunctionSamples> &ProfileMap) {
  40. if (std::error_code EC = writeHeader(ProfileMap))
  41. return EC;
  42. // Sort the ProfileMap by total samples.
  43. typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples;
  44. std::vector<NameFunctionSamples> V;
  45. for (const auto &I : ProfileMap)
  46. V.push_back(std::make_pair(I.getKey(), &I.second));
  47. std::stable_sort(
  48. V.begin(), V.end(),
  49. [](const NameFunctionSamples &A, const NameFunctionSamples &B) {
  50. if (A.second->getTotalSamples() == B.second->getTotalSamples())
  51. return A.first > B.first;
  52. return A.second->getTotalSamples() > B.second->getTotalSamples();
  53. });
  54. for (const auto &I : V) {
  55. if (std::error_code EC = write(*I.second))
  56. return EC;
  57. }
  58. return sampleprof_error::success;
  59. }
  60. /// Write samples to a text file.
  61. ///
  62. /// Note: it may be tempting to implement this in terms of
  63. /// FunctionSamples::print(). Please don't. The dump functionality is intended
  64. /// for debugging and has no specified form.
  65. ///
  66. /// The format used here is more structured and deliberate because
  67. /// it needs to be parsed by the SampleProfileReaderText class.
  68. std::error_code SampleProfileWriterText::write(const FunctionSamples &S) {
  69. auto &OS = *OutputStream;
  70. OS << S.getName() << ":" << S.getTotalSamples();
  71. if (Indent == 0)
  72. OS << ":" << S.getHeadSamples();
  73. OS << "\n";
  74. SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
  75. for (const auto &I : SortedSamples.get()) {
  76. LineLocation Loc = I->first;
  77. const SampleRecord &Sample = I->second;
  78. OS.indent(Indent + 1);
  79. if (Loc.Discriminator == 0)
  80. OS << Loc.LineOffset << ": ";
  81. else
  82. OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
  83. OS << Sample.getSamples();
  84. for (const auto &J : Sample.getCallTargets())
  85. OS << " " << J.first() << ":" << J.second;
  86. OS << "\n";
  87. }
  88. SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
  89. S.getCallsiteSamples());
  90. Indent += 1;
  91. for (const auto &I : SortedCallsiteSamples.get())
  92. for (const auto &FS : I->second) {
  93. LineLocation Loc = I->first;
  94. const FunctionSamples &CalleeSamples = FS.second;
  95. OS.indent(Indent);
  96. if (Loc.Discriminator == 0)
  97. OS << Loc.LineOffset << ": ";
  98. else
  99. OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
  100. if (std::error_code EC = write(CalleeSamples))
  101. return EC;
  102. }
  103. Indent -= 1;
  104. return sampleprof_error::success;
  105. }
  106. std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
  107. const auto &ret = NameTable.find(FName);
  108. if (ret == NameTable.end())
  109. return sampleprof_error::truncated_name_table;
  110. encodeULEB128(ret->second, *OutputStream);
  111. return sampleprof_error::success;
  112. }
  113. void SampleProfileWriterBinary::addName(StringRef FName) {
  114. NameTable.insert(std::make_pair(FName, 0));
  115. }
  116. void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
  117. // Add all the names in indirect call targets.
  118. for (const auto &I : S.getBodySamples()) {
  119. const SampleRecord &Sample = I.second;
  120. for (const auto &J : Sample.getCallTargets())
  121. addName(J.first());
  122. }
  123. // Recursively add all the names for inlined callsites.
  124. for (const auto &J : S.getCallsiteSamples())
  125. for (const auto &FS : J.second) {
  126. const FunctionSamples &CalleeSamples = FS.second;
  127. addName(CalleeSamples.getName());
  128. addNames(CalleeSamples);
  129. }
  130. }
  131. void SampleProfileWriterBinary::stablizeNameTable(std::set<StringRef> &V) {
  132. // Sort the names to make NameTable deterministic.
  133. for (const auto &I : NameTable)
  134. V.insert(I.first);
  135. int i = 0;
  136. for (const StringRef &N : V)
  137. NameTable[N] = i++;
  138. }
  139. std::error_code SampleProfileWriterRawBinary::writeNameTable() {
  140. auto &OS = *OutputStream;
  141. std::set<StringRef> V;
  142. stablizeNameTable(V);
  143. // Write out the name table.
  144. encodeULEB128(NameTable.size(), OS);
  145. for (auto N : V) {
  146. OS << N;
  147. encodeULEB128(0, OS);
  148. }
  149. return sampleprof_error::success;
  150. }
  151. std::error_code SampleProfileWriterCompactBinary::writeNameTable() {
  152. auto &OS = *OutputStream;
  153. std::set<StringRef> V;
  154. stablizeNameTable(V);
  155. // Write out the name table.
  156. encodeULEB128(NameTable.size(), OS);
  157. for (auto N : V) {
  158. encodeULEB128(MD5Hash(N), OS);
  159. }
  160. return sampleprof_error::success;
  161. }
  162. std::error_code SampleProfileWriterRawBinary::writeMagicIdent() {
  163. auto &OS = *OutputStream;
  164. // Write file magic identifier.
  165. encodeULEB128(SPMagic(), OS);
  166. encodeULEB128(SPVersion(), OS);
  167. return sampleprof_error::success;
  168. }
  169. std::error_code SampleProfileWriterCompactBinary::writeMagicIdent() {
  170. auto &OS = *OutputStream;
  171. // Write file magic identifier.
  172. encodeULEB128(SPMagic(SPF_Compact_Binary), OS);
  173. encodeULEB128(SPVersion(), OS);
  174. return sampleprof_error::success;
  175. }
  176. std::error_code SampleProfileWriterBinary::writeHeader(
  177. const StringMap<FunctionSamples> &ProfileMap) {
  178. writeMagicIdent();
  179. computeSummary(ProfileMap);
  180. if (auto EC = writeSummary())
  181. return EC;
  182. // Generate the name table for all the functions referenced in the profile.
  183. for (const auto &I : ProfileMap) {
  184. addName(I.first());
  185. addNames(I.second);
  186. }
  187. writeNameTable();
  188. return sampleprof_error::success;
  189. }
  190. std::error_code SampleProfileWriterBinary::writeSummary() {
  191. auto &OS = *OutputStream;
  192. encodeULEB128(Summary->getTotalCount(), OS);
  193. encodeULEB128(Summary->getMaxCount(), OS);
  194. encodeULEB128(Summary->getMaxFunctionCount(), OS);
  195. encodeULEB128(Summary->getNumCounts(), OS);
  196. encodeULEB128(Summary->getNumFunctions(), OS);
  197. std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary();
  198. encodeULEB128(Entries.size(), OS);
  199. for (auto Entry : Entries) {
  200. encodeULEB128(Entry.Cutoff, OS);
  201. encodeULEB128(Entry.MinCount, OS);
  202. encodeULEB128(Entry.NumCounts, OS);
  203. }
  204. return sampleprof_error::success;
  205. }
  206. std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
  207. auto &OS = *OutputStream;
  208. if (std::error_code EC = writeNameIdx(S.getName()))
  209. return EC;
  210. encodeULEB128(S.getTotalSamples(), OS);
  211. // Emit all the body samples.
  212. encodeULEB128(S.getBodySamples().size(), OS);
  213. for (const auto &I : S.getBodySamples()) {
  214. LineLocation Loc = I.first;
  215. const SampleRecord &Sample = I.second;
  216. encodeULEB128(Loc.LineOffset, OS);
  217. encodeULEB128(Loc.Discriminator, OS);
  218. encodeULEB128(Sample.getSamples(), OS);
  219. encodeULEB128(Sample.getCallTargets().size(), OS);
  220. for (const auto &J : Sample.getCallTargets()) {
  221. StringRef Callee = J.first();
  222. uint64_t CalleeSamples = J.second;
  223. if (std::error_code EC = writeNameIdx(Callee))
  224. return EC;
  225. encodeULEB128(CalleeSamples, OS);
  226. }
  227. }
  228. // Recursively emit all the callsite samples.
  229. uint64_t NumCallsites = 0;
  230. for (const auto &J : S.getCallsiteSamples())
  231. NumCallsites += J.second.size();
  232. encodeULEB128(NumCallsites, OS);
  233. for (const auto &J : S.getCallsiteSamples())
  234. for (const auto &FS : J.second) {
  235. LineLocation Loc = J.first;
  236. const FunctionSamples &CalleeSamples = FS.second;
  237. encodeULEB128(Loc.LineOffset, OS);
  238. encodeULEB128(Loc.Discriminator, OS);
  239. if (std::error_code EC = writeBody(CalleeSamples))
  240. return EC;
  241. }
  242. return sampleprof_error::success;
  243. }
  244. /// Write samples of a top-level function to a binary file.
  245. ///
  246. /// \returns true if the samples were written successfully, false otherwise.
  247. std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) {
  248. encodeULEB128(S.getHeadSamples(), *OutputStream);
  249. return writeBody(S);
  250. }
  251. /// Create a sample profile file writer based on the specified format.
  252. ///
  253. /// \param Filename The file to create.
  254. ///
  255. /// \param Format Encoding format for the profile file.
  256. ///
  257. /// \returns an error code indicating the status of the created writer.
  258. ErrorOr<std::unique_ptr<SampleProfileWriter>>
  259. SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
  260. std::error_code EC;
  261. std::unique_ptr<raw_ostream> OS;
  262. if (Format == SPF_Raw_Binary || Format == SPF_Compact_Binary)
  263. OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_None));
  264. else
  265. OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_Text));
  266. if (EC)
  267. return EC;
  268. return create(OS, Format);
  269. }
  270. /// Create a sample profile stream writer based on the specified format.
  271. ///
  272. /// \param OS The output stream to store the profile data to.
  273. ///
  274. /// \param Format Encoding format for the profile file.
  275. ///
  276. /// \returns an error code indicating the status of the created writer.
  277. ErrorOr<std::unique_ptr<SampleProfileWriter>>
  278. SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
  279. SampleProfileFormat Format) {
  280. std::error_code EC;
  281. std::unique_ptr<SampleProfileWriter> Writer;
  282. if (Format == SPF_Raw_Binary)
  283. Writer.reset(new SampleProfileWriterRawBinary(OS));
  284. else if (Format == SPF_Compact_Binary)
  285. Writer.reset(new SampleProfileWriterCompactBinary(OS));
  286. else if (Format == SPF_Text)
  287. Writer.reset(new SampleProfileWriterText(OS));
  288. else if (Format == SPF_GCC)
  289. EC = sampleprof_error::unsupported_writing_format;
  290. else
  291. EC = sampleprof_error::unrecognized_format;
  292. if (EC)
  293. return EC;
  294. return std::move(Writer);
  295. }
  296. void SampleProfileWriter::computeSummary(
  297. const StringMap<FunctionSamples> &ProfileMap) {
  298. SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
  299. for (const auto &I : ProfileMap) {
  300. const FunctionSamples &Profile = I.second;
  301. Builder.addRecord(Profile);
  302. }
  303. Summary = Builder.getSummary();
  304. }