blob: 93ec2bbbb26923b3cde059033bc6ed2313d96324 [file] [log] [blame]
Diego Novilloc572e922014-10-30 18:00:06 +00001//===- SampleProfWriter.cpp - Write LLVM sample profile data --------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Diego Novilloc572e922014-10-30 18:00:06 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the class that writes LLVM sample profiles. It
10// supports two file formats: text and binary. The textual representation
11// is useful for debugging and testing purposes. The binary representation
12// is more compact, resulting in smaller file sizes. However, they can
13// both be used interchangeably.
14//
15// See lib/ProfileData/SampleProfReader.cpp for documentation on each of the
16// supported formats.
17//
18//===----------------------------------------------------------------------===//
19
Chandler Carruth6bda14b2017-06-06 11:49:48 +000020#include "llvm/ProfileData/SampleProfWriter.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000021#include "llvm/ADT/StringRef.h"
22#include "llvm/ProfileData/ProfileCommon.h"
23#include "llvm/ProfileData/SampleProf.h"
Wei Mi6a143252018-09-14 20:52:59 +000024#include "llvm/Support/Endian.h"
25#include "llvm/Support/EndianStream.h"
Diego Novilloc572e922014-10-30 18:00:06 +000026#include "llvm/Support/ErrorOr.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000027#include "llvm/Support/FileSystem.h"
Diego Novilloc572e922014-10-30 18:00:06 +000028#include "llvm/Support/LEB128.h"
Wei Mia0c08572018-06-11 22:40:43 +000029#include "llvm/Support/MD5.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000030#include "llvm/Support/raw_ostream.h"
31#include <algorithm>
32#include <cstdint>
33#include <memory>
Dehao Chen8d1c9832017-05-11 23:43:44 +000034#include <set>
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000035#include <system_error>
36#include <utility>
37#include <vector>
Diego Novilloc572e922014-10-30 18:00:06 +000038
Diego Novilloc572e922014-10-30 18:00:06 +000039using namespace llvm;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000040using namespace sampleprof;
Diego Novilloc572e922014-10-30 18:00:06 +000041
Dehao Chen8d1c9832017-05-11 23:43:44 +000042std::error_code
43SampleProfileWriter::write(const StringMap<FunctionSamples> &ProfileMap) {
44 if (std::error_code EC = writeHeader(ProfileMap))
45 return EC;
46
47 // Sort the ProfileMap by total samples.
48 typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples;
49 std::vector<NameFunctionSamples> V;
50 for (const auto &I : ProfileMap)
51 V.push_back(std::make_pair(I.getKey(), &I.second));
52
Fangrui Songefd94c52019-04-23 14:51:27 +000053 llvm::stable_sort(
54 V, [](const NameFunctionSamples &A, const NameFunctionSamples &B) {
Dehao Chen8d1c9832017-05-11 23:43:44 +000055 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
Wei Mi6a143252018-09-14 20:52:59 +000067std::error_code SampleProfileWriterCompactBinary::write(
68 const StringMap<FunctionSamples> &ProfileMap) {
69 if (std::error_code EC = SampleProfileWriter::write(ProfileMap))
70 return EC;
71 if (std::error_code EC = writeFuncOffsetTable())
72 return EC;
73 return sampleprof_error::success;
74}
75
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000076/// Write samples to a text file.
Diego Novillo8e415a82015-11-13 20:24:28 +000077///
78/// Note: it may be tempting to implement this in terms of
Diego Novilloef548d22015-11-19 15:33:08 +000079/// FunctionSamples::print(). Please don't. The dump functionality is intended
Diego Novillo8e415a82015-11-13 20:24:28 +000080/// for debugging and has no specified form.
81///
82/// The format used here is more structured and deliberate because
83/// it needs to be parsed by the SampleProfileReaderText class.
Dehao Chen57d1dda2016-03-03 18:09:32 +000084std::error_code SampleProfileWriterText::write(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +000085 auto &OS = *OutputStream;
Dehao Chen57d1dda2016-03-03 18:09:32 +000086 OS << S.getName() << ":" << S.getTotalSamples();
Diego Novilloaae1ed82015-10-08 19:40:37 +000087 if (Indent == 0)
88 OS << ":" << S.getHeadSamples();
89 OS << "\n";
Diego Novilloc572e922014-10-30 18:00:06 +000090
Diego Novilloef548d22015-11-19 15:33:08 +000091 SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
92 for (const auto &I : SortedSamples.get()) {
93 LineLocation Loc = I->first;
94 const SampleRecord &Sample = I->second;
Diego Novilloaae1ed82015-10-08 19:40:37 +000095 OS.indent(Indent + 1);
Diego Novilloc572e922014-10-30 18:00:06 +000096 if (Loc.Discriminator == 0)
97 OS << Loc.LineOffset << ": ";
98 else
99 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
100
101 OS << Sample.getSamples();
102
Diego Novillod5336ae2014-11-01 00:56:55 +0000103 for (const auto &J : Sample.getCallTargets())
104 OS << " " << J.first() << ":" << J.second;
Diego Novilloc572e922014-10-30 18:00:06 +0000105 OS << "\n";
106 }
107
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000108 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novilloef548d22015-11-19 15:33:08 +0000109 S.getCallsiteSamples());
Diego Novilloaae1ed82015-10-08 19:40:37 +0000110 Indent += 1;
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000111 for (const auto &I : SortedCallsiteSamples.get())
112 for (const auto &FS : I->second) {
113 LineLocation Loc = I->first;
114 const FunctionSamples &CalleeSamples = FS.second;
115 OS.indent(Indent);
116 if (Loc.Discriminator == 0)
117 OS << Loc.LineOffset << ": ";
118 else
119 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
120 if (std::error_code EC = write(CalleeSamples))
121 return EC;
122 }
Diego Novilloaae1ed82015-10-08 19:40:37 +0000123 Indent -= 1;
124
Diego Novillo760c5a82015-10-13 22:48:46 +0000125 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000126}
127
Diego Novillo760c5a82015-10-13 22:48:46 +0000128std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
129 const auto &ret = NameTable.find(FName);
130 if (ret == NameTable.end())
131 return sampleprof_error::truncated_name_table;
Nathan Slingerland51abea72015-12-10 17:21:42 +0000132 encodeULEB128(ret->second, *OutputStream);
Diego Novillo760c5a82015-10-13 22:48:46 +0000133 return sampleprof_error::success;
134}
Diego Novilloc572e922014-10-30 18:00:06 +0000135
Diego Novillo760c5a82015-10-13 22:48:46 +0000136void SampleProfileWriterBinary::addName(StringRef FName) {
Dehao Chen8d1c9832017-05-11 23:43:44 +0000137 NameTable.insert(std::make_pair(FName, 0));
Diego Novillo760c5a82015-10-13 22:48:46 +0000138}
139
140void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
141 // Add all the names in indirect call targets.
142 for (const auto &I : S.getBodySamples()) {
143 const SampleRecord &Sample = I.second;
144 for (const auto &J : Sample.getCallTargets())
145 addName(J.first());
146 }
147
148 // Recursively add all the names for inlined callsites.
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000149 for (const auto &J : S.getCallsiteSamples())
150 for (const auto &FS : J.second) {
151 const FunctionSamples &CalleeSamples = FS.second;
152 addName(CalleeSamples.getName());
153 addNames(CalleeSamples);
154 }
Diego Novillo760c5a82015-10-13 22:48:46 +0000155}
156
Wei Mia0c08572018-06-11 22:40:43 +0000157void SampleProfileWriterBinary::stablizeNameTable(std::set<StringRef> &V) {
158 // Sort the names to make NameTable deterministic.
159 for (const auto &I : NameTable)
160 V.insert(I.first);
161 int i = 0;
162 for (const StringRef &N : V)
163 NameTable[N] = i++;
164}
Nathan Slingerland51abea72015-12-10 17:21:42 +0000165
Wei Mia0c08572018-06-11 22:40:43 +0000166std::error_code SampleProfileWriterRawBinary::writeNameTable() {
167 auto &OS = *OutputStream;
168 std::set<StringRef> V;
169 stablizeNameTable(V);
170
171 // Write out the name table.
172 encodeULEB128(NameTable.size(), OS);
173 for (auto N : V) {
174 OS << N;
175 encodeULEB128(0, OS);
176 }
177 return sampleprof_error::success;
178}
179
Wei Mi6a143252018-09-14 20:52:59 +0000180std::error_code SampleProfileWriterCompactBinary::writeFuncOffsetTable() {
181 auto &OS = *OutputStream;
182
183 // Fill the slot remembered by TableOffset with the offset of FuncOffsetTable.
184 auto &OFS = static_cast<raw_fd_ostream &>(OS);
185 uint64_t FuncOffsetTableStart = OS.tell();
186 if (OFS.seek(TableOffset) == (uint64_t)-1)
187 return sampleprof_error::ostream_seek_unsupported;
188 support::endian::Writer Writer(*OutputStream, support::little);
189 Writer.write(FuncOffsetTableStart);
190 if (OFS.seek(FuncOffsetTableStart) == (uint64_t)-1)
191 return sampleprof_error::ostream_seek_unsupported;
192
193 // Write out the table size.
194 encodeULEB128(FuncOffsetTable.size(), OS);
195
196 // Write out FuncOffsetTable.
197 for (auto entry : FuncOffsetTable) {
198 writeNameIdx(entry.first);
199 encodeULEB128(entry.second, OS);
200 }
201 return sampleprof_error::success;
202}
203
Wei Mia0c08572018-06-11 22:40:43 +0000204std::error_code SampleProfileWriterCompactBinary::writeNameTable() {
205 auto &OS = *OutputStream;
206 std::set<StringRef> V;
207 stablizeNameTable(V);
208
209 // Write out the name table.
210 encodeULEB128(NameTable.size(), OS);
211 for (auto N : V) {
212 encodeULEB128(MD5Hash(N), OS);
213 }
214 return sampleprof_error::success;
215}
216
217std::error_code SampleProfileWriterRawBinary::writeMagicIdent() {
218 auto &OS = *OutputStream;
Diego Novillo760c5a82015-10-13 22:48:46 +0000219 // Write file magic identifier.
Diego Novilloc572e922014-10-30 18:00:06 +0000220 encodeULEB128(SPMagic(), OS);
221 encodeULEB128(SPVersion(), OS);
Wei Mia0c08572018-06-11 22:40:43 +0000222 return sampleprof_error::success;
223}
224
225std::error_code SampleProfileWriterCompactBinary::writeMagicIdent() {
226 auto &OS = *OutputStream;
227 // Write file magic identifier.
228 encodeULEB128(SPMagic(SPF_Compact_Binary), OS);
229 encodeULEB128(SPVersion(), OS);
230 return sampleprof_error::success;
231}
232
233std::error_code SampleProfileWriterBinary::writeHeader(
234 const StringMap<FunctionSamples> &ProfileMap) {
235 writeMagicIdent();
Diego Novillo760c5a82015-10-13 22:48:46 +0000236
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000237 computeSummary(ProfileMap);
238 if (auto EC = writeSummary())
239 return EC;
240
Diego Novillo760c5a82015-10-13 22:48:46 +0000241 // Generate the name table for all the functions referenced in the profile.
242 for (const auto &I : ProfileMap) {
243 addName(I.first());
244 addNames(I.second);
245 }
246
Wei Mia0c08572018-06-11 22:40:43 +0000247 writeNameTable();
Diego Novillo760c5a82015-10-13 22:48:46 +0000248 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000249}
250
Wei Mi6a143252018-09-14 20:52:59 +0000251std::error_code SampleProfileWriterCompactBinary::writeHeader(
252 const StringMap<FunctionSamples> &ProfileMap) {
253 support::endian::Writer Writer(*OutputStream, support::little);
254 if (auto EC = SampleProfileWriterBinary::writeHeader(ProfileMap))
255 return EC;
256
257 // Reserve a slot for the offset of function offset table. The slot will
258 // be populated with the offset of FuncOffsetTable later.
259 TableOffset = OutputStream->tell();
260 Writer.write(static_cast<uint64_t>(-2));
261 return sampleprof_error::success;
262}
263
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000264std::error_code SampleProfileWriterBinary::writeSummary() {
265 auto &OS = *OutputStream;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000266 encodeULEB128(Summary->getTotalCount(), OS);
267 encodeULEB128(Summary->getMaxCount(), OS);
Easwaran Raman6f4903d2016-03-28 23:14:29 +0000268 encodeULEB128(Summary->getMaxFunctionCount(), OS);
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000269 encodeULEB128(Summary->getNumCounts(), OS);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000270 encodeULEB128(Summary->getNumFunctions(), OS);
271 std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary();
272 encodeULEB128(Entries.size(), OS);
273 for (auto Entry : Entries) {
274 encodeULEB128(Entry.Cutoff, OS);
275 encodeULEB128(Entry.MinCount, OS);
276 encodeULEB128(Entry.NumCounts, OS);
277 }
278 return sampleprof_error::success;
279}
Dehao Chen57d1dda2016-03-03 18:09:32 +0000280std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000281 auto &OS = *OutputStream;
282
Dehao Chen57d1dda2016-03-03 18:09:32 +0000283 if (std::error_code EC = writeNameIdx(S.getName()))
Diego Novillo760c5a82015-10-13 22:48:46 +0000284 return EC;
285
Diego Novilloc572e922014-10-30 18:00:06 +0000286 encodeULEB128(S.getTotalSamples(), OS);
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000287
288 // Emit all the body samples.
Diego Novillob93483d2015-10-16 18:54:35 +0000289 encodeULEB128(S.getBodySamples().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000290 for (const auto &I : S.getBodySamples()) {
291 LineLocation Loc = I.first;
292 const SampleRecord &Sample = I.second;
Diego Novilloc572e922014-10-30 18:00:06 +0000293 encodeULEB128(Loc.LineOffset, OS);
294 encodeULEB128(Loc.Discriminator, OS);
295 encodeULEB128(Sample.getSamples(), OS);
296 encodeULEB128(Sample.getCallTargets().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000297 for (const auto &J : Sample.getCallTargets()) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000298 StringRef Callee = J.first();
Diego Novillo38be3332015-10-15 16:36:21 +0000299 uint64_t CalleeSamples = J.second;
Diego Novillo760c5a82015-10-13 22:48:46 +0000300 if (std::error_code EC = writeNameIdx(Callee))
301 return EC;
Diego Novilloc572e922014-10-30 18:00:06 +0000302 encodeULEB128(CalleeSamples, OS);
303 }
304 }
305
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000306 // Recursively emit all the callsite samples.
Dehao Chen2c27daf2017-08-03 00:09:18 +0000307 uint64_t NumCallsites = 0;
308 for (const auto &J : S.getCallsiteSamples())
309 NumCallsites += J.second.size();
310 encodeULEB128(NumCallsites, OS);
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000311 for (const auto &J : S.getCallsiteSamples())
312 for (const auto &FS : J.second) {
313 LineLocation Loc = J.first;
314 const FunctionSamples &CalleeSamples = FS.second;
315 encodeULEB128(Loc.LineOffset, OS);
316 encodeULEB128(Loc.Discriminator, OS);
317 if (std::error_code EC = writeBody(CalleeSamples))
318 return EC;
319 }
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000320
Diego Novillo760c5a82015-10-13 22:48:46 +0000321 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000322}
Diego Novillod5336ae2014-11-01 00:56:55 +0000323
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000324/// Write samples of a top-level function to a binary file.
Diego Novillob93483d2015-10-16 18:54:35 +0000325///
326/// \returns true if the samples were written successfully, false otherwise.
Dehao Chen57d1dda2016-03-03 18:09:32 +0000327std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000328 encodeULEB128(S.getHeadSamples(), *OutputStream);
Dehao Chen57d1dda2016-03-03 18:09:32 +0000329 return writeBody(S);
Diego Novillob93483d2015-10-16 18:54:35 +0000330}
331
Wei Mi6a143252018-09-14 20:52:59 +0000332std::error_code
333SampleProfileWriterCompactBinary::write(const FunctionSamples &S) {
334 uint64_t Offset = OutputStream->tell();
335 StringRef Name = S.getName();
336 FuncOffsetTable[Name] = Offset;
337 encodeULEB128(S.getHeadSamples(), *OutputStream);
338 return writeBody(S);
339}
340
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000341/// Create a sample profile file writer based on the specified format.
Diego Novillod5336ae2014-11-01 00:56:55 +0000342///
343/// \param Filename The file to create.
344///
Diego Novillod5336ae2014-11-01 00:56:55 +0000345/// \param Format Encoding format for the profile file.
346///
347/// \returns an error code indicating the status of the created writer.
Diego Novillofcd55602014-11-03 00:51:45 +0000348ErrorOr<std::unique_ptr<SampleProfileWriter>>
349SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000350 std::error_code EC;
Nathan Slingerland51abea72015-12-10 17:21:42 +0000351 std::unique_ptr<raw_ostream> OS;
Wei Mid9be2c72018-06-12 05:53:49 +0000352 if (Format == SPF_Binary || Format == SPF_Compact_Binary)
Fangrui Songd9b948b2019-08-05 05:43:48 +0000353 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_None));
Nathan Slingerland51abea72015-12-10 17:21:42 +0000354 else
Fangrui Songd9b948b2019-08-05 05:43:48 +0000355 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_Text));
Nathan Slingerland51abea72015-12-10 17:21:42 +0000356 if (EC)
357 return EC;
358
359 return create(OS, Format);
360}
361
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000362/// Create a sample profile stream writer based on the specified format.
Nathan Slingerland51abea72015-12-10 17:21:42 +0000363///
364/// \param OS The output stream to store the profile data to.
365///
Nathan Slingerland51abea72015-12-10 17:21:42 +0000366/// \param Format Encoding format for the profile file.
367///
368/// \returns an error code indicating the status of the created writer.
369ErrorOr<std::unique_ptr<SampleProfileWriter>>
370SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
371 SampleProfileFormat Format) {
372 std::error_code EC;
Diego Novillofcd55602014-11-03 00:51:45 +0000373 std::unique_ptr<SampleProfileWriter> Writer;
Diego Novillod5336ae2014-11-01 00:56:55 +0000374
Wei Mid9be2c72018-06-12 05:53:49 +0000375 if (Format == SPF_Binary)
Wei Mia0c08572018-06-11 22:40:43 +0000376 Writer.reset(new SampleProfileWriterRawBinary(OS));
377 else if (Format == SPF_Compact_Binary)
378 Writer.reset(new SampleProfileWriterCompactBinary(OS));
Diego Novillod5336ae2014-11-01 00:56:55 +0000379 else if (Format == SPF_Text)
Nathan Slingerland51abea72015-12-10 17:21:42 +0000380 Writer.reset(new SampleProfileWriterText(OS));
Diego Novillo760c5a82015-10-13 22:48:46 +0000381 else if (Format == SPF_GCC)
382 EC = sampleprof_error::unsupported_writing_format;
Diego Novillod5336ae2014-11-01 00:56:55 +0000383 else
384 EC = sampleprof_error::unrecognized_format;
385
Diego Novillofcd55602014-11-03 00:51:45 +0000386 if (EC)
387 return EC;
388
389 return std::move(Writer);
Diego Novillod5336ae2014-11-01 00:56:55 +0000390}
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000391
392void SampleProfileWriter::computeSummary(
393 const StringMap<FunctionSamples> &ProfileMap) {
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000394 SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000395 for (const auto &I : ProfileMap) {
396 const FunctionSamples &Profile = I.second;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000397 Builder.addRecord(Profile);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000398 }
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000399 Summary = Builder.getSummary();
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000400}