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