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 typedef boost::coroutines::standard_stack_allocator        stack_allocator;
21 typedef boost::coroutines::symmetric_coroutine< void >     coro_type;
22 
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;
26 
fn(coro_type::yield_type &)27 void fn( coro_type::yield_type &) {}
28 
measure_time(duration_type overhead)29 duration_type measure_time( duration_type overhead)
30 {
31     stack_allocator stack_alloc;
32     boost::coroutines::attributes attrs( unwind_stack, preserve_fpu);
33 
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);
37     }
38     duration_type total = clock_type::now() - start;
39     total -= overhead; // overhead of measurement
40     total /= jobs;  // loops
41 
42     return total;
43 }
44 
45 # ifdef BOOST_CONTEXT_CYCLE
measure_cycles(cycle_type overhead)46 cycle_type measure_cycles( cycle_type overhead)
47 {
48     stack_allocator stack_alloc;
49 
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);
54     }
55     cycle_type total = cycles() - start;
56     total -= overhead; // overhead of measurement
57     total /= jobs;  // loops
58 
59     return total;
60 }
61 # endif
62 
main(int argc,char * argv[])63 int main( int argc, char * argv[])
64 {
65     try
66     {
67         bool preserve = false, unwind = true, bind = false;
68         boost::program_options::options_description desc("allowed options");
69         desc.add_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");
75 
76         boost::program_options::variables_map vm;
77         boost::program_options::store(
78                 boost::program_options::parse_command_line(
79                     argc,
80                     argv,
81                     desc),
82                 vm);
83         boost::program_options::notify( vm);
84 
85         if ( vm.count("help") ) {
86             std::cout << desc << std::endl;
87             return EXIT_SUCCESS;
88         }
89 
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);
93 
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;
103 #endif
104 
105         return EXIT_SUCCESS;
106     }
107     catch ( std::exception const& e)
108     { std::cerr << "exception: " << e.what() << std::endl; }
109     catch (...)
110     { std::cerr << "unhandled exception" << std::endl; }
111     return EXIT_FAILURE;
112 }
113