performance_create_segmented.cpp 3.1 KB

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