2
0

microbench.c 3.1 KB

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