blob: c9f8923346863b8190ad246d82507eabe26d2ad1 [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.
Diego Novillo760c5a82015-10-13 22:48:46 +000040std::error_code SampleProfileWriterText::write(StringRef FName,
41 const FunctionSamples &S) {
Diego Novilloaae1ed82015-10-08 19:40:37 +000042 OS << FName << ":" << S.getTotalSamples();
43 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
Diego Novilloef548d22015-11-19 15:33:08 +000064 SampleSorter<CallsiteLocation, FunctionSamples> SortedCallsiteSamples(
65 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()) {
68 CallsiteLocation Loc = I->first;
69 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 << ": ";
Diego Novillo760c5a82015-10-13 22:48:46 +000075 if (std::error_code EC = write(Loc.CalleeName, CalleeSamples))
76 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;
87 encodeULEB128(ret->second, OS);
88 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()) {
106 CallsiteLocation Loc = J.first;
107 const FunctionSamples &CalleeSamples = J.second;
108 addName(Loc.CalleeName);
109 addNames(CalleeSamples);
110 }
111}
112
113std::error_code SampleProfileWriterBinary::writeHeader(
114 const StringMap<FunctionSamples> &ProfileMap) {
115 // Write file magic identifier.
Diego Novilloc572e922014-10-30 18:00:06 +0000116 encodeULEB128(SPMagic(), OS);
117 encodeULEB128(SPVersion(), OS);
Diego Novillo760c5a82015-10-13 22:48:46 +0000118
119 // Generate the name table for all the functions referenced in the profile.
120 for (const auto &I : ProfileMap) {
121 addName(I.first());
122 addNames(I.second);
123 }
124
125 // Write out the name table.
126 encodeULEB128(NameTable.size(), OS);
127 for (auto N : NameTable) {
128 OS << N.first;
129 encodeULEB128(0, OS);
130 }
131
132 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000133}
134
Diego Novillob93483d2015-10-16 18:54:35 +0000135std::error_code SampleProfileWriterBinary::writeBody(StringRef FName,
136 const FunctionSamples &S) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000137 if (std::error_code EC = writeNameIdx(FName))
138 return EC;
139
Diego Novilloc572e922014-10-30 18:00:06 +0000140 encodeULEB128(S.getTotalSamples(), OS);
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000141
142 // Emit all the body samples.
Diego Novillob93483d2015-10-16 18:54:35 +0000143 encodeULEB128(S.getBodySamples().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000144 for (const auto &I : S.getBodySamples()) {
145 LineLocation Loc = I.first;
146 const SampleRecord &Sample = I.second;
Diego Novilloc572e922014-10-30 18:00:06 +0000147 encodeULEB128(Loc.LineOffset, OS);
148 encodeULEB128(Loc.Discriminator, OS);
149 encodeULEB128(Sample.getSamples(), OS);
150 encodeULEB128(Sample.getCallTargets().size(), OS);
Diego Novillod5336ae2014-11-01 00:56:55 +0000151 for (const auto &J : Sample.getCallTargets()) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000152 StringRef Callee = J.first();
Diego Novillo38be3332015-10-15 16:36:21 +0000153 uint64_t CalleeSamples = J.second;
Diego Novillo760c5a82015-10-13 22:48:46 +0000154 if (std::error_code EC = writeNameIdx(Callee))
155 return EC;
Diego Novilloc572e922014-10-30 18:00:06 +0000156 encodeULEB128(CalleeSamples, OS);
157 }
158 }
159
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000160 // Recursively emit all the callsite samples.
161 encodeULEB128(S.getCallsiteSamples().size(), OS);
162 for (const auto &J : S.getCallsiteSamples()) {
163 CallsiteLocation Loc = J.first;
164 const FunctionSamples &CalleeSamples = J.second;
165 encodeULEB128(Loc.LineOffset, OS);
166 encodeULEB128(Loc.Discriminator, OS);
Diego Novillob93483d2015-10-16 18:54:35 +0000167 if (std::error_code EC = writeBody(Loc.CalleeName, CalleeSamples))
Diego Novillo760c5a82015-10-13 22:48:46 +0000168 return EC;
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000169 }
170
Diego Novillo760c5a82015-10-13 22:48:46 +0000171 return sampleprof_error::success;
Diego Novilloc572e922014-10-30 18:00:06 +0000172}
Diego Novillod5336ae2014-11-01 00:56:55 +0000173
Diego Novillob93483d2015-10-16 18:54:35 +0000174/// \brief Write samples of a top-level function to a binary file.
175///
176/// \returns true if the samples were written successfully, false otherwise.
177std::error_code SampleProfileWriterBinary::write(StringRef FName,
178 const FunctionSamples &S) {
179 encodeULEB128(S.getHeadSamples(), OS);
180 return writeBody(FName, S);
181}
182
Diego Novillod5336ae2014-11-01 00:56:55 +0000183/// \brief Create a sample profile writer based on the specified format.
184///
185/// \param Filename The file to create.
186///
187/// \param Writer The writer to instantiate according to the specified format.
188///
189/// \param Format Encoding format for the profile file.
190///
191/// \returns an error code indicating the status of the created writer.
Diego Novillofcd55602014-11-03 00:51:45 +0000192ErrorOr<std::unique_ptr<SampleProfileWriter>>
193SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
Diego Novillod5336ae2014-11-01 00:56:55 +0000194 std::error_code EC;
Diego Novillofcd55602014-11-03 00:51:45 +0000195 std::unique_ptr<SampleProfileWriter> Writer;
Diego Novillod5336ae2014-11-01 00:56:55 +0000196
197 if (Format == SPF_Binary)
198 Writer.reset(new SampleProfileWriterBinary(Filename, EC));
199 else if (Format == SPF_Text)
200 Writer.reset(new SampleProfileWriterText(Filename, EC));
Diego Novillo760c5a82015-10-13 22:48:46 +0000201 else if (Format == SPF_GCC)
202 EC = sampleprof_error::unsupported_writing_format;
Diego Novillod5336ae2014-11-01 00:56:55 +0000203 else
204 EC = sampleprof_error::unrecognized_format;
205
Diego Novillofcd55602014-11-03 00:51:45 +0000206 if (EC)
207 return EC;
208
209 return std::move(Writer);
Diego Novillod5336ae2014-11-01 00:56:55 +0000210}