2016-06-02 09:21:52 +08:00
|
|
|
//===-- asan_memory_profile.cc.cc -----------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// This file implements __sanitizer_print_memory_profile.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stacktrace.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stoptheworld.h"
|
|
|
|
#include "lsan/lsan_common.h"
|
|
|
|
#include "asan/asan_allocator.h"
|
|
|
|
|
2016-06-02 12:01:58 +08:00
|
|
|
#if CAN_SANITIZE_LEAKS
|
2016-06-02 09:21:52 +08:00
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
struct AllocationSite {
|
|
|
|
u32 id;
|
|
|
|
uptr total_size;
|
|
|
|
uptr count;
|
|
|
|
};
|
|
|
|
|
|
|
|
class HeapProfile {
|
|
|
|
public:
|
|
|
|
HeapProfile() : allocations_(1024) {}
|
2017-01-06 06:17:53 +08:00
|
|
|
|
|
|
|
void ProcessChunk(const AsanChunkView& cv) {
|
|
|
|
if (cv.IsAllocated()) {
|
|
|
|
total_allocated_user_size_ += cv.UsedSize();
|
|
|
|
total_allocated_count_++;
|
|
|
|
u32 id = cv.GetAllocStackId();
|
|
|
|
if (id)
|
|
|
|
Insert(id, cv.UsedSize());
|
|
|
|
} else if (cv.IsQuarantined()) {
|
|
|
|
total_quarantined_user_size_ += cv.UsedSize();
|
|
|
|
total_quarantined_count_++;
|
|
|
|
} else {
|
|
|
|
total_other_count_++;
|
2016-06-02 09:21:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 07:27:14 +08:00
|
|
|
void Print(uptr top_percent, uptr max_number_of_contexts) {
|
2016-06-02 09:21:52 +08:00
|
|
|
InternalSort(&allocations_, allocations_.size(),
|
|
|
|
[](const AllocationSite &a, const AllocationSite &b) {
|
|
|
|
return a.total_size > b.total_size;
|
|
|
|
});
|
2017-01-06 06:17:53 +08:00
|
|
|
CHECK(total_allocated_user_size_);
|
2016-06-02 09:21:52 +08:00
|
|
|
uptr total_shown = 0;
|
2017-01-06 06:17:53 +08:00
|
|
|
Printf("Live Heap Allocations: %zd bytes in %zd chunks; quarantined: "
|
|
|
|
"%zd bytes in %zd chunks; %zd other chunks; total chunks: %zd; "
|
2017-03-16 07:27:14 +08:00
|
|
|
"showing top %zd%% (at most %zd unique contexts)\n",
|
2017-01-06 06:17:53 +08:00
|
|
|
total_allocated_user_size_, total_allocated_count_,
|
|
|
|
total_quarantined_user_size_, total_quarantined_count_,
|
|
|
|
total_other_count_, total_allocated_count_ +
|
2017-03-16 07:27:14 +08:00
|
|
|
total_quarantined_count_ + total_other_count_, top_percent,
|
|
|
|
max_number_of_contexts);
|
|
|
|
for (uptr i = 0; i < Min(allocations_.size(), max_number_of_contexts);
|
|
|
|
i++) {
|
2016-06-02 09:21:52 +08:00
|
|
|
auto &a = allocations_[i];
|
|
|
|
Printf("%zd byte(s) (%zd%%) in %zd allocation(s)\n", a.total_size,
|
2017-01-06 06:17:53 +08:00
|
|
|
a.total_size * 100 / total_allocated_user_size_, a.count);
|
2016-06-02 09:21:52 +08:00
|
|
|
StackDepotGet(a.id).Print();
|
|
|
|
total_shown += a.total_size;
|
2017-01-06 06:17:53 +08:00
|
|
|
if (total_shown * 100 / total_allocated_user_size_ > top_percent)
|
2016-06-02 09:21:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-01-06 06:17:53 +08:00
|
|
|
uptr total_allocated_user_size_ = 0;
|
|
|
|
uptr total_allocated_count_ = 0;
|
|
|
|
uptr total_quarantined_user_size_ = 0;
|
|
|
|
uptr total_quarantined_count_ = 0;
|
|
|
|
uptr total_other_count_ = 0;
|
2016-06-02 09:21:52 +08:00
|
|
|
InternalMmapVector<AllocationSite> allocations_;
|
2017-01-06 06:17:53 +08:00
|
|
|
|
|
|
|
void Insert(u32 id, uptr size) {
|
|
|
|
// Linear lookup will be good enough for most cases (although not all).
|
|
|
|
for (uptr i = 0; i < allocations_.size(); i++) {
|
|
|
|
if (allocations_[i].id == id) {
|
|
|
|
allocations_[i].total_size += size;
|
|
|
|
allocations_[i].count++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allocations_.push_back({id, size, 1});
|
|
|
|
}
|
2016-06-02 09:21:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void ChunkCallback(uptr chunk, void *arg) {
|
2017-01-06 06:17:53 +08:00
|
|
|
reinterpret_cast<HeapProfile*>(arg)->ProcessChunk(
|
|
|
|
FindHeapChunkByAllocBeg(chunk));
|
2016-06-02 09:21:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MemoryProfileCB(const SuspendedThreadsList &suspended_threads_list,
|
|
|
|
void *argument) {
|
|
|
|
HeapProfile hp;
|
|
|
|
__lsan::ForEachChunk(ChunkCallback, &hp);
|
2017-03-16 07:27:14 +08:00
|
|
|
uptr *Arg = reinterpret_cast<uptr*>(argument);
|
|
|
|
hp.Print(Arg[0], Arg[1]);
|
2017-10-14 02:38:10 +08:00
|
|
|
|
|
|
|
if (Verbosity())
|
|
|
|
__asan_print_accumulated_stats();
|
2016-06-02 09:21:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __asan
|
|
|
|
|
2017-03-16 07:53:12 +08:00
|
|
|
#endif // CAN_SANITIZE_LEAKS
|
|
|
|
|
2016-06-02 09:21:52 +08:00
|
|
|
extern "C" {
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
2017-03-16 07:27:14 +08:00
|
|
|
void __sanitizer_print_memory_profile(uptr top_percent,
|
|
|
|
uptr max_number_of_contexts) {
|
2017-03-16 07:53:12 +08:00
|
|
|
#if CAN_SANITIZE_LEAKS
|
2017-03-16 07:27:14 +08:00
|
|
|
uptr Arg[2];
|
|
|
|
Arg[0] = top_percent;
|
|
|
|
Arg[1] = max_number_of_contexts;
|
|
|
|
__sanitizer::StopTheWorld(__asan::MemoryProfileCB, Arg);
|
2017-03-16 07:53:12 +08:00
|
|
|
#endif // CAN_SANITIZE_LEAKS
|
2016-06-02 09:21:52 +08:00
|
|
|
}
|
|
|
|
} // extern "C"
|