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)
12 #include <boost/context/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"
19 #include "../../example/simple_stack_allocator.hpp"
21 typedef boost::context::simple_stack_allocator
<
22 8 * 1024 * 1024, 64 * 1024, 8 * 1024
25 boost::uint64_t jobs
= 1000;
27 static void foo( boost::context::transfer_t t_
) {
28 boost::context::transfer_t t
= t_
;
30 t
= boost::context::jump_fcontext( t
.fctx
, 0);
34 duration_type
measure_time_fc() {
35 stack_allocator stack_alloc
;
36 boost::context::fcontext_t ctx
= boost::context::make_fcontext(
37 stack_alloc
.allocate( stack_allocator::default_stacksize() ),
38 stack_allocator::default_stacksize(),
42 boost::context::transfer_t t
= boost::context::jump_fcontext( ctx
, 0);
44 time_point_type
start( clock_type::now() );
45 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
46 t
= boost::context::jump_fcontext( t
.fctx
, 0);
48 duration_type total
= clock_type::now() - start
;
49 total
-= overhead_clock(); // overhead of measurement
50 total
/= jobs
; // loops
51 total
/= 2; // 2x jump_fcontext
56 #ifdef BOOST_CONTEXT_CYCLE
57 cycle_type
measure_cycles_fc() {
58 stack_allocator stack_alloc
;
59 boost::context::fcontext_t ctx
= boost::context::make_fcontext(
60 stack_alloc
.allocate( stack_allocator::default_stacksize() ),
61 stack_allocator::default_stacksize(),
65 boost::context::transfer_t t
= boost::context::jump_fcontext( ctx
, 0);
67 cycle_type
start( cycles() );
68 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
69 t
= boost::context::jump_fcontext( t
.fctx
, 0);
71 cycle_type total
= cycles() - start
;
72 total
-= overhead_cycle(); // overhead of measurement
73 total
/= jobs
; // loops
74 total
/= 2; // 2x jump_fcontext
80 int main( int argc
, char * argv
[])
84 bind_to_processor( 0);
86 boost::program_options::options_description
desc("allowed options");
88 ("help", "help message")
89 ("jobs,j", boost::program_options::value
< boost::uint64_t >( & jobs
), "jobs to run");
91 boost::program_options::variables_map vm
;
92 boost::program_options::store(
93 boost::program_options::parse_command_line(
98 boost::program_options::notify( vm
);
100 if ( vm
.count("help") ) {
101 std::cout
<< desc
<< std::endl
;
105 boost::uint64_t res
= measure_time_fc().count();
106 std::cout
<< "fcontext_t: average of " << res
<< " nano seconds" << std::endl
;
107 #ifdef BOOST_CONTEXT_CYCLE
108 res
= measure_cycles_fc();
109 std::cout
<< "fcontext_t: average of " << res
<< " cpu cycles" << std::endl
;
114 catch ( std::exception
const& e
)
115 { std::cerr
<< "exception: " << e
.what() << std::endl
; }
117 { std::cerr
<< "unhandled exception" << std::endl
; }