SampleProfWriter.cpp 13 KB

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