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