2015-01-18 16:17:27 +00:00
|
|
|
//===- SampleProfWriter.cpp - Write LLVM sample profile data --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the class that writes LLVM sample profiles. It
|
|
|
|
// supports two file formats: text and binary. The textual representation
|
|
|
|
// is useful for debugging and testing purposes. The binary representation
|
|
|
|
// is more compact, resulting in smaller file sizes. However, they can
|
|
|
|
// both be used interchangeably.
|
|
|
|
//
|
|
|
|
// See lib/ProfileData/SampleProfReader.cpp for documentation on each of the
|
|
|
|
// supported formats.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-04-16 16:01:22 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ProfileData/ProfileCommon.h"
|
|
|
|
#include "llvm/ProfileData/SampleProf.h"
|
2015-01-18 16:17:27 +00:00
|
|
|
#include "llvm/ProfileData/SampleProfWriter.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2017-04-16 16:01:22 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-01-18 16:17:27 +00:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2017-04-16 16:01:22 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
2017-05-16 19:46:52 +00:00
|
|
|
#include <set>
|
2017-04-16 16:01:22 +00:00
|
|
|
#include <system_error>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2015-01-18 16:17:27 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
2017-04-16 16:01:22 +00:00
|
|
|
using namespace sampleprof;
|
2015-01-18 16:17:27 +00:00
|
|
|
|
2017-05-16 19:46:52 +00:00
|
|
|
std::error_code
|
|
|
|
SampleProfileWriter::write(const StringMap<FunctionSamples> &ProfileMap) {
|
|
|
|
if (std::error_code EC = writeHeader(ProfileMap))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
// Sort the ProfileMap by total samples.
|
|
|
|
typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples;
|
|
|
|
std::vector<NameFunctionSamples> V;
|
|
|
|
for (const auto &I : ProfileMap)
|
|
|
|
V.push_back(std::make_pair(I.getKey(), &I.second));
|
|
|
|
|
|
|
|
std::stable_sort(
|
|
|
|
V.begin(), V.end(),
|
|
|
|
[](const NameFunctionSamples &A, const NameFunctionSamples &B) {
|
|
|
|
if (A.second->getTotalSamples() == B.second->getTotalSamples())
|
|
|
|
return A.first > B.first;
|
|
|
|
return A.second->getTotalSamples() > B.second->getTotalSamples();
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const auto &I : V) {
|
|
|
|
if (std::error_code EC = write(*I.second))
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
return sampleprof_error::success;
|
|
|
|
}
|
|
|
|
|
2015-01-18 16:17:27 +00:00
|
|
|
/// \brief Write samples to a text file.
|
2015-12-30 11:46:15 +00:00
|
|
|
///
|
|
|
|
/// Note: it may be tempting to implement this in terms of
|
|
|
|
/// FunctionSamples::print(). Please don't. The dump functionality is intended
|
|
|
|
/// for debugging and has no specified form.
|
|
|
|
///
|
|
|
|
/// The format used here is more structured and deliberate because
|
|
|
|
/// it needs to be parsed by the SampleProfileReaderText class.
|
2016-07-23 20:41:05 +00:00
|
|
|
std::error_code SampleProfileWriterText::write(const FunctionSamples &S) {
|
2015-12-30 11:46:15 +00:00
|
|
|
auto &OS = *OutputStream;
|
2016-07-23 20:41:05 +00:00
|
|
|
OS << S.getName() << ":" << S.getTotalSamples();
|
2015-12-30 11:46:15 +00:00
|
|
|
if (Indent == 0)
|
|
|
|
OS << ":" << S.getHeadSamples();
|
|
|
|
OS << "\n";
|
|
|
|
|
|
|
|
SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
|
|
|
|
for (const auto &I : SortedSamples.get()) {
|
|
|
|
LineLocation Loc = I->first;
|
|
|
|
const SampleRecord &Sample = I->second;
|
|
|
|
OS.indent(Indent + 1);
|
2015-01-18 16:17:27 +00:00
|
|
|
if (Loc.Discriminator == 0)
|
|
|
|
OS << Loc.LineOffset << ": ";
|
|
|
|
else
|
|
|
|
OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
|
|
|
|
|
|
|
|
OS << Sample.getSamples();
|
|
|
|
|
|
|
|
for (const auto &J : Sample.getCallTargets())
|
|
|
|
OS << " " << J.first() << ":" << J.second;
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2017-04-16 16:01:22 +00:00
|
|
|
SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
|
2015-12-30 11:46:15 +00:00
|
|
|
S.getCallsiteSamples());
|
|
|
|
Indent += 1;
|
2017-04-16 16:01:22 +00:00
|
|
|
for (const auto &I : SortedCallsiteSamples.get())
|
|
|
|
for (const auto &FS : I->second) {
|
|
|
|
LineLocation Loc = I->first;
|
|
|
|
const FunctionSamples &CalleeSamples = FS.second;
|
|
|
|
OS.indent(Indent);
|
|
|
|
if (Loc.Discriminator == 0)
|
|
|
|
OS << Loc.LineOffset << ": ";
|
|
|
|
else
|
|
|
|
OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
|
|
|
|
if (std::error_code EC = write(CalleeSamples))
|
|
|
|
return EC;
|
|
|
|
}
|
2015-12-30 11:46:15 +00:00
|
|
|
Indent -= 1;
|
|
|
|
|
|
|
|
return sampleprof_error::success;
|
2015-01-18 16:17:27 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
|
|
|
|
const auto &ret = NameTable.find(FName);
|
|
|
|
if (ret == NameTable.end())
|
|
|
|
return sampleprof_error::truncated_name_table;
|
|
|
|
encodeULEB128(ret->second, *OutputStream);
|
|
|
|
return sampleprof_error::success;
|
|
|
|
}
|
2015-01-18 16:17:27 +00:00
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
void SampleProfileWriterBinary::addName(StringRef FName) {
|
2017-05-16 19:46:52 +00:00
|
|
|
NameTable.insert(std::make_pair(FName, 0));
|
2015-12-30 11:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
|
|
|
|
// Add all the names in indirect call targets.
|
|
|
|
for (const auto &I : S.getBodySamples()) {
|
|
|
|
const SampleRecord &Sample = I.second;
|
|
|
|
for (const auto &J : Sample.getCallTargets())
|
|
|
|
addName(J.first());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively add all the names for inlined callsites.
|
2017-04-16 16:01:22 +00:00
|
|
|
for (const auto &J : S.getCallsiteSamples())
|
|
|
|
for (const auto &FS : J.second) {
|
|
|
|
const FunctionSamples &CalleeSamples = FS.second;
|
|
|
|
addName(CalleeSamples.getName());
|
|
|
|
addNames(CalleeSamples);
|
|
|
|
}
|
2015-12-30 11:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code SampleProfileWriterBinary::writeHeader(
|
|
|
|
const StringMap<FunctionSamples> &ProfileMap) {
|
|
|
|
auto &OS = *OutputStream;
|
|
|
|
|
|
|
|
// Write file magic identifier.
|
2015-01-18 16:17:27 +00:00
|
|
|
encodeULEB128(SPMagic(), OS);
|
|
|
|
encodeULEB128(SPVersion(), OS);
|
2015-12-30 11:46:15 +00:00
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
computeSummary(ProfileMap);
|
|
|
|
if (auto EC = writeSummary())
|
|
|
|
return EC;
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
// Generate the name table for all the functions referenced in the profile.
|
|
|
|
for (const auto &I : ProfileMap) {
|
|
|
|
addName(I.first());
|
|
|
|
addNames(I.second);
|
|
|
|
}
|
|
|
|
|
2017-05-16 19:46:52 +00:00
|
|
|
// Sort the names to make NameTable is deterministic.
|
|
|
|
std::set<StringRef> V;
|
|
|
|
for (const auto &I : NameTable)
|
|
|
|
V.insert(I.first);
|
|
|
|
int i = 0;
|
|
|
|
for (const StringRef &N : V)
|
|
|
|
NameTable[N] = i++;
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
// Write out the name table.
|
|
|
|
encodeULEB128(NameTable.size(), OS);
|
2017-05-16 19:46:52 +00:00
|
|
|
for (auto N : V) {
|
|
|
|
OS << N;
|
2015-12-30 11:46:15 +00:00
|
|
|
encodeULEB128(0, OS);
|
|
|
|
}
|
|
|
|
return sampleprof_error::success;
|
2015-01-18 16:17:27 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
std::error_code SampleProfileWriterBinary::writeSummary() {
|
|
|
|
auto &OS = *OutputStream;
|
|
|
|
encodeULEB128(Summary->getTotalCount(), OS);
|
|
|
|
encodeULEB128(Summary->getMaxCount(), OS);
|
|
|
|
encodeULEB128(Summary->getMaxFunctionCount(), OS);
|
|
|
|
encodeULEB128(Summary->getNumCounts(), OS);
|
|
|
|
encodeULEB128(Summary->getNumFunctions(), OS);
|
|
|
|
std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary();
|
|
|
|
encodeULEB128(Entries.size(), OS);
|
|
|
|
for (auto Entry : Entries) {
|
|
|
|
encodeULEB128(Entry.Cutoff, OS);
|
|
|
|
encodeULEB128(Entry.MinCount, OS);
|
|
|
|
encodeULEB128(Entry.NumCounts, OS);
|
|
|
|
}
|
|
|
|
return sampleprof_error::success;
|
|
|
|
}
|
|
|
|
std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
|
2015-12-30 11:46:15 +00:00
|
|
|
auto &OS = *OutputStream;
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
if (std::error_code EC = writeNameIdx(S.getName()))
|
2015-12-30 11:46:15 +00:00
|
|
|
return EC;
|
2015-01-18 16:17:27 +00:00
|
|
|
|
|
|
|
encodeULEB128(S.getTotalSamples(), OS);
|
2015-12-30 11:46:15 +00:00
|
|
|
|
|
|
|
// Emit all the body samples.
|
2015-01-18 16:17:27 +00:00
|
|
|
encodeULEB128(S.getBodySamples().size(), OS);
|
|
|
|
for (const auto &I : S.getBodySamples()) {
|
|
|
|
LineLocation Loc = I.first;
|
|
|
|
const SampleRecord &Sample = I.second;
|
|
|
|
encodeULEB128(Loc.LineOffset, OS);
|
|
|
|
encodeULEB128(Loc.Discriminator, OS);
|
|
|
|
encodeULEB128(Sample.getSamples(), OS);
|
|
|
|
encodeULEB128(Sample.getCallTargets().size(), OS);
|
|
|
|
for (const auto &J : Sample.getCallTargets()) {
|
2015-12-30 11:46:15 +00:00
|
|
|
StringRef Callee = J.first();
|
|
|
|
uint64_t CalleeSamples = J.second;
|
|
|
|
if (std::error_code EC = writeNameIdx(Callee))
|
|
|
|
return EC;
|
2015-01-18 16:17:27 +00:00
|
|
|
encodeULEB128(CalleeSamples, OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
// Recursively emit all the callsite samples.
|
|
|
|
encodeULEB128(S.getCallsiteSamples().size(), OS);
|
2017-04-16 16:01:22 +00:00
|
|
|
for (const auto &J : S.getCallsiteSamples())
|
|
|
|
for (const auto &FS : J.second) {
|
|
|
|
LineLocation Loc = J.first;
|
|
|
|
const FunctionSamples &CalleeSamples = FS.second;
|
|
|
|
encodeULEB128(Loc.LineOffset, OS);
|
|
|
|
encodeULEB128(Loc.Discriminator, OS);
|
|
|
|
if (std::error_code EC = writeBody(CalleeSamples))
|
|
|
|
return EC;
|
|
|
|
}
|
2015-12-30 11:46:15 +00:00
|
|
|
|
|
|
|
return sampleprof_error::success;
|
2015-01-18 16:17:27 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
/// \brief Write samples of a top-level function to a binary file.
|
|
|
|
///
|
|
|
|
/// \returns true if the samples were written successfully, false otherwise.
|
2016-07-23 20:41:05 +00:00
|
|
|
std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) {
|
2015-12-30 11:46:15 +00:00
|
|
|
encodeULEB128(S.getHeadSamples(), *OutputStream);
|
2016-07-23 20:41:05 +00:00
|
|
|
return writeBody(S);
|
2015-12-30 11:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Create a sample profile file writer based on the specified format.
|
2015-01-18 16:17:27 +00:00
|
|
|
///
|
|
|
|
/// \param Filename The file to create.
|
|
|
|
///
|
|
|
|
/// \param Writer The writer to instantiate according to the specified format.
|
|
|
|
///
|
|
|
|
/// \param Format Encoding format for the profile file.
|
|
|
|
///
|
|
|
|
/// \returns an error code indicating the status of the created writer.
|
|
|
|
ErrorOr<std::unique_ptr<SampleProfileWriter>>
|
|
|
|
SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
|
|
|
|
std::error_code EC;
|
2015-12-30 11:46:15 +00:00
|
|
|
std::unique_ptr<raw_ostream> OS;
|
|
|
|
if (Format == SPF_Binary)
|
|
|
|
OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_None));
|
|
|
|
else
|
|
|
|
OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_Text));
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
return create(OS, Format);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Create a sample profile stream writer based on the specified format.
|
|
|
|
///
|
|
|
|
/// \param OS The output stream to store the profile data to.
|
|
|
|
///
|
|
|
|
/// \param Writer The writer to instantiate according to the specified format.
|
|
|
|
///
|
|
|
|
/// \param Format Encoding format for the profile file.
|
|
|
|
///
|
|
|
|
/// \returns an error code indicating the status of the created writer.
|
|
|
|
ErrorOr<std::unique_ptr<SampleProfileWriter>>
|
|
|
|
SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
|
|
|
|
SampleProfileFormat Format) {
|
|
|
|
std::error_code EC;
|
2015-01-18 16:17:27 +00:00
|
|
|
std::unique_ptr<SampleProfileWriter> Writer;
|
|
|
|
|
|
|
|
if (Format == SPF_Binary)
|
2015-12-30 11:46:15 +00:00
|
|
|
Writer.reset(new SampleProfileWriterBinary(OS));
|
2015-01-18 16:17:27 +00:00
|
|
|
else if (Format == SPF_Text)
|
2015-12-30 11:46:15 +00:00
|
|
|
Writer.reset(new SampleProfileWriterText(OS));
|
|
|
|
else if (Format == SPF_GCC)
|
|
|
|
EC = sampleprof_error::unsupported_writing_format;
|
2015-01-18 16:17:27 +00:00
|
|
|
else
|
|
|
|
EC = sampleprof_error::unrecognized_format;
|
|
|
|
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
return std::move(Writer);
|
|
|
|
}
|
2016-07-23 20:41:05 +00:00
|
|
|
|
|
|
|
void SampleProfileWriter::computeSummary(
|
|
|
|
const StringMap<FunctionSamples> &ProfileMap) {
|
|
|
|
SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
|
|
|
|
for (const auto &I : ProfileMap) {
|
|
|
|
const FunctionSamples &Profile = I.second;
|
|
|
|
Builder.addRecord(Profile);
|
|
|
|
}
|
|
|
|
Summary = Builder.getSummary();
|
|
|
|
}
|