summaryrefslogtreecommitdiffstats
path: root/llvm/lib/ProfileData/MemProf.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/ProfileData/MemProf.cpp')
-rw-r--r--llvm/lib/ProfileData/MemProf.cpp170
1 files changed, 161 insertions, 9 deletions
diff --git a/llvm/lib/ProfileData/MemProf.cpp b/llvm/lib/ProfileData/MemProf.cpp
index 6c419811d59e..ac0a8702c3f9 100644
--- a/llvm/lib/ProfileData/MemProf.cpp
+++ b/llvm/lib/ProfileData/MemProf.cpp
@@ -10,15 +10,88 @@
namespace llvm {
namespace memprof {
+namespace {
+size_t serializedSizeV0(const IndexedAllocationInfo &IAI) {
+ size_t Size = 0;
+ // The number of frames to serialize.
+ Size += sizeof(uint64_t);
+ // The callstack frame ids.
+ Size += sizeof(FrameId) * IAI.CallStack.size();
+ // The size of the payload.
+ Size += PortableMemInfoBlock::serializedSize();
+ return Size;
+}
-void IndexedMemProfRecord::serialize(const MemProfSchema &Schema,
- raw_ostream &OS) {
+size_t serializedSizeV2(const IndexedAllocationInfo &IAI) {
+ size_t Size = 0;
+ // The CallStackId
+ Size += sizeof(CallStackId);
+ // The size of the payload.
+ Size += PortableMemInfoBlock::serializedSize();
+ return Size;
+}
+} // namespace
+
+size_t IndexedAllocationInfo::serializedSize(IndexedVersion Version) const {
+ switch (Version) {
+ case Version0:
+ case Version1:
+ return serializedSizeV0(*this);
+ case Version2:
+ return serializedSizeV2(*this);
+ }
+ llvm_unreachable("unsupported MemProf version");
+}
+
+namespace {
+size_t serializedSizeV0(const IndexedMemProfRecord &Record) {
+ size_t Result = sizeof(GlobalValue::GUID);
+ for (const IndexedAllocationInfo &N : Record.AllocSites)
+ Result += N.serializedSize(Version0);
+
+ // The number of callsites we have information for.
+ Result += sizeof(uint64_t);
+ for (const auto &Frames : Record.CallSites) {
+ // The number of frame ids to serialize.
+ Result += sizeof(uint64_t);
+ Result += Frames.size() * sizeof(FrameId);
+ }
+ return Result;
+}
+
+size_t serializedSizeV2(const IndexedMemProfRecord &Record) {
+ size_t Result = sizeof(GlobalValue::GUID);
+ for (const IndexedAllocationInfo &N : Record.AllocSites)
+ Result += N.serializedSize(Version2);
+
+ // The number of callsites we have information for.
+ Result += sizeof(uint64_t);
+ // The CallStackId
+ Result += Record.CallSiteIds.size() * sizeof(CallStackId);
+ return Result;
+}
+} // namespace
+
+size_t IndexedMemProfRecord::serializedSize(IndexedVersion Version) const {
+ switch (Version) {
+ case Version0:
+ case Version1:
+ return serializedSizeV0(*this);
+ case Version2:
+ return serializedSizeV2(*this);
+ }
+ llvm_unreachable("unsupported MemProf version");
+}
+
+namespace {
+void serializeV0(const IndexedMemProfRecord &Record,
+ const MemProfSchema &Schema, raw_ostream &OS) {
using namespace support;
endian::Writer LE(OS, llvm::endianness::little);
- LE.write<uint64_t>(AllocSites.size());
- for (const IndexedAllocationInfo &N : AllocSites) {
+ LE.write<uint64_t>(Record.AllocSites.size());
+ for (const IndexedAllocationInfo &N : Record.AllocSites) {
LE.write<uint64_t>(N.CallStack.size());
for (const FrameId &Id : N.CallStack)
LE.write<FrameId>(Id);
@@ -26,17 +99,50 @@ void IndexedMemProfRecord::serialize(const MemProfSchema &Schema,
}
// Related contexts.
- LE.write<uint64_t>(CallSites.size());
- for (const auto &Frames : CallSites) {
+ LE.write<uint64_t>(Record.CallSites.size());
+ for (const auto &Frames : Record.CallSites) {
LE.write<uint64_t>(Frames.size());
for (const FrameId &Id : Frames)
LE.write<FrameId>(Id);
}
}
-IndexedMemProfRecord
-IndexedMemProfRecord::deserialize(const MemProfSchema &Schema,
- const unsigned char *Ptr) {
+void serializeV2(const IndexedMemProfRecord &Record,
+ const MemProfSchema &Schema, raw_ostream &OS) {
+ using namespace support;
+
+ endian::Writer LE(OS, llvm::endianness::little);
+
+ LE.write<uint64_t>(Record.AllocSites.size());
+ for (const IndexedAllocationInfo &N : Record.AllocSites) {
+ LE.write<CallStackId>(N.CSId);
+ N.Info.serialize(Schema, OS);
+ }
+
+ // Related contexts.
+ LE.write<uint64_t>(Record.CallSiteIds.size());
+ for (const auto &CSId : Record.CallSiteIds)
+ LE.write<CallStackId>(CSId);
+}
+} // namespace
+
+void IndexedMemProfRecord::serialize(const MemProfSchema &Schema,
+ raw_ostream &OS, IndexedVersion Version) {
+ switch (Version) {
+ case Version0:
+ case Version1:
+ serializeV0(*this, Schema, OS);
+ return;
+ case Version2:
+ serializeV2(*this, Schema, OS);
+ return;
+ }
+ llvm_unreachable("unsupported MemProf version");
+}
+
+namespace {
+IndexedMemProfRecord deserializeV0(const MemProfSchema &Schema,
+ const unsigned char *Ptr) {
using namespace support;
IndexedMemProfRecord Record;
@@ -73,11 +179,57 @@ IndexedMemProfRecord::deserialize(const MemProfSchema &Schema,
Frames.push_back(Id);
}
Record.CallSites.push_back(Frames);
+ Record.CallSiteIds.push_back(hashCallStack(Frames));
}
return Record;
}
+IndexedMemProfRecord deserializeV2(const MemProfSchema &Schema,
+ const unsigned char *Ptr) {
+ using namespace support;
+
+ IndexedMemProfRecord Record;
+
+ // Read the meminfo nodes.
+ const uint64_t NumNodes =
+ endian::readNext<uint64_t, llvm::endianness::little, unaligned>(Ptr);
+ for (uint64_t I = 0; I < NumNodes; I++) {
+ IndexedAllocationInfo Node;
+ Node.CSId =
+ endian::readNext<CallStackId, llvm::endianness::little, unaligned>(Ptr);
+ Node.Info.deserialize(Schema, Ptr);
+ Ptr += PortableMemInfoBlock::serializedSize();
+ Record.AllocSites.push_back(Node);
+ }
+
+ // Read the callsite information.
+ const uint64_t NumCtxs =
+ endian::readNext<uint64_t, llvm::endianness::little, unaligned>(Ptr);
+ for (uint64_t J = 0; J < NumCtxs; J++) {
+ CallStackId CSId =
+ endian::readNext<CallStackId, llvm::endianness::little, unaligned>(Ptr);
+ Record.CallSiteIds.push_back(CSId);
+ }
+
+ return Record;
+}
+} // namespace
+
+IndexedMemProfRecord
+IndexedMemProfRecord::deserialize(const MemProfSchema &Schema,
+ const unsigned char *Ptr,
+ IndexedVersion Version) {
+ switch (Version) {
+ case Version0:
+ case Version1:
+ return deserializeV0(Schema, Ptr);
+ case Version2:
+ return deserializeV2(Schema, Ptr);
+ }
+ llvm_unreachable("unsupported MemProf version");
+}
+
GlobalValue::GUID IndexedMemProfRecord::getGUID(const StringRef FunctionName) {
// Canonicalize the function name to drop suffixes such as ".llvm.". Note
// we do not drop any ".__uniq." suffixes, as getCanonicalFnName does not drop