1 
2 //          Copyright Oliver Kowalke 2014.
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 <cstdlib>
8 #include <iostream>
9 #include <stdexcept>
10 #include <string>
11 
12 #include <boost/chrono.hpp>
13 #include <boost/coroutine2/all.hpp>
14 #include <boost/cstdint.hpp>
15 #include <boost/program_options.hpp>
16 
17 #include "../bind_processor.hpp"
18 #include "../clock.hpp"
19 #include "../cycle.hpp"
20 
21 bool preserve = false;
22 boost::uint64_t jobs = 1000;
23 
24 struct X
25 {
26     std::string str;
27 
XX28     X( std::string const& str_) :
29         str( str_)
30     {}
31 };
32 
33 const X x("abc");
34 
fn_void(boost::coroutines2::coroutine<void>::push_type & c)35 void fn_void( boost::coroutines2::coroutine< void >::push_type & c)
36 { while ( true) c(); }
37 
fn_int(boost::coroutines2::coroutine<int>::push_type & c)38 void fn_int( boost::coroutines2::coroutine< int >::push_type & c)
39 { while ( true) c( 7); }
40 
fn_x(boost::coroutines2::coroutine<X>::push_type & c)41 void fn_x( boost::coroutines2::coroutine< X >::push_type & c)
42 {
43     while ( true) c( x);
44 }
45 
measure_time_void(duration_type overhead)46 duration_type measure_time_void( duration_type overhead)
47 {
48     boost::coroutines2::segmented_stack stack_alloc;
49     boost::coroutines2::coroutine< void >::pull_type c( stack_alloc, fn_void, preserve);
50 
51     time_point_type start( clock_type::now() );
52     for ( std::size_t i = 0; i < jobs; ++i) {
53         c();
54     }
55     duration_type total = clock_type::now() - start;
56     total -= overhead_clock(); // overhead of measurement
57     total /= jobs;  // loops
58     total /= 2;  // 2x jump_fcontext
59 
60     return total;
61 }
62 
measure_time_int(duration_type overhead)63 duration_type measure_time_int( duration_type overhead)
64 {
65     boost::coroutines2::segmented_stack stack_alloc;
66     boost::coroutines2::coroutine< int >::pull_type c( stack_alloc, fn_int, preserve);
67 
68     time_point_type start( clock_type::now() );
69     for ( std::size_t i = 0; i < jobs; ++i) {
70         c();
71     }
72     duration_type total = clock_type::now() - start;
73     total -= overhead_clock(); // overhead of measurement
74     total /= jobs;  // loops
75     total /= 2;  // 2x jump_fcontext
76 
77     return total;
78 }
79 
measure_time_x(duration_type overhead)80 duration_type measure_time_x( duration_type overhead)
81 {
82     boost::coroutines2::segmented_stack stack_alloc;
83     boost::coroutines2::coroutine< X >::pull_type c( stack_alloc, fn_x, preserve);
84 
85     time_point_type start( clock_type::now() );
86     for ( std::size_t i = 0; i < jobs; ++i) {
87         c();
88     }
89     duration_type total = clock_type::now() - start;
90     total -= overhead_clock(); // overhead of measurement
91     total /= jobs;  // loops
92     total /= 2;  // 2x jump_fcontext
93 
94     return total;
95 }
96 
97 # ifdef BOOST_CONTEXT_CYCLE
measure_cycles_void(cycle_type overhead)98 cycle_type measure_cycles_void( cycle_type overhead)
99 {
100     boost::coroutines2::segmented_stack stack_alloc;
101     boost::coroutines2::coroutine< void >::pull_type c( stack_alloc, fn_void, preserve);
102 
103     cycle_type start( cycles() );
104     for ( std::size_t i = 0; i < jobs; ++i) {
105         c();
106     }
107     cycle_type total = cycles() - start;
108     total -= overhead; // overhead of measurement
109     total /= jobs;  // loops
110     total /= 2;  // 2x jump_fcontext
111 
112     return total;
113 }
114 
measure_cycles_int(cycle_type overhead)115 cycle_type measure_cycles_int( cycle_type overhead)
116 {
117     boost::coroutines2::segmented_stack stack_alloc;
118     boost::coroutines2::coroutine< int >::pull_type c( stack_alloc, fn_int, preserve);
119 
120     cycle_type start( cycles() );
121     for ( std::size_t i = 0; i < jobs; ++i) {
122         c();
123     }
124     cycle_type total = cycles() - start;
125     total -= overhead; // overhead of measurement
126     total /= jobs;  // loops
127     total /= 2;  // 2x jump_fcontext
128 
129     return total;
130 }
131 
measure_cycles_x(cycle_type overhead)132 cycle_type measure_cycles_x( cycle_type overhead)
133 {
134     boost::coroutines2::segmented_stack stack_alloc;
135     boost::coroutines2::coroutine< X >::pull_type c( stack_alloc, fn_x, preserve);
136 
137     cycle_type start( cycles() );
138     for ( std::size_t i = 0; i < jobs; ++i) {
139         c();
140     }
141     cycle_type total = cycles() - start;
142     total -= overhead; // overhead of measurement
143     total /= jobs;  // loops
144     total /= 2;  // 2x jump_fcontext
145 
146     return total;
147 }
148 # endif
149 
main(int argc,char * argv[])150 int main( int argc, char * argv[])
151 {
152     try
153     {
154         bool bind = false;
155         boost::program_options::options_description desc("allowed options");
156         desc.add_options()
157             ("help", "help message")
158             ("bind,b", boost::program_options::value< bool >( & bind), "bind thread to CPU")
159             ("fpu,f", boost::program_options::value< bool >( & preserve), "preserve FPU registers")
160             ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run");
161 
162         boost::program_options::variables_map vm;
163         boost::program_options::store(
164                 boost::program_options::parse_command_line(
165                     argc,
166                     argv,
167                     desc),
168                 vm);
169         boost::program_options::notify( vm);
170 
171         if ( vm.count("help") ) {
172             std::cout << desc << std::endl;
173             return EXIT_SUCCESS;
174         }
175 
176         if ( bind) bind_to_processor( 0);
177 
178         duration_type overhead_c = overhead_clock();
179         std::cout << "overhead " << overhead_c.count() << " nano seconds" << std::endl;
180         boost::uint64_t res = measure_time_void( overhead_c).count();
181         std::cout << "void: average of " << res << " nano seconds" << std::endl;
182         res = measure_time_int( overhead_c).count();
183         std::cout << "int: average of " << res << " nano seconds" << std::endl;
184         res = measure_time_x( overhead_c).count();
185         std::cout << "X: average of " << res << " nano seconds" << std::endl;
186 #ifdef BOOST_CONTEXT_CYCLE
187         cycle_type overhead_y = overhead_cycle();
188         std::cout << "overhead " << overhead_y << " cpu cycles" << std::endl;
189         res = measure_cycles_void( overhead_y);
190         std::cout << "void: average of " << res << " cpu cycles" << std::endl;
191         res = measure_cycles_int( overhead_y);
192         std::cout << "int: average of " << res << " cpu cycles" << std::endl;
193         res = measure_cycles_x( overhead_y);
194         std::cout << "X: average of " << res << " cpu cycles" << std::endl;
195 #endif
196 
197         return EXIT_SUCCESS;
198     }
199     catch ( std::exception const& e)
200     { std::cerr << "exception: " << e.what() << std::endl; }
201     catch (...)
202     { std::cerr << "unhandled exception" << std::endl; }
203     return EXIT_FAILURE;
204 }
205