diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 22:40:55 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 22:40:55 +0100 |
| commit | 5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda (patch) | |
| tree | 1acdfa5220cd13b7be43a2a01368e80d306473ca /examples/redis-unstable/deps/jemalloc/test/stress/hookbench.c | |
| parent | c7ab12bba64d9c20ccd79b132dac475f7bc3923e (diff) | |
| download | crep-5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda.tar.gz | |
Add Redis source code for testing
Diffstat (limited to 'examples/redis-unstable/deps/jemalloc/test/stress/hookbench.c')
| -rw-r--r-- | examples/redis-unstable/deps/jemalloc/test/stress/hookbench.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/examples/redis-unstable/deps/jemalloc/test/stress/hookbench.c b/examples/redis-unstable/deps/jemalloc/test/stress/hookbench.c new file mode 100644 index 0000000..97e90b0 --- /dev/null +++ b/examples/redis-unstable/deps/jemalloc/test/stress/hookbench.c | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | #include "test/jemalloc_test.h" | ||
| 2 | |||
| 3 | static void | ||
| 4 | noop_alloc_hook(void *extra, hook_alloc_t type, void *result, | ||
| 5 | uintptr_t result_raw, uintptr_t args_raw[3]) { | ||
| 6 | } | ||
| 7 | |||
| 8 | static void | ||
| 9 | noop_dalloc_hook(void *extra, hook_dalloc_t type, void *address, | ||
| 10 | uintptr_t args_raw[3]) { | ||
| 11 | } | ||
| 12 | |||
| 13 | static void | ||
| 14 | noop_expand_hook(void *extra, hook_expand_t type, void *address, | ||
| 15 | size_t old_usize, size_t new_usize, uintptr_t result_raw, | ||
| 16 | uintptr_t args_raw[4]) { | ||
| 17 | } | ||
| 18 | |||
| 19 | static void | ||
| 20 | malloc_free_loop(int iters) { | ||
| 21 | for (int i = 0; i < iters; i++) { | ||
| 22 | void *p = mallocx(1, 0); | ||
| 23 | free(p); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | static void | ||
| 28 | test_hooked(int iters) { | ||
| 29 | hooks_t hooks = {&noop_alloc_hook, &noop_dalloc_hook, &noop_expand_hook, | ||
| 30 | NULL}; | ||
| 31 | |||
| 32 | int err; | ||
| 33 | void *handles[HOOK_MAX]; | ||
| 34 | size_t sz = sizeof(handles[0]); | ||
| 35 | |||
| 36 | for (int i = 0; i < HOOK_MAX; i++) { | ||
| 37 | err = mallctl("experimental.hooks.install", &handles[i], | ||
| 38 | &sz, &hooks, sizeof(hooks)); | ||
| 39 | assert(err == 0); | ||
| 40 | |||
| 41 | timedelta_t timer; | ||
| 42 | timer_start(&timer); | ||
| 43 | malloc_free_loop(iters); | ||
| 44 | timer_stop(&timer); | ||
| 45 | malloc_printf("With %d hook%s: %"FMTu64"us\n", i + 1, | ||
| 46 | i + 1 == 1 ? "" : "s", timer_usec(&timer)); | ||
| 47 | } | ||
| 48 | for (int i = 0; i < HOOK_MAX; i++) { | ||
| 49 | err = mallctl("experimental.hooks.remove", NULL, NULL, | ||
| 50 | &handles[i], sizeof(handles[i])); | ||
| 51 | assert(err == 0); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | static void | ||
| 56 | test_unhooked(int iters) { | ||
| 57 | timedelta_t timer; | ||
| 58 | timer_start(&timer); | ||
| 59 | malloc_free_loop(iters); | ||
| 60 | timer_stop(&timer); | ||
| 61 | |||
| 62 | malloc_printf("Without hooks: %"FMTu64"us\n", timer_usec(&timer)); | ||
| 63 | } | ||
| 64 | |||
| 65 | int | ||
| 66 | main(void) { | ||
| 67 | /* Initialize */ | ||
| 68 | free(mallocx(1, 0)); | ||
| 69 | int iters = 10 * 1000 * 1000; | ||
| 70 | malloc_printf("Benchmarking hooks with %d iterations:\n", iters); | ||
| 71 | test_hooked(iters); | ||
| 72 | test_unhooked(iters); | ||
| 73 | } | ||
