]>
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 | #include "../../example/simple_stack_allocator.hpp" | |
20 | ||
21 | typedef boost::context::simple_stack_allocator< | |
22 | 8 * 1024 * 1024, 64 * 1024, 8 * 1024 | |
23 | > stack_allocator; | |
24 | ||
25 | boost::uint64_t jobs = 1000; | |
26 | ||
27 | static void foo( boost::context::transfer_t t_) { | |
28 | boost::context::transfer_t t = t_; | |
29 | while ( true) { | |
30 | t = boost::context::jump_fcontext( t.fctx, 0); | |
31 | } | |
32 | } | |
33 | ||
34 | duration_type measure_time_fc() { | |
35 | stack_allocator stack_alloc; | |
36 | boost::context::fcontext_t ctx = boost::context::make_fcontext( | |
37 | stack_alloc.allocate( stack_allocator::default_stacksize() ), | |
38 | stack_allocator::default_stacksize(), | |
39 | foo); | |
40 | ||
41 | // cache warum-up | |
42 | boost::context::transfer_t t = boost::context::jump_fcontext( ctx, 0); | |
43 | ||
44 | time_point_type start( clock_type::now() ); | |
45 | for ( std::size_t i = 0; i < jobs; ++i) { | |
46 | t = boost::context::jump_fcontext( t.fctx, 0); | |
47 | } | |
48 | duration_type total = clock_type::now() - start; | |
49 | total -= overhead_clock(); // overhead of measurement | |
50 | total /= jobs; // loops | |
51 | total /= 2; // 2x jump_fcontext | |
52 | ||
53 | return total; | |
54 | } | |
55 | ||
56 | #ifdef BOOST_CONTEXT_CYCLE | |
57 | cycle_type measure_cycles_fc() { | |
58 | stack_allocator stack_alloc; | |
59 | boost::context::fcontext_t ctx = boost::context::make_fcontext( | |
60 | stack_alloc.allocate( stack_allocator::default_stacksize() ), | |
61 | stack_allocator::default_stacksize(), | |
62 | foo); | |
63 | ||
64 | // cache warum-up | |
65 | boost::context::transfer_t t = boost::context::jump_fcontext( ctx, 0); | |
66 | ||
67 | cycle_type start( cycles() ); | |
68 | for ( std::size_t i = 0; i < jobs; ++i) { | |
69 | t = boost::context::jump_fcontext( t.fctx, 0); | |
70 | } | |
71 | cycle_type total = cycles() - start; | |
72 | total -= overhead_cycle(); // overhead of measurement | |
73 | total /= jobs; // loops | |
74 | total /= 2; // 2x jump_fcontext | |
75 | ||
76 | return total; | |
77 | } | |
78 | #endif | |
79 | ||
80 | int main( int argc, char * argv[]) | |
81 | { | |
82 | try | |
83 | { | |
84 | bind_to_processor( 0); | |
85 | ||
86 | boost::program_options::options_description desc("allowed options"); | |
87 | desc.add_options() | |
88 | ("help", "help message") | |
89 | ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run"); | |
90 | ||
91 | boost::program_options::variables_map vm; | |
92 | boost::program_options::store( | |
93 | boost::program_options::parse_command_line( | |
94 | argc, | |
95 | argv, | |
96 | desc), | |
97 | vm); | |
98 | boost::program_options::notify( vm); | |
99 | ||
100 | if ( vm.count("help") ) { | |
101 | std::cout << desc << std::endl; | |
102 | return EXIT_SUCCESS; | |
103 | } | |
104 | ||
105 | boost::uint64_t res = measure_time_fc().count(); | |
106 | std::cout << "fcontext_t: average of " << res << " nano seconds" << std::endl; | |
107 | #ifdef BOOST_CONTEXT_CYCLE | |
108 | res = measure_cycles_fc(); | |
109 | std::cout << "fcontext_t: average of " << res << " cpu cycles" << std::endl; | |
110 | #endif | |
111 | ||
112 | return EXIT_SUCCESS; | |
113 | } | |
114 | catch ( std::exception const& e) | |
115 | { std::cerr << "exception: " << e.what() << std::endl; } | |
116 | catch (...) | |
117 | { std::cerr << "unhandled exception" << std::endl; } | |
118 | return EXIT_FAILURE; | |
119 | } |