[thread_event] Add support for user events in thread events when stats are enabled

This commit is contained in:
Slobodan Predolac 2025-03-28 07:35:53 -07:00
parent 387e9f0911
commit d49fc48ef1
15 changed files with 470 additions and 81 deletions

View file

@ -1348,6 +1348,43 @@ TEST_BEGIN(test_thread_activity_callback) {
}
TEST_END
static unsigned nuser_thread_event_cb_calls;
static void
user_thread_event_cb(bool is_alloc, uint64_t tallocated, uint64_t tdallocated) {
(void)tdallocated;
(void)tallocated;
++nuser_thread_event_cb_calls;
}
static user_hook_object_t user_te_obj = {
.callback = user_thread_event_cb,
.interval = 100,
.is_alloc_only = false,
};
TEST_BEGIN(test_thread_event_hook) {
const size_t big_size = 10 * 1024 * 1024;
void *ptr;
int err;
unsigned current_calls = nuser_thread_event_cb_calls;
err = mallctl("experimental.hooks.thread_event", NULL, 0,
&user_te_obj, sizeof(user_te_obj));
assert_d_eq(0, err, "");
err = mallctl("experimental.hooks.thread_event", NULL, 0,
&user_te_obj, sizeof(user_te_obj));
assert_d_eq(0, err, "Not an error to provide object with same interval and cb");
ptr = mallocx(big_size, 0);
free(ptr);
expect_u64_lt(current_calls, nuser_thread_event_cb_calls, "");
}
TEST_END
int
main(void) {
return test(
@ -1388,5 +1425,6 @@ main(void) {
test_hooks_exhaustion,
test_thread_idle,
test_thread_peak,
test_thread_activity_callback);
test_thread_activity_callback,
test_thread_event_hook);
}

View file

@ -1,5 +1,18 @@
#include "test/jemalloc_test.h"
static uint32_t nuser_hook_calls;
static bool is_registered = false;
static void
test_cb(bool is_alloc, uint64_t tallocated, uint64_t tdallocated) {
++nuser_hook_calls;
}
static user_hook_object_t tobj = {
.callback = &test_cb,
.interval = 10,
.is_alloc_only = false
};
TEST_BEGIN(test_next_event_fast) {
tsd_t *tsd = tsd_fetch();
te_ctx_t ctx;
@ -9,6 +22,12 @@ TEST_BEGIN(test_next_event_fast) {
te_ctx_current_bytes_set(&ctx, TE_NEXT_EVENT_FAST_MAX - 8U);
te_ctx_next_event_set(tsd, &ctx, TE_NEXT_EVENT_FAST_MAX);
if (!is_registered) {
is_registered = 0 == te_register_user_handler(tsd_tsdn(tsd), &tobj);
}
assert_true(is_registered || !config_stats, "Register user handler");
nuser_hook_calls = 0;
uint64_t *waits = tsd_te_datap_get_unsafe(tsd)->alloc_wait;
for (size_t i = 0; i < te_alloc_count; i++) {
waits[i] = TE_NEXT_EVENT_FAST_MAX;
@ -16,6 +35,7 @@ TEST_BEGIN(test_next_event_fast) {
/* Test next_event_fast rolling back to 0. */
void *p = malloc(16U);
assert_true(nuser_hook_calls == 1 || !config_stats, "Expected alloc call");
assert_ptr_not_null(p, "malloc() failed");
free(p);