#ifndef LLVM_PROFILEDATA_RAWMEMPROFREADER_H_
#define LLVM_PROFILEDATA_RAWMEMPROFREADER_H_
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
#include "llvm/DebugInfo/Symbolize/Symbolize.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/ProfileData/InstrProfReader.h"
#include "llvm/ProfileData/MemProf.h"
#include "llvm/ProfileData/MemProfData.inc"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cstddef>
namespace llvm {
namespace memprof {
using CallStackMap = llvm::DenseMap<uint64_t, llvm::SmallVector<uint64_t>>;
class RawMemProfReader {
public:
RawMemProfReader(const RawMemProfReader &) = delete;
RawMemProfReader &operator=(const RawMemProfReader &) = delete;
void printYAML(raw_ostream &OS);
static bool hasFormat(const MemoryBuffer &DataBuffer);
static bool hasFormat(const StringRef Path);
static Expected<std::unique_ptr<RawMemProfReader>>
create(const Twine &Path, const StringRef ProfiledBinary,
bool KeepName = false);
using GuidMemProfRecordPair = std::pair<GlobalValue::GUID, MemProfRecord>;
using Iterator = InstrProfIterator<GuidMemProfRecordPair, RawMemProfReader>;
Iterator end() { return Iterator(); }
Iterator begin() {
Iter = FunctionProfileData.begin();
return Iterator(this);
}
Error readNextRecord(GuidMemProfRecordPair &GuidRecord);
InstrProfKind getProfileKind() const { return InstrProfKind::MemProf; }
RawMemProfReader(std::unique_ptr<llvm::symbolize::SymbolizableModule> Sym,
llvm::SmallVectorImpl<SegmentEntry> &Seg,
llvm::MapVector<uint64_t, MemInfoBlock> &Prof,
CallStackMap &SM, bool KeepName = false)
: Symbolizer(std::move(Sym)), SegmentInfo(Seg.begin(), Seg.end()),
CallstackProfileData(Prof), StackMap(SM), KeepSymbolName(KeepName) {
if (Error E = symbolizeAndFilterStackFrames())
report_fatal_error(std::move(E));
if (Error E = mapRawProfileToRecords())
report_fatal_error(std::move(E));
}
const llvm::DenseMap<FrameId, Frame> &getFrameMapping() const {
return IdToFrame;
}
const llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> &
getProfileData() const {
return FunctionProfileData;
}
private:
RawMemProfReader(object::OwningBinary<object::Binary> &&Bin, bool KeepName)
: Binary(std::move(Bin)), KeepSymbolName(KeepName) {}
Error initialize(std::unique_ptr<MemoryBuffer> DataBuffer);
Error readRawProfile(std::unique_ptr<MemoryBuffer> DataBuffer);
Error symbolizeAndFilterStackFrames();
Error mapRawProfileToRecords();
const Frame &idToFrame(const FrameId Id) const {
auto It = IdToFrame.find(Id);
assert(It != IdToFrame.end() && "Id not found in map.");
return It->getSecond();
}
object::SectionedAddress getModuleOffset(uint64_t VirtualAddress);
object::OwningBinary<object::Binary> Binary;
std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer;
llvm::SmallVector<SegmentEntry, 16> SegmentInfo;
llvm::MapVector<uint64_t, MemInfoBlock> CallstackProfileData;
CallStackMap StackMap;
llvm::DenseMap<uint64_t, llvm::SmallVector<FrameId>> SymbolizedFrame;
llvm::DenseMap<FrameId, Frame> IdToFrame;
llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> FunctionProfileData;
llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord>::iterator Iter;
bool KeepSymbolName = false;
llvm::DenseMap<uint64_t, std::string> GuidToSymbolName;
};
} }
#endif