2014-10-30 18:00:06 +00: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"
|
2017-03-03 01:07:34 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2014-10-30 18:00:06 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2017-03-03 01:07:34 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
2014-10-30 18:00:06 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
2017-03-03 01:07:34 +00:00
|
|
|
using namespace sampleprof;
|
2014-10-30 18:00:06 +00:00
|
|
|
|
|
|
|
namespace {
|
2017-03-03 01:07:34 +00:00
|
|
|
|
2016-05-24 20:13:46 +00: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-30 18:00:06 +00:00
|
|
|
class SampleProfErrorCategoryType : public std::error_category {
|
2016-10-19 23:52:38 +00:00
|
|
|
const char *name() const noexcept override { return "llvm.sampleprof"; }
|
2017-03-03 01:07:34 +00:00
|
|
|
|
2014-10-30 18:00:06 +00: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 03:47:58 +00:00
|
|
|
return "Invalid sample profile data (bad magic)";
|
2014-10-30 18:00:06 +00:00
|
|
|
case sampleprof_error::unsupported_version:
|
2015-11-13 03:47:58 +00:00
|
|
|
return "Unsupported sample profile format version";
|
2014-10-30 18:00:06 +00: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 03:47:58 +00:00
|
|
|
return "Malformed sample profile data";
|
2014-11-01 00:56:55 +00:00
|
|
|
case sampleprof_error::unrecognized_format:
|
2015-11-13 03:47:58 +00:00
|
|
|
return "Unrecognized sample profile encoding format";
|
2015-10-13 22:48:46 +00: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 00:17:24 +00:00
|
|
|
case sampleprof_error::not_implemented:
|
|
|
|
return "Unimplemented feature";
|
2015-12-16 21:45:43 +00:00
|
|
|
case sampleprof_error::counter_overflow:
|
|
|
|
return "Counter overflow";
|
2014-10-30 18:00:06 +00:00
|
|
|
}
|
|
|
|
llvm_unreachable("A value of sampleprof_error has no message.");
|
|
|
|
}
|
|
|
|
};
|
2017-03-03 01:07:34 +00:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2014-10-30 18:00:06 +00:00
|
|
|
|
|
|
|
static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
|
|
|
|
|
|
|
|
const std::error_category &llvm::sampleprof_category() {
|
|
|
|
return *ErrorCategory;
|
|
|
|
}
|
2015-11-12 17:58:14 +00:00
|
|
|
|
2015-11-17 19:04:46 +00: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 02:02:38 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-29 20:50:44 +00:00
|
|
|
LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
|
2017-01-28 02:02:38 +00:00
|
|
|
#endif
|
2015-11-17 19:04:46 +00:00
|
|
|
|
2015-11-13 20:24:28 +00: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 02:02:38 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-29 20:50:44 +00:00
|
|
|
LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
|
2017-01-28 02:02:38 +00:00
|
|
|
#endif
|
2015-11-17 19:04:46 +00:00
|
|
|
|
|
|
|
raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
|
|
|
|
const SampleRecord &Sample) {
|
|
|
|
Sample.print(OS, 0);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:58:14 +00: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-13 20:24:28 +00:00
|
|
|
|
2015-11-19 22:18:30 +00:00
|
|
|
OS.indent(Indent);
|
2017-03-03 01:07:34 +00:00
|
|
|
if (!BodySamples.empty()) {
|
2015-11-19 22:18:30 +00:00
|
|
|
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-12 17:58:14 +00:00
|
|
|
OS.indent(Indent);
|
2015-11-19 22:18:30 +00:00
|
|
|
OS << "}\n";
|
|
|
|
} else {
|
|
|
|
OS << "No samples collected in the function's body\n";
|
2015-11-12 17:58:14 +00:00
|
|
|
}
|
2015-11-13 20:24:28 +00:00
|
|
|
|
2015-11-19 22:18:30 +00:00
|
|
|
OS.indent(Indent);
|
2017-03-03 01:07:34 +00:00
|
|
|
if (!CallsiteSamples.empty()) {
|
2015-11-19 22:18:30 +00:00
|
|
|
OS << "Samples collected in inlined callsites {\n";
|
2016-03-03 18:09:32 +00:00
|
|
|
SampleSorter<LineLocation, FunctionSamples> SortedCallsiteSamples(
|
2015-11-19 22:18:30 +00:00
|
|
|
CallsiteSamples);
|
|
|
|
for (const auto &CS : SortedCallsiteSamples.get()) {
|
|
|
|
OS.indent(Indent + 2);
|
2016-03-03 18:09:32 +00:00
|
|
|
OS << CS->first << ": inlined callee: " << CS->second.getName() << ": ";
|
2015-11-19 22:18:30 +00:00
|
|
|
CS->second.print(OS, Indent + 4);
|
|
|
|
}
|
|
|
|
OS << "}\n";
|
|
|
|
} else {
|
|
|
|
OS << "No inlined callsites in this function\n";
|
2015-11-12 17:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-17 19:04:46 +00:00
|
|
|
|
|
|
|
raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
|
|
|
|
const FunctionSamples &FS) {
|
|
|
|
FS.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2017-01-28 02:02:38 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2017-03-03 01:07:34 +00:00
|
|
|
LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
|
2017-01-28 02:02:38 +00:00
|
|
|
#endif
|