performance_switch.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright Oliver Kowalke 2009.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <stdexcept>
  8. #include <string>
  9. #include <boost/chrono.hpp>
  10. #include <boost/coroutine/all.hpp>
  11. #include <boost/cstdint.hpp>
  12. #include <boost/program_options.hpp>
  13. #include "../bind_processor.hpp"
  14. #include "../clock.hpp"
  15. #include "../cycle.hpp"
  16. boost::coroutines::flag_fpu_t preserve_fpu = boost::coroutines::fpu_not_preserved;
  17. boost::uint64_t jobs = 1000;
  18. time_point_type end;
  19. struct X
  20. {
  21. std::string str;
  22. X( std::string const& str_) :
  23. str( str_)
  24. {}
  25. };
  26. const X x("abc");
  27. void fn_void( boost::coroutines::symmetric_coroutine< void >::yield_type & yield)
  28. { while( true) yield(); }
  29. void fn_int( boost::coroutines::symmetric_coroutine< int >::yield_type & yield)
  30. { while( true) yield(); }
  31. void fn_x( boost::coroutines::symmetric_coroutine< X >::yield_type & yield)
  32. { while( true) yield(); }
  33. duration_type measure_time_void( duration_type overhead)
  34. {
  35. boost::coroutines::symmetric_coroutine< void >::call_type c( fn_void,
  36. boost::coroutines::attributes( preserve_fpu) );
  37. c();
  38. time_point_type start( clock_type::now() );
  39. for ( std::size_t i = 0; i < jobs; ++i) {
  40. c();
  41. }
  42. duration_type total = clock_type::now() - start;
  43. total -= overhead_clock(); // overhead of measurement
  44. total /= jobs; // loops
  45. total /= 2; // 2x jump_fcontext
  46. return total;
  47. }
  48. duration_type measure_time_int( duration_type overhead)
  49. {
  50. boost::coroutines::symmetric_coroutine< int >::call_type c( fn_int,
  51. boost::coroutines::attributes( preserve_fpu) );
  52. time_point_type start( clock_type::now() );
  53. for ( std::size_t i = 0; i < jobs; ++i) {
  54. c( i);
  55. }
  56. duration_type total = clock_type::now() - start;
  57. total -= overhead_clock(); // overhead of measurement
  58. total /= jobs; // loops
  59. total /= 2; // 2x jump_fcontext
  60. return total;
  61. }
  62. duration_type measure_time_x( duration_type overhead)
  63. {
  64. boost::coroutines::symmetric_coroutine< X >::call_type c( fn_x,
  65. boost::coroutines::attributes( preserve_fpu) );
  66. X x("abc");
  67. time_point_type start( clock_type::now() );
  68. for ( std::size_t i = 0; i < jobs; ++i) {
  69. c( x);
  70. }
  71. duration_type total = clock_type::now() - start;
  72. total -= overhead_clock(); // overhead of measurement
  73. total /= jobs; // loops
  74. total /= 2; // 2x jump_fcontext
  75. return total;
  76. }
  77. # ifdef BOOST_CONTEXT_CYCLE
  78. cycle_type measure_cycles_void( cycle_type overhead)
  79. {
  80. boost::coroutines::symmetric_coroutine< void >::call_type c( fn_void,
  81. boost::coroutines::attributes( preserve_fpu) );
  82. cycle_type start( cycles() );
  83. for ( std::size_t i = 0; i < jobs; ++i) {
  84. c();
  85. }
  86. cycle_type total = cycles() - start;
  87. total -= overhead; // overhead of measurement
  88. total /= jobs; // loops
  89. total /= 2; // 2x jump_fcontext
  90. return total;
  91. }
  92. cycle_type measure_cycles_int( cycle_type overhead)
  93. {
  94. boost::coroutines::symmetric_coroutine< int >::call_type c( fn_int,
  95. boost::coroutines::attributes( preserve_fpu) );
  96. cycle_type start( cycles() );
  97. for ( std::size_t i = 0; i < jobs; ++i) {
  98. c( i);
  99. }
  100. cycle_type total = cycles() - start;
  101. total -= overhead; // overhead of measurement
  102. total /= jobs; // loops
  103. total /= 2; // 2x jump_fcontext
  104. return total;
  105. }
  106. cycle_type measure_cycles_x( cycle_type overhead)
  107. {
  108. boost::coroutines::symmetric_coroutine< X >::call_type c( fn_x,
  109. boost::coroutines::attributes( preserve_fpu) );
  110. X x("abc");
  111. cycle_type start( cycles() );
  112. for ( std::size_t i = 0; i < jobs; ++i) {
  113. c( x);
  114. }
  115. cycle_type total = cycles() - start;
  116. total -= overhead; // overhead of measurement
  117. total /= jobs; // loops
  118. total /= 2; // 2x jump_fcontext
  119. return total;
  120. }
  121. # endif
  122. int main( int argc, char * argv[])
  123. {
  124. try
  125. {
  126. bool preserve = false, bind = false;
  127. boost::program_options::options_description desc("allowed options");
  128. desc.add_options()
  129. ("help", "help message")
  130. ("bind,b", boost::program_options::value< bool >( & bind), "bind thread to CPU")
  131. ("fpu,f", boost::program_options::value< bool >( & preserve), "preserve FPU registers")
  132. ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run");
  133. boost::program_options::variables_map vm;
  134. boost::program_options::store(
  135. boost::program_options::parse_command_line(
  136. argc,
  137. argv,
  138. desc),
  139. vm);
  140. boost::program_options::notify( vm);
  141. if ( vm.count("help") ) {
  142. std::cout << desc << std::endl;
  143. return EXIT_SUCCESS;
  144. }
  145. if ( preserve) preserve_fpu = boost::coroutines::fpu_preserved;
  146. if ( bind) bind_to_processor( 0);
  147. duration_type overhead_c = overhead_clock();
  148. std::cout << "overhead " << overhead_c.count() << " nano seconds" << std::endl;
  149. boost::uint64_t res = measure_time_void( overhead_c).count();
  150. std::cout << "void: average of " << res << " nano seconds" << std::endl;
  151. res = measure_time_int( overhead_c).count();
  152. std::cout << "int: average of " << res << " nano seconds" << std::endl;
  153. res = measure_time_x( overhead_c).count();
  154. std::cout << "X: average of " << res << " nano seconds" << std::endl;
  155. #ifdef BOOST_CONTEXT_CYCLE
  156. cycle_type overhead_y = overhead_cycle();
  157. std::cout << "overhead " << overhead_y << " cpu cycles" << std::endl;
  158. res = measure_cycles_void( overhead_y);
  159. std::cout << "void: average of " << res << " cpu cycles" << std::endl;
  160. res = measure_cycles_int( overhead_y);
  161. std::cout << "int: average of " << res << " cpu cycles" << std::endl;
  162. res = measure_cycles_x( overhead_y);
  163. std::cout << "X: average of " << res << " cpu cycles" << std::endl;
  164. #endif
  165. return EXIT_SUCCESS;
  166. }
  167. catch ( std::exception const& e)
  168. { std::cerr << "exception: " << e.what() << std::endl; }
  169. catch (...)
  170. { std::cerr << "unhandled exception" << std::endl; }
  171. return EXIT_FAILURE;
  172. }