2022-02-17 16:01:31 -08:00
|
|
|
#include "llvm/ProfileData/MemProf.h"
|
2022-03-22 14:40:02 -07:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2022-02-23 22:18:20 +01:00
|
|
|
#include "llvm/ProfileData/InstrProf.h"
|
2023-08-29 18:58:39 +00:00
|
|
|
#include "llvm/ProfileData/SampleProf.h"
|
2024-03-23 19:50:15 -07:00
|
|
|
#include "llvm/Support/BLAKE3.h"
|
2022-02-17 16:01:31 -08:00
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/EndianStream.h"
|
2024-03-23 19:50:15 -07:00
|
|
|
#include "llvm/Support/HashBuilder.h"
|
2022-02-17 16:01:31 -08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace memprof {
|
2024-04-25 12:12:28 -07:00
|
|
|
MemProfSchema getFullSchema() {
|
|
|
|
MemProfSchema List;
|
|
|
|
#define MIBEntryDef(NameTag, Name, Type) List.push_back(Meta::Name);
|
|
|
|
#include "llvm/ProfileData/MIBEntryDef.inc"
|
|
|
|
#undef MIBEntryDef
|
|
|
|
return List;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemProfSchema getHotColdSchema() {
|
|
|
|
return {Meta::AllocCount, Meta::TotalSize, Meta::TotalLifetime,
|
|
|
|
Meta::TotalLifetimeAccessDensity};
|
|
|
|
}
|
|
|
|
|
2024-04-23 13:44:31 -07:00
|
|
|
static size_t serializedSizeV0(const IndexedAllocationInfo &IAI,
|
|
|
|
const MemProfSchema &Schema) {
|
2024-04-03 21:48:38 -07:00
|
|
|
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.
|
2024-04-23 13:44:31 -07:00
|
|
|
Size += PortableMemInfoBlock::serializedSize(Schema);
|
2024-04-03 21:48:38 -07:00
|
|
|
return Size;
|
|
|
|
}
|
2022-02-17 16:01:31 -08:00
|
|
|
|
2024-04-23 13:44:31 -07:00
|
|
|
static size_t serializedSizeV2(const IndexedAllocationInfo &IAI,
|
|
|
|
const MemProfSchema &Schema) {
|
2024-04-03 21:48:38 -07:00
|
|
|
size_t Size = 0;
|
|
|
|
// The CallStackId
|
|
|
|
Size += sizeof(CallStackId);
|
|
|
|
// The size of the payload.
|
2024-04-23 13:44:31 -07:00
|
|
|
Size += PortableMemInfoBlock::serializedSize(Schema);
|
2024-04-03 21:48:38 -07:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2024-05-31 10:29:14 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-23 13:44:31 -07:00
|
|
|
size_t IndexedAllocationInfo::serializedSize(const MemProfSchema &Schema,
|
|
|
|
IndexedVersion Version) const {
|
2024-04-03 21:48:38 -07:00
|
|
|
switch (Version) {
|
|
|
|
case Version0:
|
|
|
|
case Version1:
|
2024-04-23 13:44:31 -07:00
|
|
|
return serializedSizeV0(*this, Schema);
|
2024-04-03 21:48:38 -07:00
|
|
|
case Version2:
|
2024-04-23 13:44:31 -07:00
|
|
|
return serializedSizeV2(*this, Schema);
|
2024-05-31 10:29:14 -07:00
|
|
|
case Version3:
|
|
|
|
return serializedSizeV3(*this, Schema);
|
2024-04-03 21:48:38 -07:00
|
|
|
}
|
|
|
|
llvm_unreachable("unsupported MemProf version");
|
|
|
|
}
|
|
|
|
|
2024-04-23 13:44:31 -07:00
|
|
|
static size_t serializedSizeV0(const IndexedMemProfRecord &Record,
|
|
|
|
const MemProfSchema &Schema) {
|
2024-04-15 10:00:56 -07:00
|
|
|
// The number of alloc sites to serialize.
|
|
|
|
size_t Result = sizeof(uint64_t);
|
2024-04-03 21:48:38 -07:00
|
|
|
for (const IndexedAllocationInfo &N : Record.AllocSites)
|
2024-04-23 13:44:31 -07:00
|
|
|
Result += N.serializedSize(Schema, Version0);
|
2024-04-03 21:48:38 -07:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2024-04-23 13:44:31 -07:00
|
|
|
static size_t serializedSizeV2(const IndexedMemProfRecord &Record,
|
|
|
|
const MemProfSchema &Schema) {
|
2024-04-15 10:00:56 -07:00
|
|
|
// The number of alloc sites to serialize.
|
|
|
|
size_t Result = sizeof(uint64_t);
|
2024-04-03 21:48:38 -07:00
|
|
|
for (const IndexedAllocationInfo &N : Record.AllocSites)
|
2024-04-23 13:44:31 -07:00
|
|
|
Result += N.serializedSize(Schema, Version2);
|
2024-04-03 21:48:38 -07:00
|
|
|
|
|
|
|
// The number of callsites we have information for.
|
|
|
|
Result += sizeof(uint64_t);
|
|
|
|
// The CallStackId
|
|
|
|
Result += Record.CallSiteIds.size() * sizeof(CallStackId);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2024-05-31 10:29:14 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-23 13:44:31 -07:00
|
|
|
size_t IndexedMemProfRecord::serializedSize(const MemProfSchema &Schema,
|
|
|
|
IndexedVersion Version) const {
|
2024-04-03 21:48:38 -07:00
|
|
|
switch (Version) {
|
|
|
|
case Version0:
|
|
|
|
case Version1:
|
2024-04-23 13:44:31 -07:00
|
|
|
return serializedSizeV0(*this, Schema);
|
2024-04-03 21:48:38 -07:00
|
|
|
case Version2:
|
2024-04-23 13:44:31 -07:00
|
|
|
return serializedSizeV2(*this, Schema);
|
2024-05-31 10:29:14 -07:00
|
|
|
case Version3:
|
|
|
|
return serializedSizeV3(*this, Schema);
|
2024-04-03 21:48:38 -07:00
|
|
|
}
|
|
|
|
llvm_unreachable("unsupported MemProf version");
|
|
|
|
}
|
|
|
|
|
2024-04-07 11:38:15 -07:00
|
|
|
static void serializeV0(const IndexedMemProfRecord &Record,
|
|
|
|
const MemProfSchema &Schema, raw_ostream &OS) {
|
2022-02-17 16:01:31 -08:00
|
|
|
using namespace support;
|
|
|
|
|
2023-10-13 23:16:25 -07:00
|
|
|
endian::Writer LE(OS, llvm::endianness::little);
|
2022-02-17 16:01:31 -08:00
|
|
|
|
2024-04-03 21:48:38 -07:00
|
|
|
LE.write<uint64_t>(Record.AllocSites.size());
|
|
|
|
for (const IndexedAllocationInfo &N : Record.AllocSites) {
|
2022-03-22 14:40:02 -07:00
|
|
|
LE.write<uint64_t>(N.CallStack.size());
|
2022-03-21 19:39:24 -07:00
|
|
|
for (const FrameId &Id : N.CallStack)
|
|
|
|
LE.write<FrameId>(Id);
|
2022-03-22 14:40:02 -07:00
|
|
|
N.Info.serialize(Schema, OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Related contexts.
|
2024-04-03 21:48:38 -07:00
|
|
|
LE.write<uint64_t>(Record.CallSites.size());
|
|
|
|
for (const auto &Frames : Record.CallSites) {
|
2022-03-22 14:40:02 -07:00
|
|
|
LE.write<uint64_t>(Frames.size());
|
2022-03-21 19:39:24 -07:00
|
|
|
for (const FrameId &Id : Frames)
|
|
|
|
LE.write<FrameId>(Id);
|
2022-02-17 16:01:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-07 11:38:15 -07:00
|
|
|
static void serializeV2(const IndexedMemProfRecord &Record,
|
|
|
|
const MemProfSchema &Schema, raw_ostream &OS) {
|
2024-04-03 21:48:38 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-05-30 14:28:22 -07:00
|
|
|
static void
|
|
|
|
serializeV3(const IndexedMemProfRecord &Record, const MemProfSchema &Schema,
|
|
|
|
raw_ostream &OS,
|
|
|
|
llvm::DenseMap<CallStackId, uint32_t> &MemProfCallStackIndexes) {
|
|
|
|
using namespace support;
|
|
|
|
|
|
|
|
endian::Writer LE(OS, llvm::endianness::little);
|
|
|
|
|
|
|
|
LE.write<uint64_t>(Record.AllocSites.size());
|
|
|
|
for (const IndexedAllocationInfo &N : Record.AllocSites) {
|
|
|
|
assert(MemProfCallStackIndexes.contains(N.CSId));
|
2024-05-31 10:29:14 -07:00
|
|
|
LE.write<uint32_t>(MemProfCallStackIndexes[N.CSId]);
|
2024-05-30 14:28:22 -07:00
|
|
|
N.Info.serialize(Schema, OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Related contexts.
|
|
|
|
LE.write<uint64_t>(Record.CallSiteIds.size());
|
|
|
|
for (const auto &CSId : Record.CallSiteIds) {
|
|
|
|
assert(MemProfCallStackIndexes.contains(CSId));
|
2024-05-31 10:29:14 -07:00
|
|
|
LE.write<uint32_t>(MemProfCallStackIndexes[CSId]);
|
2024-05-30 14:28:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndexedMemProfRecord::serialize(
|
|
|
|
const MemProfSchema &Schema, raw_ostream &OS, IndexedVersion Version,
|
|
|
|
llvm::DenseMap<CallStackId, uint32_t> *MemProfCallStackIndexes) {
|
2024-04-03 21:48:38 -07:00
|
|
|
switch (Version) {
|
|
|
|
case Version0:
|
|
|
|
case Version1:
|
|
|
|
serializeV0(*this, Schema, OS);
|
|
|
|
return;
|
|
|
|
case Version2:
|
|
|
|
serializeV2(*this, Schema, OS);
|
|
|
|
return;
|
2024-05-30 14:28:22 -07:00
|
|
|
case Version3:
|
|
|
|
serializeV3(*this, Schema, OS, *MemProfCallStackIndexes);
|
|
|
|
return;
|
2024-04-03 21:48:38 -07:00
|
|
|
}
|
|
|
|
llvm_unreachable("unsupported MemProf version");
|
|
|
|
}
|
|
|
|
|
2024-04-07 11:38:15 -07:00
|
|
|
static IndexedMemProfRecord deserializeV0(const MemProfSchema &Schema,
|
|
|
|
const unsigned char *Ptr) {
|
2022-02-17 16:01:31 -08:00
|
|
|
using namespace support;
|
|
|
|
|
2022-03-21 19:39:24 -07:00
|
|
|
IndexedMemProfRecord Record;
|
2022-03-22 14:40:02 -07:00
|
|
|
|
|
|
|
// Read the meminfo nodes.
|
2023-10-13 23:16:25 -07:00
|
|
|
const uint64_t NumNodes =
|
2024-04-16 12:47:02 -07:00
|
|
|
endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
|
2022-03-22 14:40:02 -07:00
|
|
|
for (uint64_t I = 0; I < NumNodes; I++) {
|
2022-03-21 19:39:24 -07:00
|
|
|
IndexedAllocationInfo Node;
|
2022-02-17 16:01:31 -08:00
|
|
|
const uint64_t NumFrames =
|
2024-04-16 12:47:02 -07:00
|
|
|
endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
|
2022-02-17 16:01:31 -08:00
|
|
|
for (uint64_t J = 0; J < NumFrames; J++) {
|
2023-10-13 23:16:25 -07:00
|
|
|
const FrameId Id =
|
2024-04-16 12:47:02 -07:00
|
|
|
endian::readNext<FrameId, llvm::endianness::little>(Ptr);
|
2022-03-21 19:39:24 -07:00
|
|
|
Node.CallStack.push_back(Id);
|
2022-02-17 16:01:31 -08:00
|
|
|
}
|
2024-03-25 14:21:49 -07:00
|
|
|
Node.CSId = hashCallStack(Node.CallStack);
|
2022-03-22 14:40:02 -07:00
|
|
|
Node.Info.deserialize(Schema, Ptr);
|
2024-04-23 13:44:31 -07:00
|
|
|
Ptr += PortableMemInfoBlock::serializedSize(Schema);
|
2022-03-22 14:40:02 -07:00
|
|
|
Record.AllocSites.push_back(Node);
|
2022-02-17 16:01:31 -08:00
|
|
|
}
|
2022-03-22 14:40:02 -07:00
|
|
|
|
|
|
|
// Read the callsite information.
|
2023-10-13 23:16:25 -07:00
|
|
|
const uint64_t NumCtxs =
|
2024-04-16 12:47:02 -07:00
|
|
|
endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
|
2022-03-22 14:40:02 -07:00
|
|
|
for (uint64_t J = 0; J < NumCtxs; J++) {
|
|
|
|
const uint64_t NumFrames =
|
2024-04-16 12:47:02 -07:00
|
|
|
endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
|
2022-03-21 19:39:24 -07:00
|
|
|
llvm::SmallVector<FrameId> Frames;
|
|
|
|
Frames.reserve(NumFrames);
|
2022-03-22 14:40:02 -07:00
|
|
|
for (uint64_t K = 0; K < NumFrames; K++) {
|
2023-10-13 23:16:25 -07:00
|
|
|
const FrameId Id =
|
2024-04-16 12:47:02 -07:00
|
|
|
endian::readNext<FrameId, llvm::endianness::little>(Ptr);
|
2022-03-21 19:39:24 -07:00
|
|
|
Frames.push_back(Id);
|
2022-03-22 14:40:02 -07:00
|
|
|
}
|
|
|
|
Record.CallSites.push_back(Frames);
|
2024-04-03 21:48:38 -07:00
|
|
|
Record.CallSiteIds.push_back(hashCallStack(Frames));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Record;
|
|
|
|
}
|
|
|
|
|
2024-04-07 11:38:15 -07:00
|
|
|
static IndexedMemProfRecord deserializeV2(const MemProfSchema &Schema,
|
|
|
|
const unsigned char *Ptr) {
|
2024-04-03 21:48:38 -07:00
|
|
|
using namespace support;
|
|
|
|
|
|
|
|
IndexedMemProfRecord Record;
|
|
|
|
|
|
|
|
// Read the meminfo nodes.
|
|
|
|
const uint64_t NumNodes =
|
2024-04-16 12:47:02 -07:00
|
|
|
endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
|
2024-05-24 10:42:46 -07:00
|
|
|
Record.AllocSites.reserve(NumNodes);
|
2024-04-03 21:48:38 -07:00
|
|
|
for (uint64_t I = 0; I < NumNodes; I++) {
|
|
|
|
IndexedAllocationInfo Node;
|
2024-04-16 12:47:02 -07:00
|
|
|
Node.CSId = endian::readNext<CallStackId, llvm::endianness::little>(Ptr);
|
2024-04-03 21:48:38 -07:00
|
|
|
Node.Info.deserialize(Schema, Ptr);
|
2024-04-23 13:44:31 -07:00
|
|
|
Ptr += PortableMemInfoBlock::serializedSize(Schema);
|
2024-04-03 21:48:38 -07:00
|
|
|
Record.AllocSites.push_back(Node);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the callsite information.
|
|
|
|
const uint64_t NumCtxs =
|
2024-04-16 12:47:02 -07:00
|
|
|
endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
|
2024-05-24 10:42:46 -07:00
|
|
|
Record.CallSiteIds.reserve(NumCtxs);
|
2024-04-03 21:48:38 -07:00
|
|
|
for (uint64_t J = 0; J < NumCtxs; J++) {
|
|
|
|
CallStackId CSId =
|
2024-04-16 12:47:02 -07:00
|
|
|
endian::readNext<CallStackId, llvm::endianness::little>(Ptr);
|
2024-04-03 21:48:38 -07:00
|
|
|
Record.CallSiteIds.push_back(CSId);
|
2022-03-22 14:40:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return Record;
|
|
|
|
}
|
2024-04-03 21:48:38 -07:00
|
|
|
|
2024-05-31 10:29:14 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-03 21:48:38 -07:00
|
|
|
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);
|
2024-05-31 10:29:14 -07:00
|
|
|
case Version3:
|
|
|
|
return deserializeV3(Schema, Ptr);
|
2024-04-03 21:48:38 -07:00
|
|
|
}
|
|
|
|
llvm_unreachable("unsupported MemProf version");
|
|
|
|
}
|
2022-03-22 14:40:02 -07:00
|
|
|
|
2024-04-16 10:16:48 -07:00
|
|
|
MemProfRecord IndexedMemProfRecord::toMemProfRecord(
|
2024-05-28 07:31:29 -07:00
|
|
|
llvm::function_ref<llvm::SmallVector<Frame>(const CallStackId)> Callback)
|
|
|
|
const {
|
2024-04-16 10:16:48 -07:00
|
|
|
MemProfRecord Record;
|
|
|
|
|
2024-05-23 12:27:38 -07:00
|
|
|
Record.AllocSites.reserve(AllocSites.size());
|
2024-04-16 10:16:48 -07:00
|
|
|
for (const memprof::IndexedAllocationInfo &IndexedAI : AllocSites) {
|
|
|
|
memprof::AllocationInfo AI;
|
|
|
|
AI.Info = IndexedAI.Info;
|
|
|
|
AI.CallStack = Callback(IndexedAI.CSId);
|
2024-05-23 12:27:38 -07:00
|
|
|
Record.AllocSites.push_back(std::move(AI));
|
2024-04-16 10:16:48 -07:00
|
|
|
}
|
|
|
|
|
2024-05-23 12:27:38 -07:00
|
|
|
Record.CallSites.reserve(CallSiteIds.size());
|
2024-04-16 10:16:48 -07:00
|
|
|
for (memprof::CallStackId CSId : CallSiteIds)
|
|
|
|
Record.CallSites.push_back(Callback(CSId));
|
|
|
|
|
|
|
|
return Record;
|
|
|
|
}
|
|
|
|
|
2022-03-21 19:39:24 -07:00
|
|
|
GlobalValue::GUID IndexedMemProfRecord::getGUID(const StringRef FunctionName) {
|
2023-11-29 10:34:21 -08:00
|
|
|
// Canonicalize the function name to drop suffixes such as ".llvm.". Note
|
|
|
|
// we do not drop any ".__uniq." suffixes, as getCanonicalFnName does not drop
|
|
|
|
// those by default. This is by design to differentiate internal linkage
|
|
|
|
// functions during matching. By dropping the other suffixes we can then match
|
|
|
|
// functions in the profile use phase prior to their addition. Note that this
|
|
|
|
// applies to both instrumented and sampled function names.
|
2023-08-29 18:58:39 +00:00
|
|
|
StringRef CanonicalName =
|
|
|
|
sampleprof::FunctionSamples::getCanonicalFnName(FunctionName);
|
2022-03-22 14:40:02 -07:00
|
|
|
|
|
|
|
// We use the function guid which we expect to be a uint64_t. At
|
2023-08-29 18:58:39 +00:00
|
|
|
// this time, it is the lower 64 bits of the md5 of the canonical
|
|
|
|
// function name.
|
|
|
|
return Function::getGUID(CanonicalName);
|
2022-02-17 16:01:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Expected<MemProfSchema> readMemProfSchema(const unsigned char *&Buffer) {
|
|
|
|
using namespace support;
|
|
|
|
|
|
|
|
const unsigned char *Ptr = Buffer;
|
|
|
|
const uint64_t NumSchemaIds =
|
2024-04-16 12:47:02 -07:00
|
|
|
endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
|
2022-02-17 16:01:31 -08:00
|
|
|
if (NumSchemaIds > static_cast<uint64_t>(Meta::Size)) {
|
|
|
|
return make_error<InstrProfError>(instrprof_error::malformed,
|
|
|
|
"memprof schema invalid");
|
|
|
|
}
|
|
|
|
|
|
|
|
MemProfSchema Result;
|
|
|
|
for (size_t I = 0; I < NumSchemaIds; I++) {
|
2023-10-13 23:16:25 -07:00
|
|
|
const uint64_t Tag =
|
2024-04-16 12:47:02 -07:00
|
|
|
endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
|
2022-02-17 16:01:31 -08:00
|
|
|
if (Tag >= static_cast<uint64_t>(Meta::Size)) {
|
|
|
|
return make_error<InstrProfError>(instrprof_error::malformed,
|
|
|
|
"memprof schema invalid");
|
|
|
|
}
|
|
|
|
Result.push_back(static_cast<Meta>(Tag));
|
|
|
|
}
|
|
|
|
// Advace the buffer to one past the schema if we succeeded.
|
|
|
|
Buffer = Ptr;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2024-03-23 19:50:15 -07:00
|
|
|
CallStackId hashCallStack(ArrayRef<FrameId> CS) {
|
|
|
|
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
|
|
|
|
HashBuilder;
|
|
|
|
for (FrameId F : CS)
|
|
|
|
HashBuilder.add(F);
|
|
|
|
llvm::BLAKE3Result<8> Hash = HashBuilder.final();
|
|
|
|
CallStackId CSId;
|
|
|
|
std::memcpy(&CSId, Hash.data(), sizeof(Hash));
|
|
|
|
return CSId;
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:21:49 -07:00
|
|
|
void verifyIndexedMemProfRecord(const IndexedMemProfRecord &Record) {
|
|
|
|
for (const auto &AS : Record.AllocSites) {
|
|
|
|
assert(AS.CSId == hashCallStack(AS.CallStack));
|
|
|
|
(void)AS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-23 19:50:15 -07:00
|
|
|
void verifyFunctionProfileData(
|
|
|
|
const llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord>
|
|
|
|
&FunctionProfileData) {
|
|
|
|
for (const auto &[GUID, Record] : FunctionProfileData) {
|
|
|
|
(void)GUID;
|
2024-03-25 14:21:49 -07:00
|
|
|
verifyIndexedMemProfRecord(Record);
|
2024-03-23 19:50:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:01:31 -08:00
|
|
|
} // namespace memprof
|
|
|
|
} // namespace llvm
|