1
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)
6
7 #include <cstdlib>
8 #include <iostream>
9 #include <stdexcept>
10 #include <string>
11
12 #include <boost/chrono.hpp>
13 #include <boost/coroutine2/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::uint64_t jobs = 1000;
22
fn(boost::coroutines2::coroutine<void>::push_type & c)23 void fn( boost::coroutines2::coroutine< void >::push_type & c) {
24 while ( true) {
25 c();
26 }
27 }
28
measure_time_void(duration_type overhead)29 duration_type measure_time_void( duration_type overhead) {
30 boost::coroutines2::coroutine< void >::pull_type c{ fn };
31 time_point_type start( clock_type::now() );
32 for ( std::size_t i = 0; i < jobs; ++i) {
33 c();
34 }
35 duration_type total = clock_type::now() - start;
36 total -= overhead_clock(); // overhead of measurement
37 total /= jobs; // loops
38 total /= 2; // 2x jump_fcontext
39
40 return total;
41 }
42
43 # ifdef BOOST_CONTEXT_CYCLE
measure_cycles_void(cycle_type overhead)44 cycle_type measure_cycles_void( cycle_type overhead) {
45 boost::coroutines2::coroutine< void >::pull_type c{ fn };
46 cycle_type start( cycles() );
47 for ( std::size_t i = 0; i < jobs; ++i) {
48 c();
49 }
50 cycle_type total = cycles() - start;
51 total -= overhead; // overhead of measurement
52 total /= jobs; // loops
53 total /= 2; // 2x jump_fcontext
54
55 return total;
56 }
57 # endif
58
main(int argc,char * argv[])59 int main( int argc, char * argv[]) {
60 try {
61 bool bind = false;
62 boost::program_options::options_description desc("allowed options");
63 desc.add_options()
64 ("help", "help message")
65 ("bind,b", boost::program_options::value< bool >( & bind), "bind thread to CPU")
66 ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run");
67 boost::program_options::variables_map vm;
68 boost::program_options::store(
69 boost::program_options::parse_command_line(
70 argc,
71 argv,
72 desc),
73 vm);
74 boost::program_options::notify( vm);
75 if ( vm.count("help") ) {
76 std::cout << desc << std::endl;
77 return EXIT_SUCCESS;
78 }
79 if ( bind) {
80 bind_to_processor( 0);
81 }
82 duration_type overhead_c = overhead_clock();
83 boost::uint64_t res = measure_time_void( overhead_c).count();
84 std::cout << "average of " << res << " nano seconds" << std::endl;
85 #ifdef BOOST_CONTEXT_CYCLE
86 cycle_type overhead_y = overhead_cycle();
87 res = measure_cycles_void( overhead_y);
88 std::cout << "average of " << res << " cpu cycles" << std::endl;
89 #endif
90 return EXIT_SUCCESS;
91 }
92 catch ( std::exception const& e) {
93 std::cerr << "exception: " << e.what() << std::endl;
94 } catch (...) {
95 std::cerr << "unhandled exception" << std::endl;
96 }
97 return EXIT_FAILURE;
98 }
99