malloc_bench.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following disclaimer
  10. // in the documentation and/or other materials provided with the
  11. // distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived from
  14. // this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <stdint.h>
  30. #include "run_benchmark.h"
  31. static void bench_fastpath_throughput(long iterations,
  32. uintptr_t param)
  33. {
  34. size_t sz = 32;
  35. for (; iterations>0; iterations--) {
  36. void *p = malloc(sz);
  37. if (!p) {
  38. abort();
  39. }
  40. free(p);
  41. // this makes next iteration use different free list. So
  42. // subsequent iterations may actually overlap in time.
  43. sz = (sz & 511) + 16;
  44. }
  45. }
  46. static void bench_fastpath_dependent(long iterations,
  47. uintptr_t param)
  48. {
  49. size_t sz = 32;
  50. for (; iterations>0; iterations--) {
  51. void *p = malloc(sz);
  52. if (!p) {
  53. abort();
  54. }
  55. free(p);
  56. // this makes next iteration depend on current iteration. But this
  57. // iteration's free may still overlap with next iteration's malloc
  58. sz = ((sz | reinterpret_cast<size_t>(p)) & 511) + 16;
  59. }
  60. }
  61. static void bench_fastpath_simple(long iterations,
  62. uintptr_t param)
  63. {
  64. size_t sz = 64;
  65. for (; iterations>0; iterations--) {
  66. void *p = malloc(sz);
  67. if (!p) {
  68. abort();
  69. }
  70. free(p);
  71. // next iteration will use same free list as this iteration. So it
  72. // should be prevent next iterations malloc to go too far before
  73. // free done. But using same size will make free "too fast" since
  74. // we'll hit size class cache.
  75. }
  76. }
  77. #define STACKSZ (1 << 16)
  78. static void bench_fastpath_stack(long iterations,
  79. uintptr_t _param)
  80. {
  81. void *stack[STACKSZ];
  82. size_t sz = 64;
  83. long param = static_cast<long>(_param);
  84. param &= STACKSZ - 1;
  85. param = param ? param : 1;
  86. for (; iterations>0; iterations -= param) {
  87. for (long k = param-1; k >= 0; k--) {
  88. void *p = malloc(sz);
  89. if (!p) {
  90. abort();
  91. }
  92. stack[k] = p;
  93. // this makes next iteration depend on result of this iteration
  94. sz = ((sz | reinterpret_cast<size_t>(p)) & 511) + 16;
  95. }
  96. for (long k = 0; k < param; k++) {
  97. free(stack[k]);
  98. }
  99. }
  100. }
  101. static void bench_fastpath_stack_simple(long iterations,
  102. uintptr_t _param)
  103. {
  104. void *stack[STACKSZ];
  105. size_t sz = 128;
  106. long param = static_cast<long>(_param);
  107. param &= STACKSZ - 1;
  108. param = param ? param : 1;
  109. for (; iterations>0; iterations -= param) {
  110. for (long k = param-1; k >= 0; k--) {
  111. void *p = malloc(sz);
  112. if (!p) {
  113. abort();
  114. }
  115. stack[k] = p;
  116. }
  117. for (long k = 0; k < param; k++) {
  118. free(stack[k]);
  119. }
  120. }
  121. }
  122. static void bench_fastpath_rnd_dependent(long iterations,
  123. uintptr_t _param)
  124. {
  125. static const uintptr_t rnd_c = 1013904223;
  126. static const uintptr_t rnd_a = 1664525;
  127. void *ptrs[STACKSZ];
  128. size_t sz = 128;
  129. if ((_param & (_param - 1))) {
  130. abort();
  131. }
  132. if (_param > STACKSZ) {
  133. abort();
  134. }
  135. int param = static_cast<int>(_param);
  136. for (; iterations>0; iterations -= param) {
  137. for (int k = param-1; k >= 0; k--) {
  138. void *p = malloc(sz);
  139. if (!p) {
  140. abort();
  141. }
  142. ptrs[k] = p;
  143. sz = ((sz | reinterpret_cast<size_t>(p)) & 511) + 16;
  144. }
  145. // this will iterate through all objects in order that is
  146. // unpredictable to processor's prefetchers
  147. uint32_t rnd = 0;
  148. uint32_t free_idx = 0;
  149. do {
  150. free(ptrs[free_idx]);
  151. rnd = rnd * rnd_a + rnd_c;
  152. free_idx = rnd & (param - 1);
  153. } while (free_idx != 0);
  154. }
  155. }
  156. int main(void)
  157. {
  158. report_benchmark("bench_fastpath_throughput", bench_fastpath_throughput, 0);
  159. report_benchmark("bench_fastpath_dependent", bench_fastpath_dependent, 0);
  160. report_benchmark("bench_fastpath_simple", bench_fastpath_simple, 0);
  161. for (int i = 8; i <= 512; i <<= 1) {
  162. report_benchmark("bench_fastpath_stack", bench_fastpath_stack, i);
  163. }
  164. report_benchmark("bench_fastpath_stack_simple", bench_fastpath_stack_simple, 32);
  165. report_benchmark("bench_fastpath_stack_simple", bench_fastpath_stack_simple, 8192);
  166. report_benchmark("bench_fastpath_rnd_dependent", bench_fastpath_rnd_dependent, 32);
  167. report_benchmark("bench_fastpath_rnd_dependent", bench_fastpath_rnd_dependent, 8192);
  168. return 0;
  169. }