Skip to content

[MemProf] Write out raw profile bytes in little endian. #150375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions compiler-rt/lib/memprof/memprof_rawprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
#include "sanitizer_common/sanitizer_allocator_internal.h"
#include "sanitizer_common/sanitizer_array_ref.h"
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_linux.h"
#include "sanitizer_common/sanitizer_procmaps.h"
#include "sanitizer_common/sanitizer_stackdepot.h"
#include "sanitizer_common/sanitizer_stackdepotbase.h"
#include "sanitizer_common/sanitizer_stacktrace.h"
#include "sanitizer_common/sanitizer_vector.h"

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

namespace {
template <class T> char *WriteBytes(const T &Pod, char *Buffer) {
*(T *)Buffer = Pod;
static_assert(is_trivially_copyable<T>::value, "T must be POD");
const uint8_t *Src = reinterpret_cast<const uint8_t *>(&Pod);

for (size_t I = 0; I < sizeof(T); ++I)
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
// Reverse byte order since reader is little-endian.
Buffer[I] = Src[sizeof(T) - 1 - I];
#else
Buffer[I] = Src[I];
#endif
return Buffer + sizeof(T);
}

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

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

// Format
// ---------- Header
Expand Down Expand Up @@ -288,5 +294,4 @@ u64 SerializeToRawProfile(MIBMapTy &MIBMap, ArrayRef<LoadedModule> Modules,

return TotalSizeBytes;
}

} // namespace __memprof
35 changes: 33 additions & 2 deletions llvm/lib/ProfileData/MemProfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,39 @@ readMemInfoBlocksCommon(const char *Ptr, bool IsHistogramEncoded = false) {
const uint64_t Id =
endian::readNext<uint64_t, llvm::endianness::little, unaligned>(Ptr);

MemInfoBlock MIB = *reinterpret_cast<const MemInfoBlock *>(Ptr);
Ptr += sizeof(MemInfoBlock);
MemInfoBlock MIB;
#define READ_MIB_FIELD(FIELD) \
MIB.FIELD = endian::readNext<decltype(MIB.FIELD), llvm::endianness::little, \
unaligned>(Ptr)

READ_MIB_FIELD(AllocCount);
READ_MIB_FIELD(TotalAccessCount);
READ_MIB_FIELD(MinAccessCount);
READ_MIB_FIELD(MaxAccessCount);
READ_MIB_FIELD(TotalSize);
READ_MIB_FIELD(MinSize);
READ_MIB_FIELD(MaxSize);
READ_MIB_FIELD(AllocTimestamp);
READ_MIB_FIELD(DeallocTimestamp);
READ_MIB_FIELD(TotalLifetime);
READ_MIB_FIELD(MinLifetime);
READ_MIB_FIELD(MaxLifetime);
READ_MIB_FIELD(AllocCpuId);
READ_MIB_FIELD(DeallocCpuId);
READ_MIB_FIELD(NumMigratedCpu);
READ_MIB_FIELD(NumLifetimeOverlaps);
READ_MIB_FIELD(NumSameAllocCpu);
READ_MIB_FIELD(NumSameDeallocCpu);
READ_MIB_FIELD(DataTypeId);
READ_MIB_FIELD(TotalAccessDensity);
READ_MIB_FIELD(MinAccessDensity);
READ_MIB_FIELD(MaxAccessDensity);
READ_MIB_FIELD(TotalLifetimeAccessDensity);
READ_MIB_FIELD(MinLifetimeAccessDensity);
READ_MIB_FIELD(MaxLifetimeAccessDensity);
READ_MIB_FIELD(AccessHistogramSize);
READ_MIB_FIELD(AccessHistogram);
#undef READ_MIB_FIELD

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