blob: b45026140c99ae8f2519a57be653f21f12c97838 [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"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000028#include "llvm/Support/raw_ostream.h"
29#include <algorithm>
30#include <cstdint>
31#include <memory>
Dehao Chen8d1c9832017-05-11 23:43:44 +000032#include <set>
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000033#include <system_error>
34#include <utility>
35#include <vector>
Diego Novilloc572e922014-10-30 18:00:06 +000036
Diego Novilloc572e922014-10-30 18:00:06 +000037using namespace llvm;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000038using namespace sampleprof;
Diego Novilloc572e922014-10-30 18:00:06 +000039
Dehao Chen8d1c9832017-05-11 23:43:44 +000040std::error_code
41SampleProfileWriter::write(const StringMap<FunctionSamples> &ProfileMap) {
42 if (std::error_code EC = writeHeader(ProfileMap))
43 return EC;
44
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));
50
51 std::stable_sort(
52 V.begin(), V.end(),
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();
57 });
58
59 for (const auto &I : V) {
60 if (std::error_code EC = write(*I.second))
61 return EC;
62 }
63 return sampleprof_error::success;
64}
65
Diego Novilloc572e922014-10-30 18:00:06 +000066/// \brief Write samples to a text file.
Diego Novillo8e415a82015-11-13 20:24:28 +000067///
68/// Note: it may be tempting to implement this in terms of
Diego Novilloef548d22015-11-19 15:33:08 +000069/// FunctionSamples::print(). Please don't. The dump functionality is intended
Diego Novillo8e415a82015-11-13 20:24:28 +000070/// for debugging and has no specified form.
71///
72/// The format used here is more structured and deliberate because
73/// it needs to be parsed by the SampleProfileReaderText class.
Dehao Chen57d1dda2016-03-03 18:09:32 +000074std::error_code SampleProfileWriterText::write(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +000075 auto &OS = *OutputStream;
Dehao Chen57d1dda2016-03-03 18:09:32 +000076 OS << S.getName() << ":" << S.getTotalSamples();
Diego Novilloaae1ed82015-10-08 19:40:37 +000077 if (Indent == 0)
78 OS << ":" << S.getHeadSamples();
79 OS << "\n";
Diego Novilloc572e922014-10-30 18:00:06 +000080
Diego Novilloef548d22015-11-19 15:33:08 +000081 SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
82 for (const auto &I : SortedSamples.get()) {
83 LineLocation Loc = I->first;
84 const SampleRecord &Sample = I->second;
Diego Novilloaae1ed82015-10-08 19:40:37 +000085 OS.indent(Indent + 1);
Diego Novilloc572e922014-10-30 18:00:06 +000086 if (Loc.Discriminator == 0)
87 OS << Loc.LineOffset << ": ";
88 else
89 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
90
91 OS << Sample.getSamples();
92
Diego Novillod5336ae2014-11-01 00:56:55 +000093 for (const auto &J : Sample.getCallTargets())
94 OS << " " << J.first() << ":" << J.second;
Diego Novilloc572e922014-10-30 18:00:06 +000095 OS << "\n";
96 }
97
Dehao Chen2c7ca9b2017-04-13 19:52:10 +000098 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novilloef548d22015-11-19 15:33:08 +000099 S.getCallsiteSamples());
Diego Novilloaae1ed82015-10-08 19:40:37 +0000100 Indent += 1;
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000101 for (const auto &I : SortedCallsiteSamples.get())
102 for (const auto &FS : I->second) {
103 LineLocation Loc = I->first;
104 const FunctionSamples &CalleeSamples = FS.second;
105 OS.indent(Indent);
106 if (Loc.Discriminator == 0)
107 OS << Loc.LineOffset << ": ";
108 else
109 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
110 if (std::error_code EC = write(CalleeSamples))
111 return EC;
112 }
Diego Novilloaae1ed82015-10-08 19:40:37 +0000113 Indent -= 1;
114
Diego Novillo760c5a82015-10-13 22:48:46 +0000115 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000116}
117
Diego Novillo760c5a82015-10-13 22:48:46 +0000118std::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;
Nathan Slingerland51abea72015-12-10 17:21:42 +0000122 encodeULEB128(ret->second, *OutputStream);
Diego Novillo760c5a82015-10-13 22:48:46 +0000123 return sampleprof_error::success;
124}
Diego Novilloc572e922014-10-30 18:00:06 +0000125
Diego Novillo760c5a82015-10-13 22:48:46 +0000126void SampleProfileWriterBinary::addName(StringRef FName) {
Dehao Chen8d1c9832017-05-11 23:43:44 +0000127 NameTable.insert(std::make_pair(FName, 0));
Diego Novillo760c5a82015-10-13 22:48:46 +0000128}
129
130void 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())
135 addName(J.first());
136 }
137
138 // Recursively add all the names for inlined callsites.
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000139 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);
144 }
Diego Novillo760c5a82015-10-13 22:48:46 +0000145}
146
147std::error_code SampleProfileWriterBinary::writeHeader(
148 const StringMap<FunctionSamples> &ProfileMap) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000149 auto &OS = *OutputStream;
150
Diego Novillo760c5a82015-10-13 22:48:46 +0000151 // Write file magic identifier.
Diego Novilloc572e922014-10-30 18:00:06 +0000152 encodeULEB128(SPMagic(), OS);
153 encodeULEB128(SPVersion(), OS);
Diego Novillo760c5a82015-10-13 22:48:46 +0000154
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000155 computeSummary(ProfileMap);
156 if (auto EC = writeSummary())
157 return EC;
158
Diego Novillo760c5a82015-10-13 22:48:46 +0000159 // Generate the name table for all the functions referenced in the profile.
160 for (const auto &I : ProfileMap) {
161 addName(I.first());
162 addNames(I.second);
163 }
164
Dehao Chen8d1c9832017-05-11 23:43:44 +0000165 // Sort the names to make NameTable is deterministic.
166 std::set<StringRef> V;
167 for (const auto &I : NameTable)
168 V.insert(I.first);
169 int i = 0;
170 for (const StringRef &N : V)
171 NameTable[N] = i++;
172
Diego Novillo760c5a82015-10-13 22:48:46 +0000173 // Write out the name table.
174 encodeULEB128(NameTable.size(), OS);
Dehao Chen8d1c9832017-05-11 23:43:44 +0000175 for (auto N : V) {
176 OS << N;
Diego Novillo760c5a82015-10-13 22:48:46 +0000177 encodeULEB128(0, OS);
178 }
Diego Novillo760c5a82015-10-13 22:48:46 +0000179 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000180}
181
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000182std::error_code SampleProfileWriterBinary::writeSummary() {
183 auto &OS = *OutputStream;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000184 encodeULEB128(Summary->getTotalCount(), OS);
185 encodeULEB128(Summary->getMaxCount(), OS);
Easwaran Raman6f4903d2016-03-28 23:14:29 +0000186 encodeULEB128(Summary->getMaxFunctionCount(), OS);
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000187 encodeULEB128(Summary->getNumCounts(), OS);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000188 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);
195 }
196 return sampleprof_error::success;
197}
Dehao Chen57d1dda2016-03-03 18:09:32 +0000198std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000199 auto &OS = *OutputStream;
200
Dehao Chen57d1dda2016-03-03 18:09:32 +0000201 if (std::error_code EC = writeNameIdx(S.getName()))
Diego Novillo760c5a82015-10-13 22:48:46 +0000202 return EC;
203
Diego Novilloc572e922014-10-30 18:00:06 +0000204 encodeULEB128(S.getTotalSamples(), OS);
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000205
206 // Emit all the body samples.
Diego Novillob93483d2015-10-16 18:54:35 +0000207 encodeULEB128(S.getBodySamples().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000208 for (const auto &I : S.getBodySamples()) {
209 LineLocation Loc = I.first;
210 const SampleRecord &Sample = I.second;
Diego Novilloc572e922014-10-30 18:00:06 +0000211 encodeULEB128(Loc.LineOffset, OS);
212 encodeULEB128(Loc.Discriminator, OS);
213 encodeULEB128(Sample.getSamples(), OS);
214 encodeULEB128(Sample.getCallTargets().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000215 for (const auto &J : Sample.getCallTargets()) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000216 StringRef Callee = J.first();
Diego Novillo38be3332015-10-15 16:36:21 +0000217 uint64_t CalleeSamples = J.second;
Diego Novillo760c5a82015-10-13 22:48:46 +0000218 if (std::error_code EC = writeNameIdx(Callee))
219 return EC;
Diego Novilloc572e922014-10-30 18:00:06 +0000220 encodeULEB128(CalleeSamples, OS);
221 }
222 }
223
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000224 // Recursively emit all the callsite samples.
225 encodeULEB128(S.getCallsiteSamples().size(), OS);
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000226 for (const auto &J : S.getCallsiteSamples())
227 for (const auto &FS : J.second) {
228 LineLocation Loc = J.first;
229 const FunctionSamples &CalleeSamples = FS.second;
230 encodeULEB128(Loc.LineOffset, OS);
231 encodeULEB128(Loc.Discriminator, OS);
232 if (std::error_code EC = writeBody(CalleeSamples))
233 return EC;
234 }
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000235
Diego Novillo760c5a82015-10-13 22:48:46 +0000236 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000237}
Diego Novillod5336ae2014-11-01 00:56:55 +0000238
Diego Novillob93483d2015-10-16 18:54:35 +0000239/// \brief Write samples of a top-level function to a binary file.
240///
241/// \returns true if the samples were written successfully, false otherwise.
Dehao Chen57d1dda2016-03-03 18:09:32 +0000242std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000243 encodeULEB128(S.getHeadSamples(), *OutputStream);
Dehao Chen57d1dda2016-03-03 18:09:32 +0000244 return writeBody(S);
Diego Novillob93483d2015-10-16 18:54:35 +0000245}
246
Nathan Slingerland51abea72015-12-10 17:21:42 +0000247/// \brief Create a sample profile file writer based on the specified format.
Diego Novillod5336ae2014-11-01 00:56:55 +0000248///
249/// \param Filename The file to create.
250///
251/// \param Writer The writer to instantiate according to the specified format.
252///
253/// \param Format Encoding format for the profile file.
254///
255/// \returns an error code indicating the status of the created writer.
Diego Novillofcd55602014-11-03 00:51:45 +0000256ErrorOr<std::unique_ptr<SampleProfileWriter>>
257SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000258 std::error_code EC;
Nathan Slingerland51abea72015-12-10 17:21:42 +0000259 std::unique_ptr<raw_ostream> OS;
260 if (Format == SPF_Binary)
261 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_None));
262 else
263 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_Text));
264 if (EC)
265 return EC;
266
267 return create(OS, Format);
268}
269
270/// \brief Create a sample profile stream writer based on the specified format.
271///
272/// \param OS The output stream to store the profile data to.
273///
274/// \param Writer The writer to instantiate according to the specified format.
275///
276/// \param Format Encoding format for the profile file.
277///
278/// \returns an error code indicating the status of the created writer.
279ErrorOr<std::unique_ptr<SampleProfileWriter>>
280SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
281 SampleProfileFormat Format) {
282 std::error_code EC;
Diego Novillofcd55602014-11-03 00:51:45 +0000283 std::unique_ptr<SampleProfileWriter> Writer;
Diego Novillod5336ae2014-11-01 00:56:55 +0000284
285 if (Format == SPF_Binary)
Nathan Slingerland51abea72015-12-10 17:21:42 +0000286 Writer.reset(new SampleProfileWriterBinary(OS));
Diego Novillod5336ae2014-11-01 00:56:55 +0000287 else if (Format == SPF_Text)
Nathan Slingerland51abea72015-12-10 17:21:42 +0000288 Writer.reset(new SampleProfileWriterText(OS));
Diego Novillo760c5a82015-10-13 22:48:46 +0000289 else if (Format == SPF_GCC)
290 EC = sampleprof_error::unsupported_writing_format;
Diego Novillod5336ae2014-11-01 00:56:55 +0000291 else
292 EC = sampleprof_error::unrecognized_format;
293
Diego Novillofcd55602014-11-03 00:51:45 +0000294 if (EC)
295 return EC;
296
297 return std::move(Writer);
Diego Novillod5336ae2014-11-01 00:56:55 +0000298}
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000299
300void SampleProfileWriter::computeSummary(
301 const StringMap<FunctionSamples> &ProfileMap) {
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000302 SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000303 for (const auto &I : ProfileMap) {
304 const FunctionSamples &Profile = I.second;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000305 Builder.addRecord(Profile);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000306 }
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000307 Summary = Builder.getSummary();
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000308}