From 98dae1bc9800689b6df6fca2a0a0f3fcf824ff3e Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Mon, 27 Jan 2014 19:52:57 +0100 Subject: [PATCH] fix performance tests --- performance/performance_create_prealloc.cpp | 38 +++++---- performance/performance_create_protected.cpp | 18 ++-- performance/performance_create_simple.cpp | 18 ++-- performance/performance_switch.cpp | 3 - performance/preallocated_stack_allocator.hpp | 89 +++++--------------- 5 files changed, 67 insertions(+), 99 deletions(-) diff --git a/performance/performance_create_prealloc.cpp b/performance/performance_create_prealloc.cpp index 32f9daa..f8210dd 100644 --- a/performance/performance_create_prealloc.cpp +++ b/performance/performance_create_prealloc.cpp @@ -18,28 +18,31 @@ #include "cycle.hpp" #include "preallocated_stack_allocator.hpp" -typedef preallocated_stack_allocator< 64 * 1024 > stack_allocator; -typedef boost::coroutines::coroutine< void, stack_allocator > coroutine; +typedef preallocated_stack_allocator stack_allocator; +typedef boost::coroutines::coroutine< void, stack_allocator > coro_type; boost::coroutines::flag_fpu_t preserve_fpu = boost::coroutines::fpu_not_preserved; boost::coroutines::flag_unwind_t unwind_stack = boost::coroutines::stack_unwind; boost::uint64_t jobs = 1000; -void fn( coroutine::push_type & c) +void fn( coro_type::push_type & c) { while ( true) c(); } duration_type measure_time() { - stack_allocator stack_alloc( jobs + 1); + stack_allocator stack_alloc; - // cache warum-up - coroutine::pull_type c( fn, - boost::coroutines::attributes( stack_allocator::default_stacksize(), unwind_stack, preserve_fpu), - stack_alloc); + { + // cache warum-up + coro_type::pull_type c( fn, + boost::coroutines::attributes( + stack_allocator::default_stacksize(), unwind_stack, preserve_fpu), + stack_alloc); + } time_point_type start( clock_type::now() ); for ( std::size_t i = 0; i < jobs; ++i) { - coroutine::pull_type c( fn, + coro_type::pull_type c( fn, boost::coroutines::attributes( stack_allocator::default_stacksize(), unwind_stack, preserve_fpu), stack_alloc); } @@ -53,16 +56,19 @@ duration_type measure_time() # ifdef BOOST_CONTEXT_CYCLE cycle_type measure_cycles() { - stack_allocator stack_alloc( jobs + 1); + stack_allocator stack_alloc; - // cache warum-up - coroutine::pull_type c( fn, - boost::coroutines::attributes( stack_allocator::default_stacksize(), unwind_stack, preserve_fpu), - stack_alloc); + { + // cache warum-up + coro_type::pull_type c( fn, + boost::coroutines::attributes( + stack_allocator::default_stacksize(), unwind_stack, preserve_fpu), + stack_alloc); + } cycle_type start( cycles() ); for ( std::size_t i = 0; i < jobs; ++i) { - coroutine::pull_type c( fn, + coro_type::pull_type c( fn, boost::coroutines::attributes( stack_allocator::default_stacksize(), unwind_stack, preserve_fpu), stack_alloc); } @@ -85,7 +91,7 @@ int main( int argc, char * argv[]) desc.add_options() ("help", "help message") ("fpu,f", boost::program_options::value< bool >( & preserve), "preserve FPU registers") - ("unwind,u", boost::program_options::value< bool >( & unwind), "unwind stack") + ("unwind,u", boost::program_options::value< bool >( & unwind), "unwind coroutine-stack") ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run"); boost::program_options::variables_map vm; diff --git a/performance/performance_create_protected.cpp b/performance/performance_create_protected.cpp index 24da6d7..5498a53 100644 --- a/performance/performance_create_protected.cpp +++ b/performance/performance_create_protected.cpp @@ -18,13 +18,13 @@ #include "cycle.hpp" typedef boost::coroutines::protected_stack_allocator stack_allocator; -typedef boost::coroutines::coroutine< void, stack_allocator > coroutine; +typedef boost::coroutines::coroutine< void, stack_allocator > coro_type; boost::coroutines::flag_fpu_t preserve_fpu = boost::coroutines::fpu_not_preserved; boost::coroutines::flag_unwind_t unwind_stack = boost::coroutines::stack_unwind; boost::uint64_t jobs = 1000; -void fn( coroutine::push_type & c) +void fn( coro_type::push_type & c) { while ( true) c(); } duration_type measure_time() @@ -32,11 +32,13 @@ duration_type measure_time() stack_allocator stack_alloc; // cache warum-up - coroutine::pull_type c( fn, boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); + coro_type::pull_type c( fn, + boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); time_point_type start( clock_type::now() ); for ( std::size_t i = 0; i < jobs; ++i) { - coroutine::pull_type c( fn, boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); + coro_type::pull_type c( fn, + boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); } duration_type total = clock_type::now() - start; total -= overhead_clock(); // overhead of measurement @@ -51,11 +53,13 @@ cycle_type measure_cycles() stack_allocator stack_alloc; // cache warum-up - coroutine::pull_type c( fn, boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); + coro_type::pull_type c( fn, + boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); cycle_type start( cycles() ); for ( std::size_t i = 0; i < jobs; ++i) { - coroutine::pull_type c( fn, boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); + coro_type::pull_type c( fn, + boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); } cycle_type total = cycles() - start; total -= overhead_cycle(); // overhead of measurement @@ -76,7 +80,7 @@ int main( int argc, char * argv[]) desc.add_options() ("help", "help message") ("fpu,f", boost::program_options::value< bool >( & preserve), "preserve FPU registers") - ("unwind,u", boost::program_options::value< bool >( & unwind), "unwind stack") + ("unwind,u", boost::program_options::value< bool >( & unwind), "unwind coroutine-stack") ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run"); boost::program_options::variables_map vm; diff --git a/performance/performance_create_simple.cpp b/performance/performance_create_simple.cpp index 034ba39..ca791a0 100644 --- a/performance/performance_create_simple.cpp +++ b/performance/performance_create_simple.cpp @@ -18,13 +18,13 @@ #include "cycle.hpp" typedef boost::coroutines::simple_stack_allocator stack_allocator; -typedef boost::coroutines::coroutine< void, stack_allocator > coroutine; +typedef boost::coroutines::coroutine< void, stack_allocator > coro_type; boost::coroutines::flag_fpu_t preserve_fpu = boost::coroutines::fpu_not_preserved; boost::coroutines::flag_unwind_t unwind_stack = boost::coroutines::stack_unwind; boost::uint64_t jobs = 1000; -void fn( coroutine::push_type & c) +void fn( coro_type::push_type & c) { while ( true) c(); } duration_type measure_time() @@ -32,11 +32,13 @@ duration_type measure_time() stack_allocator stack_alloc; // cache warum-up - coroutine::pull_type c( fn, boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); + coro_type::pull_type c( fn, + boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); time_point_type start( clock_type::now() ); for ( std::size_t i = 0; i < jobs; ++i) { - coroutine::pull_type c( fn, boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); + coro_type::pull_type c( fn, + boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); } duration_type total = clock_type::now() - start; total -= overhead_clock(); // overhead of measurement @@ -51,11 +53,13 @@ cycle_type measure_cycles() stack_allocator stack_alloc; // cache warum-up - coroutine::pull_type c( fn, boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); + coro_type::pull_type c( fn, + boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); cycle_type start( cycles() ); for ( std::size_t i = 0; i < jobs; ++i) { - coroutine::pull_type c( fn, boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); + coro_type::pull_type c( fn, + boost::coroutines::attributes( unwind_stack, preserve_fpu), stack_alloc); } cycle_type total = cycles() - start; total -= overhead_cycle(); // overhead of measurement @@ -76,7 +80,7 @@ int main( int argc, char * argv[]) desc.add_options() ("help", "help message") ("fpu,f", boost::program_options::value< bool >( & preserve), "preserve FPU registers") - ("unwind,u", boost::program_options::value< bool >( & unwind), "unwind stack") + ("unwind,u", boost::program_options::value< bool >( & unwind), "unwind coroutine-stack") ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run"); boost::program_options::variables_map vm; diff --git a/performance/performance_switch.cpp b/performance/performance_switch.cpp index 8a02134..92a2a9d 100644 --- a/performance/performance_switch.cpp +++ b/performance/performance_switch.cpp @@ -18,9 +18,6 @@ #include "clock.hpp" #include "cycle.hpp" -typedef boost::coroutines::protected_stack_allocator stack_allocator; -typedef boost::coroutines::coroutine< void, stack_allocator > coroutine; - boost::coroutines::flag_fpu_t preserve_fpu = boost::coroutines::fpu_not_preserved; boost::uint64_t jobs = 1000; diff --git a/performance/preallocated_stack_allocator.hpp b/performance/preallocated_stack_allocator.hpp index cf79e7b..d4c8796 100644 --- a/performance/preallocated_stack_allocator.hpp +++ b/performance/preallocated_stack_allocator.hpp @@ -14,89 +14,46 @@ #include #include +#include #include #include #include -#include - #ifdef BOOST_HAS_ABI_HEADERS # include BOOST_ABI_PREFIX #endif -template< std::size_t Default > class preallocated_stack_allocator { private: - class impl : private boost::noncopyable - { - private: - std::size_t pos_; - std::vector< void * > stacks_; - - public: - impl( std::size_t count) : - pos_( 0), - stacks_() - { - for ( int i = 0; i < count; ++i) - { - void * limit = std::calloc( preallocated_stack_allocator::default_stacksize(), sizeof( char) ); - if ( ! limit) throw std::bad_alloc(); - limit = static_cast< char * >( limit) + preallocated_stack_allocator::default_stacksize(); - stacks_.push_back( limit); - } - } - - ~impl() - { - for ( int i = 0; i < stacks_.size(); ++i) - { - void * limit = static_cast< char * >( stacks_[i]); - limit = static_cast< char * >( limit) - preallocated_stack_allocator::default_stacksize(); - std::free( limit); - } - } - - void allocate( boost::coroutines::stack_context & ctx, std::size_t size) - { - BOOST_ASSERT( stacks_.size() > pos_); - BOOST_ASSERT( preallocated_stack_allocator::minimum_stacksize() <= size); - BOOST_ASSERT( preallocated_stack_allocator::maximum_stacksize() >= size); - - ctx.size = preallocated_stack_allocator::default_stacksize(); - ctx.sp = stacks_[pos_++]; - } - - void deallocate( boost::coroutines::stack_context & ctx) - { - BOOST_ASSERT( ctx.sp); - BOOST_ASSERT( preallocated_stack_allocator::minimum_stacksize() <= ctx.size); - BOOST_ASSERT( preallocated_stack_allocator::maximum_stacksize() >= ctx.size); - } - }; - - boost::shared_ptr< impl > p_; + boost::coroutines::stack_context stack_ctx_; public: - static std::size_t maximum_stacksize() - { return Default; } - - static std::size_t default_stacksize() - { return Default; } - - static std::size_t minimum_stacksize() - { return Default; } - - preallocated_stack_allocator( std::size_t count = 1) : - p_( new impl( count) ) - {} + preallocated_stack_allocator() : + stack_ctx_() + { + boost::coroutines::protected_stack_allocator allocator; + allocator.allocate( stack_ctx_, default_stacksize() ); + } void allocate( boost::coroutines::stack_context & ctx, std::size_t size) - { p_->allocate( ctx, size); } + { + ctx.sp = stack_ctx_.sp; + ctx.size = stack_ctx_.size; + } void deallocate( boost::coroutines::stack_context & ctx) - { p_->deallocate( ctx); } + { + } + + static std::size_t maximum_stacksize() + { return boost::coroutines::protected_stack_allocator::maximum_stacksize(); } + + static std::size_t default_stacksize() + { return boost::coroutines::protected_stack_allocator::default_stacksize(); } + + static std::size_t minimum_stacksize() + { return boost::coroutines::protected_stack_allocator::minimum_stacksize(); } }; #ifdef BOOST_HAS_ABI_HEADERS