2 // Copyright Oliver Kowalke 2009.
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/coroutine/all.hpp>
13 #include <boost/cstdint.hpp>
14 #include <boost/program_options.hpp>
16 #include "../bind_processor.hpp"
17 #include "../clock.hpp"
18 #include "../cycle.hpp"
20 typedef boost::coroutines::standard_stack_allocator stack_allocator
;
21 typedef boost::coroutines::asymmetric_coroutine
< void > coro_type
;
23 boost::coroutines::flag_fpu_t preserve_fpu
= boost::coroutines::fpu_not_preserved
;
24 boost::coroutines::flag_unwind_t unwind_stack
= boost::coroutines::stack_unwind
;
25 boost::uint64_t jobs
= 1000;
27 void fn( coro_type::push_type
& c
)
28 { while ( true) c(); }
30 duration_type
measure_time( duration_type overhead
)
32 stack_allocator stack_alloc
;
34 time_point_type
start( clock_type::now() );
35 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
36 coro_type::pull_type
c( fn
,
37 boost::coroutines::attributes( unwind_stack
, preserve_fpu
), stack_alloc
);
39 duration_type total
= clock_type::now() - start
;
40 total
-= overhead_clock(); // overhead of measurement
41 total
/= jobs
; // loops
46 # ifdef BOOST_CONTEXT_CYCLE
47 cycle_type
measure_cycles( cycle_type overhead
)
49 stack_allocator stack_alloc
;
51 cycle_type
start( cycles() );
52 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
53 coro_type::pull_type
c( fn
,
54 boost::coroutines::attributes( unwind_stack
, preserve_fpu
), stack_alloc
);
56 cycle_type total
= cycles() - start
;
57 total
-= overhead
; // overhead of measurement
58 total
/= jobs
; // loops
64 int main( int argc
, char * argv
[])
68 bool preserve
= false, unwind
= true, bind
= false;
69 boost::program_options::options_description
desc("allowed options");
71 ("help", "help message")
72 ("bind,b", boost::program_options::value
< bool >( & bind
), "bind thread to CPU")
73 ("fpu,f", boost::program_options::value
< bool >( & preserve
), "preserve FPU registers")
74 ("unwind,u", boost::program_options::value
< bool >( & unwind
), "unwind coroutine-stack")
75 ("jobs,j", boost::program_options::value
< boost::uint64_t >( & jobs
), "jobs to run");
77 boost::program_options::variables_map vm
;
78 boost::program_options::store(
79 boost::program_options::parse_command_line(
84 boost::program_options::notify( vm
);
86 if ( vm
.count("help") ) {
87 std::cout
<< desc
<< std::endl
;
91 if ( preserve
) preserve_fpu
= boost::coroutines::fpu_preserved
;
92 if ( ! unwind
) unwind_stack
= boost::coroutines::no_stack_unwind
;
93 if ( bind
) bind_to_processor( 0);
95 duration_type overhead_c
= overhead_clock();
96 std::cout
<< "overhead " << overhead_c
.count() << " nano seconds" << std::endl
;
97 boost::uint64_t res
= measure_time( overhead_c
).count();
98 std::cout
<< "average of " << res
<< " nano seconds" << std::endl
;
99 #ifdef BOOST_CONTEXT_CYCLE
100 cycle_type overhead_y
= overhead_cycle();
101 std::cout
<< "overhead " << overhead_y
<< " cpu cycles" << std::endl
;
102 res
= measure_cycles( overhead_y
);
103 std::cout
<< "average of " << res
<< " cpu cycles" << std::endl
;
108 catch ( std::exception
const& e
)
109 { std::cerr
<< "exception: " << e
.what() << std::endl
; }
111 { std::cerr
<< "unhandled exception" << std::endl
; }