diff options
| author | Qi Wang <interwq@gwu.edu> | 2019-04-03 00:50:42 (GMT) |
|---|---|---|
| committer | Qi Wang <interwq@gwu.edu> | 2019-04-03 00:50:42 (GMT) |
| commit | b0b3e49a54ec29e32636f4577d9d5a896d67fd20 (patch) | |
| tree | e80fd5feaedd401e7e2c884e73f8c884f51b5a65 /test/stress/hookbench.c | |
| parent | 61efbda7098de6fe64c362d309824864308c36d4 (diff) | |
| parent | f7489dc8f1fac233b0cd4e40331de8b738b1f2e2 (diff) | |
| download | jemalloc-5.2.0.zip jemalloc-5.2.0.tar.gz jemalloc-5.2.0.tar.bz2 | |
Merge branch 'dev'5.2.0
Diffstat (limited to 'test/stress/hookbench.c')
| -rw-r--r-- | test/stress/hookbench.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/test/stress/hookbench.c b/test/stress/hookbench.c new file mode 100644 index 0000000..97e90b0 --- /dev/null +++ b/test/stress/hookbench.c @@ -0,0 +1,73 @@ +#include "test/jemalloc_test.h" + +static void +noop_alloc_hook(void *extra, hook_alloc_t type, void *result, + uintptr_t result_raw, uintptr_t args_raw[3]) { +} + +static void +noop_dalloc_hook(void *extra, hook_dalloc_t type, void *address, + uintptr_t args_raw[3]) { +} + +static void +noop_expand_hook(void *extra, hook_expand_t type, void *address, + size_t old_usize, size_t new_usize, uintptr_t result_raw, + uintptr_t args_raw[4]) { +} + +static void +malloc_free_loop(int iters) { + for (int i = 0; i < iters; i++) { + void *p = mallocx(1, 0); + free(p); + } +} + +static void +test_hooked(int iters) { + hooks_t hooks = {&noop_alloc_hook, &noop_dalloc_hook, &noop_expand_hook, + NULL}; + + int err; + void *handles[HOOK_MAX]; + size_t sz = sizeof(handles[0]); + + for (int i = 0; i < HOOK_MAX; i++) { + err = mallctl("experimental.hooks.install", &handles[i], + &sz, &hooks, sizeof(hooks)); + assert(err == 0); + + timedelta_t timer; + timer_start(&timer); + malloc_free_loop(iters); + timer_stop(&timer); + malloc_printf("With %d hook%s: %"FMTu64"us\n", i + 1, + i + 1 == 1 ? "" : "s", timer_usec(&timer)); + } + for (int i = 0; i < HOOK_MAX; i++) { + err = mallctl("experimental.hooks.remove", NULL, NULL, + &handles[i], sizeof(handles[i])); + assert(err == 0); + } +} + +static void +test_unhooked(int iters) { + timedelta_t timer; + timer_start(&timer); + malloc_free_loop(iters); + timer_stop(&timer); + + malloc_printf("Without hooks: %"FMTu64"us\n", timer_usec(&timer)); +} + +int +main(void) { + /* Initialize */ + free(mallocx(1, 0)); + int iters = 10 * 1000 * 1000; + malloc_printf("Benchmarking hooks with %d iterations:\n", iters); + test_hooked(iters); + test_unhooked(iters); +} |
