105 using namespace llvm::sampleprof;
106 using namespace llvm;
112 OS << TotalSamples <<
", " << TotalHeadSamples <<
", " << BodySamples.size()
113 <<
" sampled lines\n";
114 for (
const auto &
SI : BodySamples) {
119 <<
", number of samples: " << Sample.
getSamples();
123 OS <<
" " <<
I.first() <<
":" <<
I.second;
136 OS <<
"Function: " << FName <<
": ";
137 Profiles[FName].print(OS);
142 for (
const auto &
I : Profiles)
143 dumpFunctionProfile(
I.getKey(), OS);
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]+)");
175 if (!HeadRE.
match(*LineIt, &Matches)) {
177 "Expected 'mangled_name:NUM:NUM', found " + *LineIt);
180 assert(Matches.
size() == 4);
182 unsigned NumSamples, NumHeadSamples;
183 Matches[2].getAsInteger(10, NumSamples);
184 Matches[3].getAsInteger(10, NumHeadSamples);
193 while (!LineIt.
is_at_eof() && isdigit((*LineIt)[0])) {
194 if (!LineSampleRE.
match(*LineIt, &Matches)) {
197 "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " + *LineIt);
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);
209 std::string CallsLine(Matches[4]);
210 while (CallsLine !=
"") {
212 if (!CallSampleRE.
match(CallsLine, &CallSample)) {
214 "Expected 'mangled_name:NUM', found " + CallsLine);
217 StringRef CalledFunction = CallSample[1];
218 unsigned CalledFunctionSamples;
219 CallSample[2].getAsInteger(10, CalledFunctionSamples);
221 CalledFunction, CalledFunctionSamples);
222 CallsLine = CallSampleRE.
sub(
"", CallsLine);
234 unsigned NumBytesRead = 0;
238 if (Val > std::numeric_limits<T>::max())
240 else if (
Data + NumBytesRead > End)
246 reportParseError(0, EC.message());
250 Data += NumBytesRead;
251 return static_cast<T>(Val);
259 reportParseError(0, EC.message());
269 auto FName(readString());
270 if (std::error_code EC = FName.getError())
276 auto Val = readNumber<unsigned>();
277 if (std::error_code EC = Val.getError())
281 Val = readNumber<unsigned>();
282 if (std::error_code EC = Val.getError())
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())
318 *CalledFunctionSamples);
321 FProfile.
addBodySamples(*LineOffset, *Discriminator, *NumSamples);
329 Data =
reinterpret_cast<const uint8_t *
>(Buffer->getBufferStart());
330 End =
Data + Buffer->getBufferSize();
333 auto Magic = readNumber<uint64_t>();
334 if (std::error_code EC =
Magic.getError())
340 auto Version = readNumber<uint64_t>();
341 if (std::error_code EC =
Version.getError())
350 const uint8_t *
Data =
362 if (std::error_code EC = BufferOrErr.getError())
364 auto Buffer = std::move(BufferOrErr.get());
367 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
370 return std::move(Buffer);
385 if (std::error_code EC = BufferOrError.getError())
388 auto Buffer = std::move(BufferOrError.get());
389 std::unique_ptr<SampleProfileReader> Reader;
395 if (std::error_code EC = Reader->readHeader())
398 return std::move(Reader);
Represents either an error or a value T.
void addBodySamples(int LineOffset, unsigned Discriminator, unsigned Num)
size_t size() const
size - Get the string size.
const char * getBufferStart() const
int64_t line_number() const
Return the current line number. May return any number at EOF.
std::error_code read() override
Read sample profiles from the associated file.
void print(raw_ostream &OS=dbgs())
Print the samples collected for a function on stream OS.
std::error_code readHeader() override
Read and validate the file header.
void addTotalSamples(unsigned Num)
A forward iterator which reads text lines from a buffer.
const CallTargetMap & getCallTargets() const
Representation of the samples collected for a function.
static ErrorOr< std::unique_ptr< MemoryBuffer > > setupMemoryBuffer(std::string Filename)
Prepare a memory buffer for the contents of Filename.
Representation of a single sample record.
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
void addHeadSamples(unsigned Num)
static ErrorOr< std::unique_ptr< SampleProfileReader > > create(StringRef Filename, LLVMContext &C)
Create a sample profile reader appropriate to the file format.
static uint64_t SPVersion()
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr)
Utility function to decode a ULEB128 value.
This is an important class for using LLVM in a threaded context.
ErrorOr< T > readNumber()
Read a numeric value of type T from the profile.
ErrorOr< StringRef > readString()
Read a string from the profile.
std::string sub(StringRef Repl, StringRef String, std::string *Error=nullptr)
sub - Return the result of replacing the first match of the regex in String with the Repl string...
void dumpFunctionProfile(StringRef FName, raw_ostream &OS=dbgs())
Print the profile for FName on stream OS.
static const char *const Magic
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
This interface provides simple read-only access to a block of memory, and provides simple methods for...
bool hasCalls() const
Return true if this sample record contains function calls.
static uint64_t SPMagic()
Represents the relative location of an instruction.
unsigned getSamples() const
Provides ErrorOr<T> smart pointer.
bool is_at_eof() const
Return true if we've reached EOF or are an "end" iterator.
This class implements an extremely fast bulk output stream that can only output to a stream...
bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr)
matches - Match the regex against a given String.
StringRef - Represent a constant reference to a string, i.e.
void dump(raw_ostream &OS=dbgs())
Print all the profiles on stream OS.
std::error_code read() override
Read sample profiles from the associated file.
void addCalledTargetSamples(int LineOffset, unsigned Discriminator, std::string FName, unsigned Num)