blob: b91b6fb7c7add419b3600b0227952a8078d9f70d [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
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"
Diego Novilloc572e922014-10-30 18:00:06 +000024#include "llvm/ProfileData/SampleProfWriter.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>
32#include <system_error>
33#include <utility>
34#include <vector>
Diego Novilloc572e922014-10-30 18:00:06 +000035
Diego Novilloc572e922014-10-30 18:00:06 +000036using namespace llvm;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000037using namespace sampleprof;
Diego Novilloc572e922014-10-30 18:00:06 +000038
39/// \brief Write samples to a text file.
Diego Novillo8e415a82015-11-13 20:24:28 +000040///
41/// Note: it may be tempting to implement this in terms of
Diego Novilloef548d22015-11-19 15:33:08 +000042/// FunctionSamples::print(). Please don't. The dump functionality is intended
Diego Novillo8e415a82015-11-13 20:24:28 +000043/// 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.
Dehao Chen57d1dda2016-03-03 18:09:32 +000047std::error_code SampleProfileWriterText::write(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +000048 auto &OS = *OutputStream;
Dehao Chen57d1dda2016-03-03 18:09:32 +000049 OS << S.getName() << ":" << S.getTotalSamples();
Diego Novilloaae1ed82015-10-08 19:40:37 +000050 if (Indent == 0)
51 OS << ":" << S.getHeadSamples();
52 OS << "\n";
Diego Novilloc572e922014-10-30 18:00:06 +000053
Diego Novilloef548d22015-11-19 15:33:08 +000054 SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
55 for (const auto &I : SortedSamples.get()) {
56 LineLocation Loc = I->first;
57 const SampleRecord &Sample = I->second;
Diego Novilloaae1ed82015-10-08 19:40:37 +000058 OS.indent(Indent + 1);
Diego Novilloc572e922014-10-30 18:00:06 +000059 if (Loc.Discriminator == 0)
60 OS << Loc.LineOffset << ": ";
61 else
62 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
63
64 OS << Sample.getSamples();
65
Diego Novillod5336ae2014-11-01 00:56:55 +000066 for (const auto &J : Sample.getCallTargets())
67 OS << " " << J.first() << ":" << J.second;
Diego Novilloc572e922014-10-30 18:00:06 +000068 OS << "\n";
69 }
70
Dehao Chen2c7ca9b2017-04-13 19:52:10 +000071 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novilloef548d22015-11-19 15:33:08 +000072 S.getCallsiteSamples());
Diego Novilloaae1ed82015-10-08 19:40:37 +000073 Indent += 1;
Dehao Chen2c7ca9b2017-04-13 19:52:10 +000074 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 }
Diego Novilloaae1ed82015-10-08 19:40:37 +000086 Indent -= 1;
87
Diego Novillo760c5a82015-10-13 22:48:46 +000088 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +000089}
90
Diego Novillo760c5a82015-10-13 22:48:46 +000091std::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;
Nathan Slingerland51abea72015-12-10 17:21:42 +000095 encodeULEB128(ret->second, *OutputStream);
Diego Novillo760c5a82015-10-13 22:48:46 +000096 return sampleprof_error::success;
97}
Diego Novilloc572e922014-10-30 18:00:06 +000098
Diego Novillo760c5a82015-10-13 22:48:46 +000099void SampleProfileWriterBinary::addName(StringRef FName) {
100 auto NextIdx = NameTable.size();
101 NameTable.insert(std::make_pair(FName, NextIdx));
102}
103
104void 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.
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000113 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 }
Diego Novillo760c5a82015-10-13 22:48:46 +0000119}
120
121std::error_code SampleProfileWriterBinary::writeHeader(
122 const StringMap<FunctionSamples> &ProfileMap) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000123 auto &OS = *OutputStream;
124
Diego Novillo760c5a82015-10-13 22:48:46 +0000125 // Write file magic identifier.
Diego Novilloc572e922014-10-30 18:00:06 +0000126 encodeULEB128(SPMagic(), OS);
127 encodeULEB128(SPVersion(), OS);
Diego Novillo760c5a82015-10-13 22:48:46 +0000128
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000129 computeSummary(ProfileMap);
130 if (auto EC = writeSummary())
131 return EC;
132
Diego Novillo760c5a82015-10-13 22:48:46 +0000133 // 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 }
Diego Novillo760c5a82015-10-13 22:48:46 +0000145 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000146}
147
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000148std::error_code SampleProfileWriterBinary::writeSummary() {
149 auto &OS = *OutputStream;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000150 encodeULEB128(Summary->getTotalCount(), OS);
151 encodeULEB128(Summary->getMaxCount(), OS);
Easwaran Raman6f4903d2016-03-28 23:14:29 +0000152 encodeULEB128(Summary->getMaxFunctionCount(), OS);
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000153 encodeULEB128(Summary->getNumCounts(), OS);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000154 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}
Dehao Chen57d1dda2016-03-03 18:09:32 +0000164std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000165 auto &OS = *OutputStream;
166
Dehao Chen57d1dda2016-03-03 18:09:32 +0000167 if (std::error_code EC = writeNameIdx(S.getName()))
Diego Novillo760c5a82015-10-13 22:48:46 +0000168 return EC;
169
Diego Novilloc572e922014-10-30 18:00:06 +0000170 encodeULEB128(S.getTotalSamples(), OS);
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000171
172 // Emit all the body samples.
Diego Novillob93483d2015-10-16 18:54:35 +0000173 encodeULEB128(S.getBodySamples().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000174 for (const auto &I : S.getBodySamples()) {
175 LineLocation Loc = I.first;
176 const SampleRecord &Sample = I.second;
Diego Novilloc572e922014-10-30 18:00:06 +0000177 encodeULEB128(Loc.LineOffset, OS);
178 encodeULEB128(Loc.Discriminator, OS);
179 encodeULEB128(Sample.getSamples(), OS);
180 encodeULEB128(Sample.getCallTargets().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000181 for (const auto &J : Sample.getCallTargets()) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000182 StringRef Callee = J.first();
Diego Novillo38be3332015-10-15 16:36:21 +0000183 uint64_t CalleeSamples = J.second;
Diego Novillo760c5a82015-10-13 22:48:46 +0000184 if (std::error_code EC = writeNameIdx(Callee))
185 return EC;
Diego Novilloc572e922014-10-30 18:00:06 +0000186 encodeULEB128(CalleeSamples, OS);
187 }
188 }
189
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000190 // Recursively emit all the callsite samples.
191 encodeULEB128(S.getCallsiteSamples().size(), OS);
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000192 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 }
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000201
Diego Novillo760c5a82015-10-13 22:48:46 +0000202 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000203}
Diego Novillod5336ae2014-11-01 00:56:55 +0000204
Diego Novillob93483d2015-10-16 18:54:35 +0000205/// \brief Write samples of a top-level function to a binary file.
206///
207/// \returns true if the samples were written successfully, false otherwise.
Dehao Chen57d1dda2016-03-03 18:09:32 +0000208std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000209 encodeULEB128(S.getHeadSamples(), *OutputStream);
Dehao Chen57d1dda2016-03-03 18:09:32 +0000210 return writeBody(S);
Diego Novillob93483d2015-10-16 18:54:35 +0000211}
212
Nathan Slingerland51abea72015-12-10 17:21:42 +0000213/// \brief Create a sample profile file writer based on the specified format.
Diego Novillod5336ae2014-11-01 00:56:55 +0000214///
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.
Diego Novillofcd55602014-11-03 00:51:45 +0000222ErrorOr<std::unique_ptr<SampleProfileWriter>>
223SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000224 std::error_code EC;
Nathan Slingerland51abea72015-12-10 17:21:42 +0000225 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.
245ErrorOr<std::unique_ptr<SampleProfileWriter>>
246SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
247 SampleProfileFormat Format) {
248 std::error_code EC;
Diego Novillofcd55602014-11-03 00:51:45 +0000249 std::unique_ptr<SampleProfileWriter> Writer;
Diego Novillod5336ae2014-11-01 00:56:55 +0000250
251 if (Format == SPF_Binary)
Nathan Slingerland51abea72015-12-10 17:21:42 +0000252 Writer.reset(new SampleProfileWriterBinary(OS));
Diego Novillod5336ae2014-11-01 00:56:55 +0000253 else if (Format == SPF_Text)
Nathan Slingerland51abea72015-12-10 17:21:42 +0000254 Writer.reset(new SampleProfileWriterText(OS));
Diego Novillo760c5a82015-10-13 22:48:46 +0000255 else if (Format == SPF_GCC)
256 EC = sampleprof_error::unsupported_writing_format;
Diego Novillod5336ae2014-11-01 00:56:55 +0000257 else
258 EC = sampleprof_error::unrecognized_format;
259
Diego Novillofcd55602014-11-03 00:51:45 +0000260 if (EC)
261 return EC;
262
263 return std::move(Writer);
Diego Novillod5336ae2014-11-01 00:56:55 +0000264}
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000265
266void SampleProfileWriter::computeSummary(
267 const StringMap<FunctionSamples> &ProfileMap) {
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000268 SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000269 for (const auto &I : ProfileMap) {
270 const FunctionSamples &Profile = I.second;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000271 Builder.addRecord(Profile);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000272 }
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000273 Summary = Builder.getSummary();
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000274}