2 // Copyright Oliver Kowalke 2014.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
11 #include <boost/chrono.hpp>
12 #include <boost/coroutine2/all.hpp>
13 #include <boost/cstdint.hpp>
14 #include <boost/program_options.hpp>
16 #include "bind_processor.hpp"
20 typedef boost::coroutines2::fixedsize_stack stack_allocator
;
21 typedef boost::coroutines2::coroutine
< void > coro_type
;
23 boost::uint64_t jobs
= 1000;
25 void fn( coro_type::pull_type
& c
)
26 { while ( true) c(); }
28 duration_type
measure_time( duration_type overhead
)
30 stack_allocator stack_alloc
;
32 time_point_type
start( clock_type::now() );
33 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
34 coro_type::push_type
c( stack_alloc
, fn
);
36 duration_type total
= clock_type::now() - start
;
37 total
-= overhead_clock(); // overhead of measurement
38 total
/= jobs
; // loops
43 # ifdef BOOST_CONTEXT_CYCLE
44 cycle_type
measure_cycles( cycle_type overhead
)
46 stack_allocator stack_alloc
;
48 cycle_type
start( cycles() );
49 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
50 coro_type::push_type
c( stack_alloc
, fn
);
52 cycle_type total
= cycles() - start
;
53 total
-= overhead
; // overhead of measurement
54 total
/= jobs
; // loops
60 int main( int argc
, char * argv
[])
65 boost::program_options::options_description
desc("allowed options");
67 ("help", "help message")
68 ("bind,b", boost::program_options::value
< bool >( & bind
), "bind thread to CPU")
69 ("jobs,j", boost::program_options::value
< boost::uint64_t >( & jobs
), "jobs to run");
71 boost::program_options::variables_map vm
;
72 boost::program_options::store(
73 boost::program_options::parse_command_line(
78 boost::program_options::notify( vm
);
80 if ( vm
.count("help") ) {
81 std::cout
<< desc
<< std::endl
;
85 if ( bind
) bind_to_processor( 0);
87 duration_type overhead_c
= overhead_clock();
88 boost::uint64_t res
= measure_time( overhead_c
).count();
89 std::cout
<< "average of " << res
<< " nano seconds" << std::endl
;
90 #ifdef BOOST_CONTEXT_CYCLE
91 cycle_type overhead_y
= overhead_cycle();
92 res
= measure_cycles( overhead_y
);
93 std::cout
<< "average of " << res
<< " cpu cycles" << std::endl
;
98 catch ( std::exception
const& e
)
99 { std::cerr
<< "exception: " << e
.what() << std::endl
; }
101 { std::cerr
<< "unhandled exception" << std::endl
; }