2014-10-31 02:00:06 +08:00
|
|
|
//=-- SampleProf.cpp - Sample profiling format support --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains common definitions used in the reading and writing of
|
|
|
|
// sample profile data.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ProfileData/SampleProf.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
|
2015-11-13 01:58:14 +08:00
|
|
|
using namespace llvm::sampleprof;
|
2014-10-31 02:00:06 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2016-05-25 04:13:46 +08:00
|
|
|
// FIXME: This class is only here to support the transition to llvm::Error. It
|
|
|
|
// will be removed once this transition is complete. Clients should prefer to
|
|
|
|
// deal with the Error value directly, rather than converting to error_code.
|
2014-10-31 02:00:06 +08:00
|
|
|
class SampleProfErrorCategoryType : public std::error_category {
|
2016-10-20 07:52:38 +08:00
|
|
|
const char *name() const noexcept override { return "llvm.sampleprof"; }
|
2014-10-31 02:00:06 +08:00
|
|
|
std::string message(int IE) const override {
|
|
|
|
sampleprof_error E = static_cast<sampleprof_error>(IE);
|
|
|
|
switch (E) {
|
|
|
|
case sampleprof_error::success:
|
|
|
|
return "Success";
|
|
|
|
case sampleprof_error::bad_magic:
|
2015-11-13 11:47:58 +08:00
|
|
|
return "Invalid sample profile data (bad magic)";
|
2014-10-31 02:00:06 +08:00
|
|
|
case sampleprof_error::unsupported_version:
|
2015-11-13 11:47:58 +08:00
|
|
|
return "Unsupported sample profile format version";
|
2014-10-31 02:00:06 +08:00
|
|
|
case sampleprof_error::too_large:
|
|
|
|
return "Too much profile data";
|
|
|
|
case sampleprof_error::truncated:
|
|
|
|
return "Truncated profile data";
|
|
|
|
case sampleprof_error::malformed:
|
2015-11-13 11:47:58 +08:00
|
|
|
return "Malformed sample profile data";
|
2014-11-01 08:56:55 +08:00
|
|
|
case sampleprof_error::unrecognized_format:
|
2015-11-13 11:47:58 +08:00
|
|
|
return "Unrecognized sample profile encoding format";
|
2015-10-14 06:48:46 +08:00
|
|
|
case sampleprof_error::unsupported_writing_format:
|
|
|
|
return "Profile encoding format unsupported for writing operations";
|
|
|
|
case sampleprof_error::truncated_name_table:
|
|
|
|
return "Truncated function name table";
|
2015-09-17 08:17:24 +08:00
|
|
|
case sampleprof_error::not_implemented:
|
|
|
|
return "Unimplemented feature";
|
2015-12-17 05:45:43 +08:00
|
|
|
case sampleprof_error::counter_overflow:
|
|
|
|
return "Counter overflow";
|
2014-10-31 02:00:06 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("A value of sampleprof_error has no message.");
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2014-10-31 02:00:06 +08:00
|
|
|
|
|
|
|
static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
|
|
|
|
|
|
|
|
const std::error_category &llvm::sampleprof_category() {
|
|
|
|
return *ErrorCategory;
|
|
|
|
}
|
2015-11-13 01:58:14 +08:00
|
|
|
|
2015-11-18 03:04:46 +08:00
|
|
|
void LineLocation::print(raw_ostream &OS) const {
|
|
|
|
OS << LineOffset;
|
|
|
|
if (Discriminator > 0)
|
|
|
|
OS << "." << Discriminator;
|
|
|
|
}
|
|
|
|
|
|
|
|
raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
|
|
|
|
const LineLocation &Loc) {
|
|
|
|
Loc.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2017-01-28 10:02:38 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-30 04:50:44 +08:00
|
|
|
LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
|
2017-01-28 10:02:38 +08:00
|
|
|
#endif
|
2015-11-18 03:04:46 +08:00
|
|
|
|
2015-11-14 04:24:28 +08:00
|
|
|
/// \brief Print the sample record to the stream \p OS indented by \p Indent.
|
|
|
|
void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
|
|
|
|
OS << NumSamples;
|
|
|
|
if (hasCalls()) {
|
|
|
|
OS << ", calls:";
|
|
|
|
for (const auto &I : getCallTargets())
|
|
|
|
OS << " " << I.first() << ":" << I.second;
|
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2017-01-28 10:02:38 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-30 04:50:44 +08:00
|
|
|
LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
|
2017-01-28 10:02:38 +08:00
|
|
|
#endif
|
2015-11-18 03:04:46 +08:00
|
|
|
|
|
|
|
raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
|
|
|
|
const SampleRecord &Sample) {
|
|
|
|
Sample.print(OS, 0);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2015-11-13 01:58:14 +08:00
|
|
|
/// \brief Print the samples collected for a function on stream \p OS.
|
|
|
|
void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
|
|
|
|
OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
|
|
|
|
<< " sampled lines\n";
|
2015-11-14 04:24:28 +08:00
|
|
|
|
2015-11-20 06:18:30 +08:00
|
|
|
OS.indent(Indent);
|
|
|
|
if (BodySamples.size() > 0) {
|
|
|
|
OS << "Samples collected in the function's body {\n";
|
|
|
|
SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
|
|
|
|
for (const auto &SI : SortedBodySamples.get()) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << SI->first << ": " << SI->second;
|
|
|
|
}
|
2015-11-13 01:58:14 +08:00
|
|
|
OS.indent(Indent);
|
2015-11-20 06:18:30 +08:00
|
|
|
OS << "}\n";
|
|
|
|
} else {
|
|
|
|
OS << "No samples collected in the function's body\n";
|
2015-11-13 01:58:14 +08:00
|
|
|
}
|
2015-11-14 04:24:28 +08:00
|
|
|
|
2015-11-20 06:18:30 +08:00
|
|
|
OS.indent(Indent);
|
|
|
|
if (CallsiteSamples.size() > 0) {
|
|
|
|
OS << "Samples collected in inlined callsites {\n";
|
2016-03-04 02:09:32 +08:00
|
|
|
SampleSorter<LineLocation, FunctionSamples> SortedCallsiteSamples(
|
2015-11-20 06:18:30 +08:00
|
|
|
CallsiteSamples);
|
|
|
|
for (const auto &CS : SortedCallsiteSamples.get()) {
|
|
|
|
OS.indent(Indent + 2);
|
2016-03-04 02:09:32 +08:00
|
|
|
OS << CS->first << ": inlined callee: " << CS->second.getName() << ": ";
|
2015-11-20 06:18:30 +08:00
|
|
|
CS->second.print(OS, Indent + 4);
|
|
|
|
}
|
|
|
|
OS << "}\n";
|
|
|
|
} else {
|
|
|
|
OS << "No inlined callsites in this function\n";
|
2015-11-13 01:58:14 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-18 03:04:46 +08:00
|
|
|
|
|
|
|
raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
|
|
|
|
const FunctionSamples &FS) {
|
|
|
|
FS.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2017-01-28 10:02:38 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
|
|
|
LLVM_DUMP_METHOD void FunctionSamples::dump(void) const { print(dbgs(), 0); }
|
|
|
|
#endif
|