]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/coroutine2/performance/performance_create_protected.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / coroutine2 / performance / performance_create_protected.cpp
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
11 #include <boost/chrono.hpp>
12 #include <boost/coroutine2/all.hpp>
13 #include <boost/cstdint.hpp>
14 #include <boost/program_options.hpp>
15
16 #include "bind_processor.hpp"
17 #include "clock.hpp"
18 #include "cycle.hpp"
19
20 typedef boost::coroutines2::protected_fixedsize_stack stack_allocator;
21 typedef boost::coroutines2::coroutine< void > coro_type;
22
23 boost::uint64_t jobs = 1000;
24
25 void fn( coro_type::pull_type & c)
26 { while ( true) c(); }
27
28 duration_type measure_time( duration_type overhead)
29 {
30 stack_allocator stack_alloc;
31
32 time_point_type start( clock_type::now() );
33 for ( std::size_t i = 0; i < jobs; ++i) {
34 coro_type::push_type c( stack_alloc, fn);
35 }
36 duration_type total = clock_type::now() - start;
37 total -= overhead_clock(); // overhead of measurement
38 total /= jobs; // loops
39
40 return total;
41 }
42
43 # ifdef BOOST_CONTEXT_CYCLE
44 cycle_type measure_cycles( cycle_type overhead)
45 {
46 stack_allocator stack_alloc;
47
48 cycle_type start( cycles() );
49 for ( std::size_t i = 0; i < jobs; ++i) {
50 coro_type::push_type c( stack_alloc, fn);
51 }
52 cycle_type total = cycles() - start;
53 total -= overhead; // overhead of measurement
54 total /= jobs; // loops
55
56 return total;
57 }
58 # endif
59
60 int main( int argc, char * argv[])
61 {
62 try
63 {
64 bool bind = false;
65 boost::program_options::options_description desc("allowed options");
66 desc.add_options()
67 ("help", "help message")
68 ("bind,b", boost::program_options::value< bool >( & bind), "bind thread to CPU")
69 ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run");
70
71 boost::program_options::variables_map vm;
72 boost::program_options::store(
73 boost::program_options::parse_command_line(
74 argc,
75 argv,
76 desc),
77 vm);
78 boost::program_options::notify( vm);
79
80 if ( vm.count("help") ) {
81 std::cout << desc << std::endl;
82 return EXIT_SUCCESS;
83 }
84
85 if ( bind) bind_to_processor( 0);
86
87 duration_type overhead_c = overhead_clock();
88 boost::uint64_t res = measure_time( overhead_c).count();
89 std::cout << "average of " << res << " nano seconds" << std::endl;
90 #ifdef BOOST_CONTEXT_CYCLE
91 cycle_type overhead_y = overhead_cycle();
92 res = measure_cycles( overhead_y);
93 std::cout << "average of " << res << " cpu cycles" << std::endl;
94 #endif
95
96 return EXIT_SUCCESS;
97 }
98 catch ( std::exception const& e)
99 { std::cerr << "exception: " << e.what() << std::endl; }
100 catch (...)
101 { std::cerr << "unhandled exception" << std::endl; }
102 return EXIT_FAILURE;
103 }