]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | |
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) | |
6 | ||
b32b8144 | 7 | #include <cstddef> |
7c673cae FG |
8 | #include <cstdlib> |
9 | #include <iostream> | |
10 | #include <stdexcept> | |
11 | ||
b32b8144 FG |
12 | #include <boost/context/continuation.hpp> |
13 | #include <boost/cstdint.hpp> | |
7c673cae FG |
14 | #include <boost/program_options.hpp> |
15 | ||
7c673cae | 16 | #include "bind_processor.hpp" |
b32b8144 | 17 | #include "clock.hpp" |
7c673cae FG |
18 | #include "cycle.hpp" |
19 | ||
b32b8144 | 20 | boost::uint64_t jobs = 1000; |
7c673cae FG |
21 | |
22 | namespace ctx = boost::context; | |
23 | ||
b32b8144 FG |
24 | static ctx::continuation foo( ctx::continuation && c) { |
25 | while ( true) { | |
26 | c = c.resume(); | |
27 | } | |
28 | return std::move( c); | |
7c673cae FG |
29 | } |
30 | ||
b32b8144 | 31 | duration_type measure_time() { |
7c673cae | 32 | // cache warum-up |
b32b8144 FG |
33 | ctx::continuation c = ctx::callcc( foo); |
34 | c = c.resume(); | |
7c673cae | 35 | |
b32b8144 FG |
36 | time_point_type start( clock_type::now() ); |
37 | for ( std::size_t i = 0; i < jobs; ++i) { | |
38 | c = c.resume(); | |
39 | } | |
40 | duration_type total = clock_type::now() - start; | |
41 | total -= overhead_clock(); // overhead of measurement | |
42 | total /= jobs; // loops | |
43 | total /= 2; // 2x jump_fcontext | |
7c673cae FG |
44 | |
45 | return total; | |
46 | } | |
7c673cae | 47 | |
b32b8144 FG |
48 | #ifdef BOOST_CONTEXT_CYCLE |
49 | cycle_type measure_cycles() { | |
7c673cae | 50 | // cache warum-up |
b32b8144 FG |
51 | ctx::fixedsize_stack alloc; |
52 | ctx::continuation c = ctx::callcc( std::allocator_arg, alloc, foo); | |
53 | c = c.resume(); | |
7c673cae | 54 | |
b32b8144 FG |
55 | cycle_type start( cycles() ); |
56 | for ( std::size_t i = 0; i < jobs; ++i) { | |
57 | c = c.resume(); | |
58 | } | |
59 | cycle_type total = cycles() - start; | |
60 | total -= overhead_cycle(); // overhead of measurement | |
61 | total /= jobs; // loops | |
62 | total /= 2; // 2x jump_fcontext | |
7c673cae FG |
63 | |
64 | return total; | |
65 | } | |
66 | #endif | |
67 | ||
b32b8144 FG |
68 | int main( int argc, char * argv[]) { |
69 | try { | |
7c673cae FG |
70 | bind_to_processor( 0); |
71 | ||
b32b8144 FG |
72 | boost::program_options::options_description desc("allowed options"); |
73 | desc.add_options() | |
74 | ("help", "help message") | |
75 | ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run"); | |
76 | ||
77 | boost::program_options::variables_map vm; | |
78 | boost::program_options::store( | |
79 | boost::program_options::parse_command_line( | |
80 | argc, | |
81 | argv, | |
82 | desc), | |
83 | vm); | |
84 | boost::program_options::notify( vm); | |
85 | ||
86 | if ( vm.count("help") ) { | |
87 | std::cout << desc << std::endl; | |
88 | return EXIT_SUCCESS; | |
7c673cae | 89 | } |
7c673cae | 90 | |
b32b8144 FG |
91 | boost::uint64_t res = measure_time().count(); |
92 | std::cout << "continuation: average of " << res << " nano seconds" << std::endl; | |
93 | #ifdef BOOST_CONTEXT_CYCLE | |
94 | res = measure_cycles(); | |
95 | std::cout << "continuation: average of " << res << " cpu cycles" << std::endl; | |
7c673cae FG |
96 | #endif |
97 | ||
98 | return EXIT_SUCCESS; | |
b32b8144 FG |
99 | } catch ( std::exception const& e) { |
100 | std::cerr << "exception: " << e.what() << std::endl; | |
101 | } catch (...) { | |
102 | std::cerr << "unhandled exception" << std::endl; | |
7c673cae | 103 | } |
7c673cae FG |
104 | return EXIT_FAILURE; |
105 | } |