Skip to content

Commit d1e43f6

Browse files
authored
[MemProf] Write out raw profile bytes in little endian. (#150375)
Instead of writing out in native endian, write out the raw profile bytes in little endian. Also update the MIB data in little endian. Also clean up some lint and unused includes in rawprofile.cpp.
1 parent 1b9ee0b commit d1e43f6

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

compiler-rt/lib/memprof/memprof_rawprofile.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
#include "sanitizer_common/sanitizer_allocator_internal.h"
88
#include "sanitizer_common/sanitizer_array_ref.h"
99
#include "sanitizer_common/sanitizer_common.h"
10-
#include "sanitizer_common/sanitizer_linux.h"
11-
#include "sanitizer_common/sanitizer_procmaps.h"
1210
#include "sanitizer_common/sanitizer_stackdepot.h"
13-
#include "sanitizer_common/sanitizer_stackdepotbase.h"
1411
#include "sanitizer_common/sanitizer_stacktrace.h"
1512
#include "sanitizer_common/sanitizer_vector.h"
1613

@@ -23,7 +20,16 @@ using ::llvm::memprof::encodeHistogramCount;
2320

2421
namespace {
2522
template <class T> char *WriteBytes(const T &Pod, char *Buffer) {
26-
*(T *)Buffer = Pod;
23+
static_assert(is_trivially_copyable<T>::value, "T must be POD");
24+
const uint8_t *Src = reinterpret_cast<const uint8_t *>(&Pod);
25+
26+
for (size_t I = 0; I < sizeof(T); ++I)
27+
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
28+
// Reverse byte order since reader is little-endian.
29+
Buffer[I] = Src[sizeof(T) - 1 - I];
30+
#else
31+
Buffer[I] = Src[I];
32+
#endif
2733
return Buffer + sizeof(T);
2834
}
2935

@@ -33,7 +39,6 @@ void RecordStackId(const uptr Key, UNUSED LockedMemInfoBlock *const &MIB,
3339
auto *StackIds = reinterpret_cast<Vector<u64> *>(Arg);
3440
StackIds->PushBack(Key);
3541
}
36-
} // namespace
3742

3843
u64 SegmentSizeBytes(ArrayRef<LoadedModule> Modules) {
3944
u64 NumSegmentsToRecord = 0;
@@ -184,6 +189,7 @@ void SerializeMIBInfoToBuffer(MIBMapTy &MIBMap, const Vector<u64> &StackIds,
184189
CHECK(ExpectedNumBytes >= static_cast<u64>(Ptr - Buffer) &&
185190
"Expected num bytes != actual bytes written");
186191
}
192+
} // namespace
187193

188194
// Format
189195
// ---------- Header
@@ -288,5 +294,4 @@ u64 SerializeToRawProfile(MIBMapTy &MIBMap, ArrayRef<LoadedModule> Modules,
288294

289295
return TotalSizeBytes;
290296
}
291-
292297
} // namespace __memprof

llvm/lib/ProfileData/MemProfReader.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,39 @@ readMemInfoBlocksCommon(const char *Ptr, bool IsHistogramEncoded = false) {
146146
const uint64_t Id =
147147
endian::readNext<uint64_t, llvm::endianness::little, unaligned>(Ptr);
148148

149-
MemInfoBlock MIB = *reinterpret_cast<const MemInfoBlock *>(Ptr);
150-
Ptr += sizeof(MemInfoBlock);
149+
MemInfoBlock MIB;
150+
#define READ_MIB_FIELD(FIELD) \
151+
MIB.FIELD = endian::readNext<decltype(MIB.FIELD), llvm::endianness::little, \
152+
unaligned>(Ptr)
153+
154+
READ_MIB_FIELD(AllocCount);
155+
READ_MIB_FIELD(TotalAccessCount);
156+
READ_MIB_FIELD(MinAccessCount);
157+
READ_MIB_FIELD(MaxAccessCount);
158+
READ_MIB_FIELD(TotalSize);
159+
READ_MIB_FIELD(MinSize);
160+
READ_MIB_FIELD(MaxSize);
161+
READ_MIB_FIELD(AllocTimestamp);
162+
READ_MIB_FIELD(DeallocTimestamp);
163+
READ_MIB_FIELD(TotalLifetime);
164+
READ_MIB_FIELD(MinLifetime);
165+
READ_MIB_FIELD(MaxLifetime);
166+
READ_MIB_FIELD(AllocCpuId);
167+
READ_MIB_FIELD(DeallocCpuId);
168+
READ_MIB_FIELD(NumMigratedCpu);
169+
READ_MIB_FIELD(NumLifetimeOverlaps);
170+
READ_MIB_FIELD(NumSameAllocCpu);
171+
READ_MIB_FIELD(NumSameDeallocCpu);
172+
READ_MIB_FIELD(DataTypeId);
173+
READ_MIB_FIELD(TotalAccessDensity);
174+
READ_MIB_FIELD(MinAccessDensity);
175+
READ_MIB_FIELD(MaxAccessDensity);
176+
READ_MIB_FIELD(TotalLifetimeAccessDensity);
177+
READ_MIB_FIELD(MinLifetimeAccessDensity);
178+
READ_MIB_FIELD(MaxLifetimeAccessDensity);
179+
READ_MIB_FIELD(AccessHistogramSize);
180+
READ_MIB_FIELD(AccessHistogram);
181+
#undef READ_MIB_FIELD
151182

152183
if (MIB.AccessHistogramSize > 0) {
153184
// The in-memory representation uses uint64_t for histogram entries.

0 commit comments

Comments
 (0)