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