1 //===- SampleProfWriter.cpp - Write LLVM sample profile data --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
16 // See lib/ProfileData/SampleProfReader.cpp for documentation on each of the
19 //===----------------------------------------------------------------------===//
21 #include "llvm/ProfileData/SampleProfWriter.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ProfileData/ProfileCommon.h"
24 #include "llvm/ProfileData/SampleProf.h"
25 #include "llvm/Support/ErrorOr.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/LEB128.h"
28 #include "llvm/Support/raw_ostream.h"
33 #include <system_error>
38 using namespace sampleprof;
41 SampleProfileWriter::write(const StringMap<FunctionSamples> &ProfileMap) {
42 if (std::error_code EC = writeHeader(ProfileMap))
45 // Sort the ProfileMap by total samples.
46 typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples;
47 std::vector<NameFunctionSamples> V;
48 for (const auto &I : ProfileMap)
49 V.push_back(std::make_pair(I.getKey(), &I.second));
53 [](const NameFunctionSamples &A, const NameFunctionSamples &B) {
54 if (A.second->getTotalSamples() == B.second->getTotalSamples())
55 return A.first > B.first;
56 return A.second->getTotalSamples() > B.second->getTotalSamples();
59 for (const auto &I : V) {
60 if (std::error_code EC = write(*I.second))
63 return sampleprof_error::success;
66 /// \brief Write samples to a text file.
68 /// Note: it may be tempting to implement this in terms of
69 /// FunctionSamples::print(). Please don't. The dump functionality is intended
70 /// for debugging and has no specified form.
72 /// The format used here is more structured and deliberate because
73 /// it needs to be parsed by the SampleProfileReaderText class.
74 std::error_code SampleProfileWriterText::write(const FunctionSamples &S) {
75 auto &OS = *OutputStream;
76 OS << S.getName() << ":" << S.getTotalSamples();
78 OS << ":" << S.getHeadSamples();
81 SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
82 for (const auto &I : SortedSamples.get()) {
83 LineLocation Loc = I->first;
84 const SampleRecord &Sample = I->second;
85 OS.indent(Indent + 1);
86 if (Loc.Discriminator == 0)
87 OS << Loc.LineOffset << ": ";
89 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
91 OS << Sample.getSamples();
93 for (const auto &J : Sample.getCallTargets())
94 OS << " " << J.first() << ":" << J.second;
98 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
99 S.getCallsiteSamples());
101 for (const auto &I : SortedCallsiteSamples.get())
102 for (const auto &FS : I->second) {
103 LineLocation Loc = I->first;
104 const FunctionSamples &CalleeSamples = FS.second;
106 if (Loc.Discriminator == 0)
107 OS << Loc.LineOffset << ": ";
109 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
110 if (std::error_code EC = write(CalleeSamples))
115 return sampleprof_error::success;
118 std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
119 const auto &ret = NameTable.find(FName);
120 if (ret == NameTable.end())
121 return sampleprof_error::truncated_name_table;
122 encodeULEB128(ret->second, *OutputStream);
123 return sampleprof_error::success;
126 void SampleProfileWriterBinary::addName(StringRef FName) {
127 NameTable.insert(std::make_pair(FName, 0));
130 void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
131 // Add all the names in indirect call targets.
132 for (const auto &I : S.getBodySamples()) {
133 const SampleRecord &Sample = I.second;
134 for (const auto &J : Sample.getCallTargets())
138 // Recursively add all the names for inlined callsites.
139 for (const auto &J : S.getCallsiteSamples())
140 for (const auto &FS : J.second) {
141 const FunctionSamples &CalleeSamples = FS.second;
142 addName(CalleeSamples.getName());
143 addNames(CalleeSamples);
147 std::error_code SampleProfileWriterBinary::writeHeader(
148 const StringMap<FunctionSamples> &ProfileMap) {
149 auto &OS = *OutputStream;
151 // Write file magic identifier.
152 encodeULEB128(SPMagic(), OS);
153 encodeULEB128(SPVersion(), OS);
155 computeSummary(ProfileMap);
156 if (auto EC = writeSummary())
159 // Generate the name table for all the functions referenced in the profile.
160 for (const auto &I : ProfileMap) {
165 // Sort the names to make NameTable is deterministic.
166 std::set<StringRef> V;
167 for (const auto &I : NameTable)
170 for (const StringRef &N : V)
173 // Write out the name table.
174 encodeULEB128(NameTable.size(), OS);
177 encodeULEB128(0, OS);
179 return sampleprof_error::success;
182 std::error_code SampleProfileWriterBinary::writeSummary() {
183 auto &OS = *OutputStream;
184 encodeULEB128(Summary->getTotalCount(), OS);
185 encodeULEB128(Summary->getMaxCount(), OS);
186 encodeULEB128(Summary->getMaxFunctionCount(), OS);
187 encodeULEB128(Summary->getNumCounts(), OS);
188 encodeULEB128(Summary->getNumFunctions(), OS);
189 std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary();
190 encodeULEB128(Entries.size(), OS);
191 for (auto Entry : Entries) {
192 encodeULEB128(Entry.Cutoff, OS);
193 encodeULEB128(Entry.MinCount, OS);
194 encodeULEB128(Entry.NumCounts, OS);
196 return sampleprof_error::success;
198 std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
199 auto &OS = *OutputStream;
201 if (std::error_code EC = writeNameIdx(S.getName()))
204 encodeULEB128(S.getTotalSamples(), OS);
206 // Emit all the body samples.
207 encodeULEB128(S.getBodySamples().size(), OS);
208 for (const auto &I : S.getBodySamples()) {
209 LineLocation Loc = I.first;
210 const SampleRecord &Sample = I.second;
211 encodeULEB128(Loc.LineOffset, OS);
212 encodeULEB128(Loc.Discriminator, OS);
213 encodeULEB128(Sample.getSamples(), OS);
214 encodeULEB128(Sample.getCallTargets().size(), OS);
215 for (const auto &J : Sample.getCallTargets()) {
216 StringRef Callee = J.first();
217 uint64_t CalleeSamples = J.second;
218 if (std::error_code EC = writeNameIdx(Callee))
220 encodeULEB128(CalleeSamples, OS);
224 // Recursively emit all the callsite samples.
225 uint64_t NumCallsites = 0;
226 for (const auto &J : S.getCallsiteSamples())
227 NumCallsites += J.second.size();
228 encodeULEB128(NumCallsites, OS);
229 for (const auto &J : S.getCallsiteSamples())
230 for (const auto &FS : J.second) {
231 LineLocation Loc = J.first;
232 const FunctionSamples &CalleeSamples = FS.second;
233 encodeULEB128(Loc.LineOffset, OS);
234 encodeULEB128(Loc.Discriminator, OS);
235 if (std::error_code EC = writeBody(CalleeSamples))
239 return sampleprof_error::success;
242 /// \brief Write samples of a top-level function to a binary file.
244 /// \returns true if the samples were written successfully, false otherwise.
245 std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) {
246 encodeULEB128(S.getHeadSamples(), *OutputStream);
250 /// \brief Create a sample profile file writer based on the specified format.
252 /// \param Filename The file to create.
254 /// \param Format Encoding format for the profile file.
256 /// \returns an error code indicating the status of the created writer.
257 ErrorOr<std::unique_ptr<SampleProfileWriter>>
258 SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
260 std::unique_ptr<raw_ostream> OS;
261 if (Format == SPF_Binary)
262 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_None));
264 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_Text));
268 return create(OS, Format);
271 /// \brief Create a sample profile stream writer based on the specified format.
273 /// \param OS The output stream to store the profile data to.
275 /// \param Format Encoding format for the profile file.
277 /// \returns an error code indicating the status of the created writer.
278 ErrorOr<std::unique_ptr<SampleProfileWriter>>
279 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
280 SampleProfileFormat Format) {
282 std::unique_ptr<SampleProfileWriter> Writer;
284 if (Format == SPF_Binary)
285 Writer.reset(new SampleProfileWriterBinary(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;
291 EC = sampleprof_error::unrecognized_format;
296 return std::move(Writer);
299 void SampleProfileWriter::computeSummary(
300 const StringMap<FunctionSamples> &ProfileMap) {
301 SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
302 for (const auto &I : ProfileMap) {
303 const FunctionSamples &Profile = I.second;
304 Builder.addRecord(Profile);
306 Summary = Builder.getSummary();