Skip to content

Commit be8da74

Browse files
Add unit tests.
1 parent c288fc9 commit be8da74

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

llvm/include/llvm/ProfileData/MemProf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ struct PortableMemInfoBlock {
123123
#undef MIBEntryDef
124124
}
125125

126+
// Return the schema, only for unit tests.
127+
std::bitset<llvm::to_underlying(Meta::Size)> getSchema() const {
128+
return Schema;
129+
}
130+
126131
// Define getters for each type which can be called by analyses.
127132
#define MIBEntryDef(NameTag, Name, Type) \
128133
Type get##Name() const { \

llvm/unittests/ProfileData/MemProfTest.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "llvm/ProfileData/MemProf.h"
22
#include "llvm/ADT/DenseMap.h"
33
#include "llvm/ADT/MapVector.h"
4+
#include "llvm/ADT/STLForwardCompat.h"
45
#include "llvm/DebugInfo/DIContext.h"
56
#include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
67
#include "llvm/IR/Value.h"
@@ -326,6 +327,65 @@ TEST(MemProf, RecordSerializationRoundTripVerion2) {
326327
EXPECT_EQ(Record, GotRecord);
327328
}
328329

330+
TEST(MemProf, RecordSerializationRoundTripVersion2HotColdSchema) {
331+
const auto Schema = llvm::memprof::getHotColdSchema();
332+
333+
MemInfoBlock Info;
334+
Info.AllocCount = 11;
335+
Info.TotalSize = 22;
336+
Info.TotalLifetime = 33;
337+
Info.TotalLifetimeAccessDensity = 44;
338+
339+
llvm::SmallVector<llvm::memprof::CallStackId> CallStackIds = {0x123, 0x456};
340+
341+
llvm::SmallVector<llvm::memprof::CallStackId> CallSiteIds = {0x333, 0x444};
342+
343+
IndexedMemProfRecord Record;
344+
for (const auto &CSId : CallStackIds) {
345+
// Use the same info block for both allocation sites.
346+
Record.AllocSites.emplace_back(llvm::SmallVector<FrameId>(), CSId, Info,
347+
Schema);
348+
}
349+
Record.CallSiteIds.assign(CallSiteIds);
350+
351+
std::bitset<llvm::to_underlying(Meta::Size)> SchemaBitSet;
352+
for (auto Id : Schema)
353+
SchemaBitSet.set(llvm::to_underlying(Id));
354+
355+
// Verify that SchemaBitSet has the fields we expect and nothing else, which
356+
// we check with count().
357+
EXPECT_EQ(SchemaBitSet.count(), 4U);
358+
EXPECT_TRUE(SchemaBitSet[llvm::to_underlying(Meta::AllocCount)]);
359+
EXPECT_TRUE(SchemaBitSet[llvm::to_underlying(Meta::TotalSize)]);
360+
EXPECT_TRUE(SchemaBitSet[llvm::to_underlying(Meta::TotalLifetime)]);
361+
EXPECT_TRUE(
362+
SchemaBitSet[llvm::to_underlying(Meta::TotalLifetimeAccessDensity)]);
363+
364+
// Verify that Schema has propagated all the way to the Info field in each
365+
// IndexedAllocationInfo.
366+
ASSERT_THAT(Record.AllocSites, ::SizeIs(2));
367+
EXPECT_EQ(Record.AllocSites[0].Info.getSchema(), SchemaBitSet);
368+
EXPECT_EQ(Record.AllocSites[1].Info.getSchema(), SchemaBitSet);
369+
370+
std::string Buffer;
371+
llvm::raw_string_ostream OS(Buffer);
372+
Record.serialize(Schema, OS, llvm::memprof::Version2);
373+
OS.flush();
374+
375+
const IndexedMemProfRecord GotRecord = IndexedMemProfRecord::deserialize(
376+
Schema, reinterpret_cast<const unsigned char *>(Buffer.data()),
377+
llvm::memprof::Version2);
378+
379+
// Verify that Schema comes back correctly after deserialization. Technically,
380+
// the comparison between Record and GotRecord below includes the comparison
381+
// of their Schemas, but we'll verify the Schemas on our own.
382+
ASSERT_THAT(GotRecord.AllocSites, ::SizeIs(2));
383+
EXPECT_EQ(GotRecord.AllocSites[0].Info.getSchema(), SchemaBitSet);
384+
EXPECT_EQ(GotRecord.AllocSites[1].Info.getSchema(), SchemaBitSet);
385+
386+
EXPECT_EQ(Record, GotRecord);
387+
}
388+
329389
TEST(MemProf, SymbolizationFilter) {
330390
std::unique_ptr<MockSymbolizer> Symbolizer(new MockSymbolizer());
331391

0 commit comments

Comments
 (0)