Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / coroutine / performance / asymmetric / performance_switch.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 #include <string>
11
12 #include <boost/chrono.hpp>
13 #include <boost/coroutine/all.hpp>
14 #include <boost/cstdint.hpp>
15 #include <boost/program_options.hpp>
16
17 #include "../bind_processor.hpp"
18 #include "../clock.hpp"
19 #include "../cycle.hpp"
20
21 boost::coroutines::flag_fpu_t preserve_fpu = boost::coroutines::fpu_not_preserved;
22 boost::uint64_t jobs = 1000;
23
24 struct X
25 {
26     std::string str;
27
28     X( std::string const& str_) :
29         str( str_)
30     {}
31 };
32
33 const X x("abc");
34
35 void fn_void( boost::coroutines::asymmetric_coroutine< void >::push_type & c)
36 { while ( true) c(); }
37
38 void fn_int( boost::coroutines::asymmetric_coroutine< int >::push_type & c)
39 { while ( true) c( 7); }
40
41 void fn_x( boost::coroutines::asymmetric_coroutine< X >::push_type & c)
42 {
43     while ( true) c( x);
44 }
45
46 duration_type measure_time_void( duration_type overhead)
47 {
48     boost::coroutines::asymmetric_coroutine< void >::pull_type c( fn_void,
49             boost::coroutines::attributes( preserve_fpu) );
50         
51     time_point_type start( clock_type::now() );
52     for ( std::size_t i = 0; i < jobs; ++i) {
53         c();
54     }
55     duration_type total = clock_type::now() - start;
56     total -= overhead_clock(); // overhead of measurement
57     total /= jobs;  // loops
58     total /= 2;  // 2x jump_fcontext
59
60     return total;
61 }
62
63 duration_type measure_time_int( duration_type overhead)
64 {
65     boost::coroutines::asymmetric_coroutine< int >::pull_type c( fn_int,
66             boost::coroutines::attributes( preserve_fpu) );
67         
68     time_point_type start( clock_type::now() );
69     for ( std::size_t i = 0; i < jobs; ++i) {
70         c();
71     }
72     duration_type total = clock_type::now() - start;
73     total -= overhead_clock(); // overhead of measurement
74     total /= jobs;  // loops
75     total /= 2;  // 2x jump_fcontext
76
77     return total;
78 }
79
80 duration_type measure_time_x( duration_type overhead)
81 {
82     boost::coroutines::asymmetric_coroutine< X >::pull_type c( fn_x,
83             boost::coroutines::attributes( preserve_fpu) );
84         
85     time_point_type start( clock_type::now() );
86     for ( std::size_t i = 0; i < jobs; ++i) {
87         c();
88     }
89     duration_type total = clock_type::now() - start;
90     total -= overhead_clock(); // overhead of measurement
91     total /= jobs;  // loops
92     total /= 2;  // 2x jump_fcontext
93
94     return total;
95 }
96
97 # ifdef BOOST_CONTEXT_CYCLE
98 cycle_type measure_cycles_void( cycle_type overhead)
99 {
100     boost::coroutines::asymmetric_coroutine< void >::pull_type c( fn_void,
101             boost::coroutines::attributes( preserve_fpu) );
102         
103     cycle_type start( cycles() );
104     for ( std::size_t i = 0; i < jobs; ++i) {
105         c();
106     }
107     cycle_type total = cycles() - start;
108     total -= overhead; // overhead of measurement
109     total /= jobs;  // loops
110     total /= 2;  // 2x jump_fcontext
111
112     return total;
113 }
114
115 cycle_type measure_cycles_int( cycle_type overhead)
116 {
117     boost::coroutines::asymmetric_coroutine< int >::pull_type c( fn_int,
118             boost::coroutines::attributes( preserve_fpu) );
119         
120     cycle_type start( cycles() );
121     for ( std::size_t i = 0; i < jobs; ++i) {
122         c();
123     }
124     cycle_type total = cycles() - start;
125     total -= overhead; // overhead of measurement
126     total /= jobs;  // loops
127     total /= 2;  // 2x jump_fcontext
128
129     return total;
130 }
131
132 cycle_type measure_cycles_x( cycle_type overhead)
133 {
134     boost::coroutines::asymmetric_coroutine< X >::pull_type c( fn_x,
135             boost::coroutines::attributes( preserve_fpu) );
136         
137     cycle_type start( cycles() );
138     for ( std::size_t i = 0; i < jobs; ++i) {
139         c();
140     }
141     cycle_type total = cycles() - start;
142     total -= overhead; // overhead of measurement
143     total /= jobs;  // loops
144     total /= 2;  // 2x jump_fcontext
145
146     return total;
147 }
148 # endif
149
150 int main( int argc, char * argv[])
151 {
152     try
153     {
154         bool preserve = false, bind = false;
155         boost::program_options::options_description desc("allowed options");
156         desc.add_options()
157             ("help", "help message")
158             ("bind,b", boost::program_options::value< bool >( & bind), "bind thread to CPU")
159             ("fpu,f", boost::program_options::value< bool >( & preserve), "preserve FPU registers")
160             ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run");
161
162         boost::program_options::variables_map vm;
163         boost::program_options::store(
164                 boost::program_options::parse_command_line(
165                     argc,
166                     argv,
167                     desc),
168                 vm);
169         boost::program_options::notify( vm);
170
171         if ( vm.count("help") ) {
172             std::cout << desc << std::endl;
173             return EXIT_SUCCESS;
174         }
175
176         if ( preserve) preserve_fpu = boost::coroutines::fpu_preserved;
177         if ( bind) bind_to_processor( 0);
178
179         duration_type overhead_c = overhead_clock();
180         std::cout << "overhead " << overhead_c.count() << " nano seconds" << std::endl;
181         boost::uint64_t res = measure_time_void( overhead_c).count();
182         std::cout << "void: average of " << res << " nano seconds" << std::endl;
183         res = measure_time_int( overhead_c).count();
184         std::cout << "int: average of " << res << " nano seconds" << std::endl;
185         res = measure_time_x( overhead_c).count();
186         std::cout << "X: average of " << res << " nano seconds" << std::endl;
187 #ifdef BOOST_CONTEXT_CYCLE
188         cycle_type overhead_y = overhead_cycle();
189         std::cout << "overhead " << overhead_y << " cpu cycles" << std::endl;
190         res = measure_cycles_void( overhead_y);
191         std::cout << "void: average of " << res << " cpu cycles" << std::endl;
192         res = measure_cycles_int( overhead_y);
193         std::cout << "int: average of " << res << " cpu cycles" << std::endl;
194         res = measure_cycles_x( overhead_y);
195         std::cout << "X: average of " << res << " cpu cycles" << std::endl;
196 #endif
197
198         return EXIT_SUCCESS;
199     }
200     catch ( std::exception const& e)
201     { std::cerr << "exception: " << e.what() << std::endl; }
202     catch (...)
203     { std::cerr << "unhandled exception" << std::endl; }
204     return EXIT_FAILURE;
205 }