2014-10-30 19:00:06 +01:00
|
|
|
//=-- SampleProf.cpp - Sample profiling format support --------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-10-30 19:00:06 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains common definitions used in the reading and writing of
|
|
|
|
// sample profile data.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ProfileData/SampleProf.h"
|
2018-04-30 16:59:11 +02:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
|
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
2017-03-03 02:07:34 +01:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2019-08-31 04:27:26 +02:00
|
|
|
#include "llvm/Support/Error.h"
|
2014-10-30 19:00:06 +01:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2019-08-31 04:27:26 +02:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2014-10-30 19:00:06 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2017-03-03 02:07:34 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
2014-10-30 19:00:06 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
2017-03-03 02:07:34 +01:00
|
|
|
using namespace sampleprof;
|
2014-10-30 19:00:06 +01:00
|
|
|
|
2018-09-07 00:03:37 +02:00
|
|
|
namespace llvm {
|
|
|
|
namespace sampleprof {
|
|
|
|
SampleProfileFormat FunctionSamples::Format;
|
2020-03-03 22:19:32 +01:00
|
|
|
bool FunctionSamples::UseMD5;
|
2018-09-07 00:03:37 +02:00
|
|
|
} // namespace sampleprof
|
|
|
|
} // namespace llvm
|
|
|
|
|
2014-10-30 19:00:06 +01:00
|
|
|
namespace {
|
2017-03-03 02:07:34 +01:00
|
|
|
|
2016-05-24 22:13:46 +02: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 19:00:06 +01:00
|
|
|
class SampleProfErrorCategoryType : public std::error_category {
|
2016-10-20 01:52:38 +02:00
|
|
|
const char *name() const noexcept override { return "llvm.sampleprof"; }
|
2017-03-03 02:07:34 +01:00
|
|
|
|
2014-10-30 19:00:06 +01: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 04:47:58 +01:00
|
|
|
return "Invalid sample profile data (bad magic)";
|
2014-10-30 19:00:06 +01:00
|
|
|
case sampleprof_error::unsupported_version:
|
2015-11-13 04:47:58 +01:00
|
|
|
return "Unsupported sample profile format version";
|
2014-10-30 19:00:06 +01: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 04:47:58 +01:00
|
|
|
return "Malformed sample profile data";
|
2014-11-01 01:56:55 +01:00
|
|
|
case sampleprof_error::unrecognized_format:
|
2015-11-13 04:47:58 +01:00
|
|
|
return "Unrecognized sample profile encoding format";
|
2015-10-14 00:48:46 +02: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 02:17:24 +02:00
|
|
|
case sampleprof_error::not_implemented:
|
|
|
|
return "Unimplemented feature";
|
2015-12-16 22:45:43 +01:00
|
|
|
case sampleprof_error::counter_overflow:
|
|
|
|
return "Counter overflow";
|
2018-09-14 22:52:59 +02:00
|
|
|
case sampleprof_error::ostream_seek_unsupported:
|
|
|
|
return "Ostream does not support seek";
|
2019-08-31 04:27:26 +02:00
|
|
|
case sampleprof_error::compress_failed:
|
|
|
|
return "Compress failure";
|
|
|
|
case sampleprof_error::uncompress_failed:
|
|
|
|
return "Uncompress failure";
|
|
|
|
case sampleprof_error::zlib_unavailable:
|
|
|
|
return "Zlib is unavailable";
|
2014-10-30 19:00:06 +01:00
|
|
|
}
|
|
|
|
llvm_unreachable("A value of sampleprof_error has no message.");
|
|
|
|
}
|
|
|
|
};
|
2017-03-03 02:07:34 +01:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2014-10-30 19:00:06 +01:00
|
|
|
|
|
|
|
static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
|
|
|
|
|
|
|
|
const std::error_category &llvm::sampleprof_category() {
|
|
|
|
return *ErrorCategory;
|
|
|
|
}
|
2015-11-12 18:58:14 +01:00
|
|
|
|
2015-11-17 20:04:46 +01: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 16:32:27 +02:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-29 21:50:44 +01:00
|
|
|
LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
|
2017-01-28 03:02:38 +01:00
|
|
|
#endif
|
2015-11-17 20:04:46 +01:00
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Print the sample record to the stream \p OS indented by \p Indent.
|
2015-11-13 21:24:28 +01:00
|
|
|
void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
|
|
|
|
OS << NumSamples;
|
|
|
|
if (hasCalls()) {
|
|
|
|
OS << ", calls:";
|
2019-08-20 22:52:00 +02:00
|
|
|
for (const auto &I : getSortedCallTargets())
|
|
|
|
OS << " " << I.first << ":" << I.second;
|
2015-11-13 21:24:28 +01:00
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2017-10-15 16:32:27 +02:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-29 21:50:44 +01:00
|
|
|
LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
|
2017-01-28 03:02:38 +01:00
|
|
|
#endif
|
2015-11-17 20:04:46 +01:00
|
|
|
|
|
|
|
raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
|
|
|
|
const SampleRecord &Sample) {
|
|
|
|
Sample.print(OS, 0);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Print the samples collected for a function on stream \p OS.
|
2015-11-12 18:58:14 +01:00
|
|
|
void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
|
|
|
|
OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
|
|
|
|
<< " sampled lines\n";
|
2015-11-13 21:24:28 +01:00
|
|
|
|
2015-11-19 23:18:30 +01:00
|
|
|
OS.indent(Indent);
|
2017-03-03 02:07:34 +01:00
|
|
|
if (!BodySamples.empty()) {
|
2015-11-19 23:18:30 +01: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 18:58:14 +01:00
|
|
|
OS.indent(Indent);
|
2015-11-19 23:18:30 +01:00
|
|
|
OS << "}\n";
|
|
|
|
} else {
|
|
|
|
OS << "No samples collected in the function's body\n";
|
2015-11-12 18:58:14 +01:00
|
|
|
}
|
2015-11-13 21:24:28 +01:00
|
|
|
|
2015-11-19 23:18:30 +01:00
|
|
|
OS.indent(Indent);
|
2017-03-03 02:07:34 +01:00
|
|
|
if (!CallsiteSamples.empty()) {
|
2015-11-19 23:18:30 +01:00
|
|
|
OS << "Samples collected in inlined callsites {\n";
|
2017-04-13 21:52:10 +02:00
|
|
|
SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
|
2015-11-19 23:18:30 +01:00
|
|
|
CallsiteSamples);
|
|
|
|
for (const auto &CS : SortedCallsiteSamples.get()) {
|
2017-04-13 21:52:10 +02: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-19 23:18:30 +01:00
|
|
|
}
|
2019-10-07 18:30:31 +02:00
|
|
|
OS.indent(Indent);
|
2015-11-19 23:18:30 +01:00
|
|
|
OS << "}\n";
|
|
|
|
} else {
|
|
|
|
OS << "No inlined callsites in this function\n";
|
2015-11-12 18:58:14 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-17 20:04:46 +01:00
|
|
|
|
|
|
|
raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
|
|
|
|
const FunctionSamples &FS) {
|
|
|
|
FS.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:42:57 +01: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 16:32:27 +02:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2017-03-03 02:07:34 +01:00
|
|
|
LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
|
2017-01-28 03:02:38 +01:00
|
|
|
#endif
|
2019-08-31 04:27:26 +02:00
|
|
|
|
2019-10-07 18:12:37 +02:00
|
|
|
std::error_code ProfileSymbolList::read(const uint8_t *Data,
|
|
|
|
uint64_t ListSize) {
|
2019-08-31 04:27:26 +02:00
|
|
|
const char *ListStart = reinterpret_cast<const char *>(Data);
|
|
|
|
uint64_t Size = 0;
|
2019-10-07 18:12:37 +02:00
|
|
|
while (Size < ListSize) {
|
2019-08-31 04:27:26 +02:00
|
|
|
StringRef Str(ListStart + Size);
|
|
|
|
add(Str);
|
|
|
|
Size += Str.size() + 1;
|
|
|
|
}
|
2019-10-07 18:12:37 +02:00
|
|
|
if (Size != ListSize)
|
|
|
|
return sampleprof_error::malformed;
|
2019-08-31 04:27:26 +02:00
|
|
|
return sampleprof_error::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code ProfileSymbolList::write(raw_ostream &OS) {
|
2019-10-07 18:12:37 +02:00
|
|
|
// Sort the symbols before output. If doing compression.
|
|
|
|
// It will make the compression much more effective.
|
2019-08-31 04:27:26 +02:00
|
|
|
std::vector<StringRef> SortedList;
|
|
|
|
SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
|
|
|
|
llvm::sort(SortedList);
|
|
|
|
|
2019-10-07 18:12:37 +02:00
|
|
|
std::string OutputString;
|
2019-08-31 04:27:26 +02:00
|
|
|
for (auto &Sym : SortedList) {
|
2019-10-07 18:12:37 +02:00
|
|
|
OutputString.append(Sym.str());
|
|
|
|
OutputString.append(1, '\0');
|
2019-08-31 04:27:26 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 18:12:37 +02:00
|
|
|
OS << OutputString;
|
2019-08-31 04:27:26 +02:00
|
|
|
return sampleprof_error::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileSymbolList::dump(raw_ostream &OS) const {
|
|
|
|
OS << "======== Dump profile symbol list ========\n";
|
|
|
|
std::vector<StringRef> SortedList;
|
|
|
|
SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
|
|
|
|
llvm::sort(SortedList);
|
|
|
|
|
|
|
|
for (auto &Sym : SortedList)
|
|
|
|
OS << Sym << "\n";
|
|
|
|
}
|