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