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::protected_stack_allocator stack_allocator
;
21 typedef boost::coroutines::symmetric_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::yield_type
&) {}
29 duration_type
measure_time( duration_type overhead
)
31 stack_allocator stack_alloc
;
32 boost::coroutines::attributes
attrs( unwind_stack
, preserve_fpu
);
34 time_point_type
start( clock_type::now() );
35 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
36 coro_type::call_type
c( fn
, attrs
, stack_alloc
);
38 duration_type total
= clock_type::now() - start
;
39 total
-= overhead
; // overhead of measurement
40 total
/= jobs
; // loops
45 # ifdef BOOST_CONTEXT_CYCLE
46 cycle_type
measure_cycles( cycle_type overhead
)
48 stack_allocator stack_alloc
;
50 cycle_type
start( cycles() );
51 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
52 coro_type::call_type
c( fn
,
53 boost::coroutines::attributes( unwind_stack
, preserve_fpu
), stack_alloc
);
55 cycle_type total
= cycles() - start
;
56 total
-= overhead
; // overhead of measurement
57 total
/= jobs
; // loops
63 int main( int argc
, char * argv
[])
67 bool preserve
= false, unwind
= true, bind
= false;
68 boost::program_options::options_description
desc("allowed options");
70 ("help", "help message")
71 ("bind,b", boost::program_options::value
< bool >( & bind
), "bind thread to CPU")
72 ("fpu,f", boost::program_options::value
< bool >( & preserve
), "preserve FPU registers")
73 ("unwind,u", boost::program_options::value
< bool >( & unwind
), "unwind coroutine-stack")
74 ("jobs,j", boost::program_options::value
< boost::uint64_t >( & jobs
), "jobs to run");
76 boost::program_options::variables_map vm
;
77 boost::program_options::store(
78 boost::program_options::parse_command_line(
83 boost::program_options::notify( vm
);
85 if ( vm
.count("help") ) {
86 std::cout
<< desc
<< std::endl
;
90 if ( preserve
) preserve_fpu
= boost::coroutines::fpu_preserved
;
91 if ( ! unwind
) unwind_stack
= boost::coroutines::no_stack_unwind
;
92 if ( bind
) bind_to_processor( 0);
94 duration_type overhead_c
= overhead_clock();
95 std::cout
<< "overhead " << overhead_c
.count() << " nano seconds" << std::endl
;
96 boost::uint64_t res
= measure_time( overhead_c
).count();
97 std::cout
<< "average of " << res
<< " nano seconds" << std::endl
;
98 #ifdef BOOST_CONTEXT_CYCLE
99 cycle_type overhead_y
= overhead_cycle();
100 std::cout
<< "overhead " << overhead_y
<< " cpu cycles" << std::endl
;
101 res
= measure_cycles( overhead_y
);
102 std::cout
<< "average of " << res
<< " cpu cycles" << std::endl
;
107 catch ( std::exception
const& e
)
108 { std::cerr
<< "exception: " << e
.what() << std::endl
; }
110 { std::cerr
<< "unhandled exception" << std::endl
; }