LLVM  7.0.0
SampleProfWriter.cpp
Go to the documentation of this file.
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 
22 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/ErrorOr.h"
27 #include "llvm/Support/LEB128.h"
28 #include "llvm/Support/MD5.h"
30 #include <algorithm>
31 #include <cstdint>
32 #include <memory>
33 #include <set>
34 #include <system_error>
35 #include <utility>
36 #include <vector>
37 
38 using namespace llvm;
39 using namespace sampleprof;
40 
41 std::error_code
43  if (std::error_code EC = writeHeader(ProfileMap))
44  return EC;
45 
46  // Sort the ProfileMap by total samples.
47  typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples;
48  std::vector<NameFunctionSamples> V;
49  for (const auto &I : ProfileMap)
50  V.push_back(std::make_pair(I.getKey(), &I.second));
51 
52  std::stable_sort(
53  V.begin(), V.end(),
54  [](const NameFunctionSamples &A, const NameFunctionSamples &B) {
55  if (A.second->getTotalSamples() == B.second->getTotalSamples())
56  return A.first > B.first;
57  return A.second->getTotalSamples() > B.second->getTotalSamples();
58  });
59 
60  for (const auto &I : V) {
61  if (std::error_code EC = write(*I.second))
62  return EC;
63  }
65 }
66 
67 /// Write samples to a text file.
68 ///
69 /// Note: it may be tempting to implement this in terms of
70 /// FunctionSamples::print(). Please don't. The dump functionality is intended
71 /// for debugging and has no specified form.
72 ///
73 /// The format used here is more structured and deliberate because
74 /// it needs to be parsed by the SampleProfileReaderText class.
76  auto &OS = *OutputStream;
77  OS << S.getName() << ":" << S.getTotalSamples();
78  if (Indent == 0)
79  OS << ":" << S.getHeadSamples();
80  OS << "\n";
81 
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 
92  OS << Sample.getSamples();
93 
94  for (const auto &J : Sample.getCallTargets())
95  OS << " " << J.first() << ":" << J.second;
96  OS << "\n";
97  }
98 
100  S.getCallsiteSamples());
101  Indent += 1;
102  for (const auto &I : SortedCallsiteSamples.get())
103  for (const auto &FS : I->second) {
104  LineLocation Loc = I->first;
105  const FunctionSamples &CalleeSamples = FS.second;
106  OS.indent(Indent);
107  if (Loc.Discriminator == 0)
108  OS << Loc.LineOffset << ": ";
109  else
110  OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
111  if (std::error_code EC = write(CalleeSamples))
112  return EC;
113  }
114  Indent -= 1;
115 
117 }
118 
120  const auto &ret = NameTable.find(FName);
121  if (ret == NameTable.end())
123  encodeULEB128(ret->second, *OutputStream);
125 }
126 
127 void SampleProfileWriterBinary::addName(StringRef FName) {
128  NameTable.insert(std::make_pair(FName, 0));
129 }
130 
131 void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
132  // Add all the names in indirect call targets.
133  for (const auto &I : S.getBodySamples()) {
134  const SampleRecord &Sample = I.second;
135  for (const auto &J : Sample.getCallTargets())
136  addName(J.first());
137  }
138 
139  // Recursively add all the names for inlined callsites.
140  for (const auto &J : S.getCallsiteSamples())
141  for (const auto &FS : J.second) {
142  const FunctionSamples &CalleeSamples = FS.second;
143  addName(CalleeSamples.getName());
144  addNames(CalleeSamples);
145  }
146 }
147 
148 void SampleProfileWriterBinary::stablizeNameTable(std::set<StringRef> &V) {
149  // Sort the names to make NameTable deterministic.
150  for (const auto &I : NameTable)
151  V.insert(I.first);
152  int i = 0;
153  for (const StringRef &N : V)
154  NameTable[N] = i++;
155 }
156 
158  auto &OS = *OutputStream;
159  std::set<StringRef> V;
160  stablizeNameTable(V);
161 
162  // Write out the name table.
163  encodeULEB128(NameTable.size(), OS);
164  for (auto N : V) {
165  OS << N;
166  encodeULEB128(0, OS);
167  }
169 }
170 
172  auto &OS = *OutputStream;
173  std::set<StringRef> V;
174  stablizeNameTable(V);
175 
176  // Write out the name table.
177  encodeULEB128(NameTable.size(), OS);
178  for (auto N : V) {
179  encodeULEB128(MD5Hash(N), OS);
180  }
182 }
183 
185  auto &OS = *OutputStream;
186  // Write file magic identifier.
187  encodeULEB128(SPMagic(), OS);
188  encodeULEB128(SPVersion(), OS);
190 }
191 
193  auto &OS = *OutputStream;
194  // Write file magic identifier.
196  encodeULEB128(SPVersion(), OS);
198 }
199 
201  const StringMap<FunctionSamples> &ProfileMap) {
202  writeMagicIdent();
203 
204  computeSummary(ProfileMap);
205  if (auto EC = writeSummary())
206  return EC;
207 
208  // Generate the name table for all the functions referenced in the profile.
209  for (const auto &I : ProfileMap) {
210  addName(I.first());
211  addNames(I.second);
212  }
213 
214  writeNameTable();
216 }
217 
219  auto &OS = *OutputStream;
220  encodeULEB128(Summary->getTotalCount(), OS);
221  encodeULEB128(Summary->getMaxCount(), OS);
222  encodeULEB128(Summary->getMaxFunctionCount(), OS);
223  encodeULEB128(Summary->getNumCounts(), OS);
224  encodeULEB128(Summary->getNumFunctions(), OS);
225  std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary();
226  encodeULEB128(Entries.size(), OS);
227  for (auto Entry : Entries) {
228  encodeULEB128(Entry.Cutoff, OS);
229  encodeULEB128(Entry.MinCount, OS);
230  encodeULEB128(Entry.NumCounts, OS);
231  }
233 }
235  auto &OS = *OutputStream;
236 
237  if (std::error_code EC = writeNameIdx(S.getName()))
238  return EC;
239 
241 
242  // Emit all the body samples.
243  encodeULEB128(S.getBodySamples().size(), OS);
244  for (const auto &I : S.getBodySamples()) {
245  LineLocation Loc = I.first;
246  const SampleRecord &Sample = I.second;
247  encodeULEB128(Loc.LineOffset, OS);
248  encodeULEB128(Loc.Discriminator, OS);
249  encodeULEB128(Sample.getSamples(), OS);
250  encodeULEB128(Sample.getCallTargets().size(), OS);
251  for (const auto &J : Sample.getCallTargets()) {
252  StringRef Callee = J.first();
253  uint64_t CalleeSamples = J.second;
254  if (std::error_code EC = writeNameIdx(Callee))
255  return EC;
256  encodeULEB128(CalleeSamples, OS);
257  }
258  }
259 
260  // Recursively emit all the callsite samples.
261  uint64_t NumCallsites = 0;
262  for (const auto &J : S.getCallsiteSamples())
263  NumCallsites += J.second.size();
264  encodeULEB128(NumCallsites, OS);
265  for (const auto &J : S.getCallsiteSamples())
266  for (const auto &FS : J.second) {
267  LineLocation Loc = J.first;
268  const FunctionSamples &CalleeSamples = FS.second;
269  encodeULEB128(Loc.LineOffset, OS);
270  encodeULEB128(Loc.Discriminator, OS);
271  if (std::error_code EC = writeBody(CalleeSamples))
272  return EC;
273  }
274 
276 }
277 
278 /// Write samples of a top-level function to a binary file.
279 ///
280 /// \returns true if the samples were written successfully, false otherwise.
283  return writeBody(S);
284 }
285 
286 /// Create a sample profile file writer based on the specified format.
287 ///
288 /// \param Filename The file to create.
289 ///
290 /// \param Format Encoding format for the profile file.
291 ///
292 /// \returns an error code indicating the status of the created writer.
295  std::error_code EC;
296  std::unique_ptr<raw_ostream> OS;
297  if (Format == SPF_Binary || Format == SPF_Compact_Binary)
298  OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_None));
299  else
300  OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_Text));
301  if (EC)
302  return EC;
303 
304  return create(OS, Format);
305 }
306 
307 /// Create a sample profile stream writer based on the specified format.
308 ///
309 /// \param OS The output stream to store the profile data to.
310 ///
311 /// \param Format Encoding format for the profile file.
312 ///
313 /// \returns an error code indicating the status of the created writer.
315 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
316  SampleProfileFormat Format) {
317  std::error_code EC;
318  std::unique_ptr<SampleProfileWriter> Writer;
319 
320  if (Format == SPF_Binary)
321  Writer.reset(new SampleProfileWriterRawBinary(OS));
322  else if (Format == SPF_Compact_Binary)
323  Writer.reset(new SampleProfileWriterCompactBinary(OS));
324  else if (Format == SPF_Text)
325  Writer.reset(new SampleProfileWriterText(OS));
326  else if (Format == SPF_GCC)
328  else
330 
331  if (EC)
332  return EC;
333 
334  return std::move(Writer);
335 }
336 
338  const StringMap<FunctionSamples> &ProfileMap) {
340  for (const auto &I : ProfileMap) {
341  const FunctionSamples &Profile = I.second;
342  Builder.addRecord(Profile);
343  }
344  Summary = Builder.getSummary();
345 }
virtual std::error_code writeMagicIdent() override
Represents either an error or a value T.
Definition: ErrorOr.h:57
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
uint64_t MD5Hash(StringRef Str)
Helper to compute and return lower 64 bits of the given string&#39;s MD5 hash.
Definition: MD5.h:109
std::unique_ptr< raw_ostream > OutputStream
Output stream where to emit the profile to.
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:65
const StringRef & getName() const
Return the function name.
Definition: SampleProf.h:403
std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap) override
Write a file header for the profile file.
Representation of the samples collected for a function.
Definition: SampleProf.h:216
std::error_code writeNameIdx(StringRef FName)
unsigned size() const
Definition: StringMap.h:112
virtual std::error_code writeMagicIdent() override
virtual std::error_code writeNameTable() override
Representation of a single sample record.
Definition: SampleProf.h:144
static uint64_t SPVersion()
Definition: SampleProf.h:106
amdgpu Simplify well known AMD library false Value * Callee
Sort a LocationT->SampleT map by LocationT.
Definition: SampleProf.h:467
uint64_t getHeadSamples() const
Return the total number of branch samples that have the function as the branch target.
Definition: SampleProf.h:324
virtual std::error_code writeNameTable() override
std::unique_ptr< ProfileSummary > Summary
Profile summary.
uint64_t getSamples() const
Definition: SampleProf.h:180
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
Definition: SampleProf.h:347
std::error_code write(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
void stablizeNameTable(std::set< StringRef > &V)
virtual std::error_code write(const FunctionSamples &S)=0
Write sample profiles in S.
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:81
static ErrorOr< std::unique_ptr< SampleProfileWriter > > create(StringRef Filename, SampleProfileFormat Format)
Profile writer factory.
void addRecord(const sampleprof::FunctionSamples &FS)
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
std::error_code writeBody(const FunctionSamples &S)
std::unique_ptr< ProfileSummary > getSummary()
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:366
Represents the relative location of an instruction.
Definition: SampleProf.h:117
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
const CallTargetMap & getCallTargets() const
Definition: SampleProf.h:181
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
Definition: SampleProf.h:89
Provides ErrorOr<T> smart pointer.
virtual std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap)=0
Write a file header for the profile file.
Definition: JSON.cpp:592
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
Definition: SampleProf.h:317
const CallsiteSampleMap & getCallsiteSamples() const
Return all the callsite samples collected in the body of the function.
Definition: SampleProf.h:350
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void computeSummary(const StringMap< FunctionSamples > &ProfileMap)
Compute summary for this profile.
std::error_code write(const FunctionSamples &S) override
Write samples to a text file.
Sample-based profile writer (text format).