]>
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 | ||
7 | #include <cstddef> | |
8 | #include <cstdlib> | |
9 | #include <iostream> | |
10 | #include <stdexcept> | |
11 | ||
12 | #include <boost/context/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 | boost::uint64_t jobs = 1000; | |
21 | ||
22 | static boost::context::execution_context< void > foo( boost::context::execution_context< void > ctx) { | |
23 | while ( true) { | |
24 | ctx = ctx(); | |
25 | } | |
26 | } | |
27 | ||
28 | duration_type measure_time() { | |
29 | // cache warum-up | |
30 | boost::context::execution_context< void > ctx( foo); | |
31 | ctx = ctx(); | |
32 | ||
33 | time_point_type start( clock_type::now() ); | |
34 | for ( std::size_t i = 0; i < jobs; ++i) { | |
35 | ctx = ctx(); | |
36 | } | |
37 | duration_type total = clock_type::now() - start; | |
38 | total -= overhead_clock(); // overhead of measurement | |
39 | total /= jobs; // loops | |
40 | total /= 2; // 2x jump_fcontext | |
41 | ||
42 | return total; | |
43 | } | |
44 | ||
45 | duration_type measure_time_() { | |
46 | // cache warum-up | |
47 | boost::context::fixedsize_stack alloc; | |
48 | boost::context::execution_context< void > ctx( std::allocator_arg, alloc, foo); | |
49 | ctx = ctx(); | |
50 | ||
51 | time_point_type start( clock_type::now() ); | |
52 | for ( std::size_t i = 0; i < jobs; ++i) { | |
53 | ctx = ctx( boost::context::exec_ontop_arg, | |
54 | [](boost::context::execution_context< void > ctx){ | |
55 | return std::move( ctx); | |
56 | }); | |
57 | } | |
58 | duration_type total = clock_type::now() - start; | |
59 | total -= overhead_clock(); // overhead of measurement | |
60 | total /= jobs; // loops | |
61 | total /= 2; // 2x jump_fcontext | |
62 | ||
63 | return total; | |
64 | } | |
65 | ||
66 | #ifdef BOOST_CONTEXT_CYCLE | |
67 | cycle_type measure_cycles() { | |
68 | // cache warum-up | |
69 | boost::context::fixedsize_stack alloc; | |
70 | boost::context::execution_context< void > ctx( std::allocator_arg, alloc, foo); | |
71 | ctx = ctx(); | |
72 | ||
73 | cycle_type start( cycles() ); | |
74 | for ( std::size_t i = 0; i < jobs; ++i) { | |
75 | ctx = ctx(); | |
76 | } | |
77 | cycle_type total = cycles() - start; | |
78 | total -= overhead_cycle(); // overhead of measurement | |
79 | total /= jobs; // loops | |
80 | total /= 2; // 2x jump_fcontext | |
81 | ||
82 | return total; | |
83 | } | |
84 | ||
85 | cycle_type measure_cycles_() { | |
86 | // cache warum-up | |
87 | boost::context::fixedsize_stack alloc; | |
88 | boost::context::execution_context< void > ctx( std::allocator_arg, alloc, foo); | |
89 | ctx = ctx(); | |
90 | ||
91 | cycle_type start( cycles() ); | |
92 | for ( std::size_t i = 0; i < jobs; ++i) { | |
93 | ctx = ctx( boost::context::exec_ontop_arg, | |
94 | [](boost::context::execution_context< void > ctx){ | |
95 | return std::move( ctx); | |
96 | }); | |
97 | } | |
98 | cycle_type total = cycles() - start; | |
99 | total -= overhead_cycle(); // overhead of measurement | |
100 | total /= jobs; // loops | |
101 | total /= 2; // 2x jump_fcontext | |
102 | ||
103 | return total; | |
104 | } | |
105 | #endif | |
106 | ||
107 | int main( int argc, char * argv[]) { | |
108 | try { | |
109 | bind_to_processor( 0); | |
110 | ||
111 | boost::program_options::options_description desc("allowed options"); | |
112 | desc.add_options() | |
113 | ("help", "help message") | |
114 | ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run"); | |
115 | ||
116 | boost::program_options::variables_map vm; | |
117 | boost::program_options::store( | |
118 | boost::program_options::parse_command_line( | |
119 | argc, | |
120 | argv, | |
121 | desc), | |
122 | vm); | |
123 | boost::program_options::notify( vm); | |
124 | ||
125 | if ( vm.count("help") ) { | |
126 | std::cout << desc << std::endl; | |
127 | return EXIT_SUCCESS; | |
128 | } | |
129 | ||
130 | boost::uint64_t res = measure_time().count(); | |
131 | std::cout << "execution_context: average of " << res << " nano seconds" << std::endl; | |
132 | res = measure_time_().count(); | |
133 | std::cout << "execution_context: average of (ontop) " << res << " nano seconds" << std::endl; | |
134 | #ifdef BOOST_CONTEXT_CYCLE | |
135 | res = measure_cycles(); | |
136 | std::cout << "execution_context: average of " << res << " cpu cycles" << std::endl; | |
137 | res = measure_cycles_(); | |
138 | std::cout << "execution_context: average of (ontop) " << res << " cpu cycles" << std::endl; | |
139 | #endif | |
140 | ||
141 | return EXIT_SUCCESS; | |
142 | } catch ( std::exception const& e) { | |
143 | std::cerr << "exception: " << e.what() << std::endl; | |
144 | } catch (...) { | |
145 | std::cerr << "unhandled exception" << std::endl; | |
146 | } | |
147 | return EXIT_FAILURE; | |
148 | } |