From 1a53ea1d8ec0e4eeeda9d2f883ed057adba8a392 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 19 Apr 2023 20:26:29 +0300 Subject: [PATCH] Remove dependency on Boost.System; use return value of ::times as suggested in #13 --- include/boost/timer/config.hpp | 2 - src/cpu_timer.cpp | 125 ++++++++++++++++----------------- 2 files changed, 59 insertions(+), 68 deletions(-) diff --git a/include/boost/timer/config.hpp b/include/boost/timer/config.hpp index a7c3d9d..b13c4d9 100644 --- a/include/boost/timer/config.hpp +++ b/include/boost/timer/config.hpp @@ -12,8 +12,6 @@ #include -#include - // This header implements separate compilation features as described in // http://www.boost.org/more/separate_compilation.html diff --git a/src/cpu_timer.cpp b/src/cpu_timer.cpp index 36b2ac4..22edec6 100644 --- a/src/cpu_timer.cpp +++ b/src/cpu_timer.cpp @@ -17,20 +17,17 @@ #include #include #include -#include #include #include #include #include -# if defined(BOOST_WINDOWS_API) -# include -# elif defined(BOOST_POSIX_API) -# include -# include -# else -# error unknown API -# endif +#if defined(_WIN32) +# include +#else +# include +# include +#endif using boost::timer::nanosecond_type; using boost::timer::cpu_times; @@ -93,76 +90,72 @@ namespace } } -# if defined(BOOST_POSIX_API) +#if defined(_WIN32) - boost::int_least64_t tick_factor_() - { +void get_cpu_times( boost::timer::cpu_times& current ) +{ + boost::chrono::duration + x( boost::chrono::high_resolution_clock::now().time_since_epoch() ); + + current.wall = x.count(); + current.user = boost::timer::nanosecond_type( -1 ); + current.system = boost::timer::nanosecond_type( -1 ); + +#if BOOST_PLAT_WINDOWS_DESKTOP + + FILETIME creation, exit, kernel, user; + + if( !::GetProcessTimes( ::GetCurrentProcess(), &creation, &exit, &kernel, &user ) ) + { + return; + } + + // Windows uses 100 nanosecond ticks + + current.system = ( ( boost::timer::nanosecond_type( kernel.dwHighDateTime ) << 32 ) + kernel.dwLowDateTime ) * 100; + current.user = ( ( boost::timer::nanosecond_type( user.dwHighDateTime ) << 32 ) + user.dwLowDateTime ) * 100; + +#endif +} + +#else + +// multiplier to convert ticks to nanoseconds; -1 if unknown +boost::int_least64_t tick_factor() +{ boost::int_least64_t tf = ::sysconf( _SC_CLK_TCK ); if( tf <= 0 ) return -1; - tf = INT64_C(1000000000) / tf; // compute factor + tf = INT64_C( 1000000000 ) / tf; // compute factor if( tf == 0 ) tf = -1; return tf; - } +} - boost::int_least64_t tick_factor() // multiplier to convert ticks - // to nanoseconds; -1 if unknown - { - static boost::int_least64_t tf = tick_factor_(); - return tf; - } +void get_cpu_times( boost::timer::cpu_times& current ) +{ + current.wall = boost::timer::nanosecond_type( -1 ); + current.user = boost::timer::nanosecond_type( -1 ); + current.system = boost::timer::nanosecond_type( -1 ); -# endif + static boost::int_least64_t tf = tick_factor(); - void get_cpu_times(boost::timer::cpu_times& current) - { - boost::chrono::duration - x (boost::chrono::high_resolution_clock::now().time_since_epoch()); - current.wall = x.count(); + if( tf == -1 ) return; -# if defined(BOOST_WINDOWS_API) - -# if BOOST_PLAT_WINDOWS_DESKTOP || defined(__CYGWIN__) - FILETIME creation, exit; - if (::GetProcessTimes(::GetCurrentProcess(), &creation, &exit, - (LPFILETIME)¤t.system, (LPFILETIME)¤t.user)) - { - current.user *= 100; // Windows uses 100 nanosecond ticks - current.system *= 100; - } - else -# endif - { - current.system = current.user = boost::timer::nanosecond_type(-1); - } -# else tms tm; - clock_t c = ::times(&tm); - if (c == static_cast(-1)) // error - { - current.system = current.user = boost::timer::nanosecond_type(-1); - } - else - { - current.system = boost::timer::nanosecond_type(tm.tms_stime + tm.tms_cstime); - current.user = boost::timer::nanosecond_type(tm.tms_utime + tm.tms_cutime); - boost::int_least64_t factor; - if ((factor = tick_factor()) != -1) - { - current.user *= factor; - current.system *= factor; - } - else - { - current.user = current.system = boost::timer::nanosecond_type(-1); - } - } -# endif - } + clock_t c = ::times( &tm ); - // CAUTION: must be identical to same constant in auto_timers_construction.cpp - const std::string default_fmt(" %ws wall, %us user + %ss system = %ts CPU (%p%)\n"); + if( c == static_cast( -1 ) ) return; + + current.wall = boost::timer::nanosecond_type( c ) * tf; + current.system = boost::timer::nanosecond_type( tm.tms_stime + tm.tms_cstime ) * tf; + current.user = boost::timer::nanosecond_type( tm.tms_utime + tm.tms_cutime ) * tf; +} + +#endif + +// CAUTION: must be identical to same constant in auto_timers_construction.cpp +const std::string default_fmt(" %ws wall, %us user + %ss system = %ts CPU (%p%)\n"); } // unnamed namespace