performance_create_prealloc.cpp 3.8 KB

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