-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Support MemProf on darwin #69640
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
base: main
Are you sure you want to change the base?
Support MemProf on darwin #69640
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Test sanitizer link flags on Darwin. | ||
|
||
// RUN: %clang -### --target=x86_64-darwin \ | ||
// RUN: -stdlib=platform -fmemory-profile %s 2>&1 \ | ||
// RUN: | FileCheck --check-prefix=CHECK-MEMPROF %s | ||
|
||
// CHECK-MEMPROF: "{{.*}}ld{{(.exe)?}}" | ||
// CHECK-MEMPROF-NOT: "-lstdc++" | ||
// CHECK-MEMPROF-NOT: "-lc++" | ||
// CHECK-MEMPROF: libclang_rt.memprof_osx_dynamic.dylib" | ||
// CHECK-MEMPROF: "-rpath" "@executable_path" | ||
// CHECK-MEMPROF: "-rpath" "{{.*}}lib{{.*}}darwin" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,7 +78,11 @@ static int GetCpuId(void) { | |
// will seg fault as the address of __vdso_getcpu will be null. | ||
if (!memprof_inited) | ||
return -1; | ||
#if SANITIZER_APPLE | ||
return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is a way to do this on Apple then add a FIXME? |
||
#else | ||
return sched_getcpu(); | ||
#endif | ||
} | ||
|
||
// Compute the timestamp in ms. | ||
|
@@ -261,9 +265,12 @@ struct Allocator { | |
atomic_uint8_t destructing; | ||
atomic_uint8_t constructed; | ||
bool print_text; | ||
bool print_binary_refs; | ||
|
||
// ------------------- Initialization ------------------------ | ||
explicit Allocator(LinkerInitialized) : print_text(flags()->print_text) { | ||
explicit Allocator(LinkerInitialized) | ||
: print_text(flags()->print_text), | ||
print_binary_refs(flags()->print_binary_refs) { | ||
atomic_store_relaxed(&destructing, 0); | ||
atomic_store_relaxed(&constructed, 1); | ||
} | ||
|
@@ -279,13 +286,54 @@ struct Allocator { | |
Print(Value->mib, Key, bool(Arg)); | ||
} | ||
|
||
using SegmentEntry = ::llvm::memprof::SegmentEntry; | ||
void FinishAndWrite() { | ||
if (print_text && common_flags()->print_module_map) | ||
DumpProcessMap(); | ||
|
||
allocator.ForceLock(); | ||
|
||
InsertLiveBlocks(); | ||
#if SANITIZER_APPLE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which part of this handling is Apple-specific? |
||
if (print_binary_refs) { | ||
__sanitizer::ListOfModules List; | ||
List.init(); | ||
ArrayRef<LoadedModule> Modules(List.begin(), List.end()); | ||
for (const auto &Module : Modules) { | ||
for (const auto &Segment : Module.ranges()) { | ||
if (true) { // Segment.executable) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the if-condition be always true? Leftover debugging handling? |
||
SegmentEntry Entry(Segment.beg, Segment.end, Module.base_address()); | ||
// CHECK(Module.uuid_size() <= MEMPROF_BUILDID_MAX_SIZE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this check and the line below it be uncommented? |
||
// Entry.BuildIdSize = Module.uuid_size(); | ||
memcpy(Entry.BuildId, Module.uuid(), Module.uuid_size()); | ||
// Print out the segment information. | ||
Printf(" -\n"); | ||
Printf("[MemProf] BuildId: "); | ||
for (size_t I = 0; I < Module.uuid_size() /*Entry.BuildIdSize*/; | ||
I++) { | ||
Printf("%02x", Entry.BuildId[I]); | ||
} | ||
Printf("\n"); | ||
Printf("[MemProf] BuildIdName: %s\n", Module.full_name()); | ||
// If it is the main binary, check shadow. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you know if it is the main binary? |
||
Printf("[MemProf] Start: 0x%zx\n", Entry.Start); | ||
if (AddrIsInLowMem(Entry.Start)) { | ||
for (auto t = Entry.Start & SHADOW_MASK; t < Entry.End; | ||
t += MEM_GRANULARITY) { | ||
// should not be 64, as it will include the next shadow memory | ||
u64 c = GetShadowCount(t, 60); | ||
if (c > 0) | ||
Printf("[MemProf] Shadow: %p %d\n", (void *)t, c); | ||
} | ||
} | ||
Printf("[MemProf] End: 0x%zx\n", Entry.End); | ||
Printf("[MemProf] Offset: 0x%zx\n", Entry.Offset); | ||
} else { | ||
} | ||
} | ||
} | ||
} | ||
#endif | ||
if (print_text) { | ||
if (!flags()->print_terse) | ||
Printf("Recorded MIBs (incl. live on exit):\n"); | ||
|
@@ -706,6 +754,18 @@ uptr memprof_malloc_usable_size(const void *ptr, uptr pc, uptr bp) { | |
return usable_size; | ||
} | ||
|
||
uptr memprof_mz_size(const void *ptr) { | ||
return instance.AllocationSize(reinterpret_cast<uptr>(ptr)); | ||
} | ||
|
||
void memprof_mz_force_lock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS { | ||
instance.ForceLock(); | ||
} | ||
|
||
void memprof_mz_force_unlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS { | ||
instance.ForceUnlock(); | ||
} | ||
|
||
} // namespace __memprof | ||
|
||
// ---------------------- Interface ---------------- {{{1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,6 @@ | |
#include "sanitizer_common/sanitizer_allocator.h" | ||
#include "sanitizer_common/sanitizer_list.h" | ||
|
||
#if !defined(__x86_64__) | ||
#error Unsupported platform | ||
#endif | ||
#if !SANITIZER_CAN_USE_ALLOCATOR64 | ||
#error Only 64-bit allocator supported | ||
#endif | ||
|
||
namespace __memprof { | ||
|
||
enum AllocType { | ||
|
@@ -46,6 +39,7 @@ struct MemprofMapUnmapCallback { | |
void OnUnmap(uptr p, uptr size) const; | ||
}; | ||
|
||
#if SANITIZER_CAN_USE_ALLOCATOR64 | ||
constexpr uptr kAllocatorSpace = 0x600000000000ULL; | ||
constexpr uptr kAllocatorSize = 0x40000000000ULL; // 4T. | ||
typedef DefaultSizeClassMap SizeClassMap; | ||
|
@@ -63,6 +57,23 @@ struct AP64 { // Allocator64 parameters. Deliberately using a short name. | |
template <typename AddressSpaceView> | ||
using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>; | ||
using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>; | ||
#endif | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Combine these 2 lines into an "#else" ? |
||
#if !SANITIZER_CAN_USE_ALLOCATOR64 | ||
typedef CompactSizeClassMap SizeClassMap; | ||
template <typename AddressSpaceViewTy> struct AP32 { | ||
static const uptr kSpaceBeg = 0; | ||
static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE; | ||
static const uptr kMetadataSize = 0; | ||
typedef __memprof::SizeClassMap SizeClassMap; | ||
static const uptr kRegionSizeLog = 20; | ||
using AddressSpaceView = AddressSpaceViewTy; | ||
typedef MemprofMapUnmapCallback MapUnmapCallback; | ||
static const uptr kFlags = 0; | ||
}; | ||
template <typename AddressSpaceView> | ||
using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>; | ||
using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>; | ||
#endif | ||
|
||
static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses; | ||
|
||
|
@@ -101,6 +112,10 @@ int memprof_posix_memalign(void **memptr, uptr alignment, uptr size, | |
BufferedStackTrace *stack); | ||
uptr memprof_malloc_usable_size(const void *ptr, uptr pc, uptr bp); | ||
|
||
uptr memprof_mz_size(const void *ptr); | ||
void memprof_mz_force_lock(); | ||
void memprof_mz_force_unlock(); | ||
|
||
void PrintInternalAllocatorStats(); | ||
|
||
} // namespace __memprof | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this meant to be FALSE? Otherwise this new variable is always TRUE.