Reapply [memprof] Update YAML traits for writer purposes (#118720)

For Frames, we prefer the inline notation for the brevity.

For PortableMemInfoBlock, we go through all member fields and print
out those that are populated.

This iteration works around the unavailability of
ScalarTraits<uintptr_t> on macOS.
This commit is contained in:
Kazu Hirata
2024-12-05 13:19:19 -08:00
parent 3a7d1b5c16
commit dbd920b290
2 changed files with 49 additions and 2 deletions

View File

@@ -1183,6 +1183,10 @@ template <> struct MappingTraits<memprof::Frame> {
(void)Column;
(void)IsInlineFrame;
}
// Request the inline notation for brevity:
// { Function: 123, LineOffset: 11, Column: 10; IsInlineFrame: true }
static const bool flow = true;
};
template <> struct CustomMappingTraits<memprof::PortableMemInfoBlock> {
@@ -1207,8 +1211,15 @@ template <> struct CustomMappingTraits<memprof::PortableMemInfoBlock> {
Io.setError("Key is not a valid validation event");
}
static void output(IO &Io, memprof::PortableMemInfoBlock &VI) {
llvm_unreachable("To be implemented");
static void output(IO &Io, memprof::PortableMemInfoBlock &MIB) {
auto Schema = MIB.getSchema();
#define MIBEntryDef(NameTag, Name, Type) \
if (Schema.test(llvm::to_underlying(memprof::Meta::Name))) { \
uint64_t Value = MIB.Name; \
Io.mapRequired(#Name, Value); \
}
#include "llvm/ProfileData/MIBEntryDef.inc"
#undef MIBEntryDef
}
};

View File

@@ -807,4 +807,40 @@ HeapProfileRecords:
EXPECT_THAT(Record.CallSiteIds,
ElementsAre(hashCallStack(CS3), hashCallStack(CS4)));
}
template <typename T> std::string serializeInYAML(T &Val) {
std::string Out;
llvm::raw_string_ostream OS(Out);
llvm::yaml::Output Yout(OS);
Yout << Val;
return Out;
}
TEST(MemProf, YAMLWriterFrame) {
Frame F(11, 22, 33, true);
std::string Out = serializeInYAML(F);
EXPECT_EQ(Out, R"YAML(---
{ Function: 11, LineOffset: 22, Column: 33, Inline: true }
...
)YAML");
}
TEST(MemProf, YAMLWriterMIB) {
MemInfoBlock MIB;
MIB.AllocCount = 111;
MIB.TotalSize = 222;
MIB.TotalLifetime = 333;
MIB.TotalLifetimeAccessDensity = 444;
PortableMemInfoBlock PMIB(MIB, llvm::memprof::getHotColdSchema());
std::string Out = serializeInYAML(PMIB);
EXPECT_EQ(Out, R"YAML(---
AllocCount: 111
TotalSize: 222
TotalLifetime: 333
TotalLifetimeAccessDensity: 444
...
)YAML");
}
} // namespace