]>
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 <cstdlib> | |
8 | #include <iostream> | |
9 | #include <stdexcept> | |
10 | #include <string> | |
11 | ||
12 | #include <boost/atomic.hpp> | |
13 | #include <boost/chrono.hpp> | |
14 | #include <boost/cstdint.hpp> | |
15 | #include <boost/preprocessor.hpp> | |
16 | #include <boost/program_options.hpp> | |
17 | ||
18 | #include <qthread/qthread.h> | |
19 | ||
20 | #include "../clock.hpp" | |
21 | ||
22 | #ifndef JOBS | |
23 | #define JOBS BOOST_PP_LIMIT_REPEAT | |
24 | #endif | |
25 | ||
26 | boost::atomic< boost::uint64_t > counter( 0); | |
27 | ||
28 | extern "C" aligned_t worker( void *) | |
29 | { | |
30 | ++counter; | |
31 | return aligned_t(); | |
32 | } | |
33 | ||
34 | duration_type measure( duration_type overhead) | |
35 | { | |
36 | time_point_type start( clock_type::now() ); | |
37 | for ( std::size_t i = 0; i < JOBS; ++i) { | |
38 | qthread_fork( & worker, 0, 0); | |
39 | } | |
40 | do | |
41 | { | |
42 | qthread_yield(); | |
43 | } while ( counter != JOBS); | |
44 | duration_type total = clock_type::now() - start; | |
45 | total -= overhead_clock(); // overhead of measurement | |
46 | total /= JOBS; // loops | |
47 | ||
48 | return total; | |
49 | } | |
50 | ||
51 | int main( int argc, char * argv[]) | |
52 | { | |
53 | try | |
54 | { | |
55 | boost::program_options::options_description desc("allowed options"); | |
56 | desc.add_options() | |
57 | ("help", "help message"); | |
58 | ||
59 | boost::program_options::variables_map vm; | |
60 | boost::program_options::store( | |
61 | boost::program_options::parse_command_line( | |
62 | argc, | |
63 | argv, | |
64 | desc), | |
65 | vm); | |
66 | boost::program_options::notify( vm); | |
67 | ||
68 | if ( vm.count("help") ) { | |
69 | std::cout << desc << std::endl; | |
70 | return EXIT_SUCCESS; | |
71 | } | |
72 | ||
73 | setenv("QT_NUM_SHEPHERDS", "1", 1); | |
74 | setenv("QT_NUM_WORKERS_PER_SHEPHERD", "1", 1); | |
75 | ||
76 | // Setup the qthreads environment. | |
77 | if ( 0 != qthread_initialize() ) | |
78 | throw std::runtime_error("qthreads failed to initialize\n"); | |
79 | ||
80 | duration_type overhead = overhead_clock(); | |
81 | boost::uint64_t res = measure( overhead).count(); | |
82 | std::cout << JOBS << " jobs: average of " << res << " nano seconds" << std::endl; | |
83 | ||
84 | return EXIT_SUCCESS; | |
85 | } | |
86 | catch ( std::exception const& e) | |
87 | { std::cerr << "exception: " << e.what() << std::endl; } | |
88 | catch (...) | |
89 | { std::cerr << "unhandled exception" << std::endl; } | |
90 | return EXIT_FAILURE; | |
91 | } |