1
//===- SampleProfReader.cpp - Read LLVM sample profile data ---------------===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This file implements the class that reads 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.
16
// NOTE: If you are making changes to the file format, please remember
17
// to document them in the Clang documentation at
18
// tools/clang/docs/UsersManual.rst.
23
// Sample profiles are written as ASCII text. The file is divided into
24
// sections, which correspond to each of the functions executed at runtime.
25
// Each section has the following format
27
// function1:total_samples:total_head_samples
28
// offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ]
29
// offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ]
31
// offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]
33
// The file may contain blank lines between sections and within a
34
// section. However, the spacing within a single line is fixed. Additional
35
// spaces will result in an error while reading the file.
37
// Function names must be mangled in order for the profile loader to
38
// match them in the current translation unit. The two numbers in the
39
// function header specify how many total samples were accumulated in the
40
// function (first number), and the total number of samples accumulated
41
// in the prologue of the function (second number). This head sample
42
// count provides an indicator of how frequently the function is invoked.
44
// Each sampled line may contain several items. Some are optional (marked
47
// a. Source line offset. This number represents the line number
48
// in the function where the sample was collected. The line number is
49
// always relative to the line where symbol of the function is
50
// defined. So, if the function has its header at line 280, the offset
51
// 13 is at line 293 in the file.
53
// Note that this offset should never be a negative number. This could
54
// happen in cases like macros. The debug machinery will register the
55
// line number at the point of macro expansion. So, if the macro was
56
// expanded in a line before the start of the function, the profile
57
// converter should emit a 0 as the offset (this means that the optimizers
58
// will not be able to associate a meaningful weight to the instructions
61
// b. [OPTIONAL] Discriminator. This is used if the sampled program
62
// was compiled with DWARF discriminator support
63
// (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators).
64
// DWARF discriminators are unsigned integer values that allow the
65
// compiler to distinguish between multiple execution paths on the
66
// same source line location.
68
// For example, consider the line of code ``if (cond) foo(); else bar();``.
69
// If the predicate ``cond`` is true 80% of the time, then the edge
70
// into function ``foo`` should be considered to be taken most of the
71
// time. But both calls to ``foo`` and ``bar`` are at the same source
72
// line, so a sample count at that line is not sufficient. The
73
// compiler needs to know which part of that line is taken more
76
// This is what discriminators provide. In this case, the calls to
77
// ``foo`` and ``bar`` will be at the same line, but will have
78
// different discriminator values. This allows the compiler to correctly
79
// set edge weights into ``foo`` and ``bar``.
81
// c. Number of samples. This is an integer quantity representing the
82
// number of samples collected by the profiler at this source
85
// d. [OPTIONAL] Potential call targets and samples. If present, this
86
// line contains a call instruction. This models both direct and
87
// number of samples. For example,
89
// 130: 7 foo:3 bar:2 baz:7
91
// The above means that at relative line offset 130 there is a call
92
// instruction that calls one of ``foo()``, ``bar()`` and ``baz()``,
93
// with ``baz()`` being the relatively more frequently called target.
95
//===----------------------------------------------------------------------===//
97
#include "llvm/ProfileData/SampleProfReader.h"
98
#include "llvm/Support/Debug.h"
99
#include "llvm/Support/ErrorOr.h"
100
#include "llvm/Support/LEB128.h"
101
#include "llvm/Support/LineIterator.h"
102
#include "llvm/Support/MemoryBuffer.h"
103
#include "llvm/Support/Regex.h"
105
using namespace llvm::sampleprof;
106
using namespace llvm;
108
/// \brief Print the samples collected for a function on stream \p OS.
110
/// \param OS Stream to emit the output to.
111
void FunctionSamples::print(raw_ostream &OS) {
112
OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
113
<< " sampled lines\n";
114
for (const auto &SI : BodySamples) {
115
LineLocation Loc = SI.first;
116
const SampleRecord &Sample = SI.second;
117
OS << "\tline offset: " << Loc.LineOffset
118
<< ", discriminator: " << Loc.Discriminator
119
<< ", number of samples: " << Sample.getSamples();
120
if (Sample.hasCalls()) {
122
for (const auto &I : Sample.getCallTargets())
123
OS << " " << I.first() << ":" << I.second;
130
/// \brief Dump the function profile for \p FName.
132
/// \param FName Name of the function to print.
133
/// \param OS Stream to emit the output to.
134
void SampleProfileReader::dumpFunctionProfile(StringRef FName,
136
OS << "Function: " << FName << ": ";
137
Profiles[FName].print(OS);
140
/// \brief Dump all the function profiles found on stream \p OS.
141
void SampleProfileReader::dump(raw_ostream &OS) {
142
for (const auto &I : Profiles)
143
dumpFunctionProfile(I.getKey(), OS);
146
/// \brief Load samples from a text file.
148
/// See the documentation at the top of the file for an explanation of
149
/// the expected format.
151
/// \returns true if the file was loaded successfully, false otherwise.
152
std::error_code SampleProfileReaderText::read() {
153
line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#');
155
// Read the profile of each function. Since each function may be
156
// mentioned more than once, and we are collecting flat profiles,
157
// accumulate samples as we parse them.
158
Regex HeadRE("^([^0-9].*):([0-9]+):([0-9]+)$");
159
Regex LineSampleRE("^([0-9]+)\\.?([0-9]+)?: ([0-9]+)(.*)$");
160
Regex CallSampleRE(" +([^0-9 ][^ ]*):([0-9]+)");
161
while (!LineIt.is_at_eof()) {
162
// Read the header of each function.
164
// Note that for function identifiers we are actually expecting
165
// mangled names, but we may not always get them. This happens when
166
// the compiler decides not to emit the function (e.g., it was inlined
167
// and removed). In this case, the binary will not have the linkage
168
// name for the function, so the profiler will emit the function's
169
// unmangled name, which may contain characters like ':' and '>' in its
170
// name (member functions, templates, etc).
172
// The only requirement we place on the identifier, then, is that it
173
// should not begin with a number.
174
SmallVector<StringRef, 4> Matches;
175
if (!HeadRE.match(*LineIt, &Matches)) {
176
reportParseError(LineIt.line_number(),
177
"Expected 'mangled_name:NUM:NUM', found " + *LineIt);
178
return sampleprof_error::malformed;
180
assert(Matches.size() == 4);
181
StringRef FName = Matches[1];
182
unsigned NumSamples, NumHeadSamples;
183
Matches[2].getAsInteger(10, NumSamples);
184
Matches[3].getAsInteger(10, NumHeadSamples);
185
Profiles[FName] = FunctionSamples();
186
FunctionSamples &FProfile = Profiles[FName];
187
FProfile.addTotalSamples(NumSamples);
188
FProfile.addHeadSamples(NumHeadSamples);
191
// Now read the body. The body of the function ends when we reach
192
// EOF or when we see the start of the next function.
193
while (!LineIt.is_at_eof() && isdigit((*LineIt)[0])) {
194
if (!LineSampleRE.match(*LineIt, &Matches)) {
196
LineIt.line_number(),
197
"Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " + *LineIt);
198
return sampleprof_error::malformed;
200
assert(Matches.size() == 5);
201
unsigned LineOffset, NumSamples, Discriminator = 0;
202
Matches[1].getAsInteger(10, LineOffset);
203
if (Matches[2] != "")
204
Matches[2].getAsInteger(10, Discriminator);
205
Matches[3].getAsInteger(10, NumSamples);
207
// If there are function calls in this line, generate a call sample
208
// entry for each call.
209
std::string CallsLine(Matches[4]);
210
while (CallsLine != "") {
211
SmallVector<StringRef, 3> CallSample;
212
if (!CallSampleRE.match(CallsLine, &CallSample)) {
213
reportParseError(LineIt.line_number(),
214
"Expected 'mangled_name:NUM', found " + CallsLine);
215
return sampleprof_error::malformed;
217
StringRef CalledFunction = CallSample[1];
218
unsigned CalledFunctionSamples;
219
CallSample[2].getAsInteger(10, CalledFunctionSamples);
220
FProfile.addCalledTargetSamples(LineOffset, Discriminator,
221
CalledFunction, CalledFunctionSamples);
222
CallsLine = CallSampleRE.sub("", CallsLine);
225
FProfile.addBodySamples(LineOffset, Discriminator, NumSamples);
230
return sampleprof_error::success;
233
template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
234
unsigned NumBytesRead = 0;
236
uint64_t Val = decodeULEB128(Data, &NumBytesRead);
238
if (Val > std::numeric_limits<T>::max())
239
EC = sampleprof_error::malformed;
240
else if (Data + NumBytesRead > End)
241
EC = sampleprof_error::truncated;
243
EC = sampleprof_error::success;
246
reportParseError(0, EC.message());
250
Data += NumBytesRead;
251
return static_cast<T>(Val);
254
ErrorOr<StringRef> SampleProfileReaderBinary::readString() {
256
StringRef Str(reinterpret_cast<const char *>(Data));
257
if (Data + Str.size() + 1 > End) {
258
EC = sampleprof_error::truncated;
259
reportParseError(0, EC.message());
263
Data += Str.size() + 1;
267
std::error_code SampleProfileReaderBinary::read() {
269
auto FName(readString());
270
if (std::error_code EC = FName.getError())
273
Profiles[*FName] = FunctionSamples();
274
FunctionSamples &FProfile = Profiles[*FName];
276
auto Val = readNumber<unsigned>();
277
if (std::error_code EC = Val.getError())
279
FProfile.addTotalSamples(*Val);
281
Val = readNumber<unsigned>();
282
if (std::error_code EC = Val.getError())
284
FProfile.addHeadSamples(*Val);
286
// Read the samples in the body.
287
auto NumRecords = readNumber<unsigned>();
288
if (std::error_code EC = NumRecords.getError())
290
for (unsigned I = 0; I < *NumRecords; ++I) {
291
auto LineOffset = readNumber<uint64_t>();
292
if (std::error_code EC = LineOffset.getError())
295
auto Discriminator = readNumber<uint64_t>();
296
if (std::error_code EC = Discriminator.getError())
299
auto NumSamples = readNumber<uint64_t>();
300
if (std::error_code EC = NumSamples.getError())
303
auto NumCalls = readNumber<unsigned>();
304
if (std::error_code EC = NumCalls.getError())
307
for (unsigned J = 0; J < *NumCalls; ++J) {
308
auto CalledFunction(readString());
309
if (std::error_code EC = CalledFunction.getError())
312
auto CalledFunctionSamples = readNumber<uint64_t>();
313
if (std::error_code EC = CalledFunctionSamples.getError())
316
FProfile.addCalledTargetSamples(*LineOffset, *Discriminator,
318
*CalledFunctionSamples);
321
FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples);
325
return sampleprof_error::success;
328
std::error_code SampleProfileReaderBinary::readHeader() {
329
Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
330
End = Data + Buffer->getBufferSize();
332
// Read and check the magic identifier.
333
auto Magic = readNumber<uint64_t>();
334
if (std::error_code EC = Magic.getError())
336
else if (*Magic != SPMagic())
337
return sampleprof_error::bad_magic;
339
// Read the version number.
340
auto Version = readNumber<uint64_t>();
341
if (std::error_code EC = Version.getError())
343
else if (*Version != SPVersion())
344
return sampleprof_error::unsupported_version;
346
return sampleprof_error::success;
349
bool SampleProfileReaderBinary::hasFormat(const MemoryBuffer &Buffer) {
350
const uint8_t *Data =
351
reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
352
uint64_t Magic = decodeULEB128(Data);
353
return Magic == SPMagic();
356
/// \brief Prepare a memory buffer for the contents of \p Filename.
358
/// \returns an error code indicating the status of the buffer.
359
static ErrorOr<std::unique_ptr<MemoryBuffer>>
360
setupMemoryBuffer(std::string Filename) {
361
auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
362
if (std::error_code EC = BufferOrErr.getError())
364
auto Buffer = std::move(BufferOrErr.get());
366
// Sanity check the file.
367
if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
368
return sampleprof_error::too_large;
370
return std::move(Buffer);
373
/// \brief Create a sample profile reader based on the format of the input file.
375
/// \param Filename The file to open.
377
/// \param Reader The reader to instantiate according to \p Filename's format.
379
/// \param C The LLVM context to use to emit diagnostics.
381
/// \returns an error code indicating the status of the created reader.
382
ErrorOr<std::unique_ptr<SampleProfileReader>>
383
SampleProfileReader::create(StringRef Filename, LLVMContext &C) {
384
auto BufferOrError = setupMemoryBuffer(Filename);
385
if (std::error_code EC = BufferOrError.getError())
388
auto Buffer = std::move(BufferOrError.get());
389
std::unique_ptr<SampleProfileReader> Reader;
390
if (SampleProfileReaderBinary::hasFormat(*Buffer))
391
Reader.reset(new SampleProfileReaderBinary(std::move(Buffer), C));
393
Reader.reset(new SampleProfileReaderText(std::move(Buffer), C));
395
if (std::error_code EC = Reader->readHeader())
398
return std::move(Reader);