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