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