Skip to content

Commit

Permalink
Limited support for nativemem profiling on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Dec 30, 2024
1 parent ec83ae6 commit 75c71bf
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 23 deletions.
25 changes: 4 additions & 21 deletions src/mallocTracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "assert.h"
#include "codeCache.h"
#include "mallocTracer.h"
#include "os.h"
#include "profiler.h"
#include "tsc.h"
#include <dlfcn.h>
Expand Down Expand Up @@ -38,25 +37,23 @@ __attribute__((constructor)) static void getOrigAddresses() {
_orig_free = ADDRESS_OF(free);
}

extern "C" {

static void* malloc_hook(size_t size) {
extern "C" void* malloc_hook(size_t size) {
void* ret = _orig_malloc(size);
if (MallocTracer::running() && ret && size) {
MallocTracer::recordMalloc(ret, size);
}
return ret;
}

static void* calloc_hook(size_t num, size_t size) {
extern "C" void* calloc_hook(size_t num, size_t size) {
void* ret = _orig_calloc(num, size);
if (MallocTracer::running() && ret && num && size) {
MallocTracer::recordMalloc(ret, num * size);
}
return ret;
}

static void* realloc_hook(void* addr, size_t size) {
extern "C" void* realloc_hook(void* addr, size_t size) {
void* ret = _orig_realloc(addr, size);
if (MallocTracer::running() && ret) {
if (addr) {
Expand All @@ -69,15 +66,13 @@ static void* realloc_hook(void* addr, size_t size) {
return ret;
}

static void free_hook(void* addr) {
extern "C" void free_hook(void* addr) {
_orig_free(addr);
if (MallocTracer::running() && addr) {
MallocTracer::recordFree(addr);
}
}

} // extern "C"


u64 MallocTracer::_interval;
volatile u64 MallocTracer::_allocated_bytes;
Expand Down Expand Up @@ -137,19 +132,7 @@ void MallocTracer::recordFree(void* address) {
Profiler::instance()->recordEventOnly(MALLOC_SAMPLE, &event);
}

Error MallocTracer::check(Arguments& args) {
if (!OS::isLinux()) {
return Error("nativemem option is only supported on Linux");
}
return Error::OK;
}

Error MallocTracer::start(Arguments& args) {
Error error = check(args);
if (error) {
return error;
}

_interval = args._nativemem > 0 ? args._nativemem : 0;
_allocated_bytes = 0;

Expand Down
1 change: 0 additions & 1 deletion src/mallocTracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class MallocTracer : public Engine {
return "bytes";
}

Error check(Arguments& args);
Error start(Arguments& args);
void stop();

Expand Down
1 change: 0 additions & 1 deletion test/test/nativemem/NativememTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Map;

import one.jfr.JfrReader;
import one.jfr.StackTrace;
import one.jfr.event.MallocEvent;

import one.profiler.test.Assert;
Expand Down

0 comments on commit 75c71bf

Please sign in to comment.