Skip to content

Commit f9fb66f

Browse files
committed
Add reader support.
1 parent 3371ae2 commit f9fb66f

25 files changed

+81
-17
lines changed

compiler-rt/include/profile/MemProfData.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
#define MEMPROF_RAW_VERSION 5ULL
3737

3838
// Currently supported versions.
39-
#define MEMPROF_RAW_SUPPORTED_VERSIONS \
40-
{ 3ULL, 4ULL, 5ULL }
39+
#define MEMPROF_RAW_SUPPORTED_VERSIONS {3ULL, 4ULL, 5ULL}
4140

4241
#define MEMPROF_V3_MIB_SIZE 132ULL;
4342

compiler-rt/lib/memprof/memprof_allocator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ struct Allocator {
333333
u8 Counter =
334334
*((u8 *)HISTOGRAM_MEM_TO_SHADOW(p + HISTOGRAM_GRANULARITY * i));
335335
// Cap the counter at HISTOGRAM_MAX_COUNTER (255) to prevent overflow
336-
((uint8_t *)Histogram)[i] = (Counter > HISTOGRAM_MAX_COUNTER) ? HISTOGRAM_MAX_COUNTER : Counter;
336+
((uint8_t *)Histogram)[i] =
337+
(Counter > HISTOGRAM_MAX_COUNTER) ? HISTOGRAM_MAX_COUNTER : Counter;
337338
}
338339
MemInfoBlock newMIB(user_size, c, m->timestamp_ms, curtime, m->cpu_id,
339340
GetCpuId(), Histogram, HistogramSize);

compiler-rt/test/memprof/TestCases/memprof_histogram_uint8.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
int main() {
77
// Allocate memory that will create a histogram
88
char *buffer = (char *)malloc(1024);
9-
if (!buffer) return 1;
9+
if (!buffer)
10+
return 1;
1011

1112
for (int i = 0; i < 10; ++i) {
1213
// Access every 8th byte (since shadow granularity is 8b.
@@ -18,7 +19,7 @@ int main() {
1819
}
1920

2021
for (int j = 0; j < 400; ++j) {
21-
buffer[16] = 'B'; // Count is saturated at 255
22+
buffer[16] = 'B'; // Count is saturated at 255
2223
}
2324

2425
// Free the memory to trigger MIB creation with histogram

llvm/include/llvm/ProfileData/MemProfData.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
#define MEMPROF_RAW_VERSION 5ULL
3737

3838
// Currently supported versions.
39-
#define MEMPROF_RAW_SUPPORTED_VERSIONS \
40-
{ 3ULL, 4ULL, 5ULL }
39+
#define MEMPROF_RAW_SUPPORTED_VERSIONS {3ULL, 4ULL, 5ULL}
4140

4241
#define MEMPROF_V3_MIB_SIZE 132ULL;
4342

llvm/lib/ProfileData/MemProfReader.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,68 @@ readMemInfoBlocksV4(const char *Ptr) {
166166
return Items;
167167
}
168168

169+
llvm::SmallVector<std::pair<uint64_t, MemInfoBlock>>
170+
readMemInfoBlocksV5(const char *Ptr) {
171+
using namespace support;
172+
173+
const uint64_t NumItemsToRead =
174+
endian::readNext<uint64_t, llvm::endianness::little, unaligned>(Ptr);
175+
176+
llvm::SmallVector<std::pair<uint64_t, MemInfoBlock>> Items;
177+
for (uint64_t I = 0; I < NumItemsToRead; I++) {
178+
const uint64_t Id =
179+
endian::readNext<uint64_t, llvm::endianness::little, unaligned>(Ptr);
180+
181+
MemInfoBlock MIB;
182+
#define READ_MIB_FIELD(FIELD) \
183+
MIB.FIELD = endian::readNext<decltype(MIB.FIELD), llvm::endianness::little, \
184+
unaligned>(Ptr)
185+
186+
READ_MIB_FIELD(AllocCount);
187+
READ_MIB_FIELD(TotalAccessCount);
188+
READ_MIB_FIELD(MinAccessCount);
189+
READ_MIB_FIELD(MaxAccessCount);
190+
READ_MIB_FIELD(TotalSize);
191+
READ_MIB_FIELD(MinSize);
192+
READ_MIB_FIELD(MaxSize);
193+
READ_MIB_FIELD(AllocTimestamp);
194+
READ_MIB_FIELD(DeallocTimestamp);
195+
READ_MIB_FIELD(TotalLifetime);
196+
READ_MIB_FIELD(MinLifetime);
197+
READ_MIB_FIELD(MaxLifetime);
198+
READ_MIB_FIELD(AllocCpuId);
199+
READ_MIB_FIELD(DeallocCpuId);
200+
READ_MIB_FIELD(NumMigratedCpu);
201+
READ_MIB_FIELD(NumLifetimeOverlaps);
202+
READ_MIB_FIELD(NumSameAllocCpu);
203+
READ_MIB_FIELD(NumSameDeallocCpu);
204+
READ_MIB_FIELD(DataTypeId);
205+
READ_MIB_FIELD(TotalAccessDensity);
206+
READ_MIB_FIELD(MinAccessDensity);
207+
READ_MIB_FIELD(MaxAccessDensity);
208+
READ_MIB_FIELD(TotalLifetimeAccessDensity);
209+
READ_MIB_FIELD(MinLifetimeAccessDensity);
210+
READ_MIB_FIELD(MaxLifetimeAccessDensity);
211+
READ_MIB_FIELD(AccessHistogramSize);
212+
READ_MIB_FIELD(AccessHistogram);
213+
#undef READ_MIB_FIELD
214+
215+
if (MIB.AccessHistogramSize > 0) {
216+
// The in-memory representation uses uint64_t for histogram entries.
217+
MIB.AccessHistogram =
218+
(uintptr_t)malloc(MIB.AccessHistogramSize * sizeof(uint64_t));
219+
for (uint64_t J = 0; J < MIB.AccessHistogramSize; J++) {
220+
// The on-disk format for V5 uses uint8_t.
221+
const uint8_t Val =
222+
endian::readNext<uint8_t, llvm::endianness::little, unaligned>(Ptr);
223+
((uint64_t *)MIB.AccessHistogram)[J] = Val;
224+
}
225+
}
226+
Items.push_back({Id, MIB});
227+
}
228+
return Items;
229+
}
230+
169231
CallStackMap readStackInfo(const char *Ptr) {
170232
using namespace support;
171233

@@ -658,6 +720,8 @@ RawMemProfReader::readMemInfoBlocks(const char *Ptr) {
658720
return readMemInfoBlocksV3(Ptr);
659721
if (MemprofRawVersion == 4ULL)
660722
return readMemInfoBlocksV4(Ptr);
723+
if (MemprofRawVersion == 5ULL)
724+
return readMemInfoBlocksV5(Ptr);
661725
llvm_unreachable(
662726
"Panic: Unsupported version number when reading MemInfoBlocks");
663727
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)