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/cstdint.hpp>
13 #include <boost/program_options.hpp>
17 #include "../bind_processor.hpp"
18 #include "../clock.hpp"
19 #include "../cycle.hpp"
20 #include "../../example/simple_stack_allocator.hpp"
22 typedef boost::context::simple_stack_allocator
<
23 8 * 1024 * 1024, 64 * 1024, 8 * 1024
26 boost::uint64_t jobs
= 1000;
30 { while ( true) ::swapcontext( & uc
, & ucm
); }
32 duration_type
measure_time()
35 ::swapcontext( & ucm
, & uc
);
37 time_point_type
start( clock_type::now() );
38 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
39 ::swapcontext( & ucm
, & uc
);
41 duration_type total
= clock_type::now() - start
;
42 total
-= overhead_clock(); // overhead of measurement
43 total
/= jobs
; // loops
44 total
/= 2; // 2x jump_fcontext
49 #ifdef BOOST_CONTEXT_CYCLE
50 cycle_type
measure_cycles()
53 ::swapcontext( & ucm
, & uc
);
55 cycle_type
start( cycles() );
56 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
57 ::swapcontext( & ucm
, & uc
);
59 cycle_type total
= cycles() - start
;
60 total
-= overhead_cycle(); // overhead of measurement
61 total
/= jobs
; // loops
62 total
/= 2; // 2x jump_fcontext
68 int main( int argc
, char * argv
[])
72 bind_to_processor( 0);
74 boost::program_options::options_description
desc("allowed options");
76 ("help", "help message")
77 ("jobs,j", boost::program_options::value
< boost::uint64_t >( & jobs
), "jobs to run");
79 boost::program_options::variables_map vm
;
80 boost::program_options::store(
81 boost::program_options::parse_command_line(
86 boost::program_options::notify( vm
);
88 if ( vm
.count("help") ) {
89 std::cout
<< desc
<< std::endl
;
93 stack_allocator stack_alloc
;
95 uc
.uc_stack
.ss_sp
= stack_alloc
.allocate( stack_allocator::default_stacksize() );
96 uc
.uc_stack
.ss_size
= stack_allocator::default_stacksize();
97 ::makecontext( & uc
, fn
, 7);
99 boost::uint64_t res
= measure_time().count();
100 std::cout
<< "average of " << res
<< " nano seconds" << std::endl
;
101 #ifdef BOOST_CONTEXT_CYCLE
102 res
= measure_cycles();
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
; }