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"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
|
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
2017-03-03 09:07:34 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2014-10-31 02:00:06 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2017-03-03 09:07:34 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
2014-10-31 02:00:06 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2017-03-03 09:07:34 +08:00
|
|
|
using namespace sampleprof;
|
2014-10-31 02:00:06 +08:00
|
|
|
|
2018-09-07 06:03:37 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace sampleprof {
|
|
|
|
SampleProfileFormat FunctionSamples::Format;
|
|
|
|
DenseMap<uint64_t, StringRef> FunctionSamples::GUIDToFuncNameMap;
|
|
|
|
Module *FunctionSamples::CurrentModule;
|
|
|
|
} // namespace sampleprof
|
|
|
|
} // namespace llvm
|
|
|
|
|
2014-10-31 02:00:06 +08:00
|
|
|
namespace {
|
2017-03-03 09:07:34 +08:00
|
|
|
|
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"; }
|
2017-03-03 09:07:34 +08:00
|
|
|
|
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";
|
2018-09-15 04:52:59 +08:00
|
|
|
case sampleprof_error::ostream_seek_unsupported:
|
|
|
|
return "Ostream does not support seek";
|
2014-10-31 02:00:06 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("A value of sampleprof_error has no message.");
|
|
|
|
}
|
|
|
|
};
|
2017-03-03 09:07:34 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
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-10-15 22:32:27 +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
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Print the sample record to the stream \p OS indented by \p Indent.
|
2015-11-14 04:24:28 +08:00
|
|
|
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-10-15 22:32:27 +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;
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Print the samples collected for a function on stream \p OS.
|
2015-11-13 01:58:14 +08:00
|
|
|
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);
|
2017-03-03 09:07:34 +08:00
|
|
|
if (!BodySamples.empty()) {
|
2015-11-20 06:18:30 +08: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-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);
|
2017-03-03 09:07:34 +08:00
|
|
|
if (!CallsiteSamples.empty()) {
|
2015-11-20 06:18:30 +08:00
|
|
|
OS << "Samples collected in inlined callsites {\n";
|
2017-04-14 03:52:10 +08:00
|
|
|
SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
|
2015-11-20 06:18:30 +08:00
|
|
|
CallsiteSamples);
|
|
|
|
for (const auto &CS : SortedCallsiteSamples.get()) {
|
2017-04-14 03:52:10 +08:00
|
|
|
for (const auto &FS : CS->second) {
|
|
|
|
OS.indent(Indent + 2);
|
|
|
|
OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
|
|
|
|
FS.second.print(OS, Indent + 4);
|
|
|
|
}
|
2015-11-20 06:18:30 +08:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-22 14:42:57 +08:00
|
|
|
unsigned FunctionSamples::getOffset(const DILocation *DIL) {
|
|
|
|
return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
|
|
|
|
0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FunctionSamples *
|
|
|
|
FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
|
|
|
|
assert(DIL);
|
|
|
|
SmallVector<std::pair<LineLocation, StringRef>, 10> S;
|
|
|
|
|
|
|
|
const DILocation *PrevDIL = DIL;
|
|
|
|
for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
|
|
|
|
S.push_back(std::make_pair(
|
|
|
|
LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
|
|
|
|
PrevDIL->getScope()->getSubprogram()->getLinkageName()));
|
|
|
|
PrevDIL = DIL;
|
|
|
|
}
|
|
|
|
if (S.size() == 0)
|
|
|
|
return this;
|
|
|
|
const FunctionSamples *FS = this;
|
|
|
|
for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
|
|
|
|
FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
|
|
|
|
}
|
|
|
|
return FS;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2017-03-03 09:07:34 +08:00
|
|
|
LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
|
2017-01-28 10:02:38 +08:00
|
|
|
#endif
|