36 #include <system_error> 41 using namespace sampleprof;
46 typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples;
47 std::vector<NameFunctionSamples> V;
48 for (
const auto &
I : ProfileMap)
49 V.push_back(std::make_pair(
I.getKey(), &
I.second));
52 V, [](
const NameFunctionSamples &A,
const NameFunctionSamples &
B) {
53 if (A.second->getTotalSamples() == B.second->getTotalSamples())
54 return A.first > B.first;
55 return A.second->getTotalSamples() > B.second->getTotalSamples();
58 for (
const auto &
I : V) {
77 SampleProfileWriterExtBinaryBase::getEntryInLayout(
SecType Type) {
79 SectionHdrLayout.begin(), SectionHdrLayout.end(),
80 [=](
const auto &
Entry) ->
bool {
return Entry.Type == Type; });
88 auto &
Entry = getEntryInLayout(Type);
95 std::error_code SampleProfileWriterExtBinaryBase::compressAndOutput() {
98 std::string &UncompressedStrings =
100 if (UncompressedStrings.size() == 0)
110 OS << CompressedStrings.
str();
111 UncompressedStrings.clear();
118 uint64_t SectionStart) {
119 auto Entry = getEntryInLayout(Type);
122 if (std::error_code EC = compressAndOutput())
125 SecHdrTable.push_back({Type,
Entry.Flags, SectionStart - FileStart,
135 std::string LocalBuf;
136 LocalBufStream = std::make_unique<raw_string_ostream>(LocalBuf);
137 if (std::error_code EC = writeSections(ProfileMap))
140 if (std::error_code EC = writeSecHdrTable())
150 FuncOffsetTable[
Name] = Offset - SecLBRProfileStart;
155 std::error_code SampleProfileWriterExtBinary::writeFuncOffsetTable() {
162 for (
auto entry : FuncOffsetTable) {
163 writeNameIdx(
entry.first);
169 std::error_code SampleProfileWriterExtBinary::writeSections(
173 if (
auto EC = writeSummary())
175 if (std::error_code EC = addNewSection(
SecProfSummary, SectionStart))
180 for (
const auto &
I : ProfileMap) {
185 if (std::error_code EC = addNewSection(
SecNameTable, SectionStart))
192 if (std::error_code EC = addNewSection(
SecLBRProfile, SectionStart))
195 if (ProfSymList && ProfSymList->toCompress())
199 if (ProfSymList && ProfSymList->size() > 0)
200 if (std::error_code EC = ProfSymList->write(*
OutputStream))
206 if (std::error_code EC = writeFuncOffsetTable())
218 if (std::error_code EC = writeFuncOffsetTable())
239 for (
const auto &
I : SortedSamples.get()) {
242 OS.indent(Indent + 1);
251 OS <<
" " << J.first <<
":" << J.second;
258 for (
const auto &
I : SortedCallsiteSamples.get())
259 for (
const auto &
FS :
I->second) {
267 if (std::error_code EC =
writeSample(CalleeSamples))
276 const auto &ret = NameTable.find(FName);
277 if (ret == NameTable.end())
284 NameTable.insert(std::make_pair(FName, 0));
297 for (
const auto &
FS : J.second) {
299 addName(CalleeSamples.
getName());
300 addNames(CalleeSamples);
306 for (
const auto &
I : NameTable)
315 std::set<StringRef> V;
316 stablizeNameTable(V);
332 uint64_t FuncOffsetTableStart = OS.
tell();
333 if (OFS.seek(TableOffset) == (uint64_t)-1)
336 Writer.
write(FuncOffsetTableStart);
337 if (OFS.seek(FuncOffsetTableStart) == (uint64_t)-1)
344 for (
auto entry : FuncOffsetTable) {
345 writeNameIdx(
entry.first);
353 std::set<StringRef> V;
354 stablizeNameTable(V);
378 if (
auto EC = writeSummary())
382 for (
const auto &
I : ProfileMap) {
392 for (
auto &
Entry : SectionHdrLayout)
400 void SampleProfileWriterExtBinaryBase::addSectionFlags(
SecType Type,
402 for (
auto &
Entry : SectionHdrLayout) {
408 void SampleProfileWriterExtBinaryBase::allocSecHdrTable() {
411 Writer.
write(static_cast<uint64_t>(SectionHdrLayout.size()));
413 for (
uint32_t i = 0; i < SectionHdrLayout.size(); i++) {
414 Writer.
write(static_cast<uint64_t>(-1));
415 Writer.
write(static_cast<uint64_t>(-1));
416 Writer.
write(static_cast<uint64_t>(-1));
417 Writer.
write(static_cast<uint64_t>(-1));
421 std::error_code SampleProfileWriterExtBinaryBase::writeSecHdrTable() {
426 if (OFS.seek(SecHdrTableOffset) == (uint64_t)-1)
431 for (
uint32_t i = 0; i < SecHdrTable.size(); i++) {
442 for (
uint32_t i = 0; i < SectionHdrLayout.size(); i++) {
444 Writer.
write(static_cast<uint64_t>(SecHdrTable[idx].
Type));
445 Writer.
write(static_cast<uint64_t>(SecHdrTable[idx].Flags));
446 Writer.
write(static_cast<uint64_t>(SecHdrTable[idx].
Offset));
447 Writer.
write(static_cast<uint64_t>(SecHdrTable[idx].
Size));
451 if (OFS.seek(Saved) == (uint64_t)-1)
457 std::error_code SampleProfileWriterExtBinaryBase::writeHeader(
460 FileStart = OS.tell();
476 Writer.
write(static_cast<uint64_t>(-2));
487 std::vector<ProfileSummaryEntry> &Entries =
Summary->getDetailedSummary();
489 for (
auto Entry : Entries) {
499 if (std::error_code EC = writeNameIdx(S.
getName()))
515 uint64_t CalleeSamples = J.second;
516 if (std::error_code EC = writeNameIdx(Callee))
523 uint64_t NumCallsites = 0;
525 NumCallsites += J.second.size();
528 for (
const auto &
FS : J.second) {
533 if (std::error_code EC = writeBody(CalleeSamples))
568 std::unique_ptr<raw_ostream> OS;
577 return create(OS, Format);
591 std::unique_ptr<SampleProfileWriter> Writer;
610 return std::move(Writer);
616 for (
const auto &
I : ProfileMap) {
Represents either an error or a value T.
This class represents lattice values for constants.
virtual std::error_code write(const StringMap< FunctionSamples > &ProfileMap) override
Write all the sample profiles in the given map of samples.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void addRecord(const sampleprof::FunctionSamples &FS, bool isCallsiteSample=false)
virtual std::error_code writeNameTable()
void addName(StringRef FName)
uint64_t MD5Hash(StringRef Str)
Helper to compute and return lower 64 bits of the given string's MD5 hash.
std::unique_ptr< raw_ostream > OutputStream
Output stream where to emit the profile to.
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
virtual std::error_code write(const StringMap< FunctionSamples > &ProfileMap) override
Write all the sample profiles in the given map of samples.
static void addSecFlags(SecHdrTableEntry &Entry, uint64_t Flags)
virtual std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap) override
Write a file header for the profile file.
Representation of the samples collected for a function.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
std::error_code writeNameIdx(StringRef FName)
virtual std::error_code writeFuncProfiles(const StringMap< FunctionSamples > &ProfileMap)
Error compress(StringRef InputBuffer, SmallVectorImpl< char > &CompressedBuffer, int Level=DefaultCompression)
virtual std::error_code writeNameTable() override
static bool hasSecFlag(SecHdrTableEntry &Entry, SecFlags Flag)
std::error_code writeSample(const FunctionSamples &S) override
Write samples to a text file.
StringRef str() const
Explicit conversion to StringRef.
Representation of a single sample record.
virtual std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap) override
Write a file header for the profile file.
static constexpr int BestSizeCompression
void setToCompressAllSections()
static uint64_t SPVersion()
The file should be opened in text mode on platforms that make this distinction.
Sort a LocationT->SampleT map by LocationT.
uint64_t getHeadSamples() const
Return the total number of branch samples that have the function as the branch target.
virtual std::error_code write(const StringMap< FunctionSamples > &ProfileMap)
Write all the sample profiles in the given map of samples.
std::unique_ptr< ProfileSummary > Summary
Profile summary.
uint64_t getSamples() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
std::error_code addNewSection(SecType Sec, uint64_t SectionStart)
Add a new section into section header table.
The instances of the Type class are immutable: once they are created, they are never changed...
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
void addNames(const FunctionSamples &S)
void stablizeNameTable(std::set< StringRef > &V)
virtual std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
virtual std::error_code writeSample(const FunctionSamples &S)=0
Write sample profiles in S.
void write(ArrayRef< value_type > Val)
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
static ErrorOr< std::unique_ptr< SampleProfileWriter > > create(StringRef Filename, SampleProfileFormat Format)
Profile writer factory.
SampleProfileFormat Format
Profile format.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
std::error_code writeBody(const FunctionSamples &S)
amdgpu Simplify well known AMD library false FunctionCallee Callee
std::unique_ptr< ProfileSummary > getSummary()
virtual std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
Adapter to write values to a stream in a particular byte order.
uint64_t markSectionStart(SecType Type)
Return the current position and prepare to use it as the start position of a section.
A raw_ostream that writes to a file descriptor.
Represents the relative location of an instruction.
StringRef getName() const
Return the function name.
const CallTargetMap & getCallTargets() const
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
Provides ErrorOr<T> smart pointer.
virtual std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap)=0
Write a file header for the profile file.
void stable_sort(R &&Range)
A raw_ostream that writes to an std::string.
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
Lightweight error class with error context and mandatory checking.
const CallsiteSampleMap & getCallsiteSamples() const
Return all the callsite samples collected in the body of the function.
uint64_t tell() const
tell - Return the current offset with the file.
print Instructions which execute on loop entry
StringRef - Represent a constant reference to a string, i.e.
void setToCompressSection(SecType Type)
std::error_code writeSummary()
std::error_code writeFuncOffsetTable()
const SortedCallTargetSet getSortedCallTargets() const
void computeSummary(const StringMap< FunctionSamples > &ProfileMap)
Compute summary for this profile.
virtual std::error_code writeMagicIdent(SampleProfileFormat Format)
virtual std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
Sample-based profile writer (text format).