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)
12 #include <boost/chrono.hpp>
13 #include <boost/coroutine/all.hpp>
14 #include <boost/cstdint.hpp>
15 #include <boost/program_options.hpp>
17 #include "../bind_processor.hpp"
18 #include "../clock.hpp"
19 #include "../cycle.hpp"
21 boost::coroutines::flag_fpu_t preserve_fpu = boost::coroutines::fpu_not_preserved;
22 boost::uint64_t jobs = 1000;
29 X( std::string const& str_) :
36 void fn_void( boost::coroutines::symmetric_coroutine< void >::yield_type & yield)
37 { while( true) yield(); }
39 void fn_int( boost::coroutines::symmetric_coroutine< int >::yield_type & yield)
40 { while( true) yield(); }
42 void fn_x( boost::coroutines::symmetric_coroutine< X >::yield_type & yield)
43 { while( true) yield(); }
45 duration_type measure_time_void( duration_type overhead)
47 boost::coroutines::symmetric_coroutine< void >::call_type c( fn_void,
48 boost::coroutines::attributes( preserve_fpu) );
51 time_point_type start( clock_type::now() );
52 for ( std::size_t i = 0; i < jobs; ++i) {
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
63 duration_type measure_time_int( duration_type overhead)
65 boost::coroutines::symmetric_coroutine< int >::call_type c( fn_int,
66 boost::coroutines::attributes( preserve_fpu) );
68 time_point_type start( clock_type::now() );
69 for ( std::size_t i = 0; i < jobs; ++i) {
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
80 duration_type measure_time_x( duration_type overhead)
82 boost::coroutines::symmetric_coroutine< X >::call_type c( fn_x,
83 boost::coroutines::attributes( preserve_fpu) );
86 time_point_type start( clock_type::now() );
87 for ( std::size_t i = 0; i < jobs; ++i) {
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
98 # ifdef BOOST_CONTEXT_CYCLE
99 cycle_type measure_cycles_void( cycle_type overhead)
101 boost::coroutines::symmetric_coroutine< void >::call_type c( fn_void,
102 boost::coroutines::attributes( preserve_fpu) );
104 cycle_type start( cycles() );
105 for ( std::size_t i = 0; i < jobs; ++i) {
108 cycle_type total = cycles() - start;
109 total -= overhead; // overhead of measurement
110 total /= jobs; // loops
111 total /= 2; // 2x jump_fcontext
116 cycle_type measure_cycles_int( cycle_type overhead)
118 boost::coroutines::symmetric_coroutine< int >::call_type c( fn_int,
119 boost::coroutines::attributes( preserve_fpu) );
121 cycle_type start( cycles() );
122 for ( std::size_t i = 0; i < jobs; ++i) {
125 cycle_type total = cycles() - start;
126 total -= overhead; // overhead of measurement
127 total /= jobs; // loops
128 total /= 2; // 2x jump_fcontext
133 cycle_type measure_cycles_x( cycle_type overhead)
135 boost::coroutines::symmetric_coroutine< X >::call_type c( fn_x,
136 boost::coroutines::attributes( preserve_fpu) );
139 cycle_type start( cycles() );
140 for ( std::size_t i = 0; i < jobs; ++i) {
143 cycle_type total = cycles() - start;
144 total -= overhead; // overhead of measurement
145 total /= jobs; // loops
146 total /= 2; // 2x jump_fcontext
152 int main( int argc, char * argv[])
156 bool preserve = false, bind = false;
157 boost::program_options::options_description desc("allowed 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");
164 boost::program_options::variables_map vm;
165 boost::program_options::store(
166 boost::program_options::parse_command_line(
171 boost::program_options::notify( vm);
173 if ( vm.count("help") ) {
174 std::cout << desc << std::endl;
178 if ( preserve) preserve_fpu = boost::coroutines::fpu_preserved;
179 if ( bind) bind_to_processor( 0);
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;
202 catch ( std::exception const& e)
203 { std::cerr << "exception: " << e.what() << std::endl; }
205 { std::cerr << "unhandled exception" << std::endl; }