Skip to content

Commit

Permalink
Revert "[memprof] Refactor out the MemInfoBlock into a macro based def."
Browse files Browse the repository at this point in the history
This reverts commit 9def83c. [4/4]
  • Loading branch information
snehasish committed Feb 14, 2022
1 parent e699904 commit 857ec0d
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 283 deletions.
51 changes: 0 additions & 51 deletions compiler-rt/include/profile/MIBEntryDef.inc

This file was deleted.

129 changes: 60 additions & 69 deletions compiler-rt/include/profile/MemProfData.inc
Expand Up @@ -80,80 +80,71 @@ PACKED(struct SegmentEntry {
}
});

// Packed struct definition for MSVC. We can't use the PACKED macro defined in
// MemProfData.inc since it would mean we are embedding a directive (the
// #include for MIBEntryDef) into the macros which is undefined behaviour.
#ifdef _MSC_VER
__pragma(pack(push,1))
#endif

// A struct representing the heap allocation characteristics of a particular
// runtime context. This struct is shared between the compiler-rt runtime and
// the raw profile reader. The indexed format uses a separate, self-describing
// backwards compatible format.
struct MemInfoBlock{

#define MIBEntryDef(NameTag, Name, Type) Type Name;
#include "MIBEntryDef.inc"
#undef MIBEntryDef

bool operator==(const MemInfoBlock& Other) const {
bool IsEqual = true;
#define MIBEntryDef(NameTag, Name, Type) \
IsEqual = (IsEqual && Name == Other.Name);
#include "MIBEntryDef.inc"
#undef MIBEntryDef
return IsEqual;
}

MemInfoBlock() : AllocCount(0) {}

MemInfoBlock(uint32_t size, uint64_t access_count, uint32_t alloc_timestamp,
uint32_t dealloc_timestamp, uint32_t alloc_cpu, uint32_t dealloc_cpu)
: AllocCount(1), TotalAccessCount(access_count),
MinAccessCount(access_count), MaxAccessCount(access_count),
TotalSize(size), MinSize(size), MaxSize(size),
AllocTimestamp(alloc_timestamp), DeallocTimestamp(dealloc_timestamp),
TotalLifetime(dealloc_timestamp - alloc_timestamp),
MinLifetime(TotalLifetime), MaxLifetime(TotalLifetime),
AllocCpuId(alloc_cpu), DeallocCpuId(dealloc_cpu),
NumLifetimeOverlaps(0), NumSameAllocCpu(0),
NumSameDeallocCpu(0) {
NumMigratedCpu = AllocCpuId != DeallocCpuId;
}

void Merge(const MemInfoBlock &newMIB) {
AllocCount += newMIB.AllocCount;

TotalAccessCount += newMIB.TotalAccessCount;
MinAccessCount = newMIB.MinAccessCount < MinAccessCount ? newMIB.MinAccessCount : MinAccessCount;
MaxAccessCount = newMIB.MaxAccessCount < MaxAccessCount ? newMIB.MaxAccessCount : MaxAccessCount;

TotalSize += newMIB.TotalSize;
MinSize = newMIB.MinSize < MinSize ? newMIB.MinSize : MinSize;
MaxSize = newMIB.MaxSize < MaxSize ? newMIB.MaxSize : MaxSize;

TotalLifetime += newMIB.TotalLifetime;
MinLifetime = newMIB.MinLifetime < MinLifetime ? newMIB.MinLifetime : MinLifetime;
MaxLifetime = newMIB.MaxLifetime > MaxLifetime ? newMIB.MaxLifetime : MaxLifetime;

// We know newMIB was deallocated later, so just need to check if it was
// allocated before last one deallocated.
NumLifetimeOverlaps += newMIB.AllocTimestamp < DeallocTimestamp;
AllocTimestamp = newMIB.AllocTimestamp;
DeallocTimestamp = newMIB.DeallocTimestamp;

NumSameAllocCpu += AllocCpuId == newMIB.AllocCpuId;
NumSameDeallocCpu += DeallocCpuId == newMIB.DeallocCpuId;
AllocCpuId = newMIB.AllocCpuId;
DeallocCpuId = newMIB.DeallocCpuId;
}
PACKED(struct MemInfoBlock {
uint32_t alloc_count;
uint64_t total_access_count, min_access_count, max_access_count;
uint64_t total_size;
uint32_t min_size, max_size;
uint32_t alloc_timestamp, dealloc_timestamp;
uint64_t total_lifetime;
uint32_t min_lifetime, max_lifetime;
uint32_t alloc_cpu_id, dealloc_cpu_id;
uint32_t num_migrated_cpu;

// Only compared to prior deallocated object currently.
uint32_t num_lifetime_overlaps;
uint32_t num_same_alloc_cpu;
uint32_t num_same_dealloc_cpu;

uint64_t data_type_id; // TODO: hash of type name

MemInfoBlock() : alloc_count(0) {}

MemInfoBlock(uint32_t size, uint64_t access_count, uint32_t alloc_timestamp,
uint32_t dealloc_timestamp, uint32_t alloc_cpu, uint32_t dealloc_cpu)
: alloc_count(1), total_access_count(access_count),
min_access_count(access_count), max_access_count(access_count),
total_size(size), min_size(size), max_size(size),
alloc_timestamp(alloc_timestamp), dealloc_timestamp(dealloc_timestamp),
total_lifetime(dealloc_timestamp - alloc_timestamp),
min_lifetime(total_lifetime), max_lifetime(total_lifetime),
alloc_cpu_id(alloc_cpu), dealloc_cpu_id(dealloc_cpu),
num_lifetime_overlaps(0), num_same_alloc_cpu(0),
num_same_dealloc_cpu(0) {
num_migrated_cpu = alloc_cpu_id != dealloc_cpu_id;
}

#ifdef _MSC_VER
} __pragma(pack(pop));
#else
} __attribute__((__packed__));
#endif
void Merge(const MemInfoBlock &newMIB) {
alloc_count += newMIB.alloc_count;

total_access_count += newMIB.total_access_count;
min_access_count = newMIB.min_access_count < min_access_count ? newMIB.min_access_count : min_access_count;
max_access_count = newMIB.max_access_count < max_access_count ? newMIB.max_access_count : max_access_count;

total_size += newMIB.total_size;
min_size = newMIB.min_size < min_size ? newMIB.min_size : min_size;
max_size = newMIB.max_size < max_size ? newMIB.max_size : max_size;

total_lifetime += newMIB.total_lifetime;
min_lifetime = newMIB.min_lifetime < min_lifetime ? newMIB.min_lifetime : min_lifetime;
max_lifetime = newMIB.max_lifetime > max_lifetime ? newMIB.max_lifetime : max_lifetime;

// We know newMIB was deallocated later, so just need to check if it was
// allocated before last one deallocated.
num_lifetime_overlaps += newMIB.alloc_timestamp < dealloc_timestamp;
alloc_timestamp = newMIB.alloc_timestamp;
dealloc_timestamp = newMIB.dealloc_timestamp;

num_same_alloc_cpu += alloc_cpu_id == newMIB.alloc_cpu_id;
num_same_dealloc_cpu += dealloc_cpu_id == newMIB.dealloc_cpu_id;
alloc_cpu_id = newMIB.alloc_cpu_id;
dealloc_cpu_id = newMIB.dealloc_cpu_id;
}
});

} // namespace memprof
} // namespace llvm
Expand Down
38 changes: 19 additions & 19 deletions compiler-rt/lib/memprof/memprof_allocator.cpp
Expand Up @@ -43,32 +43,32 @@ void Print(const MemInfoBlock &M, const u64 id, bool print_terse) {
u64 p;

if (print_terse) {
p = M.TotalSize * 100 / M.AllocCount;
Printf("MIB:%llu/%u/%llu.%02llu/%u/%u/", id, M.AllocCount, p / 100, p % 100,
M.MinSize, M.MaxSize);
p = M.TotalAccessCount * 100 / M.AllocCount;
Printf("%llu.%02llu/%llu/%llu/", p / 100, p % 100, M.MinAccessCount,
M.MaxAccessCount);
p = M.TotalLifetime * 100 / M.AllocCount;
Printf("%llu.%02llu/%u/%u/", p / 100, p % 100, M.MinLifetime,
M.MaxLifetime);
Printf("%u/%u/%u/%u\n", M.NumMigratedCpu, M.NumLifetimeOverlaps,
M.NumSameAllocCpu, M.NumSameDeallocCpu);
p = M.total_size * 100 / M.alloc_count;
Printf("MIB:%llu/%u/%llu.%02llu/%u/%u/", id, M.alloc_count, p / 100,
p % 100, M.min_size, M.max_size);
p = M.total_access_count * 100 / M.alloc_count;
Printf("%llu.%02llu/%llu/%llu/", p / 100, p % 100, M.min_access_count,
M.max_access_count);
p = M.total_lifetime * 100 / M.alloc_count;
Printf("%llu.%02llu/%u/%u/", p / 100, p % 100, M.min_lifetime,
M.max_lifetime);
Printf("%u/%u/%u/%u\n", M.num_migrated_cpu, M.num_lifetime_overlaps,
M.num_same_alloc_cpu, M.num_same_dealloc_cpu);
} else {
p = M.TotalSize * 100 / M.AllocCount;
p = M.total_size * 100 / M.alloc_count;
Printf("Memory allocation stack id = %llu\n", id);
Printf("\talloc_count %u, size (ave/min/max) %llu.%02llu / %u / %u\n",
M.AllocCount, p / 100, p % 100, M.MinSize, M.MaxSize);
p = M.TotalAccessCount * 100 / M.AllocCount;
M.alloc_count, p / 100, p % 100, M.min_size, M.max_size);
p = M.total_access_count * 100 / M.alloc_count;
Printf("\taccess_count (ave/min/max): %llu.%02llu / %llu / %llu\n", p / 100,
p % 100, M.MinAccessCount, M.MaxAccessCount);
p = M.TotalLifetime * 100 / M.AllocCount;
p % 100, M.min_access_count, M.max_access_count);
p = M.total_lifetime * 100 / M.alloc_count;
Printf("\tlifetime (ave/min/max): %llu.%02llu / %u / %u\n", p / 100,
p % 100, M.MinLifetime, M.MaxLifetime);
p % 100, M.min_lifetime, M.max_lifetime);
Printf("\tnum migrated: %u, num lifetime overlaps: %u, num same alloc "
"cpu: %u, num same dealloc_cpu: %u\n",
M.NumMigratedCpu, M.NumLifetimeOverlaps, M.NumSameAllocCpu,
M.NumSameDeallocCpu);
M.num_migrated_cpu, M.num_lifetime_overlaps, M.num_same_alloc_cpu,
M.num_same_dealloc_cpu);
}
}
} // namespace
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/memprof/tests/rawprofile.cpp
Expand Up @@ -82,8 +82,8 @@ TEST(MemProf, Basic) {
// Since we want to override the constructor set vals to make it easier to
// test.
memset(&FakeMIB, 0, sizeof(MemInfoBlock));
FakeMIB.AllocCount = 0x1;
FakeMIB.TotalAccessCount = 0x2;
FakeMIB.alloc_count = 0x1;
FakeMIB.total_access_count = 0x2;

uint64_t FakeIds[2];
FakeIds[0] = PopulateFakeMap(FakeMIB, /*StackPCBegin=*/2, FakeMap);
Expand Down
51 changes: 0 additions & 51 deletions llvm/include/llvm/ProfileData/MIBEntryDef.inc

This file was deleted.

36 changes: 18 additions & 18 deletions llvm/include/llvm/ProfileData/MemProf.h
Expand Up @@ -51,41 +51,41 @@ struct MemProfRecord {

// TODO: Replace this once the format is updated to be version agnostic.
OS << " "
<< "AllocCount: " << Info.AllocCount << "\n";
<< "AllocCount: " << Info.alloc_count << "\n";
OS << " "
<< "TotalAccessCount: " << Info.TotalAccessCount << "\n";
<< "TotalAccessCount: " << Info.total_access_count << "\n";
OS << " "
<< "MinAccessCount: " << Info.MinAccessCount << "\n";
<< "MinAccessCount: " << Info.min_access_count << "\n";
OS << " "
<< "MaxAccessCount: " << Info.MaxAccessCount << "\n";
<< "MaxAccessCount: " << Info.max_access_count << "\n";
OS << " "
<< "TotalSize: " << Info.TotalSize << "\n";
<< "TotalSize: " << Info.total_size << "\n";
OS << " "
<< "MinSize: " << Info.MinSize << "\n";
<< "MinSize: " << Info.min_size << "\n";
OS << " "
<< "MaxSize: " << Info.MaxSize << "\n";
<< "MaxSize: " << Info.max_size << "\n";
OS << " "
<< "AllocTimestamp: " << Info.AllocTimestamp << "\n";
<< "AllocTimestamp: " << Info.alloc_timestamp << "\n";
OS << " "
<< "DeallocTimestamp: " << Info.DeallocTimestamp << "\n";
<< "DeallocTimestamp: " << Info.dealloc_timestamp << "\n";
OS << " "
<< "TotalLifetime: " << Info.TotalLifetime << "\n";
<< "TotalLifetime: " << Info.total_lifetime << "\n";
OS << " "
<< "MinLifetime: " << Info.MinLifetime << "\n";
<< "MinLifetime: " << Info.min_lifetime << "\n";
OS << " "
<< "MaxLifetime: " << Info.MaxLifetime << "\n";
<< "MaxLifetime: " << Info.max_lifetime << "\n";
OS << " "
<< "AllocCpuId: " << Info.AllocCpuId << "\n";
<< "AllocCpuId: " << Info.alloc_cpu_id << "\n";
OS << " "
<< "DeallocCpuId: " << Info.DeallocCpuId << "\n";
<< "DeallocCpuId: " << Info.dealloc_cpu_id << "\n";
OS << " "
<< "NumMigratedCpu: " << Info.NumMigratedCpu << "\n";
<< "NumMigratedCpu: " << Info.num_migrated_cpu << "\n";
OS << " "
<< "NumLifetimeOverlaps: " << Info.NumLifetimeOverlaps << "\n";
<< "NumLifetimeOverlaps: " << Info.num_lifetime_overlaps << "\n";
OS << " "
<< "NumSameAllocCpu: " << Info.NumSameAllocCpu << "\n";
<< "NumSameAllocCpu: " << Info.num_same_alloc_cpu << "\n";
OS << " "
<< "NumSameDeallocCpu: " << Info.NumSameDeallocCpu << "\n";
<< "NumSameDeallocCpu: " << Info.num_same_dealloc_cpu << "\n";
}
};

Expand Down

0 comments on commit 857ec0d

Please sign in to comment.