blob: b4de30118b8ba2f9d0cb5f3424d431440ed02fb9 [file] [log] [blame]
Diego Novilloc572e922014-10-30 18:00:06 +00001//===- 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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/ProfileData/SampleProfWriter.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000022#include "llvm/ADT/StringRef.h"
23#include "llvm/ProfileData/ProfileCommon.h"
24#include "llvm/ProfileData/SampleProf.h"
Diego Novilloc572e922014-10-30 18:00:06 +000025#include "llvm/Support/ErrorOr.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000026#include "llvm/Support/FileSystem.h"
Diego Novilloc572e922014-10-30 18:00:06 +000027#include "llvm/Support/LEB128.h"
Wei Mia0c08572018-06-11 22:40:43 +000028#include "llvm/Support/MD5.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000029#include "llvm/Support/raw_ostream.h"
30#include <algorithm>
31#include <cstdint>
32#include <memory>
Dehao Chen8d1c9832017-05-11 23:43:44 +000033#include <set>
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000034#include <system_error>
35#include <utility>
36#include <vector>
Diego Novilloc572e922014-10-30 18:00:06 +000037
Diego Novilloc572e922014-10-30 18:00:06 +000038using namespace llvm;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000039using namespace sampleprof;
Diego Novilloc572e922014-10-30 18:00:06 +000040
Dehao Chen8d1c9832017-05-11 23:43:44 +000041std::error_code
42SampleProfileWriter::write(const StringMap<FunctionSamples> &ProfileMap) {
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 }
64 return sampleprof_error::success;
65}
66
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000067/// Write samples to a text file.
Diego Novillo8e415a82015-11-13 20:24:28 +000068///
69/// Note: it may be tempting to implement this in terms of
Diego Novilloef548d22015-11-19 15:33:08 +000070/// FunctionSamples::print(). Please don't. The dump functionality is intended
Diego Novillo8e415a82015-11-13 20:24:28 +000071/// 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.
Dehao Chen57d1dda2016-03-03 18:09:32 +000075std::error_code SampleProfileWriterText::write(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +000076 auto &OS = *OutputStream;
Dehao Chen57d1dda2016-03-03 18:09:32 +000077 OS << S.getName() << ":" << S.getTotalSamples();
Diego Novilloaae1ed82015-10-08 19:40:37 +000078 if (Indent == 0)
79 OS << ":" << S.getHeadSamples();
80 OS << "\n";
Diego Novilloc572e922014-10-30 18:00:06 +000081
Diego Novilloef548d22015-11-19 15:33:08 +000082 SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
83 for (const auto &I : SortedSamples.get()) {
84 LineLocation Loc = I->first;
85 const SampleRecord &Sample = I->second;
Diego Novilloaae1ed82015-10-08 19:40:37 +000086 OS.indent(Indent + 1);
Diego Novilloc572e922014-10-30 18:00:06 +000087 if (Loc.Discriminator == 0)
88 OS << Loc.LineOffset << ": ";
89 else
90 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
91
92 OS << Sample.getSamples();
93
Diego Novillod5336ae2014-11-01 00:56:55 +000094 for (const auto &J : Sample.getCallTargets())
95 OS << " " << J.first() << ":" << J.second;
Diego Novilloc572e922014-10-30 18:00:06 +000096 OS << "\n";
97 }
98
Dehao Chen2c7ca9b2017-04-13 19:52:10 +000099 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novilloef548d22015-11-19 15:33:08 +0000100 S.getCallsiteSamples());
Diego Novilloaae1ed82015-10-08 19:40:37 +0000101 Indent += 1;
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000102 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 }
Diego Novilloaae1ed82015-10-08 19:40:37 +0000114 Indent -= 1;
115
Diego Novillo760c5a82015-10-13 22:48:46 +0000116 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000117}
118
Diego Novillo760c5a82015-10-13 22:48:46 +0000119std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
120 const auto &ret = NameTable.find(FName);
121 if (ret == NameTable.end())
122 return sampleprof_error::truncated_name_table;
Nathan Slingerland51abea72015-12-10 17:21:42 +0000123 encodeULEB128(ret->second, *OutputStream);
Diego Novillo760c5a82015-10-13 22:48:46 +0000124 return sampleprof_error::success;
125}
Diego Novilloc572e922014-10-30 18:00:06 +0000126
Diego Novillo760c5a82015-10-13 22:48:46 +0000127void SampleProfileWriterBinary::addName(StringRef FName) {
Dehao Chen8d1c9832017-05-11 23:43:44 +0000128 NameTable.insert(std::make_pair(FName, 0));
Diego Novillo760c5a82015-10-13 22:48:46 +0000129}
130
131void 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.
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000140 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 }
Diego Novillo760c5a82015-10-13 22:48:46 +0000146}
147
Wei Mia0c08572018-06-11 22:40:43 +0000148void 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}
Nathan Slingerland51abea72015-12-10 17:21:42 +0000156
Wei Mia0c08572018-06-11 22:40:43 +0000157std::error_code SampleProfileWriterRawBinary::writeNameTable() {
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 }
168 return sampleprof_error::success;
169}
170
171std::error_code SampleProfileWriterCompactBinary::writeNameTable() {
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 }
181 return sampleprof_error::success;
182}
183
184std::error_code SampleProfileWriterRawBinary::writeMagicIdent() {
185 auto &OS = *OutputStream;
Diego Novillo760c5a82015-10-13 22:48:46 +0000186 // Write file magic identifier.
Diego Novilloc572e922014-10-30 18:00:06 +0000187 encodeULEB128(SPMagic(), OS);
188 encodeULEB128(SPVersion(), OS);
Wei Mia0c08572018-06-11 22:40:43 +0000189 return sampleprof_error::success;
190}
191
192std::error_code SampleProfileWriterCompactBinary::writeMagicIdent() {
193 auto &OS = *OutputStream;
194 // Write file magic identifier.
195 encodeULEB128(SPMagic(SPF_Compact_Binary), OS);
196 encodeULEB128(SPVersion(), OS);
197 return sampleprof_error::success;
198}
199
200std::error_code SampleProfileWriterBinary::writeHeader(
201 const StringMap<FunctionSamples> &ProfileMap) {
202 writeMagicIdent();
Diego Novillo760c5a82015-10-13 22:48:46 +0000203
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000204 computeSummary(ProfileMap);
205 if (auto EC = writeSummary())
206 return EC;
207
Diego Novillo760c5a82015-10-13 22:48:46 +0000208 // 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
Wei Mia0c08572018-06-11 22:40:43 +0000214 writeNameTable();
Diego Novillo760c5a82015-10-13 22:48:46 +0000215 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000216}
217
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000218std::error_code SampleProfileWriterBinary::writeSummary() {
219 auto &OS = *OutputStream;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000220 encodeULEB128(Summary->getTotalCount(), OS);
221 encodeULEB128(Summary->getMaxCount(), OS);
Easwaran Raman6f4903d2016-03-28 23:14:29 +0000222 encodeULEB128(Summary->getMaxFunctionCount(), OS);
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000223 encodeULEB128(Summary->getNumCounts(), OS);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000224 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 }
232 return sampleprof_error::success;
233}
Dehao Chen57d1dda2016-03-03 18:09:32 +0000234std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000235 auto &OS = *OutputStream;
236
Dehao Chen57d1dda2016-03-03 18:09:32 +0000237 if (std::error_code EC = writeNameIdx(S.getName()))
Diego Novillo760c5a82015-10-13 22:48:46 +0000238 return EC;
239
Diego Novilloc572e922014-10-30 18:00:06 +0000240 encodeULEB128(S.getTotalSamples(), OS);
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000241
242 // Emit all the body samples.
Diego Novillob93483d2015-10-16 18:54:35 +0000243 encodeULEB128(S.getBodySamples().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000244 for (const auto &I : S.getBodySamples()) {
245 LineLocation Loc = I.first;
246 const SampleRecord &Sample = I.second;
Diego Novilloc572e922014-10-30 18:00:06 +0000247 encodeULEB128(Loc.LineOffset, OS);
248 encodeULEB128(Loc.Discriminator, OS);
249 encodeULEB128(Sample.getSamples(), OS);
250 encodeULEB128(Sample.getCallTargets().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000251 for (const auto &J : Sample.getCallTargets()) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000252 StringRef Callee = J.first();
Diego Novillo38be3332015-10-15 16:36:21 +0000253 uint64_t CalleeSamples = J.second;
Diego Novillo760c5a82015-10-13 22:48:46 +0000254 if (std::error_code EC = writeNameIdx(Callee))
255 return EC;
Diego Novilloc572e922014-10-30 18:00:06 +0000256 encodeULEB128(CalleeSamples, OS);
257 }
258 }
259
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000260 // Recursively emit all the callsite samples.
Dehao Chen2c27daf2017-08-03 00:09:18 +0000261 uint64_t NumCallsites = 0;
262 for (const auto &J : S.getCallsiteSamples())
263 NumCallsites += J.second.size();
264 encodeULEB128(NumCallsites, OS);
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000265 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 }
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000274
Diego Novillo760c5a82015-10-13 22:48:46 +0000275 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000276}
Diego Novillod5336ae2014-11-01 00:56:55 +0000277
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000278/// Write samples of a top-level function to a binary file.
Diego Novillob93483d2015-10-16 18:54:35 +0000279///
280/// \returns true if the samples were written successfully, false otherwise.
Dehao Chen57d1dda2016-03-03 18:09:32 +0000281std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000282 encodeULEB128(S.getHeadSamples(), *OutputStream);
Dehao Chen57d1dda2016-03-03 18:09:32 +0000283 return writeBody(S);
Diego Novillob93483d2015-10-16 18:54:35 +0000284}
285
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000286/// Create a sample profile file writer based on the specified format.
Diego Novillod5336ae2014-11-01 00:56:55 +0000287///
288/// \param Filename The file to create.
289///
Diego Novillod5336ae2014-11-01 00:56:55 +0000290/// \param Format Encoding format for the profile file.
291///
292/// \returns an error code indicating the status of the created writer.
Diego Novillofcd55602014-11-03 00:51:45 +0000293ErrorOr<std::unique_ptr<SampleProfileWriter>>
294SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000295 std::error_code EC;
Nathan Slingerland51abea72015-12-10 17:21:42 +0000296 std::unique_ptr<raw_ostream> OS;
Wei Mid9be2c72018-06-12 05:53:49 +0000297 if (Format == SPF_Binary || Format == SPF_Compact_Binary)
Nathan Slingerland51abea72015-12-10 17:21:42 +0000298 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000307/// Create a sample profile stream writer based on the specified format.
Nathan Slingerland51abea72015-12-10 17:21:42 +0000308///
309/// \param OS The output stream to store the profile data to.
310///
Nathan Slingerland51abea72015-12-10 17:21:42 +0000311/// \param Format Encoding format for the profile file.
312///
313/// \returns an error code indicating the status of the created writer.
314ErrorOr<std::unique_ptr<SampleProfileWriter>>
315SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
316 SampleProfileFormat Format) {
317 std::error_code EC;
Diego Novillofcd55602014-11-03 00:51:45 +0000318 std::unique_ptr<SampleProfileWriter> Writer;
Diego Novillod5336ae2014-11-01 00:56:55 +0000319
Wei Mid9be2c72018-06-12 05:53:49 +0000320 if (Format == SPF_Binary)
Wei Mia0c08572018-06-11 22:40:43 +0000321 Writer.reset(new SampleProfileWriterRawBinary(OS));
322 else if (Format == SPF_Compact_Binary)
323 Writer.reset(new SampleProfileWriterCompactBinary(OS));
Diego Novillod5336ae2014-11-01 00:56:55 +0000324 else if (Format == SPF_Text)
Nathan Slingerland51abea72015-12-10 17:21:42 +0000325 Writer.reset(new SampleProfileWriterText(OS));
Diego Novillo760c5a82015-10-13 22:48:46 +0000326 else if (Format == SPF_GCC)
327 EC = sampleprof_error::unsupported_writing_format;
Diego Novillod5336ae2014-11-01 00:56:55 +0000328 else
329 EC = sampleprof_error::unrecognized_format;
330
Diego Novillofcd55602014-11-03 00:51:45 +0000331 if (EC)
332 return EC;
333
334 return std::move(Writer);
Diego Novillod5336ae2014-11-01 00:56:55 +0000335}
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000336
337void SampleProfileWriter::computeSummary(
338 const StringMap<FunctionSamples> &ProfileMap) {
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000339 SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000340 for (const auto &I : ProfileMap) {
341 const FunctionSamples &Profile = I.second;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000342 Builder.addRecord(Profile);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000343 }
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000344 Summary = Builder.getSummary();
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000345}