Allow setting custom backtrace hook

Existing backtrace implementations skip native stack frames from runtimes like
Python. The hook allows to augment the backtraces to attribute allocations to
native functions in heap profiles.
This commit is contained in:
Alex Lapenkou
2021-08-30 14:05:56 -07:00
committed by Alexander Lapenkov
parent 523cfa55c5
commit f7d46b8119
11 changed files with 172 additions and 30 deletions

61
test/unit/prof_hook.c Normal file
View File

@@ -0,0 +1,61 @@
#include "test/jemalloc_test.h"
bool mock_bt_hook_called = false;
void
mock_bt_hook(void **vec, unsigned *len, unsigned max_len) {
*len = max_len;
for (unsigned i = 0; i < max_len; ++i) {
vec[i] = (void *)((uintptr_t)i);
}
mock_bt_hook_called = true;
}
TEST_BEGIN(test_prof_backtrace_hook) {
test_skip_if(!config_prof);
mock_bt_hook_called = false;
void *p0 = mallocx(1, 0);
assert_ptr_not_null(p0, "Failed to allocate");
expect_false(mock_bt_hook_called, "Called mock hook before it's set");
prof_backtrace_hook_t null_hook = NULL;
expect_d_eq(mallctl("experimental.hooks.prof_backtrace",
NULL, 0, (void *)&null_hook, sizeof(null_hook)),
EINVAL, "Incorrectly allowed NULL backtrace hook");
prof_backtrace_hook_t default_hook;
size_t default_hook_sz = sizeof(prof_backtrace_hook_t);
prof_backtrace_hook_t hook = &mock_bt_hook;
expect_d_eq(mallctl("experimental.hooks.prof_backtrace",
(void *)&default_hook, &default_hook_sz, (void *)&hook,
sizeof(hook)), 0, "Unexpected mallctl failure setting hook");
void *p1 = mallocx(1, 0);
assert_ptr_not_null(p1, "Failed to allocate");
expect_true(mock_bt_hook_called, "Didn't call mock hook");
prof_backtrace_hook_t current_hook;
size_t current_hook_sz = sizeof(prof_backtrace_hook_t);
expect_d_eq(mallctl("experimental.hooks.prof_backtrace",
(void *)&current_hook, &current_hook_sz, (void *)&default_hook,
sizeof(default_hook)), 0,
"Unexpected mallctl failure resetting hook to default");
expect_ptr_eq(current_hook, hook,
"Hook returned by mallctl is not equal to mock hook");
dallocx(p1, 0);
dallocx(p0, 0);
}
TEST_END
int
main(void) {
return test(
test_prof_backtrace_hook);
}

6
test/unit/prof_hook.sh Normal file
View File

@@ -0,0 +1,6 @@
#!/bin/sh
if [ "x${enable_prof}" = "x1" ] ; then
export MALLOC_CONF="prof:true,lg_prof_sample:0"
fi