1
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)
6
7 #include <cstdlib>
8 #include <iostream>
9 #include <stdexcept>
10
11 #include <boost/chrono.hpp>
12 #include <boost/coroutine/all.hpp>
13 #include <boost/cstdint.hpp>
14 #include <boost/program_options.hpp>
15
16 #include "../../bind_processor.hpp"
17 #include "../../clock.hpp"
18 #include "../../cycle.hpp"
19
20 boost::coroutines::flag_fpu_t preserve_fpu = boost::coroutines::fpu_not_preserved;
21 boost::coroutines::flag_unwind_t unwind_stack = boost::coroutines::stack_unwind;
22 boost::uint64_t jobs = 1000;
23
fn(boost::coroutines::asymmetric_coroutine<void>::push_type & c)24 void fn( boost::coroutines::asymmetric_coroutine< void >::push_type & c)
25 { while ( true) c(); }
26
measure_time(duration_type overhead)27 duration_type measure_time( duration_type overhead)
28 {
29 time_point_type start( clock_type::now() );
30 for ( std::size_t i = 0; i < jobs; ++i) {
31 boost::coroutines::asymmetric_coroutine< void >::pull_type c( fn,
32 boost::coroutines::attributes( unwind_stack, preserve_fpu) );
33 }
34 duration_type total = clock_type::now() - start;
35 total -= overhead_clock(); // overhead of measurement
36 total /= jobs; // loops
37
38 return total;
39 }
40
41 # ifdef BOOST_CONTEXT_CYCLE
measure_cycles(cycle_type overhead)42 cycle_type measure_cycles( cycle_type overhead)
43 {
44 cycle_type start( cycles() );
45 for ( std::size_t i = 0; i < jobs; ++i) {
46 boost::coroutines::asymmetric_coroutine< void >::pull_type c( fn,
47 boost::coroutines::attributes( unwind_stack, preserve_fpu) );
48 }
49 cycle_type total = cycles() - start;
50 total -= overhead; // overhead of measurement
51 total /= jobs; // loops
52
53 return total;
54 }
55 # endif
56
main(int argc,char * argv[])57 int main( int argc, char * argv[])
58 {
59 try
60 {
61 bool preserve = false, unwind = true, bind = false;
62 boost::program_options::options_description desc("allowed options");
63 desc.add_options()
64 ("help", "help message")
65 ("bind,b", boost::program_options::value< bool >( & bind), "bind thread to CPU")
66 ("fpu,f", boost::program_options::value< bool >( & preserve), "preserve FPU registers")
67 ("unwind,u", boost::program_options::value< bool >( & unwind), "unwind coroutine-stack")
68 ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run");
69
70 boost::program_options::variables_map vm;
71 boost::program_options::store(
72 boost::program_options::parse_command_line(
73 argc,
74 argv,
75 desc),
76 vm);
77 boost::program_options::notify( vm);
78
79 if ( vm.count("help") ) {
80 std::cout << desc << std::endl;
81 return EXIT_SUCCESS;
82 }
83
84 if ( preserve) preserve_fpu = boost::coroutines::fpu_preserved;
85 if ( ! unwind) unwind_stack = boost::coroutines::no_stack_unwind;
86 if ( bind) bind_to_processor( 0);
87
88 duration_type overhead_c = overhead_clock();
89 std::cout << "overhead " << overhead_c.count() << " nano seconds" << std::endl;
90 boost::uint64_t res = measure_time( overhead_c).count();
91 std::cout << "average of " << res << " nano seconds" << std::endl;
92 #ifdef BOOST_CONTEXT_CYCLE
93 cycle_type overhead_y = overhead_cycle();
94 std::cout << "overhead " << overhead_y << " cpu cycles" << std::endl;
95 res = measure_cycles( overhead_y);
96 std::cout << "average of " << res << " cpu cycles" << std::endl;
97 #endif
98
99 return EXIT_SUCCESS;
100 }
101 catch ( std::exception const& e)
102 { std::cerr << "exception: " << e.what() << std::endl; }
103 catch (...)
104 { std::cerr << "unhandled exception" << std::endl; }
105 return EXIT_FAILURE;
106 }
107