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