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;
28 X( std::string const& str_) :
35 void fn_void( boost::coroutines::asymmetric_coroutine< void >::push_type & c)
36 { while ( true) c(); }
38 void fn_int( boost::coroutines::asymmetric_coroutine< int >::push_type & c)
39 { while ( true) c( 7); }
41 void fn_x( boost::coroutines::asymmetric_coroutine< X >::push_type & c)
46 duration_type measure_time_void( duration_type overhead)
48 boost::coroutines::asymmetric_coroutine< void >::pull_type c( fn_void,
49 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::asymmetric_coroutine< int >::pull_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::asymmetric_coroutine< X >::pull_type c( fn_x,
83 boost::coroutines::attributes( preserve_fpu) );
85 time_point_type start( clock_type::now() );
86 for ( std::size_t i = 0; i < jobs; ++i) {
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
97 # ifdef BOOST_CONTEXT_CYCLE
98 cycle_type measure_cycles_void( cycle_type overhead)
100 boost::coroutines::asymmetric_coroutine< void >::pull_type c( fn_void,
101 boost::coroutines::attributes( preserve_fpu) );
103 cycle_type start( cycles() );
104 for ( std::size_t i = 0; i < jobs; ++i) {
107 cycle_type total = cycles() - start;
108 total -= overhead; // overhead of measurement
109 total /= jobs; // loops
110 total /= 2; // 2x jump_fcontext
115 cycle_type measure_cycles_int( cycle_type overhead)
117 boost::coroutines::asymmetric_coroutine< int >::pull_type c( fn_int,
118 boost::coroutines::attributes( preserve_fpu) );
120 cycle_type start( cycles() );
121 for ( std::size_t i = 0; i < jobs; ++i) {
124 cycle_type total = cycles() - start;
125 total -= overhead; // overhead of measurement
126 total /= jobs; // loops
127 total /= 2; // 2x jump_fcontext
132 cycle_type measure_cycles_x( cycle_type overhead)
134 boost::coroutines::asymmetric_coroutine< X >::pull_type c( fn_x,
135 boost::coroutines::attributes( preserve_fpu) );
137 cycle_type start( cycles() );
138 for ( std::size_t i = 0; i < jobs; ++i) {
141 cycle_type total = cycles() - start;
142 total -= overhead; // overhead of measurement
143 total /= jobs; // loops
144 total /= 2; // 2x jump_fcontext
150 int main( int argc, char * argv[])
154 bool preserve = false, bind = false;
155 boost::program_options::options_description desc("allowed 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");
162 boost::program_options::variables_map vm;
163 boost::program_options::store(
164 boost::program_options::parse_command_line(
169 boost::program_options::notify( vm);
171 if ( vm.count("help") ) {
172 std::cout << desc << std::endl;
176 if ( preserve) preserve_fpu = boost::coroutines::fpu_preserved;
177 if ( bind) bind_to_processor( 0);
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;
200 catch ( std::exception const& e)
201 { std::cerr << "exception: " << e.what() << std::endl; }
203 { std::cerr << "unhandled exception" << std::endl; }