2
0

hookbench.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "test/jemalloc_test.h"
  2. static void
  3. noop_alloc_hook(void *extra, hook_alloc_t type, void *result,
  4. uintptr_t result_raw, uintptr_t args_raw[3]) {
  5. }
  6. static void
  7. noop_dalloc_hook(void *extra, hook_dalloc_t type, void *address,
  8. uintptr_t args_raw[3]) {
  9. }
  10. static void
  11. noop_expand_hook(void *extra, hook_expand_t type, void *address,
  12. size_t old_usize, size_t new_usize, uintptr_t result_raw,
  13. uintptr_t args_raw[4]) {
  14. }
  15. static void
  16. malloc_free_loop(int iters) {
  17. for (int i = 0; i < iters; i++) {
  18. void *p = mallocx(1, 0);
  19. free(p);
  20. }
  21. }
  22. static void
  23. test_hooked(int iters) {
  24. hooks_t hooks = {&noop_alloc_hook, &noop_dalloc_hook, &noop_expand_hook,
  25. NULL};
  26. int err;
  27. void *handles[HOOK_MAX];
  28. size_t sz = sizeof(handles[0]);
  29. for (int i = 0; i < HOOK_MAX; i++) {
  30. err = mallctl("experimental.hooks.install", &handles[i],
  31. &sz, &hooks, sizeof(hooks));
  32. assert(err == 0);
  33. timedelta_t timer;
  34. timer_start(&timer);
  35. malloc_free_loop(iters);
  36. timer_stop(&timer);
  37. malloc_printf("With %d hook%s: %"FMTu64"us\n", i + 1,
  38. i + 1 == 1 ? "" : "s", timer_usec(&timer));
  39. }
  40. for (int i = 0; i < HOOK_MAX; i++) {
  41. err = mallctl("experimental.hooks.remove", NULL, NULL,
  42. &handles[i], sizeof(handles[i]));
  43. assert(err == 0);
  44. }
  45. }
  46. static void
  47. test_unhooked(int iters) {
  48. timedelta_t timer;
  49. timer_start(&timer);
  50. malloc_free_loop(iters);
  51. timer_stop(&timer);
  52. malloc_printf("Without hooks: %"FMTu64"us\n", timer_usec(&timer));
  53. }
  54. int
  55. main(void) {
  56. /* Initialize */
  57. free(mallocx(1, 0));
  58. int iters = 10 * 1000 * 1000;
  59. malloc_printf("Benchmarking hooks with %d iterations:\n", iters);
  60. test_hooked(iters);
  61. test_unhooked(iters);
  62. }