performance_create_segmented.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <boost/chrono.hpp>
  9. #include <boost/coroutine/all.hpp>
  10. #include <boost/cstdint.hpp>
  11. #include <boost/program_options.hpp>
  12. #include "../../bind_processor.hpp"
  13. #include "../../clock.hpp"
  14. #include "../../cycle.hpp"
  15. boost::coroutines::flag_fpu_t preserve_fpu = boost::coroutines::fpu_not_preserved;
  16. boost::coroutines::flag_unwind_t unwind_stack = boost::coroutines::stack_unwind;
  17. boost::uint64_t jobs = 1000;
  18. void fn( boost::coroutines::symmetric_coroutine< void >::yield_type &) {}
  19. duration_type measure_time( duration_type overhead)
  20. {
  21. time_point_type start( clock_type::now() );
  22. for ( std::size_t i = 0; i < jobs; ++i) {
  23. boost::coroutines::symmetric_coroutine< void >::call_type c( fn,
  24. boost::coroutines::attributes( unwind_stack, preserve_fpu) );
  25. }
  26. duration_type total = clock_type::now() - start;
  27. total -= overhead; // overhead of measurement
  28. total /= jobs; // loops
  29. return total;
  30. }
  31. # ifdef BOOST_CONTEXT_CYCLE
  32. cycle_type measure_cycles( cycle_type overhead)
  33. {
  34. cycle_type start( cycles() );
  35. for ( std::size_t i = 0; i < jobs; ++i) {
  36. boost::coroutines::symmetric_coroutine< void >::call_type c( fn,
  37. boost::coroutines::attributes( unwind_stack, preserve_fpu) );
  38. }
  39. cycle_type total = cycles() - start;
  40. total -= overhead; // overhead of measurement
  41. total /= jobs; // loops
  42. return total;
  43. }
  44. # endif
  45. int main( int argc, char * argv[])
  46. {
  47. try
  48. {
  49. bool preserve = false, unwind = true, bind = false;
  50. boost::program_options::options_description desc("allowed options");
  51. desc.add_options()
  52. ("help", "help message")
  53. ("bind,b", boost::program_options::value< bool >( & bind), "bind thread to CPU")
  54. ("fpu,f", boost::program_options::value< bool >( & preserve), "preserve FPU registers")
  55. ("unwind,u", boost::program_options::value< bool >( & unwind), "unwind coroutine-stack")
  56. ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run");
  57. boost::program_options::variables_map vm;
  58. boost::program_options::store(
  59. boost::program_options::parse_command_line(
  60. argc,
  61. argv,
  62. desc),
  63. vm);
  64. boost::program_options::notify( vm);
  65. if ( vm.count("help") ) {
  66. std::cout << desc << std::endl;
  67. return EXIT_SUCCESS;
  68. }
  69. if ( preserve) preserve_fpu = boost::coroutines::fpu_preserved;
  70. if ( ! unwind) unwind_stack = boost::coroutines::no_stack_unwind;
  71. if ( bind) bind_to_processor( 0);
  72. duration_type overhead_c = overhead_clock();
  73. std::cout << "overhead " << overhead_c.count() << " nano seconds" << std::endl;
  74. boost::uint64_t res = measure_time( overhead_c).count();
  75. std::cout << "average of " << res << " nano seconds" << std::endl;
  76. #ifdef BOOST_CONTEXT_CYCLE
  77. cycle_type overhead_y = overhead_cycle();
  78. std::cout << "overhead " << overhead_y << " cpu cycles" << std::endl;
  79. res = measure_cycles( overhead_y);
  80. std::cout << "average of " << res << " cpu cycles" << std::endl;
  81. #endif
  82. return EXIT_SUCCESS;
  83. }
  84. catch ( std::exception const& e)
  85. { std::cerr << "exception: " << e.what() << std::endl; }
  86. catch (...)
  87. { std::cerr << "unhandled exception" << std::endl; }
  88. return EXIT_FAILURE;
  89. }