bench_alloc.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifdef _MSC_VER
  11. #pragma warning (disable : 4512)
  12. #endif
  13. #include <boost/container/detail/dlmalloc.hpp>
  14. #define BOOST_INTERPROCESS_VECTOR_ALLOC_STATS
  15. #include <iostream> //std::cout, std::endl
  16. #include <typeinfo> //typeid
  17. #include <cassert> //assert
  18. #include <boost/timer/timer.hpp>
  19. using boost::timer::cpu_timer;
  20. using boost::timer::cpu_times;
  21. using boost::timer::nanosecond_type;
  22. using namespace boost::container;
  23. template <class POD>
  24. void allocation_timing_test(unsigned int num_iterations, unsigned int num_elements)
  25. {
  26. size_t capacity = 0;
  27. unsigned int numalloc = 0, numexpand = 0;
  28. std::cout
  29. << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"
  30. << " Iterations/Elements: " << num_iterations << "/" << num_elements << '\n'
  31. << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"
  32. << std::endl;
  33. allocation_type malloc_types[] = { BOOST_CONTAINER_EXPAND_BWD, BOOST_CONTAINER_EXPAND_FWD, BOOST_CONTAINER_ALLOCATE_NEW };
  34. const char * malloc_names[] = { "Backwards expansion", "Forward expansion", "New allocation" };
  35. for(size_t i = 0; i < sizeof(malloc_types)/sizeof(allocation_type); ++i){
  36. numalloc = 0; numexpand = 0;
  37. const allocation_type m_mode = malloc_types[i];
  38. const char *malloc_name = malloc_names[i];
  39. cpu_timer timer;
  40. timer.resume();
  41. for(unsigned int r = 0; r != num_iterations; ++r){
  42. void *first_mem = 0;
  43. if(m_mode != BOOST_CONTAINER_EXPAND_FWD)
  44. first_mem = dlmalloc_malloc(sizeof(POD)*num_elements*3/2);
  45. void *addr = dlmalloc_malloc(1*sizeof(POD));
  46. if(m_mode == BOOST_CONTAINER_EXPAND_FWD)
  47. first_mem = dlmalloc_malloc(sizeof(POD)*num_elements*3/2);
  48. capacity = dlmalloc_size(addr)/sizeof(POD);
  49. dlmalloc_free(first_mem);
  50. ++numalloc;
  51. try{
  52. dlmalloc_command_ret_t ret;
  53. for(size_t e = capacity + 1; e < num_elements; ++e){
  54. size_t received_size;
  55. size_t min = (capacity+1)*sizeof(POD);
  56. size_t max = (capacity*3/2)*sizeof(POD);
  57. if(min > max)
  58. max = min;
  59. ret = dlmalloc_allocation_command
  60. ( m_mode, sizeof(POD)
  61. , min, max, &received_size, addr);
  62. if(!ret.first){
  63. std::cout << "(!ret.first)!" << std::endl;
  64. throw int(0);
  65. }
  66. if(!ret.second){
  67. assert(m_mode == BOOST_CONTAINER_ALLOCATE_NEW);
  68. if(m_mode != BOOST_CONTAINER_ALLOCATE_NEW){
  69. std::cout << "m_mode != BOOST_CONTAINER_ALLOCATE_NEW!" << std::endl;
  70. return;
  71. }
  72. dlmalloc_free(addr);
  73. addr = ret.first;
  74. ++numalloc;
  75. }
  76. else{
  77. assert(m_mode != BOOST_CONTAINER_ALLOCATE_NEW);
  78. if(m_mode == BOOST_CONTAINER_ALLOCATE_NEW){
  79. std::cout << "m_mode == BOOST_CONTAINER_ALLOCATE_NEW!" << std::endl;
  80. return;
  81. }
  82. ++numexpand;
  83. }
  84. capacity = received_size/sizeof(POD);
  85. addr = ret.first;
  86. e = capacity + 1;
  87. }
  88. dlmalloc_free(addr);
  89. }
  90. catch(...){
  91. dlmalloc_free(addr);
  92. throw;
  93. }
  94. }
  95. assert( dlmalloc_allocated_memory() == 0);
  96. if(dlmalloc_allocated_memory()!= 0){
  97. std::cout << "Memory leak!" << std::endl;
  98. return;
  99. }
  100. timer.stop();
  101. nanosecond_type nseconds = timer.elapsed().wall;
  102. std::cout << " Malloc type: " << malloc_name
  103. << std::endl
  104. << " allocation ns: "
  105. << float(nseconds)/(num_iterations*num_elements)
  106. << std::endl
  107. << " capacity - alloc calls (new/expand): "
  108. << (unsigned int)capacity << " - "
  109. << (float(numalloc) + float(numexpand))/num_iterations
  110. << "(" << float(numalloc)/num_iterations << "/" << float(numexpand)/num_iterations << ")"
  111. << std::endl << std::endl;
  112. dlmalloc_trim(0);
  113. }
  114. }
  115. template<unsigned N>
  116. struct char_holder
  117. {
  118. char ints_[N];
  119. };
  120. template<class POD>
  121. int allocation_loop()
  122. {
  123. std::cout << std::endl
  124. << "-------------------------------------------\n"
  125. << "-------------------------------------------\n"
  126. << " Type(sizeof): " << typeid(POD).name() << " (" << sizeof(POD) << ")\n"
  127. << "-------------------------------------------\n"
  128. << "-------------------------------------------\n"
  129. << std::endl;
  130. //#define SINGLE_TEST
  131. #define SIMPLE_IT
  132. #ifdef SINGLE_TEST
  133. #ifdef NDEBUG
  134. unsigned int numrep [] = { 50000 };
  135. #else
  136. unsigned int numrep [] = { 5000 };
  137. #endif
  138. unsigned int numele [] = { 100 };
  139. #elif defined(SIMPLE_IT)
  140. unsigned int numrep [] = { 3 };
  141. unsigned int numele [] = { 100 };
  142. #else
  143. #ifdef NDEBUG
  144. unsigned int numrep [] = { /*10000, */10000, 100000, 1000000 };
  145. #else
  146. unsigned int numrep [] = { /*10000, */1000, 10000, 100000 };
  147. #endif
  148. unsigned int numele [] = { /*10000, */1000, 100, 10 };
  149. #endif
  150. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  151. allocation_timing_test<POD>(numrep[i], numele[i]);
  152. }
  153. return 0;
  154. }
  155. int main()
  156. {
  157. dlmalloc_mallopt( (-3)//M_MMAP_THRESHOLD
  158. , 100*10000000);
  159. //allocation_loop<char_holder<4> >();
  160. //allocation_loop<char_holder<6> >();
  161. allocation_loop<char_holder<8> >();
  162. allocation_loop<char_holder<12> >();
  163. //allocation_loop<char_holder<14> >();
  164. allocation_loop<char_holder<24> >();
  165. return 0;
  166. }