blob: 4fa71288f8d9fbbe5f949805bc7b9d2faa35432a [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
21#include "llvm/ProfileData/SampleProfWriter.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/ErrorOr.h"
Diego Novilloc572e922014-10-30 18:00:06 +000024#include "llvm/Support/LEB128.h"
25#include "llvm/Support/LineIterator.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000026#include "llvm/Support/MemoryBuffer.h"
Diego Novilloc572e922014-10-30 18:00:06 +000027#include "llvm/Support/Regex.h"
28
29using namespace llvm::sampleprof;
30using namespace llvm;
31
32/// \brief Write samples to a text file.
Diego Novillo8e415a82015-11-13 20:24:28 +000033///
34/// Note: it may be tempting to implement this in terms of
Diego Novilloef548d22015-11-19 15:33:08 +000035/// FunctionSamples::print(). Please don't. The dump functionality is intended
Diego Novillo8e415a82015-11-13 20:24:28 +000036/// for debugging and has no specified form.
37///
38/// The format used here is more structured and deliberate because
39/// it needs to be parsed by the SampleProfileReaderText class.
Dehao Chen57d1dda2016-03-03 18:09:32 +000040std::error_code SampleProfileWriterText::write(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +000041 auto &OS = *OutputStream;
Dehao Chen57d1dda2016-03-03 18:09:32 +000042 OS << S.getName() << ":" << S.getTotalSamples();
Diego Novilloaae1ed82015-10-08 19:40:37 +000043 if (Indent == 0)
44 OS << ":" << S.getHeadSamples();
45 OS << "\n";
Diego Novilloc572e922014-10-30 18:00:06 +000046
Diego Novilloef548d22015-11-19 15:33:08 +000047 SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
48 for (const auto &I : SortedSamples.get()) {
49 LineLocation Loc = I->first;
50 const SampleRecord &Sample = I->second;
Diego Novilloaae1ed82015-10-08 19:40:37 +000051 OS.indent(Indent + 1);
Diego Novilloc572e922014-10-30 18:00:06 +000052 if (Loc.Discriminator == 0)
53 OS << Loc.LineOffset << ": ";
54 else
55 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
56
57 OS << Sample.getSamples();
58
Diego Novillod5336ae2014-11-01 00:56:55 +000059 for (const auto &J : Sample.getCallTargets())
60 OS << " " << J.first() << ":" << J.second;
Diego Novilloc572e922014-10-30 18:00:06 +000061 OS << "\n";
62 }
63
Dehao Chen57d1dda2016-03-03 18:09:32 +000064 SampleSorter<LineLocation, FunctionSamples> SortedCallsiteSamples(
Diego Novilloef548d22015-11-19 15:33:08 +000065 S.getCallsiteSamples());
Diego Novilloaae1ed82015-10-08 19:40:37 +000066 Indent += 1;
Diego Novilloef548d22015-11-19 15:33:08 +000067 for (const auto &I : SortedCallsiteSamples.get()) {
Dehao Chen57d1dda2016-03-03 18:09:32 +000068 LineLocation Loc = I->first;
Diego Novilloef548d22015-11-19 15:33:08 +000069 const FunctionSamples &CalleeSamples = I->second;
Diego Novilloaae1ed82015-10-08 19:40:37 +000070 OS.indent(Indent);
71 if (Loc.Discriminator == 0)
72 OS << Loc.LineOffset << ": ";
73 else
74 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
Dehao Chen57d1dda2016-03-03 18:09:32 +000075 if (std::error_code EC = write(CalleeSamples))
Diego Novillo760c5a82015-10-13 22:48:46 +000076 return EC;
Diego Novilloaae1ed82015-10-08 19:40:37 +000077 }
78 Indent -= 1;
79
Diego Novillo760c5a82015-10-13 22:48:46 +000080 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +000081}
82
Diego Novillo760c5a82015-10-13 22:48:46 +000083std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
84 const auto &ret = NameTable.find(FName);
85 if (ret == NameTable.end())
86 return sampleprof_error::truncated_name_table;
Nathan Slingerland51abea72015-12-10 17:21:42 +000087 encodeULEB128(ret->second, *OutputStream);
Diego Novillo760c5a82015-10-13 22:48:46 +000088 return sampleprof_error::success;
89}
Diego Novilloc572e922014-10-30 18:00:06 +000090
Diego Novillo760c5a82015-10-13 22:48:46 +000091void SampleProfileWriterBinary::addName(StringRef FName) {
92 auto NextIdx = NameTable.size();
93 NameTable.insert(std::make_pair(FName, NextIdx));
94}
95
96void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
97 // Add all the names in indirect call targets.
98 for (const auto &I : S.getBodySamples()) {
99 const SampleRecord &Sample = I.second;
100 for (const auto &J : Sample.getCallTargets())
101 addName(J.first());
102 }
103
104 // Recursively add all the names for inlined callsites.
105 for (const auto &J : S.getCallsiteSamples()) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000106 const FunctionSamples &CalleeSamples = J.second;
Dehao Chen57d1dda2016-03-03 18:09:32 +0000107 addName(CalleeSamples.getName());
Diego Novillo760c5a82015-10-13 22:48:46 +0000108 addNames(CalleeSamples);
109 }
110}
111
112std::error_code SampleProfileWriterBinary::writeHeader(
113 const StringMap<FunctionSamples> &ProfileMap) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000114 auto &OS = *OutputStream;
115
Diego Novillo760c5a82015-10-13 22:48:46 +0000116 // Write file magic identifier.
Diego Novilloc572e922014-10-30 18:00:06 +0000117 encodeULEB128(SPMagic(), OS);
118 encodeULEB128(SPVersion(), OS);
Diego Novillo760c5a82015-10-13 22:48:46 +0000119
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000120 computeSummary(ProfileMap);
121 if (auto EC = writeSummary())
122 return EC;
123
Diego Novillo760c5a82015-10-13 22:48:46 +0000124 // Generate the name table for all the functions referenced in the profile.
125 for (const auto &I : ProfileMap) {
126 addName(I.first());
127 addNames(I.second);
128 }
129
130 // Write out the name table.
131 encodeULEB128(NameTable.size(), OS);
132 for (auto N : NameTable) {
133 OS << N.first;
134 encodeULEB128(0, OS);
135 }
Diego Novillo760c5a82015-10-13 22:48:46 +0000136 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000137}
138
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000139std::error_code SampleProfileWriterBinary::writeSummary() {
140 auto &OS = *OutputStream;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000141 encodeULEB128(Summary->getTotalCount(), OS);
142 encodeULEB128(Summary->getMaxCount(), OS);
Easwaran Raman6f4903d2016-03-28 23:14:29 +0000143 encodeULEB128(Summary->getMaxFunctionCount(), OS);
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000144 encodeULEB128(Summary->getNumCounts(), OS);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000145 encodeULEB128(Summary->getNumFunctions(), OS);
146 std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary();
147 encodeULEB128(Entries.size(), OS);
148 for (auto Entry : Entries) {
149 encodeULEB128(Entry.Cutoff, OS);
150 encodeULEB128(Entry.MinCount, OS);
151 encodeULEB128(Entry.NumCounts, OS);
152 }
153 return sampleprof_error::success;
154}
Dehao Chen57d1dda2016-03-03 18:09:32 +0000155std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000156 auto &OS = *OutputStream;
157
Dehao Chen57d1dda2016-03-03 18:09:32 +0000158 if (std::error_code EC = writeNameIdx(S.getName()))
Diego Novillo760c5a82015-10-13 22:48:46 +0000159 return EC;
160
Diego Novilloc572e922014-10-30 18:00:06 +0000161 encodeULEB128(S.getTotalSamples(), OS);
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000162
163 // Emit all the body samples.
Diego Novillob93483d2015-10-16 18:54:35 +0000164 encodeULEB128(S.getBodySamples().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000165 for (const auto &I : S.getBodySamples()) {
166 LineLocation Loc = I.first;
167 const SampleRecord &Sample = I.second;
Diego Novilloc572e922014-10-30 18:00:06 +0000168 encodeULEB128(Loc.LineOffset, OS);
169 encodeULEB128(Loc.Discriminator, OS);
170 encodeULEB128(Sample.getSamples(), OS);
171 encodeULEB128(Sample.getCallTargets().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000172 for (const auto &J : Sample.getCallTargets()) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000173 StringRef Callee = J.first();
Diego Novillo38be3332015-10-15 16:36:21 +0000174 uint64_t CalleeSamples = J.second;
Diego Novillo760c5a82015-10-13 22:48:46 +0000175 if (std::error_code EC = writeNameIdx(Callee))
176 return EC;
Diego Novilloc572e922014-10-30 18:00:06 +0000177 encodeULEB128(CalleeSamples, OS);
178 }
179 }
180
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000181 // Recursively emit all the callsite samples.
182 encodeULEB128(S.getCallsiteSamples().size(), OS);
183 for (const auto &J : S.getCallsiteSamples()) {
Dehao Chen57d1dda2016-03-03 18:09:32 +0000184 LineLocation Loc = J.first;
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000185 const FunctionSamples &CalleeSamples = J.second;
186 encodeULEB128(Loc.LineOffset, OS);
187 encodeULEB128(Loc.Discriminator, OS);
Dehao Chen57d1dda2016-03-03 18:09:32 +0000188 if (std::error_code EC = writeBody(CalleeSamples))
Diego Novillo760c5a82015-10-13 22:48:46 +0000189 return EC;
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000190 }
191
Diego Novillo760c5a82015-10-13 22:48:46 +0000192 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000193}
Diego Novillod5336ae2014-11-01 00:56:55 +0000194
Diego Novillob93483d2015-10-16 18:54:35 +0000195/// \brief Write samples of a top-level function to a binary file.
196///
197/// \returns true if the samples were written successfully, false otherwise.
Dehao Chen57d1dda2016-03-03 18:09:32 +0000198std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) {
Nathan Slingerland51abea72015-12-10 17:21:42 +0000199 encodeULEB128(S.getHeadSamples(), *OutputStream);
Dehao Chen57d1dda2016-03-03 18:09:32 +0000200 return writeBody(S);
Diego Novillob93483d2015-10-16 18:54:35 +0000201}
202
Nathan Slingerland51abea72015-12-10 17:21:42 +0000203/// \brief Create a sample profile file writer based on the specified format.
Diego Novillod5336ae2014-11-01 00:56:55 +0000204///
205/// \param Filename The file to create.
206///
207/// \param Writer The writer to instantiate according to the specified format.
208///
209/// \param Format Encoding format for the profile file.
210///
211/// \returns an error code indicating the status of the created writer.
Diego Novillofcd55602014-11-03 00:51:45 +0000212ErrorOr<std::unique_ptr<SampleProfileWriter>>
213SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000214 std::error_code EC;
Nathan Slingerland51abea72015-12-10 17:21:42 +0000215 std::unique_ptr<raw_ostream> OS;
216 if (Format == SPF_Binary)
217 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_None));
218 else
219 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_Text));
220 if (EC)
221 return EC;
222
223 return create(OS, Format);
224}
225
226/// \brief Create a sample profile stream writer based on the specified format.
227///
228/// \param OS The output stream to store the profile data to.
229///
230/// \param Writer The writer to instantiate according to the specified format.
231///
232/// \param Format Encoding format for the profile file.
233///
234/// \returns an error code indicating the status of the created writer.
235ErrorOr<std::unique_ptr<SampleProfileWriter>>
236SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
237 SampleProfileFormat Format) {
238 std::error_code EC;
Diego Novillofcd55602014-11-03 00:51:45 +0000239 std::unique_ptr<SampleProfileWriter> Writer;
Diego Novillod5336ae2014-11-01 00:56:55 +0000240
241 if (Format == SPF_Binary)
Nathan Slingerland51abea72015-12-10 17:21:42 +0000242 Writer.reset(new SampleProfileWriterBinary(OS));
Diego Novillod5336ae2014-11-01 00:56:55 +0000243 else if (Format == SPF_Text)
Nathan Slingerland51abea72015-12-10 17:21:42 +0000244 Writer.reset(new SampleProfileWriterText(OS));
Diego Novillo760c5a82015-10-13 22:48:46 +0000245 else if (Format == SPF_GCC)
246 EC = sampleprof_error::unsupported_writing_format;
Diego Novillod5336ae2014-11-01 00:56:55 +0000247 else
248 EC = sampleprof_error::unrecognized_format;
249
Diego Novillofcd55602014-11-03 00:51:45 +0000250 if (EC)
251 return EC;
252
253 return std::move(Writer);
Diego Novillod5336ae2014-11-01 00:56:55 +0000254}
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000255
256void SampleProfileWriter::computeSummary(
257 const StringMap<FunctionSamples> &ProfileMap) {
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000258 SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000259 for (const auto &I : ProfileMap) {
260 const FunctionSamples &Profile = I.second;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000261 Builder.addRecord(Profile);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000262 }
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000263 Summary = Builder.getSummary();
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000264}