performance_create_standard.cpp 3.8 KB

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