performance_create_standard.cpp 3.8 KB

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