Skip to content

[memprof] Use uint32_t for linear call stack IDs #93924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/include/llvm/ProfileData/MemProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ struct Frame {
// A type representing the index into the table of call stacks.
using CallStackId = uint64_t;

// A type representing the index into the call stack array.
using LinearCallStackId = uint32_t;

// Holds allocation information in a space efficient format where frames are
// represented using unique identifiers.
struct IndexedAllocationInfo {
Expand Down
72 changes: 67 additions & 5 deletions llvm/lib/ProfileData/MemProf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,26 @@ static size_t serializedSizeV2(const IndexedAllocationInfo &IAI,
return Size;
}

static size_t serializedSizeV3(const IndexedAllocationInfo &IAI,
const MemProfSchema &Schema) {
size_t Size = 0;
// The linear call stack ID.
Size += sizeof(LinearCallStackId);
// The size of the payload.
Size += PortableMemInfoBlock::serializedSize(Schema);
return Size;
}

size_t IndexedAllocationInfo::serializedSize(const MemProfSchema &Schema,
IndexedVersion Version) const {
switch (Version) {
case Version0:
case Version1:
return serializedSizeV0(*this, Schema);
case Version2:
case Version3:
return serializedSizeV2(*this, Schema);
case Version3:
return serializedSizeV3(*this, Schema);
}
llvm_unreachable("unsupported MemProf version");
}
Expand Down Expand Up @@ -89,15 +100,30 @@ static size_t serializedSizeV2(const IndexedMemProfRecord &Record,
return Result;
}

static size_t serializedSizeV3(const IndexedMemProfRecord &Record,
const MemProfSchema &Schema) {
// The number of alloc sites to serialize.
size_t Result = sizeof(uint64_t);
for (const IndexedAllocationInfo &N : Record.AllocSites)
Result += N.serializedSize(Schema, Version3);

// The number of callsites we have information for.
Result += sizeof(uint64_t);
// The linear call stack ID.
Result += Record.CallSiteIds.size() * sizeof(LinearCallStackId);
return Result;
}

size_t IndexedMemProfRecord::serializedSize(const MemProfSchema &Schema,
IndexedVersion Version) const {
switch (Version) {
case Version0:
case Version1:
return serializedSizeV0(*this, Schema);
case Version2:
case Version3:
return serializedSizeV2(*this, Schema);
case Version3:
return serializedSizeV3(*this, Schema);
}
llvm_unreachable("unsupported MemProf version");
}
Expand Down Expand Up @@ -154,15 +180,15 @@ serializeV3(const IndexedMemProfRecord &Record, const MemProfSchema &Schema,
LE.write<uint64_t>(Record.AllocSites.size());
for (const IndexedAllocationInfo &N : Record.AllocSites) {
assert(MemProfCallStackIndexes.contains(N.CSId));
LE.write<uint64_t>(MemProfCallStackIndexes[N.CSId]);
LE.write<uint32_t>(MemProfCallStackIndexes[N.CSId]);
N.Info.serialize(Schema, OS);
}

// Related contexts.
LE.write<uint64_t>(Record.CallSiteIds.size());
for (const auto &CSId : Record.CallSiteIds) {
assert(MemProfCallStackIndexes.contains(CSId));
LE.write<uint64_t>(MemProfCallStackIndexes[CSId]);
LE.write<uint32_t>(MemProfCallStackIndexes[CSId]);
}
}

Expand Down Expand Up @@ -259,6 +285,41 @@ static IndexedMemProfRecord deserializeV2(const MemProfSchema &Schema,
return Record;
}

static IndexedMemProfRecord deserializeV3(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>(Ptr);
Record.AllocSites.reserve(NumNodes);
for (uint64_t I = 0; I < NumNodes; I++) {
IndexedAllocationInfo Node;
Node.CSId = endian::readNext<uint32_t, llvm::endianness::little>(Ptr);
Node.Info.deserialize(Schema, Ptr);
Ptr += PortableMemInfoBlock::serializedSize(Schema);
Record.AllocSites.push_back(Node);
}

// Read the callsite information.
const uint64_t NumCtxs =
endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
Record.CallSiteIds.reserve(NumCtxs);
for (uint64_t J = 0; J < NumCtxs; J++) {
// We are storing LinearCallStackId in CallSiteIds, which is a vector of
// CallStackId. Assert that CallStackId is no smaller than
// LinearCallStackId.
static_assert(sizeof(LinearCallStackId) <= sizeof(CallStackId));
LinearCallStackId CSId =
endian::readNext<LinearCallStackId, llvm::endianness::little>(Ptr);
Record.CallSiteIds.push_back(CSId);
}

return Record;
}

IndexedMemProfRecord
IndexedMemProfRecord::deserialize(const MemProfSchema &Schema,
const unsigned char *Ptr,
Expand All @@ -268,8 +329,9 @@ IndexedMemProfRecord::deserialize(const MemProfSchema &Schema,
case Version1:
return deserializeV0(Schema, Ptr);
case Version2:
case Version3:
return deserializeV2(Schema, Ptr);
case Version3:
return deserializeV3(Schema, Ptr);
}
llvm_unreachable("unsupported MemProf version");
}
Expand Down
Loading