]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/coroutine2/performance/performance_switch.cpp
2 // Copyright Oliver Kowalke 2014.
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/coroutine2/all.hpp>
14 #include <boost/cstdint.hpp>
15 #include <boost/program_options.hpp>
17 #include "bind_processor.hpp"
21 boost::uint64_t jobs
= 1000;
27 X( std::string
const& str_
) :
34 void fn_void( boost::coroutines2::coroutine
< void >::push_type
& c
)
35 { while ( true) c(); }
37 void fn_int( boost::coroutines2::coroutine
< int >::push_type
& c
)
38 { while ( true) c( 7); }
40 void fn_x( boost::coroutines2::coroutine
< X
>::push_type
& c
)
45 duration_type
measure_time_void( duration_type overhead
)
47 boost::coroutines2::coroutine
< void >::pull_type
c( fn_void
);
49 time_point_type
start( clock_type::now() );
50 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
53 duration_type total
= clock_type::now() - start
;
54 total
-= overhead_clock(); // overhead of measurement
55 total
/= jobs
; // loops
56 total
/= 2; // 2x jump_fcontext
61 duration_type
measure_time_int( duration_type overhead
)
63 boost::coroutines2::coroutine
< int >::pull_type
c( fn_int
);
65 time_point_type
start( clock_type::now() );
66 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
69 duration_type total
= clock_type::now() - start
;
70 total
-= overhead_clock(); // overhead of measurement
71 total
/= jobs
; // loops
72 total
/= 2; // 2x jump_fcontext
77 duration_type
measure_time_x( duration_type overhead
)
79 boost::coroutines2::coroutine
< X
>::pull_type
c( fn_x
);
81 time_point_type
start( clock_type::now() );
82 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
85 duration_type total
= clock_type::now() - start
;
86 total
-= overhead_clock(); // overhead of measurement
87 total
/= jobs
; // loops
88 total
/= 2; // 2x jump_fcontext
93 # ifdef BOOST_CONTEXT_CYCLE
94 cycle_type
measure_cycles_void( cycle_type overhead
)
96 boost::coroutines2::coroutine
< void >::pull_type
c( fn_void
);
98 cycle_type
start( cycles() );
99 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
102 cycle_type total
= cycles() - start
;
103 total
-= overhead
; // overhead of measurement
104 total
/= jobs
; // loops
105 total
/= 2; // 2x jump_fcontext
110 cycle_type
measure_cycles_int( cycle_type overhead
)
112 boost::coroutines2::coroutine
< int >::pull_type
c( fn_int
);
114 cycle_type
start( cycles() );
115 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
118 cycle_type total
= cycles() - start
;
119 total
-= overhead
; // overhead of measurement
120 total
/= jobs
; // loops
121 total
/= 2; // 2x jump_fcontext
126 cycle_type
measure_cycles_x( cycle_type overhead
)
128 boost::coroutines2::coroutine
< X
>::pull_type
c( fn_x
);
130 cycle_type
start( cycles() );
131 for ( std::size_t i
= 0; i
< jobs
; ++i
) {
134 cycle_type total
= cycles() - start
;
135 total
-= overhead
; // overhead of measurement
136 total
/= jobs
; // loops
137 total
/= 2; // 2x jump_fcontext
143 int main( int argc
, char * argv
[])
148 boost::program_options::options_description
desc("allowed options");
150 ("help", "help message")
151 ("bind,b", boost::program_options::value
< bool >( & bind
), "bind thread to CPU")
152 ("jobs,j", boost::program_options::value
< boost::uint64_t >( & jobs
), "jobs to run");
154 boost::program_options::variables_map vm
;
155 boost::program_options::store(
156 boost::program_options::parse_command_line(
161 boost::program_options::notify( vm
);
163 if ( vm
.count("help") ) {
164 std::cout
<< desc
<< std::endl
;
168 if ( bind
) bind_to_processor( 0);
170 duration_type overhead_c
= overhead_clock();
171 boost::uint64_t res
= measure_time_void( overhead_c
).count();
172 std::cout
<< "void: average of " << res
<< " nano seconds" << std::endl
;
173 res
= measure_time_int( overhead_c
).count();
174 std::cout
<< "int: average of " << res
<< " nano seconds" << std::endl
;
175 res
= measure_time_x( overhead_c
).count();
176 std::cout
<< "X: average of " << res
<< " nano seconds" << std::endl
;
177 #ifdef BOOST_CONTEXT_CYCLE
178 cycle_type overhead_y
= overhead_cycle();
179 res
= measure_cycles_void( overhead_y
);
180 std::cout
<< "void: average of " << res
<< " cpu cycles" << std::endl
;
181 res
= measure_cycles_int( overhead_y
);
182 std::cout
<< "int: average of " << res
<< " cpu cycles" << std::endl
;
183 res
= measure_cycles_x( overhead_y
);
184 std::cout
<< "X: average of " << res
<< " cpu cycles" << std::endl
;
189 catch ( std::exception
const& e
)
190 { std::cerr
<< "exception: " << e
.what() << std::endl
; }
192 { std::cerr
<< "unhandled exception" << std::endl
; }