2
0
mirror of https://github.com/boostorg/timer.git synced 2026-01-19 04:42:13 +00:00

Remove dependency on Boost.System; use return value of ::times as suggested in #13

This commit is contained in:
Peter Dimov
2023-04-19 20:26:29 +03:00
parent cd7d42696b
commit 1a53ea1d8e
2 changed files with 59 additions and 68 deletions

View File

@@ -12,8 +12,6 @@
#include <boost/config.hpp>
#include <boost/system/api_config.hpp>
// This header implements separate compilation features as described in
// http://www.boost.org/more/separate_compilation.html

View File

@@ -17,20 +17,17 @@
#include <boost/chrono/chrono.hpp>
#include <boost/io/ios_state.hpp>
#include <boost/throw_exception.hpp>
#include <boost/cerrno.hpp>
#include <boost/predef.h>
#include <cstring>
#include <sstream>
#include <cassert>
# if defined(BOOST_WINDOWS_API)
# include <windows.h>
# elif defined(BOOST_POSIX_API)
# include <unistd.h>
# include <sys/times.h>
# else
# error unknown API
# endif
#if defined(_WIN32)
# include <windows.h>
#else
# include <unistd.h>
# include <sys/times.h>
#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<boost::int64_t, boost::nano>
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<boost::int64_t, boost::nano>
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)&current.system, (LPFILETIME)&current.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<clock_t>(-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<clock_t>( -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