microbench.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "test/jemalloc_test.h"
  2. static inline void
  3. time_func(timedelta_t *timer, uint64_t nwarmup, uint64_t niter,
  4. void (*func)(void)) {
  5. uint64_t i;
  6. for (i = 0; i < nwarmup; i++) {
  7. func();
  8. }
  9. timer_start(timer);
  10. for (i = 0; i < niter; i++) {
  11. func();
  12. }
  13. timer_stop(timer);
  14. }
  15. void
  16. compare_funcs(uint64_t nwarmup, uint64_t niter, const char *name_a,
  17. void (*func_a), const char *name_b, void (*func_b)) {
  18. timedelta_t timer_a, timer_b;
  19. char ratio_buf[6];
  20. void *p;
  21. p = mallocx(1, 0);
  22. if (p == NULL) {
  23. test_fail("Unexpected mallocx() failure");
  24. return;
  25. }
  26. time_func(&timer_a, nwarmup, niter, func_a);
  27. time_func(&timer_b, nwarmup, niter, func_b);
  28. timer_ratio(&timer_a, &timer_b, ratio_buf, sizeof(ratio_buf));
  29. malloc_printf("%"FMTu64" iterations, %s=%"FMTu64"us, "
  30. "%s=%"FMTu64"us, ratio=1:%s\n",
  31. niter, name_a, timer_usec(&timer_a), name_b, timer_usec(&timer_b),
  32. ratio_buf);
  33. dallocx(p, 0);
  34. }
  35. static void
  36. malloc_free(void) {
  37. /* The compiler can optimize away free(malloc(1))! */
  38. void *p = malloc(1);
  39. if (p == NULL) {
  40. test_fail("Unexpected malloc() failure");
  41. return;
  42. }
  43. free(p);
  44. }
  45. static void
  46. mallocx_free(void) {
  47. void *p = mallocx(1, 0);
  48. if (p == NULL) {
  49. test_fail("Unexpected mallocx() failure");
  50. return;
  51. }
  52. free(p);
  53. }
  54. TEST_BEGIN(test_malloc_vs_mallocx) {
  55. compare_funcs(10*1000*1000, 100*1000*1000, "malloc",
  56. malloc_free, "mallocx", mallocx_free);
  57. }
  58. TEST_END
  59. static void
  60. malloc_dallocx(void) {
  61. void *p = malloc(1);
  62. if (p == NULL) {
  63. test_fail("Unexpected malloc() failure");
  64. return;
  65. }
  66. dallocx(p, 0);
  67. }
  68. static void
  69. malloc_sdallocx(void) {
  70. void *p = malloc(1);
  71. if (p == NULL) {
  72. test_fail("Unexpected malloc() failure");
  73. return;
  74. }
  75. sdallocx(p, 1, 0);
  76. }
  77. TEST_BEGIN(test_free_vs_dallocx) {
  78. compare_funcs(10*1000*1000, 100*1000*1000, "free", malloc_free,
  79. "dallocx", malloc_dallocx);
  80. }
  81. TEST_END
  82. TEST_BEGIN(test_dallocx_vs_sdallocx) {
  83. compare_funcs(10*1000*1000, 100*1000*1000, "dallocx", malloc_dallocx,
  84. "sdallocx", malloc_sdallocx);
  85. }
  86. TEST_END
  87. static void
  88. malloc_mus_free(void) {
  89. void *p;
  90. p = malloc(1);
  91. if (p == NULL) {
  92. test_fail("Unexpected malloc() failure");
  93. return;
  94. }
  95. malloc_usable_size(p);
  96. free(p);
  97. }
  98. static void
  99. malloc_sallocx_free(void) {
  100. void *p;
  101. p = malloc(1);
  102. if (p == NULL) {
  103. test_fail("Unexpected malloc() failure");
  104. return;
  105. }
  106. if (sallocx(p, 0) < 1) {
  107. test_fail("Unexpected sallocx() failure");
  108. }
  109. free(p);
  110. }
  111. TEST_BEGIN(test_mus_vs_sallocx) {
  112. compare_funcs(10*1000*1000, 100*1000*1000, "malloc_usable_size",
  113. malloc_mus_free, "sallocx", malloc_sallocx_free);
  114. }
  115. TEST_END
  116. static void
  117. malloc_nallocx_free(void) {
  118. void *p;
  119. p = malloc(1);
  120. if (p == NULL) {
  121. test_fail("Unexpected malloc() failure");
  122. return;
  123. }
  124. if (nallocx(1, 0) < 1) {
  125. test_fail("Unexpected nallocx() failure");
  126. }
  127. free(p);
  128. }
  129. TEST_BEGIN(test_sallocx_vs_nallocx) {
  130. compare_funcs(10*1000*1000, 100*1000*1000, "sallocx",
  131. malloc_sallocx_free, "nallocx", malloc_nallocx_free);
  132. }
  133. TEST_END
  134. int
  135. main(void) {
  136. return test_no_reentrancy(
  137. test_malloc_vs_mallocx,
  138. test_free_vs_dallocx,
  139. test_dallocx_vs_sdallocx,
  140. test_mus_vs_sallocx,
  141. test_sallocx_vs_nallocx);
  142. }