43#define DEBUG_TYPE "memprof"
48template <
class T = u
int64_t>
inline T alignedRead(
const char *
Ptr) {
49 static_assert(std::is_integral_v<T>,
"Not an integral type");
50 assert(
reinterpret_cast<size_t>(
Ptr) %
sizeof(
T) == 0 &&
"Unaligned Read");
51 return *
reinterpret_cast<const T *
>(
Ptr);
54Error checkBuffer(
const MemoryBuffer &Buffer) {
58 if (Buffer.getBufferSize() == 0)
61 if (Buffer.getBufferSize() <
sizeof(Header)) {
68 const char *Next = Buffer.getBufferStart();
69 while (Next < Buffer.getBufferEnd()) {
70 const auto *
H =
reinterpret_cast<const Header *
>(Next);
73 bool IsSupported =
false;
74 for (
auto SupportedVersion : MEMPROF_RAW_SUPPORTED_VERSIONS) {
75 if (
H->Version == SupportedVersion)
82 TotalSize +=
H->TotalSize;
86 if (Buffer.getBufferSize() != TotalSize) {
93 using namespace support;
96 endian::readNext<uint64_t, llvm::endianness::little>(
Ptr);
99 Items.
push_back(*
reinterpret_cast<const SegmentEntry *
>(
100 Ptr +
I *
sizeof(SegmentEntry)));
106readMemInfoBlocksV3(
const char *
Ptr) {
107 using namespace support;
110 endian::readNext<uint64_t, llvm::endianness::little, unaligned>(
Ptr);
115 endian::readNext<uint64_t, llvm::endianness::little, unaligned>(
Ptr);
123 MemInfoBlock MIB = *
reinterpret_cast<const MemInfoBlock *
>(
Ptr);
125 MIB.AccessHistogramSize = 0;
126 MIB.AccessHistogram = 0;
130 Ptr += MEMPROF_V3_MIB_SIZE;
136readMemInfoBlocksV4(
const char *
Ptr) {
137 using namespace support;
140 endian::readNext<uint64_t, llvm::endianness::little, unaligned>(
Ptr);
145 endian::readNext<uint64_t, llvm::endianness::little, unaligned>(
Ptr);
148 MemInfoBlock MIB = *
reinterpret_cast<const MemInfoBlock *
>(
Ptr);
151 Ptr +=
sizeof(MemInfoBlock);
153 if (MIB.AccessHistogramSize > 0) {
154 MIB.AccessHistogram =
155 (uintptr_t)malloc(MIB.AccessHistogramSize *
sizeof(
uint64_t));
158 for (
uint64_t J = 0; J < MIB.AccessHistogramSize; J++) {
159 ((
uint64_t *)MIB.AccessHistogram)[J] =
160 endian::readNext<uint64_t, llvm::endianness::little, unaligned>(
Ptr);
168 using namespace support;
171 endian::readNext<uint64_t, llvm::endianness::little>(
Ptr);
176 endian::readNext<uint64_t, llvm::endianness::little>(
Ptr);
178 endian::readNext<uint64_t, llvm::endianness::little>(
Ptr);
180 SmallVector<uint64_t> CallStack;
181 CallStack.reserve(NumPCs);
182 for (
uint64_t J = 0; J < NumPCs; J++) {
184 endian::readNext<uint64_t, llvm::endianness::little>(
Ptr));
187 Items[StackId] = CallStack;
196 for (
const auto &[Id, Stack] :
From) {
197 auto [It, Inserted] = To.try_emplace(Id, Stack);
199 if (!Inserted && Stack != It->second)
205Error report(Error
E,
const StringRef Context) {
210bool isRuntimePath(
const StringRef Path) {
214 return Filename ==
"memprof_malloc_linux.cpp" ||
215 Filename ==
"memprof_interceptors.cpp" ||
216 Filename ==
"memprof_new_delete.cpp";
219std::string getBuildIdString(
const SegmentEntry &Entry) {
221 if (Entry.BuildIdSize == 0)
225 raw_string_ostream
OS(Str);
226 for (
size_t I = 0;
I < Entry.BuildIdSize;
I++) {
233Expected<std::unique_ptr<RawMemProfReader>>
237 if (std::error_code EC = BufferOr.getError())
240 std::unique_ptr<MemoryBuffer> Buffer(BufferOr.get().release());
241 return create(std::move(Buffer), ProfiledBinary, KeepName);
246 const StringRef ProfiledBinary,
bool KeepName) {
247 if (
Error E = checkBuffer(*Buffer))
248 return report(std::move(
E), Buffer->getBufferIdentifier());
250 if (ProfiledBinary.
empty()) {
252 const std::vector<std::string> BuildIds =
peekBuildIds(Buffer.get());
253 std::string ErrorMessage(
254 R
"(Path to profiled binary is empty, expected binary with one of the following build ids:
256 for (
const auto &Id : BuildIds) {
257 ErrorMessage +=
"\n BuildId: ";
267 return report(BinaryOr.takeError(), ProfiledBinary);
271 std::unique_ptr<RawMemProfReader> Reader(
273 if (
Error E = Reader->initialize(std::move(Buffer))) {
276 return std::move(Reader);
282 for (
auto &[
_, MIB] : CallstackProfileData) {
283 if (MemprofRawVersion >= 4ULL && MIB.AccessHistogramSize > 0) {
284 free((
void *)MIB.AccessHistogram);
294 std::unique_ptr<MemoryBuffer> Buffer(BufferOr.get().release());
304 return Magic == MEMPROF_RAW_MAGIC_64;
308 uint64_t NumAllocFunctions = 0, NumMibInfo = 0;
310 const size_t NumAllocSites = KV.second.AllocSites.size();
311 if (NumAllocSites > 0) {
313 NumMibInfo += NumAllocSites;
317 OS <<
"MemprofProfile:\n";
319 OS <<
" Version: " << MemprofRawVersion <<
"\n";
320 OS <<
" NumSegments: " << SegmentInfo.
size() <<
"\n";
321 OS <<
" NumMibInfo: " << NumMibInfo <<
"\n";
322 OS <<
" NumAllocFunctions: " << NumAllocFunctions <<
"\n";
323 OS <<
" NumStackOffsets: " << StackMap.
size() <<
"\n";
325 OS <<
" Segments:\n";
326 for (
const auto &Entry : SegmentInfo) {
328 OS <<
" BuildId: " << getBuildIdString(Entry) <<
"\n";
329 OS <<
" Start: 0x" << llvm::utohexstr(Entry.Start) <<
"\n";
330 OS <<
" End: 0x" << llvm::utohexstr(Entry.End) <<
"\n";
331 OS <<
" Offset: 0x" << llvm::utohexstr(Entry.Offset) <<
"\n";
335 for (
const auto &[GUID,
Record] : *
this) {
337 OS <<
" FunctionGUID: " << GUID <<
"\n";
342Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer) {
343 const StringRef FileName = Binary.getBinary()->getFileName();
345 auto *ElfObject = dyn_cast<object::ELFObjectFileBase>(Binary.getBinary());
347 return report(make_error<StringError>(
Twine(
"Not an ELF file: "),
355 auto *Elf64LEObject = llvm::cast<llvm::object::ELF64LEObjectFile>(ElfObject);
357 auto PHdrsOr = ElfFile.program_headers();
360 make_error<StringError>(
Twine(
"Could not read program headers: "),
364 int NumExecutableSegments = 0;
365 for (
const auto &Phdr : *PHdrsOr) {
370 if (++NumExecutableSegments > 1) {
372 make_error<StringError>(
373 "Expect only one executable load segment in the binary",
382 PreferredTextSegmentAddress = Phdr.p_vaddr;
383 assert(Phdr.p_vaddr == (Phdr.p_vaddr & ~(0x1000 - 1U)) &&
384 "Expect p_vaddr to always be page aligned");
385 assert(Phdr.p_offset == 0 &&
"Expect p_offset = 0 for symbolization.");
390 auto Triple = ElfObject->makeTriple();
392 return report(make_error<StringError>(Twine(
"Unsupported target: ") +
393 Triple.getArchName(),
398 if (Error
E = readRawProfile(std::move(DataBuffer)))
401 if (Error
E = setupForSymbolization())
404 auto *
Object = cast<object::ObjectFile>(Binary.getBinary());
409 Object, std::move(Context),
false);
411 return report(SOFOr.takeError(), FileName);
412 auto Symbolizer = std::move(SOFOr.get());
418 if (Error
E = symbolizeAndFilterStackFrames(std::move(Symbolizer)))
421 return mapRawProfileToRecords();
424Error RawMemProfReader::setupForSymbolization() {
425 auto *
Object = cast<object::ObjectFile>(Binary.getBinary());
427 if (BinaryId.empty())
428 return make_error<StringError>(Twine(
"No build id found in binary ") +
429 Binary.getBinary()->getFileName(),
433 for (
const auto &Entry : SegmentInfo) {
435 if (BinaryId == SegmentId) {
438 if (++NumMatched > 1) {
439 return make_error<StringError>(
440 "We expect only one executable segment in the profiled binary",
443 ProfiledTextSegmentStart =
Entry.Start;
444 ProfiledTextSegmentEnd =
Entry.End;
447 assert(NumMatched != 0 &&
"No matching executable segments in segment info.");
448 assert((PreferredTextSegmentAddress == 0 ||
449 (PreferredTextSegmentAddress == ProfiledTextSegmentStart)) &&
450 "Expect text segment address to be 0 or equal to profiled text "
455Error RawMemProfReader::mapRawProfileToRecords() {
461 PerFunctionCallSites;
465 for (
const auto &[StackId, MIB] : CallstackProfileData) {
466 auto It = StackMap.
find(StackId);
467 if (It == StackMap.
end())
468 return make_error<InstrProfError>(
470 "memprof callstack record does not contain id: " + Twine(StackId));
474 Callstack.
reserve(It->getSecond().size());
477 for (
size_t I = 0;
I < Addresses.
size();
I++) {
480 "Address not found in SymbolizedFrame map");
481 const SmallVector<FrameId> &Frames = SymbolizedFrame[
Address];
484 "The last frame should not be inlined");
489 for (
size_t J = 0; J < Frames.size(); J++) {
490 if (
I == 0 && J == 0)
501 Callstack.
append(Frames.begin(), Frames.end());
508 for (
size_t I = 0; ;
I++) {
511 Record.AllocSites.emplace_back(CSId, MIB);
513 if (!
F.IsInlineFrame)
519 for (
const auto &[Id, Locs] : PerFunctionCallSites) {
523 for (LocationPtr Loc : Locs)
530Error RawMemProfReader::symbolizeAndFilterStackFrames(
531 std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer) {
533 const DILineInfoSpecifier Specifier(
534 DILineInfoSpecifier::FileLineInfoKind::RawValue,
535 DILineInfoSpecifier::FunctionNameKind::LinkageName);
543 for (
auto &Entry : StackMap) {
548 if (SymbolizedFrame.count(VAddr) > 0 ||
552 Expected<DIInliningInfo> DIOr = Symbolizer->symbolizeInlinedCode(
553 getModuleOffset(VAddr), Specifier,
false);
555 return DIOr.takeError();
556 DIInliningInfo DI = DIOr.get();
560 isRuntimePath(DI.getFrame(0).FileName)) {
561 AllVAddrsToDiscard.
insert(VAddr);
565 for (
size_t I = 0, NumFrames = DI.getNumberOfFrames();
I < NumFrames;
567 const auto &DIFrame = DI.getFrame(
I);
570 const Frame
F(
Guid, DIFrame.Line - DIFrame.StartLine, DIFrame.Column,
577 if (KeepSymbolName) {
578 StringRef CanonicalName =
580 DIFrame.FunctionName);
581 GuidToSymbolName.
insert({
Guid, CanonicalName.str()});
588 auto &CallStack =
Entry.getSecond();
592 if (CallStack.empty())
597 for (
const uint64_t Id : EntriesToErase) {
599 if (CallstackProfileData[Id].AccessHistogramSize > 0)
600 free((
void *)CallstackProfileData[Id].AccessHistogram);
601 CallstackProfileData.erase(Id);
604 if (StackMap.empty())
605 return make_error<InstrProfError>(
607 "no entries in callstack map after symbolization");
612std::vector<std::string>
624 while (Next < DataBuffer->getBufferEnd()) {
625 const auto *Header =
reinterpret_cast<const memprof::Header *
>(Next);
628 readSegmentEntries(Next + Header->SegmentOffset);
630 for (
const auto &Entry : Entries)
631 BuildIds.
insert(getBuildIdString(Entry));
633 Next += Header->TotalSize;
642RawMemProfReader::readMemInfoBlocks(
const char *
Ptr) {
643 if (MemprofRawVersion == 3ULL)
644 return readMemInfoBlocksV3(
Ptr);
645 if (MemprofRawVersion == 4ULL)
646 return readMemInfoBlocksV4(
Ptr);
648 "Panic: Unsupported version number when reading MemInfoBlocks");
651Error RawMemProfReader::readRawProfile(
652 std::unique_ptr<MemoryBuffer> DataBuffer) {
653 const char *Next = DataBuffer->getBufferStart();
655 while (Next < DataBuffer->getBufferEnd()) {
656 const auto *Header =
reinterpret_cast<const memprof::Header *
>(Next);
660 MemprofRawVersion = Header->Version;
665 readSegmentEntries(Next + Header->SegmentOffset);
666 if (!SegmentInfo.empty() && SegmentInfo != Entries) {
670 return make_error<InstrProfError>(
672 "memprof raw profile has different segment information");
674 SegmentInfo.assign(Entries.begin(), Entries.end());
679 for (
const auto &[Id, MIB] : readMemInfoBlocks(Next + Header->MIBOffset)) {
680 if (CallstackProfileData.count(Id)) {
682 if (MemprofRawVersion >= 4ULL &&
683 (CallstackProfileData[Id].AccessHistogramSize > 0 ||
684 MIB.AccessHistogramSize > 0)) {
685 uintptr_t ShorterHistogram;
686 if (CallstackProfileData[Id].AccessHistogramSize >
687 MIB.AccessHistogramSize)
688 ShorterHistogram = MIB.AccessHistogram;
690 ShorterHistogram = CallstackProfileData[Id].AccessHistogram;
691 CallstackProfileData[Id].Merge(MIB);
692 free((
void *)ShorterHistogram);
694 CallstackProfileData[
Id].Merge(MIB);
697 CallstackProfileData[
Id] = MIB;
703 const CallStackMap CSM = readStackInfo(Next + Header->StackOffset);
704 if (StackMap.empty()) {
707 if (mergeStackMap(CSM, StackMap))
708 return make_error<InstrProfError>(
710 "memprof raw profile got different call stack for same id");
713 Next += Header->TotalSize;
719object::SectionedAddress
720RawMemProfReader::getModuleOffset(
const uint64_t VirtualAddress) {
721 if (VirtualAddress > ProfiledTextSegmentStart &&
722 VirtualAddress <= ProfiledTextSegmentEnd) {
728 VirtualAddress + PreferredTextSegmentAddress - ProfiledTextSegmentStart;
729 return object::SectionedAddress{AdjustedAddress};
734 return object::SectionedAddress{VirtualAddress};
743 auto IdToFrameCallback = [
this](
const FrameId Id) {
745 if (!this->KeepSymbolName)
747 auto Iter = this->GuidToSymbolName.
find(
F.Function);
749 F.SymbolName = std::make_unique<std::string>(
Iter->getSecond());
758 if (std::error_code EC = BufferOr.getError())
761 std::unique_ptr<MemoryBuffer> Buffer(BufferOr.get().release());
762 return create(std::move(Buffer));
767 auto Reader = std::make_unique<YAMLMemProfReader>();
768 Reader->parse(Buffer->getBuffer());
769 return std::move(Reader);
777 std::unique_ptr<MemoryBuffer> Buffer(BufferOr.get().release());
787 yaml::Input Yin(YAMLData);
812 for (
const auto &CallSite :
Record.CallSites) {
BlockVerifier::State From
This file declares a library for handling Build IDs and using them to find debug info.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallSet class.
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
static std::unique_ptr< DWARFContext > create(const object::ObjectFile &Obj, ProcessDebugRelocations RelocAction=ProcessDebugRelocations::Process, const LoadedObjectInfo *L=nullptr, std::string DWPName="", std::function< void(Error)> RecoverableErrorHandler=WithColor::defaultErrorHandler, std::function< void(Error)> WarningHandler=WithColor::defaultWarningHandler, bool ThreadSafe=false)
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Implements a dense probed hash-table based set.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
This class implements a map that also provides access to all stored values in a deterministic order.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
This interface provides simple read-only access to a block of memory, and provides simple methods for...
size_t getBufferSize() const
StringRef getBuffer() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
const char * getBufferStart() const
A vector that has set insertion semantics.
Vector takeVector()
Clear the SetVector and return the underlying vector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr bool empty() const
empty - Check if the string is empty.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
std::pair< iterator, bool > insert(const ValueT &V)
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Helper class to iterate through stack ids in both metadata (memprof MIB and callsite) and the corresp...
const Frame & idToFrame(const FrameId Id) const
IndexedMemProfData MemProfData
virtual Error readNextRecord(GuidMemProfRecordPair &GuidRecord, std::function< const Frame(const FrameId)> Callback=nullptr)
llvm::MapVector< GlobalValue::GUID, IndexedMemProfRecord >::iterator Iter
std::pair< GlobalValue::GUID, MemProfRecord > GuidMemProfRecordPair
void printYAML(raw_ostream &OS)
static Expected< std::unique_ptr< RawMemProfReader > > create(const Twine &Path, StringRef ProfiledBinary, bool KeepName=false)
static std::vector< std::string > peekBuildIds(MemoryBuffer *DataBuffer)
Error readNextRecord(GuidMemProfRecordPair &GuidRecord, std::function< const Frame(const FrameId)> Callback) override
static bool hasFormat(const MemoryBuffer &DataBuffer)
virtual ~RawMemProfReader() override
static bool hasFormat(const MemoryBuffer &DataBuffer)
static Expected< std::unique_ptr< YAMLMemProfReader > > create(const Twine &Path)
void parse(StringRef YAMLData)
This class implements an extremely fast bulk output stream that can only output to a stream.
static StringRef getCanonicalFnName(const Function &F)
Return the canonical name for a function, taking into account suffix elision policy attributes.
static Expected< std::unique_ptr< SymbolizableObjectFile > > create(const object::ObjectFile *Obj, std::unique_ptr< DIContext > DICtx, bool UntagAddresses)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
llvm::DenseMap< uint64_t, llvm::SmallVector< uint64_t > > CallStackMap
BuildIDRef getBuildID(const ObjectFile *Obj)
Returns the build ID, if any, contained in the given object file.
ArrayRef< uint8_t > BuildIDRef
A reference to a BuildID in binary form.
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
This is an optimization pass for GlobalISel generic memory operations.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Error joinErrors(Error E1, Error E2)
Concatenate errors.
FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width, bool Upper=false)
format_hex_no_prefix - Output N as a fixed width hexadecimal.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
static constexpr const char *const BadString
std::vector< GUIDMemProfRecordPair > HeapProfileRecords
std::vector< Frame > CallStack
PortableMemInfoBlock Info
GlobalValue::GUID Function
llvm::MapVector< GlobalValue::GUID, IndexedMemProfRecord > Records
CallStackId addCallStack(ArrayRef< FrameId > CS)
FrameId addFrame(const Frame &F)
llvm::SmallVector< CallStackId > CallSiteIds
llvm::SmallVector< IndexedAllocationInfo > AllocSites
static GlobalValue::GUID getGUID(const StringRef FunctionName)