blob: e1d6d575631aecea5c01f2a2632625e37949fd8b [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 Chen57d1dda2016-03-03 18:09:32 +000071 SampleSorter<LineLocation, FunctionSamples> SortedCallsiteSamples(
Diego Novilloef548d22015-11-19 15:33:08 +000072 S.getCallsiteSamples());
Diego Novilloaae1ed82015-10-08 19:40:37 +000073 Indent += 1;
Diego Novilloef548d22015-11-19 15:33:08 +000074 for (const auto &I : SortedCallsiteSamples.get()) {
Dehao Chen57d1dda2016-03-03 18:09:32 +000075 LineLocation Loc = I->first;
Diego Novilloef548d22015-11-19 15:33:08 +000076 const FunctionSamples &CalleeSamples = I->second;
Diego Novilloaae1ed82015-10-08 19:40:37 +000077 OS.indent(Indent);
78 if (Loc.Discriminator == 0)
79 OS << Loc.LineOffset << ": ";
80 else
81 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
Dehao Chen57d1dda2016-03-03 18:09:32 +000082 if (std::error_code EC = write(CalleeSamples))
Diego Novillo760c5a82015-10-13 22:48:46 +000083 return EC;
Diego Novilloaae1ed82015-10-08 19:40:37 +000084 }
85 Indent -= 1;
86
Diego Novillo760c5a82015-10-13 22:48:46 +000087 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +000088}
89
Diego Novillo760c5a82015-10-13 22:48:46 +000090std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
91 const auto &ret = NameTable.find(FName);
92 if (ret == NameTable.end())
93 return sampleprof_error::truncated_name_table;
Nathan Slingerland51abea72015-12-10 17:21:42 +000094 encodeULEB128(ret->second, *OutputStream);
Diego Novillo760c5a82015-10-13 22:48:46 +000095 return sampleprof_error::success;
96}
Diego Novilloc572e922014-10-30 18:00:06 +000097
Diego Novillo760c5a82015-10-13 22:48:46 +000098void SampleProfileWriterBinary::addName(StringRef FName) {
99 auto NextIdx = NameTable.size();
100 NameTable.insert(std::make_pair(FName, NextIdx));
101}
102
103void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
104 // Add all the names in indirect call targets.
105 for (const auto &I : S.getBodySamples()) {
106 const SampleRecord &Sample = I.second;
107 for (const auto &J : Sample.getCallTargets())
108 addName(J.first());
109 }
110
111 // Recursively add all the names for inlined callsites.
112 for (const auto &J : S.getCallsiteSamples()) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000113 const FunctionSamples &CalleeSamples = J.second;
Dehao Chen57d1dda2016-03-03 18:09:32 +0000114 addName(CalleeSamples.getName());
Diego Novillo760c5a82015-10-13 22:48:46 +0000115 addNames(CalleeSamples);
116 }
117}
118
119std::error_code SampleProfileWriterBinary::writeHeader(
120 const StringMap<FunctionSamples> &ProfileMap) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000121 auto &OS = *OutputStream;
122
Diego Novillo760c5a82015-10-13 22:48:46 +0000123 // Write file magic identifier.
Diego Novilloc572e922014-10-30 18:00:06 +0000124 encodeULEB128(SPMagic(), OS);
125 encodeULEB128(SPVersion(), OS);
Diego Novillo760c5a82015-10-13 22:48:46 +0000126
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000127 computeSummary(ProfileMap);
128 if (auto EC = writeSummary())
129 return EC;
130
Diego Novillo760c5a82015-10-13 22:48:46 +0000131 // Generate the name table for all the functions referenced in the profile.
132 for (const auto &I : ProfileMap) {
133 addName(I.first());
134 addNames(I.second);
135 }
136
137 // Write out the name table.
138 encodeULEB128(NameTable.size(), OS);
139 for (auto N : NameTable) {
140 OS << N.first;
141 encodeULEB128(0, OS);
142 }
Diego Novillo760c5a82015-10-13 22:48:46 +0000143 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000144}
145
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000146std::error_code SampleProfileWriterBinary::writeSummary() {
147 auto &OS = *OutputStream;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000148 encodeULEB128(Summary->getTotalCount(), OS);
149 encodeULEB128(Summary->getMaxCount(), OS);
Easwaran Raman6f4903d2016-03-28 23:14:29 +0000150 encodeULEB128(Summary->getMaxFunctionCount(), OS);
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000151 encodeULEB128(Summary->getNumCounts(), OS);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000152 encodeULEB128(Summary->getNumFunctions(), OS);
153 std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary();
154 encodeULEB128(Entries.size(), OS);
155 for (auto Entry : Entries) {
156 encodeULEB128(Entry.Cutoff, OS);
157 encodeULEB128(Entry.MinCount, OS);
158 encodeULEB128(Entry.NumCounts, OS);
159 }
160 return sampleprof_error::success;
161}
Dehao Chen57d1dda2016-03-03 18:09:32 +0000162std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000163 auto &OS = *OutputStream;
164
Dehao Chen57d1dda2016-03-03 18:09:32 +0000165 if (std::error_code EC = writeNameIdx(S.getName()))
Diego Novillo760c5a82015-10-13 22:48:46 +0000166 return EC;
167
Diego Novilloc572e922014-10-30 18:00:06 +0000168 encodeULEB128(S.getTotalSamples(), OS);
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000169
170 // Emit all the body samples.
Diego Novillob93483d2015-10-16 18:54:35 +0000171 encodeULEB128(S.getBodySamples().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000172 for (const auto &I : S.getBodySamples()) {
173 LineLocation Loc = I.first;
174 const SampleRecord &Sample = I.second;
Diego Novilloc572e922014-10-30 18:00:06 +0000175 encodeULEB128(Loc.LineOffset, OS);
176 encodeULEB128(Loc.Discriminator, OS);
177 encodeULEB128(Sample.getSamples(), OS);
178 encodeULEB128(Sample.getCallTargets().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000179 for (const auto &J : Sample.getCallTargets()) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000180 StringRef Callee = J.first();
Diego Novillo38be3332015-10-15 16:36:21 +0000181 uint64_t CalleeSamples = J.second;
Diego Novillo760c5a82015-10-13 22:48:46 +0000182 if (std::error_code EC = writeNameIdx(Callee))
183 return EC;
Diego Novilloc572e922014-10-30 18:00:06 +0000184 encodeULEB128(CalleeSamples, OS);
185 }
186 }
187
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000188 // Recursively emit all the callsite samples.
189 encodeULEB128(S.getCallsiteSamples().size(), OS);
190 for (const auto &J : S.getCallsiteSamples()) {
Dehao Chen57d1dda2016-03-03 18:09:32 +0000191 LineLocation Loc = J.first;
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000192 const FunctionSamples &CalleeSamples = J.second;
193 encodeULEB128(Loc.LineOffset, OS);
194 encodeULEB128(Loc.Discriminator, OS);
Dehao Chen57d1dda2016-03-03 18:09:32 +0000195 if (std::error_code EC = writeBody(CalleeSamples))
Diego Novillo760c5a82015-10-13 22:48:46 +0000196 return EC;
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000197 }
198
Diego Novillo760c5a82015-10-13 22:48:46 +0000199 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000200}
Diego Novillod5336ae2014-11-01 00:56:55 +0000201
Diego Novillob93483d2015-10-16 18:54:35 +0000202/// \brief Write samples of a top-level function to a binary file.
203///
204/// \returns true if the samples were written successfully, false otherwise.
Dehao Chen57d1dda2016-03-03 18:09:32 +0000205std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000206 encodeULEB128(S.getHeadSamples(), *OutputStream);
Dehao Chen57d1dda2016-03-03 18:09:32 +0000207 return writeBody(S);
Diego Novillob93483d2015-10-16 18:54:35 +0000208}
209
Nathan Slingerland51abea72015-12-10 17:21:42 +0000210/// \brief Create a sample profile file writer based on the specified format.
Diego Novillod5336ae2014-11-01 00:56:55 +0000211///
212/// \param Filename The file to create.
213///
214/// \param Writer The writer to instantiate according to the specified format.
215///
216/// \param Format Encoding format for the profile file.
217///
218/// \returns an error code indicating the status of the created writer.
Diego Novillofcd55602014-11-03 00:51:45 +0000219ErrorOr<std::unique_ptr<SampleProfileWriter>>
220SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000221 std::error_code EC;
Nathan Slingerland51abea72015-12-10 17:21:42 +0000222 std::unique_ptr<raw_ostream> OS;
223 if (Format == SPF_Binary)
224 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_None));
225 else
226 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_Text));
227 if (EC)
228 return EC;
229
230 return create(OS, Format);
231}
232
233/// \brief Create a sample profile stream writer based on the specified format.
234///
235/// \param OS The output stream to store the profile data to.
236///
237/// \param Writer The writer to instantiate according to the specified format.
238///
239/// \param Format Encoding format for the profile file.
240///
241/// \returns an error code indicating the status of the created writer.
242ErrorOr<std::unique_ptr<SampleProfileWriter>>
243SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
244 SampleProfileFormat Format) {
245 std::error_code EC;
Diego Novillofcd55602014-11-03 00:51:45 +0000246 std::unique_ptr<SampleProfileWriter> Writer;
Diego Novillod5336ae2014-11-01 00:56:55 +0000247
248 if (Format == SPF_Binary)
Nathan Slingerland51abea72015-12-10 17:21:42 +0000249 Writer.reset(new SampleProfileWriterBinary(OS));
Diego Novillod5336ae2014-11-01 00:56:55 +0000250 else if (Format == SPF_Text)
Nathan Slingerland51abea72015-12-10 17:21:42 +0000251 Writer.reset(new SampleProfileWriterText(OS));
Diego Novillo760c5a82015-10-13 22:48:46 +0000252 else if (Format == SPF_GCC)
253 EC = sampleprof_error::unsupported_writing_format;
Diego Novillod5336ae2014-11-01 00:56:55 +0000254 else
255 EC = sampleprof_error::unrecognized_format;
256
Diego Novillofcd55602014-11-03 00:51:45 +0000257 if (EC)
258 return EC;
259
260 return std::move(Writer);
Diego Novillod5336ae2014-11-01 00:56:55 +0000261}
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000262
263void SampleProfileWriter::computeSummary(
264 const StringMap<FunctionSamples> &ProfileMap) {
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000265 SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000266 for (const auto &I : ProfileMap) {
267 const FunctionSamples &Profile = I.second;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000268 Builder.addRecord(Profile);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000269 }
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000270 Summary = Builder.getSummary();
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000271}