performance_switch.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. struct X
  19. {
  20. std::string str;
  21. X( std::string const& str_) :
  22. str( str_)
  23. {}
  24. };
  25. const X x("abc");
  26. void fn_void( boost::coroutines::asymmetric_coroutine< void >::push_type & c)
  27. { while ( true) c(); }
  28. void fn_int( boost::coroutines::asymmetric_coroutine< int >::push_type & c)
  29. { while ( true) c( 7); }
  30. void fn_x( boost::coroutines::asymmetric_coroutine< X >::push_type & c)
  31. {
  32. while ( true) c( x);
  33. }
  34. duration_type measure_time_void( duration_type overhead)
  35. {
  36. boost::coroutines::asymmetric_coroutine< void >::pull_type c( fn_void,
  37. boost::coroutines::attributes( preserve_fpu) );
  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::asymmetric_coroutine< int >::pull_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();
  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::asymmetric_coroutine< X >::pull_type c( fn_x,
  65. boost::coroutines::attributes( preserve_fpu) );
  66. time_point_type start( clock_type::now() );
  67. for ( std::size_t i = 0; i < jobs; ++i) {
  68. c();
  69. }
  70. duration_type total = clock_type::now() - start;
  71. total -= overhead_clock(); // overhead of measurement
  72. total /= jobs; // loops
  73. total /= 2; // 2x jump_fcontext
  74. return total;
  75. }
  76. # ifdef BOOST_CONTEXT_CYCLE
  77. cycle_type measure_cycles_void( cycle_type overhead)
  78. {
  79. boost::coroutines::asymmetric_coroutine< void >::pull_type c( fn_void,
  80. boost::coroutines::attributes( preserve_fpu) );
  81. cycle_type start( cycles() );
  82. for ( std::size_t i = 0; i < jobs; ++i) {
  83. c();
  84. }
  85. cycle_type total = cycles() - start;
  86. total -= overhead; // overhead of measurement
  87. total /= jobs; // loops
  88. total /= 2; // 2x jump_fcontext
  89. return total;
  90. }
  91. cycle_type measure_cycles_int( cycle_type overhead)
  92. {
  93. boost::coroutines::asymmetric_coroutine< int >::pull_type c( fn_int,
  94. boost::coroutines::attributes( preserve_fpu) );
  95. cycle_type start( cycles() );
  96. for ( std::size_t i = 0; i < jobs; ++i) {
  97. c();
  98. }
  99. cycle_type total = cycles() - start;
  100. total -= overhead; // overhead of measurement
  101. total /= jobs; // loops
  102. total /= 2; // 2x jump_fcontext
  103. return total;
  104. }
  105. cycle_type measure_cycles_x( cycle_type overhead)
  106. {
  107. boost::coroutines::asymmetric_coroutine< X >::pull_type c( fn_x,
  108. boost::coroutines::attributes( preserve_fpu) );
  109. cycle_type start( cycles() );
  110. for ( std::size_t i = 0; i < jobs; ++i) {
  111. c();
  112. }
  113. cycle_type total = cycles() - start;
  114. total -= overhead; // overhead of measurement
  115. total /= jobs; // loops
  116. total /= 2; // 2x jump_fcontext
  117. return total;
  118. }
  119. # endif
  120. int main( int argc, char * argv[])
  121. {
  122. try
  123. {
  124. bool preserve = false, bind = false;
  125. boost::program_options::options_description desc("allowed options");
  126. desc.add_options()
  127. ("help", "help message")
  128. ("bind,b", boost::program_options::value< bool >( & bind), "bind thread to CPU")
  129. ("fpu,f", boost::program_options::value< bool >( & preserve), "preserve FPU registers")
  130. ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run");
  131. boost::program_options::variables_map vm;
  132. boost::program_options::store(
  133. boost::program_options::parse_command_line(
  134. argc,
  135. argv,
  136. desc),
  137. vm);
  138. boost::program_options::notify( vm);
  139. if ( vm.count("help") ) {
  140. std::cout << desc << std::endl;
  141. return EXIT_SUCCESS;
  142. }
  143. if ( preserve) preserve_fpu = boost::coroutines::fpu_preserved;
  144. if ( bind) bind_to_processor( 0);
  145. duration_type overhead_c = overhead_clock();
  146. std::cout << "overhead " << overhead_c.count() << " nano seconds" << std::endl;
  147. boost::uint64_t res = measure_time_void( overhead_c).count();
  148. std::cout << "void: average of " << res << " nano seconds" << std::endl;
  149. res = measure_time_int( overhead_c).count();
  150. std::cout << "int: average of " << res << " nano seconds" << std::endl;
  151. res = measure_time_x( overhead_c).count();
  152. std::cout << "X: average of " << res << " nano seconds" << std::endl;
  153. #ifdef BOOST_CONTEXT_CYCLE
  154. cycle_type overhead_y = overhead_cycle();
  155. std::cout << "overhead " << overhead_y << " cpu cycles" << std::endl;
  156. res = measure_cycles_void( overhead_y);
  157. std::cout << "void: average of " << res << " cpu cycles" << std::endl;
  158. res = measure_cycles_int( overhead_y);
  159. std::cout << "int: average of " << res << " cpu cycles" << std::endl;
  160. res = measure_cycles_x( overhead_y);
  161. std::cout << "X: average of " << res << " cpu cycles" << std::endl;
  162. #endif
  163. return EXIT_SUCCESS;
  164. }
  165. catch ( std::exception const& e)
  166. { std::cerr << "exception: " << e.what() << std::endl; }
  167. catch (...)
  168. { std::cerr << "unhandled exception" << std::endl; }
  169. return EXIT_FAILURE;
  170. }