Skip to content
Open
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
4 changes: 4 additions & 0 deletions heaptrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

#include <stdio.h>

#include <atomic>

extern FILE *outfp;

extern std::atomic<long long> mem_overhead;

#define TERM_COLOR_NORMAL ""
#define TERM_COLOR_RESET "\033[0m"
#define TERM_COLOR_BOLD "\033[1m"
Expand Down
56 changes: 45 additions & 11 deletions libheaptrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ struct opts opts;

FILE *outfp;

std::atomic<long long> mem_overhead;

__constructor
static void heaptrace_init()
{
Expand Down Expand Up @@ -114,6 +116,8 @@ static void heaptrace_init()
pid, comm.c_str());
}

mem_overhead = 0;

initialized = true;
}

Expand Down Expand Up @@ -143,8 +147,10 @@ void* operator new(size_t size)
{
auto* tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized))
if (unlikely(tfs->hook_guard || !initialized)) {
mem_overhead += size;
return __libc_malloc(size);
}

tfs->hook_guard = true;

Expand All @@ -162,8 +168,10 @@ void* operator new[](size_t size)
{
auto* tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized))
if (unlikely(tfs->hook_guard || !initialized)) {
mem_overhead += size;
return __libc_malloc(size);
}

tfs->hook_guard = true;

Expand All @@ -182,6 +190,8 @@ void operator delete(void *ptr)
auto* tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized)) {
auto size = addrmap.find(ptr) != addrmap.end() ? addrmap[ptr].size : 0;
mem_overhead -= size;
__libc_free(ptr);
return;
}
Expand All @@ -201,6 +211,8 @@ void operator delete[](void *ptr)
auto* tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized)) {
auto size = addrmap.find(ptr) != addrmap.end() ? addrmap[ptr].size : 0;
mem_overhead -= size;
__libc_free(ptr);
return;
}
Expand All @@ -219,8 +231,10 @@ void* malloc(size_t size)
{
auto* tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized))
if (unlikely(tfs->hook_guard || !initialized)) {
mem_overhead += size;
return __libc_malloc(size);
}

tfs->hook_guard = true;

Expand All @@ -239,6 +253,8 @@ void free(void *ptr)
auto* tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized)) {
auto size = addrmap.find(ptr) != addrmap.end() ? addrmap[ptr].size : 0;
mem_overhead -= size;
__libc_free(ptr);
return;
}
Expand All @@ -257,8 +273,10 @@ void *calloc(size_t nmemb, size_t size)
{
auto* tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized))
if (unlikely(tfs->hook_guard || !initialized)) {
mem_overhead += nmemb * size;
return __libc_calloc(nmemb, size);
}

tfs->hook_guard = true;

Expand All @@ -276,8 +294,11 @@ void *realloc(void *ptr, size_t size)
{
auto* tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized))
if (unlikely(tfs->hook_guard || !initialized)) {
auto sz = addrmap.find(ptr) != addrmap.end() ? addrmap[ptr].size : 0;
mem_overhead += (size - sz);
return __libc_realloc(ptr, size);
}

tfs->hook_guard = true;

Expand All @@ -296,8 +317,10 @@ void *memalign(size_t alignment, size_t size)
{
auto *tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized))
if (unlikely(tfs->hook_guard || !initialized)) {
mem_overhead += size;
return __libc_memalign(alignment, size);
}

tfs->hook_guard = true;

Expand All @@ -318,8 +341,10 @@ int posix_memalign(void **memptr, size_t alignment, size_t size)
if (unlikely(!real_posix_memalign))
real_posix_memalign = (PosixMemalignFunction)dlsym(RTLD_NEXT, "posix_memalign");

if (unlikely(tfs->hook_guard || !initialized))
if (unlikely(tfs->hook_guard || !initialized)) {
mem_overhead += size;
return real_posix_memalign(memptr, alignment, size);
}

tfs->hook_guard = true;

Expand All @@ -338,8 +363,10 @@ void *aligned_alloc(size_t alignment, size_t size)
{
auto *tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized))
if (unlikely(tfs->hook_guard || !initialized)) {
mem_overhead += size;
return __aligned_alloc(alignment, size);
}

tfs->hook_guard = true;

Expand All @@ -357,8 +384,10 @@ void *pvalloc(size_t size)
{
auto *tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized))
if (unlikely(tfs->hook_guard || !initialized)) {
mem_overhead += size;
return __pvalloc(size);
}

tfs->hook_guard = true;

Expand All @@ -376,8 +405,10 @@ void *valloc(size_t size)
{
auto *tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized))
if (unlikely(tfs->hook_guard || !initialized)) {
mem_overhead += size;
return __valloc(size);
}

tfs->hook_guard = true;

Expand All @@ -395,8 +426,11 @@ void *reallocarray(void *ptr, size_t nmemb, size_t size)
{
auto* tfs = &thread_flags;

if (unlikely(tfs->hook_guard || !initialized))
if (unlikely(tfs->hook_guard || !initialized)) {
auto sz = addrmap.find(ptr) != addrmap.end() ? addrmap[ptr].size : 0;
mem_overhead += (nmemb * size - sz);
return real_reallocarray(ptr, nmemb, size);
}

tfs->hook_guard = true;

Expand Down
4 changes: 3 additions & 1 deletion stacktrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <sstream>
#include <algorithm>
#include <vector>
#include <map>

#include <mutex>

Expand Down Expand Up @@ -275,6 +274,9 @@ static void print_dump_stackmap_footer(
get_byte_unit(minfo.uordblks).c_str());

pr_out("[heaptrace] statm info (VSS/RSS/shared) : %s\n", read_statm().c_str());

pr_out("[heaptrace] self consumed (mem overhead) : %s\n",
get_byte_unit(mem_overhead).c_str());
}

static void print_dump_stackmap(std::vector<std::pair<stack_trace_t, stack_info_t>>& sorted_stack)
Expand Down
3 changes: 3 additions & 0 deletions stacktrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <array>
#include <chrono>
#include <map>

#include "heaptrace.h"
#include "compiler.h"
Expand All @@ -32,6 +33,8 @@ struct object_info_t {
uint64_t size;
};

extern std::map<addr_t, object_info_t> addrmap;

void __record_backtrace(size_t size, void* addr,
stack_trace_t& stack_trace, int nptrs);

Expand Down